]>
Commit | Line | Data |
---|---|---|
726a989a | 1 | /* GIMPLE lowering pass. Converts High GIMPLE into Low GIMPLE. |
6de9cd9a | 2 | |
d1e082c2 | 3 | Copyright (C) 2003-2013 Free Software Foundation, Inc. |
6de9cd9a DN |
4 | |
5 | This file is part of GCC. | |
6 | ||
7 | GCC is free software; you can redistribute it and/or modify it under | |
8 | the terms of the GNU General Public License as published by the Free | |
9dcd6f09 | 9 | Software Foundation; either version 3, or (at your option) any later |
6de9cd9a DN |
10 | version. |
11 | ||
12 | GCC is distributed in the hope that it will be useful, but WITHOUT ANY | |
13 | WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
14 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
15 | 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/>. */ | |
6de9cd9a DN |
20 | |
21 | #include "config.h" | |
22 | #include "system.h" | |
23 | #include "coretypes.h" | |
24 | #include "tm.h" | |
25 | #include "tree.h" | |
d8a2d370 DN |
26 | #include "tree-nested.h" |
27 | #include "calls.h" | |
2fb9a547 AM |
28 | #include "basic-block.h" |
29 | #include "tree-ssa-alias.h" | |
30 | #include "internal-fn.h" | |
31 | #include "gimple-expr.h" | |
32 | #include "is-a.h" | |
726a989a | 33 | #include "gimple.h" |
5be5c238 | 34 | #include "gimple-iterator.h" |
726a989a | 35 | #include "tree-iterator.h" |
6de9cd9a | 36 | #include "tree-inline.h" |
6de9cd9a DN |
37 | #include "flags.h" |
38 | #include "function.h" | |
718f9c0f | 39 | #include "diagnostic-core.h" |
6de9cd9a | 40 | #include "tree-pass.h" |
88cd0e88 | 41 | #include "langhooks.h" |
4484a35a | 42 | #include "gimple-low.h" |
1fe37220 | 43 | #include "tree-nested.h" |
6de9cd9a | 44 | |
726a989a RB |
45 | /* The differences between High GIMPLE and Low GIMPLE are the |
46 | following: | |
47 | ||
48 | 1- Lexical scopes are removed (i.e., GIMPLE_BIND disappears). | |
49 | ||
50 | 2- GIMPLE_TRY and GIMPLE_CATCH are converted to abnormal control | |
51 | flow and exception regions are built as an on-the-side region | |
52 | hierarchy (See tree-eh.c:lower_eh_constructs). | |
53 | ||
54 | 3- Multiple identical return statements are grouped into a single | |
55 | return and gotos to the unique return site. */ | |
56 | ||
57 | /* Match a return statement with a label. During lowering, we identify | |
58 | identical return statements and replace duplicates with a jump to | |
59 | the corresponding label. */ | |
60 | struct return_statements_t | |
61 | { | |
62 | tree label; | |
63 | gimple stmt; | |
64 | }; | |
65 | typedef struct return_statements_t return_statements_t; | |
66 | ||
726a989a | 67 | |
6de9cd9a DN |
68 | struct lower_data |
69 | { | |
70 | /* Block the current statement belongs to. */ | |
71 | tree block; | |
f5a76aea | 72 | |
726a989a | 73 | /* A vector of label and return statements to be moved to the end |
71877985 | 74 | of the function. */ |
9771b263 | 75 | vec<return_statements_t> return_statements; |
4f6c2131 | 76 | |
a141816c EB |
77 | /* True if the current statement cannot fall through. */ |
78 | bool cannot_fallthru; | |
79 | ||
4f6c2131 EB |
80 | /* True if the function calls __builtin_setjmp. */ |
81 | bool calls_builtin_setjmp; | |
6de9cd9a DN |
82 | }; |
83 | ||
726a989a RB |
84 | static void lower_stmt (gimple_stmt_iterator *, struct lower_data *); |
85 | static void lower_gimple_bind (gimple_stmt_iterator *, struct lower_data *); | |
f778c049 | 86 | static void lower_try_catch (gimple_stmt_iterator *, struct lower_data *); |
726a989a RB |
87 | static void lower_gimple_return (gimple_stmt_iterator *, struct lower_data *); |
88 | static void lower_builtin_setjmp (gimple_stmt_iterator *); | |
6de9cd9a | 89 | |
726a989a RB |
90 | |
91 | /* Lower the body of current_function_decl from High GIMPLE into Low | |
92 | GIMPLE. */ | |
6de9cd9a | 93 | |
c2924966 | 94 | static unsigned int |
6de9cd9a DN |
95 | lower_function_body (void) |
96 | { | |
97 | struct lower_data data; | |
726a989a RB |
98 | gimple_seq body = gimple_body (current_function_decl); |
99 | gimple_seq lowered_body; | |
100 | gimple_stmt_iterator i; | |
101 | gimple bind; | |
102 | tree t; | |
103 | gimple x; | |
104 | ||
105 | /* The gimplifier should've left a body of exactly one statement, | |
106 | namely a GIMPLE_BIND. */ | |
107 | gcc_assert (gimple_seq_first (body) == gimple_seq_last (body) | |
108 | && gimple_code (gimple_seq_first_stmt (body)) == GIMPLE_BIND); | |
6de9cd9a | 109 | |
953ff289 | 110 | memset (&data, 0, sizeof (data)); |
6de9cd9a DN |
111 | data.block = DECL_INITIAL (current_function_decl); |
112 | BLOCK_SUBBLOCKS (data.block) = NULL_TREE; | |
113 | BLOCK_CHAIN (data.block) = NULL_TREE; | |
114 | TREE_ASM_WRITTEN (data.block) = 1; | |
9771b263 | 115 | data.return_statements.create (8); |
726a989a RB |
116 | |
117 | bind = gimple_seq_first_stmt (body); | |
118 | lowered_body = NULL; | |
119 | gimple_seq_add_stmt (&lowered_body, bind); | |
120 | i = gsi_start (lowered_body); | |
121 | lower_gimple_bind (&i, &data); | |
6de9cd9a | 122 | |
726a989a | 123 | i = gsi_last (lowered_body); |
ff98621c RH |
124 | |
125 | /* If the function falls off the end, we need a null return statement. | |
726a989a | 126 | If we've already got one in the return_statements vector, we don't |
ff98621c | 127 | need to do anything special. Otherwise build one by hand. */ |
726a989a | 128 | if (gimple_seq_may_fallthru (lowered_body) |
9771b263 | 129 | && (data.return_statements.is_empty () |
c3284718 RS |
130 | || (gimple_return_retval (data.return_statements.last().stmt) |
131 | != NULL))) | |
ff98621c | 132 | { |
726a989a RB |
133 | x = gimple_build_return (NULL); |
134 | gimple_set_location (x, cfun->function_end_locus); | |
cc2a64dd | 135 | gimple_set_block (x, DECL_INITIAL (current_function_decl)); |
726a989a | 136 | gsi_insert_after (&i, x, GSI_CONTINUE_LINKING); |
ff98621c RH |
137 | } |
138 | ||
139 | /* If we lowered any return statements, emit the representative | |
140 | at the end of the function. */ | |
9771b263 | 141 | while (!data.return_statements.is_empty ()) |
f5a76aea | 142 | { |
9771b263 | 143 | return_statements_t t = data.return_statements.pop (); |
726a989a RB |
144 | x = gimple_build_label (t.label); |
145 | gsi_insert_after (&i, x, GSI_CONTINUE_LINKING); | |
726a989a | 146 | gsi_insert_after (&i, t.stmt, GSI_CONTINUE_LINKING); |
f5a76aea RH |
147 | } |
148 | ||
4f6c2131 EB |
149 | /* If the function calls __builtin_setjmp, we need to emit the computed |
150 | goto that will serve as the unique dispatcher for all the receivers. */ | |
151 | if (data.calls_builtin_setjmp) | |
152 | { | |
153 | tree disp_label, disp_var, arg; | |
154 | ||
155 | /* Build 'DISP_LABEL:' and insert. */ | |
c2255bc4 | 156 | disp_label = create_artificial_label (cfun->function_end_locus); |
4f6c2131 EB |
157 | /* This mark will create forward edges from every call site. */ |
158 | DECL_NONLOCAL (disp_label) = 1; | |
e3b5732b | 159 | cfun->has_nonlocal_label = 1; |
726a989a RB |
160 | x = gimple_build_label (disp_label); |
161 | gsi_insert_after (&i, x, GSI_CONTINUE_LINKING); | |
4f6c2131 EB |
162 | |
163 | /* Build 'DISP_VAR = __builtin_setjmp_dispatcher (DISP_LABEL);' | |
164 | and insert. */ | |
165 | disp_var = create_tmp_var (ptr_type_node, "setjmpvar"); | |
5039610b | 166 | arg = build_addr (disp_label, current_function_decl); |
e79983f4 | 167 | t = builtin_decl_implicit (BUILT_IN_SETJMP_DISPATCHER); |
726a989a RB |
168 | x = gimple_build_call (t, 1, arg); |
169 | gimple_call_set_lhs (x, disp_var); | |
4f6c2131 EB |
170 | |
171 | /* Build 'goto DISP_VAR;' and insert. */ | |
726a989a RB |
172 | gsi_insert_after (&i, x, GSI_CONTINUE_LINKING); |
173 | x = gimple_build_goto (disp_var); | |
174 | gsi_insert_after (&i, x, GSI_CONTINUE_LINKING); | |
4f6c2131 EB |
175 | } |
176 | ||
355a7673 MM |
177 | /* Once the old body has been lowered, replace it with the new |
178 | lowered sequence. */ | |
179 | gimple_set_body (current_function_decl, lowered_body); | |
180 | ||
282899df | 181 | gcc_assert (data.block == DECL_INITIAL (current_function_decl)); |
6de9cd9a DN |
182 | BLOCK_SUBBLOCKS (data.block) |
183 | = blocks_nreverse (BLOCK_SUBBLOCKS (data.block)); | |
184 | ||
185 | clear_block_marks (data.block); | |
9771b263 | 186 | data.return_statements.release (); |
c2924966 | 187 | return 0; |
6de9cd9a DN |
188 | } |
189 | ||
27a4cd48 DM |
190 | namespace { |
191 | ||
192 | const pass_data pass_data_lower_cf = | |
6de9cd9a | 193 | { |
27a4cd48 DM |
194 | GIMPLE_PASS, /* type */ |
195 | "lower", /* name */ | |
196 | OPTGROUP_NONE, /* optinfo_flags */ | |
197 | false, /* has_gate */ | |
198 | true, /* has_execute */ | |
199 | TV_NONE, /* tv_id */ | |
200 | PROP_gimple_any, /* properties_required */ | |
201 | PROP_gimple_lcf, /* properties_provided */ | |
202 | 0, /* properties_destroyed */ | |
203 | 0, /* todo_flags_start */ | |
204 | 0, /* todo_flags_finish */ | |
6de9cd9a DN |
205 | }; |
206 | ||
27a4cd48 DM |
207 | class pass_lower_cf : public gimple_opt_pass |
208 | { | |
209 | public: | |
c3284718 RS |
210 | pass_lower_cf (gcc::context *ctxt) |
211 | : gimple_opt_pass (pass_data_lower_cf, ctxt) | |
27a4cd48 DM |
212 | {} |
213 | ||
214 | /* opt_pass methods: */ | |
215 | unsigned int execute () { return lower_function_body (); } | |
216 | ||
217 | }; // class pass_lower_cf | |
218 | ||
219 | } // anon namespace | |
220 | ||
221 | gimple_opt_pass * | |
222 | make_pass_lower_cf (gcc::context *ctxt) | |
223 | { | |
224 | return new pass_lower_cf (ctxt); | |
225 | } | |
226 | ||
726a989a | 227 | /* Lower sequence SEQ. Unlike gimplification the statements are not relowered |
6de9cd9a DN |
228 | when they are changed -- if this has to be done, the lowering routine must |
229 | do it explicitly. DATA is passed through the recursion. */ | |
230 | ||
1ebf7687 | 231 | static void |
355a7673 | 232 | lower_sequence (gimple_seq *seq, struct lower_data *data) |
6de9cd9a | 233 | { |
726a989a | 234 | gimple_stmt_iterator gsi; |
6de9cd9a | 235 | |
355a7673 | 236 | for (gsi = gsi_start (*seq); !gsi_end_p (gsi); ) |
726a989a | 237 | lower_stmt (&gsi, data); |
6de9cd9a DN |
238 | } |
239 | ||
50674e96 | 240 | |
726a989a | 241 | /* Lower the OpenMP directive statement pointed by GSI. DATA is |
50674e96 DN |
242 | passed through the recursion. */ |
243 | ||
244 | static void | |
726a989a | 245 | lower_omp_directive (gimple_stmt_iterator *gsi, struct lower_data *data) |
50674e96 | 246 | { |
726a989a | 247 | gimple stmt; |
b8698a0f | 248 | |
726a989a | 249 | stmt = gsi_stmt (*gsi); |
50674e96 | 250 | |
355a7673 MM |
251 | lower_sequence (gimple_omp_body_ptr (stmt), data); |
252 | gsi_insert_seq_after (gsi, gimple_omp_body (stmt), GSI_CONTINUE_LINKING); | |
726a989a | 253 | gimple_omp_set_body (stmt, NULL); |
355a7673 | 254 | gsi_next (gsi); |
50674e96 DN |
255 | } |
256 | ||
257 | ||
a141816c EB |
258 | /* Lower statement GSI. DATA is passed through the recursion. We try to |
259 | track the fallthruness of statements and get rid of unreachable return | |
260 | statements in order to prevent the EH lowering pass from adding useless | |
261 | edges that can cause bogus warnings to be issued later; this guess need | |
262 | not be 100% accurate, simply be conservative and reset cannot_fallthru | |
263 | to false if we don't know. */ | |
6de9cd9a DN |
264 | |
265 | static void | |
726a989a | 266 | lower_stmt (gimple_stmt_iterator *gsi, struct lower_data *data) |
6de9cd9a | 267 | { |
726a989a | 268 | gimple stmt = gsi_stmt (*gsi); |
6de9cd9a | 269 | |
726a989a | 270 | gimple_set_block (stmt, data->block); |
6de9cd9a | 271 | |
726a989a | 272 | switch (gimple_code (stmt)) |
6de9cd9a | 273 | { |
726a989a RB |
274 | case GIMPLE_BIND: |
275 | lower_gimple_bind (gsi, data); | |
a141816c | 276 | /* Propagate fallthruness. */ |
f5a76aea | 277 | return; |
6de9cd9a | 278 | |
726a989a | 279 | case GIMPLE_COND: |
a141816c EB |
280 | case GIMPLE_GOTO: |
281 | case GIMPLE_SWITCH: | |
282 | data->cannot_fallthru = true; | |
283 | gsi_next (gsi); | |
284 | return; | |
726a989a RB |
285 | |
286 | case GIMPLE_RETURN: | |
a141816c EB |
287 | if (data->cannot_fallthru) |
288 | { | |
289 | gsi_remove (gsi, false); | |
290 | /* Propagate fallthruness. */ | |
291 | } | |
292 | else | |
293 | { | |
294 | lower_gimple_return (gsi, data); | |
295 | data->cannot_fallthru = true; | |
296 | } | |
726a989a RB |
297 | return; |
298 | ||
299 | case GIMPLE_TRY: | |
f778c049 EB |
300 | if (gimple_try_kind (stmt) == GIMPLE_TRY_CATCH) |
301 | lower_try_catch (gsi, data); | |
302 | else | |
303 | { | |
304 | /* It must be a GIMPLE_TRY_FINALLY. */ | |
305 | bool cannot_fallthru; | |
306 | lower_sequence (gimple_try_eval_ptr (stmt), data); | |
307 | cannot_fallthru = data->cannot_fallthru; | |
308 | ||
309 | /* The finally clause is always executed after the try clause, | |
310 | so if it does not fall through, then the try-finally will not | |
311 | fall through. Otherwise, if the try clause does not fall | |
312 | through, then when the finally clause falls through it will | |
313 | resume execution wherever the try clause was going. So the | |
314 | whole try-finally will only fall through if both the try | |
315 | clause and the finally clause fall through. */ | |
316 | data->cannot_fallthru = false; | |
317 | lower_sequence (gimple_try_cleanup_ptr (stmt), data); | |
318 | data->cannot_fallthru |= cannot_fallthru; | |
319 | gsi_next (gsi); | |
320 | } | |
321 | return; | |
777f7f9a | 322 | |
0a35513e | 323 | case GIMPLE_EH_ELSE: |
355a7673 MM |
324 | lower_sequence (gimple_eh_else_n_body_ptr (stmt), data); |
325 | lower_sequence (gimple_eh_else_e_body_ptr (stmt), data); | |
0a35513e AH |
326 | break; |
327 | ||
726a989a RB |
328 | case GIMPLE_NOP: |
329 | case GIMPLE_ASM: | |
330 | case GIMPLE_ASSIGN: | |
726a989a RB |
331 | case GIMPLE_PREDICT: |
332 | case GIMPLE_LABEL: | |
1d65f45c | 333 | case GIMPLE_EH_MUST_NOT_THROW: |
726a989a RB |
334 | case GIMPLE_OMP_FOR: |
335 | case GIMPLE_OMP_SECTIONS: | |
336 | case GIMPLE_OMP_SECTIONS_SWITCH: | |
337 | case GIMPLE_OMP_SECTION: | |
338 | case GIMPLE_OMP_SINGLE: | |
339 | case GIMPLE_OMP_MASTER: | |
acf0174b | 340 | case GIMPLE_OMP_TASKGROUP: |
726a989a RB |
341 | case GIMPLE_OMP_ORDERED: |
342 | case GIMPLE_OMP_CRITICAL: | |
343 | case GIMPLE_OMP_RETURN: | |
344 | case GIMPLE_OMP_ATOMIC_LOAD: | |
345 | case GIMPLE_OMP_ATOMIC_STORE: | |
346 | case GIMPLE_OMP_CONTINUE: | |
347 | break; | |
4f6c2131 | 348 | |
726a989a | 349 | case GIMPLE_CALL: |
4f6c2131 | 350 | { |
726a989a | 351 | tree decl = gimple_call_fndecl (stmt); |
f16dd822 DC |
352 | unsigned i; |
353 | ||
354 | for (i = 0; i < gimple_call_num_args (stmt); i++) | |
355 | { | |
356 | tree arg = gimple_call_arg (stmt, i); | |
357 | if (EXPR_P (arg)) | |
358 | TREE_SET_BLOCK (arg, data->block); | |
359 | } | |
726a989a | 360 | |
4f6c2131 EB |
361 | if (decl |
362 | && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL | |
363 | && DECL_FUNCTION_CODE (decl) == BUILT_IN_SETJMP) | |
364 | { | |
726a989a | 365 | lower_builtin_setjmp (gsi); |
a141816c EB |
366 | data->cannot_fallthru = false; |
367 | data->calls_builtin_setjmp = true; | |
4f6c2131 EB |
368 | return; |
369 | } | |
79ddec02 | 370 | |
79ddec02 EB |
371 | if (decl && (flags_from_decl_or_type (decl) & ECF_NORETURN)) |
372 | { | |
a141816c | 373 | data->cannot_fallthru = true; |
79ddec02 | 374 | gsi_next (gsi); |
79ddec02 EB |
375 | return; |
376 | } | |
4f6c2131 EB |
377 | } |
378 | break; | |
379 | ||
726a989a RB |
380 | case GIMPLE_OMP_PARALLEL: |
381 | case GIMPLE_OMP_TASK: | |
acf0174b JJ |
382 | case GIMPLE_OMP_TARGET: |
383 | case GIMPLE_OMP_TEAMS: | |
a141816c | 384 | data->cannot_fallthru = false; |
726a989a | 385 | lower_omp_directive (gsi, data); |
a141816c | 386 | data->cannot_fallthru = false; |
50674e96 DN |
387 | return; |
388 | ||
0a35513e | 389 | case GIMPLE_TRANSACTION: |
355a7673 | 390 | lower_sequence (gimple_transaction_body_ptr (stmt), data); |
0a35513e AH |
391 | break; |
392 | ||
6de9cd9a | 393 | default: |
282899df | 394 | gcc_unreachable (); |
6de9cd9a DN |
395 | } |
396 | ||
a141816c | 397 | data->cannot_fallthru = false; |
726a989a | 398 | gsi_next (gsi); |
6de9cd9a DN |
399 | } |
400 | ||
4f6c2131 | 401 | /* Lower a bind_expr TSI. DATA is passed through the recursion. */ |
6de9cd9a DN |
402 | |
403 | static void | |
726a989a | 404 | lower_gimple_bind (gimple_stmt_iterator *gsi, struct lower_data *data) |
6de9cd9a DN |
405 | { |
406 | tree old_block = data->block; | |
726a989a RB |
407 | gimple stmt = gsi_stmt (*gsi); |
408 | tree new_block = gimple_bind_block (stmt); | |
6de9cd9a DN |
409 | |
410 | if (new_block) | |
411 | { | |
412 | if (new_block == old_block) | |
413 | { | |
414 | /* The outermost block of the original function may not be the | |
415 | outermost statement chain of the gimplified function. So we | |
416 | may see the outermost block just inside the function. */ | |
282899df | 417 | gcc_assert (new_block == DECL_INITIAL (current_function_decl)); |
6de9cd9a DN |
418 | new_block = NULL; |
419 | } | |
420 | else | |
421 | { | |
422 | /* We do not expect to handle duplicate blocks. */ | |
282899df | 423 | gcc_assert (!TREE_ASM_WRITTEN (new_block)); |
6de9cd9a DN |
424 | TREE_ASM_WRITTEN (new_block) = 1; |
425 | ||
426 | /* Block tree may get clobbered by inlining. Normally this would | |
427 | be fixed in rest_of_decl_compilation using block notes, but | |
428 | since we are not going to emit them, it is up to us. */ | |
429 | BLOCK_CHAIN (new_block) = BLOCK_SUBBLOCKS (old_block); | |
430 | BLOCK_SUBBLOCKS (old_block) = new_block; | |
431 | BLOCK_SUBBLOCKS (new_block) = NULL_TREE; | |
432 | BLOCK_SUPERCONTEXT (new_block) = old_block; | |
433 | ||
434 | data->block = new_block; | |
435 | } | |
436 | } | |
437 | ||
726a989a | 438 | record_vars (gimple_bind_vars (stmt)); |
355a7673 | 439 | lower_sequence (gimple_bind_body_ptr (stmt), data); |
6de9cd9a DN |
440 | |
441 | if (new_block) | |
442 | { | |
282899df | 443 | gcc_assert (data->block == new_block); |
6de9cd9a DN |
444 | |
445 | BLOCK_SUBBLOCKS (new_block) | |
446 | = blocks_nreverse (BLOCK_SUBBLOCKS (new_block)); | |
447 | data->block = old_block; | |
448 | } | |
449 | ||
726a989a RB |
450 | /* The GIMPLE_BIND no longer carries any useful information -- kill it. */ |
451 | gsi_insert_seq_before (gsi, gimple_bind_body (stmt), GSI_SAME_STMT); | |
452 | gsi_remove (gsi, false); | |
6de9cd9a DN |
453 | } |
454 | ||
f778c049 EB |
455 | /* Same as above, but for a GIMPLE_TRY_CATCH. */ |
456 | ||
457 | static void | |
458 | lower_try_catch (gimple_stmt_iterator *gsi, struct lower_data *data) | |
459 | { | |
460 | bool cannot_fallthru; | |
461 | gimple stmt = gsi_stmt (*gsi); | |
462 | gimple_stmt_iterator i; | |
463 | ||
464 | /* We don't handle GIMPLE_TRY_FINALLY. */ | |
465 | gcc_assert (gimple_try_kind (stmt) == GIMPLE_TRY_CATCH); | |
466 | ||
467 | lower_sequence (gimple_try_eval_ptr (stmt), data); | |
468 | cannot_fallthru = data->cannot_fallthru; | |
469 | ||
470 | i = gsi_start (*gimple_try_cleanup_ptr (stmt)); | |
471 | switch (gimple_code (gsi_stmt (i))) | |
472 | { | |
473 | case GIMPLE_CATCH: | |
474 | /* We expect to see a sequence of GIMPLE_CATCH stmts, each with a | |
475 | catch expression and a body. The whole try/catch may fall | |
476 | through iff any of the catch bodies falls through. */ | |
477 | for (; !gsi_end_p (i); gsi_next (&i)) | |
478 | { | |
479 | data->cannot_fallthru = false; | |
480 | lower_sequence (gimple_catch_handler_ptr (gsi_stmt (i)), data); | |
481 | if (!data->cannot_fallthru) | |
482 | cannot_fallthru = false; | |
483 | } | |
484 | break; | |
485 | ||
486 | case GIMPLE_EH_FILTER: | |
487 | /* The exception filter expression only matters if there is an | |
488 | exception. If the exception does not match EH_FILTER_TYPES, | |
489 | we will execute EH_FILTER_FAILURE, and we will fall through | |
490 | if that falls through. If the exception does match | |
491 | EH_FILTER_TYPES, the stack unwinder will continue up the | |
492 | stack, so we will not fall through. We don't know whether we | |
493 | will throw an exception which matches EH_FILTER_TYPES or not, | |
494 | so we just ignore EH_FILTER_TYPES and assume that we might | |
495 | throw an exception which doesn't match. */ | |
496 | data->cannot_fallthru = false; | |
497 | lower_sequence (gimple_eh_filter_failure_ptr (gsi_stmt (i)), data); | |
498 | if (!data->cannot_fallthru) | |
499 | cannot_fallthru = false; | |
500 | break; | |
501 | ||
502 | default: | |
503 | /* This case represents statements to be executed when an | |
504 | exception occurs. Those statements are implicitly followed | |
505 | by a GIMPLE_RESX to resume execution after the exception. So | |
506 | in this case the try/catch never falls through. */ | |
507 | data->cannot_fallthru = false; | |
508 | lower_sequence (gimple_try_cleanup_ptr (stmt), data); | |
509 | break; | |
510 | } | |
511 | ||
512 | data->cannot_fallthru = cannot_fallthru; | |
513 | gsi_next (gsi); | |
514 | } | |
515 | ||
6737ba67 | 516 | |
cf2d1b38 AM |
517 | /* Try to determine whether a TRY_CATCH expression can fall through. |
518 | This is a subroutine of gimple_stmt_may_fallthru. */ | |
726a989a RB |
519 | |
520 | static bool | |
521 | gimple_try_catch_may_fallthru (gimple stmt) | |
522 | { | |
523 | gimple_stmt_iterator i; | |
524 | ||
525 | /* We don't handle GIMPLE_TRY_FINALLY. */ | |
526 | gcc_assert (gimple_try_kind (stmt) == GIMPLE_TRY_CATCH); | |
527 | ||
528 | /* If the TRY block can fall through, the whole TRY_CATCH can | |
529 | fall through. */ | |
530 | if (gimple_seq_may_fallthru (gimple_try_eval (stmt))) | |
531 | return true; | |
532 | ||
355a7673 | 533 | i = gsi_start (*gimple_try_cleanup_ptr (stmt)); |
726a989a RB |
534 | switch (gimple_code (gsi_stmt (i))) |
535 | { | |
536 | case GIMPLE_CATCH: | |
537 | /* We expect to see a sequence of GIMPLE_CATCH stmts, each with a | |
538 | catch expression and a body. The whole try/catch may fall | |
539 | through iff any of the catch bodies falls through. */ | |
540 | for (; !gsi_end_p (i); gsi_next (&i)) | |
541 | { | |
542 | if (gimple_seq_may_fallthru (gimple_catch_handler (gsi_stmt (i)))) | |
543 | return true; | |
544 | } | |
545 | return false; | |
546 | ||
547 | case GIMPLE_EH_FILTER: | |
548 | /* The exception filter expression only matters if there is an | |
549 | exception. If the exception does not match EH_FILTER_TYPES, | |
550 | we will execute EH_FILTER_FAILURE, and we will fall through | |
551 | if that falls through. If the exception does match | |
552 | EH_FILTER_TYPES, the stack unwinder will continue up the | |
553 | stack, so we will not fall through. We don't know whether we | |
554 | will throw an exception which matches EH_FILTER_TYPES or not, | |
555 | so we just ignore EH_FILTER_TYPES and assume that we might | |
556 | throw an exception which doesn't match. */ | |
557 | return gimple_seq_may_fallthru (gimple_eh_filter_failure (gsi_stmt (i))); | |
558 | ||
559 | default: | |
560 | /* This case represents statements to be executed when an | |
561 | exception occurs. Those statements are implicitly followed | |
562 | by a GIMPLE_RESX to resume execution after the exception. So | |
563 | in this case the try/catch never falls through. */ | |
564 | return false; | |
565 | } | |
566 | } | |
567 | ||
568 | ||
726a989a RB |
569 | /* Try to determine if we can continue executing the statement |
570 | immediately following STMT. This guess need not be 100% accurate; | |
571 | simply be conservative and return true if we don't know. This is | |
572 | used only to avoid stupidly generating extra code. If we're wrong, | |
573 | we'll just delete the extra code later. */ | |
574 | ||
575 | bool | |
576 | gimple_stmt_may_fallthru (gimple stmt) | |
6de9cd9a | 577 | { |
726a989a RB |
578 | if (!stmt) |
579 | return true; | |
6de9cd9a | 580 | |
726a989a RB |
581 | switch (gimple_code (stmt)) |
582 | { | |
583 | case GIMPLE_GOTO: | |
584 | case GIMPLE_RETURN: | |
585 | case GIMPLE_RESX: | |
b8698a0f | 586 | /* Easy cases. If the last statement of the seq implies |
726a989a RB |
587 | control transfer, then we can't fall through. */ |
588 | return false; | |
6de9cd9a | 589 | |
726a989a | 590 | case GIMPLE_SWITCH: |
a141816c EB |
591 | /* Switch has already been lowered and represents a branch |
592 | to a selected label and hence can't fall through. */ | |
593 | return false; | |
6de9cd9a | 594 | |
726a989a RB |
595 | case GIMPLE_COND: |
596 | /* GIMPLE_COND's are already lowered into a two-way branch. They | |
597 | can't fall through. */ | |
598 | return false; | |
6de9cd9a | 599 | |
726a989a RB |
600 | case GIMPLE_BIND: |
601 | return gimple_seq_may_fallthru (gimple_bind_body (stmt)); | |
6de9cd9a | 602 | |
726a989a RB |
603 | case GIMPLE_TRY: |
604 | if (gimple_try_kind (stmt) == GIMPLE_TRY_CATCH) | |
605 | return gimple_try_catch_may_fallthru (stmt); | |
6de9cd9a | 606 | |
726a989a | 607 | /* It must be a GIMPLE_TRY_FINALLY. */ |
6de9cd9a | 608 | |
726a989a RB |
609 | /* The finally clause is always executed after the try clause, |
610 | so if it does not fall through, then the try-finally will not | |
611 | fall through. Otherwise, if the try clause does not fall | |
612 | through, then when the finally clause falls through it will | |
613 | resume execution wherever the try clause was going. So the | |
614 | whole try-finally will only fall through if both the try | |
615 | clause and the finally clause fall through. */ | |
616 | return (gimple_seq_may_fallthru (gimple_try_eval (stmt)) | |
617 | && gimple_seq_may_fallthru (gimple_try_cleanup (stmt))); | |
618 | ||
0a35513e AH |
619 | case GIMPLE_EH_ELSE: |
620 | return (gimple_seq_may_fallthru (gimple_eh_else_n_body (stmt)) | |
621 | || gimple_seq_may_fallthru (gimple_eh_else_e_body (stmt))); | |
622 | ||
726a989a RB |
623 | case GIMPLE_CALL: |
624 | /* Functions that do not return do not fall through. */ | |
625 | return (gimple_call_flags (stmt) & ECF_NORETURN) == 0; | |
a141816c | 626 | |
726a989a RB |
627 | default: |
628 | return true; | |
6de9cd9a | 629 | } |
726a989a RB |
630 | } |
631 | ||
6de9cd9a | 632 | |
726a989a | 633 | /* Same as gimple_stmt_may_fallthru, but for the gimple sequence SEQ. */ |
6de9cd9a | 634 | |
726a989a RB |
635 | bool |
636 | gimple_seq_may_fallthru (gimple_seq seq) | |
637 | { | |
638 | return gimple_stmt_may_fallthru (gimple_seq_last_stmt (seq)); | |
6de9cd9a | 639 | } |
f5a76aea | 640 | |
726a989a RB |
641 | |
642 | /* Lower a GIMPLE_RETURN GSI. DATA is passed through the recursion. */ | |
4f6c2131 | 643 | |
f5a76aea | 644 | static void |
726a989a | 645 | lower_gimple_return (gimple_stmt_iterator *gsi, struct lower_data *data) |
f5a76aea | 646 | { |
726a989a RB |
647 | gimple stmt = gsi_stmt (*gsi); |
648 | gimple t; | |
649 | int i; | |
650 | return_statements_t tmp_rs; | |
f5a76aea | 651 | |
71877985 | 652 | /* Match this up with an existing return statement that's been created. */ |
9771b263 | 653 | for (i = data->return_statements.length () - 1; |
726a989a | 654 | i >= 0; i--) |
f5a76aea | 655 | { |
9771b263 | 656 | tmp_rs = data->return_statements[i]; |
71877985 | 657 | |
726a989a | 658 | if (gimple_return_retval (stmt) == gimple_return_retval (tmp_rs.stmt)) |
0efb9d64 AK |
659 | { |
660 | /* Remove the line number from the representative return statement. | |
661 | It now fills in for many such returns. Failure to remove this | |
662 | will result in incorrect results for coverage analysis. */ | |
663 | gimple_set_location (tmp_rs.stmt, UNKNOWN_LOCATION); | |
664 | ||
665 | goto found; | |
666 | } | |
f5a76aea RH |
667 | } |
668 | ||
71877985 | 669 | /* Not found. Create a new label and record the return statement. */ |
c2255bc4 | 670 | tmp_rs.label = create_artificial_label (cfun->function_end_locus); |
726a989a | 671 | tmp_rs.stmt = stmt; |
9771b263 | 672 | data->return_statements.safe_push (tmp_rs); |
71877985 RH |
673 | |
674 | /* Generate a goto statement and remove the return statement. */ | |
675 | found: | |
516426da EB |
676 | /* When not optimizing, make sure user returns are preserved. */ |
677 | if (!optimize && gimple_has_location (stmt)) | |
678 | DECL_ARTIFICIAL (tmp_rs.label) = 0; | |
726a989a RB |
679 | t = gimple_build_goto (tmp_rs.label); |
680 | gimple_set_location (t, gimple_location (stmt)); | |
cc2a64dd | 681 | gimple_set_block (t, gimple_block (stmt)); |
726a989a RB |
682 | gsi_insert_before (gsi, t, GSI_SAME_STMT); |
683 | gsi_remove (gsi, false); | |
4f6c2131 EB |
684 | } |
685 | ||
a141816c | 686 | /* Lower a __builtin_setjmp GSI. |
4f6c2131 EB |
687 | |
688 | __builtin_setjmp is passed a pointer to an array of five words (not | |
689 | all will be used on all machines). It operates similarly to the C | |
690 | library function of the same name, but is more efficient. | |
691 | ||
692 | It is lowered into 3 other builtins, namely __builtin_setjmp_setup, | |
693 | __builtin_setjmp_dispatcher and __builtin_setjmp_receiver, but with | |
694 | __builtin_setjmp_dispatcher shared among all the instances; that's | |
695 | why it is only emitted at the end by lower_function_body. | |
696 | ||
697 | After full lowering, the body of the function should look like: | |
698 | ||
699 | { | |
700 | void * setjmpvar.0; | |
701 | int D.1844; | |
702 | int D.2844; | |
703 | ||
704 | [...] | |
705 | ||
706 | __builtin_setjmp_setup (&buf, &<D1847>); | |
707 | D.1844 = 0; | |
708 | goto <D1846>; | |
709 | <D1847>:; | |
710 | __builtin_setjmp_receiver (&<D1847>); | |
711 | D.1844 = 1; | |
712 | <D1846>:; | |
713 | if (D.1844 == 0) goto <D1848>; else goto <D1849>; | |
714 | ||
715 | [...] | |
716 | ||
717 | __builtin_setjmp_setup (&buf, &<D2847>); | |
718 | D.2844 = 0; | |
719 | goto <D2846>; | |
720 | <D2847>:; | |
721 | __builtin_setjmp_receiver (&<D2847>); | |
722 | D.2844 = 1; | |
723 | <D2846>:; | |
724 | if (D.2844 == 0) goto <D2848>; else goto <D2849>; | |
725 | ||
726 | [...] | |
727 | ||
728 | <D3850>:; | |
729 | return; | |
730 | <D3853>: [non-local]; | |
731 | setjmpvar.0 = __builtin_setjmp_dispatcher (&<D3853>); | |
732 | goto setjmpvar.0; | |
733 | } | |
734 | ||
735 | The dispatcher block will be both the unique destination of all the | |
736 | abnormal call edges and the unique source of all the abnormal edges | |
737 | to the receivers, thus keeping the complexity explosion localized. */ | |
738 | ||
739 | static void | |
726a989a | 740 | lower_builtin_setjmp (gimple_stmt_iterator *gsi) |
4f6c2131 | 741 | { |
726a989a | 742 | gimple stmt = gsi_stmt (*gsi); |
c2255bc4 AH |
743 | location_t loc = gimple_location (stmt); |
744 | tree cont_label = create_artificial_label (loc); | |
745 | tree next_label = create_artificial_label (loc); | |
4f6c2131 | 746 | tree dest, t, arg; |
726a989a | 747 | gimple g; |
4f6c2131 EB |
748 | |
749 | /* NEXT_LABEL is the label __builtin_longjmp will jump to. Its address is | |
750 | passed to both __builtin_setjmp_setup and __builtin_setjmp_receiver. */ | |
751 | FORCED_LABEL (next_label) = 1; | |
752 | ||
726a989a | 753 | dest = gimple_call_lhs (stmt); |
4f6c2131 EB |
754 | |
755 | /* Build '__builtin_setjmp_setup (BUF, NEXT_LABEL)' and insert. */ | |
5039610b | 756 | arg = build_addr (next_label, current_function_decl); |
e79983f4 | 757 | t = builtin_decl_implicit (BUILT_IN_SETJMP_SETUP); |
726a989a | 758 | g = gimple_build_call (t, 2, gimple_call_arg (stmt, 0), arg); |
db3927fb | 759 | gimple_set_location (g, loc); |
cc2a64dd | 760 | gimple_set_block (g, gimple_block (stmt)); |
726a989a | 761 | gsi_insert_before (gsi, g, GSI_SAME_STMT); |
4f6c2131 EB |
762 | |
763 | /* Build 'DEST = 0' and insert. */ | |
764 | if (dest) | |
765 | { | |
e8160c9a | 766 | g = gimple_build_assign (dest, build_zero_cst (TREE_TYPE (dest))); |
db3927fb | 767 | gimple_set_location (g, loc); |
cc2a64dd | 768 | gimple_set_block (g, gimple_block (stmt)); |
726a989a | 769 | gsi_insert_before (gsi, g, GSI_SAME_STMT); |
4f6c2131 EB |
770 | } |
771 | ||
772 | /* Build 'goto CONT_LABEL' and insert. */ | |
726a989a | 773 | g = gimple_build_goto (cont_label); |
bbbbb16a | 774 | gsi_insert_before (gsi, g, GSI_SAME_STMT); |
4f6c2131 EB |
775 | |
776 | /* Build 'NEXT_LABEL:' and insert. */ | |
726a989a RB |
777 | g = gimple_build_label (next_label); |
778 | gsi_insert_before (gsi, g, GSI_SAME_STMT); | |
4f6c2131 EB |
779 | |
780 | /* Build '__builtin_setjmp_receiver (NEXT_LABEL)' and insert. */ | |
5039610b | 781 | arg = build_addr (next_label, current_function_decl); |
e79983f4 | 782 | t = builtin_decl_implicit (BUILT_IN_SETJMP_RECEIVER); |
726a989a | 783 | g = gimple_build_call (t, 1, arg); |
db3927fb | 784 | gimple_set_location (g, loc); |
cc2a64dd | 785 | gimple_set_block (g, gimple_block (stmt)); |
726a989a | 786 | gsi_insert_before (gsi, g, GSI_SAME_STMT); |
4f6c2131 EB |
787 | |
788 | /* Build 'DEST = 1' and insert. */ | |
789 | if (dest) | |
790 | { | |
db3927fb AH |
791 | g = gimple_build_assign (dest, fold_convert_loc (loc, TREE_TYPE (dest), |
792 | integer_one_node)); | |
793 | gimple_set_location (g, loc); | |
cc2a64dd | 794 | gimple_set_block (g, gimple_block (stmt)); |
726a989a | 795 | gsi_insert_before (gsi, g, GSI_SAME_STMT); |
4f6c2131 EB |
796 | } |
797 | ||
798 | /* Build 'CONT_LABEL:' and insert. */ | |
726a989a RB |
799 | g = gimple_build_label (cont_label); |
800 | gsi_insert_before (gsi, g, GSI_SAME_STMT); | |
4f6c2131 EB |
801 | |
802 | /* Remove the call to __builtin_setjmp. */ | |
726a989a | 803 | gsi_remove (gsi, false); |
f5a76aea | 804 | } |
6de9cd9a DN |
805 | \f |
806 | ||
50674e96 | 807 | /* Record the variables in VARS into function FN. */ |
6de9cd9a DN |
808 | |
809 | void | |
50674e96 | 810 | record_vars_into (tree vars, tree fn) |
6de9cd9a | 811 | { |
af16bc76 MJ |
812 | bool change_cfun = fn != current_function_decl; |
813 | ||
814 | if (change_cfun) | |
db2960f4 | 815 | push_cfun (DECL_STRUCT_FUNCTION (fn)); |
50674e96 | 816 | |
910ad8de | 817 | for (; vars; vars = DECL_CHAIN (vars)) |
6de9cd9a DN |
818 | { |
819 | tree var = vars; | |
820 | ||
acb8f212 JH |
821 | /* BIND_EXPRs contains also function/type/constant declarations |
822 | we don't need to care about. */ | |
823 | if (TREE_CODE (var) != VAR_DECL) | |
824 | continue; | |
50674e96 | 825 | |
6de9cd9a DN |
826 | /* Nothing to do in this case. */ |
827 | if (DECL_EXTERNAL (var)) | |
828 | continue; | |
6de9cd9a DN |
829 | |
830 | /* Record the variable. */ | |
c021f10b | 831 | add_local_decl (cfun, var); |
6de9cd9a | 832 | } |
50674e96 | 833 | |
af16bc76 | 834 | if (change_cfun) |
db2960f4 | 835 | pop_cfun (); |
50674e96 DN |
836 | } |
837 | ||
838 | ||
839 | /* Record the variables in VARS into current_function_decl. */ | |
840 | ||
841 | void | |
842 | record_vars (tree vars) | |
843 | { | |
844 | record_vars_into (vars, current_function_decl); | |
6de9cd9a | 845 | } |