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