Mercurial > hg > CbC > CbC_gcc
annotate gcc/cfgexpand.c @ 49:2e19f845ea37
modify
author | kent <kent@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Sun, 07 Feb 2010 17:56:25 +0900 |
parents | 9907f3135723 |
children | 326d9e06c2e3 |
rev | line source |
---|---|
0 | 1 /* A pass for lowering trees to RTL. |
47
3bfb6c00c1e0
update it from 4.4.2 to 4.4.3.
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
19
diff
changeset
|
2 Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010 |
0 | 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 } | |
47
3bfb6c00c1e0
update it from 4.4.2 to 4.4.3.
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
19
diff
changeset
|
1125 else if (!host_integerp (DECL_SIZE_UNIT (var), 1)) |
3bfb6c00c1e0
update it from 4.4.2 to 4.4.3.
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
19
diff
changeset
|
1126 { |
3bfb6c00c1e0
update it from 4.4.2 to 4.4.3.
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
19
diff
changeset
|
1127 if (really_expand) |
3bfb6c00c1e0
update it from 4.4.2 to 4.4.3.
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
19
diff
changeset
|
1128 { |
3bfb6c00c1e0
update it from 4.4.2 to 4.4.3.
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
19
diff
changeset
|
1129 error ("size of variable %q+D is too large", var); |
3bfb6c00c1e0
update it from 4.4.2 to 4.4.3.
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
19
diff
changeset
|
1130 expand_one_error_var (var); |
3bfb6c00c1e0
update it from 4.4.2 to 4.4.3.
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
19
diff
changeset
|
1131 } |
3bfb6c00c1e0
update it from 4.4.2 to 4.4.3.
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
19
diff
changeset
|
1132 } |
0 | 1133 else if (defer_stack_allocation (var, toplevel)) |
1134 add_stack_var (var); | |
1135 else | |
1136 { | |
1137 if (really_expand) | |
1138 expand_one_stack_var (var); | |
1139 return tree_low_cst (DECL_SIZE_UNIT (var), 1); | |
1140 } | |
1141 return 0; | |
1142 } | |
1143 | |
1144 /* A subroutine of expand_used_vars. Walk down through the BLOCK tree | |
1145 expanding variables. Those variables that can be put into registers | |
1146 are allocated pseudos; those that can't are put on the stack. | |
1147 | |
1148 TOPLEVEL is true if this is the outermost BLOCK. */ | |
1149 | |
1150 static void | |
1151 expand_used_vars_for_block (tree block, bool toplevel) | |
1152 { | |
1153 size_t i, j, old_sv_num, this_sv_num, new_sv_num; | |
1154 tree t; | |
1155 | |
1156 old_sv_num = toplevel ? 0 : stack_vars_num; | |
1157 | |
1158 /* Expand all variables at this level. */ | |
1159 for (t = BLOCK_VARS (block); t ; t = TREE_CHAIN (t)) | |
1160 if (TREE_USED (t)) | |
1161 expand_one_var (t, toplevel, true); | |
1162 | |
1163 this_sv_num = stack_vars_num; | |
1164 | |
1165 /* Expand all variables at containing levels. */ | |
1166 for (t = BLOCK_SUBBLOCKS (block); t ; t = BLOCK_CHAIN (t)) | |
1167 expand_used_vars_for_block (t, false); | |
1168 | |
1169 /* Since we do not track exact variable lifetimes (which is not even | |
1170 possible for variables whose address escapes), we mirror the block | |
1171 tree in the interference graph. Here we cause all variables at this | |
1172 level, and all sublevels, to conflict. Do make certain that a | |
1173 variable conflicts with itself. */ | |
1174 if (old_sv_num < this_sv_num) | |
1175 { | |
1176 new_sv_num = stack_vars_num; | |
1177 resize_stack_vars_conflict (new_sv_num); | |
1178 | |
1179 for (i = old_sv_num; i < new_sv_num; ++i) | |
1180 for (j = i < this_sv_num ? i+1 : this_sv_num; j-- > old_sv_num ;) | |
1181 add_stack_var_conflict (i, j); | |
1182 } | |
1183 } | |
1184 | |
1185 /* A subroutine of expand_used_vars. Walk down through the BLOCK tree | |
1186 and clear TREE_USED on all local variables. */ | |
1187 | |
1188 static void | |
1189 clear_tree_used (tree block) | |
1190 { | |
1191 tree t; | |
1192 | |
1193 for (t = BLOCK_VARS (block); t ; t = TREE_CHAIN (t)) | |
1194 /* if (!TREE_STATIC (t) && !DECL_EXTERNAL (t)) */ | |
1195 TREE_USED (t) = 0; | |
1196 | |
1197 for (t = BLOCK_SUBBLOCKS (block); t ; t = BLOCK_CHAIN (t)) | |
1198 clear_tree_used (t); | |
1199 } | |
1200 | |
1201 /* Examine TYPE and determine a bit mask of the following features. */ | |
1202 | |
1203 #define SPCT_HAS_LARGE_CHAR_ARRAY 1 | |
1204 #define SPCT_HAS_SMALL_CHAR_ARRAY 2 | |
1205 #define SPCT_HAS_ARRAY 4 | |
1206 #define SPCT_HAS_AGGREGATE 8 | |
1207 | |
1208 static unsigned int | |
1209 stack_protect_classify_type (tree type) | |
1210 { | |
1211 unsigned int ret = 0; | |
1212 tree t; | |
1213 | |
1214 switch (TREE_CODE (type)) | |
1215 { | |
1216 case ARRAY_TYPE: | |
1217 t = TYPE_MAIN_VARIANT (TREE_TYPE (type)); | |
1218 if (t == char_type_node | |
1219 || t == signed_char_type_node | |
1220 || t == unsigned_char_type_node) | |
1221 { | |
1222 unsigned HOST_WIDE_INT max = PARAM_VALUE (PARAM_SSP_BUFFER_SIZE); | |
1223 unsigned HOST_WIDE_INT len; | |
1224 | |
1225 if (!TYPE_SIZE_UNIT (type) | |
1226 || !host_integerp (TYPE_SIZE_UNIT (type), 1)) | |
1227 len = max; | |
1228 else | |
1229 len = tree_low_cst (TYPE_SIZE_UNIT (type), 1); | |
1230 | |
1231 if (len < max) | |
1232 ret = SPCT_HAS_SMALL_CHAR_ARRAY | SPCT_HAS_ARRAY; | |
1233 else | |
1234 ret = SPCT_HAS_LARGE_CHAR_ARRAY | SPCT_HAS_ARRAY; | |
1235 } | |
1236 else | |
1237 ret = SPCT_HAS_ARRAY; | |
1238 break; | |
1239 | |
1240 case UNION_TYPE: | |
1241 case QUAL_UNION_TYPE: | |
1242 case RECORD_TYPE: | |
1243 ret = SPCT_HAS_AGGREGATE; | |
1244 for (t = TYPE_FIELDS (type); t ; t = TREE_CHAIN (t)) | |
1245 if (TREE_CODE (t) == FIELD_DECL) | |
1246 ret |= stack_protect_classify_type (TREE_TYPE (t)); | |
1247 break; | |
1248 | |
1249 default: | |
1250 break; | |
1251 } | |
1252 | |
1253 return ret; | |
1254 } | |
1255 | |
1256 /* Return nonzero if DECL should be segregated into the "vulnerable" upper | |
1257 part of the local stack frame. Remember if we ever return nonzero for | |
1258 any variable in this function. The return value is the phase number in | |
1259 which the variable should be allocated. */ | |
1260 | |
1261 static int | |
1262 stack_protect_decl_phase (tree decl) | |
1263 { | |
1264 unsigned int bits = stack_protect_classify_type (TREE_TYPE (decl)); | |
1265 int ret = 0; | |
1266 | |
1267 if (bits & SPCT_HAS_SMALL_CHAR_ARRAY) | |
1268 has_short_buffer = true; | |
1269 | |
1270 if (flag_stack_protect == 2) | |
1271 { | |
1272 if ((bits & (SPCT_HAS_SMALL_CHAR_ARRAY | SPCT_HAS_LARGE_CHAR_ARRAY)) | |
1273 && !(bits & SPCT_HAS_AGGREGATE)) | |
1274 ret = 1; | |
1275 else if (bits & SPCT_HAS_ARRAY) | |
1276 ret = 2; | |
1277 } | |
1278 else | |
1279 ret = (bits & SPCT_HAS_LARGE_CHAR_ARRAY) != 0; | |
1280 | |
1281 if (ret) | |
1282 has_protected_decls = true; | |
1283 | |
1284 return ret; | |
1285 } | |
1286 | |
1287 /* Two helper routines that check for phase 1 and phase 2. These are used | |
1288 as callbacks for expand_stack_vars. */ | |
1289 | |
1290 static bool | |
1291 stack_protect_decl_phase_1 (tree decl) | |
1292 { | |
1293 return stack_protect_decl_phase (decl) == 1; | |
1294 } | |
1295 | |
1296 static bool | |
1297 stack_protect_decl_phase_2 (tree decl) | |
1298 { | |
1299 return stack_protect_decl_phase (decl) == 2; | |
1300 } | |
1301 | |
1302 /* Ensure that variables in different stack protection phases conflict | |
1303 so that they are not merged and share the same stack slot. */ | |
1304 | |
1305 static void | |
1306 add_stack_protection_conflicts (void) | |
1307 { | |
1308 size_t i, j, n = stack_vars_num; | |
1309 unsigned char *phase; | |
1310 | |
1311 phase = XNEWVEC (unsigned char, n); | |
1312 for (i = 0; i < n; ++i) | |
1313 phase[i] = stack_protect_decl_phase (stack_vars[i].decl); | |
1314 | |
1315 for (i = 0; i < n; ++i) | |
1316 { | |
1317 unsigned char ph_i = phase[i]; | |
1318 for (j = 0; j < i; ++j) | |
1319 if (ph_i != phase[j]) | |
1320 add_stack_var_conflict (i, j); | |
1321 } | |
1322 | |
1323 XDELETEVEC (phase); | |
1324 } | |
1325 | |
1326 /* Create a decl for the guard at the top of the stack frame. */ | |
1327 | |
1328 static void | |
1329 create_stack_guard (void) | |
1330 { | |
1331 tree guard = build_decl (VAR_DECL, NULL, ptr_type_node); | |
1332 TREE_THIS_VOLATILE (guard) = 1; | |
1333 TREE_USED (guard) = 1; | |
1334 expand_one_stack_var (guard); | |
1335 crtl->stack_protect_guard = guard; | |
1336 } | |
1337 | |
1338 /* A subroutine of expand_used_vars. Walk down through the BLOCK tree | |
1339 expanding variables. Those variables that can be put into registers | |
1340 are allocated pseudos; those that can't are put on the stack. | |
1341 | |
1342 TOPLEVEL is true if this is the outermost BLOCK. */ | |
1343 | |
1344 static HOST_WIDE_INT | |
1345 account_used_vars_for_block (tree block, bool toplevel) | |
1346 { | |
1347 size_t i, j, old_sv_num, this_sv_num, new_sv_num; | |
1348 tree t; | |
1349 HOST_WIDE_INT size = 0; | |
1350 | |
1351 old_sv_num = toplevel ? 0 : stack_vars_num; | |
1352 | |
1353 /* Expand all variables at this level. */ | |
1354 for (t = BLOCK_VARS (block); t ; t = TREE_CHAIN (t)) | |
1355 if (TREE_USED (t)) | |
1356 size += expand_one_var (t, toplevel, false); | |
1357 | |
1358 this_sv_num = stack_vars_num; | |
1359 | |
1360 /* Expand all variables at containing levels. */ | |
1361 for (t = BLOCK_SUBBLOCKS (block); t ; t = BLOCK_CHAIN (t)) | |
1362 size += account_used_vars_for_block (t, false); | |
1363 | |
1364 /* Since we do not track exact variable lifetimes (which is not even | |
1365 possible for variables whose address escapes), we mirror the block | |
1366 tree in the interference graph. Here we cause all variables at this | |
1367 level, and all sublevels, to conflict. Do make certain that a | |
1368 variable conflicts with itself. */ | |
1369 if (old_sv_num < this_sv_num) | |
1370 { | |
1371 new_sv_num = stack_vars_num; | |
1372 resize_stack_vars_conflict (new_sv_num); | |
1373 | |
1374 for (i = old_sv_num; i < new_sv_num; ++i) | |
1375 for (j = i < this_sv_num ? i+1 : this_sv_num; j-- > old_sv_num ;) | |
1376 add_stack_var_conflict (i, j); | |
1377 } | |
1378 return size; | |
1379 } | |
1380 | |
1381 /* Prepare for expanding variables. */ | |
1382 static void | |
1383 init_vars_expansion (void) | |
1384 { | |
1385 tree t; | |
1386 /* Set TREE_USED on all variables in the local_decls. */ | |
1387 for (t = cfun->local_decls; t; t = TREE_CHAIN (t)) | |
1388 TREE_USED (TREE_VALUE (t)) = 1; | |
1389 | |
1390 /* Clear TREE_USED on all variables associated with a block scope. */ | |
1391 clear_tree_used (DECL_INITIAL (current_function_decl)); | |
1392 | |
1393 /* Initialize local stack smashing state. */ | |
1394 has_protected_decls = false; | |
1395 has_short_buffer = false; | |
1396 } | |
1397 | |
1398 /* Free up stack variable graph data. */ | |
1399 static void | |
1400 fini_vars_expansion (void) | |
1401 { | |
1402 XDELETEVEC (stack_vars); | |
1403 XDELETEVEC (stack_vars_sorted); | |
1404 XDELETEVEC (stack_vars_conflict); | |
1405 stack_vars = NULL; | |
1406 stack_vars_alloc = stack_vars_num = 0; | |
1407 stack_vars_conflict = NULL; | |
1408 stack_vars_conflict_alloc = 0; | |
1409 } | |
1410 | |
1411 /* Make a fair guess for the size of the stack frame of the current | |
1412 function. This doesn't have to be exact, the result is only used | |
1413 in the inline heuristics. So we don't want to run the full stack | |
1414 var packing algorithm (which is quadratic in the number of stack | |
1415 vars). Instead, we calculate the total size of all stack vars. | |
1416 This turns out to be a pretty fair estimate -- packing of stack | |
1417 vars doesn't happen very often. */ | |
1418 | |
1419 HOST_WIDE_INT | |
1420 estimated_stack_frame_size (void) | |
1421 { | |
1422 HOST_WIDE_INT size = 0; | |
1423 size_t i; | |
1424 tree t, outer_block = DECL_INITIAL (current_function_decl); | |
1425 | |
1426 init_vars_expansion (); | |
1427 | |
1428 for (t = cfun->local_decls; t; t = TREE_CHAIN (t)) | |
1429 { | |
1430 tree var = TREE_VALUE (t); | |
1431 | |
1432 if (TREE_USED (var)) | |
1433 size += expand_one_var (var, true, false); | |
1434 TREE_USED (var) = 1; | |
1435 } | |
1436 size += account_used_vars_for_block (outer_block, true); | |
1437 | |
1438 if (stack_vars_num > 0) | |
1439 { | |
1440 /* Fake sorting the stack vars for account_stack_vars (). */ | |
1441 stack_vars_sorted = XNEWVEC (size_t, stack_vars_num); | |
1442 for (i = 0; i < stack_vars_num; ++i) | |
1443 stack_vars_sorted[i] = i; | |
1444 size += account_stack_vars (); | |
1445 fini_vars_expansion (); | |
1446 } | |
1447 | |
1448 return size; | |
1449 } | |
1450 | |
1451 /* Expand all variables used in the function. */ | |
1452 | |
1453 static void | |
1454 expand_used_vars (void) | |
1455 { | |
1456 tree t, next, outer_block = DECL_INITIAL (current_function_decl); | |
1457 | |
1458 /* Compute the phase of the stack frame for this function. */ | |
1459 { | |
1460 int align = PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT; | |
1461 int off = STARTING_FRAME_OFFSET % align; | |
1462 frame_phase = off ? align - off : 0; | |
1463 } | |
1464 | |
1465 init_vars_expansion (); | |
1466 | |
1467 /* At this point all variables on the local_decls with TREE_USED | |
1468 set are not associated with any block scope. Lay them out. */ | |
1469 t = cfun->local_decls; | |
1470 cfun->local_decls = NULL_TREE; | |
1471 for (; t; t = next) | |
1472 { | |
1473 tree var = TREE_VALUE (t); | |
1474 bool expand_now = false; | |
1475 | |
1476 next = TREE_CHAIN (t); | |
1477 | |
1478 /* We didn't set a block for static or extern because it's hard | |
1479 to tell the difference between a global variable (re)declared | |
1480 in a local scope, and one that's really declared there to | |
1481 begin with. And it doesn't really matter much, since we're | |
1482 not giving them stack space. Expand them now. */ | |
1483 if (TREE_STATIC (var) || DECL_EXTERNAL (var)) | |
1484 expand_now = true; | |
1485 | |
1486 /* Any variable that could have been hoisted into an SSA_NAME | |
1487 will have been propagated anywhere the optimizers chose, | |
1488 i.e. not confined to their original block. Allocate them | |
1489 as if they were defined in the outermost scope. */ | |
1490 else if (is_gimple_reg (var)) | |
1491 expand_now = true; | |
1492 | |
1493 /* If the variable is not associated with any block, then it | |
1494 was created by the optimizers, and could be live anywhere | |
1495 in the function. */ | |
1496 else if (TREE_USED (var)) | |
1497 expand_now = true; | |
1498 | |
1499 /* Finally, mark all variables on the list as used. We'll use | |
1500 this in a moment when we expand those associated with scopes. */ | |
1501 TREE_USED (var) = 1; | |
1502 | |
1503 if (expand_now) | |
1504 { | |
1505 expand_one_var (var, true, true); | |
1506 if (DECL_ARTIFICIAL (var) && !DECL_IGNORED_P (var)) | |
1507 { | |
1508 rtx rtl = DECL_RTL_IF_SET (var); | |
1509 | |
1510 /* Keep artificial non-ignored vars in cfun->local_decls | |
1511 chain until instantiate_decls. */ | |
1512 if (rtl && (MEM_P (rtl) || GET_CODE (rtl) == CONCAT)) | |
1513 { | |
1514 TREE_CHAIN (t) = cfun->local_decls; | |
1515 cfun->local_decls = t; | |
1516 continue; | |
1517 } | |
1518 } | |
1519 } | |
1520 | |
1521 ggc_free (t); | |
1522 } | |
1523 | |
1524 /* At this point, all variables within the block tree with TREE_USED | |
1525 set are actually used by the optimized function. Lay them out. */ | |
1526 expand_used_vars_for_block (outer_block, true); | |
1527 | |
1528 if (stack_vars_num > 0) | |
1529 { | |
1530 /* Due to the way alias sets work, no variables with non-conflicting | |
1531 alias sets may be assigned the same address. Add conflicts to | |
1532 reflect this. */ | |
1533 add_alias_set_conflicts (); | |
1534 | |
1535 /* If stack protection is enabled, we don't share space between | |
1536 vulnerable data and non-vulnerable data. */ | |
1537 if (flag_stack_protect) | |
1538 add_stack_protection_conflicts (); | |
1539 | |
1540 /* Now that we have collected all stack variables, and have computed a | |
1541 minimal interference graph, attempt to save some stack space. */ | |
1542 partition_stack_vars (); | |
1543 if (dump_file) | |
1544 dump_stack_var_partition (); | |
1545 } | |
1546 | |
1547 /* There are several conditions under which we should create a | |
1548 stack guard: protect-all, alloca used, protected decls present. */ | |
1549 if (flag_stack_protect == 2 | |
1550 || (flag_stack_protect | |
1551 && (cfun->calls_alloca || has_protected_decls))) | |
1552 create_stack_guard (); | |
1553 | |
1554 /* Assign rtl to each variable based on these partitions. */ | |
1555 if (stack_vars_num > 0) | |
1556 { | |
1557 /* Reorder decls to be protected by iterating over the variables | |
1558 array multiple times, and allocating out of each phase in turn. */ | |
1559 /* ??? We could probably integrate this into the qsort we did | |
1560 earlier, such that we naturally see these variables first, | |
1561 and thus naturally allocate things in the right order. */ | |
1562 if (has_protected_decls) | |
1563 { | |
1564 /* Phase 1 contains only character arrays. */ | |
1565 expand_stack_vars (stack_protect_decl_phase_1); | |
1566 | |
1567 /* Phase 2 contains other kinds of arrays. */ | |
1568 if (flag_stack_protect == 2) | |
1569 expand_stack_vars (stack_protect_decl_phase_2); | |
1570 } | |
1571 | |
1572 expand_stack_vars (NULL); | |
1573 | |
1574 fini_vars_expansion (); | |
1575 } | |
1576 | |
1577 /* If the target requires that FRAME_OFFSET be aligned, do it. */ | |
1578 if (STACK_ALIGNMENT_NEEDED) | |
1579 { | |
1580 HOST_WIDE_INT align = PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT; | |
1581 if (!FRAME_GROWS_DOWNWARD) | |
1582 frame_offset += align - 1; | |
1583 frame_offset &= -align; | |
1584 } | |
1585 } | |
1586 | |
1587 | |
1588 /* If we need to produce a detailed dump, print the tree representation | |
1589 for STMT to the dump file. SINCE is the last RTX after which the RTL | |
1590 generated for STMT should have been appended. */ | |
1591 | |
1592 static void | |
1593 maybe_dump_rtl_for_gimple_stmt (gimple stmt, rtx since) | |
1594 { | |
1595 if (dump_file && (dump_flags & TDF_DETAILS)) | |
1596 { | |
1597 fprintf (dump_file, "\n;; "); | |
1598 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM); | |
1599 fprintf (dump_file, "\n"); | |
1600 | |
1601 print_rtl (dump_file, since ? NEXT_INSN (since) : since); | |
1602 } | |
1603 } | |
1604 | |
1605 /* Maps the blocks that do not contain tree labels to rtx labels. */ | |
1606 | |
1607 static struct pointer_map_t *lab_rtx_for_bb; | |
1608 | |
1609 /* Returns the label_rtx expression for a label starting basic block BB. */ | |
1610 | |
1611 static rtx | |
1612 label_rtx_for_bb (basic_block bb ATTRIBUTE_UNUSED) | |
1613 { | |
1614 gimple_stmt_iterator gsi; | |
1615 tree lab; | |
1616 gimple lab_stmt; | |
1617 void **elt; | |
1618 | |
1619 if (bb->flags & BB_RTL) | |
1620 return block_label (bb); | |
1621 | |
1622 elt = pointer_map_contains (lab_rtx_for_bb, bb); | |
1623 if (elt) | |
1624 return (rtx) *elt; | |
1625 | |
1626 /* Find the tree label if it is present. */ | |
1627 | |
1628 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi)) | |
1629 { | |
1630 lab_stmt = gsi_stmt (gsi); | |
1631 if (gimple_code (lab_stmt) != GIMPLE_LABEL) | |
1632 break; | |
1633 | |
1634 lab = gimple_label_label (lab_stmt); | |
1635 if (DECL_NONLOCAL (lab)) | |
1636 break; | |
1637 | |
1638 return label_rtx (lab); | |
1639 } | |
1640 | |
1641 elt = pointer_map_insert (lab_rtx_for_bb, bb); | |
1642 *elt = gen_label_rtx (); | |
1643 return (rtx) *elt; | |
1644 } | |
1645 | |
1646 | |
1647 /* A subroutine of expand_gimple_basic_block. Expand one GIMPLE_COND. | |
1648 Returns a new basic block if we've terminated the current basic | |
1649 block and created a new one. */ | |
1650 | |
1651 static basic_block | |
1652 expand_gimple_cond (basic_block bb, gimple stmt) | |
1653 { | |
1654 basic_block new_bb, dest; | |
1655 edge new_edge; | |
1656 edge true_edge; | |
1657 edge false_edge; | |
1658 tree pred = gimple_cond_pred_to_tree (stmt); | |
1659 rtx last2, last; | |
1660 | |
1661 last2 = last = get_last_insn (); | |
1662 | |
1663 extract_true_false_edges_from_block (bb, &true_edge, &false_edge); | |
1664 if (gimple_has_location (stmt)) | |
1665 { | |
1666 set_curr_insn_source_location (gimple_location (stmt)); | |
1667 set_curr_insn_block (gimple_block (stmt)); | |
1668 } | |
1669 | |
1670 /* These flags have no purpose in RTL land. */ | |
1671 true_edge->flags &= ~EDGE_TRUE_VALUE; | |
1672 false_edge->flags &= ~EDGE_FALSE_VALUE; | |
1673 | |
1674 /* We can either have a pure conditional jump with one fallthru edge or | |
1675 two-way jump that needs to be decomposed into two basic blocks. */ | |
1676 if (false_edge->dest == bb->next_bb) | |
1677 { | |
1678 jumpif (pred, label_rtx_for_bb (true_edge->dest)); | |
1679 add_reg_br_prob_note (last, true_edge->probability); | |
1680 maybe_dump_rtl_for_gimple_stmt (stmt, last); | |
1681 if (true_edge->goto_locus) | |
1682 { | |
1683 set_curr_insn_source_location (true_edge->goto_locus); | |
1684 set_curr_insn_block (true_edge->goto_block); | |
1685 true_edge->goto_locus = curr_insn_locator (); | |
1686 } | |
1687 true_edge->goto_block = NULL; | |
1688 false_edge->flags |= EDGE_FALLTHRU; | |
1689 ggc_free (pred); | |
1690 return NULL; | |
1691 } | |
1692 if (true_edge->dest == bb->next_bb) | |
1693 { | |
1694 jumpifnot (pred, label_rtx_for_bb (false_edge->dest)); | |
1695 add_reg_br_prob_note (last, false_edge->probability); | |
1696 maybe_dump_rtl_for_gimple_stmt (stmt, last); | |
1697 if (false_edge->goto_locus) | |
1698 { | |
1699 set_curr_insn_source_location (false_edge->goto_locus); | |
1700 set_curr_insn_block (false_edge->goto_block); | |
1701 false_edge->goto_locus = curr_insn_locator (); | |
1702 } | |
1703 false_edge->goto_block = NULL; | |
1704 true_edge->flags |= EDGE_FALLTHRU; | |
1705 ggc_free (pred); | |
1706 return NULL; | |
1707 } | |
1708 | |
1709 jumpif (pred, label_rtx_for_bb (true_edge->dest)); | |
1710 add_reg_br_prob_note (last, true_edge->probability); | |
1711 last = get_last_insn (); | |
1712 if (false_edge->goto_locus) | |
1713 { | |
1714 set_curr_insn_source_location (false_edge->goto_locus); | |
1715 set_curr_insn_block (false_edge->goto_block); | |
1716 false_edge->goto_locus = curr_insn_locator (); | |
1717 } | |
1718 false_edge->goto_block = NULL; | |
1719 emit_jump (label_rtx_for_bb (false_edge->dest)); | |
1720 | |
1721 BB_END (bb) = last; | |
1722 if (BARRIER_P (BB_END (bb))) | |
1723 BB_END (bb) = PREV_INSN (BB_END (bb)); | |
1724 update_bb_for_insn (bb); | |
1725 | |
1726 new_bb = create_basic_block (NEXT_INSN (last), get_last_insn (), bb); | |
1727 dest = false_edge->dest; | |
1728 redirect_edge_succ (false_edge, new_bb); | |
1729 false_edge->flags |= EDGE_FALLTHRU; | |
1730 new_bb->count = false_edge->count; | |
1731 new_bb->frequency = EDGE_FREQUENCY (false_edge); | |
1732 new_edge = make_edge (new_bb, dest, 0); | |
1733 new_edge->probability = REG_BR_PROB_BASE; | |
1734 new_edge->count = new_bb->count; | |
1735 if (BARRIER_P (BB_END (new_bb))) | |
1736 BB_END (new_bb) = PREV_INSN (BB_END (new_bb)); | |
1737 update_bb_for_insn (new_bb); | |
1738 | |
1739 maybe_dump_rtl_for_gimple_stmt (stmt, last2); | |
1740 | |
1741 if (true_edge->goto_locus) | |
1742 { | |
1743 set_curr_insn_source_location (true_edge->goto_locus); | |
1744 set_curr_insn_block (true_edge->goto_block); | |
1745 true_edge->goto_locus = curr_insn_locator (); | |
1746 } | |
1747 true_edge->goto_block = NULL; | |
1748 | |
1749 ggc_free (pred); | |
1750 return new_bb; | |
1751 } | |
1752 | |
1753 /* A subroutine of expand_gimple_basic_block. Expand one GIMPLE_CALL | |
1754 that has CALL_EXPR_TAILCALL set. Returns non-null if we actually | |
1755 generated a tail call (something that might be denied by the ABI | |
1756 rules governing the call; see calls.c). | |
1757 | |
1758 Sets CAN_FALLTHRU if we generated a *conditional* tail call, and | |
1759 can still reach the rest of BB. The case here is __builtin_sqrt, | |
1760 where the NaN result goes through the external function (with a | |
1761 tailcall) and the normal result happens via a sqrt instruction. */ | |
1762 | |
1763 static basic_block | |
1764 expand_gimple_tailcall (basic_block bb, gimple stmt, bool *can_fallthru) | |
1765 { | |
1766 rtx last2, last; | |
1767 edge e; | |
1768 edge_iterator ei; | |
1769 int probability; | |
1770 gcov_type count; | |
1771 tree stmt_tree = gimple_to_tree (stmt); | |
1772 | |
1773 last2 = last = get_last_insn (); | |
1774 | |
1775 expand_expr_stmt (stmt_tree); | |
1776 | |
1777 release_stmt_tree (stmt, stmt_tree); | |
1778 | |
1779 for (last = NEXT_INSN (last); last; last = NEXT_INSN (last)) | |
1780 if (CALL_P (last) && SIBLING_CALL_P (last)) | |
1781 goto found; | |
1782 | |
1783 maybe_dump_rtl_for_gimple_stmt (stmt, last2); | |
1784 | |
1785 *can_fallthru = true; | |
1786 return NULL; | |
1787 | |
1788 found: | |
1789 /* ??? Wouldn't it be better to just reset any pending stack adjust? | |
1790 Any instructions emitted here are about to be deleted. */ | |
1791 do_pending_stack_adjust (); | |
1792 | |
1793 /* Remove any non-eh, non-abnormal edges that don't go to exit. */ | |
1794 /* ??? I.e. the fallthrough edge. HOWEVER! If there were to be | |
1795 EH or abnormal edges, we shouldn't have created a tail call in | |
1796 the first place. So it seems to me we should just be removing | |
1797 all edges here, or redirecting the existing fallthru edge to | |
1798 the exit block. */ | |
1799 | |
1800 probability = 0; | |
1801 count = 0; | |
1802 | |
1803 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); ) | |
1804 { | |
1805 if (!(e->flags & (EDGE_ABNORMAL | EDGE_EH))) | |
1806 { | |
1807 if (e->dest != EXIT_BLOCK_PTR) | |
1808 { | |
1809 e->dest->count -= e->count; | |
1810 e->dest->frequency -= EDGE_FREQUENCY (e); | |
1811 if (e->dest->count < 0) | |
1812 e->dest->count = 0; | |
1813 if (e->dest->frequency < 0) | |
1814 e->dest->frequency = 0; | |
1815 } | |
1816 count += e->count; | |
1817 probability += e->probability; | |
1818 remove_edge (e); | |
1819 } | |
1820 else | |
1821 ei_next (&ei); | |
1822 } | |
1823 | |
1824 /* This is somewhat ugly: the call_expr expander often emits instructions | |
1825 after the sibcall (to perform the function return). These confuse the | |
1826 find_many_sub_basic_blocks code, so we need to get rid of these. */ | |
1827 last = NEXT_INSN (last); | |
1828 gcc_assert (BARRIER_P (last)); | |
1829 | |
1830 *can_fallthru = false; | |
1831 while (NEXT_INSN (last)) | |
1832 { | |
1833 /* For instance an sqrt builtin expander expands if with | |
1834 sibcall in the then and label for `else`. */ | |
1835 if (LABEL_P (NEXT_INSN (last))) | |
1836 { | |
1837 *can_fallthru = true; | |
1838 break; | |
1839 } | |
1840 delete_insn (NEXT_INSN (last)); | |
1841 } | |
1842 | |
1843 e = make_edge (bb, EXIT_BLOCK_PTR, EDGE_ABNORMAL | EDGE_SIBCALL); | |
1844 e->probability += probability; | |
1845 e->count += count; | |
1846 BB_END (bb) = last; | |
1847 update_bb_for_insn (bb); | |
1848 | |
1849 if (NEXT_INSN (last)) | |
1850 { | |
1851 bb = create_basic_block (NEXT_INSN (last), get_last_insn (), bb); | |
1852 | |
1853 last = BB_END (bb); | |
1854 if (BARRIER_P (last)) | |
1855 BB_END (bb) = PREV_INSN (last); | |
1856 } | |
1857 | |
1858 maybe_dump_rtl_for_gimple_stmt (stmt, last2); | |
1859 | |
1860 return bb; | |
1861 } | |
1862 | |
1863 /* Expand basic block BB from GIMPLE trees to RTL. */ | |
1864 | |
1865 static basic_block | |
1866 expand_gimple_basic_block (basic_block bb) | |
1867 { | |
1868 gimple_stmt_iterator gsi; | |
1869 gimple_seq stmts; | |
1870 gimple stmt = NULL; | |
1871 rtx note, last; | |
1872 edge e; | |
1873 edge_iterator ei; | |
1874 void **elt; | |
1875 | |
1876 if (dump_file) | |
1877 fprintf (dump_file, "\n;; Generating RTL for gimple basic block %d\n", | |
1878 bb->index); | |
1879 | |
1880 /* Note that since we are now transitioning from GIMPLE to RTL, we | |
1881 cannot use the gsi_*_bb() routines because they expect the basic | |
1882 block to be in GIMPLE, instead of RTL. Therefore, we need to | |
1883 access the BB sequence directly. */ | |
1884 stmts = bb_seq (bb); | |
1885 bb->il.gimple = NULL; | |
1886 rtl_profile_for_bb (bb); | |
1887 init_rtl_bb_info (bb); | |
1888 bb->flags |= BB_RTL; | |
1889 | |
1890 /* Remove the RETURN_EXPR if we may fall though to the exit | |
1891 instead. */ | |
1892 gsi = gsi_last (stmts); | |
1893 if (!gsi_end_p (gsi) | |
1894 && gimple_code (gsi_stmt (gsi)) == GIMPLE_RETURN) | |
1895 { | |
1896 gimple ret_stmt = gsi_stmt (gsi); | |
1897 | |
1898 gcc_assert (single_succ_p (bb)); | |
1899 gcc_assert (single_succ (bb) == EXIT_BLOCK_PTR); | |
1900 | |
1901 if (bb->next_bb == EXIT_BLOCK_PTR | |
1902 && !gimple_return_retval (ret_stmt)) | |
1903 { | |
1904 gsi_remove (&gsi, false); | |
1905 single_succ_edge (bb)->flags |= EDGE_FALLTHRU; | |
1906 } | |
1907 } | |
1908 | |
1909 gsi = gsi_start (stmts); | |
1910 if (!gsi_end_p (gsi)) | |
1911 { | |
1912 stmt = gsi_stmt (gsi); | |
1913 if (gimple_code (stmt) != GIMPLE_LABEL) | |
1914 stmt = NULL; | |
1915 } | |
1916 | |
1917 elt = pointer_map_contains (lab_rtx_for_bb, bb); | |
1918 | |
1919 if (stmt || elt) | |
1920 { | |
1921 last = get_last_insn (); | |
1922 | |
1923 if (stmt) | |
1924 { | |
1925 tree stmt_tree = gimple_to_tree (stmt); | |
1926 expand_expr_stmt (stmt_tree); | |
1927 release_stmt_tree (stmt, stmt_tree); | |
1928 gsi_next (&gsi); | |
1929 } | |
1930 | |
1931 if (elt) | |
1932 emit_label ((rtx) *elt); | |
1933 | |
1934 /* Java emits line number notes in the top of labels. | |
1935 ??? Make this go away once line number notes are obsoleted. */ | |
1936 BB_HEAD (bb) = NEXT_INSN (last); | |
1937 if (NOTE_P (BB_HEAD (bb))) | |
1938 BB_HEAD (bb) = NEXT_INSN (BB_HEAD (bb)); | |
1939 note = emit_note_after (NOTE_INSN_BASIC_BLOCK, BB_HEAD (bb)); | |
1940 | |
1941 maybe_dump_rtl_for_gimple_stmt (stmt, last); | |
1942 } | |
1943 else | |
1944 note = BB_HEAD (bb) = emit_note (NOTE_INSN_BASIC_BLOCK); | |
1945 | |
1946 NOTE_BASIC_BLOCK (note) = bb; | |
1947 | |
1948 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); ) | |
1949 { | |
1950 /* Clear EDGE_EXECUTABLE. This flag is never used in the backend. */ | |
1951 e->flags &= ~EDGE_EXECUTABLE; | |
1952 | |
1953 /* At the moment not all abnormal edges match the RTL representation. | |
1954 It is safe to remove them here as find_many_sub_basic_blocks will | |
1955 rediscover them. In the future we should get this fixed properly. */ | |
1956 if (e->flags & EDGE_ABNORMAL) | |
1957 remove_edge (e); | |
1958 else | |
1959 ei_next (&ei); | |
1960 } | |
1961 | |
1962 for (; !gsi_end_p (gsi); gsi_next (&gsi)) | |
1963 { | |
1964 gimple stmt = gsi_stmt (gsi); | |
1965 basic_block new_bb; | |
1966 | |
1967 /* Expand this statement, then evaluate the resulting RTL and | |
1968 fixup the CFG accordingly. */ | |
1969 if (gimple_code (stmt) == GIMPLE_COND) | |
1970 { | |
1971 new_bb = expand_gimple_cond (bb, stmt); | |
1972 if (new_bb) | |
1973 return new_bb; | |
1974 } | |
1975 else | |
1976 { | |
1977 if (is_gimple_call (stmt) && gimple_call_tail_p (stmt)) | |
1978 { | |
1979 bool can_fallthru; | |
1980 new_bb = expand_gimple_tailcall (bb, stmt, &can_fallthru); | |
1981 if (new_bb) | |
1982 { | |
1983 if (can_fallthru) | |
1984 bb = new_bb; | |
1985 else | |
1986 return new_bb; | |
1987 } | |
1988 } | |
1989 else if (gimple_code (stmt) != GIMPLE_CHANGE_DYNAMIC_TYPE) | |
1990 { | |
1991 tree stmt_tree = gimple_to_tree (stmt); | |
1992 last = get_last_insn (); | |
1993 expand_expr_stmt (stmt_tree); | |
1994 maybe_dump_rtl_for_gimple_stmt (stmt, last); | |
1995 release_stmt_tree (stmt, stmt_tree); | |
1996 } | |
1997 } | |
1998 } | |
1999 | |
2000 /* Expand implicit goto and convert goto_locus. */ | |
2001 FOR_EACH_EDGE (e, ei, bb->succs) | |
2002 { | |
2003 if (e->goto_locus && e->goto_block) | |
2004 { | |
2005 set_curr_insn_source_location (e->goto_locus); | |
2006 set_curr_insn_block (e->goto_block); | |
2007 e->goto_locus = curr_insn_locator (); | |
2008 } | |
2009 e->goto_block = NULL; | |
2010 if ((e->flags & EDGE_FALLTHRU) && e->dest != bb->next_bb) | |
2011 { | |
2012 emit_jump (label_rtx_for_bb (e->dest)); | |
2013 e->flags &= ~EDGE_FALLTHRU; | |
2014 } | |
2015 } | |
2016 | |
2017 do_pending_stack_adjust (); | |
2018 | |
2019 /* Find the block tail. The last insn in the block is the insn | |
2020 before a barrier and/or table jump insn. */ | |
2021 last = get_last_insn (); | |
2022 if (BARRIER_P (last)) | |
2023 last = PREV_INSN (last); | |
2024 if (JUMP_TABLE_DATA_P (last)) | |
2025 last = PREV_INSN (PREV_INSN (last)); | |
2026 BB_END (bb) = last; | |
2027 | |
2028 update_bb_for_insn (bb); | |
2029 | |
2030 return bb; | |
2031 } | |
2032 | |
2033 | |
2034 /* Create a basic block for initialization code. */ | |
2035 | |
2036 static basic_block | |
2037 construct_init_block (void) | |
2038 { | |
2039 basic_block init_block, first_block; | |
2040 edge e = NULL; | |
2041 int flags; | |
2042 | |
2043 /* Multiple entry points not supported yet. */ | |
2044 gcc_assert (EDGE_COUNT (ENTRY_BLOCK_PTR->succs) == 1); | |
2045 init_rtl_bb_info (ENTRY_BLOCK_PTR); | |
2046 init_rtl_bb_info (EXIT_BLOCK_PTR); | |
2047 ENTRY_BLOCK_PTR->flags |= BB_RTL; | |
2048 EXIT_BLOCK_PTR->flags |= BB_RTL; | |
2049 | |
2050 e = EDGE_SUCC (ENTRY_BLOCK_PTR, 0); | |
2051 | |
2052 /* When entry edge points to first basic block, we don't need jump, | |
2053 otherwise we have to jump into proper target. */ | |
2054 if (e && e->dest != ENTRY_BLOCK_PTR->next_bb) | |
2055 { | |
2056 tree label = gimple_block_label (e->dest); | |
2057 | |
2058 emit_jump (label_rtx (label)); | |
2059 flags = 0; | |
2060 } | |
2061 else | |
2062 flags = EDGE_FALLTHRU; | |
2063 | |
2064 init_block = create_basic_block (NEXT_INSN (get_insns ()), | |
2065 get_last_insn (), | |
2066 ENTRY_BLOCK_PTR); | |
2067 init_block->frequency = ENTRY_BLOCK_PTR->frequency; | |
2068 init_block->count = ENTRY_BLOCK_PTR->count; | |
2069 if (e) | |
2070 { | |
2071 first_block = e->dest; | |
2072 redirect_edge_succ (e, init_block); | |
2073 e = make_edge (init_block, first_block, flags); | |
2074 } | |
2075 else | |
2076 e = make_edge (init_block, EXIT_BLOCK_PTR, EDGE_FALLTHRU); | |
2077 e->probability = REG_BR_PROB_BASE; | |
2078 e->count = ENTRY_BLOCK_PTR->count; | |
2079 | |
2080 update_bb_for_insn (init_block); | |
2081 return init_block; | |
2082 } | |
2083 | |
2084 /* For each lexical block, set BLOCK_NUMBER to the depth at which it is | |
2085 found in the block tree. */ | |
2086 | |
2087 static void | |
2088 set_block_levels (tree block, int level) | |
2089 { | |
2090 while (block) | |
2091 { | |
2092 BLOCK_NUMBER (block) = level; | |
2093 set_block_levels (BLOCK_SUBBLOCKS (block), level + 1); | |
2094 block = BLOCK_CHAIN (block); | |
2095 } | |
2096 } | |
2097 | |
2098 /* Create a block containing landing pads and similar stuff. */ | |
2099 | |
2100 static void | |
2101 construct_exit_block (void) | |
2102 { | |
2103 rtx head = get_last_insn (); | |
2104 rtx end; | |
2105 basic_block exit_block; | |
2106 edge e, e2; | |
2107 unsigned ix; | |
2108 edge_iterator ei; | |
2109 rtx orig_end = BB_END (EXIT_BLOCK_PTR->prev_bb); | |
2110 | |
2111 rtl_profile_for_bb (EXIT_BLOCK_PTR); | |
2112 | |
2113 /* Make sure the locus is set to the end of the function, so that | |
2114 epilogue line numbers and warnings are set properly. */ | |
2115 if (cfun->function_end_locus != UNKNOWN_LOCATION) | |
2116 input_location = cfun->function_end_locus; | |
2117 | |
2118 /* The following insns belong to the top scope. */ | |
2119 set_curr_insn_block (DECL_INITIAL (current_function_decl)); | |
2120 | |
2121 /* Generate rtl for function exit. */ | |
2122 expand_function_end (); | |
2123 | |
2124 end = get_last_insn (); | |
2125 if (head == end) | |
2126 return; | |
2127 /* While emitting the function end we could move end of the last basic block. | |
2128 */ | |
2129 BB_END (EXIT_BLOCK_PTR->prev_bb) = orig_end; | |
2130 while (NEXT_INSN (head) && NOTE_P (NEXT_INSN (head))) | |
2131 head = NEXT_INSN (head); | |
2132 exit_block = create_basic_block (NEXT_INSN (head), end, | |
2133 EXIT_BLOCK_PTR->prev_bb); | |
2134 exit_block->frequency = EXIT_BLOCK_PTR->frequency; | |
2135 exit_block->count = EXIT_BLOCK_PTR->count; | |
2136 | |
2137 ix = 0; | |
2138 while (ix < EDGE_COUNT (EXIT_BLOCK_PTR->preds)) | |
2139 { | |
2140 e = EDGE_PRED (EXIT_BLOCK_PTR, ix); | |
2141 if (!(e->flags & EDGE_ABNORMAL)) | |
2142 redirect_edge_succ (e, exit_block); | |
2143 else | |
2144 ix++; | |
2145 } | |
2146 | |
2147 e = make_edge (exit_block, EXIT_BLOCK_PTR, EDGE_FALLTHRU); | |
2148 e->probability = REG_BR_PROB_BASE; | |
2149 e->count = EXIT_BLOCK_PTR->count; | |
2150 FOR_EACH_EDGE (e2, ei, EXIT_BLOCK_PTR->preds) | |
2151 if (e2 != e) | |
2152 { | |
2153 e->count -= e2->count; | |
2154 exit_block->count -= e2->count; | |
2155 exit_block->frequency -= EDGE_FREQUENCY (e2); | |
2156 } | |
2157 if (e->count < 0) | |
2158 e->count = 0; | |
2159 if (exit_block->count < 0) | |
2160 exit_block->count = 0; | |
2161 if (exit_block->frequency < 0) | |
2162 exit_block->frequency = 0; | |
2163 update_bb_for_insn (exit_block); | |
2164 } | |
2165 | |
2166 /* Helper function for discover_nonconstant_array_refs. | |
2167 Look for ARRAY_REF nodes with non-constant indexes and mark them | |
2168 addressable. */ | |
2169 | |
2170 static tree | |
2171 discover_nonconstant_array_refs_r (tree * tp, int *walk_subtrees, | |
2172 void *data ATTRIBUTE_UNUSED) | |
2173 { | |
2174 tree t = *tp; | |
2175 | |
2176 if (IS_TYPE_OR_DECL_P (t)) | |
2177 *walk_subtrees = 0; | |
2178 else if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF) | |
2179 { | |
2180 while (((TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF) | |
2181 && is_gimple_min_invariant (TREE_OPERAND (t, 1)) | |
2182 && (!TREE_OPERAND (t, 2) | |
2183 || is_gimple_min_invariant (TREE_OPERAND (t, 2)))) | |
2184 || (TREE_CODE (t) == COMPONENT_REF | |
2185 && (!TREE_OPERAND (t,2) | |
2186 || is_gimple_min_invariant (TREE_OPERAND (t, 2)))) | |
2187 || TREE_CODE (t) == BIT_FIELD_REF | |
2188 || TREE_CODE (t) == REALPART_EXPR | |
2189 || TREE_CODE (t) == IMAGPART_EXPR | |
2190 || TREE_CODE (t) == VIEW_CONVERT_EXPR | |
2191 || CONVERT_EXPR_P (t)) | |
2192 t = TREE_OPERAND (t, 0); | |
2193 | |
2194 if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF) | |
2195 { | |
2196 t = get_base_address (t); | |
2197 if (t && DECL_P (t)) | |
2198 TREE_ADDRESSABLE (t) = 1; | |
2199 } | |
2200 | |
2201 *walk_subtrees = 0; | |
2202 } | |
2203 | |
2204 return NULL_TREE; | |
2205 } | |
2206 | |
2207 /* RTL expansion is not able to compile array references with variable | |
2208 offsets for arrays stored in single register. Discover such | |
2209 expressions and mark variables as addressable to avoid this | |
2210 scenario. */ | |
2211 | |
2212 static void | |
2213 discover_nonconstant_array_refs (void) | |
2214 { | |
2215 basic_block bb; | |
2216 gimple_stmt_iterator gsi; | |
2217 | |
2218 FOR_EACH_BB (bb) | |
2219 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi)) | |
2220 { | |
2221 gimple stmt = gsi_stmt (gsi); | |
2222 walk_gimple_op (stmt, discover_nonconstant_array_refs_r, NULL); | |
2223 } | |
2224 } | |
2225 | |
2226 /* This function sets crtl->args.internal_arg_pointer to a virtual | |
2227 register if DRAP is needed. Local register allocator will replace | |
2228 virtual_incoming_args_rtx with the virtual register. */ | |
2229 | |
2230 static void | |
2231 expand_stack_alignment (void) | |
2232 { | |
2233 rtx drap_rtx; | |
2234 unsigned int preferred_stack_boundary; | |
2235 | |
2236 if (! SUPPORTS_STACK_ALIGNMENT) | |
2237 return; | |
2238 | |
2239 if (cfun->calls_alloca | |
2240 || cfun->has_nonlocal_label | |
2241 || crtl->has_nonlocal_goto) | |
2242 crtl->need_drap = true; | |
2243 | |
2244 gcc_assert (crtl->stack_alignment_needed | |
2245 <= crtl->stack_alignment_estimated); | |
2246 | |
2247 /* Update crtl->stack_alignment_estimated and use it later to align | |
2248 stack. We check PREFERRED_STACK_BOUNDARY if there may be non-call | |
2249 exceptions since callgraph doesn't collect incoming stack alignment | |
2250 in this case. */ | |
2251 if (flag_non_call_exceptions | |
2252 && PREFERRED_STACK_BOUNDARY > crtl->preferred_stack_boundary) | |
2253 preferred_stack_boundary = PREFERRED_STACK_BOUNDARY; | |
2254 else | |
2255 preferred_stack_boundary = crtl->preferred_stack_boundary; | |
2256 if (preferred_stack_boundary > crtl->stack_alignment_estimated) | |
2257 crtl->stack_alignment_estimated = preferred_stack_boundary; | |
2258 if (preferred_stack_boundary > crtl->stack_alignment_needed) | |
2259 crtl->stack_alignment_needed = preferred_stack_boundary; | |
2260 | |
2261 crtl->stack_realign_needed | |
2262 = INCOMING_STACK_BOUNDARY < crtl->stack_alignment_estimated; | |
2263 crtl->stack_realign_tried = crtl->stack_realign_needed; | |
2264 | |
2265 crtl->stack_realign_processed = true; | |
2266 | |
2267 /* Target has to redefine TARGET_GET_DRAP_RTX to support stack | |
2268 alignment. */ | |
2269 gcc_assert (targetm.calls.get_drap_rtx != NULL); | |
2270 drap_rtx = targetm.calls.get_drap_rtx (); | |
2271 | |
2272 /* stack_realign_drap and drap_rtx must match. */ | |
2273 gcc_assert ((stack_realign_drap != 0) == (drap_rtx != NULL)); | |
2274 | |
2275 /* Do nothing if NULL is returned, which means DRAP is not needed. */ | |
2276 if (NULL != drap_rtx) | |
2277 { | |
2278 crtl->args.internal_arg_pointer = drap_rtx; | |
2279 | |
2280 /* Call fixup_tail_calls to clean up REG_EQUIV note if DRAP is | |
2281 needed. */ | |
2282 fixup_tail_calls (); | |
2283 } | |
2284 } | |
2285 | |
2286 /* Translate the intermediate representation contained in the CFG | |
2287 from GIMPLE trees to RTL. | |
2288 | |
2289 We do conversion per basic block and preserve/update the tree CFG. | |
2290 This implies we have to do some magic as the CFG can simultaneously | |
2291 consist of basic blocks containing RTL and GIMPLE trees. This can | |
2292 confuse the CFG hooks, so be careful to not manipulate CFG during | |
2293 the expansion. */ | |
2294 | |
2295 static unsigned int | |
2296 gimple_expand_cfg (void) | |
2297 { | |
2298 basic_block bb, init_block; | |
2299 sbitmap blocks; | |
2300 edge_iterator ei; | |
2301 edge e; | |
2302 | |
2303 /* Some backends want to know that we are expanding to RTL. */ | |
2304 currently_expanding_to_rtl = 1; | |
2305 | |
2306 rtl_profile_for_bb (ENTRY_BLOCK_PTR); | |
2307 | |
2308 insn_locators_alloc (); | |
2309 if (!DECL_BUILT_IN (current_function_decl)) | |
2310 { | |
2311 /* Eventually, all FEs should explicitly set function_start_locus. */ | |
2312 if (cfun->function_start_locus == UNKNOWN_LOCATION) | |
2313 set_curr_insn_source_location | |
2314 (DECL_SOURCE_LOCATION (current_function_decl)); | |
2315 else | |
2316 set_curr_insn_source_location (cfun->function_start_locus); | |
2317 } | |
2318 set_curr_insn_block (DECL_INITIAL (current_function_decl)); | |
2319 prologue_locator = curr_insn_locator (); | |
2320 | |
2321 /* Make sure first insn is a note even if we don't want linenums. | |
2322 This makes sure the first insn will never be deleted. | |
2323 Also, final expects a note to appear there. */ | |
2324 emit_note (NOTE_INSN_DELETED); | |
2325 | |
2326 /* Mark arrays indexed with non-constant indices with TREE_ADDRESSABLE. */ | |
2327 discover_nonconstant_array_refs (); | |
2328 | |
2329 targetm.expand_to_rtl_hook (); | |
2330 crtl->stack_alignment_needed = STACK_BOUNDARY; | |
2331 crtl->max_used_stack_slot_alignment = STACK_BOUNDARY; | |
2332 crtl->stack_alignment_estimated = STACK_BOUNDARY; | |
2333 crtl->preferred_stack_boundary = STACK_BOUNDARY; | |
2334 cfun->cfg->max_jumptable_ents = 0; | |
2335 | |
2336 | |
2337 /* Expand the variables recorded during gimple lowering. */ | |
2338 expand_used_vars (); | |
2339 | |
2340 /* Honor stack protection warnings. */ | |
2341 if (warn_stack_protect) | |
2342 { | |
2343 if (cfun->calls_alloca) | |
2344 warning (OPT_Wstack_protector, | |
2345 "not protecting local variables: variable length buffer"); | |
2346 if (has_short_buffer && !crtl->stack_protect_guard) | |
2347 warning (OPT_Wstack_protector, | |
2348 "not protecting function: no buffer at least %d bytes long", | |
2349 (int) PARAM_VALUE (PARAM_SSP_BUFFER_SIZE)); | |
2350 } | |
2351 | |
2352 /* Set up parameters and prepare for return, for the function. */ | |
2353 expand_function_start (current_function_decl); | |
2354 | |
2355 /* If this function is `main', emit a call to `__main' | |
2356 to run global initializers, etc. */ | |
2357 if (DECL_NAME (current_function_decl) | |
2358 && MAIN_NAME_P (DECL_NAME (current_function_decl)) | |
2359 && DECL_FILE_SCOPE_P (current_function_decl)) | |
2360 expand_main_function (); | |
2361 | |
2362 /* Initialize the stack_protect_guard field. This must happen after the | |
2363 call to __main (if any) so that the external decl is initialized. */ | |
2364 if (crtl->stack_protect_guard) | |
2365 stack_protect_prologue (); | |
2366 | |
2367 /* Update stack boundary if needed. */ | |
2368 if (SUPPORTS_STACK_ALIGNMENT) | |
2369 { | |
2370 /* Call update_stack_boundary here to update incoming stack | |
2371 boundary before TARGET_FUNCTION_OK_FOR_SIBCALL is called. | |
2372 TARGET_FUNCTION_OK_FOR_SIBCALL needs to know the accurate | |
2373 incoming stack alignment to check if it is OK to perform | |
2374 sibcall optimization since sibcall optimization will only | |
2375 align the outgoing stack to incoming stack boundary. */ | |
2376 if (targetm.calls.update_stack_boundary) | |
2377 targetm.calls.update_stack_boundary (); | |
2378 | |
2379 /* The incoming stack frame has to be aligned at least at | |
2380 parm_stack_boundary. */ | |
2381 gcc_assert (crtl->parm_stack_boundary <= INCOMING_STACK_BOUNDARY); | |
2382 } | |
2383 | |
2384 /* Register rtl specific functions for cfg. */ | |
2385 rtl_register_cfg_hooks (); | |
2386 | |
2387 init_block = construct_init_block (); | |
2388 | |
2389 /* Clear EDGE_EXECUTABLE on the entry edge(s). It is cleaned from the | |
2390 remaining edges in expand_gimple_basic_block. */ | |
2391 FOR_EACH_EDGE (e, ei, ENTRY_BLOCK_PTR->succs) | |
2392 e->flags &= ~EDGE_EXECUTABLE; | |
2393 | |
2394 lab_rtx_for_bb = pointer_map_create (); | |
2395 FOR_BB_BETWEEN (bb, init_block->next_bb, EXIT_BLOCK_PTR, next_bb) | |
2396 bb = expand_gimple_basic_block (bb); | |
2397 | |
2398 /* Expansion is used by optimization passes too, set maybe_hot_insn_p | |
2399 conservatively to true until they are all profile aware. */ | |
2400 pointer_map_destroy (lab_rtx_for_bb); | |
2401 free_histograms (); | |
2402 | |
2403 construct_exit_block (); | |
2404 set_curr_insn_block (DECL_INITIAL (current_function_decl)); | |
2405 insn_locators_finalize (); | |
2406 | |
2407 /* We're done expanding trees to RTL. */ | |
2408 currently_expanding_to_rtl = 0; | |
2409 | |
2410 /* Convert tree EH labels to RTL EH labels and zap the tree EH table. */ | |
2411 convert_from_eh_region_ranges (); | |
2412 set_eh_throw_stmt_table (cfun, NULL); | |
2413 | |
2414 rebuild_jump_labels (get_insns ()); | |
2415 find_exception_handler_labels (); | |
2416 | |
2417 blocks = sbitmap_alloc (last_basic_block); | |
2418 sbitmap_ones (blocks); | |
2419 find_many_sub_basic_blocks (blocks); | |
2420 purge_all_dead_edges (); | |
2421 sbitmap_free (blocks); | |
2422 | |
2423 compact_blocks (); | |
2424 | |
2425 expand_stack_alignment (); | |
2426 | |
2427 #ifdef ENABLE_CHECKING | |
2428 verify_flow_info (); | |
2429 #endif | |
2430 | |
2431 /* There's no need to defer outputting this function any more; we | |
2432 know we want to output it. */ | |
2433 DECL_DEFER_OUTPUT (current_function_decl) = 0; | |
2434 | |
2435 /* Now that we're done expanding trees to RTL, we shouldn't have any | |
2436 more CONCATs anywhere. */ | |
2437 generating_concat_p = 0; | |
2438 | |
2439 if (dump_file) | |
2440 { | |
2441 fprintf (dump_file, | |
2442 "\n\n;;\n;; Full RTL generated for this function:\n;;\n"); | |
2443 /* And the pass manager will dump RTL for us. */ | |
2444 } | |
2445 | |
2446 /* If we're emitting a nested function, make sure its parent gets | |
2447 emitted as well. Doing otherwise confuses debug info. */ | |
2448 { | |
2449 tree parent; | |
2450 for (parent = DECL_CONTEXT (current_function_decl); | |
2451 parent != NULL_TREE; | |
2452 parent = get_containing_scope (parent)) | |
2453 if (TREE_CODE (parent) == FUNCTION_DECL) | |
2454 TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (parent)) = 1; | |
2455 } | |
2456 | |
2457 /* We are now committed to emitting code for this function. Do any | |
2458 preparation, such as emitting abstract debug info for the inline | |
2459 before it gets mangled by optimization. */ | |
2460 if (cgraph_function_possibly_inlined_p (current_function_decl)) | |
2461 (*debug_hooks->outlining_inline_function) (current_function_decl); | |
2462 | |
2463 TREE_ASM_WRITTEN (current_function_decl) = 1; | |
2464 | |
2465 /* After expanding, the return labels are no longer needed. */ | |
2466 return_label = NULL; | |
2467 naked_return_label = NULL; | |
2468 /* Tag the blocks with a depth number so that change_scope can find | |
2469 the common parent easily. */ | |
2470 set_block_levels (DECL_INITIAL (cfun->decl), 0); | |
2471 default_rtl_profile (); | |
2472 return 0; | |
2473 } | |
2474 | |
2475 struct rtl_opt_pass pass_expand = | |
2476 { | |
2477 { | |
2478 RTL_PASS, | |
2479 "expand", /* name */ | |
2480 NULL, /* gate */ | |
2481 gimple_expand_cfg, /* execute */ | |
2482 NULL, /* sub */ | |
2483 NULL, /* next */ | |
2484 0, /* static_pass_number */ | |
2485 TV_EXPAND, /* tv_id */ | |
2486 /* ??? If TER is enabled, we actually receive GENERIC. */ | |
2487 PROP_gimple_leh | PROP_cfg, /* properties_required */ | |
2488 PROP_rtl, /* properties_provided */ | |
2489 PROP_trees, /* properties_destroyed */ | |
2490 0, /* todo_flags_start */ | |
2491 TODO_dump_func, /* todo_flags_finish */ | |
2492 } | |
2493 }; |