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