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