]>
Commit | Line | Data |
---|---|---|
242229bb | 1 | /* A pass for lowering trees to RTL. |
2d593c86 | 2 | Copyright (C) 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc. |
242229bb JH |
3 | |
4 | This file is part of GCC. | |
5 | ||
6 | GCC is free software; you can redistribute it and/or modify | |
7 | it under the terms of the GNU General Public License as published by | |
9dcd6f09 | 8 | the Free Software Foundation; either version 3, or (at your option) |
242229bb JH |
9 | any later version. |
10 | ||
11 | GCC is distributed in the hope that it will be useful, | |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | GNU General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU General Public License | |
9dcd6f09 NC |
17 | along with GCC; see the file COPYING3. If not see |
18 | <http://www.gnu.org/licenses/>. */ | |
242229bb JH |
19 | |
20 | #include "config.h" | |
21 | #include "system.h" | |
22 | #include "coretypes.h" | |
23 | #include "tm.h" | |
24 | #include "tree.h" | |
25 | #include "rtl.h" | |
26 | #include "tm_p.h" | |
27 | #include "basic-block.h" | |
28 | #include "function.h" | |
29 | #include "expr.h" | |
30 | #include "langhooks.h" | |
31 | #include "tree-flow.h" | |
32 | #include "timevar.h" | |
33 | #include "tree-dump.h" | |
34 | #include "tree-pass.h" | |
35 | #include "except.h" | |
36 | #include "flags.h" | |
1f6d3a08 RH |
37 | #include "diagnostic.h" |
38 | #include "toplev.h" | |
ef330312 | 39 | #include "debug.h" |
7d69de61 | 40 | #include "params.h" |
ff28a94d | 41 | #include "tree-inline.h" |
6946b3f7 | 42 | #include "value-prof.h" |
e41b2a33 | 43 | #include "target.h" |
7d69de61 | 44 | |
e53de54d JH |
45 | /* Verify that there is exactly single jump instruction since last and attach |
46 | REG_BR_PROB note specifying probability. | |
47 | ??? We really ought to pass the probability down to RTL expanders and let it | |
d7e9e62a KH |
48 | re-distribute it when the conditional expands into multiple conditionals. |
49 | This is however difficult to do. */ | |
ef950eba | 50 | void |
10d22567 | 51 | add_reg_br_prob_note (rtx last, int probability) |
e53de54d JH |
52 | { |
53 | if (profile_status == PROFILE_ABSENT) | |
54 | return; | |
55 | for (last = NEXT_INSN (last); last && NEXT_INSN (last); last = NEXT_INSN (last)) | |
2ca202e7 | 56 | if (JUMP_P (last)) |
e53de54d JH |
57 | { |
58 | /* It is common to emit condjump-around-jump sequence when we don't know | |
59 | how to reverse the conditional. Special case this. */ | |
60 | if (!any_condjump_p (last) | |
2ca202e7 | 61 | || !JUMP_P (NEXT_INSN (last)) |
e53de54d | 62 | || !simplejump_p (NEXT_INSN (last)) |
fa1ff4eb | 63 | || !NEXT_INSN (NEXT_INSN (last)) |
2ca202e7 | 64 | || !BARRIER_P (NEXT_INSN (NEXT_INSN (last))) |
fa1ff4eb | 65 | || !NEXT_INSN (NEXT_INSN (NEXT_INSN (last))) |
2ca202e7 | 66 | || !LABEL_P (NEXT_INSN (NEXT_INSN (NEXT_INSN (last)))) |
e53de54d JH |
67 | || NEXT_INSN (NEXT_INSN (NEXT_INSN (NEXT_INSN (last))))) |
68 | goto failed; | |
41806d92 | 69 | gcc_assert (!find_reg_note (last, REG_BR_PROB, 0)); |
65c5f2a6 ILT |
70 | add_reg_note (last, REG_BR_PROB, |
71 | GEN_INT (REG_BR_PROB_BASE - probability)); | |
e53de54d JH |
72 | return; |
73 | } | |
2ca202e7 | 74 | if (!last || !JUMP_P (last) || !any_condjump_p (last)) |
41806d92 NS |
75 | goto failed; |
76 | gcc_assert (!find_reg_note (last, REG_BR_PROB, 0)); | |
65c5f2a6 | 77 | add_reg_note (last, REG_BR_PROB, GEN_INT (probability)); |
e53de54d JH |
78 | return; |
79 | failed: | |
80 | if (dump_file) | |
81 | fprintf (dump_file, "Failed to add probability note\n"); | |
82 | } | |
83 | ||
80c7a9eb | 84 | |
1f6d3a08 RH |
85 | #ifndef STACK_ALIGNMENT_NEEDED |
86 | #define STACK_ALIGNMENT_NEEDED 1 | |
87 | #endif | |
88 | ||
1f6d3a08 RH |
89 | |
90 | /* This structure holds data relevant to one variable that will be | |
91 | placed in a stack slot. */ | |
92 | struct stack_var | |
93 | { | |
94 | /* The Variable. */ | |
95 | tree decl; | |
96 | ||
97 | /* The offset of the variable. During partitioning, this is the | |
98 | offset relative to the partition. After partitioning, this | |
99 | is relative to the stack frame. */ | |
100 | HOST_WIDE_INT offset; | |
101 | ||
102 | /* Initially, the size of the variable. Later, the size of the partition, | |
103 | if this variable becomes it's partition's representative. */ | |
104 | HOST_WIDE_INT size; | |
105 | ||
106 | /* The *byte* alignment required for this variable. Or as, with the | |
107 | size, the alignment for this partition. */ | |
108 | unsigned int alignb; | |
109 | ||
110 | /* The partition representative. */ | |
111 | size_t representative; | |
112 | ||
113 | /* The next stack variable in the partition, or EOC. */ | |
114 | size_t next; | |
115 | }; | |
116 | ||
117 | #define EOC ((size_t)-1) | |
118 | ||
119 | /* We have an array of such objects while deciding allocation. */ | |
120 | static struct stack_var *stack_vars; | |
121 | static size_t stack_vars_alloc; | |
122 | static size_t stack_vars_num; | |
123 | ||
fa10beec | 124 | /* An array of indices such that stack_vars[stack_vars_sorted[i]].size |
1f6d3a08 RH |
125 | is non-decreasing. */ |
126 | static size_t *stack_vars_sorted; | |
127 | ||
128 | /* We have an interference graph between such objects. This graph | |
129 | is lower triangular. */ | |
130 | static bool *stack_vars_conflict; | |
131 | static size_t stack_vars_conflict_alloc; | |
132 | ||
133 | /* The phase of the stack frame. This is the known misalignment of | |
134 | virtual_stack_vars_rtx from PREFERRED_STACK_BOUNDARY. That is, | |
135 | (frame_offset+frame_phase) % PREFERRED_STACK_BOUNDARY == 0. */ | |
136 | static int frame_phase; | |
137 | ||
7d69de61 RH |
138 | /* Used during expand_used_vars to remember if we saw any decls for |
139 | which we'd like to enable stack smashing protection. */ | |
140 | static bool has_protected_decls; | |
141 | ||
142 | /* Used during expand_used_vars. Remember if we say a character buffer | |
143 | smaller than our cutoff threshold. Used for -Wstack-protector. */ | |
144 | static bool has_short_buffer; | |
1f6d3a08 RH |
145 | |
146 | /* Discover the byte alignment to use for DECL. Ignore alignment | |
147 | we can't do with expected alignment of the stack boundary. */ | |
148 | ||
149 | static unsigned int | |
150 | get_decl_align_unit (tree decl) | |
151 | { | |
152 | unsigned int align; | |
153 | ||
154 | align = DECL_ALIGN (decl); | |
155 | align = LOCAL_ALIGNMENT (TREE_TYPE (decl), align); | |
156 | if (align > PREFERRED_STACK_BOUNDARY) | |
157 | align = PREFERRED_STACK_BOUNDARY; | |
cb91fab0 JH |
158 | if (crtl->stack_alignment_needed < align) |
159 | crtl->stack_alignment_needed = align; | |
1f6d3a08 RH |
160 | |
161 | return align / BITS_PER_UNIT; | |
162 | } | |
163 | ||
164 | /* Allocate SIZE bytes at byte alignment ALIGN from the stack frame. | |
165 | Return the frame offset. */ | |
166 | ||
167 | static HOST_WIDE_INT | |
168 | alloc_stack_frame_space (HOST_WIDE_INT size, HOST_WIDE_INT align) | |
169 | { | |
170 | HOST_WIDE_INT offset, new_frame_offset; | |
171 | ||
172 | new_frame_offset = frame_offset; | |
173 | if (FRAME_GROWS_DOWNWARD) | |
174 | { | |
175 | new_frame_offset -= size + frame_phase; | |
176 | new_frame_offset &= -align; | |
177 | new_frame_offset += frame_phase; | |
178 | offset = new_frame_offset; | |
179 | } | |
180 | else | |
181 | { | |
182 | new_frame_offset -= frame_phase; | |
183 | new_frame_offset += align - 1; | |
184 | new_frame_offset &= -align; | |
185 | new_frame_offset += frame_phase; | |
186 | offset = new_frame_offset; | |
187 | new_frame_offset += size; | |
188 | } | |
189 | frame_offset = new_frame_offset; | |
190 | ||
9fb798d7 EB |
191 | if (frame_offset_overflow (frame_offset, cfun->decl)) |
192 | frame_offset = offset = 0; | |
193 | ||
1f6d3a08 RH |
194 | return offset; |
195 | } | |
196 | ||
197 | /* Accumulate DECL into STACK_VARS. */ | |
198 | ||
199 | static void | |
200 | add_stack_var (tree decl) | |
201 | { | |
202 | if (stack_vars_num >= stack_vars_alloc) | |
203 | { | |
204 | if (stack_vars_alloc) | |
205 | stack_vars_alloc = stack_vars_alloc * 3 / 2; | |
206 | else | |
207 | stack_vars_alloc = 32; | |
208 | stack_vars | |
209 | = XRESIZEVEC (struct stack_var, stack_vars, stack_vars_alloc); | |
210 | } | |
211 | stack_vars[stack_vars_num].decl = decl; | |
212 | stack_vars[stack_vars_num].offset = 0; | |
213 | stack_vars[stack_vars_num].size = tree_low_cst (DECL_SIZE_UNIT (decl), 1); | |
214 | stack_vars[stack_vars_num].alignb = get_decl_align_unit (decl); | |
215 | ||
216 | /* All variables are initially in their own partition. */ | |
217 | stack_vars[stack_vars_num].representative = stack_vars_num; | |
218 | stack_vars[stack_vars_num].next = EOC; | |
219 | ||
220 | /* Ensure that this decl doesn't get put onto the list twice. */ | |
221 | SET_DECL_RTL (decl, pc_rtx); | |
222 | ||
223 | stack_vars_num++; | |
224 | } | |
225 | ||
226 | /* Compute the linear index of a lower-triangular coordinate (I, J). */ | |
227 | ||
228 | static size_t | |
229 | triangular_index (size_t i, size_t j) | |
230 | { | |
231 | if (i < j) | |
232 | { | |
233 | size_t t; | |
234 | t = i, i = j, j = t; | |
235 | } | |
236 | return (i * (i + 1)) / 2 + j; | |
237 | } | |
238 | ||
239 | /* Ensure that STACK_VARS_CONFLICT is large enough for N objects. */ | |
240 | ||
241 | static void | |
242 | resize_stack_vars_conflict (size_t n) | |
243 | { | |
244 | size_t size = triangular_index (n-1, n-1) + 1; | |
245 | ||
246 | if (size <= stack_vars_conflict_alloc) | |
247 | return; | |
248 | ||
249 | stack_vars_conflict = XRESIZEVEC (bool, stack_vars_conflict, size); | |
250 | memset (stack_vars_conflict + stack_vars_conflict_alloc, 0, | |
251 | (size - stack_vars_conflict_alloc) * sizeof (bool)); | |
252 | stack_vars_conflict_alloc = size; | |
253 | } | |
254 | ||
255 | /* Make the decls associated with luid's X and Y conflict. */ | |
256 | ||
257 | static void | |
258 | add_stack_var_conflict (size_t x, size_t y) | |
259 | { | |
260 | size_t index = triangular_index (x, y); | |
261 | gcc_assert (index < stack_vars_conflict_alloc); | |
262 | stack_vars_conflict[index] = true; | |
263 | } | |
264 | ||
265 | /* Check whether the decls associated with luid's X and Y conflict. */ | |
266 | ||
267 | static bool | |
268 | stack_var_conflict_p (size_t x, size_t y) | |
269 | { | |
270 | size_t index = triangular_index (x, y); | |
271 | gcc_assert (index < stack_vars_conflict_alloc); | |
272 | return stack_vars_conflict[index]; | |
273 | } | |
d239ed56 SB |
274 | |
275 | /* Returns true if TYPE is or contains a union type. */ | |
276 | ||
277 | static bool | |
278 | aggregate_contains_union_type (tree type) | |
279 | { | |
280 | tree field; | |
281 | ||
282 | if (TREE_CODE (type) == UNION_TYPE | |
283 | || TREE_CODE (type) == QUAL_UNION_TYPE) | |
284 | return true; | |
285 | if (TREE_CODE (type) == ARRAY_TYPE) | |
286 | return aggregate_contains_union_type (TREE_TYPE (type)); | |
287 | if (TREE_CODE (type) != RECORD_TYPE) | |
288 | return false; | |
289 | ||
290 | for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field)) | |
291 | if (TREE_CODE (field) == FIELD_DECL) | |
292 | if (aggregate_contains_union_type (TREE_TYPE (field))) | |
293 | return true; | |
294 | ||
295 | return false; | |
296 | } | |
297 | ||
1f6d3a08 RH |
298 | /* A subroutine of expand_used_vars. If two variables X and Y have alias |
299 | sets that do not conflict, then do add a conflict for these variables | |
d239ed56 SB |
300 | in the interference graph. We also need to make sure to add conflicts |
301 | for union containing structures. Else RTL alias analysis comes along | |
302 | and due to type based aliasing rules decides that for two overlapping | |
303 | union temporaries { short s; int i; } accesses to the same mem through | |
304 | different types may not alias and happily reorders stores across | |
305 | life-time boundaries of the temporaries (See PR25654). | |
306 | We also have to mind MEM_IN_STRUCT_P and MEM_SCALAR_P. */ | |
1f6d3a08 RH |
307 | |
308 | static void | |
309 | add_alias_set_conflicts (void) | |
310 | { | |
311 | size_t i, j, n = stack_vars_num; | |
312 | ||
313 | for (i = 0; i < n; ++i) | |
314 | { | |
a4d25453 RH |
315 | tree type_i = TREE_TYPE (stack_vars[i].decl); |
316 | bool aggr_i = AGGREGATE_TYPE_P (type_i); | |
d239ed56 | 317 | bool contains_union; |
1f6d3a08 | 318 | |
d239ed56 | 319 | contains_union = aggregate_contains_union_type (type_i); |
1f6d3a08 RH |
320 | for (j = 0; j < i; ++j) |
321 | { | |
a4d25453 RH |
322 | tree type_j = TREE_TYPE (stack_vars[j].decl); |
323 | bool aggr_j = AGGREGATE_TYPE_P (type_j); | |
d239ed56 SB |
324 | if (aggr_i != aggr_j |
325 | /* Either the objects conflict by means of type based | |
326 | aliasing rules, or we need to add a conflict. */ | |
327 | || !objects_must_conflict_p (type_i, type_j) | |
328 | /* In case the types do not conflict ensure that access | |
329 | to elements will conflict. In case of unions we have | |
330 | to be careful as type based aliasing rules may say | |
331 | access to the same memory does not conflict. So play | |
332 | safe and add a conflict in this case. */ | |
333 | || contains_union) | |
1f6d3a08 RH |
334 | add_stack_var_conflict (i, j); |
335 | } | |
336 | } | |
337 | } | |
338 | ||
339 | /* A subroutine of partition_stack_vars. A comparison function for qsort, | |
fa10beec | 340 | sorting an array of indices by the size of the object. */ |
1f6d3a08 RH |
341 | |
342 | static int | |
343 | stack_var_size_cmp (const void *a, const void *b) | |
344 | { | |
345 | HOST_WIDE_INT sa = stack_vars[*(const size_t *)a].size; | |
346 | HOST_WIDE_INT sb = stack_vars[*(const size_t *)b].size; | |
79f802f5 RG |
347 | unsigned int uida = DECL_UID (stack_vars[*(const size_t *)a].decl); |
348 | unsigned int uidb = DECL_UID (stack_vars[*(const size_t *)b].decl); | |
1f6d3a08 RH |
349 | |
350 | if (sa < sb) | |
351 | return -1; | |
352 | if (sa > sb) | |
353 | return 1; | |
79f802f5 RG |
354 | /* For stack variables of the same size use the uid of the decl |
355 | to make the sort stable. */ | |
356 | if (uida < uidb) | |
357 | return -1; | |
358 | if (uida > uidb) | |
359 | return 1; | |
1f6d3a08 RH |
360 | return 0; |
361 | } | |
362 | ||
363 | /* A subroutine of partition_stack_vars. The UNION portion of a UNION/FIND | |
364 | partitioning algorithm. Partitions A and B are known to be non-conflicting. | |
365 | Merge them into a single partition A. | |
366 | ||
367 | At the same time, add OFFSET to all variables in partition B. At the end | |
368 | of the partitioning process we've have a nice block easy to lay out within | |
369 | the stack frame. */ | |
370 | ||
371 | static void | |
372 | union_stack_vars (size_t a, size_t b, HOST_WIDE_INT offset) | |
373 | { | |
374 | size_t i, last; | |
375 | ||
376 | /* Update each element of partition B with the given offset, | |
377 | and merge them into partition A. */ | |
378 | for (last = i = b; i != EOC; last = i, i = stack_vars[i].next) | |
379 | { | |
380 | stack_vars[i].offset += offset; | |
381 | stack_vars[i].representative = a; | |
382 | } | |
383 | stack_vars[last].next = stack_vars[a].next; | |
384 | stack_vars[a].next = b; | |
385 | ||
386 | /* Update the required alignment of partition A to account for B. */ | |
387 | if (stack_vars[a].alignb < stack_vars[b].alignb) | |
388 | stack_vars[a].alignb = stack_vars[b].alignb; | |
389 | ||
390 | /* Update the interference graph and merge the conflicts. */ | |
391 | for (last = stack_vars_num, i = 0; i < last; ++i) | |
392 | if (stack_var_conflict_p (b, i)) | |
393 | add_stack_var_conflict (a, i); | |
394 | } | |
395 | ||
396 | /* A subroutine of expand_used_vars. Binpack the variables into | |
397 | partitions constrained by the interference graph. The overall | |
398 | algorithm used is as follows: | |
399 | ||
400 | Sort the objects by size. | |
401 | For each object A { | |
402 | S = size(A) | |
403 | O = 0 | |
404 | loop { | |
405 | Look for the largest non-conflicting object B with size <= S. | |
406 | UNION (A, B) | |
407 | offset(B) = O | |
408 | O += size(B) | |
409 | S -= size(B) | |
410 | } | |
411 | } | |
412 | */ | |
413 | ||
414 | static void | |
415 | partition_stack_vars (void) | |
416 | { | |
417 | size_t si, sj, n = stack_vars_num; | |
418 | ||
419 | stack_vars_sorted = XNEWVEC (size_t, stack_vars_num); | |
420 | for (si = 0; si < n; ++si) | |
421 | stack_vars_sorted[si] = si; | |
422 | ||
423 | if (n == 1) | |
424 | return; | |
425 | ||
426 | qsort (stack_vars_sorted, n, sizeof (size_t), stack_var_size_cmp); | |
427 | ||
428 | /* Special case: detect when all variables conflict, and thus we can't | |
429 | do anything during the partitioning loop. It isn't uncommon (with | |
430 | C code at least) to declare all variables at the top of the function, | |
431 | and if we're not inlining, then all variables will be in the same scope. | |
432 | Take advantage of very fast libc routines for this scan. */ | |
433 | gcc_assert (sizeof(bool) == sizeof(char)); | |
434 | if (memchr (stack_vars_conflict, false, stack_vars_conflict_alloc) == NULL) | |
435 | return; | |
436 | ||
437 | for (si = 0; si < n; ++si) | |
438 | { | |
439 | size_t i = stack_vars_sorted[si]; | |
440 | HOST_WIDE_INT isize = stack_vars[i].size; | |
441 | HOST_WIDE_INT offset = 0; | |
442 | ||
443 | for (sj = si; sj-- > 0; ) | |
444 | { | |
445 | size_t j = stack_vars_sorted[sj]; | |
446 | HOST_WIDE_INT jsize = stack_vars[j].size; | |
447 | unsigned int jalign = stack_vars[j].alignb; | |
448 | ||
449 | /* Ignore objects that aren't partition representatives. */ | |
450 | if (stack_vars[j].representative != j) | |
451 | continue; | |
452 | ||
453 | /* Ignore objects too large for the remaining space. */ | |
454 | if (isize < jsize) | |
455 | continue; | |
456 | ||
457 | /* Ignore conflicting objects. */ | |
458 | if (stack_var_conflict_p (i, j)) | |
459 | continue; | |
460 | ||
461 | /* Refine the remaining space check to include alignment. */ | |
462 | if (offset & (jalign - 1)) | |
463 | { | |
464 | HOST_WIDE_INT toff = offset; | |
465 | toff += jalign - 1; | |
466 | toff &= -(HOST_WIDE_INT)jalign; | |
467 | if (isize - (toff - offset) < jsize) | |
468 | continue; | |
469 | ||
470 | isize -= toff - offset; | |
471 | offset = toff; | |
472 | } | |
473 | ||
474 | /* UNION the objects, placing J at OFFSET. */ | |
475 | union_stack_vars (i, j, offset); | |
476 | ||
477 | isize -= jsize; | |
478 | if (isize == 0) | |
479 | break; | |
480 | } | |
481 | } | |
482 | } | |
483 | ||
484 | /* A debugging aid for expand_used_vars. Dump the generated partitions. */ | |
485 | ||
486 | static void | |
487 | dump_stack_var_partition (void) | |
488 | { | |
489 | size_t si, i, j, n = stack_vars_num; | |
490 | ||
491 | for (si = 0; si < n; ++si) | |
492 | { | |
493 | i = stack_vars_sorted[si]; | |
494 | ||
495 | /* Skip variables that aren't partition representatives, for now. */ | |
496 | if (stack_vars[i].representative != i) | |
497 | continue; | |
498 | ||
499 | fprintf (dump_file, "Partition %lu: size " HOST_WIDE_INT_PRINT_DEC | |
500 | " align %u\n", (unsigned long) i, stack_vars[i].size, | |
501 | stack_vars[i].alignb); | |
502 | ||
503 | for (j = i; j != EOC; j = stack_vars[j].next) | |
504 | { | |
505 | fputc ('\t', dump_file); | |
506 | print_generic_expr (dump_file, stack_vars[j].decl, dump_flags); | |
507 | fprintf (dump_file, ", offset " HOST_WIDE_INT_PRINT_DEC "\n", | |
1c50a20a | 508 | stack_vars[j].offset); |
1f6d3a08 RH |
509 | } |
510 | } | |
511 | } | |
512 | ||
513 | /* Assign rtl to DECL at frame offset OFFSET. */ | |
514 | ||
515 | static void | |
516 | expand_one_stack_var_at (tree decl, HOST_WIDE_INT offset) | |
517 | { | |
518 | HOST_WIDE_INT align; | |
519 | rtx x; | |
c22cacf3 | 520 | |
1f6d3a08 RH |
521 | /* If this fails, we've overflowed the stack frame. Error nicely? */ |
522 | gcc_assert (offset == trunc_int_for_mode (offset, Pmode)); | |
523 | ||
524 | x = plus_constant (virtual_stack_vars_rtx, offset); | |
525 | x = gen_rtx_MEM (DECL_MODE (decl), x); | |
526 | ||
527 | /* Set alignment we actually gave this decl. */ | |
528 | offset -= frame_phase; | |
529 | align = offset & -offset; | |
530 | align *= BITS_PER_UNIT; | |
531 | if (align > STACK_BOUNDARY || align == 0) | |
532 | align = STACK_BOUNDARY; | |
533 | DECL_ALIGN (decl) = align; | |
534 | DECL_USER_ALIGN (decl) = 0; | |
535 | ||
536 | set_mem_attributes (x, decl, true); | |
537 | SET_DECL_RTL (decl, x); | |
538 | } | |
539 | ||
540 | /* A subroutine of expand_used_vars. Give each partition representative | |
541 | a unique location within the stack frame. Update each partition member | |
542 | with that location. */ | |
543 | ||
544 | static void | |
7d69de61 | 545 | expand_stack_vars (bool (*pred) (tree)) |
1f6d3a08 RH |
546 | { |
547 | size_t si, i, j, n = stack_vars_num; | |
548 | ||
549 | for (si = 0; si < n; ++si) | |
550 | { | |
551 | HOST_WIDE_INT offset; | |
552 | ||
553 | i = stack_vars_sorted[si]; | |
554 | ||
555 | /* Skip variables that aren't partition representatives, for now. */ | |
556 | if (stack_vars[i].representative != i) | |
557 | continue; | |
558 | ||
7d69de61 RH |
559 | /* Skip variables that have already had rtl assigned. See also |
560 | add_stack_var where we perpetrate this pc_rtx hack. */ | |
561 | if (DECL_RTL (stack_vars[i].decl) != pc_rtx) | |
562 | continue; | |
563 | ||
c22cacf3 | 564 | /* Check the predicate to see whether this variable should be |
7d69de61 RH |
565 | allocated in this pass. */ |
566 | if (pred && !pred (stack_vars[i].decl)) | |
567 | continue; | |
568 | ||
1f6d3a08 RH |
569 | offset = alloc_stack_frame_space (stack_vars[i].size, |
570 | stack_vars[i].alignb); | |
571 | ||
572 | /* Create rtl for each variable based on their location within the | |
573 | partition. */ | |
574 | for (j = i; j != EOC; j = stack_vars[j].next) | |
f8da8190 AP |
575 | { |
576 | gcc_assert (stack_vars[j].offset <= stack_vars[i].size); | |
577 | expand_one_stack_var_at (stack_vars[j].decl, | |
578 | stack_vars[j].offset + offset); | |
579 | } | |
1f6d3a08 RH |
580 | } |
581 | } | |
582 | ||
ff28a94d JH |
583 | /* Take into account all sizes of partitions and reset DECL_RTLs. */ |
584 | static HOST_WIDE_INT | |
585 | account_stack_vars (void) | |
586 | { | |
587 | size_t si, j, i, n = stack_vars_num; | |
588 | HOST_WIDE_INT size = 0; | |
589 | ||
590 | for (si = 0; si < n; ++si) | |
591 | { | |
592 | i = stack_vars_sorted[si]; | |
593 | ||
594 | /* Skip variables that aren't partition representatives, for now. */ | |
595 | if (stack_vars[i].representative != i) | |
596 | continue; | |
597 | ||
598 | size += stack_vars[i].size; | |
599 | for (j = i; j != EOC; j = stack_vars[j].next) | |
600 | SET_DECL_RTL (stack_vars[j].decl, NULL); | |
601 | } | |
602 | return size; | |
603 | } | |
604 | ||
1f6d3a08 RH |
605 | /* A subroutine of expand_one_var. Called to immediately assign rtl |
606 | to a variable to be allocated in the stack frame. */ | |
607 | ||
608 | static void | |
609 | expand_one_stack_var (tree var) | |
610 | { | |
611 | HOST_WIDE_INT size, offset, align; | |
612 | ||
613 | size = tree_low_cst (DECL_SIZE_UNIT (var), 1); | |
614 | align = get_decl_align_unit (var); | |
615 | offset = alloc_stack_frame_space (size, align); | |
616 | ||
617 | expand_one_stack_var_at (var, offset); | |
618 | } | |
619 | ||
1f6d3a08 RH |
620 | /* A subroutine of expand_one_var. Called to assign rtl to a VAR_DECL |
621 | that will reside in a hard register. */ | |
622 | ||
623 | static void | |
624 | expand_one_hard_reg_var (tree var) | |
625 | { | |
626 | rest_of_decl_compilation (var, 0, 0); | |
627 | } | |
628 | ||
629 | /* A subroutine of expand_one_var. Called to assign rtl to a VAR_DECL | |
630 | that will reside in a pseudo register. */ | |
631 | ||
632 | static void | |
633 | expand_one_register_var (tree var) | |
634 | { | |
635 | tree type = TREE_TYPE (var); | |
636 | int unsignedp = TYPE_UNSIGNED (type); | |
637 | enum machine_mode reg_mode | |
638 | = promote_mode (type, DECL_MODE (var), &unsignedp, 0); | |
639 | rtx x = gen_reg_rtx (reg_mode); | |
640 | ||
641 | SET_DECL_RTL (var, x); | |
642 | ||
643 | /* Note if the object is a user variable. */ | |
644 | if (!DECL_ARTIFICIAL (var)) | |
1f6d3a08 RH |
645 | mark_user_reg (x); |
646 | ||
61021c2c AP |
647 | if (POINTER_TYPE_P (type)) |
648 | mark_reg_pointer (x, TYPE_ALIGN (TREE_TYPE (TREE_TYPE (var)))); | |
1f6d3a08 RH |
649 | } |
650 | ||
651 | /* A subroutine of expand_one_var. Called to assign rtl to a VAR_DECL that | |
128a79fb | 652 | has some associated error, e.g. its type is error-mark. We just need |
1f6d3a08 RH |
653 | to pick something that won't crash the rest of the compiler. */ |
654 | ||
655 | static void | |
656 | expand_one_error_var (tree var) | |
657 | { | |
658 | enum machine_mode mode = DECL_MODE (var); | |
659 | rtx x; | |
660 | ||
661 | if (mode == BLKmode) | |
662 | x = gen_rtx_MEM (BLKmode, const0_rtx); | |
663 | else if (mode == VOIDmode) | |
664 | x = const0_rtx; | |
665 | else | |
666 | x = gen_reg_rtx (mode); | |
667 | ||
668 | SET_DECL_RTL (var, x); | |
669 | } | |
670 | ||
c22cacf3 | 671 | /* A subroutine of expand_one_var. VAR is a variable that will be |
1f6d3a08 RH |
672 | allocated to the local stack frame. Return true if we wish to |
673 | add VAR to STACK_VARS so that it will be coalesced with other | |
674 | variables. Return false to allocate VAR immediately. | |
675 | ||
676 | This function is used to reduce the number of variables considered | |
677 | for coalescing, which reduces the size of the quadratic problem. */ | |
678 | ||
679 | static bool | |
680 | defer_stack_allocation (tree var, bool toplevel) | |
681 | { | |
7d69de61 RH |
682 | /* If stack protection is enabled, *all* stack variables must be deferred, |
683 | so that we can re-order the strings to the top of the frame. */ | |
684 | if (flag_stack_protect) | |
685 | return true; | |
686 | ||
1f6d3a08 RH |
687 | /* Variables in the outermost scope automatically conflict with |
688 | every other variable. The only reason to want to defer them | |
689 | at all is that, after sorting, we can more efficiently pack | |
690 | small variables in the stack frame. Continue to defer at -O2. */ | |
691 | if (toplevel && optimize < 2) | |
692 | return false; | |
693 | ||
694 | /* Without optimization, *most* variables are allocated from the | |
695 | stack, which makes the quadratic problem large exactly when we | |
c22cacf3 | 696 | want compilation to proceed as quickly as possible. On the |
1f6d3a08 RH |
697 | other hand, we don't want the function's stack frame size to |
698 | get completely out of hand. So we avoid adding scalars and | |
699 | "small" aggregates to the list at all. */ | |
700 | if (optimize == 0 && tree_low_cst (DECL_SIZE_UNIT (var), 1) < 32) | |
701 | return false; | |
702 | ||
703 | return true; | |
704 | } | |
705 | ||
706 | /* A subroutine of expand_used_vars. Expand one variable according to | |
2a7e31df | 707 | its flavor. Variables to be placed on the stack are not actually |
ff28a94d JH |
708 | expanded yet, merely recorded. |
709 | When REALLY_EXPAND is false, only add stack values to be allocated. | |
710 | Return stack usage this variable is supposed to take. | |
711 | */ | |
1f6d3a08 | 712 | |
ff28a94d JH |
713 | static HOST_WIDE_INT |
714 | expand_one_var (tree var, bool toplevel, bool really_expand) | |
1f6d3a08 RH |
715 | { |
716 | if (TREE_CODE (var) != VAR_DECL) | |
4846b435 | 717 | ; |
1f6d3a08 RH |
718 | else if (DECL_EXTERNAL (var)) |
719 | ; | |
833b3afe | 720 | else if (DECL_HAS_VALUE_EXPR_P (var)) |
1f6d3a08 RH |
721 | ; |
722 | else if (TREE_STATIC (var)) | |
7e8b322a | 723 | ; |
1f6d3a08 RH |
724 | else if (DECL_RTL_SET_P (var)) |
725 | ; | |
726 | else if (TREE_TYPE (var) == error_mark_node) | |
ff28a94d JH |
727 | { |
728 | if (really_expand) | |
729 | expand_one_error_var (var); | |
730 | } | |
1f6d3a08 | 731 | else if (DECL_HARD_REGISTER (var)) |
ff28a94d JH |
732 | { |
733 | if (really_expand) | |
734 | expand_one_hard_reg_var (var); | |
735 | } | |
1f6d3a08 | 736 | else if (use_register_for_decl (var)) |
ff28a94d JH |
737 | { |
738 | if (really_expand) | |
739 | expand_one_register_var (var); | |
740 | } | |
1f6d3a08 RH |
741 | else if (defer_stack_allocation (var, toplevel)) |
742 | add_stack_var (var); | |
743 | else | |
ff28a94d | 744 | { |
bd9f1b4b JH |
745 | if (really_expand) |
746 | expand_one_stack_var (var); | |
ff28a94d JH |
747 | return tree_low_cst (DECL_SIZE_UNIT (var), 1); |
748 | } | |
749 | return 0; | |
1f6d3a08 RH |
750 | } |
751 | ||
752 | /* A subroutine of expand_used_vars. Walk down through the BLOCK tree | |
753 | expanding variables. Those variables that can be put into registers | |
754 | are allocated pseudos; those that can't are put on the stack. | |
755 | ||
756 | TOPLEVEL is true if this is the outermost BLOCK. */ | |
757 | ||
758 | static void | |
759 | expand_used_vars_for_block (tree block, bool toplevel) | |
760 | { | |
761 | size_t i, j, old_sv_num, this_sv_num, new_sv_num; | |
762 | tree t; | |
763 | ||
764 | old_sv_num = toplevel ? 0 : stack_vars_num; | |
765 | ||
766 | /* Expand all variables at this level. */ | |
767 | for (t = BLOCK_VARS (block); t ; t = TREE_CHAIN (t)) | |
7e8b322a | 768 | if (TREE_USED (t)) |
ff28a94d | 769 | expand_one_var (t, toplevel, true); |
1f6d3a08 RH |
770 | |
771 | this_sv_num = stack_vars_num; | |
772 | ||
773 | /* Expand all variables at containing levels. */ | |
774 | for (t = BLOCK_SUBBLOCKS (block); t ; t = BLOCK_CHAIN (t)) | |
775 | expand_used_vars_for_block (t, false); | |
776 | ||
777 | /* Since we do not track exact variable lifetimes (which is not even | |
6fc0bb99 | 778 | possible for variables whose address escapes), we mirror the block |
1f6d3a08 RH |
779 | tree in the interference graph. Here we cause all variables at this |
780 | level, and all sublevels, to conflict. Do make certain that a | |
781 | variable conflicts with itself. */ | |
782 | if (old_sv_num < this_sv_num) | |
783 | { | |
784 | new_sv_num = stack_vars_num; | |
785 | resize_stack_vars_conflict (new_sv_num); | |
786 | ||
787 | for (i = old_sv_num; i < new_sv_num; ++i) | |
f4a6d54e RH |
788 | for (j = i < this_sv_num ? i+1 : this_sv_num; j-- > old_sv_num ;) |
789 | add_stack_var_conflict (i, j); | |
1f6d3a08 RH |
790 | } |
791 | } | |
792 | ||
793 | /* A subroutine of expand_used_vars. Walk down through the BLOCK tree | |
794 | and clear TREE_USED on all local variables. */ | |
795 | ||
796 | static void | |
797 | clear_tree_used (tree block) | |
798 | { | |
799 | tree t; | |
800 | ||
801 | for (t = BLOCK_VARS (block); t ; t = TREE_CHAIN (t)) | |
802 | /* if (!TREE_STATIC (t) && !DECL_EXTERNAL (t)) */ | |
803 | TREE_USED (t) = 0; | |
804 | ||
805 | for (t = BLOCK_SUBBLOCKS (block); t ; t = BLOCK_CHAIN (t)) | |
806 | clear_tree_used (t); | |
807 | } | |
808 | ||
7d69de61 RH |
809 | /* Examine TYPE and determine a bit mask of the following features. */ |
810 | ||
811 | #define SPCT_HAS_LARGE_CHAR_ARRAY 1 | |
812 | #define SPCT_HAS_SMALL_CHAR_ARRAY 2 | |
813 | #define SPCT_HAS_ARRAY 4 | |
814 | #define SPCT_HAS_AGGREGATE 8 | |
815 | ||
816 | static unsigned int | |
817 | stack_protect_classify_type (tree type) | |
818 | { | |
819 | unsigned int ret = 0; | |
820 | tree t; | |
821 | ||
822 | switch (TREE_CODE (type)) | |
823 | { | |
824 | case ARRAY_TYPE: | |
825 | t = TYPE_MAIN_VARIANT (TREE_TYPE (type)); | |
826 | if (t == char_type_node | |
827 | || t == signed_char_type_node | |
828 | || t == unsigned_char_type_node) | |
829 | { | |
15362b89 JJ |
830 | unsigned HOST_WIDE_INT max = PARAM_VALUE (PARAM_SSP_BUFFER_SIZE); |
831 | unsigned HOST_WIDE_INT len; | |
7d69de61 | 832 | |
15362b89 JJ |
833 | if (!TYPE_SIZE_UNIT (type) |
834 | || !host_integerp (TYPE_SIZE_UNIT (type), 1)) | |
835 | len = max; | |
7d69de61 | 836 | else |
15362b89 | 837 | len = tree_low_cst (TYPE_SIZE_UNIT (type), 1); |
7d69de61 RH |
838 | |
839 | if (len < max) | |
840 | ret = SPCT_HAS_SMALL_CHAR_ARRAY | SPCT_HAS_ARRAY; | |
841 | else | |
842 | ret = SPCT_HAS_LARGE_CHAR_ARRAY | SPCT_HAS_ARRAY; | |
843 | } | |
844 | else | |
845 | ret = SPCT_HAS_ARRAY; | |
846 | break; | |
847 | ||
848 | case UNION_TYPE: | |
849 | case QUAL_UNION_TYPE: | |
850 | case RECORD_TYPE: | |
851 | ret = SPCT_HAS_AGGREGATE; | |
852 | for (t = TYPE_FIELDS (type); t ; t = TREE_CHAIN (t)) | |
853 | if (TREE_CODE (t) == FIELD_DECL) | |
854 | ret |= stack_protect_classify_type (TREE_TYPE (t)); | |
855 | break; | |
856 | ||
857 | default: | |
858 | break; | |
859 | } | |
860 | ||
861 | return ret; | |
862 | } | |
863 | ||
a4d05547 KH |
864 | /* Return nonzero if DECL should be segregated into the "vulnerable" upper |
865 | part of the local stack frame. Remember if we ever return nonzero for | |
7d69de61 RH |
866 | any variable in this function. The return value is the phase number in |
867 | which the variable should be allocated. */ | |
868 | ||
869 | static int | |
870 | stack_protect_decl_phase (tree decl) | |
871 | { | |
872 | unsigned int bits = stack_protect_classify_type (TREE_TYPE (decl)); | |
873 | int ret = 0; | |
874 | ||
875 | if (bits & SPCT_HAS_SMALL_CHAR_ARRAY) | |
876 | has_short_buffer = true; | |
877 | ||
878 | if (flag_stack_protect == 2) | |
879 | { | |
880 | if ((bits & (SPCT_HAS_SMALL_CHAR_ARRAY | SPCT_HAS_LARGE_CHAR_ARRAY)) | |
881 | && !(bits & SPCT_HAS_AGGREGATE)) | |
882 | ret = 1; | |
883 | else if (bits & SPCT_HAS_ARRAY) | |
884 | ret = 2; | |
885 | } | |
886 | else | |
887 | ret = (bits & SPCT_HAS_LARGE_CHAR_ARRAY) != 0; | |
888 | ||
889 | if (ret) | |
890 | has_protected_decls = true; | |
891 | ||
892 | return ret; | |
893 | } | |
894 | ||
895 | /* Two helper routines that check for phase 1 and phase 2. These are used | |
896 | as callbacks for expand_stack_vars. */ | |
897 | ||
898 | static bool | |
899 | stack_protect_decl_phase_1 (tree decl) | |
900 | { | |
901 | return stack_protect_decl_phase (decl) == 1; | |
902 | } | |
903 | ||
904 | static bool | |
905 | stack_protect_decl_phase_2 (tree decl) | |
906 | { | |
907 | return stack_protect_decl_phase (decl) == 2; | |
908 | } | |
909 | ||
910 | /* Ensure that variables in different stack protection phases conflict | |
911 | so that they are not merged and share the same stack slot. */ | |
912 | ||
913 | static void | |
914 | add_stack_protection_conflicts (void) | |
915 | { | |
916 | size_t i, j, n = stack_vars_num; | |
917 | unsigned char *phase; | |
918 | ||
919 | phase = XNEWVEC (unsigned char, n); | |
920 | for (i = 0; i < n; ++i) | |
921 | phase[i] = stack_protect_decl_phase (stack_vars[i].decl); | |
922 | ||
923 | for (i = 0; i < n; ++i) | |
924 | { | |
925 | unsigned char ph_i = phase[i]; | |
926 | for (j = 0; j < i; ++j) | |
927 | if (ph_i != phase[j]) | |
928 | add_stack_var_conflict (i, j); | |
929 | } | |
930 | ||
931 | XDELETEVEC (phase); | |
932 | } | |
933 | ||
934 | /* Create a decl for the guard at the top of the stack frame. */ | |
935 | ||
936 | static void | |
937 | create_stack_guard (void) | |
938 | { | |
939 | tree guard = build_decl (VAR_DECL, NULL, ptr_type_node); | |
940 | TREE_THIS_VOLATILE (guard) = 1; | |
941 | TREE_USED (guard) = 1; | |
942 | expand_one_stack_var (guard); | |
cb91fab0 | 943 | crtl->stack_protect_guard = guard; |
7d69de61 RH |
944 | } |
945 | ||
ff28a94d JH |
946 | /* A subroutine of expand_used_vars. Walk down through the BLOCK tree |
947 | expanding variables. Those variables that can be put into registers | |
948 | are allocated pseudos; those that can't are put on the stack. | |
949 | ||
950 | TOPLEVEL is true if this is the outermost BLOCK. */ | |
951 | ||
952 | static HOST_WIDE_INT | |
953 | account_used_vars_for_block (tree block, bool toplevel) | |
954 | { | |
955 | size_t i, j, old_sv_num, this_sv_num, new_sv_num; | |
956 | tree t; | |
957 | HOST_WIDE_INT size = 0; | |
958 | ||
959 | old_sv_num = toplevel ? 0 : stack_vars_num; | |
960 | ||
961 | /* Expand all variables at this level. */ | |
962 | for (t = BLOCK_VARS (block); t ; t = TREE_CHAIN (t)) | |
963 | if (TREE_USED (t)) | |
964 | size += expand_one_var (t, toplevel, false); | |
965 | ||
966 | this_sv_num = stack_vars_num; | |
967 | ||
968 | /* Expand all variables at containing levels. */ | |
969 | for (t = BLOCK_SUBBLOCKS (block); t ; t = BLOCK_CHAIN (t)) | |
970 | size += account_used_vars_for_block (t, false); | |
971 | ||
972 | /* Since we do not track exact variable lifetimes (which is not even | |
973 | possible for variables whose address escapes), we mirror the block | |
974 | tree in the interference graph. Here we cause all variables at this | |
975 | level, and all sublevels, to conflict. Do make certain that a | |
976 | variable conflicts with itself. */ | |
977 | if (old_sv_num < this_sv_num) | |
978 | { | |
979 | new_sv_num = stack_vars_num; | |
980 | resize_stack_vars_conflict (new_sv_num); | |
981 | ||
982 | for (i = old_sv_num; i < new_sv_num; ++i) | |
983 | for (j = i < this_sv_num ? i+1 : this_sv_num; j-- > old_sv_num ;) | |
984 | add_stack_var_conflict (i, j); | |
985 | } | |
986 | return size; | |
987 | } | |
988 | ||
989 | /* Prepare for expanding variables. */ | |
990 | static void | |
991 | init_vars_expansion (void) | |
992 | { | |
993 | tree t; | |
cb91fab0 JH |
994 | /* Set TREE_USED on all variables in the local_decls. */ |
995 | for (t = cfun->local_decls; t; t = TREE_CHAIN (t)) | |
ff28a94d JH |
996 | TREE_USED (TREE_VALUE (t)) = 1; |
997 | ||
998 | /* Clear TREE_USED on all variables associated with a block scope. */ | |
999 | clear_tree_used (DECL_INITIAL (current_function_decl)); | |
1000 | ||
1001 | /* Initialize local stack smashing state. */ | |
1002 | has_protected_decls = false; | |
1003 | has_short_buffer = false; | |
1004 | } | |
1005 | ||
1006 | /* Free up stack variable graph data. */ | |
1007 | static void | |
1008 | fini_vars_expansion (void) | |
1009 | { | |
1010 | XDELETEVEC (stack_vars); | |
1011 | XDELETEVEC (stack_vars_sorted); | |
1012 | XDELETEVEC (stack_vars_conflict); | |
1013 | stack_vars = NULL; | |
1014 | stack_vars_alloc = stack_vars_num = 0; | |
1015 | stack_vars_conflict = NULL; | |
1016 | stack_vars_conflict_alloc = 0; | |
1017 | } | |
1018 | ||
1019 | HOST_WIDE_INT | |
1020 | estimated_stack_frame_size (void) | |
1021 | { | |
1022 | HOST_WIDE_INT size = 0; | |
1023 | tree t, outer_block = DECL_INITIAL (current_function_decl); | |
1024 | ||
1025 | init_vars_expansion (); | |
1026 | ||
cb91fab0 | 1027 | /* At this point all variables on the local_decls with TREE_USED |
ff28a94d | 1028 | set are not associated with any block scope. Lay them out. */ |
cb91fab0 | 1029 | for (t = cfun->local_decls; t; t = TREE_CHAIN (t)) |
ff28a94d JH |
1030 | { |
1031 | tree var = TREE_VALUE (t); | |
1032 | ||
1033 | if (TREE_USED (var)) | |
1034 | size += expand_one_var (var, true, false); | |
1035 | TREE_USED (var) = 1; | |
1036 | } | |
1037 | size += account_used_vars_for_block (outer_block, true); | |
1038 | if (stack_vars_num > 0) | |
1039 | { | |
1040 | /* Due to the way alias sets work, no variables with non-conflicting | |
1041 | alias sets may be assigned the same address. Add conflicts to | |
1042 | reflect this. */ | |
1043 | add_alias_set_conflicts (); | |
1044 | ||
1045 | /* If stack protection is enabled, we don't share space between | |
1046 | vulnerable data and non-vulnerable data. */ | |
1047 | if (flag_stack_protect) | |
1048 | add_stack_protection_conflicts (); | |
1049 | ||
1050 | /* Now that we have collected all stack variables, and have computed a | |
1051 | minimal interference graph, attempt to save some stack space. */ | |
1052 | partition_stack_vars (); | |
1053 | if (dump_file) | |
1054 | dump_stack_var_partition (); | |
1055 | ||
1056 | size += account_stack_vars (); | |
1057 | fini_vars_expansion (); | |
1058 | } | |
1059 | return size; | |
1060 | } | |
1061 | ||
1f6d3a08 | 1062 | /* Expand all variables used in the function. */ |
727a31fa RH |
1063 | |
1064 | static void | |
1065 | expand_used_vars (void) | |
1066 | { | |
1f6d3a08 | 1067 | tree t, outer_block = DECL_INITIAL (current_function_decl); |
727a31fa | 1068 | |
1f6d3a08 RH |
1069 | /* Compute the phase of the stack frame for this function. */ |
1070 | { | |
1071 | int align = PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT; | |
1072 | int off = STARTING_FRAME_OFFSET % align; | |
1073 | frame_phase = off ? align - off : 0; | |
1074 | } | |
727a31fa | 1075 | |
ff28a94d | 1076 | init_vars_expansion (); |
7d69de61 | 1077 | |
cb91fab0 | 1078 | /* At this point all variables on the local_decls with TREE_USED |
1f6d3a08 | 1079 | set are not associated with any block scope. Lay them out. */ |
cb91fab0 | 1080 | for (t = cfun->local_decls; t; t = TREE_CHAIN (t)) |
1f6d3a08 RH |
1081 | { |
1082 | tree var = TREE_VALUE (t); | |
1083 | bool expand_now = false; | |
1084 | ||
1085 | /* We didn't set a block for static or extern because it's hard | |
1086 | to tell the difference between a global variable (re)declared | |
1087 | in a local scope, and one that's really declared there to | |
1088 | begin with. And it doesn't really matter much, since we're | |
1089 | not giving them stack space. Expand them now. */ | |
1090 | if (TREE_STATIC (var) || DECL_EXTERNAL (var)) | |
1091 | expand_now = true; | |
1092 | ||
1093 | /* Any variable that could have been hoisted into an SSA_NAME | |
1094 | will have been propagated anywhere the optimizers chose, | |
1095 | i.e. not confined to their original block. Allocate them | |
1096 | as if they were defined in the outermost scope. */ | |
1097 | else if (is_gimple_reg (var)) | |
1098 | expand_now = true; | |
1099 | ||
1100 | /* If the variable is not associated with any block, then it | |
1101 | was created by the optimizers, and could be live anywhere | |
1102 | in the function. */ | |
1103 | else if (TREE_USED (var)) | |
1104 | expand_now = true; | |
1105 | ||
1106 | /* Finally, mark all variables on the list as used. We'll use | |
1107 | this in a moment when we expand those associated with scopes. */ | |
1108 | TREE_USED (var) = 1; | |
1109 | ||
1110 | if (expand_now) | |
ff28a94d | 1111 | expand_one_var (var, true, true); |
1f6d3a08 | 1112 | } |
cb91fab0 | 1113 | cfun->local_decls = NULL_TREE; |
1f6d3a08 RH |
1114 | |
1115 | /* At this point, all variables within the block tree with TREE_USED | |
1116 | set are actually used by the optimized function. Lay them out. */ | |
1117 | expand_used_vars_for_block (outer_block, true); | |
1118 | ||
1119 | if (stack_vars_num > 0) | |
1120 | { | |
1121 | /* Due to the way alias sets work, no variables with non-conflicting | |
c22cacf3 | 1122 | alias sets may be assigned the same address. Add conflicts to |
1f6d3a08 RH |
1123 | reflect this. */ |
1124 | add_alias_set_conflicts (); | |
1125 | ||
c22cacf3 | 1126 | /* If stack protection is enabled, we don't share space between |
7d69de61 RH |
1127 | vulnerable data and non-vulnerable data. */ |
1128 | if (flag_stack_protect) | |
1129 | add_stack_protection_conflicts (); | |
1130 | ||
c22cacf3 | 1131 | /* Now that we have collected all stack variables, and have computed a |
1f6d3a08 RH |
1132 | minimal interference graph, attempt to save some stack space. */ |
1133 | partition_stack_vars (); | |
1134 | if (dump_file) | |
1135 | dump_stack_var_partition (); | |
7d69de61 RH |
1136 | } |
1137 | ||
1138 | /* There are several conditions under which we should create a | |
1139 | stack guard: protect-all, alloca used, protected decls present. */ | |
1140 | if (flag_stack_protect == 2 | |
1141 | || (flag_stack_protect | |
e3b5732b | 1142 | && (cfun->calls_alloca || has_protected_decls))) |
7d69de61 | 1143 | create_stack_guard (); |
1f6d3a08 | 1144 | |
7d69de61 RH |
1145 | /* Assign rtl to each variable based on these partitions. */ |
1146 | if (stack_vars_num > 0) | |
1147 | { | |
1148 | /* Reorder decls to be protected by iterating over the variables | |
1149 | array multiple times, and allocating out of each phase in turn. */ | |
c22cacf3 | 1150 | /* ??? We could probably integrate this into the qsort we did |
7d69de61 RH |
1151 | earlier, such that we naturally see these variables first, |
1152 | and thus naturally allocate things in the right order. */ | |
1153 | if (has_protected_decls) | |
1154 | { | |
1155 | /* Phase 1 contains only character arrays. */ | |
1156 | expand_stack_vars (stack_protect_decl_phase_1); | |
1157 | ||
1158 | /* Phase 2 contains other kinds of arrays. */ | |
1159 | if (flag_stack_protect == 2) | |
1160 | expand_stack_vars (stack_protect_decl_phase_2); | |
1161 | } | |
1162 | ||
1163 | expand_stack_vars (NULL); | |
1f6d3a08 | 1164 | |
ff28a94d | 1165 | fini_vars_expansion (); |
1f6d3a08 RH |
1166 | } |
1167 | ||
1168 | /* If the target requires that FRAME_OFFSET be aligned, do it. */ | |
1169 | if (STACK_ALIGNMENT_NEEDED) | |
1170 | { | |
1171 | HOST_WIDE_INT align = PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT; | |
1172 | if (!FRAME_GROWS_DOWNWARD) | |
1173 | frame_offset += align - 1; | |
1174 | frame_offset &= -align; | |
1175 | } | |
727a31fa RH |
1176 | } |
1177 | ||
1178 | ||
b7211528 SB |
1179 | /* If we need to produce a detailed dump, print the tree representation |
1180 | for STMT to the dump file. SINCE is the last RTX after which the RTL | |
1181 | generated for STMT should have been appended. */ | |
1182 | ||
1183 | static void | |
1184 | maybe_dump_rtl_for_tree_stmt (tree stmt, rtx since) | |
1185 | { | |
1186 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
1187 | { | |
1188 | fprintf (dump_file, "\n;; "); | |
1189 | print_generic_expr (dump_file, stmt, TDF_SLIM); | |
1190 | fprintf (dump_file, "\n"); | |
1191 | ||
1192 | print_rtl (dump_file, since ? NEXT_INSN (since) : since); | |
1193 | } | |
1194 | } | |
1195 | ||
8b11009b ZD |
1196 | /* Maps the blocks that do not contain tree labels to rtx labels. */ |
1197 | ||
1198 | static struct pointer_map_t *lab_rtx_for_bb; | |
1199 | ||
a9b77cd1 ZD |
1200 | /* Returns the label_rtx expression for a label starting basic block BB. */ |
1201 | ||
1202 | static rtx | |
1203 | label_rtx_for_bb (basic_block bb) | |
1204 | { | |
1205 | tree_stmt_iterator tsi; | |
1206 | tree lab, lab_stmt; | |
8b11009b | 1207 | void **elt; |
a9b77cd1 ZD |
1208 | |
1209 | if (bb->flags & BB_RTL) | |
1210 | return block_label (bb); | |
1211 | ||
8b11009b ZD |
1212 | elt = pointer_map_contains (lab_rtx_for_bb, bb); |
1213 | if (elt) | |
ae50c0cb | 1214 | return (rtx) *elt; |
8b11009b ZD |
1215 | |
1216 | /* Find the tree label if it is present. */ | |
1217 | ||
a9b77cd1 ZD |
1218 | for (tsi = tsi_start (bb_stmt_list (bb)); !tsi_end_p (tsi); tsi_next (&tsi)) |
1219 | { | |
1220 | lab_stmt = tsi_stmt (tsi); | |
1221 | if (TREE_CODE (lab_stmt) != LABEL_EXPR) | |
1222 | break; | |
1223 | ||
1224 | lab = LABEL_EXPR_LABEL (lab_stmt); | |
1225 | if (DECL_NONLOCAL (lab)) | |
1226 | break; | |
1227 | ||
1228 | return label_rtx (lab); | |
1229 | } | |
1230 | ||
8b11009b ZD |
1231 | elt = pointer_map_insert (lab_rtx_for_bb, bb); |
1232 | *elt = gen_label_rtx (); | |
ae50c0cb | 1233 | return (rtx) *elt; |
a9b77cd1 ZD |
1234 | } |
1235 | ||
80c7a9eb RH |
1236 | /* A subroutine of expand_gimple_basic_block. Expand one COND_EXPR. |
1237 | Returns a new basic block if we've terminated the current basic | |
1238 | block and created a new one. */ | |
1239 | ||
1240 | static basic_block | |
1241 | expand_gimple_cond_expr (basic_block bb, tree stmt) | |
1242 | { | |
1243 | basic_block new_bb, dest; | |
1244 | edge new_edge; | |
1245 | edge true_edge; | |
1246 | edge false_edge; | |
1247 | tree pred = COND_EXPR_COND (stmt); | |
b7211528 SB |
1248 | rtx last2, last; |
1249 | ||
a9b77cd1 ZD |
1250 | gcc_assert (COND_EXPR_THEN (stmt) == NULL_TREE); |
1251 | gcc_assert (COND_EXPR_ELSE (stmt) == NULL_TREE); | |
b7211528 | 1252 | last2 = last = get_last_insn (); |
80c7a9eb RH |
1253 | |
1254 | extract_true_false_edges_from_block (bb, &true_edge, &false_edge); | |
1255 | if (EXPR_LOCUS (stmt)) | |
1256 | { | |
55e092c4 JH |
1257 | set_curr_insn_source_location (*(EXPR_LOCUS (stmt))); |
1258 | set_curr_insn_block (TREE_BLOCK (stmt)); | |
80c7a9eb RH |
1259 | } |
1260 | ||
1261 | /* These flags have no purpose in RTL land. */ | |
1262 | true_edge->flags &= ~EDGE_TRUE_VALUE; | |
1263 | false_edge->flags &= ~EDGE_FALSE_VALUE; | |
1264 | ||
1265 | /* We can either have a pure conditional jump with one fallthru edge or | |
1266 | two-way jump that needs to be decomposed into two basic blocks. */ | |
a9b77cd1 | 1267 | if (false_edge->dest == bb->next_bb) |
80c7a9eb | 1268 | { |
a9b77cd1 | 1269 | jumpif (pred, label_rtx_for_bb (true_edge->dest)); |
10d22567 | 1270 | add_reg_br_prob_note (last, true_edge->probability); |
b7211528 | 1271 | maybe_dump_rtl_for_tree_stmt (stmt, last); |
a9b77cd1 | 1272 | if (true_edge->goto_locus) |
2d593c86 | 1273 | set_curr_insn_source_location (true_edge->goto_locus); |
a9b77cd1 | 1274 | false_edge->flags |= EDGE_FALLTHRU; |
80c7a9eb RH |
1275 | return NULL; |
1276 | } | |
a9b77cd1 | 1277 | if (true_edge->dest == bb->next_bb) |
80c7a9eb | 1278 | { |
a9b77cd1 | 1279 | jumpifnot (pred, label_rtx_for_bb (false_edge->dest)); |
10d22567 | 1280 | add_reg_br_prob_note (last, false_edge->probability); |
b7211528 | 1281 | maybe_dump_rtl_for_tree_stmt (stmt, last); |
a9b77cd1 | 1282 | if (false_edge->goto_locus) |
2d593c86 | 1283 | set_curr_insn_source_location (false_edge->goto_locus); |
a9b77cd1 | 1284 | true_edge->flags |= EDGE_FALLTHRU; |
80c7a9eb RH |
1285 | return NULL; |
1286 | } | |
80c7a9eb | 1287 | |
a9b77cd1 | 1288 | jumpif (pred, label_rtx_for_bb (true_edge->dest)); |
10d22567 | 1289 | add_reg_br_prob_note (last, true_edge->probability); |
80c7a9eb | 1290 | last = get_last_insn (); |
a9b77cd1 | 1291 | emit_jump (label_rtx_for_bb (false_edge->dest)); |
80c7a9eb RH |
1292 | |
1293 | BB_END (bb) = last; | |
1294 | if (BARRIER_P (BB_END (bb))) | |
1295 | BB_END (bb) = PREV_INSN (BB_END (bb)); | |
1296 | update_bb_for_insn (bb); | |
1297 | ||
1298 | new_bb = create_basic_block (NEXT_INSN (last), get_last_insn (), bb); | |
1299 | dest = false_edge->dest; | |
1300 | redirect_edge_succ (false_edge, new_bb); | |
1301 | false_edge->flags |= EDGE_FALLTHRU; | |
1302 | new_bb->count = false_edge->count; | |
1303 | new_bb->frequency = EDGE_FREQUENCY (false_edge); | |
1304 | new_edge = make_edge (new_bb, dest, 0); | |
1305 | new_edge->probability = REG_BR_PROB_BASE; | |
1306 | new_edge->count = new_bb->count; | |
1307 | if (BARRIER_P (BB_END (new_bb))) | |
1308 | BB_END (new_bb) = PREV_INSN (BB_END (new_bb)); | |
1309 | update_bb_for_insn (new_bb); | |
1310 | ||
b7211528 | 1311 | maybe_dump_rtl_for_tree_stmt (stmt, last2); |
c22cacf3 | 1312 | |
a9b77cd1 | 1313 | if (false_edge->goto_locus) |
2d593c86 | 1314 | set_curr_insn_source_location (false_edge->goto_locus); |
80c7a9eb RH |
1315 | |
1316 | return new_bb; | |
1317 | } | |
1318 | ||
1319 | /* A subroutine of expand_gimple_basic_block. Expand one CALL_EXPR | |
224e770b RH |
1320 | that has CALL_EXPR_TAILCALL set. Returns non-null if we actually |
1321 | generated a tail call (something that might be denied by the ABI | |
cea49550 RH |
1322 | rules governing the call; see calls.c). |
1323 | ||
1324 | Sets CAN_FALLTHRU if we generated a *conditional* tail call, and | |
1325 | can still reach the rest of BB. The case here is __builtin_sqrt, | |
1326 | where the NaN result goes through the external function (with a | |
1327 | tailcall) and the normal result happens via a sqrt instruction. */ | |
80c7a9eb RH |
1328 | |
1329 | static basic_block | |
cea49550 | 1330 | expand_gimple_tailcall (basic_block bb, tree stmt, bool *can_fallthru) |
80c7a9eb | 1331 | { |
b7211528 | 1332 | rtx last2, last; |
224e770b | 1333 | edge e; |
628f6a4e | 1334 | edge_iterator ei; |
224e770b RH |
1335 | int probability; |
1336 | gcov_type count; | |
80c7a9eb | 1337 | |
b7211528 SB |
1338 | last2 = last = get_last_insn (); |
1339 | ||
80c7a9eb RH |
1340 | expand_expr_stmt (stmt); |
1341 | ||
1342 | for (last = NEXT_INSN (last); last; last = NEXT_INSN (last)) | |
224e770b RH |
1343 | if (CALL_P (last) && SIBLING_CALL_P (last)) |
1344 | goto found; | |
80c7a9eb | 1345 | |
7315a949 | 1346 | maybe_dump_rtl_for_tree_stmt (stmt, last2); |
b7211528 | 1347 | |
cea49550 | 1348 | *can_fallthru = true; |
224e770b | 1349 | return NULL; |
80c7a9eb | 1350 | |
224e770b RH |
1351 | found: |
1352 | /* ??? Wouldn't it be better to just reset any pending stack adjust? | |
1353 | Any instructions emitted here are about to be deleted. */ | |
1354 | do_pending_stack_adjust (); | |
1355 | ||
1356 | /* Remove any non-eh, non-abnormal edges that don't go to exit. */ | |
1357 | /* ??? I.e. the fallthrough edge. HOWEVER! If there were to be | |
1358 | EH or abnormal edges, we shouldn't have created a tail call in | |
1359 | the first place. So it seems to me we should just be removing | |
1360 | all edges here, or redirecting the existing fallthru edge to | |
1361 | the exit block. */ | |
1362 | ||
224e770b RH |
1363 | probability = 0; |
1364 | count = 0; | |
224e770b | 1365 | |
628f6a4e BE |
1366 | for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); ) |
1367 | { | |
224e770b RH |
1368 | if (!(e->flags & (EDGE_ABNORMAL | EDGE_EH))) |
1369 | { | |
1370 | if (e->dest != EXIT_BLOCK_PTR) | |
80c7a9eb | 1371 | { |
224e770b RH |
1372 | e->dest->count -= e->count; |
1373 | e->dest->frequency -= EDGE_FREQUENCY (e); | |
1374 | if (e->dest->count < 0) | |
c22cacf3 | 1375 | e->dest->count = 0; |
224e770b | 1376 | if (e->dest->frequency < 0) |
c22cacf3 | 1377 | e->dest->frequency = 0; |
80c7a9eb | 1378 | } |
224e770b RH |
1379 | count += e->count; |
1380 | probability += e->probability; | |
1381 | remove_edge (e); | |
80c7a9eb | 1382 | } |
628f6a4e BE |
1383 | else |
1384 | ei_next (&ei); | |
80c7a9eb RH |
1385 | } |
1386 | ||
224e770b RH |
1387 | /* This is somewhat ugly: the call_expr expander often emits instructions |
1388 | after the sibcall (to perform the function return). These confuse the | |
12eff7b7 | 1389 | find_many_sub_basic_blocks code, so we need to get rid of these. */ |
224e770b | 1390 | last = NEXT_INSN (last); |
341c100f | 1391 | gcc_assert (BARRIER_P (last)); |
cea49550 RH |
1392 | |
1393 | *can_fallthru = false; | |
224e770b RH |
1394 | while (NEXT_INSN (last)) |
1395 | { | |
1396 | /* For instance an sqrt builtin expander expands if with | |
1397 | sibcall in the then and label for `else`. */ | |
1398 | if (LABEL_P (NEXT_INSN (last))) | |
cea49550 RH |
1399 | { |
1400 | *can_fallthru = true; | |
1401 | break; | |
1402 | } | |
224e770b RH |
1403 | delete_insn (NEXT_INSN (last)); |
1404 | } | |
1405 | ||
1406 | e = make_edge (bb, EXIT_BLOCK_PTR, EDGE_ABNORMAL | EDGE_SIBCALL); | |
1407 | e->probability += probability; | |
1408 | e->count += count; | |
1409 | BB_END (bb) = last; | |
1410 | update_bb_for_insn (bb); | |
1411 | ||
1412 | if (NEXT_INSN (last)) | |
1413 | { | |
1414 | bb = create_basic_block (NEXT_INSN (last), get_last_insn (), bb); | |
1415 | ||
1416 | last = BB_END (bb); | |
1417 | if (BARRIER_P (last)) | |
1418 | BB_END (bb) = PREV_INSN (last); | |
1419 | } | |
1420 | ||
b7211528 SB |
1421 | maybe_dump_rtl_for_tree_stmt (stmt, last2); |
1422 | ||
224e770b | 1423 | return bb; |
80c7a9eb RH |
1424 | } |
1425 | ||
242229bb JH |
1426 | /* Expand basic block BB from GIMPLE trees to RTL. */ |
1427 | ||
1428 | static basic_block | |
10d22567 | 1429 | expand_gimple_basic_block (basic_block bb) |
242229bb | 1430 | { |
7506e1cb ZD |
1431 | tree_stmt_iterator tsi; |
1432 | tree stmts = bb_stmt_list (bb); | |
242229bb JH |
1433 | tree stmt = NULL; |
1434 | rtx note, last; | |
1435 | edge e; | |
628f6a4e | 1436 | edge_iterator ei; |
8b11009b | 1437 | void **elt; |
242229bb JH |
1438 | |
1439 | if (dump_file) | |
1440 | { | |
b7211528 SB |
1441 | fprintf (dump_file, |
1442 | "\n;; Generating RTL for tree basic block %d\n", | |
1443 | bb->index); | |
242229bb JH |
1444 | } |
1445 | ||
7506e1cb | 1446 | bb->il.tree = NULL; |
5e2d947c JH |
1447 | init_rtl_bb_info (bb); |
1448 | bb->flags |= BB_RTL; | |
1449 | ||
a9b77cd1 ZD |
1450 | /* Remove the RETURN_EXPR if we may fall though to the exit |
1451 | instead. */ | |
1452 | tsi = tsi_last (stmts); | |
1453 | if (!tsi_end_p (tsi) | |
1454 | && TREE_CODE (tsi_stmt (tsi)) == RETURN_EXPR) | |
1455 | { | |
1456 | tree ret_stmt = tsi_stmt (tsi); | |
1457 | ||
1458 | gcc_assert (single_succ_p (bb)); | |
1459 | gcc_assert (single_succ (bb) == EXIT_BLOCK_PTR); | |
1460 | ||
1461 | if (bb->next_bb == EXIT_BLOCK_PTR | |
1462 | && !TREE_OPERAND (ret_stmt, 0)) | |
1463 | { | |
1464 | tsi_delink (&tsi); | |
1465 | single_succ_edge (bb)->flags |= EDGE_FALLTHRU; | |
1466 | } | |
1467 | } | |
1468 | ||
7506e1cb ZD |
1469 | tsi = tsi_start (stmts); |
1470 | if (!tsi_end_p (tsi)) | |
8b11009b ZD |
1471 | { |
1472 | stmt = tsi_stmt (tsi); | |
1473 | if (TREE_CODE (stmt) != LABEL_EXPR) | |
1474 | stmt = NULL_TREE; | |
1475 | } | |
242229bb | 1476 | |
8b11009b ZD |
1477 | elt = pointer_map_contains (lab_rtx_for_bb, bb); |
1478 | ||
1479 | if (stmt || elt) | |
242229bb JH |
1480 | { |
1481 | last = get_last_insn (); | |
1482 | ||
8b11009b ZD |
1483 | if (stmt) |
1484 | { | |
1485 | expand_expr_stmt (stmt); | |
1486 | tsi_next (&tsi); | |
1487 | } | |
1488 | ||
1489 | if (elt) | |
ae50c0cb | 1490 | emit_label ((rtx) *elt); |
242229bb | 1491 | |
caf93cb0 | 1492 | /* Java emits line number notes in the top of labels. |
c22cacf3 | 1493 | ??? Make this go away once line number notes are obsoleted. */ |
242229bb | 1494 | BB_HEAD (bb) = NEXT_INSN (last); |
4b4bf941 | 1495 | if (NOTE_P (BB_HEAD (bb))) |
242229bb | 1496 | BB_HEAD (bb) = NEXT_INSN (BB_HEAD (bb)); |
242229bb | 1497 | note = emit_note_after (NOTE_INSN_BASIC_BLOCK, BB_HEAD (bb)); |
b7211528 SB |
1498 | |
1499 | maybe_dump_rtl_for_tree_stmt (stmt, last); | |
242229bb JH |
1500 | } |
1501 | else | |
1502 | note = BB_HEAD (bb) = emit_note (NOTE_INSN_BASIC_BLOCK); | |
1503 | ||
1504 | NOTE_BASIC_BLOCK (note) = bb; | |
1505 | ||
628f6a4e | 1506 | for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); ) |
242229bb | 1507 | { |
242229bb JH |
1508 | /* Clear EDGE_EXECUTABLE. This flag is never used in the backend. */ |
1509 | e->flags &= ~EDGE_EXECUTABLE; | |
1510 | ||
1511 | /* At the moment not all abnormal edges match the RTL representation. | |
c22cacf3 MS |
1512 | It is safe to remove them here as find_many_sub_basic_blocks will |
1513 | rediscover them. In the future we should get this fixed properly. */ | |
242229bb JH |
1514 | if (e->flags & EDGE_ABNORMAL) |
1515 | remove_edge (e); | |
628f6a4e BE |
1516 | else |
1517 | ei_next (&ei); | |
242229bb JH |
1518 | } |
1519 | ||
7506e1cb | 1520 | for (; !tsi_end_p (tsi); tsi_next (&tsi)) |
242229bb | 1521 | { |
7506e1cb | 1522 | tree stmt = tsi_stmt (tsi); |
cea49550 | 1523 | basic_block new_bb; |
242229bb JH |
1524 | |
1525 | if (!stmt) | |
1526 | continue; | |
1527 | ||
1528 | /* Expand this statement, then evaluate the resulting RTL and | |
1529 | fixup the CFG accordingly. */ | |
80c7a9eb | 1530 | if (TREE_CODE (stmt) == COND_EXPR) |
cea49550 RH |
1531 | { |
1532 | new_bb = expand_gimple_cond_expr (bb, stmt); | |
1533 | if (new_bb) | |
1534 | return new_bb; | |
1535 | } | |
80c7a9eb | 1536 | else |
242229bb | 1537 | { |
80c7a9eb | 1538 | tree call = get_call_expr_in (stmt); |
4437b50d JH |
1539 | int region; |
1540 | /* For the benefit of calls.c, converting all this to rtl, | |
1541 | we need to record the call expression, not just the outer | |
1542 | modify statement. */ | |
079a182e JH |
1543 | if (call && call != stmt) |
1544 | { | |
1545 | if ((region = lookup_stmt_eh_region (stmt)) > 0) | |
1546 | add_stmt_to_eh_region (call, region); | |
1547 | gimple_duplicate_stmt_histograms (cfun, call, cfun, stmt); | |
1548 | } | |
80c7a9eb | 1549 | if (call && CALL_EXPR_TAILCALL (call)) |
cea49550 RH |
1550 | { |
1551 | bool can_fallthru; | |
1552 | new_bb = expand_gimple_tailcall (bb, stmt, &can_fallthru); | |
1553 | if (new_bb) | |
1554 | { | |
1555 | if (can_fallthru) | |
1556 | bb = new_bb; | |
1557 | else | |
1558 | return new_bb; | |
1559 | } | |
1560 | } | |
80c7a9eb | 1561 | else |
b7211528 SB |
1562 | { |
1563 | last = get_last_insn (); | |
1564 | expand_expr_stmt (stmt); | |
1565 | maybe_dump_rtl_for_tree_stmt (stmt, last); | |
1566 | } | |
242229bb JH |
1567 | } |
1568 | } | |
1569 | ||
a9b77cd1 ZD |
1570 | /* Expand implicit goto. */ |
1571 | FOR_EACH_EDGE (e, ei, bb->succs) | |
1572 | { | |
1573 | if (e->flags & EDGE_FALLTHRU) | |
1574 | break; | |
1575 | } | |
1576 | ||
1577 | if (e && e->dest != bb->next_bb) | |
1578 | { | |
1579 | emit_jump (label_rtx_for_bb (e->dest)); | |
1580 | if (e->goto_locus) | |
2d593c86 | 1581 | set_curr_insn_source_location (e->goto_locus); |
a9b77cd1 ZD |
1582 | e->flags &= ~EDGE_FALLTHRU; |
1583 | } | |
1584 | ||
242229bb JH |
1585 | do_pending_stack_adjust (); |
1586 | ||
3f117656 | 1587 | /* Find the block tail. The last insn in the block is the insn |
242229bb JH |
1588 | before a barrier and/or table jump insn. */ |
1589 | last = get_last_insn (); | |
4b4bf941 | 1590 | if (BARRIER_P (last)) |
242229bb JH |
1591 | last = PREV_INSN (last); |
1592 | if (JUMP_TABLE_DATA_P (last)) | |
1593 | last = PREV_INSN (PREV_INSN (last)); | |
1594 | BB_END (bb) = last; | |
caf93cb0 | 1595 | |
242229bb | 1596 | update_bb_for_insn (bb); |
80c7a9eb | 1597 | |
242229bb JH |
1598 | return bb; |
1599 | } | |
1600 | ||
1601 | ||
1602 | /* Create a basic block for initialization code. */ | |
1603 | ||
1604 | static basic_block | |
1605 | construct_init_block (void) | |
1606 | { | |
1607 | basic_block init_block, first_block; | |
fd44f634 JH |
1608 | edge e = NULL; |
1609 | int flags; | |
275a4187 | 1610 | |
fd44f634 JH |
1611 | /* Multiple entry points not supported yet. */ |
1612 | gcc_assert (EDGE_COUNT (ENTRY_BLOCK_PTR->succs) == 1); | |
5e2d947c JH |
1613 | init_rtl_bb_info (ENTRY_BLOCK_PTR); |
1614 | init_rtl_bb_info (EXIT_BLOCK_PTR); | |
1615 | ENTRY_BLOCK_PTR->flags |= BB_RTL; | |
1616 | EXIT_BLOCK_PTR->flags |= BB_RTL; | |
242229bb | 1617 | |
fd44f634 | 1618 | e = EDGE_SUCC (ENTRY_BLOCK_PTR, 0); |
275a4187 | 1619 | |
fd44f634 JH |
1620 | /* When entry edge points to first basic block, we don't need jump, |
1621 | otherwise we have to jump into proper target. */ | |
1622 | if (e && e->dest != ENTRY_BLOCK_PTR->next_bb) | |
1623 | { | |
1624 | tree label = tree_block_label (e->dest); | |
1625 | ||
1626 | emit_jump (label_rtx (label)); | |
1627 | flags = 0; | |
275a4187 | 1628 | } |
fd44f634 JH |
1629 | else |
1630 | flags = EDGE_FALLTHRU; | |
242229bb JH |
1631 | |
1632 | init_block = create_basic_block (NEXT_INSN (get_insns ()), | |
1633 | get_last_insn (), | |
1634 | ENTRY_BLOCK_PTR); | |
1635 | init_block->frequency = ENTRY_BLOCK_PTR->frequency; | |
1636 | init_block->count = ENTRY_BLOCK_PTR->count; | |
1637 | if (e) | |
1638 | { | |
1639 | first_block = e->dest; | |
1640 | redirect_edge_succ (e, init_block); | |
fd44f634 | 1641 | e = make_edge (init_block, first_block, flags); |
242229bb JH |
1642 | } |
1643 | else | |
1644 | e = make_edge (init_block, EXIT_BLOCK_PTR, EDGE_FALLTHRU); | |
1645 | e->probability = REG_BR_PROB_BASE; | |
1646 | e->count = ENTRY_BLOCK_PTR->count; | |
1647 | ||
1648 | update_bb_for_insn (init_block); | |
1649 | return init_block; | |
1650 | } | |
1651 | ||
55e092c4 JH |
1652 | /* For each lexical block, set BLOCK_NUMBER to the depth at which it is |
1653 | found in the block tree. */ | |
1654 | ||
1655 | static void | |
1656 | set_block_levels (tree block, int level) | |
1657 | { | |
1658 | while (block) | |
1659 | { | |
1660 | BLOCK_NUMBER (block) = level; | |
1661 | set_block_levels (BLOCK_SUBBLOCKS (block), level + 1); | |
1662 | block = BLOCK_CHAIN (block); | |
1663 | } | |
1664 | } | |
242229bb JH |
1665 | |
1666 | /* Create a block containing landing pads and similar stuff. */ | |
1667 | ||
1668 | static void | |
1669 | construct_exit_block (void) | |
1670 | { | |
1671 | rtx head = get_last_insn (); | |
1672 | rtx end; | |
1673 | basic_block exit_block; | |
628f6a4e BE |
1674 | edge e, e2; |
1675 | unsigned ix; | |
1676 | edge_iterator ei; | |
071a42f9 | 1677 | rtx orig_end = BB_END (EXIT_BLOCK_PTR->prev_bb); |
242229bb | 1678 | |
caf93cb0 | 1679 | /* Make sure the locus is set to the end of the function, so that |
242229bb | 1680 | epilogue line numbers and warnings are set properly. */ |
6773e15f | 1681 | if (cfun->function_end_locus != UNKNOWN_LOCATION) |
242229bb JH |
1682 | input_location = cfun->function_end_locus; |
1683 | ||
1684 | /* The following insns belong to the top scope. */ | |
55e092c4 | 1685 | set_curr_insn_block (DECL_INITIAL (current_function_decl)); |
242229bb | 1686 | |
242229bb JH |
1687 | /* Generate rtl for function exit. */ |
1688 | expand_function_end (); | |
1689 | ||
1690 | end = get_last_insn (); | |
1691 | if (head == end) | |
1692 | return; | |
071a42f9 JH |
1693 | /* While emitting the function end we could move end of the last basic block. |
1694 | */ | |
1695 | BB_END (EXIT_BLOCK_PTR->prev_bb) = orig_end; | |
4b4bf941 | 1696 | while (NEXT_INSN (head) && NOTE_P (NEXT_INSN (head))) |
242229bb | 1697 | head = NEXT_INSN (head); |
80c7a9eb RH |
1698 | exit_block = create_basic_block (NEXT_INSN (head), end, |
1699 | EXIT_BLOCK_PTR->prev_bb); | |
242229bb JH |
1700 | exit_block->frequency = EXIT_BLOCK_PTR->frequency; |
1701 | exit_block->count = EXIT_BLOCK_PTR->count; | |
628f6a4e BE |
1702 | |
1703 | ix = 0; | |
1704 | while (ix < EDGE_COUNT (EXIT_BLOCK_PTR->preds)) | |
242229bb | 1705 | { |
8fb790fd | 1706 | e = EDGE_PRED (EXIT_BLOCK_PTR, ix); |
242229bb | 1707 | if (!(e->flags & EDGE_ABNORMAL)) |
628f6a4e BE |
1708 | redirect_edge_succ (e, exit_block); |
1709 | else | |
1710 | ix++; | |
242229bb | 1711 | } |
628f6a4e | 1712 | |
242229bb JH |
1713 | e = make_edge (exit_block, EXIT_BLOCK_PTR, EDGE_FALLTHRU); |
1714 | e->probability = REG_BR_PROB_BASE; | |
1715 | e->count = EXIT_BLOCK_PTR->count; | |
628f6a4e | 1716 | FOR_EACH_EDGE (e2, ei, EXIT_BLOCK_PTR->preds) |
242229bb JH |
1717 | if (e2 != e) |
1718 | { | |
c22cacf3 | 1719 | e->count -= e2->count; |
242229bb JH |
1720 | exit_block->count -= e2->count; |
1721 | exit_block->frequency -= EDGE_FREQUENCY (e2); | |
1722 | } | |
1723 | if (e->count < 0) | |
1724 | e->count = 0; | |
1725 | if (exit_block->count < 0) | |
1726 | exit_block->count = 0; | |
1727 | if (exit_block->frequency < 0) | |
1728 | exit_block->frequency = 0; | |
1729 | update_bb_for_insn (exit_block); | |
1730 | } | |
1731 | ||
c22cacf3 | 1732 | /* Helper function for discover_nonconstant_array_refs. |
a1b23b2f UW |
1733 | Look for ARRAY_REF nodes with non-constant indexes and mark them |
1734 | addressable. */ | |
1735 | ||
1736 | static tree | |
1737 | discover_nonconstant_array_refs_r (tree * tp, int *walk_subtrees, | |
1738 | void *data ATTRIBUTE_UNUSED) | |
1739 | { | |
1740 | tree t = *tp; | |
1741 | ||
1742 | if (IS_TYPE_OR_DECL_P (t)) | |
1743 | *walk_subtrees = 0; | |
1744 | else if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF) | |
1745 | { | |
1746 | while (((TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF) | |
1747 | && is_gimple_min_invariant (TREE_OPERAND (t, 1)) | |
1748 | && (!TREE_OPERAND (t, 2) | |
1749 | || is_gimple_min_invariant (TREE_OPERAND (t, 2)))) | |
1750 | || (TREE_CODE (t) == COMPONENT_REF | |
1751 | && (!TREE_OPERAND (t,2) | |
1752 | || is_gimple_min_invariant (TREE_OPERAND (t, 2)))) | |
1753 | || TREE_CODE (t) == BIT_FIELD_REF | |
1754 | || TREE_CODE (t) == REALPART_EXPR | |
1755 | || TREE_CODE (t) == IMAGPART_EXPR | |
1756 | || TREE_CODE (t) == VIEW_CONVERT_EXPR | |
1043771b | 1757 | || CONVERT_EXPR_P (t)) |
a1b23b2f UW |
1758 | t = TREE_OPERAND (t, 0); |
1759 | ||
1760 | if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF) | |
1761 | { | |
1762 | t = get_base_address (t); | |
1763 | if (t && DECL_P (t)) | |
1764 | TREE_ADDRESSABLE (t) = 1; | |
1765 | } | |
1766 | ||
1767 | *walk_subtrees = 0; | |
1768 | } | |
1769 | ||
1770 | return NULL_TREE; | |
1771 | } | |
1772 | ||
1773 | /* RTL expansion is not able to compile array references with variable | |
1774 | offsets for arrays stored in single register. Discover such | |
1775 | expressions and mark variables as addressable to avoid this | |
1776 | scenario. */ | |
1777 | ||
1778 | static void | |
1779 | discover_nonconstant_array_refs (void) | |
1780 | { | |
1781 | basic_block bb; | |
1782 | block_stmt_iterator bsi; | |
1783 | ||
1784 | FOR_EACH_BB (bb) | |
1785 | { | |
1786 | for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi)) | |
1787 | walk_tree (bsi_stmt_ptr (bsi), discover_nonconstant_array_refs_r, | |
1788 | NULL , NULL); | |
1789 | } | |
1790 | } | |
1791 | ||
242229bb JH |
1792 | /* Translate the intermediate representation contained in the CFG |
1793 | from GIMPLE trees to RTL. | |
1794 | ||
1795 | We do conversion per basic block and preserve/update the tree CFG. | |
1796 | This implies we have to do some magic as the CFG can simultaneously | |
1797 | consist of basic blocks containing RTL and GIMPLE trees. This can | |
61ada8ae | 1798 | confuse the CFG hooks, so be careful to not manipulate CFG during |
242229bb JH |
1799 | the expansion. */ |
1800 | ||
c2924966 | 1801 | static unsigned int |
242229bb JH |
1802 | tree_expand_cfg (void) |
1803 | { | |
1804 | basic_block bb, init_block; | |
1805 | sbitmap blocks; | |
0ef90296 ZD |
1806 | edge_iterator ei; |
1807 | edge e; | |
242229bb | 1808 | |
4586b4ca SB |
1809 | /* Some backends want to know that we are expanding to RTL. */ |
1810 | currently_expanding_to_rtl = 1; | |
1811 | ||
55e092c4 JH |
1812 | insn_locators_alloc (); |
1813 | if (!DECL_BUILT_IN (current_function_decl)) | |
1814 | set_curr_insn_source_location (DECL_SOURCE_LOCATION (current_function_decl)); | |
1815 | set_curr_insn_block (DECL_INITIAL (current_function_decl)); | |
1816 | prologue_locator = curr_insn_locator (); | |
1817 | ||
1818 | /* Make sure first insn is a note even if we don't want linenums. | |
1819 | This makes sure the first insn will never be deleted. | |
1820 | Also, final expects a note to appear there. */ | |
1821 | emit_note (NOTE_INSN_DELETED); | |
6429e3be | 1822 | |
a1b23b2f UW |
1823 | /* Mark arrays indexed with non-constant indices with TREE_ADDRESSABLE. */ |
1824 | discover_nonconstant_array_refs (); | |
1825 | ||
e41b2a33 | 1826 | targetm.expand_to_rtl_hook (); |
cb91fab0 JH |
1827 | crtl->stack_alignment_needed = STACK_BOUNDARY; |
1828 | crtl->preferred_stack_boundary = STACK_BOUNDARY; | |
1829 | cfun->cfg->max_jumptable_ents = 0; | |
1830 | ||
e41b2a33 | 1831 | |
727a31fa | 1832 | /* Expand the variables recorded during gimple lowering. */ |
242229bb JH |
1833 | expand_used_vars (); |
1834 | ||
7d69de61 RH |
1835 | /* Honor stack protection warnings. */ |
1836 | if (warn_stack_protect) | |
1837 | { | |
e3b5732b | 1838 | if (cfun->calls_alloca) |
c5409249 MLI |
1839 | warning (OPT_Wstack_protector, |
1840 | "not protecting local variables: variable length buffer"); | |
cb91fab0 | 1841 | if (has_short_buffer && !crtl->stack_protect_guard) |
c5409249 MLI |
1842 | warning (OPT_Wstack_protector, |
1843 | "not protecting function: no buffer at least %d bytes long", | |
7d69de61 RH |
1844 | (int) PARAM_VALUE (PARAM_SSP_BUFFER_SIZE)); |
1845 | } | |
1846 | ||
242229bb | 1847 | /* Set up parameters and prepare for return, for the function. */ |
b79c5284 | 1848 | expand_function_start (current_function_decl); |
242229bb JH |
1849 | |
1850 | /* If this function is `main', emit a call to `__main' | |
1851 | to run global initializers, etc. */ | |
1852 | if (DECL_NAME (current_function_decl) | |
1853 | && MAIN_NAME_P (DECL_NAME (current_function_decl)) | |
1854 | && DECL_FILE_SCOPE_P (current_function_decl)) | |
1855 | expand_main_function (); | |
1856 | ||
7d69de61 RH |
1857 | /* Initialize the stack_protect_guard field. This must happen after the |
1858 | call to __main (if any) so that the external decl is initialized. */ | |
cb91fab0 | 1859 | if (crtl->stack_protect_guard) |
7d69de61 RH |
1860 | stack_protect_prologue (); |
1861 | ||
3fbd86b1 | 1862 | /* Register rtl specific functions for cfg. */ |
242229bb JH |
1863 | rtl_register_cfg_hooks (); |
1864 | ||
1865 | init_block = construct_init_block (); | |
1866 | ||
0ef90296 | 1867 | /* Clear EDGE_EXECUTABLE on the entry edge(s). It is cleaned from the |
2a8a8292 | 1868 | remaining edges in expand_gimple_basic_block. */ |
0ef90296 ZD |
1869 | FOR_EACH_EDGE (e, ei, ENTRY_BLOCK_PTR->succs) |
1870 | e->flags &= ~EDGE_EXECUTABLE; | |
1871 | ||
8b11009b | 1872 | lab_rtx_for_bb = pointer_map_create (); |
242229bb | 1873 | FOR_BB_BETWEEN (bb, init_block->next_bb, EXIT_BLOCK_PTR, next_bb) |
10d22567 | 1874 | bb = expand_gimple_basic_block (bb); |
8b11009b | 1875 | pointer_map_destroy (lab_rtx_for_bb); |
cb91fab0 | 1876 | free_histograms (); |
242229bb JH |
1877 | |
1878 | construct_exit_block (); | |
55e092c4 JH |
1879 | set_curr_insn_block (DECL_INITIAL (current_function_decl)); |
1880 | insn_locators_finalize (); | |
242229bb | 1881 | |
4586b4ca SB |
1882 | /* We're done expanding trees to RTL. */ |
1883 | currently_expanding_to_rtl = 0; | |
1884 | ||
e8a2a782 | 1885 | /* Convert tree EH labels to RTL EH labels and zap the tree EH table. */ |
242229bb | 1886 | convert_from_eh_region_ranges (); |
e8a2a782 | 1887 | set_eh_throw_stmt_table (cfun, NULL); |
242229bb JH |
1888 | |
1889 | rebuild_jump_labels (get_insns ()); | |
1890 | find_exception_handler_labels (); | |
1891 | ||
1892 | blocks = sbitmap_alloc (last_basic_block); | |
1893 | sbitmap_ones (blocks); | |
1894 | find_many_sub_basic_blocks (blocks); | |
25cd19de | 1895 | purge_all_dead_edges (); |
242229bb JH |
1896 | sbitmap_free (blocks); |
1897 | ||
1898 | compact_blocks (); | |
1899 | #ifdef ENABLE_CHECKING | |
62e5bf5d | 1900 | verify_flow_info (); |
242229bb | 1901 | #endif |
9f8628ba PB |
1902 | |
1903 | /* There's no need to defer outputting this function any more; we | |
1904 | know we want to output it. */ | |
1905 | DECL_DEFER_OUTPUT (current_function_decl) = 0; | |
1906 | ||
1907 | /* Now that we're done expanding trees to RTL, we shouldn't have any | |
1908 | more CONCATs anywhere. */ | |
1909 | generating_concat_p = 0; | |
1910 | ||
b7211528 SB |
1911 | if (dump_file) |
1912 | { | |
1913 | fprintf (dump_file, | |
1914 | "\n\n;;\n;; Full RTL generated for this function:\n;;\n"); | |
1915 | /* And the pass manager will dump RTL for us. */ | |
1916 | } | |
ef330312 PB |
1917 | |
1918 | /* If we're emitting a nested function, make sure its parent gets | |
1919 | emitted as well. Doing otherwise confuses debug info. */ | |
c22cacf3 | 1920 | { |
ef330312 PB |
1921 | tree parent; |
1922 | for (parent = DECL_CONTEXT (current_function_decl); | |
c22cacf3 MS |
1923 | parent != NULL_TREE; |
1924 | parent = get_containing_scope (parent)) | |
ef330312 | 1925 | if (TREE_CODE (parent) == FUNCTION_DECL) |
c22cacf3 | 1926 | TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (parent)) = 1; |
ef330312 | 1927 | } |
c22cacf3 | 1928 | |
ef330312 PB |
1929 | /* We are now committed to emitting code for this function. Do any |
1930 | preparation, such as emitting abstract debug info for the inline | |
1931 | before it gets mangled by optimization. */ | |
1932 | if (cgraph_function_possibly_inlined_p (current_function_decl)) | |
1933 | (*debug_hooks->outlining_inline_function) (current_function_decl); | |
1934 | ||
1935 | TREE_ASM_WRITTEN (current_function_decl) = 1; | |
4bb1e037 AP |
1936 | |
1937 | /* After expanding, the return labels are no longer needed. */ | |
1938 | return_label = NULL; | |
1939 | naked_return_label = NULL; | |
55e092c4 JH |
1940 | /* Tag the blocks with a depth number so that change_scope can find |
1941 | the common parent easily. */ | |
1942 | set_block_levels (DECL_INITIAL (cfun->decl), 0); | |
c2924966 | 1943 | return 0; |
242229bb JH |
1944 | } |
1945 | ||
e3b5732b | 1946 | struct rtl_opt_pass pass_expand = |
242229bb | 1947 | { |
8ddbbcae | 1948 | { |
e3b5732b | 1949 | RTL_PASS, |
c22cacf3 | 1950 | "expand", /* name */ |
242229bb | 1951 | NULL, /* gate */ |
c22cacf3 | 1952 | tree_expand_cfg, /* execute */ |
242229bb JH |
1953 | NULL, /* sub */ |
1954 | NULL, /* next */ | |
1955 | 0, /* static_pass_number */ | |
c22cacf3 | 1956 | TV_EXPAND, /* tv_id */ |
242229bb JH |
1957 | /* ??? If TER is enabled, we actually receive GENERIC. */ |
1958 | PROP_gimple_leh | PROP_cfg, /* properties_required */ | |
1959 | PROP_rtl, /* properties_provided */ | |
bbbe4e7b | 1960 | PROP_trees, /* properties_destroyed */ |
242229bb | 1961 | 0, /* todo_flags_start */ |
ef330312 | 1962 | TODO_dump_func, /* todo_flags_finish */ |
8ddbbcae | 1963 | } |
242229bb | 1964 | }; |