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