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