Mercurial > hg > CbC > CbC_gcc
annotate gcc/cfgexpand.c @ 22:0eb6cac880f0
add cbc example of quicksort.
author | kent <kent@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Tue, 13 Oct 2009 17:15:58 +0900 |
parents | 959d4c8c8abc |
children | 9907f3135723 |
rev | line source |
---|---|
0 | 1 /* A pass for lowering trees to RTL. |
2 Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009 | |
3 Free Software Foundation, Inc. | |
4 | |
5 This file is part of GCC. | |
6 | |
7 GCC is free software; you can redistribute it and/or modify | |
8 it under the terms of the GNU General Public License as published by | |
9 the Free Software Foundation; either version 3, or (at your option) | |
10 any later version. | |
11 | |
12 GCC is distributed in the hope that it will be useful, | |
13 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 GNU General Public License for more details. | |
16 | |
17 You should have received a copy of the GNU General Public License | |
18 along with GCC; see the file COPYING3. If not see | |
19 <http://www.gnu.org/licenses/>. */ | |
20 | |
21 #include "config.h" | |
22 #include "system.h" | |
23 #include "coretypes.h" | |
24 #include "tm.h" | |
25 #include "tree.h" | |
26 #include "rtl.h" | |
27 #include "tm_p.h" | |
28 #include "basic-block.h" | |
29 #include "function.h" | |
30 #include "expr.h" | |
31 #include "langhooks.h" | |
32 #include "tree-flow.h" | |
33 #include "timevar.h" | |
34 #include "tree-dump.h" | |
35 #include "tree-pass.h" | |
36 #include "except.h" | |
37 #include "flags.h" | |
38 #include "diagnostic.h" | |
39 #include "toplev.h" | |
40 #include "debug.h" | |
41 #include "params.h" | |
42 #include "tree-inline.h" | |
43 #include "value-prof.h" | |
44 #include "target.h" | |
5
a4c410aa4714
modifing gimple.[ch], cfgexpand.c
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
45 #ifndef noCbC |
a4c410aa4714
modifing gimple.[ch], cfgexpand.c
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
46 #include "cbc-tree.h" |
a4c410aa4714
modifing gimple.[ch], cfgexpand.c
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
47 #endif |
0 | 48 |
49 | |
50 /* Return an expression tree corresponding to the RHS of GIMPLE | |
51 statement STMT. */ | |
52 | |
53 tree | |
54 gimple_assign_rhs_to_tree (gimple stmt) | |
55 { | |
56 tree t; | |
57 enum gimple_rhs_class grhs_class; | |
58 | |
59 grhs_class = get_gimple_rhs_class (gimple_expr_code (stmt)); | |
60 | |
61 if (grhs_class == GIMPLE_BINARY_RHS) | |
62 t = build2 (gimple_assign_rhs_code (stmt), | |
63 TREE_TYPE (gimple_assign_lhs (stmt)), | |
64 gimple_assign_rhs1 (stmt), | |
65 gimple_assign_rhs2 (stmt)); | |
66 else if (grhs_class == GIMPLE_UNARY_RHS) | |
67 t = build1 (gimple_assign_rhs_code (stmt), | |
68 TREE_TYPE (gimple_assign_lhs (stmt)), | |
69 gimple_assign_rhs1 (stmt)); | |
70 else if (grhs_class == GIMPLE_SINGLE_RHS) | |
71 t = gimple_assign_rhs1 (stmt); | |
72 else | |
73 gcc_unreachable (); | |
74 | |
75 return t; | |
76 } | |
77 | |
78 /* Return an expression tree corresponding to the PREDICATE of GIMPLE_COND | |
79 statement STMT. */ | |
80 | |
81 static tree | |
82 gimple_cond_pred_to_tree (gimple stmt) | |
83 { | |
84 return build2 (gimple_cond_code (stmt), boolean_type_node, | |
85 gimple_cond_lhs (stmt), gimple_cond_rhs (stmt)); | |
86 } | |
87 | |
88 /* Helper for gimple_to_tree. Set EXPR_LOCATION for every expression | |
89 inside *TP. DATA is the location to set. */ | |
90 | |
91 static tree | |
92 set_expr_location_r (tree *tp, int *ws ATTRIBUTE_UNUSED, void *data) | |
93 { | |
94 location_t *loc = (location_t *) data; | |
95 if (EXPR_P (*tp)) | |
96 SET_EXPR_LOCATION (*tp, *loc); | |
97 | |
98 return NULL_TREE; | |
99 } | |
100 | |
101 | |
102 /* RTL expansion has traditionally been done on trees, so the | |
103 transition to doing it on GIMPLE tuples is very invasive to the RTL | |
104 expander. To facilitate the transition, this function takes a | |
105 GIMPLE tuple STMT and returns the same statement in the form of a | |
106 tree. */ | |
107 | |
108 static tree | |
109 gimple_to_tree (gimple stmt) | |
110 { | |
111 tree t; | |
112 int rn; | |
113 tree_ann_common_t ann; | |
114 location_t loc; | |
115 | |
116 switch (gimple_code (stmt)) | |
117 { | |
118 case GIMPLE_ASSIGN: | |
119 { | |
120 tree lhs = gimple_assign_lhs (stmt); | |
121 | |
122 t = gimple_assign_rhs_to_tree (stmt); | |
123 t = build2 (MODIFY_EXPR, TREE_TYPE (lhs), lhs, t); | |
124 if (gimple_assign_nontemporal_move_p (stmt)) | |
125 MOVE_NONTEMPORAL (t) = true; | |
126 } | |
127 break; | |
128 | |
129 case GIMPLE_COND: | |
130 t = gimple_cond_pred_to_tree (stmt); | |
131 t = build3 (COND_EXPR, void_type_node, t, NULL_TREE, NULL_TREE); | |
132 break; | |
133 | |
134 case GIMPLE_GOTO: | |
135 t = build1 (GOTO_EXPR, void_type_node, gimple_goto_dest (stmt)); | |
136 break; | |
137 | |
138 case GIMPLE_LABEL: | |
139 t = build1 (LABEL_EXPR, void_type_node, gimple_label_label (stmt)); | |
140 break; | |
141 | |
142 case GIMPLE_RETURN: | |
143 { | |
144 tree retval = gimple_return_retval (stmt); | |
145 | |
146 if (retval && retval != error_mark_node) | |
147 { | |
148 tree result = DECL_RESULT (current_function_decl); | |
149 | |
150 /* If we are not returning the current function's RESULT_DECL, | |
151 build an assignment to it. */ | |
152 if (retval != result) | |
153 { | |
154 /* I believe that a function's RESULT_DECL is unique. */ | |
155 gcc_assert (TREE_CODE (retval) != RESULT_DECL); | |
156 | |
157 retval = build2 (MODIFY_EXPR, TREE_TYPE (result), | |
158 result, retval); | |
159 } | |
160 } | |
161 t = build1 (RETURN_EXPR, void_type_node, retval); | |
162 } | |
163 break; | |
164 | |
165 case GIMPLE_ASM: | |
166 { | |
167 size_t i, n; | |
168 tree out, in, cl; | |
169 const char *s; | |
170 | |
171 out = NULL_TREE; | |
172 n = gimple_asm_noutputs (stmt); | |
173 if (n > 0) | |
174 { | |
175 t = out = gimple_asm_output_op (stmt, 0); | |
176 for (i = 1; i < n; i++) | |
177 { | |
178 TREE_CHAIN (t) = gimple_asm_output_op (stmt, i); | |
179 t = gimple_asm_output_op (stmt, i); | |
180 } | |
181 } | |
182 | |
183 in = NULL_TREE; | |
184 n = gimple_asm_ninputs (stmt); | |
185 if (n > 0) | |
186 { | |
187 t = in = gimple_asm_input_op (stmt, 0); | |
188 for (i = 1; i < n; i++) | |
189 { | |
190 TREE_CHAIN (t) = gimple_asm_input_op (stmt, i); | |
191 t = gimple_asm_input_op (stmt, i); | |
192 } | |
193 } | |
194 | |
195 cl = NULL_TREE; | |
196 n = gimple_asm_nclobbers (stmt); | |
197 if (n > 0) | |
198 { | |
199 t = cl = gimple_asm_clobber_op (stmt, 0); | |
200 for (i = 1; i < n; i++) | |
201 { | |
202 TREE_CHAIN (t) = gimple_asm_clobber_op (stmt, i); | |
203 t = gimple_asm_clobber_op (stmt, i); | |
204 } | |
205 } | |
206 | |
207 s = gimple_asm_string (stmt); | |
208 t = build4 (ASM_EXPR, void_type_node, build_string (strlen (s), s), | |
209 out, in, cl); | |
210 ASM_VOLATILE_P (t) = gimple_asm_volatile_p (stmt); | |
211 ASM_INPUT_P (t) = gimple_asm_input_p (stmt); | |
212 } | |
213 break; | |
214 | |
215 case GIMPLE_CALL: | |
216 { | |
217 size_t i; | |
218 tree fn; | |
219 tree_ann_common_t ann; | |
220 | |
221 t = build_vl_exp (CALL_EXPR, gimple_call_num_args (stmt) + 3); | |
222 | |
223 CALL_EXPR_FN (t) = gimple_call_fn (stmt); | |
224 TREE_TYPE (t) = gimple_call_return_type (stmt); | |
225 CALL_EXPR_STATIC_CHAIN (t) = gimple_call_chain (stmt); | |
226 | |
227 for (i = 0; i < gimple_call_num_args (stmt); i++) | |
228 CALL_EXPR_ARG (t, i) = gimple_call_arg (stmt, i); | |
229 | |
230 if (!(gimple_call_flags (stmt) & (ECF_CONST | ECF_PURE))) | |
231 TREE_SIDE_EFFECTS (t) = 1; | |
232 | |
233 if (gimple_call_flags (stmt) & ECF_NOTHROW) | |
234 TREE_NOTHROW (t) = 1; | |
235 | |
236 CALL_EXPR_TAILCALL (t) = gimple_call_tail_p (stmt); | |
237 CALL_EXPR_RETURN_SLOT_OPT (t) = gimple_call_return_slot_opt_p (stmt); | |
238 CALL_FROM_THUNK_P (t) = gimple_call_from_thunk_p (stmt); | |
239 CALL_CANNOT_INLINE_P (t) = gimple_call_cannot_inline_p (stmt); | |
240 CALL_EXPR_VA_ARG_PACK (t) = gimple_call_va_arg_pack_p (stmt); | |
5
a4c410aa4714
modifing gimple.[ch], cfgexpand.c
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
241 #ifndef noCbC |
21 | 242 CALL_EXPR_CbC_GOTO (t) = gimple_call_cbc_goto_p (stmt); |
5
a4c410aa4714
modifing gimple.[ch], cfgexpand.c
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
243 #endif |
0 | 244 |
245 /* If the call has a LHS then create a MODIFY_EXPR to hold it. */ | |
246 { | |
247 tree lhs = gimple_call_lhs (stmt); | |
248 | |
249 if (lhs) | |
250 t = build2 (MODIFY_EXPR, TREE_TYPE (lhs), lhs, t); | |
251 } | |
252 | |
253 /* Record the original call statement, as it may be used | |
254 to retrieve profile information during expansion. */ | |
255 | |
256 if ((fn = gimple_call_fndecl (stmt)) != NULL_TREE | |
257 && DECL_BUILT_IN (fn)) | |
258 { | |
259 ann = get_tree_common_ann (t); | |
260 ann->stmt = stmt; | |
261 } | |
262 } | |
263 break; | |
264 | |
265 case GIMPLE_SWITCH: | |
266 { | |
267 tree label_vec; | |
268 size_t i; | |
269 tree elt = gimple_switch_label (stmt, 0); | |
270 | |
271 label_vec = make_tree_vec (gimple_switch_num_labels (stmt)); | |
272 | |
273 if (!CASE_LOW (elt) && !CASE_HIGH (elt)) | |
274 { | |
275 for (i = 1; i < gimple_switch_num_labels (stmt); i++) | |
276 TREE_VEC_ELT (label_vec, i - 1) = gimple_switch_label (stmt, i); | |
277 | |
278 /* The default case in a SWITCH_EXPR must be at the end of | |
279 the label vector. */ | |
280 TREE_VEC_ELT (label_vec, i - 1) = gimple_switch_label (stmt, 0); | |
281 } | |
282 else | |
283 { | |
284 for (i = 0; i < gimple_switch_num_labels (stmt); i++) | |
285 TREE_VEC_ELT (label_vec, i) = gimple_switch_label (stmt, i); | |
286 } | |
287 | |
288 t = build3 (SWITCH_EXPR, void_type_node, gimple_switch_index (stmt), | |
289 NULL, label_vec); | |
290 } | |
291 break; | |
292 | |
293 case GIMPLE_NOP: | |
294 case GIMPLE_PREDICT: | |
295 t = build1 (NOP_EXPR, void_type_node, size_zero_node); | |
296 break; | |
297 | |
298 case GIMPLE_RESX: | |
299 t = build_resx (gimple_resx_region (stmt)); | |
300 break; | |
301 | |
302 default: | |
303 if (errorcount == 0) | |
304 { | |
305 error ("Unrecognized GIMPLE statement during RTL expansion"); | |
306 print_gimple_stmt (stderr, stmt, 4, 0); | |
307 gcc_unreachable (); | |
308 } | |
309 else | |
310 { | |
311 /* Ignore any bad gimple codes if we're going to die anyhow, | |
312 so we can at least set TREE_ASM_WRITTEN and have the rest | |
313 of compilation advance without sudden ICE death. */ | |
314 t = build1 (NOP_EXPR, void_type_node, size_zero_node); | |
315 break; | |
316 } | |
317 } | |
318 | |
319 /* If STMT is inside an exception region, record it in the generated | |
320 expression. */ | |
321 rn = lookup_stmt_eh_region (stmt); | |
322 if (rn >= 0) | |
323 { | |
324 tree call = get_call_expr_in (t); | |
325 | |
326 ann = get_tree_common_ann (t); | |
327 ann->rn = rn; | |
328 | |
329 /* For a CALL_EXPR on the RHS of an assignment, calls.c looks up | |
330 the CALL_EXPR not the assignment statment for EH region number. */ | |
331 if (call && call != t) | |
332 { | |
333 ann = get_tree_common_ann (call); | |
334 ann->rn = rn; | |
335 } | |
336 } | |
337 | |
338 /* Set EXPR_LOCATION in all the embedded expressions. */ | |
339 loc = gimple_location (stmt); | |
340 walk_tree (&t, set_expr_location_r, (void *) &loc, NULL); | |
341 | |
342 TREE_BLOCK (t) = gimple_block (stmt); | |
343 | |
344 return t; | |
345 } | |
346 | |
347 | |
348 /* Release back to GC memory allocated by gimple_to_tree. */ | |
349 | |
350 static void | |
351 release_stmt_tree (gimple stmt, tree stmt_tree) | |
352 { | |
353 tree_ann_common_t ann; | |
354 | |
355 switch (gimple_code (stmt)) | |
356 { | |
357 case GIMPLE_ASSIGN: | |
358 if (get_gimple_rhs_class (gimple_expr_code (stmt)) != GIMPLE_SINGLE_RHS) | |
359 ggc_free (TREE_OPERAND (stmt_tree, 1)); | |
360 break; | |
361 case GIMPLE_COND: | |
362 ggc_free (COND_EXPR_COND (stmt_tree)); | |
363 break; | |
364 case GIMPLE_RETURN: | |
365 if (TREE_OPERAND (stmt_tree, 0) | |
366 && TREE_CODE (TREE_OPERAND (stmt_tree, 0)) == MODIFY_EXPR) | |
367 ggc_free (TREE_OPERAND (stmt_tree, 0)); | |
368 break; | |
369 case GIMPLE_CALL: | |
370 if (gimple_call_lhs (stmt)) | |
371 { | |
372 ann = tree_common_ann (TREE_OPERAND (stmt_tree, 1)); | |
373 if (ann) | |
374 ggc_free (ann); | |
375 ggc_free (TREE_OPERAND (stmt_tree, 1)); | |
376 } | |
377 break; | |
378 default: | |
379 break; | |
380 } | |
381 ann = tree_common_ann (stmt_tree); | |
382 if (ann) | |
383 ggc_free (ann); | |
384 ggc_free (stmt_tree); | |
385 } | |
386 | |
387 | |
388 /* Verify that there is exactly single jump instruction since last and attach | |
389 REG_BR_PROB note specifying probability. | |
390 ??? We really ought to pass the probability down to RTL expanders and let it | |
391 re-distribute it when the conditional expands into multiple conditionals. | |
392 This is however difficult to do. */ | |
393 void | |
394 add_reg_br_prob_note (rtx last, int probability) | |
395 { | |
396 if (profile_status == PROFILE_ABSENT) | |
397 return; | |
398 for (last = NEXT_INSN (last); last && NEXT_INSN (last); last = NEXT_INSN (last)) | |
399 if (JUMP_P (last)) | |
400 { | |
401 /* It is common to emit condjump-around-jump sequence when we don't know | |
402 how to reverse the conditional. Special case this. */ | |
403 if (!any_condjump_p (last) | |
404 || !JUMP_P (NEXT_INSN (last)) | |
405 || !simplejump_p (NEXT_INSN (last)) | |
406 || !NEXT_INSN (NEXT_INSN (last)) | |
407 || !BARRIER_P (NEXT_INSN (NEXT_INSN (last))) | |
408 || !NEXT_INSN (NEXT_INSN (NEXT_INSN (last))) | |
409 || !LABEL_P (NEXT_INSN (NEXT_INSN (NEXT_INSN (last)))) | |
410 || NEXT_INSN (NEXT_INSN (NEXT_INSN (NEXT_INSN (last))))) | |
411 goto failed; | |
412 gcc_assert (!find_reg_note (last, REG_BR_PROB, 0)); | |
413 add_reg_note (last, REG_BR_PROB, | |
414 GEN_INT (REG_BR_PROB_BASE - probability)); | |
415 return; | |
416 } | |
417 if (!last || !JUMP_P (last) || !any_condjump_p (last)) | |
418 goto failed; | |
419 gcc_assert (!find_reg_note (last, REG_BR_PROB, 0)); | |
420 add_reg_note (last, REG_BR_PROB, GEN_INT (probability)); | |
421 return; | |
422 failed: | |
423 if (dump_file) | |
424 fprintf (dump_file, "Failed to add probability note\n"); | |
425 } | |
426 | |
427 | |
428 #ifndef STACK_ALIGNMENT_NEEDED | |
429 #define STACK_ALIGNMENT_NEEDED 1 | |
430 #endif | |
431 | |
432 | |
433 /* This structure holds data relevant to one variable that will be | |
434 placed in a stack slot. */ | |
435 struct stack_var | |
436 { | |
437 /* The Variable. */ | |
438 tree decl; | |
439 | |
440 /* The offset of the variable. During partitioning, this is the | |
441 offset relative to the partition. After partitioning, this | |
442 is relative to the stack frame. */ | |
443 HOST_WIDE_INT offset; | |
444 | |
445 /* Initially, the size of the variable. Later, the size of the partition, | |
446 if this variable becomes it's partition's representative. */ | |
447 HOST_WIDE_INT size; | |
448 | |
449 /* The *byte* alignment required for this variable. Or as, with the | |
450 size, the alignment for this partition. */ | |
451 unsigned int alignb; | |
452 | |
453 /* The partition representative. */ | |
454 size_t representative; | |
455 | |
456 /* The next stack variable in the partition, or EOC. */ | |
457 size_t next; | |
458 }; | |
459 | |
460 #define EOC ((size_t)-1) | |
461 | |
462 /* We have an array of such objects while deciding allocation. */ | |
463 static struct stack_var *stack_vars; | |
464 static size_t stack_vars_alloc; | |
465 static size_t stack_vars_num; | |
466 | |
467 /* An array of indices such that stack_vars[stack_vars_sorted[i]].size | |
468 is non-decreasing. */ | |
469 static size_t *stack_vars_sorted; | |
470 | |
471 /* We have an interference graph between such objects. This graph | |
472 is lower triangular. */ | |
473 static bool *stack_vars_conflict; | |
474 static size_t stack_vars_conflict_alloc; | |
475 | |
476 /* The phase of the stack frame. This is the known misalignment of | |
477 virtual_stack_vars_rtx from PREFERRED_STACK_BOUNDARY. That is, | |
478 (frame_offset+frame_phase) % PREFERRED_STACK_BOUNDARY == 0. */ | |
479 static int frame_phase; | |
480 | |
481 /* Used during expand_used_vars to remember if we saw any decls for | |
482 which we'd like to enable stack smashing protection. */ | |
483 static bool has_protected_decls; | |
484 | |
485 /* Used during expand_used_vars. Remember if we say a character buffer | |
486 smaller than our cutoff threshold. Used for -Wstack-protector. */ | |
487 static bool has_short_buffer; | |
488 | |
489 /* Discover the byte alignment to use for DECL. Ignore alignment | |
490 we can't do with expected alignment of the stack boundary. */ | |
491 | |
492 static unsigned int | |
493 get_decl_align_unit (tree decl) | |
494 { | |
495 unsigned int align; | |
496 | |
497 align = LOCAL_DECL_ALIGNMENT (decl); | |
498 | |
499 if (align > MAX_SUPPORTED_STACK_ALIGNMENT) | |
500 align = MAX_SUPPORTED_STACK_ALIGNMENT; | |
501 | |
502 if (SUPPORTS_STACK_ALIGNMENT) | |
503 { | |
504 if (crtl->stack_alignment_estimated < align) | |
505 { | |
506 gcc_assert(!crtl->stack_realign_processed); | |
507 crtl->stack_alignment_estimated = align; | |
508 } | |
509 } | |
510 | |
511 /* stack_alignment_needed > PREFERRED_STACK_BOUNDARY is permitted. | |
512 So here we only make sure stack_alignment_needed >= align. */ | |
513 if (crtl->stack_alignment_needed < align) | |
514 crtl->stack_alignment_needed = align; | |
515 if (crtl->max_used_stack_slot_alignment < crtl->stack_alignment_needed) | |
516 crtl->max_used_stack_slot_alignment = crtl->stack_alignment_needed; | |
517 | |
518 return align / BITS_PER_UNIT; | |
519 } | |
520 | |
521 /* Allocate SIZE bytes at byte alignment ALIGN from the stack frame. | |
522 Return the frame offset. */ | |
523 | |
524 static HOST_WIDE_INT | |
525 alloc_stack_frame_space (HOST_WIDE_INT size, HOST_WIDE_INT align) | |
526 { | |
527 HOST_WIDE_INT offset, new_frame_offset; | |
528 | |
529 new_frame_offset = frame_offset; | |
530 if (FRAME_GROWS_DOWNWARD) | |
531 { | |
532 new_frame_offset -= size + frame_phase; | |
533 new_frame_offset &= -align; | |
534 new_frame_offset += frame_phase; | |
535 offset = new_frame_offset; | |
536 } | |
537 else | |
538 { | |
539 new_frame_offset -= frame_phase; | |
540 new_frame_offset += align - 1; | |
541 new_frame_offset &= -align; | |
542 new_frame_offset += frame_phase; | |
543 offset = new_frame_offset; | |
544 new_frame_offset += size; | |
545 } | |
546 frame_offset = new_frame_offset; | |
547 | |
548 if (frame_offset_overflow (frame_offset, cfun->decl)) | |
549 frame_offset = offset = 0; | |
550 | |
551 return offset; | |
552 } | |
553 | |
554 /* Accumulate DECL into STACK_VARS. */ | |
555 | |
556 static void | |
557 add_stack_var (tree decl) | |
558 { | |
559 if (stack_vars_num >= stack_vars_alloc) | |
560 { | |
561 if (stack_vars_alloc) | |
562 stack_vars_alloc = stack_vars_alloc * 3 / 2; | |
563 else | |
564 stack_vars_alloc = 32; | |
565 stack_vars | |
566 = XRESIZEVEC (struct stack_var, stack_vars, stack_vars_alloc); | |
567 } | |
568 stack_vars[stack_vars_num].decl = decl; | |
569 stack_vars[stack_vars_num].offset = 0; | |
570 stack_vars[stack_vars_num].size = tree_low_cst (DECL_SIZE_UNIT (decl), 1); | |
571 stack_vars[stack_vars_num].alignb = get_decl_align_unit (decl); | |
572 | |
573 /* All variables are initially in their own partition. */ | |
574 stack_vars[stack_vars_num].representative = stack_vars_num; | |
575 stack_vars[stack_vars_num].next = EOC; | |
576 | |
577 /* Ensure that this decl doesn't get put onto the list twice. */ | |
578 SET_DECL_RTL (decl, pc_rtx); | |
579 | |
580 stack_vars_num++; | |
581 } | |
582 | |
583 /* Compute the linear index of a lower-triangular coordinate (I, J). */ | |
584 | |
585 static size_t | |
586 triangular_index (size_t i, size_t j) | |
587 { | |
588 if (i < j) | |
589 { | |
590 size_t t; | |
591 t = i, i = j, j = t; | |
592 } | |
593 return (i * (i + 1)) / 2 + j; | |
594 } | |
595 | |
596 /* Ensure that STACK_VARS_CONFLICT is large enough for N objects. */ | |
597 | |
598 static void | |
599 resize_stack_vars_conflict (size_t n) | |
600 { | |
601 size_t size = triangular_index (n-1, n-1) + 1; | |
602 | |
603 if (size <= stack_vars_conflict_alloc) | |
604 return; | |
605 | |
606 stack_vars_conflict = XRESIZEVEC (bool, stack_vars_conflict, size); | |
607 memset (stack_vars_conflict + stack_vars_conflict_alloc, 0, | |
608 (size - stack_vars_conflict_alloc) * sizeof (bool)); | |
609 stack_vars_conflict_alloc = size; | |
610 } | |
611 | |
612 /* Make the decls associated with luid's X and Y conflict. */ | |
613 | |
614 static void | |
615 add_stack_var_conflict (size_t x, size_t y) | |
616 { | |
617 size_t index = triangular_index (x, y); | |
618 gcc_assert (index < stack_vars_conflict_alloc); | |
619 stack_vars_conflict[index] = true; | |
620 } | |
621 | |
622 /* Check whether the decls associated with luid's X and Y conflict. */ | |
623 | |
624 static bool | |
625 stack_var_conflict_p (size_t x, size_t y) | |
626 { | |
627 size_t index = triangular_index (x, y); | |
628 gcc_assert (index < stack_vars_conflict_alloc); | |
629 return stack_vars_conflict[index]; | |
630 } | |
631 | |
632 /* Returns true if TYPE is or contains a union type. */ | |
633 | |
634 static bool | |
635 aggregate_contains_union_type (tree type) | |
636 { | |
637 tree field; | |
638 | |
639 if (TREE_CODE (type) == UNION_TYPE | |
640 || TREE_CODE (type) == QUAL_UNION_TYPE) | |
641 return true; | |
642 if (TREE_CODE (type) == ARRAY_TYPE) | |
643 return aggregate_contains_union_type (TREE_TYPE (type)); | |
644 if (TREE_CODE (type) != RECORD_TYPE) | |
645 return false; | |
646 | |
647 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field)) | |
648 if (TREE_CODE (field) == FIELD_DECL) | |
649 if (aggregate_contains_union_type (TREE_TYPE (field))) | |
650 return true; | |
651 | |
652 return false; | |
653 } | |
654 | |
655 /* A subroutine of expand_used_vars. If two variables X and Y have alias | |
656 sets that do not conflict, then do add a conflict for these variables | |
657 in the interference graph. We also need to make sure to add conflicts | |
658 for union containing structures. Else RTL alias analysis comes along | |
659 and due to type based aliasing rules decides that for two overlapping | |
660 union temporaries { short s; int i; } accesses to the same mem through | |
661 different types may not alias and happily reorders stores across | |
662 life-time boundaries of the temporaries (See PR25654). | |
663 We also have to mind MEM_IN_STRUCT_P and MEM_SCALAR_P. */ | |
664 | |
665 static void | |
666 add_alias_set_conflicts (void) | |
667 { | |
668 size_t i, j, n = stack_vars_num; | |
669 | |
670 for (i = 0; i < n; ++i) | |
671 { | |
672 tree type_i = TREE_TYPE (stack_vars[i].decl); | |
673 bool aggr_i = AGGREGATE_TYPE_P (type_i); | |
674 bool contains_union; | |
675 | |
676 contains_union = aggregate_contains_union_type (type_i); | |
677 for (j = 0; j < i; ++j) | |
678 { | |
679 tree type_j = TREE_TYPE (stack_vars[j].decl); | |
680 bool aggr_j = AGGREGATE_TYPE_P (type_j); | |
681 if (aggr_i != aggr_j | |
682 /* Either the objects conflict by means of type based | |
683 aliasing rules, or we need to add a conflict. */ | |
684 || !objects_must_conflict_p (type_i, type_j) | |
685 /* In case the types do not conflict ensure that access | |
686 to elements will conflict. In case of unions we have | |
687 to be careful as type based aliasing rules may say | |
688 access to the same memory does not conflict. So play | |
689 safe and add a conflict in this case. */ | |
690 || contains_union) | |
691 add_stack_var_conflict (i, j); | |
692 } | |
693 } | |
694 } | |
695 | |
696 /* A subroutine of partition_stack_vars. A comparison function for qsort, | |
697 sorting an array of indices by the size of the object. */ | |
698 | |
699 static int | |
700 stack_var_size_cmp (const void *a, const void *b) | |
701 { | |
702 HOST_WIDE_INT sa = stack_vars[*(const size_t *)a].size; | |
703 HOST_WIDE_INT sb = stack_vars[*(const size_t *)b].size; | |
704 unsigned int uida = DECL_UID (stack_vars[*(const size_t *)a].decl); | |
705 unsigned int uidb = DECL_UID (stack_vars[*(const size_t *)b].decl); | |
706 | |
707 if (sa < sb) | |
708 return -1; | |
709 if (sa > sb) | |
710 return 1; | |
711 /* For stack variables of the same size use the uid of the decl | |
712 to make the sort stable. */ | |
713 if (uida < uidb) | |
714 return -1; | |
715 if (uida > uidb) | |
716 return 1; | |
717 return 0; | |
718 } | |
719 | |
720 /* A subroutine of partition_stack_vars. The UNION portion of a UNION/FIND | |
721 partitioning algorithm. Partitions A and B are known to be non-conflicting. | |
722 Merge them into a single partition A. | |
723 | |
724 At the same time, add OFFSET to all variables in partition B. At the end | |
725 of the partitioning process we've have a nice block easy to lay out within | |
726 the stack frame. */ | |
727 | |
728 static void | |
729 union_stack_vars (size_t a, size_t b, HOST_WIDE_INT offset) | |
730 { | |
731 size_t i, last; | |
732 | |
733 /* Update each element of partition B with the given offset, | |
734 and merge them into partition A. */ | |
735 for (last = i = b; i != EOC; last = i, i = stack_vars[i].next) | |
736 { | |
737 stack_vars[i].offset += offset; | |
738 stack_vars[i].representative = a; | |
739 } | |
740 stack_vars[last].next = stack_vars[a].next; | |
741 stack_vars[a].next = b; | |
742 | |
743 /* Update the required alignment of partition A to account for B. */ | |
744 if (stack_vars[a].alignb < stack_vars[b].alignb) | |
745 stack_vars[a].alignb = stack_vars[b].alignb; | |
746 | |
747 /* Update the interference graph and merge the conflicts. */ | |
748 for (last = stack_vars_num, i = 0; i < last; ++i) | |
749 if (stack_var_conflict_p (b, i)) | |
750 add_stack_var_conflict (a, i); | |
751 } | |
752 | |
753 /* A subroutine of expand_used_vars. Binpack the variables into | |
754 partitions constrained by the interference graph. The overall | |
755 algorithm used is as follows: | |
756 | |
757 Sort the objects by size. | |
758 For each object A { | |
759 S = size(A) | |
760 O = 0 | |
761 loop { | |
762 Look for the largest non-conflicting object B with size <= S. | |
763 UNION (A, B) | |
764 offset(B) = O | |
765 O += size(B) | |
766 S -= size(B) | |
767 } | |
768 } | |
769 */ | |
770 | |
771 static void | |
772 partition_stack_vars (void) | |
773 { | |
774 size_t si, sj, n = stack_vars_num; | |
775 | |
776 stack_vars_sorted = XNEWVEC (size_t, stack_vars_num); | |
777 for (si = 0; si < n; ++si) | |
778 stack_vars_sorted[si] = si; | |
779 | |
780 if (n == 1) | |
781 return; | |
782 | |
783 qsort (stack_vars_sorted, n, sizeof (size_t), stack_var_size_cmp); | |
784 | |
785 /* Special case: detect when all variables conflict, and thus we can't | |
786 do anything during the partitioning loop. It isn't uncommon (with | |
787 C code at least) to declare all variables at the top of the function, | |
788 and if we're not inlining, then all variables will be in the same scope. | |
789 Take advantage of very fast libc routines for this scan. */ | |
790 gcc_assert (sizeof(bool) == sizeof(char)); | |
791 if (memchr (stack_vars_conflict, false, stack_vars_conflict_alloc) == NULL) | |
792 return; | |
793 | |
794 for (si = 0; si < n; ++si) | |
795 { | |
796 size_t i = stack_vars_sorted[si]; | |
797 HOST_WIDE_INT isize = stack_vars[i].size; | |
798 HOST_WIDE_INT offset = 0; | |
799 | |
800 for (sj = si; sj-- > 0; ) | |
801 { | |
802 size_t j = stack_vars_sorted[sj]; | |
803 HOST_WIDE_INT jsize = stack_vars[j].size; | |
804 unsigned int jalign = stack_vars[j].alignb; | |
805 | |
806 /* Ignore objects that aren't partition representatives. */ | |
807 if (stack_vars[j].representative != j) | |
808 continue; | |
809 | |
810 /* Ignore objects too large for the remaining space. */ | |
811 if (isize < jsize) | |
812 continue; | |
813 | |
814 /* Ignore conflicting objects. */ | |
815 if (stack_var_conflict_p (i, j)) | |
816 continue; | |
817 | |
818 /* Refine the remaining space check to include alignment. */ | |
819 if (offset & (jalign - 1)) | |
820 { | |
821 HOST_WIDE_INT toff = offset; | |
822 toff += jalign - 1; | |
823 toff &= -(HOST_WIDE_INT)jalign; | |
824 if (isize - (toff - offset) < jsize) | |
825 continue; | |
826 | |
827 isize -= toff - offset; | |
828 offset = toff; | |
829 } | |
830 | |
831 /* UNION the objects, placing J at OFFSET. */ | |
832 union_stack_vars (i, j, offset); | |
833 | |
834 isize -= jsize; | |
835 if (isize == 0) | |
836 break; | |
837 } | |
838 } | |
839 } | |
840 | |
841 /* A debugging aid for expand_used_vars. Dump the generated partitions. */ | |
842 | |
843 static void | |
844 dump_stack_var_partition (void) | |
845 { | |
846 size_t si, i, j, n = stack_vars_num; | |
847 | |
848 for (si = 0; si < n; ++si) | |
849 { | |
850 i = stack_vars_sorted[si]; | |
851 | |
852 /* Skip variables that aren't partition representatives, for now. */ | |
853 if (stack_vars[i].representative != i) | |
854 continue; | |
855 | |
856 fprintf (dump_file, "Partition %lu: size " HOST_WIDE_INT_PRINT_DEC | |
857 " align %u\n", (unsigned long) i, stack_vars[i].size, | |
858 stack_vars[i].alignb); | |
859 | |
860 for (j = i; j != EOC; j = stack_vars[j].next) | |
861 { | |
862 fputc ('\t', dump_file); | |
863 print_generic_expr (dump_file, stack_vars[j].decl, dump_flags); | |
864 fprintf (dump_file, ", offset " HOST_WIDE_INT_PRINT_DEC "\n", | |
865 stack_vars[j].offset); | |
866 } | |
867 } | |
868 } | |
869 | |
870 /* Assign rtl to DECL at frame offset OFFSET. */ | |
871 | |
872 static void | |
873 expand_one_stack_var_at (tree decl, HOST_WIDE_INT offset) | |
874 { | |
875 HOST_WIDE_INT align; | |
876 rtx x; | |
877 | |
878 /* If this fails, we've overflowed the stack frame. Error nicely? */ | |
879 gcc_assert (offset == trunc_int_for_mode (offset, Pmode)); | |
880 | |
881 x = plus_constant (virtual_stack_vars_rtx, offset); | |
882 x = gen_rtx_MEM (DECL_MODE (decl), x); | |
883 | |
884 /* Set alignment we actually gave this decl. */ | |
885 offset -= frame_phase; | |
886 align = offset & -offset; | |
887 align *= BITS_PER_UNIT; | |
888 if (align > STACK_BOUNDARY || align == 0) | |
889 align = STACK_BOUNDARY; | |
890 DECL_ALIGN (decl) = align; | |
891 DECL_USER_ALIGN (decl) = 0; | |
892 | |
893 set_mem_attributes (x, decl, true); | |
894 SET_DECL_RTL (decl, x); | |
895 } | |
896 | |
897 /* A subroutine of expand_used_vars. Give each partition representative | |
898 a unique location within the stack frame. Update each partition member | |
899 with that location. */ | |
900 | |
901 static void | |
902 expand_stack_vars (bool (*pred) (tree)) | |
903 { | |
904 size_t si, i, j, n = stack_vars_num; | |
905 | |
906 for (si = 0; si < n; ++si) | |
907 { | |
908 HOST_WIDE_INT offset; | |
909 | |
910 i = stack_vars_sorted[si]; | |
911 | |
912 /* Skip variables that aren't partition representatives, for now. */ | |
913 if (stack_vars[i].representative != i) | |
914 continue; | |
915 | |
916 /* Skip variables that have already had rtl assigned. See also | |
917 add_stack_var where we perpetrate this pc_rtx hack. */ | |
918 if (DECL_RTL (stack_vars[i].decl) != pc_rtx) | |
919 continue; | |
920 | |
921 /* Check the predicate to see whether this variable should be | |
922 allocated in this pass. */ | |
923 if (pred && !pred (stack_vars[i].decl)) | |
924 continue; | |
925 | |
926 offset = alloc_stack_frame_space (stack_vars[i].size, | |
927 stack_vars[i].alignb); | |
928 | |
929 /* Create rtl for each variable based on their location within the | |
930 partition. */ | |
931 for (j = i; j != EOC; j = stack_vars[j].next) | |
932 { | |
933 gcc_assert (stack_vars[j].offset <= stack_vars[i].size); | |
934 expand_one_stack_var_at (stack_vars[j].decl, | |
935 stack_vars[j].offset + offset); | |
936 } | |
937 } | |
938 } | |
939 | |
940 /* Take into account all sizes of partitions and reset DECL_RTLs. */ | |
941 static HOST_WIDE_INT | |
942 account_stack_vars (void) | |
943 { | |
944 size_t si, j, i, n = stack_vars_num; | |
945 HOST_WIDE_INT size = 0; | |
946 | |
947 for (si = 0; si < n; ++si) | |
948 { | |
949 i = stack_vars_sorted[si]; | |
950 | |
951 /* Skip variables that aren't partition representatives, for now. */ | |
952 if (stack_vars[i].representative != i) | |
953 continue; | |
954 | |
955 size += stack_vars[i].size; | |
956 for (j = i; j != EOC; j = stack_vars[j].next) | |
957 SET_DECL_RTL (stack_vars[j].decl, NULL); | |
958 } | |
959 return size; | |
960 } | |
961 | |
962 /* A subroutine of expand_one_var. Called to immediately assign rtl | |
963 to a variable to be allocated in the stack frame. */ | |
964 | |
965 static void | |
966 expand_one_stack_var (tree var) | |
967 { | |
968 HOST_WIDE_INT size, offset, align; | |
969 | |
970 size = tree_low_cst (DECL_SIZE_UNIT (var), 1); | |
971 align = get_decl_align_unit (var); | |
972 offset = alloc_stack_frame_space (size, align); | |
973 | |
974 expand_one_stack_var_at (var, offset); | |
975 } | |
976 | |
977 /* A subroutine of expand_one_var. Called to assign rtl to a VAR_DECL | |
978 that will reside in a hard register. */ | |
979 | |
980 static void | |
981 expand_one_hard_reg_var (tree var) | |
982 { | |
983 rest_of_decl_compilation (var, 0, 0); | |
984 } | |
985 | |
986 /* A subroutine of expand_one_var. Called to assign rtl to a VAR_DECL | |
987 that will reside in a pseudo register. */ | |
988 | |
989 static void | |
990 expand_one_register_var (tree var) | |
991 { | |
992 tree type = TREE_TYPE (var); | |
993 int unsignedp = TYPE_UNSIGNED (type); | |
994 enum machine_mode reg_mode | |
995 = promote_mode (type, DECL_MODE (var), &unsignedp, 0); | |
996 rtx x = gen_reg_rtx (reg_mode); | |
997 | |
998 SET_DECL_RTL (var, x); | |
999 | |
1000 /* Note if the object is a user variable. */ | |
1001 if (!DECL_ARTIFICIAL (var)) | |
1002 mark_user_reg (x); | |
1003 | |
1004 if (POINTER_TYPE_P (type)) | |
1005 mark_reg_pointer (x, TYPE_ALIGN (TREE_TYPE (TREE_TYPE (var)))); | |
1006 } | |
1007 | |
1008 /* A subroutine of expand_one_var. Called to assign rtl to a VAR_DECL that | |
1009 has some associated error, e.g. its type is error-mark. We just need | |
1010 to pick something that won't crash the rest of the compiler. */ | |
1011 | |
1012 static void | |
1013 expand_one_error_var (tree var) | |
1014 { | |
1015 enum machine_mode mode = DECL_MODE (var); | |
1016 rtx x; | |
1017 | |
1018 if (mode == BLKmode) | |
1019 x = gen_rtx_MEM (BLKmode, const0_rtx); | |
1020 else if (mode == VOIDmode) | |
1021 x = const0_rtx; | |
1022 else | |
1023 x = gen_reg_rtx (mode); | |
1024 | |
1025 SET_DECL_RTL (var, x); | |
1026 } | |
1027 | |
1028 /* A subroutine of expand_one_var. VAR is a variable that will be | |
1029 allocated to the local stack frame. Return true if we wish to | |
1030 add VAR to STACK_VARS so that it will be coalesced with other | |
1031 variables. Return false to allocate VAR immediately. | |
1032 | |
1033 This function is used to reduce the number of variables considered | |
1034 for coalescing, which reduces the size of the quadratic problem. */ | |
1035 | |
1036 static bool | |
1037 defer_stack_allocation (tree var, bool toplevel) | |
1038 { | |
1039 /* If stack protection is enabled, *all* stack variables must be deferred, | |
1040 so that we can re-order the strings to the top of the frame. */ | |
1041 if (flag_stack_protect) | |
1042 return true; | |
1043 | |
1044 /* Variables in the outermost scope automatically conflict with | |
1045 every other variable. The only reason to want to defer them | |
1046 at all is that, after sorting, we can more efficiently pack | |
1047 small variables in the stack frame. Continue to defer at -O2. */ | |
1048 if (toplevel && optimize < 2) | |
1049 return false; | |
1050 | |
1051 /* Without optimization, *most* variables are allocated from the | |
1052 stack, which makes the quadratic problem large exactly when we | |
1053 want compilation to proceed as quickly as possible. On the | |
1054 other hand, we don't want the function's stack frame size to | |
1055 get completely out of hand. So we avoid adding scalars and | |
1056 "small" aggregates to the list at all. */ | |
1057 if (optimize == 0 && tree_low_cst (DECL_SIZE_UNIT (var), 1) < 32) | |
1058 return false; | |
1059 | |
1060 return true; | |
1061 } | |
1062 | |
1063 /* A subroutine of expand_used_vars. Expand one variable according to | |
1064 its flavor. Variables to be placed on the stack are not actually | |
1065 expanded yet, merely recorded. | |
1066 When REALLY_EXPAND is false, only add stack values to be allocated. | |
1067 Return stack usage this variable is supposed to take. | |
1068 */ | |
1069 | |
1070 static HOST_WIDE_INT | |
1071 expand_one_var (tree var, bool toplevel, bool really_expand) | |
1072 { | |
1073 if (SUPPORTS_STACK_ALIGNMENT | |
1074 && TREE_TYPE (var) != error_mark_node | |
1075 && TREE_CODE (var) == VAR_DECL) | |
1076 { | |
1077 unsigned int align; | |
1078 | |
1079 /* Because we don't know if VAR will be in register or on stack, | |
1080 we conservatively assume it will be on stack even if VAR is | |
1081 eventually put into register after RA pass. For non-automatic | |
1082 variables, which won't be on stack, we collect alignment of | |
1083 type and ignore user specified alignment. */ | |
1084 if (TREE_STATIC (var) || DECL_EXTERNAL (var)) | |
19
58ad6c70ea60
update gcc from 4.4.0 to 4.4.1.
kent@firefly.cr.ie.u-ryukyu.ac.jp
parents:
0
diff
changeset
|
1085 align = MINIMUM_ALIGNMENT (TREE_TYPE (var), |
58ad6c70ea60
update gcc from 4.4.0 to 4.4.1.
kent@firefly.cr.ie.u-ryukyu.ac.jp
parents:
0
diff
changeset
|
1086 TYPE_MODE (TREE_TYPE (var)), |
58ad6c70ea60
update gcc from 4.4.0 to 4.4.1.
kent@firefly.cr.ie.u-ryukyu.ac.jp
parents:
0
diff
changeset
|
1087 TYPE_ALIGN (TREE_TYPE (var))); |
0 | 1088 else |
19
58ad6c70ea60
update gcc from 4.4.0 to 4.4.1.
kent@firefly.cr.ie.u-ryukyu.ac.jp
parents:
0
diff
changeset
|
1089 align = MINIMUM_ALIGNMENT (var, DECL_MODE (var), DECL_ALIGN (var)); |
0 | 1090 |
1091 if (crtl->stack_alignment_estimated < align) | |
1092 { | |
1093 /* stack_alignment_estimated shouldn't change after stack | |
1094 realign decision made */ | |
1095 gcc_assert(!crtl->stack_realign_processed); | |
1096 crtl->stack_alignment_estimated = align; | |
1097 } | |
1098 } | |
1099 | |
1100 if (TREE_CODE (var) != VAR_DECL) | |
1101 ; | |
1102 else if (DECL_EXTERNAL (var)) | |
1103 ; | |
1104 else if (DECL_HAS_VALUE_EXPR_P (var)) | |
1105 ; | |
1106 else if (TREE_STATIC (var)) | |
1107 ; | |
1108 else if (DECL_RTL_SET_P (var)) | |
1109 ; | |
1110 else if (TREE_TYPE (var) == error_mark_node) | |
1111 { | |
1112 if (really_expand) | |
1113 expand_one_error_var (var); | |
1114 } | |
1115 else if (DECL_HARD_REGISTER (var)) | |
1116 { | |
1117 if (really_expand) | |
1118 expand_one_hard_reg_var (var); | |
1119 } | |
1120 else if (use_register_for_decl (var)) | |
1121 { | |
1122 if (really_expand) | |
1123 expand_one_register_var (var); | |
1124 } | |
1125 else if (defer_stack_allocation (var, toplevel)) | |
1126 add_stack_var (var); | |
1127 else | |
1128 { | |
1129 if (really_expand) | |
1130 expand_one_stack_var (var); | |
1131 return tree_low_cst (DECL_SIZE_UNIT (var), 1); | |
1132 } | |
1133 return 0; | |
1134 } | |
1135 | |
1136 /* A subroutine of expand_used_vars. Walk down through the BLOCK tree | |
1137 expanding variables. Those variables that can be put into registers | |
1138 are allocated pseudos; those that can't are put on the stack. | |
1139 | |
1140 TOPLEVEL is true if this is the outermost BLOCK. */ | |
1141 | |
1142 static void | |
1143 expand_used_vars_for_block (tree block, bool toplevel) | |
1144 { | |
1145 size_t i, j, old_sv_num, this_sv_num, new_sv_num; | |
1146 tree t; | |
1147 | |
1148 old_sv_num = toplevel ? 0 : stack_vars_num; | |
1149 | |
1150 /* Expand all variables at this level. */ | |
1151 for (t = BLOCK_VARS (block); t ; t = TREE_CHAIN (t)) | |
1152 if (TREE_USED (t)) | |
1153 expand_one_var (t, toplevel, true); | |
1154 | |
1155 this_sv_num = stack_vars_num; | |
1156 | |
1157 /* Expand all variables at containing levels. */ | |
1158 for (t = BLOCK_SUBBLOCKS (block); t ; t = BLOCK_CHAIN (t)) | |
1159 expand_used_vars_for_block (t, false); | |
1160 | |
1161 /* Since we do not track exact variable lifetimes (which is not even | |
1162 possible for variables whose address escapes), we mirror the block | |
1163 tree in the interference graph. Here we cause all variables at this | |
1164 level, and all sublevels, to conflict. Do make certain that a | |
1165 variable conflicts with itself. */ | |
1166 if (old_sv_num < this_sv_num) | |
1167 { | |
1168 new_sv_num = stack_vars_num; | |
1169 resize_stack_vars_conflict (new_sv_num); | |
1170 | |
1171 for (i = old_sv_num; i < new_sv_num; ++i) | |
1172 for (j = i < this_sv_num ? i+1 : this_sv_num; j-- > old_sv_num ;) | |
1173 add_stack_var_conflict (i, j); | |
1174 } | |
1175 } | |
1176 | |
1177 /* A subroutine of expand_used_vars. Walk down through the BLOCK tree | |
1178 and clear TREE_USED on all local variables. */ | |
1179 | |
1180 static void | |
1181 clear_tree_used (tree block) | |
1182 { | |
1183 tree t; | |
1184 | |
1185 for (t = BLOCK_VARS (block); t ; t = TREE_CHAIN (t)) | |
1186 /* if (!TREE_STATIC (t) && !DECL_EXTERNAL (t)) */ | |
1187 TREE_USED (t) = 0; | |
1188 | |
1189 for (t = BLOCK_SUBBLOCKS (block); t ; t = BLOCK_CHAIN (t)) | |
1190 clear_tree_used (t); | |
1191 } | |
1192 | |
1193 /* Examine TYPE and determine a bit mask of the following features. */ | |
1194 | |
1195 #define SPCT_HAS_LARGE_CHAR_ARRAY 1 | |
1196 #define SPCT_HAS_SMALL_CHAR_ARRAY 2 | |
1197 #define SPCT_HAS_ARRAY 4 | |
1198 #define SPCT_HAS_AGGREGATE 8 | |
1199 | |
1200 static unsigned int | |
1201 stack_protect_classify_type (tree type) | |
1202 { | |
1203 unsigned int ret = 0; | |
1204 tree t; | |
1205 | |
1206 switch (TREE_CODE (type)) | |
1207 { | |
1208 case ARRAY_TYPE: | |
1209 t = TYPE_MAIN_VARIANT (TREE_TYPE (type)); | |
1210 if (t == char_type_node | |
1211 || t == signed_char_type_node | |
1212 || t == unsigned_char_type_node) | |
1213 { | |
1214 unsigned HOST_WIDE_INT max = PARAM_VALUE (PARAM_SSP_BUFFER_SIZE); | |
1215 unsigned HOST_WIDE_INT len; | |
1216 | |
1217 if (!TYPE_SIZE_UNIT (type) | |
1218 || !host_integerp (TYPE_SIZE_UNIT (type), 1)) | |
1219 len = max; | |
1220 else | |
1221 len = tree_low_cst (TYPE_SIZE_UNIT (type), 1); | |
1222 | |
1223 if (len < max) | |
1224 ret = SPCT_HAS_SMALL_CHAR_ARRAY | SPCT_HAS_ARRAY; | |
1225 else | |
1226 ret = SPCT_HAS_LARGE_CHAR_ARRAY | SPCT_HAS_ARRAY; | |
1227 } | |
1228 else | |
1229 ret = SPCT_HAS_ARRAY; | |
1230 break; | |
1231 | |
1232 case UNION_TYPE: | |
1233 case QUAL_UNION_TYPE: | |
1234 case RECORD_TYPE: | |
1235 ret = SPCT_HAS_AGGREGATE; | |
1236 for (t = TYPE_FIELDS (type); t ; t = TREE_CHAIN (t)) | |
1237 if (TREE_CODE (t) == FIELD_DECL) | |
1238 ret |= stack_protect_classify_type (TREE_TYPE (t)); | |
1239 break; | |
1240 | |
1241 default: | |
1242 break; | |
1243 } | |
1244 | |
1245 return ret; | |
1246 } | |
1247 | |
1248 /* Return nonzero if DECL should be segregated into the "vulnerable" upper | |
1249 part of the local stack frame. Remember if we ever return nonzero for | |
1250 any variable in this function. The return value is the phase number in | |
1251 which the variable should be allocated. */ | |
1252 | |
1253 static int | |
1254 stack_protect_decl_phase (tree decl) | |
1255 { | |
1256 unsigned int bits = stack_protect_classify_type (TREE_TYPE (decl)); | |
1257 int ret = 0; | |
1258 | |
1259 if (bits & SPCT_HAS_SMALL_CHAR_ARRAY) | |
1260 has_short_buffer = true; | |
1261 | |
1262 if (flag_stack_protect == 2) | |
1263 { | |
1264 if ((bits & (SPCT_HAS_SMALL_CHAR_ARRAY | SPCT_HAS_LARGE_CHAR_ARRAY)) | |
1265 && !(bits & SPCT_HAS_AGGREGATE)) | |
1266 ret = 1; | |
1267 else if (bits & SPCT_HAS_ARRAY) | |
1268 ret = 2; | |
1269 } | |
1270 else | |
1271 ret = (bits & SPCT_HAS_LARGE_CHAR_ARRAY) != 0; | |
1272 | |
1273 if (ret) | |
1274 has_protected_decls = true; | |
1275 | |
1276 return ret; | |
1277 } | |
1278 | |
1279 /* Two helper routines that check for phase 1 and phase 2. These are used | |
1280 as callbacks for expand_stack_vars. */ | |
1281 | |
1282 static bool | |
1283 stack_protect_decl_phase_1 (tree decl) | |
1284 { | |
1285 return stack_protect_decl_phase (decl) == 1; | |
1286 } | |
1287 | |
1288 static bool | |
1289 stack_protect_decl_phase_2 (tree decl) | |
1290 { | |
1291 return stack_protect_decl_phase (decl) == 2; | |
1292 } | |
1293 | |
1294 /* Ensure that variables in different stack protection phases conflict | |
1295 so that they are not merged and share the same stack slot. */ | |
1296 | |
1297 static void | |
1298 add_stack_protection_conflicts (void) | |
1299 { | |
1300 size_t i, j, n = stack_vars_num; | |
1301 unsigned char *phase; | |
1302 | |
1303 phase = XNEWVEC (unsigned char, n); | |
1304 for (i = 0; i < n; ++i) | |
1305 phase[i] = stack_protect_decl_phase (stack_vars[i].decl); | |
1306 | |
1307 for (i = 0; i < n; ++i) | |
1308 { | |
1309 unsigned char ph_i = phase[i]; | |
1310 for (j = 0; j < i; ++j) | |
1311 if (ph_i != phase[j]) | |
1312 add_stack_var_conflict (i, j); | |
1313 } | |
1314 | |
1315 XDELETEVEC (phase); | |
1316 } | |
1317 | |
1318 /* Create a decl for the guard at the top of the stack frame. */ | |
1319 | |
1320 static void | |
1321 create_stack_guard (void) | |
1322 { | |
1323 tree guard = build_decl (VAR_DECL, NULL, ptr_type_node); | |
1324 TREE_THIS_VOLATILE (guard) = 1; | |
1325 TREE_USED (guard) = 1; | |
1326 expand_one_stack_var (guard); | |
1327 crtl->stack_protect_guard = guard; | |
1328 } | |
1329 | |
1330 /* A subroutine of expand_used_vars. Walk down through the BLOCK tree | |
1331 expanding variables. Those variables that can be put into registers | |
1332 are allocated pseudos; those that can't are put on the stack. | |
1333 | |
1334 TOPLEVEL is true if this is the outermost BLOCK. */ | |
1335 | |
1336 static HOST_WIDE_INT | |
1337 account_used_vars_for_block (tree block, bool toplevel) | |
1338 { | |
1339 size_t i, j, old_sv_num, this_sv_num, new_sv_num; | |
1340 tree t; | |
1341 HOST_WIDE_INT size = 0; | |
1342 | |
1343 old_sv_num = toplevel ? 0 : stack_vars_num; | |
1344 | |
1345 /* Expand all variables at this level. */ | |
1346 for (t = BLOCK_VARS (block); t ; t = TREE_CHAIN (t)) | |
1347 if (TREE_USED (t)) | |
1348 size += expand_one_var (t, toplevel, false); | |
1349 | |
1350 this_sv_num = stack_vars_num; | |
1351 | |
1352 /* Expand all variables at containing levels. */ | |
1353 for (t = BLOCK_SUBBLOCKS (block); t ; t = BLOCK_CHAIN (t)) | |
1354 size += account_used_vars_for_block (t, false); | |
1355 | |
1356 /* Since we do not track exact variable lifetimes (which is not even | |
1357 possible for variables whose address escapes), we mirror the block | |
1358 tree in the interference graph. Here we cause all variables at this | |
1359 level, and all sublevels, to conflict. Do make certain that a | |
1360 variable conflicts with itself. */ | |
1361 if (old_sv_num < this_sv_num) | |
1362 { | |
1363 new_sv_num = stack_vars_num; | |
1364 resize_stack_vars_conflict (new_sv_num); | |
1365 | |
1366 for (i = old_sv_num; i < new_sv_num; ++i) | |
1367 for (j = i < this_sv_num ? i+1 : this_sv_num; j-- > old_sv_num ;) | |
1368 add_stack_var_conflict (i, j); | |
1369 } | |
1370 return size; | |
1371 } | |
1372 | |
1373 /* Prepare for expanding variables. */ | |
1374 static void | |
1375 init_vars_expansion (void) | |
1376 { | |
1377 tree t; | |
1378 /* Set TREE_USED on all variables in the local_decls. */ | |
1379 for (t = cfun->local_decls; t; t = TREE_CHAIN (t)) | |
1380 TREE_USED (TREE_VALUE (t)) = 1; | |
1381 | |
1382 /* Clear TREE_USED on all variables associated with a block scope. */ | |
1383 clear_tree_used (DECL_INITIAL (current_function_decl)); | |
1384 | |
1385 /* Initialize local stack smashing state. */ | |
1386 has_protected_decls = false; | |
1387 has_short_buffer = false; | |
1388 } | |
1389 | |
1390 /* Free up stack variable graph data. */ | |
1391 static void | |
1392 fini_vars_expansion (void) | |
1393 { | |
1394 XDELETEVEC (stack_vars); | |
1395 XDELETEVEC (stack_vars_sorted); | |
1396 XDELETEVEC (stack_vars_conflict); | |
1397 stack_vars = NULL; | |
1398 stack_vars_alloc = stack_vars_num = 0; | |
1399 stack_vars_conflict = NULL; | |
1400 stack_vars_conflict_alloc = 0; | |
1401 } | |
1402 | |
1403 /* Make a fair guess for the size of the stack frame of the current | |
1404 function. This doesn't have to be exact, the result is only used | |
1405 in the inline heuristics. So we don't want to run the full stack | |
1406 var packing algorithm (which is quadratic in the number of stack | |
1407 vars). Instead, we calculate the total size of all stack vars. | |
1408 This turns out to be a pretty fair estimate -- packing of stack | |
1409 vars doesn't happen very often. */ | |
1410 | |
1411 HOST_WIDE_INT | |
1412 estimated_stack_frame_size (void) | |
1413 { | |
1414 HOST_WIDE_INT size = 0; | |
1415 size_t i; | |
1416 tree t, outer_block = DECL_INITIAL (current_function_decl); | |
1417 | |
1418 init_vars_expansion (); | |
1419 | |
1420 for (t = cfun->local_decls; t; t = TREE_CHAIN (t)) | |
1421 { | |
1422 tree var = TREE_VALUE (t); | |
1423 | |
1424 if (TREE_USED (var)) | |
1425 size += expand_one_var (var, true, false); | |
1426 TREE_USED (var) = 1; | |
1427 } | |
1428 size += account_used_vars_for_block (outer_block, true); | |
1429 | |
1430 if (stack_vars_num > 0) | |
1431 { | |
1432 /* Fake sorting the stack vars for account_stack_vars (). */ | |
1433 stack_vars_sorted = XNEWVEC (size_t, stack_vars_num); | |
1434 for (i = 0; i < stack_vars_num; ++i) | |
1435 stack_vars_sorted[i] = i; | |
1436 size += account_stack_vars (); | |
1437 fini_vars_expansion (); | |
1438 } | |
1439 | |
1440 return size; | |
1441 } | |
1442 | |
1443 /* Expand all variables used in the function. */ | |
1444 | |
1445 static void | |
1446 expand_used_vars (void) | |
1447 { | |
1448 tree t, next, outer_block = DECL_INITIAL (current_function_decl); | |
1449 | |
1450 /* Compute the phase of the stack frame for this function. */ | |
1451 { | |
1452 int align = PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT; | |
1453 int off = STARTING_FRAME_OFFSET % align; | |
1454 frame_phase = off ? align - off : 0; | |
1455 } | |
1456 | |
1457 init_vars_expansion (); | |
1458 | |
1459 /* At this point all variables on the local_decls with TREE_USED | |
1460 set are not associated with any block scope. Lay them out. */ | |
1461 t = cfun->local_decls; | |
1462 cfun->local_decls = NULL_TREE; | |
1463 for (; t; t = next) | |
1464 { | |
1465 tree var = TREE_VALUE (t); | |
1466 bool expand_now = false; | |
1467 | |
1468 next = TREE_CHAIN (t); | |
1469 | |
1470 /* We didn't set a block for static or extern because it's hard | |
1471 to tell the difference between a global variable (re)declared | |
1472 in a local scope, and one that's really declared there to | |
1473 begin with. And it doesn't really matter much, since we're | |
1474 not giving them stack space. Expand them now. */ | |
1475 if (TREE_STATIC (var) || DECL_EXTERNAL (var)) | |
1476 expand_now = true; | |
1477 | |
1478 /* Any variable that could have been hoisted into an SSA_NAME | |
1479 will have been propagated anywhere the optimizers chose, | |
1480 i.e. not confined to their original block. Allocate them | |
1481 as if they were defined in the outermost scope. */ | |
1482 else if (is_gimple_reg (var)) | |
1483 expand_now = true; | |
1484 | |
1485 /* If the variable is not associated with any block, then it | |
1486 was created by the optimizers, and could be live anywhere | |
1487 in the function. */ | |
1488 else if (TREE_USED (var)) | |
1489 expand_now = true; | |
1490 | |
1491 /* Finally, mark all variables on the list as used. We'll use | |
1492 this in a moment when we expand those associated with scopes. */ | |
1493 TREE_USED (var) = 1; | |
1494 | |
1495 if (expand_now) | |
1496 { | |
1497 expand_one_var (var, true, true); | |
1498 if (DECL_ARTIFICIAL (var) && !DECL_IGNORED_P (var)) | |
1499 { | |
1500 rtx rtl = DECL_RTL_IF_SET (var); | |
1501 | |
1502 /* Keep artificial non-ignored vars in cfun->local_decls | |
1503 chain until instantiate_decls. */ | |
1504 if (rtl && (MEM_P (rtl) || GET_CODE (rtl) == CONCAT)) | |
1505 { | |
1506 TREE_CHAIN (t) = cfun->local_decls; | |
1507 cfun->local_decls = t; | |
1508 continue; | |
1509 } | |
1510 } | |
1511 } | |
1512 | |
1513 ggc_free (t); | |
1514 } | |
1515 | |
1516 /* At this point, all variables within the block tree with TREE_USED | |
1517 set are actually used by the optimized function. Lay them out. */ | |
1518 expand_used_vars_for_block (outer_block, true); | |
1519 | |
1520 if (stack_vars_num > 0) | |
1521 { | |
1522 /* Due to the way alias sets work, no variables with non-conflicting | |
1523 alias sets may be assigned the same address. Add conflicts to | |
1524 reflect this. */ | |
1525 add_alias_set_conflicts (); | |
1526 | |
1527 /* If stack protection is enabled, we don't share space between | |
1528 vulnerable data and non-vulnerable data. */ | |
1529 if (flag_stack_protect) | |
1530 add_stack_protection_conflicts (); | |
1531 | |
1532 /* Now that we have collected all stack variables, and have computed a | |
1533 minimal interference graph, attempt to save some stack space. */ | |
1534 partition_stack_vars (); | |
1535 if (dump_file) | |
1536 dump_stack_var_partition (); | |
1537 } | |
1538 | |
1539 /* There are several conditions under which we should create a | |
1540 stack guard: protect-all, alloca used, protected decls present. */ | |
1541 if (flag_stack_protect == 2 | |
1542 || (flag_stack_protect | |
1543 && (cfun->calls_alloca || has_protected_decls))) | |
1544 create_stack_guard (); | |
1545 | |
1546 /* Assign rtl to each variable based on these partitions. */ | |
1547 if (stack_vars_num > 0) | |
1548 { | |
1549 /* Reorder decls to be protected by iterating over the variables | |
1550 array multiple times, and allocating out of each phase in turn. */ | |
1551 /* ??? We could probably integrate this into the qsort we did | |
1552 earlier, such that we naturally see these variables first, | |
1553 and thus naturally allocate things in the right order. */ | |
1554 if (has_protected_decls) | |
1555 { | |
1556 /* Phase 1 contains only character arrays. */ | |
1557 expand_stack_vars (stack_protect_decl_phase_1); | |
1558 | |
1559 /* Phase 2 contains other kinds of arrays. */ | |
1560 if (flag_stack_protect == 2) | |
1561 expand_stack_vars (stack_protect_decl_phase_2); | |
1562 } | |
1563 | |
1564 expand_stack_vars (NULL); | |
1565 | |
1566 fini_vars_expansion (); | |
1567 } | |
1568 | |
1569 /* If the target requires that FRAME_OFFSET be aligned, do it. */ | |
1570 if (STACK_ALIGNMENT_NEEDED) | |
1571 { | |
1572 HOST_WIDE_INT align = PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT; | |
1573 if (!FRAME_GROWS_DOWNWARD) | |
1574 frame_offset += align - 1; | |
1575 frame_offset &= -align; | |
1576 } | |
1577 } | |
1578 | |
1579 | |
1580 /* If we need to produce a detailed dump, print the tree representation | |
1581 for STMT to the dump file. SINCE is the last RTX after which the RTL | |
1582 generated for STMT should have been appended. */ | |
1583 | |
1584 static void | |
1585 maybe_dump_rtl_for_gimple_stmt (gimple stmt, rtx since) | |
1586 { | |
1587 if (dump_file && (dump_flags & TDF_DETAILS)) | |
1588 { | |
1589 fprintf (dump_file, "\n;; "); | |
1590 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM); | |
1591 fprintf (dump_file, "\n"); | |
1592 | |
1593 print_rtl (dump_file, since ? NEXT_INSN (since) : since); | |
1594 } | |
1595 } | |
1596 | |
1597 /* Maps the blocks that do not contain tree labels to rtx labels. */ | |
1598 | |
1599 static struct pointer_map_t *lab_rtx_for_bb; | |
1600 | |
1601 /* Returns the label_rtx expression for a label starting basic block BB. */ | |
1602 | |
1603 static rtx | |
1604 label_rtx_for_bb (basic_block bb ATTRIBUTE_UNUSED) | |
1605 { | |
1606 gimple_stmt_iterator gsi; | |
1607 tree lab; | |
1608 gimple lab_stmt; | |
1609 void **elt; | |
1610 | |
1611 if (bb->flags & BB_RTL) | |
1612 return block_label (bb); | |
1613 | |
1614 elt = pointer_map_contains (lab_rtx_for_bb, bb); | |
1615 if (elt) | |
1616 return (rtx) *elt; | |
1617 | |
1618 /* Find the tree label if it is present. */ | |
1619 | |
1620 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi)) | |
1621 { | |
1622 lab_stmt = gsi_stmt (gsi); | |
1623 if (gimple_code (lab_stmt) != GIMPLE_LABEL) | |
1624 break; | |
1625 | |
1626 lab = gimple_label_label (lab_stmt); | |
1627 if (DECL_NONLOCAL (lab)) | |
1628 break; | |
1629 | |
1630 return label_rtx (lab); | |
1631 } | |
1632 | |
1633 elt = pointer_map_insert (lab_rtx_for_bb, bb); | |
1634 *elt = gen_label_rtx (); | |
1635 return (rtx) *elt; | |
1636 } | |
1637 | |
1638 | |
1639 /* A subroutine of expand_gimple_basic_block. Expand one GIMPLE_COND. | |
1640 Returns a new basic block if we've terminated the current basic | |
1641 block and created a new one. */ | |
1642 | |
1643 static basic_block | |
1644 expand_gimple_cond (basic_block bb, gimple stmt) | |
1645 { | |
1646 basic_block new_bb, dest; | |
1647 edge new_edge; | |
1648 edge true_edge; | |
1649 edge false_edge; | |
1650 tree pred = gimple_cond_pred_to_tree (stmt); | |
1651 rtx last2, last; | |
1652 | |
1653 last2 = last = get_last_insn (); | |
1654 | |
1655 extract_true_false_edges_from_block (bb, &true_edge, &false_edge); | |
1656 if (gimple_has_location (stmt)) | |
1657 { | |
1658 set_curr_insn_source_location (gimple_location (stmt)); | |
1659 set_curr_insn_block (gimple_block (stmt)); | |
1660 } | |
1661 | |
1662 /* These flags have no purpose in RTL land. */ | |
1663 true_edge->flags &= ~EDGE_TRUE_VALUE; | |
1664 false_edge->flags &= ~EDGE_FALSE_VALUE; | |
1665 | |
1666 /* We can either have a pure conditional jump with one fallthru edge or | |
1667 two-way jump that needs to be decomposed into two basic blocks. */ | |
1668 if (false_edge->dest == bb->next_bb) | |
1669 { | |
1670 jumpif (pred, label_rtx_for_bb (true_edge->dest)); | |
1671 add_reg_br_prob_note (last, true_edge->probability); | |
1672 maybe_dump_rtl_for_gimple_stmt (stmt, last); | |
1673 if (true_edge->goto_locus) | |
1674 { | |
1675 set_curr_insn_source_location (true_edge->goto_locus); | |
1676 set_curr_insn_block (true_edge->goto_block); | |
1677 true_edge->goto_locus = curr_insn_locator (); | |
1678 } | |
1679 true_edge->goto_block = NULL; | |
1680 false_edge->flags |= EDGE_FALLTHRU; | |
1681 ggc_free (pred); | |
1682 return NULL; | |
1683 } | |
1684 if (true_edge->dest == bb->next_bb) | |
1685 { | |
1686 jumpifnot (pred, label_rtx_for_bb (false_edge->dest)); | |
1687 add_reg_br_prob_note (last, false_edge->probability); | |
1688 maybe_dump_rtl_for_gimple_stmt (stmt, last); | |
1689 if (false_edge->goto_locus) | |
1690 { | |
1691 set_curr_insn_source_location (false_edge->goto_locus); | |
1692 set_curr_insn_block (false_edge->goto_block); | |
1693 false_edge->goto_locus = curr_insn_locator (); | |
1694 } | |
1695 false_edge->goto_block = NULL; | |
1696 true_edge->flags |= EDGE_FALLTHRU; | |
1697 ggc_free (pred); | |
1698 return NULL; | |
1699 } | |
1700 | |
1701 jumpif (pred, label_rtx_for_bb (true_edge->dest)); | |
1702 add_reg_br_prob_note (last, true_edge->probability); | |
1703 last = get_last_insn (); | |
1704 if (false_edge->goto_locus) | |
1705 { | |
1706 set_curr_insn_source_location (false_edge->goto_locus); | |
1707 set_curr_insn_block (false_edge->goto_block); | |
1708 false_edge->goto_locus = curr_insn_locator (); | |
1709 } | |
1710 false_edge->goto_block = NULL; | |
1711 emit_jump (label_rtx_for_bb (false_edge->dest)); | |
1712 | |
1713 BB_END (bb) = last; | |
1714 if (BARRIER_P (BB_END (bb))) | |
1715 BB_END (bb) = PREV_INSN (BB_END (bb)); | |
1716 update_bb_for_insn (bb); | |
1717 | |
1718 new_bb = create_basic_block (NEXT_INSN (last), get_last_insn (), bb); | |
1719 dest = false_edge->dest; | |
1720 redirect_edge_succ (false_edge, new_bb); | |
1721 false_edge->flags |= EDGE_FALLTHRU; | |
1722 new_bb->count = false_edge->count; | |
1723 new_bb->frequency = EDGE_FREQUENCY (false_edge); | |
1724 new_edge = make_edge (new_bb, dest, 0); | |
1725 new_edge->probability = REG_BR_PROB_BASE; | |
1726 new_edge->count = new_bb->count; | |
1727 if (BARRIER_P (BB_END (new_bb))) | |
1728 BB_END (new_bb) = PREV_INSN (BB_END (new_bb)); | |
1729 update_bb_for_insn (new_bb); | |
1730 | |
1731 maybe_dump_rtl_for_gimple_stmt (stmt, last2); | |
1732 | |
1733 if (true_edge->goto_locus) | |
1734 { | |
1735 set_curr_insn_source_location (true_edge->goto_locus); | |
1736 set_curr_insn_block (true_edge->goto_block); | |
1737 true_edge->goto_locus = curr_insn_locator (); | |
1738 } | |
1739 true_edge->goto_block = NULL; | |
1740 | |
1741 ggc_free (pred); | |
1742 return new_bb; | |
1743 } | |
1744 | |
1745 /* A subroutine of expand_gimple_basic_block. Expand one GIMPLE_CALL | |
1746 that has CALL_EXPR_TAILCALL set. Returns non-null if we actually | |
1747 generated a tail call (something that might be denied by the ABI | |
1748 rules governing the call; see calls.c). | |
1749 | |
1750 Sets CAN_FALLTHRU if we generated a *conditional* tail call, and | |
1751 can still reach the rest of BB. The case here is __builtin_sqrt, | |
1752 where the NaN result goes through the external function (with a | |
1753 tailcall) and the normal result happens via a sqrt instruction. */ | |
1754 | |
1755 static basic_block | |
1756 expand_gimple_tailcall (basic_block bb, gimple stmt, bool *can_fallthru) | |
1757 { | |
1758 rtx last2, last; | |
1759 edge e; | |
1760 edge_iterator ei; | |
1761 int probability; | |
1762 gcov_type count; | |
1763 tree stmt_tree = gimple_to_tree (stmt); | |
1764 | |
1765 last2 = last = get_last_insn (); | |
1766 | |
1767 expand_expr_stmt (stmt_tree); | |
1768 | |
1769 release_stmt_tree (stmt, stmt_tree); | |
1770 | |
1771 for (last = NEXT_INSN (last); last; last = NEXT_INSN (last)) | |
1772 if (CALL_P (last) && SIBLING_CALL_P (last)) | |
1773 goto found; | |
1774 | |
1775 maybe_dump_rtl_for_gimple_stmt (stmt, last2); | |
1776 | |
1777 *can_fallthru = true; | |
1778 return NULL; | |
1779 | |
1780 found: | |
1781 /* ??? Wouldn't it be better to just reset any pending stack adjust? | |
1782 Any instructions emitted here are about to be deleted. */ | |
1783 do_pending_stack_adjust (); | |
1784 | |
1785 /* Remove any non-eh, non-abnormal edges that don't go to exit. */ | |
1786 /* ??? I.e. the fallthrough edge. HOWEVER! If there were to be | |
1787 EH or abnormal edges, we shouldn't have created a tail call in | |
1788 the first place. So it seems to me we should just be removing | |
1789 all edges here, or redirecting the existing fallthru edge to | |
1790 the exit block. */ | |
1791 | |
1792 probability = 0; | |
1793 count = 0; | |
1794 | |
1795 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); ) | |
1796 { | |
1797 if (!(e->flags & (EDGE_ABNORMAL | EDGE_EH))) | |
1798 { | |
1799 if (e->dest != EXIT_BLOCK_PTR) | |
1800 { | |
1801 e->dest->count -= e->count; | |
1802 e->dest->frequency -= EDGE_FREQUENCY (e); | |
1803 if (e->dest->count < 0) | |
1804 e->dest->count = 0; | |
1805 if (e->dest->frequency < 0) | |
1806 e->dest->frequency = 0; | |
1807 } | |
1808 count += e->count; | |
1809 probability += e->probability; | |
1810 remove_edge (e); | |
1811 } | |
1812 else | |
1813 ei_next (&ei); | |
1814 } | |
1815 | |
1816 /* This is somewhat ugly: the call_expr expander often emits instructions | |
1817 after the sibcall (to perform the function return). These confuse the | |
1818 find_many_sub_basic_blocks code, so we need to get rid of these. */ | |
1819 last = NEXT_INSN (last); | |
1820 gcc_assert (BARRIER_P (last)); | |
1821 | |
1822 *can_fallthru = false; | |
1823 while (NEXT_INSN (last)) | |
1824 { | |
1825 /* For instance an sqrt builtin expander expands if with | |
1826 sibcall in the then and label for `else`. */ | |
1827 if (LABEL_P (NEXT_INSN (last))) | |
1828 { | |
1829 *can_fallthru = true; | |
1830 break; | |
1831 } | |
1832 delete_insn (NEXT_INSN (last)); | |
1833 } | |
1834 | |
1835 e = make_edge (bb, EXIT_BLOCK_PTR, EDGE_ABNORMAL | EDGE_SIBCALL); | |
1836 e->probability += probability; | |
1837 e->count += count; | |
1838 BB_END (bb) = last; | |
1839 update_bb_for_insn (bb); | |
1840 | |
1841 if (NEXT_INSN (last)) | |
1842 { | |
1843 bb = create_basic_block (NEXT_INSN (last), get_last_insn (), bb); | |
1844 | |
1845 last = BB_END (bb); | |
1846 if (BARRIER_P (last)) | |
1847 BB_END (bb) = PREV_INSN (last); | |
1848 } | |
1849 | |
1850 maybe_dump_rtl_for_gimple_stmt (stmt, last2); | |
1851 | |
1852 return bb; | |
1853 } | |
1854 | |
1855 /* Expand basic block BB from GIMPLE trees to RTL. */ | |
1856 | |
1857 static basic_block | |
1858 expand_gimple_basic_block (basic_block bb) | |
1859 { | |
1860 gimple_stmt_iterator gsi; | |
1861 gimple_seq stmts; | |
1862 gimple stmt = NULL; | |
1863 rtx note, last; | |
1864 edge e; | |
1865 edge_iterator ei; | |
1866 void **elt; | |
1867 | |
1868 if (dump_file) | |
1869 fprintf (dump_file, "\n;; Generating RTL for gimple basic block %d\n", | |
1870 bb->index); | |
1871 | |
1872 /* Note that since we are now transitioning from GIMPLE to RTL, we | |
1873 cannot use the gsi_*_bb() routines because they expect the basic | |
1874 block to be in GIMPLE, instead of RTL. Therefore, we need to | |
1875 access the BB sequence directly. */ | |
1876 stmts = bb_seq (bb); | |
1877 bb->il.gimple = NULL; | |
1878 rtl_profile_for_bb (bb); | |
1879 init_rtl_bb_info (bb); | |
1880 bb->flags |= BB_RTL; | |
1881 | |
1882 /* Remove the RETURN_EXPR if we may fall though to the exit | |
1883 instead. */ | |
1884 gsi = gsi_last (stmts); | |
1885 if (!gsi_end_p (gsi) | |
1886 && gimple_code (gsi_stmt (gsi)) == GIMPLE_RETURN) | |
1887 { | |
1888 gimple ret_stmt = gsi_stmt (gsi); | |
1889 | |
1890 gcc_assert (single_succ_p (bb)); | |
1891 gcc_assert (single_succ (bb) == EXIT_BLOCK_PTR); | |
1892 | |
1893 if (bb->next_bb == EXIT_BLOCK_PTR | |
1894 && !gimple_return_retval (ret_stmt)) | |
1895 { | |
1896 gsi_remove (&gsi, false); | |
1897 single_succ_edge (bb)->flags |= EDGE_FALLTHRU; | |
1898 } | |
1899 } | |
1900 | |
1901 gsi = gsi_start (stmts); | |
1902 if (!gsi_end_p (gsi)) | |
1903 { | |
1904 stmt = gsi_stmt (gsi); | |
1905 if (gimple_code (stmt) != GIMPLE_LABEL) | |
1906 stmt = NULL; | |
1907 } | |
1908 | |
1909 elt = pointer_map_contains (lab_rtx_for_bb, bb); | |
1910 | |
1911 if (stmt || elt) | |
1912 { | |
1913 last = get_last_insn (); | |
1914 | |
1915 if (stmt) | |
1916 { | |
1917 tree stmt_tree = gimple_to_tree (stmt); | |
1918 expand_expr_stmt (stmt_tree); | |
1919 release_stmt_tree (stmt, stmt_tree); | |
1920 gsi_next (&gsi); | |
1921 } | |
1922 | |
1923 if (elt) | |
1924 emit_label ((rtx) *elt); | |
1925 | |
1926 /* Java emits line number notes in the top of labels. | |
1927 ??? Make this go away once line number notes are obsoleted. */ | |
1928 BB_HEAD (bb) = NEXT_INSN (last); | |
1929 if (NOTE_P (BB_HEAD (bb))) | |
1930 BB_HEAD (bb) = NEXT_INSN (BB_HEAD (bb)); | |
1931 note = emit_note_after (NOTE_INSN_BASIC_BLOCK, BB_HEAD (bb)); | |
1932 | |
1933 maybe_dump_rtl_for_gimple_stmt (stmt, last); | |
1934 } | |
1935 else | |
1936 note = BB_HEAD (bb) = emit_note (NOTE_INSN_BASIC_BLOCK); | |
1937 | |
1938 NOTE_BASIC_BLOCK (note) = bb; | |
1939 | |
1940 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); ) | |
1941 { | |
1942 /* Clear EDGE_EXECUTABLE. This flag is never used in the backend. */ | |
1943 e->flags &= ~EDGE_EXECUTABLE; | |
1944 | |
1945 /* At the moment not all abnormal edges match the RTL representation. | |
1946 It is safe to remove them here as find_many_sub_basic_blocks will | |
1947 rediscover them. In the future we should get this fixed properly. */ | |
1948 if (e->flags & EDGE_ABNORMAL) | |
1949 remove_edge (e); | |
1950 else | |
1951 ei_next (&ei); | |
1952 } | |
1953 | |
1954 for (; !gsi_end_p (gsi); gsi_next (&gsi)) | |
1955 { | |
1956 gimple stmt = gsi_stmt (gsi); | |
1957 basic_block new_bb; | |
1958 | |
1959 /* Expand this statement, then evaluate the resulting RTL and | |
1960 fixup the CFG accordingly. */ | |
1961 if (gimple_code (stmt) == GIMPLE_COND) | |
1962 { | |
1963 new_bb = expand_gimple_cond (bb, stmt); | |
1964 if (new_bb) | |
1965 return new_bb; | |
1966 } | |
1967 else | |
1968 { | |
1969 if (is_gimple_call (stmt) && gimple_call_tail_p (stmt)) | |
1970 { | |
1971 bool can_fallthru; | |
1972 new_bb = expand_gimple_tailcall (bb, stmt, &can_fallthru); | |
1973 if (new_bb) | |
1974 { | |
1975 if (can_fallthru) | |
1976 bb = new_bb; | |
1977 else | |
1978 return new_bb; | |
1979 } | |
1980 } | |
1981 else if (gimple_code (stmt) != GIMPLE_CHANGE_DYNAMIC_TYPE) | |
1982 { | |
1983 tree stmt_tree = gimple_to_tree (stmt); | |
1984 last = get_last_insn (); | |
1985 expand_expr_stmt (stmt_tree); | |
1986 maybe_dump_rtl_for_gimple_stmt (stmt, last); | |
1987 release_stmt_tree (stmt, stmt_tree); | |
1988 } | |
1989 } | |
1990 } | |
1991 | |
1992 /* Expand implicit goto and convert goto_locus. */ | |
1993 FOR_EACH_EDGE (e, ei, bb->succs) | |
1994 { | |
1995 if (e->goto_locus && e->goto_block) | |
1996 { | |
1997 set_curr_insn_source_location (e->goto_locus); | |
1998 set_curr_insn_block (e->goto_block); | |
1999 e->goto_locus = curr_insn_locator (); | |
2000 } | |
2001 e->goto_block = NULL; | |
2002 if ((e->flags & EDGE_FALLTHRU) && e->dest != bb->next_bb) | |
2003 { | |
2004 emit_jump (label_rtx_for_bb (e->dest)); | |
2005 e->flags &= ~EDGE_FALLTHRU; | |
2006 } | |
2007 } | |
2008 | |
2009 do_pending_stack_adjust (); | |
2010 | |
2011 /* Find the block tail. The last insn in the block is the insn | |
2012 before a barrier and/or table jump insn. */ | |
2013 last = get_last_insn (); | |
2014 if (BARRIER_P (last)) | |
2015 last = PREV_INSN (last); | |
2016 if (JUMP_TABLE_DATA_P (last)) | |
2017 last = PREV_INSN (PREV_INSN (last)); | |
2018 BB_END (bb) = last; | |
2019 | |
2020 update_bb_for_insn (bb); | |
2021 | |
2022 return bb; | |
2023 } | |
2024 | |
2025 | |
2026 /* Create a basic block for initialization code. */ | |
2027 | |
2028 static basic_block | |
2029 construct_init_block (void) | |
2030 { | |
2031 basic_block init_block, first_block; | |
2032 edge e = NULL; | |
2033 int flags; | |
2034 | |
2035 /* Multiple entry points not supported yet. */ | |
2036 gcc_assert (EDGE_COUNT (ENTRY_BLOCK_PTR->succs) == 1); | |
2037 init_rtl_bb_info (ENTRY_BLOCK_PTR); | |
2038 init_rtl_bb_info (EXIT_BLOCK_PTR); | |
2039 ENTRY_BLOCK_PTR->flags |= BB_RTL; | |
2040 EXIT_BLOCK_PTR->flags |= BB_RTL; | |
2041 | |
2042 e = EDGE_SUCC (ENTRY_BLOCK_PTR, 0); | |
2043 | |
2044 /* When entry edge points to first basic block, we don't need jump, | |
2045 otherwise we have to jump into proper target. */ | |
2046 if (e && e->dest != ENTRY_BLOCK_PTR->next_bb) | |
2047 { | |
2048 tree label = gimple_block_label (e->dest); | |
2049 | |
2050 emit_jump (label_rtx (label)); | |
2051 flags = 0; | |
2052 } | |
2053 else | |
2054 flags = EDGE_FALLTHRU; | |
2055 | |
2056 init_block = create_basic_block (NEXT_INSN (get_insns ()), | |
2057 get_last_insn (), | |
2058 ENTRY_BLOCK_PTR); | |
2059 init_block->frequency = ENTRY_BLOCK_PTR->frequency; | |
2060 init_block->count = ENTRY_BLOCK_PTR->count; | |
2061 if (e) | |
2062 { | |
2063 first_block = e->dest; | |
2064 redirect_edge_succ (e, init_block); | |
2065 e = make_edge (init_block, first_block, flags); | |
2066 } | |
2067 else | |
2068 e = make_edge (init_block, EXIT_BLOCK_PTR, EDGE_FALLTHRU); | |
2069 e->probability = REG_BR_PROB_BASE; | |
2070 e->count = ENTRY_BLOCK_PTR->count; | |
2071 | |
2072 update_bb_for_insn (init_block); | |
2073 return init_block; | |
2074 } | |
2075 | |
2076 /* For each lexical block, set BLOCK_NUMBER to the depth at which it is | |
2077 found in the block tree. */ | |
2078 | |
2079 static void | |
2080 set_block_levels (tree block, int level) | |
2081 { | |
2082 while (block) | |
2083 { | |
2084 BLOCK_NUMBER (block) = level; | |
2085 set_block_levels (BLOCK_SUBBLOCKS (block), level + 1); | |
2086 block = BLOCK_CHAIN (block); | |
2087 } | |
2088 } | |
2089 | |
2090 /* Create a block containing landing pads and similar stuff. */ | |
2091 | |
2092 static void | |
2093 construct_exit_block (void) | |
2094 { | |
2095 rtx head = get_last_insn (); | |
2096 rtx end; | |
2097 basic_block exit_block; | |
2098 edge e, e2; | |
2099 unsigned ix; | |
2100 edge_iterator ei; | |
2101 rtx orig_end = BB_END (EXIT_BLOCK_PTR->prev_bb); | |
2102 | |
2103 rtl_profile_for_bb (EXIT_BLOCK_PTR); | |
2104 | |
2105 /* Make sure the locus is set to the end of the function, so that | |
2106 epilogue line numbers and warnings are set properly. */ | |
2107 if (cfun->function_end_locus != UNKNOWN_LOCATION) | |
2108 input_location = cfun->function_end_locus; | |
2109 | |
2110 /* The following insns belong to the top scope. */ | |
2111 set_curr_insn_block (DECL_INITIAL (current_function_decl)); | |
2112 | |
2113 /* Generate rtl for function exit. */ | |
2114 expand_function_end (); | |
2115 | |
2116 end = get_last_insn (); | |
2117 if (head == end) | |
2118 return; | |
2119 /* While emitting the function end we could move end of the last basic block. | |
2120 */ | |
2121 BB_END (EXIT_BLOCK_PTR->prev_bb) = orig_end; | |
2122 while (NEXT_INSN (head) && NOTE_P (NEXT_INSN (head))) | |
2123 head = NEXT_INSN (head); | |
2124 exit_block = create_basic_block (NEXT_INSN (head), end, | |
2125 EXIT_BLOCK_PTR->prev_bb); | |
2126 exit_block->frequency = EXIT_BLOCK_PTR->frequency; | |
2127 exit_block->count = EXIT_BLOCK_PTR->count; | |
2128 | |
2129 ix = 0; | |
2130 while (ix < EDGE_COUNT (EXIT_BLOCK_PTR->preds)) | |
2131 { | |
2132 e = EDGE_PRED (EXIT_BLOCK_PTR, ix); | |
2133 if (!(e->flags & EDGE_ABNORMAL)) | |
2134 redirect_edge_succ (e, exit_block); | |
2135 else | |
2136 ix++; | |
2137 } | |
2138 | |
2139 e = make_edge (exit_block, EXIT_BLOCK_PTR, EDGE_FALLTHRU); | |
2140 e->probability = REG_BR_PROB_BASE; | |
2141 e->count = EXIT_BLOCK_PTR->count; | |
2142 FOR_EACH_EDGE (e2, ei, EXIT_BLOCK_PTR->preds) | |
2143 if (e2 != e) | |
2144 { | |
2145 e->count -= e2->count; | |
2146 exit_block->count -= e2->count; | |
2147 exit_block->frequency -= EDGE_FREQUENCY (e2); | |
2148 } | |
2149 if (e->count < 0) | |
2150 e->count = 0; | |
2151 if (exit_block->count < 0) | |
2152 exit_block->count = 0; | |
2153 if (exit_block->frequency < 0) | |
2154 exit_block->frequency = 0; | |
2155 update_bb_for_insn (exit_block); | |
2156 } | |
2157 | |
2158 /* Helper function for discover_nonconstant_array_refs. | |
2159 Look for ARRAY_REF nodes with non-constant indexes and mark them | |
2160 addressable. */ | |
2161 | |
2162 static tree | |
2163 discover_nonconstant_array_refs_r (tree * tp, int *walk_subtrees, | |
2164 void *data ATTRIBUTE_UNUSED) | |
2165 { | |
2166 tree t = *tp; | |
2167 | |
2168 if (IS_TYPE_OR_DECL_P (t)) | |
2169 *walk_subtrees = 0; | |
2170 else if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF) | |
2171 { | |
2172 while (((TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF) | |
2173 && is_gimple_min_invariant (TREE_OPERAND (t, 1)) | |
2174 && (!TREE_OPERAND (t, 2) | |
2175 || is_gimple_min_invariant (TREE_OPERAND (t, 2)))) | |
2176 || (TREE_CODE (t) == COMPONENT_REF | |
2177 && (!TREE_OPERAND (t,2) | |
2178 || is_gimple_min_invariant (TREE_OPERAND (t, 2)))) | |
2179 || TREE_CODE (t) == BIT_FIELD_REF | |
2180 || TREE_CODE (t) == REALPART_EXPR | |
2181 || TREE_CODE (t) == IMAGPART_EXPR | |
2182 || TREE_CODE (t) == VIEW_CONVERT_EXPR | |
2183 || CONVERT_EXPR_P (t)) | |
2184 t = TREE_OPERAND (t, 0); | |
2185 | |
2186 if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF) | |
2187 { | |
2188 t = get_base_address (t); | |
2189 if (t && DECL_P (t)) | |
2190 TREE_ADDRESSABLE (t) = 1; | |
2191 } | |
2192 | |
2193 *walk_subtrees = 0; | |
2194 } | |
2195 | |
2196 return NULL_TREE; | |
2197 } | |
2198 | |
2199 /* RTL expansion is not able to compile array references with variable | |
2200 offsets for arrays stored in single register. Discover such | |
2201 expressions and mark variables as addressable to avoid this | |
2202 scenario. */ | |
2203 | |
2204 static void | |
2205 discover_nonconstant_array_refs (void) | |
2206 { | |
2207 basic_block bb; | |
2208 gimple_stmt_iterator gsi; | |
2209 | |
2210 FOR_EACH_BB (bb) | |
2211 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi)) | |
2212 { | |
2213 gimple stmt = gsi_stmt (gsi); | |
2214 walk_gimple_op (stmt, discover_nonconstant_array_refs_r, NULL); | |
2215 } | |
2216 } | |
2217 | |
2218 /* This function sets crtl->args.internal_arg_pointer to a virtual | |
2219 register if DRAP is needed. Local register allocator will replace | |
2220 virtual_incoming_args_rtx with the virtual register. */ | |
2221 | |
2222 static void | |
2223 expand_stack_alignment (void) | |
2224 { | |
2225 rtx drap_rtx; | |
2226 unsigned int preferred_stack_boundary; | |
2227 | |
2228 if (! SUPPORTS_STACK_ALIGNMENT) | |
2229 return; | |
2230 | |
2231 if (cfun->calls_alloca | |
2232 || cfun->has_nonlocal_label | |
2233 || crtl->has_nonlocal_goto) | |
2234 crtl->need_drap = true; | |
2235 | |
2236 gcc_assert (crtl->stack_alignment_needed | |
2237 <= crtl->stack_alignment_estimated); | |
2238 | |
2239 /* Update crtl->stack_alignment_estimated and use it later to align | |
2240 stack. We check PREFERRED_STACK_BOUNDARY if there may be non-call | |
2241 exceptions since callgraph doesn't collect incoming stack alignment | |
2242 in this case. */ | |
2243 if (flag_non_call_exceptions | |
2244 && PREFERRED_STACK_BOUNDARY > crtl->preferred_stack_boundary) | |
2245 preferred_stack_boundary = PREFERRED_STACK_BOUNDARY; | |
2246 else | |
2247 preferred_stack_boundary = crtl->preferred_stack_boundary; | |
2248 if (preferred_stack_boundary > crtl->stack_alignment_estimated) | |
2249 crtl->stack_alignment_estimated = preferred_stack_boundary; | |
2250 if (preferred_stack_boundary > crtl->stack_alignment_needed) | |
2251 crtl->stack_alignment_needed = preferred_stack_boundary; | |
2252 | |
2253 crtl->stack_realign_needed | |
2254 = INCOMING_STACK_BOUNDARY < crtl->stack_alignment_estimated; | |
2255 crtl->stack_realign_tried = crtl->stack_realign_needed; | |
2256 | |
2257 crtl->stack_realign_processed = true; | |
2258 | |
2259 /* Target has to redefine TARGET_GET_DRAP_RTX to support stack | |
2260 alignment. */ | |
2261 gcc_assert (targetm.calls.get_drap_rtx != NULL); | |
2262 drap_rtx = targetm.calls.get_drap_rtx (); | |
2263 | |
2264 /* stack_realign_drap and drap_rtx must match. */ | |
2265 gcc_assert ((stack_realign_drap != 0) == (drap_rtx != NULL)); | |
2266 | |
2267 /* Do nothing if NULL is returned, which means DRAP is not needed. */ | |
2268 if (NULL != drap_rtx) | |
2269 { | |
2270 crtl->args.internal_arg_pointer = drap_rtx; | |
2271 | |
2272 /* Call fixup_tail_calls to clean up REG_EQUIV note if DRAP is | |
2273 needed. */ | |
2274 fixup_tail_calls (); | |
2275 } | |
2276 } | |
2277 | |
2278 /* Translate the intermediate representation contained in the CFG | |
2279 from GIMPLE trees to RTL. | |
2280 | |
2281 We do conversion per basic block and preserve/update the tree CFG. | |
2282 This implies we have to do some magic as the CFG can simultaneously | |
2283 consist of basic blocks containing RTL and GIMPLE trees. This can | |
2284 confuse the CFG hooks, so be careful to not manipulate CFG during | |
2285 the expansion. */ | |
2286 | |
2287 static unsigned int | |
2288 gimple_expand_cfg (void) | |
2289 { | |
2290 basic_block bb, init_block; | |
2291 sbitmap blocks; | |
2292 edge_iterator ei; | |
2293 edge e; | |
2294 | |
2295 /* Some backends want to know that we are expanding to RTL. */ | |
2296 currently_expanding_to_rtl = 1; | |
2297 | |
2298 rtl_profile_for_bb (ENTRY_BLOCK_PTR); | |
2299 | |
2300 insn_locators_alloc (); | |
2301 if (!DECL_BUILT_IN (current_function_decl)) | |
2302 { | |
2303 /* Eventually, all FEs should explicitly set function_start_locus. */ | |
2304 if (cfun->function_start_locus == UNKNOWN_LOCATION) | |
2305 set_curr_insn_source_location | |
2306 (DECL_SOURCE_LOCATION (current_function_decl)); | |
2307 else | |
2308 set_curr_insn_source_location (cfun->function_start_locus); | |
2309 } | |
2310 set_curr_insn_block (DECL_INITIAL (current_function_decl)); | |
2311 prologue_locator = curr_insn_locator (); | |
2312 | |
2313 /* Make sure first insn is a note even if we don't want linenums. | |
2314 This makes sure the first insn will never be deleted. | |
2315 Also, final expects a note to appear there. */ | |
2316 emit_note (NOTE_INSN_DELETED); | |
2317 | |
2318 /* Mark arrays indexed with non-constant indices with TREE_ADDRESSABLE. */ | |
2319 discover_nonconstant_array_refs (); | |
2320 | |
2321 targetm.expand_to_rtl_hook (); | |
2322 crtl->stack_alignment_needed = STACK_BOUNDARY; | |
2323 crtl->max_used_stack_slot_alignment = STACK_BOUNDARY; | |
2324 crtl->stack_alignment_estimated = STACK_BOUNDARY; | |
2325 crtl->preferred_stack_boundary = STACK_BOUNDARY; | |
2326 cfun->cfg->max_jumptable_ents = 0; | |
2327 | |
2328 | |
2329 /* Expand the variables recorded during gimple lowering. */ | |
2330 expand_used_vars (); | |
2331 | |
2332 /* Honor stack protection warnings. */ | |
2333 if (warn_stack_protect) | |
2334 { | |
2335 if (cfun->calls_alloca) | |
2336 warning (OPT_Wstack_protector, | |
2337 "not protecting local variables: variable length buffer"); | |
2338 if (has_short_buffer && !crtl->stack_protect_guard) | |
2339 warning (OPT_Wstack_protector, | |
2340 "not protecting function: no buffer at least %d bytes long", | |
2341 (int) PARAM_VALUE (PARAM_SSP_BUFFER_SIZE)); | |
2342 } | |
2343 | |
2344 /* Set up parameters and prepare for return, for the function. */ | |
2345 expand_function_start (current_function_decl); | |
2346 | |
2347 /* If this function is `main', emit a call to `__main' | |
2348 to run global initializers, etc. */ | |
2349 if (DECL_NAME (current_function_decl) | |
2350 && MAIN_NAME_P (DECL_NAME (current_function_decl)) | |
2351 && DECL_FILE_SCOPE_P (current_function_decl)) | |
2352 expand_main_function (); | |
2353 | |
2354 /* Initialize the stack_protect_guard field. This must happen after the | |
2355 call to __main (if any) so that the external decl is initialized. */ | |
2356 if (crtl->stack_protect_guard) | |
2357 stack_protect_prologue (); | |
2358 | |
2359 /* Update stack boundary if needed. */ | |
2360 if (SUPPORTS_STACK_ALIGNMENT) | |
2361 { | |
2362 /* Call update_stack_boundary here to update incoming stack | |
2363 boundary before TARGET_FUNCTION_OK_FOR_SIBCALL is called. | |
2364 TARGET_FUNCTION_OK_FOR_SIBCALL needs to know the accurate | |
2365 incoming stack alignment to check if it is OK to perform | |
2366 sibcall optimization since sibcall optimization will only | |
2367 align the outgoing stack to incoming stack boundary. */ | |
2368 if (targetm.calls.update_stack_boundary) | |
2369 targetm.calls.update_stack_boundary (); | |
2370 | |
2371 /* The incoming stack frame has to be aligned at least at | |
2372 parm_stack_boundary. */ | |
2373 gcc_assert (crtl->parm_stack_boundary <= INCOMING_STACK_BOUNDARY); | |
2374 } | |
2375 | |
2376 /* Register rtl specific functions for cfg. */ | |
2377 rtl_register_cfg_hooks (); | |
2378 | |
2379 init_block = construct_init_block (); | |
2380 | |
2381 /* Clear EDGE_EXECUTABLE on the entry edge(s). It is cleaned from the | |
2382 remaining edges in expand_gimple_basic_block. */ | |
2383 FOR_EACH_EDGE (e, ei, ENTRY_BLOCK_PTR->succs) | |
2384 e->flags &= ~EDGE_EXECUTABLE; | |
2385 | |
2386 lab_rtx_for_bb = pointer_map_create (); | |
2387 FOR_BB_BETWEEN (bb, init_block->next_bb, EXIT_BLOCK_PTR, next_bb) | |
2388 bb = expand_gimple_basic_block (bb); | |
2389 | |
2390 /* Expansion is used by optimization passes too, set maybe_hot_insn_p | |
2391 conservatively to true until they are all profile aware. */ | |
2392 pointer_map_destroy (lab_rtx_for_bb); | |
2393 free_histograms (); | |
2394 | |
2395 construct_exit_block (); | |
2396 set_curr_insn_block (DECL_INITIAL (current_function_decl)); | |
2397 insn_locators_finalize (); | |
2398 | |
2399 /* We're done expanding trees to RTL. */ | |
2400 currently_expanding_to_rtl = 0; | |
2401 | |
2402 /* Convert tree EH labels to RTL EH labels and zap the tree EH table. */ | |
2403 convert_from_eh_region_ranges (); | |
2404 set_eh_throw_stmt_table (cfun, NULL); | |
2405 | |
2406 rebuild_jump_labels (get_insns ()); | |
2407 find_exception_handler_labels (); | |
2408 | |
2409 blocks = sbitmap_alloc (last_basic_block); | |
2410 sbitmap_ones (blocks); | |
2411 find_many_sub_basic_blocks (blocks); | |
2412 purge_all_dead_edges (); | |
2413 sbitmap_free (blocks); | |
2414 | |
2415 compact_blocks (); | |
2416 | |
2417 expand_stack_alignment (); | |
2418 | |
2419 #ifdef ENABLE_CHECKING | |
2420 verify_flow_info (); | |
2421 #endif | |
2422 | |
2423 /* There's no need to defer outputting this function any more; we | |
2424 know we want to output it. */ | |
2425 DECL_DEFER_OUTPUT (current_function_decl) = 0; | |
2426 | |
2427 /* Now that we're done expanding trees to RTL, we shouldn't have any | |
2428 more CONCATs anywhere. */ | |
2429 generating_concat_p = 0; | |
2430 | |
2431 if (dump_file) | |
2432 { | |
2433 fprintf (dump_file, | |
2434 "\n\n;;\n;; Full RTL generated for this function:\n;;\n"); | |
2435 /* And the pass manager will dump RTL for us. */ | |
2436 } | |
2437 | |
2438 /* If we're emitting a nested function, make sure its parent gets | |
2439 emitted as well. Doing otherwise confuses debug info. */ | |
2440 { | |
2441 tree parent; | |
2442 for (parent = DECL_CONTEXT (current_function_decl); | |
2443 parent != NULL_TREE; | |
2444 parent = get_containing_scope (parent)) | |
2445 if (TREE_CODE (parent) == FUNCTION_DECL) | |
2446 TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (parent)) = 1; | |
2447 } | |
2448 | |
2449 /* We are now committed to emitting code for this function. Do any | |
2450 preparation, such as emitting abstract debug info for the inline | |
2451 before it gets mangled by optimization. */ | |
2452 if (cgraph_function_possibly_inlined_p (current_function_decl)) | |
2453 (*debug_hooks->outlining_inline_function) (current_function_decl); | |
2454 | |
2455 TREE_ASM_WRITTEN (current_function_decl) = 1; | |
2456 | |
2457 /* After expanding, the return labels are no longer needed. */ | |
2458 return_label = NULL; | |
2459 naked_return_label = NULL; | |
2460 /* Tag the blocks with a depth number so that change_scope can find | |
2461 the common parent easily. */ | |
2462 set_block_levels (DECL_INITIAL (cfun->decl), 0); | |
2463 default_rtl_profile (); | |
2464 return 0; | |
2465 } | |
2466 | |
2467 struct rtl_opt_pass pass_expand = | |
2468 { | |
2469 { | |
2470 RTL_PASS, | |
2471 "expand", /* name */ | |
2472 NULL, /* gate */ | |
2473 gimple_expand_cfg, /* execute */ | |
2474 NULL, /* sub */ | |
2475 NULL, /* next */ | |
2476 0, /* static_pass_number */ | |
2477 TV_EXPAND, /* tv_id */ | |
2478 /* ??? If TER is enabled, we actually receive GENERIC. */ | |
2479 PROP_gimple_leh | PROP_cfg, /* properties_required */ | |
2480 PROP_rtl, /* properties_provided */ | |
2481 PROP_trees, /* properties_destroyed */ | |
2482 0, /* todo_flags_start */ | |
2483 TODO_dump_func, /* todo_flags_finish */ | |
2484 } | |
2485 }; |