Preparatory work before subclass renaming
[official-gcc.git] / gcc / cfgexpand.c
blob88bcd72a98acd4530515aac15dfaf1253d91b98d
1 /* A pass for lowering trees to RTL.
2 Copyright (C) 2004-2014 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
9 any later version.
11 GCC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "tm.h"
24 #include "rtl.h"
25 #include "hard-reg-set.h"
26 #include "tree.h"
27 #include "stringpool.h"
28 #include "varasm.h"
29 #include "stor-layout.h"
30 #include "stmt.h"
31 #include "print-tree.h"
32 #include "tm_p.h"
33 #include "basic-block.h"
34 #include "function.h"
35 #include "expr.h"
36 #include "langhooks.h"
37 #include "bitmap.h"
38 #include "hash-set.h"
39 #include "tree-ssa-alias.h"
40 #include "internal-fn.h"
41 #include "tree-eh.h"
42 #include "gimple-expr.h"
43 #include "is-a.h"
44 #include "gimple.h"
45 #include "gimple-iterator.h"
46 #include "gimple-walk.h"
47 #include "gimple-ssa.h"
48 #include "cgraph.h"
49 #include "tree-cfg.h"
50 #include "tree-phinodes.h"
51 #include "ssa-iterators.h"
52 #include "tree-ssanames.h"
53 #include "tree-dfa.h"
54 #include "tree-ssa.h"
55 #include "tree-pass.h"
56 #include "except.h"
57 #include "flags.h"
58 #include "diagnostic.h"
59 #include "gimple-pretty-print.h"
60 #include "toplev.h"
61 #include "debug.h"
62 #include "params.h"
63 #include "tree-inline.h"
64 #include "value-prof.h"
65 #include "target.h"
66 #include "tree-ssa-live.h"
67 #include "tree-outof-ssa.h"
68 #include "sbitmap.h"
69 #include "cfgloop.h"
70 #include "regs.h" /* For reg_renumber. */
71 #include "insn-attr.h" /* For INSN_SCHEDULING. */
72 #include "asan.h"
73 #include "tree-ssa-address.h"
74 #include "recog.h"
75 #include "output.h"
76 #include "builtins.h"
78 /* Some systems use __main in a way incompatible with its use in gcc, in these
79 cases use the macros NAME__MAIN to give a quoted symbol and SYMBOL__MAIN to
80 give the same symbol without quotes for an alternative entry point. You
81 must define both, or neither. */
82 #ifndef NAME__MAIN
83 #define NAME__MAIN "__main"
84 #endif
86 /* This variable holds information helping the rewriting of SSA trees
87 into RTL. */
88 struct ssaexpand SA;
90 /* This variable holds the currently expanded gimple statement for purposes
91 of comminucating the profile info to the builtin expanders. */
92 gimple currently_expanding_gimple_stmt;
94 static rtx expand_debug_expr (tree);
96 /* Return an expression tree corresponding to the RHS of GIMPLE
97 statement STMT. */
99 tree
100 gimple_assign_rhs_to_tree (gimple stmt)
102 tree t;
103 enum gimple_rhs_class grhs_class;
105 grhs_class = get_gimple_rhs_class (gimple_expr_code (stmt));
107 if (grhs_class == GIMPLE_TERNARY_RHS)
108 t = build3 (gimple_assign_rhs_code (stmt),
109 TREE_TYPE (gimple_assign_lhs (stmt)),
110 gimple_assign_rhs1 (stmt),
111 gimple_assign_rhs2 (stmt),
112 gimple_assign_rhs3 (stmt));
113 else if (grhs_class == GIMPLE_BINARY_RHS)
114 t = build2 (gimple_assign_rhs_code (stmt),
115 TREE_TYPE (gimple_assign_lhs (stmt)),
116 gimple_assign_rhs1 (stmt),
117 gimple_assign_rhs2 (stmt));
118 else if (grhs_class == GIMPLE_UNARY_RHS)
119 t = build1 (gimple_assign_rhs_code (stmt),
120 TREE_TYPE (gimple_assign_lhs (stmt)),
121 gimple_assign_rhs1 (stmt));
122 else if (grhs_class == GIMPLE_SINGLE_RHS)
124 t = gimple_assign_rhs1 (stmt);
125 /* Avoid modifying this tree in place below. */
126 if ((gimple_has_location (stmt) && CAN_HAVE_LOCATION_P (t)
127 && gimple_location (stmt) != EXPR_LOCATION (t))
128 || (gimple_block (stmt)
129 && currently_expanding_to_rtl
130 && EXPR_P (t)))
131 t = copy_node (t);
133 else
134 gcc_unreachable ();
136 if (gimple_has_location (stmt) && CAN_HAVE_LOCATION_P (t))
137 SET_EXPR_LOCATION (t, gimple_location (stmt));
139 return t;
143 #ifndef STACK_ALIGNMENT_NEEDED
144 #define STACK_ALIGNMENT_NEEDED 1
145 #endif
147 #define SSAVAR(x) (TREE_CODE (x) == SSA_NAME ? SSA_NAME_VAR (x) : x)
149 /* Associate declaration T with storage space X. If T is no
150 SSA name this is exactly SET_DECL_RTL, otherwise make the
151 partition of T associated with X. */
152 static inline void
153 set_rtl (tree t, rtx x)
155 if (TREE_CODE (t) == SSA_NAME)
157 SA.partition_to_pseudo[var_to_partition (SA.map, t)] = x;
158 if (x && !MEM_P (x))
159 set_reg_attrs_for_decl_rtl (SSA_NAME_VAR (t), x);
160 /* For the benefit of debug information at -O0 (where vartracking
161 doesn't run) record the place also in the base DECL if it's
162 a normal variable (not a parameter). */
163 if (x && x != pc_rtx && TREE_CODE (SSA_NAME_VAR (t)) == VAR_DECL)
165 tree var = SSA_NAME_VAR (t);
166 /* If we don't yet have something recorded, just record it now. */
167 if (!DECL_RTL_SET_P (var))
168 SET_DECL_RTL (var, x);
169 /* If we have it set already to "multiple places" don't
170 change this. */
171 else if (DECL_RTL (var) == pc_rtx)
173 /* If we have something recorded and it's not the same place
174 as we want to record now, we have multiple partitions for the
175 same base variable, with different places. We can't just
176 randomly chose one, hence we have to say that we don't know.
177 This only happens with optimization, and there var-tracking
178 will figure out the right thing. */
179 else if (DECL_RTL (var) != x)
180 SET_DECL_RTL (var, pc_rtx);
183 else
184 SET_DECL_RTL (t, x);
187 /* This structure holds data relevant to one variable that will be
188 placed in a stack slot. */
189 struct stack_var
191 /* The Variable. */
192 tree decl;
194 /* Initially, the size of the variable. Later, the size of the partition,
195 if this variable becomes it's partition's representative. */
196 HOST_WIDE_INT size;
198 /* The *byte* alignment required for this variable. Or as, with the
199 size, the alignment for this partition. */
200 unsigned int alignb;
202 /* The partition representative. */
203 size_t representative;
205 /* The next stack variable in the partition, or EOC. */
206 size_t next;
208 /* The numbers of conflicting stack variables. */
209 bitmap conflicts;
212 #define EOC ((size_t)-1)
214 /* We have an array of such objects while deciding allocation. */
215 static struct stack_var *stack_vars;
216 static size_t stack_vars_alloc;
217 static size_t stack_vars_num;
218 static hash_map<tree, size_t> *decl_to_stack_part;
220 /* Conflict bitmaps go on this obstack. This allows us to destroy
221 all of them in one big sweep. */
222 static bitmap_obstack stack_var_bitmap_obstack;
224 /* An array of indices such that stack_vars[stack_vars_sorted[i]].size
225 is non-decreasing. */
226 static size_t *stack_vars_sorted;
228 /* The phase of the stack frame. This is the known misalignment of
229 virtual_stack_vars_rtx from PREFERRED_STACK_BOUNDARY. That is,
230 (frame_offset+frame_phase) % PREFERRED_STACK_BOUNDARY == 0. */
231 static int frame_phase;
233 /* Used during expand_used_vars to remember if we saw any decls for
234 which we'd like to enable stack smashing protection. */
235 static bool has_protected_decls;
237 /* Used during expand_used_vars. Remember if we say a character buffer
238 smaller than our cutoff threshold. Used for -Wstack-protector. */
239 static bool has_short_buffer;
241 /* Compute the byte alignment to use for DECL. Ignore alignment
242 we can't do with expected alignment of the stack boundary. */
244 static unsigned int
245 align_local_variable (tree decl)
247 unsigned int align = LOCAL_DECL_ALIGNMENT (decl);
248 DECL_ALIGN (decl) = align;
249 return align / BITS_PER_UNIT;
252 /* Allocate SIZE bytes at byte alignment ALIGN from the stack frame.
253 Return the frame offset. */
255 static HOST_WIDE_INT
256 alloc_stack_frame_space (HOST_WIDE_INT size, unsigned HOST_WIDE_INT align)
258 HOST_WIDE_INT offset, new_frame_offset;
260 new_frame_offset = frame_offset;
261 if (FRAME_GROWS_DOWNWARD)
263 new_frame_offset -= size + frame_phase;
264 new_frame_offset &= -align;
265 new_frame_offset += frame_phase;
266 offset = new_frame_offset;
268 else
270 new_frame_offset -= frame_phase;
271 new_frame_offset += align - 1;
272 new_frame_offset &= -align;
273 new_frame_offset += frame_phase;
274 offset = new_frame_offset;
275 new_frame_offset += size;
277 frame_offset = new_frame_offset;
279 if (frame_offset_overflow (frame_offset, cfun->decl))
280 frame_offset = offset = 0;
282 return offset;
285 /* Accumulate DECL into STACK_VARS. */
287 static void
288 add_stack_var (tree decl)
290 struct stack_var *v;
292 if (stack_vars_num >= stack_vars_alloc)
294 if (stack_vars_alloc)
295 stack_vars_alloc = stack_vars_alloc * 3 / 2;
296 else
297 stack_vars_alloc = 32;
298 stack_vars
299 = XRESIZEVEC (struct stack_var, stack_vars, stack_vars_alloc);
301 if (!decl_to_stack_part)
302 decl_to_stack_part = new hash_map<tree, size_t>;
304 v = &stack_vars[stack_vars_num];
305 decl_to_stack_part->put (decl, stack_vars_num);
307 v->decl = decl;
308 v->size = tree_to_uhwi (DECL_SIZE_UNIT (SSAVAR (decl)));
309 /* Ensure that all variables have size, so that &a != &b for any two
310 variables that are simultaneously live. */
311 if (v->size == 0)
312 v->size = 1;
313 v->alignb = align_local_variable (SSAVAR (decl));
314 /* An alignment of zero can mightily confuse us later. */
315 gcc_assert (v->alignb != 0);
317 /* All variables are initially in their own partition. */
318 v->representative = stack_vars_num;
319 v->next = EOC;
321 /* All variables initially conflict with no other. */
322 v->conflicts = NULL;
324 /* Ensure that this decl doesn't get put onto the list twice. */
325 set_rtl (decl, pc_rtx);
327 stack_vars_num++;
330 /* Make the decls associated with luid's X and Y conflict. */
332 static void
333 add_stack_var_conflict (size_t x, size_t y)
335 struct stack_var *a = &stack_vars[x];
336 struct stack_var *b = &stack_vars[y];
337 if (!a->conflicts)
338 a->conflicts = BITMAP_ALLOC (&stack_var_bitmap_obstack);
339 if (!b->conflicts)
340 b->conflicts = BITMAP_ALLOC (&stack_var_bitmap_obstack);
341 bitmap_set_bit (a->conflicts, y);
342 bitmap_set_bit (b->conflicts, x);
345 /* Check whether the decls associated with luid's X and Y conflict. */
347 static bool
348 stack_var_conflict_p (size_t x, size_t y)
350 struct stack_var *a = &stack_vars[x];
351 struct stack_var *b = &stack_vars[y];
352 if (x == y)
353 return false;
354 /* Partitions containing an SSA name result from gimple registers
355 with things like unsupported modes. They are top-level and
356 hence conflict with everything else. */
357 if (TREE_CODE (a->decl) == SSA_NAME || TREE_CODE (b->decl) == SSA_NAME)
358 return true;
360 if (!a->conflicts || !b->conflicts)
361 return false;
362 return bitmap_bit_p (a->conflicts, y);
365 /* Callback for walk_stmt_ops. If OP is a decl touched by add_stack_var
366 enter its partition number into bitmap DATA. */
368 static bool
369 visit_op (gimple, tree op, tree, void *data)
371 bitmap active = (bitmap)data;
372 op = get_base_address (op);
373 if (op
374 && DECL_P (op)
375 && DECL_RTL_IF_SET (op) == pc_rtx)
377 size_t *v = decl_to_stack_part->get (op);
378 if (v)
379 bitmap_set_bit (active, *v);
381 return false;
384 /* Callback for walk_stmt_ops. If OP is a decl touched by add_stack_var
385 record conflicts between it and all currently active other partitions
386 from bitmap DATA. */
388 static bool
389 visit_conflict (gimple, tree op, tree, void *data)
391 bitmap active = (bitmap)data;
392 op = get_base_address (op);
393 if (op
394 && DECL_P (op)
395 && DECL_RTL_IF_SET (op) == pc_rtx)
397 size_t *v = decl_to_stack_part->get (op);
398 if (v && bitmap_set_bit (active, *v))
400 size_t num = *v;
401 bitmap_iterator bi;
402 unsigned i;
403 gcc_assert (num < stack_vars_num);
404 EXECUTE_IF_SET_IN_BITMAP (active, 0, i, bi)
405 add_stack_var_conflict (num, i);
408 return false;
411 /* Helper routine for add_scope_conflicts, calculating the active partitions
412 at the end of BB, leaving the result in WORK. We're called to generate
413 conflicts when FOR_CONFLICT is true, otherwise we're just tracking
414 liveness. */
416 static void
417 add_scope_conflicts_1 (basic_block bb, bitmap work, bool for_conflict)
419 edge e;
420 edge_iterator ei;
421 gimple_stmt_iterator gsi;
422 walk_stmt_load_store_addr_fn visit;
424 bitmap_clear (work);
425 FOR_EACH_EDGE (e, ei, bb->preds)
426 bitmap_ior_into (work, (bitmap)e->src->aux);
428 visit = visit_op;
430 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
432 gimple stmt = gsi_stmt (gsi);
433 walk_stmt_load_store_addr_ops (stmt, work, NULL, NULL, visit);
435 for (gsi = gsi_after_labels (bb); !gsi_end_p (gsi); gsi_next (&gsi))
437 gimple stmt = gsi_stmt (gsi);
439 if (gimple_clobber_p (stmt))
441 tree lhs = gimple_assign_lhs (stmt);
442 size_t *v;
443 /* Nested function lowering might introduce LHSs
444 that are COMPONENT_REFs. */
445 if (TREE_CODE (lhs) != VAR_DECL)
446 continue;
447 if (DECL_RTL_IF_SET (lhs) == pc_rtx
448 && (v = decl_to_stack_part->get (lhs)))
449 bitmap_clear_bit (work, *v);
451 else if (!is_gimple_debug (stmt))
453 if (for_conflict
454 && visit == visit_op)
456 /* If this is the first real instruction in this BB we need
457 to add conflicts for everything live at this point now.
458 Unlike classical liveness for named objects we can't
459 rely on seeing a def/use of the names we're interested in.
460 There might merely be indirect loads/stores. We'd not add any
461 conflicts for such partitions. */
462 bitmap_iterator bi;
463 unsigned i;
464 EXECUTE_IF_SET_IN_BITMAP (work, 0, i, bi)
466 struct stack_var *a = &stack_vars[i];
467 if (!a->conflicts)
468 a->conflicts = BITMAP_ALLOC (&stack_var_bitmap_obstack);
469 bitmap_ior_into (a->conflicts, work);
471 visit = visit_conflict;
473 walk_stmt_load_store_addr_ops (stmt, work, visit, visit, visit);
478 /* Generate stack partition conflicts between all partitions that are
479 simultaneously live. */
481 static void
482 add_scope_conflicts (void)
484 basic_block bb;
485 bool changed;
486 bitmap work = BITMAP_ALLOC (NULL);
487 int *rpo;
488 int n_bbs;
490 /* We approximate the live range of a stack variable by taking the first
491 mention of its name as starting point(s), and by the end-of-scope
492 death clobber added by gimplify as ending point(s) of the range.
493 This overapproximates in the case we for instance moved an address-taken
494 operation upward, without also moving a dereference to it upwards.
495 But it's conservatively correct as a variable never can hold values
496 before its name is mentioned at least once.
498 We then do a mostly classical bitmap liveness algorithm. */
500 FOR_ALL_BB_FN (bb, cfun)
501 bb->aux = BITMAP_ALLOC (&stack_var_bitmap_obstack);
503 rpo = XNEWVEC (int, last_basic_block_for_fn (cfun));
504 n_bbs = pre_and_rev_post_order_compute (NULL, rpo, false);
506 changed = true;
507 while (changed)
509 int i;
510 changed = false;
511 for (i = 0; i < n_bbs; i++)
513 bitmap active;
514 bb = BASIC_BLOCK_FOR_FN (cfun, rpo[i]);
515 active = (bitmap)bb->aux;
516 add_scope_conflicts_1 (bb, work, false);
517 if (bitmap_ior_into (active, work))
518 changed = true;
522 FOR_EACH_BB_FN (bb, cfun)
523 add_scope_conflicts_1 (bb, work, true);
525 free (rpo);
526 BITMAP_FREE (work);
527 FOR_ALL_BB_FN (bb, cfun)
528 BITMAP_FREE (bb->aux);
531 /* A subroutine of partition_stack_vars. A comparison function for qsort,
532 sorting an array of indices by the properties of the object. */
534 static int
535 stack_var_cmp (const void *a, const void *b)
537 size_t ia = *(const size_t *)a;
538 size_t ib = *(const size_t *)b;
539 unsigned int aligna = stack_vars[ia].alignb;
540 unsigned int alignb = stack_vars[ib].alignb;
541 HOST_WIDE_INT sizea = stack_vars[ia].size;
542 HOST_WIDE_INT sizeb = stack_vars[ib].size;
543 tree decla = stack_vars[ia].decl;
544 tree declb = stack_vars[ib].decl;
545 bool largea, largeb;
546 unsigned int uida, uidb;
548 /* Primary compare on "large" alignment. Large comes first. */
549 largea = (aligna * BITS_PER_UNIT > MAX_SUPPORTED_STACK_ALIGNMENT);
550 largeb = (alignb * BITS_PER_UNIT > MAX_SUPPORTED_STACK_ALIGNMENT);
551 if (largea != largeb)
552 return (int)largeb - (int)largea;
554 /* Secondary compare on size, decreasing */
555 if (sizea > sizeb)
556 return -1;
557 if (sizea < sizeb)
558 return 1;
560 /* Tertiary compare on true alignment, decreasing. */
561 if (aligna < alignb)
562 return -1;
563 if (aligna > alignb)
564 return 1;
566 /* Final compare on ID for sort stability, increasing.
567 Two SSA names are compared by their version, SSA names come before
568 non-SSA names, and two normal decls are compared by their DECL_UID. */
569 if (TREE_CODE (decla) == SSA_NAME)
571 if (TREE_CODE (declb) == SSA_NAME)
572 uida = SSA_NAME_VERSION (decla), uidb = SSA_NAME_VERSION (declb);
573 else
574 return -1;
576 else if (TREE_CODE (declb) == SSA_NAME)
577 return 1;
578 else
579 uida = DECL_UID (decla), uidb = DECL_UID (declb);
580 if (uida < uidb)
581 return 1;
582 if (uida > uidb)
583 return -1;
584 return 0;
587 struct part_traits : default_hashmap_traits
589 template<typename T>
590 static bool
591 is_deleted (T &e)
592 { return e.m_value == reinterpret_cast<void *> (1); }
594 template<typename T> static bool is_empty (T &e) { return e.m_value == NULL; }
595 template<typename T>
596 static void
597 mark_deleted (T &e)
598 { e.m_value = reinterpret_cast<T> (1); }
600 template<typename T>
601 static void
602 mark_empty (T &e)
603 { e.m_value = NULL; }
606 typedef hash_map<size_t, bitmap, part_traits> part_hashmap;
608 /* If the points-to solution *PI points to variables that are in a partition
609 together with other variables add all partition members to the pointed-to
610 variables bitmap. */
612 static void
613 add_partitioned_vars_to_ptset (struct pt_solution *pt,
614 part_hashmap *decls_to_partitions,
615 hash_set<bitmap> *visited, bitmap temp)
617 bitmap_iterator bi;
618 unsigned i;
619 bitmap *part;
621 if (pt->anything
622 || pt->vars == NULL
623 /* The pointed-to vars bitmap is shared, it is enough to
624 visit it once. */
625 || visited->add (pt->vars))
626 return;
628 bitmap_clear (temp);
630 /* By using a temporary bitmap to store all members of the partitions
631 we have to add we make sure to visit each of the partitions only
632 once. */
633 EXECUTE_IF_SET_IN_BITMAP (pt->vars, 0, i, bi)
634 if ((!temp
635 || !bitmap_bit_p (temp, i))
636 && (part = decls_to_partitions->get (i)))
637 bitmap_ior_into (temp, *part);
638 if (!bitmap_empty_p (temp))
639 bitmap_ior_into (pt->vars, temp);
642 /* Update points-to sets based on partition info, so we can use them on RTL.
643 The bitmaps representing stack partitions will be saved until expand,
644 where partitioned decls used as bases in memory expressions will be
645 rewritten. */
647 static void
648 update_alias_info_with_stack_vars (void)
650 part_hashmap *decls_to_partitions = NULL;
651 size_t i, j;
652 tree var = NULL_TREE;
654 for (i = 0; i < stack_vars_num; i++)
656 bitmap part = NULL;
657 tree name;
658 struct ptr_info_def *pi;
660 /* Not interested in partitions with single variable. */
661 if (stack_vars[i].representative != i
662 || stack_vars[i].next == EOC)
663 continue;
665 if (!decls_to_partitions)
667 decls_to_partitions = new part_hashmap;
668 cfun->gimple_df->decls_to_pointers = new hash_map<tree, tree>;
671 /* Create an SSA_NAME that points to the partition for use
672 as base during alias-oracle queries on RTL for bases that
673 have been partitioned. */
674 if (var == NULL_TREE)
675 var = create_tmp_var (ptr_type_node, NULL);
676 name = make_ssa_name (var, NULL);
678 /* Create bitmaps representing partitions. They will be used for
679 points-to sets later, so use GGC alloc. */
680 part = BITMAP_GGC_ALLOC ();
681 for (j = i; j != EOC; j = stack_vars[j].next)
683 tree decl = stack_vars[j].decl;
684 unsigned int uid = DECL_PT_UID (decl);
685 bitmap_set_bit (part, uid);
686 decls_to_partitions->put (uid, part);
687 cfun->gimple_df->decls_to_pointers->put (decl, name);
688 if (TREE_ADDRESSABLE (decl))
689 TREE_ADDRESSABLE (name) = 1;
692 /* Make the SSA name point to all partition members. */
693 pi = get_ptr_info (name);
694 pt_solution_set (&pi->pt, part, false);
697 /* Make all points-to sets that contain one member of a partition
698 contain all members of the partition. */
699 if (decls_to_partitions)
701 unsigned i;
702 hash_set<bitmap> visited;
703 bitmap temp = BITMAP_ALLOC (&stack_var_bitmap_obstack);
705 for (i = 1; i < num_ssa_names; i++)
707 tree name = ssa_name (i);
708 struct ptr_info_def *pi;
710 if (name
711 && POINTER_TYPE_P (TREE_TYPE (name))
712 && ((pi = SSA_NAME_PTR_INFO (name)) != NULL))
713 add_partitioned_vars_to_ptset (&pi->pt, decls_to_partitions,
714 &visited, temp);
717 add_partitioned_vars_to_ptset (&cfun->gimple_df->escaped,
718 decls_to_partitions, &visited, temp);
720 delete decls_to_partitions;
721 BITMAP_FREE (temp);
725 /* A subroutine of partition_stack_vars. The UNION portion of a UNION/FIND
726 partitioning algorithm. Partitions A and B are known to be non-conflicting.
727 Merge them into a single partition A. */
729 static void
730 union_stack_vars (size_t a, size_t b)
732 struct stack_var *vb = &stack_vars[b];
733 bitmap_iterator bi;
734 unsigned u;
736 gcc_assert (stack_vars[b].next == EOC);
737 /* Add B to A's partition. */
738 stack_vars[b].next = stack_vars[a].next;
739 stack_vars[b].representative = a;
740 stack_vars[a].next = b;
742 /* Update the required alignment of partition A to account for B. */
743 if (stack_vars[a].alignb < stack_vars[b].alignb)
744 stack_vars[a].alignb = stack_vars[b].alignb;
746 /* Update the interference graph and merge the conflicts. */
747 if (vb->conflicts)
749 EXECUTE_IF_SET_IN_BITMAP (vb->conflicts, 0, u, bi)
750 add_stack_var_conflict (a, stack_vars[u].representative);
751 BITMAP_FREE (vb->conflicts);
755 /* A subroutine of expand_used_vars. Binpack the variables into
756 partitions constrained by the interference graph. The overall
757 algorithm used is as follows:
759 Sort the objects by size in descending order.
760 For each object A {
761 S = size(A)
762 O = 0
763 loop {
764 Look for the largest non-conflicting object B with size <= S.
765 UNION (A, B)
770 static void
771 partition_stack_vars (void)
773 size_t si, sj, n = stack_vars_num;
775 stack_vars_sorted = XNEWVEC (size_t, stack_vars_num);
776 for (si = 0; si < n; ++si)
777 stack_vars_sorted[si] = si;
779 if (n == 1)
780 return;
782 qsort (stack_vars_sorted, n, sizeof (size_t), stack_var_cmp);
784 for (si = 0; si < n; ++si)
786 size_t i = stack_vars_sorted[si];
787 unsigned int ialign = stack_vars[i].alignb;
788 HOST_WIDE_INT isize = stack_vars[i].size;
790 /* Ignore objects that aren't partition representatives. If we
791 see a var that is not a partition representative, it must
792 have been merged earlier. */
793 if (stack_vars[i].representative != i)
794 continue;
796 for (sj = si + 1; sj < n; ++sj)
798 size_t j = stack_vars_sorted[sj];
799 unsigned int jalign = stack_vars[j].alignb;
800 HOST_WIDE_INT jsize = stack_vars[j].size;
802 /* Ignore objects that aren't partition representatives. */
803 if (stack_vars[j].representative != j)
804 continue;
806 /* Do not mix objects of "small" (supported) alignment
807 and "large" (unsupported) alignment. */
808 if ((ialign * BITS_PER_UNIT <= MAX_SUPPORTED_STACK_ALIGNMENT)
809 != (jalign * BITS_PER_UNIT <= MAX_SUPPORTED_STACK_ALIGNMENT))
810 break;
812 /* For Address Sanitizer do not mix objects with different
813 sizes, as the shorter vars wouldn't be adequately protected.
814 Don't do that for "large" (unsupported) alignment objects,
815 those aren't protected anyway. */
816 if ((flag_sanitize & SANITIZE_ADDRESS) && ASAN_STACK && isize != jsize
817 && ialign * BITS_PER_UNIT <= MAX_SUPPORTED_STACK_ALIGNMENT)
818 break;
820 /* Ignore conflicting objects. */
821 if (stack_var_conflict_p (i, j))
822 continue;
824 /* UNION the objects, placing J at OFFSET. */
825 union_stack_vars (i, j);
829 update_alias_info_with_stack_vars ();
832 /* A debugging aid for expand_used_vars. Dump the generated partitions. */
834 static void
835 dump_stack_var_partition (void)
837 size_t si, i, j, n = stack_vars_num;
839 for (si = 0; si < n; ++si)
841 i = stack_vars_sorted[si];
843 /* Skip variables that aren't partition representatives, for now. */
844 if (stack_vars[i].representative != i)
845 continue;
847 fprintf (dump_file, "Partition %lu: size " HOST_WIDE_INT_PRINT_DEC
848 " align %u\n", (unsigned long) i, stack_vars[i].size,
849 stack_vars[i].alignb);
851 for (j = i; j != EOC; j = stack_vars[j].next)
853 fputc ('\t', dump_file);
854 print_generic_expr (dump_file, stack_vars[j].decl, dump_flags);
856 fputc ('\n', dump_file);
860 /* Assign rtl to DECL at BASE + OFFSET. */
862 static void
863 expand_one_stack_var_at (tree decl, rtx base, unsigned base_align,
864 HOST_WIDE_INT offset)
866 unsigned align;
867 rtx x;
869 /* If this fails, we've overflowed the stack frame. Error nicely? */
870 gcc_assert (offset == trunc_int_for_mode (offset, Pmode));
872 x = plus_constant (Pmode, base, offset);
873 x = gen_rtx_MEM (DECL_MODE (SSAVAR (decl)), x);
875 if (TREE_CODE (decl) != SSA_NAME)
877 /* Set alignment we actually gave this decl if it isn't an SSA name.
878 If it is we generate stack slots only accidentally so it isn't as
879 important, we'll simply use the alignment that is already set. */
880 if (base == virtual_stack_vars_rtx)
881 offset -= frame_phase;
882 align = offset & -offset;
883 align *= BITS_PER_UNIT;
884 if (align == 0 || align > base_align)
885 align = base_align;
887 /* One would think that we could assert that we're not decreasing
888 alignment here, but (at least) the i386 port does exactly this
889 via the MINIMUM_ALIGNMENT hook. */
891 DECL_ALIGN (decl) = align;
892 DECL_USER_ALIGN (decl) = 0;
895 set_mem_attributes (x, SSAVAR (decl), true);
896 set_rtl (decl, x);
899 struct stack_vars_data
901 /* Vector of offset pairs, always end of some padding followed
902 by start of the padding that needs Address Sanitizer protection.
903 The vector is in reversed, highest offset pairs come first. */
904 vec<HOST_WIDE_INT> asan_vec;
906 /* Vector of partition representative decls in between the paddings. */
907 vec<tree> asan_decl_vec;
909 /* Base pseudo register for Address Sanitizer protected automatic vars. */
910 rtx asan_base;
912 /* Alignment needed for the Address Sanitizer protected automatic vars. */
913 unsigned int asan_alignb;
916 /* A subroutine of expand_used_vars. Give each partition representative
917 a unique location within the stack frame. Update each partition member
918 with that location. */
920 static void
921 expand_stack_vars (bool (*pred) (size_t), struct stack_vars_data *data)
923 size_t si, i, j, n = stack_vars_num;
924 HOST_WIDE_INT large_size = 0, large_alloc = 0;
925 rtx large_base = NULL;
926 unsigned large_align = 0;
927 tree decl;
929 /* Determine if there are any variables requiring "large" alignment.
930 Since these are dynamically allocated, we only process these if
931 no predicate involved. */
932 large_align = stack_vars[stack_vars_sorted[0]].alignb * BITS_PER_UNIT;
933 if (pred == NULL && large_align > MAX_SUPPORTED_STACK_ALIGNMENT)
935 /* Find the total size of these variables. */
936 for (si = 0; si < n; ++si)
938 unsigned alignb;
940 i = stack_vars_sorted[si];
941 alignb = stack_vars[i].alignb;
943 /* Stop when we get to the first decl with "small" alignment. */
944 if (alignb * BITS_PER_UNIT <= MAX_SUPPORTED_STACK_ALIGNMENT)
945 break;
947 /* Skip variables that aren't partition representatives. */
948 if (stack_vars[i].representative != i)
949 continue;
951 /* Skip variables that have already had rtl assigned. See also
952 add_stack_var where we perpetrate this pc_rtx hack. */
953 decl = stack_vars[i].decl;
954 if ((TREE_CODE (decl) == SSA_NAME
955 ? SA.partition_to_pseudo[var_to_partition (SA.map, decl)]
956 : DECL_RTL (decl)) != pc_rtx)
957 continue;
959 large_size += alignb - 1;
960 large_size &= -(HOST_WIDE_INT)alignb;
961 large_size += stack_vars[i].size;
964 /* If there were any, allocate space. */
965 if (large_size > 0)
966 large_base = allocate_dynamic_stack_space (GEN_INT (large_size), 0,
967 large_align, true);
970 for (si = 0; si < n; ++si)
972 rtx base;
973 unsigned base_align, alignb;
974 HOST_WIDE_INT offset;
976 i = stack_vars_sorted[si];
978 /* Skip variables that aren't partition representatives, for now. */
979 if (stack_vars[i].representative != i)
980 continue;
982 /* Skip variables that have already had rtl assigned. See also
983 add_stack_var where we perpetrate this pc_rtx hack. */
984 decl = stack_vars[i].decl;
985 if ((TREE_CODE (decl) == SSA_NAME
986 ? SA.partition_to_pseudo[var_to_partition (SA.map, decl)]
987 : DECL_RTL (decl)) != pc_rtx)
988 continue;
990 /* Check the predicate to see whether this variable should be
991 allocated in this pass. */
992 if (pred && !pred (i))
993 continue;
995 alignb = stack_vars[i].alignb;
996 if (alignb * BITS_PER_UNIT <= MAX_SUPPORTED_STACK_ALIGNMENT)
998 base = virtual_stack_vars_rtx;
999 if ((flag_sanitize & SANITIZE_ADDRESS) && ASAN_STACK && pred)
1001 HOST_WIDE_INT prev_offset = frame_offset;
1002 tree repr_decl = NULL_TREE;
1004 offset
1005 = alloc_stack_frame_space (stack_vars[i].size
1006 + ASAN_RED_ZONE_SIZE,
1007 MAX (alignb, ASAN_RED_ZONE_SIZE));
1008 data->asan_vec.safe_push (prev_offset);
1009 data->asan_vec.safe_push (offset + stack_vars[i].size);
1010 /* Find best representative of the partition.
1011 Prefer those with DECL_NAME, even better
1012 satisfying asan_protect_stack_decl predicate. */
1013 for (j = i; j != EOC; j = stack_vars[j].next)
1014 if (asan_protect_stack_decl (stack_vars[j].decl)
1015 && DECL_NAME (stack_vars[j].decl))
1017 repr_decl = stack_vars[j].decl;
1018 break;
1020 else if (repr_decl == NULL_TREE
1021 && DECL_P (stack_vars[j].decl)
1022 && DECL_NAME (stack_vars[j].decl))
1023 repr_decl = stack_vars[j].decl;
1024 if (repr_decl == NULL_TREE)
1025 repr_decl = stack_vars[i].decl;
1026 data->asan_decl_vec.safe_push (repr_decl);
1027 data->asan_alignb = MAX (data->asan_alignb, alignb);
1028 if (data->asan_base == NULL)
1029 data->asan_base = gen_reg_rtx (Pmode);
1030 base = data->asan_base;
1032 if (!STRICT_ALIGNMENT)
1033 base_align = crtl->max_used_stack_slot_alignment;
1034 else
1035 base_align = MAX (crtl->max_used_stack_slot_alignment,
1036 GET_MODE_ALIGNMENT (SImode)
1037 << ASAN_SHADOW_SHIFT);
1039 else
1041 offset = alloc_stack_frame_space (stack_vars[i].size, alignb);
1042 base_align = crtl->max_used_stack_slot_alignment;
1045 else
1047 /* Large alignment is only processed in the last pass. */
1048 if (pred)
1049 continue;
1050 gcc_assert (large_base != NULL);
1052 large_alloc += alignb - 1;
1053 large_alloc &= -(HOST_WIDE_INT)alignb;
1054 offset = large_alloc;
1055 large_alloc += stack_vars[i].size;
1057 base = large_base;
1058 base_align = large_align;
1061 /* Create rtl for each variable based on their location within the
1062 partition. */
1063 for (j = i; j != EOC; j = stack_vars[j].next)
1065 expand_one_stack_var_at (stack_vars[j].decl,
1066 base, base_align,
1067 offset);
1071 gcc_assert (large_alloc == large_size);
1074 /* Take into account all sizes of partitions and reset DECL_RTLs. */
1075 static HOST_WIDE_INT
1076 account_stack_vars (void)
1078 size_t si, j, i, n = stack_vars_num;
1079 HOST_WIDE_INT size = 0;
1081 for (si = 0; si < n; ++si)
1083 i = stack_vars_sorted[si];
1085 /* Skip variables that aren't partition representatives, for now. */
1086 if (stack_vars[i].representative != i)
1087 continue;
1089 size += stack_vars[i].size;
1090 for (j = i; j != EOC; j = stack_vars[j].next)
1091 set_rtl (stack_vars[j].decl, NULL);
1093 return size;
1096 /* A subroutine of expand_one_var. Called to immediately assign rtl
1097 to a variable to be allocated in the stack frame. */
1099 static void
1100 expand_one_stack_var (tree var)
1102 HOST_WIDE_INT size, offset;
1103 unsigned byte_align;
1105 size = tree_to_uhwi (DECL_SIZE_UNIT (SSAVAR (var)));
1106 byte_align = align_local_variable (SSAVAR (var));
1108 /* We handle highly aligned variables in expand_stack_vars. */
1109 gcc_assert (byte_align * BITS_PER_UNIT <= MAX_SUPPORTED_STACK_ALIGNMENT);
1111 offset = alloc_stack_frame_space (size, byte_align);
1113 expand_one_stack_var_at (var, virtual_stack_vars_rtx,
1114 crtl->max_used_stack_slot_alignment, offset);
1117 /* A subroutine of expand_one_var. Called to assign rtl to a VAR_DECL
1118 that will reside in a hard register. */
1120 static void
1121 expand_one_hard_reg_var (tree var)
1123 rest_of_decl_compilation (var, 0, 0);
1126 /* A subroutine of expand_one_var. Called to assign rtl to a VAR_DECL
1127 that will reside in a pseudo register. */
1129 static void
1130 expand_one_register_var (tree var)
1132 tree decl = SSAVAR (var);
1133 tree type = TREE_TYPE (decl);
1134 enum machine_mode reg_mode = promote_decl_mode (decl, NULL);
1135 rtx x = gen_reg_rtx (reg_mode);
1137 set_rtl (var, x);
1139 /* Note if the object is a user variable. */
1140 if (!DECL_ARTIFICIAL (decl))
1141 mark_user_reg (x);
1143 if (POINTER_TYPE_P (type))
1144 mark_reg_pointer (x, get_pointer_alignment (var));
1147 /* A subroutine of expand_one_var. Called to assign rtl to a VAR_DECL that
1148 has some associated error, e.g. its type is error-mark. We just need
1149 to pick something that won't crash the rest of the compiler. */
1151 static void
1152 expand_one_error_var (tree var)
1154 enum machine_mode mode = DECL_MODE (var);
1155 rtx x;
1157 if (mode == BLKmode)
1158 x = gen_rtx_MEM (BLKmode, const0_rtx);
1159 else if (mode == VOIDmode)
1160 x = const0_rtx;
1161 else
1162 x = gen_reg_rtx (mode);
1164 SET_DECL_RTL (var, x);
1167 /* A subroutine of expand_one_var. VAR is a variable that will be
1168 allocated to the local stack frame. Return true if we wish to
1169 add VAR to STACK_VARS so that it will be coalesced with other
1170 variables. Return false to allocate VAR immediately.
1172 This function is used to reduce the number of variables considered
1173 for coalescing, which reduces the size of the quadratic problem. */
1175 static bool
1176 defer_stack_allocation (tree var, bool toplevel)
1178 /* Whether the variable is small enough for immediate allocation not to be
1179 a problem with regard to the frame size. */
1180 bool smallish
1181 = ((HOST_WIDE_INT) tree_to_uhwi (DECL_SIZE_UNIT (var))
1182 < PARAM_VALUE (PARAM_MIN_SIZE_FOR_STACK_SHARING));
1184 /* If stack protection is enabled, *all* stack variables must be deferred,
1185 so that we can re-order the strings to the top of the frame.
1186 Similarly for Address Sanitizer. */
1187 if (flag_stack_protect || ((flag_sanitize & SANITIZE_ADDRESS) && ASAN_STACK))
1188 return true;
1190 /* We handle "large" alignment via dynamic allocation. We want to handle
1191 this extra complication in only one place, so defer them. */
1192 if (DECL_ALIGN (var) > MAX_SUPPORTED_STACK_ALIGNMENT)
1193 return true;
1195 /* When optimization is enabled, DECL_IGNORED_P variables originally scoped
1196 might be detached from their block and appear at toplevel when we reach
1197 here. We want to coalesce them with variables from other blocks when
1198 the immediate contribution to the frame size would be noticeable. */
1199 if (toplevel && optimize > 0 && DECL_IGNORED_P (var) && !smallish)
1200 return true;
1202 /* Variables declared in the outermost scope automatically conflict
1203 with every other variable. The only reason to want to defer them
1204 at all is that, after sorting, we can more efficiently pack
1205 small variables in the stack frame. Continue to defer at -O2. */
1206 if (toplevel && optimize < 2)
1207 return false;
1209 /* Without optimization, *most* variables are allocated from the
1210 stack, which makes the quadratic problem large exactly when we
1211 want compilation to proceed as quickly as possible. On the
1212 other hand, we don't want the function's stack frame size to
1213 get completely out of hand. So we avoid adding scalars and
1214 "small" aggregates to the list at all. */
1215 if (optimize == 0 && smallish)
1216 return false;
1218 return true;
1221 /* A subroutine of expand_used_vars. Expand one variable according to
1222 its flavor. Variables to be placed on the stack are not actually
1223 expanded yet, merely recorded.
1224 When REALLY_EXPAND is false, only add stack values to be allocated.
1225 Return stack usage this variable is supposed to take.
1228 static HOST_WIDE_INT
1229 expand_one_var (tree var, bool toplevel, bool really_expand)
1231 unsigned int align = BITS_PER_UNIT;
1232 tree origvar = var;
1234 var = SSAVAR (var);
1236 if (TREE_TYPE (var) != error_mark_node && TREE_CODE (var) == VAR_DECL)
1238 /* Because we don't know if VAR will be in register or on stack,
1239 we conservatively assume it will be on stack even if VAR is
1240 eventually put into register after RA pass. For non-automatic
1241 variables, which won't be on stack, we collect alignment of
1242 type and ignore user specified alignment. Similarly for
1243 SSA_NAMEs for which use_register_for_decl returns true. */
1244 if (TREE_STATIC (var)
1245 || DECL_EXTERNAL (var)
1246 || (TREE_CODE (origvar) == SSA_NAME && use_register_for_decl (var)))
1247 align = MINIMUM_ALIGNMENT (TREE_TYPE (var),
1248 TYPE_MODE (TREE_TYPE (var)),
1249 TYPE_ALIGN (TREE_TYPE (var)));
1250 else if (DECL_HAS_VALUE_EXPR_P (var)
1251 || (DECL_RTL_SET_P (var) && MEM_P (DECL_RTL (var))))
1252 /* Don't consider debug only variables with DECL_HAS_VALUE_EXPR_P set
1253 or variables which were assigned a stack slot already by
1254 expand_one_stack_var_at - in the latter case DECL_ALIGN has been
1255 changed from the offset chosen to it. */
1256 align = crtl->stack_alignment_estimated;
1257 else
1258 align = MINIMUM_ALIGNMENT (var, DECL_MODE (var), DECL_ALIGN (var));
1260 /* If the variable alignment is very large we'll dynamicaly allocate
1261 it, which means that in-frame portion is just a pointer. */
1262 if (align > MAX_SUPPORTED_STACK_ALIGNMENT)
1263 align = POINTER_SIZE;
1266 if (SUPPORTS_STACK_ALIGNMENT
1267 && crtl->stack_alignment_estimated < align)
1269 /* stack_alignment_estimated shouldn't change after stack
1270 realign decision made */
1271 gcc_assert (!crtl->stack_realign_processed);
1272 crtl->stack_alignment_estimated = align;
1275 /* stack_alignment_needed > PREFERRED_STACK_BOUNDARY is permitted.
1276 So here we only make sure stack_alignment_needed >= align. */
1277 if (crtl->stack_alignment_needed < align)
1278 crtl->stack_alignment_needed = align;
1279 if (crtl->max_used_stack_slot_alignment < align)
1280 crtl->max_used_stack_slot_alignment = align;
1282 if (TREE_CODE (origvar) == SSA_NAME)
1284 gcc_assert (TREE_CODE (var) != VAR_DECL
1285 || (!DECL_EXTERNAL (var)
1286 && !DECL_HAS_VALUE_EXPR_P (var)
1287 && !TREE_STATIC (var)
1288 && TREE_TYPE (var) != error_mark_node
1289 && !DECL_HARD_REGISTER (var)
1290 && really_expand));
1292 if (TREE_CODE (var) != VAR_DECL && TREE_CODE (origvar) != SSA_NAME)
1294 else if (DECL_EXTERNAL (var))
1296 else if (DECL_HAS_VALUE_EXPR_P (var))
1298 else if (TREE_STATIC (var))
1300 else if (TREE_CODE (origvar) != SSA_NAME && DECL_RTL_SET_P (var))
1302 else if (TREE_TYPE (var) == error_mark_node)
1304 if (really_expand)
1305 expand_one_error_var (var);
1307 else if (TREE_CODE (var) == VAR_DECL && DECL_HARD_REGISTER (var))
1309 if (really_expand)
1311 expand_one_hard_reg_var (var);
1312 if (!DECL_HARD_REGISTER (var))
1313 /* Invalid register specification. */
1314 expand_one_error_var (var);
1317 else if (use_register_for_decl (var))
1319 if (really_expand)
1320 expand_one_register_var (origvar);
1322 else if (! valid_constant_size_p (DECL_SIZE_UNIT (var)))
1324 /* Reject variables which cover more than half of the address-space. */
1325 if (really_expand)
1327 error ("size of variable %q+D is too large", var);
1328 expand_one_error_var (var);
1331 else if (defer_stack_allocation (var, toplevel))
1332 add_stack_var (origvar);
1333 else
1335 if (really_expand)
1336 expand_one_stack_var (origvar);
1337 return tree_to_uhwi (DECL_SIZE_UNIT (var));
1339 return 0;
1342 /* A subroutine of expand_used_vars. Walk down through the BLOCK tree
1343 expanding variables. Those variables that can be put into registers
1344 are allocated pseudos; those that can't are put on the stack.
1346 TOPLEVEL is true if this is the outermost BLOCK. */
1348 static void
1349 expand_used_vars_for_block (tree block, bool toplevel)
1351 tree t;
1353 /* Expand all variables at this level. */
1354 for (t = BLOCK_VARS (block); t ; t = DECL_CHAIN (t))
1355 if (TREE_USED (t)
1356 && ((TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != RESULT_DECL)
1357 || !DECL_NONSHAREABLE (t)))
1358 expand_one_var (t, toplevel, true);
1360 /* Expand all variables at containing levels. */
1361 for (t = BLOCK_SUBBLOCKS (block); t ; t = BLOCK_CHAIN (t))
1362 expand_used_vars_for_block (t, false);
1365 /* A subroutine of expand_used_vars. Walk down through the BLOCK tree
1366 and clear TREE_USED on all local variables. */
1368 static void
1369 clear_tree_used (tree block)
1371 tree t;
1373 for (t = BLOCK_VARS (block); t ; t = DECL_CHAIN (t))
1374 /* if (!TREE_STATIC (t) && !DECL_EXTERNAL (t)) */
1375 if ((TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != RESULT_DECL)
1376 || !DECL_NONSHAREABLE (t))
1377 TREE_USED (t) = 0;
1379 for (t = BLOCK_SUBBLOCKS (block); t ; t = BLOCK_CHAIN (t))
1380 clear_tree_used (t);
1383 enum {
1384 SPCT_FLAG_DEFAULT = 1,
1385 SPCT_FLAG_ALL = 2,
1386 SPCT_FLAG_STRONG = 3
1389 /* Examine TYPE and determine a bit mask of the following features. */
1391 #define SPCT_HAS_LARGE_CHAR_ARRAY 1
1392 #define SPCT_HAS_SMALL_CHAR_ARRAY 2
1393 #define SPCT_HAS_ARRAY 4
1394 #define SPCT_HAS_AGGREGATE 8
1396 static unsigned int
1397 stack_protect_classify_type (tree type)
1399 unsigned int ret = 0;
1400 tree t;
1402 switch (TREE_CODE (type))
1404 case ARRAY_TYPE:
1405 t = TYPE_MAIN_VARIANT (TREE_TYPE (type));
1406 if (t == char_type_node
1407 || t == signed_char_type_node
1408 || t == unsigned_char_type_node)
1410 unsigned HOST_WIDE_INT max = PARAM_VALUE (PARAM_SSP_BUFFER_SIZE);
1411 unsigned HOST_WIDE_INT len;
1413 if (!TYPE_SIZE_UNIT (type)
1414 || !tree_fits_uhwi_p (TYPE_SIZE_UNIT (type)))
1415 len = max;
1416 else
1417 len = tree_to_uhwi (TYPE_SIZE_UNIT (type));
1419 if (len < max)
1420 ret = SPCT_HAS_SMALL_CHAR_ARRAY | SPCT_HAS_ARRAY;
1421 else
1422 ret = SPCT_HAS_LARGE_CHAR_ARRAY | SPCT_HAS_ARRAY;
1424 else
1425 ret = SPCT_HAS_ARRAY;
1426 break;
1428 case UNION_TYPE:
1429 case QUAL_UNION_TYPE:
1430 case RECORD_TYPE:
1431 ret = SPCT_HAS_AGGREGATE;
1432 for (t = TYPE_FIELDS (type); t ; t = TREE_CHAIN (t))
1433 if (TREE_CODE (t) == FIELD_DECL)
1434 ret |= stack_protect_classify_type (TREE_TYPE (t));
1435 break;
1437 default:
1438 break;
1441 return ret;
1444 /* Return nonzero if DECL should be segregated into the "vulnerable" upper
1445 part of the local stack frame. Remember if we ever return nonzero for
1446 any variable in this function. The return value is the phase number in
1447 which the variable should be allocated. */
1449 static int
1450 stack_protect_decl_phase (tree decl)
1452 unsigned int bits = stack_protect_classify_type (TREE_TYPE (decl));
1453 int ret = 0;
1455 if (bits & SPCT_HAS_SMALL_CHAR_ARRAY)
1456 has_short_buffer = true;
1458 if (flag_stack_protect == SPCT_FLAG_ALL
1459 || flag_stack_protect == SPCT_FLAG_STRONG)
1461 if ((bits & (SPCT_HAS_SMALL_CHAR_ARRAY | SPCT_HAS_LARGE_CHAR_ARRAY))
1462 && !(bits & SPCT_HAS_AGGREGATE))
1463 ret = 1;
1464 else if (bits & SPCT_HAS_ARRAY)
1465 ret = 2;
1467 else
1468 ret = (bits & SPCT_HAS_LARGE_CHAR_ARRAY) != 0;
1470 if (ret)
1471 has_protected_decls = true;
1473 return ret;
1476 /* Two helper routines that check for phase 1 and phase 2. These are used
1477 as callbacks for expand_stack_vars. */
1479 static bool
1480 stack_protect_decl_phase_1 (size_t i)
1482 return stack_protect_decl_phase (stack_vars[i].decl) == 1;
1485 static bool
1486 stack_protect_decl_phase_2 (size_t i)
1488 return stack_protect_decl_phase (stack_vars[i].decl) == 2;
1491 /* And helper function that checks for asan phase (with stack protector
1492 it is phase 3). This is used as callback for expand_stack_vars.
1493 Returns true if any of the vars in the partition need to be protected. */
1495 static bool
1496 asan_decl_phase_3 (size_t i)
1498 while (i != EOC)
1500 if (asan_protect_stack_decl (stack_vars[i].decl))
1501 return true;
1502 i = stack_vars[i].next;
1504 return false;
1507 /* Ensure that variables in different stack protection phases conflict
1508 so that they are not merged and share the same stack slot. */
1510 static void
1511 add_stack_protection_conflicts (void)
1513 size_t i, j, n = stack_vars_num;
1514 unsigned char *phase;
1516 phase = XNEWVEC (unsigned char, n);
1517 for (i = 0; i < n; ++i)
1518 phase[i] = stack_protect_decl_phase (stack_vars[i].decl);
1520 for (i = 0; i < n; ++i)
1522 unsigned char ph_i = phase[i];
1523 for (j = i + 1; j < n; ++j)
1524 if (ph_i != phase[j])
1525 add_stack_var_conflict (i, j);
1528 XDELETEVEC (phase);
1531 /* Create a decl for the guard at the top of the stack frame. */
1533 static void
1534 create_stack_guard (void)
1536 tree guard = build_decl (DECL_SOURCE_LOCATION (current_function_decl),
1537 VAR_DECL, NULL, ptr_type_node);
1538 TREE_THIS_VOLATILE (guard) = 1;
1539 TREE_USED (guard) = 1;
1540 expand_one_stack_var (guard);
1541 crtl->stack_protect_guard = guard;
1544 /* Prepare for expanding variables. */
1545 static void
1546 init_vars_expansion (void)
1548 /* Conflict bitmaps, and a few related temporary bitmaps, go here. */
1549 bitmap_obstack_initialize (&stack_var_bitmap_obstack);
1551 /* A map from decl to stack partition. */
1552 decl_to_stack_part = new hash_map<tree, size_t>;
1554 /* Initialize local stack smashing state. */
1555 has_protected_decls = false;
1556 has_short_buffer = false;
1559 /* Free up stack variable graph data. */
1560 static void
1561 fini_vars_expansion (void)
1563 bitmap_obstack_release (&stack_var_bitmap_obstack);
1564 if (stack_vars)
1565 XDELETEVEC (stack_vars);
1566 if (stack_vars_sorted)
1567 XDELETEVEC (stack_vars_sorted);
1568 stack_vars = NULL;
1569 stack_vars_sorted = NULL;
1570 stack_vars_alloc = stack_vars_num = 0;
1571 delete decl_to_stack_part;
1572 decl_to_stack_part = NULL;
1575 /* Make a fair guess for the size of the stack frame of the function
1576 in NODE. This doesn't have to be exact, the result is only used in
1577 the inline heuristics. So we don't want to run the full stack var
1578 packing algorithm (which is quadratic in the number of stack vars).
1579 Instead, we calculate the total size of all stack vars. This turns
1580 out to be a pretty fair estimate -- packing of stack vars doesn't
1581 happen very often. */
1583 HOST_WIDE_INT
1584 estimated_stack_frame_size (struct cgraph_node *node)
1586 HOST_WIDE_INT size = 0;
1587 size_t i;
1588 tree var;
1589 struct function *fn = DECL_STRUCT_FUNCTION (node->decl);
1591 push_cfun (fn);
1593 init_vars_expansion ();
1595 FOR_EACH_LOCAL_DECL (fn, i, var)
1596 if (auto_var_in_fn_p (var, fn->decl))
1597 size += expand_one_var (var, true, false);
1599 if (stack_vars_num > 0)
1601 /* Fake sorting the stack vars for account_stack_vars (). */
1602 stack_vars_sorted = XNEWVEC (size_t, stack_vars_num);
1603 for (i = 0; i < stack_vars_num; ++i)
1604 stack_vars_sorted[i] = i;
1605 size += account_stack_vars ();
1608 fini_vars_expansion ();
1609 pop_cfun ();
1610 return size;
1613 /* Helper routine to check if a record or union contains an array field. */
1615 static int
1616 record_or_union_type_has_array_p (const_tree tree_type)
1618 tree fields = TYPE_FIELDS (tree_type);
1619 tree f;
1621 for (f = fields; f; f = DECL_CHAIN (f))
1622 if (TREE_CODE (f) == FIELD_DECL)
1624 tree field_type = TREE_TYPE (f);
1625 if (RECORD_OR_UNION_TYPE_P (field_type)
1626 && record_or_union_type_has_array_p (field_type))
1627 return 1;
1628 if (TREE_CODE (field_type) == ARRAY_TYPE)
1629 return 1;
1631 return 0;
1634 /* Check if the current function has local referenced variables that
1635 have their addresses taken, contain an array, or are arrays. */
1637 static bool
1638 stack_protect_decl_p ()
1640 unsigned i;
1641 tree var;
1643 FOR_EACH_LOCAL_DECL (cfun, i, var)
1644 if (!is_global_var (var))
1646 tree var_type = TREE_TYPE (var);
1647 if (TREE_CODE (var) == VAR_DECL
1648 && (TREE_CODE (var_type) == ARRAY_TYPE
1649 || TREE_ADDRESSABLE (var)
1650 || (RECORD_OR_UNION_TYPE_P (var_type)
1651 && record_or_union_type_has_array_p (var_type))))
1652 return true;
1654 return false;
1657 /* Check if the current function has calls that use a return slot. */
1659 static bool
1660 stack_protect_return_slot_p ()
1662 basic_block bb;
1664 FOR_ALL_BB_FN (bb, cfun)
1665 for (gimple_stmt_iterator gsi = gsi_start_bb (bb);
1666 !gsi_end_p (gsi); gsi_next (&gsi))
1668 gimple stmt = gsi_stmt (gsi);
1669 /* This assumes that calls to internal-only functions never
1670 use a return slot. */
1671 if (is_gimple_call (stmt)
1672 && !gimple_call_internal_p (stmt)
1673 && aggregate_value_p (TREE_TYPE (gimple_call_fntype (stmt)),
1674 gimple_call_fndecl (stmt)))
1675 return true;
1677 return false;
1680 /* Expand all variables used in the function. */
1682 static rtx_insn *
1683 expand_used_vars (void)
1685 tree var, outer_block = DECL_INITIAL (current_function_decl);
1686 vec<tree> maybe_local_decls = vNULL;
1687 rtx_insn *var_end_seq = NULL;
1688 unsigned i;
1689 unsigned len;
1690 bool gen_stack_protect_signal = false;
1692 /* Compute the phase of the stack frame for this function. */
1694 int align = PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT;
1695 int off = STARTING_FRAME_OFFSET % align;
1696 frame_phase = off ? align - off : 0;
1699 /* Set TREE_USED on all variables in the local_decls. */
1700 FOR_EACH_LOCAL_DECL (cfun, i, var)
1701 TREE_USED (var) = 1;
1702 /* Clear TREE_USED on all variables associated with a block scope. */
1703 clear_tree_used (DECL_INITIAL (current_function_decl));
1705 init_vars_expansion ();
1707 hash_map<tree, tree> ssa_name_decls;
1708 for (i = 0; i < SA.map->num_partitions; i++)
1710 tree var = partition_to_var (SA.map, i);
1712 gcc_assert (!virtual_operand_p (var));
1714 /* Assign decls to each SSA name partition, share decls for partitions
1715 we could have coalesced (those with the same type). */
1716 if (SSA_NAME_VAR (var) == NULL_TREE)
1718 tree *slot = &ssa_name_decls.get_or_insert (TREE_TYPE (var));
1719 if (!*slot)
1720 *slot = create_tmp_reg (TREE_TYPE (var), NULL);
1721 replace_ssa_name_symbol (var, *slot);
1724 /* Always allocate space for partitions based on VAR_DECLs. But for
1725 those based on PARM_DECLs or RESULT_DECLs and which matter for the
1726 debug info, there is no need to do so if optimization is disabled
1727 because all the SSA_NAMEs based on these DECLs have been coalesced
1728 into a single partition, which is thus assigned the canonical RTL
1729 location of the DECLs. If in_lto_p, we can't rely on optimize,
1730 a function could be compiled with -O1 -flto first and only the
1731 link performed at -O0. */
1732 if (TREE_CODE (SSA_NAME_VAR (var)) == VAR_DECL)
1733 expand_one_var (var, true, true);
1734 else if (DECL_IGNORED_P (SSA_NAME_VAR (var)) || optimize || in_lto_p)
1736 /* This is a PARM_DECL or RESULT_DECL. For those partitions that
1737 contain the default def (representing the parm or result itself)
1738 we don't do anything here. But those which don't contain the
1739 default def (representing a temporary based on the parm/result)
1740 we need to allocate space just like for normal VAR_DECLs. */
1741 if (!bitmap_bit_p (SA.partition_has_default_def, i))
1743 expand_one_var (var, true, true);
1744 gcc_assert (SA.partition_to_pseudo[i]);
1749 if (flag_stack_protect == SPCT_FLAG_STRONG)
1750 gen_stack_protect_signal
1751 = stack_protect_decl_p () || stack_protect_return_slot_p ();
1753 /* At this point all variables on the local_decls with TREE_USED
1754 set are not associated with any block scope. Lay them out. */
1756 len = vec_safe_length (cfun->local_decls);
1757 FOR_EACH_LOCAL_DECL (cfun, i, var)
1759 bool expand_now = false;
1761 /* Expanded above already. */
1762 if (is_gimple_reg (var))
1764 TREE_USED (var) = 0;
1765 goto next;
1767 /* We didn't set a block for static or extern because it's hard
1768 to tell the difference between a global variable (re)declared
1769 in a local scope, and one that's really declared there to
1770 begin with. And it doesn't really matter much, since we're
1771 not giving them stack space. Expand them now. */
1772 else if (TREE_STATIC (var) || DECL_EXTERNAL (var))
1773 expand_now = true;
1775 /* Expand variables not associated with any block now. Those created by
1776 the optimizers could be live anywhere in the function. Those that
1777 could possibly have been scoped originally and detached from their
1778 block will have their allocation deferred so we coalesce them with
1779 others when optimization is enabled. */
1780 else if (TREE_USED (var))
1781 expand_now = true;
1783 /* Finally, mark all variables on the list as used. We'll use
1784 this in a moment when we expand those associated with scopes. */
1785 TREE_USED (var) = 1;
1787 if (expand_now)
1788 expand_one_var (var, true, true);
1790 next:
1791 if (DECL_ARTIFICIAL (var) && !DECL_IGNORED_P (var))
1793 rtx rtl = DECL_RTL_IF_SET (var);
1795 /* Keep artificial non-ignored vars in cfun->local_decls
1796 chain until instantiate_decls. */
1797 if (rtl && (MEM_P (rtl) || GET_CODE (rtl) == CONCAT))
1798 add_local_decl (cfun, var);
1799 else if (rtl == NULL_RTX)
1800 /* If rtl isn't set yet, which can happen e.g. with
1801 -fstack-protector, retry before returning from this
1802 function. */
1803 maybe_local_decls.safe_push (var);
1807 /* We duplicated some of the decls in CFUN->LOCAL_DECLS.
1809 +-----------------+-----------------+
1810 | ...processed... | ...duplicates...|
1811 +-----------------+-----------------+
1813 +-- LEN points here.
1815 We just want the duplicates, as those are the artificial
1816 non-ignored vars that we want to keep until instantiate_decls.
1817 Move them down and truncate the array. */
1818 if (!vec_safe_is_empty (cfun->local_decls))
1819 cfun->local_decls->block_remove (0, len);
1821 /* At this point, all variables within the block tree with TREE_USED
1822 set are actually used by the optimized function. Lay them out. */
1823 expand_used_vars_for_block (outer_block, true);
1825 if (stack_vars_num > 0)
1827 add_scope_conflicts ();
1829 /* If stack protection is enabled, we don't share space between
1830 vulnerable data and non-vulnerable data. */
1831 if (flag_stack_protect)
1832 add_stack_protection_conflicts ();
1834 /* Now that we have collected all stack variables, and have computed a
1835 minimal interference graph, attempt to save some stack space. */
1836 partition_stack_vars ();
1837 if (dump_file)
1838 dump_stack_var_partition ();
1841 switch (flag_stack_protect)
1843 case SPCT_FLAG_ALL:
1844 create_stack_guard ();
1845 break;
1847 case SPCT_FLAG_STRONG:
1848 if (gen_stack_protect_signal
1849 || cfun->calls_alloca || has_protected_decls)
1850 create_stack_guard ();
1851 break;
1853 case SPCT_FLAG_DEFAULT:
1854 if (cfun->calls_alloca || has_protected_decls)
1855 create_stack_guard ();
1856 break;
1858 default:
1862 /* Assign rtl to each variable based on these partitions. */
1863 if (stack_vars_num > 0)
1865 struct stack_vars_data data;
1867 data.asan_vec = vNULL;
1868 data.asan_decl_vec = vNULL;
1869 data.asan_base = NULL_RTX;
1870 data.asan_alignb = 0;
1872 /* Reorder decls to be protected by iterating over the variables
1873 array multiple times, and allocating out of each phase in turn. */
1874 /* ??? We could probably integrate this into the qsort we did
1875 earlier, such that we naturally see these variables first,
1876 and thus naturally allocate things in the right order. */
1877 if (has_protected_decls)
1879 /* Phase 1 contains only character arrays. */
1880 expand_stack_vars (stack_protect_decl_phase_1, &data);
1882 /* Phase 2 contains other kinds of arrays. */
1883 if (flag_stack_protect == 2)
1884 expand_stack_vars (stack_protect_decl_phase_2, &data);
1887 if ((flag_sanitize & SANITIZE_ADDRESS) && ASAN_STACK)
1888 /* Phase 3, any partitions that need asan protection
1889 in addition to phase 1 and 2. */
1890 expand_stack_vars (asan_decl_phase_3, &data);
1892 if (!data.asan_vec.is_empty ())
1894 HOST_WIDE_INT prev_offset = frame_offset;
1895 HOST_WIDE_INT offset, sz, redzonesz;
1896 redzonesz = ASAN_RED_ZONE_SIZE;
1897 sz = data.asan_vec[0] - prev_offset;
1898 if (data.asan_alignb > ASAN_RED_ZONE_SIZE
1899 && data.asan_alignb <= 4096
1900 && sz + ASAN_RED_ZONE_SIZE >= (int) data.asan_alignb)
1901 redzonesz = ((sz + ASAN_RED_ZONE_SIZE + data.asan_alignb - 1)
1902 & ~(data.asan_alignb - HOST_WIDE_INT_1)) - sz;
1903 offset
1904 = alloc_stack_frame_space (redzonesz, ASAN_RED_ZONE_SIZE);
1905 data.asan_vec.safe_push (prev_offset);
1906 data.asan_vec.safe_push (offset);
1907 /* Leave space for alignment if STRICT_ALIGNMENT. */
1908 if (STRICT_ALIGNMENT)
1909 alloc_stack_frame_space ((GET_MODE_ALIGNMENT (SImode)
1910 << ASAN_SHADOW_SHIFT)
1911 / BITS_PER_UNIT, 1);
1913 var_end_seq
1914 = asan_emit_stack_protection (virtual_stack_vars_rtx,
1915 data.asan_base,
1916 data.asan_alignb,
1917 data.asan_vec.address (),
1918 data.asan_decl_vec.address (),
1919 data.asan_vec.length ());
1922 expand_stack_vars (NULL, &data);
1924 data.asan_vec.release ();
1925 data.asan_decl_vec.release ();
1928 fini_vars_expansion ();
1930 /* If there were any artificial non-ignored vars without rtl
1931 found earlier, see if deferred stack allocation hasn't assigned
1932 rtl to them. */
1933 FOR_EACH_VEC_ELT_REVERSE (maybe_local_decls, i, var)
1935 rtx rtl = DECL_RTL_IF_SET (var);
1937 /* Keep artificial non-ignored vars in cfun->local_decls
1938 chain until instantiate_decls. */
1939 if (rtl && (MEM_P (rtl) || GET_CODE (rtl) == CONCAT))
1940 add_local_decl (cfun, var);
1942 maybe_local_decls.release ();
1944 /* If the target requires that FRAME_OFFSET be aligned, do it. */
1945 if (STACK_ALIGNMENT_NEEDED)
1947 HOST_WIDE_INT align = PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT;
1948 if (!FRAME_GROWS_DOWNWARD)
1949 frame_offset += align - 1;
1950 frame_offset &= -align;
1953 return var_end_seq;
1957 /* If we need to produce a detailed dump, print the tree representation
1958 for STMT to the dump file. SINCE is the last RTX after which the RTL
1959 generated for STMT should have been appended. */
1961 static void
1962 maybe_dump_rtl_for_gimple_stmt (gimple stmt, rtx_insn *since)
1964 if (dump_file && (dump_flags & TDF_DETAILS))
1966 fprintf (dump_file, "\n;; ");
1967 print_gimple_stmt (dump_file, stmt, 0,
1968 TDF_SLIM | (dump_flags & TDF_LINENO));
1969 fprintf (dump_file, "\n");
1971 print_rtl (dump_file, since ? NEXT_INSN (since) : since);
1975 /* Maps the blocks that do not contain tree labels to rtx labels. */
1977 static hash_map<basic_block, rtx_code_label *> *lab_rtx_for_bb;
1979 /* Returns the label_rtx expression for a label starting basic block BB. */
1981 static rtx
1982 label_rtx_for_bb (basic_block bb ATTRIBUTE_UNUSED)
1984 gimple_stmt_iterator gsi;
1985 tree lab;
1987 if (bb->flags & BB_RTL)
1988 return block_label (bb);
1990 rtx_code_label **elt = lab_rtx_for_bb->get (bb);
1991 if (elt)
1992 return *elt;
1994 /* Find the tree label if it is present. */
1996 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1998 gimple_label lab_stmt;
2000 lab_stmt = dyn_cast <gimple_label> (gsi_stmt (gsi));
2001 if (!lab_stmt)
2002 break;
2004 lab = gimple_label_label (lab_stmt);
2005 if (DECL_NONLOCAL (lab))
2006 break;
2008 return label_rtx (lab);
2011 rtx_code_label *l = gen_label_rtx ();
2012 lab_rtx_for_bb->put (bb, l);
2013 return l;
2017 /* A subroutine of expand_gimple_cond. Given E, a fallthrough edge
2018 of a basic block where we just expanded the conditional at the end,
2019 possibly clean up the CFG and instruction sequence. LAST is the
2020 last instruction before the just emitted jump sequence. */
2022 static void
2023 maybe_cleanup_end_of_block (edge e, rtx_insn *last)
2025 /* Special case: when jumpif decides that the condition is
2026 trivial it emits an unconditional jump (and the necessary
2027 barrier). But we still have two edges, the fallthru one is
2028 wrong. purge_dead_edges would clean this up later. Unfortunately
2029 we have to insert insns (and split edges) before
2030 find_many_sub_basic_blocks and hence before purge_dead_edges.
2031 But splitting edges might create new blocks which depend on the
2032 fact that if there are two edges there's no barrier. So the
2033 barrier would get lost and verify_flow_info would ICE. Instead
2034 of auditing all edge splitters to care for the barrier (which
2035 normally isn't there in a cleaned CFG), fix it here. */
2036 if (BARRIER_P (get_last_insn ()))
2038 rtx_insn *insn;
2039 remove_edge (e);
2040 /* Now, we have a single successor block, if we have insns to
2041 insert on the remaining edge we potentially will insert
2042 it at the end of this block (if the dest block isn't feasible)
2043 in order to avoid splitting the edge. This insertion will take
2044 place in front of the last jump. But we might have emitted
2045 multiple jumps (conditional and one unconditional) to the
2046 same destination. Inserting in front of the last one then
2047 is a problem. See PR 40021. We fix this by deleting all
2048 jumps except the last unconditional one. */
2049 insn = PREV_INSN (get_last_insn ());
2050 /* Make sure we have an unconditional jump. Otherwise we're
2051 confused. */
2052 gcc_assert (JUMP_P (insn) && !any_condjump_p (insn));
2053 for (insn = PREV_INSN (insn); insn != last;)
2055 insn = PREV_INSN (insn);
2056 if (JUMP_P (NEXT_INSN (insn)))
2058 if (!any_condjump_p (NEXT_INSN (insn)))
2060 gcc_assert (BARRIER_P (NEXT_INSN (NEXT_INSN (insn))));
2061 delete_insn (NEXT_INSN (NEXT_INSN (insn)));
2063 delete_insn (NEXT_INSN (insn));
2069 /* A subroutine of expand_gimple_basic_block. Expand one GIMPLE_COND.
2070 Returns a new basic block if we've terminated the current basic
2071 block and created a new one. */
2073 static basic_block
2074 expand_gimple_cond (basic_block bb, gimple_cond stmt)
2076 basic_block new_bb, dest;
2077 edge new_edge;
2078 edge true_edge;
2079 edge false_edge;
2080 rtx_insn *last2, *last;
2081 enum tree_code code;
2082 tree op0, op1;
2084 code = gimple_cond_code (stmt);
2085 op0 = gimple_cond_lhs (stmt);
2086 op1 = gimple_cond_rhs (stmt);
2087 /* We're sometimes presented with such code:
2088 D.123_1 = x < y;
2089 if (D.123_1 != 0)
2091 This would expand to two comparisons which then later might
2092 be cleaned up by combine. But some pattern matchers like if-conversion
2093 work better when there's only one compare, so make up for this
2094 here as special exception if TER would have made the same change. */
2095 if (SA.values
2096 && TREE_CODE (op0) == SSA_NAME
2097 && TREE_CODE (TREE_TYPE (op0)) == BOOLEAN_TYPE
2098 && TREE_CODE (op1) == INTEGER_CST
2099 && ((gimple_cond_code (stmt) == NE_EXPR
2100 && integer_zerop (op1))
2101 || (gimple_cond_code (stmt) == EQ_EXPR
2102 && integer_onep (op1)))
2103 && bitmap_bit_p (SA.values, SSA_NAME_VERSION (op0)))
2105 gimple second = SSA_NAME_DEF_STMT (op0);
2106 if (gimple_code (second) == GIMPLE_ASSIGN)
2108 enum tree_code code2 = gimple_assign_rhs_code (second);
2109 if (TREE_CODE_CLASS (code2) == tcc_comparison)
2111 code = code2;
2112 op0 = gimple_assign_rhs1 (second);
2113 op1 = gimple_assign_rhs2 (second);
2115 /* If jumps are cheap turn some more codes into
2116 jumpy sequences. */
2117 else if (BRANCH_COST (optimize_insn_for_speed_p (), false) < 4)
2119 if ((code2 == BIT_AND_EXPR
2120 && TYPE_PRECISION (TREE_TYPE (op0)) == 1
2121 && TREE_CODE (gimple_assign_rhs2 (second)) != INTEGER_CST)
2122 || code2 == TRUTH_AND_EXPR)
2124 code = TRUTH_ANDIF_EXPR;
2125 op0 = gimple_assign_rhs1 (second);
2126 op1 = gimple_assign_rhs2 (second);
2128 else if (code2 == BIT_IOR_EXPR || code2 == TRUTH_OR_EXPR)
2130 code = TRUTH_ORIF_EXPR;
2131 op0 = gimple_assign_rhs1 (second);
2132 op1 = gimple_assign_rhs2 (second);
2138 last2 = last = get_last_insn ();
2140 extract_true_false_edges_from_block (bb, &true_edge, &false_edge);
2141 set_curr_insn_location (gimple_location (stmt));
2143 /* These flags have no purpose in RTL land. */
2144 true_edge->flags &= ~EDGE_TRUE_VALUE;
2145 false_edge->flags &= ~EDGE_FALSE_VALUE;
2147 /* We can either have a pure conditional jump with one fallthru edge or
2148 two-way jump that needs to be decomposed into two basic blocks. */
2149 if (false_edge->dest == bb->next_bb)
2151 jumpif_1 (code, op0, op1, label_rtx_for_bb (true_edge->dest),
2152 true_edge->probability);
2153 maybe_dump_rtl_for_gimple_stmt (stmt, last);
2154 if (true_edge->goto_locus != UNKNOWN_LOCATION)
2155 set_curr_insn_location (true_edge->goto_locus);
2156 false_edge->flags |= EDGE_FALLTHRU;
2157 maybe_cleanup_end_of_block (false_edge, last);
2158 return NULL;
2160 if (true_edge->dest == bb->next_bb)
2162 jumpifnot_1 (code, op0, op1, label_rtx_for_bb (false_edge->dest),
2163 false_edge->probability);
2164 maybe_dump_rtl_for_gimple_stmt (stmt, last);
2165 if (false_edge->goto_locus != UNKNOWN_LOCATION)
2166 set_curr_insn_location (false_edge->goto_locus);
2167 true_edge->flags |= EDGE_FALLTHRU;
2168 maybe_cleanup_end_of_block (true_edge, last);
2169 return NULL;
2172 jumpif_1 (code, op0, op1, label_rtx_for_bb (true_edge->dest),
2173 true_edge->probability);
2174 last = get_last_insn ();
2175 if (false_edge->goto_locus != UNKNOWN_LOCATION)
2176 set_curr_insn_location (false_edge->goto_locus);
2177 emit_jump (label_rtx_for_bb (false_edge->dest));
2179 BB_END (bb) = last;
2180 if (BARRIER_P (BB_END (bb)))
2181 BB_END (bb) = PREV_INSN (BB_END (bb));
2182 update_bb_for_insn (bb);
2184 new_bb = create_basic_block (NEXT_INSN (last), get_last_insn (), bb);
2185 dest = false_edge->dest;
2186 redirect_edge_succ (false_edge, new_bb);
2187 false_edge->flags |= EDGE_FALLTHRU;
2188 new_bb->count = false_edge->count;
2189 new_bb->frequency = EDGE_FREQUENCY (false_edge);
2190 add_bb_to_loop (new_bb, bb->loop_father);
2191 new_edge = make_edge (new_bb, dest, 0);
2192 new_edge->probability = REG_BR_PROB_BASE;
2193 new_edge->count = new_bb->count;
2194 if (BARRIER_P (BB_END (new_bb)))
2195 BB_END (new_bb) = PREV_INSN (BB_END (new_bb));
2196 update_bb_for_insn (new_bb);
2198 maybe_dump_rtl_for_gimple_stmt (stmt, last2);
2200 if (true_edge->goto_locus != UNKNOWN_LOCATION)
2202 set_curr_insn_location (true_edge->goto_locus);
2203 true_edge->goto_locus = curr_insn_location ();
2206 return new_bb;
2209 /* Mark all calls that can have a transaction restart. */
2211 static void
2212 mark_transaction_restart_calls (gimple stmt)
2214 struct tm_restart_node dummy;
2215 void **slot;
2217 if (!cfun->gimple_df->tm_restart)
2218 return;
2220 dummy.stmt = stmt;
2221 slot = htab_find_slot (cfun->gimple_df->tm_restart, &dummy, NO_INSERT);
2222 if (slot)
2224 struct tm_restart_node *n = (struct tm_restart_node *) *slot;
2225 tree list = n->label_or_list;
2226 rtx_insn *insn;
2228 for (insn = next_real_insn (get_last_insn ());
2229 !CALL_P (insn);
2230 insn = next_real_insn (insn))
2231 continue;
2233 if (TREE_CODE (list) == LABEL_DECL)
2234 add_reg_note (insn, REG_TM, label_rtx (list));
2235 else
2236 for (; list ; list = TREE_CHAIN (list))
2237 add_reg_note (insn, REG_TM, label_rtx (TREE_VALUE (list)));
2241 /* A subroutine of expand_gimple_stmt_1, expanding one GIMPLE_CALL
2242 statement STMT. */
2244 static void
2245 expand_call_stmt (gimple_call stmt)
2247 tree exp, decl, lhs;
2248 bool builtin_p;
2249 size_t i;
2251 if (gimple_call_internal_p (stmt))
2253 expand_internal_call (stmt);
2254 return;
2257 exp = build_vl_exp (CALL_EXPR, gimple_call_num_args (stmt) + 3);
2259 CALL_EXPR_FN (exp) = gimple_call_fn (stmt);
2260 decl = gimple_call_fndecl (stmt);
2261 builtin_p = decl && DECL_BUILT_IN (decl);
2263 /* If this is not a builtin function, the function type through which the
2264 call is made may be different from the type of the function. */
2265 if (!builtin_p)
2266 CALL_EXPR_FN (exp)
2267 = fold_convert (build_pointer_type (gimple_call_fntype (stmt)),
2268 CALL_EXPR_FN (exp));
2270 TREE_TYPE (exp) = gimple_call_return_type (stmt);
2271 CALL_EXPR_STATIC_CHAIN (exp) = gimple_call_chain (stmt);
2273 for (i = 0; i < gimple_call_num_args (stmt); i++)
2275 tree arg = gimple_call_arg (stmt, i);
2276 gimple def;
2277 /* TER addresses into arguments of builtin functions so we have a
2278 chance to infer more correct alignment information. See PR39954. */
2279 if (builtin_p
2280 && TREE_CODE (arg) == SSA_NAME
2281 && (def = get_gimple_for_ssa_name (arg))
2282 && gimple_assign_rhs_code (def) == ADDR_EXPR)
2283 arg = gimple_assign_rhs1 (def);
2284 CALL_EXPR_ARG (exp, i) = arg;
2287 if (gimple_has_side_effects (stmt))
2288 TREE_SIDE_EFFECTS (exp) = 1;
2290 if (gimple_call_nothrow_p (stmt))
2291 TREE_NOTHROW (exp) = 1;
2293 CALL_EXPR_TAILCALL (exp) = gimple_call_tail_p (stmt);
2294 CALL_EXPR_RETURN_SLOT_OPT (exp) = gimple_call_return_slot_opt_p (stmt);
2295 if (decl
2296 && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL
2297 && (DECL_FUNCTION_CODE (decl) == BUILT_IN_ALLOCA
2298 || DECL_FUNCTION_CODE (decl) == BUILT_IN_ALLOCA_WITH_ALIGN))
2299 CALL_ALLOCA_FOR_VAR_P (exp) = gimple_call_alloca_for_var_p (stmt);
2300 else
2301 CALL_FROM_THUNK_P (exp) = gimple_call_from_thunk_p (stmt);
2302 CALL_EXPR_VA_ARG_PACK (exp) = gimple_call_va_arg_pack_p (stmt);
2303 SET_EXPR_LOCATION (exp, gimple_location (stmt));
2305 /* Ensure RTL is created for debug args. */
2306 if (decl && DECL_HAS_DEBUG_ARGS_P (decl))
2308 vec<tree, va_gc> **debug_args = decl_debug_args_lookup (decl);
2309 unsigned int ix;
2310 tree dtemp;
2312 if (debug_args)
2313 for (ix = 1; (*debug_args)->iterate (ix, &dtemp); ix += 2)
2315 gcc_assert (TREE_CODE (dtemp) == DEBUG_EXPR_DECL);
2316 expand_debug_expr (dtemp);
2320 lhs = gimple_call_lhs (stmt);
2321 if (lhs)
2322 expand_assignment (lhs, exp, false);
2323 else
2324 expand_expr (exp, const0_rtx, VOIDmode, EXPAND_NORMAL);
2326 mark_transaction_restart_calls (stmt);
2330 /* Generate RTL for an asm statement (explicit assembler code).
2331 STRING is a STRING_CST node containing the assembler code text,
2332 or an ADDR_EXPR containing a STRING_CST. VOL nonzero means the
2333 insn is volatile; don't optimize it. */
2335 static void
2336 expand_asm_loc (tree string, int vol, location_t locus)
2338 rtx body;
2340 if (TREE_CODE (string) == ADDR_EXPR)
2341 string = TREE_OPERAND (string, 0);
2343 body = gen_rtx_ASM_INPUT_loc (VOIDmode,
2344 ggc_strdup (TREE_STRING_POINTER (string)),
2345 locus);
2347 MEM_VOLATILE_P (body) = vol;
2349 emit_insn (body);
2352 /* Return the number of times character C occurs in string S. */
2353 static int
2354 n_occurrences (int c, const char *s)
2356 int n = 0;
2357 while (*s)
2358 n += (*s++ == c);
2359 return n;
2362 /* A subroutine of expand_asm_operands. Check that all operands have
2363 the same number of alternatives. Return true if so. */
2365 static bool
2366 check_operand_nalternatives (tree outputs, tree inputs)
2368 if (outputs || inputs)
2370 tree tmp = TREE_PURPOSE (outputs ? outputs : inputs);
2371 int nalternatives
2372 = n_occurrences (',', TREE_STRING_POINTER (TREE_VALUE (tmp)));
2373 tree next = inputs;
2375 if (nalternatives + 1 > MAX_RECOG_ALTERNATIVES)
2377 error ("too many alternatives in %<asm%>");
2378 return false;
2381 tmp = outputs;
2382 while (tmp)
2384 const char *constraint
2385 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tmp)));
2387 if (n_occurrences (',', constraint) != nalternatives)
2389 error ("operand constraints for %<asm%> differ "
2390 "in number of alternatives");
2391 return false;
2394 if (TREE_CHAIN (tmp))
2395 tmp = TREE_CHAIN (tmp);
2396 else
2397 tmp = next, next = 0;
2401 return true;
2404 /* Check for overlap between registers marked in CLOBBERED_REGS and
2405 anything inappropriate in T. Emit error and return the register
2406 variable definition for error, NULL_TREE for ok. */
2408 static bool
2409 tree_conflicts_with_clobbers_p (tree t, HARD_REG_SET *clobbered_regs)
2411 /* Conflicts between asm-declared register variables and the clobber
2412 list are not allowed. */
2413 tree overlap = tree_overlaps_hard_reg_set (t, clobbered_regs);
2415 if (overlap)
2417 error ("asm-specifier for variable %qE conflicts with asm clobber list",
2418 DECL_NAME (overlap));
2420 /* Reset registerness to stop multiple errors emitted for a single
2421 variable. */
2422 DECL_REGISTER (overlap) = 0;
2423 return true;
2426 return false;
2429 /* Generate RTL for an asm statement with arguments.
2430 STRING is the instruction template.
2431 OUTPUTS is a list of output arguments (lvalues); INPUTS a list of inputs.
2432 Each output or input has an expression in the TREE_VALUE and
2433 a tree list in TREE_PURPOSE which in turn contains a constraint
2434 name in TREE_VALUE (or NULL_TREE) and a constraint string
2435 in TREE_PURPOSE.
2436 CLOBBERS is a list of STRING_CST nodes each naming a hard register
2437 that is clobbered by this insn.
2439 LABELS is a list of labels, and if LABELS is non-NULL, FALLTHRU_BB
2440 should be the fallthru basic block of the asm goto.
2442 Not all kinds of lvalue that may appear in OUTPUTS can be stored directly.
2443 Some elements of OUTPUTS may be replaced with trees representing temporary
2444 values. The caller should copy those temporary values to the originally
2445 specified lvalues.
2447 VOL nonzero means the insn is volatile; don't optimize it. */
2449 static void
2450 expand_asm_operands (tree string, tree outputs, tree inputs,
2451 tree clobbers, tree labels, basic_block fallthru_bb,
2452 int vol, location_t locus)
2454 rtvec argvec, constraintvec, labelvec;
2455 rtx body;
2456 int ninputs = list_length (inputs);
2457 int noutputs = list_length (outputs);
2458 int nlabels = list_length (labels);
2459 int ninout;
2460 int nclobbers;
2461 HARD_REG_SET clobbered_regs;
2462 int clobber_conflict_found = 0;
2463 tree tail;
2464 tree t;
2465 int i;
2466 /* Vector of RTX's of evaluated output operands. */
2467 rtx *output_rtx = XALLOCAVEC (rtx, noutputs);
2468 int *inout_opnum = XALLOCAVEC (int, noutputs);
2469 rtx *real_output_rtx = XALLOCAVEC (rtx, noutputs);
2470 enum machine_mode *inout_mode = XALLOCAVEC (enum machine_mode, noutputs);
2471 const char **constraints = XALLOCAVEC (const char *, noutputs + ninputs);
2472 int old_generating_concat_p = generating_concat_p;
2473 rtx_code_label *fallthru_label = NULL;
2475 /* An ASM with no outputs needs to be treated as volatile, for now. */
2476 if (noutputs == 0)
2477 vol = 1;
2479 if (! check_operand_nalternatives (outputs, inputs))
2480 return;
2482 string = resolve_asm_operand_names (string, outputs, inputs, labels);
2484 /* Collect constraints. */
2485 i = 0;
2486 for (t = outputs; t ; t = TREE_CHAIN (t), i++)
2487 constraints[i] = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
2488 for (t = inputs; t ; t = TREE_CHAIN (t), i++)
2489 constraints[i] = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
2491 /* Sometimes we wish to automatically clobber registers across an asm.
2492 Case in point is when the i386 backend moved from cc0 to a hard reg --
2493 maintaining source-level compatibility means automatically clobbering
2494 the flags register. */
2495 clobbers = targetm.md_asm_clobbers (outputs, inputs, clobbers);
2497 /* Count the number of meaningful clobbered registers, ignoring what
2498 we would ignore later. */
2499 nclobbers = 0;
2500 CLEAR_HARD_REG_SET (clobbered_regs);
2501 for (tail = clobbers; tail; tail = TREE_CHAIN (tail))
2503 const char *regname;
2504 int nregs;
2506 if (TREE_VALUE (tail) == error_mark_node)
2507 return;
2508 regname = TREE_STRING_POINTER (TREE_VALUE (tail));
2510 i = decode_reg_name_and_count (regname, &nregs);
2511 if (i == -4)
2512 ++nclobbers;
2513 else if (i == -2)
2514 error ("unknown register name %qs in %<asm%>", regname);
2516 /* Mark clobbered registers. */
2517 if (i >= 0)
2519 int reg;
2521 for (reg = i; reg < i + nregs; reg++)
2523 ++nclobbers;
2525 /* Clobbering the PIC register is an error. */
2526 if (reg == (int) PIC_OFFSET_TABLE_REGNUM)
2528 error ("PIC register clobbered by %qs in %<asm%>", regname);
2529 return;
2532 SET_HARD_REG_BIT (clobbered_regs, reg);
2537 /* First pass over inputs and outputs checks validity and sets
2538 mark_addressable if needed. */
2540 ninout = 0;
2541 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
2543 tree val = TREE_VALUE (tail);
2544 tree type = TREE_TYPE (val);
2545 const char *constraint;
2546 bool is_inout;
2547 bool allows_reg;
2548 bool allows_mem;
2550 /* If there's an erroneous arg, emit no insn. */
2551 if (type == error_mark_node)
2552 return;
2554 /* Try to parse the output constraint. If that fails, there's
2555 no point in going further. */
2556 constraint = constraints[i];
2557 if (!parse_output_constraint (&constraint, i, ninputs, noutputs,
2558 &allows_mem, &allows_reg, &is_inout))
2559 return;
2561 if (! allows_reg
2562 && (allows_mem
2563 || is_inout
2564 || (DECL_P (val)
2565 && REG_P (DECL_RTL (val))
2566 && GET_MODE (DECL_RTL (val)) != TYPE_MODE (type))))
2567 mark_addressable (val);
2569 if (is_inout)
2570 ninout++;
2573 ninputs += ninout;
2574 if (ninputs + noutputs > MAX_RECOG_OPERANDS)
2576 error ("more than %d operands in %<asm%>", MAX_RECOG_OPERANDS);
2577 return;
2580 for (i = 0, tail = inputs; tail; i++, tail = TREE_CHAIN (tail))
2582 bool allows_reg, allows_mem;
2583 const char *constraint;
2585 /* If there's an erroneous arg, emit no insn, because the ASM_INPUT
2586 would get VOIDmode and that could cause a crash in reload. */
2587 if (TREE_TYPE (TREE_VALUE (tail)) == error_mark_node)
2588 return;
2590 constraint = constraints[i + noutputs];
2591 if (! parse_input_constraint (&constraint, i, ninputs, noutputs, ninout,
2592 constraints, &allows_mem, &allows_reg))
2593 return;
2595 if (! allows_reg && allows_mem)
2596 mark_addressable (TREE_VALUE (tail));
2599 /* Second pass evaluates arguments. */
2601 /* Make sure stack is consistent for asm goto. */
2602 if (nlabels > 0)
2603 do_pending_stack_adjust ();
2605 ninout = 0;
2606 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
2608 tree val = TREE_VALUE (tail);
2609 tree type = TREE_TYPE (val);
2610 bool is_inout;
2611 bool allows_reg;
2612 bool allows_mem;
2613 rtx op;
2614 bool ok;
2616 ok = parse_output_constraint (&constraints[i], i, ninputs,
2617 noutputs, &allows_mem, &allows_reg,
2618 &is_inout);
2619 gcc_assert (ok);
2621 /* If an output operand is not a decl or indirect ref and our constraint
2622 allows a register, make a temporary to act as an intermediate.
2623 Make the asm insn write into that, then our caller will copy it to
2624 the real output operand. Likewise for promoted variables. */
2626 generating_concat_p = 0;
2628 real_output_rtx[i] = NULL_RTX;
2629 if ((TREE_CODE (val) == INDIRECT_REF
2630 && allows_mem)
2631 || (DECL_P (val)
2632 && (allows_mem || REG_P (DECL_RTL (val)))
2633 && ! (REG_P (DECL_RTL (val))
2634 && GET_MODE (DECL_RTL (val)) != TYPE_MODE (type)))
2635 || ! allows_reg
2636 || is_inout)
2638 op = expand_expr (val, NULL_RTX, VOIDmode,
2639 !allows_reg ? EXPAND_MEMORY : EXPAND_WRITE);
2640 if (MEM_P (op))
2641 op = validize_mem (op);
2643 if (! allows_reg && !MEM_P (op))
2644 error ("output number %d not directly addressable", i);
2645 if ((! allows_mem && MEM_P (op))
2646 || GET_CODE (op) == CONCAT)
2648 real_output_rtx[i] = op;
2649 op = gen_reg_rtx (GET_MODE (op));
2650 if (is_inout)
2651 emit_move_insn (op, real_output_rtx[i]);
2654 else
2656 op = assign_temp (type, 0, 1);
2657 op = validize_mem (op);
2658 if (!MEM_P (op) && TREE_CODE (TREE_VALUE (tail)) == SSA_NAME)
2659 set_reg_attrs_for_decl_rtl (SSA_NAME_VAR (TREE_VALUE (tail)), op);
2660 TREE_VALUE (tail) = make_tree (type, op);
2662 output_rtx[i] = op;
2664 generating_concat_p = old_generating_concat_p;
2666 if (is_inout)
2668 inout_mode[ninout] = TYPE_MODE (type);
2669 inout_opnum[ninout++] = i;
2672 if (tree_conflicts_with_clobbers_p (val, &clobbered_regs))
2673 clobber_conflict_found = 1;
2676 /* Make vectors for the expression-rtx, constraint strings,
2677 and named operands. */
2679 argvec = rtvec_alloc (ninputs);
2680 constraintvec = rtvec_alloc (ninputs);
2681 labelvec = rtvec_alloc (nlabels);
2683 body = gen_rtx_ASM_OPERANDS ((noutputs == 0 ? VOIDmode
2684 : GET_MODE (output_rtx[0])),
2685 ggc_strdup (TREE_STRING_POINTER (string)),
2686 empty_string, 0, argvec, constraintvec,
2687 labelvec, locus);
2689 MEM_VOLATILE_P (body) = vol;
2691 /* Eval the inputs and put them into ARGVEC.
2692 Put their constraints into ASM_INPUTs and store in CONSTRAINTS. */
2694 for (i = 0, tail = inputs; tail; tail = TREE_CHAIN (tail), ++i)
2696 bool allows_reg, allows_mem;
2697 const char *constraint;
2698 tree val, type;
2699 rtx op;
2700 bool ok;
2702 constraint = constraints[i + noutputs];
2703 ok = parse_input_constraint (&constraint, i, ninputs, noutputs, ninout,
2704 constraints, &allows_mem, &allows_reg);
2705 gcc_assert (ok);
2707 generating_concat_p = 0;
2709 val = TREE_VALUE (tail);
2710 type = TREE_TYPE (val);
2711 /* EXPAND_INITIALIZER will not generate code for valid initializer
2712 constants, but will still generate code for other types of operand.
2713 This is the behavior we want for constant constraints. */
2714 op = expand_expr (val, NULL_RTX, VOIDmode,
2715 allows_reg ? EXPAND_NORMAL
2716 : allows_mem ? EXPAND_MEMORY
2717 : EXPAND_INITIALIZER);
2719 /* Never pass a CONCAT to an ASM. */
2720 if (GET_CODE (op) == CONCAT)
2721 op = force_reg (GET_MODE (op), op);
2722 else if (MEM_P (op))
2723 op = validize_mem (op);
2725 if (asm_operand_ok (op, constraint, NULL) <= 0)
2727 if (allows_reg && TYPE_MODE (type) != BLKmode)
2728 op = force_reg (TYPE_MODE (type), op);
2729 else if (!allows_mem)
2730 warning (0, "asm operand %d probably doesn%'t match constraints",
2731 i + noutputs);
2732 else if (MEM_P (op))
2734 /* We won't recognize either volatile memory or memory
2735 with a queued address as available a memory_operand
2736 at this point. Ignore it: clearly this *is* a memory. */
2738 else
2739 gcc_unreachable ();
2742 generating_concat_p = old_generating_concat_p;
2743 ASM_OPERANDS_INPUT (body, i) = op;
2745 ASM_OPERANDS_INPUT_CONSTRAINT_EXP (body, i)
2746 = gen_rtx_ASM_INPUT_loc (TYPE_MODE (type),
2747 ggc_strdup (constraints[i + noutputs]),
2748 locus);
2750 if (tree_conflicts_with_clobbers_p (val, &clobbered_regs))
2751 clobber_conflict_found = 1;
2754 /* Protect all the operands from the queue now that they have all been
2755 evaluated. */
2757 generating_concat_p = 0;
2759 /* For in-out operands, copy output rtx to input rtx. */
2760 for (i = 0; i < ninout; i++)
2762 int j = inout_opnum[i];
2763 char buffer[16];
2765 ASM_OPERANDS_INPUT (body, ninputs - ninout + i)
2766 = output_rtx[j];
2768 sprintf (buffer, "%d", j);
2769 ASM_OPERANDS_INPUT_CONSTRAINT_EXP (body, ninputs - ninout + i)
2770 = gen_rtx_ASM_INPUT_loc (inout_mode[i], ggc_strdup (buffer), locus);
2773 /* Copy labels to the vector. */
2774 for (i = 0, tail = labels; i < nlabels; ++i, tail = TREE_CHAIN (tail))
2776 rtx r;
2777 /* If asm goto has any labels in the fallthru basic block, use
2778 a label that we emit immediately after the asm goto. Expansion
2779 may insert further instructions into the same basic block after
2780 asm goto and if we don't do this, insertion of instructions on
2781 the fallthru edge might misbehave. See PR58670. */
2782 if (fallthru_bb
2783 && label_to_block_fn (cfun, TREE_VALUE (tail)) == fallthru_bb)
2785 if (fallthru_label == NULL_RTX)
2786 fallthru_label = gen_label_rtx ();
2787 r = fallthru_label;
2789 else
2790 r = label_rtx (TREE_VALUE (tail));
2791 ASM_OPERANDS_LABEL (body, i) = gen_rtx_LABEL_REF (Pmode, r);
2794 generating_concat_p = old_generating_concat_p;
2796 /* Now, for each output, construct an rtx
2797 (set OUTPUT (asm_operands INSN OUTPUTCONSTRAINT OUTPUTNUMBER
2798 ARGVEC CONSTRAINTS OPNAMES))
2799 If there is more than one, put them inside a PARALLEL. */
2801 if (nlabels > 0 && nclobbers == 0)
2803 gcc_assert (noutputs == 0);
2804 emit_jump_insn (body);
2806 else if (noutputs == 0 && nclobbers == 0)
2808 /* No output operands: put in a raw ASM_OPERANDS rtx. */
2809 emit_insn (body);
2811 else if (noutputs == 1 && nclobbers == 0)
2813 ASM_OPERANDS_OUTPUT_CONSTRAINT (body) = ggc_strdup (constraints[0]);
2814 emit_insn (gen_rtx_SET (VOIDmode, output_rtx[0], body));
2816 else
2818 rtx obody = body;
2819 int num = noutputs;
2821 if (num == 0)
2822 num = 1;
2824 body = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (num + nclobbers));
2826 /* For each output operand, store a SET. */
2827 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
2829 XVECEXP (body, 0, i)
2830 = gen_rtx_SET (VOIDmode,
2831 output_rtx[i],
2832 gen_rtx_ASM_OPERANDS
2833 (GET_MODE (output_rtx[i]),
2834 ggc_strdup (TREE_STRING_POINTER (string)),
2835 ggc_strdup (constraints[i]),
2836 i, argvec, constraintvec, labelvec, locus));
2838 MEM_VOLATILE_P (SET_SRC (XVECEXP (body, 0, i))) = vol;
2841 /* If there are no outputs (but there are some clobbers)
2842 store the bare ASM_OPERANDS into the PARALLEL. */
2844 if (i == 0)
2845 XVECEXP (body, 0, i++) = obody;
2847 /* Store (clobber REG) for each clobbered register specified. */
2849 for (tail = clobbers; tail; tail = TREE_CHAIN (tail))
2851 const char *regname = TREE_STRING_POINTER (TREE_VALUE (tail));
2852 int reg, nregs;
2853 int j = decode_reg_name_and_count (regname, &nregs);
2854 rtx clobbered_reg;
2856 if (j < 0)
2858 if (j == -3) /* `cc', which is not a register */
2859 continue;
2861 if (j == -4) /* `memory', don't cache memory across asm */
2863 XVECEXP (body, 0, i++)
2864 = gen_rtx_CLOBBER (VOIDmode,
2865 gen_rtx_MEM
2866 (BLKmode,
2867 gen_rtx_SCRATCH (VOIDmode)));
2868 continue;
2871 /* Ignore unknown register, error already signaled. */
2872 continue;
2875 for (reg = j; reg < j + nregs; reg++)
2877 /* Use QImode since that's guaranteed to clobber just
2878 * one reg. */
2879 clobbered_reg = gen_rtx_REG (QImode, reg);
2881 /* Do sanity check for overlap between clobbers and
2882 respectively input and outputs that hasn't been
2883 handled. Such overlap should have been detected and
2884 reported above. */
2885 if (!clobber_conflict_found)
2887 int opno;
2889 /* We test the old body (obody) contents to avoid
2890 tripping over the under-construction body. */
2891 for (opno = 0; opno < noutputs; opno++)
2892 if (reg_overlap_mentioned_p (clobbered_reg,
2893 output_rtx[opno]))
2894 internal_error
2895 ("asm clobber conflict with output operand");
2897 for (opno = 0; opno < ninputs - ninout; opno++)
2898 if (reg_overlap_mentioned_p (clobbered_reg,
2899 ASM_OPERANDS_INPUT (obody,
2900 opno)))
2901 internal_error
2902 ("asm clobber conflict with input operand");
2905 XVECEXP (body, 0, i++)
2906 = gen_rtx_CLOBBER (VOIDmode, clobbered_reg);
2910 if (nlabels > 0)
2911 emit_jump_insn (body);
2912 else
2913 emit_insn (body);
2916 if (fallthru_label)
2917 emit_label (fallthru_label);
2919 /* For any outputs that needed reloading into registers, spill them
2920 back to where they belong. */
2921 for (i = 0; i < noutputs; ++i)
2922 if (real_output_rtx[i])
2923 emit_move_insn (real_output_rtx[i], output_rtx[i]);
2925 crtl->has_asm_statement = 1;
2926 free_temp_slots ();
2930 static void
2931 expand_asm_stmt (gimple_asm stmt)
2933 int noutputs;
2934 tree outputs, tail, t;
2935 tree *o;
2936 size_t i, n;
2937 const char *s;
2938 tree str, out, in, cl, labels;
2939 location_t locus = gimple_location (stmt);
2940 basic_block fallthru_bb = NULL;
2942 /* Meh... convert the gimple asm operands into real tree lists.
2943 Eventually we should make all routines work on the vectors instead
2944 of relying on TREE_CHAIN. */
2945 out = NULL_TREE;
2946 n = gimple_asm_noutputs (stmt);
2947 if (n > 0)
2949 t = out = gimple_asm_output_op (stmt, 0);
2950 for (i = 1; i < n; i++)
2951 t = TREE_CHAIN (t) = gimple_asm_output_op (stmt, i);
2954 in = NULL_TREE;
2955 n = gimple_asm_ninputs (stmt);
2956 if (n > 0)
2958 t = in = gimple_asm_input_op (stmt, 0);
2959 for (i = 1; i < n; i++)
2960 t = TREE_CHAIN (t) = gimple_asm_input_op (stmt, i);
2963 cl = NULL_TREE;
2964 n = gimple_asm_nclobbers (stmt);
2965 if (n > 0)
2967 t = cl = gimple_asm_clobber_op (stmt, 0);
2968 for (i = 1; i < n; i++)
2969 t = TREE_CHAIN (t) = gimple_asm_clobber_op (stmt, i);
2972 labels = NULL_TREE;
2973 n = gimple_asm_nlabels (stmt);
2974 if (n > 0)
2976 edge fallthru = find_fallthru_edge (gimple_bb (stmt)->succs);
2977 if (fallthru)
2978 fallthru_bb = fallthru->dest;
2979 t = labels = gimple_asm_label_op (stmt, 0);
2980 for (i = 1; i < n; i++)
2981 t = TREE_CHAIN (t) = gimple_asm_label_op (stmt, i);
2984 s = gimple_asm_string (stmt);
2985 str = build_string (strlen (s), s);
2987 if (gimple_asm_input_p (stmt))
2989 expand_asm_loc (str, gimple_asm_volatile_p (stmt), locus);
2990 return;
2993 outputs = out;
2994 noutputs = gimple_asm_noutputs (stmt);
2995 /* o[I] is the place that output number I should be written. */
2996 o = (tree *) alloca (noutputs * sizeof (tree));
2998 /* Record the contents of OUTPUTS before it is modified. */
2999 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
3000 o[i] = TREE_VALUE (tail);
3002 /* Generate the ASM_OPERANDS insn; store into the TREE_VALUEs of
3003 OUTPUTS some trees for where the values were actually stored. */
3004 expand_asm_operands (str, outputs, in, cl, labels, fallthru_bb,
3005 gimple_asm_volatile_p (stmt), locus);
3007 /* Copy all the intermediate outputs into the specified outputs. */
3008 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
3010 if (o[i] != TREE_VALUE (tail))
3012 expand_assignment (o[i], TREE_VALUE (tail), false);
3013 free_temp_slots ();
3015 /* Restore the original value so that it's correct the next
3016 time we expand this function. */
3017 TREE_VALUE (tail) = o[i];
3022 /* Emit code to jump to the address
3023 specified by the pointer expression EXP. */
3025 static void
3026 expand_computed_goto (tree exp)
3028 rtx x = expand_normal (exp);
3030 x = convert_memory_address (Pmode, x);
3032 do_pending_stack_adjust ();
3033 emit_indirect_jump (x);
3036 /* Generate RTL code for a `goto' statement with target label LABEL.
3037 LABEL should be a LABEL_DECL tree node that was or will later be
3038 defined with `expand_label'. */
3040 static void
3041 expand_goto (tree label)
3043 #ifdef ENABLE_CHECKING
3044 /* Check for a nonlocal goto to a containing function. Should have
3045 gotten translated to __builtin_nonlocal_goto. */
3046 tree context = decl_function_context (label);
3047 gcc_assert (!context || context == current_function_decl);
3048 #endif
3050 emit_jump (label_rtx (label));
3053 /* Output a return with no value. */
3055 static void
3056 expand_null_return_1 (void)
3058 clear_pending_stack_adjust ();
3059 do_pending_stack_adjust ();
3060 emit_jump (return_label);
3063 /* Generate RTL to return from the current function, with no value.
3064 (That is, we do not do anything about returning any value.) */
3066 void
3067 expand_null_return (void)
3069 /* If this function was declared to return a value, but we
3070 didn't, clobber the return registers so that they are not
3071 propagated live to the rest of the function. */
3072 clobber_return_register ();
3074 expand_null_return_1 ();
3077 /* Generate RTL to return from the current function, with value VAL. */
3079 static void
3080 expand_value_return (rtx val)
3082 /* Copy the value to the return location unless it's already there. */
3084 tree decl = DECL_RESULT (current_function_decl);
3085 rtx return_reg = DECL_RTL (decl);
3086 if (return_reg != val)
3088 tree funtype = TREE_TYPE (current_function_decl);
3089 tree type = TREE_TYPE (decl);
3090 int unsignedp = TYPE_UNSIGNED (type);
3091 enum machine_mode old_mode = DECL_MODE (decl);
3092 enum machine_mode mode;
3093 if (DECL_BY_REFERENCE (decl))
3094 mode = promote_function_mode (type, old_mode, &unsignedp, funtype, 2);
3095 else
3096 mode = promote_function_mode (type, old_mode, &unsignedp, funtype, 1);
3098 if (mode != old_mode)
3099 val = convert_modes (mode, old_mode, val, unsignedp);
3101 if (GET_CODE (return_reg) == PARALLEL)
3102 emit_group_load (return_reg, val, type, int_size_in_bytes (type));
3103 else
3104 emit_move_insn (return_reg, val);
3107 expand_null_return_1 ();
3110 /* Generate RTL to evaluate the expression RETVAL and return it
3111 from the current function. */
3113 static void
3114 expand_return (tree retval)
3116 rtx result_rtl;
3117 rtx val = 0;
3118 tree retval_rhs;
3120 /* If function wants no value, give it none. */
3121 if (TREE_CODE (TREE_TYPE (TREE_TYPE (current_function_decl))) == VOID_TYPE)
3123 expand_normal (retval);
3124 expand_null_return ();
3125 return;
3128 if (retval == error_mark_node)
3130 /* Treat this like a return of no value from a function that
3131 returns a value. */
3132 expand_null_return ();
3133 return;
3135 else if ((TREE_CODE (retval) == MODIFY_EXPR
3136 || TREE_CODE (retval) == INIT_EXPR)
3137 && TREE_CODE (TREE_OPERAND (retval, 0)) == RESULT_DECL)
3138 retval_rhs = TREE_OPERAND (retval, 1);
3139 else
3140 retval_rhs = retval;
3142 result_rtl = DECL_RTL (DECL_RESULT (current_function_decl));
3144 /* If we are returning the RESULT_DECL, then the value has already
3145 been stored into it, so we don't have to do anything special. */
3146 if (TREE_CODE (retval_rhs) == RESULT_DECL)
3147 expand_value_return (result_rtl);
3149 /* If the result is an aggregate that is being returned in one (or more)
3150 registers, load the registers here. */
3152 else if (retval_rhs != 0
3153 && TYPE_MODE (TREE_TYPE (retval_rhs)) == BLKmode
3154 && REG_P (result_rtl))
3156 val = copy_blkmode_to_reg (GET_MODE (result_rtl), retval_rhs);
3157 if (val)
3159 /* Use the mode of the result value on the return register. */
3160 PUT_MODE (result_rtl, GET_MODE (val));
3161 expand_value_return (val);
3163 else
3164 expand_null_return ();
3166 else if (retval_rhs != 0
3167 && !VOID_TYPE_P (TREE_TYPE (retval_rhs))
3168 && (REG_P (result_rtl)
3169 || (GET_CODE (result_rtl) == PARALLEL)))
3171 /* Compute the return value into a temporary (usually a pseudo reg). */
3173 = assign_temp (TREE_TYPE (DECL_RESULT (current_function_decl)), 0, 1);
3174 val = expand_expr (retval_rhs, val, GET_MODE (val), EXPAND_NORMAL);
3175 val = force_not_mem (val);
3176 expand_value_return (val);
3178 else
3180 /* No hard reg used; calculate value into hard return reg. */
3181 expand_expr (retval, const0_rtx, VOIDmode, EXPAND_NORMAL);
3182 expand_value_return (result_rtl);
3186 /* A subroutine of expand_gimple_stmt, expanding one gimple statement
3187 STMT that doesn't require special handling for outgoing edges. That
3188 is no tailcalls and no GIMPLE_COND. */
3190 static void
3191 expand_gimple_stmt_1 (gimple stmt)
3193 tree op0;
3195 set_curr_insn_location (gimple_location (stmt));
3197 switch (gimple_code (stmt))
3199 case GIMPLE_GOTO:
3200 op0 = gimple_goto_dest (stmt);
3201 if (TREE_CODE (op0) == LABEL_DECL)
3202 expand_goto (op0);
3203 else
3204 expand_computed_goto (op0);
3205 break;
3206 case GIMPLE_LABEL:
3207 expand_label (gimple_label_label (as_a <gimple_label> (stmt)));
3208 break;
3209 case GIMPLE_NOP:
3210 case GIMPLE_PREDICT:
3211 break;
3212 case GIMPLE_SWITCH:
3213 expand_case (as_a <gimple_switch> (stmt));
3214 break;
3215 case GIMPLE_ASM:
3216 expand_asm_stmt (as_a <gimple_asm> (stmt));
3217 break;
3218 case GIMPLE_CALL:
3219 expand_call_stmt (as_a <gimple_call> (stmt));
3220 break;
3222 case GIMPLE_RETURN:
3223 op0 = gimple_return_retval (as_a <gimple_return> (stmt));
3225 if (op0 && op0 != error_mark_node)
3227 tree result = DECL_RESULT (current_function_decl);
3229 /* If we are not returning the current function's RESULT_DECL,
3230 build an assignment to it. */
3231 if (op0 != result)
3233 /* I believe that a function's RESULT_DECL is unique. */
3234 gcc_assert (TREE_CODE (op0) != RESULT_DECL);
3236 /* ??? We'd like to use simply expand_assignment here,
3237 but this fails if the value is of BLKmode but the return
3238 decl is a register. expand_return has special handling
3239 for this combination, which eventually should move
3240 to common code. See comments there. Until then, let's
3241 build a modify expression :-/ */
3242 op0 = build2 (MODIFY_EXPR, TREE_TYPE (result),
3243 result, op0);
3246 if (!op0)
3247 expand_null_return ();
3248 else
3249 expand_return (op0);
3250 break;
3252 case GIMPLE_ASSIGN:
3254 gimple_assign assign_stmt = as_a <gimple_assign> (stmt);
3255 tree lhs = gimple_assign_lhs (assign_stmt);
3257 /* Tree expand used to fiddle with |= and &= of two bitfield
3258 COMPONENT_REFs here. This can't happen with gimple, the LHS
3259 of binary assigns must be a gimple reg. */
3261 if (TREE_CODE (lhs) != SSA_NAME
3262 || get_gimple_rhs_class (gimple_expr_code (stmt))
3263 == GIMPLE_SINGLE_RHS)
3265 tree rhs = gimple_assign_rhs1 (assign_stmt);
3266 gcc_assert (get_gimple_rhs_class (gimple_expr_code (stmt))
3267 == GIMPLE_SINGLE_RHS);
3268 if (gimple_has_location (stmt) && CAN_HAVE_LOCATION_P (rhs))
3269 SET_EXPR_LOCATION (rhs, gimple_location (stmt));
3270 if (TREE_CLOBBER_P (rhs))
3271 /* This is a clobber to mark the going out of scope for
3272 this LHS. */
3274 else
3275 expand_assignment (lhs, rhs,
3276 gimple_assign_nontemporal_move_p (
3277 assign_stmt));
3279 else
3281 rtx target, temp;
3282 bool nontemporal = gimple_assign_nontemporal_move_p (assign_stmt);
3283 struct separate_ops ops;
3284 bool promoted = false;
3286 target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
3287 if (GET_CODE (target) == SUBREG && SUBREG_PROMOTED_VAR_P (target))
3288 promoted = true;
3290 ops.code = gimple_assign_rhs_code (assign_stmt);
3291 ops.type = TREE_TYPE (lhs);
3292 switch (get_gimple_rhs_class (gimple_expr_code (stmt)))
3294 case GIMPLE_TERNARY_RHS:
3295 ops.op2 = gimple_assign_rhs3 (assign_stmt);
3296 /* Fallthru */
3297 case GIMPLE_BINARY_RHS:
3298 ops.op1 = gimple_assign_rhs2 (assign_stmt);
3299 /* Fallthru */
3300 case GIMPLE_UNARY_RHS:
3301 ops.op0 = gimple_assign_rhs1 (assign_stmt);
3302 break;
3303 default:
3304 gcc_unreachable ();
3306 ops.location = gimple_location (stmt);
3308 /* If we want to use a nontemporal store, force the value to
3309 register first. If we store into a promoted register,
3310 don't directly expand to target. */
3311 temp = nontemporal || promoted ? NULL_RTX : target;
3312 temp = expand_expr_real_2 (&ops, temp, GET_MODE (target),
3313 EXPAND_NORMAL);
3315 if (temp == target)
3317 else if (promoted)
3319 int unsignedp = SUBREG_PROMOTED_SIGN (target);
3320 /* If TEMP is a VOIDmode constant, use convert_modes to make
3321 sure that we properly convert it. */
3322 if (CONSTANT_P (temp) && GET_MODE (temp) == VOIDmode)
3324 temp = convert_modes (GET_MODE (target),
3325 TYPE_MODE (ops.type),
3326 temp, unsignedp);
3327 temp = convert_modes (GET_MODE (SUBREG_REG (target)),
3328 GET_MODE (target), temp, unsignedp);
3331 convert_move (SUBREG_REG (target), temp, unsignedp);
3333 else if (nontemporal && emit_storent_insn (target, temp))
3335 else
3337 temp = force_operand (temp, target);
3338 if (temp != target)
3339 emit_move_insn (target, temp);
3343 break;
3345 default:
3346 gcc_unreachable ();
3350 /* Expand one gimple statement STMT and return the last RTL instruction
3351 before any of the newly generated ones.
3353 In addition to generating the necessary RTL instructions this also
3354 sets REG_EH_REGION notes if necessary and sets the current source
3355 location for diagnostics. */
3357 static rtx_insn *
3358 expand_gimple_stmt (gimple stmt)
3360 location_t saved_location = input_location;
3361 rtx_insn *last = get_last_insn ();
3362 int lp_nr;
3364 gcc_assert (cfun);
3366 /* We need to save and restore the current source location so that errors
3367 discovered during expansion are emitted with the right location. But
3368 it would be better if the diagnostic routines used the source location
3369 embedded in the tree nodes rather than globals. */
3370 if (gimple_has_location (stmt))
3371 input_location = gimple_location (stmt);
3373 expand_gimple_stmt_1 (stmt);
3375 /* Free any temporaries used to evaluate this statement. */
3376 free_temp_slots ();
3378 input_location = saved_location;
3380 /* Mark all insns that may trap. */
3381 lp_nr = lookup_stmt_eh_lp (stmt);
3382 if (lp_nr)
3384 rtx_insn *insn;
3385 for (insn = next_real_insn (last); insn;
3386 insn = next_real_insn (insn))
3388 if (! find_reg_note (insn, REG_EH_REGION, NULL_RTX)
3389 /* If we want exceptions for non-call insns, any
3390 may_trap_p instruction may throw. */
3391 && GET_CODE (PATTERN (insn)) != CLOBBER
3392 && GET_CODE (PATTERN (insn)) != USE
3393 && insn_could_throw_p (insn))
3394 make_reg_eh_region_note (insn, 0, lp_nr);
3398 return last;
3401 /* A subroutine of expand_gimple_basic_block. Expand one GIMPLE_CALL
3402 that has CALL_EXPR_TAILCALL set. Returns non-null if we actually
3403 generated a tail call (something that might be denied by the ABI
3404 rules governing the call; see calls.c).
3406 Sets CAN_FALLTHRU if we generated a *conditional* tail call, and
3407 can still reach the rest of BB. The case here is __builtin_sqrt,
3408 where the NaN result goes through the external function (with a
3409 tailcall) and the normal result happens via a sqrt instruction. */
3411 static basic_block
3412 expand_gimple_tailcall (basic_block bb, gimple_call stmt, bool *can_fallthru)
3414 rtx_insn *last2, *last;
3415 edge e;
3416 edge_iterator ei;
3417 int probability;
3418 gcov_type count;
3420 last2 = last = expand_gimple_stmt (stmt);
3422 for (last = NEXT_INSN (last); last; last = NEXT_INSN (last))
3423 if (CALL_P (last) && SIBLING_CALL_P (last))
3424 goto found;
3426 maybe_dump_rtl_for_gimple_stmt (stmt, last2);
3428 *can_fallthru = true;
3429 return NULL;
3431 found:
3432 /* ??? Wouldn't it be better to just reset any pending stack adjust?
3433 Any instructions emitted here are about to be deleted. */
3434 do_pending_stack_adjust ();
3436 /* Remove any non-eh, non-abnormal edges that don't go to exit. */
3437 /* ??? I.e. the fallthrough edge. HOWEVER! If there were to be
3438 EH or abnormal edges, we shouldn't have created a tail call in
3439 the first place. So it seems to me we should just be removing
3440 all edges here, or redirecting the existing fallthru edge to
3441 the exit block. */
3443 probability = 0;
3444 count = 0;
3446 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
3448 if (!(e->flags & (EDGE_ABNORMAL | EDGE_EH)))
3450 if (e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun))
3452 e->dest->count -= e->count;
3453 e->dest->frequency -= EDGE_FREQUENCY (e);
3454 if (e->dest->count < 0)
3455 e->dest->count = 0;
3456 if (e->dest->frequency < 0)
3457 e->dest->frequency = 0;
3459 count += e->count;
3460 probability += e->probability;
3461 remove_edge (e);
3463 else
3464 ei_next (&ei);
3467 /* This is somewhat ugly: the call_expr expander often emits instructions
3468 after the sibcall (to perform the function return). These confuse the
3469 find_many_sub_basic_blocks code, so we need to get rid of these. */
3470 last = NEXT_INSN (last);
3471 gcc_assert (BARRIER_P (last));
3473 *can_fallthru = false;
3474 while (NEXT_INSN (last))
3476 /* For instance an sqrt builtin expander expands if with
3477 sibcall in the then and label for `else`. */
3478 if (LABEL_P (NEXT_INSN (last)))
3480 *can_fallthru = true;
3481 break;
3483 delete_insn (NEXT_INSN (last));
3486 e = make_edge (bb, EXIT_BLOCK_PTR_FOR_FN (cfun), EDGE_ABNORMAL
3487 | EDGE_SIBCALL);
3488 e->probability += probability;
3489 e->count += count;
3490 BB_END (bb) = last;
3491 update_bb_for_insn (bb);
3493 if (NEXT_INSN (last))
3495 bb = create_basic_block (NEXT_INSN (last), get_last_insn (), bb);
3497 last = BB_END (bb);
3498 if (BARRIER_P (last))
3499 BB_END (bb) = PREV_INSN (last);
3502 maybe_dump_rtl_for_gimple_stmt (stmt, last2);
3504 return bb;
3507 /* Return the difference between the floor and the truncated result of
3508 a signed division by OP1 with remainder MOD. */
3509 static rtx
3510 floor_sdiv_adjust (enum machine_mode mode, rtx mod, rtx op1)
3512 /* (mod != 0 ? (op1 / mod < 0 ? -1 : 0) : 0) */
3513 return gen_rtx_IF_THEN_ELSE
3514 (mode, gen_rtx_NE (BImode, mod, const0_rtx),
3515 gen_rtx_IF_THEN_ELSE
3516 (mode, gen_rtx_LT (BImode,
3517 gen_rtx_DIV (mode, op1, mod),
3518 const0_rtx),
3519 constm1_rtx, const0_rtx),
3520 const0_rtx);
3523 /* Return the difference between the ceil and the truncated result of
3524 a signed division by OP1 with remainder MOD. */
3525 static rtx
3526 ceil_sdiv_adjust (enum machine_mode mode, rtx mod, rtx op1)
3528 /* (mod != 0 ? (op1 / mod > 0 ? 1 : 0) : 0) */
3529 return gen_rtx_IF_THEN_ELSE
3530 (mode, gen_rtx_NE (BImode, mod, const0_rtx),
3531 gen_rtx_IF_THEN_ELSE
3532 (mode, gen_rtx_GT (BImode,
3533 gen_rtx_DIV (mode, op1, mod),
3534 const0_rtx),
3535 const1_rtx, const0_rtx),
3536 const0_rtx);
3539 /* Return the difference between the ceil and the truncated result of
3540 an unsigned division by OP1 with remainder MOD. */
3541 static rtx
3542 ceil_udiv_adjust (enum machine_mode mode, rtx mod, rtx op1 ATTRIBUTE_UNUSED)
3544 /* (mod != 0 ? 1 : 0) */
3545 return gen_rtx_IF_THEN_ELSE
3546 (mode, gen_rtx_NE (BImode, mod, const0_rtx),
3547 const1_rtx, const0_rtx);
3550 /* Return the difference between the rounded and the truncated result
3551 of a signed division by OP1 with remainder MOD. Halfway cases are
3552 rounded away from zero, rather than to the nearest even number. */
3553 static rtx
3554 round_sdiv_adjust (enum machine_mode mode, rtx mod, rtx op1)
3556 /* (abs (mod) >= abs (op1) - abs (mod)
3557 ? (op1 / mod > 0 ? 1 : -1)
3558 : 0) */
3559 return gen_rtx_IF_THEN_ELSE
3560 (mode, gen_rtx_GE (BImode, gen_rtx_ABS (mode, mod),
3561 gen_rtx_MINUS (mode,
3562 gen_rtx_ABS (mode, op1),
3563 gen_rtx_ABS (mode, mod))),
3564 gen_rtx_IF_THEN_ELSE
3565 (mode, gen_rtx_GT (BImode,
3566 gen_rtx_DIV (mode, op1, mod),
3567 const0_rtx),
3568 const1_rtx, constm1_rtx),
3569 const0_rtx);
3572 /* Return the difference between the rounded and the truncated result
3573 of a unsigned division by OP1 with remainder MOD. Halfway cases
3574 are rounded away from zero, rather than to the nearest even
3575 number. */
3576 static rtx
3577 round_udiv_adjust (enum machine_mode mode, rtx mod, rtx op1)
3579 /* (mod >= op1 - mod ? 1 : 0) */
3580 return gen_rtx_IF_THEN_ELSE
3581 (mode, gen_rtx_GE (BImode, mod,
3582 gen_rtx_MINUS (mode, op1, mod)),
3583 const1_rtx, const0_rtx);
3586 /* Convert X to MODE, that must be Pmode or ptr_mode, without emitting
3587 any rtl. */
3589 static rtx
3590 convert_debug_memory_address (enum machine_mode mode, rtx x,
3591 addr_space_t as)
3593 enum machine_mode xmode = GET_MODE (x);
3595 #ifndef POINTERS_EXTEND_UNSIGNED
3596 gcc_assert (mode == Pmode
3597 || mode == targetm.addr_space.address_mode (as));
3598 gcc_assert (xmode == mode || xmode == VOIDmode);
3599 #else
3600 rtx temp;
3602 gcc_assert (targetm.addr_space.valid_pointer_mode (mode, as));
3604 if (GET_MODE (x) == mode || GET_MODE (x) == VOIDmode)
3605 return x;
3607 if (GET_MODE_PRECISION (mode) < GET_MODE_PRECISION (xmode))
3608 x = simplify_gen_subreg (mode, x, xmode,
3609 subreg_lowpart_offset
3610 (mode, xmode));
3611 else if (POINTERS_EXTEND_UNSIGNED > 0)
3612 x = gen_rtx_ZERO_EXTEND (mode, x);
3613 else if (!POINTERS_EXTEND_UNSIGNED)
3614 x = gen_rtx_SIGN_EXTEND (mode, x);
3615 else
3617 switch (GET_CODE (x))
3619 case SUBREG:
3620 if ((SUBREG_PROMOTED_VAR_P (x)
3621 || (REG_P (SUBREG_REG (x)) && REG_POINTER (SUBREG_REG (x)))
3622 || (GET_CODE (SUBREG_REG (x)) == PLUS
3623 && REG_P (XEXP (SUBREG_REG (x), 0))
3624 && REG_POINTER (XEXP (SUBREG_REG (x), 0))
3625 && CONST_INT_P (XEXP (SUBREG_REG (x), 1))))
3626 && GET_MODE (SUBREG_REG (x)) == mode)
3627 return SUBREG_REG (x);
3628 break;
3629 case LABEL_REF:
3630 temp = gen_rtx_LABEL_REF (mode, LABEL_REF_LABEL (x));
3631 LABEL_REF_NONLOCAL_P (temp) = LABEL_REF_NONLOCAL_P (x);
3632 return temp;
3633 case SYMBOL_REF:
3634 temp = shallow_copy_rtx (x);
3635 PUT_MODE (temp, mode);
3636 return temp;
3637 case CONST:
3638 temp = convert_debug_memory_address (mode, XEXP (x, 0), as);
3639 if (temp)
3640 temp = gen_rtx_CONST (mode, temp);
3641 return temp;
3642 case PLUS:
3643 case MINUS:
3644 if (CONST_INT_P (XEXP (x, 1)))
3646 temp = convert_debug_memory_address (mode, XEXP (x, 0), as);
3647 if (temp)
3648 return gen_rtx_fmt_ee (GET_CODE (x), mode, temp, XEXP (x, 1));
3650 break;
3651 default:
3652 break;
3654 /* Don't know how to express ptr_extend as operation in debug info. */
3655 return NULL;
3657 #endif /* POINTERS_EXTEND_UNSIGNED */
3659 return x;
3662 /* Return an RTX equivalent to the value of the parameter DECL. */
3664 static rtx
3665 expand_debug_parm_decl (tree decl)
3667 rtx incoming = DECL_INCOMING_RTL (decl);
3669 if (incoming
3670 && GET_MODE (incoming) != BLKmode
3671 && ((REG_P (incoming) && HARD_REGISTER_P (incoming))
3672 || (MEM_P (incoming)
3673 && REG_P (XEXP (incoming, 0))
3674 && HARD_REGISTER_P (XEXP (incoming, 0)))))
3676 rtx rtl = gen_rtx_ENTRY_VALUE (GET_MODE (incoming));
3678 #ifdef HAVE_window_save
3679 /* DECL_INCOMING_RTL uses the INCOMING_REGNO of parameter registers.
3680 If the target machine has an explicit window save instruction, the
3681 actual entry value is the corresponding OUTGOING_REGNO instead. */
3682 if (REG_P (incoming)
3683 && OUTGOING_REGNO (REGNO (incoming)) != REGNO (incoming))
3684 incoming
3685 = gen_rtx_REG_offset (incoming, GET_MODE (incoming),
3686 OUTGOING_REGNO (REGNO (incoming)), 0);
3687 else if (MEM_P (incoming))
3689 rtx reg = XEXP (incoming, 0);
3690 if (OUTGOING_REGNO (REGNO (reg)) != REGNO (reg))
3692 reg = gen_raw_REG (GET_MODE (reg), OUTGOING_REGNO (REGNO (reg)));
3693 incoming = replace_equiv_address_nv (incoming, reg);
3695 else
3696 incoming = copy_rtx (incoming);
3698 #endif
3700 ENTRY_VALUE_EXP (rtl) = incoming;
3701 return rtl;
3704 if (incoming
3705 && GET_MODE (incoming) != BLKmode
3706 && !TREE_ADDRESSABLE (decl)
3707 && MEM_P (incoming)
3708 && (XEXP (incoming, 0) == virtual_incoming_args_rtx
3709 || (GET_CODE (XEXP (incoming, 0)) == PLUS
3710 && XEXP (XEXP (incoming, 0), 0) == virtual_incoming_args_rtx
3711 && CONST_INT_P (XEXP (XEXP (incoming, 0), 1)))))
3712 return copy_rtx (incoming);
3714 return NULL_RTX;
3717 /* Return an RTX equivalent to the value of the tree expression EXP. */
3719 static rtx
3720 expand_debug_expr (tree exp)
3722 rtx op0 = NULL_RTX, op1 = NULL_RTX, op2 = NULL_RTX;
3723 enum machine_mode mode = TYPE_MODE (TREE_TYPE (exp));
3724 enum machine_mode inner_mode = VOIDmode;
3725 int unsignedp = TYPE_UNSIGNED (TREE_TYPE (exp));
3726 addr_space_t as;
3728 switch (TREE_CODE_CLASS (TREE_CODE (exp)))
3730 case tcc_expression:
3731 switch (TREE_CODE (exp))
3733 case COND_EXPR:
3734 case DOT_PROD_EXPR:
3735 case SAD_EXPR:
3736 case WIDEN_MULT_PLUS_EXPR:
3737 case WIDEN_MULT_MINUS_EXPR:
3738 case FMA_EXPR:
3739 goto ternary;
3741 case TRUTH_ANDIF_EXPR:
3742 case TRUTH_ORIF_EXPR:
3743 case TRUTH_AND_EXPR:
3744 case TRUTH_OR_EXPR:
3745 case TRUTH_XOR_EXPR:
3746 goto binary;
3748 case TRUTH_NOT_EXPR:
3749 goto unary;
3751 default:
3752 break;
3754 break;
3756 ternary:
3757 op2 = expand_debug_expr (TREE_OPERAND (exp, 2));
3758 if (!op2)
3759 return NULL_RTX;
3760 /* Fall through. */
3762 binary:
3763 case tcc_binary:
3764 case tcc_comparison:
3765 op1 = expand_debug_expr (TREE_OPERAND (exp, 1));
3766 if (!op1)
3767 return NULL_RTX;
3768 /* Fall through. */
3770 unary:
3771 case tcc_unary:
3772 inner_mode = TYPE_MODE (TREE_TYPE (TREE_OPERAND (exp, 0)));
3773 op0 = expand_debug_expr (TREE_OPERAND (exp, 0));
3774 if (!op0)
3775 return NULL_RTX;
3776 break;
3778 case tcc_type:
3779 case tcc_statement:
3780 gcc_unreachable ();
3782 case tcc_constant:
3783 case tcc_exceptional:
3784 case tcc_declaration:
3785 case tcc_reference:
3786 case tcc_vl_exp:
3787 break;
3790 switch (TREE_CODE (exp))
3792 case STRING_CST:
3793 if (!lookup_constant_def (exp))
3795 if (strlen (TREE_STRING_POINTER (exp)) + 1
3796 != (size_t) TREE_STRING_LENGTH (exp))
3797 return NULL_RTX;
3798 op0 = gen_rtx_CONST_STRING (Pmode, TREE_STRING_POINTER (exp));
3799 op0 = gen_rtx_MEM (BLKmode, op0);
3800 set_mem_attributes (op0, exp, 0);
3801 return op0;
3803 /* Fall through... */
3805 case INTEGER_CST:
3806 case REAL_CST:
3807 case FIXED_CST:
3808 op0 = expand_expr (exp, NULL_RTX, mode, EXPAND_INITIALIZER);
3809 return op0;
3811 case COMPLEX_CST:
3812 gcc_assert (COMPLEX_MODE_P (mode));
3813 op0 = expand_debug_expr (TREE_REALPART (exp));
3814 op1 = expand_debug_expr (TREE_IMAGPART (exp));
3815 return gen_rtx_CONCAT (mode, op0, op1);
3817 case DEBUG_EXPR_DECL:
3818 op0 = DECL_RTL_IF_SET (exp);
3820 if (op0)
3821 return op0;
3823 op0 = gen_rtx_DEBUG_EXPR (mode);
3824 DEBUG_EXPR_TREE_DECL (op0) = exp;
3825 SET_DECL_RTL (exp, op0);
3827 return op0;
3829 case VAR_DECL:
3830 case PARM_DECL:
3831 case FUNCTION_DECL:
3832 case LABEL_DECL:
3833 case CONST_DECL:
3834 case RESULT_DECL:
3835 op0 = DECL_RTL_IF_SET (exp);
3837 /* This decl was probably optimized away. */
3838 if (!op0)
3840 if (TREE_CODE (exp) != VAR_DECL
3841 || DECL_EXTERNAL (exp)
3842 || !TREE_STATIC (exp)
3843 || !DECL_NAME (exp)
3844 || DECL_HARD_REGISTER (exp)
3845 || DECL_IN_CONSTANT_POOL (exp)
3846 || mode == VOIDmode)
3847 return NULL;
3849 op0 = make_decl_rtl_for_debug (exp);
3850 if (!MEM_P (op0)
3851 || GET_CODE (XEXP (op0, 0)) != SYMBOL_REF
3852 || SYMBOL_REF_DECL (XEXP (op0, 0)) != exp)
3853 return NULL;
3855 else
3856 op0 = copy_rtx (op0);
3858 if (GET_MODE (op0) == BLKmode
3859 /* If op0 is not BLKmode, but BLKmode is, adjust_mode
3860 below would ICE. While it is likely a FE bug,
3861 try to be robust here. See PR43166. */
3862 || mode == BLKmode
3863 || (mode == VOIDmode && GET_MODE (op0) != VOIDmode))
3865 gcc_assert (MEM_P (op0));
3866 op0 = adjust_address_nv (op0, mode, 0);
3867 return op0;
3870 /* Fall through. */
3872 adjust_mode:
3873 case PAREN_EXPR:
3874 case NOP_EXPR:
3875 case CONVERT_EXPR:
3877 inner_mode = GET_MODE (op0);
3879 if (mode == inner_mode)
3880 return op0;
3882 if (inner_mode == VOIDmode)
3884 if (TREE_CODE (exp) == SSA_NAME)
3885 inner_mode = TYPE_MODE (TREE_TYPE (exp));
3886 else
3887 inner_mode = TYPE_MODE (TREE_TYPE (TREE_OPERAND (exp, 0)));
3888 if (mode == inner_mode)
3889 return op0;
3892 if (FLOAT_MODE_P (mode) && FLOAT_MODE_P (inner_mode))
3894 if (GET_MODE_BITSIZE (mode) == GET_MODE_BITSIZE (inner_mode))
3895 op0 = simplify_gen_subreg (mode, op0, inner_mode, 0);
3896 else if (GET_MODE_BITSIZE (mode) < GET_MODE_BITSIZE (inner_mode))
3897 op0 = simplify_gen_unary (FLOAT_TRUNCATE, mode, op0, inner_mode);
3898 else
3899 op0 = simplify_gen_unary (FLOAT_EXTEND, mode, op0, inner_mode);
3901 else if (FLOAT_MODE_P (mode))
3903 gcc_assert (TREE_CODE (exp) != SSA_NAME);
3904 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp, 0))))
3905 op0 = simplify_gen_unary (UNSIGNED_FLOAT, mode, op0, inner_mode);
3906 else
3907 op0 = simplify_gen_unary (FLOAT, mode, op0, inner_mode);
3909 else if (FLOAT_MODE_P (inner_mode))
3911 if (unsignedp)
3912 op0 = simplify_gen_unary (UNSIGNED_FIX, mode, op0, inner_mode);
3913 else
3914 op0 = simplify_gen_unary (FIX, mode, op0, inner_mode);
3916 else if (CONSTANT_P (op0)
3917 || GET_MODE_PRECISION (mode) <= GET_MODE_PRECISION (inner_mode))
3918 op0 = simplify_gen_subreg (mode, op0, inner_mode,
3919 subreg_lowpart_offset (mode,
3920 inner_mode));
3921 else if (TREE_CODE_CLASS (TREE_CODE (exp)) == tcc_unary
3922 ? TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp, 0)))
3923 : unsignedp)
3924 op0 = simplify_gen_unary (ZERO_EXTEND, mode, op0, inner_mode);
3925 else
3926 op0 = simplify_gen_unary (SIGN_EXTEND, mode, op0, inner_mode);
3928 return op0;
3931 case MEM_REF:
3932 if (!is_gimple_mem_ref_addr (TREE_OPERAND (exp, 0)))
3934 tree newexp = fold_binary (MEM_REF, TREE_TYPE (exp),
3935 TREE_OPERAND (exp, 0),
3936 TREE_OPERAND (exp, 1));
3937 if (newexp)
3938 return expand_debug_expr (newexp);
3940 /* FALLTHROUGH */
3941 case INDIRECT_REF:
3942 inner_mode = TYPE_MODE (TREE_TYPE (TREE_OPERAND (exp, 0)));
3943 op0 = expand_debug_expr (TREE_OPERAND (exp, 0));
3944 if (!op0)
3945 return NULL;
3947 if (TREE_CODE (exp) == MEM_REF)
3949 if (GET_CODE (op0) == DEBUG_IMPLICIT_PTR
3950 || (GET_CODE (op0) == PLUS
3951 && GET_CODE (XEXP (op0, 0)) == DEBUG_IMPLICIT_PTR))
3952 /* (mem (debug_implicit_ptr)) might confuse aliasing.
3953 Instead just use get_inner_reference. */
3954 goto component_ref;
3956 op1 = expand_debug_expr (TREE_OPERAND (exp, 1));
3957 if (!op1 || !CONST_INT_P (op1))
3958 return NULL;
3960 op0 = plus_constant (inner_mode, op0, INTVAL (op1));
3963 as = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0))));
3965 op0 = convert_debug_memory_address (targetm.addr_space.address_mode (as),
3966 op0, as);
3967 if (op0 == NULL_RTX)
3968 return NULL;
3970 op0 = gen_rtx_MEM (mode, op0);
3971 set_mem_attributes (op0, exp, 0);
3972 if (TREE_CODE (exp) == MEM_REF
3973 && !is_gimple_mem_ref_addr (TREE_OPERAND (exp, 0)))
3974 set_mem_expr (op0, NULL_TREE);
3975 set_mem_addr_space (op0, as);
3977 return op0;
3979 case TARGET_MEM_REF:
3980 if (TREE_CODE (TMR_BASE (exp)) == ADDR_EXPR
3981 && !DECL_RTL_SET_P (TREE_OPERAND (TMR_BASE (exp), 0)))
3982 return NULL;
3984 op0 = expand_debug_expr
3985 (tree_mem_ref_addr (build_pointer_type (TREE_TYPE (exp)), exp));
3986 if (!op0)
3987 return NULL;
3989 as = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0))));
3990 op0 = convert_debug_memory_address (targetm.addr_space.address_mode (as),
3991 op0, as);
3992 if (op0 == NULL_RTX)
3993 return NULL;
3995 op0 = gen_rtx_MEM (mode, op0);
3997 set_mem_attributes (op0, exp, 0);
3998 set_mem_addr_space (op0, as);
4000 return op0;
4002 component_ref:
4003 case ARRAY_REF:
4004 case ARRAY_RANGE_REF:
4005 case COMPONENT_REF:
4006 case BIT_FIELD_REF:
4007 case REALPART_EXPR:
4008 case IMAGPART_EXPR:
4009 case VIEW_CONVERT_EXPR:
4011 enum machine_mode mode1;
4012 HOST_WIDE_INT bitsize, bitpos;
4013 tree offset;
4014 int volatilep = 0;
4015 tree tem = get_inner_reference (exp, &bitsize, &bitpos, &offset,
4016 &mode1, &unsignedp, &volatilep, false);
4017 rtx orig_op0;
4019 if (bitsize == 0)
4020 return NULL;
4022 orig_op0 = op0 = expand_debug_expr (tem);
4024 if (!op0)
4025 return NULL;
4027 if (offset)
4029 enum machine_mode addrmode, offmode;
4031 if (!MEM_P (op0))
4032 return NULL;
4034 op0 = XEXP (op0, 0);
4035 addrmode = GET_MODE (op0);
4036 if (addrmode == VOIDmode)
4037 addrmode = Pmode;
4039 op1 = expand_debug_expr (offset);
4040 if (!op1)
4041 return NULL;
4043 offmode = GET_MODE (op1);
4044 if (offmode == VOIDmode)
4045 offmode = TYPE_MODE (TREE_TYPE (offset));
4047 if (addrmode != offmode)
4048 op1 = simplify_gen_subreg (addrmode, op1, offmode,
4049 subreg_lowpart_offset (addrmode,
4050 offmode));
4052 /* Don't use offset_address here, we don't need a
4053 recognizable address, and we don't want to generate
4054 code. */
4055 op0 = gen_rtx_MEM (mode, simplify_gen_binary (PLUS, addrmode,
4056 op0, op1));
4059 if (MEM_P (op0))
4061 if (mode1 == VOIDmode)
4062 /* Bitfield. */
4063 mode1 = smallest_mode_for_size (bitsize, MODE_INT);
4064 if (bitpos >= BITS_PER_UNIT)
4066 op0 = adjust_address_nv (op0, mode1, bitpos / BITS_PER_UNIT);
4067 bitpos %= BITS_PER_UNIT;
4069 else if (bitpos < 0)
4071 HOST_WIDE_INT units
4072 = (-bitpos + BITS_PER_UNIT - 1) / BITS_PER_UNIT;
4073 op0 = adjust_address_nv (op0, mode1, units);
4074 bitpos += units * BITS_PER_UNIT;
4076 else if (bitpos == 0 && bitsize == GET_MODE_BITSIZE (mode))
4077 op0 = adjust_address_nv (op0, mode, 0);
4078 else if (GET_MODE (op0) != mode1)
4079 op0 = adjust_address_nv (op0, mode1, 0);
4080 else
4081 op0 = copy_rtx (op0);
4082 if (op0 == orig_op0)
4083 op0 = shallow_copy_rtx (op0);
4084 set_mem_attributes (op0, exp, 0);
4087 if (bitpos == 0 && mode == GET_MODE (op0))
4088 return op0;
4090 if (bitpos < 0)
4091 return NULL;
4093 if (GET_MODE (op0) == BLKmode)
4094 return NULL;
4096 if ((bitpos % BITS_PER_UNIT) == 0
4097 && bitsize == GET_MODE_BITSIZE (mode1))
4099 enum machine_mode opmode = GET_MODE (op0);
4101 if (opmode == VOIDmode)
4102 opmode = TYPE_MODE (TREE_TYPE (tem));
4104 /* This condition may hold if we're expanding the address
4105 right past the end of an array that turned out not to
4106 be addressable (i.e., the address was only computed in
4107 debug stmts). The gen_subreg below would rightfully
4108 crash, and the address doesn't really exist, so just
4109 drop it. */
4110 if (bitpos >= GET_MODE_BITSIZE (opmode))
4111 return NULL;
4113 if ((bitpos % GET_MODE_BITSIZE (mode)) == 0)
4114 return simplify_gen_subreg (mode, op0, opmode,
4115 bitpos / BITS_PER_UNIT);
4118 return simplify_gen_ternary (SCALAR_INT_MODE_P (GET_MODE (op0))
4119 && TYPE_UNSIGNED (TREE_TYPE (exp))
4120 ? SIGN_EXTRACT
4121 : ZERO_EXTRACT, mode,
4122 GET_MODE (op0) != VOIDmode
4123 ? GET_MODE (op0)
4124 : TYPE_MODE (TREE_TYPE (tem)),
4125 op0, GEN_INT (bitsize), GEN_INT (bitpos));
4128 case ABS_EXPR:
4129 return simplify_gen_unary (ABS, mode, op0, mode);
4131 case NEGATE_EXPR:
4132 return simplify_gen_unary (NEG, mode, op0, mode);
4134 case BIT_NOT_EXPR:
4135 return simplify_gen_unary (NOT, mode, op0, mode);
4137 case FLOAT_EXPR:
4138 return simplify_gen_unary (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp,
4139 0)))
4140 ? UNSIGNED_FLOAT : FLOAT, mode, op0,
4141 inner_mode);
4143 case FIX_TRUNC_EXPR:
4144 return simplify_gen_unary (unsignedp ? UNSIGNED_FIX : FIX, mode, op0,
4145 inner_mode);
4147 case POINTER_PLUS_EXPR:
4148 /* For the rare target where pointers are not the same size as
4149 size_t, we need to check for mis-matched modes and correct
4150 the addend. */
4151 if (op0 && op1
4152 && GET_MODE (op0) != VOIDmode && GET_MODE (op1) != VOIDmode
4153 && GET_MODE (op0) != GET_MODE (op1))
4155 if (GET_MODE_BITSIZE (GET_MODE (op0)) < GET_MODE_BITSIZE (GET_MODE (op1))
4156 /* If OP0 is a partial mode, then we must truncate, even if it has
4157 the same bitsize as OP1 as GCC's representation of partial modes
4158 is opaque. */
4159 || (GET_MODE_CLASS (GET_MODE (op0)) == MODE_PARTIAL_INT
4160 && GET_MODE_BITSIZE (GET_MODE (op0)) == GET_MODE_BITSIZE (GET_MODE (op1))))
4161 op1 = simplify_gen_unary (TRUNCATE, GET_MODE (op0), op1,
4162 GET_MODE (op1));
4163 else
4164 /* We always sign-extend, regardless of the signedness of
4165 the operand, because the operand is always unsigned
4166 here even if the original C expression is signed. */
4167 op1 = simplify_gen_unary (SIGN_EXTEND, GET_MODE (op0), op1,
4168 GET_MODE (op1));
4170 /* Fall through. */
4171 case PLUS_EXPR:
4172 return simplify_gen_binary (PLUS, mode, op0, op1);
4174 case MINUS_EXPR:
4175 return simplify_gen_binary (MINUS, mode, op0, op1);
4177 case MULT_EXPR:
4178 return simplify_gen_binary (MULT, mode, op0, op1);
4180 case RDIV_EXPR:
4181 case TRUNC_DIV_EXPR:
4182 case EXACT_DIV_EXPR:
4183 if (unsignedp)
4184 return simplify_gen_binary (UDIV, mode, op0, op1);
4185 else
4186 return simplify_gen_binary (DIV, mode, op0, op1);
4188 case TRUNC_MOD_EXPR:
4189 return simplify_gen_binary (unsignedp ? UMOD : MOD, mode, op0, op1);
4191 case FLOOR_DIV_EXPR:
4192 if (unsignedp)
4193 return simplify_gen_binary (UDIV, mode, op0, op1);
4194 else
4196 rtx div = simplify_gen_binary (DIV, mode, op0, op1);
4197 rtx mod = simplify_gen_binary (MOD, mode, op0, op1);
4198 rtx adj = floor_sdiv_adjust (mode, mod, op1);
4199 return simplify_gen_binary (PLUS, mode, div, adj);
4202 case FLOOR_MOD_EXPR:
4203 if (unsignedp)
4204 return simplify_gen_binary (UMOD, mode, op0, op1);
4205 else
4207 rtx mod = simplify_gen_binary (MOD, mode, op0, op1);
4208 rtx adj = floor_sdiv_adjust (mode, mod, op1);
4209 adj = simplify_gen_unary (NEG, mode,
4210 simplify_gen_binary (MULT, mode, adj, op1),
4211 mode);
4212 return simplify_gen_binary (PLUS, mode, mod, adj);
4215 case CEIL_DIV_EXPR:
4216 if (unsignedp)
4218 rtx div = simplify_gen_binary (UDIV, mode, op0, op1);
4219 rtx mod = simplify_gen_binary (UMOD, mode, op0, op1);
4220 rtx adj = ceil_udiv_adjust (mode, mod, op1);
4221 return simplify_gen_binary (PLUS, mode, div, adj);
4223 else
4225 rtx div = simplify_gen_binary (DIV, mode, op0, op1);
4226 rtx mod = simplify_gen_binary (MOD, mode, op0, op1);
4227 rtx adj = ceil_sdiv_adjust (mode, mod, op1);
4228 return simplify_gen_binary (PLUS, mode, div, adj);
4231 case CEIL_MOD_EXPR:
4232 if (unsignedp)
4234 rtx mod = simplify_gen_binary (UMOD, mode, op0, op1);
4235 rtx adj = ceil_udiv_adjust (mode, mod, op1);
4236 adj = simplify_gen_unary (NEG, mode,
4237 simplify_gen_binary (MULT, mode, adj, op1),
4238 mode);
4239 return simplify_gen_binary (PLUS, mode, mod, adj);
4241 else
4243 rtx mod = simplify_gen_binary (MOD, mode, op0, op1);
4244 rtx adj = ceil_sdiv_adjust (mode, mod, op1);
4245 adj = simplify_gen_unary (NEG, mode,
4246 simplify_gen_binary (MULT, mode, adj, op1),
4247 mode);
4248 return simplify_gen_binary (PLUS, mode, mod, adj);
4251 case ROUND_DIV_EXPR:
4252 if (unsignedp)
4254 rtx div = simplify_gen_binary (UDIV, mode, op0, op1);
4255 rtx mod = simplify_gen_binary (UMOD, mode, op0, op1);
4256 rtx adj = round_udiv_adjust (mode, mod, op1);
4257 return simplify_gen_binary (PLUS, mode, div, adj);
4259 else
4261 rtx div = simplify_gen_binary (DIV, mode, op0, op1);
4262 rtx mod = simplify_gen_binary (MOD, mode, op0, op1);
4263 rtx adj = round_sdiv_adjust (mode, mod, op1);
4264 return simplify_gen_binary (PLUS, mode, div, adj);
4267 case ROUND_MOD_EXPR:
4268 if (unsignedp)
4270 rtx mod = simplify_gen_binary (UMOD, mode, op0, op1);
4271 rtx adj = round_udiv_adjust (mode, mod, op1);
4272 adj = simplify_gen_unary (NEG, mode,
4273 simplify_gen_binary (MULT, mode, adj, op1),
4274 mode);
4275 return simplify_gen_binary (PLUS, mode, mod, adj);
4277 else
4279 rtx mod = simplify_gen_binary (MOD, mode, op0, op1);
4280 rtx adj = round_sdiv_adjust (mode, mod, op1);
4281 adj = simplify_gen_unary (NEG, mode,
4282 simplify_gen_binary (MULT, mode, adj, op1),
4283 mode);
4284 return simplify_gen_binary (PLUS, mode, mod, adj);
4287 case LSHIFT_EXPR:
4288 return simplify_gen_binary (ASHIFT, mode, op0, op1);
4290 case RSHIFT_EXPR:
4291 if (unsignedp)
4292 return simplify_gen_binary (LSHIFTRT, mode, op0, op1);
4293 else
4294 return simplify_gen_binary (ASHIFTRT, mode, op0, op1);
4296 case LROTATE_EXPR:
4297 return simplify_gen_binary (ROTATE, mode, op0, op1);
4299 case RROTATE_EXPR:
4300 return simplify_gen_binary (ROTATERT, mode, op0, op1);
4302 case MIN_EXPR:
4303 return simplify_gen_binary (unsignedp ? UMIN : SMIN, mode, op0, op1);
4305 case MAX_EXPR:
4306 return simplify_gen_binary (unsignedp ? UMAX : SMAX, mode, op0, op1);
4308 case BIT_AND_EXPR:
4309 case TRUTH_AND_EXPR:
4310 return simplify_gen_binary (AND, mode, op0, op1);
4312 case BIT_IOR_EXPR:
4313 case TRUTH_OR_EXPR:
4314 return simplify_gen_binary (IOR, mode, op0, op1);
4316 case BIT_XOR_EXPR:
4317 case TRUTH_XOR_EXPR:
4318 return simplify_gen_binary (XOR, mode, op0, op1);
4320 case TRUTH_ANDIF_EXPR:
4321 return gen_rtx_IF_THEN_ELSE (mode, op0, op1, const0_rtx);
4323 case TRUTH_ORIF_EXPR:
4324 return gen_rtx_IF_THEN_ELSE (mode, op0, const_true_rtx, op1);
4326 case TRUTH_NOT_EXPR:
4327 return simplify_gen_relational (EQ, mode, inner_mode, op0, const0_rtx);
4329 case LT_EXPR:
4330 return simplify_gen_relational (unsignedp ? LTU : LT, mode, inner_mode,
4331 op0, op1);
4333 case LE_EXPR:
4334 return simplify_gen_relational (unsignedp ? LEU : LE, mode, inner_mode,
4335 op0, op1);
4337 case GT_EXPR:
4338 return simplify_gen_relational (unsignedp ? GTU : GT, mode, inner_mode,
4339 op0, op1);
4341 case GE_EXPR:
4342 return simplify_gen_relational (unsignedp ? GEU : GE, mode, inner_mode,
4343 op0, op1);
4345 case EQ_EXPR:
4346 return simplify_gen_relational (EQ, mode, inner_mode, op0, op1);
4348 case NE_EXPR:
4349 return simplify_gen_relational (NE, mode, inner_mode, op0, op1);
4351 case UNORDERED_EXPR:
4352 return simplify_gen_relational (UNORDERED, mode, inner_mode, op0, op1);
4354 case ORDERED_EXPR:
4355 return simplify_gen_relational (ORDERED, mode, inner_mode, op0, op1);
4357 case UNLT_EXPR:
4358 return simplify_gen_relational (UNLT, mode, inner_mode, op0, op1);
4360 case UNLE_EXPR:
4361 return simplify_gen_relational (UNLE, mode, inner_mode, op0, op1);
4363 case UNGT_EXPR:
4364 return simplify_gen_relational (UNGT, mode, inner_mode, op0, op1);
4366 case UNGE_EXPR:
4367 return simplify_gen_relational (UNGE, mode, inner_mode, op0, op1);
4369 case UNEQ_EXPR:
4370 return simplify_gen_relational (UNEQ, mode, inner_mode, op0, op1);
4372 case LTGT_EXPR:
4373 return simplify_gen_relational (LTGT, mode, inner_mode, op0, op1);
4375 case COND_EXPR:
4376 return gen_rtx_IF_THEN_ELSE (mode, op0, op1, op2);
4378 case COMPLEX_EXPR:
4379 gcc_assert (COMPLEX_MODE_P (mode));
4380 if (GET_MODE (op0) == VOIDmode)
4381 op0 = gen_rtx_CONST (GET_MODE_INNER (mode), op0);
4382 if (GET_MODE (op1) == VOIDmode)
4383 op1 = gen_rtx_CONST (GET_MODE_INNER (mode), op1);
4384 return gen_rtx_CONCAT (mode, op0, op1);
4386 case CONJ_EXPR:
4387 if (GET_CODE (op0) == CONCAT)
4388 return gen_rtx_CONCAT (mode, XEXP (op0, 0),
4389 simplify_gen_unary (NEG, GET_MODE_INNER (mode),
4390 XEXP (op0, 1),
4391 GET_MODE_INNER (mode)));
4392 else
4394 enum machine_mode imode = GET_MODE_INNER (mode);
4395 rtx re, im;
4397 if (MEM_P (op0))
4399 re = adjust_address_nv (op0, imode, 0);
4400 im = adjust_address_nv (op0, imode, GET_MODE_SIZE (imode));
4402 else
4404 enum machine_mode ifmode = int_mode_for_mode (mode);
4405 enum machine_mode ihmode = int_mode_for_mode (imode);
4406 rtx halfsize;
4407 if (ifmode == BLKmode || ihmode == BLKmode)
4408 return NULL;
4409 halfsize = GEN_INT (GET_MODE_BITSIZE (ihmode));
4410 re = op0;
4411 if (mode != ifmode)
4412 re = gen_rtx_SUBREG (ifmode, re, 0);
4413 re = gen_rtx_ZERO_EXTRACT (ihmode, re, halfsize, const0_rtx);
4414 if (imode != ihmode)
4415 re = gen_rtx_SUBREG (imode, re, 0);
4416 im = copy_rtx (op0);
4417 if (mode != ifmode)
4418 im = gen_rtx_SUBREG (ifmode, im, 0);
4419 im = gen_rtx_ZERO_EXTRACT (ihmode, im, halfsize, halfsize);
4420 if (imode != ihmode)
4421 im = gen_rtx_SUBREG (imode, im, 0);
4423 im = gen_rtx_NEG (imode, im);
4424 return gen_rtx_CONCAT (mode, re, im);
4427 case ADDR_EXPR:
4428 op0 = expand_debug_expr (TREE_OPERAND (exp, 0));
4429 if (!op0 || !MEM_P (op0))
4431 if ((TREE_CODE (TREE_OPERAND (exp, 0)) == VAR_DECL
4432 || TREE_CODE (TREE_OPERAND (exp, 0)) == PARM_DECL
4433 || TREE_CODE (TREE_OPERAND (exp, 0)) == RESULT_DECL)
4434 && (!TREE_ADDRESSABLE (TREE_OPERAND (exp, 0))
4435 || target_for_debug_bind (TREE_OPERAND (exp, 0))))
4436 return gen_rtx_DEBUG_IMPLICIT_PTR (mode, TREE_OPERAND (exp, 0));
4438 if (handled_component_p (TREE_OPERAND (exp, 0)))
4440 HOST_WIDE_INT bitoffset, bitsize, maxsize;
4441 tree decl
4442 = get_ref_base_and_extent (TREE_OPERAND (exp, 0),
4443 &bitoffset, &bitsize, &maxsize);
4444 if ((TREE_CODE (decl) == VAR_DECL
4445 || TREE_CODE (decl) == PARM_DECL
4446 || TREE_CODE (decl) == RESULT_DECL)
4447 && (!TREE_ADDRESSABLE (decl)
4448 || target_for_debug_bind (decl))
4449 && (bitoffset % BITS_PER_UNIT) == 0
4450 && bitsize > 0
4451 && bitsize == maxsize)
4453 rtx base = gen_rtx_DEBUG_IMPLICIT_PTR (mode, decl);
4454 return plus_constant (mode, base, bitoffset / BITS_PER_UNIT);
4458 if (TREE_CODE (TREE_OPERAND (exp, 0)) == MEM_REF
4459 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
4460 == ADDR_EXPR)
4462 op0 = expand_debug_expr (TREE_OPERAND (TREE_OPERAND (exp, 0),
4463 0));
4464 if (op0 != NULL
4465 && (GET_CODE (op0) == DEBUG_IMPLICIT_PTR
4466 || (GET_CODE (op0) == PLUS
4467 && GET_CODE (XEXP (op0, 0)) == DEBUG_IMPLICIT_PTR
4468 && CONST_INT_P (XEXP (op0, 1)))))
4470 op1 = expand_debug_expr (TREE_OPERAND (TREE_OPERAND (exp, 0),
4471 1));
4472 if (!op1 || !CONST_INT_P (op1))
4473 return NULL;
4475 return plus_constant (mode, op0, INTVAL (op1));
4479 return NULL;
4482 as = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (exp)));
4483 op0 = convert_debug_memory_address (mode, XEXP (op0, 0), as);
4485 return op0;
4487 case VECTOR_CST:
4489 unsigned i;
4491 op0 = gen_rtx_CONCATN
4492 (mode, rtvec_alloc (TYPE_VECTOR_SUBPARTS (TREE_TYPE (exp))));
4494 for (i = 0; i < VECTOR_CST_NELTS (exp); ++i)
4496 op1 = expand_debug_expr (VECTOR_CST_ELT (exp, i));
4497 if (!op1)
4498 return NULL;
4499 XVECEXP (op0, 0, i) = op1;
4502 return op0;
4505 case CONSTRUCTOR:
4506 if (TREE_CLOBBER_P (exp))
4507 return NULL;
4508 else if (TREE_CODE (TREE_TYPE (exp)) == VECTOR_TYPE)
4510 unsigned i;
4511 tree val;
4513 op0 = gen_rtx_CONCATN
4514 (mode, rtvec_alloc (TYPE_VECTOR_SUBPARTS (TREE_TYPE (exp))));
4516 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), i, val)
4518 op1 = expand_debug_expr (val);
4519 if (!op1)
4520 return NULL;
4521 XVECEXP (op0, 0, i) = op1;
4524 if (i < TYPE_VECTOR_SUBPARTS (TREE_TYPE (exp)))
4526 op1 = expand_debug_expr
4527 (build_zero_cst (TREE_TYPE (TREE_TYPE (exp))));
4529 if (!op1)
4530 return NULL;
4532 for (; i < TYPE_VECTOR_SUBPARTS (TREE_TYPE (exp)); i++)
4533 XVECEXP (op0, 0, i) = op1;
4536 return op0;
4538 else
4539 goto flag_unsupported;
4541 case CALL_EXPR:
4542 /* ??? Maybe handle some builtins? */
4543 return NULL;
4545 case SSA_NAME:
4547 gimple g = get_gimple_for_ssa_name (exp);
4548 if (g)
4550 op0 = expand_debug_expr (gimple_assign_rhs_to_tree (g));
4551 if (!op0)
4552 return NULL;
4554 else
4556 int part = var_to_partition (SA.map, exp);
4558 if (part == NO_PARTITION)
4560 /* If this is a reference to an incoming value of parameter
4561 that is never used in the code or where the incoming
4562 value is never used in the code, use PARM_DECL's
4563 DECL_RTL if set. */
4564 if (SSA_NAME_IS_DEFAULT_DEF (exp)
4565 && TREE_CODE (SSA_NAME_VAR (exp)) == PARM_DECL)
4567 op0 = expand_debug_parm_decl (SSA_NAME_VAR (exp));
4568 if (op0)
4569 goto adjust_mode;
4570 op0 = expand_debug_expr (SSA_NAME_VAR (exp));
4571 if (op0)
4572 goto adjust_mode;
4574 return NULL;
4577 gcc_assert (part >= 0 && (unsigned)part < SA.map->num_partitions);
4579 op0 = copy_rtx (SA.partition_to_pseudo[part]);
4581 goto adjust_mode;
4584 case ERROR_MARK:
4585 return NULL;
4587 /* Vector stuff. For most of the codes we don't have rtl codes. */
4588 case REALIGN_LOAD_EXPR:
4589 case REDUC_MAX_EXPR:
4590 case REDUC_MIN_EXPR:
4591 case REDUC_PLUS_EXPR:
4592 case VEC_COND_EXPR:
4593 case VEC_LSHIFT_EXPR:
4594 case VEC_PACK_FIX_TRUNC_EXPR:
4595 case VEC_PACK_SAT_EXPR:
4596 case VEC_PACK_TRUNC_EXPR:
4597 case VEC_RSHIFT_EXPR:
4598 case VEC_UNPACK_FLOAT_HI_EXPR:
4599 case VEC_UNPACK_FLOAT_LO_EXPR:
4600 case VEC_UNPACK_HI_EXPR:
4601 case VEC_UNPACK_LO_EXPR:
4602 case VEC_WIDEN_MULT_HI_EXPR:
4603 case VEC_WIDEN_MULT_LO_EXPR:
4604 case VEC_WIDEN_MULT_EVEN_EXPR:
4605 case VEC_WIDEN_MULT_ODD_EXPR:
4606 case VEC_WIDEN_LSHIFT_HI_EXPR:
4607 case VEC_WIDEN_LSHIFT_LO_EXPR:
4608 case VEC_PERM_EXPR:
4609 return NULL;
4611 /* Misc codes. */
4612 case ADDR_SPACE_CONVERT_EXPR:
4613 case FIXED_CONVERT_EXPR:
4614 case OBJ_TYPE_REF:
4615 case WITH_SIZE_EXPR:
4616 return NULL;
4618 case DOT_PROD_EXPR:
4619 if (SCALAR_INT_MODE_P (GET_MODE (op0))
4620 && SCALAR_INT_MODE_P (mode))
4623 = simplify_gen_unary (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp,
4624 0)))
4625 ? ZERO_EXTEND : SIGN_EXTEND, mode, op0,
4626 inner_mode);
4628 = simplify_gen_unary (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp,
4629 1)))
4630 ? ZERO_EXTEND : SIGN_EXTEND, mode, op1,
4631 inner_mode);
4632 op0 = simplify_gen_binary (MULT, mode, op0, op1);
4633 return simplify_gen_binary (PLUS, mode, op0, op2);
4635 return NULL;
4637 case WIDEN_MULT_EXPR:
4638 case WIDEN_MULT_PLUS_EXPR:
4639 case WIDEN_MULT_MINUS_EXPR:
4640 if (SCALAR_INT_MODE_P (GET_MODE (op0))
4641 && SCALAR_INT_MODE_P (mode))
4643 inner_mode = GET_MODE (op0);
4644 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp, 0))))
4645 op0 = simplify_gen_unary (ZERO_EXTEND, mode, op0, inner_mode);
4646 else
4647 op0 = simplify_gen_unary (SIGN_EXTEND, mode, op0, inner_mode);
4648 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp, 1))))
4649 op1 = simplify_gen_unary (ZERO_EXTEND, mode, op1, inner_mode);
4650 else
4651 op1 = simplify_gen_unary (SIGN_EXTEND, mode, op1, inner_mode);
4652 op0 = simplify_gen_binary (MULT, mode, op0, op1);
4653 if (TREE_CODE (exp) == WIDEN_MULT_EXPR)
4654 return op0;
4655 else if (TREE_CODE (exp) == WIDEN_MULT_PLUS_EXPR)
4656 return simplify_gen_binary (PLUS, mode, op0, op2);
4657 else
4658 return simplify_gen_binary (MINUS, mode, op2, op0);
4660 return NULL;
4662 case MULT_HIGHPART_EXPR:
4663 /* ??? Similar to the above. */
4664 return NULL;
4666 case WIDEN_SUM_EXPR:
4667 case WIDEN_LSHIFT_EXPR:
4668 if (SCALAR_INT_MODE_P (GET_MODE (op0))
4669 && SCALAR_INT_MODE_P (mode))
4672 = simplify_gen_unary (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp,
4673 0)))
4674 ? ZERO_EXTEND : SIGN_EXTEND, mode, op0,
4675 inner_mode);
4676 return simplify_gen_binary (TREE_CODE (exp) == WIDEN_LSHIFT_EXPR
4677 ? ASHIFT : PLUS, mode, op0, op1);
4679 return NULL;
4681 case FMA_EXPR:
4682 return simplify_gen_ternary (FMA, mode, inner_mode, op0, op1, op2);
4684 default:
4685 flag_unsupported:
4686 #ifdef ENABLE_CHECKING
4687 debug_tree (exp);
4688 gcc_unreachable ();
4689 #else
4690 return NULL;
4691 #endif
4695 /* Return an RTX equivalent to the source bind value of the tree expression
4696 EXP. */
4698 static rtx
4699 expand_debug_source_expr (tree exp)
4701 rtx op0 = NULL_RTX;
4702 enum machine_mode mode = VOIDmode, inner_mode;
4704 switch (TREE_CODE (exp))
4706 case PARM_DECL:
4708 mode = DECL_MODE (exp);
4709 op0 = expand_debug_parm_decl (exp);
4710 if (op0)
4711 break;
4712 /* See if this isn't an argument that has been completely
4713 optimized out. */
4714 if (!DECL_RTL_SET_P (exp)
4715 && !DECL_INCOMING_RTL (exp)
4716 && DECL_ABSTRACT_ORIGIN (current_function_decl))
4718 tree aexp = DECL_ORIGIN (exp);
4719 if (DECL_CONTEXT (aexp)
4720 == DECL_ABSTRACT_ORIGIN (current_function_decl))
4722 vec<tree, va_gc> **debug_args;
4723 unsigned int ix;
4724 tree ddecl;
4725 debug_args = decl_debug_args_lookup (current_function_decl);
4726 if (debug_args != NULL)
4728 for (ix = 0; vec_safe_iterate (*debug_args, ix, &ddecl);
4729 ix += 2)
4730 if (ddecl == aexp)
4731 return gen_rtx_DEBUG_PARAMETER_REF (mode, aexp);
4735 break;
4737 default:
4738 break;
4741 if (op0 == NULL_RTX)
4742 return NULL_RTX;
4744 inner_mode = GET_MODE (op0);
4745 if (mode == inner_mode)
4746 return op0;
4748 if (FLOAT_MODE_P (mode) && FLOAT_MODE_P (inner_mode))
4750 if (GET_MODE_BITSIZE (mode) == GET_MODE_BITSIZE (inner_mode))
4751 op0 = simplify_gen_subreg (mode, op0, inner_mode, 0);
4752 else if (GET_MODE_BITSIZE (mode) < GET_MODE_BITSIZE (inner_mode))
4753 op0 = simplify_gen_unary (FLOAT_TRUNCATE, mode, op0, inner_mode);
4754 else
4755 op0 = simplify_gen_unary (FLOAT_EXTEND, mode, op0, inner_mode);
4757 else if (FLOAT_MODE_P (mode))
4758 gcc_unreachable ();
4759 else if (FLOAT_MODE_P (inner_mode))
4761 if (TYPE_UNSIGNED (TREE_TYPE (exp)))
4762 op0 = simplify_gen_unary (UNSIGNED_FIX, mode, op0, inner_mode);
4763 else
4764 op0 = simplify_gen_unary (FIX, mode, op0, inner_mode);
4766 else if (CONSTANT_P (op0)
4767 || GET_MODE_BITSIZE (mode) <= GET_MODE_BITSIZE (inner_mode))
4768 op0 = simplify_gen_subreg (mode, op0, inner_mode,
4769 subreg_lowpart_offset (mode, inner_mode));
4770 else if (TYPE_UNSIGNED (TREE_TYPE (exp)))
4771 op0 = simplify_gen_unary (ZERO_EXTEND, mode, op0, inner_mode);
4772 else
4773 op0 = simplify_gen_unary (SIGN_EXTEND, mode, op0, inner_mode);
4775 return op0;
4778 /* Ensure INSN_VAR_LOCATION_LOC (insn) doesn't have unbound complexity.
4779 Allow 4 levels of rtl nesting for most rtl codes, and if we see anything
4780 deeper than that, create DEBUG_EXPRs and emit DEBUG_INSNs before INSN. */
4782 static void
4783 avoid_complex_debug_insns (rtx_insn *insn, rtx *exp_p, int depth)
4785 rtx exp = *exp_p;
4787 if (exp == NULL_RTX)
4788 return;
4790 if ((OBJECT_P (exp) && !MEM_P (exp)) || GET_CODE (exp) == CLOBBER)
4791 return;
4793 if (depth == 4)
4795 /* Create DEBUG_EXPR (and DEBUG_EXPR_DECL). */
4796 rtx dval = make_debug_expr_from_rtl (exp);
4798 /* Emit a debug bind insn before INSN. */
4799 rtx bind = gen_rtx_VAR_LOCATION (GET_MODE (exp),
4800 DEBUG_EXPR_TREE_DECL (dval), exp,
4801 VAR_INIT_STATUS_INITIALIZED);
4803 emit_debug_insn_before (bind, insn);
4804 *exp_p = dval;
4805 return;
4808 const char *format_ptr = GET_RTX_FORMAT (GET_CODE (exp));
4809 int i, j;
4810 for (i = 0; i < GET_RTX_LENGTH (GET_CODE (exp)); i++)
4811 switch (*format_ptr++)
4813 case 'e':
4814 avoid_complex_debug_insns (insn, &XEXP (exp, i), depth + 1);
4815 break;
4817 case 'E':
4818 case 'V':
4819 for (j = 0; j < XVECLEN (exp, i); j++)
4820 avoid_complex_debug_insns (insn, &XVECEXP (exp, i, j), depth + 1);
4821 break;
4823 default:
4824 break;
4828 /* Expand the _LOCs in debug insns. We run this after expanding all
4829 regular insns, so that any variables referenced in the function
4830 will have their DECL_RTLs set. */
4832 static void
4833 expand_debug_locations (void)
4835 rtx_insn *insn;
4836 rtx_insn *last = get_last_insn ();
4837 int save_strict_alias = flag_strict_aliasing;
4839 /* New alias sets while setting up memory attributes cause
4840 -fcompare-debug failures, even though it doesn't bring about any
4841 codegen changes. */
4842 flag_strict_aliasing = 0;
4844 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
4845 if (DEBUG_INSN_P (insn))
4847 tree value = (tree)INSN_VAR_LOCATION_LOC (insn);
4848 rtx val;
4849 rtx_insn *prev_insn, *insn2;
4850 enum machine_mode mode;
4852 if (value == NULL_TREE)
4853 val = NULL_RTX;
4854 else
4856 if (INSN_VAR_LOCATION_STATUS (insn)
4857 == VAR_INIT_STATUS_UNINITIALIZED)
4858 val = expand_debug_source_expr (value);
4859 else
4860 val = expand_debug_expr (value);
4861 gcc_assert (last == get_last_insn ());
4864 if (!val)
4865 val = gen_rtx_UNKNOWN_VAR_LOC ();
4866 else
4868 mode = GET_MODE (INSN_VAR_LOCATION (insn));
4870 gcc_assert (mode == GET_MODE (val)
4871 || (GET_MODE (val) == VOIDmode
4872 && (CONST_SCALAR_INT_P (val)
4873 || GET_CODE (val) == CONST_FIXED
4874 || GET_CODE (val) == LABEL_REF)));
4877 INSN_VAR_LOCATION_LOC (insn) = val;
4878 prev_insn = PREV_INSN (insn);
4879 for (insn2 = insn; insn2 != prev_insn; insn2 = PREV_INSN (insn2))
4880 avoid_complex_debug_insns (insn2, &INSN_VAR_LOCATION_LOC (insn2), 0);
4883 flag_strict_aliasing = save_strict_alias;
4886 /* Expand basic block BB from GIMPLE trees to RTL. */
4888 static basic_block
4889 expand_gimple_basic_block (basic_block bb, bool disable_tail_calls)
4891 gimple_stmt_iterator gsi;
4892 gimple_seq stmts;
4893 gimple stmt = NULL;
4894 rtx_note *note;
4895 rtx_insn *last;
4896 edge e;
4897 edge_iterator ei;
4899 if (dump_file)
4900 fprintf (dump_file, "\n;; Generating RTL for gimple basic block %d\n",
4901 bb->index);
4903 /* Note that since we are now transitioning from GIMPLE to RTL, we
4904 cannot use the gsi_*_bb() routines because they expect the basic
4905 block to be in GIMPLE, instead of RTL. Therefore, we need to
4906 access the BB sequence directly. */
4907 stmts = bb_seq (bb);
4908 bb->il.gimple.seq = NULL;
4909 bb->il.gimple.phi_nodes = NULL;
4910 rtl_profile_for_bb (bb);
4911 init_rtl_bb_info (bb);
4912 bb->flags |= BB_RTL;
4914 /* Remove the RETURN_EXPR if we may fall though to the exit
4915 instead. */
4916 gsi = gsi_last (stmts);
4917 if (!gsi_end_p (gsi)
4918 && gimple_code (gsi_stmt (gsi)) == GIMPLE_RETURN)
4920 gimple_return ret_stmt = as_a <gimple_return> (gsi_stmt (gsi));
4922 gcc_assert (single_succ_p (bb));
4923 gcc_assert (single_succ (bb) == EXIT_BLOCK_PTR_FOR_FN (cfun));
4925 if (bb->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
4926 && !gimple_return_retval (ret_stmt))
4928 gsi_remove (&gsi, false);
4929 single_succ_edge (bb)->flags |= EDGE_FALLTHRU;
4933 gsi = gsi_start (stmts);
4934 if (!gsi_end_p (gsi))
4936 stmt = gsi_stmt (gsi);
4937 if (gimple_code (stmt) != GIMPLE_LABEL)
4938 stmt = NULL;
4941 rtx_code_label **elt = lab_rtx_for_bb->get (bb);
4943 if (stmt || elt)
4945 last = get_last_insn ();
4947 if (stmt)
4949 expand_gimple_stmt (stmt);
4950 gsi_next (&gsi);
4953 if (elt)
4954 emit_label (*elt);
4956 /* Java emits line number notes in the top of labels.
4957 ??? Make this go away once line number notes are obsoleted. */
4958 BB_HEAD (bb) = NEXT_INSN (last);
4959 if (NOTE_P (BB_HEAD (bb)))
4960 BB_HEAD (bb) = NEXT_INSN (BB_HEAD (bb));
4961 note = emit_note_after (NOTE_INSN_BASIC_BLOCK, BB_HEAD (bb));
4963 maybe_dump_rtl_for_gimple_stmt (stmt, last);
4965 else
4966 BB_HEAD (bb) = note = emit_note (NOTE_INSN_BASIC_BLOCK);
4968 NOTE_BASIC_BLOCK (note) = bb;
4970 for (; !gsi_end_p (gsi); gsi_next (&gsi))
4972 basic_block new_bb;
4974 stmt = gsi_stmt (gsi);
4976 /* If this statement is a non-debug one, and we generate debug
4977 insns, then this one might be the last real use of a TERed
4978 SSA_NAME, but where there are still some debug uses further
4979 down. Expanding the current SSA name in such further debug
4980 uses by their RHS might lead to wrong debug info, as coalescing
4981 might make the operands of such RHS be placed into the same
4982 pseudo as something else. Like so:
4983 a_1 = a_0 + 1; // Assume a_1 is TERed and a_0 is dead
4984 use(a_1);
4985 a_2 = ...
4986 #DEBUG ... => a_1
4987 As a_0 and a_2 don't overlap in lifetime, assume they are coalesced.
4988 If we now would expand a_1 by it's RHS (a_0 + 1) in the debug use,
4989 the write to a_2 would actually have clobbered the place which
4990 formerly held a_0.
4992 So, instead of that, we recognize the situation, and generate
4993 debug temporaries at the last real use of TERed SSA names:
4994 a_1 = a_0 + 1;
4995 #DEBUG #D1 => a_1
4996 use(a_1);
4997 a_2 = ...
4998 #DEBUG ... => #D1
5000 if (MAY_HAVE_DEBUG_INSNS
5001 && SA.values
5002 && !is_gimple_debug (stmt))
5004 ssa_op_iter iter;
5005 tree op;
5006 gimple def;
5008 location_t sloc = curr_insn_location ();
5010 /* Look for SSA names that have their last use here (TERed
5011 names always have only one real use). */
5012 FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_USE)
5013 if ((def = get_gimple_for_ssa_name (op)))
5015 imm_use_iterator imm_iter;
5016 use_operand_p use_p;
5017 bool have_debug_uses = false;
5019 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, op)
5021 if (gimple_debug_bind_p (USE_STMT (use_p)))
5023 have_debug_uses = true;
5024 break;
5028 if (have_debug_uses)
5030 /* OP is a TERed SSA name, with DEF it's defining
5031 statement, and where OP is used in further debug
5032 instructions. Generate a debug temporary, and
5033 replace all uses of OP in debug insns with that
5034 temporary. */
5035 gimple debugstmt;
5036 tree value = gimple_assign_rhs_to_tree (def);
5037 tree vexpr = make_node (DEBUG_EXPR_DECL);
5038 rtx val;
5039 enum machine_mode mode;
5041 set_curr_insn_location (gimple_location (def));
5043 DECL_ARTIFICIAL (vexpr) = 1;
5044 TREE_TYPE (vexpr) = TREE_TYPE (value);
5045 if (DECL_P (value))
5046 mode = DECL_MODE (value);
5047 else
5048 mode = TYPE_MODE (TREE_TYPE (value));
5049 DECL_MODE (vexpr) = mode;
5051 val = gen_rtx_VAR_LOCATION
5052 (mode, vexpr, (rtx)value, VAR_INIT_STATUS_INITIALIZED);
5054 emit_debug_insn (val);
5056 FOR_EACH_IMM_USE_STMT (debugstmt, imm_iter, op)
5058 if (!gimple_debug_bind_p (debugstmt))
5059 continue;
5061 FOR_EACH_IMM_USE_ON_STMT (use_p, imm_iter)
5062 SET_USE (use_p, vexpr);
5064 update_stmt (debugstmt);
5068 set_curr_insn_location (sloc);
5071 currently_expanding_gimple_stmt = stmt;
5073 /* Expand this statement, then evaluate the resulting RTL and
5074 fixup the CFG accordingly. */
5075 if (gimple_code (stmt) == GIMPLE_COND)
5077 new_bb = expand_gimple_cond (bb, as_a <gimple_cond> (stmt));
5078 if (new_bb)
5079 return new_bb;
5081 else if (gimple_debug_bind_p (stmt))
5083 location_t sloc = curr_insn_location ();
5084 gimple_stmt_iterator nsi = gsi;
5086 for (;;)
5088 tree var = gimple_debug_bind_get_var (stmt);
5089 tree value;
5090 rtx val;
5091 enum machine_mode mode;
5093 if (TREE_CODE (var) != DEBUG_EXPR_DECL
5094 && TREE_CODE (var) != LABEL_DECL
5095 && !target_for_debug_bind (var))
5096 goto delink_debug_stmt;
5098 if (gimple_debug_bind_has_value_p (stmt))
5099 value = gimple_debug_bind_get_value (stmt);
5100 else
5101 value = NULL_TREE;
5103 last = get_last_insn ();
5105 set_curr_insn_location (gimple_location (stmt));
5107 if (DECL_P (var))
5108 mode = DECL_MODE (var);
5109 else
5110 mode = TYPE_MODE (TREE_TYPE (var));
5112 val = gen_rtx_VAR_LOCATION
5113 (mode, var, (rtx)value, VAR_INIT_STATUS_INITIALIZED);
5115 emit_debug_insn (val);
5117 if (dump_file && (dump_flags & TDF_DETAILS))
5119 /* We can't dump the insn with a TREE where an RTX
5120 is expected. */
5121 PAT_VAR_LOCATION_LOC (val) = const0_rtx;
5122 maybe_dump_rtl_for_gimple_stmt (stmt, last);
5123 PAT_VAR_LOCATION_LOC (val) = (rtx)value;
5126 delink_debug_stmt:
5127 /* In order not to generate too many debug temporaries,
5128 we delink all uses of debug statements we already expanded.
5129 Therefore debug statements between definition and real
5130 use of TERed SSA names will continue to use the SSA name,
5131 and not be replaced with debug temps. */
5132 delink_stmt_imm_use (stmt);
5134 gsi = nsi;
5135 gsi_next (&nsi);
5136 if (gsi_end_p (nsi))
5137 break;
5138 stmt = gsi_stmt (nsi);
5139 if (!gimple_debug_bind_p (stmt))
5140 break;
5143 set_curr_insn_location (sloc);
5145 else if (gimple_debug_source_bind_p (stmt))
5147 location_t sloc = curr_insn_location ();
5148 tree var = gimple_debug_source_bind_get_var (stmt);
5149 tree value = gimple_debug_source_bind_get_value (stmt);
5150 rtx val;
5151 enum machine_mode mode;
5153 last = get_last_insn ();
5155 set_curr_insn_location (gimple_location (stmt));
5157 mode = DECL_MODE (var);
5159 val = gen_rtx_VAR_LOCATION (mode, var, (rtx)value,
5160 VAR_INIT_STATUS_UNINITIALIZED);
5162 emit_debug_insn (val);
5164 if (dump_file && (dump_flags & TDF_DETAILS))
5166 /* We can't dump the insn with a TREE where an RTX
5167 is expected. */
5168 PAT_VAR_LOCATION_LOC (val) = const0_rtx;
5169 maybe_dump_rtl_for_gimple_stmt (stmt, last);
5170 PAT_VAR_LOCATION_LOC (val) = (rtx)value;
5173 set_curr_insn_location (sloc);
5175 else
5177 gimple_call call_stmt = dyn_cast <gimple_call> (stmt);
5178 if (call_stmt
5179 && gimple_call_tail_p (call_stmt)
5180 && disable_tail_calls)
5181 gimple_call_set_tail (call_stmt, false);
5183 if (call_stmt && gimple_call_tail_p (call_stmt))
5185 bool can_fallthru;
5186 new_bb = expand_gimple_tailcall (bb, call_stmt, &can_fallthru);
5187 if (new_bb)
5189 if (can_fallthru)
5190 bb = new_bb;
5191 else
5192 return new_bb;
5195 else
5197 def_operand_p def_p;
5198 def_p = SINGLE_SSA_DEF_OPERAND (stmt, SSA_OP_DEF);
5200 if (def_p != NULL)
5202 /* Ignore this stmt if it is in the list of
5203 replaceable expressions. */
5204 if (SA.values
5205 && bitmap_bit_p (SA.values,
5206 SSA_NAME_VERSION (DEF_FROM_PTR (def_p))))
5207 continue;
5209 last = expand_gimple_stmt (stmt);
5210 maybe_dump_rtl_for_gimple_stmt (stmt, last);
5215 currently_expanding_gimple_stmt = NULL;
5217 /* Expand implicit goto and convert goto_locus. */
5218 FOR_EACH_EDGE (e, ei, bb->succs)
5220 if (e->goto_locus != UNKNOWN_LOCATION)
5221 set_curr_insn_location (e->goto_locus);
5222 if ((e->flags & EDGE_FALLTHRU) && e->dest != bb->next_bb)
5224 emit_jump (label_rtx_for_bb (e->dest));
5225 e->flags &= ~EDGE_FALLTHRU;
5229 /* Expanded RTL can create a jump in the last instruction of block.
5230 This later might be assumed to be a jump to successor and break edge insertion.
5231 We need to insert dummy move to prevent this. PR41440. */
5232 if (single_succ_p (bb)
5233 && (single_succ_edge (bb)->flags & EDGE_FALLTHRU)
5234 && (last = get_last_insn ())
5235 && JUMP_P (last))
5237 rtx dummy = gen_reg_rtx (SImode);
5238 emit_insn_after_noloc (gen_move_insn (dummy, dummy), last, NULL);
5241 do_pending_stack_adjust ();
5243 /* Find the block tail. The last insn in the block is the insn
5244 before a barrier and/or table jump insn. */
5245 last = get_last_insn ();
5246 if (BARRIER_P (last))
5247 last = PREV_INSN (last);
5248 if (JUMP_TABLE_DATA_P (last))
5249 last = PREV_INSN (PREV_INSN (last));
5250 BB_END (bb) = last;
5252 update_bb_for_insn (bb);
5254 return bb;
5258 /* Create a basic block for initialization code. */
5260 static basic_block
5261 construct_init_block (void)
5263 basic_block init_block, first_block;
5264 edge e = NULL;
5265 int flags;
5267 /* Multiple entry points not supported yet. */
5268 gcc_assert (EDGE_COUNT (ENTRY_BLOCK_PTR_FOR_FN (cfun)->succs) == 1);
5269 init_rtl_bb_info (ENTRY_BLOCK_PTR_FOR_FN (cfun));
5270 init_rtl_bb_info (EXIT_BLOCK_PTR_FOR_FN (cfun));
5271 ENTRY_BLOCK_PTR_FOR_FN (cfun)->flags |= BB_RTL;
5272 EXIT_BLOCK_PTR_FOR_FN (cfun)->flags |= BB_RTL;
5274 e = EDGE_SUCC (ENTRY_BLOCK_PTR_FOR_FN (cfun), 0);
5276 /* When entry edge points to first basic block, we don't need jump,
5277 otherwise we have to jump into proper target. */
5278 if (e && e->dest != ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb)
5280 tree label = gimple_block_label (e->dest);
5282 emit_jump (label_rtx (label));
5283 flags = 0;
5285 else
5286 flags = EDGE_FALLTHRU;
5288 init_block = create_basic_block (NEXT_INSN (get_insns ()),
5289 get_last_insn (),
5290 ENTRY_BLOCK_PTR_FOR_FN (cfun));
5291 init_block->frequency = ENTRY_BLOCK_PTR_FOR_FN (cfun)->frequency;
5292 init_block->count = ENTRY_BLOCK_PTR_FOR_FN (cfun)->count;
5293 add_bb_to_loop (init_block, ENTRY_BLOCK_PTR_FOR_FN (cfun)->loop_father);
5294 if (e)
5296 first_block = e->dest;
5297 redirect_edge_succ (e, init_block);
5298 e = make_edge (init_block, first_block, flags);
5300 else
5301 e = make_edge (init_block, EXIT_BLOCK_PTR_FOR_FN (cfun), EDGE_FALLTHRU);
5302 e->probability = REG_BR_PROB_BASE;
5303 e->count = ENTRY_BLOCK_PTR_FOR_FN (cfun)->count;
5305 update_bb_for_insn (init_block);
5306 return init_block;
5309 /* For each lexical block, set BLOCK_NUMBER to the depth at which it is
5310 found in the block tree. */
5312 static void
5313 set_block_levels (tree block, int level)
5315 while (block)
5317 BLOCK_NUMBER (block) = level;
5318 set_block_levels (BLOCK_SUBBLOCKS (block), level + 1);
5319 block = BLOCK_CHAIN (block);
5323 /* Create a block containing landing pads and similar stuff. */
5325 static void
5326 construct_exit_block (void)
5328 rtx_insn *head = get_last_insn ();
5329 rtx_insn *end;
5330 basic_block exit_block;
5331 edge e, e2;
5332 unsigned ix;
5333 edge_iterator ei;
5334 basic_block prev_bb = EXIT_BLOCK_PTR_FOR_FN (cfun)->prev_bb;
5335 rtx_insn *orig_end = BB_END (prev_bb);
5337 rtl_profile_for_bb (EXIT_BLOCK_PTR_FOR_FN (cfun));
5339 /* Make sure the locus is set to the end of the function, so that
5340 epilogue line numbers and warnings are set properly. */
5341 if (LOCATION_LOCUS (cfun->function_end_locus) != UNKNOWN_LOCATION)
5342 input_location = cfun->function_end_locus;
5344 /* Generate rtl for function exit. */
5345 expand_function_end ();
5347 end = get_last_insn ();
5348 if (head == end)
5349 return;
5350 /* While emitting the function end we could move end of the last basic
5351 block. */
5352 BB_END (prev_bb) = orig_end;
5353 while (NEXT_INSN (head) && NOTE_P (NEXT_INSN (head)))
5354 head = NEXT_INSN (head);
5355 /* But make sure exit_block starts with RETURN_LABEL, otherwise the
5356 bb frequency counting will be confused. Any instructions before that
5357 label are emitted for the case where PREV_BB falls through into the
5358 exit block, so append those instructions to prev_bb in that case. */
5359 if (NEXT_INSN (head) != return_label)
5361 while (NEXT_INSN (head) != return_label)
5363 if (!NOTE_P (NEXT_INSN (head)))
5364 BB_END (prev_bb) = NEXT_INSN (head);
5365 head = NEXT_INSN (head);
5368 exit_block = create_basic_block (NEXT_INSN (head), end, prev_bb);
5369 exit_block->frequency = EXIT_BLOCK_PTR_FOR_FN (cfun)->frequency;
5370 exit_block->count = EXIT_BLOCK_PTR_FOR_FN (cfun)->count;
5371 add_bb_to_loop (exit_block, EXIT_BLOCK_PTR_FOR_FN (cfun)->loop_father);
5373 ix = 0;
5374 while (ix < EDGE_COUNT (EXIT_BLOCK_PTR_FOR_FN (cfun)->preds))
5376 e = EDGE_PRED (EXIT_BLOCK_PTR_FOR_FN (cfun), ix);
5377 if (!(e->flags & EDGE_ABNORMAL))
5378 redirect_edge_succ (e, exit_block);
5379 else
5380 ix++;
5383 e = make_edge (exit_block, EXIT_BLOCK_PTR_FOR_FN (cfun), EDGE_FALLTHRU);
5384 e->probability = REG_BR_PROB_BASE;
5385 e->count = EXIT_BLOCK_PTR_FOR_FN (cfun)->count;
5386 FOR_EACH_EDGE (e2, ei, EXIT_BLOCK_PTR_FOR_FN (cfun)->preds)
5387 if (e2 != e)
5389 e->count -= e2->count;
5390 exit_block->count -= e2->count;
5391 exit_block->frequency -= EDGE_FREQUENCY (e2);
5393 if (e->count < 0)
5394 e->count = 0;
5395 if (exit_block->count < 0)
5396 exit_block->count = 0;
5397 if (exit_block->frequency < 0)
5398 exit_block->frequency = 0;
5399 update_bb_for_insn (exit_block);
5402 /* Helper function for discover_nonconstant_array_refs.
5403 Look for ARRAY_REF nodes with non-constant indexes and mark them
5404 addressable. */
5406 static tree
5407 discover_nonconstant_array_refs_r (tree * tp, int *walk_subtrees,
5408 void *data ATTRIBUTE_UNUSED)
5410 tree t = *tp;
5412 if (IS_TYPE_OR_DECL_P (t))
5413 *walk_subtrees = 0;
5414 else if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
5416 while (((TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
5417 && is_gimple_min_invariant (TREE_OPERAND (t, 1))
5418 && (!TREE_OPERAND (t, 2)
5419 || is_gimple_min_invariant (TREE_OPERAND (t, 2))))
5420 || (TREE_CODE (t) == COMPONENT_REF
5421 && (!TREE_OPERAND (t,2)
5422 || is_gimple_min_invariant (TREE_OPERAND (t, 2))))
5423 || TREE_CODE (t) == BIT_FIELD_REF
5424 || TREE_CODE (t) == REALPART_EXPR
5425 || TREE_CODE (t) == IMAGPART_EXPR
5426 || TREE_CODE (t) == VIEW_CONVERT_EXPR
5427 || CONVERT_EXPR_P (t))
5428 t = TREE_OPERAND (t, 0);
5430 if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
5432 t = get_base_address (t);
5433 if (t && DECL_P (t)
5434 && DECL_MODE (t) != BLKmode)
5435 TREE_ADDRESSABLE (t) = 1;
5438 *walk_subtrees = 0;
5441 return NULL_TREE;
5444 /* RTL expansion is not able to compile array references with variable
5445 offsets for arrays stored in single register. Discover such
5446 expressions and mark variables as addressable to avoid this
5447 scenario. */
5449 static void
5450 discover_nonconstant_array_refs (void)
5452 basic_block bb;
5453 gimple_stmt_iterator gsi;
5455 FOR_EACH_BB_FN (bb, cfun)
5456 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
5458 gimple stmt = gsi_stmt (gsi);
5459 if (!is_gimple_debug (stmt))
5460 walk_gimple_op (stmt, discover_nonconstant_array_refs_r, NULL);
5464 /* This function sets crtl->args.internal_arg_pointer to a virtual
5465 register if DRAP is needed. Local register allocator will replace
5466 virtual_incoming_args_rtx with the virtual register. */
5468 static void
5469 expand_stack_alignment (void)
5471 rtx drap_rtx;
5472 unsigned int preferred_stack_boundary;
5474 if (! SUPPORTS_STACK_ALIGNMENT)
5475 return;
5477 if (cfun->calls_alloca
5478 || cfun->has_nonlocal_label
5479 || crtl->has_nonlocal_goto)
5480 crtl->need_drap = true;
5482 /* Call update_stack_boundary here again to update incoming stack
5483 boundary. It may set incoming stack alignment to a different
5484 value after RTL expansion. TARGET_FUNCTION_OK_FOR_SIBCALL may
5485 use the minimum incoming stack alignment to check if it is OK
5486 to perform sibcall optimization since sibcall optimization will
5487 only align the outgoing stack to incoming stack boundary. */
5488 if (targetm.calls.update_stack_boundary)
5489 targetm.calls.update_stack_boundary ();
5491 /* The incoming stack frame has to be aligned at least at
5492 parm_stack_boundary. */
5493 gcc_assert (crtl->parm_stack_boundary <= INCOMING_STACK_BOUNDARY);
5495 /* Update crtl->stack_alignment_estimated and use it later to align
5496 stack. We check PREFERRED_STACK_BOUNDARY if there may be non-call
5497 exceptions since callgraph doesn't collect incoming stack alignment
5498 in this case. */
5499 if (cfun->can_throw_non_call_exceptions
5500 && PREFERRED_STACK_BOUNDARY > crtl->preferred_stack_boundary)
5501 preferred_stack_boundary = PREFERRED_STACK_BOUNDARY;
5502 else
5503 preferred_stack_boundary = crtl->preferred_stack_boundary;
5504 if (preferred_stack_boundary > crtl->stack_alignment_estimated)
5505 crtl->stack_alignment_estimated = preferred_stack_boundary;
5506 if (preferred_stack_boundary > crtl->stack_alignment_needed)
5507 crtl->stack_alignment_needed = preferred_stack_boundary;
5509 gcc_assert (crtl->stack_alignment_needed
5510 <= crtl->stack_alignment_estimated);
5512 crtl->stack_realign_needed
5513 = INCOMING_STACK_BOUNDARY < crtl->stack_alignment_estimated;
5514 crtl->stack_realign_tried = crtl->stack_realign_needed;
5516 crtl->stack_realign_processed = true;
5518 /* Target has to redefine TARGET_GET_DRAP_RTX to support stack
5519 alignment. */
5520 gcc_assert (targetm.calls.get_drap_rtx != NULL);
5521 drap_rtx = targetm.calls.get_drap_rtx ();
5523 /* stack_realign_drap and drap_rtx must match. */
5524 gcc_assert ((stack_realign_drap != 0) == (drap_rtx != NULL));
5526 /* Do nothing if NULL is returned, which means DRAP is not needed. */
5527 if (NULL != drap_rtx)
5529 crtl->args.internal_arg_pointer = drap_rtx;
5531 /* Call fixup_tail_calls to clean up REG_EQUIV note if DRAP is
5532 needed. */
5533 fixup_tail_calls ();
5538 static void
5539 expand_main_function (void)
5541 #if (defined(INVOKE__main) \
5542 || (!defined(HAS_INIT_SECTION) \
5543 && !defined(INIT_SECTION_ASM_OP) \
5544 && !defined(INIT_ARRAY_SECTION_ASM_OP)))
5545 emit_library_call (init_one_libfunc (NAME__MAIN), LCT_NORMAL, VOIDmode, 0);
5546 #endif
5550 /* Expand code to initialize the stack_protect_guard. This is invoked at
5551 the beginning of a function to be protected. */
5553 #ifndef HAVE_stack_protect_set
5554 # define HAVE_stack_protect_set 0
5555 # define gen_stack_protect_set(x,y) (gcc_unreachable (), NULL_RTX)
5556 #endif
5558 static void
5559 stack_protect_prologue (void)
5561 tree guard_decl = targetm.stack_protect_guard ();
5562 rtx x, y;
5564 x = expand_normal (crtl->stack_protect_guard);
5565 y = expand_normal (guard_decl);
5567 /* Allow the target to copy from Y to X without leaking Y into a
5568 register. */
5569 if (HAVE_stack_protect_set)
5571 rtx insn = gen_stack_protect_set (x, y);
5572 if (insn)
5574 emit_insn (insn);
5575 return;
5579 /* Otherwise do a straight move. */
5580 emit_move_insn (x, y);
5583 /* Translate the intermediate representation contained in the CFG
5584 from GIMPLE trees to RTL.
5586 We do conversion per basic block and preserve/update the tree CFG.
5587 This implies we have to do some magic as the CFG can simultaneously
5588 consist of basic blocks containing RTL and GIMPLE trees. This can
5589 confuse the CFG hooks, so be careful to not manipulate CFG during
5590 the expansion. */
5592 namespace {
5594 const pass_data pass_data_expand =
5596 RTL_PASS, /* type */
5597 "expand", /* name */
5598 OPTGROUP_NONE, /* optinfo_flags */
5599 TV_EXPAND, /* tv_id */
5600 ( PROP_ssa | PROP_gimple_leh | PROP_cfg
5601 | PROP_gimple_lcx
5602 | PROP_gimple_lvec ), /* properties_required */
5603 PROP_rtl, /* properties_provided */
5604 ( PROP_ssa | PROP_trees ), /* properties_destroyed */
5605 0, /* todo_flags_start */
5606 0, /* todo_flags_finish */
5609 class pass_expand : public rtl_opt_pass
5611 public:
5612 pass_expand (gcc::context *ctxt)
5613 : rtl_opt_pass (pass_data_expand, ctxt)
5616 /* opt_pass methods: */
5617 virtual unsigned int execute (function *);
5619 }; // class pass_expand
5621 unsigned int
5622 pass_expand::execute (function *fun)
5624 basic_block bb, init_block;
5625 sbitmap blocks;
5626 edge_iterator ei;
5627 edge e;
5628 rtx_insn *var_seq, *var_ret_seq;
5629 unsigned i;
5631 timevar_push (TV_OUT_OF_SSA);
5632 rewrite_out_of_ssa (&SA);
5633 timevar_pop (TV_OUT_OF_SSA);
5634 SA.partition_to_pseudo = XCNEWVEC (rtx, SA.map->num_partitions);
5636 /* Make sure all values used by the optimization passes have sane
5637 defaults. */
5638 reg_renumber = 0;
5640 /* Some backends want to know that we are expanding to RTL. */
5641 currently_expanding_to_rtl = 1;
5642 /* Dominators are not kept up-to-date as we may create new basic-blocks. */
5643 free_dominance_info (CDI_DOMINATORS);
5645 rtl_profile_for_bb (ENTRY_BLOCK_PTR_FOR_FN (fun));
5647 insn_locations_init ();
5648 if (!DECL_IS_BUILTIN (current_function_decl))
5650 /* Eventually, all FEs should explicitly set function_start_locus. */
5651 if (LOCATION_LOCUS (fun->function_start_locus) == UNKNOWN_LOCATION)
5652 set_curr_insn_location
5653 (DECL_SOURCE_LOCATION (current_function_decl));
5654 else
5655 set_curr_insn_location (fun->function_start_locus);
5657 else
5658 set_curr_insn_location (UNKNOWN_LOCATION);
5659 prologue_location = curr_insn_location ();
5661 #ifdef INSN_SCHEDULING
5662 init_sched_attrs ();
5663 #endif
5665 /* Make sure first insn is a note even if we don't want linenums.
5666 This makes sure the first insn will never be deleted.
5667 Also, final expects a note to appear there. */
5668 emit_note (NOTE_INSN_DELETED);
5670 /* Mark arrays indexed with non-constant indices with TREE_ADDRESSABLE. */
5671 discover_nonconstant_array_refs ();
5673 targetm.expand_to_rtl_hook ();
5674 crtl->stack_alignment_needed = STACK_BOUNDARY;
5675 crtl->max_used_stack_slot_alignment = STACK_BOUNDARY;
5676 crtl->stack_alignment_estimated = 0;
5677 crtl->preferred_stack_boundary = STACK_BOUNDARY;
5678 fun->cfg->max_jumptable_ents = 0;
5680 /* Resovle the function section. Some targets, like ARM EABI rely on knowledge
5681 of the function section at exapnsion time to predict distance of calls. */
5682 resolve_unique_section (current_function_decl, 0, flag_function_sections);
5684 /* Expand the variables recorded during gimple lowering. */
5685 timevar_push (TV_VAR_EXPAND);
5686 start_sequence ();
5688 var_ret_seq = expand_used_vars ();
5690 var_seq = get_insns ();
5691 end_sequence ();
5692 timevar_pop (TV_VAR_EXPAND);
5694 /* Honor stack protection warnings. */
5695 if (warn_stack_protect)
5697 if (fun->calls_alloca)
5698 warning (OPT_Wstack_protector,
5699 "stack protector not protecting local variables: "
5700 "variable length buffer");
5701 if (has_short_buffer && !crtl->stack_protect_guard)
5702 warning (OPT_Wstack_protector,
5703 "stack protector not protecting function: "
5704 "all local arrays are less than %d bytes long",
5705 (int) PARAM_VALUE (PARAM_SSP_BUFFER_SIZE));
5708 /* Set up parameters and prepare for return, for the function. */
5709 expand_function_start (current_function_decl);
5711 /* If we emitted any instructions for setting up the variables,
5712 emit them before the FUNCTION_START note. */
5713 if (var_seq)
5715 emit_insn_before (var_seq, parm_birth_insn);
5717 /* In expand_function_end we'll insert the alloca save/restore
5718 before parm_birth_insn. We've just insertted an alloca call.
5719 Adjust the pointer to match. */
5720 parm_birth_insn = var_seq;
5723 /* Now that we also have the parameter RTXs, copy them over to our
5724 partitions. */
5725 for (i = 0; i < SA.map->num_partitions; i++)
5727 tree var = SSA_NAME_VAR (partition_to_var (SA.map, i));
5729 if (TREE_CODE (var) != VAR_DECL
5730 && !SA.partition_to_pseudo[i])
5731 SA.partition_to_pseudo[i] = DECL_RTL_IF_SET (var);
5732 gcc_assert (SA.partition_to_pseudo[i]);
5734 /* If this decl was marked as living in multiple places, reset
5735 this now to NULL. */
5736 if (DECL_RTL_IF_SET (var) == pc_rtx)
5737 SET_DECL_RTL (var, NULL);
5739 /* Some RTL parts really want to look at DECL_RTL(x) when x
5740 was a decl marked in REG_ATTR or MEM_ATTR. We could use
5741 SET_DECL_RTL here making this available, but that would mean
5742 to select one of the potentially many RTLs for one DECL. Instead
5743 of doing that we simply reset the MEM_EXPR of the RTL in question,
5744 then nobody can get at it and hence nobody can call DECL_RTL on it. */
5745 if (!DECL_RTL_SET_P (var))
5747 if (MEM_P (SA.partition_to_pseudo[i]))
5748 set_mem_expr (SA.partition_to_pseudo[i], NULL);
5752 /* If we have a class containing differently aligned pointers
5753 we need to merge those into the corresponding RTL pointer
5754 alignment. */
5755 for (i = 1; i < num_ssa_names; i++)
5757 tree name = ssa_name (i);
5758 int part;
5759 rtx r;
5761 if (!name
5762 /* We might have generated new SSA names in
5763 update_alias_info_with_stack_vars. They will have a NULL
5764 defining statements, and won't be part of the partitioning,
5765 so ignore those. */
5766 || !SSA_NAME_DEF_STMT (name))
5767 continue;
5768 part = var_to_partition (SA.map, name);
5769 if (part == NO_PARTITION)
5770 continue;
5772 /* Adjust all partition members to get the underlying decl of
5773 the representative which we might have created in expand_one_var. */
5774 if (SSA_NAME_VAR (name) == NULL_TREE)
5776 tree leader = partition_to_var (SA.map, part);
5777 gcc_assert (SSA_NAME_VAR (leader) != NULL_TREE);
5778 replace_ssa_name_symbol (name, SSA_NAME_VAR (leader));
5780 if (!POINTER_TYPE_P (TREE_TYPE (name)))
5781 continue;
5783 r = SA.partition_to_pseudo[part];
5784 if (REG_P (r))
5785 mark_reg_pointer (r, get_pointer_alignment (name));
5788 /* If this function is `main', emit a call to `__main'
5789 to run global initializers, etc. */
5790 if (DECL_NAME (current_function_decl)
5791 && MAIN_NAME_P (DECL_NAME (current_function_decl))
5792 && DECL_FILE_SCOPE_P (current_function_decl))
5793 expand_main_function ();
5795 /* Initialize the stack_protect_guard field. This must happen after the
5796 call to __main (if any) so that the external decl is initialized. */
5797 if (crtl->stack_protect_guard)
5798 stack_protect_prologue ();
5800 expand_phi_nodes (&SA);
5802 /* Register rtl specific functions for cfg. */
5803 rtl_register_cfg_hooks ();
5805 init_block = construct_init_block ();
5807 /* Clear EDGE_EXECUTABLE on the entry edge(s). It is cleaned from the
5808 remaining edges later. */
5809 FOR_EACH_EDGE (e, ei, ENTRY_BLOCK_PTR_FOR_FN (fun)->succs)
5810 e->flags &= ~EDGE_EXECUTABLE;
5812 lab_rtx_for_bb = new hash_map<basic_block, rtx_code_label *>;
5813 FOR_BB_BETWEEN (bb, init_block->next_bb, EXIT_BLOCK_PTR_FOR_FN (fun),
5814 next_bb)
5815 bb = expand_gimple_basic_block (bb, var_ret_seq != NULL_RTX);
5817 if (MAY_HAVE_DEBUG_INSNS)
5818 expand_debug_locations ();
5820 /* Free stuff we no longer need after GIMPLE optimizations. */
5821 free_dominance_info (CDI_DOMINATORS);
5822 free_dominance_info (CDI_POST_DOMINATORS);
5823 delete_tree_cfg_annotations ();
5825 timevar_push (TV_OUT_OF_SSA);
5826 finish_out_of_ssa (&SA);
5827 timevar_pop (TV_OUT_OF_SSA);
5829 timevar_push (TV_POST_EXPAND);
5830 /* We are no longer in SSA form. */
5831 fun->gimple_df->in_ssa_p = false;
5832 loops_state_clear (LOOP_CLOSED_SSA);
5834 /* Expansion is used by optimization passes too, set maybe_hot_insn_p
5835 conservatively to true until they are all profile aware. */
5836 delete lab_rtx_for_bb;
5837 free_histograms ();
5839 construct_exit_block ();
5840 insn_locations_finalize ();
5842 if (var_ret_seq)
5844 rtx_insn *after = return_label;
5845 rtx_insn *next = NEXT_INSN (after);
5846 if (next && NOTE_INSN_BASIC_BLOCK_P (next))
5847 after = next;
5848 emit_insn_after (var_ret_seq, after);
5851 /* Zap the tree EH table. */
5852 set_eh_throw_stmt_table (fun, NULL);
5854 /* We need JUMP_LABEL be set in order to redirect jumps, and hence
5855 split edges which edge insertions might do. */
5856 rebuild_jump_labels (get_insns ());
5858 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (fun),
5859 EXIT_BLOCK_PTR_FOR_FN (fun), next_bb)
5861 edge e;
5862 edge_iterator ei;
5863 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
5865 if (e->insns.r)
5867 rebuild_jump_labels_chain (e->insns.r);
5868 /* Put insns after parm birth, but before
5869 NOTE_INSNS_FUNCTION_BEG. */
5870 if (e->src == ENTRY_BLOCK_PTR_FOR_FN (fun)
5871 && single_succ_p (ENTRY_BLOCK_PTR_FOR_FN (fun)))
5873 rtx_insn *insns = e->insns.r;
5874 e->insns.r = NULL;
5875 if (NOTE_P (parm_birth_insn)
5876 && NOTE_KIND (parm_birth_insn) == NOTE_INSN_FUNCTION_BEG)
5877 emit_insn_before_noloc (insns, parm_birth_insn, e->dest);
5878 else
5879 emit_insn_after_noloc (insns, parm_birth_insn, e->dest);
5881 else
5882 commit_one_edge_insertion (e);
5884 else
5885 ei_next (&ei);
5889 /* We're done expanding trees to RTL. */
5890 currently_expanding_to_rtl = 0;
5892 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (fun)->next_bb,
5893 EXIT_BLOCK_PTR_FOR_FN (fun), next_bb)
5895 edge e;
5896 edge_iterator ei;
5897 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
5899 /* Clear EDGE_EXECUTABLE. This flag is never used in the backend. */
5900 e->flags &= ~EDGE_EXECUTABLE;
5902 /* At the moment not all abnormal edges match the RTL
5903 representation. It is safe to remove them here as
5904 find_many_sub_basic_blocks will rediscover them.
5905 In the future we should get this fixed properly. */
5906 if ((e->flags & EDGE_ABNORMAL)
5907 && !(e->flags & EDGE_SIBCALL))
5908 remove_edge (e);
5909 else
5910 ei_next (&ei);
5914 blocks = sbitmap_alloc (last_basic_block_for_fn (fun));
5915 bitmap_ones (blocks);
5916 find_many_sub_basic_blocks (blocks);
5917 sbitmap_free (blocks);
5918 purge_all_dead_edges ();
5920 expand_stack_alignment ();
5922 /* Fixup REG_EQUIV notes in the prologue if there are tailcalls in this
5923 function. */
5924 if (crtl->tail_call_emit)
5925 fixup_tail_calls ();
5927 /* After initial rtl generation, call back to finish generating
5928 exception support code. We need to do this before cleaning up
5929 the CFG as the code does not expect dead landing pads. */
5930 if (fun->eh->region_tree != NULL)
5931 finish_eh_generation ();
5933 /* Remove unreachable blocks, otherwise we cannot compute dominators
5934 which are needed for loop state verification. As a side-effect
5935 this also compacts blocks.
5936 ??? We cannot remove trivially dead insns here as for example
5937 the DRAP reg on i?86 is not magically live at this point.
5938 gcc.c-torture/execute/ipa-sra-2.c execution, -Os -m32 fails otherwise. */
5939 cleanup_cfg (CLEANUP_NO_INSN_DEL);
5941 #ifdef ENABLE_CHECKING
5942 verify_flow_info ();
5943 #endif
5945 /* Initialize pseudos allocated for hard registers. */
5946 emit_initial_value_sets ();
5948 /* And finally unshare all RTL. */
5949 unshare_all_rtl ();
5951 /* There's no need to defer outputting this function any more; we
5952 know we want to output it. */
5953 DECL_DEFER_OUTPUT (current_function_decl) = 0;
5955 /* Now that we're done expanding trees to RTL, we shouldn't have any
5956 more CONCATs anywhere. */
5957 generating_concat_p = 0;
5959 if (dump_file)
5961 fprintf (dump_file,
5962 "\n\n;;\n;; Full RTL generated for this function:\n;;\n");
5963 /* And the pass manager will dump RTL for us. */
5966 /* If we're emitting a nested function, make sure its parent gets
5967 emitted as well. Doing otherwise confuses debug info. */
5969 tree parent;
5970 for (parent = DECL_CONTEXT (current_function_decl);
5971 parent != NULL_TREE;
5972 parent = get_containing_scope (parent))
5973 if (TREE_CODE (parent) == FUNCTION_DECL)
5974 TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (parent)) = 1;
5977 /* We are now committed to emitting code for this function. Do any
5978 preparation, such as emitting abstract debug info for the inline
5979 before it gets mangled by optimization. */
5980 if (cgraph_function_possibly_inlined_p (current_function_decl))
5981 (*debug_hooks->outlining_inline_function) (current_function_decl);
5983 TREE_ASM_WRITTEN (current_function_decl) = 1;
5985 /* After expanding, the return labels are no longer needed. */
5986 return_label = NULL;
5987 naked_return_label = NULL;
5989 /* After expanding, the tm_restart map is no longer needed. */
5990 if (fun->gimple_df->tm_restart)
5992 htab_delete (fun->gimple_df->tm_restart);
5993 fun->gimple_df->tm_restart = NULL;
5996 /* Tag the blocks with a depth number so that change_scope can find
5997 the common parent easily. */
5998 set_block_levels (DECL_INITIAL (fun->decl), 0);
5999 default_rtl_profile ();
6001 timevar_pop (TV_POST_EXPAND);
6003 return 0;
6006 } // anon namespace
6008 rtl_opt_pass *
6009 make_pass_expand (gcc::context *ctxt)
6011 return new pass_expand (ctxt);