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