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