Merge branch 'master' r217593-r217725 into gimple-classes-v2-option-3
[official-gcc.git] / gcc / cfgexpand.c
blob45c13b48ae3caa51c1e3af73dd7d551da6c2b5fb
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 "predict.h"
34 #include "vec.h"
35 #include "hashtab.h"
36 #include "hash-set.h"
37 #include "machmode.h"
38 #include "input.h"
39 #include "function.h"
40 #include "dominance.h"
41 #include "cfg.h"
42 #include "cfgrtl.h"
43 #include "cfganal.h"
44 #include "cfgbuild.h"
45 #include "cfgcleanup.h"
46 #include "basic-block.h"
47 #include "insn-codes.h"
48 #include "optabs.h"
49 #include "expr.h"
50 #include "langhooks.h"
51 #include "bitmap.h"
52 #include "tree-ssa-alias.h"
53 #include "internal-fn.h"
54 #include "tree-eh.h"
55 #include "gimple-expr.h"
56 #include "is-a.h"
57 #include "gimple.h"
58 #include "gimple-iterator.h"
59 #include "gimple-walk.h"
60 #include "gimple-ssa.h"
61 #include "hash-map.h"
62 #include "plugin-api.h"
63 #include "ipa-ref.h"
64 #include "cgraph.h"
65 #include "tree-cfg.h"
66 #include "tree-phinodes.h"
67 #include "ssa-iterators.h"
68 #include "tree-ssanames.h"
69 #include "tree-dfa.h"
70 #include "tree-ssa.h"
71 #include "tree-pass.h"
72 #include "except.h"
73 #include "flags.h"
74 #include "diagnostic.h"
75 #include "gimple-pretty-print.h"
76 #include "toplev.h"
77 #include "debug.h"
78 #include "params.h"
79 #include "tree-inline.h"
80 #include "value-prof.h"
81 #include "target.h"
82 #include "tree-ssa-live.h"
83 #include "tree-outof-ssa.h"
84 #include "sbitmap.h"
85 #include "cfgloop.h"
86 #include "regs.h" /* For reg_renumber. */
87 #include "insn-attr.h" /* For INSN_SCHEDULING. */
88 #include "asan.h"
89 #include "tree-ssa-address.h"
90 #include "recog.h"
91 #include "output.h"
92 #include "builtins.h"
93 #include "tree-chkp.h"
94 #include "rtl-chkp.h"
96 /* Some systems use __main in a way incompatible with its use in gcc, in these
97 cases use the macros NAME__MAIN to give a quoted symbol and SYMBOL__MAIN to
98 give the same symbol without quotes for an alternative entry point. You
99 must define both, or neither. */
100 #ifndef NAME__MAIN
101 #define NAME__MAIN "__main"
102 #endif
104 /* This variable holds information helping the rewriting of SSA trees
105 into RTL. */
106 struct ssaexpand SA;
108 /* This variable holds the currently expanded gimple statement for purposes
109 of comminucating the profile info to the builtin expanders. */
110 gimple currently_expanding_gimple_stmt;
112 static rtx expand_debug_expr (tree);
114 /* Return an expression tree corresponding to the RHS of GIMPLE
115 statement STMT. */
117 tree
118 gimple_assign_rhs_to_tree (gimple stmt)
120 tree t;
121 enum gimple_rhs_class grhs_class;
123 grhs_class = get_gimple_rhs_class (gimple_expr_code (stmt));
125 if (grhs_class == GIMPLE_TERNARY_RHS)
126 t = build3 (gimple_assign_rhs_code (stmt),
127 TREE_TYPE (gimple_assign_lhs (stmt)),
128 gimple_assign_rhs1 (stmt),
129 gimple_assign_rhs2 (stmt),
130 gimple_assign_rhs3 (stmt));
131 else if (grhs_class == GIMPLE_BINARY_RHS)
132 t = build2 (gimple_assign_rhs_code (stmt),
133 TREE_TYPE (gimple_assign_lhs (stmt)),
134 gimple_assign_rhs1 (stmt),
135 gimple_assign_rhs2 (stmt));
136 else if (grhs_class == GIMPLE_UNARY_RHS)
137 t = build1 (gimple_assign_rhs_code (stmt),
138 TREE_TYPE (gimple_assign_lhs (stmt)),
139 gimple_assign_rhs1 (stmt));
140 else if (grhs_class == GIMPLE_SINGLE_RHS)
142 t = gimple_assign_rhs1 (stmt);
143 /* Avoid modifying this tree in place below. */
144 if ((gimple_has_location (stmt) && CAN_HAVE_LOCATION_P (t)
145 && gimple_location (stmt) != EXPR_LOCATION (t))
146 || (gimple_block (stmt)
147 && currently_expanding_to_rtl
148 && EXPR_P (t)))
149 t = copy_node (t);
151 else
152 gcc_unreachable ();
154 if (gimple_has_location (stmt) && CAN_HAVE_LOCATION_P (t))
155 SET_EXPR_LOCATION (t, gimple_location (stmt));
157 return t;
161 #ifndef STACK_ALIGNMENT_NEEDED
162 #define STACK_ALIGNMENT_NEEDED 1
163 #endif
165 #define SSAVAR(x) (TREE_CODE (x) == SSA_NAME ? SSA_NAME_VAR (x) : x)
167 /* Associate declaration T with storage space X. If T is no
168 SSA name this is exactly SET_DECL_RTL, otherwise make the
169 partition of T associated with X. */
170 static inline void
171 set_rtl (tree t, rtx x)
173 if (TREE_CODE (t) == SSA_NAME)
175 SA.partition_to_pseudo[var_to_partition (SA.map, t)] = x;
176 if (x && !MEM_P (x))
177 set_reg_attrs_for_decl_rtl (SSA_NAME_VAR (t), x);
178 /* For the benefit of debug information at -O0 (where vartracking
179 doesn't run) record the place also in the base DECL if it's
180 a normal variable (not a parameter). */
181 if (x && x != pc_rtx && TREE_CODE (SSA_NAME_VAR (t)) == VAR_DECL)
183 tree var = SSA_NAME_VAR (t);
184 /* If we don't yet have something recorded, just record it now. */
185 if (!DECL_RTL_SET_P (var))
186 SET_DECL_RTL (var, x);
187 /* If we have it set already to "multiple places" don't
188 change this. */
189 else if (DECL_RTL (var) == pc_rtx)
191 /* If we have something recorded and it's not the same place
192 as we want to record now, we have multiple partitions for the
193 same base variable, with different places. We can't just
194 randomly chose one, hence we have to say that we don't know.
195 This only happens with optimization, and there var-tracking
196 will figure out the right thing. */
197 else if (DECL_RTL (var) != x)
198 SET_DECL_RTL (var, pc_rtx);
201 else
202 SET_DECL_RTL (t, x);
205 /* This structure holds data relevant to one variable that will be
206 placed in a stack slot. */
207 struct stack_var
209 /* The Variable. */
210 tree decl;
212 /* Initially, the size of the variable. Later, the size of the partition,
213 if this variable becomes it's partition's representative. */
214 HOST_WIDE_INT size;
216 /* The *byte* alignment required for this variable. Or as, with the
217 size, the alignment for this partition. */
218 unsigned int alignb;
220 /* The partition representative. */
221 size_t representative;
223 /* The next stack variable in the partition, or EOC. */
224 size_t next;
226 /* The numbers of conflicting stack variables. */
227 bitmap conflicts;
230 #define EOC ((size_t)-1)
232 /* We have an array of such objects while deciding allocation. */
233 static struct stack_var *stack_vars;
234 static size_t stack_vars_alloc;
235 static size_t stack_vars_num;
236 static hash_map<tree, size_t> *decl_to_stack_part;
238 /* Conflict bitmaps go on this obstack. This allows us to destroy
239 all of them in one big sweep. */
240 static bitmap_obstack stack_var_bitmap_obstack;
242 /* An array of indices such that stack_vars[stack_vars_sorted[i]].size
243 is non-decreasing. */
244 static size_t *stack_vars_sorted;
246 /* The phase of the stack frame. This is the known misalignment of
247 virtual_stack_vars_rtx from PREFERRED_STACK_BOUNDARY. That is,
248 (frame_offset+frame_phase) % PREFERRED_STACK_BOUNDARY == 0. */
249 static int frame_phase;
251 /* Used during expand_used_vars to remember if we saw any decls for
252 which we'd like to enable stack smashing protection. */
253 static bool has_protected_decls;
255 /* Used during expand_used_vars. Remember if we say a character buffer
256 smaller than our cutoff threshold. Used for -Wstack-protector. */
257 static bool has_short_buffer;
259 /* Compute the byte alignment to use for DECL. Ignore alignment
260 we can't do with expected alignment of the stack boundary. */
262 static unsigned int
263 align_local_variable (tree decl)
265 unsigned int align = LOCAL_DECL_ALIGNMENT (decl);
266 DECL_ALIGN (decl) = align;
267 return align / BITS_PER_UNIT;
270 /* Allocate SIZE bytes at byte alignment ALIGN from the stack frame.
271 Return the frame offset. */
273 static HOST_WIDE_INT
274 alloc_stack_frame_space (HOST_WIDE_INT size, unsigned HOST_WIDE_INT align)
276 HOST_WIDE_INT offset, new_frame_offset;
278 new_frame_offset = frame_offset;
279 if (FRAME_GROWS_DOWNWARD)
281 new_frame_offset -= size + frame_phase;
282 new_frame_offset &= -align;
283 new_frame_offset += frame_phase;
284 offset = new_frame_offset;
286 else
288 new_frame_offset -= frame_phase;
289 new_frame_offset += align - 1;
290 new_frame_offset &= -align;
291 new_frame_offset += frame_phase;
292 offset = new_frame_offset;
293 new_frame_offset += size;
295 frame_offset = new_frame_offset;
297 if (frame_offset_overflow (frame_offset, cfun->decl))
298 frame_offset = offset = 0;
300 return offset;
303 /* Accumulate DECL into STACK_VARS. */
305 static void
306 add_stack_var (tree decl)
308 struct stack_var *v;
310 if (stack_vars_num >= stack_vars_alloc)
312 if (stack_vars_alloc)
313 stack_vars_alloc = stack_vars_alloc * 3 / 2;
314 else
315 stack_vars_alloc = 32;
316 stack_vars
317 = XRESIZEVEC (struct stack_var, stack_vars, stack_vars_alloc);
319 if (!decl_to_stack_part)
320 decl_to_stack_part = new hash_map<tree, size_t>;
322 v = &stack_vars[stack_vars_num];
323 decl_to_stack_part->put (decl, stack_vars_num);
325 v->decl = decl;
326 v->size = tree_to_uhwi (DECL_SIZE_UNIT (SSAVAR (decl)));
327 /* Ensure that all variables have size, so that &a != &b for any two
328 variables that are simultaneously live. */
329 if (v->size == 0)
330 v->size = 1;
331 v->alignb = align_local_variable (SSAVAR (decl));
332 /* An alignment of zero can mightily confuse us later. */
333 gcc_assert (v->alignb != 0);
335 /* All variables are initially in their own partition. */
336 v->representative = stack_vars_num;
337 v->next = EOC;
339 /* All variables initially conflict with no other. */
340 v->conflicts = NULL;
342 /* Ensure that this decl doesn't get put onto the list twice. */
343 set_rtl (decl, pc_rtx);
345 stack_vars_num++;
348 /* Make the decls associated with luid's X and Y conflict. */
350 static void
351 add_stack_var_conflict (size_t x, size_t y)
353 struct stack_var *a = &stack_vars[x];
354 struct stack_var *b = &stack_vars[y];
355 if (!a->conflicts)
356 a->conflicts = BITMAP_ALLOC (&stack_var_bitmap_obstack);
357 if (!b->conflicts)
358 b->conflicts = BITMAP_ALLOC (&stack_var_bitmap_obstack);
359 bitmap_set_bit (a->conflicts, y);
360 bitmap_set_bit (b->conflicts, x);
363 /* Check whether the decls associated with luid's X and Y conflict. */
365 static bool
366 stack_var_conflict_p (size_t x, size_t y)
368 struct stack_var *a = &stack_vars[x];
369 struct stack_var *b = &stack_vars[y];
370 if (x == y)
371 return false;
372 /* Partitions containing an SSA name result from gimple registers
373 with things like unsupported modes. They are top-level and
374 hence conflict with everything else. */
375 if (TREE_CODE (a->decl) == SSA_NAME || TREE_CODE (b->decl) == SSA_NAME)
376 return true;
378 if (!a->conflicts || !b->conflicts)
379 return false;
380 return bitmap_bit_p (a->conflicts, y);
383 /* Callback for walk_stmt_ops. If OP is a decl touched by add_stack_var
384 enter its partition number into bitmap DATA. */
386 static bool
387 visit_op (gimple, tree op, tree, void *data)
389 bitmap active = (bitmap)data;
390 op = get_base_address (op);
391 if (op
392 && DECL_P (op)
393 && DECL_RTL_IF_SET (op) == pc_rtx)
395 size_t *v = decl_to_stack_part->get (op);
396 if (v)
397 bitmap_set_bit (active, *v);
399 return false;
402 /* Callback for walk_stmt_ops. If OP is a decl touched by add_stack_var
403 record conflicts between it and all currently active other partitions
404 from bitmap DATA. */
406 static bool
407 visit_conflict (gimple, tree op, tree, void *data)
409 bitmap active = (bitmap)data;
410 op = get_base_address (op);
411 if (op
412 && DECL_P (op)
413 && DECL_RTL_IF_SET (op) == pc_rtx)
415 size_t *v = decl_to_stack_part->get (op);
416 if (v && bitmap_set_bit (active, *v))
418 size_t num = *v;
419 bitmap_iterator bi;
420 unsigned i;
421 gcc_assert (num < stack_vars_num);
422 EXECUTE_IF_SET_IN_BITMAP (active, 0, i, bi)
423 add_stack_var_conflict (num, i);
426 return false;
429 /* Helper routine for add_scope_conflicts, calculating the active partitions
430 at the end of BB, leaving the result in WORK. We're called to generate
431 conflicts when FOR_CONFLICT is true, otherwise we're just tracking
432 liveness. */
434 static void
435 add_scope_conflicts_1 (basic_block bb, bitmap work, bool for_conflict)
437 edge e;
438 edge_iterator ei;
439 gimple_stmt_iterator gsi;
440 walk_stmt_load_store_addr_fn visit;
442 bitmap_clear (work);
443 FOR_EACH_EDGE (e, ei, bb->preds)
444 bitmap_ior_into (work, (bitmap)e->src->aux);
446 visit = visit_op;
448 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
450 gimple stmt = gsi_stmt (gsi);
451 walk_stmt_load_store_addr_ops (stmt, work, NULL, NULL, visit);
453 for (gsi = gsi_after_labels (bb); !gsi_end_p (gsi); gsi_next (&gsi))
455 gimple stmt = gsi_stmt (gsi);
457 if (gimple_clobber_p (stmt))
459 tree lhs = gimple_assign_lhs (stmt);
460 size_t *v;
461 /* Nested function lowering might introduce LHSs
462 that are COMPONENT_REFs. */
463 if (TREE_CODE (lhs) != VAR_DECL)
464 continue;
465 if (DECL_RTL_IF_SET (lhs) == pc_rtx
466 && (v = decl_to_stack_part->get (lhs)))
467 bitmap_clear_bit (work, *v);
469 else if (!is_gimple_debug (stmt))
471 if (for_conflict
472 && visit == visit_op)
474 /* If this is the first real instruction in this BB we need
475 to add conflicts for everything live at this point now.
476 Unlike classical liveness for named objects we can't
477 rely on seeing a def/use of the names we're interested in.
478 There might merely be indirect loads/stores. We'd not add any
479 conflicts for such partitions. */
480 bitmap_iterator bi;
481 unsigned i;
482 EXECUTE_IF_SET_IN_BITMAP (work, 0, i, bi)
484 struct stack_var *a = &stack_vars[i];
485 if (!a->conflicts)
486 a->conflicts = BITMAP_ALLOC (&stack_var_bitmap_obstack);
487 bitmap_ior_into (a->conflicts, work);
489 visit = visit_conflict;
491 walk_stmt_load_store_addr_ops (stmt, work, visit, visit, visit);
496 /* Generate stack partition conflicts between all partitions that are
497 simultaneously live. */
499 static void
500 add_scope_conflicts (void)
502 basic_block bb;
503 bool changed;
504 bitmap work = BITMAP_ALLOC (NULL);
505 int *rpo;
506 int n_bbs;
508 /* We approximate the live range of a stack variable by taking the first
509 mention of its name as starting point(s), and by the end-of-scope
510 death clobber added by gimplify as ending point(s) of the range.
511 This overapproximates in the case we for instance moved an address-taken
512 operation upward, without also moving a dereference to it upwards.
513 But it's conservatively correct as a variable never can hold values
514 before its name is mentioned at least once.
516 We then do a mostly classical bitmap liveness algorithm. */
518 FOR_ALL_BB_FN (bb, cfun)
519 bb->aux = BITMAP_ALLOC (&stack_var_bitmap_obstack);
521 rpo = XNEWVEC (int, last_basic_block_for_fn (cfun));
522 n_bbs = pre_and_rev_post_order_compute (NULL, rpo, false);
524 changed = true;
525 while (changed)
527 int i;
528 changed = false;
529 for (i = 0; i < n_bbs; i++)
531 bitmap active;
532 bb = BASIC_BLOCK_FOR_FN (cfun, rpo[i]);
533 active = (bitmap)bb->aux;
534 add_scope_conflicts_1 (bb, work, false);
535 if (bitmap_ior_into (active, work))
536 changed = true;
540 FOR_EACH_BB_FN (bb, cfun)
541 add_scope_conflicts_1 (bb, work, true);
543 free (rpo);
544 BITMAP_FREE (work);
545 FOR_ALL_BB_FN (bb, cfun)
546 BITMAP_FREE (bb->aux);
549 /* A subroutine of partition_stack_vars. A comparison function for qsort,
550 sorting an array of indices by the properties of the object. */
552 static int
553 stack_var_cmp (const void *a, const void *b)
555 size_t ia = *(const size_t *)a;
556 size_t ib = *(const size_t *)b;
557 unsigned int aligna = stack_vars[ia].alignb;
558 unsigned int alignb = stack_vars[ib].alignb;
559 HOST_WIDE_INT sizea = stack_vars[ia].size;
560 HOST_WIDE_INT sizeb = stack_vars[ib].size;
561 tree decla = stack_vars[ia].decl;
562 tree declb = stack_vars[ib].decl;
563 bool largea, largeb;
564 unsigned int uida, uidb;
566 /* Primary compare on "large" alignment. Large comes first. */
567 largea = (aligna * BITS_PER_UNIT > MAX_SUPPORTED_STACK_ALIGNMENT);
568 largeb = (alignb * BITS_PER_UNIT > MAX_SUPPORTED_STACK_ALIGNMENT);
569 if (largea != largeb)
570 return (int)largeb - (int)largea;
572 /* Secondary compare on size, decreasing */
573 if (sizea > sizeb)
574 return -1;
575 if (sizea < sizeb)
576 return 1;
578 /* Tertiary compare on true alignment, decreasing. */
579 if (aligna < alignb)
580 return -1;
581 if (aligna > alignb)
582 return 1;
584 /* Final compare on ID for sort stability, increasing.
585 Two SSA names are compared by their version, SSA names come before
586 non-SSA names, and two normal decls are compared by their DECL_UID. */
587 if (TREE_CODE (decla) == SSA_NAME)
589 if (TREE_CODE (declb) == SSA_NAME)
590 uida = SSA_NAME_VERSION (decla), uidb = SSA_NAME_VERSION (declb);
591 else
592 return -1;
594 else if (TREE_CODE (declb) == SSA_NAME)
595 return 1;
596 else
597 uida = DECL_UID (decla), uidb = DECL_UID (declb);
598 if (uida < uidb)
599 return 1;
600 if (uida > uidb)
601 return -1;
602 return 0;
605 struct part_traits : default_hashmap_traits
607 template<typename T>
608 static bool
609 is_deleted (T &e)
610 { return e.m_value == reinterpret_cast<void *> (1); }
612 template<typename T> static bool is_empty (T &e) { return e.m_value == NULL; }
613 template<typename T>
614 static void
615 mark_deleted (T &e)
616 { e.m_value = reinterpret_cast<T> (1); }
618 template<typename T>
619 static void
620 mark_empty (T &e)
621 { e.m_value = NULL; }
624 typedef hash_map<size_t, bitmap, part_traits> part_hashmap;
626 /* If the points-to solution *PI points to variables that are in a partition
627 together with other variables add all partition members to the pointed-to
628 variables bitmap. */
630 static void
631 add_partitioned_vars_to_ptset (struct pt_solution *pt,
632 part_hashmap *decls_to_partitions,
633 hash_set<bitmap> *visited, bitmap temp)
635 bitmap_iterator bi;
636 unsigned i;
637 bitmap *part;
639 if (pt->anything
640 || pt->vars == NULL
641 /* The pointed-to vars bitmap is shared, it is enough to
642 visit it once. */
643 || visited->add (pt->vars))
644 return;
646 bitmap_clear (temp);
648 /* By using a temporary bitmap to store all members of the partitions
649 we have to add we make sure to visit each of the partitions only
650 once. */
651 EXECUTE_IF_SET_IN_BITMAP (pt->vars, 0, i, bi)
652 if ((!temp
653 || !bitmap_bit_p (temp, i))
654 && (part = decls_to_partitions->get (i)))
655 bitmap_ior_into (temp, *part);
656 if (!bitmap_empty_p (temp))
657 bitmap_ior_into (pt->vars, temp);
660 /* Update points-to sets based on partition info, so we can use them on RTL.
661 The bitmaps representing stack partitions will be saved until expand,
662 where partitioned decls used as bases in memory expressions will be
663 rewritten. */
665 static void
666 update_alias_info_with_stack_vars (void)
668 part_hashmap *decls_to_partitions = NULL;
669 size_t i, j;
670 tree var = NULL_TREE;
672 for (i = 0; i < stack_vars_num; i++)
674 bitmap part = NULL;
675 tree name;
676 struct ptr_info_def *pi;
678 /* Not interested in partitions with single variable. */
679 if (stack_vars[i].representative != i
680 || stack_vars[i].next == EOC)
681 continue;
683 if (!decls_to_partitions)
685 decls_to_partitions = new part_hashmap;
686 cfun->gimple_df->decls_to_pointers = new hash_map<tree, tree>;
689 /* Create an SSA_NAME that points to the partition for use
690 as base during alias-oracle queries on RTL for bases that
691 have been partitioned. */
692 if (var == NULL_TREE)
693 var = create_tmp_var (ptr_type_node, NULL);
694 name = make_ssa_name (var, NULL);
696 /* Create bitmaps representing partitions. They will be used for
697 points-to sets later, so use GGC alloc. */
698 part = BITMAP_GGC_ALLOC ();
699 for (j = i; j != EOC; j = stack_vars[j].next)
701 tree decl = stack_vars[j].decl;
702 unsigned int uid = DECL_PT_UID (decl);
703 bitmap_set_bit (part, uid);
704 decls_to_partitions->put (uid, part);
705 cfun->gimple_df->decls_to_pointers->put (decl, name);
706 if (TREE_ADDRESSABLE (decl))
707 TREE_ADDRESSABLE (name) = 1;
710 /* Make the SSA name point to all partition members. */
711 pi = get_ptr_info (name);
712 pt_solution_set (&pi->pt, part, false);
715 /* Make all points-to sets that contain one member of a partition
716 contain all members of the partition. */
717 if (decls_to_partitions)
719 unsigned i;
720 hash_set<bitmap> visited;
721 bitmap temp = BITMAP_ALLOC (&stack_var_bitmap_obstack);
723 for (i = 1; i < num_ssa_names; i++)
725 tree name = ssa_name (i);
726 struct ptr_info_def *pi;
728 if (name
729 && POINTER_TYPE_P (TREE_TYPE (name))
730 && ((pi = SSA_NAME_PTR_INFO (name)) != NULL))
731 add_partitioned_vars_to_ptset (&pi->pt, decls_to_partitions,
732 &visited, temp);
735 add_partitioned_vars_to_ptset (&cfun->gimple_df->escaped,
736 decls_to_partitions, &visited, temp);
738 delete decls_to_partitions;
739 BITMAP_FREE (temp);
743 /* A subroutine of partition_stack_vars. The UNION portion of a UNION/FIND
744 partitioning algorithm. Partitions A and B are known to be non-conflicting.
745 Merge them into a single partition A. */
747 static void
748 union_stack_vars (size_t a, size_t b)
750 struct stack_var *vb = &stack_vars[b];
751 bitmap_iterator bi;
752 unsigned u;
754 gcc_assert (stack_vars[b].next == EOC);
755 /* Add B to A's partition. */
756 stack_vars[b].next = stack_vars[a].next;
757 stack_vars[b].representative = a;
758 stack_vars[a].next = b;
760 /* Update the required alignment of partition A to account for B. */
761 if (stack_vars[a].alignb < stack_vars[b].alignb)
762 stack_vars[a].alignb = stack_vars[b].alignb;
764 /* Update the interference graph and merge the conflicts. */
765 if (vb->conflicts)
767 EXECUTE_IF_SET_IN_BITMAP (vb->conflicts, 0, u, bi)
768 add_stack_var_conflict (a, stack_vars[u].representative);
769 BITMAP_FREE (vb->conflicts);
773 /* A subroutine of expand_used_vars. Binpack the variables into
774 partitions constrained by the interference graph. The overall
775 algorithm used is as follows:
777 Sort the objects by size in descending order.
778 For each object A {
779 S = size(A)
780 O = 0
781 loop {
782 Look for the largest non-conflicting object B with size <= S.
783 UNION (A, B)
788 static void
789 partition_stack_vars (void)
791 size_t si, sj, n = stack_vars_num;
793 stack_vars_sorted = XNEWVEC (size_t, stack_vars_num);
794 for (si = 0; si < n; ++si)
795 stack_vars_sorted[si] = si;
797 if (n == 1)
798 return;
800 qsort (stack_vars_sorted, n, sizeof (size_t), stack_var_cmp);
802 for (si = 0; si < n; ++si)
804 size_t i = stack_vars_sorted[si];
805 unsigned int ialign = stack_vars[i].alignb;
806 HOST_WIDE_INT isize = stack_vars[i].size;
808 /* Ignore objects that aren't partition representatives. If we
809 see a var that is not a partition representative, it must
810 have been merged earlier. */
811 if (stack_vars[i].representative != i)
812 continue;
814 for (sj = si + 1; sj < n; ++sj)
816 size_t j = stack_vars_sorted[sj];
817 unsigned int jalign = stack_vars[j].alignb;
818 HOST_WIDE_INT jsize = stack_vars[j].size;
820 /* Ignore objects that aren't partition representatives. */
821 if (stack_vars[j].representative != j)
822 continue;
824 /* Do not mix objects of "small" (supported) alignment
825 and "large" (unsupported) alignment. */
826 if ((ialign * BITS_PER_UNIT <= MAX_SUPPORTED_STACK_ALIGNMENT)
827 != (jalign * BITS_PER_UNIT <= MAX_SUPPORTED_STACK_ALIGNMENT))
828 break;
830 /* For Address Sanitizer do not mix objects with different
831 sizes, as the shorter vars wouldn't be adequately protected.
832 Don't do that for "large" (unsupported) alignment objects,
833 those aren't protected anyway. */
834 if ((flag_sanitize & SANITIZE_ADDRESS) && ASAN_STACK && isize != jsize
835 && ialign * BITS_PER_UNIT <= MAX_SUPPORTED_STACK_ALIGNMENT)
836 break;
838 /* Ignore conflicting objects. */
839 if (stack_var_conflict_p (i, j))
840 continue;
842 /* UNION the objects, placing J at OFFSET. */
843 union_stack_vars (i, j);
847 update_alias_info_with_stack_vars ();
850 /* A debugging aid for expand_used_vars. Dump the generated partitions. */
852 static void
853 dump_stack_var_partition (void)
855 size_t si, i, j, n = stack_vars_num;
857 for (si = 0; si < n; ++si)
859 i = stack_vars_sorted[si];
861 /* Skip variables that aren't partition representatives, for now. */
862 if (stack_vars[i].representative != i)
863 continue;
865 fprintf (dump_file, "Partition %lu: size " HOST_WIDE_INT_PRINT_DEC
866 " align %u\n", (unsigned long) i, stack_vars[i].size,
867 stack_vars[i].alignb);
869 for (j = i; j != EOC; j = stack_vars[j].next)
871 fputc ('\t', dump_file);
872 print_generic_expr (dump_file, stack_vars[j].decl, dump_flags);
874 fputc ('\n', dump_file);
878 /* Assign rtl to DECL at BASE + OFFSET. */
880 static void
881 expand_one_stack_var_at (tree decl, rtx base, unsigned base_align,
882 HOST_WIDE_INT offset)
884 unsigned align;
885 rtx x;
887 /* If this fails, we've overflowed the stack frame. Error nicely? */
888 gcc_assert (offset == trunc_int_for_mode (offset, Pmode));
890 x = plus_constant (Pmode, base, offset);
891 x = gen_rtx_MEM (DECL_MODE (SSAVAR (decl)), x);
893 if (TREE_CODE (decl) != SSA_NAME)
895 /* Set alignment we actually gave this decl if it isn't an SSA name.
896 If it is we generate stack slots only accidentally so it isn't as
897 important, we'll simply use the alignment that is already set. */
898 if (base == virtual_stack_vars_rtx)
899 offset -= frame_phase;
900 align = offset & -offset;
901 align *= BITS_PER_UNIT;
902 if (align == 0 || align > base_align)
903 align = base_align;
905 /* One would think that we could assert that we're not decreasing
906 alignment here, but (at least) the i386 port does exactly this
907 via the MINIMUM_ALIGNMENT hook. */
909 DECL_ALIGN (decl) = align;
910 DECL_USER_ALIGN (decl) = 0;
913 set_mem_attributes (x, SSAVAR (decl), true);
914 set_rtl (decl, x);
917 struct stack_vars_data
919 /* Vector of offset pairs, always end of some padding followed
920 by start of the padding that needs Address Sanitizer protection.
921 The vector is in reversed, highest offset pairs come first. */
922 vec<HOST_WIDE_INT> asan_vec;
924 /* Vector of partition representative decls in between the paddings. */
925 vec<tree> asan_decl_vec;
927 /* Base pseudo register for Address Sanitizer protected automatic vars. */
928 rtx asan_base;
930 /* Alignment needed for the Address Sanitizer protected automatic vars. */
931 unsigned int asan_alignb;
934 /* A subroutine of expand_used_vars. Give each partition representative
935 a unique location within the stack frame. Update each partition member
936 with that location. */
938 static void
939 expand_stack_vars (bool (*pred) (size_t), struct stack_vars_data *data)
941 size_t si, i, j, n = stack_vars_num;
942 HOST_WIDE_INT large_size = 0, large_alloc = 0;
943 rtx large_base = NULL;
944 unsigned large_align = 0;
945 tree decl;
947 /* Determine if there are any variables requiring "large" alignment.
948 Since these are dynamically allocated, we only process these if
949 no predicate involved. */
950 large_align = stack_vars[stack_vars_sorted[0]].alignb * BITS_PER_UNIT;
951 if (pred == NULL && large_align > MAX_SUPPORTED_STACK_ALIGNMENT)
953 /* Find the total size of these variables. */
954 for (si = 0; si < n; ++si)
956 unsigned alignb;
958 i = stack_vars_sorted[si];
959 alignb = stack_vars[i].alignb;
961 /* Stop when we get to the first decl with "small" alignment. */
962 if (alignb * BITS_PER_UNIT <= MAX_SUPPORTED_STACK_ALIGNMENT)
963 break;
965 /* Skip variables that aren't partition representatives. */
966 if (stack_vars[i].representative != i)
967 continue;
969 /* Skip variables that have already had rtl assigned. See also
970 add_stack_var where we perpetrate this pc_rtx hack. */
971 decl = stack_vars[i].decl;
972 if ((TREE_CODE (decl) == SSA_NAME
973 ? SA.partition_to_pseudo[var_to_partition (SA.map, decl)]
974 : DECL_RTL (decl)) != pc_rtx)
975 continue;
977 large_size += alignb - 1;
978 large_size &= -(HOST_WIDE_INT)alignb;
979 large_size += stack_vars[i].size;
982 /* If there were any, allocate space. */
983 if (large_size > 0)
984 large_base = allocate_dynamic_stack_space (GEN_INT (large_size), 0,
985 large_align, true);
988 for (si = 0; si < n; ++si)
990 rtx base;
991 unsigned base_align, alignb;
992 HOST_WIDE_INT offset;
994 i = stack_vars_sorted[si];
996 /* Skip variables that aren't partition representatives, for now. */
997 if (stack_vars[i].representative != i)
998 continue;
1000 /* Skip variables that have already had rtl assigned. See also
1001 add_stack_var where we perpetrate this pc_rtx hack. */
1002 decl = stack_vars[i].decl;
1003 if ((TREE_CODE (decl) == SSA_NAME
1004 ? SA.partition_to_pseudo[var_to_partition (SA.map, decl)]
1005 : DECL_RTL (decl)) != pc_rtx)
1006 continue;
1008 /* Check the predicate to see whether this variable should be
1009 allocated in this pass. */
1010 if (pred && !pred (i))
1011 continue;
1013 alignb = stack_vars[i].alignb;
1014 if (alignb * BITS_PER_UNIT <= MAX_SUPPORTED_STACK_ALIGNMENT)
1016 base = virtual_stack_vars_rtx;
1017 if ((flag_sanitize & SANITIZE_ADDRESS) && ASAN_STACK && pred)
1019 HOST_WIDE_INT prev_offset = frame_offset;
1020 tree repr_decl = NULL_TREE;
1022 offset
1023 = alloc_stack_frame_space (stack_vars[i].size
1024 + ASAN_RED_ZONE_SIZE,
1025 MAX (alignb, ASAN_RED_ZONE_SIZE));
1026 data->asan_vec.safe_push (prev_offset);
1027 data->asan_vec.safe_push (offset + stack_vars[i].size);
1028 /* Find best representative of the partition.
1029 Prefer those with DECL_NAME, even better
1030 satisfying asan_protect_stack_decl predicate. */
1031 for (j = i; j != EOC; j = stack_vars[j].next)
1032 if (asan_protect_stack_decl (stack_vars[j].decl)
1033 && DECL_NAME (stack_vars[j].decl))
1035 repr_decl = stack_vars[j].decl;
1036 break;
1038 else if (repr_decl == NULL_TREE
1039 && DECL_P (stack_vars[j].decl)
1040 && DECL_NAME (stack_vars[j].decl))
1041 repr_decl = stack_vars[j].decl;
1042 if (repr_decl == NULL_TREE)
1043 repr_decl = stack_vars[i].decl;
1044 data->asan_decl_vec.safe_push (repr_decl);
1045 data->asan_alignb = MAX (data->asan_alignb, alignb);
1046 if (data->asan_base == NULL)
1047 data->asan_base = gen_reg_rtx (Pmode);
1048 base = data->asan_base;
1050 if (!STRICT_ALIGNMENT)
1051 base_align = crtl->max_used_stack_slot_alignment;
1052 else
1053 base_align = MAX (crtl->max_used_stack_slot_alignment,
1054 GET_MODE_ALIGNMENT (SImode)
1055 << ASAN_SHADOW_SHIFT);
1057 else
1059 offset = alloc_stack_frame_space (stack_vars[i].size, alignb);
1060 base_align = crtl->max_used_stack_slot_alignment;
1063 else
1065 /* Large alignment is only processed in the last pass. */
1066 if (pred)
1067 continue;
1068 gcc_assert (large_base != NULL);
1070 large_alloc += alignb - 1;
1071 large_alloc &= -(HOST_WIDE_INT)alignb;
1072 offset = large_alloc;
1073 large_alloc += stack_vars[i].size;
1075 base = large_base;
1076 base_align = large_align;
1079 /* Create rtl for each variable based on their location within the
1080 partition. */
1081 for (j = i; j != EOC; j = stack_vars[j].next)
1083 expand_one_stack_var_at (stack_vars[j].decl,
1084 base, base_align,
1085 offset);
1089 gcc_assert (large_alloc == large_size);
1092 /* Take into account all sizes of partitions and reset DECL_RTLs. */
1093 static HOST_WIDE_INT
1094 account_stack_vars (void)
1096 size_t si, j, i, n = stack_vars_num;
1097 HOST_WIDE_INT size = 0;
1099 for (si = 0; si < n; ++si)
1101 i = stack_vars_sorted[si];
1103 /* Skip variables that aren't partition representatives, for now. */
1104 if (stack_vars[i].representative != i)
1105 continue;
1107 size += stack_vars[i].size;
1108 for (j = i; j != EOC; j = stack_vars[j].next)
1109 set_rtl (stack_vars[j].decl, NULL);
1111 return size;
1114 /* A subroutine of expand_one_var. Called to immediately assign rtl
1115 to a variable to be allocated in the stack frame. */
1117 static void
1118 expand_one_stack_var (tree var)
1120 HOST_WIDE_INT size, offset;
1121 unsigned byte_align;
1123 size = tree_to_uhwi (DECL_SIZE_UNIT (SSAVAR (var)));
1124 byte_align = align_local_variable (SSAVAR (var));
1126 /* We handle highly aligned variables in expand_stack_vars. */
1127 gcc_assert (byte_align * BITS_PER_UNIT <= MAX_SUPPORTED_STACK_ALIGNMENT);
1129 offset = alloc_stack_frame_space (size, byte_align);
1131 expand_one_stack_var_at (var, virtual_stack_vars_rtx,
1132 crtl->max_used_stack_slot_alignment, offset);
1135 /* A subroutine of expand_one_var. Called to assign rtl to a VAR_DECL
1136 that will reside in a hard register. */
1138 static void
1139 expand_one_hard_reg_var (tree var)
1141 rest_of_decl_compilation (var, 0, 0);
1144 /* A subroutine of expand_one_var. Called to assign rtl to a VAR_DECL
1145 that will reside in a pseudo register. */
1147 static void
1148 expand_one_register_var (tree var)
1150 tree decl = SSAVAR (var);
1151 tree type = TREE_TYPE (decl);
1152 machine_mode reg_mode = promote_decl_mode (decl, NULL);
1153 rtx x = gen_reg_rtx (reg_mode);
1155 set_rtl (var, x);
1157 /* Note if the object is a user variable. */
1158 if (!DECL_ARTIFICIAL (decl))
1159 mark_user_reg (x);
1161 if (POINTER_TYPE_P (type))
1162 mark_reg_pointer (x, get_pointer_alignment (var));
1165 /* A subroutine of expand_one_var. Called to assign rtl to a VAR_DECL that
1166 has some associated error, e.g. its type is error-mark. We just need
1167 to pick something that won't crash the rest of the compiler. */
1169 static void
1170 expand_one_error_var (tree var)
1172 machine_mode mode = DECL_MODE (var);
1173 rtx x;
1175 if (mode == BLKmode)
1176 x = gen_rtx_MEM (BLKmode, const0_rtx);
1177 else if (mode == VOIDmode)
1178 x = const0_rtx;
1179 else
1180 x = gen_reg_rtx (mode);
1182 SET_DECL_RTL (var, x);
1185 /* A subroutine of expand_one_var. VAR is a variable that will be
1186 allocated to the local stack frame. Return true if we wish to
1187 add VAR to STACK_VARS so that it will be coalesced with other
1188 variables. Return false to allocate VAR immediately.
1190 This function is used to reduce the number of variables considered
1191 for coalescing, which reduces the size of the quadratic problem. */
1193 static bool
1194 defer_stack_allocation (tree var, bool toplevel)
1196 /* Whether the variable is small enough for immediate allocation not to be
1197 a problem with regard to the frame size. */
1198 bool smallish
1199 = ((HOST_WIDE_INT) tree_to_uhwi (DECL_SIZE_UNIT (var))
1200 < PARAM_VALUE (PARAM_MIN_SIZE_FOR_STACK_SHARING));
1202 /* If stack protection is enabled, *all* stack variables must be deferred,
1203 so that we can re-order the strings to the top of the frame.
1204 Similarly for Address Sanitizer. */
1205 if (flag_stack_protect || ((flag_sanitize & SANITIZE_ADDRESS) && ASAN_STACK))
1206 return true;
1208 /* We handle "large" alignment via dynamic allocation. We want to handle
1209 this extra complication in only one place, so defer them. */
1210 if (DECL_ALIGN (var) > MAX_SUPPORTED_STACK_ALIGNMENT)
1211 return true;
1213 /* When optimization is enabled, DECL_IGNORED_P variables originally scoped
1214 might be detached from their block and appear at toplevel when we reach
1215 here. We want to coalesce them with variables from other blocks when
1216 the immediate contribution to the frame size would be noticeable. */
1217 if (toplevel && optimize > 0 && DECL_IGNORED_P (var) && !smallish)
1218 return true;
1220 /* Variables declared in the outermost scope automatically conflict
1221 with every other variable. The only reason to want to defer them
1222 at all is that, after sorting, we can more efficiently pack
1223 small variables in the stack frame. Continue to defer at -O2. */
1224 if (toplevel && optimize < 2)
1225 return false;
1227 /* Without optimization, *most* variables are allocated from the
1228 stack, which makes the quadratic problem large exactly when we
1229 want compilation to proceed as quickly as possible. On the
1230 other hand, we don't want the function's stack frame size to
1231 get completely out of hand. So we avoid adding scalars and
1232 "small" aggregates to the list at all. */
1233 if (optimize == 0 && smallish)
1234 return false;
1236 return true;
1239 /* A subroutine of expand_used_vars. Expand one variable according to
1240 its flavor. Variables to be placed on the stack are not actually
1241 expanded yet, merely recorded.
1242 When REALLY_EXPAND is false, only add stack values to be allocated.
1243 Return stack usage this variable is supposed to take.
1246 static HOST_WIDE_INT
1247 expand_one_var (tree var, bool toplevel, bool really_expand)
1249 unsigned int align = BITS_PER_UNIT;
1250 tree origvar = var;
1252 var = SSAVAR (var);
1254 if (TREE_TYPE (var) != error_mark_node && TREE_CODE (var) == VAR_DECL)
1256 /* Because we don't know if VAR will be in register or on stack,
1257 we conservatively assume it will be on stack even if VAR is
1258 eventually put into register after RA pass. For non-automatic
1259 variables, which won't be on stack, we collect alignment of
1260 type and ignore user specified alignment. Similarly for
1261 SSA_NAMEs for which use_register_for_decl returns true. */
1262 if (TREE_STATIC (var)
1263 || DECL_EXTERNAL (var)
1264 || (TREE_CODE (origvar) == SSA_NAME && use_register_for_decl (var)))
1265 align = MINIMUM_ALIGNMENT (TREE_TYPE (var),
1266 TYPE_MODE (TREE_TYPE (var)),
1267 TYPE_ALIGN (TREE_TYPE (var)));
1268 else if (DECL_HAS_VALUE_EXPR_P (var)
1269 || (DECL_RTL_SET_P (var) && MEM_P (DECL_RTL (var))))
1270 /* Don't consider debug only variables with DECL_HAS_VALUE_EXPR_P set
1271 or variables which were assigned a stack slot already by
1272 expand_one_stack_var_at - in the latter case DECL_ALIGN has been
1273 changed from the offset chosen to it. */
1274 align = crtl->stack_alignment_estimated;
1275 else
1276 align = MINIMUM_ALIGNMENT (var, DECL_MODE (var), DECL_ALIGN (var));
1278 /* If the variable alignment is very large we'll dynamicaly allocate
1279 it, which means that in-frame portion is just a pointer. */
1280 if (align > MAX_SUPPORTED_STACK_ALIGNMENT)
1281 align = POINTER_SIZE;
1284 if (SUPPORTS_STACK_ALIGNMENT
1285 && crtl->stack_alignment_estimated < align)
1287 /* stack_alignment_estimated shouldn't change after stack
1288 realign decision made */
1289 gcc_assert (!crtl->stack_realign_processed);
1290 crtl->stack_alignment_estimated = align;
1293 /* stack_alignment_needed > PREFERRED_STACK_BOUNDARY is permitted.
1294 So here we only make sure stack_alignment_needed >= align. */
1295 if (crtl->stack_alignment_needed < align)
1296 crtl->stack_alignment_needed = align;
1297 if (crtl->max_used_stack_slot_alignment < align)
1298 crtl->max_used_stack_slot_alignment = align;
1300 if (TREE_CODE (origvar) == SSA_NAME)
1302 gcc_assert (TREE_CODE (var) != VAR_DECL
1303 || (!DECL_EXTERNAL (var)
1304 && !DECL_HAS_VALUE_EXPR_P (var)
1305 && !TREE_STATIC (var)
1306 && TREE_TYPE (var) != error_mark_node
1307 && !DECL_HARD_REGISTER (var)
1308 && really_expand));
1310 if (TREE_CODE (var) != VAR_DECL && TREE_CODE (origvar) != SSA_NAME)
1312 else if (DECL_EXTERNAL (var))
1314 else if (DECL_HAS_VALUE_EXPR_P (var))
1316 else if (TREE_STATIC (var))
1318 else if (TREE_CODE (origvar) != SSA_NAME && DECL_RTL_SET_P (var))
1320 else if (TREE_TYPE (var) == error_mark_node)
1322 if (really_expand)
1323 expand_one_error_var (var);
1325 else if (TREE_CODE (var) == VAR_DECL && DECL_HARD_REGISTER (var))
1327 if (really_expand)
1329 expand_one_hard_reg_var (var);
1330 if (!DECL_HARD_REGISTER (var))
1331 /* Invalid register specification. */
1332 expand_one_error_var (var);
1335 else if (use_register_for_decl (var))
1337 if (really_expand)
1338 expand_one_register_var (origvar);
1340 else if (! valid_constant_size_p (DECL_SIZE_UNIT (var)))
1342 /* Reject variables which cover more than half of the address-space. */
1343 if (really_expand)
1345 error ("size of variable %q+D is too large", var);
1346 expand_one_error_var (var);
1349 else if (defer_stack_allocation (var, toplevel))
1350 add_stack_var (origvar);
1351 else
1353 if (really_expand)
1354 expand_one_stack_var (origvar);
1355 return tree_to_uhwi (DECL_SIZE_UNIT (var));
1357 return 0;
1360 /* A subroutine of expand_used_vars. Walk down through the BLOCK tree
1361 expanding variables. Those variables that can be put into registers
1362 are allocated pseudos; those that can't are put on the stack.
1364 TOPLEVEL is true if this is the outermost BLOCK. */
1366 static void
1367 expand_used_vars_for_block (tree block, bool toplevel)
1369 tree t;
1371 /* Expand all variables at this level. */
1372 for (t = BLOCK_VARS (block); t ; t = DECL_CHAIN (t))
1373 if (TREE_USED (t)
1374 && ((TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != RESULT_DECL)
1375 || !DECL_NONSHAREABLE (t)))
1376 expand_one_var (t, toplevel, true);
1378 /* Expand all variables at containing levels. */
1379 for (t = BLOCK_SUBBLOCKS (block); t ; t = BLOCK_CHAIN (t))
1380 expand_used_vars_for_block (t, false);
1383 /* A subroutine of expand_used_vars. Walk down through the BLOCK tree
1384 and clear TREE_USED on all local variables. */
1386 static void
1387 clear_tree_used (tree block)
1389 tree t;
1391 for (t = BLOCK_VARS (block); t ; t = DECL_CHAIN (t))
1392 /* if (!TREE_STATIC (t) && !DECL_EXTERNAL (t)) */
1393 if ((TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != RESULT_DECL)
1394 || !DECL_NONSHAREABLE (t))
1395 TREE_USED (t) = 0;
1397 for (t = BLOCK_SUBBLOCKS (block); t ; t = BLOCK_CHAIN (t))
1398 clear_tree_used (t);
1401 enum {
1402 SPCT_FLAG_DEFAULT = 1,
1403 SPCT_FLAG_ALL = 2,
1404 SPCT_FLAG_STRONG = 3
1407 /* Examine TYPE and determine a bit mask of the following features. */
1409 #define SPCT_HAS_LARGE_CHAR_ARRAY 1
1410 #define SPCT_HAS_SMALL_CHAR_ARRAY 2
1411 #define SPCT_HAS_ARRAY 4
1412 #define SPCT_HAS_AGGREGATE 8
1414 static unsigned int
1415 stack_protect_classify_type (tree type)
1417 unsigned int ret = 0;
1418 tree t;
1420 switch (TREE_CODE (type))
1422 case ARRAY_TYPE:
1423 t = TYPE_MAIN_VARIANT (TREE_TYPE (type));
1424 if (t == char_type_node
1425 || t == signed_char_type_node
1426 || t == unsigned_char_type_node)
1428 unsigned HOST_WIDE_INT max = PARAM_VALUE (PARAM_SSP_BUFFER_SIZE);
1429 unsigned HOST_WIDE_INT len;
1431 if (!TYPE_SIZE_UNIT (type)
1432 || !tree_fits_uhwi_p (TYPE_SIZE_UNIT (type)))
1433 len = max;
1434 else
1435 len = tree_to_uhwi (TYPE_SIZE_UNIT (type));
1437 if (len < max)
1438 ret = SPCT_HAS_SMALL_CHAR_ARRAY | SPCT_HAS_ARRAY;
1439 else
1440 ret = SPCT_HAS_LARGE_CHAR_ARRAY | SPCT_HAS_ARRAY;
1442 else
1443 ret = SPCT_HAS_ARRAY;
1444 break;
1446 case UNION_TYPE:
1447 case QUAL_UNION_TYPE:
1448 case RECORD_TYPE:
1449 ret = SPCT_HAS_AGGREGATE;
1450 for (t = TYPE_FIELDS (type); t ; t = TREE_CHAIN (t))
1451 if (TREE_CODE (t) == FIELD_DECL)
1452 ret |= stack_protect_classify_type (TREE_TYPE (t));
1453 break;
1455 default:
1456 break;
1459 return ret;
1462 /* Return nonzero if DECL should be segregated into the "vulnerable" upper
1463 part of the local stack frame. Remember if we ever return nonzero for
1464 any variable in this function. The return value is the phase number in
1465 which the variable should be allocated. */
1467 static int
1468 stack_protect_decl_phase (tree decl)
1470 unsigned int bits = stack_protect_classify_type (TREE_TYPE (decl));
1471 int ret = 0;
1473 if (bits & SPCT_HAS_SMALL_CHAR_ARRAY)
1474 has_short_buffer = true;
1476 if (flag_stack_protect == SPCT_FLAG_ALL
1477 || flag_stack_protect == SPCT_FLAG_STRONG)
1479 if ((bits & (SPCT_HAS_SMALL_CHAR_ARRAY | SPCT_HAS_LARGE_CHAR_ARRAY))
1480 && !(bits & SPCT_HAS_AGGREGATE))
1481 ret = 1;
1482 else if (bits & SPCT_HAS_ARRAY)
1483 ret = 2;
1485 else
1486 ret = (bits & SPCT_HAS_LARGE_CHAR_ARRAY) != 0;
1488 if (ret)
1489 has_protected_decls = true;
1491 return ret;
1494 /* Two helper routines that check for phase 1 and phase 2. These are used
1495 as callbacks for expand_stack_vars. */
1497 static bool
1498 stack_protect_decl_phase_1 (size_t i)
1500 return stack_protect_decl_phase (stack_vars[i].decl) == 1;
1503 static bool
1504 stack_protect_decl_phase_2 (size_t i)
1506 return stack_protect_decl_phase (stack_vars[i].decl) == 2;
1509 /* And helper function that checks for asan phase (with stack protector
1510 it is phase 3). This is used as callback for expand_stack_vars.
1511 Returns true if any of the vars in the partition need to be protected. */
1513 static bool
1514 asan_decl_phase_3 (size_t i)
1516 while (i != EOC)
1518 if (asan_protect_stack_decl (stack_vars[i].decl))
1519 return true;
1520 i = stack_vars[i].next;
1522 return false;
1525 /* Ensure that variables in different stack protection phases conflict
1526 so that they are not merged and share the same stack slot. */
1528 static void
1529 add_stack_protection_conflicts (void)
1531 size_t i, j, n = stack_vars_num;
1532 unsigned char *phase;
1534 phase = XNEWVEC (unsigned char, n);
1535 for (i = 0; i < n; ++i)
1536 phase[i] = stack_protect_decl_phase (stack_vars[i].decl);
1538 for (i = 0; i < n; ++i)
1540 unsigned char ph_i = phase[i];
1541 for (j = i + 1; j < n; ++j)
1542 if (ph_i != phase[j])
1543 add_stack_var_conflict (i, j);
1546 XDELETEVEC (phase);
1549 /* Create a decl for the guard at the top of the stack frame. */
1551 static void
1552 create_stack_guard (void)
1554 tree guard = build_decl (DECL_SOURCE_LOCATION (current_function_decl),
1555 VAR_DECL, NULL, ptr_type_node);
1556 TREE_THIS_VOLATILE (guard) = 1;
1557 TREE_USED (guard) = 1;
1558 expand_one_stack_var (guard);
1559 crtl->stack_protect_guard = guard;
1562 /* Prepare for expanding variables. */
1563 static void
1564 init_vars_expansion (void)
1566 /* Conflict bitmaps, and a few related temporary bitmaps, go here. */
1567 bitmap_obstack_initialize (&stack_var_bitmap_obstack);
1569 /* A map from decl to stack partition. */
1570 decl_to_stack_part = new hash_map<tree, size_t>;
1572 /* Initialize local stack smashing state. */
1573 has_protected_decls = false;
1574 has_short_buffer = false;
1577 /* Free up stack variable graph data. */
1578 static void
1579 fini_vars_expansion (void)
1581 bitmap_obstack_release (&stack_var_bitmap_obstack);
1582 if (stack_vars)
1583 XDELETEVEC (stack_vars);
1584 if (stack_vars_sorted)
1585 XDELETEVEC (stack_vars_sorted);
1586 stack_vars = NULL;
1587 stack_vars_sorted = NULL;
1588 stack_vars_alloc = stack_vars_num = 0;
1589 delete decl_to_stack_part;
1590 decl_to_stack_part = NULL;
1593 /* Make a fair guess for the size of the stack frame of the function
1594 in NODE. This doesn't have to be exact, the result is only used in
1595 the inline heuristics. So we don't want to run the full stack var
1596 packing algorithm (which is quadratic in the number of stack vars).
1597 Instead, we calculate the total size of all stack vars. This turns
1598 out to be a pretty fair estimate -- packing of stack vars doesn't
1599 happen very often. */
1601 HOST_WIDE_INT
1602 estimated_stack_frame_size (struct cgraph_node *node)
1604 HOST_WIDE_INT size = 0;
1605 size_t i;
1606 tree var;
1607 struct function *fn = DECL_STRUCT_FUNCTION (node->decl);
1609 push_cfun (fn);
1611 init_vars_expansion ();
1613 FOR_EACH_LOCAL_DECL (fn, i, var)
1614 if (auto_var_in_fn_p (var, fn->decl))
1615 size += expand_one_var (var, true, false);
1617 if (stack_vars_num > 0)
1619 /* Fake sorting the stack vars for account_stack_vars (). */
1620 stack_vars_sorted = XNEWVEC (size_t, stack_vars_num);
1621 for (i = 0; i < stack_vars_num; ++i)
1622 stack_vars_sorted[i] = i;
1623 size += account_stack_vars ();
1626 fini_vars_expansion ();
1627 pop_cfun ();
1628 return size;
1631 /* Helper routine to check if a record or union contains an array field. */
1633 static int
1634 record_or_union_type_has_array_p (const_tree tree_type)
1636 tree fields = TYPE_FIELDS (tree_type);
1637 tree f;
1639 for (f = fields; f; f = DECL_CHAIN (f))
1640 if (TREE_CODE (f) == FIELD_DECL)
1642 tree field_type = TREE_TYPE (f);
1643 if (RECORD_OR_UNION_TYPE_P (field_type)
1644 && record_or_union_type_has_array_p (field_type))
1645 return 1;
1646 if (TREE_CODE (field_type) == ARRAY_TYPE)
1647 return 1;
1649 return 0;
1652 /* Check if the current function has local referenced variables that
1653 have their addresses taken, contain an array, or are arrays. */
1655 static bool
1656 stack_protect_decl_p ()
1658 unsigned i;
1659 tree var;
1661 FOR_EACH_LOCAL_DECL (cfun, i, var)
1662 if (!is_global_var (var))
1664 tree var_type = TREE_TYPE (var);
1665 if (TREE_CODE (var) == VAR_DECL
1666 && (TREE_CODE (var_type) == ARRAY_TYPE
1667 || TREE_ADDRESSABLE (var)
1668 || (RECORD_OR_UNION_TYPE_P (var_type)
1669 && record_or_union_type_has_array_p (var_type))))
1670 return true;
1672 return false;
1675 /* Check if the current function has calls that use a return slot. */
1677 static bool
1678 stack_protect_return_slot_p ()
1680 basic_block bb;
1682 FOR_ALL_BB_FN (bb, cfun)
1683 for (gimple_stmt_iterator gsi = gsi_start_bb (bb);
1684 !gsi_end_p (gsi); gsi_next (&gsi))
1686 gimple stmt = gsi_stmt (gsi);
1687 /* This assumes that calls to internal-only functions never
1688 use a return slot. */
1689 if (is_gimple_call (stmt)
1690 && !gimple_call_internal_p (stmt)
1691 && aggregate_value_p (TREE_TYPE (gimple_call_fntype (stmt)),
1692 gimple_call_fndecl (stmt)))
1693 return true;
1695 return false;
1698 /* Expand all variables used in the function. */
1700 static rtx_insn *
1701 expand_used_vars (void)
1703 tree var, outer_block = DECL_INITIAL (current_function_decl);
1704 vec<tree> maybe_local_decls = vNULL;
1705 rtx_insn *var_end_seq = NULL;
1706 unsigned i;
1707 unsigned len;
1708 bool gen_stack_protect_signal = false;
1710 /* Compute the phase of the stack frame for this function. */
1712 int align = PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT;
1713 int off = STARTING_FRAME_OFFSET % align;
1714 frame_phase = off ? align - off : 0;
1717 /* Set TREE_USED on all variables in the local_decls. */
1718 FOR_EACH_LOCAL_DECL (cfun, i, var)
1719 TREE_USED (var) = 1;
1720 /* Clear TREE_USED on all variables associated with a block scope. */
1721 clear_tree_used (DECL_INITIAL (current_function_decl));
1723 init_vars_expansion ();
1725 hash_map<tree, tree> ssa_name_decls;
1726 for (i = 0; i < SA.map->num_partitions; i++)
1728 tree var = partition_to_var (SA.map, i);
1730 gcc_assert (!virtual_operand_p (var));
1732 /* Assign decls to each SSA name partition, share decls for partitions
1733 we could have coalesced (those with the same type). */
1734 if (SSA_NAME_VAR (var) == NULL_TREE)
1736 tree *slot = &ssa_name_decls.get_or_insert (TREE_TYPE (var));
1737 if (!*slot)
1738 *slot = create_tmp_reg (TREE_TYPE (var), NULL);
1739 replace_ssa_name_symbol (var, *slot);
1742 /* Always allocate space for partitions based on VAR_DECLs. But for
1743 those based on PARM_DECLs or RESULT_DECLs and which matter for the
1744 debug info, there is no need to do so if optimization is disabled
1745 because all the SSA_NAMEs based on these DECLs have been coalesced
1746 into a single partition, which is thus assigned the canonical RTL
1747 location of the DECLs. If in_lto_p, we can't rely on optimize,
1748 a function could be compiled with -O1 -flto first and only the
1749 link performed at -O0. */
1750 if (TREE_CODE (SSA_NAME_VAR (var)) == VAR_DECL)
1751 expand_one_var (var, true, true);
1752 else if (DECL_IGNORED_P (SSA_NAME_VAR (var)) || optimize || in_lto_p)
1754 /* This is a PARM_DECL or RESULT_DECL. For those partitions that
1755 contain the default def (representing the parm or result itself)
1756 we don't do anything here. But those which don't contain the
1757 default def (representing a temporary based on the parm/result)
1758 we need to allocate space just like for normal VAR_DECLs. */
1759 if (!bitmap_bit_p (SA.partition_has_default_def, i))
1761 expand_one_var (var, true, true);
1762 gcc_assert (SA.partition_to_pseudo[i]);
1767 if (flag_stack_protect == SPCT_FLAG_STRONG)
1768 gen_stack_protect_signal
1769 = stack_protect_decl_p () || stack_protect_return_slot_p ();
1771 /* At this point all variables on the local_decls with TREE_USED
1772 set are not associated with any block scope. Lay them out. */
1774 len = vec_safe_length (cfun->local_decls);
1775 FOR_EACH_LOCAL_DECL (cfun, i, var)
1777 bool expand_now = false;
1779 /* Expanded above already. */
1780 if (is_gimple_reg (var))
1782 TREE_USED (var) = 0;
1783 goto next;
1785 /* We didn't set a block for static or extern because it's hard
1786 to tell the difference between a global variable (re)declared
1787 in a local scope, and one that's really declared there to
1788 begin with. And it doesn't really matter much, since we're
1789 not giving them stack space. Expand them now. */
1790 else if (TREE_STATIC (var) || DECL_EXTERNAL (var))
1791 expand_now = true;
1793 /* Expand variables not associated with any block now. Those created by
1794 the optimizers could be live anywhere in the function. Those that
1795 could possibly have been scoped originally and detached from their
1796 block will have their allocation deferred so we coalesce them with
1797 others when optimization is enabled. */
1798 else if (TREE_USED (var))
1799 expand_now = true;
1801 /* Finally, mark all variables on the list as used. We'll use
1802 this in a moment when we expand those associated with scopes. */
1803 TREE_USED (var) = 1;
1805 if (expand_now)
1806 expand_one_var (var, true, true);
1808 next:
1809 if (DECL_ARTIFICIAL (var) && !DECL_IGNORED_P (var))
1811 rtx rtl = DECL_RTL_IF_SET (var);
1813 /* Keep artificial non-ignored vars in cfun->local_decls
1814 chain until instantiate_decls. */
1815 if (rtl && (MEM_P (rtl) || GET_CODE (rtl) == CONCAT))
1816 add_local_decl (cfun, var);
1817 else if (rtl == NULL_RTX)
1818 /* If rtl isn't set yet, which can happen e.g. with
1819 -fstack-protector, retry before returning from this
1820 function. */
1821 maybe_local_decls.safe_push (var);
1825 /* We duplicated some of the decls in CFUN->LOCAL_DECLS.
1827 +-----------------+-----------------+
1828 | ...processed... | ...duplicates...|
1829 +-----------------+-----------------+
1831 +-- LEN points here.
1833 We just want the duplicates, as those are the artificial
1834 non-ignored vars that we want to keep until instantiate_decls.
1835 Move them down and truncate the array. */
1836 if (!vec_safe_is_empty (cfun->local_decls))
1837 cfun->local_decls->block_remove (0, len);
1839 /* At this point, all variables within the block tree with TREE_USED
1840 set are actually used by the optimized function. Lay them out. */
1841 expand_used_vars_for_block (outer_block, true);
1843 if (stack_vars_num > 0)
1845 add_scope_conflicts ();
1847 /* If stack protection is enabled, we don't share space between
1848 vulnerable data and non-vulnerable data. */
1849 if (flag_stack_protect)
1850 add_stack_protection_conflicts ();
1852 /* Now that we have collected all stack variables, and have computed a
1853 minimal interference graph, attempt to save some stack space. */
1854 partition_stack_vars ();
1855 if (dump_file)
1856 dump_stack_var_partition ();
1859 switch (flag_stack_protect)
1861 case SPCT_FLAG_ALL:
1862 create_stack_guard ();
1863 break;
1865 case SPCT_FLAG_STRONG:
1866 if (gen_stack_protect_signal
1867 || cfun->calls_alloca || has_protected_decls)
1868 create_stack_guard ();
1869 break;
1871 case SPCT_FLAG_DEFAULT:
1872 if (cfun->calls_alloca || has_protected_decls)
1873 create_stack_guard ();
1874 break;
1876 default:
1880 /* Assign rtl to each variable based on these partitions. */
1881 if (stack_vars_num > 0)
1883 struct stack_vars_data data;
1885 data.asan_vec = vNULL;
1886 data.asan_decl_vec = vNULL;
1887 data.asan_base = NULL_RTX;
1888 data.asan_alignb = 0;
1890 /* Reorder decls to be protected by iterating over the variables
1891 array multiple times, and allocating out of each phase in turn. */
1892 /* ??? We could probably integrate this into the qsort we did
1893 earlier, such that we naturally see these variables first,
1894 and thus naturally allocate things in the right order. */
1895 if (has_protected_decls)
1897 /* Phase 1 contains only character arrays. */
1898 expand_stack_vars (stack_protect_decl_phase_1, &data);
1900 /* Phase 2 contains other kinds of arrays. */
1901 if (flag_stack_protect == 2)
1902 expand_stack_vars (stack_protect_decl_phase_2, &data);
1905 if ((flag_sanitize & SANITIZE_ADDRESS) && ASAN_STACK)
1906 /* Phase 3, any partitions that need asan protection
1907 in addition to phase 1 and 2. */
1908 expand_stack_vars (asan_decl_phase_3, &data);
1910 if (!data.asan_vec.is_empty ())
1912 HOST_WIDE_INT prev_offset = frame_offset;
1913 HOST_WIDE_INT offset, sz, redzonesz;
1914 redzonesz = ASAN_RED_ZONE_SIZE;
1915 sz = data.asan_vec[0] - prev_offset;
1916 if (data.asan_alignb > ASAN_RED_ZONE_SIZE
1917 && data.asan_alignb <= 4096
1918 && sz + ASAN_RED_ZONE_SIZE >= (int) data.asan_alignb)
1919 redzonesz = ((sz + ASAN_RED_ZONE_SIZE + data.asan_alignb - 1)
1920 & ~(data.asan_alignb - HOST_WIDE_INT_1)) - sz;
1921 offset
1922 = alloc_stack_frame_space (redzonesz, ASAN_RED_ZONE_SIZE);
1923 data.asan_vec.safe_push (prev_offset);
1924 data.asan_vec.safe_push (offset);
1925 /* Leave space for alignment if STRICT_ALIGNMENT. */
1926 if (STRICT_ALIGNMENT)
1927 alloc_stack_frame_space ((GET_MODE_ALIGNMENT (SImode)
1928 << ASAN_SHADOW_SHIFT)
1929 / BITS_PER_UNIT, 1);
1931 var_end_seq
1932 = asan_emit_stack_protection (virtual_stack_vars_rtx,
1933 data.asan_base,
1934 data.asan_alignb,
1935 data.asan_vec.address (),
1936 data.asan_decl_vec.address (),
1937 data.asan_vec.length ());
1940 expand_stack_vars (NULL, &data);
1942 data.asan_vec.release ();
1943 data.asan_decl_vec.release ();
1946 fini_vars_expansion ();
1948 /* If there were any artificial non-ignored vars without rtl
1949 found earlier, see if deferred stack allocation hasn't assigned
1950 rtl to them. */
1951 FOR_EACH_VEC_ELT_REVERSE (maybe_local_decls, i, var)
1953 rtx rtl = DECL_RTL_IF_SET (var);
1955 /* Keep artificial non-ignored vars in cfun->local_decls
1956 chain until instantiate_decls. */
1957 if (rtl && (MEM_P (rtl) || GET_CODE (rtl) == CONCAT))
1958 add_local_decl (cfun, var);
1960 maybe_local_decls.release ();
1962 /* If the target requires that FRAME_OFFSET be aligned, do it. */
1963 if (STACK_ALIGNMENT_NEEDED)
1965 HOST_WIDE_INT align = PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT;
1966 if (!FRAME_GROWS_DOWNWARD)
1967 frame_offset += align - 1;
1968 frame_offset &= -align;
1971 return var_end_seq;
1975 /* If we need to produce a detailed dump, print the tree representation
1976 for STMT to the dump file. SINCE is the last RTX after which the RTL
1977 generated for STMT should have been appended. */
1979 static void
1980 maybe_dump_rtl_for_gimple_stmt (gimple stmt, rtx_insn *since)
1982 if (dump_file && (dump_flags & TDF_DETAILS))
1984 fprintf (dump_file, "\n;; ");
1985 print_gimple_stmt (dump_file, stmt, 0,
1986 TDF_SLIM | (dump_flags & TDF_LINENO));
1987 fprintf (dump_file, "\n");
1989 print_rtl (dump_file, since ? NEXT_INSN (since) : since);
1993 /* Maps the blocks that do not contain tree labels to rtx labels. */
1995 static hash_map<basic_block, rtx_code_label *> *lab_rtx_for_bb;
1997 /* Returns the label_rtx expression for a label starting basic block BB. */
1999 static rtx
2000 label_rtx_for_bb (basic_block bb ATTRIBUTE_UNUSED)
2002 gimple_stmt_iterator gsi;
2003 tree lab;
2005 if (bb->flags & BB_RTL)
2006 return block_label (bb);
2008 rtx_code_label **elt = lab_rtx_for_bb->get (bb);
2009 if (elt)
2010 return *elt;
2012 /* Find the tree label if it is present. */
2014 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
2016 glabel *lab_stmt;
2018 lab_stmt = dyn_cast <glabel *> (gsi_stmt (gsi));
2019 if (!lab_stmt)
2020 break;
2022 lab = gimple_label_label (lab_stmt);
2023 if (DECL_NONLOCAL (lab))
2024 break;
2026 return label_rtx (lab);
2029 rtx_code_label *l = gen_label_rtx ();
2030 lab_rtx_for_bb->put (bb, l);
2031 return l;
2035 /* A subroutine of expand_gimple_cond. Given E, a fallthrough edge
2036 of a basic block where we just expanded the conditional at the end,
2037 possibly clean up the CFG and instruction sequence. LAST is the
2038 last instruction before the just emitted jump sequence. */
2040 static void
2041 maybe_cleanup_end_of_block (edge e, rtx_insn *last)
2043 /* Special case: when jumpif decides that the condition is
2044 trivial it emits an unconditional jump (and the necessary
2045 barrier). But we still have two edges, the fallthru one is
2046 wrong. purge_dead_edges would clean this up later. Unfortunately
2047 we have to insert insns (and split edges) before
2048 find_many_sub_basic_blocks and hence before purge_dead_edges.
2049 But splitting edges might create new blocks which depend on the
2050 fact that if there are two edges there's no barrier. So the
2051 barrier would get lost and verify_flow_info would ICE. Instead
2052 of auditing all edge splitters to care for the barrier (which
2053 normally isn't there in a cleaned CFG), fix it here. */
2054 if (BARRIER_P (get_last_insn ()))
2056 rtx_insn *insn;
2057 remove_edge (e);
2058 /* Now, we have a single successor block, if we have insns to
2059 insert on the remaining edge we potentially will insert
2060 it at the end of this block (if the dest block isn't feasible)
2061 in order to avoid splitting the edge. This insertion will take
2062 place in front of the last jump. But we might have emitted
2063 multiple jumps (conditional and one unconditional) to the
2064 same destination. Inserting in front of the last one then
2065 is a problem. See PR 40021. We fix this by deleting all
2066 jumps except the last unconditional one. */
2067 insn = PREV_INSN (get_last_insn ());
2068 /* Make sure we have an unconditional jump. Otherwise we're
2069 confused. */
2070 gcc_assert (JUMP_P (insn) && !any_condjump_p (insn));
2071 for (insn = PREV_INSN (insn); insn != last;)
2073 insn = PREV_INSN (insn);
2074 if (JUMP_P (NEXT_INSN (insn)))
2076 if (!any_condjump_p (NEXT_INSN (insn)))
2078 gcc_assert (BARRIER_P (NEXT_INSN (NEXT_INSN (insn))));
2079 delete_insn (NEXT_INSN (NEXT_INSN (insn)));
2081 delete_insn (NEXT_INSN (insn));
2087 /* A subroutine of expand_gimple_basic_block. Expand one GIMPLE_COND.
2088 Returns a new basic block if we've terminated the current basic
2089 block and created a new one. */
2091 static basic_block
2092 expand_gimple_cond (basic_block bb, gcond *stmt)
2094 basic_block new_bb, dest;
2095 edge new_edge;
2096 edge true_edge;
2097 edge false_edge;
2098 rtx_insn *last2, *last;
2099 enum tree_code code;
2100 tree op0, op1;
2102 code = gimple_cond_code (stmt);
2103 op0 = gimple_cond_lhs (stmt);
2104 op1 = gimple_cond_rhs (stmt);
2105 /* We're sometimes presented with such code:
2106 D.123_1 = x < y;
2107 if (D.123_1 != 0)
2109 This would expand to two comparisons which then later might
2110 be cleaned up by combine. But some pattern matchers like if-conversion
2111 work better when there's only one compare, so make up for this
2112 here as special exception if TER would have made the same change. */
2113 if (SA.values
2114 && TREE_CODE (op0) == SSA_NAME
2115 && TREE_CODE (TREE_TYPE (op0)) == BOOLEAN_TYPE
2116 && TREE_CODE (op1) == INTEGER_CST
2117 && ((gimple_cond_code (stmt) == NE_EXPR
2118 && integer_zerop (op1))
2119 || (gimple_cond_code (stmt) == EQ_EXPR
2120 && integer_onep (op1)))
2121 && bitmap_bit_p (SA.values, SSA_NAME_VERSION (op0)))
2123 gimple second = SSA_NAME_DEF_STMT (op0);
2124 if (gimple_code (second) == GIMPLE_ASSIGN)
2126 enum tree_code code2 = gimple_assign_rhs_code (second);
2127 if (TREE_CODE_CLASS (code2) == tcc_comparison)
2129 code = code2;
2130 op0 = gimple_assign_rhs1 (second);
2131 op1 = gimple_assign_rhs2 (second);
2133 /* If jumps are cheap and the target does not support conditional
2134 compare, turn some more codes into jumpy sequences. */
2135 else if (BRANCH_COST (optimize_insn_for_speed_p (), false) < 4
2136 && targetm.gen_ccmp_first == NULL)
2138 if ((code2 == BIT_AND_EXPR
2139 && TYPE_PRECISION (TREE_TYPE (op0)) == 1
2140 && TREE_CODE (gimple_assign_rhs2 (second)) != INTEGER_CST)
2141 || code2 == TRUTH_AND_EXPR)
2143 code = TRUTH_ANDIF_EXPR;
2144 op0 = gimple_assign_rhs1 (second);
2145 op1 = gimple_assign_rhs2 (second);
2147 else if (code2 == BIT_IOR_EXPR || code2 == TRUTH_OR_EXPR)
2149 code = TRUTH_ORIF_EXPR;
2150 op0 = gimple_assign_rhs1 (second);
2151 op1 = gimple_assign_rhs2 (second);
2157 last2 = last = get_last_insn ();
2159 extract_true_false_edges_from_block (bb, &true_edge, &false_edge);
2160 set_curr_insn_location (gimple_location (stmt));
2162 /* These flags have no purpose in RTL land. */
2163 true_edge->flags &= ~EDGE_TRUE_VALUE;
2164 false_edge->flags &= ~EDGE_FALSE_VALUE;
2166 /* We can either have a pure conditional jump with one fallthru edge or
2167 two-way jump that needs to be decomposed into two basic blocks. */
2168 if (false_edge->dest == bb->next_bb)
2170 jumpif_1 (code, op0, op1, label_rtx_for_bb (true_edge->dest),
2171 true_edge->probability);
2172 maybe_dump_rtl_for_gimple_stmt (stmt, last);
2173 if (true_edge->goto_locus != UNKNOWN_LOCATION)
2174 set_curr_insn_location (true_edge->goto_locus);
2175 false_edge->flags |= EDGE_FALLTHRU;
2176 maybe_cleanup_end_of_block (false_edge, last);
2177 return NULL;
2179 if (true_edge->dest == bb->next_bb)
2181 jumpifnot_1 (code, op0, op1, label_rtx_for_bb (false_edge->dest),
2182 false_edge->probability);
2183 maybe_dump_rtl_for_gimple_stmt (stmt, last);
2184 if (false_edge->goto_locus != UNKNOWN_LOCATION)
2185 set_curr_insn_location (false_edge->goto_locus);
2186 true_edge->flags |= EDGE_FALLTHRU;
2187 maybe_cleanup_end_of_block (true_edge, last);
2188 return NULL;
2191 jumpif_1 (code, op0, op1, label_rtx_for_bb (true_edge->dest),
2192 true_edge->probability);
2193 last = get_last_insn ();
2194 if (false_edge->goto_locus != UNKNOWN_LOCATION)
2195 set_curr_insn_location (false_edge->goto_locus);
2196 emit_jump (label_rtx_for_bb (false_edge->dest));
2198 BB_END (bb) = last;
2199 if (BARRIER_P (BB_END (bb)))
2200 BB_END (bb) = PREV_INSN (BB_END (bb));
2201 update_bb_for_insn (bb);
2203 new_bb = create_basic_block (NEXT_INSN (last), get_last_insn (), bb);
2204 dest = false_edge->dest;
2205 redirect_edge_succ (false_edge, new_bb);
2206 false_edge->flags |= EDGE_FALLTHRU;
2207 new_bb->count = false_edge->count;
2208 new_bb->frequency = EDGE_FREQUENCY (false_edge);
2209 add_bb_to_loop (new_bb, bb->loop_father);
2210 new_edge = make_edge (new_bb, dest, 0);
2211 new_edge->probability = REG_BR_PROB_BASE;
2212 new_edge->count = new_bb->count;
2213 if (BARRIER_P (BB_END (new_bb)))
2214 BB_END (new_bb) = PREV_INSN (BB_END (new_bb));
2215 update_bb_for_insn (new_bb);
2217 maybe_dump_rtl_for_gimple_stmt (stmt, last2);
2219 if (true_edge->goto_locus != UNKNOWN_LOCATION)
2221 set_curr_insn_location (true_edge->goto_locus);
2222 true_edge->goto_locus = curr_insn_location ();
2225 return new_bb;
2228 /* Mark all calls that can have a transaction restart. */
2230 static void
2231 mark_transaction_restart_calls (gimple stmt)
2233 struct tm_restart_node dummy;
2234 void **slot;
2236 if (!cfun->gimple_df->tm_restart)
2237 return;
2239 dummy.stmt = stmt;
2240 slot = htab_find_slot (cfun->gimple_df->tm_restart, &dummy, NO_INSERT);
2241 if (slot)
2243 struct tm_restart_node *n = (struct tm_restart_node *) *slot;
2244 tree list = n->label_or_list;
2245 rtx_insn *insn;
2247 for (insn = next_real_insn (get_last_insn ());
2248 !CALL_P (insn);
2249 insn = next_real_insn (insn))
2250 continue;
2252 if (TREE_CODE (list) == LABEL_DECL)
2253 add_reg_note (insn, REG_TM, label_rtx (list));
2254 else
2255 for (; list ; list = TREE_CHAIN (list))
2256 add_reg_note (insn, REG_TM, label_rtx (TREE_VALUE (list)));
2260 /* A subroutine of expand_gimple_stmt_1, expanding one GIMPLE_CALL
2261 statement STMT. */
2263 static void
2264 expand_call_stmt (gcall *stmt)
2266 tree exp, decl, lhs;
2267 bool builtin_p;
2268 size_t i;
2270 if (gimple_call_internal_p (stmt))
2272 expand_internal_call (stmt);
2273 return;
2276 exp = build_vl_exp (CALL_EXPR, gimple_call_num_args (stmt) + 3);
2278 CALL_EXPR_FN (exp) = gimple_call_fn (stmt);
2279 decl = gimple_call_fndecl (stmt);
2280 builtin_p = decl && DECL_BUILT_IN (decl);
2282 /* If this is not a builtin function, the function type through which the
2283 call is made may be different from the type of the function. */
2284 if (!builtin_p)
2285 CALL_EXPR_FN (exp)
2286 = fold_convert (build_pointer_type (gimple_call_fntype (stmt)),
2287 CALL_EXPR_FN (exp));
2289 TREE_TYPE (exp) = gimple_call_return_type (stmt);
2290 CALL_EXPR_STATIC_CHAIN (exp) = gimple_call_chain (stmt);
2292 for (i = 0; i < gimple_call_num_args (stmt); i++)
2294 tree arg = gimple_call_arg (stmt, i);
2295 gimple def;
2296 /* TER addresses into arguments of builtin functions so we have a
2297 chance to infer more correct alignment information. See PR39954. */
2298 if (builtin_p
2299 && TREE_CODE (arg) == SSA_NAME
2300 && (def = get_gimple_for_ssa_name (arg))
2301 && gimple_assign_rhs_code (def) == ADDR_EXPR)
2302 arg = gimple_assign_rhs1 (def);
2303 CALL_EXPR_ARG (exp, i) = arg;
2306 if (gimple_has_side_effects (stmt))
2307 TREE_SIDE_EFFECTS (exp) = 1;
2309 if (gimple_call_nothrow_p (stmt))
2310 TREE_NOTHROW (exp) = 1;
2312 CALL_EXPR_TAILCALL (exp) = gimple_call_tail_p (stmt);
2313 CALL_EXPR_RETURN_SLOT_OPT (exp) = gimple_call_return_slot_opt_p (stmt);
2314 if (decl
2315 && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL
2316 && (DECL_FUNCTION_CODE (decl) == BUILT_IN_ALLOCA
2317 || DECL_FUNCTION_CODE (decl) == BUILT_IN_ALLOCA_WITH_ALIGN))
2318 CALL_ALLOCA_FOR_VAR_P (exp) = gimple_call_alloca_for_var_p (stmt);
2319 else
2320 CALL_FROM_THUNK_P (exp) = gimple_call_from_thunk_p (stmt);
2321 CALL_EXPR_VA_ARG_PACK (exp) = gimple_call_va_arg_pack_p (stmt);
2322 SET_EXPR_LOCATION (exp, gimple_location (stmt));
2323 CALL_WITH_BOUNDS_P (exp) = gimple_call_with_bounds_p (stmt);
2325 /* Ensure RTL is created for debug args. */
2326 if (decl && DECL_HAS_DEBUG_ARGS_P (decl))
2328 vec<tree, va_gc> **debug_args = decl_debug_args_lookup (decl);
2329 unsigned int ix;
2330 tree dtemp;
2332 if (debug_args)
2333 for (ix = 1; (*debug_args)->iterate (ix, &dtemp); ix += 2)
2335 gcc_assert (TREE_CODE (dtemp) == DEBUG_EXPR_DECL);
2336 expand_debug_expr (dtemp);
2340 lhs = gimple_call_lhs (stmt);
2341 if (lhs)
2342 expand_assignment (lhs, exp, false);
2343 else
2344 expand_expr (exp, const0_rtx, VOIDmode, EXPAND_NORMAL);
2346 mark_transaction_restart_calls (stmt);
2350 /* Generate RTL for an asm statement (explicit assembler code).
2351 STRING is a STRING_CST node containing the assembler code text,
2352 or an ADDR_EXPR containing a STRING_CST. VOL nonzero means the
2353 insn is volatile; don't optimize it. */
2355 static void
2356 expand_asm_loc (tree string, int vol, location_t locus)
2358 rtx body;
2360 if (TREE_CODE (string) == ADDR_EXPR)
2361 string = TREE_OPERAND (string, 0);
2363 body = gen_rtx_ASM_INPUT_loc (VOIDmode,
2364 ggc_strdup (TREE_STRING_POINTER (string)),
2365 locus);
2367 MEM_VOLATILE_P (body) = vol;
2369 emit_insn (body);
2372 /* Return the number of times character C occurs in string S. */
2373 static int
2374 n_occurrences (int c, const char *s)
2376 int n = 0;
2377 while (*s)
2378 n += (*s++ == c);
2379 return n;
2382 /* A subroutine of expand_asm_operands. Check that all operands have
2383 the same number of alternatives. Return true if so. */
2385 static bool
2386 check_operand_nalternatives (tree outputs, tree inputs)
2388 if (outputs || inputs)
2390 tree tmp = TREE_PURPOSE (outputs ? outputs : inputs);
2391 int nalternatives
2392 = n_occurrences (',', TREE_STRING_POINTER (TREE_VALUE (tmp)));
2393 tree next = inputs;
2395 if (nalternatives + 1 > MAX_RECOG_ALTERNATIVES)
2397 error ("too many alternatives in %<asm%>");
2398 return false;
2401 tmp = outputs;
2402 while (tmp)
2404 const char *constraint
2405 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tmp)));
2407 if (n_occurrences (',', constraint) != nalternatives)
2409 error ("operand constraints for %<asm%> differ "
2410 "in number of alternatives");
2411 return false;
2414 if (TREE_CHAIN (tmp))
2415 tmp = TREE_CHAIN (tmp);
2416 else
2417 tmp = next, next = 0;
2421 return true;
2424 /* Check for overlap between registers marked in CLOBBERED_REGS and
2425 anything inappropriate in T. Emit error and return the register
2426 variable definition for error, NULL_TREE for ok. */
2428 static bool
2429 tree_conflicts_with_clobbers_p (tree t, HARD_REG_SET *clobbered_regs)
2431 /* Conflicts between asm-declared register variables and the clobber
2432 list are not allowed. */
2433 tree overlap = tree_overlaps_hard_reg_set (t, clobbered_regs);
2435 if (overlap)
2437 error ("asm-specifier for variable %qE conflicts with asm clobber list",
2438 DECL_NAME (overlap));
2440 /* Reset registerness to stop multiple errors emitted for a single
2441 variable. */
2442 DECL_REGISTER (overlap) = 0;
2443 return true;
2446 return false;
2449 /* Generate RTL for an asm statement with arguments.
2450 STRING is the instruction template.
2451 OUTPUTS is a list of output arguments (lvalues); INPUTS a list of inputs.
2452 Each output or input has an expression in the TREE_VALUE and
2453 a tree list in TREE_PURPOSE which in turn contains a constraint
2454 name in TREE_VALUE (or NULL_TREE) and a constraint string
2455 in TREE_PURPOSE.
2456 CLOBBERS is a list of STRING_CST nodes each naming a hard register
2457 that is clobbered by this insn.
2459 LABELS is a list of labels, and if LABELS is non-NULL, FALLTHRU_BB
2460 should be the fallthru basic block of the asm goto.
2462 Not all kinds of lvalue that may appear in OUTPUTS can be stored directly.
2463 Some elements of OUTPUTS may be replaced with trees representing temporary
2464 values. The caller should copy those temporary values to the originally
2465 specified lvalues.
2467 VOL nonzero means the insn is volatile; don't optimize it. */
2469 static void
2470 expand_asm_operands (tree string, tree outputs, tree inputs,
2471 tree clobbers, tree labels, basic_block fallthru_bb,
2472 int vol, location_t locus)
2474 rtvec argvec, constraintvec, labelvec;
2475 rtx body;
2476 int ninputs = list_length (inputs);
2477 int noutputs = list_length (outputs);
2478 int nlabels = list_length (labels);
2479 int ninout;
2480 int nclobbers;
2481 HARD_REG_SET clobbered_regs;
2482 int clobber_conflict_found = 0;
2483 tree tail;
2484 tree t;
2485 int i;
2486 /* Vector of RTX's of evaluated output operands. */
2487 rtx *output_rtx = XALLOCAVEC (rtx, noutputs);
2488 int *inout_opnum = XALLOCAVEC (int, noutputs);
2489 rtx *real_output_rtx = XALLOCAVEC (rtx, noutputs);
2490 machine_mode *inout_mode = XALLOCAVEC (machine_mode, noutputs);
2491 const char **constraints = XALLOCAVEC (const char *, noutputs + ninputs);
2492 int old_generating_concat_p = generating_concat_p;
2493 rtx_code_label *fallthru_label = NULL;
2495 /* An ASM with no outputs needs to be treated as volatile, for now. */
2496 if (noutputs == 0)
2497 vol = 1;
2499 if (! check_operand_nalternatives (outputs, inputs))
2500 return;
2502 string = resolve_asm_operand_names (string, outputs, inputs, labels);
2504 /* Collect constraints. */
2505 i = 0;
2506 for (t = outputs; t ; t = TREE_CHAIN (t), i++)
2507 constraints[i] = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
2508 for (t = inputs; t ; t = TREE_CHAIN (t), i++)
2509 constraints[i] = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
2511 /* Sometimes we wish to automatically clobber registers across an asm.
2512 Case in point is when the i386 backend moved from cc0 to a hard reg --
2513 maintaining source-level compatibility means automatically clobbering
2514 the flags register. */
2515 clobbers = targetm.md_asm_clobbers (outputs, inputs, clobbers);
2517 /* Count the number of meaningful clobbered registers, ignoring what
2518 we would ignore later. */
2519 nclobbers = 0;
2520 CLEAR_HARD_REG_SET (clobbered_regs);
2521 for (tail = clobbers; tail; tail = TREE_CHAIN (tail))
2523 const char *regname;
2524 int nregs;
2526 if (TREE_VALUE (tail) == error_mark_node)
2527 return;
2528 regname = TREE_STRING_POINTER (TREE_VALUE (tail));
2530 i = decode_reg_name_and_count (regname, &nregs);
2531 if (i == -4)
2532 ++nclobbers;
2533 else if (i == -2)
2534 error ("unknown register name %qs in %<asm%>", regname);
2536 /* Mark clobbered registers. */
2537 if (i >= 0)
2539 int reg;
2541 for (reg = i; reg < i + nregs; reg++)
2543 ++nclobbers;
2545 /* Clobbering the PIC register is an error. */
2546 if (reg == (int) PIC_OFFSET_TABLE_REGNUM)
2548 error ("PIC register clobbered by %qs in %<asm%>", regname);
2549 return;
2552 SET_HARD_REG_BIT (clobbered_regs, reg);
2557 /* First pass over inputs and outputs checks validity and sets
2558 mark_addressable if needed. */
2560 ninout = 0;
2561 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
2563 tree val = TREE_VALUE (tail);
2564 tree type = TREE_TYPE (val);
2565 const char *constraint;
2566 bool is_inout;
2567 bool allows_reg;
2568 bool allows_mem;
2570 /* If there's an erroneous arg, emit no insn. */
2571 if (type == error_mark_node)
2572 return;
2574 /* Try to parse the output constraint. If that fails, there's
2575 no point in going further. */
2576 constraint = constraints[i];
2577 if (!parse_output_constraint (&constraint, i, ninputs, noutputs,
2578 &allows_mem, &allows_reg, &is_inout))
2579 return;
2581 if (! allows_reg
2582 && (allows_mem
2583 || is_inout
2584 || (DECL_P (val)
2585 && REG_P (DECL_RTL (val))
2586 && GET_MODE (DECL_RTL (val)) != TYPE_MODE (type))))
2587 mark_addressable (val);
2589 if (is_inout)
2590 ninout++;
2593 ninputs += ninout;
2594 if (ninputs + noutputs > MAX_RECOG_OPERANDS)
2596 error ("more than %d operands in %<asm%>", MAX_RECOG_OPERANDS);
2597 return;
2600 for (i = 0, tail = inputs; tail; i++, tail = TREE_CHAIN (tail))
2602 bool allows_reg, allows_mem;
2603 const char *constraint;
2605 /* If there's an erroneous arg, emit no insn, because the ASM_INPUT
2606 would get VOIDmode and that could cause a crash in reload. */
2607 if (TREE_TYPE (TREE_VALUE (tail)) == error_mark_node)
2608 return;
2610 constraint = constraints[i + noutputs];
2611 if (! parse_input_constraint (&constraint, i, ninputs, noutputs, ninout,
2612 constraints, &allows_mem, &allows_reg))
2613 return;
2615 if (! allows_reg && allows_mem)
2616 mark_addressable (TREE_VALUE (tail));
2619 /* Second pass evaluates arguments. */
2621 /* Make sure stack is consistent for asm goto. */
2622 if (nlabels > 0)
2623 do_pending_stack_adjust ();
2625 ninout = 0;
2626 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
2628 tree val = TREE_VALUE (tail);
2629 tree type = TREE_TYPE (val);
2630 bool is_inout;
2631 bool allows_reg;
2632 bool allows_mem;
2633 rtx op;
2634 bool ok;
2636 ok = parse_output_constraint (&constraints[i], i, ninputs,
2637 noutputs, &allows_mem, &allows_reg,
2638 &is_inout);
2639 gcc_assert (ok);
2641 /* If an output operand is not a decl or indirect ref and our constraint
2642 allows a register, make a temporary to act as an intermediate.
2643 Make the asm insn write into that, then our caller will copy it to
2644 the real output operand. Likewise for promoted variables. */
2646 generating_concat_p = 0;
2648 real_output_rtx[i] = NULL_RTX;
2649 if ((TREE_CODE (val) == INDIRECT_REF
2650 && allows_mem)
2651 || (DECL_P (val)
2652 && (allows_mem || REG_P (DECL_RTL (val)))
2653 && ! (REG_P (DECL_RTL (val))
2654 && GET_MODE (DECL_RTL (val)) != TYPE_MODE (type)))
2655 || ! allows_reg
2656 || is_inout)
2658 op = expand_expr (val, NULL_RTX, VOIDmode,
2659 !allows_reg ? EXPAND_MEMORY : EXPAND_WRITE);
2660 if (MEM_P (op))
2661 op = validize_mem (op);
2663 if (! allows_reg && !MEM_P (op))
2664 error ("output number %d not directly addressable", i);
2665 if ((! allows_mem && MEM_P (op))
2666 || GET_CODE (op) == CONCAT)
2668 real_output_rtx[i] = op;
2669 op = gen_reg_rtx (GET_MODE (op));
2670 if (is_inout)
2671 emit_move_insn (op, real_output_rtx[i]);
2674 else
2676 op = assign_temp (type, 0, 1);
2677 op = validize_mem (op);
2678 if (!MEM_P (op) && TREE_CODE (TREE_VALUE (tail)) == SSA_NAME)
2679 set_reg_attrs_for_decl_rtl (SSA_NAME_VAR (TREE_VALUE (tail)), op);
2680 TREE_VALUE (tail) = make_tree (type, op);
2682 output_rtx[i] = op;
2684 generating_concat_p = old_generating_concat_p;
2686 if (is_inout)
2688 inout_mode[ninout] = TYPE_MODE (type);
2689 inout_opnum[ninout++] = i;
2692 if (tree_conflicts_with_clobbers_p (val, &clobbered_regs))
2693 clobber_conflict_found = 1;
2696 /* Make vectors for the expression-rtx, constraint strings,
2697 and named operands. */
2699 argvec = rtvec_alloc (ninputs);
2700 constraintvec = rtvec_alloc (ninputs);
2701 labelvec = rtvec_alloc (nlabels);
2703 body = gen_rtx_ASM_OPERANDS ((noutputs == 0 ? VOIDmode
2704 : GET_MODE (output_rtx[0])),
2705 ggc_strdup (TREE_STRING_POINTER (string)),
2706 empty_string, 0, argvec, constraintvec,
2707 labelvec, locus);
2709 MEM_VOLATILE_P (body) = vol;
2711 /* Eval the inputs and put them into ARGVEC.
2712 Put their constraints into ASM_INPUTs and store in CONSTRAINTS. */
2714 for (i = 0, tail = inputs; tail; tail = TREE_CHAIN (tail), ++i)
2716 bool allows_reg, allows_mem;
2717 const char *constraint;
2718 tree val, type;
2719 rtx op;
2720 bool ok;
2722 constraint = constraints[i + noutputs];
2723 ok = parse_input_constraint (&constraint, i, ninputs, noutputs, ninout,
2724 constraints, &allows_mem, &allows_reg);
2725 gcc_assert (ok);
2727 generating_concat_p = 0;
2729 val = TREE_VALUE (tail);
2730 type = TREE_TYPE (val);
2731 /* EXPAND_INITIALIZER will not generate code for valid initializer
2732 constants, but will still generate code for other types of operand.
2733 This is the behavior we want for constant constraints. */
2734 op = expand_expr (val, NULL_RTX, VOIDmode,
2735 allows_reg ? EXPAND_NORMAL
2736 : allows_mem ? EXPAND_MEMORY
2737 : EXPAND_INITIALIZER);
2739 /* Never pass a CONCAT to an ASM. */
2740 if (GET_CODE (op) == CONCAT)
2741 op = force_reg (GET_MODE (op), op);
2742 else if (MEM_P (op))
2743 op = validize_mem (op);
2745 if (asm_operand_ok (op, constraint, NULL) <= 0)
2747 if (allows_reg && TYPE_MODE (type) != BLKmode)
2748 op = force_reg (TYPE_MODE (type), op);
2749 else if (!allows_mem)
2750 warning (0, "asm operand %d probably doesn%'t match constraints",
2751 i + noutputs);
2752 else if (MEM_P (op))
2754 /* We won't recognize either volatile memory or memory
2755 with a queued address as available a memory_operand
2756 at this point. Ignore it: clearly this *is* a memory. */
2758 else
2759 gcc_unreachable ();
2762 generating_concat_p = old_generating_concat_p;
2763 ASM_OPERANDS_INPUT (body, i) = op;
2765 ASM_OPERANDS_INPUT_CONSTRAINT_EXP (body, i)
2766 = gen_rtx_ASM_INPUT_loc (TYPE_MODE (type),
2767 ggc_strdup (constraints[i + noutputs]),
2768 locus);
2770 if (tree_conflicts_with_clobbers_p (val, &clobbered_regs))
2771 clobber_conflict_found = 1;
2774 /* Protect all the operands from the queue now that they have all been
2775 evaluated. */
2777 generating_concat_p = 0;
2779 /* For in-out operands, copy output rtx to input rtx. */
2780 for (i = 0; i < ninout; i++)
2782 int j = inout_opnum[i];
2783 char buffer[16];
2785 ASM_OPERANDS_INPUT (body, ninputs - ninout + i)
2786 = output_rtx[j];
2788 sprintf (buffer, "%d", j);
2789 ASM_OPERANDS_INPUT_CONSTRAINT_EXP (body, ninputs - ninout + i)
2790 = gen_rtx_ASM_INPUT_loc (inout_mode[i], ggc_strdup (buffer), locus);
2793 /* Copy labels to the vector. */
2794 for (i = 0, tail = labels; i < nlabels; ++i, tail = TREE_CHAIN (tail))
2796 rtx r;
2797 /* If asm goto has any labels in the fallthru basic block, use
2798 a label that we emit immediately after the asm goto. Expansion
2799 may insert further instructions into the same basic block after
2800 asm goto and if we don't do this, insertion of instructions on
2801 the fallthru edge might misbehave. See PR58670. */
2802 if (fallthru_bb
2803 && label_to_block_fn (cfun, TREE_VALUE (tail)) == fallthru_bb)
2805 if (fallthru_label == NULL_RTX)
2806 fallthru_label = gen_label_rtx ();
2807 r = fallthru_label;
2809 else
2810 r = label_rtx (TREE_VALUE (tail));
2811 ASM_OPERANDS_LABEL (body, i) = gen_rtx_LABEL_REF (Pmode, r);
2814 generating_concat_p = old_generating_concat_p;
2816 /* Now, for each output, construct an rtx
2817 (set OUTPUT (asm_operands INSN OUTPUTCONSTRAINT OUTPUTNUMBER
2818 ARGVEC CONSTRAINTS OPNAMES))
2819 If there is more than one, put them inside a PARALLEL. */
2821 if (nlabels > 0 && nclobbers == 0)
2823 gcc_assert (noutputs == 0);
2824 emit_jump_insn (body);
2826 else if (noutputs == 0 && nclobbers == 0)
2828 /* No output operands: put in a raw ASM_OPERANDS rtx. */
2829 emit_insn (body);
2831 else if (noutputs == 1 && nclobbers == 0)
2833 ASM_OPERANDS_OUTPUT_CONSTRAINT (body) = ggc_strdup (constraints[0]);
2834 emit_insn (gen_rtx_SET (VOIDmode, output_rtx[0], body));
2836 else
2838 rtx obody = body;
2839 int num = noutputs;
2841 if (num == 0)
2842 num = 1;
2844 body = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (num + nclobbers));
2846 /* For each output operand, store a SET. */
2847 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
2849 XVECEXP (body, 0, i)
2850 = gen_rtx_SET (VOIDmode,
2851 output_rtx[i],
2852 gen_rtx_ASM_OPERANDS
2853 (GET_MODE (output_rtx[i]),
2854 ggc_strdup (TREE_STRING_POINTER (string)),
2855 ggc_strdup (constraints[i]),
2856 i, argvec, constraintvec, labelvec, locus));
2858 MEM_VOLATILE_P (SET_SRC (XVECEXP (body, 0, i))) = vol;
2861 /* If there are no outputs (but there are some clobbers)
2862 store the bare ASM_OPERANDS into the PARALLEL. */
2864 if (i == 0)
2865 XVECEXP (body, 0, i++) = obody;
2867 /* Store (clobber REG) for each clobbered register specified. */
2869 for (tail = clobbers; tail; tail = TREE_CHAIN (tail))
2871 const char *regname = TREE_STRING_POINTER (TREE_VALUE (tail));
2872 int reg, nregs;
2873 int j = decode_reg_name_and_count (regname, &nregs);
2874 rtx clobbered_reg;
2876 if (j < 0)
2878 if (j == -3) /* `cc', which is not a register */
2879 continue;
2881 if (j == -4) /* `memory', don't cache memory across asm */
2883 XVECEXP (body, 0, i++)
2884 = gen_rtx_CLOBBER (VOIDmode,
2885 gen_rtx_MEM
2886 (BLKmode,
2887 gen_rtx_SCRATCH (VOIDmode)));
2888 continue;
2891 /* Ignore unknown register, error already signaled. */
2892 continue;
2895 for (reg = j; reg < j + nregs; reg++)
2897 /* Use QImode since that's guaranteed to clobber just
2898 * one reg. */
2899 clobbered_reg = gen_rtx_REG (QImode, reg);
2901 /* Do sanity check for overlap between clobbers and
2902 respectively input and outputs that hasn't been
2903 handled. Such overlap should have been detected and
2904 reported above. */
2905 if (!clobber_conflict_found)
2907 int opno;
2909 /* We test the old body (obody) contents to avoid
2910 tripping over the under-construction body. */
2911 for (opno = 0; opno < noutputs; opno++)
2912 if (reg_overlap_mentioned_p (clobbered_reg,
2913 output_rtx[opno]))
2914 internal_error
2915 ("asm clobber conflict with output operand");
2917 for (opno = 0; opno < ninputs - ninout; opno++)
2918 if (reg_overlap_mentioned_p (clobbered_reg,
2919 ASM_OPERANDS_INPUT (obody,
2920 opno)))
2921 internal_error
2922 ("asm clobber conflict with input operand");
2925 XVECEXP (body, 0, i++)
2926 = gen_rtx_CLOBBER (VOIDmode, clobbered_reg);
2930 if (nlabels > 0)
2931 emit_jump_insn (body);
2932 else
2933 emit_insn (body);
2936 if (fallthru_label)
2937 emit_label (fallthru_label);
2939 /* For any outputs that needed reloading into registers, spill them
2940 back to where they belong. */
2941 for (i = 0; i < noutputs; ++i)
2942 if (real_output_rtx[i])
2943 emit_move_insn (real_output_rtx[i], output_rtx[i]);
2945 crtl->has_asm_statement = 1;
2946 free_temp_slots ();
2950 static void
2951 expand_asm_stmt (gasm *stmt)
2953 int noutputs;
2954 tree outputs, tail, t;
2955 tree *o;
2956 size_t i, n;
2957 const char *s;
2958 tree str, out, in, cl, labels;
2959 location_t locus = gimple_location (stmt);
2960 basic_block fallthru_bb = NULL;
2962 /* Meh... convert the gimple asm operands into real tree lists.
2963 Eventually we should make all routines work on the vectors instead
2964 of relying on TREE_CHAIN. */
2965 out = NULL_TREE;
2966 n = gimple_asm_noutputs (stmt);
2967 if (n > 0)
2969 t = out = gimple_asm_output_op (stmt, 0);
2970 for (i = 1; i < n; i++)
2971 t = TREE_CHAIN (t) = gimple_asm_output_op (stmt, i);
2974 in = NULL_TREE;
2975 n = gimple_asm_ninputs (stmt);
2976 if (n > 0)
2978 t = in = gimple_asm_input_op (stmt, 0);
2979 for (i = 1; i < n; i++)
2980 t = TREE_CHAIN (t) = gimple_asm_input_op (stmt, i);
2983 cl = NULL_TREE;
2984 n = gimple_asm_nclobbers (stmt);
2985 if (n > 0)
2987 t = cl = gimple_asm_clobber_op (stmt, 0);
2988 for (i = 1; i < n; i++)
2989 t = TREE_CHAIN (t) = gimple_asm_clobber_op (stmt, i);
2992 labels = NULL_TREE;
2993 n = gimple_asm_nlabels (stmt);
2994 if (n > 0)
2996 edge fallthru = find_fallthru_edge (gimple_bb (stmt)->succs);
2997 if (fallthru)
2998 fallthru_bb = fallthru->dest;
2999 t = labels = gimple_asm_label_op (stmt, 0);
3000 for (i = 1; i < n; i++)
3001 t = TREE_CHAIN (t) = gimple_asm_label_op (stmt, i);
3004 s = gimple_asm_string (stmt);
3005 str = build_string (strlen (s), s);
3007 if (gimple_asm_input_p (stmt))
3009 expand_asm_loc (str, gimple_asm_volatile_p (stmt), locus);
3010 return;
3013 outputs = out;
3014 noutputs = gimple_asm_noutputs (stmt);
3015 /* o[I] is the place that output number I should be written. */
3016 o = (tree *) alloca (noutputs * sizeof (tree));
3018 /* Record the contents of OUTPUTS before it is modified. */
3019 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
3020 o[i] = TREE_VALUE (tail);
3022 /* Generate the ASM_OPERANDS insn; store into the TREE_VALUEs of
3023 OUTPUTS some trees for where the values were actually stored. */
3024 expand_asm_operands (str, outputs, in, cl, labels, fallthru_bb,
3025 gimple_asm_volatile_p (stmt), locus);
3027 /* Copy all the intermediate outputs into the specified outputs. */
3028 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
3030 if (o[i] != TREE_VALUE (tail))
3032 expand_assignment (o[i], TREE_VALUE (tail), false);
3033 free_temp_slots ();
3035 /* Restore the original value so that it's correct the next
3036 time we expand this function. */
3037 TREE_VALUE (tail) = o[i];
3042 /* Emit code to jump to the address
3043 specified by the pointer expression EXP. */
3045 static void
3046 expand_computed_goto (tree exp)
3048 rtx x = expand_normal (exp);
3050 x = convert_memory_address (Pmode, x);
3052 do_pending_stack_adjust ();
3053 emit_indirect_jump (x);
3056 /* Generate RTL code for a `goto' statement with target label LABEL.
3057 LABEL should be a LABEL_DECL tree node that was or will later be
3058 defined with `expand_label'. */
3060 static void
3061 expand_goto (tree label)
3063 #ifdef ENABLE_CHECKING
3064 /* Check for a nonlocal goto to a containing function. Should have
3065 gotten translated to __builtin_nonlocal_goto. */
3066 tree context = decl_function_context (label);
3067 gcc_assert (!context || context == current_function_decl);
3068 #endif
3070 emit_jump (label_rtx (label));
3073 /* Output a return with no value. */
3075 static void
3076 expand_null_return_1 (void)
3078 clear_pending_stack_adjust ();
3079 do_pending_stack_adjust ();
3080 emit_jump (return_label);
3083 /* Generate RTL to return from the current function, with no value.
3084 (That is, we do not do anything about returning any value.) */
3086 void
3087 expand_null_return (void)
3089 /* If this function was declared to return a value, but we
3090 didn't, clobber the return registers so that they are not
3091 propagated live to the rest of the function. */
3092 clobber_return_register ();
3094 expand_null_return_1 ();
3097 /* Generate RTL to return from the current function, with value VAL. */
3099 static void
3100 expand_value_return (rtx val)
3102 /* Copy the value to the return location unless it's already there. */
3104 tree decl = DECL_RESULT (current_function_decl);
3105 rtx return_reg = DECL_RTL (decl);
3106 if (return_reg != val)
3108 tree funtype = TREE_TYPE (current_function_decl);
3109 tree type = TREE_TYPE (decl);
3110 int unsignedp = TYPE_UNSIGNED (type);
3111 machine_mode old_mode = DECL_MODE (decl);
3112 machine_mode mode;
3113 if (DECL_BY_REFERENCE (decl))
3114 mode = promote_function_mode (type, old_mode, &unsignedp, funtype, 2);
3115 else
3116 mode = promote_function_mode (type, old_mode, &unsignedp, funtype, 1);
3118 if (mode != old_mode)
3119 val = convert_modes (mode, old_mode, val, unsignedp);
3121 if (GET_CODE (return_reg) == PARALLEL)
3122 emit_group_load (return_reg, val, type, int_size_in_bytes (type));
3123 else
3124 emit_move_insn (return_reg, val);
3127 expand_null_return_1 ();
3130 /* Generate RTL to evaluate the expression RETVAL and return it
3131 from the current function. */
3133 static void
3134 expand_return (tree retval, tree bounds)
3136 rtx result_rtl;
3137 rtx val = 0;
3138 tree retval_rhs;
3139 rtx bounds_rtl;
3141 /* If function wants no value, give it none. */
3142 if (TREE_CODE (TREE_TYPE (TREE_TYPE (current_function_decl))) == VOID_TYPE)
3144 expand_normal (retval);
3145 expand_null_return ();
3146 return;
3149 if (retval == error_mark_node)
3151 /* Treat this like a return of no value from a function that
3152 returns a value. */
3153 expand_null_return ();
3154 return;
3156 else if ((TREE_CODE (retval) == MODIFY_EXPR
3157 || TREE_CODE (retval) == INIT_EXPR)
3158 && TREE_CODE (TREE_OPERAND (retval, 0)) == RESULT_DECL)
3159 retval_rhs = TREE_OPERAND (retval, 1);
3160 else
3161 retval_rhs = retval;
3163 result_rtl = DECL_RTL (DECL_RESULT (current_function_decl));
3165 /* Put returned bounds to the right place. */
3166 bounds_rtl = DECL_BOUNDS_RTL (DECL_RESULT (current_function_decl));
3167 if (bounds_rtl)
3169 rtx addr, bnd;
3171 if (bounds)
3173 bnd = expand_normal (bounds);
3174 targetm.calls.store_returned_bounds (bounds_rtl, bnd);
3176 else if (REG_P (bounds_rtl))
3178 addr = expand_normal (build_fold_addr_expr (retval_rhs));
3179 addr = gen_rtx_MEM (Pmode, addr);
3180 bnd = targetm.calls.load_bounds_for_arg (addr, NULL, NULL);
3181 targetm.calls.store_returned_bounds (bounds_rtl, bnd);
3183 else
3185 int n;
3187 gcc_assert (GET_CODE (bounds_rtl) == PARALLEL);
3189 addr = expand_normal (build_fold_addr_expr (retval_rhs));
3190 addr = gen_rtx_MEM (Pmode, addr);
3192 for (n = 0; n < XVECLEN (bounds_rtl, 0); n++)
3194 rtx offs = XEXP (XVECEXP (bounds_rtl, 0, n), 1);
3195 rtx slot = XEXP (XVECEXP (bounds_rtl, 0, n), 0);
3196 rtx from = adjust_address (addr, Pmode, INTVAL (offs));
3197 rtx bnd = targetm.calls.load_bounds_for_arg (from, NULL, NULL);
3198 targetm.calls.store_returned_bounds (slot, bnd);
3202 else if (chkp_function_instrumented_p (current_function_decl)
3203 && !BOUNDED_P (retval_rhs)
3204 && chkp_type_has_pointer (TREE_TYPE (retval_rhs))
3205 && TREE_CODE (retval_rhs) != RESULT_DECL)
3207 rtx addr = expand_normal (build_fold_addr_expr (retval_rhs));
3208 addr = gen_rtx_MEM (Pmode, addr);
3210 gcc_assert (MEM_P (result_rtl));
3212 chkp_copy_bounds_for_stack_parm (result_rtl, addr, TREE_TYPE (retval_rhs));
3215 /* If we are returning the RESULT_DECL, then the value has already
3216 been stored into it, so we don't have to do anything special. */
3217 if (TREE_CODE (retval_rhs) == RESULT_DECL)
3218 expand_value_return (result_rtl);
3220 /* If the result is an aggregate that is being returned in one (or more)
3221 registers, load the registers here. */
3223 else if (retval_rhs != 0
3224 && TYPE_MODE (TREE_TYPE (retval_rhs)) == BLKmode
3225 && REG_P (result_rtl))
3227 val = copy_blkmode_to_reg (GET_MODE (result_rtl), retval_rhs);
3228 if (val)
3230 /* Use the mode of the result value on the return register. */
3231 PUT_MODE (result_rtl, GET_MODE (val));
3232 expand_value_return (val);
3234 else
3235 expand_null_return ();
3237 else if (retval_rhs != 0
3238 && !VOID_TYPE_P (TREE_TYPE (retval_rhs))
3239 && (REG_P (result_rtl)
3240 || (GET_CODE (result_rtl) == PARALLEL)))
3242 /* Compute the return value into a temporary (usually a pseudo reg). */
3244 = assign_temp (TREE_TYPE (DECL_RESULT (current_function_decl)), 0, 1);
3245 val = expand_expr (retval_rhs, val, GET_MODE (val), EXPAND_NORMAL);
3246 val = force_not_mem (val);
3247 expand_value_return (val);
3249 else
3251 /* No hard reg used; calculate value into hard return reg. */
3252 expand_expr (retval, const0_rtx, VOIDmode, EXPAND_NORMAL);
3253 expand_value_return (result_rtl);
3257 /* A subroutine of expand_gimple_stmt, expanding one gimple statement
3258 STMT that doesn't require special handling for outgoing edges. That
3259 is no tailcalls and no GIMPLE_COND. */
3261 static void
3262 expand_gimple_stmt_1 (gimple stmt)
3264 tree op0;
3266 set_curr_insn_location (gimple_location (stmt));
3268 switch (gimple_code (stmt))
3270 case GIMPLE_GOTO:
3271 op0 = gimple_goto_dest (stmt);
3272 if (TREE_CODE (op0) == LABEL_DECL)
3273 expand_goto (op0);
3274 else
3275 expand_computed_goto (op0);
3276 break;
3277 case GIMPLE_LABEL:
3278 expand_label (gimple_label_label (as_a <glabel *> (stmt)));
3279 break;
3280 case GIMPLE_NOP:
3281 case GIMPLE_PREDICT:
3282 break;
3283 case GIMPLE_SWITCH:
3284 expand_case (as_a <gswitch *> (stmt));
3285 break;
3286 case GIMPLE_ASM:
3287 expand_asm_stmt (as_a <gasm *> (stmt));
3288 break;
3289 case GIMPLE_CALL:
3290 expand_call_stmt (as_a <gcall *> (stmt));
3291 break;
3293 case GIMPLE_RETURN:
3294 op0 = gimple_return_retval (as_a <greturn *> (stmt));
3296 if (op0 && op0 != error_mark_node)
3298 tree result = DECL_RESULT (current_function_decl);
3300 /* If we are not returning the current function's RESULT_DECL,
3301 build an assignment to it. */
3302 if (op0 != result)
3304 /* I believe that a function's RESULT_DECL is unique. */
3305 gcc_assert (TREE_CODE (op0) != RESULT_DECL);
3307 /* ??? We'd like to use simply expand_assignment here,
3308 but this fails if the value is of BLKmode but the return
3309 decl is a register. expand_return has special handling
3310 for this combination, which eventually should move
3311 to common code. See comments there. Until then, let's
3312 build a modify expression :-/ */
3313 op0 = build2 (MODIFY_EXPR, TREE_TYPE (result),
3314 result, op0);
3317 if (!op0)
3318 expand_null_return ();
3319 else
3320 expand_return (op0, gimple_return_retbnd (stmt));
3321 break;
3323 case GIMPLE_ASSIGN:
3325 gassign *assign_stmt = as_a <gassign *> (stmt);
3326 tree lhs = gimple_assign_lhs (assign_stmt);
3328 /* Tree expand used to fiddle with |= and &= of two bitfield
3329 COMPONENT_REFs here. This can't happen with gimple, the LHS
3330 of binary assigns must be a gimple reg. */
3332 if (TREE_CODE (lhs) != SSA_NAME
3333 || get_gimple_rhs_class (gimple_expr_code (stmt))
3334 == GIMPLE_SINGLE_RHS)
3336 tree rhs = gimple_assign_rhs1 (assign_stmt);
3337 gcc_assert (get_gimple_rhs_class (gimple_expr_code (stmt))
3338 == GIMPLE_SINGLE_RHS);
3339 if (gimple_has_location (stmt) && CAN_HAVE_LOCATION_P (rhs))
3340 SET_EXPR_LOCATION (rhs, gimple_location (stmt));
3341 if (TREE_CLOBBER_P (rhs))
3342 /* This is a clobber to mark the going out of scope for
3343 this LHS. */
3345 else
3346 expand_assignment (lhs, rhs,
3347 gimple_assign_nontemporal_move_p (
3348 assign_stmt));
3350 else
3352 rtx target, temp;
3353 bool nontemporal = gimple_assign_nontemporal_move_p (assign_stmt);
3354 struct separate_ops ops;
3355 bool promoted = false;
3357 target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
3358 if (GET_CODE (target) == SUBREG && SUBREG_PROMOTED_VAR_P (target))
3359 promoted = true;
3361 ops.code = gimple_assign_rhs_code (assign_stmt);
3362 ops.type = TREE_TYPE (lhs);
3363 switch (get_gimple_rhs_class (gimple_expr_code (stmt)))
3365 case GIMPLE_TERNARY_RHS:
3366 ops.op2 = gimple_assign_rhs3 (assign_stmt);
3367 /* Fallthru */
3368 case GIMPLE_BINARY_RHS:
3369 ops.op1 = gimple_assign_rhs2 (assign_stmt);
3370 /* Fallthru */
3371 case GIMPLE_UNARY_RHS:
3372 ops.op0 = gimple_assign_rhs1 (assign_stmt);
3373 break;
3374 default:
3375 gcc_unreachable ();
3377 ops.location = gimple_location (stmt);
3379 /* If we want to use a nontemporal store, force the value to
3380 register first. If we store into a promoted register,
3381 don't directly expand to target. */
3382 temp = nontemporal || promoted ? NULL_RTX : target;
3383 temp = expand_expr_real_2 (&ops, temp, GET_MODE (target),
3384 EXPAND_NORMAL);
3386 if (temp == target)
3388 else if (promoted)
3390 int unsignedp = SUBREG_PROMOTED_SIGN (target);
3391 /* If TEMP is a VOIDmode constant, use convert_modes to make
3392 sure that we properly convert it. */
3393 if (CONSTANT_P (temp) && GET_MODE (temp) == VOIDmode)
3395 temp = convert_modes (GET_MODE (target),
3396 TYPE_MODE (ops.type),
3397 temp, unsignedp);
3398 temp = convert_modes (GET_MODE (SUBREG_REG (target)),
3399 GET_MODE (target), temp, unsignedp);
3402 convert_move (SUBREG_REG (target), temp, unsignedp);
3404 else if (nontemporal && emit_storent_insn (target, temp))
3406 else
3408 temp = force_operand (temp, target);
3409 if (temp != target)
3410 emit_move_insn (target, temp);
3414 break;
3416 default:
3417 gcc_unreachable ();
3421 /* Expand one gimple statement STMT and return the last RTL instruction
3422 before any of the newly generated ones.
3424 In addition to generating the necessary RTL instructions this also
3425 sets REG_EH_REGION notes if necessary and sets the current source
3426 location for diagnostics. */
3428 static rtx_insn *
3429 expand_gimple_stmt (gimple stmt)
3431 location_t saved_location = input_location;
3432 rtx_insn *last = get_last_insn ();
3433 int lp_nr;
3435 gcc_assert (cfun);
3437 /* We need to save and restore the current source location so that errors
3438 discovered during expansion are emitted with the right location. But
3439 it would be better if the diagnostic routines used the source location
3440 embedded in the tree nodes rather than globals. */
3441 if (gimple_has_location (stmt))
3442 input_location = gimple_location (stmt);
3444 expand_gimple_stmt_1 (stmt);
3446 /* Free any temporaries used to evaluate this statement. */
3447 free_temp_slots ();
3449 input_location = saved_location;
3451 /* Mark all insns that may trap. */
3452 lp_nr = lookup_stmt_eh_lp (stmt);
3453 if (lp_nr)
3455 rtx_insn *insn;
3456 for (insn = next_real_insn (last); insn;
3457 insn = next_real_insn (insn))
3459 if (! find_reg_note (insn, REG_EH_REGION, NULL_RTX)
3460 /* If we want exceptions for non-call insns, any
3461 may_trap_p instruction may throw. */
3462 && GET_CODE (PATTERN (insn)) != CLOBBER
3463 && GET_CODE (PATTERN (insn)) != USE
3464 && insn_could_throw_p (insn))
3465 make_reg_eh_region_note (insn, 0, lp_nr);
3469 return last;
3472 /* A subroutine of expand_gimple_basic_block. Expand one GIMPLE_CALL
3473 that has CALL_EXPR_TAILCALL set. Returns non-null if we actually
3474 generated a tail call (something that might be denied by the ABI
3475 rules governing the call; see calls.c).
3477 Sets CAN_FALLTHRU if we generated a *conditional* tail call, and
3478 can still reach the rest of BB. The case here is __builtin_sqrt,
3479 where the NaN result goes through the external function (with a
3480 tailcall) and the normal result happens via a sqrt instruction. */
3482 static basic_block
3483 expand_gimple_tailcall (basic_block bb, gcall *stmt, bool *can_fallthru)
3485 rtx_insn *last2, *last;
3486 edge e;
3487 edge_iterator ei;
3488 int probability;
3489 gcov_type count;
3491 last2 = last = expand_gimple_stmt (stmt);
3493 for (last = NEXT_INSN (last); last; last = NEXT_INSN (last))
3494 if (CALL_P (last) && SIBLING_CALL_P (last))
3495 goto found;
3497 maybe_dump_rtl_for_gimple_stmt (stmt, last2);
3499 *can_fallthru = true;
3500 return NULL;
3502 found:
3503 /* ??? Wouldn't it be better to just reset any pending stack adjust?
3504 Any instructions emitted here are about to be deleted. */
3505 do_pending_stack_adjust ();
3507 /* Remove any non-eh, non-abnormal edges that don't go to exit. */
3508 /* ??? I.e. the fallthrough edge. HOWEVER! If there were to be
3509 EH or abnormal edges, we shouldn't have created a tail call in
3510 the first place. So it seems to me we should just be removing
3511 all edges here, or redirecting the existing fallthru edge to
3512 the exit block. */
3514 probability = 0;
3515 count = 0;
3517 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
3519 if (!(e->flags & (EDGE_ABNORMAL | EDGE_EH)))
3521 if (e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun))
3523 e->dest->count -= e->count;
3524 e->dest->frequency -= EDGE_FREQUENCY (e);
3525 if (e->dest->count < 0)
3526 e->dest->count = 0;
3527 if (e->dest->frequency < 0)
3528 e->dest->frequency = 0;
3530 count += e->count;
3531 probability += e->probability;
3532 remove_edge (e);
3534 else
3535 ei_next (&ei);
3538 /* This is somewhat ugly: the call_expr expander often emits instructions
3539 after the sibcall (to perform the function return). These confuse the
3540 find_many_sub_basic_blocks code, so we need to get rid of these. */
3541 last = NEXT_INSN (last);
3542 gcc_assert (BARRIER_P (last));
3544 *can_fallthru = false;
3545 while (NEXT_INSN (last))
3547 /* For instance an sqrt builtin expander expands if with
3548 sibcall in the then and label for `else`. */
3549 if (LABEL_P (NEXT_INSN (last)))
3551 *can_fallthru = true;
3552 break;
3554 delete_insn (NEXT_INSN (last));
3557 e = make_edge (bb, EXIT_BLOCK_PTR_FOR_FN (cfun), EDGE_ABNORMAL
3558 | EDGE_SIBCALL);
3559 e->probability += probability;
3560 e->count += count;
3561 BB_END (bb) = last;
3562 update_bb_for_insn (bb);
3564 if (NEXT_INSN (last))
3566 bb = create_basic_block (NEXT_INSN (last), get_last_insn (), bb);
3568 last = BB_END (bb);
3569 if (BARRIER_P (last))
3570 BB_END (bb) = PREV_INSN (last);
3573 maybe_dump_rtl_for_gimple_stmt (stmt, last2);
3575 return bb;
3578 /* Return the difference between the floor and the truncated result of
3579 a signed division by OP1 with remainder MOD. */
3580 static rtx
3581 floor_sdiv_adjust (machine_mode mode, rtx mod, rtx op1)
3583 /* (mod != 0 ? (op1 / mod < 0 ? -1 : 0) : 0) */
3584 return gen_rtx_IF_THEN_ELSE
3585 (mode, gen_rtx_NE (BImode, mod, const0_rtx),
3586 gen_rtx_IF_THEN_ELSE
3587 (mode, gen_rtx_LT (BImode,
3588 gen_rtx_DIV (mode, op1, mod),
3589 const0_rtx),
3590 constm1_rtx, const0_rtx),
3591 const0_rtx);
3594 /* Return the difference between the ceil and the truncated result of
3595 a signed division by OP1 with remainder MOD. */
3596 static rtx
3597 ceil_sdiv_adjust (machine_mode mode, rtx mod, rtx op1)
3599 /* (mod != 0 ? (op1 / mod > 0 ? 1 : 0) : 0) */
3600 return gen_rtx_IF_THEN_ELSE
3601 (mode, gen_rtx_NE (BImode, mod, const0_rtx),
3602 gen_rtx_IF_THEN_ELSE
3603 (mode, gen_rtx_GT (BImode,
3604 gen_rtx_DIV (mode, op1, mod),
3605 const0_rtx),
3606 const1_rtx, const0_rtx),
3607 const0_rtx);
3610 /* Return the difference between the ceil and the truncated result of
3611 an unsigned division by OP1 with remainder MOD. */
3612 static rtx
3613 ceil_udiv_adjust (machine_mode mode, rtx mod, rtx op1 ATTRIBUTE_UNUSED)
3615 /* (mod != 0 ? 1 : 0) */
3616 return gen_rtx_IF_THEN_ELSE
3617 (mode, gen_rtx_NE (BImode, mod, const0_rtx),
3618 const1_rtx, const0_rtx);
3621 /* Return the difference between the rounded and the truncated result
3622 of a signed division by OP1 with remainder MOD. Halfway cases are
3623 rounded away from zero, rather than to the nearest even number. */
3624 static rtx
3625 round_sdiv_adjust (machine_mode mode, rtx mod, rtx op1)
3627 /* (abs (mod) >= abs (op1) - abs (mod)
3628 ? (op1 / mod > 0 ? 1 : -1)
3629 : 0) */
3630 return gen_rtx_IF_THEN_ELSE
3631 (mode, gen_rtx_GE (BImode, gen_rtx_ABS (mode, mod),
3632 gen_rtx_MINUS (mode,
3633 gen_rtx_ABS (mode, op1),
3634 gen_rtx_ABS (mode, mod))),
3635 gen_rtx_IF_THEN_ELSE
3636 (mode, gen_rtx_GT (BImode,
3637 gen_rtx_DIV (mode, op1, mod),
3638 const0_rtx),
3639 const1_rtx, constm1_rtx),
3640 const0_rtx);
3643 /* Return the difference between the rounded and the truncated result
3644 of a unsigned division by OP1 with remainder MOD. Halfway cases
3645 are rounded away from zero, rather than to the nearest even
3646 number. */
3647 static rtx
3648 round_udiv_adjust (machine_mode mode, rtx mod, rtx op1)
3650 /* (mod >= op1 - mod ? 1 : 0) */
3651 return gen_rtx_IF_THEN_ELSE
3652 (mode, gen_rtx_GE (BImode, mod,
3653 gen_rtx_MINUS (mode, op1, mod)),
3654 const1_rtx, const0_rtx);
3657 /* Convert X to MODE, that must be Pmode or ptr_mode, without emitting
3658 any rtl. */
3660 static rtx
3661 convert_debug_memory_address (machine_mode mode, rtx x,
3662 addr_space_t as)
3664 machine_mode xmode = GET_MODE (x);
3666 #ifndef POINTERS_EXTEND_UNSIGNED
3667 gcc_assert (mode == Pmode
3668 || mode == targetm.addr_space.address_mode (as));
3669 gcc_assert (xmode == mode || xmode == VOIDmode);
3670 #else
3671 rtx temp;
3673 gcc_assert (targetm.addr_space.valid_pointer_mode (mode, as));
3675 if (GET_MODE (x) == mode || GET_MODE (x) == VOIDmode)
3676 return x;
3678 if (GET_MODE_PRECISION (mode) < GET_MODE_PRECISION (xmode))
3679 x = simplify_gen_subreg (mode, x, xmode,
3680 subreg_lowpart_offset
3681 (mode, xmode));
3682 else if (POINTERS_EXTEND_UNSIGNED > 0)
3683 x = gen_rtx_ZERO_EXTEND (mode, x);
3684 else if (!POINTERS_EXTEND_UNSIGNED)
3685 x = gen_rtx_SIGN_EXTEND (mode, x);
3686 else
3688 switch (GET_CODE (x))
3690 case SUBREG:
3691 if ((SUBREG_PROMOTED_VAR_P (x)
3692 || (REG_P (SUBREG_REG (x)) && REG_POINTER (SUBREG_REG (x)))
3693 || (GET_CODE (SUBREG_REG (x)) == PLUS
3694 && REG_P (XEXP (SUBREG_REG (x), 0))
3695 && REG_POINTER (XEXP (SUBREG_REG (x), 0))
3696 && CONST_INT_P (XEXP (SUBREG_REG (x), 1))))
3697 && GET_MODE (SUBREG_REG (x)) == mode)
3698 return SUBREG_REG (x);
3699 break;
3700 case LABEL_REF:
3701 temp = gen_rtx_LABEL_REF (mode, LABEL_REF_LABEL (x));
3702 LABEL_REF_NONLOCAL_P (temp) = LABEL_REF_NONLOCAL_P (x);
3703 return temp;
3704 case SYMBOL_REF:
3705 temp = shallow_copy_rtx (x);
3706 PUT_MODE (temp, mode);
3707 return temp;
3708 case CONST:
3709 temp = convert_debug_memory_address (mode, XEXP (x, 0), as);
3710 if (temp)
3711 temp = gen_rtx_CONST (mode, temp);
3712 return temp;
3713 case PLUS:
3714 case MINUS:
3715 if (CONST_INT_P (XEXP (x, 1)))
3717 temp = convert_debug_memory_address (mode, XEXP (x, 0), as);
3718 if (temp)
3719 return gen_rtx_fmt_ee (GET_CODE (x), mode, temp, XEXP (x, 1));
3721 break;
3722 default:
3723 break;
3725 /* Don't know how to express ptr_extend as operation in debug info. */
3726 return NULL;
3728 #endif /* POINTERS_EXTEND_UNSIGNED */
3730 return x;
3733 /* Return an RTX equivalent to the value of the parameter DECL. */
3735 static rtx
3736 expand_debug_parm_decl (tree decl)
3738 rtx incoming = DECL_INCOMING_RTL (decl);
3740 if (incoming
3741 && GET_MODE (incoming) != BLKmode
3742 && ((REG_P (incoming) && HARD_REGISTER_P (incoming))
3743 || (MEM_P (incoming)
3744 && REG_P (XEXP (incoming, 0))
3745 && HARD_REGISTER_P (XEXP (incoming, 0)))))
3747 rtx rtl = gen_rtx_ENTRY_VALUE (GET_MODE (incoming));
3749 #ifdef HAVE_window_save
3750 /* DECL_INCOMING_RTL uses the INCOMING_REGNO of parameter registers.
3751 If the target machine has an explicit window save instruction, the
3752 actual entry value is the corresponding OUTGOING_REGNO instead. */
3753 if (REG_P (incoming)
3754 && OUTGOING_REGNO (REGNO (incoming)) != REGNO (incoming))
3755 incoming
3756 = gen_rtx_REG_offset (incoming, GET_MODE (incoming),
3757 OUTGOING_REGNO (REGNO (incoming)), 0);
3758 else if (MEM_P (incoming))
3760 rtx reg = XEXP (incoming, 0);
3761 if (OUTGOING_REGNO (REGNO (reg)) != REGNO (reg))
3763 reg = gen_raw_REG (GET_MODE (reg), OUTGOING_REGNO (REGNO (reg)));
3764 incoming = replace_equiv_address_nv (incoming, reg);
3766 else
3767 incoming = copy_rtx (incoming);
3769 #endif
3771 ENTRY_VALUE_EXP (rtl) = incoming;
3772 return rtl;
3775 if (incoming
3776 && GET_MODE (incoming) != BLKmode
3777 && !TREE_ADDRESSABLE (decl)
3778 && MEM_P (incoming)
3779 && (XEXP (incoming, 0) == virtual_incoming_args_rtx
3780 || (GET_CODE (XEXP (incoming, 0)) == PLUS
3781 && XEXP (XEXP (incoming, 0), 0) == virtual_incoming_args_rtx
3782 && CONST_INT_P (XEXP (XEXP (incoming, 0), 1)))))
3783 return copy_rtx (incoming);
3785 return NULL_RTX;
3788 /* Return an RTX equivalent to the value of the tree expression EXP. */
3790 static rtx
3791 expand_debug_expr (tree exp)
3793 rtx op0 = NULL_RTX, op1 = NULL_RTX, op2 = NULL_RTX;
3794 machine_mode mode = TYPE_MODE (TREE_TYPE (exp));
3795 machine_mode inner_mode = VOIDmode;
3796 int unsignedp = TYPE_UNSIGNED (TREE_TYPE (exp));
3797 addr_space_t as;
3799 switch (TREE_CODE_CLASS (TREE_CODE (exp)))
3801 case tcc_expression:
3802 switch (TREE_CODE (exp))
3804 case COND_EXPR:
3805 case DOT_PROD_EXPR:
3806 case SAD_EXPR:
3807 case WIDEN_MULT_PLUS_EXPR:
3808 case WIDEN_MULT_MINUS_EXPR:
3809 case FMA_EXPR:
3810 goto ternary;
3812 case TRUTH_ANDIF_EXPR:
3813 case TRUTH_ORIF_EXPR:
3814 case TRUTH_AND_EXPR:
3815 case TRUTH_OR_EXPR:
3816 case TRUTH_XOR_EXPR:
3817 goto binary;
3819 case TRUTH_NOT_EXPR:
3820 goto unary;
3822 default:
3823 break;
3825 break;
3827 ternary:
3828 op2 = expand_debug_expr (TREE_OPERAND (exp, 2));
3829 if (!op2)
3830 return NULL_RTX;
3831 /* Fall through. */
3833 binary:
3834 case tcc_binary:
3835 case tcc_comparison:
3836 op1 = expand_debug_expr (TREE_OPERAND (exp, 1));
3837 if (!op1)
3838 return NULL_RTX;
3839 /* Fall through. */
3841 unary:
3842 case tcc_unary:
3843 inner_mode = TYPE_MODE (TREE_TYPE (TREE_OPERAND (exp, 0)));
3844 op0 = expand_debug_expr (TREE_OPERAND (exp, 0));
3845 if (!op0)
3846 return NULL_RTX;
3847 break;
3849 case tcc_type:
3850 case tcc_statement:
3851 gcc_unreachable ();
3853 case tcc_constant:
3854 case tcc_exceptional:
3855 case tcc_declaration:
3856 case tcc_reference:
3857 case tcc_vl_exp:
3858 break;
3861 switch (TREE_CODE (exp))
3863 case STRING_CST:
3864 if (!lookup_constant_def (exp))
3866 if (strlen (TREE_STRING_POINTER (exp)) + 1
3867 != (size_t) TREE_STRING_LENGTH (exp))
3868 return NULL_RTX;
3869 op0 = gen_rtx_CONST_STRING (Pmode, TREE_STRING_POINTER (exp));
3870 op0 = gen_rtx_MEM (BLKmode, op0);
3871 set_mem_attributes (op0, exp, 0);
3872 return op0;
3874 /* Fall through... */
3876 case INTEGER_CST:
3877 case REAL_CST:
3878 case FIXED_CST:
3879 op0 = expand_expr (exp, NULL_RTX, mode, EXPAND_INITIALIZER);
3880 return op0;
3882 case COMPLEX_CST:
3883 gcc_assert (COMPLEX_MODE_P (mode));
3884 op0 = expand_debug_expr (TREE_REALPART (exp));
3885 op1 = expand_debug_expr (TREE_IMAGPART (exp));
3886 return gen_rtx_CONCAT (mode, op0, op1);
3888 case DEBUG_EXPR_DECL:
3889 op0 = DECL_RTL_IF_SET (exp);
3891 if (op0)
3892 return op0;
3894 op0 = gen_rtx_DEBUG_EXPR (mode);
3895 DEBUG_EXPR_TREE_DECL (op0) = exp;
3896 SET_DECL_RTL (exp, op0);
3898 return op0;
3900 case VAR_DECL:
3901 case PARM_DECL:
3902 case FUNCTION_DECL:
3903 case LABEL_DECL:
3904 case CONST_DECL:
3905 case RESULT_DECL:
3906 op0 = DECL_RTL_IF_SET (exp);
3908 /* This decl was probably optimized away. */
3909 if (!op0)
3911 if (TREE_CODE (exp) != VAR_DECL
3912 || DECL_EXTERNAL (exp)
3913 || !TREE_STATIC (exp)
3914 || !DECL_NAME (exp)
3915 || DECL_HARD_REGISTER (exp)
3916 || DECL_IN_CONSTANT_POOL (exp)
3917 || mode == VOIDmode)
3918 return NULL;
3920 op0 = make_decl_rtl_for_debug (exp);
3921 if (!MEM_P (op0)
3922 || GET_CODE (XEXP (op0, 0)) != SYMBOL_REF
3923 || SYMBOL_REF_DECL (XEXP (op0, 0)) != exp)
3924 return NULL;
3926 else
3927 op0 = copy_rtx (op0);
3929 if (GET_MODE (op0) == BLKmode
3930 /* If op0 is not BLKmode, but BLKmode is, adjust_mode
3931 below would ICE. While it is likely a FE bug,
3932 try to be robust here. See PR43166. */
3933 || mode == BLKmode
3934 || (mode == VOIDmode && GET_MODE (op0) != VOIDmode))
3936 gcc_assert (MEM_P (op0));
3937 op0 = adjust_address_nv (op0, mode, 0);
3938 return op0;
3941 /* Fall through. */
3943 adjust_mode:
3944 case PAREN_EXPR:
3945 CASE_CONVERT:
3947 inner_mode = GET_MODE (op0);
3949 if (mode == inner_mode)
3950 return op0;
3952 if (inner_mode == VOIDmode)
3954 if (TREE_CODE (exp) == SSA_NAME)
3955 inner_mode = TYPE_MODE (TREE_TYPE (exp));
3956 else
3957 inner_mode = TYPE_MODE (TREE_TYPE (TREE_OPERAND (exp, 0)));
3958 if (mode == inner_mode)
3959 return op0;
3962 if (FLOAT_MODE_P (mode) && FLOAT_MODE_P (inner_mode))
3964 if (GET_MODE_BITSIZE (mode) == GET_MODE_BITSIZE (inner_mode))
3965 op0 = simplify_gen_subreg (mode, op0, inner_mode, 0);
3966 else if (GET_MODE_BITSIZE (mode) < GET_MODE_BITSIZE (inner_mode))
3967 op0 = simplify_gen_unary (FLOAT_TRUNCATE, mode, op0, inner_mode);
3968 else
3969 op0 = simplify_gen_unary (FLOAT_EXTEND, mode, op0, inner_mode);
3971 else if (FLOAT_MODE_P (mode))
3973 gcc_assert (TREE_CODE (exp) != SSA_NAME);
3974 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp, 0))))
3975 op0 = simplify_gen_unary (UNSIGNED_FLOAT, mode, op0, inner_mode);
3976 else
3977 op0 = simplify_gen_unary (FLOAT, mode, op0, inner_mode);
3979 else if (FLOAT_MODE_P (inner_mode))
3981 if (unsignedp)
3982 op0 = simplify_gen_unary (UNSIGNED_FIX, mode, op0, inner_mode);
3983 else
3984 op0 = simplify_gen_unary (FIX, mode, op0, inner_mode);
3986 else if (CONSTANT_P (op0)
3987 || GET_MODE_PRECISION (mode) <= GET_MODE_PRECISION (inner_mode))
3988 op0 = simplify_gen_subreg (mode, op0, inner_mode,
3989 subreg_lowpart_offset (mode,
3990 inner_mode));
3991 else if (TREE_CODE_CLASS (TREE_CODE (exp)) == tcc_unary
3992 ? TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp, 0)))
3993 : unsignedp)
3994 op0 = simplify_gen_unary (ZERO_EXTEND, mode, op0, inner_mode);
3995 else
3996 op0 = simplify_gen_unary (SIGN_EXTEND, mode, op0, inner_mode);
3998 return op0;
4001 case MEM_REF:
4002 if (!is_gimple_mem_ref_addr (TREE_OPERAND (exp, 0)))
4004 tree newexp = fold_binary (MEM_REF, TREE_TYPE (exp),
4005 TREE_OPERAND (exp, 0),
4006 TREE_OPERAND (exp, 1));
4007 if (newexp)
4008 return expand_debug_expr (newexp);
4010 /* FALLTHROUGH */
4011 case INDIRECT_REF:
4012 inner_mode = TYPE_MODE (TREE_TYPE (TREE_OPERAND (exp, 0)));
4013 op0 = expand_debug_expr (TREE_OPERAND (exp, 0));
4014 if (!op0)
4015 return NULL;
4017 if (TREE_CODE (exp) == MEM_REF)
4019 if (GET_CODE (op0) == DEBUG_IMPLICIT_PTR
4020 || (GET_CODE (op0) == PLUS
4021 && GET_CODE (XEXP (op0, 0)) == DEBUG_IMPLICIT_PTR))
4022 /* (mem (debug_implicit_ptr)) might confuse aliasing.
4023 Instead just use get_inner_reference. */
4024 goto component_ref;
4026 op1 = expand_debug_expr (TREE_OPERAND (exp, 1));
4027 if (!op1 || !CONST_INT_P (op1))
4028 return NULL;
4030 op0 = plus_constant (inner_mode, op0, INTVAL (op1));
4033 as = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0))));
4035 op0 = convert_debug_memory_address (targetm.addr_space.address_mode (as),
4036 op0, as);
4037 if (op0 == NULL_RTX)
4038 return NULL;
4040 op0 = gen_rtx_MEM (mode, op0);
4041 set_mem_attributes (op0, exp, 0);
4042 if (TREE_CODE (exp) == MEM_REF
4043 && !is_gimple_mem_ref_addr (TREE_OPERAND (exp, 0)))
4044 set_mem_expr (op0, NULL_TREE);
4045 set_mem_addr_space (op0, as);
4047 return op0;
4049 case TARGET_MEM_REF:
4050 if (TREE_CODE (TMR_BASE (exp)) == ADDR_EXPR
4051 && !DECL_RTL_SET_P (TREE_OPERAND (TMR_BASE (exp), 0)))
4052 return NULL;
4054 op0 = expand_debug_expr
4055 (tree_mem_ref_addr (build_pointer_type (TREE_TYPE (exp)), exp));
4056 if (!op0)
4057 return NULL;
4059 as = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0))));
4060 op0 = convert_debug_memory_address (targetm.addr_space.address_mode (as),
4061 op0, as);
4062 if (op0 == NULL_RTX)
4063 return NULL;
4065 op0 = gen_rtx_MEM (mode, op0);
4067 set_mem_attributes (op0, exp, 0);
4068 set_mem_addr_space (op0, as);
4070 return op0;
4072 component_ref:
4073 case ARRAY_REF:
4074 case ARRAY_RANGE_REF:
4075 case COMPONENT_REF:
4076 case BIT_FIELD_REF:
4077 case REALPART_EXPR:
4078 case IMAGPART_EXPR:
4079 case VIEW_CONVERT_EXPR:
4081 machine_mode mode1;
4082 HOST_WIDE_INT bitsize, bitpos;
4083 tree offset;
4084 int volatilep = 0;
4085 tree tem = get_inner_reference (exp, &bitsize, &bitpos, &offset,
4086 &mode1, &unsignedp, &volatilep, false);
4087 rtx orig_op0;
4089 if (bitsize == 0)
4090 return NULL;
4092 orig_op0 = op0 = expand_debug_expr (tem);
4094 if (!op0)
4095 return NULL;
4097 if (offset)
4099 machine_mode addrmode, offmode;
4101 if (!MEM_P (op0))
4102 return NULL;
4104 op0 = XEXP (op0, 0);
4105 addrmode = GET_MODE (op0);
4106 if (addrmode == VOIDmode)
4107 addrmode = Pmode;
4109 op1 = expand_debug_expr (offset);
4110 if (!op1)
4111 return NULL;
4113 offmode = GET_MODE (op1);
4114 if (offmode == VOIDmode)
4115 offmode = TYPE_MODE (TREE_TYPE (offset));
4117 if (addrmode != offmode)
4118 op1 = simplify_gen_subreg (addrmode, op1, offmode,
4119 subreg_lowpart_offset (addrmode,
4120 offmode));
4122 /* Don't use offset_address here, we don't need a
4123 recognizable address, and we don't want to generate
4124 code. */
4125 op0 = gen_rtx_MEM (mode, simplify_gen_binary (PLUS, addrmode,
4126 op0, op1));
4129 if (MEM_P (op0))
4131 if (mode1 == VOIDmode)
4132 /* Bitfield. */
4133 mode1 = smallest_mode_for_size (bitsize, MODE_INT);
4134 if (bitpos >= BITS_PER_UNIT)
4136 op0 = adjust_address_nv (op0, mode1, bitpos / BITS_PER_UNIT);
4137 bitpos %= BITS_PER_UNIT;
4139 else if (bitpos < 0)
4141 HOST_WIDE_INT units
4142 = (-bitpos + BITS_PER_UNIT - 1) / BITS_PER_UNIT;
4143 op0 = adjust_address_nv (op0, mode1, units);
4144 bitpos += units * BITS_PER_UNIT;
4146 else if (bitpos == 0 && bitsize == GET_MODE_BITSIZE (mode))
4147 op0 = adjust_address_nv (op0, mode, 0);
4148 else if (GET_MODE (op0) != mode1)
4149 op0 = adjust_address_nv (op0, mode1, 0);
4150 else
4151 op0 = copy_rtx (op0);
4152 if (op0 == orig_op0)
4153 op0 = shallow_copy_rtx (op0);
4154 set_mem_attributes (op0, exp, 0);
4157 if (bitpos == 0 && mode == GET_MODE (op0))
4158 return op0;
4160 if (bitpos < 0)
4161 return NULL;
4163 if (GET_MODE (op0) == BLKmode)
4164 return NULL;
4166 if ((bitpos % BITS_PER_UNIT) == 0
4167 && bitsize == GET_MODE_BITSIZE (mode1))
4169 machine_mode opmode = GET_MODE (op0);
4171 if (opmode == VOIDmode)
4172 opmode = TYPE_MODE (TREE_TYPE (tem));
4174 /* This condition may hold if we're expanding the address
4175 right past the end of an array that turned out not to
4176 be addressable (i.e., the address was only computed in
4177 debug stmts). The gen_subreg below would rightfully
4178 crash, and the address doesn't really exist, so just
4179 drop it. */
4180 if (bitpos >= GET_MODE_BITSIZE (opmode))
4181 return NULL;
4183 if ((bitpos % GET_MODE_BITSIZE (mode)) == 0)
4184 return simplify_gen_subreg (mode, op0, opmode,
4185 bitpos / BITS_PER_UNIT);
4188 return simplify_gen_ternary (SCALAR_INT_MODE_P (GET_MODE (op0))
4189 && TYPE_UNSIGNED (TREE_TYPE (exp))
4190 ? SIGN_EXTRACT
4191 : ZERO_EXTRACT, mode,
4192 GET_MODE (op0) != VOIDmode
4193 ? GET_MODE (op0)
4194 : TYPE_MODE (TREE_TYPE (tem)),
4195 op0, GEN_INT (bitsize), GEN_INT (bitpos));
4198 case ABS_EXPR:
4199 return simplify_gen_unary (ABS, mode, op0, mode);
4201 case NEGATE_EXPR:
4202 return simplify_gen_unary (NEG, mode, op0, mode);
4204 case BIT_NOT_EXPR:
4205 return simplify_gen_unary (NOT, mode, op0, mode);
4207 case FLOAT_EXPR:
4208 return simplify_gen_unary (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp,
4209 0)))
4210 ? UNSIGNED_FLOAT : FLOAT, mode, op0,
4211 inner_mode);
4213 case FIX_TRUNC_EXPR:
4214 return simplify_gen_unary (unsignedp ? UNSIGNED_FIX : FIX, mode, op0,
4215 inner_mode);
4217 case POINTER_PLUS_EXPR:
4218 /* For the rare target where pointers are not the same size as
4219 size_t, we need to check for mis-matched modes and correct
4220 the addend. */
4221 if (op0 && op1
4222 && GET_MODE (op0) != VOIDmode && GET_MODE (op1) != VOIDmode
4223 && GET_MODE (op0) != GET_MODE (op1))
4225 if (GET_MODE_BITSIZE (GET_MODE (op0)) < GET_MODE_BITSIZE (GET_MODE (op1))
4226 /* If OP0 is a partial mode, then we must truncate, even if it has
4227 the same bitsize as OP1 as GCC's representation of partial modes
4228 is opaque. */
4229 || (GET_MODE_CLASS (GET_MODE (op0)) == MODE_PARTIAL_INT
4230 && GET_MODE_BITSIZE (GET_MODE (op0)) == GET_MODE_BITSIZE (GET_MODE (op1))))
4231 op1 = simplify_gen_unary (TRUNCATE, GET_MODE (op0), op1,
4232 GET_MODE (op1));
4233 else
4234 /* We always sign-extend, regardless of the signedness of
4235 the operand, because the operand is always unsigned
4236 here even if the original C expression is signed. */
4237 op1 = simplify_gen_unary (SIGN_EXTEND, GET_MODE (op0), op1,
4238 GET_MODE (op1));
4240 /* Fall through. */
4241 case PLUS_EXPR:
4242 return simplify_gen_binary (PLUS, mode, op0, op1);
4244 case MINUS_EXPR:
4245 return simplify_gen_binary (MINUS, mode, op0, op1);
4247 case MULT_EXPR:
4248 return simplify_gen_binary (MULT, mode, op0, op1);
4250 case RDIV_EXPR:
4251 case TRUNC_DIV_EXPR:
4252 case EXACT_DIV_EXPR:
4253 if (unsignedp)
4254 return simplify_gen_binary (UDIV, mode, op0, op1);
4255 else
4256 return simplify_gen_binary (DIV, mode, op0, op1);
4258 case TRUNC_MOD_EXPR:
4259 return simplify_gen_binary (unsignedp ? UMOD : MOD, mode, op0, op1);
4261 case FLOOR_DIV_EXPR:
4262 if (unsignedp)
4263 return simplify_gen_binary (UDIV, mode, op0, op1);
4264 else
4266 rtx div = simplify_gen_binary (DIV, mode, op0, op1);
4267 rtx mod = simplify_gen_binary (MOD, mode, op0, op1);
4268 rtx adj = floor_sdiv_adjust (mode, mod, op1);
4269 return simplify_gen_binary (PLUS, mode, div, adj);
4272 case FLOOR_MOD_EXPR:
4273 if (unsignedp)
4274 return simplify_gen_binary (UMOD, mode, op0, op1);
4275 else
4277 rtx mod = simplify_gen_binary (MOD, mode, op0, op1);
4278 rtx adj = floor_sdiv_adjust (mode, mod, op1);
4279 adj = simplify_gen_unary (NEG, mode,
4280 simplify_gen_binary (MULT, mode, adj, op1),
4281 mode);
4282 return simplify_gen_binary (PLUS, mode, mod, adj);
4285 case CEIL_DIV_EXPR:
4286 if (unsignedp)
4288 rtx div = simplify_gen_binary (UDIV, mode, op0, op1);
4289 rtx mod = simplify_gen_binary (UMOD, mode, op0, op1);
4290 rtx adj = ceil_udiv_adjust (mode, mod, op1);
4291 return simplify_gen_binary (PLUS, mode, div, adj);
4293 else
4295 rtx div = simplify_gen_binary (DIV, mode, op0, op1);
4296 rtx mod = simplify_gen_binary (MOD, mode, op0, op1);
4297 rtx adj = ceil_sdiv_adjust (mode, mod, op1);
4298 return simplify_gen_binary (PLUS, mode, div, adj);
4301 case CEIL_MOD_EXPR:
4302 if (unsignedp)
4304 rtx mod = simplify_gen_binary (UMOD, mode, op0, op1);
4305 rtx adj = ceil_udiv_adjust (mode, mod, op1);
4306 adj = simplify_gen_unary (NEG, mode,
4307 simplify_gen_binary (MULT, mode, adj, op1),
4308 mode);
4309 return simplify_gen_binary (PLUS, mode, mod, adj);
4311 else
4313 rtx mod = simplify_gen_binary (MOD, mode, op0, op1);
4314 rtx adj = ceil_sdiv_adjust (mode, mod, op1);
4315 adj = simplify_gen_unary (NEG, mode,
4316 simplify_gen_binary (MULT, mode, adj, op1),
4317 mode);
4318 return simplify_gen_binary (PLUS, mode, mod, adj);
4321 case ROUND_DIV_EXPR:
4322 if (unsignedp)
4324 rtx div = simplify_gen_binary (UDIV, mode, op0, op1);
4325 rtx mod = simplify_gen_binary (UMOD, mode, op0, op1);
4326 rtx adj = round_udiv_adjust (mode, mod, op1);
4327 return simplify_gen_binary (PLUS, mode, div, adj);
4329 else
4331 rtx div = simplify_gen_binary (DIV, mode, op0, op1);
4332 rtx mod = simplify_gen_binary (MOD, mode, op0, op1);
4333 rtx adj = round_sdiv_adjust (mode, mod, op1);
4334 return simplify_gen_binary (PLUS, mode, div, adj);
4337 case ROUND_MOD_EXPR:
4338 if (unsignedp)
4340 rtx mod = simplify_gen_binary (UMOD, mode, op0, op1);
4341 rtx adj = round_udiv_adjust (mode, mod, op1);
4342 adj = simplify_gen_unary (NEG, mode,
4343 simplify_gen_binary (MULT, mode, adj, op1),
4344 mode);
4345 return simplify_gen_binary (PLUS, mode, mod, adj);
4347 else
4349 rtx mod = simplify_gen_binary (MOD, mode, op0, op1);
4350 rtx adj = round_sdiv_adjust (mode, mod, op1);
4351 adj = simplify_gen_unary (NEG, mode,
4352 simplify_gen_binary (MULT, mode, adj, op1),
4353 mode);
4354 return simplify_gen_binary (PLUS, mode, mod, adj);
4357 case LSHIFT_EXPR:
4358 return simplify_gen_binary (ASHIFT, mode, op0, op1);
4360 case RSHIFT_EXPR:
4361 if (unsignedp)
4362 return simplify_gen_binary (LSHIFTRT, mode, op0, op1);
4363 else
4364 return simplify_gen_binary (ASHIFTRT, mode, op0, op1);
4366 case LROTATE_EXPR:
4367 return simplify_gen_binary (ROTATE, mode, op0, op1);
4369 case RROTATE_EXPR:
4370 return simplify_gen_binary (ROTATERT, mode, op0, op1);
4372 case MIN_EXPR:
4373 return simplify_gen_binary (unsignedp ? UMIN : SMIN, mode, op0, op1);
4375 case MAX_EXPR:
4376 return simplify_gen_binary (unsignedp ? UMAX : SMAX, mode, op0, op1);
4378 case BIT_AND_EXPR:
4379 case TRUTH_AND_EXPR:
4380 return simplify_gen_binary (AND, mode, op0, op1);
4382 case BIT_IOR_EXPR:
4383 case TRUTH_OR_EXPR:
4384 return simplify_gen_binary (IOR, mode, op0, op1);
4386 case BIT_XOR_EXPR:
4387 case TRUTH_XOR_EXPR:
4388 return simplify_gen_binary (XOR, mode, op0, op1);
4390 case TRUTH_ANDIF_EXPR:
4391 return gen_rtx_IF_THEN_ELSE (mode, op0, op1, const0_rtx);
4393 case TRUTH_ORIF_EXPR:
4394 return gen_rtx_IF_THEN_ELSE (mode, op0, const_true_rtx, op1);
4396 case TRUTH_NOT_EXPR:
4397 return simplify_gen_relational (EQ, mode, inner_mode, op0, const0_rtx);
4399 case LT_EXPR:
4400 return simplify_gen_relational (unsignedp ? LTU : LT, mode, inner_mode,
4401 op0, op1);
4403 case LE_EXPR:
4404 return simplify_gen_relational (unsignedp ? LEU : LE, mode, inner_mode,
4405 op0, op1);
4407 case GT_EXPR:
4408 return simplify_gen_relational (unsignedp ? GTU : GT, mode, inner_mode,
4409 op0, op1);
4411 case GE_EXPR:
4412 return simplify_gen_relational (unsignedp ? GEU : GE, mode, inner_mode,
4413 op0, op1);
4415 case EQ_EXPR:
4416 return simplify_gen_relational (EQ, mode, inner_mode, op0, op1);
4418 case NE_EXPR:
4419 return simplify_gen_relational (NE, mode, inner_mode, op0, op1);
4421 case UNORDERED_EXPR:
4422 return simplify_gen_relational (UNORDERED, mode, inner_mode, op0, op1);
4424 case ORDERED_EXPR:
4425 return simplify_gen_relational (ORDERED, mode, inner_mode, op0, op1);
4427 case UNLT_EXPR:
4428 return simplify_gen_relational (UNLT, mode, inner_mode, op0, op1);
4430 case UNLE_EXPR:
4431 return simplify_gen_relational (UNLE, mode, inner_mode, op0, op1);
4433 case UNGT_EXPR:
4434 return simplify_gen_relational (UNGT, mode, inner_mode, op0, op1);
4436 case UNGE_EXPR:
4437 return simplify_gen_relational (UNGE, mode, inner_mode, op0, op1);
4439 case UNEQ_EXPR:
4440 return simplify_gen_relational (UNEQ, mode, inner_mode, op0, op1);
4442 case LTGT_EXPR:
4443 return simplify_gen_relational (LTGT, mode, inner_mode, op0, op1);
4445 case COND_EXPR:
4446 return gen_rtx_IF_THEN_ELSE (mode, op0, op1, op2);
4448 case COMPLEX_EXPR:
4449 gcc_assert (COMPLEX_MODE_P (mode));
4450 if (GET_MODE (op0) == VOIDmode)
4451 op0 = gen_rtx_CONST (GET_MODE_INNER (mode), op0);
4452 if (GET_MODE (op1) == VOIDmode)
4453 op1 = gen_rtx_CONST (GET_MODE_INNER (mode), op1);
4454 return gen_rtx_CONCAT (mode, op0, op1);
4456 case CONJ_EXPR:
4457 if (GET_CODE (op0) == CONCAT)
4458 return gen_rtx_CONCAT (mode, XEXP (op0, 0),
4459 simplify_gen_unary (NEG, GET_MODE_INNER (mode),
4460 XEXP (op0, 1),
4461 GET_MODE_INNER (mode)));
4462 else
4464 machine_mode imode = GET_MODE_INNER (mode);
4465 rtx re, im;
4467 if (MEM_P (op0))
4469 re = adjust_address_nv (op0, imode, 0);
4470 im = adjust_address_nv (op0, imode, GET_MODE_SIZE (imode));
4472 else
4474 machine_mode ifmode = int_mode_for_mode (mode);
4475 machine_mode ihmode = int_mode_for_mode (imode);
4476 rtx halfsize;
4477 if (ifmode == BLKmode || ihmode == BLKmode)
4478 return NULL;
4479 halfsize = GEN_INT (GET_MODE_BITSIZE (ihmode));
4480 re = op0;
4481 if (mode != ifmode)
4482 re = gen_rtx_SUBREG (ifmode, re, 0);
4483 re = gen_rtx_ZERO_EXTRACT (ihmode, re, halfsize, const0_rtx);
4484 if (imode != ihmode)
4485 re = gen_rtx_SUBREG (imode, re, 0);
4486 im = copy_rtx (op0);
4487 if (mode != ifmode)
4488 im = gen_rtx_SUBREG (ifmode, im, 0);
4489 im = gen_rtx_ZERO_EXTRACT (ihmode, im, halfsize, halfsize);
4490 if (imode != ihmode)
4491 im = gen_rtx_SUBREG (imode, im, 0);
4493 im = gen_rtx_NEG (imode, im);
4494 return gen_rtx_CONCAT (mode, re, im);
4497 case ADDR_EXPR:
4498 op0 = expand_debug_expr (TREE_OPERAND (exp, 0));
4499 if (!op0 || !MEM_P (op0))
4501 if ((TREE_CODE (TREE_OPERAND (exp, 0)) == VAR_DECL
4502 || TREE_CODE (TREE_OPERAND (exp, 0)) == PARM_DECL
4503 || TREE_CODE (TREE_OPERAND (exp, 0)) == RESULT_DECL)
4504 && (!TREE_ADDRESSABLE (TREE_OPERAND (exp, 0))
4505 || target_for_debug_bind (TREE_OPERAND (exp, 0))))
4506 return gen_rtx_DEBUG_IMPLICIT_PTR (mode, TREE_OPERAND (exp, 0));
4508 if (handled_component_p (TREE_OPERAND (exp, 0)))
4510 HOST_WIDE_INT bitoffset, bitsize, maxsize;
4511 tree decl
4512 = get_ref_base_and_extent (TREE_OPERAND (exp, 0),
4513 &bitoffset, &bitsize, &maxsize);
4514 if ((TREE_CODE (decl) == VAR_DECL
4515 || TREE_CODE (decl) == PARM_DECL
4516 || TREE_CODE (decl) == RESULT_DECL)
4517 && (!TREE_ADDRESSABLE (decl)
4518 || target_for_debug_bind (decl))
4519 && (bitoffset % BITS_PER_UNIT) == 0
4520 && bitsize > 0
4521 && bitsize == maxsize)
4523 rtx base = gen_rtx_DEBUG_IMPLICIT_PTR (mode, decl);
4524 return plus_constant (mode, base, bitoffset / BITS_PER_UNIT);
4528 if (TREE_CODE (TREE_OPERAND (exp, 0)) == MEM_REF
4529 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
4530 == ADDR_EXPR)
4532 op0 = expand_debug_expr (TREE_OPERAND (TREE_OPERAND (exp, 0),
4533 0));
4534 if (op0 != NULL
4535 && (GET_CODE (op0) == DEBUG_IMPLICIT_PTR
4536 || (GET_CODE (op0) == PLUS
4537 && GET_CODE (XEXP (op0, 0)) == DEBUG_IMPLICIT_PTR
4538 && CONST_INT_P (XEXP (op0, 1)))))
4540 op1 = expand_debug_expr (TREE_OPERAND (TREE_OPERAND (exp, 0),
4541 1));
4542 if (!op1 || !CONST_INT_P (op1))
4543 return NULL;
4545 return plus_constant (mode, op0, INTVAL (op1));
4549 return NULL;
4552 as = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (exp)));
4553 op0 = convert_debug_memory_address (mode, XEXP (op0, 0), as);
4555 return op0;
4557 case VECTOR_CST:
4559 unsigned i;
4561 op0 = gen_rtx_CONCATN
4562 (mode, rtvec_alloc (TYPE_VECTOR_SUBPARTS (TREE_TYPE (exp))));
4564 for (i = 0; i < VECTOR_CST_NELTS (exp); ++i)
4566 op1 = expand_debug_expr (VECTOR_CST_ELT (exp, i));
4567 if (!op1)
4568 return NULL;
4569 XVECEXP (op0, 0, i) = op1;
4572 return op0;
4575 case CONSTRUCTOR:
4576 if (TREE_CLOBBER_P (exp))
4577 return NULL;
4578 else if (TREE_CODE (TREE_TYPE (exp)) == VECTOR_TYPE)
4580 unsigned i;
4581 tree val;
4583 op0 = gen_rtx_CONCATN
4584 (mode, rtvec_alloc (TYPE_VECTOR_SUBPARTS (TREE_TYPE (exp))));
4586 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), i, val)
4588 op1 = expand_debug_expr (val);
4589 if (!op1)
4590 return NULL;
4591 XVECEXP (op0, 0, i) = op1;
4594 if (i < TYPE_VECTOR_SUBPARTS (TREE_TYPE (exp)))
4596 op1 = expand_debug_expr
4597 (build_zero_cst (TREE_TYPE (TREE_TYPE (exp))));
4599 if (!op1)
4600 return NULL;
4602 for (; i < TYPE_VECTOR_SUBPARTS (TREE_TYPE (exp)); i++)
4603 XVECEXP (op0, 0, i) = op1;
4606 return op0;
4608 else
4609 goto flag_unsupported;
4611 case CALL_EXPR:
4612 /* ??? Maybe handle some builtins? */
4613 return NULL;
4615 case SSA_NAME:
4617 gimple g = get_gimple_for_ssa_name (exp);
4618 if (g)
4620 op0 = expand_debug_expr (gimple_assign_rhs_to_tree (g));
4621 if (!op0)
4622 return NULL;
4624 else
4626 int part = var_to_partition (SA.map, exp);
4628 if (part == NO_PARTITION)
4630 /* If this is a reference to an incoming value of parameter
4631 that is never used in the code or where the incoming
4632 value is never used in the code, use PARM_DECL's
4633 DECL_RTL if set. */
4634 if (SSA_NAME_IS_DEFAULT_DEF (exp)
4635 && TREE_CODE (SSA_NAME_VAR (exp)) == PARM_DECL)
4637 op0 = expand_debug_parm_decl (SSA_NAME_VAR (exp));
4638 if (op0)
4639 goto adjust_mode;
4640 op0 = expand_debug_expr (SSA_NAME_VAR (exp));
4641 if (op0)
4642 goto adjust_mode;
4644 return NULL;
4647 gcc_assert (part >= 0 && (unsigned)part < SA.map->num_partitions);
4649 op0 = copy_rtx (SA.partition_to_pseudo[part]);
4651 goto adjust_mode;
4654 case ERROR_MARK:
4655 return NULL;
4657 /* Vector stuff. For most of the codes we don't have rtl codes. */
4658 case REALIGN_LOAD_EXPR:
4659 case REDUC_MAX_EXPR:
4660 case REDUC_MIN_EXPR:
4661 case REDUC_PLUS_EXPR:
4662 case VEC_COND_EXPR:
4663 case VEC_PACK_FIX_TRUNC_EXPR:
4664 case VEC_PACK_SAT_EXPR:
4665 case VEC_PACK_TRUNC_EXPR:
4666 case VEC_UNPACK_FLOAT_HI_EXPR:
4667 case VEC_UNPACK_FLOAT_LO_EXPR:
4668 case VEC_UNPACK_HI_EXPR:
4669 case VEC_UNPACK_LO_EXPR:
4670 case VEC_WIDEN_MULT_HI_EXPR:
4671 case VEC_WIDEN_MULT_LO_EXPR:
4672 case VEC_WIDEN_MULT_EVEN_EXPR:
4673 case VEC_WIDEN_MULT_ODD_EXPR:
4674 case VEC_WIDEN_LSHIFT_HI_EXPR:
4675 case VEC_WIDEN_LSHIFT_LO_EXPR:
4676 case VEC_PERM_EXPR:
4677 return NULL;
4679 /* Misc codes. */
4680 case ADDR_SPACE_CONVERT_EXPR:
4681 case FIXED_CONVERT_EXPR:
4682 case OBJ_TYPE_REF:
4683 case WITH_SIZE_EXPR:
4684 return NULL;
4686 case DOT_PROD_EXPR:
4687 if (SCALAR_INT_MODE_P (GET_MODE (op0))
4688 && SCALAR_INT_MODE_P (mode))
4691 = simplify_gen_unary (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp,
4692 0)))
4693 ? ZERO_EXTEND : SIGN_EXTEND, mode, op0,
4694 inner_mode);
4696 = simplify_gen_unary (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp,
4697 1)))
4698 ? ZERO_EXTEND : SIGN_EXTEND, mode, op1,
4699 inner_mode);
4700 op0 = simplify_gen_binary (MULT, mode, op0, op1);
4701 return simplify_gen_binary (PLUS, mode, op0, op2);
4703 return NULL;
4705 case WIDEN_MULT_EXPR:
4706 case WIDEN_MULT_PLUS_EXPR:
4707 case WIDEN_MULT_MINUS_EXPR:
4708 if (SCALAR_INT_MODE_P (GET_MODE (op0))
4709 && SCALAR_INT_MODE_P (mode))
4711 inner_mode = GET_MODE (op0);
4712 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp, 0))))
4713 op0 = simplify_gen_unary (ZERO_EXTEND, mode, op0, inner_mode);
4714 else
4715 op0 = simplify_gen_unary (SIGN_EXTEND, mode, op0, inner_mode);
4716 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp, 1))))
4717 op1 = simplify_gen_unary (ZERO_EXTEND, mode, op1, inner_mode);
4718 else
4719 op1 = simplify_gen_unary (SIGN_EXTEND, mode, op1, inner_mode);
4720 op0 = simplify_gen_binary (MULT, mode, op0, op1);
4721 if (TREE_CODE (exp) == WIDEN_MULT_EXPR)
4722 return op0;
4723 else if (TREE_CODE (exp) == WIDEN_MULT_PLUS_EXPR)
4724 return simplify_gen_binary (PLUS, mode, op0, op2);
4725 else
4726 return simplify_gen_binary (MINUS, mode, op2, op0);
4728 return NULL;
4730 case MULT_HIGHPART_EXPR:
4731 /* ??? Similar to the above. */
4732 return NULL;
4734 case WIDEN_SUM_EXPR:
4735 case WIDEN_LSHIFT_EXPR:
4736 if (SCALAR_INT_MODE_P (GET_MODE (op0))
4737 && SCALAR_INT_MODE_P (mode))
4740 = simplify_gen_unary (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp,
4741 0)))
4742 ? ZERO_EXTEND : SIGN_EXTEND, mode, op0,
4743 inner_mode);
4744 return simplify_gen_binary (TREE_CODE (exp) == WIDEN_LSHIFT_EXPR
4745 ? ASHIFT : PLUS, mode, op0, op1);
4747 return NULL;
4749 case FMA_EXPR:
4750 return simplify_gen_ternary (FMA, mode, inner_mode, op0, op1, op2);
4752 default:
4753 flag_unsupported:
4754 #ifdef ENABLE_CHECKING
4755 debug_tree (exp);
4756 gcc_unreachable ();
4757 #else
4758 return NULL;
4759 #endif
4763 /* Return an RTX equivalent to the source bind value of the tree expression
4764 EXP. */
4766 static rtx
4767 expand_debug_source_expr (tree exp)
4769 rtx op0 = NULL_RTX;
4770 machine_mode mode = VOIDmode, inner_mode;
4772 switch (TREE_CODE (exp))
4774 case PARM_DECL:
4776 mode = DECL_MODE (exp);
4777 op0 = expand_debug_parm_decl (exp);
4778 if (op0)
4779 break;
4780 /* See if this isn't an argument that has been completely
4781 optimized out. */
4782 if (!DECL_RTL_SET_P (exp)
4783 && !DECL_INCOMING_RTL (exp)
4784 && DECL_ABSTRACT_ORIGIN (current_function_decl))
4786 tree aexp = DECL_ORIGIN (exp);
4787 if (DECL_CONTEXT (aexp)
4788 == DECL_ABSTRACT_ORIGIN (current_function_decl))
4790 vec<tree, va_gc> **debug_args;
4791 unsigned int ix;
4792 tree ddecl;
4793 debug_args = decl_debug_args_lookup (current_function_decl);
4794 if (debug_args != NULL)
4796 for (ix = 0; vec_safe_iterate (*debug_args, ix, &ddecl);
4797 ix += 2)
4798 if (ddecl == aexp)
4799 return gen_rtx_DEBUG_PARAMETER_REF (mode, aexp);
4803 break;
4805 default:
4806 break;
4809 if (op0 == NULL_RTX)
4810 return NULL_RTX;
4812 inner_mode = GET_MODE (op0);
4813 if (mode == inner_mode)
4814 return op0;
4816 if (FLOAT_MODE_P (mode) && FLOAT_MODE_P (inner_mode))
4818 if (GET_MODE_BITSIZE (mode) == GET_MODE_BITSIZE (inner_mode))
4819 op0 = simplify_gen_subreg (mode, op0, inner_mode, 0);
4820 else if (GET_MODE_BITSIZE (mode) < GET_MODE_BITSIZE (inner_mode))
4821 op0 = simplify_gen_unary (FLOAT_TRUNCATE, mode, op0, inner_mode);
4822 else
4823 op0 = simplify_gen_unary (FLOAT_EXTEND, mode, op0, inner_mode);
4825 else if (FLOAT_MODE_P (mode))
4826 gcc_unreachable ();
4827 else if (FLOAT_MODE_P (inner_mode))
4829 if (TYPE_UNSIGNED (TREE_TYPE (exp)))
4830 op0 = simplify_gen_unary (UNSIGNED_FIX, mode, op0, inner_mode);
4831 else
4832 op0 = simplify_gen_unary (FIX, mode, op0, inner_mode);
4834 else if (CONSTANT_P (op0)
4835 || GET_MODE_BITSIZE (mode) <= GET_MODE_BITSIZE (inner_mode))
4836 op0 = simplify_gen_subreg (mode, op0, inner_mode,
4837 subreg_lowpart_offset (mode, inner_mode));
4838 else if (TYPE_UNSIGNED (TREE_TYPE (exp)))
4839 op0 = simplify_gen_unary (ZERO_EXTEND, mode, op0, inner_mode);
4840 else
4841 op0 = simplify_gen_unary (SIGN_EXTEND, mode, op0, inner_mode);
4843 return op0;
4846 /* Ensure INSN_VAR_LOCATION_LOC (insn) doesn't have unbound complexity.
4847 Allow 4 levels of rtl nesting for most rtl codes, and if we see anything
4848 deeper than that, create DEBUG_EXPRs and emit DEBUG_INSNs before INSN. */
4850 static void
4851 avoid_complex_debug_insns (rtx_insn *insn, rtx *exp_p, int depth)
4853 rtx exp = *exp_p;
4855 if (exp == NULL_RTX)
4856 return;
4858 if ((OBJECT_P (exp) && !MEM_P (exp)) || GET_CODE (exp) == CLOBBER)
4859 return;
4861 if (depth == 4)
4863 /* Create DEBUG_EXPR (and DEBUG_EXPR_DECL). */
4864 rtx dval = make_debug_expr_from_rtl (exp);
4866 /* Emit a debug bind insn before INSN. */
4867 rtx bind = gen_rtx_VAR_LOCATION (GET_MODE (exp),
4868 DEBUG_EXPR_TREE_DECL (dval), exp,
4869 VAR_INIT_STATUS_INITIALIZED);
4871 emit_debug_insn_before (bind, insn);
4872 *exp_p = dval;
4873 return;
4876 const char *format_ptr = GET_RTX_FORMAT (GET_CODE (exp));
4877 int i, j;
4878 for (i = 0; i < GET_RTX_LENGTH (GET_CODE (exp)); i++)
4879 switch (*format_ptr++)
4881 case 'e':
4882 avoid_complex_debug_insns (insn, &XEXP (exp, i), depth + 1);
4883 break;
4885 case 'E':
4886 case 'V':
4887 for (j = 0; j < XVECLEN (exp, i); j++)
4888 avoid_complex_debug_insns (insn, &XVECEXP (exp, i, j), depth + 1);
4889 break;
4891 default:
4892 break;
4896 /* Expand the _LOCs in debug insns. We run this after expanding all
4897 regular insns, so that any variables referenced in the function
4898 will have their DECL_RTLs set. */
4900 static void
4901 expand_debug_locations (void)
4903 rtx_insn *insn;
4904 rtx_insn *last = get_last_insn ();
4905 int save_strict_alias = flag_strict_aliasing;
4907 /* New alias sets while setting up memory attributes cause
4908 -fcompare-debug failures, even though it doesn't bring about any
4909 codegen changes. */
4910 flag_strict_aliasing = 0;
4912 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
4913 if (DEBUG_INSN_P (insn))
4915 tree value = (tree)INSN_VAR_LOCATION_LOC (insn);
4916 rtx val;
4917 rtx_insn *prev_insn, *insn2;
4918 machine_mode mode;
4920 if (value == NULL_TREE)
4921 val = NULL_RTX;
4922 else
4924 if (INSN_VAR_LOCATION_STATUS (insn)
4925 == VAR_INIT_STATUS_UNINITIALIZED)
4926 val = expand_debug_source_expr (value);
4927 else
4928 val = expand_debug_expr (value);
4929 gcc_assert (last == get_last_insn ());
4932 if (!val)
4933 val = gen_rtx_UNKNOWN_VAR_LOC ();
4934 else
4936 mode = GET_MODE (INSN_VAR_LOCATION (insn));
4938 gcc_assert (mode == GET_MODE (val)
4939 || (GET_MODE (val) == VOIDmode
4940 && (CONST_SCALAR_INT_P (val)
4941 || GET_CODE (val) == CONST_FIXED
4942 || GET_CODE (val) == LABEL_REF)));
4945 INSN_VAR_LOCATION_LOC (insn) = val;
4946 prev_insn = PREV_INSN (insn);
4947 for (insn2 = insn; insn2 != prev_insn; insn2 = PREV_INSN (insn2))
4948 avoid_complex_debug_insns (insn2, &INSN_VAR_LOCATION_LOC (insn2), 0);
4951 flag_strict_aliasing = save_strict_alias;
4954 /* Expand basic block BB from GIMPLE trees to RTL. */
4956 static basic_block
4957 expand_gimple_basic_block (basic_block bb, bool disable_tail_calls)
4959 gimple_stmt_iterator gsi;
4960 gimple_seq stmts;
4961 gimple stmt = NULL;
4962 rtx_note *note;
4963 rtx_insn *last;
4964 edge e;
4965 edge_iterator ei;
4967 if (dump_file)
4968 fprintf (dump_file, "\n;; Generating RTL for gimple basic block %d\n",
4969 bb->index);
4971 /* Note that since we are now transitioning from GIMPLE to RTL, we
4972 cannot use the gsi_*_bb() routines because they expect the basic
4973 block to be in GIMPLE, instead of RTL. Therefore, we need to
4974 access the BB sequence directly. */
4975 stmts = bb_seq (bb);
4976 bb->il.gimple.seq = NULL;
4977 bb->il.gimple.phi_nodes = NULL;
4978 rtl_profile_for_bb (bb);
4979 init_rtl_bb_info (bb);
4980 bb->flags |= BB_RTL;
4982 /* Remove the RETURN_EXPR if we may fall though to the exit
4983 instead. */
4984 gsi = gsi_last (stmts);
4985 if (!gsi_end_p (gsi)
4986 && gimple_code (gsi_stmt (gsi)) == GIMPLE_RETURN)
4988 greturn *ret_stmt = as_a <greturn *> (gsi_stmt (gsi));
4990 gcc_assert (single_succ_p (bb));
4991 gcc_assert (single_succ (bb) == EXIT_BLOCK_PTR_FOR_FN (cfun));
4993 if (bb->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
4994 && !gimple_return_retval (ret_stmt))
4996 gsi_remove (&gsi, false);
4997 single_succ_edge (bb)->flags |= EDGE_FALLTHRU;
5001 gsi = gsi_start (stmts);
5002 if (!gsi_end_p (gsi))
5004 stmt = gsi_stmt (gsi);
5005 if (gimple_code (stmt) != GIMPLE_LABEL)
5006 stmt = NULL;
5009 rtx_code_label **elt = lab_rtx_for_bb->get (bb);
5011 if (stmt || elt)
5013 last = get_last_insn ();
5015 if (stmt)
5017 expand_gimple_stmt (stmt);
5018 gsi_next (&gsi);
5021 if (elt)
5022 emit_label (*elt);
5024 /* Java emits line number notes in the top of labels.
5025 ??? Make this go away once line number notes are obsoleted. */
5026 BB_HEAD (bb) = NEXT_INSN (last);
5027 if (NOTE_P (BB_HEAD (bb)))
5028 BB_HEAD (bb) = NEXT_INSN (BB_HEAD (bb));
5029 note = emit_note_after (NOTE_INSN_BASIC_BLOCK, BB_HEAD (bb));
5031 maybe_dump_rtl_for_gimple_stmt (stmt, last);
5033 else
5034 BB_HEAD (bb) = note = emit_note (NOTE_INSN_BASIC_BLOCK);
5036 NOTE_BASIC_BLOCK (note) = bb;
5038 for (; !gsi_end_p (gsi); gsi_next (&gsi))
5040 basic_block new_bb;
5042 stmt = gsi_stmt (gsi);
5044 /* If this statement is a non-debug one, and we generate debug
5045 insns, then this one might be the last real use of a TERed
5046 SSA_NAME, but where there are still some debug uses further
5047 down. Expanding the current SSA name in such further debug
5048 uses by their RHS might lead to wrong debug info, as coalescing
5049 might make the operands of such RHS be placed into the same
5050 pseudo as something else. Like so:
5051 a_1 = a_0 + 1; // Assume a_1 is TERed and a_0 is dead
5052 use(a_1);
5053 a_2 = ...
5054 #DEBUG ... => a_1
5055 As a_0 and a_2 don't overlap in lifetime, assume they are coalesced.
5056 If we now would expand a_1 by it's RHS (a_0 + 1) in the debug use,
5057 the write to a_2 would actually have clobbered the place which
5058 formerly held a_0.
5060 So, instead of that, we recognize the situation, and generate
5061 debug temporaries at the last real use of TERed SSA names:
5062 a_1 = a_0 + 1;
5063 #DEBUG #D1 => a_1
5064 use(a_1);
5065 a_2 = ...
5066 #DEBUG ... => #D1
5068 if (MAY_HAVE_DEBUG_INSNS
5069 && SA.values
5070 && !is_gimple_debug (stmt))
5072 ssa_op_iter iter;
5073 tree op;
5074 gimple def;
5076 location_t sloc = curr_insn_location ();
5078 /* Look for SSA names that have their last use here (TERed
5079 names always have only one real use). */
5080 FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_USE)
5081 if ((def = get_gimple_for_ssa_name (op)))
5083 imm_use_iterator imm_iter;
5084 use_operand_p use_p;
5085 bool have_debug_uses = false;
5087 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, op)
5089 if (gimple_debug_bind_p (USE_STMT (use_p)))
5091 have_debug_uses = true;
5092 break;
5096 if (have_debug_uses)
5098 /* OP is a TERed SSA name, with DEF it's defining
5099 statement, and where OP is used in further debug
5100 instructions. Generate a debug temporary, and
5101 replace all uses of OP in debug insns with that
5102 temporary. */
5103 gimple debugstmt;
5104 tree value = gimple_assign_rhs_to_tree (def);
5105 tree vexpr = make_node (DEBUG_EXPR_DECL);
5106 rtx val;
5107 machine_mode mode;
5109 set_curr_insn_location (gimple_location (def));
5111 DECL_ARTIFICIAL (vexpr) = 1;
5112 TREE_TYPE (vexpr) = TREE_TYPE (value);
5113 if (DECL_P (value))
5114 mode = DECL_MODE (value);
5115 else
5116 mode = TYPE_MODE (TREE_TYPE (value));
5117 DECL_MODE (vexpr) = mode;
5119 val = gen_rtx_VAR_LOCATION
5120 (mode, vexpr, (rtx)value, VAR_INIT_STATUS_INITIALIZED);
5122 emit_debug_insn (val);
5124 FOR_EACH_IMM_USE_STMT (debugstmt, imm_iter, op)
5126 if (!gimple_debug_bind_p (debugstmt))
5127 continue;
5129 FOR_EACH_IMM_USE_ON_STMT (use_p, imm_iter)
5130 SET_USE (use_p, vexpr);
5132 update_stmt (debugstmt);
5136 set_curr_insn_location (sloc);
5139 currently_expanding_gimple_stmt = stmt;
5141 /* Expand this statement, then evaluate the resulting RTL and
5142 fixup the CFG accordingly. */
5143 if (gimple_code (stmt) == GIMPLE_COND)
5145 new_bb = expand_gimple_cond (bb, as_a <gcond *> (stmt));
5146 if (new_bb)
5147 return new_bb;
5149 else if (gimple_debug_bind_p (stmt))
5151 location_t sloc = curr_insn_location ();
5152 gimple_stmt_iterator nsi = gsi;
5154 for (;;)
5156 tree var = gimple_debug_bind_get_var (stmt);
5157 tree value;
5158 rtx val;
5159 machine_mode mode;
5161 if (TREE_CODE (var) != DEBUG_EXPR_DECL
5162 && TREE_CODE (var) != LABEL_DECL
5163 && !target_for_debug_bind (var))
5164 goto delink_debug_stmt;
5166 if (gimple_debug_bind_has_value_p (stmt))
5167 value = gimple_debug_bind_get_value (stmt);
5168 else
5169 value = NULL_TREE;
5171 last = get_last_insn ();
5173 set_curr_insn_location (gimple_location (stmt));
5175 if (DECL_P (var))
5176 mode = DECL_MODE (var);
5177 else
5178 mode = TYPE_MODE (TREE_TYPE (var));
5180 val = gen_rtx_VAR_LOCATION
5181 (mode, var, (rtx)value, VAR_INIT_STATUS_INITIALIZED);
5183 emit_debug_insn (val);
5185 if (dump_file && (dump_flags & TDF_DETAILS))
5187 /* We can't dump the insn with a TREE where an RTX
5188 is expected. */
5189 PAT_VAR_LOCATION_LOC (val) = const0_rtx;
5190 maybe_dump_rtl_for_gimple_stmt (stmt, last);
5191 PAT_VAR_LOCATION_LOC (val) = (rtx)value;
5194 delink_debug_stmt:
5195 /* In order not to generate too many debug temporaries,
5196 we delink all uses of debug statements we already expanded.
5197 Therefore debug statements between definition and real
5198 use of TERed SSA names will continue to use the SSA name,
5199 and not be replaced with debug temps. */
5200 delink_stmt_imm_use (stmt);
5202 gsi = nsi;
5203 gsi_next (&nsi);
5204 if (gsi_end_p (nsi))
5205 break;
5206 stmt = gsi_stmt (nsi);
5207 if (!gimple_debug_bind_p (stmt))
5208 break;
5211 set_curr_insn_location (sloc);
5213 else if (gimple_debug_source_bind_p (stmt))
5215 location_t sloc = curr_insn_location ();
5216 tree var = gimple_debug_source_bind_get_var (stmt);
5217 tree value = gimple_debug_source_bind_get_value (stmt);
5218 rtx val;
5219 machine_mode mode;
5221 last = get_last_insn ();
5223 set_curr_insn_location (gimple_location (stmt));
5225 mode = DECL_MODE (var);
5227 val = gen_rtx_VAR_LOCATION (mode, var, (rtx)value,
5228 VAR_INIT_STATUS_UNINITIALIZED);
5230 emit_debug_insn (val);
5232 if (dump_file && (dump_flags & TDF_DETAILS))
5234 /* We can't dump the insn with a TREE where an RTX
5235 is expected. */
5236 PAT_VAR_LOCATION_LOC (val) = const0_rtx;
5237 maybe_dump_rtl_for_gimple_stmt (stmt, last);
5238 PAT_VAR_LOCATION_LOC (val) = (rtx)value;
5241 set_curr_insn_location (sloc);
5243 else
5245 gcall *call_stmt = dyn_cast <gcall *> (stmt);
5246 if (call_stmt
5247 && gimple_call_tail_p (call_stmt)
5248 && disable_tail_calls)
5249 gimple_call_set_tail (call_stmt, false);
5251 if (call_stmt && gimple_call_tail_p (call_stmt))
5253 bool can_fallthru;
5254 new_bb = expand_gimple_tailcall (bb, call_stmt, &can_fallthru);
5255 if (new_bb)
5257 if (can_fallthru)
5258 bb = new_bb;
5259 else
5260 return new_bb;
5263 else
5265 def_operand_p def_p;
5266 def_p = SINGLE_SSA_DEF_OPERAND (stmt, SSA_OP_DEF);
5268 if (def_p != NULL)
5270 /* Ignore this stmt if it is in the list of
5271 replaceable expressions. */
5272 if (SA.values
5273 && bitmap_bit_p (SA.values,
5274 SSA_NAME_VERSION (DEF_FROM_PTR (def_p))))
5275 continue;
5277 last = expand_gimple_stmt (stmt);
5278 maybe_dump_rtl_for_gimple_stmt (stmt, last);
5283 currently_expanding_gimple_stmt = NULL;
5285 /* Expand implicit goto and convert goto_locus. */
5286 FOR_EACH_EDGE (e, ei, bb->succs)
5288 if (e->goto_locus != UNKNOWN_LOCATION)
5289 set_curr_insn_location (e->goto_locus);
5290 if ((e->flags & EDGE_FALLTHRU) && e->dest != bb->next_bb)
5292 emit_jump (label_rtx_for_bb (e->dest));
5293 e->flags &= ~EDGE_FALLTHRU;
5297 /* Expanded RTL can create a jump in the last instruction of block.
5298 This later might be assumed to be a jump to successor and break edge insertion.
5299 We need to insert dummy move to prevent this. PR41440. */
5300 if (single_succ_p (bb)
5301 && (single_succ_edge (bb)->flags & EDGE_FALLTHRU)
5302 && (last = get_last_insn ())
5303 && JUMP_P (last))
5305 rtx dummy = gen_reg_rtx (SImode);
5306 emit_insn_after_noloc (gen_move_insn (dummy, dummy), last, NULL);
5309 do_pending_stack_adjust ();
5311 /* Find the block tail. The last insn in the block is the insn
5312 before a barrier and/or table jump insn. */
5313 last = get_last_insn ();
5314 if (BARRIER_P (last))
5315 last = PREV_INSN (last);
5316 if (JUMP_TABLE_DATA_P (last))
5317 last = PREV_INSN (PREV_INSN (last));
5318 BB_END (bb) = last;
5320 update_bb_for_insn (bb);
5322 return bb;
5326 /* Create a basic block for initialization code. */
5328 static basic_block
5329 construct_init_block (void)
5331 basic_block init_block, first_block;
5332 edge e = NULL;
5333 int flags;
5335 /* Multiple entry points not supported yet. */
5336 gcc_assert (EDGE_COUNT (ENTRY_BLOCK_PTR_FOR_FN (cfun)->succs) == 1);
5337 init_rtl_bb_info (ENTRY_BLOCK_PTR_FOR_FN (cfun));
5338 init_rtl_bb_info (EXIT_BLOCK_PTR_FOR_FN (cfun));
5339 ENTRY_BLOCK_PTR_FOR_FN (cfun)->flags |= BB_RTL;
5340 EXIT_BLOCK_PTR_FOR_FN (cfun)->flags |= BB_RTL;
5342 e = EDGE_SUCC (ENTRY_BLOCK_PTR_FOR_FN (cfun), 0);
5344 /* When entry edge points to first basic block, we don't need jump,
5345 otherwise we have to jump into proper target. */
5346 if (e && e->dest != ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb)
5348 tree label = gimple_block_label (e->dest);
5350 emit_jump (label_rtx (label));
5351 flags = 0;
5353 else
5354 flags = EDGE_FALLTHRU;
5356 init_block = create_basic_block (NEXT_INSN (get_insns ()),
5357 get_last_insn (),
5358 ENTRY_BLOCK_PTR_FOR_FN (cfun));
5359 init_block->frequency = ENTRY_BLOCK_PTR_FOR_FN (cfun)->frequency;
5360 init_block->count = ENTRY_BLOCK_PTR_FOR_FN (cfun)->count;
5361 add_bb_to_loop (init_block, ENTRY_BLOCK_PTR_FOR_FN (cfun)->loop_father);
5362 if (e)
5364 first_block = e->dest;
5365 redirect_edge_succ (e, init_block);
5366 e = make_edge (init_block, first_block, flags);
5368 else
5369 e = make_edge (init_block, EXIT_BLOCK_PTR_FOR_FN (cfun), EDGE_FALLTHRU);
5370 e->probability = REG_BR_PROB_BASE;
5371 e->count = ENTRY_BLOCK_PTR_FOR_FN (cfun)->count;
5373 update_bb_for_insn (init_block);
5374 return init_block;
5377 /* For each lexical block, set BLOCK_NUMBER to the depth at which it is
5378 found in the block tree. */
5380 static void
5381 set_block_levels (tree block, int level)
5383 while (block)
5385 BLOCK_NUMBER (block) = level;
5386 set_block_levels (BLOCK_SUBBLOCKS (block), level + 1);
5387 block = BLOCK_CHAIN (block);
5391 /* Create a block containing landing pads and similar stuff. */
5393 static void
5394 construct_exit_block (void)
5396 rtx_insn *head = get_last_insn ();
5397 rtx_insn *end;
5398 basic_block exit_block;
5399 edge e, e2;
5400 unsigned ix;
5401 edge_iterator ei;
5402 basic_block prev_bb = EXIT_BLOCK_PTR_FOR_FN (cfun)->prev_bb;
5403 rtx_insn *orig_end = BB_END (prev_bb);
5405 rtl_profile_for_bb (EXIT_BLOCK_PTR_FOR_FN (cfun));
5407 /* Make sure the locus is set to the end of the function, so that
5408 epilogue line numbers and warnings are set properly. */
5409 if (LOCATION_LOCUS (cfun->function_end_locus) != UNKNOWN_LOCATION)
5410 input_location = cfun->function_end_locus;
5412 /* Generate rtl for function exit. */
5413 expand_function_end ();
5415 end = get_last_insn ();
5416 if (head == end)
5417 return;
5418 /* While emitting the function end we could move end of the last basic
5419 block. */
5420 BB_END (prev_bb) = orig_end;
5421 while (NEXT_INSN (head) && NOTE_P (NEXT_INSN (head)))
5422 head = NEXT_INSN (head);
5423 /* But make sure exit_block starts with RETURN_LABEL, otherwise the
5424 bb frequency counting will be confused. Any instructions before that
5425 label are emitted for the case where PREV_BB falls through into the
5426 exit block, so append those instructions to prev_bb in that case. */
5427 if (NEXT_INSN (head) != return_label)
5429 while (NEXT_INSN (head) != return_label)
5431 if (!NOTE_P (NEXT_INSN (head)))
5432 BB_END (prev_bb) = NEXT_INSN (head);
5433 head = NEXT_INSN (head);
5436 exit_block = create_basic_block (NEXT_INSN (head), end, prev_bb);
5437 exit_block->frequency = EXIT_BLOCK_PTR_FOR_FN (cfun)->frequency;
5438 exit_block->count = EXIT_BLOCK_PTR_FOR_FN (cfun)->count;
5439 add_bb_to_loop (exit_block, EXIT_BLOCK_PTR_FOR_FN (cfun)->loop_father);
5441 ix = 0;
5442 while (ix < EDGE_COUNT (EXIT_BLOCK_PTR_FOR_FN (cfun)->preds))
5444 e = EDGE_PRED (EXIT_BLOCK_PTR_FOR_FN (cfun), ix);
5445 if (!(e->flags & EDGE_ABNORMAL))
5446 redirect_edge_succ (e, exit_block);
5447 else
5448 ix++;
5451 e = make_edge (exit_block, EXIT_BLOCK_PTR_FOR_FN (cfun), EDGE_FALLTHRU);
5452 e->probability = REG_BR_PROB_BASE;
5453 e->count = EXIT_BLOCK_PTR_FOR_FN (cfun)->count;
5454 FOR_EACH_EDGE (e2, ei, EXIT_BLOCK_PTR_FOR_FN (cfun)->preds)
5455 if (e2 != e)
5457 e->count -= e2->count;
5458 exit_block->count -= e2->count;
5459 exit_block->frequency -= EDGE_FREQUENCY (e2);
5461 if (e->count < 0)
5462 e->count = 0;
5463 if (exit_block->count < 0)
5464 exit_block->count = 0;
5465 if (exit_block->frequency < 0)
5466 exit_block->frequency = 0;
5467 update_bb_for_insn (exit_block);
5470 /* Helper function for discover_nonconstant_array_refs.
5471 Look for ARRAY_REF nodes with non-constant indexes and mark them
5472 addressable. */
5474 static tree
5475 discover_nonconstant_array_refs_r (tree * tp, int *walk_subtrees,
5476 void *data ATTRIBUTE_UNUSED)
5478 tree t = *tp;
5480 if (IS_TYPE_OR_DECL_P (t))
5481 *walk_subtrees = 0;
5482 else if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
5484 while (((TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
5485 && is_gimple_min_invariant (TREE_OPERAND (t, 1))
5486 && (!TREE_OPERAND (t, 2)
5487 || is_gimple_min_invariant (TREE_OPERAND (t, 2))))
5488 || (TREE_CODE (t) == COMPONENT_REF
5489 && (!TREE_OPERAND (t,2)
5490 || is_gimple_min_invariant (TREE_OPERAND (t, 2))))
5491 || TREE_CODE (t) == BIT_FIELD_REF
5492 || TREE_CODE (t) == REALPART_EXPR
5493 || TREE_CODE (t) == IMAGPART_EXPR
5494 || TREE_CODE (t) == VIEW_CONVERT_EXPR
5495 || CONVERT_EXPR_P (t))
5496 t = TREE_OPERAND (t, 0);
5498 if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
5500 t = get_base_address (t);
5501 if (t && DECL_P (t)
5502 && DECL_MODE (t) != BLKmode)
5503 TREE_ADDRESSABLE (t) = 1;
5506 *walk_subtrees = 0;
5509 return NULL_TREE;
5512 /* RTL expansion is not able to compile array references with variable
5513 offsets for arrays stored in single register. Discover such
5514 expressions and mark variables as addressable to avoid this
5515 scenario. */
5517 static void
5518 discover_nonconstant_array_refs (void)
5520 basic_block bb;
5521 gimple_stmt_iterator gsi;
5523 FOR_EACH_BB_FN (bb, cfun)
5524 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
5526 gimple stmt = gsi_stmt (gsi);
5527 if (!is_gimple_debug (stmt))
5528 walk_gimple_op (stmt, discover_nonconstant_array_refs_r, NULL);
5532 /* This function sets crtl->args.internal_arg_pointer to a virtual
5533 register if DRAP is needed. Local register allocator will replace
5534 virtual_incoming_args_rtx with the virtual register. */
5536 static void
5537 expand_stack_alignment (void)
5539 rtx drap_rtx;
5540 unsigned int preferred_stack_boundary;
5542 if (! SUPPORTS_STACK_ALIGNMENT)
5543 return;
5545 if (cfun->calls_alloca
5546 || cfun->has_nonlocal_label
5547 || crtl->has_nonlocal_goto)
5548 crtl->need_drap = true;
5550 /* Call update_stack_boundary here again to update incoming stack
5551 boundary. It may set incoming stack alignment to a different
5552 value after RTL expansion. TARGET_FUNCTION_OK_FOR_SIBCALL may
5553 use the minimum incoming stack alignment to check if it is OK
5554 to perform sibcall optimization since sibcall optimization will
5555 only align the outgoing stack to incoming stack boundary. */
5556 if (targetm.calls.update_stack_boundary)
5557 targetm.calls.update_stack_boundary ();
5559 /* The incoming stack frame has to be aligned at least at
5560 parm_stack_boundary. */
5561 gcc_assert (crtl->parm_stack_boundary <= INCOMING_STACK_BOUNDARY);
5563 /* Update crtl->stack_alignment_estimated and use it later to align
5564 stack. We check PREFERRED_STACK_BOUNDARY if there may be non-call
5565 exceptions since callgraph doesn't collect incoming stack alignment
5566 in this case. */
5567 if (cfun->can_throw_non_call_exceptions
5568 && PREFERRED_STACK_BOUNDARY > crtl->preferred_stack_boundary)
5569 preferred_stack_boundary = PREFERRED_STACK_BOUNDARY;
5570 else
5571 preferred_stack_boundary = crtl->preferred_stack_boundary;
5572 if (preferred_stack_boundary > crtl->stack_alignment_estimated)
5573 crtl->stack_alignment_estimated = preferred_stack_boundary;
5574 if (preferred_stack_boundary > crtl->stack_alignment_needed)
5575 crtl->stack_alignment_needed = preferred_stack_boundary;
5577 gcc_assert (crtl->stack_alignment_needed
5578 <= crtl->stack_alignment_estimated);
5580 crtl->stack_realign_needed
5581 = INCOMING_STACK_BOUNDARY < crtl->stack_alignment_estimated;
5582 crtl->stack_realign_tried = crtl->stack_realign_needed;
5584 crtl->stack_realign_processed = true;
5586 /* Target has to redefine TARGET_GET_DRAP_RTX to support stack
5587 alignment. */
5588 gcc_assert (targetm.calls.get_drap_rtx != NULL);
5589 drap_rtx = targetm.calls.get_drap_rtx ();
5591 /* stack_realign_drap and drap_rtx must match. */
5592 gcc_assert ((stack_realign_drap != 0) == (drap_rtx != NULL));
5594 /* Do nothing if NULL is returned, which means DRAP is not needed. */
5595 if (NULL != drap_rtx)
5597 crtl->args.internal_arg_pointer = drap_rtx;
5599 /* Call fixup_tail_calls to clean up REG_EQUIV note if DRAP is
5600 needed. */
5601 fixup_tail_calls ();
5606 static void
5607 expand_main_function (void)
5609 #if (defined(INVOKE__main) \
5610 || (!defined(HAS_INIT_SECTION) \
5611 && !defined(INIT_SECTION_ASM_OP) \
5612 && !defined(INIT_ARRAY_SECTION_ASM_OP)))
5613 emit_library_call (init_one_libfunc (NAME__MAIN), LCT_NORMAL, VOIDmode, 0);
5614 #endif
5618 /* Expand code to initialize the stack_protect_guard. This is invoked at
5619 the beginning of a function to be protected. */
5621 #ifndef HAVE_stack_protect_set
5622 # define HAVE_stack_protect_set 0
5623 # define gen_stack_protect_set(x,y) (gcc_unreachable (), NULL_RTX)
5624 #endif
5626 static void
5627 stack_protect_prologue (void)
5629 tree guard_decl = targetm.stack_protect_guard ();
5630 rtx x, y;
5632 x = expand_normal (crtl->stack_protect_guard);
5633 y = expand_normal (guard_decl);
5635 /* Allow the target to copy from Y to X without leaking Y into a
5636 register. */
5637 if (HAVE_stack_protect_set)
5639 rtx insn = gen_stack_protect_set (x, y);
5640 if (insn)
5642 emit_insn (insn);
5643 return;
5647 /* Otherwise do a straight move. */
5648 emit_move_insn (x, y);
5651 /* Translate the intermediate representation contained in the CFG
5652 from GIMPLE trees to RTL.
5654 We do conversion per basic block and preserve/update the tree CFG.
5655 This implies we have to do some magic as the CFG can simultaneously
5656 consist of basic blocks containing RTL and GIMPLE trees. This can
5657 confuse the CFG hooks, so be careful to not manipulate CFG during
5658 the expansion. */
5660 namespace {
5662 const pass_data pass_data_expand =
5664 RTL_PASS, /* type */
5665 "expand", /* name */
5666 OPTGROUP_NONE, /* optinfo_flags */
5667 TV_EXPAND, /* tv_id */
5668 ( PROP_ssa | PROP_gimple_leh | PROP_cfg
5669 | PROP_gimple_lcx
5670 | PROP_gimple_lvec ), /* properties_required */
5671 PROP_rtl, /* properties_provided */
5672 ( PROP_ssa | PROP_trees ), /* properties_destroyed */
5673 0, /* todo_flags_start */
5674 0, /* todo_flags_finish */
5677 class pass_expand : public rtl_opt_pass
5679 public:
5680 pass_expand (gcc::context *ctxt)
5681 : rtl_opt_pass (pass_data_expand, ctxt)
5684 /* opt_pass methods: */
5685 virtual unsigned int execute (function *);
5687 }; // class pass_expand
5689 unsigned int
5690 pass_expand::execute (function *fun)
5692 basic_block bb, init_block;
5693 sbitmap blocks;
5694 edge_iterator ei;
5695 edge e;
5696 rtx_insn *var_seq, *var_ret_seq;
5697 unsigned i;
5699 timevar_push (TV_OUT_OF_SSA);
5700 rewrite_out_of_ssa (&SA);
5701 timevar_pop (TV_OUT_OF_SSA);
5702 SA.partition_to_pseudo = XCNEWVEC (rtx, SA.map->num_partitions);
5704 /* Make sure all values used by the optimization passes have sane
5705 defaults. */
5706 reg_renumber = 0;
5708 /* Some backends want to know that we are expanding to RTL. */
5709 currently_expanding_to_rtl = 1;
5710 /* Dominators are not kept up-to-date as we may create new basic-blocks. */
5711 free_dominance_info (CDI_DOMINATORS);
5713 rtl_profile_for_bb (ENTRY_BLOCK_PTR_FOR_FN (fun));
5715 if (chkp_function_instrumented_p (current_function_decl))
5716 chkp_reset_rtl_bounds ();
5718 insn_locations_init ();
5719 if (!DECL_IS_BUILTIN (current_function_decl))
5721 /* Eventually, all FEs should explicitly set function_start_locus. */
5722 if (LOCATION_LOCUS (fun->function_start_locus) == UNKNOWN_LOCATION)
5723 set_curr_insn_location
5724 (DECL_SOURCE_LOCATION (current_function_decl));
5725 else
5726 set_curr_insn_location (fun->function_start_locus);
5728 else
5729 set_curr_insn_location (UNKNOWN_LOCATION);
5730 prologue_location = curr_insn_location ();
5732 #ifdef INSN_SCHEDULING
5733 init_sched_attrs ();
5734 #endif
5736 /* Make sure first insn is a note even if we don't want linenums.
5737 This makes sure the first insn will never be deleted.
5738 Also, final expects a note to appear there. */
5739 emit_note (NOTE_INSN_DELETED);
5741 /* Mark arrays indexed with non-constant indices with TREE_ADDRESSABLE. */
5742 discover_nonconstant_array_refs ();
5744 targetm.expand_to_rtl_hook ();
5745 crtl->stack_alignment_needed = STACK_BOUNDARY;
5746 crtl->max_used_stack_slot_alignment = STACK_BOUNDARY;
5747 crtl->stack_alignment_estimated = 0;
5748 crtl->preferred_stack_boundary = STACK_BOUNDARY;
5749 fun->cfg->max_jumptable_ents = 0;
5751 /* Resovle the function section. Some targets, like ARM EABI rely on knowledge
5752 of the function section at exapnsion time to predict distance of calls. */
5753 resolve_unique_section (current_function_decl, 0, flag_function_sections);
5755 /* Expand the variables recorded during gimple lowering. */
5756 timevar_push (TV_VAR_EXPAND);
5757 start_sequence ();
5759 var_ret_seq = expand_used_vars ();
5761 var_seq = get_insns ();
5762 end_sequence ();
5763 timevar_pop (TV_VAR_EXPAND);
5765 /* Honor stack protection warnings. */
5766 if (warn_stack_protect)
5768 if (fun->calls_alloca)
5769 warning (OPT_Wstack_protector,
5770 "stack protector not protecting local variables: "
5771 "variable length buffer");
5772 if (has_short_buffer && !crtl->stack_protect_guard)
5773 warning (OPT_Wstack_protector,
5774 "stack protector not protecting function: "
5775 "all local arrays are less than %d bytes long",
5776 (int) PARAM_VALUE (PARAM_SSP_BUFFER_SIZE));
5779 /* Set up parameters and prepare for return, for the function. */
5780 expand_function_start (current_function_decl);
5782 /* If we emitted any instructions for setting up the variables,
5783 emit them before the FUNCTION_START note. */
5784 if (var_seq)
5786 emit_insn_before (var_seq, parm_birth_insn);
5788 /* In expand_function_end we'll insert the alloca save/restore
5789 before parm_birth_insn. We've just insertted an alloca call.
5790 Adjust the pointer to match. */
5791 parm_birth_insn = var_seq;
5794 /* Now that we also have the parameter RTXs, copy them over to our
5795 partitions. */
5796 for (i = 0; i < SA.map->num_partitions; i++)
5798 tree var = SSA_NAME_VAR (partition_to_var (SA.map, i));
5800 if (TREE_CODE (var) != VAR_DECL
5801 && !SA.partition_to_pseudo[i])
5802 SA.partition_to_pseudo[i] = DECL_RTL_IF_SET (var);
5803 gcc_assert (SA.partition_to_pseudo[i]);
5805 /* If this decl was marked as living in multiple places, reset
5806 this now to NULL. */
5807 if (DECL_RTL_IF_SET (var) == pc_rtx)
5808 SET_DECL_RTL (var, NULL);
5810 /* Some RTL parts really want to look at DECL_RTL(x) when x
5811 was a decl marked in REG_ATTR or MEM_ATTR. We could use
5812 SET_DECL_RTL here making this available, but that would mean
5813 to select one of the potentially many RTLs for one DECL. Instead
5814 of doing that we simply reset the MEM_EXPR of the RTL in question,
5815 then nobody can get at it and hence nobody can call DECL_RTL on it. */
5816 if (!DECL_RTL_SET_P (var))
5818 if (MEM_P (SA.partition_to_pseudo[i]))
5819 set_mem_expr (SA.partition_to_pseudo[i], NULL);
5823 /* If we have a class containing differently aligned pointers
5824 we need to merge those into the corresponding RTL pointer
5825 alignment. */
5826 for (i = 1; i < num_ssa_names; i++)
5828 tree name = ssa_name (i);
5829 int part;
5830 rtx r;
5832 if (!name
5833 /* We might have generated new SSA names in
5834 update_alias_info_with_stack_vars. They will have a NULL
5835 defining statements, and won't be part of the partitioning,
5836 so ignore those. */
5837 || !SSA_NAME_DEF_STMT (name))
5838 continue;
5839 part = var_to_partition (SA.map, name);
5840 if (part == NO_PARTITION)
5841 continue;
5843 /* Adjust all partition members to get the underlying decl of
5844 the representative which we might have created in expand_one_var. */
5845 if (SSA_NAME_VAR (name) == NULL_TREE)
5847 tree leader = partition_to_var (SA.map, part);
5848 gcc_assert (SSA_NAME_VAR (leader) != NULL_TREE);
5849 replace_ssa_name_symbol (name, SSA_NAME_VAR (leader));
5851 if (!POINTER_TYPE_P (TREE_TYPE (name)))
5852 continue;
5854 r = SA.partition_to_pseudo[part];
5855 if (REG_P (r))
5856 mark_reg_pointer (r, get_pointer_alignment (name));
5859 /* If this function is `main', emit a call to `__main'
5860 to run global initializers, etc. */
5861 if (DECL_NAME (current_function_decl)
5862 && MAIN_NAME_P (DECL_NAME (current_function_decl))
5863 && DECL_FILE_SCOPE_P (current_function_decl))
5864 expand_main_function ();
5866 /* Initialize the stack_protect_guard field. This must happen after the
5867 call to __main (if any) so that the external decl is initialized. */
5868 if (crtl->stack_protect_guard)
5869 stack_protect_prologue ();
5871 expand_phi_nodes (&SA);
5873 /* Register rtl specific functions for cfg. */
5874 rtl_register_cfg_hooks ();
5876 init_block = construct_init_block ();
5878 /* Clear EDGE_EXECUTABLE on the entry edge(s). It is cleaned from the
5879 remaining edges later. */
5880 FOR_EACH_EDGE (e, ei, ENTRY_BLOCK_PTR_FOR_FN (fun)->succs)
5881 e->flags &= ~EDGE_EXECUTABLE;
5883 lab_rtx_for_bb = new hash_map<basic_block, rtx_code_label *>;
5884 FOR_BB_BETWEEN (bb, init_block->next_bb, EXIT_BLOCK_PTR_FOR_FN (fun),
5885 next_bb)
5886 bb = expand_gimple_basic_block (bb, var_ret_seq != NULL_RTX);
5888 if (MAY_HAVE_DEBUG_INSNS)
5889 expand_debug_locations ();
5891 /* Free stuff we no longer need after GIMPLE optimizations. */
5892 free_dominance_info (CDI_DOMINATORS);
5893 free_dominance_info (CDI_POST_DOMINATORS);
5894 delete_tree_cfg_annotations ();
5896 timevar_push (TV_OUT_OF_SSA);
5897 finish_out_of_ssa (&SA);
5898 timevar_pop (TV_OUT_OF_SSA);
5900 timevar_push (TV_POST_EXPAND);
5901 /* We are no longer in SSA form. */
5902 fun->gimple_df->in_ssa_p = false;
5903 loops_state_clear (LOOP_CLOSED_SSA);
5905 /* Expansion is used by optimization passes too, set maybe_hot_insn_p
5906 conservatively to true until they are all profile aware. */
5907 delete lab_rtx_for_bb;
5908 free_histograms ();
5910 construct_exit_block ();
5911 insn_locations_finalize ();
5913 if (var_ret_seq)
5915 rtx_insn *after = return_label;
5916 rtx_insn *next = NEXT_INSN (after);
5917 if (next && NOTE_INSN_BASIC_BLOCK_P (next))
5918 after = next;
5919 emit_insn_after (var_ret_seq, after);
5922 /* Zap the tree EH table. */
5923 set_eh_throw_stmt_table (fun, NULL);
5925 /* We need JUMP_LABEL be set in order to redirect jumps, and hence
5926 split edges which edge insertions might do. */
5927 rebuild_jump_labels (get_insns ());
5929 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (fun),
5930 EXIT_BLOCK_PTR_FOR_FN (fun), next_bb)
5932 edge e;
5933 edge_iterator ei;
5934 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
5936 if (e->insns.r)
5938 rebuild_jump_labels_chain (e->insns.r);
5939 /* Put insns after parm birth, but before
5940 NOTE_INSNS_FUNCTION_BEG. */
5941 if (e->src == ENTRY_BLOCK_PTR_FOR_FN (fun)
5942 && single_succ_p (ENTRY_BLOCK_PTR_FOR_FN (fun)))
5944 rtx_insn *insns = e->insns.r;
5945 e->insns.r = NULL;
5946 if (NOTE_P (parm_birth_insn)
5947 && NOTE_KIND (parm_birth_insn) == NOTE_INSN_FUNCTION_BEG)
5948 emit_insn_before_noloc (insns, parm_birth_insn, e->dest);
5949 else
5950 emit_insn_after_noloc (insns, parm_birth_insn, e->dest);
5952 else
5953 commit_one_edge_insertion (e);
5955 else
5956 ei_next (&ei);
5960 /* We're done expanding trees to RTL. */
5961 currently_expanding_to_rtl = 0;
5963 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (fun)->next_bb,
5964 EXIT_BLOCK_PTR_FOR_FN (fun), next_bb)
5966 edge e;
5967 edge_iterator ei;
5968 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
5970 /* Clear EDGE_EXECUTABLE. This flag is never used in the backend. */
5971 e->flags &= ~EDGE_EXECUTABLE;
5973 /* At the moment not all abnormal edges match the RTL
5974 representation. It is safe to remove them here as
5975 find_many_sub_basic_blocks will rediscover them.
5976 In the future we should get this fixed properly. */
5977 if ((e->flags & EDGE_ABNORMAL)
5978 && !(e->flags & EDGE_SIBCALL))
5979 remove_edge (e);
5980 else
5981 ei_next (&ei);
5985 blocks = sbitmap_alloc (last_basic_block_for_fn (fun));
5986 bitmap_ones (blocks);
5987 find_many_sub_basic_blocks (blocks);
5988 sbitmap_free (blocks);
5989 purge_all_dead_edges ();
5991 expand_stack_alignment ();
5993 /* Fixup REG_EQUIV notes in the prologue if there are tailcalls in this
5994 function. */
5995 if (crtl->tail_call_emit)
5996 fixup_tail_calls ();
5998 /* After initial rtl generation, call back to finish generating
5999 exception support code. We need to do this before cleaning up
6000 the CFG as the code does not expect dead landing pads. */
6001 if (fun->eh->region_tree != NULL)
6002 finish_eh_generation ();
6004 /* Remove unreachable blocks, otherwise we cannot compute dominators
6005 which are needed for loop state verification. As a side-effect
6006 this also compacts blocks.
6007 ??? We cannot remove trivially dead insns here as for example
6008 the DRAP reg on i?86 is not magically live at this point.
6009 gcc.c-torture/execute/ipa-sra-2.c execution, -Os -m32 fails otherwise. */
6010 cleanup_cfg (CLEANUP_NO_INSN_DEL);
6012 #ifdef ENABLE_CHECKING
6013 verify_flow_info ();
6014 #endif
6016 /* Initialize pseudos allocated for hard registers. */
6017 emit_initial_value_sets ();
6019 /* And finally unshare all RTL. */
6020 unshare_all_rtl ();
6022 /* There's no need to defer outputting this function any more; we
6023 know we want to output it. */
6024 DECL_DEFER_OUTPUT (current_function_decl) = 0;
6026 /* Now that we're done expanding trees to RTL, we shouldn't have any
6027 more CONCATs anywhere. */
6028 generating_concat_p = 0;
6030 if (dump_file)
6032 fprintf (dump_file,
6033 "\n\n;;\n;; Full RTL generated for this function:\n;;\n");
6034 /* And the pass manager will dump RTL for us. */
6037 /* If we're emitting a nested function, make sure its parent gets
6038 emitted as well. Doing otherwise confuses debug info. */
6040 tree parent;
6041 for (parent = DECL_CONTEXT (current_function_decl);
6042 parent != NULL_TREE;
6043 parent = get_containing_scope (parent))
6044 if (TREE_CODE (parent) == FUNCTION_DECL)
6045 TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (parent)) = 1;
6048 /* We are now committed to emitting code for this function. Do any
6049 preparation, such as emitting abstract debug info for the inline
6050 before it gets mangled by optimization. */
6051 if (cgraph_function_possibly_inlined_p (current_function_decl))
6052 (*debug_hooks->outlining_inline_function) (current_function_decl);
6054 TREE_ASM_WRITTEN (current_function_decl) = 1;
6056 /* After expanding, the return labels are no longer needed. */
6057 return_label = NULL;
6058 naked_return_label = NULL;
6060 /* After expanding, the tm_restart map is no longer needed. */
6061 if (fun->gimple_df->tm_restart)
6063 htab_delete (fun->gimple_df->tm_restart);
6064 fun->gimple_df->tm_restart = NULL;
6067 /* Tag the blocks with a depth number so that change_scope can find
6068 the common parent easily. */
6069 set_block_levels (DECL_INITIAL (fun->decl), 0);
6070 default_rtl_profile ();
6072 timevar_pop (TV_POST_EXPAND);
6074 return 0;
6077 } // anon namespace
6079 rtl_opt_pass *
6080 make_pass_expand (gcc::context *ctxt)
6082 return new pass_expand (ctxt);