Line data Source code
1 : /* GIMPLE lowering pass. Converts High GIMPLE into Low GIMPLE.
2 :
3 : Copyright (C) 2003-2026 Free Software Foundation, Inc.
4 :
5 : This file is part of GCC.
6 :
7 : GCC is free software; you can redistribute it and/or modify it under
8 : the terms of the GNU General Public License as published by the Free
9 : Software Foundation; either version 3, or (at your option) any later
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
18 : along with GCC; see the file COPYING3. If not see
19 : <http://www.gnu.org/licenses/>. */
20 :
21 : #include "config.h"
22 : #include "system.h"
23 : #include "coretypes.h"
24 : #include "backend.h"
25 : #include "tree.h"
26 : #include "gimple.h"
27 : #include "tree-pass.h"
28 : #include "fold-const.h"
29 : #include "tree-nested.h"
30 : #include "calls.h"
31 : #include "gimple-iterator.h"
32 : #include "gimple-low.h"
33 : #include "predict.h"
34 : #include "gimple-predict.h"
35 : #include "gimple-fold.h"
36 : #include "cgraph.h"
37 : #include "tree-ssa.h"
38 : #include "value-range.h"
39 : #include "stringpool.h"
40 : #include "tree-ssanames.h"
41 : #include "tree-inline.h"
42 : #include "gimple-walk.h"
43 : #include "attribs.h"
44 : #include "diagnostic-core.h"
45 :
46 : /* The differences between High GIMPLE and Low GIMPLE are the
47 : following:
48 :
49 : 1- Lexical scopes are removed (i.e., GIMPLE_BIND disappears).
50 :
51 : 2- GIMPLE_TRY and GIMPLE_CATCH are converted to abnormal control
52 : flow and exception regions are built as an on-the-side region
53 : hierarchy (See tree-eh.cc:lower_eh_constructs).
54 :
55 : 3- Multiple identical return statements are grouped into a single
56 : return and gotos to the unique return site. */
57 :
58 : /* Match a return statement with a label. During lowering, we identify
59 : identical return statements and replace duplicates with a jump to
60 : the corresponding label. */
61 : struct return_statements_t
62 : {
63 : tree label;
64 : greturn *stmt;
65 : };
66 : typedef struct return_statements_t return_statements_t;
67 :
68 :
69 : struct lower_data
70 : {
71 : /* Block the current statement belongs to. */
72 : tree block;
73 :
74 : /* A vector of label and return statements to be moved to the end
75 : of the function. */
76 : vec<return_statements_t> return_statements;
77 :
78 : /* True if the current statement cannot fall through. */
79 : bool cannot_fallthru;
80 : };
81 :
82 : /* Bitmap of LABEL_DECL uids for user labels moved into assume outlined
83 : functions. */
84 : static bitmap assume_labels;
85 :
86 : static void lower_stmt (gimple_stmt_iterator *, struct lower_data *);
87 : static void lower_gimple_bind (gimple_stmt_iterator *, struct lower_data *);
88 : static void lower_try_catch (gimple_stmt_iterator *, struct lower_data *);
89 : static void lower_gimple_return (gimple_stmt_iterator *, struct lower_data *);
90 : static void lower_builtin_setjmp (gimple_stmt_iterator *);
91 : static void lower_builtin_posix_memalign (gimple_stmt_iterator *);
92 : static void lower_builtin_assume_aligned (gimple_stmt_iterator *);
93 :
94 :
95 : /* Helper function for lower_function_body, called via walk_gimple_seq.
96 : Diagnose uses of user labels defined inside of assume attribute
97 : expressions. */
98 :
99 : static tree
100 160 : diagnose_assume_labels (tree *tp, int *, void *data)
101 : {
102 160 : if (TREE_CODE (*tp) == LABEL_DECL
103 56 : && !DECL_ARTIFICIAL (*tp)
104 24 : && DECL_NAME (*tp)
105 184 : && bitmap_bit_p (assume_labels, DECL_UID (*tp)))
106 : {
107 24 : struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
108 24 : auto_diagnostic_group d;
109 24 : error_at (gimple_location (gsi_stmt (wi->gsi)),
110 : "reference to label %qD defined inside of %<assume%> "
111 : "attribute expression from outside of the attribute", *tp);
112 24 : inform (DECL_SOURCE_LOCATION (*tp), "%qD defined here", *tp);
113 24 : }
114 160 : return NULL_TREE;
115 : }
116 :
117 :
118 : /* Lower the body of current_function_decl from High GIMPLE into Low
119 : GIMPLE. */
120 :
121 : static unsigned int
122 3010153 : lower_function_body (void)
123 : {
124 3010153 : struct lower_data data;
125 3010153 : gimple_seq body = gimple_body (current_function_decl);
126 3010153 : gimple_seq lowered_body;
127 3010153 : gimple_stmt_iterator i;
128 3010153 : gimple *bind;
129 3010153 : gimple *x;
130 :
131 : /* The gimplifier should've left a body of exactly one statement,
132 : namely a GIMPLE_BIND. */
133 3010153 : gcc_assert (gimple_seq_first (body) == gimple_seq_last (body)
134 : && gimple_code (gimple_seq_first_stmt (body)) == GIMPLE_BIND);
135 :
136 3010153 : memset (&data, 0, sizeof (data));
137 3010153 : data.block = DECL_INITIAL (current_function_decl);
138 3010153 : BLOCK_SUBBLOCKS (data.block) = NULL_TREE;
139 3010153 : BLOCK_CHAIN (data.block) = NULL_TREE;
140 3010153 : TREE_ASM_WRITTEN (data.block) = 1;
141 3010153 : data.return_statements.create (8);
142 :
143 3010153 : bind = gimple_seq_first_stmt (body);
144 3010153 : lowered_body = NULL;
145 3010153 : gimple_seq_add_stmt (&lowered_body, bind);
146 3010153 : i = gsi_start (lowered_body);
147 3010153 : lower_gimple_bind (&i, &data);
148 :
149 3010153 : i = gsi_last (lowered_body);
150 :
151 : /* If we had begin stmt markers from e.g. PCH, but this compilation
152 : doesn't want them, lower_stmt will have cleaned them up; we can
153 : now clear the flag that indicates we had them. */
154 3010153 : if (!MAY_HAVE_DEBUG_MARKER_STMTS && cfun->debug_nonbind_markers)
155 : {
156 : /* This counter needs not be exact, but before lowering it will
157 : most certainly be. */
158 0 : gcc_assert (cfun->debug_marker_count == 0);
159 0 : cfun->debug_nonbind_markers = false;
160 : }
161 :
162 : /* If the function falls off the end, we need a null return statement.
163 : If we've already got one in the return_statements vector, we don't
164 : need to do anything special. Otherwise build one by hand. */
165 3010153 : bool may_fallthru = gimple_seq_may_fallthru (lowered_body);
166 3010153 : if (may_fallthru
167 3063269 : && (data.return_statements.is_empty ()
168 53116 : || (gimple_return_retval (data.return_statements.last().stmt)
169 : != NULL)))
170 : {
171 1289286 : x = gimple_build_return (NULL);
172 1289286 : gimple_set_location (x, cfun->function_end_locus);
173 1289286 : gimple_set_block (x, DECL_INITIAL (current_function_decl));
174 1289286 : gsi_insert_after (&i, x, GSI_CONTINUE_LINKING);
175 1289286 : may_fallthru = false;
176 : }
177 :
178 : /* If we lowered any return statements, emit the representative
179 : at the end of the function. */
180 4721214 : while (!data.return_statements.is_empty ())
181 : {
182 1711061 : return_statements_t t = data.return_statements.pop ();
183 1711061 : x = gimple_build_label (t.label);
184 1711061 : gsi_insert_after (&i, x, GSI_CONTINUE_LINKING);
185 1711061 : gsi_insert_after (&i, t.stmt, GSI_CONTINUE_LINKING);
186 1711061 : if (may_fallthru)
187 : {
188 : /* Remove the line number from the representative return statement.
189 : It now fills in for the fallthru too. Failure to remove this
190 : will result in incorrect results for coverage analysis. */
191 38932 : gimple_set_location (t.stmt, UNKNOWN_LOCATION);
192 38932 : may_fallthru = false;
193 : }
194 : }
195 :
196 : /* Once the old body has been lowered, replace it with the new
197 : lowered sequence. */
198 3010153 : gimple_set_body (current_function_decl, lowered_body);
199 :
200 3010153 : if (assume_labels)
201 : {
202 8 : struct walk_stmt_info wi;
203 :
204 8 : memset (&wi, 0, sizeof (wi));
205 8 : walk_gimple_seq (lowered_body, NULL, diagnose_assume_labels, &wi);
206 8 : BITMAP_FREE (assume_labels);
207 : }
208 :
209 3010153 : gcc_assert (data.block == DECL_INITIAL (current_function_decl));
210 3010153 : BLOCK_SUBBLOCKS (data.block)
211 3010153 : = blocks_nreverse (BLOCK_SUBBLOCKS (data.block));
212 :
213 3010153 : clear_block_marks (data.block);
214 3010153 : data.return_statements.release ();
215 3010153 : return 0;
216 : }
217 :
218 : namespace {
219 :
220 : const pass_data pass_data_lower_cf =
221 : {
222 : GIMPLE_PASS, /* type */
223 : "lower", /* name */
224 : OPTGROUP_NONE, /* optinfo_flags */
225 : TV_NONE, /* tv_id */
226 : PROP_gimple_any, /* properties_required */
227 : PROP_gimple_lcf, /* properties_provided */
228 : 0, /* properties_destroyed */
229 : 0, /* todo_flags_start */
230 : 0, /* todo_flags_finish */
231 : };
232 :
233 : class pass_lower_cf : public gimple_opt_pass
234 : {
235 : public:
236 297196 : pass_lower_cf (gcc::context *ctxt)
237 594392 : : gimple_opt_pass (pass_data_lower_cf, ctxt)
238 : {}
239 :
240 : /* opt_pass methods: */
241 3010153 : unsigned int execute (function *) final override
242 : {
243 3010153 : return lower_function_body ();
244 : }
245 :
246 : }; // class pass_lower_cf
247 :
248 : } // anon namespace
249 :
250 : gimple_opt_pass *
251 297196 : make_pass_lower_cf (gcc::context *ctxt)
252 : {
253 297196 : return new pass_lower_cf (ctxt);
254 : }
255 :
256 : /* Lower sequence SEQ. Unlike gimplification the statements are not relowered
257 : when they are changed -- if this has to be done, the lowering routine must
258 : do it explicitly. DATA is passed through the recursion. */
259 :
260 : static void
261 12668320 : lower_sequence (gimple_seq *seq, struct lower_data *data)
262 : {
263 12668320 : gimple_stmt_iterator gsi;
264 :
265 115330619 : for (gsi = gsi_start (*seq); !gsi_end_p (gsi); )
266 90195819 : lower_stmt (&gsi, data);
267 12668320 : }
268 :
269 :
270 : /* Lower the OpenMP directive statement pointed by GSI. DATA is
271 : passed through the recursion. */
272 :
273 : static void
274 66055 : lower_omp_directive (gimple_stmt_iterator *gsi, struct lower_data *data)
275 : {
276 66055 : gimple *stmt;
277 :
278 66055 : stmt = gsi_stmt (*gsi);
279 :
280 66055 : lower_sequence (gimple_omp_body_ptr (stmt), data);
281 66055 : gsi_insert_seq_after (gsi, gimple_omp_body (stmt), GSI_CONTINUE_LINKING);
282 66055 : gimple_omp_set_body (stmt, NULL);
283 66055 : gsi_next (gsi);
284 66055 : }
285 :
286 : /* Create an artificial FUNCTION_DECL for assumption at LOC. */
287 :
288 : static tree
289 168 : create_assumption_fn (location_t loc)
290 : {
291 168 : tree name = clone_function_name_numbered (current_function_decl, "_assume");
292 : /* Temporarily, until we determine all the arguments. */
293 168 : tree type = build_varargs_function_type_list (boolean_type_node, NULL_TREE);
294 168 : tree decl = build_decl (loc, FUNCTION_DECL, name, type);
295 168 : TREE_STATIC (decl) = 1;
296 168 : TREE_USED (decl) = 1;
297 168 : DECL_ARTIFICIAL (decl) = 1;
298 168 : DECL_IGNORED_P (decl) = 1;
299 168 : DECL_NAMELESS (decl) = 1;
300 168 : TREE_PUBLIC (decl) = 0;
301 168 : DECL_UNINLINABLE (decl) = 1;
302 168 : DECL_EXTERNAL (decl) = 0;
303 168 : DECL_CONTEXT (decl) = NULL_TREE;
304 168 : DECL_INITIAL (decl) = make_node (BLOCK);
305 168 : DECL_ATTRIBUTES (decl) = DECL_ATTRIBUTES (current_function_decl);
306 168 : BLOCK_SUPERCONTEXT (DECL_INITIAL (decl)) = decl;
307 336 : DECL_FUNCTION_SPECIFIC_OPTIMIZATION (decl)
308 168 : = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (current_function_decl);
309 336 : DECL_FUNCTION_SPECIFIC_TARGET (decl)
310 168 : = DECL_FUNCTION_SPECIFIC_TARGET (current_function_decl);
311 168 : tree t = build_decl (DECL_SOURCE_LOCATION (decl),
312 : RESULT_DECL, NULL_TREE, boolean_type_node);
313 168 : DECL_ARTIFICIAL (t) = 1;
314 168 : DECL_IGNORED_P (t) = 1;
315 168 : DECL_CONTEXT (t) = decl;
316 168 : DECL_RESULT (decl) = t;
317 168 : push_struct_function (decl);
318 168 : cfun->function_end_locus = loc;
319 168 : init_tree_ssa (cfun);
320 168 : return decl;
321 : }
322 :
323 504 : struct lower_assumption_data
324 : {
325 : copy_body_data id;
326 : tree return_false_label;
327 : tree guard_copy;
328 : auto_vec<tree> decls;
329 : };
330 :
331 : /* Helper function for lower_assumptions. Find local vars and labels
332 : in the assumption sequence and remove debug stmts. */
333 :
334 : static tree
335 898 : find_assumption_locals_r (gimple_stmt_iterator *gsi_p, bool *,
336 : struct walk_stmt_info *wi)
337 : {
338 898 : lower_assumption_data *data = (lower_assumption_data *) wi->info;
339 898 : gimple *stmt = gsi_stmt (*gsi_p);
340 898 : tree lhs = gimple_get_lhs (stmt);
341 898 : if (lhs && TREE_CODE (lhs) == SSA_NAME)
342 : {
343 0 : gcc_assert (SSA_NAME_VAR (lhs) == NULL_TREE);
344 0 : data->id.decl_map->put (lhs, NULL_TREE);
345 0 : data->decls.safe_push (lhs);
346 : }
347 898 : switch (gimple_code (stmt))
348 : {
349 178 : case GIMPLE_BIND:
350 178 : for (tree var = gimple_bind_vars (as_a <gbind *> (stmt));
351 391 : var; var = DECL_CHAIN (var))
352 213 : if (VAR_P (var)
353 213 : && !DECL_EXTERNAL (var)
354 426 : && DECL_CONTEXT (var) == data->id.src_fn)
355 : {
356 213 : data->id.decl_map->put (var, var);
357 213 : data->decls.safe_push (var);
358 : }
359 178 : break;
360 130 : case GIMPLE_LABEL:
361 130 : {
362 130 : tree label = gimple_label_label (as_a <glabel *> (stmt));
363 130 : data->id.decl_map->put (label, label);
364 130 : if (DECL_NAME (label) && !DECL_ARTIFICIAL (label))
365 : {
366 16 : if (assume_labels == NULL)
367 8 : assume_labels = BITMAP_ALLOC (NULL);
368 16 : bitmap_set_bit (assume_labels, DECL_UID (label));
369 : }
370 130 : break;
371 : }
372 3 : case GIMPLE_RETURN:
373 : /* If something in assumption tries to return from parent function,
374 : if it would be reached in hypothetical evaluation, it would be UB,
375 : so transform such returns into return false; */
376 3 : {
377 3 : gimple *g = gimple_build_assign (data->guard_copy, boolean_false_node);
378 3 : gsi_insert_before (gsi_p, g, GSI_SAME_STMT);
379 3 : gimple_return_set_retval (as_a <greturn *> (stmt), data->guard_copy);
380 3 : break;
381 : }
382 0 : case GIMPLE_DEBUG:
383 : /* As assumptions won't be emitted, debug info stmts in them
384 : are useless. */
385 0 : gsi_remove (gsi_p, true);
386 0 : wi->removed_stmt = true;
387 0 : break;
388 : default:
389 : break;
390 : }
391 898 : return NULL_TREE;
392 : }
393 :
394 : /* Create a new PARM_DECL that is identical in all respect to DECL except that
395 : DECL can be either a VAR_DECL, a PARM_DECL or RESULT_DECL. The original
396 : DECL must come from ID->src_fn and the copy will be part of ID->dst_fn. */
397 :
398 : static tree
399 211 : assumption_copy_decl (tree decl, copy_body_data *id)
400 : {
401 211 : tree type = TREE_TYPE (decl);
402 :
403 211 : if (is_global_var (decl))
404 : return decl;
405 :
406 184 : gcc_assert (VAR_P (decl)
407 : || TREE_CODE (decl) == PARM_DECL
408 : || TREE_CODE (decl) == RESULT_DECL);
409 184 : if (TREE_THIS_VOLATILE (decl))
410 4 : type = build_pointer_type (type);
411 184 : tree copy = build_decl (DECL_SOURCE_LOCATION (decl),
412 184 : PARM_DECL, DECL_NAME (decl), type);
413 184 : if (DECL_PT_UID_SET_P (decl))
414 0 : SET_DECL_PT_UID (copy, DECL_PT_UID (decl));
415 184 : TREE_THIS_VOLATILE (copy) = 0;
416 184 : if (TREE_THIS_VOLATILE (decl))
417 4 : TREE_READONLY (copy) = 1;
418 : else
419 : {
420 180 : TREE_ADDRESSABLE (copy) = TREE_ADDRESSABLE (decl);
421 180 : TREE_READONLY (copy) = TREE_READONLY (decl);
422 180 : DECL_NOT_GIMPLE_REG_P (copy) = DECL_NOT_GIMPLE_REG_P (decl);
423 180 : DECL_BY_REFERENCE (copy) = DECL_BY_REFERENCE (decl);
424 : }
425 184 : DECL_ARG_TYPE (copy) = type;
426 184 : ((lower_assumption_data *) id)->decls.safe_push (decl);
427 184 : return copy_decl_for_dup_finish (id, decl, copy);
428 : }
429 :
430 : /* Transform gotos out of the assumption into return false. */
431 :
432 : static tree
433 901 : adjust_assumption_stmt_r (gimple_stmt_iterator *gsi_p, bool *,
434 : struct walk_stmt_info *wi)
435 : {
436 901 : lower_assumption_data *data = (lower_assumption_data *) wi->info;
437 901 : gimple *stmt = gsi_stmt (*gsi_p);
438 901 : tree lab = NULL_TREE;
439 901 : unsigned int idx = 0;
440 901 : if (gimple_code (stmt) == GIMPLE_GOTO)
441 24 : lab = gimple_goto_dest (stmt);
442 877 : else if (gimple_code (stmt) == GIMPLE_COND)
443 : {
444 57 : repeat:
445 114 : if (idx == 0)
446 57 : lab = gimple_cond_true_label (as_a <gcond *> (stmt));
447 : else
448 57 : lab = gimple_cond_false_label (as_a <gcond *> (stmt));
449 : }
450 820 : else if (gimple_code (stmt) == GIMPLE_LABEL)
451 : {
452 130 : tree label = gimple_label_label (as_a <glabel *> (stmt));
453 130 : DECL_CONTEXT (label) = current_function_decl;
454 : }
455 958 : if (lab)
456 : {
457 138 : if (!data->id.decl_map->get (lab))
458 : {
459 3 : if (!data->return_false_label)
460 3 : data->return_false_label
461 3 : = create_artificial_label (UNKNOWN_LOCATION);
462 3 : if (gimple_code (stmt) == GIMPLE_GOTO)
463 3 : gimple_goto_set_dest (as_a <ggoto *> (stmt),
464 : data->return_false_label);
465 0 : else if (idx == 0)
466 0 : gimple_cond_set_true_label (as_a <gcond *> (stmt),
467 : data->return_false_label);
468 : else
469 0 : gimple_cond_set_false_label (as_a <gcond *> (stmt),
470 : data->return_false_label);
471 : }
472 138 : if (gimple_code (stmt) == GIMPLE_COND && idx == 0)
473 : {
474 57 : idx = 1;
475 57 : goto repeat;
476 : }
477 : }
478 901 : return NULL_TREE;
479 : }
480 :
481 : /* Adjust trees in the assumption body. Called through walk_tree. */
482 :
483 : static tree
484 1894 : adjust_assumption_stmt_op (tree *tp, int *, void *datap)
485 : {
486 1894 : struct walk_stmt_info *wi = (struct walk_stmt_info *) datap;
487 1894 : lower_assumption_data *data = (lower_assumption_data *) wi->info;
488 1894 : tree t = *tp;
489 1894 : tree *newt;
490 1894 : switch (TREE_CODE (t))
491 : {
492 0 : case SSA_NAME:
493 0 : newt = data->id.decl_map->get (t);
494 : /* There shouldn't be SSA_NAMEs other than ones defined in the
495 : assumption's body. */
496 0 : gcc_assert (newt);
497 0 : *tp = *newt;
498 0 : break;
499 268 : case LABEL_DECL:
500 268 : newt = data->id.decl_map->get (t);
501 268 : if (newt)
502 265 : *tp = *newt;
503 : break;
504 970 : case VAR_DECL:
505 970 : case PARM_DECL:
506 970 : case RESULT_DECL:
507 970 : *tp = remap_decl (t, &data->id);
508 970 : if (TREE_THIS_VOLATILE (t) && *tp != t)
509 : {
510 4 : *tp = build_simple_mem_ref (*tp);
511 4 : TREE_THIS_NOTRAP (*tp) = 1;
512 : }
513 : break;
514 : default:
515 : break;
516 : }
517 1894 : return NULL_TREE;
518 : }
519 :
520 : /* Lower assumption.
521 : The gimplifier transformed:
522 : .ASSUME (cond);
523 : into:
524 : [[assume (guard)]]
525 : {
526 : guard = cond;
527 : }
528 : which we should transform into:
529 : .ASSUME (&artificial_fn, args...);
530 : where artificial_fn will look like:
531 : bool artificial_fn (args...)
532 : {
533 : guard = cond;
534 : return guard;
535 : }
536 : with any debug stmts in the block removed and jumps out of
537 : the block or return stmts replaced with return false; */
538 :
539 : static void
540 168 : lower_assumption (gimple_stmt_iterator *gsi, struct lower_data *data)
541 : {
542 168 : gimple *stmt = gsi_stmt (*gsi);
543 168 : tree guard = gimple_assume_guard (stmt);
544 168 : gimple *bind = gimple_assume_body (stmt);
545 168 : location_t loc = gimple_location (stmt);
546 168 : gcc_assert (gimple_code (bind) == GIMPLE_BIND);
547 :
548 168 : lower_assumption_data lad;
549 168 : hash_map<tree, tree> decl_map;
550 168 : memset (&lad.id, 0, sizeof (lad.id));
551 168 : lad.return_false_label = NULL_TREE;
552 168 : lad.id.src_fn = current_function_decl;
553 168 : lad.id.dst_fn = create_assumption_fn (loc);
554 168 : lad.id.src_cfun = DECL_STRUCT_FUNCTION (lad.id.src_fn);
555 168 : lad.id.decl_map = &decl_map;
556 168 : lad.id.copy_decl = assumption_copy_decl;
557 168 : lad.id.transform_call_graph_edges = CB_CGE_DUPLICATE;
558 168 : lad.id.transform_parameter = true;
559 168 : lad.id.do_not_unshare = true;
560 168 : lad.id.do_not_fold = true;
561 168 : cfun->curr_properties = lad.id.src_cfun->curr_properties;
562 168 : lad.guard_copy = create_tmp_var (boolean_type_node);
563 168 : decl_map.put (lad.guard_copy, lad.guard_copy);
564 168 : decl_map.put (guard, lad.guard_copy);
565 168 : cfun->assume_function = 1;
566 :
567 : /* Find variables, labels and SSA_NAMEs local to the assume GIMPLE_BIND. */
568 168 : gimple_stmt_iterator gsi2 = gsi_start (*gimple_assume_body_ptr (stmt));
569 168 : struct walk_stmt_info wi;
570 168 : memset (&wi, 0, sizeof (wi));
571 168 : wi.info = (void *) &lad;
572 168 : walk_gimple_stmt (&gsi2, find_assumption_locals_r, NULL, &wi);
573 168 : unsigned int sz = lad.decls.length ();
574 381 : for (unsigned i = 0; i < sz; ++i)
575 : {
576 213 : tree v = lad.decls[i];
577 213 : tree newv;
578 : /* SSA_NAMEs defined in the assume condition should be replaced
579 : by new SSA_NAMEs in the artificial function. */
580 213 : if (TREE_CODE (v) == SSA_NAME)
581 : {
582 0 : newv = make_ssa_name (remap_type (TREE_TYPE (v), &lad.id));
583 0 : decl_map.put (v, newv);
584 : }
585 : /* Local vars should have context and type adjusted to the
586 : new artificial function. */
587 213 : else if (VAR_P (v))
588 : {
589 213 : if (is_global_var (v) && !DECL_ASSEMBLER_NAME_SET_P (v))
590 3 : DECL_ASSEMBLER_NAME (v);
591 213 : TREE_TYPE (v) = remap_type (TREE_TYPE (v), &lad.id);
592 213 : DECL_CONTEXT (v) = current_function_decl;
593 : }
594 : }
595 : /* References to other automatic vars should be replaced by
596 : PARM_DECLs to the artificial function. */
597 168 : memset (&wi, 0, sizeof (wi));
598 168 : wi.info = (void *) &lad;
599 168 : walk_gimple_stmt (&gsi2, adjust_assumption_stmt_r,
600 : adjust_assumption_stmt_op, &wi);
601 :
602 : /* At the start prepend guard = false; */
603 168 : gimple_seq body = NULL;
604 168 : gimple *g = gimple_build_assign (lad.guard_copy, boolean_false_node);
605 168 : gimple_seq_add_stmt (&body, g);
606 168 : gimple_seq_add_stmt (&body, bind);
607 : /* At the end add return guard; */
608 168 : greturn *gr = gimple_build_return (lad.guard_copy);
609 168 : gimple_seq_add_stmt (&body, gr);
610 : /* If there were any jumps to labels outside of the condition,
611 : replace them with a jump to
612 : return_false_label:
613 : guard = false;
614 : return guard; */
615 168 : if (lad.return_false_label)
616 : {
617 3 : g = gimple_build_label (lad.return_false_label);
618 3 : gimple_seq_add_stmt (&body, g);
619 3 : g = gimple_build_assign (lad.guard_copy, boolean_false_node);
620 3 : gimple_seq_add_stmt (&body, g);
621 3 : gr = gimple_build_return (lad.guard_copy);
622 3 : gimple_seq_add_stmt (&body, gr);
623 : }
624 168 : bind = gimple_build_bind (NULL_TREE, body, NULL_TREE);
625 168 : body = NULL;
626 168 : gimple_seq_add_stmt (&body, bind);
627 168 : gimple_set_body (current_function_decl, body);
628 168 : pop_cfun ();
629 :
630 168 : tree parms = NULL_TREE;
631 168 : tree parmt = void_list_node;
632 168 : auto_vec<tree, 8> vargs;
633 324 : vargs.safe_grow (1 + (lad.decls.length () - sz), true);
634 : /* First argument to IFN_ASSUME will be address of the
635 : artificial function. */
636 168 : vargs[0] = build_fold_addr_expr (lad.id.dst_fn);
637 508 : for (unsigned i = lad.decls.length (); i > sz; --i)
638 : {
639 184 : tree *v = decl_map.get (lad.decls[i - 1]);
640 184 : gcc_assert (v && TREE_CODE (*v) == PARM_DECL);
641 184 : DECL_CHAIN (*v) = parms;
642 184 : parms = *v;
643 184 : parmt = tree_cons (NULL_TREE, TREE_TYPE (*v), parmt);
644 : /* Remaining arguments will be the variables/parameters
645 : mentioned in the condition. */
646 184 : vargs[i - sz] = lad.decls[i - 1];
647 184 : if (TREE_THIS_VOLATILE (lad.decls[i - 1]))
648 : {
649 4 : TREE_ADDRESSABLE (lad.decls[i - 1]) = 1;
650 4 : vargs[i - sz] = build_fold_addr_expr (lad.decls[i - 1]);
651 : }
652 : /* If they have gimple types, we might need to regimplify
653 : them to make the IFN_ASSUME call valid. */
654 184 : if (is_gimple_reg_type (TREE_TYPE (vargs[i - sz]))
655 184 : && !is_gimple_val (vargs[i - sz]))
656 : {
657 6 : tree t = make_ssa_name (TREE_TYPE (vargs[i - sz]));
658 6 : g = gimple_build_assign (t, vargs[i - sz]);
659 6 : gsi_insert_before (gsi, g, GSI_SAME_STMT);
660 6 : vargs[i - sz] = t;
661 : }
662 : }
663 168 : DECL_ARGUMENTS (lad.id.dst_fn) = parms;
664 168 : TREE_TYPE (lad.id.dst_fn) = build_function_type (boolean_type_node, parmt);
665 : /* The body function no longer has var. args, unset stdarg. */
666 168 : DECL_STRUCT_FUNCTION (lad.id.dst_fn)->stdarg = 0;
667 :
668 168 : cgraph_node *dst_node = cgraph_node::add_new_function (lad.id.dst_fn, false);
669 168 : dst_node->assume_function = 1;
670 :
671 381 : for (unsigned i = 0; i < sz; ++i)
672 : {
673 213 : tree v = lad.decls[i];
674 213 : if (TREE_CODE (v) == SSA_NAME)
675 0 : release_ssa_name (v);
676 : }
677 :
678 168 : data->cannot_fallthru = false;
679 : /* Replace GIMPLE_ASSUME statement with IFN_ASSUME call. */
680 168 : gcall *call = gimple_build_call_internal_vec (IFN_ASSUME, vargs);
681 168 : gimple_set_location (call, loc);
682 168 : gsi_replace (gsi, call, true);
683 168 : }
684 :
685 : /* Lower statement GSI. DATA is passed through the recursion. We try to
686 : track the fallthruness of statements and get rid of unreachable return
687 : statements in order to prevent the EH lowering pass from adding useless
688 : edges that can cause bogus warnings to be issued later; this guess need
689 : not be 100% accurate, simply be conservative and reset cannot_fallthru
690 : to false if we don't know. */
691 :
692 : static void
693 90195819 : lower_stmt (gimple_stmt_iterator *gsi, struct lower_data *data)
694 : {
695 90195819 : gimple *stmt = gsi_stmt (*gsi);
696 :
697 90195819 : gimple_set_block (stmt, data->block);
698 :
699 90195819 : switch (gimple_code (stmt))
700 : {
701 4484797 : case GIMPLE_BIND:
702 4484797 : lower_gimple_bind (gsi, data);
703 : /* Propagate fallthruness. */
704 4484797 : return;
705 :
706 8914564 : case GIMPLE_COND:
707 8914564 : case GIMPLE_GOTO:
708 8914564 : case GIMPLE_SWITCH:
709 8914564 : data->cannot_fallthru = true;
710 8914564 : gsi_next (gsi);
711 8914564 : return;
712 :
713 2298754 : case GIMPLE_RETURN:
714 2298754 : if (data->cannot_fallthru)
715 : {
716 513 : gsi_remove (gsi, false);
717 : /* Propagate fallthruness. */
718 : }
719 : else
720 : {
721 2298241 : lower_gimple_return (gsi, data);
722 2298241 : data->cannot_fallthru = true;
723 : }
724 : return;
725 :
726 2551518 : case GIMPLE_TRY:
727 2551518 : if (gimple_try_kind (stmt) == GIMPLE_TRY_CATCH)
728 811154 : lower_try_catch (gsi, data);
729 : else
730 : {
731 : /* It must be a GIMPLE_TRY_FINALLY. */
732 1740364 : bool cannot_fallthru;
733 1740364 : lower_sequence (gimple_try_eval_ptr (stmt), data);
734 1740364 : cannot_fallthru = data->cannot_fallthru;
735 :
736 : /* The finally clause is always executed after the try clause,
737 : so if it does not fall through, then the try-finally will not
738 : fall through. Otherwise, if the try clause does not fall
739 : through, then when the finally clause falls through it will
740 : resume execution wherever the try clause was going. So the
741 : whole try-finally will only fall through if both the try
742 : clause and the finally clause fall through. */
743 1740364 : data->cannot_fallthru = false;
744 1740364 : lower_sequence (gimple_try_cleanup_ptr (stmt), data);
745 1740364 : data->cannot_fallthru |= cannot_fallthru;
746 1740364 : gsi_next (gsi);
747 : }
748 : return;
749 :
750 278 : case GIMPLE_EH_ELSE:
751 278 : {
752 278 : geh_else *eh_else_stmt = as_a <geh_else *> (stmt);
753 278 : lower_sequence (gimple_eh_else_n_body_ptr (eh_else_stmt), data);
754 278 : lower_sequence (gimple_eh_else_e_body_ptr (eh_else_stmt), data);
755 : }
756 278 : break;
757 :
758 2486301 : case GIMPLE_DEBUG:
759 2486301 : gcc_checking_assert (cfun->debug_nonbind_markers);
760 : /* We can't possibly have debug bind stmts before lowering, we
761 : first emit them when entering SSA. */
762 2486301 : gcc_checking_assert (gimple_debug_nonbind_marker_p (stmt));
763 : /* Propagate fallthruness. */
764 : /* If the function (e.g. from PCH) had debug stmts, but they're
765 : disabled for this compilation, remove them. */
766 2486301 : if (!MAY_HAVE_DEBUG_MARKER_STMTS)
767 0 : gsi_remove (gsi, true);
768 : else
769 2486301 : gsi_next (gsi);
770 : return;
771 :
772 0 : case GIMPLE_OMP_STRUCTURED_BLOCK:
773 : /* These are supposed to be removed already in OMP lowering. */
774 0 : gcc_unreachable ();
775 :
776 : case GIMPLE_NOP:
777 : case GIMPLE_ASM:
778 : case GIMPLE_ASSIGN:
779 : case GIMPLE_PREDICT:
780 : case GIMPLE_LABEL:
781 : case GIMPLE_EH_MUST_NOT_THROW:
782 : case GIMPLE_OMP_FOR:
783 : case GIMPLE_OMP_SCOPE:
784 : case GIMPLE_OMP_DISPATCH:
785 : case GIMPLE_OMP_INTEROP:
786 : case GIMPLE_OMP_SECTIONS:
787 : case GIMPLE_OMP_SECTIONS_SWITCH:
788 : case GIMPLE_OMP_SECTION:
789 : case GIMPLE_OMP_SINGLE:
790 : case GIMPLE_OMP_MASTER:
791 : case GIMPLE_OMP_MASKED:
792 : case GIMPLE_OMP_TASKGROUP:
793 : case GIMPLE_OMP_ORDERED:
794 : case GIMPLE_OMP_SCAN:
795 : case GIMPLE_OMP_CRITICAL:
796 : case GIMPLE_OMP_RETURN:
797 : case GIMPLE_OMP_ATOMIC_LOAD:
798 : case GIMPLE_OMP_ATOMIC_STORE:
799 : case GIMPLE_OMP_CONTINUE:
800 : break;
801 :
802 11420604 : case GIMPLE_CALL:
803 11420604 : {
804 11420604 : tree decl = gimple_call_fndecl (stmt);
805 11420604 : unsigned i;
806 :
807 43828050 : for (i = 0; i < gimple_call_num_args (stmt); i++)
808 : {
809 20986842 : tree arg = gimple_call_arg (stmt, i);
810 20986842 : if (EXPR_P (arg))
811 5493098 : TREE_SET_BLOCK (arg, data->block);
812 : }
813 :
814 11420604 : if (decl
815 11420604 : && fndecl_built_in_p (decl, BUILT_IN_NORMAL))
816 : {
817 2300214 : if (DECL_FUNCTION_CODE (decl) == BUILT_IN_SETJMP)
818 : {
819 797 : lower_builtin_setjmp (gsi);
820 797 : data->cannot_fallthru = false;
821 797 : return;
822 : }
823 2299417 : else if (DECL_FUNCTION_CODE (decl) == BUILT_IN_POSIX_MEMALIGN
824 119 : && flag_tree_bit_ccp
825 2299523 : && gimple_builtin_call_types_compatible_p (stmt, decl))
826 : {
827 34 : lower_builtin_posix_memalign (gsi);
828 34 : return;
829 : }
830 2299383 : else if (DECL_FUNCTION_CODE (decl) == BUILT_IN_ASSUME_ALIGNED
831 2299383 : && !optimize)
832 : {
833 74 : lower_builtin_assume_aligned (gsi);
834 74 : data->cannot_fallthru = false;
835 74 : gsi_next (gsi);
836 74 : return;
837 : }
838 : }
839 :
840 11419699 : if (decl && (flags_from_decl_or_type (decl) & ECF_NORETURN))
841 : {
842 1692521 : data->cannot_fallthru = true;
843 1692521 : gsi_next (gsi);
844 1692521 : return;
845 : }
846 :
847 9727178 : if (gimple_call_internal_p (stmt, IFN_ASAN_MARK))
848 : {
849 5549 : tree base = gimple_call_arg (stmt, 1);
850 5549 : gcc_checking_assert (TREE_CODE (base) == ADDR_EXPR);
851 5549 : tree decl = TREE_OPERAND (base, 0);
852 5549 : if (VAR_P (decl) && TREE_STATIC (decl))
853 : {
854 : /* Don't poison a variable with static storage; it might have
855 : gotten marked before gimplify_init_constructor promoted it
856 : to static. */
857 62 : gsi_remove (gsi, true);
858 62 : return;
859 : }
860 : }
861 :
862 : /* We delay folding of built calls from gimplification to
863 : here so the IL is in consistent state for the diagnostic
864 : machineries job. */
865 9727116 : if (gimple_call_builtin_p (stmt))
866 1487066 : fold_stmt (gsi);
867 : }
868 : break;
869 :
870 66055 : case GIMPLE_OMP_PARALLEL:
871 66055 : case GIMPLE_OMP_TASK:
872 66055 : case GIMPLE_OMP_TARGET:
873 66055 : case GIMPLE_OMP_TEAMS:
874 66055 : data->cannot_fallthru = false;
875 66055 : lower_omp_directive (gsi, data);
876 66055 : data->cannot_fallthru = false;
877 66055 : return;
878 :
879 168 : case GIMPLE_ASSUME:
880 168 : lower_assumption (gsi, data);
881 168 : return;
882 :
883 478 : case GIMPLE_TRANSACTION:
884 478 : lower_sequence (gimple_transaction_body_ptr (
885 : as_a <gtransaction *> (stmt)),
886 : data);
887 478 : break;
888 :
889 0 : default:
890 0 : gcc_unreachable ();
891 : }
892 :
893 67700174 : data->cannot_fallthru = false;
894 67700174 : gsi_next (gsi);
895 : }
896 :
897 : /* Lower a bind_expr TSI. DATA is passed through the recursion. */
898 :
899 : static void
900 7494950 : lower_gimple_bind (gimple_stmt_iterator *gsi, struct lower_data *data)
901 : {
902 7494950 : tree old_block = data->block;
903 7494950 : gbind *stmt = as_a <gbind *> (gsi_stmt (*gsi));
904 7494950 : tree new_block = gimple_bind_block (stmt);
905 :
906 7494950 : if (new_block)
907 : {
908 6009701 : if (new_block == old_block)
909 : {
910 : /* The outermost block of the original function may not be the
911 : outermost statement chain of the gimplified function. So we
912 : may see the outermost block just inside the function. */
913 1610826 : gcc_assert (new_block == DECL_INITIAL (current_function_decl));
914 : new_block = NULL;
915 : }
916 : else
917 : {
918 : /* We do not expect to handle duplicate blocks. */
919 4398875 : gcc_assert (!TREE_ASM_WRITTEN (new_block));
920 4398875 : TREE_ASM_WRITTEN (new_block) = 1;
921 :
922 : /* Block tree may get clobbered by inlining. Normally this would
923 : be fixed in rest_of_decl_compilation using block notes, but
924 : since we are not going to emit them, it is up to us. */
925 4398875 : BLOCK_CHAIN (new_block) = BLOCK_SUBBLOCKS (old_block);
926 4398875 : BLOCK_SUBBLOCKS (old_block) = new_block;
927 4398875 : BLOCK_SUBBLOCKS (new_block) = NULL_TREE;
928 4398875 : BLOCK_SUPERCONTEXT (new_block) = old_block;
929 :
930 4398875 : data->block = new_block;
931 : }
932 : }
933 :
934 7494950 : record_vars (gimple_bind_vars (stmt));
935 :
936 : /* Scrap DECL_CHAIN up to BLOCK_VARS to ease GC after we no longer
937 : need gimple_bind_vars. */
938 7494950 : tree next;
939 : /* BLOCK_VARS and gimple_bind_vars share a common sub-chain. Find
940 : it by marking all BLOCK_VARS. */
941 7494950 : if (gimple_bind_block (stmt))
942 13318392 : for (tree t = BLOCK_VARS (gimple_bind_block (stmt)); t; t = DECL_CHAIN (t))
943 7308691 : TREE_VISITED (t) = 1;
944 7494950 : for (tree var = gimple_bind_vars (stmt);
945 11671279 : var && ! TREE_VISITED (var); var = next)
946 : {
947 4176329 : next = DECL_CHAIN (var);
948 4176329 : DECL_CHAIN (var) = NULL_TREE;
949 : }
950 : /* Unmark BLOCK_VARS. */
951 7494950 : if (gimple_bind_block (stmt))
952 13318392 : for (tree t = BLOCK_VARS (gimple_bind_block (stmt)); t; t = DECL_CHAIN (t))
953 7308691 : TREE_VISITED (t) = 0;
954 :
955 7494950 : lower_sequence (gimple_bind_body_ptr (stmt), data);
956 :
957 7494950 : if (new_block)
958 : {
959 4398875 : gcc_assert (data->block == new_block);
960 :
961 4398875 : BLOCK_SUBBLOCKS (new_block)
962 4398875 : = blocks_nreverse (BLOCK_SUBBLOCKS (new_block));
963 4398875 : data->block = old_block;
964 : }
965 :
966 : /* The GIMPLE_BIND no longer carries any useful information -- kill it. */
967 7494950 : gsi_insert_seq_before (gsi, gimple_bind_body (stmt), GSI_SAME_STMT);
968 7494950 : gsi_remove (gsi, false);
969 7494950 : }
970 :
971 : /* Same as above, but for a GIMPLE_TRY_CATCH. */
972 :
973 : static void
974 811154 : lower_try_catch (gimple_stmt_iterator *gsi, struct lower_data *data)
975 : {
976 811154 : bool cannot_fallthru;
977 811154 : gimple *stmt = gsi_stmt (*gsi);
978 811154 : gimple_stmt_iterator i;
979 :
980 : /* We don't handle GIMPLE_TRY_FINALLY. */
981 811154 : gcc_assert (gimple_try_kind (stmt) == GIMPLE_TRY_CATCH);
982 :
983 811154 : lower_sequence (gimple_try_eval_ptr (stmt), data);
984 811154 : cannot_fallthru = data->cannot_fallthru;
985 :
986 811154 : i = gsi_start (*gimple_try_cleanup_ptr (stmt));
987 811154 : switch (gimple_code (gsi_stmt (i)))
988 : {
989 : case GIMPLE_CATCH:
990 : /* We expect to see a sequence of GIMPLE_CATCH stmts, each with a
991 : catch expression and a body. The whole try/catch may fall
992 : through iff any of the catch bodies falls through. */
993 82815 : for (; !gsi_end_p (i); gsi_next (&i))
994 : {
995 43030 : data->cannot_fallthru = false;
996 43030 : lower_sequence (gimple_catch_handler_ptr (
997 : as_a <gcatch *> (gsi_stmt (i))),
998 : data);
999 43030 : if (!data->cannot_fallthru)
1000 18742 : cannot_fallthru = false;
1001 : }
1002 : break;
1003 :
1004 4491 : case GIMPLE_EH_FILTER:
1005 : /* The exception filter expression only matters if there is an
1006 : exception. If the exception does not match EH_FILTER_TYPES,
1007 : we will execute EH_FILTER_FAILURE, and we will fall through
1008 : if that falls through. If the exception does match
1009 : EH_FILTER_TYPES, the stack unwinder will continue up the
1010 : stack, so we will not fall through. We don't know whether we
1011 : will throw an exception which matches EH_FILTER_TYPES or not,
1012 : so we just ignore EH_FILTER_TYPES and assume that we might
1013 : throw an exception which doesn't match. */
1014 4491 : data->cannot_fallthru = false;
1015 4491 : lower_sequence (gimple_eh_filter_failure_ptr (gsi_stmt (i)), data);
1016 4491 : if (!data->cannot_fallthru)
1017 811154 : cannot_fallthru = false;
1018 : break;
1019 :
1020 0 : case GIMPLE_DEBUG:
1021 0 : gcc_checking_assert (gimple_debug_begin_stmt_p (stmt));
1022 : break;
1023 :
1024 766878 : default:
1025 : /* This case represents statements to be executed when an
1026 : exception occurs. Those statements are implicitly followed
1027 : by a GIMPLE_RESX to resume execution after the exception. So
1028 : in this case the try/catch never falls through. */
1029 766878 : data->cannot_fallthru = false;
1030 766878 : lower_sequence (gimple_try_cleanup_ptr (stmt), data);
1031 766878 : break;
1032 : }
1033 :
1034 811154 : data->cannot_fallthru = cannot_fallthru;
1035 811154 : gsi_next (gsi);
1036 811154 : }
1037 :
1038 :
1039 : /* Try to determine whether a TRY_CATCH expression can fall through.
1040 : This is a subroutine of gimple_stmt_may_fallthru. */
1041 :
1042 : static bool
1043 392893 : gimple_try_catch_may_fallthru (gtry *stmt)
1044 : {
1045 392893 : gimple_stmt_iterator i;
1046 :
1047 : /* We don't handle GIMPLE_TRY_FINALLY. */
1048 392893 : gcc_assert (gimple_try_kind (stmt) == GIMPLE_TRY_CATCH);
1049 :
1050 : /* If the TRY block can fall through, the whole TRY_CATCH can
1051 : fall through. */
1052 392893 : if (gimple_seq_may_fallthru (gimple_try_eval (stmt)))
1053 : return true;
1054 :
1055 6130 : i = gsi_start (*gimple_try_cleanup_ptr (stmt));
1056 6130 : switch (gimple_code (gsi_stmt (i)))
1057 : {
1058 : case GIMPLE_CATCH:
1059 : /* We expect to see a sequence of GIMPLE_CATCH stmts, each with a
1060 : catch expression and a body. The whole try/catch may fall
1061 : through iff any of the catch bodies falls through. */
1062 8157 : for (; !gsi_end_p (i); gsi_next (&i))
1063 : {
1064 4532 : if (gimple_seq_may_fallthru (gimple_catch_handler (
1065 4532 : as_a <gcatch *> (gsi_stmt (i)))))
1066 : return true;
1067 : }
1068 : return false;
1069 :
1070 82 : case GIMPLE_EH_FILTER:
1071 : /* The exception filter expression only matters if there is an
1072 : exception. If the exception does not match EH_FILTER_TYPES,
1073 : we will execute EH_FILTER_FAILURE, and we will fall through
1074 : if that falls through. If the exception does match
1075 : EH_FILTER_TYPES, the stack unwinder will continue up the
1076 : stack, so we will not fall through. We don't know whether we
1077 : will throw an exception which matches EH_FILTER_TYPES or not,
1078 : so we just ignore EH_FILTER_TYPES and assume that we might
1079 : throw an exception which doesn't match. */
1080 82 : return gimple_seq_may_fallthru (gimple_eh_filter_failure (gsi_stmt (i)));
1081 :
1082 : default:
1083 : /* This case represents statements to be executed when an
1084 : exception occurs. Those statements are implicitly followed
1085 : by a GIMPLE_RESX to resume execution after the exception. So
1086 : in this case the try/catch never falls through. */
1087 : return false;
1088 : }
1089 : }
1090 :
1091 :
1092 : /* Try to determine if we can continue executing the statement
1093 : immediately following STMT. This guess need not be 100% accurate;
1094 : simply be conservative and return true if we don't know. This is
1095 : used only to avoid stupidly generating extra code. If we're wrong,
1096 : we'll just delete the extra code later. */
1097 :
1098 : bool
1099 18108269 : gimple_stmt_may_fallthru (gimple *stmt)
1100 : {
1101 18108269 : if (!stmt)
1102 : return true;
1103 :
1104 17884194 : switch (gimple_code (stmt))
1105 : {
1106 : case GIMPLE_GOTO:
1107 : case GIMPLE_RETURN:
1108 : case GIMPLE_RESX:
1109 : /* Easy cases. If the last statement of the seq implies
1110 : control transfer, then we can't fall through. */
1111 : return false;
1112 :
1113 : case GIMPLE_SWITCH:
1114 : /* Switch has already been lowered and represents a branch
1115 : to a selected label and hence can't fall through. */
1116 : return false;
1117 :
1118 : case GIMPLE_COND:
1119 : /* GIMPLE_COND's are already lowered into a two-way branch. They
1120 : can't fall through. */
1121 : return false;
1122 :
1123 374230 : case GIMPLE_BIND:
1124 374230 : return gimple_seq_may_fallthru (
1125 374230 : gimple_bind_body (as_a <gbind *> (stmt)));
1126 :
1127 1346491 : case GIMPLE_TRY:
1128 1346491 : if (gimple_try_kind (stmt) == GIMPLE_TRY_CATCH)
1129 392893 : return gimple_try_catch_may_fallthru (as_a <gtry *> (stmt));
1130 :
1131 : /* It must be a GIMPLE_TRY_FINALLY. */
1132 :
1133 : /* The finally clause is always executed after the try clause,
1134 : so if it does not fall through, then the try-finally will not
1135 : fall through. Otherwise, if the try clause does not fall
1136 : through, then when the finally clause falls through it will
1137 : resume execution wherever the try clause was going. So the
1138 : whole try-finally will only fall through if both the try
1139 : clause and the finally clause fall through. */
1140 953598 : return (gimple_seq_may_fallthru (gimple_try_eval (stmt))
1141 1511814 : && gimple_seq_may_fallthru (gimple_try_cleanup (stmt)));
1142 :
1143 586 : case GIMPLE_EH_ELSE:
1144 586 : {
1145 586 : geh_else *eh_else_stmt = as_a <geh_else *> (stmt);
1146 586 : return (gimple_seq_may_fallthru (gimple_eh_else_n_body (eh_else_stmt))
1147 586 : || gimple_seq_may_fallthru (gimple_eh_else_e_body (
1148 : eh_else_stmt)));
1149 : }
1150 :
1151 4579949 : case GIMPLE_CALL:
1152 : /* Functions that do not return do not fall through. */
1153 4579949 : return !gimple_call_noreturn_p (stmt);
1154 :
1155 : default:
1156 : return true;
1157 : }
1158 : }
1159 :
1160 :
1161 : /* Same as gimple_stmt_may_fallthru, but for the gimple sequence SEQ. */
1162 :
1163 : bool
1164 15447825 : gimple_seq_may_fallthru (gimple_seq seq)
1165 : {
1166 15447825 : return gimple_stmt_may_fallthru (gimple_seq_last_nondebug_stmt (seq));
1167 : }
1168 :
1169 :
1170 : /* Lower a GIMPLE_RETURN GSI. DATA is passed through the recursion. */
1171 :
1172 : static void
1173 2298241 : lower_gimple_return (gimple_stmt_iterator *gsi, struct lower_data *data)
1174 : {
1175 2298241 : greturn *stmt = as_a <greturn *> (gsi_stmt (*gsi));
1176 2298241 : gimple *t;
1177 2298241 : int i;
1178 2298241 : return_statements_t tmp_rs;
1179 :
1180 : /* Match this up with an existing return statement that's been created. */
1181 4596502 : for (i = data->return_statements.length () - 1;
1182 2298261 : i >= 0; i--)
1183 : {
1184 587200 : tmp_rs = data->return_statements[i];
1185 :
1186 587200 : if (gimple_return_retval (stmt) == gimple_return_retval (tmp_rs.stmt))
1187 : {
1188 : /* Remove the line number from the representative return statement.
1189 : It now fills in for many such returns. Failure to remove this
1190 : will result in incorrect results for coverage analysis. */
1191 587180 : gimple_set_location (tmp_rs.stmt, UNKNOWN_LOCATION);
1192 :
1193 587180 : goto found;
1194 : }
1195 : }
1196 :
1197 : /* Not found. Create a new label and record the return statement. */
1198 1711061 : tmp_rs.label = create_artificial_label (cfun->function_end_locus);
1199 1711061 : tmp_rs.stmt = stmt;
1200 1711061 : data->return_statements.safe_push (tmp_rs);
1201 :
1202 : /* Generate a goto statement and remove the return statement. */
1203 2298241 : found:
1204 : /* When not optimizing, make sure user returns are preserved. */
1205 2298241 : if (!optimize && gimple_has_location (stmt))
1206 272713 : DECL_ARTIFICIAL (tmp_rs.label) = 0;
1207 2298241 : t = gimple_build_goto (tmp_rs.label);
1208 : /* location includes block. */
1209 2298241 : gimple_set_location (t, gimple_location (stmt));
1210 2298241 : gsi_insert_before (gsi, t, GSI_SAME_STMT);
1211 2298241 : gsi_remove (gsi, false);
1212 2298241 : }
1213 :
1214 : /* Lower a __builtin_setjmp GSI.
1215 :
1216 : __builtin_setjmp is passed a pointer to an array of five words (not
1217 : all will be used on all machines). It operates similarly to the C
1218 : library function of the same name, but is more efficient.
1219 :
1220 : It is lowered into 2 other builtins, namely __builtin_setjmp_setup,
1221 : __builtin_setjmp_receiver.
1222 :
1223 : After full lowering, the body of the function should look like:
1224 :
1225 : {
1226 : int D.1844;
1227 : int D.2844;
1228 :
1229 : [...]
1230 :
1231 : __builtin_setjmp_setup (&buf, &<D1847>);
1232 : D.1844 = 0;
1233 : goto <D1846>;
1234 : <D1847>:;
1235 : __builtin_setjmp_receiver (&<D1847>);
1236 : D.1844 = 1;
1237 : <D1846>:;
1238 : if (D.1844 == 0) goto <D1848>; else goto <D1849>;
1239 :
1240 : [...]
1241 :
1242 : __builtin_setjmp_setup (&buf, &<D2847>);
1243 : D.2844 = 0;
1244 : goto <D2846>;
1245 : <D2847>:;
1246 : __builtin_setjmp_receiver (&<D2847>);
1247 : D.2844 = 1;
1248 : <D2846>:;
1249 : if (D.2844 == 0) goto <D2848>; else goto <D2849>;
1250 :
1251 : [...]
1252 :
1253 : <D3850>:;
1254 : return;
1255 : }
1256 :
1257 : During cfg creation an extra per-function (or per-OpenMP region)
1258 : block with ABNORMAL_DISPATCHER internal call will be added, unique
1259 : destination of all the abnormal call edges and the unique source of
1260 : all the abnormal edges to the receivers, thus keeping the complexity
1261 : explosion localized. */
1262 :
1263 : static void
1264 797 : lower_builtin_setjmp (gimple_stmt_iterator *gsi)
1265 : {
1266 797 : gimple *stmt = gsi_stmt (*gsi);
1267 797 : location_t loc = gimple_location (stmt);
1268 797 : tree cont_label = create_artificial_label (loc);
1269 797 : tree next_label = create_artificial_label (loc);
1270 797 : tree dest, t, arg;
1271 797 : gimple *g;
1272 :
1273 : /* __builtin_setjmp_{setup,receiver} aren't ECF_RETURNS_TWICE and for RTL
1274 : these builtins are modelled as non-local label jumps to the label
1275 : that is passed to these two builtins, so pretend we have a non-local
1276 : label during GIMPLE passes too. See PR60003. */
1277 797 : cfun->has_nonlocal_label = 1;
1278 :
1279 : /* NEXT_LABEL is the label __builtin_longjmp will jump to. Its address is
1280 : passed to both __builtin_setjmp_setup and __builtin_setjmp_receiver. */
1281 797 : FORCED_LABEL (next_label) = 1;
1282 :
1283 797 : tree orig_dest = dest = gimple_call_lhs (stmt);
1284 797 : if (orig_dest && TREE_CODE (orig_dest) == SSA_NAME)
1285 726 : dest = create_tmp_reg (TREE_TYPE (orig_dest));
1286 :
1287 : /* Build '__builtin_setjmp_setup (BUF, NEXT_LABEL)' and insert. */
1288 797 : arg = build_addr (next_label);
1289 797 : t = builtin_decl_implicit (BUILT_IN_SETJMP_SETUP);
1290 797 : g = gimple_build_call (t, 2, gimple_call_arg (stmt, 0), arg);
1291 : /* location includes block. */
1292 797 : gimple_set_location (g, loc);
1293 797 : gsi_insert_before (gsi, g, GSI_SAME_STMT);
1294 :
1295 : /* Build 'DEST = 0' and insert. */
1296 797 : if (dest)
1297 : {
1298 746 : g = gimple_build_assign (dest, build_zero_cst (TREE_TYPE (dest)));
1299 746 : gimple_set_location (g, loc);
1300 746 : gsi_insert_before (gsi, g, GSI_SAME_STMT);
1301 : }
1302 :
1303 : /* Build 'goto CONT_LABEL' and insert. */
1304 797 : g = gimple_build_goto (cont_label);
1305 797 : gsi_insert_before (gsi, g, GSI_SAME_STMT);
1306 :
1307 : /* Build 'NEXT_LABEL:' and insert. */
1308 797 : g = gimple_build_label (next_label);
1309 797 : gsi_insert_before (gsi, g, GSI_SAME_STMT);
1310 :
1311 : /* Build '__builtin_setjmp_receiver (NEXT_LABEL)' and insert. */
1312 797 : arg = build_addr (next_label);
1313 797 : t = builtin_decl_implicit (BUILT_IN_SETJMP_RECEIVER);
1314 797 : g = gimple_build_call (t, 1, arg);
1315 797 : gimple_set_location (g, loc);
1316 797 : gsi_insert_before (gsi, g, GSI_SAME_STMT);
1317 :
1318 : /* Build 'DEST = 1' and insert. */
1319 797 : if (dest)
1320 : {
1321 746 : g = gimple_build_assign (dest, fold_convert_loc (loc, TREE_TYPE (dest),
1322 : integer_one_node));
1323 746 : gimple_set_location (g, loc);
1324 746 : gsi_insert_before (gsi, g, GSI_SAME_STMT);
1325 : }
1326 :
1327 : /* Build 'CONT_LABEL:' and insert. */
1328 797 : g = gimple_build_label (cont_label);
1329 797 : gsi_insert_before (gsi, g, GSI_SAME_STMT);
1330 :
1331 : /* Build orig_dest = dest if necessary. */
1332 797 : if (dest != orig_dest)
1333 : {
1334 726 : g = gimple_build_assign (orig_dest, dest);
1335 726 : gsi_insert_before (gsi, g, GSI_SAME_STMT);
1336 : }
1337 :
1338 : /* Remove the call to __builtin_setjmp. */
1339 797 : gsi_remove (gsi, false);
1340 797 : }
1341 :
1342 : /* Lower calls to posix_memalign to
1343 : res = posix_memalign (ptr, align, size);
1344 : if (res == 0)
1345 : *ptr = __builtin_assume_aligned (*ptr, align);
1346 : or to
1347 : void *tem;
1348 : res = posix_memalign (&tem, align, size);
1349 : if (res == 0)
1350 : ptr = __builtin_assume_aligned (tem, align);
1351 : in case the first argument was &ptr. That way we can get at the
1352 : alignment of the heap pointer in CCP. */
1353 :
1354 : static void
1355 34 : lower_builtin_posix_memalign (gimple_stmt_iterator *gsi)
1356 : {
1357 34 : gimple *stmt, *call = gsi_stmt (*gsi);
1358 34 : tree pptr = gimple_call_arg (call, 0);
1359 34 : tree align = gimple_call_arg (call, 1);
1360 34 : tree res = gimple_call_lhs (call);
1361 34 : tree ptr = create_tmp_reg (ptr_type_node);
1362 34 : if (TREE_CODE (pptr) == ADDR_EXPR)
1363 : {
1364 34 : tree tem = create_tmp_var (ptr_type_node);
1365 34 : TREE_ADDRESSABLE (tem) = 1;
1366 34 : gimple_call_set_arg (call, 0, build_fold_addr_expr (tem));
1367 34 : stmt = gimple_build_assign (ptr, tem);
1368 : }
1369 : else
1370 0 : stmt = gimple_build_assign (ptr,
1371 : fold_build2 (MEM_REF, ptr_type_node, pptr,
1372 : build_int_cst (ptr_type_node, 0)));
1373 34 : if (res == NULL_TREE)
1374 : {
1375 0 : res = create_tmp_reg (integer_type_node);
1376 0 : gimple_call_set_lhs (call, res);
1377 : }
1378 34 : tree align_label = create_artificial_label (UNKNOWN_LOCATION);
1379 34 : tree noalign_label = create_artificial_label (UNKNOWN_LOCATION);
1380 34 : gimple *cond = gimple_build_cond (EQ_EXPR, res, integer_zero_node,
1381 : align_label, noalign_label);
1382 34 : gsi_insert_after (gsi, cond, GSI_NEW_STMT);
1383 34 : gsi_insert_after (gsi, gimple_build_label (align_label), GSI_NEW_STMT);
1384 34 : gsi_insert_after (gsi, stmt, GSI_NEW_STMT);
1385 68 : stmt = gimple_build_call (builtin_decl_implicit (BUILT_IN_ASSUME_ALIGNED),
1386 : 2, ptr, align);
1387 34 : gimple_call_set_lhs (stmt, ptr);
1388 34 : gsi_insert_after (gsi, stmt, GSI_NEW_STMT);
1389 34 : stmt = gimple_build_assign (fold_build2 (MEM_REF, ptr_type_node, pptr,
1390 : build_int_cst (ptr_type_node, 0)),
1391 : ptr);
1392 34 : gsi_insert_after (gsi, stmt, GSI_NEW_STMT);
1393 34 : gsi_insert_after (gsi, gimple_build_label (noalign_label), GSI_NEW_STMT);
1394 34 : }
1395 :
1396 : /* Lower calls to __builtin_assume_aligned when not optimizing. */
1397 :
1398 : static void
1399 74 : lower_builtin_assume_aligned (gimple_stmt_iterator *gsi)
1400 : {
1401 74 : gcall *call = as_a <gcall *> (gsi_stmt (*gsi));
1402 :
1403 74 : tree lhs = gimple_call_lhs (call);
1404 74 : if (!lhs || !POINTER_TYPE_P (TREE_TYPE (lhs)) || TREE_CODE (lhs) != SSA_NAME)
1405 : return;
1406 :
1407 2 : tree align = gimple_call_arg (call, 1);
1408 2 : tree misalign = (gimple_call_num_args (call) > 2
1409 2 : ? gimple_call_arg (call, 2) : NULL_TREE);
1410 2 : if (!tree_fits_uhwi_p (align)
1411 2 : || (misalign && !tree_fits_uhwi_p (misalign)))
1412 : return;
1413 :
1414 2 : unsigned aligni = TREE_INT_CST_LOW (align);
1415 2 : unsigned misaligni = misalign ? TREE_INT_CST_LOW (misalign) : 0;
1416 2 : if (aligni <= 1
1417 1 : || (aligni & (aligni - 1)) != 0
1418 1 : || (misaligni & ~(aligni - 1)) != 0)
1419 : return;
1420 :
1421 : /* For lowering we simply transfer alignment information to the
1422 : result and leave the call otherwise unchanged, it will be elided
1423 : at RTL expansion time. */
1424 1 : set_ssa_name_alignment (lhs, aligni, misaligni);
1425 : }
1426 :
1427 :
1428 : /* Record the variables in VARS into function FN. */
1429 :
1430 : void
1431 24626315 : record_vars_into (tree vars, tree fn)
1432 : {
1433 48733926 : for (; vars; vars = DECL_CHAIN (vars))
1434 : {
1435 24107611 : tree var = vars;
1436 :
1437 : /* BIND_EXPRs contains also function/type/constant declarations
1438 : we don't need to care about. */
1439 24107611 : if (!VAR_P (var))
1440 674247 : continue;
1441 :
1442 : /* Nothing to do in this case. */
1443 23433364 : if (DECL_EXTERNAL (var))
1444 2593 : continue;
1445 :
1446 : /* Record the variable. */
1447 23430771 : add_local_decl (DECL_STRUCT_FUNCTION (fn), var);
1448 : }
1449 24626315 : }
1450 :
1451 :
1452 : /* Record the variables in VARS into current_function_decl. */
1453 :
1454 : void
1455 24508382 : record_vars (tree vars)
1456 : {
1457 24508382 : record_vars_into (vars, current_function_decl);
1458 24508382 : }
|