rs6000: Merge the var_shift yes/no alternatives
[official-gcc.git] / gcc / cfgexpand.c
blobe8cd87f1c47eab64010621ba2a23c26bc2fa1be3
1 /* A pass for lowering trees to RTL.
2 Copyright (C) 2004-2014 Free Software Foundation, Inc.
4 This file is part of GCC.
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.
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.
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/>. */
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "tm.h"
24 #include "rtl.h"
25 #include "hard-reg-set.h"
26 #include "tree.h"
27 #include "stringpool.h"
28 #include "varasm.h"
29 #include "stor-layout.h"
30 #include "stmt.h"
31 #include "print-tree.h"
32 #include "tm_p.h"
33 #include "basic-block.h"
34 #include "function.h"
35 #include "expr.h"
36 #include "langhooks.h"
37 #include "bitmap.h"
38 #include "pointer-set.h"
39 #include "tree-ssa-alias.h"
40 #include "internal-fn.h"
41 #include "tree-eh.h"
42 #include "gimple-expr.h"
43 #include "is-a.h"
44 #include "gimple.h"
45 #include "gimple-iterator.h"
46 #include "gimple-walk.h"
47 #include "gimple-ssa.h"
48 #include "cgraph.h"
49 #include "tree-cfg.h"
50 #include "tree-phinodes.h"
51 #include "ssa-iterators.h"
52 #include "tree-ssanames.h"
53 #include "tree-dfa.h"
54 #include "tree-ssa.h"
55 #include "tree-pass.h"
56 #include "except.h"
57 #include "flags.h"
58 #include "diagnostic.h"
59 #include "gimple-pretty-print.h"
60 #include "toplev.h"
61 #include "debug.h"
62 #include "params.h"
63 #include "tree-inline.h"
64 #include "value-prof.h"
65 #include "target.h"
66 #include "tree-ssa-live.h"
67 #include "tree-outof-ssa.h"
68 #include "sbitmap.h"
69 #include "cfgloop.h"
70 #include "regs.h" /* For reg_renumber. */
71 #include "insn-attr.h" /* For INSN_SCHEDULING. */
72 #include "asan.h"
73 #include "tree-ssa-address.h"
74 #include "recog.h"
75 #include "output.h"
76 #include "builtins.h"
78 /* Some systems use __main in a way incompatible with its use in gcc, in these
79 cases use the macros NAME__MAIN to give a quoted symbol and SYMBOL__MAIN to
80 give the same symbol without quotes for an alternative entry point. You
81 must define both, or neither. */
82 #ifndef NAME__MAIN
83 #define NAME__MAIN "__main"
84 #endif
86 /* This variable holds information helping the rewriting of SSA trees
87 into RTL. */
88 struct ssaexpand SA;
90 /* This variable holds the currently expanded gimple statement for purposes
91 of comminucating the profile info to the builtin expanders. */
92 gimple currently_expanding_gimple_stmt;
94 static rtx expand_debug_expr (tree);
96 /* Return an expression tree corresponding to the RHS of GIMPLE
97 statement STMT. */
99 tree
100 gimple_assign_rhs_to_tree (gimple stmt)
102 tree t;
103 enum gimple_rhs_class grhs_class;
105 grhs_class = get_gimple_rhs_class (gimple_expr_code (stmt));
107 if (grhs_class == GIMPLE_TERNARY_RHS)
108 t = build3 (gimple_assign_rhs_code (stmt),
109 TREE_TYPE (gimple_assign_lhs (stmt)),
110 gimple_assign_rhs1 (stmt),
111 gimple_assign_rhs2 (stmt),
112 gimple_assign_rhs3 (stmt));
113 else if (grhs_class == GIMPLE_BINARY_RHS)
114 t = build2 (gimple_assign_rhs_code (stmt),
115 TREE_TYPE (gimple_assign_lhs (stmt)),
116 gimple_assign_rhs1 (stmt),
117 gimple_assign_rhs2 (stmt));
118 else if (grhs_class == GIMPLE_UNARY_RHS)
119 t = build1 (gimple_assign_rhs_code (stmt),
120 TREE_TYPE (gimple_assign_lhs (stmt)),
121 gimple_assign_rhs1 (stmt));
122 else if (grhs_class == GIMPLE_SINGLE_RHS)
124 t = gimple_assign_rhs1 (stmt);
125 /* Avoid modifying this tree in place below. */
126 if ((gimple_has_location (stmt) && CAN_HAVE_LOCATION_P (t)
127 && gimple_location (stmt) != EXPR_LOCATION (t))
128 || (gimple_block (stmt)
129 && currently_expanding_to_rtl
130 && EXPR_P (t)))
131 t = copy_node (t);
133 else
134 gcc_unreachable ();
136 if (gimple_has_location (stmt) && CAN_HAVE_LOCATION_P (t))
137 SET_EXPR_LOCATION (t, gimple_location (stmt));
139 return t;
143 #ifndef STACK_ALIGNMENT_NEEDED
144 #define STACK_ALIGNMENT_NEEDED 1
145 #endif
147 #define SSAVAR(x) (TREE_CODE (x) == SSA_NAME ? SSA_NAME_VAR (x) : x)
149 /* Associate declaration T with storage space X. If T is no
150 SSA name this is exactly SET_DECL_RTL, otherwise make the
151 partition of T associated with X. */
152 static inline void
153 set_rtl (tree t, rtx x)
155 if (TREE_CODE (t) == SSA_NAME)
157 SA.partition_to_pseudo[var_to_partition (SA.map, t)] = x;
158 if (x && !MEM_P (x))
159 set_reg_attrs_for_decl_rtl (SSA_NAME_VAR (t), x);
160 /* For the benefit of debug information at -O0 (where vartracking
161 doesn't run) record the place also in the base DECL if it's
162 a normal variable (not a parameter). */
163 if (x && x != pc_rtx && TREE_CODE (SSA_NAME_VAR (t)) == VAR_DECL)
165 tree var = SSA_NAME_VAR (t);
166 /* If we don't yet have something recorded, just record it now. */
167 if (!DECL_RTL_SET_P (var))
168 SET_DECL_RTL (var, x);
169 /* If we have it set already to "multiple places" don't
170 change this. */
171 else if (DECL_RTL (var) == pc_rtx)
173 /* If we have something recorded and it's not the same place
174 as we want to record now, we have multiple partitions for the
175 same base variable, with different places. We can't just
176 randomly chose one, hence we have to say that we don't know.
177 This only happens with optimization, and there var-tracking
178 will figure out the right thing. */
179 else if (DECL_RTL (var) != x)
180 SET_DECL_RTL (var, pc_rtx);
183 else
184 SET_DECL_RTL (t, x);
187 /* This structure holds data relevant to one variable that will be
188 placed in a stack slot. */
189 struct stack_var
191 /* The Variable. */
192 tree decl;
194 /* Initially, the size of the variable. Later, the size of the partition,
195 if this variable becomes it's partition's representative. */
196 HOST_WIDE_INT size;
198 /* The *byte* alignment required for this variable. Or as, with the
199 size, the alignment for this partition. */
200 unsigned int alignb;
202 /* The partition representative. */
203 size_t representative;
205 /* The next stack variable in the partition, or EOC. */
206 size_t next;
208 /* The numbers of conflicting stack variables. */
209 bitmap conflicts;
212 #define EOC ((size_t)-1)
214 /* We have an array of such objects while deciding allocation. */
215 static struct stack_var *stack_vars;
216 static size_t stack_vars_alloc;
217 static size_t stack_vars_num;
218 static struct pointer_map_t *decl_to_stack_part;
220 /* Conflict bitmaps go on this obstack. This allows us to destroy
221 all of them in one big sweep. */
222 static bitmap_obstack stack_var_bitmap_obstack;
224 /* An array of indices such that stack_vars[stack_vars_sorted[i]].size
225 is non-decreasing. */
226 static size_t *stack_vars_sorted;
228 /* The phase of the stack frame. This is the known misalignment of
229 virtual_stack_vars_rtx from PREFERRED_STACK_BOUNDARY. That is,
230 (frame_offset+frame_phase) % PREFERRED_STACK_BOUNDARY == 0. */
231 static int frame_phase;
233 /* Used during expand_used_vars to remember if we saw any decls for
234 which we'd like to enable stack smashing protection. */
235 static bool has_protected_decls;
237 /* Used during expand_used_vars. Remember if we say a character buffer
238 smaller than our cutoff threshold. Used for -Wstack-protector. */
239 static bool has_short_buffer;
241 /* Compute the byte alignment to use for DECL. Ignore alignment
242 we can't do with expected alignment of the stack boundary. */
244 static unsigned int
245 align_local_variable (tree decl)
247 unsigned int align = LOCAL_DECL_ALIGNMENT (decl);
248 DECL_ALIGN (decl) = align;
249 return align / BITS_PER_UNIT;
252 /* Allocate SIZE bytes at byte alignment ALIGN from the stack frame.
253 Return the frame offset. */
255 static HOST_WIDE_INT
256 alloc_stack_frame_space (HOST_WIDE_INT size, unsigned HOST_WIDE_INT align)
258 HOST_WIDE_INT offset, new_frame_offset;
260 new_frame_offset = frame_offset;
261 if (FRAME_GROWS_DOWNWARD)
263 new_frame_offset -= size + frame_phase;
264 new_frame_offset &= -align;
265 new_frame_offset += frame_phase;
266 offset = new_frame_offset;
268 else
270 new_frame_offset -= frame_phase;
271 new_frame_offset += align - 1;
272 new_frame_offset &= -align;
273 new_frame_offset += frame_phase;
274 offset = new_frame_offset;
275 new_frame_offset += size;
277 frame_offset = new_frame_offset;
279 if (frame_offset_overflow (frame_offset, cfun->decl))
280 frame_offset = offset = 0;
282 return offset;
285 /* Accumulate DECL into STACK_VARS. */
287 static void
288 add_stack_var (tree decl)
290 struct stack_var *v;
292 if (stack_vars_num >= stack_vars_alloc)
294 if (stack_vars_alloc)
295 stack_vars_alloc = stack_vars_alloc * 3 / 2;
296 else
297 stack_vars_alloc = 32;
298 stack_vars
299 = XRESIZEVEC (struct stack_var, stack_vars, stack_vars_alloc);
301 if (!decl_to_stack_part)
302 decl_to_stack_part = pointer_map_create ();
304 v = &stack_vars[stack_vars_num];
305 * (size_t *)pointer_map_insert (decl_to_stack_part, decl) = stack_vars_num;
307 v->decl = decl;
308 v->size = tree_to_uhwi (DECL_SIZE_UNIT (SSAVAR (decl)));
309 /* Ensure that all variables have size, so that &a != &b for any two
310 variables that are simultaneously live. */
311 if (v->size == 0)
312 v->size = 1;
313 v->alignb = align_local_variable (SSAVAR (decl));
314 /* An alignment of zero can mightily confuse us later. */
315 gcc_assert (v->alignb != 0);
317 /* All variables are initially in their own partition. */
318 v->representative = stack_vars_num;
319 v->next = EOC;
321 /* All variables initially conflict with no other. */
322 v->conflicts = NULL;
324 /* Ensure that this decl doesn't get put onto the list twice. */
325 set_rtl (decl, pc_rtx);
327 stack_vars_num++;
330 /* Make the decls associated with luid's X and Y conflict. */
332 static void
333 add_stack_var_conflict (size_t x, size_t y)
335 struct stack_var *a = &stack_vars[x];
336 struct stack_var *b = &stack_vars[y];
337 if (!a->conflicts)
338 a->conflicts = BITMAP_ALLOC (&stack_var_bitmap_obstack);
339 if (!b->conflicts)
340 b->conflicts = BITMAP_ALLOC (&stack_var_bitmap_obstack);
341 bitmap_set_bit (a->conflicts, y);
342 bitmap_set_bit (b->conflicts, x);
345 /* Check whether the decls associated with luid's X and Y conflict. */
347 static bool
348 stack_var_conflict_p (size_t x, size_t y)
350 struct stack_var *a = &stack_vars[x];
351 struct stack_var *b = &stack_vars[y];
352 if (x == y)
353 return false;
354 /* Partitions containing an SSA name result from gimple registers
355 with things like unsupported modes. They are top-level and
356 hence conflict with everything else. */
357 if (TREE_CODE (a->decl) == SSA_NAME || TREE_CODE (b->decl) == SSA_NAME)
358 return true;
360 if (!a->conflicts || !b->conflicts)
361 return false;
362 return bitmap_bit_p (a->conflicts, y);
365 /* Callback for walk_stmt_ops. If OP is a decl touched by add_stack_var
366 enter its partition number into bitmap DATA. */
368 static bool
369 visit_op (gimple, tree op, tree, void *data)
371 bitmap active = (bitmap)data;
372 op = get_base_address (op);
373 if (op
374 && DECL_P (op)
375 && DECL_RTL_IF_SET (op) == pc_rtx)
377 size_t *v = (size_t *) pointer_map_contains (decl_to_stack_part, op);
378 if (v)
379 bitmap_set_bit (active, *v);
381 return false;
384 /* Callback for walk_stmt_ops. If OP is a decl touched by add_stack_var
385 record conflicts between it and all currently active other partitions
386 from bitmap DATA. */
388 static bool
389 visit_conflict (gimple, tree op, tree, void *data)
391 bitmap active = (bitmap)data;
392 op = get_base_address (op);
393 if (op
394 && DECL_P (op)
395 && DECL_RTL_IF_SET (op) == pc_rtx)
397 size_t *v =
398 (size_t *) pointer_map_contains (decl_to_stack_part, op);
399 if (v && bitmap_set_bit (active, *v))
401 size_t num = *v;
402 bitmap_iterator bi;
403 unsigned i;
404 gcc_assert (num < stack_vars_num);
405 EXECUTE_IF_SET_IN_BITMAP (active, 0, i, bi)
406 add_stack_var_conflict (num, i);
409 return false;
412 /* Helper routine for add_scope_conflicts, calculating the active partitions
413 at the end of BB, leaving the result in WORK. We're called to generate
414 conflicts when FOR_CONFLICT is true, otherwise we're just tracking
415 liveness. */
417 static void
418 add_scope_conflicts_1 (basic_block bb, bitmap work, bool for_conflict)
420 edge e;
421 edge_iterator ei;
422 gimple_stmt_iterator gsi;
423 walk_stmt_load_store_addr_fn visit;
425 bitmap_clear (work);
426 FOR_EACH_EDGE (e, ei, bb->preds)
427 bitmap_ior_into (work, (bitmap)e->src->aux);
429 visit = visit_op;
431 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
433 gimple stmt = gsi_stmt (gsi);
434 walk_stmt_load_store_addr_ops (stmt, work, NULL, NULL, visit);
436 for (gsi = gsi_after_labels (bb); !gsi_end_p (gsi); gsi_next (&gsi))
438 gimple stmt = gsi_stmt (gsi);
440 if (gimple_clobber_p (stmt))
442 tree lhs = gimple_assign_lhs (stmt);
443 size_t *v;
444 /* Nested function lowering might introduce LHSs
445 that are COMPONENT_REFs. */
446 if (TREE_CODE (lhs) != VAR_DECL)
447 continue;
448 if (DECL_RTL_IF_SET (lhs) == pc_rtx
449 && (v = (size_t *)
450 pointer_map_contains (decl_to_stack_part, lhs)))
451 bitmap_clear_bit (work, *v);
453 else if (!is_gimple_debug (stmt))
455 if (for_conflict
456 && visit == visit_op)
458 /* If this is the first real instruction in this BB we need
459 to add conflicts for everything live at this point now.
460 Unlike classical liveness for named objects we can't
461 rely on seeing a def/use of the names we're interested in.
462 There might merely be indirect loads/stores. We'd not add any
463 conflicts for such partitions. */
464 bitmap_iterator bi;
465 unsigned i;
466 EXECUTE_IF_SET_IN_BITMAP (work, 0, i, bi)
468 struct stack_var *a = &stack_vars[i];
469 if (!a->conflicts)
470 a->conflicts = BITMAP_ALLOC (&stack_var_bitmap_obstack);
471 bitmap_ior_into (a->conflicts, work);
473 visit = visit_conflict;
475 walk_stmt_load_store_addr_ops (stmt, work, visit, visit, visit);
480 /* Generate stack partition conflicts between all partitions that are
481 simultaneously live. */
483 static void
484 add_scope_conflicts (void)
486 basic_block bb;
487 bool changed;
488 bitmap work = BITMAP_ALLOC (NULL);
489 int *rpo;
490 int n_bbs;
492 /* We approximate the live range of a stack variable by taking the first
493 mention of its name as starting point(s), and by the end-of-scope
494 death clobber added by gimplify as ending point(s) of the range.
495 This overapproximates in the case we for instance moved an address-taken
496 operation upward, without also moving a dereference to it upwards.
497 But it's conservatively correct as a variable never can hold values
498 before its name is mentioned at least once.
500 We then do a mostly classical bitmap liveness algorithm. */
502 FOR_ALL_BB_FN (bb, cfun)
503 bb->aux = BITMAP_ALLOC (&stack_var_bitmap_obstack);
505 rpo = XNEWVEC (int, last_basic_block_for_fn (cfun));
506 n_bbs = pre_and_rev_post_order_compute (NULL, rpo, false);
508 changed = true;
509 while (changed)
511 int i;
512 changed = false;
513 for (i = 0; i < n_bbs; i++)
515 bitmap active;
516 bb = BASIC_BLOCK_FOR_FN (cfun, rpo[i]);
517 active = (bitmap)bb->aux;
518 add_scope_conflicts_1 (bb, work, false);
519 if (bitmap_ior_into (active, work))
520 changed = true;
524 FOR_EACH_BB_FN (bb, cfun)
525 add_scope_conflicts_1 (bb, work, true);
527 free (rpo);
528 BITMAP_FREE (work);
529 FOR_ALL_BB_FN (bb, cfun)
530 BITMAP_FREE (bb->aux);
533 /* A subroutine of partition_stack_vars. A comparison function for qsort,
534 sorting an array of indices by the properties of the object. */
536 static int
537 stack_var_cmp (const void *a, const void *b)
539 size_t ia = *(const size_t *)a;
540 size_t ib = *(const size_t *)b;
541 unsigned int aligna = stack_vars[ia].alignb;
542 unsigned int alignb = stack_vars[ib].alignb;
543 HOST_WIDE_INT sizea = stack_vars[ia].size;
544 HOST_WIDE_INT sizeb = stack_vars[ib].size;
545 tree decla = stack_vars[ia].decl;
546 tree declb = stack_vars[ib].decl;
547 bool largea, largeb;
548 unsigned int uida, uidb;
550 /* Primary compare on "large" alignment. Large comes first. */
551 largea = (aligna * BITS_PER_UNIT > MAX_SUPPORTED_STACK_ALIGNMENT);
552 largeb = (alignb * BITS_PER_UNIT > MAX_SUPPORTED_STACK_ALIGNMENT);
553 if (largea != largeb)
554 return (int)largeb - (int)largea;
556 /* Secondary compare on size, decreasing */
557 if (sizea > sizeb)
558 return -1;
559 if (sizea < sizeb)
560 return 1;
562 /* Tertiary compare on true alignment, decreasing. */
563 if (aligna < alignb)
564 return -1;
565 if (aligna > alignb)
566 return 1;
568 /* Final compare on ID for sort stability, increasing.
569 Two SSA names are compared by their version, SSA names come before
570 non-SSA names, and two normal decls are compared by their DECL_UID. */
571 if (TREE_CODE (decla) == SSA_NAME)
573 if (TREE_CODE (declb) == SSA_NAME)
574 uida = SSA_NAME_VERSION (decla), uidb = SSA_NAME_VERSION (declb);
575 else
576 return -1;
578 else if (TREE_CODE (declb) == SSA_NAME)
579 return 1;
580 else
581 uida = DECL_UID (decla), uidb = DECL_UID (declb);
582 if (uida < uidb)
583 return 1;
584 if (uida > uidb)
585 return -1;
586 return 0;
590 /* If the points-to solution *PI points to variables that are in a partition
591 together with other variables add all partition members to the pointed-to
592 variables bitmap. */
594 static void
595 add_partitioned_vars_to_ptset (struct pt_solution *pt,
596 struct pointer_map_t *decls_to_partitions,
597 struct pointer_set_t *visited, bitmap temp)
599 bitmap_iterator bi;
600 unsigned i;
601 bitmap *part;
603 if (pt->anything
604 || pt->vars == NULL
605 /* The pointed-to vars bitmap is shared, it is enough to
606 visit it once. */
607 || pointer_set_insert (visited, pt->vars))
608 return;
610 bitmap_clear (temp);
612 /* By using a temporary bitmap to store all members of the partitions
613 we have to add we make sure to visit each of the partitions only
614 once. */
615 EXECUTE_IF_SET_IN_BITMAP (pt->vars, 0, i, bi)
616 if ((!temp
617 || !bitmap_bit_p (temp, i))
618 && (part = (bitmap *) pointer_map_contains (decls_to_partitions,
619 (void *)(size_t) i)))
620 bitmap_ior_into (temp, *part);
621 if (!bitmap_empty_p (temp))
622 bitmap_ior_into (pt->vars, temp);
625 /* Update points-to sets based on partition info, so we can use them on RTL.
626 The bitmaps representing stack partitions will be saved until expand,
627 where partitioned decls used as bases in memory expressions will be
628 rewritten. */
630 static void
631 update_alias_info_with_stack_vars (void)
633 struct pointer_map_t *decls_to_partitions = NULL;
634 size_t i, j;
635 tree var = NULL_TREE;
637 for (i = 0; i < stack_vars_num; i++)
639 bitmap part = NULL;
640 tree name;
641 struct ptr_info_def *pi;
643 /* Not interested in partitions with single variable. */
644 if (stack_vars[i].representative != i
645 || stack_vars[i].next == EOC)
646 continue;
648 if (!decls_to_partitions)
650 decls_to_partitions = pointer_map_create ();
651 cfun->gimple_df->decls_to_pointers = pointer_map_create ();
654 /* Create an SSA_NAME that points to the partition for use
655 as base during alias-oracle queries on RTL for bases that
656 have been partitioned. */
657 if (var == NULL_TREE)
658 var = create_tmp_var (ptr_type_node, NULL);
659 name = make_ssa_name (var, NULL);
661 /* Create bitmaps representing partitions. They will be used for
662 points-to sets later, so use GGC alloc. */
663 part = BITMAP_GGC_ALLOC ();
664 for (j = i; j != EOC; j = stack_vars[j].next)
666 tree decl = stack_vars[j].decl;
667 unsigned int uid = DECL_PT_UID (decl);
668 bitmap_set_bit (part, uid);
669 *((bitmap *) pointer_map_insert (decls_to_partitions,
670 (void *)(size_t) uid)) = part;
671 *((tree *) pointer_map_insert (cfun->gimple_df->decls_to_pointers,
672 decl)) = name;
673 if (TREE_ADDRESSABLE (decl))
674 TREE_ADDRESSABLE (name) = 1;
677 /* Make the SSA name point to all partition members. */
678 pi = get_ptr_info (name);
679 pt_solution_set (&pi->pt, part, false);
682 /* Make all points-to sets that contain one member of a partition
683 contain all members of the partition. */
684 if (decls_to_partitions)
686 unsigned i;
687 struct pointer_set_t *visited = pointer_set_create ();
688 bitmap temp = BITMAP_ALLOC (&stack_var_bitmap_obstack);
690 for (i = 1; i < num_ssa_names; i++)
692 tree name = ssa_name (i);
693 struct ptr_info_def *pi;
695 if (name
696 && POINTER_TYPE_P (TREE_TYPE (name))
697 && ((pi = SSA_NAME_PTR_INFO (name)) != NULL))
698 add_partitioned_vars_to_ptset (&pi->pt, decls_to_partitions,
699 visited, temp);
702 add_partitioned_vars_to_ptset (&cfun->gimple_df->escaped,
703 decls_to_partitions, visited, temp);
705 pointer_set_destroy (visited);
706 pointer_map_destroy (decls_to_partitions);
707 BITMAP_FREE (temp);
711 /* A subroutine of partition_stack_vars. The UNION portion of a UNION/FIND
712 partitioning algorithm. Partitions A and B are known to be non-conflicting.
713 Merge them into a single partition A. */
715 static void
716 union_stack_vars (size_t a, size_t b)
718 struct stack_var *vb = &stack_vars[b];
719 bitmap_iterator bi;
720 unsigned u;
722 gcc_assert (stack_vars[b].next == EOC);
723 /* Add B to A's partition. */
724 stack_vars[b].next = stack_vars[a].next;
725 stack_vars[b].representative = a;
726 stack_vars[a].next = b;
728 /* Update the required alignment of partition A to account for B. */
729 if (stack_vars[a].alignb < stack_vars[b].alignb)
730 stack_vars[a].alignb = stack_vars[b].alignb;
732 /* Update the interference graph and merge the conflicts. */
733 if (vb->conflicts)
735 EXECUTE_IF_SET_IN_BITMAP (vb->conflicts, 0, u, bi)
736 add_stack_var_conflict (a, stack_vars[u].representative);
737 BITMAP_FREE (vb->conflicts);
741 /* A subroutine of expand_used_vars. Binpack the variables into
742 partitions constrained by the interference graph. The overall
743 algorithm used is as follows:
745 Sort the objects by size in descending order.
746 For each object A {
747 S = size(A)
748 O = 0
749 loop {
750 Look for the largest non-conflicting object B with size <= S.
751 UNION (A, B)
756 static void
757 partition_stack_vars (void)
759 size_t si, sj, n = stack_vars_num;
761 stack_vars_sorted = XNEWVEC (size_t, stack_vars_num);
762 for (si = 0; si < n; ++si)
763 stack_vars_sorted[si] = si;
765 if (n == 1)
766 return;
768 qsort (stack_vars_sorted, n, sizeof (size_t), stack_var_cmp);
770 for (si = 0; si < n; ++si)
772 size_t i = stack_vars_sorted[si];
773 unsigned int ialign = stack_vars[i].alignb;
774 HOST_WIDE_INT isize = stack_vars[i].size;
776 /* Ignore objects that aren't partition representatives. If we
777 see a var that is not a partition representative, it must
778 have been merged earlier. */
779 if (stack_vars[i].representative != i)
780 continue;
782 for (sj = si + 1; sj < n; ++sj)
784 size_t j = stack_vars_sorted[sj];
785 unsigned int jalign = stack_vars[j].alignb;
786 HOST_WIDE_INT jsize = stack_vars[j].size;
788 /* Ignore objects that aren't partition representatives. */
789 if (stack_vars[j].representative != j)
790 continue;
792 /* Do not mix objects of "small" (supported) alignment
793 and "large" (unsupported) alignment. */
794 if ((ialign * BITS_PER_UNIT <= MAX_SUPPORTED_STACK_ALIGNMENT)
795 != (jalign * BITS_PER_UNIT <= MAX_SUPPORTED_STACK_ALIGNMENT))
796 break;
798 /* For Address Sanitizer do not mix objects with different
799 sizes, as the shorter vars wouldn't be adequately protected.
800 Don't do that for "large" (unsupported) alignment objects,
801 those aren't protected anyway. */
802 if ((flag_sanitize & SANITIZE_ADDRESS) && ASAN_STACK && isize != jsize
803 && ialign * BITS_PER_UNIT <= MAX_SUPPORTED_STACK_ALIGNMENT)
804 break;
806 /* Ignore conflicting objects. */
807 if (stack_var_conflict_p (i, j))
808 continue;
810 /* UNION the objects, placing J at OFFSET. */
811 union_stack_vars (i, j);
815 update_alias_info_with_stack_vars ();
818 /* A debugging aid for expand_used_vars. Dump the generated partitions. */
820 static void
821 dump_stack_var_partition (void)
823 size_t si, i, j, n = stack_vars_num;
825 for (si = 0; si < n; ++si)
827 i = stack_vars_sorted[si];
829 /* Skip variables that aren't partition representatives, for now. */
830 if (stack_vars[i].representative != i)
831 continue;
833 fprintf (dump_file, "Partition %lu: size " HOST_WIDE_INT_PRINT_DEC
834 " align %u\n", (unsigned long) i, stack_vars[i].size,
835 stack_vars[i].alignb);
837 for (j = i; j != EOC; j = stack_vars[j].next)
839 fputc ('\t', dump_file);
840 print_generic_expr (dump_file, stack_vars[j].decl, dump_flags);
842 fputc ('\n', dump_file);
846 /* Assign rtl to DECL at BASE + OFFSET. */
848 static void
849 expand_one_stack_var_at (tree decl, rtx base, unsigned base_align,
850 HOST_WIDE_INT offset)
852 unsigned align;
853 rtx x;
855 /* If this fails, we've overflowed the stack frame. Error nicely? */
856 gcc_assert (offset == trunc_int_for_mode (offset, Pmode));
858 x = plus_constant (Pmode, base, offset);
859 x = gen_rtx_MEM (DECL_MODE (SSAVAR (decl)), x);
861 if (TREE_CODE (decl) != SSA_NAME)
863 /* Set alignment we actually gave this decl if it isn't an SSA name.
864 If it is we generate stack slots only accidentally so it isn't as
865 important, we'll simply use the alignment that is already set. */
866 if (base == virtual_stack_vars_rtx)
867 offset -= frame_phase;
868 align = offset & -offset;
869 align *= BITS_PER_UNIT;
870 if (align == 0 || align > base_align)
871 align = base_align;
873 /* One would think that we could assert that we're not decreasing
874 alignment here, but (at least) the i386 port does exactly this
875 via the MINIMUM_ALIGNMENT hook. */
877 DECL_ALIGN (decl) = align;
878 DECL_USER_ALIGN (decl) = 0;
881 set_mem_attributes (x, SSAVAR (decl), true);
882 set_rtl (decl, x);
885 struct stack_vars_data
887 /* Vector of offset pairs, always end of some padding followed
888 by start of the padding that needs Address Sanitizer protection.
889 The vector is in reversed, highest offset pairs come first. */
890 vec<HOST_WIDE_INT> asan_vec;
892 /* Vector of partition representative decls in between the paddings. */
893 vec<tree> asan_decl_vec;
895 /* Base pseudo register for Address Sanitizer protected automatic vars. */
896 rtx asan_base;
898 /* Alignment needed for the Address Sanitizer protected automatic vars. */
899 unsigned int asan_alignb;
902 /* A subroutine of expand_used_vars. Give each partition representative
903 a unique location within the stack frame. Update each partition member
904 with that location. */
906 static void
907 expand_stack_vars (bool (*pred) (size_t), struct stack_vars_data *data)
909 size_t si, i, j, n = stack_vars_num;
910 HOST_WIDE_INT large_size = 0, large_alloc = 0;
911 rtx large_base = NULL;
912 unsigned large_align = 0;
913 tree decl;
915 /* Determine if there are any variables requiring "large" alignment.
916 Since these are dynamically allocated, we only process these if
917 no predicate involved. */
918 large_align = stack_vars[stack_vars_sorted[0]].alignb * BITS_PER_UNIT;
919 if (pred == NULL && large_align > MAX_SUPPORTED_STACK_ALIGNMENT)
921 /* Find the total size of these variables. */
922 for (si = 0; si < n; ++si)
924 unsigned alignb;
926 i = stack_vars_sorted[si];
927 alignb = stack_vars[i].alignb;
929 /* Stop when we get to the first decl with "small" alignment. */
930 if (alignb * BITS_PER_UNIT <= MAX_SUPPORTED_STACK_ALIGNMENT)
931 break;
933 /* Skip variables that aren't partition representatives. */
934 if (stack_vars[i].representative != i)
935 continue;
937 /* Skip variables that have already had rtl assigned. See also
938 add_stack_var where we perpetrate this pc_rtx hack. */
939 decl = stack_vars[i].decl;
940 if ((TREE_CODE (decl) == SSA_NAME
941 ? SA.partition_to_pseudo[var_to_partition (SA.map, decl)]
942 : DECL_RTL (decl)) != pc_rtx)
943 continue;
945 large_size += alignb - 1;
946 large_size &= -(HOST_WIDE_INT)alignb;
947 large_size += stack_vars[i].size;
950 /* If there were any, allocate space. */
951 if (large_size > 0)
952 large_base = allocate_dynamic_stack_space (GEN_INT (large_size), 0,
953 large_align, true);
956 for (si = 0; si < n; ++si)
958 rtx base;
959 unsigned base_align, alignb;
960 HOST_WIDE_INT offset;
962 i = stack_vars_sorted[si];
964 /* Skip variables that aren't partition representatives, for now. */
965 if (stack_vars[i].representative != i)
966 continue;
968 /* Skip variables that have already had rtl assigned. See also
969 add_stack_var where we perpetrate this pc_rtx hack. */
970 decl = stack_vars[i].decl;
971 if ((TREE_CODE (decl) == SSA_NAME
972 ? SA.partition_to_pseudo[var_to_partition (SA.map, decl)]
973 : DECL_RTL (decl)) != pc_rtx)
974 continue;
976 /* Check the predicate to see whether this variable should be
977 allocated in this pass. */
978 if (pred && !pred (i))
979 continue;
981 alignb = stack_vars[i].alignb;
982 if (alignb * BITS_PER_UNIT <= MAX_SUPPORTED_STACK_ALIGNMENT)
984 base = virtual_stack_vars_rtx;
985 if ((flag_sanitize & SANITIZE_ADDRESS) && ASAN_STACK && pred)
987 HOST_WIDE_INT prev_offset = frame_offset;
988 tree repr_decl = NULL_TREE;
990 offset
991 = alloc_stack_frame_space (stack_vars[i].size
992 + ASAN_RED_ZONE_SIZE,
993 MAX (alignb, ASAN_RED_ZONE_SIZE));
994 data->asan_vec.safe_push (prev_offset);
995 data->asan_vec.safe_push (offset + stack_vars[i].size);
996 /* Find best representative of the partition.
997 Prefer those with DECL_NAME, even better
998 satisfying asan_protect_stack_decl predicate. */
999 for (j = i; j != EOC; j = stack_vars[j].next)
1000 if (asan_protect_stack_decl (stack_vars[j].decl)
1001 && DECL_NAME (stack_vars[j].decl))
1003 repr_decl = stack_vars[j].decl;
1004 break;
1006 else if (repr_decl == NULL_TREE
1007 && DECL_P (stack_vars[j].decl)
1008 && DECL_NAME (stack_vars[j].decl))
1009 repr_decl = stack_vars[j].decl;
1010 if (repr_decl == NULL_TREE)
1011 repr_decl = stack_vars[i].decl;
1012 data->asan_decl_vec.safe_push (repr_decl);
1013 data->asan_alignb = MAX (data->asan_alignb, alignb);
1014 if (data->asan_base == NULL)
1015 data->asan_base = gen_reg_rtx (Pmode);
1016 base = data->asan_base;
1018 if (!STRICT_ALIGNMENT)
1019 base_align = crtl->max_used_stack_slot_alignment;
1020 else
1021 base_align = MAX (crtl->max_used_stack_slot_alignment,
1022 GET_MODE_ALIGNMENT (SImode)
1023 << ASAN_SHADOW_SHIFT);
1025 else
1027 offset = alloc_stack_frame_space (stack_vars[i].size, alignb);
1028 base_align = crtl->max_used_stack_slot_alignment;
1031 else
1033 /* Large alignment is only processed in the last pass. */
1034 if (pred)
1035 continue;
1036 gcc_assert (large_base != NULL);
1038 large_alloc += alignb - 1;
1039 large_alloc &= -(HOST_WIDE_INT)alignb;
1040 offset = large_alloc;
1041 large_alloc += stack_vars[i].size;
1043 base = large_base;
1044 base_align = large_align;
1047 /* Create rtl for each variable based on their location within the
1048 partition. */
1049 for (j = i; j != EOC; j = stack_vars[j].next)
1051 expand_one_stack_var_at (stack_vars[j].decl,
1052 base, base_align,
1053 offset);
1057 gcc_assert (large_alloc == large_size);
1060 /* Take into account all sizes of partitions and reset DECL_RTLs. */
1061 static HOST_WIDE_INT
1062 account_stack_vars (void)
1064 size_t si, j, i, n = stack_vars_num;
1065 HOST_WIDE_INT size = 0;
1067 for (si = 0; si < n; ++si)
1069 i = stack_vars_sorted[si];
1071 /* Skip variables that aren't partition representatives, for now. */
1072 if (stack_vars[i].representative != i)
1073 continue;
1075 size += stack_vars[i].size;
1076 for (j = i; j != EOC; j = stack_vars[j].next)
1077 set_rtl (stack_vars[j].decl, NULL);
1079 return size;
1082 /* A subroutine of expand_one_var. Called to immediately assign rtl
1083 to a variable to be allocated in the stack frame. */
1085 static void
1086 expand_one_stack_var (tree var)
1088 HOST_WIDE_INT size, offset;
1089 unsigned byte_align;
1091 size = tree_to_uhwi (DECL_SIZE_UNIT (SSAVAR (var)));
1092 byte_align = align_local_variable (SSAVAR (var));
1094 /* We handle highly aligned variables in expand_stack_vars. */
1095 gcc_assert (byte_align * BITS_PER_UNIT <= MAX_SUPPORTED_STACK_ALIGNMENT);
1097 offset = alloc_stack_frame_space (size, byte_align);
1099 expand_one_stack_var_at (var, virtual_stack_vars_rtx,
1100 crtl->max_used_stack_slot_alignment, offset);
1103 /* A subroutine of expand_one_var. Called to assign rtl to a VAR_DECL
1104 that will reside in a hard register. */
1106 static void
1107 expand_one_hard_reg_var (tree var)
1109 rest_of_decl_compilation (var, 0, 0);
1112 /* A subroutine of expand_one_var. Called to assign rtl to a VAR_DECL
1113 that will reside in a pseudo register. */
1115 static void
1116 expand_one_register_var (tree var)
1118 tree decl = SSAVAR (var);
1119 tree type = TREE_TYPE (decl);
1120 enum machine_mode reg_mode = promote_decl_mode (decl, NULL);
1121 rtx x = gen_reg_rtx (reg_mode);
1123 set_rtl (var, x);
1125 /* Note if the object is a user variable. */
1126 if (!DECL_ARTIFICIAL (decl))
1127 mark_user_reg (x);
1129 if (POINTER_TYPE_P (type))
1130 mark_reg_pointer (x, get_pointer_alignment (var));
1133 /* A subroutine of expand_one_var. Called to assign rtl to a VAR_DECL that
1134 has some associated error, e.g. its type is error-mark. We just need
1135 to pick something that won't crash the rest of the compiler. */
1137 static void
1138 expand_one_error_var (tree var)
1140 enum machine_mode mode = DECL_MODE (var);
1141 rtx x;
1143 if (mode == BLKmode)
1144 x = gen_rtx_MEM (BLKmode, const0_rtx);
1145 else if (mode == VOIDmode)
1146 x = const0_rtx;
1147 else
1148 x = gen_reg_rtx (mode);
1150 SET_DECL_RTL (var, x);
1153 /* A subroutine of expand_one_var. VAR is a variable that will be
1154 allocated to the local stack frame. Return true if we wish to
1155 add VAR to STACK_VARS so that it will be coalesced with other
1156 variables. Return false to allocate VAR immediately.
1158 This function is used to reduce the number of variables considered
1159 for coalescing, which reduces the size of the quadratic problem. */
1161 static bool
1162 defer_stack_allocation (tree var, bool toplevel)
1164 /* Whether the variable is small enough for immediate allocation not to be
1165 a problem with regard to the frame size. */
1166 bool smallish
1167 = ((HOST_WIDE_INT) tree_to_uhwi (DECL_SIZE_UNIT (var))
1168 < PARAM_VALUE (PARAM_MIN_SIZE_FOR_STACK_SHARING));
1170 /* If stack protection is enabled, *all* stack variables must be deferred,
1171 so that we can re-order the strings to the top of the frame.
1172 Similarly for Address Sanitizer. */
1173 if (flag_stack_protect || ((flag_sanitize & SANITIZE_ADDRESS) && ASAN_STACK))
1174 return true;
1176 /* We handle "large" alignment via dynamic allocation. We want to handle
1177 this extra complication in only one place, so defer them. */
1178 if (DECL_ALIGN (var) > MAX_SUPPORTED_STACK_ALIGNMENT)
1179 return true;
1181 /* When optimization is enabled, DECL_IGNORED_P variables originally scoped
1182 might be detached from their block and appear at toplevel when we reach
1183 here. We want to coalesce them with variables from other blocks when
1184 the immediate contribution to the frame size would be noticeable. */
1185 if (toplevel && optimize > 0 && DECL_IGNORED_P (var) && !smallish)
1186 return true;
1188 /* Variables declared in the outermost scope automatically conflict
1189 with every other variable. The only reason to want to defer them
1190 at all is that, after sorting, we can more efficiently pack
1191 small variables in the stack frame. Continue to defer at -O2. */
1192 if (toplevel && optimize < 2)
1193 return false;
1195 /* Without optimization, *most* variables are allocated from the
1196 stack, which makes the quadratic problem large exactly when we
1197 want compilation to proceed as quickly as possible. On the
1198 other hand, we don't want the function's stack frame size to
1199 get completely out of hand. So we avoid adding scalars and
1200 "small" aggregates to the list at all. */
1201 if (optimize == 0 && smallish)
1202 return false;
1204 return true;
1207 /* A subroutine of expand_used_vars. Expand one variable according to
1208 its flavor. Variables to be placed on the stack are not actually
1209 expanded yet, merely recorded.
1210 When REALLY_EXPAND is false, only add stack values to be allocated.
1211 Return stack usage this variable is supposed to take.
1214 static HOST_WIDE_INT
1215 expand_one_var (tree var, bool toplevel, bool really_expand)
1217 unsigned int align = BITS_PER_UNIT;
1218 tree origvar = var;
1220 var = SSAVAR (var);
1222 if (TREE_TYPE (var) != error_mark_node && TREE_CODE (var) == VAR_DECL)
1224 /* Because we don't know if VAR will be in register or on stack,
1225 we conservatively assume it will be on stack even if VAR is
1226 eventually put into register after RA pass. For non-automatic
1227 variables, which won't be on stack, we collect alignment of
1228 type and ignore user specified alignment. Similarly for
1229 SSA_NAMEs for which use_register_for_decl returns true. */
1230 if (TREE_STATIC (var)
1231 || DECL_EXTERNAL (var)
1232 || (TREE_CODE (origvar) == SSA_NAME && use_register_for_decl (var)))
1233 align = MINIMUM_ALIGNMENT (TREE_TYPE (var),
1234 TYPE_MODE (TREE_TYPE (var)),
1235 TYPE_ALIGN (TREE_TYPE (var)));
1236 else if (DECL_HAS_VALUE_EXPR_P (var)
1237 || (DECL_RTL_SET_P (var) && MEM_P (DECL_RTL (var))))
1238 /* Don't consider debug only variables with DECL_HAS_VALUE_EXPR_P set
1239 or variables which were assigned a stack slot already by
1240 expand_one_stack_var_at - in the latter case DECL_ALIGN has been
1241 changed from the offset chosen to it. */
1242 align = crtl->stack_alignment_estimated;
1243 else
1244 align = MINIMUM_ALIGNMENT (var, DECL_MODE (var), DECL_ALIGN (var));
1246 /* If the variable alignment is very large we'll dynamicaly allocate
1247 it, which means that in-frame portion is just a pointer. */
1248 if (align > MAX_SUPPORTED_STACK_ALIGNMENT)
1249 align = POINTER_SIZE;
1252 if (SUPPORTS_STACK_ALIGNMENT
1253 && crtl->stack_alignment_estimated < align)
1255 /* stack_alignment_estimated shouldn't change after stack
1256 realign decision made */
1257 gcc_assert (!crtl->stack_realign_processed);
1258 crtl->stack_alignment_estimated = align;
1261 /* stack_alignment_needed > PREFERRED_STACK_BOUNDARY is permitted.
1262 So here we only make sure stack_alignment_needed >= align. */
1263 if (crtl->stack_alignment_needed < align)
1264 crtl->stack_alignment_needed = align;
1265 if (crtl->max_used_stack_slot_alignment < align)
1266 crtl->max_used_stack_slot_alignment = align;
1268 if (TREE_CODE (origvar) == SSA_NAME)
1270 gcc_assert (TREE_CODE (var) != VAR_DECL
1271 || (!DECL_EXTERNAL (var)
1272 && !DECL_HAS_VALUE_EXPR_P (var)
1273 && !TREE_STATIC (var)
1274 && TREE_TYPE (var) != error_mark_node
1275 && !DECL_HARD_REGISTER (var)
1276 && really_expand));
1278 if (TREE_CODE (var) != VAR_DECL && TREE_CODE (origvar) != SSA_NAME)
1280 else if (DECL_EXTERNAL (var))
1282 else if (DECL_HAS_VALUE_EXPR_P (var))
1284 else if (TREE_STATIC (var))
1286 else if (TREE_CODE (origvar) != SSA_NAME && DECL_RTL_SET_P (var))
1288 else if (TREE_TYPE (var) == error_mark_node)
1290 if (really_expand)
1291 expand_one_error_var (var);
1293 else if (TREE_CODE (var) == VAR_DECL && DECL_HARD_REGISTER (var))
1295 if (really_expand)
1296 expand_one_hard_reg_var (var);
1298 else if (use_register_for_decl (var))
1300 if (really_expand)
1301 expand_one_register_var (origvar);
1303 else if (! valid_constant_size_p (DECL_SIZE_UNIT (var)))
1305 /* Reject variables which cover more than half of the address-space. */
1306 if (really_expand)
1308 error ("size of variable %q+D is too large", var);
1309 expand_one_error_var (var);
1312 else if (defer_stack_allocation (var, toplevel))
1313 add_stack_var (origvar);
1314 else
1316 if (really_expand)
1317 expand_one_stack_var (origvar);
1318 return tree_to_uhwi (DECL_SIZE_UNIT (var));
1320 return 0;
1323 /* A subroutine of expand_used_vars. Walk down through the BLOCK tree
1324 expanding variables. Those variables that can be put into registers
1325 are allocated pseudos; those that can't are put on the stack.
1327 TOPLEVEL is true if this is the outermost BLOCK. */
1329 static void
1330 expand_used_vars_for_block (tree block, bool toplevel)
1332 tree t;
1334 /* Expand all variables at this level. */
1335 for (t = BLOCK_VARS (block); t ; t = DECL_CHAIN (t))
1336 if (TREE_USED (t)
1337 && ((TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != RESULT_DECL)
1338 || !DECL_NONSHAREABLE (t)))
1339 expand_one_var (t, toplevel, true);
1341 /* Expand all variables at containing levels. */
1342 for (t = BLOCK_SUBBLOCKS (block); t ; t = BLOCK_CHAIN (t))
1343 expand_used_vars_for_block (t, false);
1346 /* A subroutine of expand_used_vars. Walk down through the BLOCK tree
1347 and clear TREE_USED on all local variables. */
1349 static void
1350 clear_tree_used (tree block)
1352 tree t;
1354 for (t = BLOCK_VARS (block); t ; t = DECL_CHAIN (t))
1355 /* if (!TREE_STATIC (t) && !DECL_EXTERNAL (t)) */
1356 if ((TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != RESULT_DECL)
1357 || !DECL_NONSHAREABLE (t))
1358 TREE_USED (t) = 0;
1360 for (t = BLOCK_SUBBLOCKS (block); t ; t = BLOCK_CHAIN (t))
1361 clear_tree_used (t);
1364 enum {
1365 SPCT_FLAG_DEFAULT = 1,
1366 SPCT_FLAG_ALL = 2,
1367 SPCT_FLAG_STRONG = 3
1370 /* Examine TYPE and determine a bit mask of the following features. */
1372 #define SPCT_HAS_LARGE_CHAR_ARRAY 1
1373 #define SPCT_HAS_SMALL_CHAR_ARRAY 2
1374 #define SPCT_HAS_ARRAY 4
1375 #define SPCT_HAS_AGGREGATE 8
1377 static unsigned int
1378 stack_protect_classify_type (tree type)
1380 unsigned int ret = 0;
1381 tree t;
1383 switch (TREE_CODE (type))
1385 case ARRAY_TYPE:
1386 t = TYPE_MAIN_VARIANT (TREE_TYPE (type));
1387 if (t == char_type_node
1388 || t == signed_char_type_node
1389 || t == unsigned_char_type_node)
1391 unsigned HOST_WIDE_INT max = PARAM_VALUE (PARAM_SSP_BUFFER_SIZE);
1392 unsigned HOST_WIDE_INT len;
1394 if (!TYPE_SIZE_UNIT (type)
1395 || !tree_fits_uhwi_p (TYPE_SIZE_UNIT (type)))
1396 len = max;
1397 else
1398 len = tree_to_uhwi (TYPE_SIZE_UNIT (type));
1400 if (len < max)
1401 ret = SPCT_HAS_SMALL_CHAR_ARRAY | SPCT_HAS_ARRAY;
1402 else
1403 ret = SPCT_HAS_LARGE_CHAR_ARRAY | SPCT_HAS_ARRAY;
1405 else
1406 ret = SPCT_HAS_ARRAY;
1407 break;
1409 case UNION_TYPE:
1410 case QUAL_UNION_TYPE:
1411 case RECORD_TYPE:
1412 ret = SPCT_HAS_AGGREGATE;
1413 for (t = TYPE_FIELDS (type); t ; t = TREE_CHAIN (t))
1414 if (TREE_CODE (t) == FIELD_DECL)
1415 ret |= stack_protect_classify_type (TREE_TYPE (t));
1416 break;
1418 default:
1419 break;
1422 return ret;
1425 /* Return nonzero if DECL should be segregated into the "vulnerable" upper
1426 part of the local stack frame. Remember if we ever return nonzero for
1427 any variable in this function. The return value is the phase number in
1428 which the variable should be allocated. */
1430 static int
1431 stack_protect_decl_phase (tree decl)
1433 unsigned int bits = stack_protect_classify_type (TREE_TYPE (decl));
1434 int ret = 0;
1436 if (bits & SPCT_HAS_SMALL_CHAR_ARRAY)
1437 has_short_buffer = true;
1439 if (flag_stack_protect == SPCT_FLAG_ALL
1440 || flag_stack_protect == SPCT_FLAG_STRONG)
1442 if ((bits & (SPCT_HAS_SMALL_CHAR_ARRAY | SPCT_HAS_LARGE_CHAR_ARRAY))
1443 && !(bits & SPCT_HAS_AGGREGATE))
1444 ret = 1;
1445 else if (bits & SPCT_HAS_ARRAY)
1446 ret = 2;
1448 else
1449 ret = (bits & SPCT_HAS_LARGE_CHAR_ARRAY) != 0;
1451 if (ret)
1452 has_protected_decls = true;
1454 return ret;
1457 /* Two helper routines that check for phase 1 and phase 2. These are used
1458 as callbacks for expand_stack_vars. */
1460 static bool
1461 stack_protect_decl_phase_1 (size_t i)
1463 return stack_protect_decl_phase (stack_vars[i].decl) == 1;
1466 static bool
1467 stack_protect_decl_phase_2 (size_t i)
1469 return stack_protect_decl_phase (stack_vars[i].decl) == 2;
1472 /* And helper function that checks for asan phase (with stack protector
1473 it is phase 3). This is used as callback for expand_stack_vars.
1474 Returns true if any of the vars in the partition need to be protected. */
1476 static bool
1477 asan_decl_phase_3 (size_t i)
1479 while (i != EOC)
1481 if (asan_protect_stack_decl (stack_vars[i].decl))
1482 return true;
1483 i = stack_vars[i].next;
1485 return false;
1488 /* Ensure that variables in different stack protection phases conflict
1489 so that they are not merged and share the same stack slot. */
1491 static void
1492 add_stack_protection_conflicts (void)
1494 size_t i, j, n = stack_vars_num;
1495 unsigned char *phase;
1497 phase = XNEWVEC (unsigned char, n);
1498 for (i = 0; i < n; ++i)
1499 phase[i] = stack_protect_decl_phase (stack_vars[i].decl);
1501 for (i = 0; i < n; ++i)
1503 unsigned char ph_i = phase[i];
1504 for (j = i + 1; j < n; ++j)
1505 if (ph_i != phase[j])
1506 add_stack_var_conflict (i, j);
1509 XDELETEVEC (phase);
1512 /* Create a decl for the guard at the top of the stack frame. */
1514 static void
1515 create_stack_guard (void)
1517 tree guard = build_decl (DECL_SOURCE_LOCATION (current_function_decl),
1518 VAR_DECL, NULL, ptr_type_node);
1519 TREE_THIS_VOLATILE (guard) = 1;
1520 TREE_USED (guard) = 1;
1521 expand_one_stack_var (guard);
1522 crtl->stack_protect_guard = guard;
1525 /* Prepare for expanding variables. */
1526 static void
1527 init_vars_expansion (void)
1529 /* Conflict bitmaps, and a few related temporary bitmaps, go here. */
1530 bitmap_obstack_initialize (&stack_var_bitmap_obstack);
1532 /* A map from decl to stack partition. */
1533 decl_to_stack_part = pointer_map_create ();
1535 /* Initialize local stack smashing state. */
1536 has_protected_decls = false;
1537 has_short_buffer = false;
1540 /* Free up stack variable graph data. */
1541 static void
1542 fini_vars_expansion (void)
1544 bitmap_obstack_release (&stack_var_bitmap_obstack);
1545 if (stack_vars)
1546 XDELETEVEC (stack_vars);
1547 if (stack_vars_sorted)
1548 XDELETEVEC (stack_vars_sorted);
1549 stack_vars = NULL;
1550 stack_vars_sorted = NULL;
1551 stack_vars_alloc = stack_vars_num = 0;
1552 pointer_map_destroy (decl_to_stack_part);
1553 decl_to_stack_part = NULL;
1556 /* Make a fair guess for the size of the stack frame of the function
1557 in NODE. This doesn't have to be exact, the result is only used in
1558 the inline heuristics. So we don't want to run the full stack var
1559 packing algorithm (which is quadratic in the number of stack vars).
1560 Instead, we calculate the total size of all stack vars. This turns
1561 out to be a pretty fair estimate -- packing of stack vars doesn't
1562 happen very often. */
1564 HOST_WIDE_INT
1565 estimated_stack_frame_size (struct cgraph_node *node)
1567 HOST_WIDE_INT size = 0;
1568 size_t i;
1569 tree var;
1570 struct function *fn = DECL_STRUCT_FUNCTION (node->decl);
1572 push_cfun (fn);
1574 init_vars_expansion ();
1576 FOR_EACH_LOCAL_DECL (fn, i, var)
1577 if (auto_var_in_fn_p (var, fn->decl))
1578 size += expand_one_var (var, true, false);
1580 if (stack_vars_num > 0)
1582 /* Fake sorting the stack vars for account_stack_vars (). */
1583 stack_vars_sorted = XNEWVEC (size_t, stack_vars_num);
1584 for (i = 0; i < stack_vars_num; ++i)
1585 stack_vars_sorted[i] = i;
1586 size += account_stack_vars ();
1589 fini_vars_expansion ();
1590 pop_cfun ();
1591 return size;
1594 /* Helper routine to check if a record or union contains an array field. */
1596 static int
1597 record_or_union_type_has_array_p (const_tree tree_type)
1599 tree fields = TYPE_FIELDS (tree_type);
1600 tree f;
1602 for (f = fields; f; f = DECL_CHAIN (f))
1603 if (TREE_CODE (f) == FIELD_DECL)
1605 tree field_type = TREE_TYPE (f);
1606 if (RECORD_OR_UNION_TYPE_P (field_type)
1607 && record_or_union_type_has_array_p (field_type))
1608 return 1;
1609 if (TREE_CODE (field_type) == ARRAY_TYPE)
1610 return 1;
1612 return 0;
1615 /* Check if the current function has local referenced variables that
1616 have their addresses taken, contain an array, or are arrays. */
1618 static bool
1619 stack_protect_decl_p ()
1621 unsigned i;
1622 tree var;
1624 FOR_EACH_LOCAL_DECL (cfun, i, var)
1625 if (!is_global_var (var))
1627 tree var_type = TREE_TYPE (var);
1628 if (TREE_CODE (var) == VAR_DECL
1629 && (TREE_CODE (var_type) == ARRAY_TYPE
1630 || TREE_ADDRESSABLE (var)
1631 || (RECORD_OR_UNION_TYPE_P (var_type)
1632 && record_or_union_type_has_array_p (var_type))))
1633 return true;
1635 return false;
1638 /* Check if the current function has calls that use a return slot. */
1640 static bool
1641 stack_protect_return_slot_p ()
1643 basic_block bb;
1645 FOR_ALL_BB_FN (bb, cfun)
1646 for (gimple_stmt_iterator gsi = gsi_start_bb (bb);
1647 !gsi_end_p (gsi); gsi_next (&gsi))
1649 gimple stmt = gsi_stmt (gsi);
1650 /* This assumes that calls to internal-only functions never
1651 use a return slot. */
1652 if (is_gimple_call (stmt)
1653 && !gimple_call_internal_p (stmt)
1654 && aggregate_value_p (TREE_TYPE (gimple_call_fntype (stmt)),
1655 gimple_call_fndecl (stmt)))
1656 return true;
1658 return false;
1661 /* Expand all variables used in the function. */
1663 static rtx
1664 expand_used_vars (void)
1666 tree var, outer_block = DECL_INITIAL (current_function_decl);
1667 vec<tree> maybe_local_decls = vNULL;
1668 rtx var_end_seq = NULL_RTX;
1669 struct pointer_map_t *ssa_name_decls;
1670 unsigned i;
1671 unsigned len;
1672 bool gen_stack_protect_signal = false;
1674 /* Compute the phase of the stack frame for this function. */
1676 int align = PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT;
1677 int off = STARTING_FRAME_OFFSET % align;
1678 frame_phase = off ? align - off : 0;
1681 /* Set TREE_USED on all variables in the local_decls. */
1682 FOR_EACH_LOCAL_DECL (cfun, i, var)
1683 TREE_USED (var) = 1;
1684 /* Clear TREE_USED on all variables associated with a block scope. */
1685 clear_tree_used (DECL_INITIAL (current_function_decl));
1687 init_vars_expansion ();
1689 ssa_name_decls = pointer_map_create ();
1690 for (i = 0; i < SA.map->num_partitions; i++)
1692 tree var = partition_to_var (SA.map, i);
1694 gcc_assert (!virtual_operand_p (var));
1696 /* Assign decls to each SSA name partition, share decls for partitions
1697 we could have coalesced (those with the same type). */
1698 if (SSA_NAME_VAR (var) == NULL_TREE)
1700 void **slot = pointer_map_insert (ssa_name_decls, TREE_TYPE (var));
1701 if (!*slot)
1702 *slot = (void *) create_tmp_reg (TREE_TYPE (var), NULL);
1703 replace_ssa_name_symbol (var, (tree) *slot);
1706 /* Always allocate space for partitions based on VAR_DECLs. But for
1707 those based on PARM_DECLs or RESULT_DECLs and which matter for the
1708 debug info, there is no need to do so if optimization is disabled
1709 because all the SSA_NAMEs based on these DECLs have been coalesced
1710 into a single partition, which is thus assigned the canonical RTL
1711 location of the DECLs. If in_lto_p, we can't rely on optimize,
1712 a function could be compiled with -O1 -flto first and only the
1713 link performed at -O0. */
1714 if (TREE_CODE (SSA_NAME_VAR (var)) == VAR_DECL)
1715 expand_one_var (var, true, true);
1716 else if (DECL_IGNORED_P (SSA_NAME_VAR (var)) || optimize || in_lto_p)
1718 /* This is a PARM_DECL or RESULT_DECL. For those partitions that
1719 contain the default def (representing the parm or result itself)
1720 we don't do anything here. But those which don't contain the
1721 default def (representing a temporary based on the parm/result)
1722 we need to allocate space just like for normal VAR_DECLs. */
1723 if (!bitmap_bit_p (SA.partition_has_default_def, i))
1725 expand_one_var (var, true, true);
1726 gcc_assert (SA.partition_to_pseudo[i]);
1730 pointer_map_destroy (ssa_name_decls);
1732 if (flag_stack_protect == SPCT_FLAG_STRONG)
1733 gen_stack_protect_signal
1734 = stack_protect_decl_p () || stack_protect_return_slot_p ();
1736 /* At this point all variables on the local_decls with TREE_USED
1737 set are not associated with any block scope. Lay them out. */
1739 len = vec_safe_length (cfun->local_decls);
1740 FOR_EACH_LOCAL_DECL (cfun, i, var)
1742 bool expand_now = false;
1744 /* Expanded above already. */
1745 if (is_gimple_reg (var))
1747 TREE_USED (var) = 0;
1748 goto next;
1750 /* We didn't set a block for static or extern because it's hard
1751 to tell the difference between a global variable (re)declared
1752 in a local scope, and one that's really declared there to
1753 begin with. And it doesn't really matter much, since we're
1754 not giving them stack space. Expand them now. */
1755 else if (TREE_STATIC (var) || DECL_EXTERNAL (var))
1756 expand_now = true;
1758 /* Expand variables not associated with any block now. Those created by
1759 the optimizers could be live anywhere in the function. Those that
1760 could possibly have been scoped originally and detached from their
1761 block will have their allocation deferred so we coalesce them with
1762 others when optimization is enabled. */
1763 else if (TREE_USED (var))
1764 expand_now = true;
1766 /* Finally, mark all variables on the list as used. We'll use
1767 this in a moment when we expand those associated with scopes. */
1768 TREE_USED (var) = 1;
1770 if (expand_now)
1771 expand_one_var (var, true, true);
1773 next:
1774 if (DECL_ARTIFICIAL (var) && !DECL_IGNORED_P (var))
1776 rtx rtl = DECL_RTL_IF_SET (var);
1778 /* Keep artificial non-ignored vars in cfun->local_decls
1779 chain until instantiate_decls. */
1780 if (rtl && (MEM_P (rtl) || GET_CODE (rtl) == CONCAT))
1781 add_local_decl (cfun, var);
1782 else if (rtl == NULL_RTX)
1783 /* If rtl isn't set yet, which can happen e.g. with
1784 -fstack-protector, retry before returning from this
1785 function. */
1786 maybe_local_decls.safe_push (var);
1790 /* We duplicated some of the decls in CFUN->LOCAL_DECLS.
1792 +-----------------+-----------------+
1793 | ...processed... | ...duplicates...|
1794 +-----------------+-----------------+
1796 +-- LEN points here.
1798 We just want the duplicates, as those are the artificial
1799 non-ignored vars that we want to keep until instantiate_decls.
1800 Move them down and truncate the array. */
1801 if (!vec_safe_is_empty (cfun->local_decls))
1802 cfun->local_decls->block_remove (0, len);
1804 /* At this point, all variables within the block tree with TREE_USED
1805 set are actually used by the optimized function. Lay them out. */
1806 expand_used_vars_for_block (outer_block, true);
1808 if (stack_vars_num > 0)
1810 add_scope_conflicts ();
1812 /* If stack protection is enabled, we don't share space between
1813 vulnerable data and non-vulnerable data. */
1814 if (flag_stack_protect)
1815 add_stack_protection_conflicts ();
1817 /* Now that we have collected all stack variables, and have computed a
1818 minimal interference graph, attempt to save some stack space. */
1819 partition_stack_vars ();
1820 if (dump_file)
1821 dump_stack_var_partition ();
1824 switch (flag_stack_protect)
1826 case SPCT_FLAG_ALL:
1827 create_stack_guard ();
1828 break;
1830 case SPCT_FLAG_STRONG:
1831 if (gen_stack_protect_signal
1832 || cfun->calls_alloca || has_protected_decls)
1833 create_stack_guard ();
1834 break;
1836 case SPCT_FLAG_DEFAULT:
1837 if (cfun->calls_alloca || has_protected_decls)
1838 create_stack_guard ();
1839 break;
1841 default:
1845 /* Assign rtl to each variable based on these partitions. */
1846 if (stack_vars_num > 0)
1848 struct stack_vars_data data;
1850 data.asan_vec = vNULL;
1851 data.asan_decl_vec = vNULL;
1852 data.asan_base = NULL_RTX;
1853 data.asan_alignb = 0;
1855 /* Reorder decls to be protected by iterating over the variables
1856 array multiple times, and allocating out of each phase in turn. */
1857 /* ??? We could probably integrate this into the qsort we did
1858 earlier, such that we naturally see these variables first,
1859 and thus naturally allocate things in the right order. */
1860 if (has_protected_decls)
1862 /* Phase 1 contains only character arrays. */
1863 expand_stack_vars (stack_protect_decl_phase_1, &data);
1865 /* Phase 2 contains other kinds of arrays. */
1866 if (flag_stack_protect == 2)
1867 expand_stack_vars (stack_protect_decl_phase_2, &data);
1870 if ((flag_sanitize & SANITIZE_ADDRESS) && ASAN_STACK)
1871 /* Phase 3, any partitions that need asan protection
1872 in addition to phase 1 and 2. */
1873 expand_stack_vars (asan_decl_phase_3, &data);
1875 if (!data.asan_vec.is_empty ())
1877 HOST_WIDE_INT prev_offset = frame_offset;
1878 HOST_WIDE_INT offset, sz, redzonesz;
1879 redzonesz = ASAN_RED_ZONE_SIZE;
1880 sz = data.asan_vec[0] - prev_offset;
1881 if (data.asan_alignb > ASAN_RED_ZONE_SIZE
1882 && data.asan_alignb <= 4096
1883 && sz + ASAN_RED_ZONE_SIZE >= (int) data.asan_alignb)
1884 redzonesz = ((sz + ASAN_RED_ZONE_SIZE + data.asan_alignb - 1)
1885 & ~(data.asan_alignb - HOST_WIDE_INT_1)) - sz;
1886 offset
1887 = alloc_stack_frame_space (redzonesz, ASAN_RED_ZONE_SIZE);
1888 data.asan_vec.safe_push (prev_offset);
1889 data.asan_vec.safe_push (offset);
1890 /* Leave space for alignment if STRICT_ALIGNMENT. */
1891 if (STRICT_ALIGNMENT)
1892 alloc_stack_frame_space ((GET_MODE_ALIGNMENT (SImode)
1893 << ASAN_SHADOW_SHIFT)
1894 / BITS_PER_UNIT, 1);
1896 var_end_seq
1897 = asan_emit_stack_protection (virtual_stack_vars_rtx,
1898 data.asan_base,
1899 data.asan_alignb,
1900 data.asan_vec.address (),
1901 data.asan_decl_vec.address (),
1902 data.asan_vec.length ());
1905 expand_stack_vars (NULL, &data);
1907 data.asan_vec.release ();
1908 data.asan_decl_vec.release ();
1911 fini_vars_expansion ();
1913 /* If there were any artificial non-ignored vars without rtl
1914 found earlier, see if deferred stack allocation hasn't assigned
1915 rtl to them. */
1916 FOR_EACH_VEC_ELT_REVERSE (maybe_local_decls, i, var)
1918 rtx rtl = DECL_RTL_IF_SET (var);
1920 /* Keep artificial non-ignored vars in cfun->local_decls
1921 chain until instantiate_decls. */
1922 if (rtl && (MEM_P (rtl) || GET_CODE (rtl) == CONCAT))
1923 add_local_decl (cfun, var);
1925 maybe_local_decls.release ();
1927 /* If the target requires that FRAME_OFFSET be aligned, do it. */
1928 if (STACK_ALIGNMENT_NEEDED)
1930 HOST_WIDE_INT align = PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT;
1931 if (!FRAME_GROWS_DOWNWARD)
1932 frame_offset += align - 1;
1933 frame_offset &= -align;
1936 return var_end_seq;
1940 /* If we need to produce a detailed dump, print the tree representation
1941 for STMT to the dump file. SINCE is the last RTX after which the RTL
1942 generated for STMT should have been appended. */
1944 static void
1945 maybe_dump_rtl_for_gimple_stmt (gimple stmt, rtx since)
1947 if (dump_file && (dump_flags & TDF_DETAILS))
1949 fprintf (dump_file, "\n;; ");
1950 print_gimple_stmt (dump_file, stmt, 0,
1951 TDF_SLIM | (dump_flags & TDF_LINENO));
1952 fprintf (dump_file, "\n");
1954 print_rtl (dump_file, since ? NEXT_INSN (since) : since);
1958 /* Maps the blocks that do not contain tree labels to rtx labels. */
1960 static struct pointer_map_t *lab_rtx_for_bb;
1962 /* Returns the label_rtx expression for a label starting basic block BB. */
1964 static rtx
1965 label_rtx_for_bb (basic_block bb ATTRIBUTE_UNUSED)
1967 gimple_stmt_iterator gsi;
1968 tree lab;
1969 gimple lab_stmt;
1970 void **elt;
1972 if (bb->flags & BB_RTL)
1973 return block_label (bb);
1975 elt = pointer_map_contains (lab_rtx_for_bb, bb);
1976 if (elt)
1977 return (rtx) *elt;
1979 /* Find the tree label if it is present. */
1981 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1983 lab_stmt = gsi_stmt (gsi);
1984 if (gimple_code (lab_stmt) != GIMPLE_LABEL)
1985 break;
1987 lab = gimple_label_label (lab_stmt);
1988 if (DECL_NONLOCAL (lab))
1989 break;
1991 return label_rtx (lab);
1994 elt = pointer_map_insert (lab_rtx_for_bb, bb);
1995 *elt = gen_label_rtx ();
1996 return (rtx) *elt;
2000 /* A subroutine of expand_gimple_cond. Given E, a fallthrough edge
2001 of a basic block where we just expanded the conditional at the end,
2002 possibly clean up the CFG and instruction sequence. LAST is the
2003 last instruction before the just emitted jump sequence. */
2005 static void
2006 maybe_cleanup_end_of_block (edge e, rtx last)
2008 /* Special case: when jumpif decides that the condition is
2009 trivial it emits an unconditional jump (and the necessary
2010 barrier). But we still have two edges, the fallthru one is
2011 wrong. purge_dead_edges would clean this up later. Unfortunately
2012 we have to insert insns (and split edges) before
2013 find_many_sub_basic_blocks and hence before purge_dead_edges.
2014 But splitting edges might create new blocks which depend on the
2015 fact that if there are two edges there's no barrier. So the
2016 barrier would get lost and verify_flow_info would ICE. Instead
2017 of auditing all edge splitters to care for the barrier (which
2018 normally isn't there in a cleaned CFG), fix it here. */
2019 if (BARRIER_P (get_last_insn ()))
2021 rtx insn;
2022 remove_edge (e);
2023 /* Now, we have a single successor block, if we have insns to
2024 insert on the remaining edge we potentially will insert
2025 it at the end of this block (if the dest block isn't feasible)
2026 in order to avoid splitting the edge. This insertion will take
2027 place in front of the last jump. But we might have emitted
2028 multiple jumps (conditional and one unconditional) to the
2029 same destination. Inserting in front of the last one then
2030 is a problem. See PR 40021. We fix this by deleting all
2031 jumps except the last unconditional one. */
2032 insn = PREV_INSN (get_last_insn ());
2033 /* Make sure we have an unconditional jump. Otherwise we're
2034 confused. */
2035 gcc_assert (JUMP_P (insn) && !any_condjump_p (insn));
2036 for (insn = PREV_INSN (insn); insn != last;)
2038 insn = PREV_INSN (insn);
2039 if (JUMP_P (NEXT_INSN (insn)))
2041 if (!any_condjump_p (NEXT_INSN (insn)))
2043 gcc_assert (BARRIER_P (NEXT_INSN (NEXT_INSN (insn))));
2044 delete_insn (NEXT_INSN (NEXT_INSN (insn)));
2046 delete_insn (NEXT_INSN (insn));
2052 /* A subroutine of expand_gimple_basic_block. Expand one GIMPLE_COND.
2053 Returns a new basic block if we've terminated the current basic
2054 block and created a new one. */
2056 static basic_block
2057 expand_gimple_cond (basic_block bb, gimple stmt)
2059 basic_block new_bb, dest;
2060 edge new_edge;
2061 edge true_edge;
2062 edge false_edge;
2063 rtx last2, last;
2064 enum tree_code code;
2065 tree op0, op1;
2067 code = gimple_cond_code (stmt);
2068 op0 = gimple_cond_lhs (stmt);
2069 op1 = gimple_cond_rhs (stmt);
2070 /* We're sometimes presented with such code:
2071 D.123_1 = x < y;
2072 if (D.123_1 != 0)
2074 This would expand to two comparisons which then later might
2075 be cleaned up by combine. But some pattern matchers like if-conversion
2076 work better when there's only one compare, so make up for this
2077 here as special exception if TER would have made the same change. */
2078 if (SA.values
2079 && TREE_CODE (op0) == SSA_NAME
2080 && TREE_CODE (TREE_TYPE (op0)) == BOOLEAN_TYPE
2081 && TREE_CODE (op1) == INTEGER_CST
2082 && ((gimple_cond_code (stmt) == NE_EXPR
2083 && integer_zerop (op1))
2084 || (gimple_cond_code (stmt) == EQ_EXPR
2085 && integer_onep (op1)))
2086 && bitmap_bit_p (SA.values, SSA_NAME_VERSION (op0)))
2088 gimple second = SSA_NAME_DEF_STMT (op0);
2089 if (gimple_code (second) == GIMPLE_ASSIGN)
2091 enum tree_code code2 = gimple_assign_rhs_code (second);
2092 if (TREE_CODE_CLASS (code2) == tcc_comparison)
2094 code = code2;
2095 op0 = gimple_assign_rhs1 (second);
2096 op1 = gimple_assign_rhs2 (second);
2098 /* If jumps are cheap turn some more codes into
2099 jumpy sequences. */
2100 else if (BRANCH_COST (optimize_insn_for_speed_p (), false) < 4)
2102 if ((code2 == BIT_AND_EXPR
2103 && TYPE_PRECISION (TREE_TYPE (op0)) == 1
2104 && TREE_CODE (gimple_assign_rhs2 (second)) != INTEGER_CST)
2105 || code2 == TRUTH_AND_EXPR)
2107 code = TRUTH_ANDIF_EXPR;
2108 op0 = gimple_assign_rhs1 (second);
2109 op1 = gimple_assign_rhs2 (second);
2111 else if (code2 == BIT_IOR_EXPR || code2 == TRUTH_OR_EXPR)
2113 code = TRUTH_ORIF_EXPR;
2114 op0 = gimple_assign_rhs1 (second);
2115 op1 = gimple_assign_rhs2 (second);
2121 last2 = last = get_last_insn ();
2123 extract_true_false_edges_from_block (bb, &true_edge, &false_edge);
2124 set_curr_insn_location (gimple_location (stmt));
2126 /* These flags have no purpose in RTL land. */
2127 true_edge->flags &= ~EDGE_TRUE_VALUE;
2128 false_edge->flags &= ~EDGE_FALSE_VALUE;
2130 /* We can either have a pure conditional jump with one fallthru edge or
2131 two-way jump that needs to be decomposed into two basic blocks. */
2132 if (false_edge->dest == bb->next_bb)
2134 jumpif_1 (code, op0, op1, label_rtx_for_bb (true_edge->dest),
2135 true_edge->probability);
2136 maybe_dump_rtl_for_gimple_stmt (stmt, last);
2137 if (true_edge->goto_locus != UNKNOWN_LOCATION)
2138 set_curr_insn_location (true_edge->goto_locus);
2139 false_edge->flags |= EDGE_FALLTHRU;
2140 maybe_cleanup_end_of_block (false_edge, last);
2141 return NULL;
2143 if (true_edge->dest == bb->next_bb)
2145 jumpifnot_1 (code, op0, op1, label_rtx_for_bb (false_edge->dest),
2146 false_edge->probability);
2147 maybe_dump_rtl_for_gimple_stmt (stmt, last);
2148 if (false_edge->goto_locus != UNKNOWN_LOCATION)
2149 set_curr_insn_location (false_edge->goto_locus);
2150 true_edge->flags |= EDGE_FALLTHRU;
2151 maybe_cleanup_end_of_block (true_edge, last);
2152 return NULL;
2155 jumpif_1 (code, op0, op1, label_rtx_for_bb (true_edge->dest),
2156 true_edge->probability);
2157 last = get_last_insn ();
2158 if (false_edge->goto_locus != UNKNOWN_LOCATION)
2159 set_curr_insn_location (false_edge->goto_locus);
2160 emit_jump (label_rtx_for_bb (false_edge->dest));
2162 BB_END (bb) = last;
2163 if (BARRIER_P (BB_END (bb)))
2164 BB_END (bb) = PREV_INSN (BB_END (bb));
2165 update_bb_for_insn (bb);
2167 new_bb = create_basic_block (NEXT_INSN (last), get_last_insn (), bb);
2168 dest = false_edge->dest;
2169 redirect_edge_succ (false_edge, new_bb);
2170 false_edge->flags |= EDGE_FALLTHRU;
2171 new_bb->count = false_edge->count;
2172 new_bb->frequency = EDGE_FREQUENCY (false_edge);
2173 add_bb_to_loop (new_bb, bb->loop_father);
2174 new_edge = make_edge (new_bb, dest, 0);
2175 new_edge->probability = REG_BR_PROB_BASE;
2176 new_edge->count = new_bb->count;
2177 if (BARRIER_P (BB_END (new_bb)))
2178 BB_END (new_bb) = PREV_INSN (BB_END (new_bb));
2179 update_bb_for_insn (new_bb);
2181 maybe_dump_rtl_for_gimple_stmt (stmt, last2);
2183 if (true_edge->goto_locus != UNKNOWN_LOCATION)
2185 set_curr_insn_location (true_edge->goto_locus);
2186 true_edge->goto_locus = curr_insn_location ();
2189 return new_bb;
2192 /* Mark all calls that can have a transaction restart. */
2194 static void
2195 mark_transaction_restart_calls (gimple stmt)
2197 struct tm_restart_node dummy;
2198 void **slot;
2200 if (!cfun->gimple_df->tm_restart)
2201 return;
2203 dummy.stmt = stmt;
2204 slot = htab_find_slot (cfun->gimple_df->tm_restart, &dummy, NO_INSERT);
2205 if (slot)
2207 struct tm_restart_node *n = (struct tm_restart_node *) *slot;
2208 tree list = n->label_or_list;
2209 rtx insn;
2211 for (insn = next_real_insn (get_last_insn ());
2212 !CALL_P (insn);
2213 insn = next_real_insn (insn))
2214 continue;
2216 if (TREE_CODE (list) == LABEL_DECL)
2217 add_reg_note (insn, REG_TM, label_rtx (list));
2218 else
2219 for (; list ; list = TREE_CHAIN (list))
2220 add_reg_note (insn, REG_TM, label_rtx (TREE_VALUE (list)));
2224 /* A subroutine of expand_gimple_stmt_1, expanding one GIMPLE_CALL
2225 statement STMT. */
2227 static void
2228 expand_call_stmt (gimple stmt)
2230 tree exp, decl, lhs;
2231 bool builtin_p;
2232 size_t i;
2234 if (gimple_call_internal_p (stmt))
2236 expand_internal_call (stmt);
2237 return;
2240 exp = build_vl_exp (CALL_EXPR, gimple_call_num_args (stmt) + 3);
2242 CALL_EXPR_FN (exp) = gimple_call_fn (stmt);
2243 decl = gimple_call_fndecl (stmt);
2244 builtin_p = decl && DECL_BUILT_IN (decl);
2246 /* If this is not a builtin function, the function type through which the
2247 call is made may be different from the type of the function. */
2248 if (!builtin_p)
2249 CALL_EXPR_FN (exp)
2250 = fold_convert (build_pointer_type (gimple_call_fntype (stmt)),
2251 CALL_EXPR_FN (exp));
2253 TREE_TYPE (exp) = gimple_call_return_type (stmt);
2254 CALL_EXPR_STATIC_CHAIN (exp) = gimple_call_chain (stmt);
2256 for (i = 0; i < gimple_call_num_args (stmt); i++)
2258 tree arg = gimple_call_arg (stmt, i);
2259 gimple def;
2260 /* TER addresses into arguments of builtin functions so we have a
2261 chance to infer more correct alignment information. See PR39954. */
2262 if (builtin_p
2263 && TREE_CODE (arg) == SSA_NAME
2264 && (def = get_gimple_for_ssa_name (arg))
2265 && gimple_assign_rhs_code (def) == ADDR_EXPR)
2266 arg = gimple_assign_rhs1 (def);
2267 CALL_EXPR_ARG (exp, i) = arg;
2270 if (gimple_has_side_effects (stmt))
2271 TREE_SIDE_EFFECTS (exp) = 1;
2273 if (gimple_call_nothrow_p (stmt))
2274 TREE_NOTHROW (exp) = 1;
2276 CALL_EXPR_TAILCALL (exp) = gimple_call_tail_p (stmt);
2277 CALL_EXPR_RETURN_SLOT_OPT (exp) = gimple_call_return_slot_opt_p (stmt);
2278 if (decl
2279 && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL
2280 && (DECL_FUNCTION_CODE (decl) == BUILT_IN_ALLOCA
2281 || DECL_FUNCTION_CODE (decl) == BUILT_IN_ALLOCA_WITH_ALIGN))
2282 CALL_ALLOCA_FOR_VAR_P (exp) = gimple_call_alloca_for_var_p (stmt);
2283 else
2284 CALL_FROM_THUNK_P (exp) = gimple_call_from_thunk_p (stmt);
2285 CALL_EXPR_VA_ARG_PACK (exp) = gimple_call_va_arg_pack_p (stmt);
2286 SET_EXPR_LOCATION (exp, gimple_location (stmt));
2288 /* Ensure RTL is created for debug args. */
2289 if (decl && DECL_HAS_DEBUG_ARGS_P (decl))
2291 vec<tree, va_gc> **debug_args = decl_debug_args_lookup (decl);
2292 unsigned int ix;
2293 tree dtemp;
2295 if (debug_args)
2296 for (ix = 1; (*debug_args)->iterate (ix, &dtemp); ix += 2)
2298 gcc_assert (TREE_CODE (dtemp) == DEBUG_EXPR_DECL);
2299 expand_debug_expr (dtemp);
2303 lhs = gimple_call_lhs (stmt);
2304 if (lhs)
2305 expand_assignment (lhs, exp, false);
2306 else
2307 expand_expr (exp, const0_rtx, VOIDmode, EXPAND_NORMAL);
2309 mark_transaction_restart_calls (stmt);
2313 /* Generate RTL for an asm statement (explicit assembler code).
2314 STRING is a STRING_CST node containing the assembler code text,
2315 or an ADDR_EXPR containing a STRING_CST. VOL nonzero means the
2316 insn is volatile; don't optimize it. */
2318 static void
2319 expand_asm_loc (tree string, int vol, location_t locus)
2321 rtx body;
2323 if (TREE_CODE (string) == ADDR_EXPR)
2324 string = TREE_OPERAND (string, 0);
2326 body = gen_rtx_ASM_INPUT_loc (VOIDmode,
2327 ggc_strdup (TREE_STRING_POINTER (string)),
2328 locus);
2330 MEM_VOLATILE_P (body) = vol;
2332 emit_insn (body);
2335 /* Return the number of times character C occurs in string S. */
2336 static int
2337 n_occurrences (int c, const char *s)
2339 int n = 0;
2340 while (*s)
2341 n += (*s++ == c);
2342 return n;
2345 /* A subroutine of expand_asm_operands. Check that all operands have
2346 the same number of alternatives. Return true if so. */
2348 static bool
2349 check_operand_nalternatives (tree outputs, tree inputs)
2351 if (outputs || inputs)
2353 tree tmp = TREE_PURPOSE (outputs ? outputs : inputs);
2354 int nalternatives
2355 = n_occurrences (',', TREE_STRING_POINTER (TREE_VALUE (tmp)));
2356 tree next = inputs;
2358 if (nalternatives + 1 > MAX_RECOG_ALTERNATIVES)
2360 error ("too many alternatives in %<asm%>");
2361 return false;
2364 tmp = outputs;
2365 while (tmp)
2367 const char *constraint
2368 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tmp)));
2370 if (n_occurrences (',', constraint) != nalternatives)
2372 error ("operand constraints for %<asm%> differ "
2373 "in number of alternatives");
2374 return false;
2377 if (TREE_CHAIN (tmp))
2378 tmp = TREE_CHAIN (tmp);
2379 else
2380 tmp = next, next = 0;
2384 return true;
2387 /* Check for overlap between registers marked in CLOBBERED_REGS and
2388 anything inappropriate in T. Emit error and return the register
2389 variable definition for error, NULL_TREE for ok. */
2391 static bool
2392 tree_conflicts_with_clobbers_p (tree t, HARD_REG_SET *clobbered_regs)
2394 /* Conflicts between asm-declared register variables and the clobber
2395 list are not allowed. */
2396 tree overlap = tree_overlaps_hard_reg_set (t, clobbered_regs);
2398 if (overlap)
2400 error ("asm-specifier for variable %qE conflicts with asm clobber list",
2401 DECL_NAME (overlap));
2403 /* Reset registerness to stop multiple errors emitted for a single
2404 variable. */
2405 DECL_REGISTER (overlap) = 0;
2406 return true;
2409 return false;
2412 /* Generate RTL for an asm statement with arguments.
2413 STRING is the instruction template.
2414 OUTPUTS is a list of output arguments (lvalues); INPUTS a list of inputs.
2415 Each output or input has an expression in the TREE_VALUE and
2416 a tree list in TREE_PURPOSE which in turn contains a constraint
2417 name in TREE_VALUE (or NULL_TREE) and a constraint string
2418 in TREE_PURPOSE.
2419 CLOBBERS is a list of STRING_CST nodes each naming a hard register
2420 that is clobbered by this insn.
2422 LABELS is a list of labels, and if LABELS is non-NULL, FALLTHRU_BB
2423 should be the fallthru basic block of the asm goto.
2425 Not all kinds of lvalue that may appear in OUTPUTS can be stored directly.
2426 Some elements of OUTPUTS may be replaced with trees representing temporary
2427 values. The caller should copy those temporary values to the originally
2428 specified lvalues.
2430 VOL nonzero means the insn is volatile; don't optimize it. */
2432 static void
2433 expand_asm_operands (tree string, tree outputs, tree inputs,
2434 tree clobbers, tree labels, basic_block fallthru_bb,
2435 int vol, location_t locus)
2437 rtvec argvec, constraintvec, labelvec;
2438 rtx body;
2439 int ninputs = list_length (inputs);
2440 int noutputs = list_length (outputs);
2441 int nlabels = list_length (labels);
2442 int ninout;
2443 int nclobbers;
2444 HARD_REG_SET clobbered_regs;
2445 int clobber_conflict_found = 0;
2446 tree tail;
2447 tree t;
2448 int i;
2449 /* Vector of RTX's of evaluated output operands. */
2450 rtx *output_rtx = XALLOCAVEC (rtx, noutputs);
2451 int *inout_opnum = XALLOCAVEC (int, noutputs);
2452 rtx *real_output_rtx = XALLOCAVEC (rtx, noutputs);
2453 enum machine_mode *inout_mode = XALLOCAVEC (enum machine_mode, noutputs);
2454 const char **constraints = XALLOCAVEC (const char *, noutputs + ninputs);
2455 int old_generating_concat_p = generating_concat_p;
2456 rtx fallthru_label = NULL_RTX;
2458 /* An ASM with no outputs needs to be treated as volatile, for now. */
2459 if (noutputs == 0)
2460 vol = 1;
2462 if (! check_operand_nalternatives (outputs, inputs))
2463 return;
2465 string = resolve_asm_operand_names (string, outputs, inputs, labels);
2467 /* Collect constraints. */
2468 i = 0;
2469 for (t = outputs; t ; t = TREE_CHAIN (t), i++)
2470 constraints[i] = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
2471 for (t = inputs; t ; t = TREE_CHAIN (t), i++)
2472 constraints[i] = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
2474 /* Sometimes we wish to automatically clobber registers across an asm.
2475 Case in point is when the i386 backend moved from cc0 to a hard reg --
2476 maintaining source-level compatibility means automatically clobbering
2477 the flags register. */
2478 clobbers = targetm.md_asm_clobbers (outputs, inputs, clobbers);
2480 /* Count the number of meaningful clobbered registers, ignoring what
2481 we would ignore later. */
2482 nclobbers = 0;
2483 CLEAR_HARD_REG_SET (clobbered_regs);
2484 for (tail = clobbers; tail; tail = TREE_CHAIN (tail))
2486 const char *regname;
2487 int nregs;
2489 if (TREE_VALUE (tail) == error_mark_node)
2490 return;
2491 regname = TREE_STRING_POINTER (TREE_VALUE (tail));
2493 i = decode_reg_name_and_count (regname, &nregs);
2494 if (i == -4)
2495 ++nclobbers;
2496 else if (i == -2)
2497 error ("unknown register name %qs in %<asm%>", regname);
2499 /* Mark clobbered registers. */
2500 if (i >= 0)
2502 int reg;
2504 for (reg = i; reg < i + nregs; reg++)
2506 ++nclobbers;
2508 /* Clobbering the PIC register is an error. */
2509 if (reg == (int) PIC_OFFSET_TABLE_REGNUM)
2511 error ("PIC register clobbered by %qs in %<asm%>", regname);
2512 return;
2515 SET_HARD_REG_BIT (clobbered_regs, reg);
2520 /* First pass over inputs and outputs checks validity and sets
2521 mark_addressable if needed. */
2523 ninout = 0;
2524 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
2526 tree val = TREE_VALUE (tail);
2527 tree type = TREE_TYPE (val);
2528 const char *constraint;
2529 bool is_inout;
2530 bool allows_reg;
2531 bool allows_mem;
2533 /* If there's an erroneous arg, emit no insn. */
2534 if (type == error_mark_node)
2535 return;
2537 /* Try to parse the output constraint. If that fails, there's
2538 no point in going further. */
2539 constraint = constraints[i];
2540 if (!parse_output_constraint (&constraint, i, ninputs, noutputs,
2541 &allows_mem, &allows_reg, &is_inout))
2542 return;
2544 if (! allows_reg
2545 && (allows_mem
2546 || is_inout
2547 || (DECL_P (val)
2548 && REG_P (DECL_RTL (val))
2549 && GET_MODE (DECL_RTL (val)) != TYPE_MODE (type))))
2550 mark_addressable (val);
2552 if (is_inout)
2553 ninout++;
2556 ninputs += ninout;
2557 if (ninputs + noutputs > MAX_RECOG_OPERANDS)
2559 error ("more than %d operands in %<asm%>", MAX_RECOG_OPERANDS);
2560 return;
2563 for (i = 0, tail = inputs; tail; i++, tail = TREE_CHAIN (tail))
2565 bool allows_reg, allows_mem;
2566 const char *constraint;
2568 /* If there's an erroneous arg, emit no insn, because the ASM_INPUT
2569 would get VOIDmode and that could cause a crash in reload. */
2570 if (TREE_TYPE (TREE_VALUE (tail)) == error_mark_node)
2571 return;
2573 constraint = constraints[i + noutputs];
2574 if (! parse_input_constraint (&constraint, i, ninputs, noutputs, ninout,
2575 constraints, &allows_mem, &allows_reg))
2576 return;
2578 if (! allows_reg && allows_mem)
2579 mark_addressable (TREE_VALUE (tail));
2582 /* Second pass evaluates arguments. */
2584 /* Make sure stack is consistent for asm goto. */
2585 if (nlabels > 0)
2586 do_pending_stack_adjust ();
2588 ninout = 0;
2589 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
2591 tree val = TREE_VALUE (tail);
2592 tree type = TREE_TYPE (val);
2593 bool is_inout;
2594 bool allows_reg;
2595 bool allows_mem;
2596 rtx op;
2597 bool ok;
2599 ok = parse_output_constraint (&constraints[i], i, ninputs,
2600 noutputs, &allows_mem, &allows_reg,
2601 &is_inout);
2602 gcc_assert (ok);
2604 /* If an output operand is not a decl or indirect ref and our constraint
2605 allows a register, make a temporary to act as an intermediate.
2606 Make the asm insn write into that, then our caller will copy it to
2607 the real output operand. Likewise for promoted variables. */
2609 generating_concat_p = 0;
2611 real_output_rtx[i] = NULL_RTX;
2612 if ((TREE_CODE (val) == INDIRECT_REF
2613 && allows_mem)
2614 || (DECL_P (val)
2615 && (allows_mem || REG_P (DECL_RTL (val)))
2616 && ! (REG_P (DECL_RTL (val))
2617 && GET_MODE (DECL_RTL (val)) != TYPE_MODE (type)))
2618 || ! allows_reg
2619 || is_inout)
2621 op = expand_expr (val, NULL_RTX, VOIDmode,
2622 !allows_reg ? EXPAND_MEMORY : EXPAND_WRITE);
2623 if (MEM_P (op))
2624 op = validize_mem (op);
2626 if (! allows_reg && !MEM_P (op))
2627 error ("output number %d not directly addressable", i);
2628 if ((! allows_mem && MEM_P (op))
2629 || GET_CODE (op) == CONCAT)
2631 real_output_rtx[i] = op;
2632 op = gen_reg_rtx (GET_MODE (op));
2633 if (is_inout)
2634 emit_move_insn (op, real_output_rtx[i]);
2637 else
2639 op = assign_temp (type, 0, 1);
2640 op = validize_mem (op);
2641 if (!MEM_P (op) && TREE_CODE (TREE_VALUE (tail)) == SSA_NAME)
2642 set_reg_attrs_for_decl_rtl (SSA_NAME_VAR (TREE_VALUE (tail)), op);
2643 TREE_VALUE (tail) = make_tree (type, op);
2645 output_rtx[i] = op;
2647 generating_concat_p = old_generating_concat_p;
2649 if (is_inout)
2651 inout_mode[ninout] = TYPE_MODE (type);
2652 inout_opnum[ninout++] = i;
2655 if (tree_conflicts_with_clobbers_p (val, &clobbered_regs))
2656 clobber_conflict_found = 1;
2659 /* Make vectors for the expression-rtx, constraint strings,
2660 and named operands. */
2662 argvec = rtvec_alloc (ninputs);
2663 constraintvec = rtvec_alloc (ninputs);
2664 labelvec = rtvec_alloc (nlabels);
2666 body = gen_rtx_ASM_OPERANDS ((noutputs == 0 ? VOIDmode
2667 : GET_MODE (output_rtx[0])),
2668 ggc_strdup (TREE_STRING_POINTER (string)),
2669 empty_string, 0, argvec, constraintvec,
2670 labelvec, locus);
2672 MEM_VOLATILE_P (body) = vol;
2674 /* Eval the inputs and put them into ARGVEC.
2675 Put their constraints into ASM_INPUTs and store in CONSTRAINTS. */
2677 for (i = 0, tail = inputs; tail; tail = TREE_CHAIN (tail), ++i)
2679 bool allows_reg, allows_mem;
2680 const char *constraint;
2681 tree val, type;
2682 rtx op;
2683 bool ok;
2685 constraint = constraints[i + noutputs];
2686 ok = parse_input_constraint (&constraint, i, ninputs, noutputs, ninout,
2687 constraints, &allows_mem, &allows_reg);
2688 gcc_assert (ok);
2690 generating_concat_p = 0;
2692 val = TREE_VALUE (tail);
2693 type = TREE_TYPE (val);
2694 /* EXPAND_INITIALIZER will not generate code for valid initializer
2695 constants, but will still generate code for other types of operand.
2696 This is the behavior we want for constant constraints. */
2697 op = expand_expr (val, NULL_RTX, VOIDmode,
2698 allows_reg ? EXPAND_NORMAL
2699 : allows_mem ? EXPAND_MEMORY
2700 : EXPAND_INITIALIZER);
2702 /* Never pass a CONCAT to an ASM. */
2703 if (GET_CODE (op) == CONCAT)
2704 op = force_reg (GET_MODE (op), op);
2705 else if (MEM_P (op))
2706 op = validize_mem (op);
2708 if (asm_operand_ok (op, constraint, NULL) <= 0)
2710 if (allows_reg && TYPE_MODE (type) != BLKmode)
2711 op = force_reg (TYPE_MODE (type), op);
2712 else if (!allows_mem)
2713 warning (0, "asm operand %d probably doesn%'t match constraints",
2714 i + noutputs);
2715 else if (MEM_P (op))
2717 /* We won't recognize either volatile memory or memory
2718 with a queued address as available a memory_operand
2719 at this point. Ignore it: clearly this *is* a memory. */
2721 else
2722 gcc_unreachable ();
2725 generating_concat_p = old_generating_concat_p;
2726 ASM_OPERANDS_INPUT (body, i) = op;
2728 ASM_OPERANDS_INPUT_CONSTRAINT_EXP (body, i)
2729 = gen_rtx_ASM_INPUT_loc (TYPE_MODE (type),
2730 ggc_strdup (constraints[i + noutputs]),
2731 locus);
2733 if (tree_conflicts_with_clobbers_p (val, &clobbered_regs))
2734 clobber_conflict_found = 1;
2737 /* Protect all the operands from the queue now that they have all been
2738 evaluated. */
2740 generating_concat_p = 0;
2742 /* For in-out operands, copy output rtx to input rtx. */
2743 for (i = 0; i < ninout; i++)
2745 int j = inout_opnum[i];
2746 char buffer[16];
2748 ASM_OPERANDS_INPUT (body, ninputs - ninout + i)
2749 = output_rtx[j];
2751 sprintf (buffer, "%d", j);
2752 ASM_OPERANDS_INPUT_CONSTRAINT_EXP (body, ninputs - ninout + i)
2753 = gen_rtx_ASM_INPUT_loc (inout_mode[i], ggc_strdup (buffer), locus);
2756 /* Copy labels to the vector. */
2757 for (i = 0, tail = labels; i < nlabels; ++i, tail = TREE_CHAIN (tail))
2759 rtx r;
2760 /* If asm goto has any labels in the fallthru basic block, use
2761 a label that we emit immediately after the asm goto. Expansion
2762 may insert further instructions into the same basic block after
2763 asm goto and if we don't do this, insertion of instructions on
2764 the fallthru edge might misbehave. See PR58670. */
2765 if (fallthru_bb
2766 && label_to_block_fn (cfun, TREE_VALUE (tail)) == fallthru_bb)
2768 if (fallthru_label == NULL_RTX)
2769 fallthru_label = gen_label_rtx ();
2770 r = fallthru_label;
2772 else
2773 r = label_rtx (TREE_VALUE (tail));
2774 ASM_OPERANDS_LABEL (body, i) = gen_rtx_LABEL_REF (Pmode, r);
2777 generating_concat_p = old_generating_concat_p;
2779 /* Now, for each output, construct an rtx
2780 (set OUTPUT (asm_operands INSN OUTPUTCONSTRAINT OUTPUTNUMBER
2781 ARGVEC CONSTRAINTS OPNAMES))
2782 If there is more than one, put them inside a PARALLEL. */
2784 if (nlabels > 0 && nclobbers == 0)
2786 gcc_assert (noutputs == 0);
2787 emit_jump_insn (body);
2789 else if (noutputs == 0 && nclobbers == 0)
2791 /* No output operands: put in a raw ASM_OPERANDS rtx. */
2792 emit_insn (body);
2794 else if (noutputs == 1 && nclobbers == 0)
2796 ASM_OPERANDS_OUTPUT_CONSTRAINT (body) = ggc_strdup (constraints[0]);
2797 emit_insn (gen_rtx_SET (VOIDmode, output_rtx[0], body));
2799 else
2801 rtx obody = body;
2802 int num = noutputs;
2804 if (num == 0)
2805 num = 1;
2807 body = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (num + nclobbers));
2809 /* For each output operand, store a SET. */
2810 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
2812 XVECEXP (body, 0, i)
2813 = gen_rtx_SET (VOIDmode,
2814 output_rtx[i],
2815 gen_rtx_ASM_OPERANDS
2816 (GET_MODE (output_rtx[i]),
2817 ggc_strdup (TREE_STRING_POINTER (string)),
2818 ggc_strdup (constraints[i]),
2819 i, argvec, constraintvec, labelvec, locus));
2821 MEM_VOLATILE_P (SET_SRC (XVECEXP (body, 0, i))) = vol;
2824 /* If there are no outputs (but there are some clobbers)
2825 store the bare ASM_OPERANDS into the PARALLEL. */
2827 if (i == 0)
2828 XVECEXP (body, 0, i++) = obody;
2830 /* Store (clobber REG) for each clobbered register specified. */
2832 for (tail = clobbers; tail; tail = TREE_CHAIN (tail))
2834 const char *regname = TREE_STRING_POINTER (TREE_VALUE (tail));
2835 int reg, nregs;
2836 int j = decode_reg_name_and_count (regname, &nregs);
2837 rtx clobbered_reg;
2839 if (j < 0)
2841 if (j == -3) /* `cc', which is not a register */
2842 continue;
2844 if (j == -4) /* `memory', don't cache memory across asm */
2846 XVECEXP (body, 0, i++)
2847 = gen_rtx_CLOBBER (VOIDmode,
2848 gen_rtx_MEM
2849 (BLKmode,
2850 gen_rtx_SCRATCH (VOIDmode)));
2851 continue;
2854 /* Ignore unknown register, error already signaled. */
2855 continue;
2858 for (reg = j; reg < j + nregs; reg++)
2860 /* Use QImode since that's guaranteed to clobber just
2861 * one reg. */
2862 clobbered_reg = gen_rtx_REG (QImode, reg);
2864 /* Do sanity check for overlap between clobbers and
2865 respectively input and outputs that hasn't been
2866 handled. Such overlap should have been detected and
2867 reported above. */
2868 if (!clobber_conflict_found)
2870 int opno;
2872 /* We test the old body (obody) contents to avoid
2873 tripping over the under-construction body. */
2874 for (opno = 0; opno < noutputs; opno++)
2875 if (reg_overlap_mentioned_p (clobbered_reg,
2876 output_rtx[opno]))
2877 internal_error
2878 ("asm clobber conflict with output operand");
2880 for (opno = 0; opno < ninputs - ninout; opno++)
2881 if (reg_overlap_mentioned_p (clobbered_reg,
2882 ASM_OPERANDS_INPUT (obody,
2883 opno)))
2884 internal_error
2885 ("asm clobber conflict with input operand");
2888 XVECEXP (body, 0, i++)
2889 = gen_rtx_CLOBBER (VOIDmode, clobbered_reg);
2893 if (nlabels > 0)
2894 emit_jump_insn (body);
2895 else
2896 emit_insn (body);
2899 if (fallthru_label)
2900 emit_label (fallthru_label);
2902 /* For any outputs that needed reloading into registers, spill them
2903 back to where they belong. */
2904 for (i = 0; i < noutputs; ++i)
2905 if (real_output_rtx[i])
2906 emit_move_insn (real_output_rtx[i], output_rtx[i]);
2908 crtl->has_asm_statement = 1;
2909 free_temp_slots ();
2913 static void
2914 expand_asm_stmt (gimple stmt)
2916 int noutputs;
2917 tree outputs, tail, t;
2918 tree *o;
2919 size_t i, n;
2920 const char *s;
2921 tree str, out, in, cl, labels;
2922 location_t locus = gimple_location (stmt);
2923 basic_block fallthru_bb = NULL;
2925 /* Meh... convert the gimple asm operands into real tree lists.
2926 Eventually we should make all routines work on the vectors instead
2927 of relying on TREE_CHAIN. */
2928 out = NULL_TREE;
2929 n = gimple_asm_noutputs (stmt);
2930 if (n > 0)
2932 t = out = gimple_asm_output_op (stmt, 0);
2933 for (i = 1; i < n; i++)
2934 t = TREE_CHAIN (t) = gimple_asm_output_op (stmt, i);
2937 in = NULL_TREE;
2938 n = gimple_asm_ninputs (stmt);
2939 if (n > 0)
2941 t = in = gimple_asm_input_op (stmt, 0);
2942 for (i = 1; i < n; i++)
2943 t = TREE_CHAIN (t) = gimple_asm_input_op (stmt, i);
2946 cl = NULL_TREE;
2947 n = gimple_asm_nclobbers (stmt);
2948 if (n > 0)
2950 t = cl = gimple_asm_clobber_op (stmt, 0);
2951 for (i = 1; i < n; i++)
2952 t = TREE_CHAIN (t) = gimple_asm_clobber_op (stmt, i);
2955 labels = NULL_TREE;
2956 n = gimple_asm_nlabels (stmt);
2957 if (n > 0)
2959 edge fallthru = find_fallthru_edge (gimple_bb (stmt)->succs);
2960 if (fallthru)
2961 fallthru_bb = fallthru->dest;
2962 t = labels = gimple_asm_label_op (stmt, 0);
2963 for (i = 1; i < n; i++)
2964 t = TREE_CHAIN (t) = gimple_asm_label_op (stmt, i);
2967 s = gimple_asm_string (stmt);
2968 str = build_string (strlen (s), s);
2970 if (gimple_asm_input_p (stmt))
2972 expand_asm_loc (str, gimple_asm_volatile_p (stmt), locus);
2973 return;
2976 outputs = out;
2977 noutputs = gimple_asm_noutputs (stmt);
2978 /* o[I] is the place that output number I should be written. */
2979 o = (tree *) alloca (noutputs * sizeof (tree));
2981 /* Record the contents of OUTPUTS before it is modified. */
2982 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
2983 o[i] = TREE_VALUE (tail);
2985 /* Generate the ASM_OPERANDS insn; store into the TREE_VALUEs of
2986 OUTPUTS some trees for where the values were actually stored. */
2987 expand_asm_operands (str, outputs, in, cl, labels, fallthru_bb,
2988 gimple_asm_volatile_p (stmt), locus);
2990 /* Copy all the intermediate outputs into the specified outputs. */
2991 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
2993 if (o[i] != TREE_VALUE (tail))
2995 expand_assignment (o[i], TREE_VALUE (tail), false);
2996 free_temp_slots ();
2998 /* Restore the original value so that it's correct the next
2999 time we expand this function. */
3000 TREE_VALUE (tail) = o[i];
3005 /* Emit code to jump to the address
3006 specified by the pointer expression EXP. */
3008 static void
3009 expand_computed_goto (tree exp)
3011 rtx x = expand_normal (exp);
3013 x = convert_memory_address (Pmode, x);
3015 do_pending_stack_adjust ();
3016 emit_indirect_jump (x);
3019 /* Generate RTL code for a `goto' statement with target label LABEL.
3020 LABEL should be a LABEL_DECL tree node that was or will later be
3021 defined with `expand_label'. */
3023 static void
3024 expand_goto (tree label)
3026 #ifdef ENABLE_CHECKING
3027 /* Check for a nonlocal goto to a containing function. Should have
3028 gotten translated to __builtin_nonlocal_goto. */
3029 tree context = decl_function_context (label);
3030 gcc_assert (!context || context == current_function_decl);
3031 #endif
3033 emit_jump (label_rtx (label));
3036 /* Output a return with no value. */
3038 static void
3039 expand_null_return_1 (void)
3041 clear_pending_stack_adjust ();
3042 do_pending_stack_adjust ();
3043 emit_jump (return_label);
3046 /* Generate RTL to return from the current function, with no value.
3047 (That is, we do not do anything about returning any value.) */
3049 void
3050 expand_null_return (void)
3052 /* If this function was declared to return a value, but we
3053 didn't, clobber the return registers so that they are not
3054 propagated live to the rest of the function. */
3055 clobber_return_register ();
3057 expand_null_return_1 ();
3060 /* Generate RTL to return from the current function, with value VAL. */
3062 static void
3063 expand_value_return (rtx val)
3065 /* Copy the value to the return location unless it's already there. */
3067 tree decl = DECL_RESULT (current_function_decl);
3068 rtx return_reg = DECL_RTL (decl);
3069 if (return_reg != val)
3071 tree funtype = TREE_TYPE (current_function_decl);
3072 tree type = TREE_TYPE (decl);
3073 int unsignedp = TYPE_UNSIGNED (type);
3074 enum machine_mode old_mode = DECL_MODE (decl);
3075 enum machine_mode mode;
3076 if (DECL_BY_REFERENCE (decl))
3077 mode = promote_function_mode (type, old_mode, &unsignedp, funtype, 2);
3078 else
3079 mode = promote_function_mode (type, old_mode, &unsignedp, funtype, 1);
3081 if (mode != old_mode)
3082 val = convert_modes (mode, old_mode, val, unsignedp);
3084 if (GET_CODE (return_reg) == PARALLEL)
3085 emit_group_load (return_reg, val, type, int_size_in_bytes (type));
3086 else
3087 emit_move_insn (return_reg, val);
3090 expand_null_return_1 ();
3093 /* Generate RTL to evaluate the expression RETVAL and return it
3094 from the current function. */
3096 static void
3097 expand_return (tree retval)
3099 rtx result_rtl;
3100 rtx val = 0;
3101 tree retval_rhs;
3103 /* If function wants no value, give it none. */
3104 if (TREE_CODE (TREE_TYPE (TREE_TYPE (current_function_decl))) == VOID_TYPE)
3106 expand_normal (retval);
3107 expand_null_return ();
3108 return;
3111 if (retval == error_mark_node)
3113 /* Treat this like a return of no value from a function that
3114 returns a value. */
3115 expand_null_return ();
3116 return;
3118 else if ((TREE_CODE (retval) == MODIFY_EXPR
3119 || TREE_CODE (retval) == INIT_EXPR)
3120 && TREE_CODE (TREE_OPERAND (retval, 0)) == RESULT_DECL)
3121 retval_rhs = TREE_OPERAND (retval, 1);
3122 else
3123 retval_rhs = retval;
3125 result_rtl = DECL_RTL (DECL_RESULT (current_function_decl));
3127 /* If we are returning the RESULT_DECL, then the value has already
3128 been stored into it, so we don't have to do anything special. */
3129 if (TREE_CODE (retval_rhs) == RESULT_DECL)
3130 expand_value_return (result_rtl);
3132 /* If the result is an aggregate that is being returned in one (or more)
3133 registers, load the registers here. */
3135 else if (retval_rhs != 0
3136 && TYPE_MODE (TREE_TYPE (retval_rhs)) == BLKmode
3137 && REG_P (result_rtl))
3139 val = copy_blkmode_to_reg (GET_MODE (result_rtl), retval_rhs);
3140 if (val)
3142 /* Use the mode of the result value on the return register. */
3143 PUT_MODE (result_rtl, GET_MODE (val));
3144 expand_value_return (val);
3146 else
3147 expand_null_return ();
3149 else if (retval_rhs != 0
3150 && !VOID_TYPE_P (TREE_TYPE (retval_rhs))
3151 && (REG_P (result_rtl)
3152 || (GET_CODE (result_rtl) == PARALLEL)))
3154 /* Compute the return value into a temporary (usually a pseudo reg). */
3156 = assign_temp (TREE_TYPE (DECL_RESULT (current_function_decl)), 0, 1);
3157 val = expand_expr (retval_rhs, val, GET_MODE (val), EXPAND_NORMAL);
3158 val = force_not_mem (val);
3159 expand_value_return (val);
3161 else
3163 /* No hard reg used; calculate value into hard return reg. */
3164 expand_expr (retval, const0_rtx, VOIDmode, EXPAND_NORMAL);
3165 expand_value_return (result_rtl);
3169 /* A subroutine of expand_gimple_stmt, expanding one gimple statement
3170 STMT that doesn't require special handling for outgoing edges. That
3171 is no tailcalls and no GIMPLE_COND. */
3173 static void
3174 expand_gimple_stmt_1 (gimple stmt)
3176 tree op0;
3178 set_curr_insn_location (gimple_location (stmt));
3180 switch (gimple_code (stmt))
3182 case GIMPLE_GOTO:
3183 op0 = gimple_goto_dest (stmt);
3184 if (TREE_CODE (op0) == LABEL_DECL)
3185 expand_goto (op0);
3186 else
3187 expand_computed_goto (op0);
3188 break;
3189 case GIMPLE_LABEL:
3190 expand_label (gimple_label_label (stmt));
3191 break;
3192 case GIMPLE_NOP:
3193 case GIMPLE_PREDICT:
3194 break;
3195 case GIMPLE_SWITCH:
3196 expand_case (stmt);
3197 break;
3198 case GIMPLE_ASM:
3199 expand_asm_stmt (stmt);
3200 break;
3201 case GIMPLE_CALL:
3202 expand_call_stmt (stmt);
3203 break;
3205 case GIMPLE_RETURN:
3206 op0 = gimple_return_retval (stmt);
3208 if (op0 && op0 != error_mark_node)
3210 tree result = DECL_RESULT (current_function_decl);
3212 /* If we are not returning the current function's RESULT_DECL,
3213 build an assignment to it. */
3214 if (op0 != result)
3216 /* I believe that a function's RESULT_DECL is unique. */
3217 gcc_assert (TREE_CODE (op0) != RESULT_DECL);
3219 /* ??? We'd like to use simply expand_assignment here,
3220 but this fails if the value is of BLKmode but the return
3221 decl is a register. expand_return has special handling
3222 for this combination, which eventually should move
3223 to common code. See comments there. Until then, let's
3224 build a modify expression :-/ */
3225 op0 = build2 (MODIFY_EXPR, TREE_TYPE (result),
3226 result, op0);
3229 if (!op0)
3230 expand_null_return ();
3231 else
3232 expand_return (op0);
3233 break;
3235 case GIMPLE_ASSIGN:
3237 tree lhs = gimple_assign_lhs (stmt);
3239 /* Tree expand used to fiddle with |= and &= of two bitfield
3240 COMPONENT_REFs here. This can't happen with gimple, the LHS
3241 of binary assigns must be a gimple reg. */
3243 if (TREE_CODE (lhs) != SSA_NAME
3244 || get_gimple_rhs_class (gimple_expr_code (stmt))
3245 == GIMPLE_SINGLE_RHS)
3247 tree rhs = gimple_assign_rhs1 (stmt);
3248 gcc_assert (get_gimple_rhs_class (gimple_expr_code (stmt))
3249 == GIMPLE_SINGLE_RHS);
3250 if (gimple_has_location (stmt) && CAN_HAVE_LOCATION_P (rhs))
3251 SET_EXPR_LOCATION (rhs, gimple_location (stmt));
3252 if (TREE_CLOBBER_P (rhs))
3253 /* This is a clobber to mark the going out of scope for
3254 this LHS. */
3256 else
3257 expand_assignment (lhs, rhs,
3258 gimple_assign_nontemporal_move_p (stmt));
3260 else
3262 rtx target, temp;
3263 bool nontemporal = gimple_assign_nontemporal_move_p (stmt);
3264 struct separate_ops ops;
3265 bool promoted = false;
3267 target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
3268 if (GET_CODE (target) == SUBREG && SUBREG_PROMOTED_VAR_P (target))
3269 promoted = true;
3271 ops.code = gimple_assign_rhs_code (stmt);
3272 ops.type = TREE_TYPE (lhs);
3273 switch (get_gimple_rhs_class (gimple_expr_code (stmt)))
3275 case GIMPLE_TERNARY_RHS:
3276 ops.op2 = gimple_assign_rhs3 (stmt);
3277 /* Fallthru */
3278 case GIMPLE_BINARY_RHS:
3279 ops.op1 = gimple_assign_rhs2 (stmt);
3280 /* Fallthru */
3281 case GIMPLE_UNARY_RHS:
3282 ops.op0 = gimple_assign_rhs1 (stmt);
3283 break;
3284 default:
3285 gcc_unreachable ();
3287 ops.location = gimple_location (stmt);
3289 /* If we want to use a nontemporal store, force the value to
3290 register first. If we store into a promoted register,
3291 don't directly expand to target. */
3292 temp = nontemporal || promoted ? NULL_RTX : target;
3293 temp = expand_expr_real_2 (&ops, temp, GET_MODE (target),
3294 EXPAND_NORMAL);
3296 if (temp == target)
3298 else if (promoted)
3300 int unsignedp = SUBREG_PROMOTED_UNSIGNED_P (target);
3301 /* If TEMP is a VOIDmode constant, use convert_modes to make
3302 sure that we properly convert it. */
3303 if (CONSTANT_P (temp) && GET_MODE (temp) == VOIDmode)
3305 temp = convert_modes (GET_MODE (target),
3306 TYPE_MODE (ops.type),
3307 temp, unsignedp);
3308 temp = convert_modes (GET_MODE (SUBREG_REG (target)),
3309 GET_MODE (target), temp, unsignedp);
3312 convert_move (SUBREG_REG (target), temp, unsignedp);
3314 else if (nontemporal && emit_storent_insn (target, temp))
3316 else
3318 temp = force_operand (temp, target);
3319 if (temp != target)
3320 emit_move_insn (target, temp);
3324 break;
3326 default:
3327 gcc_unreachable ();
3331 /* Expand one gimple statement STMT and return the last RTL instruction
3332 before any of the newly generated ones.
3334 In addition to generating the necessary RTL instructions this also
3335 sets REG_EH_REGION notes if necessary and sets the current source
3336 location for diagnostics. */
3338 static rtx
3339 expand_gimple_stmt (gimple stmt)
3341 location_t saved_location = input_location;
3342 rtx last = get_last_insn ();
3343 int lp_nr;
3345 gcc_assert (cfun);
3347 /* We need to save and restore the current source location so that errors
3348 discovered during expansion are emitted with the right location. But
3349 it would be better if the diagnostic routines used the source location
3350 embedded in the tree nodes rather than globals. */
3351 if (gimple_has_location (stmt))
3352 input_location = gimple_location (stmt);
3354 expand_gimple_stmt_1 (stmt);
3356 /* Free any temporaries used to evaluate this statement. */
3357 free_temp_slots ();
3359 input_location = saved_location;
3361 /* Mark all insns that may trap. */
3362 lp_nr = lookup_stmt_eh_lp (stmt);
3363 if (lp_nr)
3365 rtx insn;
3366 for (insn = next_real_insn (last); insn;
3367 insn = next_real_insn (insn))
3369 if (! find_reg_note (insn, REG_EH_REGION, NULL_RTX)
3370 /* If we want exceptions for non-call insns, any
3371 may_trap_p instruction may throw. */
3372 && GET_CODE (PATTERN (insn)) != CLOBBER
3373 && GET_CODE (PATTERN (insn)) != USE
3374 && insn_could_throw_p (insn))
3375 make_reg_eh_region_note (insn, 0, lp_nr);
3379 return last;
3382 /* A subroutine of expand_gimple_basic_block. Expand one GIMPLE_CALL
3383 that has CALL_EXPR_TAILCALL set. Returns non-null if we actually
3384 generated a tail call (something that might be denied by the ABI
3385 rules governing the call; see calls.c).
3387 Sets CAN_FALLTHRU if we generated a *conditional* tail call, and
3388 can still reach the rest of BB. The case here is __builtin_sqrt,
3389 where the NaN result goes through the external function (with a
3390 tailcall) and the normal result happens via a sqrt instruction. */
3392 static basic_block
3393 expand_gimple_tailcall (basic_block bb, gimple stmt, bool *can_fallthru)
3395 rtx last2, last;
3396 edge e;
3397 edge_iterator ei;
3398 int probability;
3399 gcov_type count;
3401 last2 = last = expand_gimple_stmt (stmt);
3403 for (last = NEXT_INSN (last); last; last = NEXT_INSN (last))
3404 if (CALL_P (last) && SIBLING_CALL_P (last))
3405 goto found;
3407 maybe_dump_rtl_for_gimple_stmt (stmt, last2);
3409 *can_fallthru = true;
3410 return NULL;
3412 found:
3413 /* ??? Wouldn't it be better to just reset any pending stack adjust?
3414 Any instructions emitted here are about to be deleted. */
3415 do_pending_stack_adjust ();
3417 /* Remove any non-eh, non-abnormal edges that don't go to exit. */
3418 /* ??? I.e. the fallthrough edge. HOWEVER! If there were to be
3419 EH or abnormal edges, we shouldn't have created a tail call in
3420 the first place. So it seems to me we should just be removing
3421 all edges here, or redirecting the existing fallthru edge to
3422 the exit block. */
3424 probability = 0;
3425 count = 0;
3427 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
3429 if (!(e->flags & (EDGE_ABNORMAL | EDGE_EH)))
3431 if (e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun))
3433 e->dest->count -= e->count;
3434 e->dest->frequency -= EDGE_FREQUENCY (e);
3435 if (e->dest->count < 0)
3436 e->dest->count = 0;
3437 if (e->dest->frequency < 0)
3438 e->dest->frequency = 0;
3440 count += e->count;
3441 probability += e->probability;
3442 remove_edge (e);
3444 else
3445 ei_next (&ei);
3448 /* This is somewhat ugly: the call_expr expander often emits instructions
3449 after the sibcall (to perform the function return). These confuse the
3450 find_many_sub_basic_blocks code, so we need to get rid of these. */
3451 last = NEXT_INSN (last);
3452 gcc_assert (BARRIER_P (last));
3454 *can_fallthru = false;
3455 while (NEXT_INSN (last))
3457 /* For instance an sqrt builtin expander expands if with
3458 sibcall in the then and label for `else`. */
3459 if (LABEL_P (NEXT_INSN (last)))
3461 *can_fallthru = true;
3462 break;
3464 delete_insn (NEXT_INSN (last));
3467 e = make_edge (bb, EXIT_BLOCK_PTR_FOR_FN (cfun), EDGE_ABNORMAL
3468 | EDGE_SIBCALL);
3469 e->probability += probability;
3470 e->count += count;
3471 BB_END (bb) = last;
3472 update_bb_for_insn (bb);
3474 if (NEXT_INSN (last))
3476 bb = create_basic_block (NEXT_INSN (last), get_last_insn (), bb);
3478 last = BB_END (bb);
3479 if (BARRIER_P (last))
3480 BB_END (bb) = PREV_INSN (last);
3483 maybe_dump_rtl_for_gimple_stmt (stmt, last2);
3485 return bb;
3488 /* Return the difference between the floor and the truncated result of
3489 a signed division by OP1 with remainder MOD. */
3490 static rtx
3491 floor_sdiv_adjust (enum machine_mode mode, rtx mod, rtx op1)
3493 /* (mod != 0 ? (op1 / mod < 0 ? -1 : 0) : 0) */
3494 return gen_rtx_IF_THEN_ELSE
3495 (mode, gen_rtx_NE (BImode, mod, const0_rtx),
3496 gen_rtx_IF_THEN_ELSE
3497 (mode, gen_rtx_LT (BImode,
3498 gen_rtx_DIV (mode, op1, mod),
3499 const0_rtx),
3500 constm1_rtx, const0_rtx),
3501 const0_rtx);
3504 /* Return the difference between the ceil and the truncated result of
3505 a signed division by OP1 with remainder MOD. */
3506 static rtx
3507 ceil_sdiv_adjust (enum machine_mode mode, rtx mod, rtx op1)
3509 /* (mod != 0 ? (op1 / mod > 0 ? 1 : 0) : 0) */
3510 return gen_rtx_IF_THEN_ELSE
3511 (mode, gen_rtx_NE (BImode, mod, const0_rtx),
3512 gen_rtx_IF_THEN_ELSE
3513 (mode, gen_rtx_GT (BImode,
3514 gen_rtx_DIV (mode, op1, mod),
3515 const0_rtx),
3516 const1_rtx, const0_rtx),
3517 const0_rtx);
3520 /* Return the difference between the ceil and the truncated result of
3521 an unsigned division by OP1 with remainder MOD. */
3522 static rtx
3523 ceil_udiv_adjust (enum machine_mode mode, rtx mod, rtx op1 ATTRIBUTE_UNUSED)
3525 /* (mod != 0 ? 1 : 0) */
3526 return gen_rtx_IF_THEN_ELSE
3527 (mode, gen_rtx_NE (BImode, mod, const0_rtx),
3528 const1_rtx, const0_rtx);
3531 /* Return the difference between the rounded and the truncated result
3532 of a signed division by OP1 with remainder MOD. Halfway cases are
3533 rounded away from zero, rather than to the nearest even number. */
3534 static rtx
3535 round_sdiv_adjust (enum machine_mode mode, rtx mod, rtx op1)
3537 /* (abs (mod) >= abs (op1) - abs (mod)
3538 ? (op1 / mod > 0 ? 1 : -1)
3539 : 0) */
3540 return gen_rtx_IF_THEN_ELSE
3541 (mode, gen_rtx_GE (BImode, gen_rtx_ABS (mode, mod),
3542 gen_rtx_MINUS (mode,
3543 gen_rtx_ABS (mode, op1),
3544 gen_rtx_ABS (mode, mod))),
3545 gen_rtx_IF_THEN_ELSE
3546 (mode, gen_rtx_GT (BImode,
3547 gen_rtx_DIV (mode, op1, mod),
3548 const0_rtx),
3549 const1_rtx, constm1_rtx),
3550 const0_rtx);
3553 /* Return the difference between the rounded and the truncated result
3554 of a unsigned division by OP1 with remainder MOD. Halfway cases
3555 are rounded away from zero, rather than to the nearest even
3556 number. */
3557 static rtx
3558 round_udiv_adjust (enum machine_mode mode, rtx mod, rtx op1)
3560 /* (mod >= op1 - mod ? 1 : 0) */
3561 return gen_rtx_IF_THEN_ELSE
3562 (mode, gen_rtx_GE (BImode, mod,
3563 gen_rtx_MINUS (mode, op1, mod)),
3564 const1_rtx, const0_rtx);
3567 /* Convert X to MODE, that must be Pmode or ptr_mode, without emitting
3568 any rtl. */
3570 static rtx
3571 convert_debug_memory_address (enum machine_mode mode, rtx x,
3572 addr_space_t as)
3574 enum machine_mode xmode = GET_MODE (x);
3576 #ifndef POINTERS_EXTEND_UNSIGNED
3577 gcc_assert (mode == Pmode
3578 || mode == targetm.addr_space.address_mode (as));
3579 gcc_assert (xmode == mode || xmode == VOIDmode);
3580 #else
3581 rtx temp;
3583 gcc_assert (targetm.addr_space.valid_pointer_mode (mode, as));
3585 if (GET_MODE (x) == mode || GET_MODE (x) == VOIDmode)
3586 return x;
3588 if (GET_MODE_PRECISION (mode) < GET_MODE_PRECISION (xmode))
3589 x = simplify_gen_subreg (mode, x, xmode,
3590 subreg_lowpart_offset
3591 (mode, xmode));
3592 else if (POINTERS_EXTEND_UNSIGNED > 0)
3593 x = gen_rtx_ZERO_EXTEND (mode, x);
3594 else if (!POINTERS_EXTEND_UNSIGNED)
3595 x = gen_rtx_SIGN_EXTEND (mode, x);
3596 else
3598 switch (GET_CODE (x))
3600 case SUBREG:
3601 if ((SUBREG_PROMOTED_VAR_P (x)
3602 || (REG_P (SUBREG_REG (x)) && REG_POINTER (SUBREG_REG (x)))
3603 || (GET_CODE (SUBREG_REG (x)) == PLUS
3604 && REG_P (XEXP (SUBREG_REG (x), 0))
3605 && REG_POINTER (XEXP (SUBREG_REG (x), 0))
3606 && CONST_INT_P (XEXP (SUBREG_REG (x), 1))))
3607 && GET_MODE (SUBREG_REG (x)) == mode)
3608 return SUBREG_REG (x);
3609 break;
3610 case LABEL_REF:
3611 temp = gen_rtx_LABEL_REF (mode, XEXP (x, 0));
3612 LABEL_REF_NONLOCAL_P (temp) = LABEL_REF_NONLOCAL_P (x);
3613 return temp;
3614 case SYMBOL_REF:
3615 temp = shallow_copy_rtx (x);
3616 PUT_MODE (temp, mode);
3617 return temp;
3618 case CONST:
3619 temp = convert_debug_memory_address (mode, XEXP (x, 0), as);
3620 if (temp)
3621 temp = gen_rtx_CONST (mode, temp);
3622 return temp;
3623 case PLUS:
3624 case MINUS:
3625 if (CONST_INT_P (XEXP (x, 1)))
3627 temp = convert_debug_memory_address (mode, XEXP (x, 0), as);
3628 if (temp)
3629 return gen_rtx_fmt_ee (GET_CODE (x), mode, temp, XEXP (x, 1));
3631 break;
3632 default:
3633 break;
3635 /* Don't know how to express ptr_extend as operation in debug info. */
3636 return NULL;
3638 #endif /* POINTERS_EXTEND_UNSIGNED */
3640 return x;
3643 /* Return an RTX equivalent to the value of the parameter DECL. */
3645 static rtx
3646 expand_debug_parm_decl (tree decl)
3648 rtx incoming = DECL_INCOMING_RTL (decl);
3650 if (incoming
3651 && GET_MODE (incoming) != BLKmode
3652 && ((REG_P (incoming) && HARD_REGISTER_P (incoming))
3653 || (MEM_P (incoming)
3654 && REG_P (XEXP (incoming, 0))
3655 && HARD_REGISTER_P (XEXP (incoming, 0)))))
3657 rtx rtl = gen_rtx_ENTRY_VALUE (GET_MODE (incoming));
3659 #ifdef HAVE_window_save
3660 /* DECL_INCOMING_RTL uses the INCOMING_REGNO of parameter registers.
3661 If the target machine has an explicit window save instruction, the
3662 actual entry value is the corresponding OUTGOING_REGNO instead. */
3663 if (REG_P (incoming)
3664 && OUTGOING_REGNO (REGNO (incoming)) != REGNO (incoming))
3665 incoming
3666 = gen_rtx_REG_offset (incoming, GET_MODE (incoming),
3667 OUTGOING_REGNO (REGNO (incoming)), 0);
3668 else if (MEM_P (incoming))
3670 rtx reg = XEXP (incoming, 0);
3671 if (OUTGOING_REGNO (REGNO (reg)) != REGNO (reg))
3673 reg = gen_raw_REG (GET_MODE (reg), OUTGOING_REGNO (REGNO (reg)));
3674 incoming = replace_equiv_address_nv (incoming, reg);
3676 else
3677 incoming = copy_rtx (incoming);
3679 #endif
3681 ENTRY_VALUE_EXP (rtl) = incoming;
3682 return rtl;
3685 if (incoming
3686 && GET_MODE (incoming) != BLKmode
3687 && !TREE_ADDRESSABLE (decl)
3688 && MEM_P (incoming)
3689 && (XEXP (incoming, 0) == virtual_incoming_args_rtx
3690 || (GET_CODE (XEXP (incoming, 0)) == PLUS
3691 && XEXP (XEXP (incoming, 0), 0) == virtual_incoming_args_rtx
3692 && CONST_INT_P (XEXP (XEXP (incoming, 0), 1)))))
3693 return copy_rtx (incoming);
3695 return NULL_RTX;
3698 /* Return an RTX equivalent to the value of the tree expression EXP. */
3700 static rtx
3701 expand_debug_expr (tree exp)
3703 rtx op0 = NULL_RTX, op1 = NULL_RTX, op2 = NULL_RTX;
3704 enum machine_mode mode = TYPE_MODE (TREE_TYPE (exp));
3705 enum machine_mode inner_mode = VOIDmode;
3706 int unsignedp = TYPE_UNSIGNED (TREE_TYPE (exp));
3707 addr_space_t as;
3709 switch (TREE_CODE_CLASS (TREE_CODE (exp)))
3711 case tcc_expression:
3712 switch (TREE_CODE (exp))
3714 case COND_EXPR:
3715 case DOT_PROD_EXPR:
3716 case WIDEN_MULT_PLUS_EXPR:
3717 case WIDEN_MULT_MINUS_EXPR:
3718 case FMA_EXPR:
3719 goto ternary;
3721 case TRUTH_ANDIF_EXPR:
3722 case TRUTH_ORIF_EXPR:
3723 case TRUTH_AND_EXPR:
3724 case TRUTH_OR_EXPR:
3725 case TRUTH_XOR_EXPR:
3726 goto binary;
3728 case TRUTH_NOT_EXPR:
3729 goto unary;
3731 default:
3732 break;
3734 break;
3736 ternary:
3737 op2 = expand_debug_expr (TREE_OPERAND (exp, 2));
3738 if (!op2)
3739 return NULL_RTX;
3740 /* Fall through. */
3742 binary:
3743 case tcc_binary:
3744 case tcc_comparison:
3745 op1 = expand_debug_expr (TREE_OPERAND (exp, 1));
3746 if (!op1)
3747 return NULL_RTX;
3748 /* Fall through. */
3750 unary:
3751 case tcc_unary:
3752 inner_mode = TYPE_MODE (TREE_TYPE (TREE_OPERAND (exp, 0)));
3753 op0 = expand_debug_expr (TREE_OPERAND (exp, 0));
3754 if (!op0)
3755 return NULL_RTX;
3756 break;
3758 case tcc_type:
3759 case tcc_statement:
3760 gcc_unreachable ();
3762 case tcc_constant:
3763 case tcc_exceptional:
3764 case tcc_declaration:
3765 case tcc_reference:
3766 case tcc_vl_exp:
3767 break;
3770 switch (TREE_CODE (exp))
3772 case STRING_CST:
3773 if (!lookup_constant_def (exp))
3775 if (strlen (TREE_STRING_POINTER (exp)) + 1
3776 != (size_t) TREE_STRING_LENGTH (exp))
3777 return NULL_RTX;
3778 op0 = gen_rtx_CONST_STRING (Pmode, TREE_STRING_POINTER (exp));
3779 op0 = gen_rtx_MEM (BLKmode, op0);
3780 set_mem_attributes (op0, exp, 0);
3781 return op0;
3783 /* Fall through... */
3785 case INTEGER_CST:
3786 case REAL_CST:
3787 case FIXED_CST:
3788 op0 = expand_expr (exp, NULL_RTX, mode, EXPAND_INITIALIZER);
3789 return op0;
3791 case COMPLEX_CST:
3792 gcc_assert (COMPLEX_MODE_P (mode));
3793 op0 = expand_debug_expr (TREE_REALPART (exp));
3794 op1 = expand_debug_expr (TREE_IMAGPART (exp));
3795 return gen_rtx_CONCAT (mode, op0, op1);
3797 case DEBUG_EXPR_DECL:
3798 op0 = DECL_RTL_IF_SET (exp);
3800 if (op0)
3801 return op0;
3803 op0 = gen_rtx_DEBUG_EXPR (mode);
3804 DEBUG_EXPR_TREE_DECL (op0) = exp;
3805 SET_DECL_RTL (exp, op0);
3807 return op0;
3809 case VAR_DECL:
3810 case PARM_DECL:
3811 case FUNCTION_DECL:
3812 case LABEL_DECL:
3813 case CONST_DECL:
3814 case RESULT_DECL:
3815 op0 = DECL_RTL_IF_SET (exp);
3817 /* This decl was probably optimized away. */
3818 if (!op0)
3820 if (TREE_CODE (exp) != VAR_DECL
3821 || DECL_EXTERNAL (exp)
3822 || !TREE_STATIC (exp)
3823 || !DECL_NAME (exp)
3824 || DECL_HARD_REGISTER (exp)
3825 || DECL_IN_CONSTANT_POOL (exp)
3826 || mode == VOIDmode)
3827 return NULL;
3829 op0 = make_decl_rtl_for_debug (exp);
3830 if (!MEM_P (op0)
3831 || GET_CODE (XEXP (op0, 0)) != SYMBOL_REF
3832 || SYMBOL_REF_DECL (XEXP (op0, 0)) != exp)
3833 return NULL;
3835 else
3836 op0 = copy_rtx (op0);
3838 if (GET_MODE (op0) == BLKmode
3839 /* If op0 is not BLKmode, but BLKmode is, adjust_mode
3840 below would ICE. While it is likely a FE bug,
3841 try to be robust here. See PR43166. */
3842 || mode == BLKmode
3843 || (mode == VOIDmode && GET_MODE (op0) != VOIDmode))
3845 gcc_assert (MEM_P (op0));
3846 op0 = adjust_address_nv (op0, mode, 0);
3847 return op0;
3850 /* Fall through. */
3852 adjust_mode:
3853 case PAREN_EXPR:
3854 case NOP_EXPR:
3855 case CONVERT_EXPR:
3857 inner_mode = GET_MODE (op0);
3859 if (mode == inner_mode)
3860 return op0;
3862 if (inner_mode == VOIDmode)
3864 if (TREE_CODE (exp) == SSA_NAME)
3865 inner_mode = TYPE_MODE (TREE_TYPE (exp));
3866 else
3867 inner_mode = TYPE_MODE (TREE_TYPE (TREE_OPERAND (exp, 0)));
3868 if (mode == inner_mode)
3869 return op0;
3872 if (FLOAT_MODE_P (mode) && FLOAT_MODE_P (inner_mode))
3874 if (GET_MODE_BITSIZE (mode) == GET_MODE_BITSIZE (inner_mode))
3875 op0 = simplify_gen_subreg (mode, op0, inner_mode, 0);
3876 else if (GET_MODE_BITSIZE (mode) < GET_MODE_BITSIZE (inner_mode))
3877 op0 = simplify_gen_unary (FLOAT_TRUNCATE, mode, op0, inner_mode);
3878 else
3879 op0 = simplify_gen_unary (FLOAT_EXTEND, mode, op0, inner_mode);
3881 else if (FLOAT_MODE_P (mode))
3883 gcc_assert (TREE_CODE (exp) != SSA_NAME);
3884 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp, 0))))
3885 op0 = simplify_gen_unary (UNSIGNED_FLOAT, mode, op0, inner_mode);
3886 else
3887 op0 = simplify_gen_unary (FLOAT, mode, op0, inner_mode);
3889 else if (FLOAT_MODE_P (inner_mode))
3891 if (unsignedp)
3892 op0 = simplify_gen_unary (UNSIGNED_FIX, mode, op0, inner_mode);
3893 else
3894 op0 = simplify_gen_unary (FIX, mode, op0, inner_mode);
3896 else if (CONSTANT_P (op0)
3897 || GET_MODE_PRECISION (mode) <= GET_MODE_PRECISION (inner_mode))
3898 op0 = simplify_gen_subreg (mode, op0, inner_mode,
3899 subreg_lowpart_offset (mode,
3900 inner_mode));
3901 else if (TREE_CODE_CLASS (TREE_CODE (exp)) == tcc_unary
3902 ? TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp, 0)))
3903 : unsignedp)
3904 op0 = simplify_gen_unary (ZERO_EXTEND, mode, op0, inner_mode);
3905 else
3906 op0 = simplify_gen_unary (SIGN_EXTEND, mode, op0, inner_mode);
3908 return op0;
3911 case MEM_REF:
3912 if (!is_gimple_mem_ref_addr (TREE_OPERAND (exp, 0)))
3914 tree newexp = fold_binary (MEM_REF, TREE_TYPE (exp),
3915 TREE_OPERAND (exp, 0),
3916 TREE_OPERAND (exp, 1));
3917 if (newexp)
3918 return expand_debug_expr (newexp);
3920 /* FALLTHROUGH */
3921 case INDIRECT_REF:
3922 inner_mode = TYPE_MODE (TREE_TYPE (TREE_OPERAND (exp, 0)));
3923 op0 = expand_debug_expr (TREE_OPERAND (exp, 0));
3924 if (!op0)
3925 return NULL;
3927 if (TREE_CODE (exp) == MEM_REF)
3929 if (GET_CODE (op0) == DEBUG_IMPLICIT_PTR
3930 || (GET_CODE (op0) == PLUS
3931 && GET_CODE (XEXP (op0, 0)) == DEBUG_IMPLICIT_PTR))
3932 /* (mem (debug_implicit_ptr)) might confuse aliasing.
3933 Instead just use get_inner_reference. */
3934 goto component_ref;
3936 op1 = expand_debug_expr (TREE_OPERAND (exp, 1));
3937 if (!op1 || !CONST_INT_P (op1))
3938 return NULL;
3940 op0 = plus_constant (inner_mode, op0, INTVAL (op1));
3943 as = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0))));
3945 op0 = convert_debug_memory_address (targetm.addr_space.address_mode (as),
3946 op0, as);
3947 if (op0 == NULL_RTX)
3948 return NULL;
3950 op0 = gen_rtx_MEM (mode, op0);
3951 set_mem_attributes (op0, exp, 0);
3952 if (TREE_CODE (exp) == MEM_REF
3953 && !is_gimple_mem_ref_addr (TREE_OPERAND (exp, 0)))
3954 set_mem_expr (op0, NULL_TREE);
3955 set_mem_addr_space (op0, as);
3957 return op0;
3959 case TARGET_MEM_REF:
3960 if (TREE_CODE (TMR_BASE (exp)) == ADDR_EXPR
3961 && !DECL_RTL_SET_P (TREE_OPERAND (TMR_BASE (exp), 0)))
3962 return NULL;
3964 op0 = expand_debug_expr
3965 (tree_mem_ref_addr (build_pointer_type (TREE_TYPE (exp)), exp));
3966 if (!op0)
3967 return NULL;
3969 if (POINTER_TYPE_P (TREE_TYPE (exp)))
3970 as = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (exp)));
3971 else
3972 as = ADDR_SPACE_GENERIC;
3974 op0 = convert_debug_memory_address (targetm.addr_space.address_mode (as),
3975 op0, as);
3976 if (op0 == NULL_RTX)
3977 return NULL;
3979 op0 = gen_rtx_MEM (mode, op0);
3981 set_mem_attributes (op0, exp, 0);
3982 set_mem_addr_space (op0, as);
3984 return op0;
3986 component_ref:
3987 case ARRAY_REF:
3988 case ARRAY_RANGE_REF:
3989 case COMPONENT_REF:
3990 case BIT_FIELD_REF:
3991 case REALPART_EXPR:
3992 case IMAGPART_EXPR:
3993 case VIEW_CONVERT_EXPR:
3995 enum machine_mode mode1;
3996 HOST_WIDE_INT bitsize, bitpos;
3997 tree offset;
3998 int volatilep = 0;
3999 tree tem = get_inner_reference (exp, &bitsize, &bitpos, &offset,
4000 &mode1, &unsignedp, &volatilep, false);
4001 rtx orig_op0;
4003 if (bitsize == 0)
4004 return NULL;
4006 orig_op0 = op0 = expand_debug_expr (tem);
4008 if (!op0)
4009 return NULL;
4011 if (offset)
4013 enum machine_mode addrmode, offmode;
4015 if (!MEM_P (op0))
4016 return NULL;
4018 op0 = XEXP (op0, 0);
4019 addrmode = GET_MODE (op0);
4020 if (addrmode == VOIDmode)
4021 addrmode = Pmode;
4023 op1 = expand_debug_expr (offset);
4024 if (!op1)
4025 return NULL;
4027 offmode = GET_MODE (op1);
4028 if (offmode == VOIDmode)
4029 offmode = TYPE_MODE (TREE_TYPE (offset));
4031 if (addrmode != offmode)
4032 op1 = simplify_gen_subreg (addrmode, op1, offmode,
4033 subreg_lowpart_offset (addrmode,
4034 offmode));
4036 /* Don't use offset_address here, we don't need a
4037 recognizable address, and we don't want to generate
4038 code. */
4039 op0 = gen_rtx_MEM (mode, simplify_gen_binary (PLUS, addrmode,
4040 op0, op1));
4043 if (MEM_P (op0))
4045 if (mode1 == VOIDmode)
4046 /* Bitfield. */
4047 mode1 = smallest_mode_for_size (bitsize, MODE_INT);
4048 if (bitpos >= BITS_PER_UNIT)
4050 op0 = adjust_address_nv (op0, mode1, bitpos / BITS_PER_UNIT);
4051 bitpos %= BITS_PER_UNIT;
4053 else if (bitpos < 0)
4055 HOST_WIDE_INT units
4056 = (-bitpos + BITS_PER_UNIT - 1) / BITS_PER_UNIT;
4057 op0 = adjust_address_nv (op0, mode1, units);
4058 bitpos += units * BITS_PER_UNIT;
4060 else if (bitpos == 0 && bitsize == GET_MODE_BITSIZE (mode))
4061 op0 = adjust_address_nv (op0, mode, 0);
4062 else if (GET_MODE (op0) != mode1)
4063 op0 = adjust_address_nv (op0, mode1, 0);
4064 else
4065 op0 = copy_rtx (op0);
4066 if (op0 == orig_op0)
4067 op0 = shallow_copy_rtx (op0);
4068 set_mem_attributes (op0, exp, 0);
4071 if (bitpos == 0 && mode == GET_MODE (op0))
4072 return op0;
4074 if (bitpos < 0)
4075 return NULL;
4077 if (GET_MODE (op0) == BLKmode)
4078 return NULL;
4080 if ((bitpos % BITS_PER_UNIT) == 0
4081 && bitsize == GET_MODE_BITSIZE (mode1))
4083 enum machine_mode opmode = GET_MODE (op0);
4085 if (opmode == VOIDmode)
4086 opmode = TYPE_MODE (TREE_TYPE (tem));
4088 /* This condition may hold if we're expanding the address
4089 right past the end of an array that turned out not to
4090 be addressable (i.e., the address was only computed in
4091 debug stmts). The gen_subreg below would rightfully
4092 crash, and the address doesn't really exist, so just
4093 drop it. */
4094 if (bitpos >= GET_MODE_BITSIZE (opmode))
4095 return NULL;
4097 if ((bitpos % GET_MODE_BITSIZE (mode)) == 0)
4098 return simplify_gen_subreg (mode, op0, opmode,
4099 bitpos / BITS_PER_UNIT);
4102 return simplify_gen_ternary (SCALAR_INT_MODE_P (GET_MODE (op0))
4103 && TYPE_UNSIGNED (TREE_TYPE (exp))
4104 ? SIGN_EXTRACT
4105 : ZERO_EXTRACT, mode,
4106 GET_MODE (op0) != VOIDmode
4107 ? GET_MODE (op0)
4108 : TYPE_MODE (TREE_TYPE (tem)),
4109 op0, GEN_INT (bitsize), GEN_INT (bitpos));
4112 case ABS_EXPR:
4113 return simplify_gen_unary (ABS, mode, op0, mode);
4115 case NEGATE_EXPR:
4116 return simplify_gen_unary (NEG, mode, op0, mode);
4118 case BIT_NOT_EXPR:
4119 return simplify_gen_unary (NOT, mode, op0, mode);
4121 case FLOAT_EXPR:
4122 return simplify_gen_unary (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp,
4123 0)))
4124 ? UNSIGNED_FLOAT : FLOAT, mode, op0,
4125 inner_mode);
4127 case FIX_TRUNC_EXPR:
4128 return simplify_gen_unary (unsignedp ? UNSIGNED_FIX : FIX, mode, op0,
4129 inner_mode);
4131 case POINTER_PLUS_EXPR:
4132 /* For the rare target where pointers are not the same size as
4133 size_t, we need to check for mis-matched modes and correct
4134 the addend. */
4135 if (op0 && op1
4136 && GET_MODE (op0) != VOIDmode && GET_MODE (op1) != VOIDmode
4137 && GET_MODE (op0) != GET_MODE (op1))
4139 if (GET_MODE_BITSIZE (GET_MODE (op0)) < GET_MODE_BITSIZE (GET_MODE (op1))
4140 /* If OP0 is a partial mode, then we must truncate, even if it has
4141 the same bitsize as OP1 as GCC's representation of partial modes
4142 is opaque. */
4143 || (GET_MODE_CLASS (GET_MODE (op0)) == MODE_PARTIAL_INT
4144 && GET_MODE_BITSIZE (GET_MODE (op0)) == GET_MODE_BITSIZE (GET_MODE (op1))))
4145 op1 = simplify_gen_unary (TRUNCATE, GET_MODE (op0), op1,
4146 GET_MODE (op1));
4147 else
4148 /* We always sign-extend, regardless of the signedness of
4149 the operand, because the operand is always unsigned
4150 here even if the original C expression is signed. */
4151 op1 = simplify_gen_unary (SIGN_EXTEND, GET_MODE (op0), op1,
4152 GET_MODE (op1));
4154 /* Fall through. */
4155 case PLUS_EXPR:
4156 return simplify_gen_binary (PLUS, mode, op0, op1);
4158 case MINUS_EXPR:
4159 return simplify_gen_binary (MINUS, mode, op0, op1);
4161 case MULT_EXPR:
4162 return simplify_gen_binary (MULT, mode, op0, op1);
4164 case RDIV_EXPR:
4165 case TRUNC_DIV_EXPR:
4166 case EXACT_DIV_EXPR:
4167 if (unsignedp)
4168 return simplify_gen_binary (UDIV, mode, op0, op1);
4169 else
4170 return simplify_gen_binary (DIV, mode, op0, op1);
4172 case TRUNC_MOD_EXPR:
4173 return simplify_gen_binary (unsignedp ? UMOD : MOD, mode, op0, op1);
4175 case FLOOR_DIV_EXPR:
4176 if (unsignedp)
4177 return simplify_gen_binary (UDIV, mode, op0, op1);
4178 else
4180 rtx div = simplify_gen_binary (DIV, mode, op0, op1);
4181 rtx mod = simplify_gen_binary (MOD, mode, op0, op1);
4182 rtx adj = floor_sdiv_adjust (mode, mod, op1);
4183 return simplify_gen_binary (PLUS, mode, div, adj);
4186 case FLOOR_MOD_EXPR:
4187 if (unsignedp)
4188 return simplify_gen_binary (UMOD, mode, op0, op1);
4189 else
4191 rtx mod = simplify_gen_binary (MOD, mode, op0, op1);
4192 rtx adj = floor_sdiv_adjust (mode, mod, op1);
4193 adj = simplify_gen_unary (NEG, mode,
4194 simplify_gen_binary (MULT, mode, adj, op1),
4195 mode);
4196 return simplify_gen_binary (PLUS, mode, mod, adj);
4199 case CEIL_DIV_EXPR:
4200 if (unsignedp)
4202 rtx div = simplify_gen_binary (UDIV, mode, op0, op1);
4203 rtx mod = simplify_gen_binary (UMOD, mode, op0, op1);
4204 rtx adj = ceil_udiv_adjust (mode, mod, op1);
4205 return simplify_gen_binary (PLUS, mode, div, adj);
4207 else
4209 rtx div = simplify_gen_binary (DIV, mode, op0, op1);
4210 rtx mod = simplify_gen_binary (MOD, mode, op0, op1);
4211 rtx adj = ceil_sdiv_adjust (mode, mod, op1);
4212 return simplify_gen_binary (PLUS, mode, div, adj);
4215 case CEIL_MOD_EXPR:
4216 if (unsignedp)
4218 rtx mod = simplify_gen_binary (UMOD, mode, op0, op1);
4219 rtx adj = ceil_udiv_adjust (mode, mod, op1);
4220 adj = simplify_gen_unary (NEG, mode,
4221 simplify_gen_binary (MULT, mode, adj, op1),
4222 mode);
4223 return simplify_gen_binary (PLUS, mode, mod, adj);
4225 else
4227 rtx mod = simplify_gen_binary (MOD, mode, op0, op1);
4228 rtx adj = ceil_sdiv_adjust (mode, mod, op1);
4229 adj = simplify_gen_unary (NEG, mode,
4230 simplify_gen_binary (MULT, mode, adj, op1),
4231 mode);
4232 return simplify_gen_binary (PLUS, mode, mod, adj);
4235 case ROUND_DIV_EXPR:
4236 if (unsignedp)
4238 rtx div = simplify_gen_binary (UDIV, mode, op0, op1);
4239 rtx mod = simplify_gen_binary (UMOD, mode, op0, op1);
4240 rtx adj = round_udiv_adjust (mode, mod, op1);
4241 return simplify_gen_binary (PLUS, mode, div, adj);
4243 else
4245 rtx div = simplify_gen_binary (DIV, mode, op0, op1);
4246 rtx mod = simplify_gen_binary (MOD, mode, op0, op1);
4247 rtx adj = round_sdiv_adjust (mode, mod, op1);
4248 return simplify_gen_binary (PLUS, mode, div, adj);
4251 case ROUND_MOD_EXPR:
4252 if (unsignedp)
4254 rtx mod = simplify_gen_binary (UMOD, mode, op0, op1);
4255 rtx adj = round_udiv_adjust (mode, mod, op1);
4256 adj = simplify_gen_unary (NEG, mode,
4257 simplify_gen_binary (MULT, mode, adj, op1),
4258 mode);
4259 return simplify_gen_binary (PLUS, mode, mod, adj);
4261 else
4263 rtx mod = simplify_gen_binary (MOD, mode, op0, op1);
4264 rtx adj = round_sdiv_adjust (mode, mod, op1);
4265 adj = simplify_gen_unary (NEG, mode,
4266 simplify_gen_binary (MULT, mode, adj, op1),
4267 mode);
4268 return simplify_gen_binary (PLUS, mode, mod, adj);
4271 case LSHIFT_EXPR:
4272 return simplify_gen_binary (ASHIFT, mode, op0, op1);
4274 case RSHIFT_EXPR:
4275 if (unsignedp)
4276 return simplify_gen_binary (LSHIFTRT, mode, op0, op1);
4277 else
4278 return simplify_gen_binary (ASHIFTRT, mode, op0, op1);
4280 case LROTATE_EXPR:
4281 return simplify_gen_binary (ROTATE, mode, op0, op1);
4283 case RROTATE_EXPR:
4284 return simplify_gen_binary (ROTATERT, mode, op0, op1);
4286 case MIN_EXPR:
4287 return simplify_gen_binary (unsignedp ? UMIN : SMIN, mode, op0, op1);
4289 case MAX_EXPR:
4290 return simplify_gen_binary (unsignedp ? UMAX : SMAX, mode, op0, op1);
4292 case BIT_AND_EXPR:
4293 case TRUTH_AND_EXPR:
4294 return simplify_gen_binary (AND, mode, op0, op1);
4296 case BIT_IOR_EXPR:
4297 case TRUTH_OR_EXPR:
4298 return simplify_gen_binary (IOR, mode, op0, op1);
4300 case BIT_XOR_EXPR:
4301 case TRUTH_XOR_EXPR:
4302 return simplify_gen_binary (XOR, mode, op0, op1);
4304 case TRUTH_ANDIF_EXPR:
4305 return gen_rtx_IF_THEN_ELSE (mode, op0, op1, const0_rtx);
4307 case TRUTH_ORIF_EXPR:
4308 return gen_rtx_IF_THEN_ELSE (mode, op0, const_true_rtx, op1);
4310 case TRUTH_NOT_EXPR:
4311 return simplify_gen_relational (EQ, mode, inner_mode, op0, const0_rtx);
4313 case LT_EXPR:
4314 return simplify_gen_relational (unsignedp ? LTU : LT, mode, inner_mode,
4315 op0, op1);
4317 case LE_EXPR:
4318 return simplify_gen_relational (unsignedp ? LEU : LE, mode, inner_mode,
4319 op0, op1);
4321 case GT_EXPR:
4322 return simplify_gen_relational (unsignedp ? GTU : GT, mode, inner_mode,
4323 op0, op1);
4325 case GE_EXPR:
4326 return simplify_gen_relational (unsignedp ? GEU : GE, mode, inner_mode,
4327 op0, op1);
4329 case EQ_EXPR:
4330 return simplify_gen_relational (EQ, mode, inner_mode, op0, op1);
4332 case NE_EXPR:
4333 return simplify_gen_relational (NE, mode, inner_mode, op0, op1);
4335 case UNORDERED_EXPR:
4336 return simplify_gen_relational (UNORDERED, mode, inner_mode, op0, op1);
4338 case ORDERED_EXPR:
4339 return simplify_gen_relational (ORDERED, mode, inner_mode, op0, op1);
4341 case UNLT_EXPR:
4342 return simplify_gen_relational (UNLT, mode, inner_mode, op0, op1);
4344 case UNLE_EXPR:
4345 return simplify_gen_relational (UNLE, mode, inner_mode, op0, op1);
4347 case UNGT_EXPR:
4348 return simplify_gen_relational (UNGT, mode, inner_mode, op0, op1);
4350 case UNGE_EXPR:
4351 return simplify_gen_relational (UNGE, mode, inner_mode, op0, op1);
4353 case UNEQ_EXPR:
4354 return simplify_gen_relational (UNEQ, mode, inner_mode, op0, op1);
4356 case LTGT_EXPR:
4357 return simplify_gen_relational (LTGT, mode, inner_mode, op0, op1);
4359 case COND_EXPR:
4360 return gen_rtx_IF_THEN_ELSE (mode, op0, op1, op2);
4362 case COMPLEX_EXPR:
4363 gcc_assert (COMPLEX_MODE_P (mode));
4364 if (GET_MODE (op0) == VOIDmode)
4365 op0 = gen_rtx_CONST (GET_MODE_INNER (mode), op0);
4366 if (GET_MODE (op1) == VOIDmode)
4367 op1 = gen_rtx_CONST (GET_MODE_INNER (mode), op1);
4368 return gen_rtx_CONCAT (mode, op0, op1);
4370 case CONJ_EXPR:
4371 if (GET_CODE (op0) == CONCAT)
4372 return gen_rtx_CONCAT (mode, XEXP (op0, 0),
4373 simplify_gen_unary (NEG, GET_MODE_INNER (mode),
4374 XEXP (op0, 1),
4375 GET_MODE_INNER (mode)));
4376 else
4378 enum machine_mode imode = GET_MODE_INNER (mode);
4379 rtx re, im;
4381 if (MEM_P (op0))
4383 re = adjust_address_nv (op0, imode, 0);
4384 im = adjust_address_nv (op0, imode, GET_MODE_SIZE (imode));
4386 else
4388 enum machine_mode ifmode = int_mode_for_mode (mode);
4389 enum machine_mode ihmode = int_mode_for_mode (imode);
4390 rtx halfsize;
4391 if (ifmode == BLKmode || ihmode == BLKmode)
4392 return NULL;
4393 halfsize = GEN_INT (GET_MODE_BITSIZE (ihmode));
4394 re = op0;
4395 if (mode != ifmode)
4396 re = gen_rtx_SUBREG (ifmode, re, 0);
4397 re = gen_rtx_ZERO_EXTRACT (ihmode, re, halfsize, const0_rtx);
4398 if (imode != ihmode)
4399 re = gen_rtx_SUBREG (imode, re, 0);
4400 im = copy_rtx (op0);
4401 if (mode != ifmode)
4402 im = gen_rtx_SUBREG (ifmode, im, 0);
4403 im = gen_rtx_ZERO_EXTRACT (ihmode, im, halfsize, halfsize);
4404 if (imode != ihmode)
4405 im = gen_rtx_SUBREG (imode, im, 0);
4407 im = gen_rtx_NEG (imode, im);
4408 return gen_rtx_CONCAT (mode, re, im);
4411 case ADDR_EXPR:
4412 op0 = expand_debug_expr (TREE_OPERAND (exp, 0));
4413 if (!op0 || !MEM_P (op0))
4415 if ((TREE_CODE (TREE_OPERAND (exp, 0)) == VAR_DECL
4416 || TREE_CODE (TREE_OPERAND (exp, 0)) == PARM_DECL
4417 || TREE_CODE (TREE_OPERAND (exp, 0)) == RESULT_DECL)
4418 && (!TREE_ADDRESSABLE (TREE_OPERAND (exp, 0))
4419 || target_for_debug_bind (TREE_OPERAND (exp, 0))))
4420 return gen_rtx_DEBUG_IMPLICIT_PTR (mode, TREE_OPERAND (exp, 0));
4422 if (handled_component_p (TREE_OPERAND (exp, 0)))
4424 HOST_WIDE_INT bitoffset, bitsize, maxsize;
4425 tree decl
4426 = get_ref_base_and_extent (TREE_OPERAND (exp, 0),
4427 &bitoffset, &bitsize, &maxsize);
4428 if ((TREE_CODE (decl) == VAR_DECL
4429 || TREE_CODE (decl) == PARM_DECL
4430 || TREE_CODE (decl) == RESULT_DECL)
4431 && (!TREE_ADDRESSABLE (decl)
4432 || target_for_debug_bind (decl))
4433 && (bitoffset % BITS_PER_UNIT) == 0
4434 && bitsize > 0
4435 && bitsize == maxsize)
4437 rtx base = gen_rtx_DEBUG_IMPLICIT_PTR (mode, decl);
4438 return plus_constant (mode, base, bitoffset / BITS_PER_UNIT);
4442 if (TREE_CODE (TREE_OPERAND (exp, 0)) == MEM_REF
4443 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
4444 == ADDR_EXPR)
4446 op0 = expand_debug_expr (TREE_OPERAND (TREE_OPERAND (exp, 0),
4447 0));
4448 if (op0 != NULL
4449 && (GET_CODE (op0) == DEBUG_IMPLICIT_PTR
4450 || (GET_CODE (op0) == PLUS
4451 && GET_CODE (XEXP (op0, 0)) == DEBUG_IMPLICIT_PTR
4452 && CONST_INT_P (XEXP (op0, 1)))))
4454 op1 = expand_debug_expr (TREE_OPERAND (TREE_OPERAND (exp, 0),
4455 1));
4456 if (!op1 || !CONST_INT_P (op1))
4457 return NULL;
4459 return plus_constant (mode, op0, INTVAL (op1));
4463 return NULL;
4466 as = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (exp)));
4467 op0 = convert_debug_memory_address (mode, XEXP (op0, 0), as);
4469 return op0;
4471 case VECTOR_CST:
4473 unsigned i;
4475 op0 = gen_rtx_CONCATN
4476 (mode, rtvec_alloc (TYPE_VECTOR_SUBPARTS (TREE_TYPE (exp))));
4478 for (i = 0; i < VECTOR_CST_NELTS (exp); ++i)
4480 op1 = expand_debug_expr (VECTOR_CST_ELT (exp, i));
4481 if (!op1)
4482 return NULL;
4483 XVECEXP (op0, 0, i) = op1;
4486 return op0;
4489 case CONSTRUCTOR:
4490 if (TREE_CLOBBER_P (exp))
4491 return NULL;
4492 else if (TREE_CODE (TREE_TYPE (exp)) == VECTOR_TYPE)
4494 unsigned i;
4495 tree val;
4497 op0 = gen_rtx_CONCATN
4498 (mode, rtvec_alloc (TYPE_VECTOR_SUBPARTS (TREE_TYPE (exp))));
4500 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), i, val)
4502 op1 = expand_debug_expr (val);
4503 if (!op1)
4504 return NULL;
4505 XVECEXP (op0, 0, i) = op1;
4508 if (i < TYPE_VECTOR_SUBPARTS (TREE_TYPE (exp)))
4510 op1 = expand_debug_expr
4511 (build_zero_cst (TREE_TYPE (TREE_TYPE (exp))));
4513 if (!op1)
4514 return NULL;
4516 for (; i < TYPE_VECTOR_SUBPARTS (TREE_TYPE (exp)); i++)
4517 XVECEXP (op0, 0, i) = op1;
4520 return op0;
4522 else
4523 goto flag_unsupported;
4525 case CALL_EXPR:
4526 /* ??? Maybe handle some builtins? */
4527 return NULL;
4529 case SSA_NAME:
4531 gimple g = get_gimple_for_ssa_name (exp);
4532 if (g)
4534 op0 = expand_debug_expr (gimple_assign_rhs_to_tree (g));
4535 if (!op0)
4536 return NULL;
4538 else
4540 int part = var_to_partition (SA.map, exp);
4542 if (part == NO_PARTITION)
4544 /* If this is a reference to an incoming value of parameter
4545 that is never used in the code or where the incoming
4546 value is never used in the code, use PARM_DECL's
4547 DECL_RTL if set. */
4548 if (SSA_NAME_IS_DEFAULT_DEF (exp)
4549 && TREE_CODE (SSA_NAME_VAR (exp)) == PARM_DECL)
4551 op0 = expand_debug_parm_decl (SSA_NAME_VAR (exp));
4552 if (op0)
4553 goto adjust_mode;
4554 op0 = expand_debug_expr (SSA_NAME_VAR (exp));
4555 if (op0)
4556 goto adjust_mode;
4558 return NULL;
4561 gcc_assert (part >= 0 && (unsigned)part < SA.map->num_partitions);
4563 op0 = copy_rtx (SA.partition_to_pseudo[part]);
4565 goto adjust_mode;
4568 case ERROR_MARK:
4569 return NULL;
4571 /* Vector stuff. For most of the codes we don't have rtl codes. */
4572 case REALIGN_LOAD_EXPR:
4573 case REDUC_MAX_EXPR:
4574 case REDUC_MIN_EXPR:
4575 case REDUC_PLUS_EXPR:
4576 case VEC_COND_EXPR:
4577 case VEC_LSHIFT_EXPR:
4578 case VEC_PACK_FIX_TRUNC_EXPR:
4579 case VEC_PACK_SAT_EXPR:
4580 case VEC_PACK_TRUNC_EXPR:
4581 case VEC_RSHIFT_EXPR:
4582 case VEC_UNPACK_FLOAT_HI_EXPR:
4583 case VEC_UNPACK_FLOAT_LO_EXPR:
4584 case VEC_UNPACK_HI_EXPR:
4585 case VEC_UNPACK_LO_EXPR:
4586 case VEC_WIDEN_MULT_HI_EXPR:
4587 case VEC_WIDEN_MULT_LO_EXPR:
4588 case VEC_WIDEN_MULT_EVEN_EXPR:
4589 case VEC_WIDEN_MULT_ODD_EXPR:
4590 case VEC_WIDEN_LSHIFT_HI_EXPR:
4591 case VEC_WIDEN_LSHIFT_LO_EXPR:
4592 case VEC_PERM_EXPR:
4593 return NULL;
4595 /* Misc codes. */
4596 case ADDR_SPACE_CONVERT_EXPR:
4597 case FIXED_CONVERT_EXPR:
4598 case OBJ_TYPE_REF:
4599 case WITH_SIZE_EXPR:
4600 return NULL;
4602 case DOT_PROD_EXPR:
4603 if (SCALAR_INT_MODE_P (GET_MODE (op0))
4604 && SCALAR_INT_MODE_P (mode))
4607 = simplify_gen_unary (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp,
4608 0)))
4609 ? ZERO_EXTEND : SIGN_EXTEND, mode, op0,
4610 inner_mode);
4612 = simplify_gen_unary (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp,
4613 1)))
4614 ? ZERO_EXTEND : SIGN_EXTEND, mode, op1,
4615 inner_mode);
4616 op0 = simplify_gen_binary (MULT, mode, op0, op1);
4617 return simplify_gen_binary (PLUS, mode, op0, op2);
4619 return NULL;
4621 case WIDEN_MULT_EXPR:
4622 case WIDEN_MULT_PLUS_EXPR:
4623 case WIDEN_MULT_MINUS_EXPR:
4624 if (SCALAR_INT_MODE_P (GET_MODE (op0))
4625 && SCALAR_INT_MODE_P (mode))
4627 inner_mode = GET_MODE (op0);
4628 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp, 0))))
4629 op0 = simplify_gen_unary (ZERO_EXTEND, mode, op0, inner_mode);
4630 else
4631 op0 = simplify_gen_unary (SIGN_EXTEND, mode, op0, inner_mode);
4632 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp, 1))))
4633 op1 = simplify_gen_unary (ZERO_EXTEND, mode, op1, inner_mode);
4634 else
4635 op1 = simplify_gen_unary (SIGN_EXTEND, mode, op1, inner_mode);
4636 op0 = simplify_gen_binary (MULT, mode, op0, op1);
4637 if (TREE_CODE (exp) == WIDEN_MULT_EXPR)
4638 return op0;
4639 else if (TREE_CODE (exp) == WIDEN_MULT_PLUS_EXPR)
4640 return simplify_gen_binary (PLUS, mode, op0, op2);
4641 else
4642 return simplify_gen_binary (MINUS, mode, op2, op0);
4644 return NULL;
4646 case MULT_HIGHPART_EXPR:
4647 /* ??? Similar to the above. */
4648 return NULL;
4650 case WIDEN_SUM_EXPR:
4651 case WIDEN_LSHIFT_EXPR:
4652 if (SCALAR_INT_MODE_P (GET_MODE (op0))
4653 && SCALAR_INT_MODE_P (mode))
4656 = simplify_gen_unary (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp,
4657 0)))
4658 ? ZERO_EXTEND : SIGN_EXTEND, mode, op0,
4659 inner_mode);
4660 return simplify_gen_binary (TREE_CODE (exp) == WIDEN_LSHIFT_EXPR
4661 ? ASHIFT : PLUS, mode, op0, op1);
4663 return NULL;
4665 case FMA_EXPR:
4666 return simplify_gen_ternary (FMA, mode, inner_mode, op0, op1, op2);
4668 default:
4669 flag_unsupported:
4670 #ifdef ENABLE_CHECKING
4671 debug_tree (exp);
4672 gcc_unreachable ();
4673 #else
4674 return NULL;
4675 #endif
4679 /* Return an RTX equivalent to the source bind value of the tree expression
4680 EXP. */
4682 static rtx
4683 expand_debug_source_expr (tree exp)
4685 rtx op0 = NULL_RTX;
4686 enum machine_mode mode = VOIDmode, inner_mode;
4688 switch (TREE_CODE (exp))
4690 case PARM_DECL:
4692 mode = DECL_MODE (exp);
4693 op0 = expand_debug_parm_decl (exp);
4694 if (op0)
4695 break;
4696 /* See if this isn't an argument that has been completely
4697 optimized out. */
4698 if (!DECL_RTL_SET_P (exp)
4699 && !DECL_INCOMING_RTL (exp)
4700 && DECL_ABSTRACT_ORIGIN (current_function_decl))
4702 tree aexp = DECL_ORIGIN (exp);
4703 if (DECL_CONTEXT (aexp)
4704 == DECL_ABSTRACT_ORIGIN (current_function_decl))
4706 vec<tree, va_gc> **debug_args;
4707 unsigned int ix;
4708 tree ddecl;
4709 debug_args = decl_debug_args_lookup (current_function_decl);
4710 if (debug_args != NULL)
4712 for (ix = 0; vec_safe_iterate (*debug_args, ix, &ddecl);
4713 ix += 2)
4714 if (ddecl == aexp)
4715 return gen_rtx_DEBUG_PARAMETER_REF (mode, aexp);
4719 break;
4721 default:
4722 break;
4725 if (op0 == NULL_RTX)
4726 return NULL_RTX;
4728 inner_mode = GET_MODE (op0);
4729 if (mode == inner_mode)
4730 return op0;
4732 if (FLOAT_MODE_P (mode) && FLOAT_MODE_P (inner_mode))
4734 if (GET_MODE_BITSIZE (mode) == GET_MODE_BITSIZE (inner_mode))
4735 op0 = simplify_gen_subreg (mode, op0, inner_mode, 0);
4736 else if (GET_MODE_BITSIZE (mode) < GET_MODE_BITSIZE (inner_mode))
4737 op0 = simplify_gen_unary (FLOAT_TRUNCATE, mode, op0, inner_mode);
4738 else
4739 op0 = simplify_gen_unary (FLOAT_EXTEND, mode, op0, inner_mode);
4741 else if (FLOAT_MODE_P (mode))
4742 gcc_unreachable ();
4743 else if (FLOAT_MODE_P (inner_mode))
4745 if (TYPE_UNSIGNED (TREE_TYPE (exp)))
4746 op0 = simplify_gen_unary (UNSIGNED_FIX, mode, op0, inner_mode);
4747 else
4748 op0 = simplify_gen_unary (FIX, mode, op0, inner_mode);
4750 else if (CONSTANT_P (op0)
4751 || GET_MODE_BITSIZE (mode) <= GET_MODE_BITSIZE (inner_mode))
4752 op0 = simplify_gen_subreg (mode, op0, inner_mode,
4753 subreg_lowpart_offset (mode, inner_mode));
4754 else if (TYPE_UNSIGNED (TREE_TYPE (exp)))
4755 op0 = simplify_gen_unary (ZERO_EXTEND, mode, op0, inner_mode);
4756 else
4757 op0 = simplify_gen_unary (SIGN_EXTEND, mode, op0, inner_mode);
4759 return op0;
4762 /* Ensure INSN_VAR_LOCATION_LOC (insn) doesn't have unbound complexity.
4763 Allow 4 levels of rtl nesting for most rtl codes, and if we see anything
4764 deeper than that, create DEBUG_EXPRs and emit DEBUG_INSNs before INSN. */
4766 static void
4767 avoid_complex_debug_insns (rtx insn, rtx *exp_p, int depth)
4769 rtx exp = *exp_p;
4771 if (exp == NULL_RTX)
4772 return;
4774 if ((OBJECT_P (exp) && !MEM_P (exp)) || GET_CODE (exp) == CLOBBER)
4775 return;
4777 if (depth == 4)
4779 /* Create DEBUG_EXPR (and DEBUG_EXPR_DECL). */
4780 rtx dval = make_debug_expr_from_rtl (exp);
4782 /* Emit a debug bind insn before INSN. */
4783 rtx bind = gen_rtx_VAR_LOCATION (GET_MODE (exp),
4784 DEBUG_EXPR_TREE_DECL (dval), exp,
4785 VAR_INIT_STATUS_INITIALIZED);
4787 emit_debug_insn_before (bind, insn);
4788 *exp_p = dval;
4789 return;
4792 const char *format_ptr = GET_RTX_FORMAT (GET_CODE (exp));
4793 int i, j;
4794 for (i = 0; i < GET_RTX_LENGTH (GET_CODE (exp)); i++)
4795 switch (*format_ptr++)
4797 case 'e':
4798 avoid_complex_debug_insns (insn, &XEXP (exp, i), depth + 1);
4799 break;
4801 case 'E':
4802 case 'V':
4803 for (j = 0; j < XVECLEN (exp, i); j++)
4804 avoid_complex_debug_insns (insn, &XVECEXP (exp, i, j), depth + 1);
4805 break;
4807 default:
4808 break;
4812 /* Expand the _LOCs in debug insns. We run this after expanding all
4813 regular insns, so that any variables referenced in the function
4814 will have their DECL_RTLs set. */
4816 static void
4817 expand_debug_locations (void)
4819 rtx insn;
4820 rtx last = get_last_insn ();
4821 int save_strict_alias = flag_strict_aliasing;
4823 /* New alias sets while setting up memory attributes cause
4824 -fcompare-debug failures, even though it doesn't bring about any
4825 codegen changes. */
4826 flag_strict_aliasing = 0;
4828 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
4829 if (DEBUG_INSN_P (insn))
4831 tree value = (tree)INSN_VAR_LOCATION_LOC (insn);
4832 rtx val, prev_insn, insn2;
4833 enum machine_mode mode;
4835 if (value == NULL_TREE)
4836 val = NULL_RTX;
4837 else
4839 if (INSN_VAR_LOCATION_STATUS (insn)
4840 == VAR_INIT_STATUS_UNINITIALIZED)
4841 val = expand_debug_source_expr (value);
4842 else
4843 val = expand_debug_expr (value);
4844 gcc_assert (last == get_last_insn ());
4847 if (!val)
4848 val = gen_rtx_UNKNOWN_VAR_LOC ();
4849 else
4851 mode = GET_MODE (INSN_VAR_LOCATION (insn));
4853 gcc_assert (mode == GET_MODE (val)
4854 || (GET_MODE (val) == VOIDmode
4855 && (CONST_SCALAR_INT_P (val)
4856 || GET_CODE (val) == CONST_FIXED
4857 || GET_CODE (val) == LABEL_REF)));
4860 INSN_VAR_LOCATION_LOC (insn) = val;
4861 prev_insn = PREV_INSN (insn);
4862 for (insn2 = insn; insn2 != prev_insn; insn2 = PREV_INSN (insn2))
4863 avoid_complex_debug_insns (insn2, &INSN_VAR_LOCATION_LOC (insn2), 0);
4866 flag_strict_aliasing = save_strict_alias;
4869 /* Expand basic block BB from GIMPLE trees to RTL. */
4871 static basic_block
4872 expand_gimple_basic_block (basic_block bb, bool disable_tail_calls)
4874 gimple_stmt_iterator gsi;
4875 gimple_seq stmts;
4876 gimple stmt = NULL;
4877 rtx note, last;
4878 edge e;
4879 edge_iterator ei;
4880 void **elt;
4882 if (dump_file)
4883 fprintf (dump_file, "\n;; Generating RTL for gimple basic block %d\n",
4884 bb->index);
4886 /* Note that since we are now transitioning from GIMPLE to RTL, we
4887 cannot use the gsi_*_bb() routines because they expect the basic
4888 block to be in GIMPLE, instead of RTL. Therefore, we need to
4889 access the BB sequence directly. */
4890 stmts = bb_seq (bb);
4891 bb->il.gimple.seq = NULL;
4892 bb->il.gimple.phi_nodes = NULL;
4893 rtl_profile_for_bb (bb);
4894 init_rtl_bb_info (bb);
4895 bb->flags |= BB_RTL;
4897 /* Remove the RETURN_EXPR if we may fall though to the exit
4898 instead. */
4899 gsi = gsi_last (stmts);
4900 if (!gsi_end_p (gsi)
4901 && gimple_code (gsi_stmt (gsi)) == GIMPLE_RETURN)
4903 gimple ret_stmt = gsi_stmt (gsi);
4905 gcc_assert (single_succ_p (bb));
4906 gcc_assert (single_succ (bb) == EXIT_BLOCK_PTR_FOR_FN (cfun));
4908 if (bb->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
4909 && !gimple_return_retval (ret_stmt))
4911 gsi_remove (&gsi, false);
4912 single_succ_edge (bb)->flags |= EDGE_FALLTHRU;
4916 gsi = gsi_start (stmts);
4917 if (!gsi_end_p (gsi))
4919 stmt = gsi_stmt (gsi);
4920 if (gimple_code (stmt) != GIMPLE_LABEL)
4921 stmt = NULL;
4924 elt = pointer_map_contains (lab_rtx_for_bb, bb);
4926 if (stmt || elt)
4928 last = get_last_insn ();
4930 if (stmt)
4932 expand_gimple_stmt (stmt);
4933 gsi_next (&gsi);
4936 if (elt)
4937 emit_label ((rtx) *elt);
4939 /* Java emits line number notes in the top of labels.
4940 ??? Make this go away once line number notes are obsoleted. */
4941 BB_HEAD (bb) = NEXT_INSN (last);
4942 if (NOTE_P (BB_HEAD (bb)))
4943 BB_HEAD (bb) = NEXT_INSN (BB_HEAD (bb));
4944 note = emit_note_after (NOTE_INSN_BASIC_BLOCK, BB_HEAD (bb));
4946 maybe_dump_rtl_for_gimple_stmt (stmt, last);
4948 else
4949 note = BB_HEAD (bb) = emit_note (NOTE_INSN_BASIC_BLOCK);
4951 NOTE_BASIC_BLOCK (note) = bb;
4953 for (; !gsi_end_p (gsi); gsi_next (&gsi))
4955 basic_block new_bb;
4957 stmt = gsi_stmt (gsi);
4959 /* If this statement is a non-debug one, and we generate debug
4960 insns, then this one might be the last real use of a TERed
4961 SSA_NAME, but where there are still some debug uses further
4962 down. Expanding the current SSA name in such further debug
4963 uses by their RHS might lead to wrong debug info, as coalescing
4964 might make the operands of such RHS be placed into the same
4965 pseudo as something else. Like so:
4966 a_1 = a_0 + 1; // Assume a_1 is TERed and a_0 is dead
4967 use(a_1);
4968 a_2 = ...
4969 #DEBUG ... => a_1
4970 As a_0 and a_2 don't overlap in lifetime, assume they are coalesced.
4971 If we now would expand a_1 by it's RHS (a_0 + 1) in the debug use,
4972 the write to a_2 would actually have clobbered the place which
4973 formerly held a_0.
4975 So, instead of that, we recognize the situation, and generate
4976 debug temporaries at the last real use of TERed SSA names:
4977 a_1 = a_0 + 1;
4978 #DEBUG #D1 => a_1
4979 use(a_1);
4980 a_2 = ...
4981 #DEBUG ... => #D1
4983 if (MAY_HAVE_DEBUG_INSNS
4984 && SA.values
4985 && !is_gimple_debug (stmt))
4987 ssa_op_iter iter;
4988 tree op;
4989 gimple def;
4991 location_t sloc = curr_insn_location ();
4993 /* Look for SSA names that have their last use here (TERed
4994 names always have only one real use). */
4995 FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_USE)
4996 if ((def = get_gimple_for_ssa_name (op)))
4998 imm_use_iterator imm_iter;
4999 use_operand_p use_p;
5000 bool have_debug_uses = false;
5002 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, op)
5004 if (gimple_debug_bind_p (USE_STMT (use_p)))
5006 have_debug_uses = true;
5007 break;
5011 if (have_debug_uses)
5013 /* OP is a TERed SSA name, with DEF it's defining
5014 statement, and where OP is used in further debug
5015 instructions. Generate a debug temporary, and
5016 replace all uses of OP in debug insns with that
5017 temporary. */
5018 gimple debugstmt;
5019 tree value = gimple_assign_rhs_to_tree (def);
5020 tree vexpr = make_node (DEBUG_EXPR_DECL);
5021 rtx val;
5022 enum machine_mode mode;
5024 set_curr_insn_location (gimple_location (def));
5026 DECL_ARTIFICIAL (vexpr) = 1;
5027 TREE_TYPE (vexpr) = TREE_TYPE (value);
5028 if (DECL_P (value))
5029 mode = DECL_MODE (value);
5030 else
5031 mode = TYPE_MODE (TREE_TYPE (value));
5032 DECL_MODE (vexpr) = mode;
5034 val = gen_rtx_VAR_LOCATION
5035 (mode, vexpr, (rtx)value, VAR_INIT_STATUS_INITIALIZED);
5037 emit_debug_insn (val);
5039 FOR_EACH_IMM_USE_STMT (debugstmt, imm_iter, op)
5041 if (!gimple_debug_bind_p (debugstmt))
5042 continue;
5044 FOR_EACH_IMM_USE_ON_STMT (use_p, imm_iter)
5045 SET_USE (use_p, vexpr);
5047 update_stmt (debugstmt);
5051 set_curr_insn_location (sloc);
5054 currently_expanding_gimple_stmt = stmt;
5056 /* Expand this statement, then evaluate the resulting RTL and
5057 fixup the CFG accordingly. */
5058 if (gimple_code (stmt) == GIMPLE_COND)
5060 new_bb = expand_gimple_cond (bb, stmt);
5061 if (new_bb)
5062 return new_bb;
5064 else if (gimple_debug_bind_p (stmt))
5066 location_t sloc = curr_insn_location ();
5067 gimple_stmt_iterator nsi = gsi;
5069 for (;;)
5071 tree var = gimple_debug_bind_get_var (stmt);
5072 tree value;
5073 rtx val;
5074 enum machine_mode mode;
5076 if (TREE_CODE (var) != DEBUG_EXPR_DECL
5077 && TREE_CODE (var) != LABEL_DECL
5078 && !target_for_debug_bind (var))
5079 goto delink_debug_stmt;
5081 if (gimple_debug_bind_has_value_p (stmt))
5082 value = gimple_debug_bind_get_value (stmt);
5083 else
5084 value = NULL_TREE;
5086 last = get_last_insn ();
5088 set_curr_insn_location (gimple_location (stmt));
5090 if (DECL_P (var))
5091 mode = DECL_MODE (var);
5092 else
5093 mode = TYPE_MODE (TREE_TYPE (var));
5095 val = gen_rtx_VAR_LOCATION
5096 (mode, var, (rtx)value, VAR_INIT_STATUS_INITIALIZED);
5098 emit_debug_insn (val);
5100 if (dump_file && (dump_flags & TDF_DETAILS))
5102 /* We can't dump the insn with a TREE where an RTX
5103 is expected. */
5104 PAT_VAR_LOCATION_LOC (val) = const0_rtx;
5105 maybe_dump_rtl_for_gimple_stmt (stmt, last);
5106 PAT_VAR_LOCATION_LOC (val) = (rtx)value;
5109 delink_debug_stmt:
5110 /* In order not to generate too many debug temporaries,
5111 we delink all uses of debug statements we already expanded.
5112 Therefore debug statements between definition and real
5113 use of TERed SSA names will continue to use the SSA name,
5114 and not be replaced with debug temps. */
5115 delink_stmt_imm_use (stmt);
5117 gsi = nsi;
5118 gsi_next (&nsi);
5119 if (gsi_end_p (nsi))
5120 break;
5121 stmt = gsi_stmt (nsi);
5122 if (!gimple_debug_bind_p (stmt))
5123 break;
5126 set_curr_insn_location (sloc);
5128 else if (gimple_debug_source_bind_p (stmt))
5130 location_t sloc = curr_insn_location ();
5131 tree var = gimple_debug_source_bind_get_var (stmt);
5132 tree value = gimple_debug_source_bind_get_value (stmt);
5133 rtx val;
5134 enum machine_mode mode;
5136 last = get_last_insn ();
5138 set_curr_insn_location (gimple_location (stmt));
5140 mode = DECL_MODE (var);
5142 val = gen_rtx_VAR_LOCATION (mode, var, (rtx)value,
5143 VAR_INIT_STATUS_UNINITIALIZED);
5145 emit_debug_insn (val);
5147 if (dump_file && (dump_flags & TDF_DETAILS))
5149 /* We can't dump the insn with a TREE where an RTX
5150 is expected. */
5151 PAT_VAR_LOCATION_LOC (val) = const0_rtx;
5152 maybe_dump_rtl_for_gimple_stmt (stmt, last);
5153 PAT_VAR_LOCATION_LOC (val) = (rtx)value;
5156 set_curr_insn_location (sloc);
5158 else
5160 if (is_gimple_call (stmt)
5161 && gimple_call_tail_p (stmt)
5162 && disable_tail_calls)
5163 gimple_call_set_tail (stmt, false);
5165 if (is_gimple_call (stmt) && gimple_call_tail_p (stmt))
5167 bool can_fallthru;
5168 new_bb = expand_gimple_tailcall (bb, stmt, &can_fallthru);
5169 if (new_bb)
5171 if (can_fallthru)
5172 bb = new_bb;
5173 else
5174 return new_bb;
5177 else
5179 def_operand_p def_p;
5180 def_p = SINGLE_SSA_DEF_OPERAND (stmt, SSA_OP_DEF);
5182 if (def_p != NULL)
5184 /* Ignore this stmt if it is in the list of
5185 replaceable expressions. */
5186 if (SA.values
5187 && bitmap_bit_p (SA.values,
5188 SSA_NAME_VERSION (DEF_FROM_PTR (def_p))))
5189 continue;
5191 last = expand_gimple_stmt (stmt);
5192 maybe_dump_rtl_for_gimple_stmt (stmt, last);
5197 currently_expanding_gimple_stmt = NULL;
5199 /* Expand implicit goto and convert goto_locus. */
5200 FOR_EACH_EDGE (e, ei, bb->succs)
5202 if (e->goto_locus != UNKNOWN_LOCATION)
5203 set_curr_insn_location (e->goto_locus);
5204 if ((e->flags & EDGE_FALLTHRU) && e->dest != bb->next_bb)
5206 emit_jump (label_rtx_for_bb (e->dest));
5207 e->flags &= ~EDGE_FALLTHRU;
5211 /* Expanded RTL can create a jump in the last instruction of block.
5212 This later might be assumed to be a jump to successor and break edge insertion.
5213 We need to insert dummy move to prevent this. PR41440. */
5214 if (single_succ_p (bb)
5215 && (single_succ_edge (bb)->flags & EDGE_FALLTHRU)
5216 && (last = get_last_insn ())
5217 && JUMP_P (last))
5219 rtx dummy = gen_reg_rtx (SImode);
5220 emit_insn_after_noloc (gen_move_insn (dummy, dummy), last, NULL);
5223 do_pending_stack_adjust ();
5225 /* Find the block tail. The last insn in the block is the insn
5226 before a barrier and/or table jump insn. */
5227 last = get_last_insn ();
5228 if (BARRIER_P (last))
5229 last = PREV_INSN (last);
5230 if (JUMP_TABLE_DATA_P (last))
5231 last = PREV_INSN (PREV_INSN (last));
5232 BB_END (bb) = last;
5234 update_bb_for_insn (bb);
5236 return bb;
5240 /* Create a basic block for initialization code. */
5242 static basic_block
5243 construct_init_block (void)
5245 basic_block init_block, first_block;
5246 edge e = NULL;
5247 int flags;
5249 /* Multiple entry points not supported yet. */
5250 gcc_assert (EDGE_COUNT (ENTRY_BLOCK_PTR_FOR_FN (cfun)->succs) == 1);
5251 init_rtl_bb_info (ENTRY_BLOCK_PTR_FOR_FN (cfun));
5252 init_rtl_bb_info (EXIT_BLOCK_PTR_FOR_FN (cfun));
5253 ENTRY_BLOCK_PTR_FOR_FN (cfun)->flags |= BB_RTL;
5254 EXIT_BLOCK_PTR_FOR_FN (cfun)->flags |= BB_RTL;
5256 e = EDGE_SUCC (ENTRY_BLOCK_PTR_FOR_FN (cfun), 0);
5258 /* When entry edge points to first basic block, we don't need jump,
5259 otherwise we have to jump into proper target. */
5260 if (e && e->dest != ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb)
5262 tree label = gimple_block_label (e->dest);
5264 emit_jump (label_rtx (label));
5265 flags = 0;
5267 else
5268 flags = EDGE_FALLTHRU;
5270 init_block = create_basic_block (NEXT_INSN (get_insns ()),
5271 get_last_insn (),
5272 ENTRY_BLOCK_PTR_FOR_FN (cfun));
5273 init_block->frequency = ENTRY_BLOCK_PTR_FOR_FN (cfun)->frequency;
5274 init_block->count = ENTRY_BLOCK_PTR_FOR_FN (cfun)->count;
5275 add_bb_to_loop (init_block, ENTRY_BLOCK_PTR_FOR_FN (cfun)->loop_father);
5276 if (e)
5278 first_block = e->dest;
5279 redirect_edge_succ (e, init_block);
5280 e = make_edge (init_block, first_block, flags);
5282 else
5283 e = make_edge (init_block, EXIT_BLOCK_PTR_FOR_FN (cfun), EDGE_FALLTHRU);
5284 e->probability = REG_BR_PROB_BASE;
5285 e->count = ENTRY_BLOCK_PTR_FOR_FN (cfun)->count;
5287 update_bb_for_insn (init_block);
5288 return init_block;
5291 /* For each lexical block, set BLOCK_NUMBER to the depth at which it is
5292 found in the block tree. */
5294 static void
5295 set_block_levels (tree block, int level)
5297 while (block)
5299 BLOCK_NUMBER (block) = level;
5300 set_block_levels (BLOCK_SUBBLOCKS (block), level + 1);
5301 block = BLOCK_CHAIN (block);
5305 /* Create a block containing landing pads and similar stuff. */
5307 static void
5308 construct_exit_block (void)
5310 rtx head = get_last_insn ();
5311 rtx end;
5312 basic_block exit_block;
5313 edge e, e2;
5314 unsigned ix;
5315 edge_iterator ei;
5316 basic_block prev_bb = EXIT_BLOCK_PTR_FOR_FN (cfun)->prev_bb;
5317 rtx orig_end = BB_END (prev_bb);
5319 rtl_profile_for_bb (EXIT_BLOCK_PTR_FOR_FN (cfun));
5321 /* Make sure the locus is set to the end of the function, so that
5322 epilogue line numbers and warnings are set properly. */
5323 if (LOCATION_LOCUS (cfun->function_end_locus) != UNKNOWN_LOCATION)
5324 input_location = cfun->function_end_locus;
5326 /* Generate rtl for function exit. */
5327 expand_function_end ();
5329 end = get_last_insn ();
5330 if (head == end)
5331 return;
5332 /* While emitting the function end we could move end of the last basic
5333 block. */
5334 BB_END (prev_bb) = orig_end;
5335 while (NEXT_INSN (head) && NOTE_P (NEXT_INSN (head)))
5336 head = NEXT_INSN (head);
5337 /* But make sure exit_block starts with RETURN_LABEL, otherwise the
5338 bb frequency counting will be confused. Any instructions before that
5339 label are emitted for the case where PREV_BB falls through into the
5340 exit block, so append those instructions to prev_bb in that case. */
5341 if (NEXT_INSN (head) != return_label)
5343 while (NEXT_INSN (head) != return_label)
5345 if (!NOTE_P (NEXT_INSN (head)))
5346 BB_END (prev_bb) = NEXT_INSN (head);
5347 head = NEXT_INSN (head);
5350 exit_block = create_basic_block (NEXT_INSN (head), end, prev_bb);
5351 exit_block->frequency = EXIT_BLOCK_PTR_FOR_FN (cfun)->frequency;
5352 exit_block->count = EXIT_BLOCK_PTR_FOR_FN (cfun)->count;
5353 add_bb_to_loop (exit_block, EXIT_BLOCK_PTR_FOR_FN (cfun)->loop_father);
5355 ix = 0;
5356 while (ix < EDGE_COUNT (EXIT_BLOCK_PTR_FOR_FN (cfun)->preds))
5358 e = EDGE_PRED (EXIT_BLOCK_PTR_FOR_FN (cfun), ix);
5359 if (!(e->flags & EDGE_ABNORMAL))
5360 redirect_edge_succ (e, exit_block);
5361 else
5362 ix++;
5365 e = make_edge (exit_block, EXIT_BLOCK_PTR_FOR_FN (cfun), EDGE_FALLTHRU);
5366 e->probability = REG_BR_PROB_BASE;
5367 e->count = EXIT_BLOCK_PTR_FOR_FN (cfun)->count;
5368 FOR_EACH_EDGE (e2, ei, EXIT_BLOCK_PTR_FOR_FN (cfun)->preds)
5369 if (e2 != e)
5371 e->count -= e2->count;
5372 exit_block->count -= e2->count;
5373 exit_block->frequency -= EDGE_FREQUENCY (e2);
5375 if (e->count < 0)
5376 e->count = 0;
5377 if (exit_block->count < 0)
5378 exit_block->count = 0;
5379 if (exit_block->frequency < 0)
5380 exit_block->frequency = 0;
5381 update_bb_for_insn (exit_block);
5384 /* Helper function for discover_nonconstant_array_refs.
5385 Look for ARRAY_REF nodes with non-constant indexes and mark them
5386 addressable. */
5388 static tree
5389 discover_nonconstant_array_refs_r (tree * tp, int *walk_subtrees,
5390 void *data ATTRIBUTE_UNUSED)
5392 tree t = *tp;
5394 if (IS_TYPE_OR_DECL_P (t))
5395 *walk_subtrees = 0;
5396 else if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
5398 while (((TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
5399 && is_gimple_min_invariant (TREE_OPERAND (t, 1))
5400 && (!TREE_OPERAND (t, 2)
5401 || is_gimple_min_invariant (TREE_OPERAND (t, 2))))
5402 || (TREE_CODE (t) == COMPONENT_REF
5403 && (!TREE_OPERAND (t,2)
5404 || is_gimple_min_invariant (TREE_OPERAND (t, 2))))
5405 || TREE_CODE (t) == BIT_FIELD_REF
5406 || TREE_CODE (t) == REALPART_EXPR
5407 || TREE_CODE (t) == IMAGPART_EXPR
5408 || TREE_CODE (t) == VIEW_CONVERT_EXPR
5409 || CONVERT_EXPR_P (t))
5410 t = TREE_OPERAND (t, 0);
5412 if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
5414 t = get_base_address (t);
5415 if (t && DECL_P (t)
5416 && DECL_MODE (t) != BLKmode)
5417 TREE_ADDRESSABLE (t) = 1;
5420 *walk_subtrees = 0;
5423 return NULL_TREE;
5426 /* RTL expansion is not able to compile array references with variable
5427 offsets for arrays stored in single register. Discover such
5428 expressions and mark variables as addressable to avoid this
5429 scenario. */
5431 static void
5432 discover_nonconstant_array_refs (void)
5434 basic_block bb;
5435 gimple_stmt_iterator gsi;
5437 FOR_EACH_BB_FN (bb, cfun)
5438 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
5440 gimple stmt = gsi_stmt (gsi);
5441 if (!is_gimple_debug (stmt))
5442 walk_gimple_op (stmt, discover_nonconstant_array_refs_r, NULL);
5446 /* This function sets crtl->args.internal_arg_pointer to a virtual
5447 register if DRAP is needed. Local register allocator will replace
5448 virtual_incoming_args_rtx with the virtual register. */
5450 static void
5451 expand_stack_alignment (void)
5453 rtx drap_rtx;
5454 unsigned int preferred_stack_boundary;
5456 if (! SUPPORTS_STACK_ALIGNMENT)
5457 return;
5459 if (cfun->calls_alloca
5460 || cfun->has_nonlocal_label
5461 || crtl->has_nonlocal_goto)
5462 crtl->need_drap = true;
5464 /* Call update_stack_boundary here again to update incoming stack
5465 boundary. It may set incoming stack alignment to a different
5466 value after RTL expansion. TARGET_FUNCTION_OK_FOR_SIBCALL may
5467 use the minimum incoming stack alignment to check if it is OK
5468 to perform sibcall optimization since sibcall optimization will
5469 only align the outgoing stack to incoming stack boundary. */
5470 if (targetm.calls.update_stack_boundary)
5471 targetm.calls.update_stack_boundary ();
5473 /* The incoming stack frame has to be aligned at least at
5474 parm_stack_boundary. */
5475 gcc_assert (crtl->parm_stack_boundary <= INCOMING_STACK_BOUNDARY);
5477 /* Update crtl->stack_alignment_estimated and use it later to align
5478 stack. We check PREFERRED_STACK_BOUNDARY if there may be non-call
5479 exceptions since callgraph doesn't collect incoming stack alignment
5480 in this case. */
5481 if (cfun->can_throw_non_call_exceptions
5482 && PREFERRED_STACK_BOUNDARY > crtl->preferred_stack_boundary)
5483 preferred_stack_boundary = PREFERRED_STACK_BOUNDARY;
5484 else
5485 preferred_stack_boundary = crtl->preferred_stack_boundary;
5486 if (preferred_stack_boundary > crtl->stack_alignment_estimated)
5487 crtl->stack_alignment_estimated = preferred_stack_boundary;
5488 if (preferred_stack_boundary > crtl->stack_alignment_needed)
5489 crtl->stack_alignment_needed = preferred_stack_boundary;
5491 gcc_assert (crtl->stack_alignment_needed
5492 <= crtl->stack_alignment_estimated);
5494 crtl->stack_realign_needed
5495 = INCOMING_STACK_BOUNDARY < crtl->stack_alignment_estimated;
5496 crtl->stack_realign_tried = crtl->stack_realign_needed;
5498 crtl->stack_realign_processed = true;
5500 /* Target has to redefine TARGET_GET_DRAP_RTX to support stack
5501 alignment. */
5502 gcc_assert (targetm.calls.get_drap_rtx != NULL);
5503 drap_rtx = targetm.calls.get_drap_rtx ();
5505 /* stack_realign_drap and drap_rtx must match. */
5506 gcc_assert ((stack_realign_drap != 0) == (drap_rtx != NULL));
5508 /* Do nothing if NULL is returned, which means DRAP is not needed. */
5509 if (NULL != drap_rtx)
5511 crtl->args.internal_arg_pointer = drap_rtx;
5513 /* Call fixup_tail_calls to clean up REG_EQUIV note if DRAP is
5514 needed. */
5515 fixup_tail_calls ();
5520 static void
5521 expand_main_function (void)
5523 #if (defined(INVOKE__main) \
5524 || (!defined(HAS_INIT_SECTION) \
5525 && !defined(INIT_SECTION_ASM_OP) \
5526 && !defined(INIT_ARRAY_SECTION_ASM_OP)))
5527 emit_library_call (init_one_libfunc (NAME__MAIN), LCT_NORMAL, VOIDmode, 0);
5528 #endif
5532 /* Expand code to initialize the stack_protect_guard. This is invoked at
5533 the beginning of a function to be protected. */
5535 #ifndef HAVE_stack_protect_set
5536 # define HAVE_stack_protect_set 0
5537 # define gen_stack_protect_set(x,y) (gcc_unreachable (), NULL_RTX)
5538 #endif
5540 static void
5541 stack_protect_prologue (void)
5543 tree guard_decl = targetm.stack_protect_guard ();
5544 rtx x, y;
5546 x = expand_normal (crtl->stack_protect_guard);
5547 y = expand_normal (guard_decl);
5549 /* Allow the target to copy from Y to X without leaking Y into a
5550 register. */
5551 if (HAVE_stack_protect_set)
5553 rtx insn = gen_stack_protect_set (x, y);
5554 if (insn)
5556 emit_insn (insn);
5557 return;
5561 /* Otherwise do a straight move. */
5562 emit_move_insn (x, y);
5565 /* Translate the intermediate representation contained in the CFG
5566 from GIMPLE trees to RTL.
5568 We do conversion per basic block and preserve/update the tree CFG.
5569 This implies we have to do some magic as the CFG can simultaneously
5570 consist of basic blocks containing RTL and GIMPLE trees. This can
5571 confuse the CFG hooks, so be careful to not manipulate CFG during
5572 the expansion. */
5574 namespace {
5576 const pass_data pass_data_expand =
5578 RTL_PASS, /* type */
5579 "expand", /* name */
5580 OPTGROUP_NONE, /* optinfo_flags */
5581 true, /* has_execute */
5582 TV_EXPAND, /* tv_id */
5583 ( PROP_ssa | PROP_gimple_leh | PROP_cfg
5584 | PROP_gimple_lcx
5585 | PROP_gimple_lvec ), /* properties_required */
5586 PROP_rtl, /* properties_provided */
5587 ( PROP_ssa | PROP_trees ), /* properties_destroyed */
5588 0, /* todo_flags_start */
5589 0, /* todo_flags_finish */
5592 class pass_expand : public rtl_opt_pass
5594 public:
5595 pass_expand (gcc::context *ctxt)
5596 : rtl_opt_pass (pass_data_expand, ctxt)
5599 /* opt_pass methods: */
5600 virtual unsigned int execute (function *);
5602 }; // class pass_expand
5604 unsigned int
5605 pass_expand::execute (function *fun)
5607 basic_block bb, init_block;
5608 sbitmap blocks;
5609 edge_iterator ei;
5610 edge e;
5611 rtx var_seq, var_ret_seq;
5612 unsigned i;
5614 timevar_push (TV_OUT_OF_SSA);
5615 rewrite_out_of_ssa (&SA);
5616 timevar_pop (TV_OUT_OF_SSA);
5617 SA.partition_to_pseudo = XCNEWVEC (rtx, SA.map->num_partitions);
5619 /* Make sure all values used by the optimization passes have sane
5620 defaults. */
5621 reg_renumber = 0;
5623 /* Some backends want to know that we are expanding to RTL. */
5624 currently_expanding_to_rtl = 1;
5625 /* Dominators are not kept up-to-date as we may create new basic-blocks. */
5626 free_dominance_info (CDI_DOMINATORS);
5628 rtl_profile_for_bb (ENTRY_BLOCK_PTR_FOR_FN (fun));
5630 insn_locations_init ();
5631 if (!DECL_IS_BUILTIN (current_function_decl))
5633 /* Eventually, all FEs should explicitly set function_start_locus. */
5634 if (LOCATION_LOCUS (fun->function_start_locus) == UNKNOWN_LOCATION)
5635 set_curr_insn_location
5636 (DECL_SOURCE_LOCATION (current_function_decl));
5637 else
5638 set_curr_insn_location (fun->function_start_locus);
5640 else
5641 set_curr_insn_location (UNKNOWN_LOCATION);
5642 prologue_location = curr_insn_location ();
5644 #ifdef INSN_SCHEDULING
5645 init_sched_attrs ();
5646 #endif
5648 /* Make sure first insn is a note even if we don't want linenums.
5649 This makes sure the first insn will never be deleted.
5650 Also, final expects a note to appear there. */
5651 emit_note (NOTE_INSN_DELETED);
5653 /* Mark arrays indexed with non-constant indices with TREE_ADDRESSABLE. */
5654 discover_nonconstant_array_refs ();
5656 targetm.expand_to_rtl_hook ();
5657 crtl->stack_alignment_needed = STACK_BOUNDARY;
5658 crtl->max_used_stack_slot_alignment = STACK_BOUNDARY;
5659 crtl->stack_alignment_estimated = 0;
5660 crtl->preferred_stack_boundary = STACK_BOUNDARY;
5661 fun->cfg->max_jumptable_ents = 0;
5663 /* Resovle the function section. Some targets, like ARM EABI rely on knowledge
5664 of the function section at exapnsion time to predict distance of calls. */
5665 resolve_unique_section (current_function_decl, 0, flag_function_sections);
5667 /* Expand the variables recorded during gimple lowering. */
5668 timevar_push (TV_VAR_EXPAND);
5669 start_sequence ();
5671 var_ret_seq = expand_used_vars ();
5673 var_seq = get_insns ();
5674 end_sequence ();
5675 timevar_pop (TV_VAR_EXPAND);
5677 /* Honor stack protection warnings. */
5678 if (warn_stack_protect)
5680 if (fun->calls_alloca)
5681 warning (OPT_Wstack_protector,
5682 "stack protector not protecting local variables: "
5683 "variable length buffer");
5684 if (has_short_buffer && !crtl->stack_protect_guard)
5685 warning (OPT_Wstack_protector,
5686 "stack protector not protecting function: "
5687 "all local arrays are less than %d bytes long",
5688 (int) PARAM_VALUE (PARAM_SSP_BUFFER_SIZE));
5691 /* Set up parameters and prepare for return, for the function. */
5692 expand_function_start (current_function_decl);
5694 /* If we emitted any instructions for setting up the variables,
5695 emit them before the FUNCTION_START note. */
5696 if (var_seq)
5698 emit_insn_before (var_seq, parm_birth_insn);
5700 /* In expand_function_end we'll insert the alloca save/restore
5701 before parm_birth_insn. We've just insertted an alloca call.
5702 Adjust the pointer to match. */
5703 parm_birth_insn = var_seq;
5706 /* Now that we also have the parameter RTXs, copy them over to our
5707 partitions. */
5708 for (i = 0; i < SA.map->num_partitions; i++)
5710 tree var = SSA_NAME_VAR (partition_to_var (SA.map, i));
5712 if (TREE_CODE (var) != VAR_DECL
5713 && !SA.partition_to_pseudo[i])
5714 SA.partition_to_pseudo[i] = DECL_RTL_IF_SET (var);
5715 gcc_assert (SA.partition_to_pseudo[i]);
5717 /* If this decl was marked as living in multiple places, reset
5718 this now to NULL. */
5719 if (DECL_RTL_IF_SET (var) == pc_rtx)
5720 SET_DECL_RTL (var, NULL);
5722 /* Some RTL parts really want to look at DECL_RTL(x) when x
5723 was a decl marked in REG_ATTR or MEM_ATTR. We could use
5724 SET_DECL_RTL here making this available, but that would mean
5725 to select one of the potentially many RTLs for one DECL. Instead
5726 of doing that we simply reset the MEM_EXPR of the RTL in question,
5727 then nobody can get at it and hence nobody can call DECL_RTL on it. */
5728 if (!DECL_RTL_SET_P (var))
5730 if (MEM_P (SA.partition_to_pseudo[i]))
5731 set_mem_expr (SA.partition_to_pseudo[i], NULL);
5735 /* If we have a class containing differently aligned pointers
5736 we need to merge those into the corresponding RTL pointer
5737 alignment. */
5738 for (i = 1; i < num_ssa_names; i++)
5740 tree name = ssa_name (i);
5741 int part;
5742 rtx r;
5744 if (!name
5745 /* We might have generated new SSA names in
5746 update_alias_info_with_stack_vars. They will have a NULL
5747 defining statements, and won't be part of the partitioning,
5748 so ignore those. */
5749 || !SSA_NAME_DEF_STMT (name))
5750 continue;
5751 part = var_to_partition (SA.map, name);
5752 if (part == NO_PARTITION)
5753 continue;
5755 /* Adjust all partition members to get the underlying decl of
5756 the representative which we might have created in expand_one_var. */
5757 if (SSA_NAME_VAR (name) == NULL_TREE)
5759 tree leader = partition_to_var (SA.map, part);
5760 gcc_assert (SSA_NAME_VAR (leader) != NULL_TREE);
5761 replace_ssa_name_symbol (name, SSA_NAME_VAR (leader));
5763 if (!POINTER_TYPE_P (TREE_TYPE (name)))
5764 continue;
5766 r = SA.partition_to_pseudo[part];
5767 if (REG_P (r))
5768 mark_reg_pointer (r, get_pointer_alignment (name));
5771 /* If this function is `main', emit a call to `__main'
5772 to run global initializers, etc. */
5773 if (DECL_NAME (current_function_decl)
5774 && MAIN_NAME_P (DECL_NAME (current_function_decl))
5775 && DECL_FILE_SCOPE_P (current_function_decl))
5776 expand_main_function ();
5778 /* Initialize the stack_protect_guard field. This must happen after the
5779 call to __main (if any) so that the external decl is initialized. */
5780 if (crtl->stack_protect_guard)
5781 stack_protect_prologue ();
5783 expand_phi_nodes (&SA);
5785 /* Register rtl specific functions for cfg. */
5786 rtl_register_cfg_hooks ();
5788 init_block = construct_init_block ();
5790 /* Clear EDGE_EXECUTABLE on the entry edge(s). It is cleaned from the
5791 remaining edges later. */
5792 FOR_EACH_EDGE (e, ei, ENTRY_BLOCK_PTR_FOR_FN (fun)->succs)
5793 e->flags &= ~EDGE_EXECUTABLE;
5795 lab_rtx_for_bb = pointer_map_create ();
5796 FOR_BB_BETWEEN (bb, init_block->next_bb, EXIT_BLOCK_PTR_FOR_FN (fun),
5797 next_bb)
5798 bb = expand_gimple_basic_block (bb, var_ret_seq != NULL_RTX);
5800 if (MAY_HAVE_DEBUG_INSNS)
5801 expand_debug_locations ();
5803 /* Free stuff we no longer need after GIMPLE optimizations. */
5804 free_dominance_info (CDI_DOMINATORS);
5805 free_dominance_info (CDI_POST_DOMINATORS);
5806 delete_tree_cfg_annotations ();
5808 timevar_push (TV_OUT_OF_SSA);
5809 finish_out_of_ssa (&SA);
5810 timevar_pop (TV_OUT_OF_SSA);
5812 timevar_push (TV_POST_EXPAND);
5813 /* We are no longer in SSA form. */
5814 fun->gimple_df->in_ssa_p = false;
5815 loops_state_clear (LOOP_CLOSED_SSA);
5817 /* Expansion is used by optimization passes too, set maybe_hot_insn_p
5818 conservatively to true until they are all profile aware. */
5819 pointer_map_destroy (lab_rtx_for_bb);
5820 free_histograms ();
5822 construct_exit_block ();
5823 insn_locations_finalize ();
5825 if (var_ret_seq)
5827 rtx after = return_label;
5828 rtx next = NEXT_INSN (after);
5829 if (next && NOTE_INSN_BASIC_BLOCK_P (next))
5830 after = next;
5831 emit_insn_after (var_ret_seq, after);
5834 /* Zap the tree EH table. */
5835 set_eh_throw_stmt_table (fun, NULL);
5837 /* We need JUMP_LABEL be set in order to redirect jumps, and hence
5838 split edges which edge insertions might do. */
5839 rebuild_jump_labels (get_insns ());
5841 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (fun),
5842 EXIT_BLOCK_PTR_FOR_FN (fun), next_bb)
5844 edge e;
5845 edge_iterator ei;
5846 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
5848 if (e->insns.r)
5850 rebuild_jump_labels_chain (e->insns.r);
5851 /* Put insns after parm birth, but before
5852 NOTE_INSNS_FUNCTION_BEG. */
5853 if (e->src == ENTRY_BLOCK_PTR_FOR_FN (fun)
5854 && single_succ_p (ENTRY_BLOCK_PTR_FOR_FN (fun)))
5856 rtx insns = e->insns.r;
5857 e->insns.r = NULL_RTX;
5858 if (NOTE_P (parm_birth_insn)
5859 && NOTE_KIND (parm_birth_insn) == NOTE_INSN_FUNCTION_BEG)
5860 emit_insn_before_noloc (insns, parm_birth_insn, e->dest);
5861 else
5862 emit_insn_after_noloc (insns, parm_birth_insn, e->dest);
5864 else
5865 commit_one_edge_insertion (e);
5867 else
5868 ei_next (&ei);
5872 /* We're done expanding trees to RTL. */
5873 currently_expanding_to_rtl = 0;
5875 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (fun)->next_bb,
5876 EXIT_BLOCK_PTR_FOR_FN (fun), next_bb)
5878 edge e;
5879 edge_iterator ei;
5880 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
5882 /* Clear EDGE_EXECUTABLE. This flag is never used in the backend. */
5883 e->flags &= ~EDGE_EXECUTABLE;
5885 /* At the moment not all abnormal edges match the RTL
5886 representation. It is safe to remove them here as
5887 find_many_sub_basic_blocks will rediscover them.
5888 In the future we should get this fixed properly. */
5889 if ((e->flags & EDGE_ABNORMAL)
5890 && !(e->flags & EDGE_SIBCALL))
5891 remove_edge (e);
5892 else
5893 ei_next (&ei);
5897 blocks = sbitmap_alloc (last_basic_block_for_fn (fun));
5898 bitmap_ones (blocks);
5899 find_many_sub_basic_blocks (blocks);
5900 sbitmap_free (blocks);
5901 purge_all_dead_edges ();
5903 expand_stack_alignment ();
5905 /* Fixup REG_EQUIV notes in the prologue if there are tailcalls in this
5906 function. */
5907 if (crtl->tail_call_emit)
5908 fixup_tail_calls ();
5910 /* After initial rtl generation, call back to finish generating
5911 exception support code. We need to do this before cleaning up
5912 the CFG as the code does not expect dead landing pads. */
5913 if (fun->eh->region_tree != NULL)
5914 finish_eh_generation ();
5916 /* Remove unreachable blocks, otherwise we cannot compute dominators
5917 which are needed for loop state verification. As a side-effect
5918 this also compacts blocks.
5919 ??? We cannot remove trivially dead insns here as for example
5920 the DRAP reg on i?86 is not magically live at this point.
5921 gcc.c-torture/execute/ipa-sra-2.c execution, -Os -m32 fails otherwise. */
5922 cleanup_cfg (CLEANUP_NO_INSN_DEL);
5924 #ifdef ENABLE_CHECKING
5925 verify_flow_info ();
5926 #endif
5928 /* Initialize pseudos allocated for hard registers. */
5929 emit_initial_value_sets ();
5931 /* And finally unshare all RTL. */
5932 unshare_all_rtl ();
5934 /* There's no need to defer outputting this function any more; we
5935 know we want to output it. */
5936 DECL_DEFER_OUTPUT (current_function_decl) = 0;
5938 /* Now that we're done expanding trees to RTL, we shouldn't have any
5939 more CONCATs anywhere. */
5940 generating_concat_p = 0;
5942 if (dump_file)
5944 fprintf (dump_file,
5945 "\n\n;;\n;; Full RTL generated for this function:\n;;\n");
5946 /* And the pass manager will dump RTL for us. */
5949 /* If we're emitting a nested function, make sure its parent gets
5950 emitted as well. Doing otherwise confuses debug info. */
5952 tree parent;
5953 for (parent = DECL_CONTEXT (current_function_decl);
5954 parent != NULL_TREE;
5955 parent = get_containing_scope (parent))
5956 if (TREE_CODE (parent) == FUNCTION_DECL)
5957 TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (parent)) = 1;
5960 /* We are now committed to emitting code for this function. Do any
5961 preparation, such as emitting abstract debug info for the inline
5962 before it gets mangled by optimization. */
5963 if (cgraph_function_possibly_inlined_p (current_function_decl))
5964 (*debug_hooks->outlining_inline_function) (current_function_decl);
5966 TREE_ASM_WRITTEN (current_function_decl) = 1;
5968 /* After expanding, the return labels are no longer needed. */
5969 return_label = NULL;
5970 naked_return_label = NULL;
5972 /* After expanding, the tm_restart map is no longer needed. */
5973 if (fun->gimple_df->tm_restart)
5975 htab_delete (fun->gimple_df->tm_restart);
5976 fun->gimple_df->tm_restart = NULL;
5979 /* Tag the blocks with a depth number so that change_scope can find
5980 the common parent easily. */
5981 set_block_levels (DECL_INITIAL (fun->decl), 0);
5982 default_rtl_profile ();
5984 timevar_pop (TV_POST_EXPAND);
5986 return 0;
5989 } // anon namespace
5991 rtl_opt_pass *
5992 make_pass_expand (gcc::context *ctxt)
5994 return new pass_expand (ctxt);