OpenACC deviceptr clause: Remove bogus assertion.
[official-gcc.git] / gcc / cfgexpand.c
blob9bd61359e80fb4f878c2ba0c7c4f82534583fe5c
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 "expr.h"
48 #include "langhooks.h"
49 #include "bitmap.h"
50 #include "tree-ssa-alias.h"
51 #include "internal-fn.h"
52 #include "tree-eh.h"
53 #include "gimple-expr.h"
54 #include "is-a.h"
55 #include "gimple.h"
56 #include "gimple-iterator.h"
57 #include "gimple-walk.h"
58 #include "gimple-ssa.h"
59 #include "hash-map.h"
60 #include "plugin-api.h"
61 #include "ipa-ref.h"
62 #include "cgraph.h"
63 #include "tree-cfg.h"
64 #include "tree-phinodes.h"
65 #include "ssa-iterators.h"
66 #include "tree-ssanames.h"
67 #include "tree-dfa.h"
68 #include "tree-ssa.h"
69 #include "tree-pass.h"
70 #include "except.h"
71 #include "flags.h"
72 #include "diagnostic.h"
73 #include "gimple-pretty-print.h"
74 #include "toplev.h"
75 #include "debug.h"
76 #include "params.h"
77 #include "tree-inline.h"
78 #include "value-prof.h"
79 #include "target.h"
80 #include "tree-ssa-live.h"
81 #include "tree-outof-ssa.h"
82 #include "sbitmap.h"
83 #include "cfgloop.h"
84 #include "regs.h" /* For reg_renumber. */
85 #include "insn-attr.h" /* For INSN_SCHEDULING. */
86 #include "asan.h"
87 #include "tree-ssa-address.h"
88 #include "recog.h"
89 #include "output.h"
90 #include "builtins.h"
92 /* Some systems use __main in a way incompatible with its use in gcc, in these
93 cases use the macros NAME__MAIN to give a quoted symbol and SYMBOL__MAIN to
94 give the same symbol without quotes for an alternative entry point. You
95 must define both, or neither. */
96 #ifndef NAME__MAIN
97 #define NAME__MAIN "__main"
98 #endif
100 /* This variable holds information helping the rewriting of SSA trees
101 into RTL. */
102 struct ssaexpand SA;
104 /* This variable holds the currently expanded gimple statement for purposes
105 of comminucating the profile info to the builtin expanders. */
106 gimple currently_expanding_gimple_stmt;
108 static rtx expand_debug_expr (tree);
110 /* Return an expression tree corresponding to the RHS of GIMPLE
111 statement STMT. */
113 tree
114 gimple_assign_rhs_to_tree (gimple stmt)
116 tree t;
117 enum gimple_rhs_class grhs_class;
119 grhs_class = get_gimple_rhs_class (gimple_expr_code (stmt));
121 if (grhs_class == GIMPLE_TERNARY_RHS)
122 t = build3 (gimple_assign_rhs_code (stmt),
123 TREE_TYPE (gimple_assign_lhs (stmt)),
124 gimple_assign_rhs1 (stmt),
125 gimple_assign_rhs2 (stmt),
126 gimple_assign_rhs3 (stmt));
127 else if (grhs_class == GIMPLE_BINARY_RHS)
128 t = build2 (gimple_assign_rhs_code (stmt),
129 TREE_TYPE (gimple_assign_lhs (stmt)),
130 gimple_assign_rhs1 (stmt),
131 gimple_assign_rhs2 (stmt));
132 else if (grhs_class == GIMPLE_UNARY_RHS)
133 t = build1 (gimple_assign_rhs_code (stmt),
134 TREE_TYPE (gimple_assign_lhs (stmt)),
135 gimple_assign_rhs1 (stmt));
136 else if (grhs_class == GIMPLE_SINGLE_RHS)
138 t = gimple_assign_rhs1 (stmt);
139 /* Avoid modifying this tree in place below. */
140 if ((gimple_has_location (stmt) && CAN_HAVE_LOCATION_P (t)
141 && gimple_location (stmt) != EXPR_LOCATION (t))
142 || (gimple_block (stmt)
143 && currently_expanding_to_rtl
144 && EXPR_P (t)))
145 t = copy_node (t);
147 else
148 gcc_unreachable ();
150 if (gimple_has_location (stmt) && CAN_HAVE_LOCATION_P (t))
151 SET_EXPR_LOCATION (t, gimple_location (stmt));
153 return t;
157 #ifndef STACK_ALIGNMENT_NEEDED
158 #define STACK_ALIGNMENT_NEEDED 1
159 #endif
161 #define SSAVAR(x) (TREE_CODE (x) == SSA_NAME ? SSA_NAME_VAR (x) : x)
163 /* Associate declaration T with storage space X. If T is no
164 SSA name this is exactly SET_DECL_RTL, otherwise make the
165 partition of T associated with X. */
166 static inline void
167 set_rtl (tree t, rtx x)
169 if (TREE_CODE (t) == SSA_NAME)
171 SA.partition_to_pseudo[var_to_partition (SA.map, t)] = x;
172 if (x && !MEM_P (x))
173 set_reg_attrs_for_decl_rtl (SSA_NAME_VAR (t), x);
174 /* For the benefit of debug information at -O0 (where vartracking
175 doesn't run) record the place also in the base DECL if it's
176 a normal variable (not a parameter). */
177 if (x && x != pc_rtx && TREE_CODE (SSA_NAME_VAR (t)) == VAR_DECL)
179 tree var = SSA_NAME_VAR (t);
180 /* If we don't yet have something recorded, just record it now. */
181 if (!DECL_RTL_SET_P (var))
182 SET_DECL_RTL (var, x);
183 /* If we have it set already to "multiple places" don't
184 change this. */
185 else if (DECL_RTL (var) == pc_rtx)
187 /* If we have something recorded and it's not the same place
188 as we want to record now, we have multiple partitions for the
189 same base variable, with different places. We can't just
190 randomly chose one, hence we have to say that we don't know.
191 This only happens with optimization, and there var-tracking
192 will figure out the right thing. */
193 else if (DECL_RTL (var) != x)
194 SET_DECL_RTL (var, pc_rtx);
197 else
198 SET_DECL_RTL (t, x);
201 /* This structure holds data relevant to one variable that will be
202 placed in a stack slot. */
203 struct stack_var
205 /* The Variable. */
206 tree decl;
208 /* Initially, the size of the variable. Later, the size of the partition,
209 if this variable becomes it's partition's representative. */
210 HOST_WIDE_INT size;
212 /* The *byte* alignment required for this variable. Or as, with the
213 size, the alignment for this partition. */
214 unsigned int alignb;
216 /* The partition representative. */
217 size_t representative;
219 /* The next stack variable in the partition, or EOC. */
220 size_t next;
222 /* The numbers of conflicting stack variables. */
223 bitmap conflicts;
226 #define EOC ((size_t)-1)
228 /* We have an array of such objects while deciding allocation. */
229 static struct stack_var *stack_vars;
230 static size_t stack_vars_alloc;
231 static size_t stack_vars_num;
232 static hash_map<tree, size_t> *decl_to_stack_part;
234 /* Conflict bitmaps go on this obstack. This allows us to destroy
235 all of them in one big sweep. */
236 static bitmap_obstack stack_var_bitmap_obstack;
238 /* An array of indices such that stack_vars[stack_vars_sorted[i]].size
239 is non-decreasing. */
240 static size_t *stack_vars_sorted;
242 /* The phase of the stack frame. This is the known misalignment of
243 virtual_stack_vars_rtx from PREFERRED_STACK_BOUNDARY. That is,
244 (frame_offset+frame_phase) % PREFERRED_STACK_BOUNDARY == 0. */
245 static int frame_phase;
247 /* Used during expand_used_vars to remember if we saw any decls for
248 which we'd like to enable stack smashing protection. */
249 static bool has_protected_decls;
251 /* Used during expand_used_vars. Remember if we say a character buffer
252 smaller than our cutoff threshold. Used for -Wstack-protector. */
253 static bool has_short_buffer;
255 /* Compute the byte alignment to use for DECL. Ignore alignment
256 we can't do with expected alignment of the stack boundary. */
258 static unsigned int
259 align_local_variable (tree decl)
261 unsigned int align = LOCAL_DECL_ALIGNMENT (decl);
262 DECL_ALIGN (decl) = align;
263 return align / BITS_PER_UNIT;
266 /* Allocate SIZE bytes at byte alignment ALIGN from the stack frame.
267 Return the frame offset. */
269 static HOST_WIDE_INT
270 alloc_stack_frame_space (HOST_WIDE_INT size, unsigned HOST_WIDE_INT align)
272 HOST_WIDE_INT offset, new_frame_offset;
274 new_frame_offset = frame_offset;
275 if (FRAME_GROWS_DOWNWARD)
277 new_frame_offset -= size + frame_phase;
278 new_frame_offset &= -align;
279 new_frame_offset += frame_phase;
280 offset = new_frame_offset;
282 else
284 new_frame_offset -= frame_phase;
285 new_frame_offset += align - 1;
286 new_frame_offset &= -align;
287 new_frame_offset += frame_phase;
288 offset = new_frame_offset;
289 new_frame_offset += size;
291 frame_offset = new_frame_offset;
293 if (frame_offset_overflow (frame_offset, cfun->decl))
294 frame_offset = offset = 0;
296 return offset;
299 /* Accumulate DECL into STACK_VARS. */
301 static void
302 add_stack_var (tree decl)
304 struct stack_var *v;
306 if (stack_vars_num >= stack_vars_alloc)
308 if (stack_vars_alloc)
309 stack_vars_alloc = stack_vars_alloc * 3 / 2;
310 else
311 stack_vars_alloc = 32;
312 stack_vars
313 = XRESIZEVEC (struct stack_var, stack_vars, stack_vars_alloc);
315 if (!decl_to_stack_part)
316 decl_to_stack_part = new hash_map<tree, size_t>;
318 v = &stack_vars[stack_vars_num];
319 decl_to_stack_part->put (decl, stack_vars_num);
321 v->decl = decl;
322 v->size = tree_to_uhwi (DECL_SIZE_UNIT (SSAVAR (decl)));
323 /* Ensure that all variables have size, so that &a != &b for any two
324 variables that are simultaneously live. */
325 if (v->size == 0)
326 v->size = 1;
327 v->alignb = align_local_variable (SSAVAR (decl));
328 /* An alignment of zero can mightily confuse us later. */
329 gcc_assert (v->alignb != 0);
331 /* All variables are initially in their own partition. */
332 v->representative = stack_vars_num;
333 v->next = EOC;
335 /* All variables initially conflict with no other. */
336 v->conflicts = NULL;
338 /* Ensure that this decl doesn't get put onto the list twice. */
339 set_rtl (decl, pc_rtx);
341 stack_vars_num++;
344 /* Make the decls associated with luid's X and Y conflict. */
346 static void
347 add_stack_var_conflict (size_t x, size_t y)
349 struct stack_var *a = &stack_vars[x];
350 struct stack_var *b = &stack_vars[y];
351 if (!a->conflicts)
352 a->conflicts = BITMAP_ALLOC (&stack_var_bitmap_obstack);
353 if (!b->conflicts)
354 b->conflicts = BITMAP_ALLOC (&stack_var_bitmap_obstack);
355 bitmap_set_bit (a->conflicts, y);
356 bitmap_set_bit (b->conflicts, x);
359 /* Check whether the decls associated with luid's X and Y conflict. */
361 static bool
362 stack_var_conflict_p (size_t x, size_t y)
364 struct stack_var *a = &stack_vars[x];
365 struct stack_var *b = &stack_vars[y];
366 if (x == y)
367 return false;
368 /* Partitions containing an SSA name result from gimple registers
369 with things like unsupported modes. They are top-level and
370 hence conflict with everything else. */
371 if (TREE_CODE (a->decl) == SSA_NAME || TREE_CODE (b->decl) == SSA_NAME)
372 return true;
374 if (!a->conflicts || !b->conflicts)
375 return false;
376 return bitmap_bit_p (a->conflicts, y);
379 /* Callback for walk_stmt_ops. If OP is a decl touched by add_stack_var
380 enter its partition number into bitmap DATA. */
382 static bool
383 visit_op (gimple, tree op, tree, void *data)
385 bitmap active = (bitmap)data;
386 op = get_base_address (op);
387 if (op
388 && DECL_P (op)
389 && DECL_RTL_IF_SET (op) == pc_rtx)
391 size_t *v = decl_to_stack_part->get (op);
392 if (v)
393 bitmap_set_bit (active, *v);
395 return false;
398 /* Callback for walk_stmt_ops. If OP is a decl touched by add_stack_var
399 record conflicts between it and all currently active other partitions
400 from bitmap DATA. */
402 static bool
403 visit_conflict (gimple, tree op, tree, void *data)
405 bitmap active = (bitmap)data;
406 op = get_base_address (op);
407 if (op
408 && DECL_P (op)
409 && DECL_RTL_IF_SET (op) == pc_rtx)
411 size_t *v = decl_to_stack_part->get (op);
412 if (v && bitmap_set_bit (active, *v))
414 size_t num = *v;
415 bitmap_iterator bi;
416 unsigned i;
417 gcc_assert (num < stack_vars_num);
418 EXECUTE_IF_SET_IN_BITMAP (active, 0, i, bi)
419 add_stack_var_conflict (num, i);
422 return false;
425 /* Helper routine for add_scope_conflicts, calculating the active partitions
426 at the end of BB, leaving the result in WORK. We're called to generate
427 conflicts when FOR_CONFLICT is true, otherwise we're just tracking
428 liveness. */
430 static void
431 add_scope_conflicts_1 (basic_block bb, bitmap work, bool for_conflict)
433 edge e;
434 edge_iterator ei;
435 gimple_stmt_iterator gsi;
436 walk_stmt_load_store_addr_fn visit;
438 bitmap_clear (work);
439 FOR_EACH_EDGE (e, ei, bb->preds)
440 bitmap_ior_into (work, (bitmap)e->src->aux);
442 visit = visit_op;
444 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
446 gimple stmt = gsi_stmt (gsi);
447 walk_stmt_load_store_addr_ops (stmt, work, NULL, NULL, visit);
449 for (gsi = gsi_after_labels (bb); !gsi_end_p (gsi); gsi_next (&gsi))
451 gimple stmt = gsi_stmt (gsi);
453 if (gimple_clobber_p (stmt))
455 tree lhs = gimple_assign_lhs (stmt);
456 size_t *v;
457 /* Nested function lowering might introduce LHSs
458 that are COMPONENT_REFs. */
459 if (TREE_CODE (lhs) != VAR_DECL)
460 continue;
461 if (DECL_RTL_IF_SET (lhs) == pc_rtx
462 && (v = decl_to_stack_part->get (lhs)))
463 bitmap_clear_bit (work, *v);
465 else if (!is_gimple_debug (stmt))
467 if (for_conflict
468 && visit == visit_op)
470 /* If this is the first real instruction in this BB we need
471 to add conflicts for everything live at this point now.
472 Unlike classical liveness for named objects we can't
473 rely on seeing a def/use of the names we're interested in.
474 There might merely be indirect loads/stores. We'd not add any
475 conflicts for such partitions. */
476 bitmap_iterator bi;
477 unsigned i;
478 EXECUTE_IF_SET_IN_BITMAP (work, 0, i, bi)
480 struct stack_var *a = &stack_vars[i];
481 if (!a->conflicts)
482 a->conflicts = BITMAP_ALLOC (&stack_var_bitmap_obstack);
483 bitmap_ior_into (a->conflicts, work);
485 visit = visit_conflict;
487 walk_stmt_load_store_addr_ops (stmt, work, visit, visit, visit);
492 /* Generate stack partition conflicts between all partitions that are
493 simultaneously live. */
495 static void
496 add_scope_conflicts (void)
498 basic_block bb;
499 bool changed;
500 bitmap work = BITMAP_ALLOC (NULL);
501 int *rpo;
502 int n_bbs;
504 /* We approximate the live range of a stack variable by taking the first
505 mention of its name as starting point(s), and by the end-of-scope
506 death clobber added by gimplify as ending point(s) of the range.
507 This overapproximates in the case we for instance moved an address-taken
508 operation upward, without also moving a dereference to it upwards.
509 But it's conservatively correct as a variable never can hold values
510 before its name is mentioned at least once.
512 We then do a mostly classical bitmap liveness algorithm. */
514 FOR_ALL_BB_FN (bb, cfun)
515 bb->aux = BITMAP_ALLOC (&stack_var_bitmap_obstack);
517 rpo = XNEWVEC (int, last_basic_block_for_fn (cfun));
518 n_bbs = pre_and_rev_post_order_compute (NULL, rpo, false);
520 changed = true;
521 while (changed)
523 int i;
524 changed = false;
525 for (i = 0; i < n_bbs; i++)
527 bitmap active;
528 bb = BASIC_BLOCK_FOR_FN (cfun, rpo[i]);
529 active = (bitmap)bb->aux;
530 add_scope_conflicts_1 (bb, work, false);
531 if (bitmap_ior_into (active, work))
532 changed = true;
536 FOR_EACH_BB_FN (bb, cfun)
537 add_scope_conflicts_1 (bb, work, true);
539 free (rpo);
540 BITMAP_FREE (work);
541 FOR_ALL_BB_FN (bb, cfun)
542 BITMAP_FREE (bb->aux);
545 /* A subroutine of partition_stack_vars. A comparison function for qsort,
546 sorting an array of indices by the properties of the object. */
548 static int
549 stack_var_cmp (const void *a, const void *b)
551 size_t ia = *(const size_t *)a;
552 size_t ib = *(const size_t *)b;
553 unsigned int aligna = stack_vars[ia].alignb;
554 unsigned int alignb = stack_vars[ib].alignb;
555 HOST_WIDE_INT sizea = stack_vars[ia].size;
556 HOST_WIDE_INT sizeb = stack_vars[ib].size;
557 tree decla = stack_vars[ia].decl;
558 tree declb = stack_vars[ib].decl;
559 bool largea, largeb;
560 unsigned int uida, uidb;
562 /* Primary compare on "large" alignment. Large comes first. */
563 largea = (aligna * BITS_PER_UNIT > MAX_SUPPORTED_STACK_ALIGNMENT);
564 largeb = (alignb * BITS_PER_UNIT > MAX_SUPPORTED_STACK_ALIGNMENT);
565 if (largea != largeb)
566 return (int)largeb - (int)largea;
568 /* Secondary compare on size, decreasing */
569 if (sizea > sizeb)
570 return -1;
571 if (sizea < sizeb)
572 return 1;
574 /* Tertiary compare on true alignment, decreasing. */
575 if (aligna < alignb)
576 return -1;
577 if (aligna > alignb)
578 return 1;
580 /* Final compare on ID for sort stability, increasing.
581 Two SSA names are compared by their version, SSA names come before
582 non-SSA names, and two normal decls are compared by their DECL_UID. */
583 if (TREE_CODE (decla) == SSA_NAME)
585 if (TREE_CODE (declb) == SSA_NAME)
586 uida = SSA_NAME_VERSION (decla), uidb = SSA_NAME_VERSION (declb);
587 else
588 return -1;
590 else if (TREE_CODE (declb) == SSA_NAME)
591 return 1;
592 else
593 uida = DECL_UID (decla), uidb = DECL_UID (declb);
594 if (uida < uidb)
595 return 1;
596 if (uida > uidb)
597 return -1;
598 return 0;
601 struct part_traits : default_hashmap_traits
603 template<typename T>
604 static bool
605 is_deleted (T &e)
606 { return e.m_value == reinterpret_cast<void *> (1); }
608 template<typename T> static bool is_empty (T &e) { return e.m_value == NULL; }
609 template<typename T>
610 static void
611 mark_deleted (T &e)
612 { e.m_value = reinterpret_cast<T> (1); }
614 template<typename T>
615 static void
616 mark_empty (T &e)
617 { e.m_value = NULL; }
620 typedef hash_map<size_t, bitmap, part_traits> part_hashmap;
622 /* If the points-to solution *PI points to variables that are in a partition
623 together with other variables add all partition members to the pointed-to
624 variables bitmap. */
626 static void
627 add_partitioned_vars_to_ptset (struct pt_solution *pt,
628 part_hashmap *decls_to_partitions,
629 hash_set<bitmap> *visited, bitmap temp)
631 bitmap_iterator bi;
632 unsigned i;
633 bitmap *part;
635 if (pt->anything
636 || pt->vars == NULL
637 /* The pointed-to vars bitmap is shared, it is enough to
638 visit it once. */
639 || visited->add (pt->vars))
640 return;
642 bitmap_clear (temp);
644 /* By using a temporary bitmap to store all members of the partitions
645 we have to add we make sure to visit each of the partitions only
646 once. */
647 EXECUTE_IF_SET_IN_BITMAP (pt->vars, 0, i, bi)
648 if ((!temp
649 || !bitmap_bit_p (temp, i))
650 && (part = decls_to_partitions->get (i)))
651 bitmap_ior_into (temp, *part);
652 if (!bitmap_empty_p (temp))
653 bitmap_ior_into (pt->vars, temp);
656 /* Update points-to sets based on partition info, so we can use them on RTL.
657 The bitmaps representing stack partitions will be saved until expand,
658 where partitioned decls used as bases in memory expressions will be
659 rewritten. */
661 static void
662 update_alias_info_with_stack_vars (void)
664 part_hashmap *decls_to_partitions = NULL;
665 size_t i, j;
666 tree var = NULL_TREE;
668 for (i = 0; i < stack_vars_num; i++)
670 bitmap part = NULL;
671 tree name;
672 struct ptr_info_def *pi;
674 /* Not interested in partitions with single variable. */
675 if (stack_vars[i].representative != i
676 || stack_vars[i].next == EOC)
677 continue;
679 if (!decls_to_partitions)
681 decls_to_partitions = new part_hashmap;
682 cfun->gimple_df->decls_to_pointers = new hash_map<tree, tree>;
685 /* Create an SSA_NAME that points to the partition for use
686 as base during alias-oracle queries on RTL for bases that
687 have been partitioned. */
688 if (var == NULL_TREE)
689 var = create_tmp_var (ptr_type_node, NULL);
690 name = make_ssa_name (var, NULL);
692 /* Create bitmaps representing partitions. They will be used for
693 points-to sets later, so use GGC alloc. */
694 part = BITMAP_GGC_ALLOC ();
695 for (j = i; j != EOC; j = stack_vars[j].next)
697 tree decl = stack_vars[j].decl;
698 unsigned int uid = DECL_PT_UID (decl);
699 bitmap_set_bit (part, uid);
700 decls_to_partitions->put (uid, part);
701 cfun->gimple_df->decls_to_pointers->put (decl, name);
702 if (TREE_ADDRESSABLE (decl))
703 TREE_ADDRESSABLE (name) = 1;
706 /* Make the SSA name point to all partition members. */
707 pi = get_ptr_info (name);
708 pt_solution_set (&pi->pt, part, false);
711 /* Make all points-to sets that contain one member of a partition
712 contain all members of the partition. */
713 if (decls_to_partitions)
715 unsigned i;
716 hash_set<bitmap> visited;
717 bitmap temp = BITMAP_ALLOC (&stack_var_bitmap_obstack);
719 for (i = 1; i < num_ssa_names; i++)
721 tree name = ssa_name (i);
722 struct ptr_info_def *pi;
724 if (name
725 && POINTER_TYPE_P (TREE_TYPE (name))
726 && ((pi = SSA_NAME_PTR_INFO (name)) != NULL))
727 add_partitioned_vars_to_ptset (&pi->pt, decls_to_partitions,
728 &visited, temp);
731 add_partitioned_vars_to_ptset (&cfun->gimple_df->escaped,
732 decls_to_partitions, &visited, temp);
734 delete decls_to_partitions;
735 BITMAP_FREE (temp);
739 /* A subroutine of partition_stack_vars. The UNION portion of a UNION/FIND
740 partitioning algorithm. Partitions A and B are known to be non-conflicting.
741 Merge them into a single partition A. */
743 static void
744 union_stack_vars (size_t a, size_t b)
746 struct stack_var *vb = &stack_vars[b];
747 bitmap_iterator bi;
748 unsigned u;
750 gcc_assert (stack_vars[b].next == EOC);
751 /* Add B to A's partition. */
752 stack_vars[b].next = stack_vars[a].next;
753 stack_vars[b].representative = a;
754 stack_vars[a].next = b;
756 /* Update the required alignment of partition A to account for B. */
757 if (stack_vars[a].alignb < stack_vars[b].alignb)
758 stack_vars[a].alignb = stack_vars[b].alignb;
760 /* Update the interference graph and merge the conflicts. */
761 if (vb->conflicts)
763 EXECUTE_IF_SET_IN_BITMAP (vb->conflicts, 0, u, bi)
764 add_stack_var_conflict (a, stack_vars[u].representative);
765 BITMAP_FREE (vb->conflicts);
769 /* A subroutine of expand_used_vars. Binpack the variables into
770 partitions constrained by the interference graph. The overall
771 algorithm used is as follows:
773 Sort the objects by size in descending order.
774 For each object A {
775 S = size(A)
776 O = 0
777 loop {
778 Look for the largest non-conflicting object B with size <= S.
779 UNION (A, B)
784 static void
785 partition_stack_vars (void)
787 size_t si, sj, n = stack_vars_num;
789 stack_vars_sorted = XNEWVEC (size_t, stack_vars_num);
790 for (si = 0; si < n; ++si)
791 stack_vars_sorted[si] = si;
793 if (n == 1)
794 return;
796 qsort (stack_vars_sorted, n, sizeof (size_t), stack_var_cmp);
798 for (si = 0; si < n; ++si)
800 size_t i = stack_vars_sorted[si];
801 unsigned int ialign = stack_vars[i].alignb;
802 HOST_WIDE_INT isize = stack_vars[i].size;
804 /* Ignore objects that aren't partition representatives. If we
805 see a var that is not a partition representative, it must
806 have been merged earlier. */
807 if (stack_vars[i].representative != i)
808 continue;
810 for (sj = si + 1; sj < n; ++sj)
812 size_t j = stack_vars_sorted[sj];
813 unsigned int jalign = stack_vars[j].alignb;
814 HOST_WIDE_INT jsize = stack_vars[j].size;
816 /* Ignore objects that aren't partition representatives. */
817 if (stack_vars[j].representative != j)
818 continue;
820 /* Do not mix objects of "small" (supported) alignment
821 and "large" (unsupported) alignment. */
822 if ((ialign * BITS_PER_UNIT <= MAX_SUPPORTED_STACK_ALIGNMENT)
823 != (jalign * BITS_PER_UNIT <= MAX_SUPPORTED_STACK_ALIGNMENT))
824 break;
826 /* For Address Sanitizer do not mix objects with different
827 sizes, as the shorter vars wouldn't be adequately protected.
828 Don't do that for "large" (unsupported) alignment objects,
829 those aren't protected anyway. */
830 if ((flag_sanitize & SANITIZE_ADDRESS) && ASAN_STACK && isize != jsize
831 && ialign * BITS_PER_UNIT <= MAX_SUPPORTED_STACK_ALIGNMENT)
832 break;
834 /* Ignore conflicting objects. */
835 if (stack_var_conflict_p (i, j))
836 continue;
838 /* UNION the objects, placing J at OFFSET. */
839 union_stack_vars (i, j);
843 update_alias_info_with_stack_vars ();
846 /* A debugging aid for expand_used_vars. Dump the generated partitions. */
848 static void
849 dump_stack_var_partition (void)
851 size_t si, i, j, n = stack_vars_num;
853 for (si = 0; si < n; ++si)
855 i = stack_vars_sorted[si];
857 /* Skip variables that aren't partition representatives, for now. */
858 if (stack_vars[i].representative != i)
859 continue;
861 fprintf (dump_file, "Partition %lu: size " HOST_WIDE_INT_PRINT_DEC
862 " align %u\n", (unsigned long) i, stack_vars[i].size,
863 stack_vars[i].alignb);
865 for (j = i; j != EOC; j = stack_vars[j].next)
867 fputc ('\t', dump_file);
868 print_generic_expr (dump_file, stack_vars[j].decl, dump_flags);
870 fputc ('\n', dump_file);
874 /* Assign rtl to DECL at BASE + OFFSET. */
876 static void
877 expand_one_stack_var_at (tree decl, rtx base, unsigned base_align,
878 HOST_WIDE_INT offset)
880 unsigned align;
881 rtx x;
883 /* If this fails, we've overflowed the stack frame. Error nicely? */
884 gcc_assert (offset == trunc_int_for_mode (offset, Pmode));
886 x = plus_constant (Pmode, base, offset);
887 x = gen_rtx_MEM (DECL_MODE (SSAVAR (decl)), x);
889 if (TREE_CODE (decl) != SSA_NAME)
891 /* Set alignment we actually gave this decl if it isn't an SSA name.
892 If it is we generate stack slots only accidentally so it isn't as
893 important, we'll simply use the alignment that is already set. */
894 if (base == virtual_stack_vars_rtx)
895 offset -= frame_phase;
896 align = offset & -offset;
897 align *= BITS_PER_UNIT;
898 if (align == 0 || align > base_align)
899 align = base_align;
901 /* One would think that we could assert that we're not decreasing
902 alignment here, but (at least) the i386 port does exactly this
903 via the MINIMUM_ALIGNMENT hook. */
905 DECL_ALIGN (decl) = align;
906 DECL_USER_ALIGN (decl) = 0;
909 set_mem_attributes (x, SSAVAR (decl), true);
910 set_rtl (decl, x);
913 struct stack_vars_data
915 /* Vector of offset pairs, always end of some padding followed
916 by start of the padding that needs Address Sanitizer protection.
917 The vector is in reversed, highest offset pairs come first. */
918 vec<HOST_WIDE_INT> asan_vec;
920 /* Vector of partition representative decls in between the paddings. */
921 vec<tree> asan_decl_vec;
923 /* Base pseudo register for Address Sanitizer protected automatic vars. */
924 rtx asan_base;
926 /* Alignment needed for the Address Sanitizer protected automatic vars. */
927 unsigned int asan_alignb;
930 /* A subroutine of expand_used_vars. Give each partition representative
931 a unique location within the stack frame. Update each partition member
932 with that location. */
934 static void
935 expand_stack_vars (bool (*pred) (size_t), struct stack_vars_data *data)
937 size_t si, i, j, n = stack_vars_num;
938 HOST_WIDE_INT large_size = 0, large_alloc = 0;
939 rtx large_base = NULL;
940 unsigned large_align = 0;
941 tree decl;
943 /* Determine if there are any variables requiring "large" alignment.
944 Since these are dynamically allocated, we only process these if
945 no predicate involved. */
946 large_align = stack_vars[stack_vars_sorted[0]].alignb * BITS_PER_UNIT;
947 if (pred == NULL && large_align > MAX_SUPPORTED_STACK_ALIGNMENT)
949 /* Find the total size of these variables. */
950 for (si = 0; si < n; ++si)
952 unsigned alignb;
954 i = stack_vars_sorted[si];
955 alignb = stack_vars[i].alignb;
957 /* Stop when we get to the first decl with "small" alignment. */
958 if (alignb * BITS_PER_UNIT <= MAX_SUPPORTED_STACK_ALIGNMENT)
959 break;
961 /* Skip variables that aren't partition representatives. */
962 if (stack_vars[i].representative != i)
963 continue;
965 /* Skip variables that have already had rtl assigned. See also
966 add_stack_var where we perpetrate this pc_rtx hack. */
967 decl = stack_vars[i].decl;
968 if ((TREE_CODE (decl) == SSA_NAME
969 ? SA.partition_to_pseudo[var_to_partition (SA.map, decl)]
970 : DECL_RTL (decl)) != pc_rtx)
971 continue;
973 large_size += alignb - 1;
974 large_size &= -(HOST_WIDE_INT)alignb;
975 large_size += stack_vars[i].size;
978 /* If there were any, allocate space. */
979 if (large_size > 0)
980 large_base = allocate_dynamic_stack_space (GEN_INT (large_size), 0,
981 large_align, true);
984 for (si = 0; si < n; ++si)
986 rtx base;
987 unsigned base_align, alignb;
988 HOST_WIDE_INT offset;
990 i = stack_vars_sorted[si];
992 /* Skip variables that aren't partition representatives, for now. */
993 if (stack_vars[i].representative != i)
994 continue;
996 /* Skip variables that have already had rtl assigned. See also
997 add_stack_var where we perpetrate this pc_rtx hack. */
998 decl = stack_vars[i].decl;
999 if ((TREE_CODE (decl) == SSA_NAME
1000 ? SA.partition_to_pseudo[var_to_partition (SA.map, decl)]
1001 : DECL_RTL (decl)) != pc_rtx)
1002 continue;
1004 /* Check the predicate to see whether this variable should be
1005 allocated in this pass. */
1006 if (pred && !pred (i))
1007 continue;
1009 alignb = stack_vars[i].alignb;
1010 if (alignb * BITS_PER_UNIT <= MAX_SUPPORTED_STACK_ALIGNMENT)
1012 base = virtual_stack_vars_rtx;
1013 if ((flag_sanitize & SANITIZE_ADDRESS) && ASAN_STACK && pred)
1015 HOST_WIDE_INT prev_offset = frame_offset;
1016 tree repr_decl = NULL_TREE;
1018 offset
1019 = alloc_stack_frame_space (stack_vars[i].size
1020 + ASAN_RED_ZONE_SIZE,
1021 MAX (alignb, ASAN_RED_ZONE_SIZE));
1022 data->asan_vec.safe_push (prev_offset);
1023 data->asan_vec.safe_push (offset + stack_vars[i].size);
1024 /* Find best representative of the partition.
1025 Prefer those with DECL_NAME, even better
1026 satisfying asan_protect_stack_decl predicate. */
1027 for (j = i; j != EOC; j = stack_vars[j].next)
1028 if (asan_protect_stack_decl (stack_vars[j].decl)
1029 && DECL_NAME (stack_vars[j].decl))
1031 repr_decl = stack_vars[j].decl;
1032 break;
1034 else if (repr_decl == NULL_TREE
1035 && DECL_P (stack_vars[j].decl)
1036 && DECL_NAME (stack_vars[j].decl))
1037 repr_decl = stack_vars[j].decl;
1038 if (repr_decl == NULL_TREE)
1039 repr_decl = stack_vars[i].decl;
1040 data->asan_decl_vec.safe_push (repr_decl);
1041 data->asan_alignb = MAX (data->asan_alignb, alignb);
1042 if (data->asan_base == NULL)
1043 data->asan_base = gen_reg_rtx (Pmode);
1044 base = data->asan_base;
1046 if (!STRICT_ALIGNMENT)
1047 base_align = crtl->max_used_stack_slot_alignment;
1048 else
1049 base_align = MAX (crtl->max_used_stack_slot_alignment,
1050 GET_MODE_ALIGNMENT (SImode)
1051 << ASAN_SHADOW_SHIFT);
1053 else
1055 offset = alloc_stack_frame_space (stack_vars[i].size, alignb);
1056 base_align = crtl->max_used_stack_slot_alignment;
1059 else
1061 /* Large alignment is only processed in the last pass. */
1062 if (pred)
1063 continue;
1064 gcc_assert (large_base != NULL);
1066 large_alloc += alignb - 1;
1067 large_alloc &= -(HOST_WIDE_INT)alignb;
1068 offset = large_alloc;
1069 large_alloc += stack_vars[i].size;
1071 base = large_base;
1072 base_align = large_align;
1075 /* Create rtl for each variable based on their location within the
1076 partition. */
1077 for (j = i; j != EOC; j = stack_vars[j].next)
1079 expand_one_stack_var_at (stack_vars[j].decl,
1080 base, base_align,
1081 offset);
1085 gcc_assert (large_alloc == large_size);
1088 /* Take into account all sizes of partitions and reset DECL_RTLs. */
1089 static HOST_WIDE_INT
1090 account_stack_vars (void)
1092 size_t si, j, i, n = stack_vars_num;
1093 HOST_WIDE_INT size = 0;
1095 for (si = 0; si < n; ++si)
1097 i = stack_vars_sorted[si];
1099 /* Skip variables that aren't partition representatives, for now. */
1100 if (stack_vars[i].representative != i)
1101 continue;
1103 size += stack_vars[i].size;
1104 for (j = i; j != EOC; j = stack_vars[j].next)
1105 set_rtl (stack_vars[j].decl, NULL);
1107 return size;
1110 /* A subroutine of expand_one_var. Called to immediately assign rtl
1111 to a variable to be allocated in the stack frame. */
1113 static void
1114 expand_one_stack_var (tree var)
1116 HOST_WIDE_INT size, offset;
1117 unsigned byte_align;
1119 size = tree_to_uhwi (DECL_SIZE_UNIT (SSAVAR (var)));
1120 byte_align = align_local_variable (SSAVAR (var));
1122 /* We handle highly aligned variables in expand_stack_vars. */
1123 gcc_assert (byte_align * BITS_PER_UNIT <= MAX_SUPPORTED_STACK_ALIGNMENT);
1125 offset = alloc_stack_frame_space (size, byte_align);
1127 expand_one_stack_var_at (var, virtual_stack_vars_rtx,
1128 crtl->max_used_stack_slot_alignment, offset);
1131 /* A subroutine of expand_one_var. Called to assign rtl to a VAR_DECL
1132 that will reside in a hard register. */
1134 static void
1135 expand_one_hard_reg_var (tree var)
1137 rest_of_decl_compilation (var, 0, 0);
1140 /* A subroutine of expand_one_var. Called to assign rtl to a VAR_DECL
1141 that will reside in a pseudo register. */
1143 static void
1144 expand_one_register_var (tree var)
1146 tree decl = SSAVAR (var);
1147 tree type = TREE_TYPE (decl);
1148 machine_mode reg_mode = promote_decl_mode (decl, NULL);
1149 rtx x = gen_reg_rtx (reg_mode);
1151 set_rtl (var, x);
1153 /* Note if the object is a user variable. */
1154 if (!DECL_ARTIFICIAL (decl))
1155 mark_user_reg (x);
1157 if (POINTER_TYPE_P (type))
1158 mark_reg_pointer (x, get_pointer_alignment (var));
1161 /* A subroutine of expand_one_var. Called to assign rtl to a VAR_DECL that
1162 has some associated error, e.g. its type is error-mark. We just need
1163 to pick something that won't crash the rest of the compiler. */
1165 static void
1166 expand_one_error_var (tree var)
1168 machine_mode mode = DECL_MODE (var);
1169 rtx x;
1171 if (mode == BLKmode)
1172 x = gen_rtx_MEM (BLKmode, const0_rtx);
1173 else if (mode == VOIDmode)
1174 x = const0_rtx;
1175 else
1176 x = gen_reg_rtx (mode);
1178 SET_DECL_RTL (var, x);
1181 /* A subroutine of expand_one_var. VAR is a variable that will be
1182 allocated to the local stack frame. Return true if we wish to
1183 add VAR to STACK_VARS so that it will be coalesced with other
1184 variables. Return false to allocate VAR immediately.
1186 This function is used to reduce the number of variables considered
1187 for coalescing, which reduces the size of the quadratic problem. */
1189 static bool
1190 defer_stack_allocation (tree var, bool toplevel)
1192 /* Whether the variable is small enough for immediate allocation not to be
1193 a problem with regard to the frame size. */
1194 bool smallish
1195 = ((HOST_WIDE_INT) tree_to_uhwi (DECL_SIZE_UNIT (var))
1196 < PARAM_VALUE (PARAM_MIN_SIZE_FOR_STACK_SHARING));
1198 /* If stack protection is enabled, *all* stack variables must be deferred,
1199 so that we can re-order the strings to the top of the frame.
1200 Similarly for Address Sanitizer. */
1201 if (flag_stack_protect || ((flag_sanitize & SANITIZE_ADDRESS) && ASAN_STACK))
1202 return true;
1204 /* We handle "large" alignment via dynamic allocation. We want to handle
1205 this extra complication in only one place, so defer them. */
1206 if (DECL_ALIGN (var) > MAX_SUPPORTED_STACK_ALIGNMENT)
1207 return true;
1209 /* When optimization is enabled, DECL_IGNORED_P variables originally scoped
1210 might be detached from their block and appear at toplevel when we reach
1211 here. We want to coalesce them with variables from other blocks when
1212 the immediate contribution to the frame size would be noticeable. */
1213 if (toplevel && optimize > 0 && DECL_IGNORED_P (var) && !smallish)
1214 return true;
1216 /* Variables declared in the outermost scope automatically conflict
1217 with every other variable. The only reason to want to defer them
1218 at all is that, after sorting, we can more efficiently pack
1219 small variables in the stack frame. Continue to defer at -O2. */
1220 if (toplevel && optimize < 2)
1221 return false;
1223 /* Without optimization, *most* variables are allocated from the
1224 stack, which makes the quadratic problem large exactly when we
1225 want compilation to proceed as quickly as possible. On the
1226 other hand, we don't want the function's stack frame size to
1227 get completely out of hand. So we avoid adding scalars and
1228 "small" aggregates to the list at all. */
1229 if (optimize == 0 && smallish)
1230 return false;
1232 return true;
1235 /* A subroutine of expand_used_vars. Expand one variable according to
1236 its flavor. Variables to be placed on the stack are not actually
1237 expanded yet, merely recorded.
1238 When REALLY_EXPAND is false, only add stack values to be allocated.
1239 Return stack usage this variable is supposed to take.
1242 static HOST_WIDE_INT
1243 expand_one_var (tree var, bool toplevel, bool really_expand)
1245 unsigned int align = BITS_PER_UNIT;
1246 tree origvar = var;
1248 var = SSAVAR (var);
1250 if (TREE_TYPE (var) != error_mark_node && TREE_CODE (var) == VAR_DECL)
1252 /* Because we don't know if VAR will be in register or on stack,
1253 we conservatively assume it will be on stack even if VAR is
1254 eventually put into register after RA pass. For non-automatic
1255 variables, which won't be on stack, we collect alignment of
1256 type and ignore user specified alignment. Similarly for
1257 SSA_NAMEs for which use_register_for_decl returns true. */
1258 if (TREE_STATIC (var)
1259 || DECL_EXTERNAL (var)
1260 || (TREE_CODE (origvar) == SSA_NAME && use_register_for_decl (var)))
1261 align = MINIMUM_ALIGNMENT (TREE_TYPE (var),
1262 TYPE_MODE (TREE_TYPE (var)),
1263 TYPE_ALIGN (TREE_TYPE (var)));
1264 else if (DECL_HAS_VALUE_EXPR_P (var)
1265 || (DECL_RTL_SET_P (var) && MEM_P (DECL_RTL (var))))
1266 /* Don't consider debug only variables with DECL_HAS_VALUE_EXPR_P set
1267 or variables which were assigned a stack slot already by
1268 expand_one_stack_var_at - in the latter case DECL_ALIGN has been
1269 changed from the offset chosen to it. */
1270 align = crtl->stack_alignment_estimated;
1271 else
1272 align = MINIMUM_ALIGNMENT (var, DECL_MODE (var), DECL_ALIGN (var));
1274 /* If the variable alignment is very large we'll dynamicaly allocate
1275 it, which means that in-frame portion is just a pointer. */
1276 if (align > MAX_SUPPORTED_STACK_ALIGNMENT)
1277 align = POINTER_SIZE;
1280 if (SUPPORTS_STACK_ALIGNMENT
1281 && crtl->stack_alignment_estimated < align)
1283 /* stack_alignment_estimated shouldn't change after stack
1284 realign decision made */
1285 gcc_assert (!crtl->stack_realign_processed);
1286 crtl->stack_alignment_estimated = align;
1289 /* stack_alignment_needed > PREFERRED_STACK_BOUNDARY is permitted.
1290 So here we only make sure stack_alignment_needed >= align. */
1291 if (crtl->stack_alignment_needed < align)
1292 crtl->stack_alignment_needed = align;
1293 if (crtl->max_used_stack_slot_alignment < align)
1294 crtl->max_used_stack_slot_alignment = align;
1296 if (TREE_CODE (origvar) == SSA_NAME)
1298 gcc_assert (TREE_CODE (var) != VAR_DECL
1299 || (!DECL_EXTERNAL (var)
1300 && !DECL_HAS_VALUE_EXPR_P (var)
1301 && !TREE_STATIC (var)
1302 && TREE_TYPE (var) != error_mark_node
1303 && !DECL_HARD_REGISTER (var)
1304 && really_expand));
1306 if (TREE_CODE (var) != VAR_DECL && TREE_CODE (origvar) != SSA_NAME)
1308 else if (DECL_EXTERNAL (var))
1310 else if (DECL_HAS_VALUE_EXPR_P (var))
1312 else if (TREE_STATIC (var))
1314 else if (TREE_CODE (origvar) != SSA_NAME && DECL_RTL_SET_P (var))
1316 else if (TREE_TYPE (var) == error_mark_node)
1318 if (really_expand)
1319 expand_one_error_var (var);
1321 else if (TREE_CODE (var) == VAR_DECL && DECL_HARD_REGISTER (var))
1323 if (really_expand)
1325 expand_one_hard_reg_var (var);
1326 if (!DECL_HARD_REGISTER (var))
1327 /* Invalid register specification. */
1328 expand_one_error_var (var);
1331 else if (use_register_for_decl (var))
1333 if (really_expand)
1334 expand_one_register_var (origvar);
1336 else if (! valid_constant_size_p (DECL_SIZE_UNIT (var)))
1338 /* Reject variables which cover more than half of the address-space. */
1339 if (really_expand)
1341 error ("size of variable %q+D is too large", var);
1342 expand_one_error_var (var);
1345 else if (defer_stack_allocation (var, toplevel))
1346 add_stack_var (origvar);
1347 else
1349 if (really_expand)
1350 expand_one_stack_var (origvar);
1351 return tree_to_uhwi (DECL_SIZE_UNIT (var));
1353 return 0;
1356 /* A subroutine of expand_used_vars. Walk down through the BLOCK tree
1357 expanding variables. Those variables that can be put into registers
1358 are allocated pseudos; those that can't are put on the stack.
1360 TOPLEVEL is true if this is the outermost BLOCK. */
1362 static void
1363 expand_used_vars_for_block (tree block, bool toplevel)
1365 tree t;
1367 /* Expand all variables at this level. */
1368 for (t = BLOCK_VARS (block); t ; t = DECL_CHAIN (t))
1369 if (TREE_USED (t)
1370 && ((TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != RESULT_DECL)
1371 || !DECL_NONSHAREABLE (t)))
1372 expand_one_var (t, toplevel, true);
1374 /* Expand all variables at containing levels. */
1375 for (t = BLOCK_SUBBLOCKS (block); t ; t = BLOCK_CHAIN (t))
1376 expand_used_vars_for_block (t, false);
1379 /* A subroutine of expand_used_vars. Walk down through the BLOCK tree
1380 and clear TREE_USED on all local variables. */
1382 static void
1383 clear_tree_used (tree block)
1385 tree t;
1387 for (t = BLOCK_VARS (block); t ; t = DECL_CHAIN (t))
1388 /* if (!TREE_STATIC (t) && !DECL_EXTERNAL (t)) */
1389 if ((TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != RESULT_DECL)
1390 || !DECL_NONSHAREABLE (t))
1391 TREE_USED (t) = 0;
1393 for (t = BLOCK_SUBBLOCKS (block); t ; t = BLOCK_CHAIN (t))
1394 clear_tree_used (t);
1397 enum {
1398 SPCT_FLAG_DEFAULT = 1,
1399 SPCT_FLAG_ALL = 2,
1400 SPCT_FLAG_STRONG = 3
1403 /* Examine TYPE and determine a bit mask of the following features. */
1405 #define SPCT_HAS_LARGE_CHAR_ARRAY 1
1406 #define SPCT_HAS_SMALL_CHAR_ARRAY 2
1407 #define SPCT_HAS_ARRAY 4
1408 #define SPCT_HAS_AGGREGATE 8
1410 static unsigned int
1411 stack_protect_classify_type (tree type)
1413 unsigned int ret = 0;
1414 tree t;
1416 switch (TREE_CODE (type))
1418 case ARRAY_TYPE:
1419 t = TYPE_MAIN_VARIANT (TREE_TYPE (type));
1420 if (t == char_type_node
1421 || t == signed_char_type_node
1422 || t == unsigned_char_type_node)
1424 unsigned HOST_WIDE_INT max = PARAM_VALUE (PARAM_SSP_BUFFER_SIZE);
1425 unsigned HOST_WIDE_INT len;
1427 if (!TYPE_SIZE_UNIT (type)
1428 || !tree_fits_uhwi_p (TYPE_SIZE_UNIT (type)))
1429 len = max;
1430 else
1431 len = tree_to_uhwi (TYPE_SIZE_UNIT (type));
1433 if (len < max)
1434 ret = SPCT_HAS_SMALL_CHAR_ARRAY | SPCT_HAS_ARRAY;
1435 else
1436 ret = SPCT_HAS_LARGE_CHAR_ARRAY | SPCT_HAS_ARRAY;
1438 else
1439 ret = SPCT_HAS_ARRAY;
1440 break;
1442 case UNION_TYPE:
1443 case QUAL_UNION_TYPE:
1444 case RECORD_TYPE:
1445 ret = SPCT_HAS_AGGREGATE;
1446 for (t = TYPE_FIELDS (type); t ; t = TREE_CHAIN (t))
1447 if (TREE_CODE (t) == FIELD_DECL)
1448 ret |= stack_protect_classify_type (TREE_TYPE (t));
1449 break;
1451 default:
1452 break;
1455 return ret;
1458 /* Return nonzero if DECL should be segregated into the "vulnerable" upper
1459 part of the local stack frame. Remember if we ever return nonzero for
1460 any variable in this function. The return value is the phase number in
1461 which the variable should be allocated. */
1463 static int
1464 stack_protect_decl_phase (tree decl)
1466 unsigned int bits = stack_protect_classify_type (TREE_TYPE (decl));
1467 int ret = 0;
1469 if (bits & SPCT_HAS_SMALL_CHAR_ARRAY)
1470 has_short_buffer = true;
1472 if (flag_stack_protect == SPCT_FLAG_ALL
1473 || flag_stack_protect == SPCT_FLAG_STRONG)
1475 if ((bits & (SPCT_HAS_SMALL_CHAR_ARRAY | SPCT_HAS_LARGE_CHAR_ARRAY))
1476 && !(bits & SPCT_HAS_AGGREGATE))
1477 ret = 1;
1478 else if (bits & SPCT_HAS_ARRAY)
1479 ret = 2;
1481 else
1482 ret = (bits & SPCT_HAS_LARGE_CHAR_ARRAY) != 0;
1484 if (ret)
1485 has_protected_decls = true;
1487 return ret;
1490 /* Two helper routines that check for phase 1 and phase 2. These are used
1491 as callbacks for expand_stack_vars. */
1493 static bool
1494 stack_protect_decl_phase_1 (size_t i)
1496 return stack_protect_decl_phase (stack_vars[i].decl) == 1;
1499 static bool
1500 stack_protect_decl_phase_2 (size_t i)
1502 return stack_protect_decl_phase (stack_vars[i].decl) == 2;
1505 /* And helper function that checks for asan phase (with stack protector
1506 it is phase 3). This is used as callback for expand_stack_vars.
1507 Returns true if any of the vars in the partition need to be protected. */
1509 static bool
1510 asan_decl_phase_3 (size_t i)
1512 while (i != EOC)
1514 if (asan_protect_stack_decl (stack_vars[i].decl))
1515 return true;
1516 i = stack_vars[i].next;
1518 return false;
1521 /* Ensure that variables in different stack protection phases conflict
1522 so that they are not merged and share the same stack slot. */
1524 static void
1525 add_stack_protection_conflicts (void)
1527 size_t i, j, n = stack_vars_num;
1528 unsigned char *phase;
1530 phase = XNEWVEC (unsigned char, n);
1531 for (i = 0; i < n; ++i)
1532 phase[i] = stack_protect_decl_phase (stack_vars[i].decl);
1534 for (i = 0; i < n; ++i)
1536 unsigned char ph_i = phase[i];
1537 for (j = i + 1; j < n; ++j)
1538 if (ph_i != phase[j])
1539 add_stack_var_conflict (i, j);
1542 XDELETEVEC (phase);
1545 /* Create a decl for the guard at the top of the stack frame. */
1547 static void
1548 create_stack_guard (void)
1550 tree guard = build_decl (DECL_SOURCE_LOCATION (current_function_decl),
1551 VAR_DECL, NULL, ptr_type_node);
1552 TREE_THIS_VOLATILE (guard) = 1;
1553 TREE_USED (guard) = 1;
1554 expand_one_stack_var (guard);
1555 crtl->stack_protect_guard = guard;
1558 /* Prepare for expanding variables. */
1559 static void
1560 init_vars_expansion (void)
1562 /* Conflict bitmaps, and a few related temporary bitmaps, go here. */
1563 bitmap_obstack_initialize (&stack_var_bitmap_obstack);
1565 /* A map from decl to stack partition. */
1566 decl_to_stack_part = new hash_map<tree, size_t>;
1568 /* Initialize local stack smashing state. */
1569 has_protected_decls = false;
1570 has_short_buffer = false;
1573 /* Free up stack variable graph data. */
1574 static void
1575 fini_vars_expansion (void)
1577 bitmap_obstack_release (&stack_var_bitmap_obstack);
1578 if (stack_vars)
1579 XDELETEVEC (stack_vars);
1580 if (stack_vars_sorted)
1581 XDELETEVEC (stack_vars_sorted);
1582 stack_vars = NULL;
1583 stack_vars_sorted = NULL;
1584 stack_vars_alloc = stack_vars_num = 0;
1585 delete decl_to_stack_part;
1586 decl_to_stack_part = NULL;
1589 /* Make a fair guess for the size of the stack frame of the function
1590 in NODE. This doesn't have to be exact, the result is only used in
1591 the inline heuristics. So we don't want to run the full stack var
1592 packing algorithm (which is quadratic in the number of stack vars).
1593 Instead, we calculate the total size of all stack vars. This turns
1594 out to be a pretty fair estimate -- packing of stack vars doesn't
1595 happen very often. */
1597 HOST_WIDE_INT
1598 estimated_stack_frame_size (struct cgraph_node *node)
1600 HOST_WIDE_INT size = 0;
1601 size_t i;
1602 tree var;
1603 struct function *fn = DECL_STRUCT_FUNCTION (node->decl);
1605 push_cfun (fn);
1607 init_vars_expansion ();
1609 FOR_EACH_LOCAL_DECL (fn, i, var)
1610 if (auto_var_in_fn_p (var, fn->decl))
1611 size += expand_one_var (var, true, false);
1613 if (stack_vars_num > 0)
1615 /* Fake sorting the stack vars for account_stack_vars (). */
1616 stack_vars_sorted = XNEWVEC (size_t, stack_vars_num);
1617 for (i = 0; i < stack_vars_num; ++i)
1618 stack_vars_sorted[i] = i;
1619 size += account_stack_vars ();
1622 fini_vars_expansion ();
1623 pop_cfun ();
1624 return size;
1627 /* Helper routine to check if a record or union contains an array field. */
1629 static int
1630 record_or_union_type_has_array_p (const_tree tree_type)
1632 tree fields = TYPE_FIELDS (tree_type);
1633 tree f;
1635 for (f = fields; f; f = DECL_CHAIN (f))
1636 if (TREE_CODE (f) == FIELD_DECL)
1638 tree field_type = TREE_TYPE (f);
1639 if (RECORD_OR_UNION_TYPE_P (field_type)
1640 && record_or_union_type_has_array_p (field_type))
1641 return 1;
1642 if (TREE_CODE (field_type) == ARRAY_TYPE)
1643 return 1;
1645 return 0;
1648 /* Check if the current function has local referenced variables that
1649 have their addresses taken, contain an array, or are arrays. */
1651 static bool
1652 stack_protect_decl_p ()
1654 unsigned i;
1655 tree var;
1657 FOR_EACH_LOCAL_DECL (cfun, i, var)
1658 if (!is_global_var (var))
1660 tree var_type = TREE_TYPE (var);
1661 if (TREE_CODE (var) == VAR_DECL
1662 && (TREE_CODE (var_type) == ARRAY_TYPE
1663 || TREE_ADDRESSABLE (var)
1664 || (RECORD_OR_UNION_TYPE_P (var_type)
1665 && record_or_union_type_has_array_p (var_type))))
1666 return true;
1668 return false;
1671 /* Check if the current function has calls that use a return slot. */
1673 static bool
1674 stack_protect_return_slot_p ()
1676 basic_block bb;
1678 FOR_ALL_BB_FN (bb, cfun)
1679 for (gimple_stmt_iterator gsi = gsi_start_bb (bb);
1680 !gsi_end_p (gsi); gsi_next (&gsi))
1682 gimple stmt = gsi_stmt (gsi);
1683 /* This assumes that calls to internal-only functions never
1684 use a return slot. */
1685 if (is_gimple_call (stmt)
1686 && !gimple_call_internal_p (stmt)
1687 && aggregate_value_p (TREE_TYPE (gimple_call_fntype (stmt)),
1688 gimple_call_fndecl (stmt)))
1689 return true;
1691 return false;
1694 /* Expand all variables used in the function. */
1696 static rtx_insn *
1697 expand_used_vars (void)
1699 tree var, outer_block = DECL_INITIAL (current_function_decl);
1700 vec<tree> maybe_local_decls = vNULL;
1701 rtx_insn *var_end_seq = NULL;
1702 unsigned i;
1703 unsigned len;
1704 bool gen_stack_protect_signal = false;
1706 /* Compute the phase of the stack frame for this function. */
1708 int align = PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT;
1709 int off = STARTING_FRAME_OFFSET % align;
1710 frame_phase = off ? align - off : 0;
1713 /* Set TREE_USED on all variables in the local_decls. */
1714 FOR_EACH_LOCAL_DECL (cfun, i, var)
1715 TREE_USED (var) = 1;
1716 /* Clear TREE_USED on all variables associated with a block scope. */
1717 clear_tree_used (DECL_INITIAL (current_function_decl));
1719 init_vars_expansion ();
1721 hash_map<tree, tree> ssa_name_decls;
1722 for (i = 0; i < SA.map->num_partitions; i++)
1724 tree var = partition_to_var (SA.map, i);
1726 gcc_assert (!virtual_operand_p (var));
1728 /* Assign decls to each SSA name partition, share decls for partitions
1729 we could have coalesced (those with the same type). */
1730 if (SSA_NAME_VAR (var) == NULL_TREE)
1732 tree *slot = &ssa_name_decls.get_or_insert (TREE_TYPE (var));
1733 if (!*slot)
1734 *slot = create_tmp_reg (TREE_TYPE (var), NULL);
1735 replace_ssa_name_symbol (var, *slot);
1738 /* Always allocate space for partitions based on VAR_DECLs. But for
1739 those based on PARM_DECLs or RESULT_DECLs and which matter for the
1740 debug info, there is no need to do so if optimization is disabled
1741 because all the SSA_NAMEs based on these DECLs have been coalesced
1742 into a single partition, which is thus assigned the canonical RTL
1743 location of the DECLs. If in_lto_p, we can't rely on optimize,
1744 a function could be compiled with -O1 -flto first and only the
1745 link performed at -O0. */
1746 if (TREE_CODE (SSA_NAME_VAR (var)) == VAR_DECL)
1747 expand_one_var (var, true, true);
1748 else if (DECL_IGNORED_P (SSA_NAME_VAR (var)) || optimize || in_lto_p)
1750 /* This is a PARM_DECL or RESULT_DECL. For those partitions that
1751 contain the default def (representing the parm or result itself)
1752 we don't do anything here. But those which don't contain the
1753 default def (representing a temporary based on the parm/result)
1754 we need to allocate space just like for normal VAR_DECLs. */
1755 if (!bitmap_bit_p (SA.partition_has_default_def, i))
1757 expand_one_var (var, true, true);
1758 gcc_assert (SA.partition_to_pseudo[i]);
1763 if (flag_stack_protect == SPCT_FLAG_STRONG)
1764 gen_stack_protect_signal
1765 = stack_protect_decl_p () || stack_protect_return_slot_p ();
1767 /* At this point all variables on the local_decls with TREE_USED
1768 set are not associated with any block scope. Lay them out. */
1770 len = vec_safe_length (cfun->local_decls);
1771 FOR_EACH_LOCAL_DECL (cfun, i, var)
1773 bool expand_now = false;
1775 /* Expanded above already. */
1776 if (is_gimple_reg (var))
1778 TREE_USED (var) = 0;
1779 goto next;
1781 /* We didn't set a block for static or extern because it's hard
1782 to tell the difference between a global variable (re)declared
1783 in a local scope, and one that's really declared there to
1784 begin with. And it doesn't really matter much, since we're
1785 not giving them stack space. Expand them now. */
1786 else if (TREE_STATIC (var) || DECL_EXTERNAL (var))
1787 expand_now = true;
1789 /* Expand variables not associated with any block now. Those created by
1790 the optimizers could be live anywhere in the function. Those that
1791 could possibly have been scoped originally and detached from their
1792 block will have their allocation deferred so we coalesce them with
1793 others when optimization is enabled. */
1794 else if (TREE_USED (var))
1795 expand_now = true;
1797 /* Finally, mark all variables on the list as used. We'll use
1798 this in a moment when we expand those associated with scopes. */
1799 TREE_USED (var) = 1;
1801 if (expand_now)
1802 expand_one_var (var, true, true);
1804 next:
1805 if (DECL_ARTIFICIAL (var) && !DECL_IGNORED_P (var))
1807 rtx rtl = DECL_RTL_IF_SET (var);
1809 /* Keep artificial non-ignored vars in cfun->local_decls
1810 chain until instantiate_decls. */
1811 if (rtl && (MEM_P (rtl) || GET_CODE (rtl) == CONCAT))
1812 add_local_decl (cfun, var);
1813 else if (rtl == NULL_RTX)
1814 /* If rtl isn't set yet, which can happen e.g. with
1815 -fstack-protector, retry before returning from this
1816 function. */
1817 maybe_local_decls.safe_push (var);
1821 /* We duplicated some of the decls in CFUN->LOCAL_DECLS.
1823 +-----------------+-----------------+
1824 | ...processed... | ...duplicates...|
1825 +-----------------+-----------------+
1827 +-- LEN points here.
1829 We just want the duplicates, as those are the artificial
1830 non-ignored vars that we want to keep until instantiate_decls.
1831 Move them down and truncate the array. */
1832 if (!vec_safe_is_empty (cfun->local_decls))
1833 cfun->local_decls->block_remove (0, len);
1835 /* At this point, all variables within the block tree with TREE_USED
1836 set are actually used by the optimized function. Lay them out. */
1837 expand_used_vars_for_block (outer_block, true);
1839 if (stack_vars_num > 0)
1841 add_scope_conflicts ();
1843 /* If stack protection is enabled, we don't share space between
1844 vulnerable data and non-vulnerable data. */
1845 if (flag_stack_protect)
1846 add_stack_protection_conflicts ();
1848 /* Now that we have collected all stack variables, and have computed a
1849 minimal interference graph, attempt to save some stack space. */
1850 partition_stack_vars ();
1851 if (dump_file)
1852 dump_stack_var_partition ();
1855 switch (flag_stack_protect)
1857 case SPCT_FLAG_ALL:
1858 create_stack_guard ();
1859 break;
1861 case SPCT_FLAG_STRONG:
1862 if (gen_stack_protect_signal
1863 || cfun->calls_alloca || has_protected_decls)
1864 create_stack_guard ();
1865 break;
1867 case SPCT_FLAG_DEFAULT:
1868 if (cfun->calls_alloca || has_protected_decls)
1869 create_stack_guard ();
1870 break;
1872 default:
1876 /* Assign rtl to each variable based on these partitions. */
1877 if (stack_vars_num > 0)
1879 struct stack_vars_data data;
1881 data.asan_vec = vNULL;
1882 data.asan_decl_vec = vNULL;
1883 data.asan_base = NULL_RTX;
1884 data.asan_alignb = 0;
1886 /* Reorder decls to be protected by iterating over the variables
1887 array multiple times, and allocating out of each phase in turn. */
1888 /* ??? We could probably integrate this into the qsort we did
1889 earlier, such that we naturally see these variables first,
1890 and thus naturally allocate things in the right order. */
1891 if (has_protected_decls)
1893 /* Phase 1 contains only character arrays. */
1894 expand_stack_vars (stack_protect_decl_phase_1, &data);
1896 /* Phase 2 contains other kinds of arrays. */
1897 if (flag_stack_protect == 2)
1898 expand_stack_vars (stack_protect_decl_phase_2, &data);
1901 if ((flag_sanitize & SANITIZE_ADDRESS) && ASAN_STACK)
1902 /* Phase 3, any partitions that need asan protection
1903 in addition to phase 1 and 2. */
1904 expand_stack_vars (asan_decl_phase_3, &data);
1906 if (!data.asan_vec.is_empty ())
1908 HOST_WIDE_INT prev_offset = frame_offset;
1909 HOST_WIDE_INT offset, sz, redzonesz;
1910 redzonesz = ASAN_RED_ZONE_SIZE;
1911 sz = data.asan_vec[0] - prev_offset;
1912 if (data.asan_alignb > ASAN_RED_ZONE_SIZE
1913 && data.asan_alignb <= 4096
1914 && sz + ASAN_RED_ZONE_SIZE >= (int) data.asan_alignb)
1915 redzonesz = ((sz + ASAN_RED_ZONE_SIZE + data.asan_alignb - 1)
1916 & ~(data.asan_alignb - HOST_WIDE_INT_1)) - sz;
1917 offset
1918 = alloc_stack_frame_space (redzonesz, ASAN_RED_ZONE_SIZE);
1919 data.asan_vec.safe_push (prev_offset);
1920 data.asan_vec.safe_push (offset);
1921 /* Leave space for alignment if STRICT_ALIGNMENT. */
1922 if (STRICT_ALIGNMENT)
1923 alloc_stack_frame_space ((GET_MODE_ALIGNMENT (SImode)
1924 << ASAN_SHADOW_SHIFT)
1925 / BITS_PER_UNIT, 1);
1927 var_end_seq
1928 = asan_emit_stack_protection (virtual_stack_vars_rtx,
1929 data.asan_base,
1930 data.asan_alignb,
1931 data.asan_vec.address (),
1932 data.asan_decl_vec.address (),
1933 data.asan_vec.length ());
1936 expand_stack_vars (NULL, &data);
1938 data.asan_vec.release ();
1939 data.asan_decl_vec.release ();
1942 fini_vars_expansion ();
1944 /* If there were any artificial non-ignored vars without rtl
1945 found earlier, see if deferred stack allocation hasn't assigned
1946 rtl to them. */
1947 FOR_EACH_VEC_ELT_REVERSE (maybe_local_decls, i, var)
1949 rtx rtl = DECL_RTL_IF_SET (var);
1951 /* Keep artificial non-ignored vars in cfun->local_decls
1952 chain until instantiate_decls. */
1953 if (rtl && (MEM_P (rtl) || GET_CODE (rtl) == CONCAT))
1954 add_local_decl (cfun, var);
1956 maybe_local_decls.release ();
1958 /* If the target requires that FRAME_OFFSET be aligned, do it. */
1959 if (STACK_ALIGNMENT_NEEDED)
1961 HOST_WIDE_INT align = PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT;
1962 if (!FRAME_GROWS_DOWNWARD)
1963 frame_offset += align - 1;
1964 frame_offset &= -align;
1967 return var_end_seq;
1971 /* If we need to produce a detailed dump, print the tree representation
1972 for STMT to the dump file. SINCE is the last RTX after which the RTL
1973 generated for STMT should have been appended. */
1975 static void
1976 maybe_dump_rtl_for_gimple_stmt (gimple stmt, rtx_insn *since)
1978 if (dump_file && (dump_flags & TDF_DETAILS))
1980 fprintf (dump_file, "\n;; ");
1981 print_gimple_stmt (dump_file, stmt, 0,
1982 TDF_SLIM | (dump_flags & TDF_LINENO));
1983 fprintf (dump_file, "\n");
1985 print_rtl (dump_file, since ? NEXT_INSN (since) : since);
1989 /* Maps the blocks that do not contain tree labels to rtx labels. */
1991 static hash_map<basic_block, rtx_code_label *> *lab_rtx_for_bb;
1993 /* Returns the label_rtx expression for a label starting basic block BB. */
1995 static rtx
1996 label_rtx_for_bb (basic_block bb ATTRIBUTE_UNUSED)
1998 gimple_stmt_iterator gsi;
1999 tree lab;
2000 gimple lab_stmt;
2002 if (bb->flags & BB_RTL)
2003 return block_label (bb);
2005 rtx_code_label **elt = lab_rtx_for_bb->get (bb);
2006 if (elt)
2007 return *elt;
2009 /* Find the tree label if it is present. */
2011 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
2013 lab_stmt = gsi_stmt (gsi);
2014 if (gimple_code (lab_stmt) != GIMPLE_LABEL)
2015 break;
2017 lab = gimple_label_label (lab_stmt);
2018 if (DECL_NONLOCAL (lab))
2019 break;
2021 return label_rtx (lab);
2024 rtx_code_label *l = gen_label_rtx ();
2025 lab_rtx_for_bb->put (bb, l);
2026 return l;
2030 /* A subroutine of expand_gimple_cond. Given E, a fallthrough edge
2031 of a basic block where we just expanded the conditional at the end,
2032 possibly clean up the CFG and instruction sequence. LAST is the
2033 last instruction before the just emitted jump sequence. */
2035 static void
2036 maybe_cleanup_end_of_block (edge e, rtx_insn *last)
2038 /* Special case: when jumpif decides that the condition is
2039 trivial it emits an unconditional jump (and the necessary
2040 barrier). But we still have two edges, the fallthru one is
2041 wrong. purge_dead_edges would clean this up later. Unfortunately
2042 we have to insert insns (and split edges) before
2043 find_many_sub_basic_blocks and hence before purge_dead_edges.
2044 But splitting edges might create new blocks which depend on the
2045 fact that if there are two edges there's no barrier. So the
2046 barrier would get lost and verify_flow_info would ICE. Instead
2047 of auditing all edge splitters to care for the barrier (which
2048 normally isn't there in a cleaned CFG), fix it here. */
2049 if (BARRIER_P (get_last_insn ()))
2051 rtx_insn *insn;
2052 remove_edge (e);
2053 /* Now, we have a single successor block, if we have insns to
2054 insert on the remaining edge we potentially will insert
2055 it at the end of this block (if the dest block isn't feasible)
2056 in order to avoid splitting the edge. This insertion will take
2057 place in front of the last jump. But we might have emitted
2058 multiple jumps (conditional and one unconditional) to the
2059 same destination. Inserting in front of the last one then
2060 is a problem. See PR 40021. We fix this by deleting all
2061 jumps except the last unconditional one. */
2062 insn = PREV_INSN (get_last_insn ());
2063 /* Make sure we have an unconditional jump. Otherwise we're
2064 confused. */
2065 gcc_assert (JUMP_P (insn) && !any_condjump_p (insn));
2066 for (insn = PREV_INSN (insn); insn != last;)
2068 insn = PREV_INSN (insn);
2069 if (JUMP_P (NEXT_INSN (insn)))
2071 if (!any_condjump_p (NEXT_INSN (insn)))
2073 gcc_assert (BARRIER_P (NEXT_INSN (NEXT_INSN (insn))));
2074 delete_insn (NEXT_INSN (NEXT_INSN (insn)));
2076 delete_insn (NEXT_INSN (insn));
2082 /* A subroutine of expand_gimple_basic_block. Expand one GIMPLE_COND.
2083 Returns a new basic block if we've terminated the current basic
2084 block and created a new one. */
2086 static basic_block
2087 expand_gimple_cond (basic_block bb, gimple stmt)
2089 basic_block new_bb, dest;
2090 edge new_edge;
2091 edge true_edge;
2092 edge false_edge;
2093 rtx_insn *last2, *last;
2094 enum tree_code code;
2095 tree op0, op1;
2097 code = gimple_cond_code (stmt);
2098 op0 = gimple_cond_lhs (stmt);
2099 op1 = gimple_cond_rhs (stmt);
2100 /* We're sometimes presented with such code:
2101 D.123_1 = x < y;
2102 if (D.123_1 != 0)
2104 This would expand to two comparisons which then later might
2105 be cleaned up by combine. But some pattern matchers like if-conversion
2106 work better when there's only one compare, so make up for this
2107 here as special exception if TER would have made the same change. */
2108 if (SA.values
2109 && TREE_CODE (op0) == SSA_NAME
2110 && TREE_CODE (TREE_TYPE (op0)) == BOOLEAN_TYPE
2111 && TREE_CODE (op1) == INTEGER_CST
2112 && ((gimple_cond_code (stmt) == NE_EXPR
2113 && integer_zerop (op1))
2114 || (gimple_cond_code (stmt) == EQ_EXPR
2115 && integer_onep (op1)))
2116 && bitmap_bit_p (SA.values, SSA_NAME_VERSION (op0)))
2118 gimple second = SSA_NAME_DEF_STMT (op0);
2119 if (gimple_code (second) == GIMPLE_ASSIGN)
2121 enum tree_code code2 = gimple_assign_rhs_code (second);
2122 if (TREE_CODE_CLASS (code2) == tcc_comparison)
2124 code = code2;
2125 op0 = gimple_assign_rhs1 (second);
2126 op1 = gimple_assign_rhs2 (second);
2128 /* If jumps are cheap turn some more codes into
2129 jumpy sequences. */
2130 else if (BRANCH_COST (optimize_insn_for_speed_p (), false) < 4)
2132 if ((code2 == BIT_AND_EXPR
2133 && TYPE_PRECISION (TREE_TYPE (op0)) == 1
2134 && TREE_CODE (gimple_assign_rhs2 (second)) != INTEGER_CST)
2135 || code2 == TRUTH_AND_EXPR)
2137 code = TRUTH_ANDIF_EXPR;
2138 op0 = gimple_assign_rhs1 (second);
2139 op1 = gimple_assign_rhs2 (second);
2141 else if (code2 == BIT_IOR_EXPR || code2 == TRUTH_OR_EXPR)
2143 code = TRUTH_ORIF_EXPR;
2144 op0 = gimple_assign_rhs1 (second);
2145 op1 = gimple_assign_rhs2 (second);
2151 last2 = last = get_last_insn ();
2153 extract_true_false_edges_from_block (bb, &true_edge, &false_edge);
2154 set_curr_insn_location (gimple_location (stmt));
2156 /* These flags have no purpose in RTL land. */
2157 true_edge->flags &= ~EDGE_TRUE_VALUE;
2158 false_edge->flags &= ~EDGE_FALSE_VALUE;
2160 /* We can either have a pure conditional jump with one fallthru edge or
2161 two-way jump that needs to be decomposed into two basic blocks. */
2162 if (false_edge->dest == bb->next_bb)
2164 jumpif_1 (code, op0, op1, label_rtx_for_bb (true_edge->dest),
2165 true_edge->probability);
2166 maybe_dump_rtl_for_gimple_stmt (stmt, last);
2167 if (true_edge->goto_locus != UNKNOWN_LOCATION)
2168 set_curr_insn_location (true_edge->goto_locus);
2169 false_edge->flags |= EDGE_FALLTHRU;
2170 maybe_cleanup_end_of_block (false_edge, last);
2171 return NULL;
2173 if (true_edge->dest == bb->next_bb)
2175 jumpifnot_1 (code, op0, op1, label_rtx_for_bb (false_edge->dest),
2176 false_edge->probability);
2177 maybe_dump_rtl_for_gimple_stmt (stmt, last);
2178 if (false_edge->goto_locus != UNKNOWN_LOCATION)
2179 set_curr_insn_location (false_edge->goto_locus);
2180 true_edge->flags |= EDGE_FALLTHRU;
2181 maybe_cleanup_end_of_block (true_edge, last);
2182 return NULL;
2185 jumpif_1 (code, op0, op1, label_rtx_for_bb (true_edge->dest),
2186 true_edge->probability);
2187 last = get_last_insn ();
2188 if (false_edge->goto_locus != UNKNOWN_LOCATION)
2189 set_curr_insn_location (false_edge->goto_locus);
2190 emit_jump (label_rtx_for_bb (false_edge->dest));
2192 BB_END (bb) = last;
2193 if (BARRIER_P (BB_END (bb)))
2194 BB_END (bb) = PREV_INSN (BB_END (bb));
2195 update_bb_for_insn (bb);
2197 new_bb = create_basic_block (NEXT_INSN (last), get_last_insn (), bb);
2198 dest = false_edge->dest;
2199 redirect_edge_succ (false_edge, new_bb);
2200 false_edge->flags |= EDGE_FALLTHRU;
2201 new_bb->count = false_edge->count;
2202 new_bb->frequency = EDGE_FREQUENCY (false_edge);
2203 add_bb_to_loop (new_bb, bb->loop_father);
2204 new_edge = make_edge (new_bb, dest, 0);
2205 new_edge->probability = REG_BR_PROB_BASE;
2206 new_edge->count = new_bb->count;
2207 if (BARRIER_P (BB_END (new_bb)))
2208 BB_END (new_bb) = PREV_INSN (BB_END (new_bb));
2209 update_bb_for_insn (new_bb);
2211 maybe_dump_rtl_for_gimple_stmt (stmt, last2);
2213 if (true_edge->goto_locus != UNKNOWN_LOCATION)
2215 set_curr_insn_location (true_edge->goto_locus);
2216 true_edge->goto_locus = curr_insn_location ();
2219 return new_bb;
2222 /* Mark all calls that can have a transaction restart. */
2224 static void
2225 mark_transaction_restart_calls (gimple stmt)
2227 struct tm_restart_node dummy;
2228 void **slot;
2230 if (!cfun->gimple_df->tm_restart)
2231 return;
2233 dummy.stmt = stmt;
2234 slot = htab_find_slot (cfun->gimple_df->tm_restart, &dummy, NO_INSERT);
2235 if (slot)
2237 struct tm_restart_node *n = (struct tm_restart_node *) *slot;
2238 tree list = n->label_or_list;
2239 rtx_insn *insn;
2241 for (insn = next_real_insn (get_last_insn ());
2242 !CALL_P (insn);
2243 insn = next_real_insn (insn))
2244 continue;
2246 if (TREE_CODE (list) == LABEL_DECL)
2247 add_reg_note (insn, REG_TM, label_rtx (list));
2248 else
2249 for (; list ; list = TREE_CHAIN (list))
2250 add_reg_note (insn, REG_TM, label_rtx (TREE_VALUE (list)));
2254 /* A subroutine of expand_gimple_stmt_1, expanding one GIMPLE_CALL
2255 statement STMT. */
2257 static void
2258 expand_call_stmt (gimple stmt)
2260 tree exp, decl, lhs;
2261 bool builtin_p;
2262 size_t i;
2264 if (gimple_call_internal_p (stmt))
2266 expand_internal_call (stmt);
2267 return;
2270 exp = build_vl_exp (CALL_EXPR, gimple_call_num_args (stmt) + 3);
2272 CALL_EXPR_FN (exp) = gimple_call_fn (stmt);
2273 decl = gimple_call_fndecl (stmt);
2274 builtin_p = decl && DECL_BUILT_IN (decl);
2276 /* If this is not a builtin function, the function type through which the
2277 call is made may be different from the type of the function. */
2278 if (!builtin_p)
2279 CALL_EXPR_FN (exp)
2280 = fold_convert (build_pointer_type (gimple_call_fntype (stmt)),
2281 CALL_EXPR_FN (exp));
2283 TREE_TYPE (exp) = gimple_call_return_type (stmt);
2284 CALL_EXPR_STATIC_CHAIN (exp) = gimple_call_chain (stmt);
2286 for (i = 0; i < gimple_call_num_args (stmt); i++)
2288 tree arg = gimple_call_arg (stmt, i);
2289 gimple def;
2290 /* TER addresses into arguments of builtin functions so we have a
2291 chance to infer more correct alignment information. See PR39954. */
2292 if (builtin_p
2293 && TREE_CODE (arg) == SSA_NAME
2294 && (def = get_gimple_for_ssa_name (arg))
2295 && gimple_assign_rhs_code (def) == ADDR_EXPR)
2296 arg = gimple_assign_rhs1 (def);
2297 CALL_EXPR_ARG (exp, i) = arg;
2300 if (gimple_has_side_effects (stmt))
2301 TREE_SIDE_EFFECTS (exp) = 1;
2303 if (gimple_call_nothrow_p (stmt))
2304 TREE_NOTHROW (exp) = 1;
2306 CALL_EXPR_TAILCALL (exp) = gimple_call_tail_p (stmt);
2307 CALL_EXPR_RETURN_SLOT_OPT (exp) = gimple_call_return_slot_opt_p (stmt);
2308 if (decl
2309 && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL
2310 && (DECL_FUNCTION_CODE (decl) == BUILT_IN_ALLOCA
2311 || DECL_FUNCTION_CODE (decl) == BUILT_IN_ALLOCA_WITH_ALIGN))
2312 CALL_ALLOCA_FOR_VAR_P (exp) = gimple_call_alloca_for_var_p (stmt);
2313 else
2314 CALL_FROM_THUNK_P (exp) = gimple_call_from_thunk_p (stmt);
2315 CALL_EXPR_VA_ARG_PACK (exp) = gimple_call_va_arg_pack_p (stmt);
2316 SET_EXPR_LOCATION (exp, gimple_location (stmt));
2318 /* Ensure RTL is created for debug args. */
2319 if (decl && DECL_HAS_DEBUG_ARGS_P (decl))
2321 vec<tree, va_gc> **debug_args = decl_debug_args_lookup (decl);
2322 unsigned int ix;
2323 tree dtemp;
2325 if (debug_args)
2326 for (ix = 1; (*debug_args)->iterate (ix, &dtemp); ix += 2)
2328 gcc_assert (TREE_CODE (dtemp) == DEBUG_EXPR_DECL);
2329 expand_debug_expr (dtemp);
2333 lhs = gimple_call_lhs (stmt);
2334 if (lhs)
2335 expand_assignment (lhs, exp, false);
2336 else
2337 expand_expr (exp, const0_rtx, VOIDmode, EXPAND_NORMAL);
2339 mark_transaction_restart_calls (stmt);
2343 /* Generate RTL for an asm statement (explicit assembler code).
2344 STRING is a STRING_CST node containing the assembler code text,
2345 or an ADDR_EXPR containing a STRING_CST. VOL nonzero means the
2346 insn is volatile; don't optimize it. */
2348 static void
2349 expand_asm_loc (tree string, int vol, location_t locus)
2351 rtx body;
2353 if (TREE_CODE (string) == ADDR_EXPR)
2354 string = TREE_OPERAND (string, 0);
2356 body = gen_rtx_ASM_INPUT_loc (VOIDmode,
2357 ggc_strdup (TREE_STRING_POINTER (string)),
2358 locus);
2360 MEM_VOLATILE_P (body) = vol;
2362 emit_insn (body);
2365 /* Return the number of times character C occurs in string S. */
2366 static int
2367 n_occurrences (int c, const char *s)
2369 int n = 0;
2370 while (*s)
2371 n += (*s++ == c);
2372 return n;
2375 /* A subroutine of expand_asm_operands. Check that all operands have
2376 the same number of alternatives. Return true if so. */
2378 static bool
2379 check_operand_nalternatives (tree outputs, tree inputs)
2381 if (outputs || inputs)
2383 tree tmp = TREE_PURPOSE (outputs ? outputs : inputs);
2384 int nalternatives
2385 = n_occurrences (',', TREE_STRING_POINTER (TREE_VALUE (tmp)));
2386 tree next = inputs;
2388 if (nalternatives + 1 > MAX_RECOG_ALTERNATIVES)
2390 error ("too many alternatives in %<asm%>");
2391 return false;
2394 tmp = outputs;
2395 while (tmp)
2397 const char *constraint
2398 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tmp)));
2400 if (n_occurrences (',', constraint) != nalternatives)
2402 error ("operand constraints for %<asm%> differ "
2403 "in number of alternatives");
2404 return false;
2407 if (TREE_CHAIN (tmp))
2408 tmp = TREE_CHAIN (tmp);
2409 else
2410 tmp = next, next = 0;
2414 return true;
2417 /* Check for overlap between registers marked in CLOBBERED_REGS and
2418 anything inappropriate in T. Emit error and return the register
2419 variable definition for error, NULL_TREE for ok. */
2421 static bool
2422 tree_conflicts_with_clobbers_p (tree t, HARD_REG_SET *clobbered_regs)
2424 /* Conflicts between asm-declared register variables and the clobber
2425 list are not allowed. */
2426 tree overlap = tree_overlaps_hard_reg_set (t, clobbered_regs);
2428 if (overlap)
2430 error ("asm-specifier for variable %qE conflicts with asm clobber list",
2431 DECL_NAME (overlap));
2433 /* Reset registerness to stop multiple errors emitted for a single
2434 variable. */
2435 DECL_REGISTER (overlap) = 0;
2436 return true;
2439 return false;
2442 /* Generate RTL for an asm statement with arguments.
2443 STRING is the instruction template.
2444 OUTPUTS is a list of output arguments (lvalues); INPUTS a list of inputs.
2445 Each output or input has an expression in the TREE_VALUE and
2446 a tree list in TREE_PURPOSE which in turn contains a constraint
2447 name in TREE_VALUE (or NULL_TREE) and a constraint string
2448 in TREE_PURPOSE.
2449 CLOBBERS is a list of STRING_CST nodes each naming a hard register
2450 that is clobbered by this insn.
2452 LABELS is a list of labels, and if LABELS is non-NULL, FALLTHRU_BB
2453 should be the fallthru basic block of the asm goto.
2455 Not all kinds of lvalue that may appear in OUTPUTS can be stored directly.
2456 Some elements of OUTPUTS may be replaced with trees representing temporary
2457 values. The caller should copy those temporary values to the originally
2458 specified lvalues.
2460 VOL nonzero means the insn is volatile; don't optimize it. */
2462 static void
2463 expand_asm_operands (tree string, tree outputs, tree inputs,
2464 tree clobbers, tree labels, basic_block fallthru_bb,
2465 int vol, location_t locus)
2467 rtvec argvec, constraintvec, labelvec;
2468 rtx body;
2469 int ninputs = list_length (inputs);
2470 int noutputs = list_length (outputs);
2471 int nlabels = list_length (labels);
2472 int ninout;
2473 int nclobbers;
2474 HARD_REG_SET clobbered_regs;
2475 int clobber_conflict_found = 0;
2476 tree tail;
2477 tree t;
2478 int i;
2479 /* Vector of RTX's of evaluated output operands. */
2480 rtx *output_rtx = XALLOCAVEC (rtx, noutputs);
2481 int *inout_opnum = XALLOCAVEC (int, noutputs);
2482 rtx *real_output_rtx = XALLOCAVEC (rtx, noutputs);
2483 machine_mode *inout_mode = XALLOCAVEC (machine_mode, noutputs);
2484 const char **constraints = XALLOCAVEC (const char *, noutputs + ninputs);
2485 int old_generating_concat_p = generating_concat_p;
2486 rtx_code_label *fallthru_label = NULL;
2488 /* An ASM with no outputs needs to be treated as volatile, for now. */
2489 if (noutputs == 0)
2490 vol = 1;
2492 if (! check_operand_nalternatives (outputs, inputs))
2493 return;
2495 string = resolve_asm_operand_names (string, outputs, inputs, labels);
2497 /* Collect constraints. */
2498 i = 0;
2499 for (t = outputs; t ; t = TREE_CHAIN (t), i++)
2500 constraints[i] = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
2501 for (t = inputs; t ; t = TREE_CHAIN (t), i++)
2502 constraints[i] = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
2504 /* Sometimes we wish to automatically clobber registers across an asm.
2505 Case in point is when the i386 backend moved from cc0 to a hard reg --
2506 maintaining source-level compatibility means automatically clobbering
2507 the flags register. */
2508 clobbers = targetm.md_asm_clobbers (outputs, inputs, clobbers);
2510 /* Count the number of meaningful clobbered registers, ignoring what
2511 we would ignore later. */
2512 nclobbers = 0;
2513 CLEAR_HARD_REG_SET (clobbered_regs);
2514 for (tail = clobbers; tail; tail = TREE_CHAIN (tail))
2516 const char *regname;
2517 int nregs;
2519 if (TREE_VALUE (tail) == error_mark_node)
2520 return;
2521 regname = TREE_STRING_POINTER (TREE_VALUE (tail));
2523 i = decode_reg_name_and_count (regname, &nregs);
2524 if (i == -4)
2525 ++nclobbers;
2526 else if (i == -2)
2527 error ("unknown register name %qs in %<asm%>", regname);
2529 /* Mark clobbered registers. */
2530 if (i >= 0)
2532 int reg;
2534 for (reg = i; reg < i + nregs; reg++)
2536 ++nclobbers;
2538 /* Clobbering the PIC register is an error. */
2539 if (reg == (int) PIC_OFFSET_TABLE_REGNUM)
2541 error ("PIC register clobbered by %qs in %<asm%>", regname);
2542 return;
2545 SET_HARD_REG_BIT (clobbered_regs, reg);
2550 /* First pass over inputs and outputs checks validity and sets
2551 mark_addressable if needed. */
2553 ninout = 0;
2554 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
2556 tree val = TREE_VALUE (tail);
2557 tree type = TREE_TYPE (val);
2558 const char *constraint;
2559 bool is_inout;
2560 bool allows_reg;
2561 bool allows_mem;
2563 /* If there's an erroneous arg, emit no insn. */
2564 if (type == error_mark_node)
2565 return;
2567 /* Try to parse the output constraint. If that fails, there's
2568 no point in going further. */
2569 constraint = constraints[i];
2570 if (!parse_output_constraint (&constraint, i, ninputs, noutputs,
2571 &allows_mem, &allows_reg, &is_inout))
2572 return;
2574 if (! allows_reg
2575 && (allows_mem
2576 || is_inout
2577 || (DECL_P (val)
2578 && REG_P (DECL_RTL (val))
2579 && GET_MODE (DECL_RTL (val)) != TYPE_MODE (type))))
2580 mark_addressable (val);
2582 if (is_inout)
2583 ninout++;
2586 ninputs += ninout;
2587 if (ninputs + noutputs > MAX_RECOG_OPERANDS)
2589 error ("more than %d operands in %<asm%>", MAX_RECOG_OPERANDS);
2590 return;
2593 for (i = 0, tail = inputs; tail; i++, tail = TREE_CHAIN (tail))
2595 bool allows_reg, allows_mem;
2596 const char *constraint;
2598 /* If there's an erroneous arg, emit no insn, because the ASM_INPUT
2599 would get VOIDmode and that could cause a crash in reload. */
2600 if (TREE_TYPE (TREE_VALUE (tail)) == error_mark_node)
2601 return;
2603 constraint = constraints[i + noutputs];
2604 if (! parse_input_constraint (&constraint, i, ninputs, noutputs, ninout,
2605 constraints, &allows_mem, &allows_reg))
2606 return;
2608 if (! allows_reg && allows_mem)
2609 mark_addressable (TREE_VALUE (tail));
2612 /* Second pass evaluates arguments. */
2614 /* Make sure stack is consistent for asm goto. */
2615 if (nlabels > 0)
2616 do_pending_stack_adjust ();
2618 ninout = 0;
2619 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
2621 tree val = TREE_VALUE (tail);
2622 tree type = TREE_TYPE (val);
2623 bool is_inout;
2624 bool allows_reg;
2625 bool allows_mem;
2626 rtx op;
2627 bool ok;
2629 ok = parse_output_constraint (&constraints[i], i, ninputs,
2630 noutputs, &allows_mem, &allows_reg,
2631 &is_inout);
2632 gcc_assert (ok);
2634 /* If an output operand is not a decl or indirect ref and our constraint
2635 allows a register, make a temporary to act as an intermediate.
2636 Make the asm insn write into that, then our caller will copy it to
2637 the real output operand. Likewise for promoted variables. */
2639 generating_concat_p = 0;
2641 real_output_rtx[i] = NULL_RTX;
2642 if ((TREE_CODE (val) == INDIRECT_REF
2643 && allows_mem)
2644 || (DECL_P (val)
2645 && (allows_mem || REG_P (DECL_RTL (val)))
2646 && ! (REG_P (DECL_RTL (val))
2647 && GET_MODE (DECL_RTL (val)) != TYPE_MODE (type)))
2648 || ! allows_reg
2649 || is_inout)
2651 op = expand_expr (val, NULL_RTX, VOIDmode,
2652 !allows_reg ? EXPAND_MEMORY : EXPAND_WRITE);
2653 if (MEM_P (op))
2654 op = validize_mem (op);
2656 if (! allows_reg && !MEM_P (op))
2657 error ("output number %d not directly addressable", i);
2658 if ((! allows_mem && MEM_P (op))
2659 || GET_CODE (op) == CONCAT)
2661 real_output_rtx[i] = op;
2662 op = gen_reg_rtx (GET_MODE (op));
2663 if (is_inout)
2664 emit_move_insn (op, real_output_rtx[i]);
2667 else
2669 op = assign_temp (type, 0, 1);
2670 op = validize_mem (op);
2671 if (!MEM_P (op) && TREE_CODE (TREE_VALUE (tail)) == SSA_NAME)
2672 set_reg_attrs_for_decl_rtl (SSA_NAME_VAR (TREE_VALUE (tail)), op);
2673 TREE_VALUE (tail) = make_tree (type, op);
2675 output_rtx[i] = op;
2677 generating_concat_p = old_generating_concat_p;
2679 if (is_inout)
2681 inout_mode[ninout] = TYPE_MODE (type);
2682 inout_opnum[ninout++] = i;
2685 if (tree_conflicts_with_clobbers_p (val, &clobbered_regs))
2686 clobber_conflict_found = 1;
2689 /* Make vectors for the expression-rtx, constraint strings,
2690 and named operands. */
2692 argvec = rtvec_alloc (ninputs);
2693 constraintvec = rtvec_alloc (ninputs);
2694 labelvec = rtvec_alloc (nlabels);
2696 body = gen_rtx_ASM_OPERANDS ((noutputs == 0 ? VOIDmode
2697 : GET_MODE (output_rtx[0])),
2698 ggc_strdup (TREE_STRING_POINTER (string)),
2699 empty_string, 0, argvec, constraintvec,
2700 labelvec, locus);
2702 MEM_VOLATILE_P (body) = vol;
2704 /* Eval the inputs and put them into ARGVEC.
2705 Put their constraints into ASM_INPUTs and store in CONSTRAINTS. */
2707 for (i = 0, tail = inputs; tail; tail = TREE_CHAIN (tail), ++i)
2709 bool allows_reg, allows_mem;
2710 const char *constraint;
2711 tree val, type;
2712 rtx op;
2713 bool ok;
2715 constraint = constraints[i + noutputs];
2716 ok = parse_input_constraint (&constraint, i, ninputs, noutputs, ninout,
2717 constraints, &allows_mem, &allows_reg);
2718 gcc_assert (ok);
2720 generating_concat_p = 0;
2722 val = TREE_VALUE (tail);
2723 type = TREE_TYPE (val);
2724 /* EXPAND_INITIALIZER will not generate code for valid initializer
2725 constants, but will still generate code for other types of operand.
2726 This is the behavior we want for constant constraints. */
2727 op = expand_expr (val, NULL_RTX, VOIDmode,
2728 allows_reg ? EXPAND_NORMAL
2729 : allows_mem ? EXPAND_MEMORY
2730 : EXPAND_INITIALIZER);
2732 /* Never pass a CONCAT to an ASM. */
2733 if (GET_CODE (op) == CONCAT)
2734 op = force_reg (GET_MODE (op), op);
2735 else if (MEM_P (op))
2736 op = validize_mem (op);
2738 if (asm_operand_ok (op, constraint, NULL) <= 0)
2740 if (allows_reg && TYPE_MODE (type) != BLKmode)
2741 op = force_reg (TYPE_MODE (type), op);
2742 else if (!allows_mem)
2743 warning (0, "asm operand %d probably doesn%'t match constraints",
2744 i + noutputs);
2745 else if (MEM_P (op))
2747 /* We won't recognize either volatile memory or memory
2748 with a queued address as available a memory_operand
2749 at this point. Ignore it: clearly this *is* a memory. */
2751 else
2752 gcc_unreachable ();
2755 generating_concat_p = old_generating_concat_p;
2756 ASM_OPERANDS_INPUT (body, i) = op;
2758 ASM_OPERANDS_INPUT_CONSTRAINT_EXP (body, i)
2759 = gen_rtx_ASM_INPUT_loc (TYPE_MODE (type),
2760 ggc_strdup (constraints[i + noutputs]),
2761 locus);
2763 if (tree_conflicts_with_clobbers_p (val, &clobbered_regs))
2764 clobber_conflict_found = 1;
2767 /* Protect all the operands from the queue now that they have all been
2768 evaluated. */
2770 generating_concat_p = 0;
2772 /* For in-out operands, copy output rtx to input rtx. */
2773 for (i = 0; i < ninout; i++)
2775 int j = inout_opnum[i];
2776 char buffer[16];
2778 ASM_OPERANDS_INPUT (body, ninputs - ninout + i)
2779 = output_rtx[j];
2781 sprintf (buffer, "%d", j);
2782 ASM_OPERANDS_INPUT_CONSTRAINT_EXP (body, ninputs - ninout + i)
2783 = gen_rtx_ASM_INPUT_loc (inout_mode[i], ggc_strdup (buffer), locus);
2786 /* Copy labels to the vector. */
2787 for (i = 0, tail = labels; i < nlabels; ++i, tail = TREE_CHAIN (tail))
2789 rtx r;
2790 /* If asm goto has any labels in the fallthru basic block, use
2791 a label that we emit immediately after the asm goto. Expansion
2792 may insert further instructions into the same basic block after
2793 asm goto and if we don't do this, insertion of instructions on
2794 the fallthru edge might misbehave. See PR58670. */
2795 if (fallthru_bb
2796 && label_to_block_fn (cfun, TREE_VALUE (tail)) == fallthru_bb)
2798 if (fallthru_label == NULL_RTX)
2799 fallthru_label = gen_label_rtx ();
2800 r = fallthru_label;
2802 else
2803 r = label_rtx (TREE_VALUE (tail));
2804 ASM_OPERANDS_LABEL (body, i) = gen_rtx_LABEL_REF (Pmode, r);
2807 generating_concat_p = old_generating_concat_p;
2809 /* Now, for each output, construct an rtx
2810 (set OUTPUT (asm_operands INSN OUTPUTCONSTRAINT OUTPUTNUMBER
2811 ARGVEC CONSTRAINTS OPNAMES))
2812 If there is more than one, put them inside a PARALLEL. */
2814 if (nlabels > 0 && nclobbers == 0)
2816 gcc_assert (noutputs == 0);
2817 emit_jump_insn (body);
2819 else if (noutputs == 0 && nclobbers == 0)
2821 /* No output operands: put in a raw ASM_OPERANDS rtx. */
2822 emit_insn (body);
2824 else if (noutputs == 1 && nclobbers == 0)
2826 ASM_OPERANDS_OUTPUT_CONSTRAINT (body) = ggc_strdup (constraints[0]);
2827 emit_insn (gen_rtx_SET (VOIDmode, output_rtx[0], body));
2829 else
2831 rtx obody = body;
2832 int num = noutputs;
2834 if (num == 0)
2835 num = 1;
2837 body = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (num + nclobbers));
2839 /* For each output operand, store a SET. */
2840 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
2842 XVECEXP (body, 0, i)
2843 = gen_rtx_SET (VOIDmode,
2844 output_rtx[i],
2845 gen_rtx_ASM_OPERANDS
2846 (GET_MODE (output_rtx[i]),
2847 ggc_strdup (TREE_STRING_POINTER (string)),
2848 ggc_strdup (constraints[i]),
2849 i, argvec, constraintvec, labelvec, locus));
2851 MEM_VOLATILE_P (SET_SRC (XVECEXP (body, 0, i))) = vol;
2854 /* If there are no outputs (but there are some clobbers)
2855 store the bare ASM_OPERANDS into the PARALLEL. */
2857 if (i == 0)
2858 XVECEXP (body, 0, i++) = obody;
2860 /* Store (clobber REG) for each clobbered register specified. */
2862 for (tail = clobbers; tail; tail = TREE_CHAIN (tail))
2864 const char *regname = TREE_STRING_POINTER (TREE_VALUE (tail));
2865 int reg, nregs;
2866 int j = decode_reg_name_and_count (regname, &nregs);
2867 rtx clobbered_reg;
2869 if (j < 0)
2871 if (j == -3) /* `cc', which is not a register */
2872 continue;
2874 if (j == -4) /* `memory', don't cache memory across asm */
2876 XVECEXP (body, 0, i++)
2877 = gen_rtx_CLOBBER (VOIDmode,
2878 gen_rtx_MEM
2879 (BLKmode,
2880 gen_rtx_SCRATCH (VOIDmode)));
2881 continue;
2884 /* Ignore unknown register, error already signaled. */
2885 continue;
2888 for (reg = j; reg < j + nregs; reg++)
2890 /* Use QImode since that's guaranteed to clobber just
2891 * one reg. */
2892 clobbered_reg = gen_rtx_REG (QImode, reg);
2894 /* Do sanity check for overlap between clobbers and
2895 respectively input and outputs that hasn't been
2896 handled. Such overlap should have been detected and
2897 reported above. */
2898 if (!clobber_conflict_found)
2900 int opno;
2902 /* We test the old body (obody) contents to avoid
2903 tripping over the under-construction body. */
2904 for (opno = 0; opno < noutputs; opno++)
2905 if (reg_overlap_mentioned_p (clobbered_reg,
2906 output_rtx[opno]))
2907 internal_error
2908 ("asm clobber conflict with output operand");
2910 for (opno = 0; opno < ninputs - ninout; opno++)
2911 if (reg_overlap_mentioned_p (clobbered_reg,
2912 ASM_OPERANDS_INPUT (obody,
2913 opno)))
2914 internal_error
2915 ("asm clobber conflict with input operand");
2918 XVECEXP (body, 0, i++)
2919 = gen_rtx_CLOBBER (VOIDmode, clobbered_reg);
2923 if (nlabels > 0)
2924 emit_jump_insn (body);
2925 else
2926 emit_insn (body);
2929 if (fallthru_label)
2930 emit_label (fallthru_label);
2932 /* For any outputs that needed reloading into registers, spill them
2933 back to where they belong. */
2934 for (i = 0; i < noutputs; ++i)
2935 if (real_output_rtx[i])
2936 emit_move_insn (real_output_rtx[i], output_rtx[i]);
2938 crtl->has_asm_statement = 1;
2939 free_temp_slots ();
2943 static void
2944 expand_asm_stmt (gimple stmt)
2946 int noutputs;
2947 tree outputs, tail, t;
2948 tree *o;
2949 size_t i, n;
2950 const char *s;
2951 tree str, out, in, cl, labels;
2952 location_t locus = gimple_location (stmt);
2953 basic_block fallthru_bb = NULL;
2955 /* Meh... convert the gimple asm operands into real tree lists.
2956 Eventually we should make all routines work on the vectors instead
2957 of relying on TREE_CHAIN. */
2958 out = NULL_TREE;
2959 n = gimple_asm_noutputs (stmt);
2960 if (n > 0)
2962 t = out = gimple_asm_output_op (stmt, 0);
2963 for (i = 1; i < n; i++)
2964 t = TREE_CHAIN (t) = gimple_asm_output_op (stmt, i);
2967 in = NULL_TREE;
2968 n = gimple_asm_ninputs (stmt);
2969 if (n > 0)
2971 t = in = gimple_asm_input_op (stmt, 0);
2972 for (i = 1; i < n; i++)
2973 t = TREE_CHAIN (t) = gimple_asm_input_op (stmt, i);
2976 cl = NULL_TREE;
2977 n = gimple_asm_nclobbers (stmt);
2978 if (n > 0)
2980 t = cl = gimple_asm_clobber_op (stmt, 0);
2981 for (i = 1; i < n; i++)
2982 t = TREE_CHAIN (t) = gimple_asm_clobber_op (stmt, i);
2985 labels = NULL_TREE;
2986 n = gimple_asm_nlabels (stmt);
2987 if (n > 0)
2989 edge fallthru = find_fallthru_edge (gimple_bb (stmt)->succs);
2990 if (fallthru)
2991 fallthru_bb = fallthru->dest;
2992 t = labels = gimple_asm_label_op (stmt, 0);
2993 for (i = 1; i < n; i++)
2994 t = TREE_CHAIN (t) = gimple_asm_label_op (stmt, i);
2997 s = gimple_asm_string (stmt);
2998 str = build_string (strlen (s), s);
3000 if (gimple_asm_input_p (stmt))
3002 expand_asm_loc (str, gimple_asm_volatile_p (stmt), locus);
3003 return;
3006 outputs = out;
3007 noutputs = gimple_asm_noutputs (stmt);
3008 /* o[I] is the place that output number I should be written. */
3009 o = (tree *) alloca (noutputs * sizeof (tree));
3011 /* Record the contents of OUTPUTS before it is modified. */
3012 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
3013 o[i] = TREE_VALUE (tail);
3015 /* Generate the ASM_OPERANDS insn; store into the TREE_VALUEs of
3016 OUTPUTS some trees for where the values were actually stored. */
3017 expand_asm_operands (str, outputs, in, cl, labels, fallthru_bb,
3018 gimple_asm_volatile_p (stmt), locus);
3020 /* Copy all the intermediate outputs into the specified outputs. */
3021 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
3023 if (o[i] != TREE_VALUE (tail))
3025 expand_assignment (o[i], TREE_VALUE (tail), false);
3026 free_temp_slots ();
3028 /* Restore the original value so that it's correct the next
3029 time we expand this function. */
3030 TREE_VALUE (tail) = o[i];
3035 /* Emit code to jump to the address
3036 specified by the pointer expression EXP. */
3038 static void
3039 expand_computed_goto (tree exp)
3041 rtx x = expand_normal (exp);
3043 x = convert_memory_address (Pmode, x);
3045 do_pending_stack_adjust ();
3046 emit_indirect_jump (x);
3049 /* Generate RTL code for a `goto' statement with target label LABEL.
3050 LABEL should be a LABEL_DECL tree node that was or will later be
3051 defined with `expand_label'. */
3053 static void
3054 expand_goto (tree label)
3056 #ifdef ENABLE_CHECKING
3057 /* Check for a nonlocal goto to a containing function. Should have
3058 gotten translated to __builtin_nonlocal_goto. */
3059 tree context = decl_function_context (label);
3060 gcc_assert (!context || context == current_function_decl);
3061 #endif
3063 emit_jump (label_rtx (label));
3066 /* Output a return with no value. */
3068 static void
3069 expand_null_return_1 (void)
3071 clear_pending_stack_adjust ();
3072 do_pending_stack_adjust ();
3073 emit_jump (return_label);
3076 /* Generate RTL to return from the current function, with no value.
3077 (That is, we do not do anything about returning any value.) */
3079 void
3080 expand_null_return (void)
3082 /* If this function was declared to return a value, but we
3083 didn't, clobber the return registers so that they are not
3084 propagated live to the rest of the function. */
3085 clobber_return_register ();
3087 expand_null_return_1 ();
3090 /* Generate RTL to return from the current function, with value VAL. */
3092 static void
3093 expand_value_return (rtx val)
3095 /* Copy the value to the return location unless it's already there. */
3097 tree decl = DECL_RESULT (current_function_decl);
3098 rtx return_reg = DECL_RTL (decl);
3099 if (return_reg != val)
3101 tree funtype = TREE_TYPE (current_function_decl);
3102 tree type = TREE_TYPE (decl);
3103 int unsignedp = TYPE_UNSIGNED (type);
3104 machine_mode old_mode = DECL_MODE (decl);
3105 machine_mode mode;
3106 if (DECL_BY_REFERENCE (decl))
3107 mode = promote_function_mode (type, old_mode, &unsignedp, funtype, 2);
3108 else
3109 mode = promote_function_mode (type, old_mode, &unsignedp, funtype, 1);
3111 if (mode != old_mode)
3112 val = convert_modes (mode, old_mode, val, unsignedp);
3114 if (GET_CODE (return_reg) == PARALLEL)
3115 emit_group_load (return_reg, val, type, int_size_in_bytes (type));
3116 else
3117 emit_move_insn (return_reg, val);
3120 expand_null_return_1 ();
3123 /* Generate RTL to evaluate the expression RETVAL and return it
3124 from the current function. */
3126 static void
3127 expand_return (tree retval)
3129 rtx result_rtl;
3130 rtx val = 0;
3131 tree retval_rhs;
3133 /* If function wants no value, give it none. */
3134 if (TREE_CODE (TREE_TYPE (TREE_TYPE (current_function_decl))) == VOID_TYPE)
3136 expand_normal (retval);
3137 expand_null_return ();
3138 return;
3141 if (retval == error_mark_node)
3143 /* Treat this like a return of no value from a function that
3144 returns a value. */
3145 expand_null_return ();
3146 return;
3148 else if ((TREE_CODE (retval) == MODIFY_EXPR
3149 || TREE_CODE (retval) == INIT_EXPR)
3150 && TREE_CODE (TREE_OPERAND (retval, 0)) == RESULT_DECL)
3151 retval_rhs = TREE_OPERAND (retval, 1);
3152 else
3153 retval_rhs = retval;
3155 result_rtl = DECL_RTL (DECL_RESULT (current_function_decl));
3157 /* If we are returning the RESULT_DECL, then the value has already
3158 been stored into it, so we don't have to do anything special. */
3159 if (TREE_CODE (retval_rhs) == RESULT_DECL)
3160 expand_value_return (result_rtl);
3162 /* If the result is an aggregate that is being returned in one (or more)
3163 registers, load the registers here. */
3165 else if (retval_rhs != 0
3166 && TYPE_MODE (TREE_TYPE (retval_rhs)) == BLKmode
3167 && REG_P (result_rtl))
3169 val = copy_blkmode_to_reg (GET_MODE (result_rtl), retval_rhs);
3170 if (val)
3172 /* Use the mode of the result value on the return register. */
3173 PUT_MODE (result_rtl, GET_MODE (val));
3174 expand_value_return (val);
3176 else
3177 expand_null_return ();
3179 else if (retval_rhs != 0
3180 && !VOID_TYPE_P (TREE_TYPE (retval_rhs))
3181 && (REG_P (result_rtl)
3182 || (GET_CODE (result_rtl) == PARALLEL)))
3184 /* Compute the return value into a temporary (usually a pseudo reg). */
3186 = assign_temp (TREE_TYPE (DECL_RESULT (current_function_decl)), 0, 1);
3187 val = expand_expr (retval_rhs, val, GET_MODE (val), EXPAND_NORMAL);
3188 val = force_not_mem (val);
3189 expand_value_return (val);
3191 else
3193 /* No hard reg used; calculate value into hard return reg. */
3194 expand_expr (retval, const0_rtx, VOIDmode, EXPAND_NORMAL);
3195 expand_value_return (result_rtl);
3199 /* A subroutine of expand_gimple_stmt, expanding one gimple statement
3200 STMT that doesn't require special handling for outgoing edges. That
3201 is no tailcalls and no GIMPLE_COND. */
3203 static void
3204 expand_gimple_stmt_1 (gimple stmt)
3206 tree op0;
3208 set_curr_insn_location (gimple_location (stmt));
3210 switch (gimple_code (stmt))
3212 case GIMPLE_GOTO:
3213 op0 = gimple_goto_dest (stmt);
3214 if (TREE_CODE (op0) == LABEL_DECL)
3215 expand_goto (op0);
3216 else
3217 expand_computed_goto (op0);
3218 break;
3219 case GIMPLE_LABEL:
3220 expand_label (gimple_label_label (stmt));
3221 break;
3222 case GIMPLE_NOP:
3223 case GIMPLE_PREDICT:
3224 break;
3225 case GIMPLE_SWITCH:
3226 expand_case (stmt);
3227 break;
3228 case GIMPLE_ASM:
3229 expand_asm_stmt (stmt);
3230 break;
3231 case GIMPLE_CALL:
3232 expand_call_stmt (stmt);
3233 break;
3235 case GIMPLE_RETURN:
3236 op0 = gimple_return_retval (stmt);
3238 if (op0 && op0 != error_mark_node)
3240 tree result = DECL_RESULT (current_function_decl);
3242 /* If we are not returning the current function's RESULT_DECL,
3243 build an assignment to it. */
3244 if (op0 != result)
3246 /* I believe that a function's RESULT_DECL is unique. */
3247 gcc_assert (TREE_CODE (op0) != RESULT_DECL);
3249 /* ??? We'd like to use simply expand_assignment here,
3250 but this fails if the value is of BLKmode but the return
3251 decl is a register. expand_return has special handling
3252 for this combination, which eventually should move
3253 to common code. See comments there. Until then, let's
3254 build a modify expression :-/ */
3255 op0 = build2 (MODIFY_EXPR, TREE_TYPE (result),
3256 result, op0);
3259 if (!op0)
3260 expand_null_return ();
3261 else
3262 expand_return (op0);
3263 break;
3265 case GIMPLE_ASSIGN:
3267 tree lhs = gimple_assign_lhs (stmt);
3269 /* Tree expand used to fiddle with |= and &= of two bitfield
3270 COMPONENT_REFs here. This can't happen with gimple, the LHS
3271 of binary assigns must be a gimple reg. */
3273 if (TREE_CODE (lhs) != SSA_NAME
3274 || get_gimple_rhs_class (gimple_expr_code (stmt))
3275 == GIMPLE_SINGLE_RHS)
3277 tree rhs = gimple_assign_rhs1 (stmt);
3278 gcc_assert (get_gimple_rhs_class (gimple_expr_code (stmt))
3279 == GIMPLE_SINGLE_RHS);
3280 if (gimple_has_location (stmt) && CAN_HAVE_LOCATION_P (rhs))
3281 SET_EXPR_LOCATION (rhs, gimple_location (stmt));
3282 if (TREE_CLOBBER_P (rhs))
3283 /* This is a clobber to mark the going out of scope for
3284 this LHS. */
3286 else
3287 expand_assignment (lhs, rhs,
3288 gimple_assign_nontemporal_move_p (stmt));
3290 else
3292 rtx target, temp;
3293 bool nontemporal = gimple_assign_nontemporal_move_p (stmt);
3294 struct separate_ops ops;
3295 bool promoted = false;
3297 target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
3298 if (GET_CODE (target) == SUBREG && SUBREG_PROMOTED_VAR_P (target))
3299 promoted = true;
3301 ops.code = gimple_assign_rhs_code (stmt);
3302 ops.type = TREE_TYPE (lhs);
3303 switch (get_gimple_rhs_class (gimple_expr_code (stmt)))
3305 case GIMPLE_TERNARY_RHS:
3306 ops.op2 = gimple_assign_rhs3 (stmt);
3307 /* Fallthru */
3308 case GIMPLE_BINARY_RHS:
3309 ops.op1 = gimple_assign_rhs2 (stmt);
3310 /* Fallthru */
3311 case GIMPLE_UNARY_RHS:
3312 ops.op0 = gimple_assign_rhs1 (stmt);
3313 break;
3314 default:
3315 gcc_unreachable ();
3317 ops.location = gimple_location (stmt);
3319 /* If we want to use a nontemporal store, force the value to
3320 register first. If we store into a promoted register,
3321 don't directly expand to target. */
3322 temp = nontemporal || promoted ? NULL_RTX : target;
3323 temp = expand_expr_real_2 (&ops, temp, GET_MODE (target),
3324 EXPAND_NORMAL);
3326 if (temp == target)
3328 else if (promoted)
3330 int unsignedp = SUBREG_PROMOTED_SIGN (target);
3331 /* If TEMP is a VOIDmode constant, use convert_modes to make
3332 sure that we properly convert it. */
3333 if (CONSTANT_P (temp) && GET_MODE (temp) == VOIDmode)
3335 temp = convert_modes (GET_MODE (target),
3336 TYPE_MODE (ops.type),
3337 temp, unsignedp);
3338 temp = convert_modes (GET_MODE (SUBREG_REG (target)),
3339 GET_MODE (target), temp, unsignedp);
3342 convert_move (SUBREG_REG (target), temp, unsignedp);
3344 else if (nontemporal && emit_storent_insn (target, temp))
3346 else
3348 temp = force_operand (temp, target);
3349 if (temp != target)
3350 emit_move_insn (target, temp);
3354 break;
3356 default:
3357 gcc_unreachable ();
3361 /* Expand one gimple statement STMT and return the last RTL instruction
3362 before any of the newly generated ones.
3364 In addition to generating the necessary RTL instructions this also
3365 sets REG_EH_REGION notes if necessary and sets the current source
3366 location for diagnostics. */
3368 static rtx_insn *
3369 expand_gimple_stmt (gimple stmt)
3371 location_t saved_location = input_location;
3372 rtx_insn *last = get_last_insn ();
3373 int lp_nr;
3375 gcc_assert (cfun);
3377 /* We need to save and restore the current source location so that errors
3378 discovered during expansion are emitted with the right location. But
3379 it would be better if the diagnostic routines used the source location
3380 embedded in the tree nodes rather than globals. */
3381 if (gimple_has_location (stmt))
3382 input_location = gimple_location (stmt);
3384 expand_gimple_stmt_1 (stmt);
3386 /* Free any temporaries used to evaluate this statement. */
3387 free_temp_slots ();
3389 input_location = saved_location;
3391 /* Mark all insns that may trap. */
3392 lp_nr = lookup_stmt_eh_lp (stmt);
3393 if (lp_nr)
3395 rtx_insn *insn;
3396 for (insn = next_real_insn (last); insn;
3397 insn = next_real_insn (insn))
3399 if (! find_reg_note (insn, REG_EH_REGION, NULL_RTX)
3400 /* If we want exceptions for non-call insns, any
3401 may_trap_p instruction may throw. */
3402 && GET_CODE (PATTERN (insn)) != CLOBBER
3403 && GET_CODE (PATTERN (insn)) != USE
3404 && insn_could_throw_p (insn))
3405 make_reg_eh_region_note (insn, 0, lp_nr);
3409 return last;
3412 /* A subroutine of expand_gimple_basic_block. Expand one GIMPLE_CALL
3413 that has CALL_EXPR_TAILCALL set. Returns non-null if we actually
3414 generated a tail call (something that might be denied by the ABI
3415 rules governing the call; see calls.c).
3417 Sets CAN_FALLTHRU if we generated a *conditional* tail call, and
3418 can still reach the rest of BB. The case here is __builtin_sqrt,
3419 where the NaN result goes through the external function (with a
3420 tailcall) and the normal result happens via a sqrt instruction. */
3422 static basic_block
3423 expand_gimple_tailcall (basic_block bb, gimple stmt, bool *can_fallthru)
3425 rtx_insn *last2, *last;
3426 edge e;
3427 edge_iterator ei;
3428 int probability;
3429 gcov_type count;
3431 last2 = last = expand_gimple_stmt (stmt);
3433 for (last = NEXT_INSN (last); last; last = NEXT_INSN (last))
3434 if (CALL_P (last) && SIBLING_CALL_P (last))
3435 goto found;
3437 maybe_dump_rtl_for_gimple_stmt (stmt, last2);
3439 *can_fallthru = true;
3440 return NULL;
3442 found:
3443 /* ??? Wouldn't it be better to just reset any pending stack adjust?
3444 Any instructions emitted here are about to be deleted. */
3445 do_pending_stack_adjust ();
3447 /* Remove any non-eh, non-abnormal edges that don't go to exit. */
3448 /* ??? I.e. the fallthrough edge. HOWEVER! If there were to be
3449 EH or abnormal edges, we shouldn't have created a tail call in
3450 the first place. So it seems to me we should just be removing
3451 all edges here, or redirecting the existing fallthru edge to
3452 the exit block. */
3454 probability = 0;
3455 count = 0;
3457 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
3459 if (!(e->flags & (EDGE_ABNORMAL | EDGE_EH)))
3461 if (e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun))
3463 e->dest->count -= e->count;
3464 e->dest->frequency -= EDGE_FREQUENCY (e);
3465 if (e->dest->count < 0)
3466 e->dest->count = 0;
3467 if (e->dest->frequency < 0)
3468 e->dest->frequency = 0;
3470 count += e->count;
3471 probability += e->probability;
3472 remove_edge (e);
3474 else
3475 ei_next (&ei);
3478 /* This is somewhat ugly: the call_expr expander often emits instructions
3479 after the sibcall (to perform the function return). These confuse the
3480 find_many_sub_basic_blocks code, so we need to get rid of these. */
3481 last = NEXT_INSN (last);
3482 gcc_assert (BARRIER_P (last));
3484 *can_fallthru = false;
3485 while (NEXT_INSN (last))
3487 /* For instance an sqrt builtin expander expands if with
3488 sibcall in the then and label for `else`. */
3489 if (LABEL_P (NEXT_INSN (last)))
3491 *can_fallthru = true;
3492 break;
3494 delete_insn (NEXT_INSN (last));
3497 e = make_edge (bb, EXIT_BLOCK_PTR_FOR_FN (cfun), EDGE_ABNORMAL
3498 | EDGE_SIBCALL);
3499 e->probability += probability;
3500 e->count += count;
3501 BB_END (bb) = last;
3502 update_bb_for_insn (bb);
3504 if (NEXT_INSN (last))
3506 bb = create_basic_block (NEXT_INSN (last), get_last_insn (), bb);
3508 last = BB_END (bb);
3509 if (BARRIER_P (last))
3510 BB_END (bb) = PREV_INSN (last);
3513 maybe_dump_rtl_for_gimple_stmt (stmt, last2);
3515 return bb;
3518 /* Return the difference between the floor and the truncated result of
3519 a signed division by OP1 with remainder MOD. */
3520 static rtx
3521 floor_sdiv_adjust (machine_mode mode, rtx mod, rtx op1)
3523 /* (mod != 0 ? (op1 / mod < 0 ? -1 : 0) : 0) */
3524 return gen_rtx_IF_THEN_ELSE
3525 (mode, gen_rtx_NE (BImode, mod, const0_rtx),
3526 gen_rtx_IF_THEN_ELSE
3527 (mode, gen_rtx_LT (BImode,
3528 gen_rtx_DIV (mode, op1, mod),
3529 const0_rtx),
3530 constm1_rtx, const0_rtx),
3531 const0_rtx);
3534 /* Return the difference between the ceil and the truncated result of
3535 a signed division by OP1 with remainder MOD. */
3536 static rtx
3537 ceil_sdiv_adjust (machine_mode mode, rtx mod, rtx op1)
3539 /* (mod != 0 ? (op1 / mod > 0 ? 1 : 0) : 0) */
3540 return gen_rtx_IF_THEN_ELSE
3541 (mode, gen_rtx_NE (BImode, mod, const0_rtx),
3542 gen_rtx_IF_THEN_ELSE
3543 (mode, gen_rtx_GT (BImode,
3544 gen_rtx_DIV (mode, op1, mod),
3545 const0_rtx),
3546 const1_rtx, const0_rtx),
3547 const0_rtx);
3550 /* Return the difference between the ceil and the truncated result of
3551 an unsigned division by OP1 with remainder MOD. */
3552 static rtx
3553 ceil_udiv_adjust (machine_mode mode, rtx mod, rtx op1 ATTRIBUTE_UNUSED)
3555 /* (mod != 0 ? 1 : 0) */
3556 return gen_rtx_IF_THEN_ELSE
3557 (mode, gen_rtx_NE (BImode, mod, const0_rtx),
3558 const1_rtx, const0_rtx);
3561 /* Return the difference between the rounded and the truncated result
3562 of a signed division by OP1 with remainder MOD. Halfway cases are
3563 rounded away from zero, rather than to the nearest even number. */
3564 static rtx
3565 round_sdiv_adjust (machine_mode mode, rtx mod, rtx op1)
3567 /* (abs (mod) >= abs (op1) - abs (mod)
3568 ? (op1 / mod > 0 ? 1 : -1)
3569 : 0) */
3570 return gen_rtx_IF_THEN_ELSE
3571 (mode, gen_rtx_GE (BImode, gen_rtx_ABS (mode, mod),
3572 gen_rtx_MINUS (mode,
3573 gen_rtx_ABS (mode, op1),
3574 gen_rtx_ABS (mode, mod))),
3575 gen_rtx_IF_THEN_ELSE
3576 (mode, gen_rtx_GT (BImode,
3577 gen_rtx_DIV (mode, op1, mod),
3578 const0_rtx),
3579 const1_rtx, constm1_rtx),
3580 const0_rtx);
3583 /* Return the difference between the rounded and the truncated result
3584 of a unsigned division by OP1 with remainder MOD. Halfway cases
3585 are rounded away from zero, rather than to the nearest even
3586 number. */
3587 static rtx
3588 round_udiv_adjust (machine_mode mode, rtx mod, rtx op1)
3590 /* (mod >= op1 - mod ? 1 : 0) */
3591 return gen_rtx_IF_THEN_ELSE
3592 (mode, gen_rtx_GE (BImode, mod,
3593 gen_rtx_MINUS (mode, op1, mod)),
3594 const1_rtx, const0_rtx);
3597 /* Convert X to MODE, that must be Pmode or ptr_mode, without emitting
3598 any rtl. */
3600 static rtx
3601 convert_debug_memory_address (machine_mode mode, rtx x,
3602 addr_space_t as)
3604 machine_mode xmode = GET_MODE (x);
3606 #ifndef POINTERS_EXTEND_UNSIGNED
3607 gcc_assert (mode == Pmode
3608 || mode == targetm.addr_space.address_mode (as));
3609 gcc_assert (xmode == mode || xmode == VOIDmode);
3610 #else
3611 rtx temp;
3613 gcc_assert (targetm.addr_space.valid_pointer_mode (mode, as));
3615 if (GET_MODE (x) == mode || GET_MODE (x) == VOIDmode)
3616 return x;
3618 if (GET_MODE_PRECISION (mode) < GET_MODE_PRECISION (xmode))
3619 x = simplify_gen_subreg (mode, x, xmode,
3620 subreg_lowpart_offset
3621 (mode, xmode));
3622 else if (POINTERS_EXTEND_UNSIGNED > 0)
3623 x = gen_rtx_ZERO_EXTEND (mode, x);
3624 else if (!POINTERS_EXTEND_UNSIGNED)
3625 x = gen_rtx_SIGN_EXTEND (mode, x);
3626 else
3628 switch (GET_CODE (x))
3630 case SUBREG:
3631 if ((SUBREG_PROMOTED_VAR_P (x)
3632 || (REG_P (SUBREG_REG (x)) && REG_POINTER (SUBREG_REG (x)))
3633 || (GET_CODE (SUBREG_REG (x)) == PLUS
3634 && REG_P (XEXP (SUBREG_REG (x), 0))
3635 && REG_POINTER (XEXP (SUBREG_REG (x), 0))
3636 && CONST_INT_P (XEXP (SUBREG_REG (x), 1))))
3637 && GET_MODE (SUBREG_REG (x)) == mode)
3638 return SUBREG_REG (x);
3639 break;
3640 case LABEL_REF:
3641 temp = gen_rtx_LABEL_REF (mode, LABEL_REF_LABEL (x));
3642 LABEL_REF_NONLOCAL_P (temp) = LABEL_REF_NONLOCAL_P (x);
3643 return temp;
3644 case SYMBOL_REF:
3645 temp = shallow_copy_rtx (x);
3646 PUT_MODE (temp, mode);
3647 return temp;
3648 case CONST:
3649 temp = convert_debug_memory_address (mode, XEXP (x, 0), as);
3650 if (temp)
3651 temp = gen_rtx_CONST (mode, temp);
3652 return temp;
3653 case PLUS:
3654 case MINUS:
3655 if (CONST_INT_P (XEXP (x, 1)))
3657 temp = convert_debug_memory_address (mode, XEXP (x, 0), as);
3658 if (temp)
3659 return gen_rtx_fmt_ee (GET_CODE (x), mode, temp, XEXP (x, 1));
3661 break;
3662 default:
3663 break;
3665 /* Don't know how to express ptr_extend as operation in debug info. */
3666 return NULL;
3668 #endif /* POINTERS_EXTEND_UNSIGNED */
3670 return x;
3673 /* Return an RTX equivalent to the value of the parameter DECL. */
3675 static rtx
3676 expand_debug_parm_decl (tree decl)
3678 rtx incoming = DECL_INCOMING_RTL (decl);
3680 if (incoming
3681 && GET_MODE (incoming) != BLKmode
3682 && ((REG_P (incoming) && HARD_REGISTER_P (incoming))
3683 || (MEM_P (incoming)
3684 && REG_P (XEXP (incoming, 0))
3685 && HARD_REGISTER_P (XEXP (incoming, 0)))))
3687 rtx rtl = gen_rtx_ENTRY_VALUE (GET_MODE (incoming));
3689 #ifdef HAVE_window_save
3690 /* DECL_INCOMING_RTL uses the INCOMING_REGNO of parameter registers.
3691 If the target machine has an explicit window save instruction, the
3692 actual entry value is the corresponding OUTGOING_REGNO instead. */
3693 if (REG_P (incoming)
3694 && OUTGOING_REGNO (REGNO (incoming)) != REGNO (incoming))
3695 incoming
3696 = gen_rtx_REG_offset (incoming, GET_MODE (incoming),
3697 OUTGOING_REGNO (REGNO (incoming)), 0);
3698 else if (MEM_P (incoming))
3700 rtx reg = XEXP (incoming, 0);
3701 if (OUTGOING_REGNO (REGNO (reg)) != REGNO (reg))
3703 reg = gen_raw_REG (GET_MODE (reg), OUTGOING_REGNO (REGNO (reg)));
3704 incoming = replace_equiv_address_nv (incoming, reg);
3706 else
3707 incoming = copy_rtx (incoming);
3709 #endif
3711 ENTRY_VALUE_EXP (rtl) = incoming;
3712 return rtl;
3715 if (incoming
3716 && GET_MODE (incoming) != BLKmode
3717 && !TREE_ADDRESSABLE (decl)
3718 && MEM_P (incoming)
3719 && (XEXP (incoming, 0) == virtual_incoming_args_rtx
3720 || (GET_CODE (XEXP (incoming, 0)) == PLUS
3721 && XEXP (XEXP (incoming, 0), 0) == virtual_incoming_args_rtx
3722 && CONST_INT_P (XEXP (XEXP (incoming, 0), 1)))))
3723 return copy_rtx (incoming);
3725 return NULL_RTX;
3728 /* Return an RTX equivalent to the value of the tree expression EXP. */
3730 static rtx
3731 expand_debug_expr (tree exp)
3733 rtx op0 = NULL_RTX, op1 = NULL_RTX, op2 = NULL_RTX;
3734 machine_mode mode = TYPE_MODE (TREE_TYPE (exp));
3735 machine_mode inner_mode = VOIDmode;
3736 int unsignedp = TYPE_UNSIGNED (TREE_TYPE (exp));
3737 addr_space_t as;
3739 switch (TREE_CODE_CLASS (TREE_CODE (exp)))
3741 case tcc_expression:
3742 switch (TREE_CODE (exp))
3744 case COND_EXPR:
3745 case DOT_PROD_EXPR:
3746 case SAD_EXPR:
3747 case WIDEN_MULT_PLUS_EXPR:
3748 case WIDEN_MULT_MINUS_EXPR:
3749 case FMA_EXPR:
3750 goto ternary;
3752 case TRUTH_ANDIF_EXPR:
3753 case TRUTH_ORIF_EXPR:
3754 case TRUTH_AND_EXPR:
3755 case TRUTH_OR_EXPR:
3756 case TRUTH_XOR_EXPR:
3757 goto binary;
3759 case TRUTH_NOT_EXPR:
3760 goto unary;
3762 default:
3763 break;
3765 break;
3767 ternary:
3768 op2 = expand_debug_expr (TREE_OPERAND (exp, 2));
3769 if (!op2)
3770 return NULL_RTX;
3771 /* Fall through. */
3773 binary:
3774 case tcc_binary:
3775 case tcc_comparison:
3776 op1 = expand_debug_expr (TREE_OPERAND (exp, 1));
3777 if (!op1)
3778 return NULL_RTX;
3779 /* Fall through. */
3781 unary:
3782 case tcc_unary:
3783 inner_mode = TYPE_MODE (TREE_TYPE (TREE_OPERAND (exp, 0)));
3784 op0 = expand_debug_expr (TREE_OPERAND (exp, 0));
3785 if (!op0)
3786 return NULL_RTX;
3787 break;
3789 case tcc_type:
3790 case tcc_statement:
3791 gcc_unreachable ();
3793 case tcc_constant:
3794 case tcc_exceptional:
3795 case tcc_declaration:
3796 case tcc_reference:
3797 case tcc_vl_exp:
3798 break;
3801 switch (TREE_CODE (exp))
3803 case STRING_CST:
3804 if (!lookup_constant_def (exp))
3806 if (strlen (TREE_STRING_POINTER (exp)) + 1
3807 != (size_t) TREE_STRING_LENGTH (exp))
3808 return NULL_RTX;
3809 op0 = gen_rtx_CONST_STRING (Pmode, TREE_STRING_POINTER (exp));
3810 op0 = gen_rtx_MEM (BLKmode, op0);
3811 set_mem_attributes (op0, exp, 0);
3812 return op0;
3814 /* Fall through... */
3816 case INTEGER_CST:
3817 case REAL_CST:
3818 case FIXED_CST:
3819 op0 = expand_expr (exp, NULL_RTX, mode, EXPAND_INITIALIZER);
3820 return op0;
3822 case COMPLEX_CST:
3823 gcc_assert (COMPLEX_MODE_P (mode));
3824 op0 = expand_debug_expr (TREE_REALPART (exp));
3825 op1 = expand_debug_expr (TREE_IMAGPART (exp));
3826 return gen_rtx_CONCAT (mode, op0, op1);
3828 case DEBUG_EXPR_DECL:
3829 op0 = DECL_RTL_IF_SET (exp);
3831 if (op0)
3832 return op0;
3834 op0 = gen_rtx_DEBUG_EXPR (mode);
3835 DEBUG_EXPR_TREE_DECL (op0) = exp;
3836 SET_DECL_RTL (exp, op0);
3838 return op0;
3840 case VAR_DECL:
3841 case PARM_DECL:
3842 case FUNCTION_DECL:
3843 case LABEL_DECL:
3844 case CONST_DECL:
3845 case RESULT_DECL:
3846 op0 = DECL_RTL_IF_SET (exp);
3848 /* This decl was probably optimized away. */
3849 if (!op0)
3851 if (TREE_CODE (exp) != VAR_DECL
3852 || DECL_EXTERNAL (exp)
3853 || !TREE_STATIC (exp)
3854 || !DECL_NAME (exp)
3855 || DECL_HARD_REGISTER (exp)
3856 || DECL_IN_CONSTANT_POOL (exp)
3857 || mode == VOIDmode)
3858 return NULL;
3860 op0 = make_decl_rtl_for_debug (exp);
3861 if (!MEM_P (op0)
3862 || GET_CODE (XEXP (op0, 0)) != SYMBOL_REF
3863 || SYMBOL_REF_DECL (XEXP (op0, 0)) != exp)
3864 return NULL;
3866 else
3867 op0 = copy_rtx (op0);
3869 if (GET_MODE (op0) == BLKmode
3870 /* If op0 is not BLKmode, but BLKmode is, adjust_mode
3871 below would ICE. While it is likely a FE bug,
3872 try to be robust here. See PR43166. */
3873 || mode == BLKmode
3874 || (mode == VOIDmode && GET_MODE (op0) != VOIDmode))
3876 gcc_assert (MEM_P (op0));
3877 op0 = adjust_address_nv (op0, mode, 0);
3878 return op0;
3881 /* Fall through. */
3883 adjust_mode:
3884 case PAREN_EXPR:
3885 case NOP_EXPR:
3886 case CONVERT_EXPR:
3888 inner_mode = GET_MODE (op0);
3890 if (mode == inner_mode)
3891 return op0;
3893 if (inner_mode == VOIDmode)
3895 if (TREE_CODE (exp) == SSA_NAME)
3896 inner_mode = TYPE_MODE (TREE_TYPE (exp));
3897 else
3898 inner_mode = TYPE_MODE (TREE_TYPE (TREE_OPERAND (exp, 0)));
3899 if (mode == inner_mode)
3900 return op0;
3903 if (FLOAT_MODE_P (mode) && FLOAT_MODE_P (inner_mode))
3905 if (GET_MODE_BITSIZE (mode) == GET_MODE_BITSIZE (inner_mode))
3906 op0 = simplify_gen_subreg (mode, op0, inner_mode, 0);
3907 else if (GET_MODE_BITSIZE (mode) < GET_MODE_BITSIZE (inner_mode))
3908 op0 = simplify_gen_unary (FLOAT_TRUNCATE, mode, op0, inner_mode);
3909 else
3910 op0 = simplify_gen_unary (FLOAT_EXTEND, mode, op0, inner_mode);
3912 else if (FLOAT_MODE_P (mode))
3914 gcc_assert (TREE_CODE (exp) != SSA_NAME);
3915 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp, 0))))
3916 op0 = simplify_gen_unary (UNSIGNED_FLOAT, mode, op0, inner_mode);
3917 else
3918 op0 = simplify_gen_unary (FLOAT, mode, op0, inner_mode);
3920 else if (FLOAT_MODE_P (inner_mode))
3922 if (unsignedp)
3923 op0 = simplify_gen_unary (UNSIGNED_FIX, mode, op0, inner_mode);
3924 else
3925 op0 = simplify_gen_unary (FIX, mode, op0, inner_mode);
3927 else if (CONSTANT_P (op0)
3928 || GET_MODE_PRECISION (mode) <= GET_MODE_PRECISION (inner_mode))
3929 op0 = simplify_gen_subreg (mode, op0, inner_mode,
3930 subreg_lowpart_offset (mode,
3931 inner_mode));
3932 else if (TREE_CODE_CLASS (TREE_CODE (exp)) == tcc_unary
3933 ? TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp, 0)))
3934 : unsignedp)
3935 op0 = simplify_gen_unary (ZERO_EXTEND, mode, op0, inner_mode);
3936 else
3937 op0 = simplify_gen_unary (SIGN_EXTEND, mode, op0, inner_mode);
3939 return op0;
3942 case MEM_REF:
3943 if (!is_gimple_mem_ref_addr (TREE_OPERAND (exp, 0)))
3945 tree newexp = fold_binary (MEM_REF, TREE_TYPE (exp),
3946 TREE_OPERAND (exp, 0),
3947 TREE_OPERAND (exp, 1));
3948 if (newexp)
3949 return expand_debug_expr (newexp);
3951 /* FALLTHROUGH */
3952 case INDIRECT_REF:
3953 inner_mode = TYPE_MODE (TREE_TYPE (TREE_OPERAND (exp, 0)));
3954 op0 = expand_debug_expr (TREE_OPERAND (exp, 0));
3955 if (!op0)
3956 return NULL;
3958 if (TREE_CODE (exp) == MEM_REF)
3960 if (GET_CODE (op0) == DEBUG_IMPLICIT_PTR
3961 || (GET_CODE (op0) == PLUS
3962 && GET_CODE (XEXP (op0, 0)) == DEBUG_IMPLICIT_PTR))
3963 /* (mem (debug_implicit_ptr)) might confuse aliasing.
3964 Instead just use get_inner_reference. */
3965 goto component_ref;
3967 op1 = expand_debug_expr (TREE_OPERAND (exp, 1));
3968 if (!op1 || !CONST_INT_P (op1))
3969 return NULL;
3971 op0 = plus_constant (inner_mode, op0, INTVAL (op1));
3974 as = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0))));
3976 op0 = convert_debug_memory_address (targetm.addr_space.address_mode (as),
3977 op0, as);
3978 if (op0 == NULL_RTX)
3979 return NULL;
3981 op0 = gen_rtx_MEM (mode, op0);
3982 set_mem_attributes (op0, exp, 0);
3983 if (TREE_CODE (exp) == MEM_REF
3984 && !is_gimple_mem_ref_addr (TREE_OPERAND (exp, 0)))
3985 set_mem_expr (op0, NULL_TREE);
3986 set_mem_addr_space (op0, as);
3988 return op0;
3990 case TARGET_MEM_REF:
3991 if (TREE_CODE (TMR_BASE (exp)) == ADDR_EXPR
3992 && !DECL_RTL_SET_P (TREE_OPERAND (TMR_BASE (exp), 0)))
3993 return NULL;
3995 op0 = expand_debug_expr
3996 (tree_mem_ref_addr (build_pointer_type (TREE_TYPE (exp)), exp));
3997 if (!op0)
3998 return NULL;
4000 as = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0))));
4001 op0 = convert_debug_memory_address (targetm.addr_space.address_mode (as),
4002 op0, as);
4003 if (op0 == NULL_RTX)
4004 return NULL;
4006 op0 = gen_rtx_MEM (mode, op0);
4008 set_mem_attributes (op0, exp, 0);
4009 set_mem_addr_space (op0, as);
4011 return op0;
4013 component_ref:
4014 case ARRAY_REF:
4015 case ARRAY_RANGE_REF:
4016 case COMPONENT_REF:
4017 case BIT_FIELD_REF:
4018 case REALPART_EXPR:
4019 case IMAGPART_EXPR:
4020 case VIEW_CONVERT_EXPR:
4022 machine_mode mode1;
4023 HOST_WIDE_INT bitsize, bitpos;
4024 tree offset;
4025 int volatilep = 0;
4026 tree tem = get_inner_reference (exp, &bitsize, &bitpos, &offset,
4027 &mode1, &unsignedp, &volatilep, false);
4028 rtx orig_op0;
4030 if (bitsize == 0)
4031 return NULL;
4033 orig_op0 = op0 = expand_debug_expr (tem);
4035 if (!op0)
4036 return NULL;
4038 if (offset)
4040 machine_mode addrmode, offmode;
4042 if (!MEM_P (op0))
4043 return NULL;
4045 op0 = XEXP (op0, 0);
4046 addrmode = GET_MODE (op0);
4047 if (addrmode == VOIDmode)
4048 addrmode = Pmode;
4050 op1 = expand_debug_expr (offset);
4051 if (!op1)
4052 return NULL;
4054 offmode = GET_MODE (op1);
4055 if (offmode == VOIDmode)
4056 offmode = TYPE_MODE (TREE_TYPE (offset));
4058 if (addrmode != offmode)
4059 op1 = simplify_gen_subreg (addrmode, op1, offmode,
4060 subreg_lowpart_offset (addrmode,
4061 offmode));
4063 /* Don't use offset_address here, we don't need a
4064 recognizable address, and we don't want to generate
4065 code. */
4066 op0 = gen_rtx_MEM (mode, simplify_gen_binary (PLUS, addrmode,
4067 op0, op1));
4070 if (MEM_P (op0))
4072 if (mode1 == VOIDmode)
4073 /* Bitfield. */
4074 mode1 = smallest_mode_for_size (bitsize, MODE_INT);
4075 if (bitpos >= BITS_PER_UNIT)
4077 op0 = adjust_address_nv (op0, mode1, bitpos / BITS_PER_UNIT);
4078 bitpos %= BITS_PER_UNIT;
4080 else if (bitpos < 0)
4082 HOST_WIDE_INT units
4083 = (-bitpos + BITS_PER_UNIT - 1) / BITS_PER_UNIT;
4084 op0 = adjust_address_nv (op0, mode1, units);
4085 bitpos += units * BITS_PER_UNIT;
4087 else if (bitpos == 0 && bitsize == GET_MODE_BITSIZE (mode))
4088 op0 = adjust_address_nv (op0, mode, 0);
4089 else if (GET_MODE (op0) != mode1)
4090 op0 = adjust_address_nv (op0, mode1, 0);
4091 else
4092 op0 = copy_rtx (op0);
4093 if (op0 == orig_op0)
4094 op0 = shallow_copy_rtx (op0);
4095 set_mem_attributes (op0, exp, 0);
4098 if (bitpos == 0 && mode == GET_MODE (op0))
4099 return op0;
4101 if (bitpos < 0)
4102 return NULL;
4104 if (GET_MODE (op0) == BLKmode)
4105 return NULL;
4107 if ((bitpos % BITS_PER_UNIT) == 0
4108 && bitsize == GET_MODE_BITSIZE (mode1))
4110 machine_mode opmode = GET_MODE (op0);
4112 if (opmode == VOIDmode)
4113 opmode = TYPE_MODE (TREE_TYPE (tem));
4115 /* This condition may hold if we're expanding the address
4116 right past the end of an array that turned out not to
4117 be addressable (i.e., the address was only computed in
4118 debug stmts). The gen_subreg below would rightfully
4119 crash, and the address doesn't really exist, so just
4120 drop it. */
4121 if (bitpos >= GET_MODE_BITSIZE (opmode))
4122 return NULL;
4124 if ((bitpos % GET_MODE_BITSIZE (mode)) == 0)
4125 return simplify_gen_subreg (mode, op0, opmode,
4126 bitpos / BITS_PER_UNIT);
4129 return simplify_gen_ternary (SCALAR_INT_MODE_P (GET_MODE (op0))
4130 && TYPE_UNSIGNED (TREE_TYPE (exp))
4131 ? SIGN_EXTRACT
4132 : ZERO_EXTRACT, mode,
4133 GET_MODE (op0) != VOIDmode
4134 ? GET_MODE (op0)
4135 : TYPE_MODE (TREE_TYPE (tem)),
4136 op0, GEN_INT (bitsize), GEN_INT (bitpos));
4139 case ABS_EXPR:
4140 return simplify_gen_unary (ABS, mode, op0, mode);
4142 case NEGATE_EXPR:
4143 return simplify_gen_unary (NEG, mode, op0, mode);
4145 case BIT_NOT_EXPR:
4146 return simplify_gen_unary (NOT, mode, op0, mode);
4148 case FLOAT_EXPR:
4149 return simplify_gen_unary (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp,
4150 0)))
4151 ? UNSIGNED_FLOAT : FLOAT, mode, op0,
4152 inner_mode);
4154 case FIX_TRUNC_EXPR:
4155 return simplify_gen_unary (unsignedp ? UNSIGNED_FIX : FIX, mode, op0,
4156 inner_mode);
4158 case POINTER_PLUS_EXPR:
4159 /* For the rare target where pointers are not the same size as
4160 size_t, we need to check for mis-matched modes and correct
4161 the addend. */
4162 if (op0 && op1
4163 && GET_MODE (op0) != VOIDmode && GET_MODE (op1) != VOIDmode
4164 && GET_MODE (op0) != GET_MODE (op1))
4166 if (GET_MODE_BITSIZE (GET_MODE (op0)) < GET_MODE_BITSIZE (GET_MODE (op1))
4167 /* If OP0 is a partial mode, then we must truncate, even if it has
4168 the same bitsize as OP1 as GCC's representation of partial modes
4169 is opaque. */
4170 || (GET_MODE_CLASS (GET_MODE (op0)) == MODE_PARTIAL_INT
4171 && GET_MODE_BITSIZE (GET_MODE (op0)) == GET_MODE_BITSIZE (GET_MODE (op1))))
4172 op1 = simplify_gen_unary (TRUNCATE, GET_MODE (op0), op1,
4173 GET_MODE (op1));
4174 else
4175 /* We always sign-extend, regardless of the signedness of
4176 the operand, because the operand is always unsigned
4177 here even if the original C expression is signed. */
4178 op1 = simplify_gen_unary (SIGN_EXTEND, GET_MODE (op0), op1,
4179 GET_MODE (op1));
4181 /* Fall through. */
4182 case PLUS_EXPR:
4183 return simplify_gen_binary (PLUS, mode, op0, op1);
4185 case MINUS_EXPR:
4186 return simplify_gen_binary (MINUS, mode, op0, op1);
4188 case MULT_EXPR:
4189 return simplify_gen_binary (MULT, mode, op0, op1);
4191 case RDIV_EXPR:
4192 case TRUNC_DIV_EXPR:
4193 case EXACT_DIV_EXPR:
4194 if (unsignedp)
4195 return simplify_gen_binary (UDIV, mode, op0, op1);
4196 else
4197 return simplify_gen_binary (DIV, mode, op0, op1);
4199 case TRUNC_MOD_EXPR:
4200 return simplify_gen_binary (unsignedp ? UMOD : MOD, mode, op0, op1);
4202 case FLOOR_DIV_EXPR:
4203 if (unsignedp)
4204 return simplify_gen_binary (UDIV, mode, op0, op1);
4205 else
4207 rtx div = simplify_gen_binary (DIV, mode, op0, op1);
4208 rtx mod = simplify_gen_binary (MOD, mode, op0, op1);
4209 rtx adj = floor_sdiv_adjust (mode, mod, op1);
4210 return simplify_gen_binary (PLUS, mode, div, adj);
4213 case FLOOR_MOD_EXPR:
4214 if (unsignedp)
4215 return simplify_gen_binary (UMOD, mode, op0, op1);
4216 else
4218 rtx mod = simplify_gen_binary (MOD, mode, op0, op1);
4219 rtx adj = floor_sdiv_adjust (mode, mod, op1);
4220 adj = simplify_gen_unary (NEG, mode,
4221 simplify_gen_binary (MULT, mode, adj, op1),
4222 mode);
4223 return simplify_gen_binary (PLUS, mode, mod, adj);
4226 case CEIL_DIV_EXPR:
4227 if (unsignedp)
4229 rtx div = simplify_gen_binary (UDIV, mode, op0, op1);
4230 rtx mod = simplify_gen_binary (UMOD, mode, op0, op1);
4231 rtx adj = ceil_udiv_adjust (mode, mod, op1);
4232 return simplify_gen_binary (PLUS, mode, div, adj);
4234 else
4236 rtx div = simplify_gen_binary (DIV, mode, op0, op1);
4237 rtx mod = simplify_gen_binary (MOD, mode, op0, op1);
4238 rtx adj = ceil_sdiv_adjust (mode, mod, op1);
4239 return simplify_gen_binary (PLUS, mode, div, adj);
4242 case CEIL_MOD_EXPR:
4243 if (unsignedp)
4245 rtx mod = simplify_gen_binary (UMOD, mode, op0, op1);
4246 rtx adj = ceil_udiv_adjust (mode, mod, op1);
4247 adj = simplify_gen_unary (NEG, mode,
4248 simplify_gen_binary (MULT, mode, adj, op1),
4249 mode);
4250 return simplify_gen_binary (PLUS, mode, mod, adj);
4252 else
4254 rtx mod = simplify_gen_binary (MOD, mode, op0, op1);
4255 rtx adj = ceil_sdiv_adjust (mode, mod, op1);
4256 adj = simplify_gen_unary (NEG, mode,
4257 simplify_gen_binary (MULT, mode, adj, op1),
4258 mode);
4259 return simplify_gen_binary (PLUS, mode, mod, adj);
4262 case ROUND_DIV_EXPR:
4263 if (unsignedp)
4265 rtx div = simplify_gen_binary (UDIV, mode, op0, op1);
4266 rtx mod = simplify_gen_binary (UMOD, mode, op0, op1);
4267 rtx adj = round_udiv_adjust (mode, mod, op1);
4268 return simplify_gen_binary (PLUS, mode, div, adj);
4270 else
4272 rtx div = simplify_gen_binary (DIV, mode, op0, op1);
4273 rtx mod = simplify_gen_binary (MOD, mode, op0, op1);
4274 rtx adj = round_sdiv_adjust (mode, mod, op1);
4275 return simplify_gen_binary (PLUS, mode, div, adj);
4278 case ROUND_MOD_EXPR:
4279 if (unsignedp)
4281 rtx mod = simplify_gen_binary (UMOD, mode, op0, op1);
4282 rtx adj = round_udiv_adjust (mode, mod, op1);
4283 adj = simplify_gen_unary (NEG, mode,
4284 simplify_gen_binary (MULT, mode, adj, op1),
4285 mode);
4286 return simplify_gen_binary (PLUS, mode, mod, adj);
4288 else
4290 rtx mod = simplify_gen_binary (MOD, mode, op0, op1);
4291 rtx adj = round_sdiv_adjust (mode, mod, op1);
4292 adj = simplify_gen_unary (NEG, mode,
4293 simplify_gen_binary (MULT, mode, adj, op1),
4294 mode);
4295 return simplify_gen_binary (PLUS, mode, mod, adj);
4298 case LSHIFT_EXPR:
4299 return simplify_gen_binary (ASHIFT, mode, op0, op1);
4301 case RSHIFT_EXPR:
4302 if (unsignedp)
4303 return simplify_gen_binary (LSHIFTRT, mode, op0, op1);
4304 else
4305 return simplify_gen_binary (ASHIFTRT, mode, op0, op1);
4307 case LROTATE_EXPR:
4308 return simplify_gen_binary (ROTATE, mode, op0, op1);
4310 case RROTATE_EXPR:
4311 return simplify_gen_binary (ROTATERT, mode, op0, op1);
4313 case MIN_EXPR:
4314 return simplify_gen_binary (unsignedp ? UMIN : SMIN, mode, op0, op1);
4316 case MAX_EXPR:
4317 return simplify_gen_binary (unsignedp ? UMAX : SMAX, mode, op0, op1);
4319 case BIT_AND_EXPR:
4320 case TRUTH_AND_EXPR:
4321 return simplify_gen_binary (AND, mode, op0, op1);
4323 case BIT_IOR_EXPR:
4324 case TRUTH_OR_EXPR:
4325 return simplify_gen_binary (IOR, mode, op0, op1);
4327 case BIT_XOR_EXPR:
4328 case TRUTH_XOR_EXPR:
4329 return simplify_gen_binary (XOR, mode, op0, op1);
4331 case TRUTH_ANDIF_EXPR:
4332 return gen_rtx_IF_THEN_ELSE (mode, op0, op1, const0_rtx);
4334 case TRUTH_ORIF_EXPR:
4335 return gen_rtx_IF_THEN_ELSE (mode, op0, const_true_rtx, op1);
4337 case TRUTH_NOT_EXPR:
4338 return simplify_gen_relational (EQ, mode, inner_mode, op0, const0_rtx);
4340 case LT_EXPR:
4341 return simplify_gen_relational (unsignedp ? LTU : LT, mode, inner_mode,
4342 op0, op1);
4344 case LE_EXPR:
4345 return simplify_gen_relational (unsignedp ? LEU : LE, mode, inner_mode,
4346 op0, op1);
4348 case GT_EXPR:
4349 return simplify_gen_relational (unsignedp ? GTU : GT, mode, inner_mode,
4350 op0, op1);
4352 case GE_EXPR:
4353 return simplify_gen_relational (unsignedp ? GEU : GE, mode, inner_mode,
4354 op0, op1);
4356 case EQ_EXPR:
4357 return simplify_gen_relational (EQ, mode, inner_mode, op0, op1);
4359 case NE_EXPR:
4360 return simplify_gen_relational (NE, mode, inner_mode, op0, op1);
4362 case UNORDERED_EXPR:
4363 return simplify_gen_relational (UNORDERED, mode, inner_mode, op0, op1);
4365 case ORDERED_EXPR:
4366 return simplify_gen_relational (ORDERED, mode, inner_mode, op0, op1);
4368 case UNLT_EXPR:
4369 return simplify_gen_relational (UNLT, mode, inner_mode, op0, op1);
4371 case UNLE_EXPR:
4372 return simplify_gen_relational (UNLE, mode, inner_mode, op0, op1);
4374 case UNGT_EXPR:
4375 return simplify_gen_relational (UNGT, mode, inner_mode, op0, op1);
4377 case UNGE_EXPR:
4378 return simplify_gen_relational (UNGE, mode, inner_mode, op0, op1);
4380 case UNEQ_EXPR:
4381 return simplify_gen_relational (UNEQ, mode, inner_mode, op0, op1);
4383 case LTGT_EXPR:
4384 return simplify_gen_relational (LTGT, mode, inner_mode, op0, op1);
4386 case COND_EXPR:
4387 return gen_rtx_IF_THEN_ELSE (mode, op0, op1, op2);
4389 case COMPLEX_EXPR:
4390 gcc_assert (COMPLEX_MODE_P (mode));
4391 if (GET_MODE (op0) == VOIDmode)
4392 op0 = gen_rtx_CONST (GET_MODE_INNER (mode), op0);
4393 if (GET_MODE (op1) == VOIDmode)
4394 op1 = gen_rtx_CONST (GET_MODE_INNER (mode), op1);
4395 return gen_rtx_CONCAT (mode, op0, op1);
4397 case CONJ_EXPR:
4398 if (GET_CODE (op0) == CONCAT)
4399 return gen_rtx_CONCAT (mode, XEXP (op0, 0),
4400 simplify_gen_unary (NEG, GET_MODE_INNER (mode),
4401 XEXP (op0, 1),
4402 GET_MODE_INNER (mode)));
4403 else
4405 machine_mode imode = GET_MODE_INNER (mode);
4406 rtx re, im;
4408 if (MEM_P (op0))
4410 re = adjust_address_nv (op0, imode, 0);
4411 im = adjust_address_nv (op0, imode, GET_MODE_SIZE (imode));
4413 else
4415 machine_mode ifmode = int_mode_for_mode (mode);
4416 machine_mode ihmode = int_mode_for_mode (imode);
4417 rtx halfsize;
4418 if (ifmode == BLKmode || ihmode == BLKmode)
4419 return NULL;
4420 halfsize = GEN_INT (GET_MODE_BITSIZE (ihmode));
4421 re = op0;
4422 if (mode != ifmode)
4423 re = gen_rtx_SUBREG (ifmode, re, 0);
4424 re = gen_rtx_ZERO_EXTRACT (ihmode, re, halfsize, const0_rtx);
4425 if (imode != ihmode)
4426 re = gen_rtx_SUBREG (imode, re, 0);
4427 im = copy_rtx (op0);
4428 if (mode != ifmode)
4429 im = gen_rtx_SUBREG (ifmode, im, 0);
4430 im = gen_rtx_ZERO_EXTRACT (ihmode, im, halfsize, halfsize);
4431 if (imode != ihmode)
4432 im = gen_rtx_SUBREG (imode, im, 0);
4434 im = gen_rtx_NEG (imode, im);
4435 return gen_rtx_CONCAT (mode, re, im);
4438 case ADDR_EXPR:
4439 op0 = expand_debug_expr (TREE_OPERAND (exp, 0));
4440 if (!op0 || !MEM_P (op0))
4442 if ((TREE_CODE (TREE_OPERAND (exp, 0)) == VAR_DECL
4443 || TREE_CODE (TREE_OPERAND (exp, 0)) == PARM_DECL
4444 || TREE_CODE (TREE_OPERAND (exp, 0)) == RESULT_DECL)
4445 && (!TREE_ADDRESSABLE (TREE_OPERAND (exp, 0))
4446 || target_for_debug_bind (TREE_OPERAND (exp, 0))))
4447 return gen_rtx_DEBUG_IMPLICIT_PTR (mode, TREE_OPERAND (exp, 0));
4449 if (handled_component_p (TREE_OPERAND (exp, 0)))
4451 HOST_WIDE_INT bitoffset, bitsize, maxsize;
4452 tree decl
4453 = get_ref_base_and_extent (TREE_OPERAND (exp, 0),
4454 &bitoffset, &bitsize, &maxsize);
4455 if ((TREE_CODE (decl) == VAR_DECL
4456 || TREE_CODE (decl) == PARM_DECL
4457 || TREE_CODE (decl) == RESULT_DECL)
4458 && (!TREE_ADDRESSABLE (decl)
4459 || target_for_debug_bind (decl))
4460 && (bitoffset % BITS_PER_UNIT) == 0
4461 && bitsize > 0
4462 && bitsize == maxsize)
4464 rtx base = gen_rtx_DEBUG_IMPLICIT_PTR (mode, decl);
4465 return plus_constant (mode, base, bitoffset / BITS_PER_UNIT);
4469 if (TREE_CODE (TREE_OPERAND (exp, 0)) == MEM_REF
4470 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
4471 == ADDR_EXPR)
4473 op0 = expand_debug_expr (TREE_OPERAND (TREE_OPERAND (exp, 0),
4474 0));
4475 if (op0 != NULL
4476 && (GET_CODE (op0) == DEBUG_IMPLICIT_PTR
4477 || (GET_CODE (op0) == PLUS
4478 && GET_CODE (XEXP (op0, 0)) == DEBUG_IMPLICIT_PTR
4479 && CONST_INT_P (XEXP (op0, 1)))))
4481 op1 = expand_debug_expr (TREE_OPERAND (TREE_OPERAND (exp, 0),
4482 1));
4483 if (!op1 || !CONST_INT_P (op1))
4484 return NULL;
4486 return plus_constant (mode, op0, INTVAL (op1));
4490 return NULL;
4493 as = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (exp)));
4494 op0 = convert_debug_memory_address (mode, XEXP (op0, 0), as);
4496 return op0;
4498 case VECTOR_CST:
4500 unsigned i;
4502 op0 = gen_rtx_CONCATN
4503 (mode, rtvec_alloc (TYPE_VECTOR_SUBPARTS (TREE_TYPE (exp))));
4505 for (i = 0; i < VECTOR_CST_NELTS (exp); ++i)
4507 op1 = expand_debug_expr (VECTOR_CST_ELT (exp, i));
4508 if (!op1)
4509 return NULL;
4510 XVECEXP (op0, 0, i) = op1;
4513 return op0;
4516 case CONSTRUCTOR:
4517 if (TREE_CLOBBER_P (exp))
4518 return NULL;
4519 else if (TREE_CODE (TREE_TYPE (exp)) == VECTOR_TYPE)
4521 unsigned i;
4522 tree val;
4524 op0 = gen_rtx_CONCATN
4525 (mode, rtvec_alloc (TYPE_VECTOR_SUBPARTS (TREE_TYPE (exp))));
4527 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), i, val)
4529 op1 = expand_debug_expr (val);
4530 if (!op1)
4531 return NULL;
4532 XVECEXP (op0, 0, i) = op1;
4535 if (i < TYPE_VECTOR_SUBPARTS (TREE_TYPE (exp)))
4537 op1 = expand_debug_expr
4538 (build_zero_cst (TREE_TYPE (TREE_TYPE (exp))));
4540 if (!op1)
4541 return NULL;
4543 for (; i < TYPE_VECTOR_SUBPARTS (TREE_TYPE (exp)); i++)
4544 XVECEXP (op0, 0, i) = op1;
4547 return op0;
4549 else
4550 goto flag_unsupported;
4552 case CALL_EXPR:
4553 /* ??? Maybe handle some builtins? */
4554 return NULL;
4556 case SSA_NAME:
4558 gimple g = get_gimple_for_ssa_name (exp);
4559 if (g)
4561 op0 = expand_debug_expr (gimple_assign_rhs_to_tree (g));
4562 if (!op0)
4563 return NULL;
4565 else
4567 int part = var_to_partition (SA.map, exp);
4569 if (part == NO_PARTITION)
4571 /* If this is a reference to an incoming value of parameter
4572 that is never used in the code or where the incoming
4573 value is never used in the code, use PARM_DECL's
4574 DECL_RTL if set. */
4575 if (SSA_NAME_IS_DEFAULT_DEF (exp)
4576 && TREE_CODE (SSA_NAME_VAR (exp)) == PARM_DECL)
4578 op0 = expand_debug_parm_decl (SSA_NAME_VAR (exp));
4579 if (op0)
4580 goto adjust_mode;
4581 op0 = expand_debug_expr (SSA_NAME_VAR (exp));
4582 if (op0)
4583 goto adjust_mode;
4585 return NULL;
4588 gcc_assert (part >= 0 && (unsigned)part < SA.map->num_partitions);
4590 op0 = copy_rtx (SA.partition_to_pseudo[part]);
4592 goto adjust_mode;
4595 case ERROR_MARK:
4596 return NULL;
4598 /* Vector stuff. For most of the codes we don't have rtl codes. */
4599 case REALIGN_LOAD_EXPR:
4600 case REDUC_MAX_EXPR:
4601 case REDUC_MIN_EXPR:
4602 case REDUC_PLUS_EXPR:
4603 case VEC_COND_EXPR:
4604 case VEC_PACK_FIX_TRUNC_EXPR:
4605 case VEC_PACK_SAT_EXPR:
4606 case VEC_PACK_TRUNC_EXPR:
4607 case VEC_RSHIFT_EXPR:
4608 case VEC_UNPACK_FLOAT_HI_EXPR:
4609 case VEC_UNPACK_FLOAT_LO_EXPR:
4610 case VEC_UNPACK_HI_EXPR:
4611 case VEC_UNPACK_LO_EXPR:
4612 case VEC_WIDEN_MULT_HI_EXPR:
4613 case VEC_WIDEN_MULT_LO_EXPR:
4614 case VEC_WIDEN_MULT_EVEN_EXPR:
4615 case VEC_WIDEN_MULT_ODD_EXPR:
4616 case VEC_WIDEN_LSHIFT_HI_EXPR:
4617 case VEC_WIDEN_LSHIFT_LO_EXPR:
4618 case VEC_PERM_EXPR:
4619 return NULL;
4621 /* Misc codes. */
4622 case ADDR_SPACE_CONVERT_EXPR:
4623 case FIXED_CONVERT_EXPR:
4624 case OBJ_TYPE_REF:
4625 case WITH_SIZE_EXPR:
4626 return NULL;
4628 case DOT_PROD_EXPR:
4629 if (SCALAR_INT_MODE_P (GET_MODE (op0))
4630 && SCALAR_INT_MODE_P (mode))
4633 = simplify_gen_unary (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp,
4634 0)))
4635 ? ZERO_EXTEND : SIGN_EXTEND, mode, op0,
4636 inner_mode);
4638 = simplify_gen_unary (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp,
4639 1)))
4640 ? ZERO_EXTEND : SIGN_EXTEND, mode, op1,
4641 inner_mode);
4642 op0 = simplify_gen_binary (MULT, mode, op0, op1);
4643 return simplify_gen_binary (PLUS, mode, op0, op2);
4645 return NULL;
4647 case WIDEN_MULT_EXPR:
4648 case WIDEN_MULT_PLUS_EXPR:
4649 case WIDEN_MULT_MINUS_EXPR:
4650 if (SCALAR_INT_MODE_P (GET_MODE (op0))
4651 && SCALAR_INT_MODE_P (mode))
4653 inner_mode = GET_MODE (op0);
4654 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp, 0))))
4655 op0 = simplify_gen_unary (ZERO_EXTEND, mode, op0, inner_mode);
4656 else
4657 op0 = simplify_gen_unary (SIGN_EXTEND, mode, op0, inner_mode);
4658 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp, 1))))
4659 op1 = simplify_gen_unary (ZERO_EXTEND, mode, op1, inner_mode);
4660 else
4661 op1 = simplify_gen_unary (SIGN_EXTEND, mode, op1, inner_mode);
4662 op0 = simplify_gen_binary (MULT, mode, op0, op1);
4663 if (TREE_CODE (exp) == WIDEN_MULT_EXPR)
4664 return op0;
4665 else if (TREE_CODE (exp) == WIDEN_MULT_PLUS_EXPR)
4666 return simplify_gen_binary (PLUS, mode, op0, op2);
4667 else
4668 return simplify_gen_binary (MINUS, mode, op2, op0);
4670 return NULL;
4672 case MULT_HIGHPART_EXPR:
4673 /* ??? Similar to the above. */
4674 return NULL;
4676 case WIDEN_SUM_EXPR:
4677 case WIDEN_LSHIFT_EXPR:
4678 if (SCALAR_INT_MODE_P (GET_MODE (op0))
4679 && SCALAR_INT_MODE_P (mode))
4682 = simplify_gen_unary (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp,
4683 0)))
4684 ? ZERO_EXTEND : SIGN_EXTEND, mode, op0,
4685 inner_mode);
4686 return simplify_gen_binary (TREE_CODE (exp) == WIDEN_LSHIFT_EXPR
4687 ? ASHIFT : PLUS, mode, op0, op1);
4689 return NULL;
4691 case FMA_EXPR:
4692 return simplify_gen_ternary (FMA, mode, inner_mode, op0, op1, op2);
4694 default:
4695 flag_unsupported:
4696 #ifdef ENABLE_CHECKING
4697 debug_tree (exp);
4698 gcc_unreachable ();
4699 #else
4700 return NULL;
4701 #endif
4705 /* Return an RTX equivalent to the source bind value of the tree expression
4706 EXP. */
4708 static rtx
4709 expand_debug_source_expr (tree exp)
4711 rtx op0 = NULL_RTX;
4712 machine_mode mode = VOIDmode, inner_mode;
4714 switch (TREE_CODE (exp))
4716 case PARM_DECL:
4718 mode = DECL_MODE (exp);
4719 op0 = expand_debug_parm_decl (exp);
4720 if (op0)
4721 break;
4722 /* See if this isn't an argument that has been completely
4723 optimized out. */
4724 if (!DECL_RTL_SET_P (exp)
4725 && !DECL_INCOMING_RTL (exp)
4726 && DECL_ABSTRACT_ORIGIN (current_function_decl))
4728 tree aexp = DECL_ORIGIN (exp);
4729 if (DECL_CONTEXT (aexp)
4730 == DECL_ABSTRACT_ORIGIN (current_function_decl))
4732 vec<tree, va_gc> **debug_args;
4733 unsigned int ix;
4734 tree ddecl;
4735 debug_args = decl_debug_args_lookup (current_function_decl);
4736 if (debug_args != NULL)
4738 for (ix = 0; vec_safe_iterate (*debug_args, ix, &ddecl);
4739 ix += 2)
4740 if (ddecl == aexp)
4741 return gen_rtx_DEBUG_PARAMETER_REF (mode, aexp);
4745 break;
4747 default:
4748 break;
4751 if (op0 == NULL_RTX)
4752 return NULL_RTX;
4754 inner_mode = GET_MODE (op0);
4755 if (mode == inner_mode)
4756 return op0;
4758 if (FLOAT_MODE_P (mode) && FLOAT_MODE_P (inner_mode))
4760 if (GET_MODE_BITSIZE (mode) == GET_MODE_BITSIZE (inner_mode))
4761 op0 = simplify_gen_subreg (mode, op0, inner_mode, 0);
4762 else if (GET_MODE_BITSIZE (mode) < GET_MODE_BITSIZE (inner_mode))
4763 op0 = simplify_gen_unary (FLOAT_TRUNCATE, mode, op0, inner_mode);
4764 else
4765 op0 = simplify_gen_unary (FLOAT_EXTEND, mode, op0, inner_mode);
4767 else if (FLOAT_MODE_P (mode))
4768 gcc_unreachable ();
4769 else if (FLOAT_MODE_P (inner_mode))
4771 if (TYPE_UNSIGNED (TREE_TYPE (exp)))
4772 op0 = simplify_gen_unary (UNSIGNED_FIX, mode, op0, inner_mode);
4773 else
4774 op0 = simplify_gen_unary (FIX, mode, op0, inner_mode);
4776 else if (CONSTANT_P (op0)
4777 || GET_MODE_BITSIZE (mode) <= GET_MODE_BITSIZE (inner_mode))
4778 op0 = simplify_gen_subreg (mode, op0, inner_mode,
4779 subreg_lowpart_offset (mode, inner_mode));
4780 else if (TYPE_UNSIGNED (TREE_TYPE (exp)))
4781 op0 = simplify_gen_unary (ZERO_EXTEND, mode, op0, inner_mode);
4782 else
4783 op0 = simplify_gen_unary (SIGN_EXTEND, mode, op0, inner_mode);
4785 return op0;
4788 /* Ensure INSN_VAR_LOCATION_LOC (insn) doesn't have unbound complexity.
4789 Allow 4 levels of rtl nesting for most rtl codes, and if we see anything
4790 deeper than that, create DEBUG_EXPRs and emit DEBUG_INSNs before INSN. */
4792 static void
4793 avoid_complex_debug_insns (rtx_insn *insn, rtx *exp_p, int depth)
4795 rtx exp = *exp_p;
4797 if (exp == NULL_RTX)
4798 return;
4800 if ((OBJECT_P (exp) && !MEM_P (exp)) || GET_CODE (exp) == CLOBBER)
4801 return;
4803 if (depth == 4)
4805 /* Create DEBUG_EXPR (and DEBUG_EXPR_DECL). */
4806 rtx dval = make_debug_expr_from_rtl (exp);
4808 /* Emit a debug bind insn before INSN. */
4809 rtx bind = gen_rtx_VAR_LOCATION (GET_MODE (exp),
4810 DEBUG_EXPR_TREE_DECL (dval), exp,
4811 VAR_INIT_STATUS_INITIALIZED);
4813 emit_debug_insn_before (bind, insn);
4814 *exp_p = dval;
4815 return;
4818 const char *format_ptr = GET_RTX_FORMAT (GET_CODE (exp));
4819 int i, j;
4820 for (i = 0; i < GET_RTX_LENGTH (GET_CODE (exp)); i++)
4821 switch (*format_ptr++)
4823 case 'e':
4824 avoid_complex_debug_insns (insn, &XEXP (exp, i), depth + 1);
4825 break;
4827 case 'E':
4828 case 'V':
4829 for (j = 0; j < XVECLEN (exp, i); j++)
4830 avoid_complex_debug_insns (insn, &XVECEXP (exp, i, j), depth + 1);
4831 break;
4833 default:
4834 break;
4838 /* Expand the _LOCs in debug insns. We run this after expanding all
4839 regular insns, so that any variables referenced in the function
4840 will have their DECL_RTLs set. */
4842 static void
4843 expand_debug_locations (void)
4845 rtx_insn *insn;
4846 rtx_insn *last = get_last_insn ();
4847 int save_strict_alias = flag_strict_aliasing;
4849 /* New alias sets while setting up memory attributes cause
4850 -fcompare-debug failures, even though it doesn't bring about any
4851 codegen changes. */
4852 flag_strict_aliasing = 0;
4854 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
4855 if (DEBUG_INSN_P (insn))
4857 tree value = (tree)INSN_VAR_LOCATION_LOC (insn);
4858 rtx val;
4859 rtx_insn *prev_insn, *insn2;
4860 machine_mode mode;
4862 if (value == NULL_TREE)
4863 val = NULL_RTX;
4864 else
4866 if (INSN_VAR_LOCATION_STATUS (insn)
4867 == VAR_INIT_STATUS_UNINITIALIZED)
4868 val = expand_debug_source_expr (value);
4869 else
4870 val = expand_debug_expr (value);
4871 gcc_assert (last == get_last_insn ());
4874 if (!val)
4875 val = gen_rtx_UNKNOWN_VAR_LOC ();
4876 else
4878 mode = GET_MODE (INSN_VAR_LOCATION (insn));
4880 gcc_assert (mode == GET_MODE (val)
4881 || (GET_MODE (val) == VOIDmode
4882 && (CONST_SCALAR_INT_P (val)
4883 || GET_CODE (val) == CONST_FIXED
4884 || GET_CODE (val) == LABEL_REF)));
4887 INSN_VAR_LOCATION_LOC (insn) = val;
4888 prev_insn = PREV_INSN (insn);
4889 for (insn2 = insn; insn2 != prev_insn; insn2 = PREV_INSN (insn2))
4890 avoid_complex_debug_insns (insn2, &INSN_VAR_LOCATION_LOC (insn2), 0);
4893 flag_strict_aliasing = save_strict_alias;
4896 /* Expand basic block BB from GIMPLE trees to RTL. */
4898 static basic_block
4899 expand_gimple_basic_block (basic_block bb, bool disable_tail_calls)
4901 gimple_stmt_iterator gsi;
4902 gimple_seq stmts;
4903 gimple stmt = NULL;
4904 rtx_note *note;
4905 rtx_insn *last;
4906 edge e;
4907 edge_iterator ei;
4909 if (dump_file)
4910 fprintf (dump_file, "\n;; Generating RTL for gimple basic block %d\n",
4911 bb->index);
4913 /* Note that since we are now transitioning from GIMPLE to RTL, we
4914 cannot use the gsi_*_bb() routines because they expect the basic
4915 block to be in GIMPLE, instead of RTL. Therefore, we need to
4916 access the BB sequence directly. */
4917 stmts = bb_seq (bb);
4918 bb->il.gimple.seq = NULL;
4919 bb->il.gimple.phi_nodes = NULL;
4920 rtl_profile_for_bb (bb);
4921 init_rtl_bb_info (bb);
4922 bb->flags |= BB_RTL;
4924 /* Remove the RETURN_EXPR if we may fall though to the exit
4925 instead. */
4926 gsi = gsi_last (stmts);
4927 if (!gsi_end_p (gsi)
4928 && gimple_code (gsi_stmt (gsi)) == GIMPLE_RETURN)
4930 gimple ret_stmt = gsi_stmt (gsi);
4932 gcc_assert (single_succ_p (bb));
4933 gcc_assert (single_succ (bb) == EXIT_BLOCK_PTR_FOR_FN (cfun));
4935 if (bb->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
4936 && !gimple_return_retval (ret_stmt))
4938 gsi_remove (&gsi, false);
4939 single_succ_edge (bb)->flags |= EDGE_FALLTHRU;
4943 gsi = gsi_start (stmts);
4944 if (!gsi_end_p (gsi))
4946 stmt = gsi_stmt (gsi);
4947 if (gimple_code (stmt) != GIMPLE_LABEL)
4948 stmt = NULL;
4951 rtx_code_label **elt = lab_rtx_for_bb->get (bb);
4953 if (stmt || elt)
4955 last = get_last_insn ();
4957 if (stmt)
4959 expand_gimple_stmt (stmt);
4960 gsi_next (&gsi);
4963 if (elt)
4964 emit_label (*elt);
4966 /* Java emits line number notes in the top of labels.
4967 ??? Make this go away once line number notes are obsoleted. */
4968 BB_HEAD (bb) = NEXT_INSN (last);
4969 if (NOTE_P (BB_HEAD (bb)))
4970 BB_HEAD (bb) = NEXT_INSN (BB_HEAD (bb));
4971 note = emit_note_after (NOTE_INSN_BASIC_BLOCK, BB_HEAD (bb));
4973 maybe_dump_rtl_for_gimple_stmt (stmt, last);
4975 else
4976 BB_HEAD (bb) = note = emit_note (NOTE_INSN_BASIC_BLOCK);
4978 NOTE_BASIC_BLOCK (note) = bb;
4980 for (; !gsi_end_p (gsi); gsi_next (&gsi))
4982 basic_block new_bb;
4984 stmt = gsi_stmt (gsi);
4986 /* If this statement is a non-debug one, and we generate debug
4987 insns, then this one might be the last real use of a TERed
4988 SSA_NAME, but where there are still some debug uses further
4989 down. Expanding the current SSA name in such further debug
4990 uses by their RHS might lead to wrong debug info, as coalescing
4991 might make the operands of such RHS be placed into the same
4992 pseudo as something else. Like so:
4993 a_1 = a_0 + 1; // Assume a_1 is TERed and a_0 is dead
4994 use(a_1);
4995 a_2 = ...
4996 #DEBUG ... => a_1
4997 As a_0 and a_2 don't overlap in lifetime, assume they are coalesced.
4998 If we now would expand a_1 by it's RHS (a_0 + 1) in the debug use,
4999 the write to a_2 would actually have clobbered the place which
5000 formerly held a_0.
5002 So, instead of that, we recognize the situation, and generate
5003 debug temporaries at the last real use of TERed SSA names:
5004 a_1 = a_0 + 1;
5005 #DEBUG #D1 => a_1
5006 use(a_1);
5007 a_2 = ...
5008 #DEBUG ... => #D1
5010 if (MAY_HAVE_DEBUG_INSNS
5011 && SA.values
5012 && !is_gimple_debug (stmt))
5014 ssa_op_iter iter;
5015 tree op;
5016 gimple def;
5018 location_t sloc = curr_insn_location ();
5020 /* Look for SSA names that have their last use here (TERed
5021 names always have only one real use). */
5022 FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_USE)
5023 if ((def = get_gimple_for_ssa_name (op)))
5025 imm_use_iterator imm_iter;
5026 use_operand_p use_p;
5027 bool have_debug_uses = false;
5029 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, op)
5031 if (gimple_debug_bind_p (USE_STMT (use_p)))
5033 have_debug_uses = true;
5034 break;
5038 if (have_debug_uses)
5040 /* OP is a TERed SSA name, with DEF it's defining
5041 statement, and where OP is used in further debug
5042 instructions. Generate a debug temporary, and
5043 replace all uses of OP in debug insns with that
5044 temporary. */
5045 gimple debugstmt;
5046 tree value = gimple_assign_rhs_to_tree (def);
5047 tree vexpr = make_node (DEBUG_EXPR_DECL);
5048 rtx val;
5049 machine_mode mode;
5051 set_curr_insn_location (gimple_location (def));
5053 DECL_ARTIFICIAL (vexpr) = 1;
5054 TREE_TYPE (vexpr) = TREE_TYPE (value);
5055 if (DECL_P (value))
5056 mode = DECL_MODE (value);
5057 else
5058 mode = TYPE_MODE (TREE_TYPE (value));
5059 DECL_MODE (vexpr) = mode;
5061 val = gen_rtx_VAR_LOCATION
5062 (mode, vexpr, (rtx)value, VAR_INIT_STATUS_INITIALIZED);
5064 emit_debug_insn (val);
5066 FOR_EACH_IMM_USE_STMT (debugstmt, imm_iter, op)
5068 if (!gimple_debug_bind_p (debugstmt))
5069 continue;
5071 FOR_EACH_IMM_USE_ON_STMT (use_p, imm_iter)
5072 SET_USE (use_p, vexpr);
5074 update_stmt (debugstmt);
5078 set_curr_insn_location (sloc);
5081 currently_expanding_gimple_stmt = stmt;
5083 /* Expand this statement, then evaluate the resulting RTL and
5084 fixup the CFG accordingly. */
5085 if (gimple_code (stmt) == GIMPLE_COND)
5087 new_bb = expand_gimple_cond (bb, stmt);
5088 if (new_bb)
5089 return new_bb;
5091 else if (gimple_debug_bind_p (stmt))
5093 location_t sloc = curr_insn_location ();
5094 gimple_stmt_iterator nsi = gsi;
5096 for (;;)
5098 tree var = gimple_debug_bind_get_var (stmt);
5099 tree value;
5100 rtx val;
5101 machine_mode mode;
5103 if (TREE_CODE (var) != DEBUG_EXPR_DECL
5104 && TREE_CODE (var) != LABEL_DECL
5105 && !target_for_debug_bind (var))
5106 goto delink_debug_stmt;
5108 if (gimple_debug_bind_has_value_p (stmt))
5109 value = gimple_debug_bind_get_value (stmt);
5110 else
5111 value = NULL_TREE;
5113 last = get_last_insn ();
5115 set_curr_insn_location (gimple_location (stmt));
5117 if (DECL_P (var))
5118 mode = DECL_MODE (var);
5119 else
5120 mode = TYPE_MODE (TREE_TYPE (var));
5122 val = gen_rtx_VAR_LOCATION
5123 (mode, var, (rtx)value, VAR_INIT_STATUS_INITIALIZED);
5125 emit_debug_insn (val);
5127 if (dump_file && (dump_flags & TDF_DETAILS))
5129 /* We can't dump the insn with a TREE where an RTX
5130 is expected. */
5131 PAT_VAR_LOCATION_LOC (val) = const0_rtx;
5132 maybe_dump_rtl_for_gimple_stmt (stmt, last);
5133 PAT_VAR_LOCATION_LOC (val) = (rtx)value;
5136 delink_debug_stmt:
5137 /* In order not to generate too many debug temporaries,
5138 we delink all uses of debug statements we already expanded.
5139 Therefore debug statements between definition and real
5140 use of TERed SSA names will continue to use the SSA name,
5141 and not be replaced with debug temps. */
5142 delink_stmt_imm_use (stmt);
5144 gsi = nsi;
5145 gsi_next (&nsi);
5146 if (gsi_end_p (nsi))
5147 break;
5148 stmt = gsi_stmt (nsi);
5149 if (!gimple_debug_bind_p (stmt))
5150 break;
5153 set_curr_insn_location (sloc);
5155 else if (gimple_debug_source_bind_p (stmt))
5157 location_t sloc = curr_insn_location ();
5158 tree var = gimple_debug_source_bind_get_var (stmt);
5159 tree value = gimple_debug_source_bind_get_value (stmt);
5160 rtx val;
5161 machine_mode mode;
5163 last = get_last_insn ();
5165 set_curr_insn_location (gimple_location (stmt));
5167 mode = DECL_MODE (var);
5169 val = gen_rtx_VAR_LOCATION (mode, var, (rtx)value,
5170 VAR_INIT_STATUS_UNINITIALIZED);
5172 emit_debug_insn (val);
5174 if (dump_file && (dump_flags & TDF_DETAILS))
5176 /* We can't dump the insn with a TREE where an RTX
5177 is expected. */
5178 PAT_VAR_LOCATION_LOC (val) = const0_rtx;
5179 maybe_dump_rtl_for_gimple_stmt (stmt, last);
5180 PAT_VAR_LOCATION_LOC (val) = (rtx)value;
5183 set_curr_insn_location (sloc);
5185 else
5187 if (is_gimple_call (stmt)
5188 && gimple_call_tail_p (stmt)
5189 && disable_tail_calls)
5190 gimple_call_set_tail (stmt, false);
5192 if (is_gimple_call (stmt) && gimple_call_tail_p (stmt))
5194 bool can_fallthru;
5195 new_bb = expand_gimple_tailcall (bb, stmt, &can_fallthru);
5196 if (new_bb)
5198 if (can_fallthru)
5199 bb = new_bb;
5200 else
5201 return new_bb;
5204 else
5206 def_operand_p def_p;
5207 def_p = SINGLE_SSA_DEF_OPERAND (stmt, SSA_OP_DEF);
5209 if (def_p != NULL)
5211 /* Ignore this stmt if it is in the list of
5212 replaceable expressions. */
5213 if (SA.values
5214 && bitmap_bit_p (SA.values,
5215 SSA_NAME_VERSION (DEF_FROM_PTR (def_p))))
5216 continue;
5218 last = expand_gimple_stmt (stmt);
5219 maybe_dump_rtl_for_gimple_stmt (stmt, last);
5224 currently_expanding_gimple_stmt = NULL;
5226 /* Expand implicit goto and convert goto_locus. */
5227 FOR_EACH_EDGE (e, ei, bb->succs)
5229 if (e->goto_locus != UNKNOWN_LOCATION)
5230 set_curr_insn_location (e->goto_locus);
5231 if ((e->flags & EDGE_FALLTHRU) && e->dest != bb->next_bb)
5233 emit_jump (label_rtx_for_bb (e->dest));
5234 e->flags &= ~EDGE_FALLTHRU;
5238 /* Expanded RTL can create a jump in the last instruction of block.
5239 This later might be assumed to be a jump to successor and break edge insertion.
5240 We need to insert dummy move to prevent this. PR41440. */
5241 if (single_succ_p (bb)
5242 && (single_succ_edge (bb)->flags & EDGE_FALLTHRU)
5243 && (last = get_last_insn ())
5244 && JUMP_P (last))
5246 rtx dummy = gen_reg_rtx (SImode);
5247 emit_insn_after_noloc (gen_move_insn (dummy, dummy), last, NULL);
5250 do_pending_stack_adjust ();
5252 /* Find the block tail. The last insn in the block is the insn
5253 before a barrier and/or table jump insn. */
5254 last = get_last_insn ();
5255 if (BARRIER_P (last))
5256 last = PREV_INSN (last);
5257 if (JUMP_TABLE_DATA_P (last))
5258 last = PREV_INSN (PREV_INSN (last));
5259 BB_END (bb) = last;
5261 update_bb_for_insn (bb);
5263 return bb;
5267 /* Create a basic block for initialization code. */
5269 static basic_block
5270 construct_init_block (void)
5272 basic_block init_block, first_block;
5273 edge e = NULL;
5274 int flags;
5276 /* Multiple entry points not supported yet. */
5277 gcc_assert (EDGE_COUNT (ENTRY_BLOCK_PTR_FOR_FN (cfun)->succs) == 1);
5278 init_rtl_bb_info (ENTRY_BLOCK_PTR_FOR_FN (cfun));
5279 init_rtl_bb_info (EXIT_BLOCK_PTR_FOR_FN (cfun));
5280 ENTRY_BLOCK_PTR_FOR_FN (cfun)->flags |= BB_RTL;
5281 EXIT_BLOCK_PTR_FOR_FN (cfun)->flags |= BB_RTL;
5283 e = EDGE_SUCC (ENTRY_BLOCK_PTR_FOR_FN (cfun), 0);
5285 /* When entry edge points to first basic block, we don't need jump,
5286 otherwise we have to jump into proper target. */
5287 if (e && e->dest != ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb)
5289 tree label = gimple_block_label (e->dest);
5291 emit_jump (label_rtx (label));
5292 flags = 0;
5294 else
5295 flags = EDGE_FALLTHRU;
5297 init_block = create_basic_block (NEXT_INSN (get_insns ()),
5298 get_last_insn (),
5299 ENTRY_BLOCK_PTR_FOR_FN (cfun));
5300 init_block->frequency = ENTRY_BLOCK_PTR_FOR_FN (cfun)->frequency;
5301 init_block->count = ENTRY_BLOCK_PTR_FOR_FN (cfun)->count;
5302 add_bb_to_loop (init_block, ENTRY_BLOCK_PTR_FOR_FN (cfun)->loop_father);
5303 if (e)
5305 first_block = e->dest;
5306 redirect_edge_succ (e, init_block);
5307 e = make_edge (init_block, first_block, flags);
5309 else
5310 e = make_edge (init_block, EXIT_BLOCK_PTR_FOR_FN (cfun), EDGE_FALLTHRU);
5311 e->probability = REG_BR_PROB_BASE;
5312 e->count = ENTRY_BLOCK_PTR_FOR_FN (cfun)->count;
5314 update_bb_for_insn (init_block);
5315 return init_block;
5318 /* For each lexical block, set BLOCK_NUMBER to the depth at which it is
5319 found in the block tree. */
5321 static void
5322 set_block_levels (tree block, int level)
5324 while (block)
5326 BLOCK_NUMBER (block) = level;
5327 set_block_levels (BLOCK_SUBBLOCKS (block), level + 1);
5328 block = BLOCK_CHAIN (block);
5332 /* Create a block containing landing pads and similar stuff. */
5334 static void
5335 construct_exit_block (void)
5337 rtx_insn *head = get_last_insn ();
5338 rtx_insn *end;
5339 basic_block exit_block;
5340 edge e, e2;
5341 unsigned ix;
5342 edge_iterator ei;
5343 basic_block prev_bb = EXIT_BLOCK_PTR_FOR_FN (cfun)->prev_bb;
5344 rtx_insn *orig_end = BB_END (prev_bb);
5346 rtl_profile_for_bb (EXIT_BLOCK_PTR_FOR_FN (cfun));
5348 /* Make sure the locus is set to the end of the function, so that
5349 epilogue line numbers and warnings are set properly. */
5350 if (LOCATION_LOCUS (cfun->function_end_locus) != UNKNOWN_LOCATION)
5351 input_location = cfun->function_end_locus;
5353 /* Generate rtl for function exit. */
5354 expand_function_end ();
5356 end = get_last_insn ();
5357 if (head == end)
5358 return;
5359 /* While emitting the function end we could move end of the last basic
5360 block. */
5361 BB_END (prev_bb) = orig_end;
5362 while (NEXT_INSN (head) && NOTE_P (NEXT_INSN (head)))
5363 head = NEXT_INSN (head);
5364 /* But make sure exit_block starts with RETURN_LABEL, otherwise the
5365 bb frequency counting will be confused. Any instructions before that
5366 label are emitted for the case where PREV_BB falls through into the
5367 exit block, so append those instructions to prev_bb in that case. */
5368 if (NEXT_INSN (head) != return_label)
5370 while (NEXT_INSN (head) != return_label)
5372 if (!NOTE_P (NEXT_INSN (head)))
5373 BB_END (prev_bb) = NEXT_INSN (head);
5374 head = NEXT_INSN (head);
5377 exit_block = create_basic_block (NEXT_INSN (head), end, prev_bb);
5378 exit_block->frequency = EXIT_BLOCK_PTR_FOR_FN (cfun)->frequency;
5379 exit_block->count = EXIT_BLOCK_PTR_FOR_FN (cfun)->count;
5380 add_bb_to_loop (exit_block, EXIT_BLOCK_PTR_FOR_FN (cfun)->loop_father);
5382 ix = 0;
5383 while (ix < EDGE_COUNT (EXIT_BLOCK_PTR_FOR_FN (cfun)->preds))
5385 e = EDGE_PRED (EXIT_BLOCK_PTR_FOR_FN (cfun), ix);
5386 if (!(e->flags & EDGE_ABNORMAL))
5387 redirect_edge_succ (e, exit_block);
5388 else
5389 ix++;
5392 e = make_edge (exit_block, EXIT_BLOCK_PTR_FOR_FN (cfun), EDGE_FALLTHRU);
5393 e->probability = REG_BR_PROB_BASE;
5394 e->count = EXIT_BLOCK_PTR_FOR_FN (cfun)->count;
5395 FOR_EACH_EDGE (e2, ei, EXIT_BLOCK_PTR_FOR_FN (cfun)->preds)
5396 if (e2 != e)
5398 e->count -= e2->count;
5399 exit_block->count -= e2->count;
5400 exit_block->frequency -= EDGE_FREQUENCY (e2);
5402 if (e->count < 0)
5403 e->count = 0;
5404 if (exit_block->count < 0)
5405 exit_block->count = 0;
5406 if (exit_block->frequency < 0)
5407 exit_block->frequency = 0;
5408 update_bb_for_insn (exit_block);
5411 /* Helper function for discover_nonconstant_array_refs.
5412 Look for ARRAY_REF nodes with non-constant indexes and mark them
5413 addressable. */
5415 static tree
5416 discover_nonconstant_array_refs_r (tree * tp, int *walk_subtrees,
5417 void *data ATTRIBUTE_UNUSED)
5419 tree t = *tp;
5421 if (IS_TYPE_OR_DECL_P (t))
5422 *walk_subtrees = 0;
5423 else if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
5425 while (((TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
5426 && is_gimple_min_invariant (TREE_OPERAND (t, 1))
5427 && (!TREE_OPERAND (t, 2)
5428 || is_gimple_min_invariant (TREE_OPERAND (t, 2))))
5429 || (TREE_CODE (t) == COMPONENT_REF
5430 && (!TREE_OPERAND (t,2)
5431 || is_gimple_min_invariant (TREE_OPERAND (t, 2))))
5432 || TREE_CODE (t) == BIT_FIELD_REF
5433 || TREE_CODE (t) == REALPART_EXPR
5434 || TREE_CODE (t) == IMAGPART_EXPR
5435 || TREE_CODE (t) == VIEW_CONVERT_EXPR
5436 || CONVERT_EXPR_P (t))
5437 t = TREE_OPERAND (t, 0);
5439 if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
5441 t = get_base_address (t);
5442 if (t && DECL_P (t)
5443 && DECL_MODE (t) != BLKmode)
5444 TREE_ADDRESSABLE (t) = 1;
5447 *walk_subtrees = 0;
5450 return NULL_TREE;
5453 /* RTL expansion is not able to compile array references with variable
5454 offsets for arrays stored in single register. Discover such
5455 expressions and mark variables as addressable to avoid this
5456 scenario. */
5458 static void
5459 discover_nonconstant_array_refs (void)
5461 basic_block bb;
5462 gimple_stmt_iterator gsi;
5464 FOR_EACH_BB_FN (bb, cfun)
5465 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
5467 gimple stmt = gsi_stmt (gsi);
5468 if (!is_gimple_debug (stmt))
5469 walk_gimple_op (stmt, discover_nonconstant_array_refs_r, NULL);
5473 /* This function sets crtl->args.internal_arg_pointer to a virtual
5474 register if DRAP is needed. Local register allocator will replace
5475 virtual_incoming_args_rtx with the virtual register. */
5477 static void
5478 expand_stack_alignment (void)
5480 rtx drap_rtx;
5481 unsigned int preferred_stack_boundary;
5483 if (! SUPPORTS_STACK_ALIGNMENT)
5484 return;
5486 if (cfun->calls_alloca
5487 || cfun->has_nonlocal_label
5488 || crtl->has_nonlocal_goto)
5489 crtl->need_drap = true;
5491 /* Call update_stack_boundary here again to update incoming stack
5492 boundary. It may set incoming stack alignment to a different
5493 value after RTL expansion. TARGET_FUNCTION_OK_FOR_SIBCALL may
5494 use the minimum incoming stack alignment to check if it is OK
5495 to perform sibcall optimization since sibcall optimization will
5496 only align the outgoing stack to incoming stack boundary. */
5497 if (targetm.calls.update_stack_boundary)
5498 targetm.calls.update_stack_boundary ();
5500 /* The incoming stack frame has to be aligned at least at
5501 parm_stack_boundary. */
5502 gcc_assert (crtl->parm_stack_boundary <= INCOMING_STACK_BOUNDARY);
5504 /* Update crtl->stack_alignment_estimated and use it later to align
5505 stack. We check PREFERRED_STACK_BOUNDARY if there may be non-call
5506 exceptions since callgraph doesn't collect incoming stack alignment
5507 in this case. */
5508 if (cfun->can_throw_non_call_exceptions
5509 && PREFERRED_STACK_BOUNDARY > crtl->preferred_stack_boundary)
5510 preferred_stack_boundary = PREFERRED_STACK_BOUNDARY;
5511 else
5512 preferred_stack_boundary = crtl->preferred_stack_boundary;
5513 if (preferred_stack_boundary > crtl->stack_alignment_estimated)
5514 crtl->stack_alignment_estimated = preferred_stack_boundary;
5515 if (preferred_stack_boundary > crtl->stack_alignment_needed)
5516 crtl->stack_alignment_needed = preferred_stack_boundary;
5518 gcc_assert (crtl->stack_alignment_needed
5519 <= crtl->stack_alignment_estimated);
5521 crtl->stack_realign_needed
5522 = INCOMING_STACK_BOUNDARY < crtl->stack_alignment_estimated;
5523 crtl->stack_realign_tried = crtl->stack_realign_needed;
5525 crtl->stack_realign_processed = true;
5527 /* Target has to redefine TARGET_GET_DRAP_RTX to support stack
5528 alignment. */
5529 gcc_assert (targetm.calls.get_drap_rtx != NULL);
5530 drap_rtx = targetm.calls.get_drap_rtx ();
5532 /* stack_realign_drap and drap_rtx must match. */
5533 gcc_assert ((stack_realign_drap != 0) == (drap_rtx != NULL));
5535 /* Do nothing if NULL is returned, which means DRAP is not needed. */
5536 if (NULL != drap_rtx)
5538 crtl->args.internal_arg_pointer = drap_rtx;
5540 /* Call fixup_tail_calls to clean up REG_EQUIV note if DRAP is
5541 needed. */
5542 fixup_tail_calls ();
5547 static void
5548 expand_main_function (void)
5550 #if (defined(INVOKE__main) \
5551 || (!defined(HAS_INIT_SECTION) \
5552 && !defined(INIT_SECTION_ASM_OP) \
5553 && !defined(INIT_ARRAY_SECTION_ASM_OP)))
5554 emit_library_call (init_one_libfunc (NAME__MAIN), LCT_NORMAL, VOIDmode, 0);
5555 #endif
5559 /* Expand code to initialize the stack_protect_guard. This is invoked at
5560 the beginning of a function to be protected. */
5562 #ifndef HAVE_stack_protect_set
5563 # define HAVE_stack_protect_set 0
5564 # define gen_stack_protect_set(x,y) (gcc_unreachable (), NULL_RTX)
5565 #endif
5567 static void
5568 stack_protect_prologue (void)
5570 tree guard_decl = targetm.stack_protect_guard ();
5571 rtx x, y;
5573 x = expand_normal (crtl->stack_protect_guard);
5574 y = expand_normal (guard_decl);
5576 /* Allow the target to copy from Y to X without leaking Y into a
5577 register. */
5578 if (HAVE_stack_protect_set)
5580 rtx insn = gen_stack_protect_set (x, y);
5581 if (insn)
5583 emit_insn (insn);
5584 return;
5588 /* Otherwise do a straight move. */
5589 emit_move_insn (x, y);
5592 /* Translate the intermediate representation contained in the CFG
5593 from GIMPLE trees to RTL.
5595 We do conversion per basic block and preserve/update the tree CFG.
5596 This implies we have to do some magic as the CFG can simultaneously
5597 consist of basic blocks containing RTL and GIMPLE trees. This can
5598 confuse the CFG hooks, so be careful to not manipulate CFG during
5599 the expansion. */
5601 namespace {
5603 const pass_data pass_data_expand =
5605 RTL_PASS, /* type */
5606 "expand", /* name */
5607 OPTGROUP_NONE, /* optinfo_flags */
5608 TV_EXPAND, /* tv_id */
5609 ( PROP_ssa | PROP_gimple_leh | PROP_cfg
5610 | PROP_gimple_lcx
5611 | PROP_gimple_lvec ), /* properties_required */
5612 PROP_rtl, /* properties_provided */
5613 ( PROP_ssa | PROP_trees ), /* properties_destroyed */
5614 0, /* todo_flags_start */
5615 0, /* todo_flags_finish */
5618 class pass_expand : public rtl_opt_pass
5620 public:
5621 pass_expand (gcc::context *ctxt)
5622 : rtl_opt_pass (pass_data_expand, ctxt)
5625 /* opt_pass methods: */
5626 virtual unsigned int execute (function *);
5628 }; // class pass_expand
5630 unsigned int
5631 pass_expand::execute (function *fun)
5633 basic_block bb, init_block;
5634 sbitmap blocks;
5635 edge_iterator ei;
5636 edge e;
5637 rtx_insn *var_seq, *var_ret_seq;
5638 unsigned i;
5640 timevar_push (TV_OUT_OF_SSA);
5641 rewrite_out_of_ssa (&SA);
5642 timevar_pop (TV_OUT_OF_SSA);
5643 SA.partition_to_pseudo = XCNEWVEC (rtx, SA.map->num_partitions);
5645 /* Make sure all values used by the optimization passes have sane
5646 defaults. */
5647 reg_renumber = 0;
5649 /* Some backends want to know that we are expanding to RTL. */
5650 currently_expanding_to_rtl = 1;
5651 /* Dominators are not kept up-to-date as we may create new basic-blocks. */
5652 free_dominance_info (CDI_DOMINATORS);
5654 rtl_profile_for_bb (ENTRY_BLOCK_PTR_FOR_FN (fun));
5656 insn_locations_init ();
5657 if (!DECL_IS_BUILTIN (current_function_decl))
5659 /* Eventually, all FEs should explicitly set function_start_locus. */
5660 if (LOCATION_LOCUS (fun->function_start_locus) == UNKNOWN_LOCATION)
5661 set_curr_insn_location
5662 (DECL_SOURCE_LOCATION (current_function_decl));
5663 else
5664 set_curr_insn_location (fun->function_start_locus);
5666 else
5667 set_curr_insn_location (UNKNOWN_LOCATION);
5668 prologue_location = curr_insn_location ();
5670 #ifdef INSN_SCHEDULING
5671 init_sched_attrs ();
5672 #endif
5674 /* Make sure first insn is a note even if we don't want linenums.
5675 This makes sure the first insn will never be deleted.
5676 Also, final expects a note to appear there. */
5677 emit_note (NOTE_INSN_DELETED);
5679 /* Mark arrays indexed with non-constant indices with TREE_ADDRESSABLE. */
5680 discover_nonconstant_array_refs ();
5682 targetm.expand_to_rtl_hook ();
5683 crtl->stack_alignment_needed = STACK_BOUNDARY;
5684 crtl->max_used_stack_slot_alignment = STACK_BOUNDARY;
5685 crtl->stack_alignment_estimated = 0;
5686 crtl->preferred_stack_boundary = STACK_BOUNDARY;
5687 fun->cfg->max_jumptable_ents = 0;
5689 /* Resovle the function section. Some targets, like ARM EABI rely on knowledge
5690 of the function section at exapnsion time to predict distance of calls. */
5691 resolve_unique_section (current_function_decl, 0, flag_function_sections);
5693 /* Expand the variables recorded during gimple lowering. */
5694 timevar_push (TV_VAR_EXPAND);
5695 start_sequence ();
5697 var_ret_seq = expand_used_vars ();
5699 var_seq = get_insns ();
5700 end_sequence ();
5701 timevar_pop (TV_VAR_EXPAND);
5703 /* Honor stack protection warnings. */
5704 if (warn_stack_protect)
5706 if (fun->calls_alloca)
5707 warning (OPT_Wstack_protector,
5708 "stack protector not protecting local variables: "
5709 "variable length buffer");
5710 if (has_short_buffer && !crtl->stack_protect_guard)
5711 warning (OPT_Wstack_protector,
5712 "stack protector not protecting function: "
5713 "all local arrays are less than %d bytes long",
5714 (int) PARAM_VALUE (PARAM_SSP_BUFFER_SIZE));
5717 /* Set up parameters and prepare for return, for the function. */
5718 expand_function_start (current_function_decl);
5720 /* If we emitted any instructions for setting up the variables,
5721 emit them before the FUNCTION_START note. */
5722 if (var_seq)
5724 emit_insn_before (var_seq, parm_birth_insn);
5726 /* In expand_function_end we'll insert the alloca save/restore
5727 before parm_birth_insn. We've just insertted an alloca call.
5728 Adjust the pointer to match. */
5729 parm_birth_insn = var_seq;
5732 /* Now that we also have the parameter RTXs, copy them over to our
5733 partitions. */
5734 for (i = 0; i < SA.map->num_partitions; i++)
5736 tree var = SSA_NAME_VAR (partition_to_var (SA.map, i));
5738 if (TREE_CODE (var) != VAR_DECL
5739 && !SA.partition_to_pseudo[i])
5740 SA.partition_to_pseudo[i] = DECL_RTL_IF_SET (var);
5741 gcc_assert (SA.partition_to_pseudo[i]);
5743 /* If this decl was marked as living in multiple places, reset
5744 this now to NULL. */
5745 if (DECL_RTL_IF_SET (var) == pc_rtx)
5746 SET_DECL_RTL (var, NULL);
5748 /* Some RTL parts really want to look at DECL_RTL(x) when x
5749 was a decl marked in REG_ATTR or MEM_ATTR. We could use
5750 SET_DECL_RTL here making this available, but that would mean
5751 to select one of the potentially many RTLs for one DECL. Instead
5752 of doing that we simply reset the MEM_EXPR of the RTL in question,
5753 then nobody can get at it and hence nobody can call DECL_RTL on it. */
5754 if (!DECL_RTL_SET_P (var))
5756 if (MEM_P (SA.partition_to_pseudo[i]))
5757 set_mem_expr (SA.partition_to_pseudo[i], NULL);
5761 /* If we have a class containing differently aligned pointers
5762 we need to merge those into the corresponding RTL pointer
5763 alignment. */
5764 for (i = 1; i < num_ssa_names; i++)
5766 tree name = ssa_name (i);
5767 int part;
5768 rtx r;
5770 if (!name
5771 /* We might have generated new SSA names in
5772 update_alias_info_with_stack_vars. They will have a NULL
5773 defining statements, and won't be part of the partitioning,
5774 so ignore those. */
5775 || !SSA_NAME_DEF_STMT (name))
5776 continue;
5777 part = var_to_partition (SA.map, name);
5778 if (part == NO_PARTITION)
5779 continue;
5781 /* Adjust all partition members to get the underlying decl of
5782 the representative which we might have created in expand_one_var. */
5783 if (SSA_NAME_VAR (name) == NULL_TREE)
5785 tree leader = partition_to_var (SA.map, part);
5786 gcc_assert (SSA_NAME_VAR (leader) != NULL_TREE);
5787 replace_ssa_name_symbol (name, SSA_NAME_VAR (leader));
5789 if (!POINTER_TYPE_P (TREE_TYPE (name)))
5790 continue;
5792 r = SA.partition_to_pseudo[part];
5793 if (REG_P (r))
5794 mark_reg_pointer (r, get_pointer_alignment (name));
5797 /* If this function is `main', emit a call to `__main'
5798 to run global initializers, etc. */
5799 if (DECL_NAME (current_function_decl)
5800 && MAIN_NAME_P (DECL_NAME (current_function_decl))
5801 && DECL_FILE_SCOPE_P (current_function_decl))
5802 expand_main_function ();
5804 /* Initialize the stack_protect_guard field. This must happen after the
5805 call to __main (if any) so that the external decl is initialized. */
5806 if (crtl->stack_protect_guard)
5807 stack_protect_prologue ();
5809 expand_phi_nodes (&SA);
5811 /* Register rtl specific functions for cfg. */
5812 rtl_register_cfg_hooks ();
5814 init_block = construct_init_block ();
5816 /* Clear EDGE_EXECUTABLE on the entry edge(s). It is cleaned from the
5817 remaining edges later. */
5818 FOR_EACH_EDGE (e, ei, ENTRY_BLOCK_PTR_FOR_FN (fun)->succs)
5819 e->flags &= ~EDGE_EXECUTABLE;
5821 lab_rtx_for_bb = new hash_map<basic_block, rtx_code_label *>;
5822 FOR_BB_BETWEEN (bb, init_block->next_bb, EXIT_BLOCK_PTR_FOR_FN (fun),
5823 next_bb)
5824 bb = expand_gimple_basic_block (bb, var_ret_seq != NULL_RTX);
5826 if (MAY_HAVE_DEBUG_INSNS)
5827 expand_debug_locations ();
5829 /* Free stuff we no longer need after GIMPLE optimizations. */
5830 free_dominance_info (CDI_DOMINATORS);
5831 free_dominance_info (CDI_POST_DOMINATORS);
5832 delete_tree_cfg_annotations ();
5834 timevar_push (TV_OUT_OF_SSA);
5835 finish_out_of_ssa (&SA);
5836 timevar_pop (TV_OUT_OF_SSA);
5838 timevar_push (TV_POST_EXPAND);
5839 /* We are no longer in SSA form. */
5840 fun->gimple_df->in_ssa_p = false;
5841 loops_state_clear (LOOP_CLOSED_SSA);
5843 /* Expansion is used by optimization passes too, set maybe_hot_insn_p
5844 conservatively to true until they are all profile aware. */
5845 delete lab_rtx_for_bb;
5846 free_histograms ();
5848 construct_exit_block ();
5849 insn_locations_finalize ();
5851 if (var_ret_seq)
5853 rtx_insn *after = return_label;
5854 rtx_insn *next = NEXT_INSN (after);
5855 if (next && NOTE_INSN_BASIC_BLOCK_P (next))
5856 after = next;
5857 emit_insn_after (var_ret_seq, after);
5860 /* Zap the tree EH table. */
5861 set_eh_throw_stmt_table (fun, NULL);
5863 /* We need JUMP_LABEL be set in order to redirect jumps, and hence
5864 split edges which edge insertions might do. */
5865 rebuild_jump_labels (get_insns ());
5867 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (fun),
5868 EXIT_BLOCK_PTR_FOR_FN (fun), next_bb)
5870 edge e;
5871 edge_iterator ei;
5872 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
5874 if (e->insns.r)
5876 rebuild_jump_labels_chain (e->insns.r);
5877 /* Put insns after parm birth, but before
5878 NOTE_INSNS_FUNCTION_BEG. */
5879 if (e->src == ENTRY_BLOCK_PTR_FOR_FN (fun)
5880 && single_succ_p (ENTRY_BLOCK_PTR_FOR_FN (fun)))
5882 rtx_insn *insns = e->insns.r;
5883 e->insns.r = NULL;
5884 if (NOTE_P (parm_birth_insn)
5885 && NOTE_KIND (parm_birth_insn) == NOTE_INSN_FUNCTION_BEG)
5886 emit_insn_before_noloc (insns, parm_birth_insn, e->dest);
5887 else
5888 emit_insn_after_noloc (insns, parm_birth_insn, e->dest);
5890 else
5891 commit_one_edge_insertion (e);
5893 else
5894 ei_next (&ei);
5898 /* We're done expanding trees to RTL. */
5899 currently_expanding_to_rtl = 0;
5901 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (fun)->next_bb,
5902 EXIT_BLOCK_PTR_FOR_FN (fun), next_bb)
5904 edge e;
5905 edge_iterator ei;
5906 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
5908 /* Clear EDGE_EXECUTABLE. This flag is never used in the backend. */
5909 e->flags &= ~EDGE_EXECUTABLE;
5911 /* At the moment not all abnormal edges match the RTL
5912 representation. It is safe to remove them here as
5913 find_many_sub_basic_blocks will rediscover them.
5914 In the future we should get this fixed properly. */
5915 if ((e->flags & EDGE_ABNORMAL)
5916 && !(e->flags & EDGE_SIBCALL))
5917 remove_edge (e);
5918 else
5919 ei_next (&ei);
5923 blocks = sbitmap_alloc (last_basic_block_for_fn (fun));
5924 bitmap_ones (blocks);
5925 find_many_sub_basic_blocks (blocks);
5926 sbitmap_free (blocks);
5927 purge_all_dead_edges ();
5929 expand_stack_alignment ();
5931 /* Fixup REG_EQUIV notes in the prologue if there are tailcalls in this
5932 function. */
5933 if (crtl->tail_call_emit)
5934 fixup_tail_calls ();
5936 /* After initial rtl generation, call back to finish generating
5937 exception support code. We need to do this before cleaning up
5938 the CFG as the code does not expect dead landing pads. */
5939 if (fun->eh->region_tree != NULL)
5940 finish_eh_generation ();
5942 /* Remove unreachable blocks, otherwise we cannot compute dominators
5943 which are needed for loop state verification. As a side-effect
5944 this also compacts blocks.
5945 ??? We cannot remove trivially dead insns here as for example
5946 the DRAP reg on i?86 is not magically live at this point.
5947 gcc.c-torture/execute/ipa-sra-2.c execution, -Os -m32 fails otherwise. */
5948 cleanup_cfg (CLEANUP_NO_INSN_DEL);
5950 #ifdef ENABLE_CHECKING
5951 verify_flow_info ();
5952 #endif
5954 /* Initialize pseudos allocated for hard registers. */
5955 emit_initial_value_sets ();
5957 /* And finally unshare all RTL. */
5958 unshare_all_rtl ();
5960 /* There's no need to defer outputting this function any more; we
5961 know we want to output it. */
5962 DECL_DEFER_OUTPUT (current_function_decl) = 0;
5964 /* Now that we're done expanding trees to RTL, we shouldn't have any
5965 more CONCATs anywhere. */
5966 generating_concat_p = 0;
5968 if (dump_file)
5970 fprintf (dump_file,
5971 "\n\n;;\n;; Full RTL generated for this function:\n;;\n");
5972 /* And the pass manager will dump RTL for us. */
5975 /* If we're emitting a nested function, make sure its parent gets
5976 emitted as well. Doing otherwise confuses debug info. */
5978 tree parent;
5979 for (parent = DECL_CONTEXT (current_function_decl);
5980 parent != NULL_TREE;
5981 parent = get_containing_scope (parent))
5982 if (TREE_CODE (parent) == FUNCTION_DECL)
5983 TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (parent)) = 1;
5986 /* We are now committed to emitting code for this function. Do any
5987 preparation, such as emitting abstract debug info for the inline
5988 before it gets mangled by optimization. */
5989 if (cgraph_function_possibly_inlined_p (current_function_decl))
5990 (*debug_hooks->outlining_inline_function) (current_function_decl);
5992 TREE_ASM_WRITTEN (current_function_decl) = 1;
5994 /* After expanding, the return labels are no longer needed. */
5995 return_label = NULL;
5996 naked_return_label = NULL;
5998 /* After expanding, the tm_restart map is no longer needed. */
5999 if (fun->gimple_df->tm_restart)
6001 htab_delete (fun->gimple_df->tm_restart);
6002 fun->gimple_df->tm_restart = NULL;
6005 /* Tag the blocks with a depth number so that change_scope can find
6006 the common parent easily. */
6007 set_block_levels (DECL_INITIAL (fun->decl), 0);
6008 default_rtl_profile ();
6010 timevar_pop (TV_POST_EXPAND);
6012 return 0;
6015 } // anon namespace
6017 rtl_opt_pass *
6018 make_pass_expand (gcc::context *ctxt)
6020 return new pass_expand (ctxt);