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