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