Daily bump.
[official-gcc.git] / gcc / gimple.c
blob488f8c82b82e4b7266d3bfc97e74250fd65d0ed9
1 /* Gimple IR support functions.
3 Copyright (C) 2007-2017 Free Software Foundation, Inc.
4 Contributed by Aldy Hernandez <aldyh@redhat.com>
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "backend.h"
26 #include "tree.h"
27 #include "gimple.h"
28 #include "ssa.h"
29 #include "cgraph.h"
30 #include "diagnostic.h"
31 #include "alias.h"
32 #include "fold-const.h"
33 #include "calls.h"
34 #include "stor-layout.h"
35 #include "internal-fn.h"
36 #include "tree-eh.h"
37 #include "gimple-iterator.h"
38 #include "gimple-walk.h"
39 #include "gimplify.h"
40 #include "target.h"
41 #include "builtins.h"
42 #include "selftest.h"
43 #include "gimple-pretty-print.h"
44 #include "asan.h"
47 /* All the tuples have their operand vector (if present) at the very bottom
48 of the structure. Therefore, the offset required to find the
49 operands vector the size of the structure minus the size of the 1
50 element tree array at the end (see gimple_ops). */
51 #define DEFGSSTRUCT(SYM, STRUCT, HAS_TREE_OP) \
52 (HAS_TREE_OP ? sizeof (struct STRUCT) - sizeof (tree) : 0),
53 EXPORTED_CONST size_t gimple_ops_offset_[] = {
54 #include "gsstruct.def"
56 #undef DEFGSSTRUCT
58 #define DEFGSSTRUCT(SYM, STRUCT, HAS_TREE_OP) sizeof (struct STRUCT),
59 static const size_t gsstruct_code_size[] = {
60 #include "gsstruct.def"
62 #undef DEFGSSTRUCT
64 #define DEFGSCODE(SYM, NAME, GSSCODE) NAME,
65 const char *const gimple_code_name[] = {
66 #include "gimple.def"
68 #undef DEFGSCODE
70 #define DEFGSCODE(SYM, NAME, GSSCODE) GSSCODE,
71 EXPORTED_CONST enum gimple_statement_structure_enum gss_for_code_[] = {
72 #include "gimple.def"
74 #undef DEFGSCODE
76 /* Gimple stats. */
78 int gimple_alloc_counts[(int) gimple_alloc_kind_all];
79 int gimple_alloc_sizes[(int) gimple_alloc_kind_all];
81 /* Keep in sync with gimple.h:enum gimple_alloc_kind. */
82 static const char * const gimple_alloc_kind_names[] = {
83 "assignments",
84 "phi nodes",
85 "conditionals",
86 "everything else"
89 /* Static gimple tuple members. */
90 const enum gimple_code gassign::code_;
91 const enum gimple_code gcall::code_;
92 const enum gimple_code gcond::code_;
95 /* Gimple tuple constructors.
96 Note: Any constructor taking a ``gimple_seq'' as a parameter, can
97 be passed a NULL to start with an empty sequence. */
99 /* Set the code for statement G to CODE. */
101 static inline void
102 gimple_set_code (gimple *g, enum gimple_code code)
104 g->code = code;
107 /* Return the number of bytes needed to hold a GIMPLE statement with
108 code CODE. */
110 static inline size_t
111 gimple_size (enum gimple_code code)
113 return gsstruct_code_size[gss_for_code (code)];
116 /* Allocate memory for a GIMPLE statement with code CODE and NUM_OPS
117 operands. */
119 gimple *
120 gimple_alloc_stat (enum gimple_code code, unsigned num_ops MEM_STAT_DECL)
122 size_t size;
123 gimple *stmt;
125 size = gimple_size (code);
126 if (num_ops > 0)
127 size += sizeof (tree) * (num_ops - 1);
129 if (GATHER_STATISTICS)
131 enum gimple_alloc_kind kind = gimple_alloc_kind (code);
132 gimple_alloc_counts[(int) kind]++;
133 gimple_alloc_sizes[(int) kind] += size;
136 stmt = ggc_alloc_cleared_gimple_statement_stat (size PASS_MEM_STAT);
137 gimple_set_code (stmt, code);
138 gimple_set_num_ops (stmt, num_ops);
140 /* Do not call gimple_set_modified here as it has other side
141 effects and this tuple is still not completely built. */
142 stmt->modified = 1;
143 gimple_init_singleton (stmt);
145 return stmt;
148 /* Set SUBCODE to be the code of the expression computed by statement G. */
150 static inline void
151 gimple_set_subcode (gimple *g, unsigned subcode)
153 /* We only have 16 bits for the RHS code. Assert that we are not
154 overflowing it. */
155 gcc_assert (subcode < (1 << 16));
156 g->subcode = subcode;
161 /* Build a tuple with operands. CODE is the statement to build (which
162 must be one of the GIMPLE_WITH_OPS tuples). SUBCODE is the subcode
163 for the new tuple. NUM_OPS is the number of operands to allocate. */
165 #define gimple_build_with_ops(c, s, n) \
166 gimple_build_with_ops_stat (c, s, n MEM_STAT_INFO)
168 static gimple *
169 gimple_build_with_ops_stat (enum gimple_code code, unsigned subcode,
170 unsigned num_ops MEM_STAT_DECL)
172 gimple *s = gimple_alloc_stat (code, num_ops PASS_MEM_STAT);
173 gimple_set_subcode (s, subcode);
175 return s;
179 /* Build a GIMPLE_RETURN statement returning RETVAL. */
181 greturn *
182 gimple_build_return (tree retval)
184 greturn *s
185 = as_a <greturn *> (gimple_build_with_ops (GIMPLE_RETURN, ERROR_MARK,
186 2));
187 if (retval)
188 gimple_return_set_retval (s, retval);
189 return s;
192 /* Reset alias information on call S. */
194 void
195 gimple_call_reset_alias_info (gcall *s)
197 if (gimple_call_flags (s) & ECF_CONST)
198 memset (gimple_call_use_set (s), 0, sizeof (struct pt_solution));
199 else
200 pt_solution_reset (gimple_call_use_set (s));
201 if (gimple_call_flags (s) & (ECF_CONST|ECF_PURE|ECF_NOVOPS))
202 memset (gimple_call_clobber_set (s), 0, sizeof (struct pt_solution));
203 else
204 pt_solution_reset (gimple_call_clobber_set (s));
207 /* Helper for gimple_build_call, gimple_build_call_valist,
208 gimple_build_call_vec and gimple_build_call_from_tree. Build the basic
209 components of a GIMPLE_CALL statement to function FN with NARGS
210 arguments. */
212 static inline gcall *
213 gimple_build_call_1 (tree fn, unsigned nargs)
215 gcall *s
216 = as_a <gcall *> (gimple_build_with_ops (GIMPLE_CALL, ERROR_MARK,
217 nargs + 3));
218 if (TREE_CODE (fn) == FUNCTION_DECL)
219 fn = build_fold_addr_expr (fn);
220 gimple_set_op (s, 1, fn);
221 gimple_call_set_fntype (s, TREE_TYPE (TREE_TYPE (fn)));
222 gimple_call_reset_alias_info (s);
223 return s;
227 /* Build a GIMPLE_CALL statement to function FN with the arguments
228 specified in vector ARGS. */
230 gcall *
231 gimple_build_call_vec (tree fn, vec<tree> args)
233 unsigned i;
234 unsigned nargs = args.length ();
235 gcall *call = gimple_build_call_1 (fn, nargs);
237 for (i = 0; i < nargs; i++)
238 gimple_call_set_arg (call, i, args[i]);
240 return call;
244 /* Build a GIMPLE_CALL statement to function FN. NARGS is the number of
245 arguments. The ... are the arguments. */
247 gcall *
248 gimple_build_call (tree fn, unsigned nargs, ...)
250 va_list ap;
251 gcall *call;
252 unsigned i;
254 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL || is_gimple_call_addr (fn));
256 call = gimple_build_call_1 (fn, nargs);
258 va_start (ap, nargs);
259 for (i = 0; i < nargs; i++)
260 gimple_call_set_arg (call, i, va_arg (ap, tree));
261 va_end (ap);
263 return call;
267 /* Build a GIMPLE_CALL statement to function FN. NARGS is the number of
268 arguments. AP contains the arguments. */
270 gcall *
271 gimple_build_call_valist (tree fn, unsigned nargs, va_list ap)
273 gcall *call;
274 unsigned i;
276 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL || is_gimple_call_addr (fn));
278 call = gimple_build_call_1 (fn, nargs);
280 for (i = 0; i < nargs; i++)
281 gimple_call_set_arg (call, i, va_arg (ap, tree));
283 return call;
287 /* Helper for gimple_build_call_internal and gimple_build_call_internal_vec.
288 Build the basic components of a GIMPLE_CALL statement to internal
289 function FN with NARGS arguments. */
291 static inline gcall *
292 gimple_build_call_internal_1 (enum internal_fn fn, unsigned nargs)
294 gcall *s
295 = as_a <gcall *> (gimple_build_with_ops (GIMPLE_CALL, ERROR_MARK,
296 nargs + 3));
297 s->subcode |= GF_CALL_INTERNAL;
298 gimple_call_set_internal_fn (s, fn);
299 gimple_call_reset_alias_info (s);
300 return s;
304 /* Build a GIMPLE_CALL statement to internal function FN. NARGS is
305 the number of arguments. The ... are the arguments. */
307 gcall *
308 gimple_build_call_internal (enum internal_fn fn, unsigned nargs, ...)
310 va_list ap;
311 gcall *call;
312 unsigned i;
314 call = gimple_build_call_internal_1 (fn, nargs);
315 va_start (ap, nargs);
316 for (i = 0; i < nargs; i++)
317 gimple_call_set_arg (call, i, va_arg (ap, tree));
318 va_end (ap);
320 return call;
324 /* Build a GIMPLE_CALL statement to internal function FN with the arguments
325 specified in vector ARGS. */
327 gcall *
328 gimple_build_call_internal_vec (enum internal_fn fn, vec<tree> args)
330 unsigned i, nargs;
331 gcall *call;
333 nargs = args.length ();
334 call = gimple_build_call_internal_1 (fn, nargs);
335 for (i = 0; i < nargs; i++)
336 gimple_call_set_arg (call, i, args[i]);
338 return call;
342 /* Build a GIMPLE_CALL statement from CALL_EXPR T. Note that T is
343 assumed to be in GIMPLE form already. Minimal checking is done of
344 this fact. */
346 gcall *
347 gimple_build_call_from_tree (tree t)
349 unsigned i, nargs;
350 gcall *call;
351 tree fndecl = get_callee_fndecl (t);
353 gcc_assert (TREE_CODE (t) == CALL_EXPR);
355 nargs = call_expr_nargs (t);
356 call = gimple_build_call_1 (fndecl ? fndecl : CALL_EXPR_FN (t), nargs);
358 for (i = 0; i < nargs; i++)
359 gimple_call_set_arg (call, i, CALL_EXPR_ARG (t, i));
361 gimple_set_block (call, TREE_BLOCK (t));
363 /* Carry all the CALL_EXPR flags to the new GIMPLE_CALL. */
364 gimple_call_set_chain (call, CALL_EXPR_STATIC_CHAIN (t));
365 gimple_call_set_tail (call, CALL_EXPR_TAILCALL (t));
366 gimple_call_set_must_tail (call, CALL_EXPR_MUST_TAIL_CALL (t));
367 gimple_call_set_return_slot_opt (call, CALL_EXPR_RETURN_SLOT_OPT (t));
368 if (fndecl
369 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
370 && (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_ALLOCA
371 || DECL_FUNCTION_CODE (fndecl) == BUILT_IN_ALLOCA_WITH_ALIGN))
372 gimple_call_set_alloca_for_var (call, CALL_ALLOCA_FOR_VAR_P (t));
373 else
374 gimple_call_set_from_thunk (call, CALL_FROM_THUNK_P (t));
375 gimple_call_set_va_arg_pack (call, CALL_EXPR_VA_ARG_PACK (t));
376 gimple_call_set_nothrow (call, TREE_NOTHROW (t));
377 gimple_call_set_by_descriptor (call, CALL_EXPR_BY_DESCRIPTOR (t));
378 gimple_set_no_warning (call, TREE_NO_WARNING (t));
379 gimple_call_set_with_bounds (call, CALL_WITH_BOUNDS_P (t));
381 return call;
385 /* Build a GIMPLE_ASSIGN statement.
387 LHS of the assignment.
388 RHS of the assignment which can be unary or binary. */
390 gassign *
391 gimple_build_assign (tree lhs, tree rhs MEM_STAT_DECL)
393 enum tree_code subcode;
394 tree op1, op2, op3;
396 extract_ops_from_tree (rhs, &subcode, &op1, &op2, &op3);
397 return gimple_build_assign (lhs, subcode, op1, op2, op3 PASS_MEM_STAT);
401 /* Build a GIMPLE_ASSIGN statement with subcode SUBCODE and operands
402 OP1, OP2 and OP3. */
404 static inline gassign *
405 gimple_build_assign_1 (tree lhs, enum tree_code subcode, tree op1,
406 tree op2, tree op3 MEM_STAT_DECL)
408 unsigned num_ops;
409 gassign *p;
411 /* Need 1 operand for LHS and 1 or 2 for the RHS (depending on the
412 code). */
413 num_ops = get_gimple_rhs_num_ops (subcode) + 1;
415 p = as_a <gassign *> (
416 gimple_build_with_ops_stat (GIMPLE_ASSIGN, (unsigned)subcode, num_ops
417 PASS_MEM_STAT));
418 gimple_assign_set_lhs (p, lhs);
419 gimple_assign_set_rhs1 (p, op1);
420 if (op2)
422 gcc_assert (num_ops > 2);
423 gimple_assign_set_rhs2 (p, op2);
426 if (op3)
428 gcc_assert (num_ops > 3);
429 gimple_assign_set_rhs3 (p, op3);
432 return p;
435 /* Build a GIMPLE_ASSIGN statement with subcode SUBCODE and operands
436 OP1, OP2 and OP3. */
438 gassign *
439 gimple_build_assign (tree lhs, enum tree_code subcode, tree op1,
440 tree op2, tree op3 MEM_STAT_DECL)
442 return gimple_build_assign_1 (lhs, subcode, op1, op2, op3 PASS_MEM_STAT);
445 /* Build a GIMPLE_ASSIGN statement with subcode SUBCODE and operands
446 OP1 and OP2. */
448 gassign *
449 gimple_build_assign (tree lhs, enum tree_code subcode, tree op1,
450 tree op2 MEM_STAT_DECL)
452 return gimple_build_assign_1 (lhs, subcode, op1, op2, NULL_TREE
453 PASS_MEM_STAT);
456 /* Build a GIMPLE_ASSIGN statement with subcode SUBCODE and operand OP1. */
458 gassign *
459 gimple_build_assign (tree lhs, enum tree_code subcode, tree op1 MEM_STAT_DECL)
461 return gimple_build_assign_1 (lhs, subcode, op1, NULL_TREE, NULL_TREE
462 PASS_MEM_STAT);
466 /* Build a GIMPLE_COND statement.
468 PRED is the condition used to compare LHS and the RHS.
469 T_LABEL is the label to jump to if the condition is true.
470 F_LABEL is the label to jump to otherwise. */
472 gcond *
473 gimple_build_cond (enum tree_code pred_code, tree lhs, tree rhs,
474 tree t_label, tree f_label)
476 gcond *p;
478 gcc_assert (TREE_CODE_CLASS (pred_code) == tcc_comparison);
479 p = as_a <gcond *> (gimple_build_with_ops (GIMPLE_COND, pred_code, 4));
480 gimple_cond_set_lhs (p, lhs);
481 gimple_cond_set_rhs (p, rhs);
482 gimple_cond_set_true_label (p, t_label);
483 gimple_cond_set_false_label (p, f_label);
484 return p;
487 /* Build a GIMPLE_COND statement from the conditional expression tree
488 COND. T_LABEL and F_LABEL are as in gimple_build_cond. */
490 gcond *
491 gimple_build_cond_from_tree (tree cond, tree t_label, tree f_label)
493 enum tree_code code;
494 tree lhs, rhs;
496 gimple_cond_get_ops_from_tree (cond, &code, &lhs, &rhs);
497 return gimple_build_cond (code, lhs, rhs, t_label, f_label);
500 /* Set code, lhs, and rhs of a GIMPLE_COND from a suitable
501 boolean expression tree COND. */
503 void
504 gimple_cond_set_condition_from_tree (gcond *stmt, tree cond)
506 enum tree_code code;
507 tree lhs, rhs;
509 gimple_cond_get_ops_from_tree (cond, &code, &lhs, &rhs);
510 gimple_cond_set_condition (stmt, code, lhs, rhs);
513 /* Build a GIMPLE_LABEL statement for LABEL. */
515 glabel *
516 gimple_build_label (tree label)
518 glabel *p
519 = as_a <glabel *> (gimple_build_with_ops (GIMPLE_LABEL, ERROR_MARK, 1));
520 gimple_label_set_label (p, label);
521 return p;
524 /* Build a GIMPLE_GOTO statement to label DEST. */
526 ggoto *
527 gimple_build_goto (tree dest)
529 ggoto *p
530 = as_a <ggoto *> (gimple_build_with_ops (GIMPLE_GOTO, ERROR_MARK, 1));
531 gimple_goto_set_dest (p, dest);
532 return p;
536 /* Build a GIMPLE_NOP statement. */
538 gimple *
539 gimple_build_nop (void)
541 return gimple_alloc (GIMPLE_NOP, 0);
545 /* Build a GIMPLE_BIND statement.
546 VARS are the variables in BODY.
547 BLOCK is the containing block. */
549 gbind *
550 gimple_build_bind (tree vars, gimple_seq body, tree block)
552 gbind *p = as_a <gbind *> (gimple_alloc (GIMPLE_BIND, 0));
553 gimple_bind_set_vars (p, vars);
554 if (body)
555 gimple_bind_set_body (p, body);
556 if (block)
557 gimple_bind_set_block (p, block);
558 return p;
561 /* Helper function to set the simple fields of a asm stmt.
563 STRING is a pointer to a string that is the asm blocks assembly code.
564 NINPUT is the number of register inputs.
565 NOUTPUT is the number of register outputs.
566 NCLOBBERS is the number of clobbered registers.
569 static inline gasm *
570 gimple_build_asm_1 (const char *string, unsigned ninputs, unsigned noutputs,
571 unsigned nclobbers, unsigned nlabels)
573 gasm *p;
574 int size = strlen (string);
576 /* ASMs with labels cannot have outputs. This should have been
577 enforced by the front end. */
578 gcc_assert (nlabels == 0 || noutputs == 0);
580 p = as_a <gasm *> (
581 gimple_build_with_ops (GIMPLE_ASM, ERROR_MARK,
582 ninputs + noutputs + nclobbers + nlabels));
584 p->ni = ninputs;
585 p->no = noutputs;
586 p->nc = nclobbers;
587 p->nl = nlabels;
588 p->string = ggc_alloc_string (string, size);
590 if (GATHER_STATISTICS)
591 gimple_alloc_sizes[(int) gimple_alloc_kind (GIMPLE_ASM)] += size;
593 return p;
596 /* Build a GIMPLE_ASM statement.
598 STRING is the assembly code.
599 NINPUT is the number of register inputs.
600 NOUTPUT is the number of register outputs.
601 NCLOBBERS is the number of clobbered registers.
602 INPUTS is a vector of the input register parameters.
603 OUTPUTS is a vector of the output register parameters.
604 CLOBBERS is a vector of the clobbered register parameters.
605 LABELS is a vector of destination labels. */
607 gasm *
608 gimple_build_asm_vec (const char *string, vec<tree, va_gc> *inputs,
609 vec<tree, va_gc> *outputs, vec<tree, va_gc> *clobbers,
610 vec<tree, va_gc> *labels)
612 gasm *p;
613 unsigned i;
615 p = gimple_build_asm_1 (string,
616 vec_safe_length (inputs),
617 vec_safe_length (outputs),
618 vec_safe_length (clobbers),
619 vec_safe_length (labels));
621 for (i = 0; i < vec_safe_length (inputs); i++)
622 gimple_asm_set_input_op (p, i, (*inputs)[i]);
624 for (i = 0; i < vec_safe_length (outputs); i++)
625 gimple_asm_set_output_op (p, i, (*outputs)[i]);
627 for (i = 0; i < vec_safe_length (clobbers); i++)
628 gimple_asm_set_clobber_op (p, i, (*clobbers)[i]);
630 for (i = 0; i < vec_safe_length (labels); i++)
631 gimple_asm_set_label_op (p, i, (*labels)[i]);
633 return p;
636 /* Build a GIMPLE_CATCH statement.
638 TYPES are the catch types.
639 HANDLER is the exception handler. */
641 gcatch *
642 gimple_build_catch (tree types, gimple_seq handler)
644 gcatch *p = as_a <gcatch *> (gimple_alloc (GIMPLE_CATCH, 0));
645 gimple_catch_set_types (p, types);
646 if (handler)
647 gimple_catch_set_handler (p, handler);
649 return p;
652 /* Build a GIMPLE_EH_FILTER statement.
654 TYPES are the filter's types.
655 FAILURE is the filter's failure action. */
657 geh_filter *
658 gimple_build_eh_filter (tree types, gimple_seq failure)
660 geh_filter *p = as_a <geh_filter *> (gimple_alloc (GIMPLE_EH_FILTER, 0));
661 gimple_eh_filter_set_types (p, types);
662 if (failure)
663 gimple_eh_filter_set_failure (p, failure);
665 return p;
668 /* Build a GIMPLE_EH_MUST_NOT_THROW statement. */
670 geh_mnt *
671 gimple_build_eh_must_not_throw (tree decl)
673 geh_mnt *p = as_a <geh_mnt *> (gimple_alloc (GIMPLE_EH_MUST_NOT_THROW, 0));
675 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
676 gcc_assert (flags_from_decl_or_type (decl) & ECF_NORETURN);
677 gimple_eh_must_not_throw_set_fndecl (p, decl);
679 return p;
682 /* Build a GIMPLE_EH_ELSE statement. */
684 geh_else *
685 gimple_build_eh_else (gimple_seq n_body, gimple_seq e_body)
687 geh_else *p = as_a <geh_else *> (gimple_alloc (GIMPLE_EH_ELSE, 0));
688 gimple_eh_else_set_n_body (p, n_body);
689 gimple_eh_else_set_e_body (p, e_body);
690 return p;
693 /* Build a GIMPLE_TRY statement.
695 EVAL is the expression to evaluate.
696 CLEANUP is the cleanup expression.
697 KIND is either GIMPLE_TRY_CATCH or GIMPLE_TRY_FINALLY depending on
698 whether this is a try/catch or a try/finally respectively. */
700 gtry *
701 gimple_build_try (gimple_seq eval, gimple_seq cleanup,
702 enum gimple_try_flags kind)
704 gtry *p;
706 gcc_assert (kind == GIMPLE_TRY_CATCH || kind == GIMPLE_TRY_FINALLY);
707 p = as_a <gtry *> (gimple_alloc (GIMPLE_TRY, 0));
708 gimple_set_subcode (p, kind);
709 if (eval)
710 gimple_try_set_eval (p, eval);
711 if (cleanup)
712 gimple_try_set_cleanup (p, cleanup);
714 return p;
717 /* Construct a GIMPLE_WITH_CLEANUP_EXPR statement.
719 CLEANUP is the cleanup expression. */
721 gimple *
722 gimple_build_wce (gimple_seq cleanup)
724 gimple *p = gimple_alloc (GIMPLE_WITH_CLEANUP_EXPR, 0);
725 if (cleanup)
726 gimple_wce_set_cleanup (p, cleanup);
728 return p;
732 /* Build a GIMPLE_RESX statement. */
734 gresx *
735 gimple_build_resx (int region)
737 gresx *p
738 = as_a <gresx *> (gimple_build_with_ops (GIMPLE_RESX, ERROR_MARK, 0));
739 p->region = region;
740 return p;
744 /* The helper for constructing a gimple switch statement.
745 INDEX is the switch's index.
746 NLABELS is the number of labels in the switch excluding the default.
747 DEFAULT_LABEL is the default label for the switch statement. */
749 gswitch *
750 gimple_build_switch_nlabels (unsigned nlabels, tree index, tree default_label)
752 /* nlabels + 1 default label + 1 index. */
753 gcc_checking_assert (default_label);
754 gswitch *p = as_a <gswitch *> (gimple_build_with_ops (GIMPLE_SWITCH,
755 ERROR_MARK,
756 1 + 1 + nlabels));
757 gimple_switch_set_index (p, index);
758 gimple_switch_set_default_label (p, default_label);
759 return p;
762 /* Build a GIMPLE_SWITCH statement.
764 INDEX is the switch's index.
765 DEFAULT_LABEL is the default label
766 ARGS is a vector of labels excluding the default. */
768 gswitch *
769 gimple_build_switch (tree index, tree default_label, vec<tree> args)
771 unsigned i, nlabels = args.length ();
773 gswitch *p = gimple_build_switch_nlabels (nlabels, index, default_label);
775 /* Copy the labels from the vector to the switch statement. */
776 for (i = 0; i < nlabels; i++)
777 gimple_switch_set_label (p, i + 1, args[i]);
779 return p;
782 /* Build a GIMPLE_EH_DISPATCH statement. */
784 geh_dispatch *
785 gimple_build_eh_dispatch (int region)
787 geh_dispatch *p
788 = as_a <geh_dispatch *> (
789 gimple_build_with_ops (GIMPLE_EH_DISPATCH, ERROR_MARK, 0));
790 p->region = region;
791 return p;
794 /* Build a new GIMPLE_DEBUG_BIND statement.
796 VAR is bound to VALUE; block and location are taken from STMT. */
798 gdebug *
799 gimple_build_debug_bind_stat (tree var, tree value, gimple *stmt MEM_STAT_DECL)
801 gdebug *p
802 = as_a <gdebug *> (gimple_build_with_ops_stat (GIMPLE_DEBUG,
803 (unsigned)GIMPLE_DEBUG_BIND, 2
804 PASS_MEM_STAT));
805 gimple_debug_bind_set_var (p, var);
806 gimple_debug_bind_set_value (p, value);
807 if (stmt)
808 gimple_set_location (p, gimple_location (stmt));
810 return p;
814 /* Build a new GIMPLE_DEBUG_SOURCE_BIND statement.
816 VAR is bound to VALUE; block and location are taken from STMT. */
818 gdebug *
819 gimple_build_debug_source_bind_stat (tree var, tree value,
820 gimple *stmt MEM_STAT_DECL)
822 gdebug *p
823 = as_a <gdebug *> (
824 gimple_build_with_ops_stat (GIMPLE_DEBUG,
825 (unsigned)GIMPLE_DEBUG_SOURCE_BIND, 2
826 PASS_MEM_STAT));
828 gimple_debug_source_bind_set_var (p, var);
829 gimple_debug_source_bind_set_value (p, value);
830 if (stmt)
831 gimple_set_location (p, gimple_location (stmt));
833 return p;
837 /* Build a GIMPLE_OMP_CRITICAL statement.
839 BODY is the sequence of statements for which only one thread can execute.
840 NAME is optional identifier for this critical block.
841 CLAUSES are clauses for this critical block. */
843 gomp_critical *
844 gimple_build_omp_critical (gimple_seq body, tree name, tree clauses)
846 gomp_critical *p
847 = as_a <gomp_critical *> (gimple_alloc (GIMPLE_OMP_CRITICAL, 0));
848 gimple_omp_critical_set_name (p, name);
849 gimple_omp_critical_set_clauses (p, clauses);
850 if (body)
851 gimple_omp_set_body (p, body);
853 return p;
856 /* Build a GIMPLE_OMP_FOR statement.
858 BODY is sequence of statements inside the for loop.
859 KIND is the `for' variant.
860 CLAUSES, are any of the construct's clauses.
861 COLLAPSE is the collapse count.
862 PRE_BODY is the sequence of statements that are loop invariant. */
864 gomp_for *
865 gimple_build_omp_for (gimple_seq body, int kind, tree clauses, size_t collapse,
866 gimple_seq pre_body)
868 gomp_for *p = as_a <gomp_for *> (gimple_alloc (GIMPLE_OMP_FOR, 0));
869 if (body)
870 gimple_omp_set_body (p, body);
871 gimple_omp_for_set_clauses (p, clauses);
872 gimple_omp_for_set_kind (p, kind);
873 p->collapse = collapse;
874 p->iter = ggc_cleared_vec_alloc<gimple_omp_for_iter> (collapse);
876 if (pre_body)
877 gimple_omp_for_set_pre_body (p, pre_body);
879 return p;
883 /* Build a GIMPLE_OMP_PARALLEL statement.
885 BODY is sequence of statements which are executed in parallel.
886 CLAUSES, are the OMP parallel construct's clauses.
887 CHILD_FN is the function created for the parallel threads to execute.
888 DATA_ARG are the shared data argument(s). */
890 gomp_parallel *
891 gimple_build_omp_parallel (gimple_seq body, tree clauses, tree child_fn,
892 tree data_arg)
894 gomp_parallel *p
895 = as_a <gomp_parallel *> (gimple_alloc (GIMPLE_OMP_PARALLEL, 0));
896 if (body)
897 gimple_omp_set_body (p, body);
898 gimple_omp_parallel_set_clauses (p, clauses);
899 gimple_omp_parallel_set_child_fn (p, child_fn);
900 gimple_omp_parallel_set_data_arg (p, data_arg);
902 return p;
906 /* Build a GIMPLE_OMP_TASK statement.
908 BODY is sequence of statements which are executed by the explicit task.
909 CLAUSES, are the OMP parallel construct's clauses.
910 CHILD_FN is the function created for the parallel threads to execute.
911 DATA_ARG are the shared data argument(s).
912 COPY_FN is the optional function for firstprivate initialization.
913 ARG_SIZE and ARG_ALIGN are size and alignment of the data block. */
915 gomp_task *
916 gimple_build_omp_task (gimple_seq body, tree clauses, tree child_fn,
917 tree data_arg, tree copy_fn, tree arg_size,
918 tree arg_align)
920 gomp_task *p = as_a <gomp_task *> (gimple_alloc (GIMPLE_OMP_TASK, 0));
921 if (body)
922 gimple_omp_set_body (p, body);
923 gimple_omp_task_set_clauses (p, clauses);
924 gimple_omp_task_set_child_fn (p, child_fn);
925 gimple_omp_task_set_data_arg (p, data_arg);
926 gimple_omp_task_set_copy_fn (p, copy_fn);
927 gimple_omp_task_set_arg_size (p, arg_size);
928 gimple_omp_task_set_arg_align (p, arg_align);
930 return p;
934 /* Build a GIMPLE_OMP_SECTION statement for a sections statement.
936 BODY is the sequence of statements in the section. */
938 gimple *
939 gimple_build_omp_section (gimple_seq body)
941 gimple *p = gimple_alloc (GIMPLE_OMP_SECTION, 0);
942 if (body)
943 gimple_omp_set_body (p, body);
945 return p;
949 /* Build a GIMPLE_OMP_MASTER statement.
951 BODY is the sequence of statements to be executed by just the master. */
953 gimple *
954 gimple_build_omp_master (gimple_seq body)
956 gimple *p = gimple_alloc (GIMPLE_OMP_MASTER, 0);
957 if (body)
958 gimple_omp_set_body (p, body);
960 return p;
963 /* Build a GIMPLE_OMP_GRID_BODY statement.
965 BODY is the sequence of statements to be executed by the kernel. */
967 gimple *
968 gimple_build_omp_grid_body (gimple_seq body)
970 gimple *p = gimple_alloc (GIMPLE_OMP_GRID_BODY, 0);
971 if (body)
972 gimple_omp_set_body (p, body);
974 return p;
977 /* Build a GIMPLE_OMP_TASKGROUP statement.
979 BODY is the sequence of statements to be executed by the taskgroup
980 construct. */
982 gimple *
983 gimple_build_omp_taskgroup (gimple_seq body)
985 gimple *p = gimple_alloc (GIMPLE_OMP_TASKGROUP, 0);
986 if (body)
987 gimple_omp_set_body (p, body);
989 return p;
993 /* Build a GIMPLE_OMP_CONTINUE statement.
995 CONTROL_DEF is the definition of the control variable.
996 CONTROL_USE is the use of the control variable. */
998 gomp_continue *
999 gimple_build_omp_continue (tree control_def, tree control_use)
1001 gomp_continue *p
1002 = as_a <gomp_continue *> (gimple_alloc (GIMPLE_OMP_CONTINUE, 0));
1003 gimple_omp_continue_set_control_def (p, control_def);
1004 gimple_omp_continue_set_control_use (p, control_use);
1005 return p;
1008 /* Build a GIMPLE_OMP_ORDERED statement.
1010 BODY is the sequence of statements inside a loop that will executed in
1011 sequence.
1012 CLAUSES are clauses for this statement. */
1014 gomp_ordered *
1015 gimple_build_omp_ordered (gimple_seq body, tree clauses)
1017 gomp_ordered *p
1018 = as_a <gomp_ordered *> (gimple_alloc (GIMPLE_OMP_ORDERED, 0));
1019 gimple_omp_ordered_set_clauses (p, clauses);
1020 if (body)
1021 gimple_omp_set_body (p, body);
1023 return p;
1027 /* Build a GIMPLE_OMP_RETURN statement.
1028 WAIT_P is true if this is a non-waiting return. */
1030 gimple *
1031 gimple_build_omp_return (bool wait_p)
1033 gimple *p = gimple_alloc (GIMPLE_OMP_RETURN, 0);
1034 if (wait_p)
1035 gimple_omp_return_set_nowait (p);
1037 return p;
1041 /* Build a GIMPLE_OMP_SECTIONS statement.
1043 BODY is a sequence of section statements.
1044 CLAUSES are any of the OMP sections contsruct's clauses: private,
1045 firstprivate, lastprivate, reduction, and nowait. */
1047 gomp_sections *
1048 gimple_build_omp_sections (gimple_seq body, tree clauses)
1050 gomp_sections *p
1051 = as_a <gomp_sections *> (gimple_alloc (GIMPLE_OMP_SECTIONS, 0));
1052 if (body)
1053 gimple_omp_set_body (p, body);
1054 gimple_omp_sections_set_clauses (p, clauses);
1056 return p;
1060 /* Build a GIMPLE_OMP_SECTIONS_SWITCH. */
1062 gimple *
1063 gimple_build_omp_sections_switch (void)
1065 return gimple_alloc (GIMPLE_OMP_SECTIONS_SWITCH, 0);
1069 /* Build a GIMPLE_OMP_SINGLE statement.
1071 BODY is the sequence of statements that will be executed once.
1072 CLAUSES are any of the OMP single construct's clauses: private, firstprivate,
1073 copyprivate, nowait. */
1075 gomp_single *
1076 gimple_build_omp_single (gimple_seq body, tree clauses)
1078 gomp_single *p
1079 = as_a <gomp_single *> (gimple_alloc (GIMPLE_OMP_SINGLE, 0));
1080 if (body)
1081 gimple_omp_set_body (p, body);
1082 gimple_omp_single_set_clauses (p, clauses);
1084 return p;
1088 /* Build a GIMPLE_OMP_TARGET statement.
1090 BODY is the sequence of statements that will be executed.
1091 KIND is the kind of the region.
1092 CLAUSES are any of the construct's clauses. */
1094 gomp_target *
1095 gimple_build_omp_target (gimple_seq body, int kind, tree clauses)
1097 gomp_target *p
1098 = as_a <gomp_target *> (gimple_alloc (GIMPLE_OMP_TARGET, 0));
1099 if (body)
1100 gimple_omp_set_body (p, body);
1101 gimple_omp_target_set_clauses (p, clauses);
1102 gimple_omp_target_set_kind (p, kind);
1104 return p;
1108 /* Build a GIMPLE_OMP_TEAMS statement.
1110 BODY is the sequence of statements that will be executed.
1111 CLAUSES are any of the OMP teams construct's clauses. */
1113 gomp_teams *
1114 gimple_build_omp_teams (gimple_seq body, tree clauses)
1116 gomp_teams *p = as_a <gomp_teams *> (gimple_alloc (GIMPLE_OMP_TEAMS, 0));
1117 if (body)
1118 gimple_omp_set_body (p, body);
1119 gimple_omp_teams_set_clauses (p, clauses);
1121 return p;
1125 /* Build a GIMPLE_OMP_ATOMIC_LOAD statement. */
1127 gomp_atomic_load *
1128 gimple_build_omp_atomic_load (tree lhs, tree rhs)
1130 gomp_atomic_load *p
1131 = as_a <gomp_atomic_load *> (gimple_alloc (GIMPLE_OMP_ATOMIC_LOAD, 0));
1132 gimple_omp_atomic_load_set_lhs (p, lhs);
1133 gimple_omp_atomic_load_set_rhs (p, rhs);
1134 return p;
1137 /* Build a GIMPLE_OMP_ATOMIC_STORE statement.
1139 VAL is the value we are storing. */
1141 gomp_atomic_store *
1142 gimple_build_omp_atomic_store (tree val)
1144 gomp_atomic_store *p
1145 = as_a <gomp_atomic_store *> (gimple_alloc (GIMPLE_OMP_ATOMIC_STORE, 0));
1146 gimple_omp_atomic_store_set_val (p, val);
1147 return p;
1150 /* Build a GIMPLE_TRANSACTION statement. */
1152 gtransaction *
1153 gimple_build_transaction (gimple_seq body)
1155 gtransaction *p
1156 = as_a <gtransaction *> (gimple_alloc (GIMPLE_TRANSACTION, 0));
1157 gimple_transaction_set_body (p, body);
1158 gimple_transaction_set_label_norm (p, 0);
1159 gimple_transaction_set_label_uninst (p, 0);
1160 gimple_transaction_set_label_over (p, 0);
1161 return p;
1164 #if defined ENABLE_GIMPLE_CHECKING
1165 /* Complain of a gimple type mismatch and die. */
1167 void
1168 gimple_check_failed (const gimple *gs, const char *file, int line,
1169 const char *function, enum gimple_code code,
1170 enum tree_code subcode)
1172 internal_error ("gimple check: expected %s(%s), have %s(%s) in %s, at %s:%d",
1173 gimple_code_name[code],
1174 get_tree_code_name (subcode),
1175 gimple_code_name[gimple_code (gs)],
1176 gs->subcode > 0
1177 ? get_tree_code_name ((enum tree_code) gs->subcode)
1178 : "",
1179 function, trim_filename (file), line);
1181 #endif /* ENABLE_GIMPLE_CHECKING */
1184 /* Link gimple statement GS to the end of the sequence *SEQ_P. If
1185 *SEQ_P is NULL, a new sequence is allocated. */
1187 void
1188 gimple_seq_add_stmt (gimple_seq *seq_p, gimple *gs)
1190 gimple_stmt_iterator si;
1191 if (gs == NULL)
1192 return;
1194 si = gsi_last (*seq_p);
1195 gsi_insert_after (&si, gs, GSI_NEW_STMT);
1198 /* Link gimple statement GS to the end of the sequence *SEQ_P. If
1199 *SEQ_P is NULL, a new sequence is allocated. This function is
1200 similar to gimple_seq_add_stmt, but does not scan the operands.
1201 During gimplification, we need to manipulate statement sequences
1202 before the def/use vectors have been constructed. */
1204 void
1205 gimple_seq_add_stmt_without_update (gimple_seq *seq_p, gimple *gs)
1207 gimple_stmt_iterator si;
1209 if (gs == NULL)
1210 return;
1212 si = gsi_last (*seq_p);
1213 gsi_insert_after_without_update (&si, gs, GSI_NEW_STMT);
1216 /* Append sequence SRC to the end of sequence *DST_P. If *DST_P is
1217 NULL, a new sequence is allocated. */
1219 void
1220 gimple_seq_add_seq (gimple_seq *dst_p, gimple_seq src)
1222 gimple_stmt_iterator si;
1223 if (src == NULL)
1224 return;
1226 si = gsi_last (*dst_p);
1227 gsi_insert_seq_after (&si, src, GSI_NEW_STMT);
1230 /* Append sequence SRC to the end of sequence *DST_P. If *DST_P is
1231 NULL, a new sequence is allocated. This function is
1232 similar to gimple_seq_add_seq, but does not scan the operands. */
1234 void
1235 gimple_seq_add_seq_without_update (gimple_seq *dst_p, gimple_seq src)
1237 gimple_stmt_iterator si;
1238 if (src == NULL)
1239 return;
1241 si = gsi_last (*dst_p);
1242 gsi_insert_seq_after_without_update (&si, src, GSI_NEW_STMT);
1245 /* Determine whether to assign a location to the statement GS. */
1247 static bool
1248 should_carry_location_p (gimple *gs)
1250 /* Don't emit a line note for a label. We particularly don't want to
1251 emit one for the break label, since it doesn't actually correspond
1252 to the beginning of the loop/switch. */
1253 if (gimple_code (gs) == GIMPLE_LABEL)
1254 return false;
1256 return true;
1259 /* Set the location for gimple statement GS to LOCATION. */
1261 static void
1262 annotate_one_with_location (gimple *gs, location_t location)
1264 if (!gimple_has_location (gs)
1265 && !gimple_do_not_emit_location_p (gs)
1266 && should_carry_location_p (gs))
1267 gimple_set_location (gs, location);
1270 /* Set LOCATION for all the statements after iterator GSI in sequence
1271 SEQ. If GSI is pointing to the end of the sequence, start with the
1272 first statement in SEQ. */
1274 void
1275 annotate_all_with_location_after (gimple_seq seq, gimple_stmt_iterator gsi,
1276 location_t location)
1278 if (gsi_end_p (gsi))
1279 gsi = gsi_start (seq);
1280 else
1281 gsi_next (&gsi);
1283 for (; !gsi_end_p (gsi); gsi_next (&gsi))
1284 annotate_one_with_location (gsi_stmt (gsi), location);
1287 /* Set the location for all the statements in a sequence STMT_P to LOCATION. */
1289 void
1290 annotate_all_with_location (gimple_seq stmt_p, location_t location)
1292 gimple_stmt_iterator i;
1294 if (gimple_seq_empty_p (stmt_p))
1295 return;
1297 for (i = gsi_start (stmt_p); !gsi_end_p (i); gsi_next (&i))
1299 gimple *gs = gsi_stmt (i);
1300 annotate_one_with_location (gs, location);
1304 /* Helper function of empty_body_p. Return true if STMT is an empty
1305 statement. */
1307 static bool
1308 empty_stmt_p (gimple *stmt)
1310 if (gimple_code (stmt) == GIMPLE_NOP)
1311 return true;
1312 if (gbind *bind_stmt = dyn_cast <gbind *> (stmt))
1313 return empty_body_p (gimple_bind_body (bind_stmt));
1314 return false;
1318 /* Return true if BODY contains nothing but empty statements. */
1320 bool
1321 empty_body_p (gimple_seq body)
1323 gimple_stmt_iterator i;
1325 if (gimple_seq_empty_p (body))
1326 return true;
1327 for (i = gsi_start (body); !gsi_end_p (i); gsi_next (&i))
1328 if (!empty_stmt_p (gsi_stmt (i))
1329 && !is_gimple_debug (gsi_stmt (i)))
1330 return false;
1332 return true;
1336 /* Perform a deep copy of sequence SRC and return the result. */
1338 gimple_seq
1339 gimple_seq_copy (gimple_seq src)
1341 gimple_stmt_iterator gsi;
1342 gimple_seq new_seq = NULL;
1343 gimple *stmt;
1345 for (gsi = gsi_start (src); !gsi_end_p (gsi); gsi_next (&gsi))
1347 stmt = gimple_copy (gsi_stmt (gsi));
1348 gimple_seq_add_stmt (&new_seq, stmt);
1351 return new_seq;
1356 /* Return true if calls C1 and C2 are known to go to the same function. */
1358 bool
1359 gimple_call_same_target_p (const gimple *c1, const gimple *c2)
1361 if (gimple_call_internal_p (c1))
1362 return (gimple_call_internal_p (c2)
1363 && gimple_call_internal_fn (c1) == gimple_call_internal_fn (c2)
1364 && (!gimple_call_internal_unique_p (as_a <const gcall *> (c1))
1365 || c1 == c2));
1366 else
1367 return (gimple_call_fn (c1) == gimple_call_fn (c2)
1368 || (gimple_call_fndecl (c1)
1369 && gimple_call_fndecl (c1) == gimple_call_fndecl (c2)));
1372 /* Detect flags from a GIMPLE_CALL. This is just like
1373 call_expr_flags, but for gimple tuples. */
1376 gimple_call_flags (const gimple *stmt)
1378 int flags;
1379 tree decl = gimple_call_fndecl (stmt);
1381 if (decl)
1382 flags = flags_from_decl_or_type (decl);
1383 else if (gimple_call_internal_p (stmt))
1384 flags = internal_fn_flags (gimple_call_internal_fn (stmt));
1385 else
1386 flags = flags_from_decl_or_type (gimple_call_fntype (stmt));
1388 if (stmt->subcode & GF_CALL_NOTHROW)
1389 flags |= ECF_NOTHROW;
1391 if (stmt->subcode & GF_CALL_BY_DESCRIPTOR)
1392 flags |= ECF_BY_DESCRIPTOR;
1394 return flags;
1397 /* Return the "fn spec" string for call STMT. */
1399 static const_tree
1400 gimple_call_fnspec (const gcall *stmt)
1402 tree type, attr;
1404 if (gimple_call_internal_p (stmt))
1405 return internal_fn_fnspec (gimple_call_internal_fn (stmt));
1407 type = gimple_call_fntype (stmt);
1408 if (!type)
1409 return NULL_TREE;
1411 attr = lookup_attribute ("fn spec", TYPE_ATTRIBUTES (type));
1412 if (!attr)
1413 return NULL_TREE;
1415 return TREE_VALUE (TREE_VALUE (attr));
1418 /* Detects argument flags for argument number ARG on call STMT. */
1421 gimple_call_arg_flags (const gcall *stmt, unsigned arg)
1423 const_tree attr = gimple_call_fnspec (stmt);
1425 if (!attr || 1 + arg >= (unsigned) TREE_STRING_LENGTH (attr))
1426 return 0;
1428 switch (TREE_STRING_POINTER (attr)[1 + arg])
1430 case 'x':
1431 case 'X':
1432 return EAF_UNUSED;
1434 case 'R':
1435 return EAF_DIRECT | EAF_NOCLOBBER | EAF_NOESCAPE;
1437 case 'r':
1438 return EAF_NOCLOBBER | EAF_NOESCAPE;
1440 case 'W':
1441 return EAF_DIRECT | EAF_NOESCAPE;
1443 case 'w':
1444 return EAF_NOESCAPE;
1446 case '.':
1447 default:
1448 return 0;
1452 /* Detects return flags for the call STMT. */
1455 gimple_call_return_flags (const gcall *stmt)
1457 const_tree attr;
1459 if (gimple_call_flags (stmt) & ECF_MALLOC)
1460 return ERF_NOALIAS;
1462 attr = gimple_call_fnspec (stmt);
1463 if (!attr || TREE_STRING_LENGTH (attr) < 1)
1464 return 0;
1466 switch (TREE_STRING_POINTER (attr)[0])
1468 case '1':
1469 case '2':
1470 case '3':
1471 case '4':
1472 return ERF_RETURNS_ARG | (TREE_STRING_POINTER (attr)[0] - '1');
1474 case 'm':
1475 return ERF_NOALIAS;
1477 case '.':
1478 default:
1479 return 0;
1484 /* Return true if GS is a copy assignment. */
1486 bool
1487 gimple_assign_copy_p (gimple *gs)
1489 return (gimple_assign_single_p (gs)
1490 && is_gimple_val (gimple_op (gs, 1)));
1494 /* Return true if GS is a SSA_NAME copy assignment. */
1496 bool
1497 gimple_assign_ssa_name_copy_p (gimple *gs)
1499 return (gimple_assign_single_p (gs)
1500 && TREE_CODE (gimple_assign_lhs (gs)) == SSA_NAME
1501 && TREE_CODE (gimple_assign_rhs1 (gs)) == SSA_NAME);
1505 /* Return true if GS is an assignment with a unary RHS, but the
1506 operator has no effect on the assigned value. The logic is adapted
1507 from STRIP_NOPS. This predicate is intended to be used in tuplifying
1508 instances in which STRIP_NOPS was previously applied to the RHS of
1509 an assignment.
1511 NOTE: In the use cases that led to the creation of this function
1512 and of gimple_assign_single_p, it is typical to test for either
1513 condition and to proceed in the same manner. In each case, the
1514 assigned value is represented by the single RHS operand of the
1515 assignment. I suspect there may be cases where gimple_assign_copy_p,
1516 gimple_assign_single_p, or equivalent logic is used where a similar
1517 treatment of unary NOPs is appropriate. */
1519 bool
1520 gimple_assign_unary_nop_p (gimple *gs)
1522 return (is_gimple_assign (gs)
1523 && (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (gs))
1524 || gimple_assign_rhs_code (gs) == NON_LVALUE_EXPR)
1525 && gimple_assign_rhs1 (gs) != error_mark_node
1526 && (TYPE_MODE (TREE_TYPE (gimple_assign_lhs (gs)))
1527 == TYPE_MODE (TREE_TYPE (gimple_assign_rhs1 (gs)))));
1530 /* Set BB to be the basic block holding G. */
1532 void
1533 gimple_set_bb (gimple *stmt, basic_block bb)
1535 stmt->bb = bb;
1537 if (gimple_code (stmt) != GIMPLE_LABEL)
1538 return;
1540 /* If the statement is a label, add the label to block-to-labels map
1541 so that we can speed up edge creation for GIMPLE_GOTOs. */
1542 if (cfun->cfg)
1544 tree t;
1545 int uid;
1547 t = gimple_label_label (as_a <glabel *> (stmt));
1548 uid = LABEL_DECL_UID (t);
1549 if (uid == -1)
1551 unsigned old_len =
1552 vec_safe_length (label_to_block_map_for_fn (cfun));
1553 LABEL_DECL_UID (t) = uid = cfun->cfg->last_label_uid++;
1554 if (old_len <= (unsigned) uid)
1556 unsigned new_len = 3 * uid / 2 + 1;
1558 vec_safe_grow_cleared (label_to_block_map_for_fn (cfun),
1559 new_len);
1563 (*label_to_block_map_for_fn (cfun))[uid] = bb;
1568 /* Modify the RHS of the assignment pointed-to by GSI using the
1569 operands in the expression tree EXPR.
1571 NOTE: The statement pointed-to by GSI may be reallocated if it
1572 did not have enough operand slots.
1574 This function is useful to convert an existing tree expression into
1575 the flat representation used for the RHS of a GIMPLE assignment.
1576 It will reallocate memory as needed to expand or shrink the number
1577 of operand slots needed to represent EXPR.
1579 NOTE: If you find yourself building a tree and then calling this
1580 function, you are most certainly doing it the slow way. It is much
1581 better to build a new assignment or to use the function
1582 gimple_assign_set_rhs_with_ops, which does not require an
1583 expression tree to be built. */
1585 void
1586 gimple_assign_set_rhs_from_tree (gimple_stmt_iterator *gsi, tree expr)
1588 enum tree_code subcode;
1589 tree op1, op2, op3;
1591 extract_ops_from_tree (expr, &subcode, &op1, &op2, &op3);
1592 gimple_assign_set_rhs_with_ops (gsi, subcode, op1, op2, op3);
1596 /* Set the RHS of assignment statement pointed-to by GSI to CODE with
1597 operands OP1, OP2 and OP3.
1599 NOTE: The statement pointed-to by GSI may be reallocated if it
1600 did not have enough operand slots. */
1602 void
1603 gimple_assign_set_rhs_with_ops (gimple_stmt_iterator *gsi, enum tree_code code,
1604 tree op1, tree op2, tree op3)
1606 unsigned new_rhs_ops = get_gimple_rhs_num_ops (code);
1607 gimple *stmt = gsi_stmt (*gsi);
1609 /* If the new CODE needs more operands, allocate a new statement. */
1610 if (gimple_num_ops (stmt) < new_rhs_ops + 1)
1612 tree lhs = gimple_assign_lhs (stmt);
1613 gimple *new_stmt = gimple_alloc (gimple_code (stmt), new_rhs_ops + 1);
1614 memcpy (new_stmt, stmt, gimple_size (gimple_code (stmt)));
1615 gimple_init_singleton (new_stmt);
1616 gsi_replace (gsi, new_stmt, true);
1617 stmt = new_stmt;
1619 /* The LHS needs to be reset as this also changes the SSA name
1620 on the LHS. */
1621 gimple_assign_set_lhs (stmt, lhs);
1624 gimple_set_num_ops (stmt, new_rhs_ops + 1);
1625 gimple_set_subcode (stmt, code);
1626 gimple_assign_set_rhs1 (stmt, op1);
1627 if (new_rhs_ops > 1)
1628 gimple_assign_set_rhs2 (stmt, op2);
1629 if (new_rhs_ops > 2)
1630 gimple_assign_set_rhs3 (stmt, op3);
1634 /* Return the LHS of a statement that performs an assignment,
1635 either a GIMPLE_ASSIGN or a GIMPLE_CALL. Returns NULL_TREE
1636 for a call to a function that returns no value, or for a
1637 statement other than an assignment or a call. */
1639 tree
1640 gimple_get_lhs (const gimple *stmt)
1642 enum gimple_code code = gimple_code (stmt);
1644 if (code == GIMPLE_ASSIGN)
1645 return gimple_assign_lhs (stmt);
1646 else if (code == GIMPLE_CALL)
1647 return gimple_call_lhs (stmt);
1648 else
1649 return NULL_TREE;
1653 /* Set the LHS of a statement that performs an assignment,
1654 either a GIMPLE_ASSIGN or a GIMPLE_CALL. */
1656 void
1657 gimple_set_lhs (gimple *stmt, tree lhs)
1659 enum gimple_code code = gimple_code (stmt);
1661 if (code == GIMPLE_ASSIGN)
1662 gimple_assign_set_lhs (stmt, lhs);
1663 else if (code == GIMPLE_CALL)
1664 gimple_call_set_lhs (stmt, lhs);
1665 else
1666 gcc_unreachable ();
1670 /* Return a deep copy of statement STMT. All the operands from STMT
1671 are reallocated and copied using unshare_expr. The DEF, USE, VDEF
1672 and VUSE operand arrays are set to empty in the new copy. The new
1673 copy isn't part of any sequence. */
1675 gimple *
1676 gimple_copy (gimple *stmt)
1678 enum gimple_code code = gimple_code (stmt);
1679 unsigned num_ops = gimple_num_ops (stmt);
1680 gimple *copy = gimple_alloc (code, num_ops);
1681 unsigned i;
1683 /* Shallow copy all the fields from STMT. */
1684 memcpy (copy, stmt, gimple_size (code));
1685 gimple_init_singleton (copy);
1687 /* If STMT has sub-statements, deep-copy them as well. */
1688 if (gimple_has_substatements (stmt))
1690 gimple_seq new_seq;
1691 tree t;
1693 switch (gimple_code (stmt))
1695 case GIMPLE_BIND:
1697 gbind *bind_stmt = as_a <gbind *> (stmt);
1698 gbind *bind_copy = as_a <gbind *> (copy);
1699 new_seq = gimple_seq_copy (gimple_bind_body (bind_stmt));
1700 gimple_bind_set_body (bind_copy, new_seq);
1701 gimple_bind_set_vars (bind_copy,
1702 unshare_expr (gimple_bind_vars (bind_stmt)));
1703 gimple_bind_set_block (bind_copy, gimple_bind_block (bind_stmt));
1705 break;
1707 case GIMPLE_CATCH:
1709 gcatch *catch_stmt = as_a <gcatch *> (stmt);
1710 gcatch *catch_copy = as_a <gcatch *> (copy);
1711 new_seq = gimple_seq_copy (gimple_catch_handler (catch_stmt));
1712 gimple_catch_set_handler (catch_copy, new_seq);
1713 t = unshare_expr (gimple_catch_types (catch_stmt));
1714 gimple_catch_set_types (catch_copy, t);
1716 break;
1718 case GIMPLE_EH_FILTER:
1720 geh_filter *eh_filter_stmt = as_a <geh_filter *> (stmt);
1721 geh_filter *eh_filter_copy = as_a <geh_filter *> (copy);
1722 new_seq
1723 = gimple_seq_copy (gimple_eh_filter_failure (eh_filter_stmt));
1724 gimple_eh_filter_set_failure (eh_filter_copy, new_seq);
1725 t = unshare_expr (gimple_eh_filter_types (eh_filter_stmt));
1726 gimple_eh_filter_set_types (eh_filter_copy, t);
1728 break;
1730 case GIMPLE_EH_ELSE:
1732 geh_else *eh_else_stmt = as_a <geh_else *> (stmt);
1733 geh_else *eh_else_copy = as_a <geh_else *> (copy);
1734 new_seq = gimple_seq_copy (gimple_eh_else_n_body (eh_else_stmt));
1735 gimple_eh_else_set_n_body (eh_else_copy, new_seq);
1736 new_seq = gimple_seq_copy (gimple_eh_else_e_body (eh_else_stmt));
1737 gimple_eh_else_set_e_body (eh_else_copy, new_seq);
1739 break;
1741 case GIMPLE_TRY:
1743 gtry *try_stmt = as_a <gtry *> (stmt);
1744 gtry *try_copy = as_a <gtry *> (copy);
1745 new_seq = gimple_seq_copy (gimple_try_eval (try_stmt));
1746 gimple_try_set_eval (try_copy, new_seq);
1747 new_seq = gimple_seq_copy (gimple_try_cleanup (try_stmt));
1748 gimple_try_set_cleanup (try_copy, new_seq);
1750 break;
1752 case GIMPLE_OMP_FOR:
1753 new_seq = gimple_seq_copy (gimple_omp_for_pre_body (stmt));
1754 gimple_omp_for_set_pre_body (copy, new_seq);
1755 t = unshare_expr (gimple_omp_for_clauses (stmt));
1756 gimple_omp_for_set_clauses (copy, t);
1758 gomp_for *omp_for_copy = as_a <gomp_for *> (copy);
1759 omp_for_copy->iter = ggc_vec_alloc<gimple_omp_for_iter>
1760 ( gimple_omp_for_collapse (stmt));
1762 for (i = 0; i < gimple_omp_for_collapse (stmt); i++)
1764 gimple_omp_for_set_cond (copy, i,
1765 gimple_omp_for_cond (stmt, i));
1766 gimple_omp_for_set_index (copy, i,
1767 gimple_omp_for_index (stmt, i));
1768 t = unshare_expr (gimple_omp_for_initial (stmt, i));
1769 gimple_omp_for_set_initial (copy, i, t);
1770 t = unshare_expr (gimple_omp_for_final (stmt, i));
1771 gimple_omp_for_set_final (copy, i, t);
1772 t = unshare_expr (gimple_omp_for_incr (stmt, i));
1773 gimple_omp_for_set_incr (copy, i, t);
1775 goto copy_omp_body;
1777 case GIMPLE_OMP_PARALLEL:
1779 gomp_parallel *omp_par_stmt = as_a <gomp_parallel *> (stmt);
1780 gomp_parallel *omp_par_copy = as_a <gomp_parallel *> (copy);
1781 t = unshare_expr (gimple_omp_parallel_clauses (omp_par_stmt));
1782 gimple_omp_parallel_set_clauses (omp_par_copy, t);
1783 t = unshare_expr (gimple_omp_parallel_child_fn (omp_par_stmt));
1784 gimple_omp_parallel_set_child_fn (omp_par_copy, t);
1785 t = unshare_expr (gimple_omp_parallel_data_arg (omp_par_stmt));
1786 gimple_omp_parallel_set_data_arg (omp_par_copy, t);
1788 goto copy_omp_body;
1790 case GIMPLE_OMP_TASK:
1791 t = unshare_expr (gimple_omp_task_clauses (stmt));
1792 gimple_omp_task_set_clauses (copy, t);
1793 t = unshare_expr (gimple_omp_task_child_fn (stmt));
1794 gimple_omp_task_set_child_fn (copy, t);
1795 t = unshare_expr (gimple_omp_task_data_arg (stmt));
1796 gimple_omp_task_set_data_arg (copy, t);
1797 t = unshare_expr (gimple_omp_task_copy_fn (stmt));
1798 gimple_omp_task_set_copy_fn (copy, t);
1799 t = unshare_expr (gimple_omp_task_arg_size (stmt));
1800 gimple_omp_task_set_arg_size (copy, t);
1801 t = unshare_expr (gimple_omp_task_arg_align (stmt));
1802 gimple_omp_task_set_arg_align (copy, t);
1803 goto copy_omp_body;
1805 case GIMPLE_OMP_CRITICAL:
1806 t = unshare_expr (gimple_omp_critical_name
1807 (as_a <gomp_critical *> (stmt)));
1808 gimple_omp_critical_set_name (as_a <gomp_critical *> (copy), t);
1809 t = unshare_expr (gimple_omp_critical_clauses
1810 (as_a <gomp_critical *> (stmt)));
1811 gimple_omp_critical_set_clauses (as_a <gomp_critical *> (copy), t);
1812 goto copy_omp_body;
1814 case GIMPLE_OMP_ORDERED:
1815 t = unshare_expr (gimple_omp_ordered_clauses
1816 (as_a <gomp_ordered *> (stmt)));
1817 gimple_omp_ordered_set_clauses (as_a <gomp_ordered *> (copy), t);
1818 goto copy_omp_body;
1820 case GIMPLE_OMP_SECTIONS:
1821 t = unshare_expr (gimple_omp_sections_clauses (stmt));
1822 gimple_omp_sections_set_clauses (copy, t);
1823 t = unshare_expr (gimple_omp_sections_control (stmt));
1824 gimple_omp_sections_set_control (copy, t);
1825 /* FALLTHRU */
1827 case GIMPLE_OMP_SINGLE:
1828 case GIMPLE_OMP_TARGET:
1829 case GIMPLE_OMP_TEAMS:
1830 case GIMPLE_OMP_SECTION:
1831 case GIMPLE_OMP_MASTER:
1832 case GIMPLE_OMP_TASKGROUP:
1833 case GIMPLE_OMP_GRID_BODY:
1834 copy_omp_body:
1835 new_seq = gimple_seq_copy (gimple_omp_body (stmt));
1836 gimple_omp_set_body (copy, new_seq);
1837 break;
1839 case GIMPLE_TRANSACTION:
1840 new_seq = gimple_seq_copy (gimple_transaction_body (
1841 as_a <gtransaction *> (stmt)));
1842 gimple_transaction_set_body (as_a <gtransaction *> (copy),
1843 new_seq);
1844 break;
1846 case GIMPLE_WITH_CLEANUP_EXPR:
1847 new_seq = gimple_seq_copy (gimple_wce_cleanup (stmt));
1848 gimple_wce_set_cleanup (copy, new_seq);
1849 break;
1851 default:
1852 gcc_unreachable ();
1856 /* Make copy of operands. */
1857 for (i = 0; i < num_ops; i++)
1858 gimple_set_op (copy, i, unshare_expr (gimple_op (stmt, i)));
1860 if (gimple_has_mem_ops (stmt))
1862 gimple_set_vdef (copy, gimple_vdef (stmt));
1863 gimple_set_vuse (copy, gimple_vuse (stmt));
1866 /* Clear out SSA operand vectors on COPY. */
1867 if (gimple_has_ops (stmt))
1869 gimple_set_use_ops (copy, NULL);
1871 /* SSA operands need to be updated. */
1872 gimple_set_modified (copy, true);
1875 return copy;
1879 /* Return true if statement S has side-effects. We consider a
1880 statement to have side effects if:
1882 - It is a GIMPLE_CALL not marked with ECF_PURE or ECF_CONST.
1883 - Any of its operands are marked TREE_THIS_VOLATILE or TREE_SIDE_EFFECTS. */
1885 bool
1886 gimple_has_side_effects (const gimple *s)
1888 if (is_gimple_debug (s))
1889 return false;
1891 /* We don't have to scan the arguments to check for
1892 volatile arguments, though, at present, we still
1893 do a scan to check for TREE_SIDE_EFFECTS. */
1894 if (gimple_has_volatile_ops (s))
1895 return true;
1897 if (gimple_code (s) == GIMPLE_ASM
1898 && gimple_asm_volatile_p (as_a <const gasm *> (s)))
1899 return true;
1901 if (is_gimple_call (s))
1903 int flags = gimple_call_flags (s);
1905 /* An infinite loop is considered a side effect. */
1906 if (!(flags & (ECF_CONST | ECF_PURE))
1907 || (flags & ECF_LOOPING_CONST_OR_PURE))
1908 return true;
1910 return false;
1913 return false;
1916 /* Helper for gimple_could_trap_p and gimple_assign_rhs_could_trap_p.
1917 Return true if S can trap. When INCLUDE_MEM is true, check whether
1918 the memory operations could trap. When INCLUDE_STORES is true and
1919 S is a GIMPLE_ASSIGN, the LHS of the assignment is also checked. */
1921 bool
1922 gimple_could_trap_p_1 (gimple *s, bool include_mem, bool include_stores)
1924 tree t, div = NULL_TREE;
1925 enum tree_code op;
1927 if (include_mem)
1929 unsigned i, start = (is_gimple_assign (s) && !include_stores) ? 1 : 0;
1931 for (i = start; i < gimple_num_ops (s); i++)
1932 if (tree_could_trap_p (gimple_op (s, i)))
1933 return true;
1936 switch (gimple_code (s))
1938 case GIMPLE_ASM:
1939 return gimple_asm_volatile_p (as_a <gasm *> (s));
1941 case GIMPLE_CALL:
1942 t = gimple_call_fndecl (s);
1943 /* Assume that calls to weak functions may trap. */
1944 if (!t || !DECL_P (t) || DECL_WEAK (t))
1945 return true;
1946 return false;
1948 case GIMPLE_ASSIGN:
1949 t = gimple_expr_type (s);
1950 op = gimple_assign_rhs_code (s);
1951 if (get_gimple_rhs_class (op) == GIMPLE_BINARY_RHS)
1952 div = gimple_assign_rhs2 (s);
1953 return (operation_could_trap_p (op, FLOAT_TYPE_P (t),
1954 (INTEGRAL_TYPE_P (t)
1955 && TYPE_OVERFLOW_TRAPS (t)),
1956 div));
1958 case GIMPLE_COND:
1959 t = TREE_TYPE (gimple_cond_lhs (s));
1960 return operation_could_trap_p (gimple_cond_code (s),
1961 FLOAT_TYPE_P (t), false, NULL_TREE);
1963 default:
1964 break;
1967 return false;
1970 /* Return true if statement S can trap. */
1972 bool
1973 gimple_could_trap_p (gimple *s)
1975 return gimple_could_trap_p_1 (s, true, true);
1978 /* Return true if RHS of a GIMPLE_ASSIGN S can trap. */
1980 bool
1981 gimple_assign_rhs_could_trap_p (gimple *s)
1983 gcc_assert (is_gimple_assign (s));
1984 return gimple_could_trap_p_1 (s, true, false);
1988 /* Print debugging information for gimple stmts generated. */
1990 void
1991 dump_gimple_statistics (void)
1993 int i, total_tuples = 0, total_bytes = 0;
1995 if (! GATHER_STATISTICS)
1997 fprintf (stderr, "No gimple statistics\n");
1998 return;
2001 fprintf (stderr, "\nGIMPLE statements\n");
2002 fprintf (stderr, "Kind Stmts Bytes\n");
2003 fprintf (stderr, "---------------------------------------\n");
2004 for (i = 0; i < (int) gimple_alloc_kind_all; ++i)
2006 fprintf (stderr, "%-20s %7d %10d\n", gimple_alloc_kind_names[i],
2007 gimple_alloc_counts[i], gimple_alloc_sizes[i]);
2008 total_tuples += gimple_alloc_counts[i];
2009 total_bytes += gimple_alloc_sizes[i];
2011 fprintf (stderr, "---------------------------------------\n");
2012 fprintf (stderr, "%-20s %7d %10d\n", "Total", total_tuples, total_bytes);
2013 fprintf (stderr, "---------------------------------------\n");
2017 /* Return the number of operands needed on the RHS of a GIMPLE
2018 assignment for an expression with tree code CODE. */
2020 unsigned
2021 get_gimple_rhs_num_ops (enum tree_code code)
2023 enum gimple_rhs_class rhs_class = get_gimple_rhs_class (code);
2025 if (rhs_class == GIMPLE_UNARY_RHS || rhs_class == GIMPLE_SINGLE_RHS)
2026 return 1;
2027 else if (rhs_class == GIMPLE_BINARY_RHS)
2028 return 2;
2029 else if (rhs_class == GIMPLE_TERNARY_RHS)
2030 return 3;
2031 else
2032 gcc_unreachable ();
2035 #define DEFTREECODE(SYM, STRING, TYPE, NARGS) \
2036 (unsigned char) \
2037 ((TYPE) == tcc_unary ? GIMPLE_UNARY_RHS \
2038 : ((TYPE) == tcc_binary \
2039 || (TYPE) == tcc_comparison) ? GIMPLE_BINARY_RHS \
2040 : ((TYPE) == tcc_constant \
2041 || (TYPE) == tcc_declaration \
2042 || (TYPE) == tcc_reference) ? GIMPLE_SINGLE_RHS \
2043 : ((SYM) == TRUTH_AND_EXPR \
2044 || (SYM) == TRUTH_OR_EXPR \
2045 || (SYM) == TRUTH_XOR_EXPR) ? GIMPLE_BINARY_RHS \
2046 : (SYM) == TRUTH_NOT_EXPR ? GIMPLE_UNARY_RHS \
2047 : ((SYM) == COND_EXPR \
2048 || (SYM) == WIDEN_MULT_PLUS_EXPR \
2049 || (SYM) == WIDEN_MULT_MINUS_EXPR \
2050 || (SYM) == DOT_PROD_EXPR \
2051 || (SYM) == SAD_EXPR \
2052 || (SYM) == REALIGN_LOAD_EXPR \
2053 || (SYM) == VEC_COND_EXPR \
2054 || (SYM) == VEC_PERM_EXPR \
2055 || (SYM) == BIT_INSERT_EXPR \
2056 || (SYM) == FMA_EXPR) ? GIMPLE_TERNARY_RHS \
2057 : ((SYM) == CONSTRUCTOR \
2058 || (SYM) == OBJ_TYPE_REF \
2059 || (SYM) == ASSERT_EXPR \
2060 || (SYM) == ADDR_EXPR \
2061 || (SYM) == WITH_SIZE_EXPR \
2062 || (SYM) == SSA_NAME) ? GIMPLE_SINGLE_RHS \
2063 : GIMPLE_INVALID_RHS),
2064 #define END_OF_BASE_TREE_CODES (unsigned char) GIMPLE_INVALID_RHS,
2066 const unsigned char gimple_rhs_class_table[] = {
2067 #include "all-tree.def"
2070 #undef DEFTREECODE
2071 #undef END_OF_BASE_TREE_CODES
2073 /* Canonicalize a tree T for use in a COND_EXPR as conditional. Returns
2074 a canonicalized tree that is valid for a COND_EXPR or NULL_TREE, if
2075 we failed to create one. */
2077 tree
2078 canonicalize_cond_expr_cond (tree t)
2080 /* Strip conversions around boolean operations. */
2081 if (CONVERT_EXPR_P (t)
2082 && (truth_value_p (TREE_CODE (TREE_OPERAND (t, 0)))
2083 || TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0)))
2084 == BOOLEAN_TYPE))
2085 t = TREE_OPERAND (t, 0);
2087 /* For !x use x == 0. */
2088 if (TREE_CODE (t) == TRUTH_NOT_EXPR)
2090 tree top0 = TREE_OPERAND (t, 0);
2091 t = build2 (EQ_EXPR, TREE_TYPE (t),
2092 top0, build_int_cst (TREE_TYPE (top0), 0));
2094 /* For cmp ? 1 : 0 use cmp. */
2095 else if (TREE_CODE (t) == COND_EXPR
2096 && COMPARISON_CLASS_P (TREE_OPERAND (t, 0))
2097 && integer_onep (TREE_OPERAND (t, 1))
2098 && integer_zerop (TREE_OPERAND (t, 2)))
2100 tree top0 = TREE_OPERAND (t, 0);
2101 t = build2 (TREE_CODE (top0), TREE_TYPE (t),
2102 TREE_OPERAND (top0, 0), TREE_OPERAND (top0, 1));
2104 /* For x ^ y use x != y. */
2105 else if (TREE_CODE (t) == BIT_XOR_EXPR)
2106 t = build2 (NE_EXPR, TREE_TYPE (t),
2107 TREE_OPERAND (t, 0), TREE_OPERAND (t, 1));
2109 if (is_gimple_condexpr (t))
2110 return t;
2112 return NULL_TREE;
2115 /* Build a GIMPLE_CALL identical to STMT but skipping the arguments in
2116 the positions marked by the set ARGS_TO_SKIP. */
2118 gcall *
2119 gimple_call_copy_skip_args (gcall *stmt, bitmap args_to_skip)
2121 int i;
2122 int nargs = gimple_call_num_args (stmt);
2123 auto_vec<tree> vargs (nargs);
2124 gcall *new_stmt;
2126 for (i = 0; i < nargs; i++)
2127 if (!bitmap_bit_p (args_to_skip, i))
2128 vargs.quick_push (gimple_call_arg (stmt, i));
2130 if (gimple_call_internal_p (stmt))
2131 new_stmt = gimple_build_call_internal_vec (gimple_call_internal_fn (stmt),
2132 vargs);
2133 else
2134 new_stmt = gimple_build_call_vec (gimple_call_fn (stmt), vargs);
2136 if (gimple_call_lhs (stmt))
2137 gimple_call_set_lhs (new_stmt, gimple_call_lhs (stmt));
2139 gimple_set_vuse (new_stmt, gimple_vuse (stmt));
2140 gimple_set_vdef (new_stmt, gimple_vdef (stmt));
2142 if (gimple_has_location (stmt))
2143 gimple_set_location (new_stmt, gimple_location (stmt));
2144 gimple_call_copy_flags (new_stmt, stmt);
2145 gimple_call_set_chain (new_stmt, gimple_call_chain (stmt));
2147 gimple_set_modified (new_stmt, true);
2149 return new_stmt;
2154 /* Return true if the field decls F1 and F2 are at the same offset.
2156 This is intended to be used on GIMPLE types only. */
2158 bool
2159 gimple_compare_field_offset (tree f1, tree f2)
2161 if (DECL_OFFSET_ALIGN (f1) == DECL_OFFSET_ALIGN (f2))
2163 tree offset1 = DECL_FIELD_OFFSET (f1);
2164 tree offset2 = DECL_FIELD_OFFSET (f2);
2165 return ((offset1 == offset2
2166 /* Once gimplification is done, self-referential offsets are
2167 instantiated as operand #2 of the COMPONENT_REF built for
2168 each access and reset. Therefore, they are not relevant
2169 anymore and fields are interchangeable provided that they
2170 represent the same access. */
2171 || (TREE_CODE (offset1) == PLACEHOLDER_EXPR
2172 && TREE_CODE (offset2) == PLACEHOLDER_EXPR
2173 && (DECL_SIZE (f1) == DECL_SIZE (f2)
2174 || (TREE_CODE (DECL_SIZE (f1)) == PLACEHOLDER_EXPR
2175 && TREE_CODE (DECL_SIZE (f2)) == PLACEHOLDER_EXPR)
2176 || operand_equal_p (DECL_SIZE (f1), DECL_SIZE (f2), 0))
2177 && DECL_ALIGN (f1) == DECL_ALIGN (f2))
2178 || operand_equal_p (offset1, offset2, 0))
2179 && tree_int_cst_equal (DECL_FIELD_BIT_OFFSET (f1),
2180 DECL_FIELD_BIT_OFFSET (f2)));
2183 /* Fortran and C do not always agree on what DECL_OFFSET_ALIGN
2184 should be, so handle differing ones specially by decomposing
2185 the offset into a byte and bit offset manually. */
2186 if (tree_fits_shwi_p (DECL_FIELD_OFFSET (f1))
2187 && tree_fits_shwi_p (DECL_FIELD_OFFSET (f2)))
2189 unsigned HOST_WIDE_INT byte_offset1, byte_offset2;
2190 unsigned HOST_WIDE_INT bit_offset1, bit_offset2;
2191 bit_offset1 = TREE_INT_CST_LOW (DECL_FIELD_BIT_OFFSET (f1));
2192 byte_offset1 = (TREE_INT_CST_LOW (DECL_FIELD_OFFSET (f1))
2193 + bit_offset1 / BITS_PER_UNIT);
2194 bit_offset2 = TREE_INT_CST_LOW (DECL_FIELD_BIT_OFFSET (f2));
2195 byte_offset2 = (TREE_INT_CST_LOW (DECL_FIELD_OFFSET (f2))
2196 + bit_offset2 / BITS_PER_UNIT);
2197 if (byte_offset1 != byte_offset2)
2198 return false;
2199 return bit_offset1 % BITS_PER_UNIT == bit_offset2 % BITS_PER_UNIT;
2202 return false;
2206 /* Return a type the same as TYPE except unsigned or
2207 signed according to UNSIGNEDP. */
2209 static tree
2210 gimple_signed_or_unsigned_type (bool unsignedp, tree type)
2212 tree type1;
2213 int i;
2215 type1 = TYPE_MAIN_VARIANT (type);
2216 if (type1 == signed_char_type_node
2217 || type1 == char_type_node
2218 || type1 == unsigned_char_type_node)
2219 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
2220 if (type1 == integer_type_node || type1 == unsigned_type_node)
2221 return unsignedp ? unsigned_type_node : integer_type_node;
2222 if (type1 == short_integer_type_node || type1 == short_unsigned_type_node)
2223 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
2224 if (type1 == long_integer_type_node || type1 == long_unsigned_type_node)
2225 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
2226 if (type1 == long_long_integer_type_node
2227 || type1 == long_long_unsigned_type_node)
2228 return unsignedp
2229 ? long_long_unsigned_type_node
2230 : long_long_integer_type_node;
2232 for (i = 0; i < NUM_INT_N_ENTS; i ++)
2233 if (int_n_enabled_p[i]
2234 && (type1 == int_n_trees[i].unsigned_type
2235 || type1 == int_n_trees[i].signed_type))
2236 return unsignedp
2237 ? int_n_trees[i].unsigned_type
2238 : int_n_trees[i].signed_type;
2240 #if HOST_BITS_PER_WIDE_INT >= 64
2241 if (type1 == intTI_type_node || type1 == unsigned_intTI_type_node)
2242 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
2243 #endif
2244 if (type1 == intDI_type_node || type1 == unsigned_intDI_type_node)
2245 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
2246 if (type1 == intSI_type_node || type1 == unsigned_intSI_type_node)
2247 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
2248 if (type1 == intHI_type_node || type1 == unsigned_intHI_type_node)
2249 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
2250 if (type1 == intQI_type_node || type1 == unsigned_intQI_type_node)
2251 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
2253 #define GIMPLE_FIXED_TYPES(NAME) \
2254 if (type1 == short_ ## NAME ## _type_node \
2255 || type1 == unsigned_short_ ## NAME ## _type_node) \
2256 return unsignedp ? unsigned_short_ ## NAME ## _type_node \
2257 : short_ ## NAME ## _type_node; \
2258 if (type1 == NAME ## _type_node \
2259 || type1 == unsigned_ ## NAME ## _type_node) \
2260 return unsignedp ? unsigned_ ## NAME ## _type_node \
2261 : NAME ## _type_node; \
2262 if (type1 == long_ ## NAME ## _type_node \
2263 || type1 == unsigned_long_ ## NAME ## _type_node) \
2264 return unsignedp ? unsigned_long_ ## NAME ## _type_node \
2265 : long_ ## NAME ## _type_node; \
2266 if (type1 == long_long_ ## NAME ## _type_node \
2267 || type1 == unsigned_long_long_ ## NAME ## _type_node) \
2268 return unsignedp ? unsigned_long_long_ ## NAME ## _type_node \
2269 : long_long_ ## NAME ## _type_node;
2271 #define GIMPLE_FIXED_MODE_TYPES(NAME) \
2272 if (type1 == NAME ## _type_node \
2273 || type1 == u ## NAME ## _type_node) \
2274 return unsignedp ? u ## NAME ## _type_node \
2275 : NAME ## _type_node;
2277 #define GIMPLE_FIXED_TYPES_SAT(NAME) \
2278 if (type1 == sat_ ## short_ ## NAME ## _type_node \
2279 || type1 == sat_ ## unsigned_short_ ## NAME ## _type_node) \
2280 return unsignedp ? sat_ ## unsigned_short_ ## NAME ## _type_node \
2281 : sat_ ## short_ ## NAME ## _type_node; \
2282 if (type1 == sat_ ## NAME ## _type_node \
2283 || type1 == sat_ ## unsigned_ ## NAME ## _type_node) \
2284 return unsignedp ? sat_ ## unsigned_ ## NAME ## _type_node \
2285 : sat_ ## NAME ## _type_node; \
2286 if (type1 == sat_ ## long_ ## NAME ## _type_node \
2287 || type1 == sat_ ## unsigned_long_ ## NAME ## _type_node) \
2288 return unsignedp ? sat_ ## unsigned_long_ ## NAME ## _type_node \
2289 : sat_ ## long_ ## NAME ## _type_node; \
2290 if (type1 == sat_ ## long_long_ ## NAME ## _type_node \
2291 || type1 == sat_ ## unsigned_long_long_ ## NAME ## _type_node) \
2292 return unsignedp ? sat_ ## unsigned_long_long_ ## NAME ## _type_node \
2293 : sat_ ## long_long_ ## NAME ## _type_node;
2295 #define GIMPLE_FIXED_MODE_TYPES_SAT(NAME) \
2296 if (type1 == sat_ ## NAME ## _type_node \
2297 || type1 == sat_ ## u ## NAME ## _type_node) \
2298 return unsignedp ? sat_ ## u ## NAME ## _type_node \
2299 : sat_ ## NAME ## _type_node;
2301 GIMPLE_FIXED_TYPES (fract);
2302 GIMPLE_FIXED_TYPES_SAT (fract);
2303 GIMPLE_FIXED_TYPES (accum);
2304 GIMPLE_FIXED_TYPES_SAT (accum);
2306 GIMPLE_FIXED_MODE_TYPES (qq);
2307 GIMPLE_FIXED_MODE_TYPES (hq);
2308 GIMPLE_FIXED_MODE_TYPES (sq);
2309 GIMPLE_FIXED_MODE_TYPES (dq);
2310 GIMPLE_FIXED_MODE_TYPES (tq);
2311 GIMPLE_FIXED_MODE_TYPES_SAT (qq);
2312 GIMPLE_FIXED_MODE_TYPES_SAT (hq);
2313 GIMPLE_FIXED_MODE_TYPES_SAT (sq);
2314 GIMPLE_FIXED_MODE_TYPES_SAT (dq);
2315 GIMPLE_FIXED_MODE_TYPES_SAT (tq);
2316 GIMPLE_FIXED_MODE_TYPES (ha);
2317 GIMPLE_FIXED_MODE_TYPES (sa);
2318 GIMPLE_FIXED_MODE_TYPES (da);
2319 GIMPLE_FIXED_MODE_TYPES (ta);
2320 GIMPLE_FIXED_MODE_TYPES_SAT (ha);
2321 GIMPLE_FIXED_MODE_TYPES_SAT (sa);
2322 GIMPLE_FIXED_MODE_TYPES_SAT (da);
2323 GIMPLE_FIXED_MODE_TYPES_SAT (ta);
2325 /* For ENUMERAL_TYPEs in C++, must check the mode of the types, not
2326 the precision; they have precision set to match their range, but
2327 may use a wider mode to match an ABI. If we change modes, we may
2328 wind up with bad conversions. For INTEGER_TYPEs in C, must check
2329 the precision as well, so as to yield correct results for
2330 bit-field types. C++ does not have these separate bit-field
2331 types, and producing a signed or unsigned variant of an
2332 ENUMERAL_TYPE may cause other problems as well. */
2333 if (!INTEGRAL_TYPE_P (type)
2334 || TYPE_UNSIGNED (type) == unsignedp)
2335 return type;
2337 #define TYPE_OK(node) \
2338 (TYPE_MODE (type) == TYPE_MODE (node) \
2339 && TYPE_PRECISION (type) == TYPE_PRECISION (node))
2340 if (TYPE_OK (signed_char_type_node))
2341 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
2342 if (TYPE_OK (integer_type_node))
2343 return unsignedp ? unsigned_type_node : integer_type_node;
2344 if (TYPE_OK (short_integer_type_node))
2345 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
2346 if (TYPE_OK (long_integer_type_node))
2347 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
2348 if (TYPE_OK (long_long_integer_type_node))
2349 return (unsignedp
2350 ? long_long_unsigned_type_node
2351 : long_long_integer_type_node);
2353 for (i = 0; i < NUM_INT_N_ENTS; i ++)
2354 if (int_n_enabled_p[i]
2355 && TYPE_MODE (type) == int_n_data[i].m
2356 && TYPE_PRECISION (type) == int_n_data[i].bitsize)
2357 return unsignedp
2358 ? int_n_trees[i].unsigned_type
2359 : int_n_trees[i].signed_type;
2361 #if HOST_BITS_PER_WIDE_INT >= 64
2362 if (TYPE_OK (intTI_type_node))
2363 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
2364 #endif
2365 if (TYPE_OK (intDI_type_node))
2366 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
2367 if (TYPE_OK (intSI_type_node))
2368 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
2369 if (TYPE_OK (intHI_type_node))
2370 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
2371 if (TYPE_OK (intQI_type_node))
2372 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
2374 #undef GIMPLE_FIXED_TYPES
2375 #undef GIMPLE_FIXED_MODE_TYPES
2376 #undef GIMPLE_FIXED_TYPES_SAT
2377 #undef GIMPLE_FIXED_MODE_TYPES_SAT
2378 #undef TYPE_OK
2380 return build_nonstandard_integer_type (TYPE_PRECISION (type), unsignedp);
2384 /* Return an unsigned type the same as TYPE in other respects. */
2386 tree
2387 gimple_unsigned_type (tree type)
2389 return gimple_signed_or_unsigned_type (true, type);
2393 /* Return a signed type the same as TYPE in other respects. */
2395 tree
2396 gimple_signed_type (tree type)
2398 return gimple_signed_or_unsigned_type (false, type);
2402 /* Return the typed-based alias set for T, which may be an expression
2403 or a type. Return -1 if we don't do anything special. */
2405 alias_set_type
2406 gimple_get_alias_set (tree t)
2408 /* That's all the expressions we handle specially. */
2409 if (!TYPE_P (t))
2410 return -1;
2412 /* For convenience, follow the C standard when dealing with
2413 character types. Any object may be accessed via an lvalue that
2414 has character type. */
2415 if (t == char_type_node
2416 || t == signed_char_type_node
2417 || t == unsigned_char_type_node)
2418 return 0;
2420 /* Allow aliasing between signed and unsigned variants of the same
2421 type. We treat the signed variant as canonical. */
2422 if (TREE_CODE (t) == INTEGER_TYPE && TYPE_UNSIGNED (t))
2424 tree t1 = gimple_signed_type (t);
2426 /* t1 == t can happen for boolean nodes which are always unsigned. */
2427 if (t1 != t)
2428 return get_alias_set (t1);
2431 return -1;
2435 /* Helper for gimple_ior_addresses_taken_1. */
2437 static bool
2438 gimple_ior_addresses_taken_1 (gimple *, tree addr, tree, void *data)
2440 bitmap addresses_taken = (bitmap)data;
2441 addr = get_base_address (addr);
2442 if (addr
2443 && DECL_P (addr))
2445 bitmap_set_bit (addresses_taken, DECL_UID (addr));
2446 return true;
2448 return false;
2451 /* Set the bit for the uid of all decls that have their address taken
2452 in STMT in the ADDRESSES_TAKEN bitmap. Returns true if there
2453 were any in this stmt. */
2455 bool
2456 gimple_ior_addresses_taken (bitmap addresses_taken, gimple *stmt)
2458 return walk_stmt_load_store_addr_ops (stmt, addresses_taken, NULL, NULL,
2459 gimple_ior_addresses_taken_1);
2463 /* Return true when STMTs arguments and return value match those of FNDECL,
2464 a decl of a builtin function. */
2466 bool
2467 gimple_builtin_call_types_compatible_p (const gimple *stmt, tree fndecl)
2469 gcc_checking_assert (DECL_BUILT_IN_CLASS (fndecl) != NOT_BUILT_IN);
2471 tree ret = gimple_call_lhs (stmt);
2472 if (ret
2473 && !useless_type_conversion_p (TREE_TYPE (ret),
2474 TREE_TYPE (TREE_TYPE (fndecl))))
2475 return false;
2477 tree targs = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
2478 unsigned nargs = gimple_call_num_args (stmt);
2479 for (unsigned i = 0; i < nargs; ++i)
2481 /* Variadic args follow. */
2482 if (!targs)
2483 return true;
2484 tree arg = gimple_call_arg (stmt, i);
2485 tree type = TREE_VALUE (targs);
2486 if (!useless_type_conversion_p (type, TREE_TYPE (arg))
2487 /* char/short integral arguments are promoted to int
2488 by several frontends if targetm.calls.promote_prototypes
2489 is true. Allow such promotion too. */
2490 && !(INTEGRAL_TYPE_P (type)
2491 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)
2492 && targetm.calls.promote_prototypes (TREE_TYPE (fndecl))
2493 && useless_type_conversion_p (integer_type_node,
2494 TREE_TYPE (arg))))
2495 return false;
2496 targs = TREE_CHAIN (targs);
2498 if (targs && !VOID_TYPE_P (TREE_VALUE (targs)))
2499 return false;
2500 return true;
2503 /* Return true when STMT is builtins call. */
2505 bool
2506 gimple_call_builtin_p (const gimple *stmt)
2508 tree fndecl;
2509 if (is_gimple_call (stmt)
2510 && (fndecl = gimple_call_fndecl (stmt)) != NULL_TREE
2511 && DECL_BUILT_IN_CLASS (fndecl) != NOT_BUILT_IN)
2512 return gimple_builtin_call_types_compatible_p (stmt, fndecl);
2513 return false;
2516 /* Return true when STMT is builtins call to CLASS. */
2518 bool
2519 gimple_call_builtin_p (const gimple *stmt, enum built_in_class klass)
2521 tree fndecl;
2522 if (is_gimple_call (stmt)
2523 && (fndecl = gimple_call_fndecl (stmt)) != NULL_TREE
2524 && DECL_BUILT_IN_CLASS (fndecl) == klass)
2525 return gimple_builtin_call_types_compatible_p (stmt, fndecl);
2526 return false;
2529 /* Return true when STMT is builtins call to CODE of CLASS. */
2531 bool
2532 gimple_call_builtin_p (const gimple *stmt, enum built_in_function code)
2534 tree fndecl;
2535 if (is_gimple_call (stmt)
2536 && (fndecl = gimple_call_fndecl (stmt)) != NULL_TREE
2537 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
2538 && DECL_FUNCTION_CODE (fndecl) == code)
2539 return gimple_builtin_call_types_compatible_p (stmt, fndecl);
2540 return false;
2543 /* If CALL is a call to a combined_fn (i.e. an internal function or
2544 a normal built-in function), return its code, otherwise return
2545 CFN_LAST. */
2547 combined_fn
2548 gimple_call_combined_fn (const gimple *stmt)
2550 if (const gcall *call = dyn_cast <const gcall *> (stmt))
2552 if (gimple_call_internal_p (call))
2553 return as_combined_fn (gimple_call_internal_fn (call));
2555 tree fndecl = gimple_call_fndecl (stmt);
2556 if (fndecl
2557 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
2558 && gimple_builtin_call_types_compatible_p (stmt, fndecl))
2559 return as_combined_fn (DECL_FUNCTION_CODE (fndecl));
2561 return CFN_LAST;
2564 /* Return true if STMT clobbers memory. STMT is required to be a
2565 GIMPLE_ASM. */
2567 bool
2568 gimple_asm_clobbers_memory_p (const gasm *stmt)
2570 unsigned i;
2572 for (i = 0; i < gimple_asm_nclobbers (stmt); i++)
2574 tree op = gimple_asm_clobber_op (stmt, i);
2575 if (strcmp (TREE_STRING_POINTER (TREE_VALUE (op)), "memory") == 0)
2576 return true;
2579 /* Non-empty basic ASM implicitly clobbers memory. */
2580 if (gimple_asm_input_p (stmt) && strlen (gimple_asm_string (stmt)) != 0)
2581 return true;
2583 return false;
2586 /* Dump bitmap SET (assumed to contain VAR_DECLs) to FILE. */
2588 void
2589 dump_decl_set (FILE *file, bitmap set)
2591 if (set)
2593 bitmap_iterator bi;
2594 unsigned i;
2596 fprintf (file, "{ ");
2598 EXECUTE_IF_SET_IN_BITMAP (set, 0, i, bi)
2600 fprintf (file, "D.%u", i);
2601 fprintf (file, " ");
2604 fprintf (file, "}");
2606 else
2607 fprintf (file, "NIL");
2610 /* Return true when CALL is a call stmt that definitely doesn't
2611 free any memory or makes it unavailable otherwise. */
2612 bool
2613 nonfreeing_call_p (gimple *call)
2615 if (gimple_call_builtin_p (call, BUILT_IN_NORMAL)
2616 && gimple_call_flags (call) & ECF_LEAF)
2617 switch (DECL_FUNCTION_CODE (gimple_call_fndecl (call)))
2619 /* Just in case these become ECF_LEAF in the future. */
2620 case BUILT_IN_FREE:
2621 case BUILT_IN_TM_FREE:
2622 case BUILT_IN_REALLOC:
2623 case BUILT_IN_STACK_RESTORE:
2624 return false;
2625 default:
2626 return true;
2628 else if (gimple_call_internal_p (call))
2629 switch (gimple_call_internal_fn (call))
2631 case IFN_ABNORMAL_DISPATCHER:
2632 return true;
2633 case IFN_ASAN_MARK:
2634 return tree_to_uhwi (gimple_call_arg (call, 0)) == ASAN_MARK_UNPOISON;
2635 default:
2636 if (gimple_call_flags (call) & ECF_LEAF)
2637 return true;
2638 return false;
2641 tree fndecl = gimple_call_fndecl (call);
2642 if (!fndecl)
2643 return false;
2644 struct cgraph_node *n = cgraph_node::get (fndecl);
2645 if (!n)
2646 return false;
2647 enum availability availability;
2648 n = n->function_symbol (&availability);
2649 if (!n || availability <= AVAIL_INTERPOSABLE)
2650 return false;
2651 return n->nonfreeing_fn;
2654 /* Return true when CALL is a call stmt that definitely need not
2655 be considered to be a memory barrier. */
2656 bool
2657 nonbarrier_call_p (gimple *call)
2659 if (gimple_call_flags (call) & (ECF_PURE | ECF_CONST))
2660 return true;
2661 /* Should extend this to have a nonbarrier_fn flag, just as above in
2662 the nonfreeing case. */
2663 return false;
2666 /* Callback for walk_stmt_load_store_ops.
2668 Return TRUE if OP will dereference the tree stored in DATA, FALSE
2669 otherwise.
2671 This routine only makes a superficial check for a dereference. Thus
2672 it must only be used if it is safe to return a false negative. */
2673 static bool
2674 check_loadstore (gimple *, tree op, tree, void *data)
2676 if (TREE_CODE (op) == MEM_REF || TREE_CODE (op) == TARGET_MEM_REF)
2678 /* Some address spaces may legitimately dereference zero. */
2679 addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (op));
2680 if (targetm.addr_space.zero_address_valid (as))
2681 return false;
2683 return operand_equal_p (TREE_OPERAND (op, 0), (tree)data, 0);
2685 return false;
2689 /* Return true if OP can be inferred to be non-NULL after STMT executes,
2690 either by using a pointer dereference or attributes. */
2691 bool
2692 infer_nonnull_range (gimple *stmt, tree op)
2694 return infer_nonnull_range_by_dereference (stmt, op)
2695 || infer_nonnull_range_by_attribute (stmt, op);
2698 /* Return true if OP can be inferred to be non-NULL after STMT
2699 executes by using a pointer dereference. */
2700 bool
2701 infer_nonnull_range_by_dereference (gimple *stmt, tree op)
2703 /* We can only assume that a pointer dereference will yield
2704 non-NULL if -fdelete-null-pointer-checks is enabled. */
2705 if (!flag_delete_null_pointer_checks
2706 || !POINTER_TYPE_P (TREE_TYPE (op))
2707 || gimple_code (stmt) == GIMPLE_ASM)
2708 return false;
2710 if (walk_stmt_load_store_ops (stmt, (void *)op,
2711 check_loadstore, check_loadstore))
2712 return true;
2714 return false;
2717 /* Return true if OP can be inferred to be a non-NULL after STMT
2718 executes by using attributes. */
2719 bool
2720 infer_nonnull_range_by_attribute (gimple *stmt, tree op)
2722 /* We can only assume that a pointer dereference will yield
2723 non-NULL if -fdelete-null-pointer-checks is enabled. */
2724 if (!flag_delete_null_pointer_checks
2725 || !POINTER_TYPE_P (TREE_TYPE (op))
2726 || gimple_code (stmt) == GIMPLE_ASM)
2727 return false;
2729 if (is_gimple_call (stmt) && !gimple_call_internal_p (stmt))
2731 tree fntype = gimple_call_fntype (stmt);
2732 tree attrs = TYPE_ATTRIBUTES (fntype);
2733 for (; attrs; attrs = TREE_CHAIN (attrs))
2735 attrs = lookup_attribute ("nonnull", attrs);
2737 /* If "nonnull" wasn't specified, we know nothing about
2738 the argument. */
2739 if (attrs == NULL_TREE)
2740 return false;
2742 /* If "nonnull" applies to all the arguments, then ARG
2743 is non-null if it's in the argument list. */
2744 if (TREE_VALUE (attrs) == NULL_TREE)
2746 for (unsigned int i = 0; i < gimple_call_num_args (stmt); i++)
2748 if (POINTER_TYPE_P (TREE_TYPE (gimple_call_arg (stmt, i)))
2749 && operand_equal_p (op, gimple_call_arg (stmt, i), 0))
2750 return true;
2752 return false;
2755 /* Now see if op appears in the nonnull list. */
2756 for (tree t = TREE_VALUE (attrs); t; t = TREE_CHAIN (t))
2758 unsigned int idx = TREE_INT_CST_LOW (TREE_VALUE (t)) - 1;
2759 if (idx < gimple_call_num_args (stmt))
2761 tree arg = gimple_call_arg (stmt, idx);
2762 if (operand_equal_p (op, arg, 0))
2763 return true;
2769 /* If this function is marked as returning non-null, then we can
2770 infer OP is non-null if it is used in the return statement. */
2771 if (greturn *return_stmt = dyn_cast <greturn *> (stmt))
2772 if (gimple_return_retval (return_stmt)
2773 && operand_equal_p (gimple_return_retval (return_stmt), op, 0)
2774 && lookup_attribute ("returns_nonnull",
2775 TYPE_ATTRIBUTES (TREE_TYPE (current_function_decl))))
2776 return true;
2778 return false;
2781 /* Compare two case labels. Because the front end should already have
2782 made sure that case ranges do not overlap, it is enough to only compare
2783 the CASE_LOW values of each case label. */
2785 static int
2786 compare_case_labels (const void *p1, const void *p2)
2788 const_tree const case1 = *(const_tree const*)p1;
2789 const_tree const case2 = *(const_tree const*)p2;
2791 /* The 'default' case label always goes first. */
2792 if (!CASE_LOW (case1))
2793 return -1;
2794 else if (!CASE_LOW (case2))
2795 return 1;
2796 else
2797 return tree_int_cst_compare (CASE_LOW (case1), CASE_LOW (case2));
2800 /* Sort the case labels in LABEL_VEC in place in ascending order. */
2802 void
2803 sort_case_labels (vec<tree> label_vec)
2805 label_vec.qsort (compare_case_labels);
2808 /* Prepare a vector of case labels to be used in a GIMPLE_SWITCH statement.
2810 LABELS is a vector that contains all case labels to look at.
2812 INDEX_TYPE is the type of the switch index expression. Case labels
2813 in LABELS are discarded if their values are not in the value range
2814 covered by INDEX_TYPE. The remaining case label values are folded
2815 to INDEX_TYPE.
2817 If a default case exists in LABELS, it is removed from LABELS and
2818 returned in DEFAULT_CASEP. If no default case exists, but the
2819 case labels already cover the whole range of INDEX_TYPE, a default
2820 case is returned pointing to one of the existing case labels.
2821 Otherwise DEFAULT_CASEP is set to NULL_TREE.
2823 DEFAULT_CASEP may be NULL, in which case the above comment doesn't
2824 apply and no action is taken regardless of whether a default case is
2825 found or not. */
2827 void
2828 preprocess_case_label_vec_for_gimple (vec<tree> labels,
2829 tree index_type,
2830 tree *default_casep)
2832 tree min_value, max_value;
2833 tree default_case = NULL_TREE;
2834 size_t i, len;
2836 i = 0;
2837 min_value = TYPE_MIN_VALUE (index_type);
2838 max_value = TYPE_MAX_VALUE (index_type);
2839 while (i < labels.length ())
2841 tree elt = labels[i];
2842 tree low = CASE_LOW (elt);
2843 tree high = CASE_HIGH (elt);
2844 bool remove_element = FALSE;
2846 if (low)
2848 gcc_checking_assert (TREE_CODE (low) == INTEGER_CST);
2849 gcc_checking_assert (!high || TREE_CODE (high) == INTEGER_CST);
2851 /* This is a non-default case label, i.e. it has a value.
2853 See if the case label is reachable within the range of
2854 the index type. Remove out-of-range case values. Turn
2855 case ranges into a canonical form (high > low strictly)
2856 and convert the case label values to the index type.
2858 NB: The type of gimple_switch_index() may be the promoted
2859 type, but the case labels retain the original type. */
2861 if (high)
2863 /* This is a case range. Discard empty ranges.
2864 If the bounds or the range are equal, turn this
2865 into a simple (one-value) case. */
2866 int cmp = tree_int_cst_compare (high, low);
2867 if (cmp < 0)
2868 remove_element = TRUE;
2869 else if (cmp == 0)
2870 high = NULL_TREE;
2873 if (! high)
2875 /* If the simple case value is unreachable, ignore it. */
2876 if ((TREE_CODE (min_value) == INTEGER_CST
2877 && tree_int_cst_compare (low, min_value) < 0)
2878 || (TREE_CODE (max_value) == INTEGER_CST
2879 && tree_int_cst_compare (low, max_value) > 0))
2880 remove_element = TRUE;
2881 else
2882 low = fold_convert (index_type, low);
2884 else
2886 /* If the entire case range is unreachable, ignore it. */
2887 if ((TREE_CODE (min_value) == INTEGER_CST
2888 && tree_int_cst_compare (high, min_value) < 0)
2889 || (TREE_CODE (max_value) == INTEGER_CST
2890 && tree_int_cst_compare (low, max_value) > 0))
2891 remove_element = TRUE;
2892 else
2894 /* If the lower bound is less than the index type's
2895 minimum value, truncate the range bounds. */
2896 if (TREE_CODE (min_value) == INTEGER_CST
2897 && tree_int_cst_compare (low, min_value) < 0)
2898 low = min_value;
2899 low = fold_convert (index_type, low);
2901 /* If the upper bound is greater than the index type's
2902 maximum value, truncate the range bounds. */
2903 if (TREE_CODE (max_value) == INTEGER_CST
2904 && tree_int_cst_compare (high, max_value) > 0)
2905 high = max_value;
2906 high = fold_convert (index_type, high);
2908 /* We may have folded a case range to a one-value case. */
2909 if (tree_int_cst_equal (low, high))
2910 high = NULL_TREE;
2914 CASE_LOW (elt) = low;
2915 CASE_HIGH (elt) = high;
2917 else
2919 gcc_assert (!default_case);
2920 default_case = elt;
2921 /* The default case must be passed separately to the
2922 gimple_build_switch routine. But if DEFAULT_CASEP
2923 is NULL, we do not remove the default case (it would
2924 be completely lost). */
2925 if (default_casep)
2926 remove_element = TRUE;
2929 if (remove_element)
2930 labels.ordered_remove (i);
2931 else
2932 i++;
2934 len = i;
2936 if (!labels.is_empty ())
2937 sort_case_labels (labels);
2939 if (default_casep && !default_case)
2941 /* If the switch has no default label, add one, so that we jump
2942 around the switch body. If the labels already cover the whole
2943 range of the switch index_type, add the default label pointing
2944 to one of the existing labels. */
2945 if (len
2946 && TYPE_MIN_VALUE (index_type)
2947 && TYPE_MAX_VALUE (index_type)
2948 && tree_int_cst_equal (CASE_LOW (labels[0]),
2949 TYPE_MIN_VALUE (index_type)))
2951 tree low, high = CASE_HIGH (labels[len - 1]);
2952 if (!high)
2953 high = CASE_LOW (labels[len - 1]);
2954 if (tree_int_cst_equal (high, TYPE_MAX_VALUE (index_type)))
2956 tree widest_label = labels[0];
2957 for (i = 1; i < len; i++)
2959 high = CASE_LOW (labels[i]);
2960 low = CASE_HIGH (labels[i - 1]);
2961 if (!low)
2962 low = CASE_LOW (labels[i - 1]);
2964 if (CASE_HIGH (labels[i]) != NULL_TREE
2965 && (CASE_HIGH (widest_label) == NULL_TREE
2966 || wi::gtu_p (wi::sub (CASE_HIGH (labels[i]),
2967 CASE_LOW (labels[i])),
2968 wi::sub (CASE_HIGH (widest_label),
2969 CASE_LOW (widest_label)))))
2970 widest_label = labels[i];
2972 if (wi::add (low, 1) != high)
2973 break;
2975 if (i == len)
2977 /* Designate the label with the widest range to be the
2978 default label. */
2979 tree label = CASE_LABEL (widest_label);
2980 default_case = build_case_label (NULL_TREE, NULL_TREE,
2981 label);
2987 if (default_casep)
2988 *default_casep = default_case;
2991 /* Set the location of all statements in SEQ to LOC. */
2993 void
2994 gimple_seq_set_location (gimple_seq seq, location_t loc)
2996 for (gimple_stmt_iterator i = gsi_start (seq); !gsi_end_p (i); gsi_next (&i))
2997 gimple_set_location (gsi_stmt (i), loc);
3000 /* Release SSA_NAMEs in SEQ as well as the GIMPLE statements. */
3002 void
3003 gimple_seq_discard (gimple_seq seq)
3005 gimple_stmt_iterator gsi;
3007 for (gsi = gsi_start (seq); !gsi_end_p (gsi); )
3009 gimple *stmt = gsi_stmt (gsi);
3010 gsi_remove (&gsi, true);
3011 release_defs (stmt);
3012 ggc_free (stmt);
3016 /* See if STMT now calls function that takes no parameters and if so, drop
3017 call arguments. This is used when devirtualization machinery redirects
3018 to __builtin_unreachable or __cxa_pure_virtual. */
3020 void
3021 maybe_remove_unused_call_args (struct function *fn, gimple *stmt)
3023 tree decl = gimple_call_fndecl (stmt);
3024 if (TYPE_ARG_TYPES (TREE_TYPE (decl))
3025 && TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl))) == void_type_node
3026 && gimple_call_num_args (stmt))
3028 gimple_set_num_ops (stmt, 3);
3029 update_stmt_fn (fn, stmt);
3033 /* Return false if STMT will likely expand to real function call. */
3035 bool
3036 gimple_inexpensive_call_p (gcall *stmt)
3038 if (gimple_call_internal_p (stmt))
3039 return true;
3040 tree decl = gimple_call_fndecl (stmt);
3041 if (decl && is_inexpensive_builtin (decl))
3042 return true;
3043 return false;
3046 #if CHECKING_P
3048 namespace selftest {
3050 /* Selftests for core gimple structures. */
3052 /* Verify that STMT is pretty-printed as EXPECTED.
3053 Helper function for selftests. */
3055 static void
3056 verify_gimple_pp (const char *expected, gimple *stmt)
3058 pretty_printer pp;
3059 pp_gimple_stmt_1 (&pp, stmt, 0 /* spc */, 0 /* flags */);
3060 ASSERT_STREQ (expected, pp_formatted_text (&pp));
3063 /* Build a GIMPLE_ASSIGN equivalent to
3064 tmp = 5;
3065 and verify various properties of it. */
3067 static void
3068 test_assign_single ()
3070 tree type = integer_type_node;
3071 tree lhs = build_decl (UNKNOWN_LOCATION, VAR_DECL,
3072 get_identifier ("tmp"),
3073 type);
3074 tree rhs = build_int_cst (type, 5);
3075 gassign *stmt = gimple_build_assign (lhs, rhs);
3076 verify_gimple_pp ("tmp = 5;", stmt);
3078 ASSERT_TRUE (is_gimple_assign (stmt));
3079 ASSERT_EQ (lhs, gimple_assign_lhs (stmt));
3080 ASSERT_EQ (lhs, gimple_get_lhs (stmt));
3081 ASSERT_EQ (rhs, gimple_assign_rhs1 (stmt));
3082 ASSERT_EQ (NULL, gimple_assign_rhs2 (stmt));
3083 ASSERT_EQ (NULL, gimple_assign_rhs3 (stmt));
3084 ASSERT_TRUE (gimple_assign_single_p (stmt));
3085 ASSERT_EQ (INTEGER_CST, gimple_assign_rhs_code (stmt));
3088 /* Build a GIMPLE_ASSIGN equivalent to
3089 tmp = a * b;
3090 and verify various properties of it. */
3092 static void
3093 test_assign_binop ()
3095 tree type = integer_type_node;
3096 tree lhs = build_decl (UNKNOWN_LOCATION, VAR_DECL,
3097 get_identifier ("tmp"),
3098 type);
3099 tree a = build_decl (UNKNOWN_LOCATION, VAR_DECL,
3100 get_identifier ("a"),
3101 type);
3102 tree b = build_decl (UNKNOWN_LOCATION, VAR_DECL,
3103 get_identifier ("b"),
3104 type);
3105 gassign *stmt = gimple_build_assign (lhs, MULT_EXPR, a, b);
3106 verify_gimple_pp ("tmp = a * b;", stmt);
3108 ASSERT_TRUE (is_gimple_assign (stmt));
3109 ASSERT_EQ (lhs, gimple_assign_lhs (stmt));
3110 ASSERT_EQ (lhs, gimple_get_lhs (stmt));
3111 ASSERT_EQ (a, gimple_assign_rhs1 (stmt));
3112 ASSERT_EQ (b, gimple_assign_rhs2 (stmt));
3113 ASSERT_EQ (NULL, gimple_assign_rhs3 (stmt));
3114 ASSERT_FALSE (gimple_assign_single_p (stmt));
3115 ASSERT_EQ (MULT_EXPR, gimple_assign_rhs_code (stmt));
3118 /* Build a GIMPLE_NOP and verify various properties of it. */
3120 static void
3121 test_nop_stmt ()
3123 gimple *stmt = gimple_build_nop ();
3124 verify_gimple_pp ("GIMPLE_NOP", stmt);
3125 ASSERT_EQ (GIMPLE_NOP, gimple_code (stmt));
3126 ASSERT_EQ (NULL, gimple_get_lhs (stmt));
3127 ASSERT_FALSE (gimple_assign_single_p (stmt));
3130 /* Build a GIMPLE_RETURN equivalent to
3131 return 7;
3132 and verify various properties of it. */
3134 static void
3135 test_return_stmt ()
3137 tree type = integer_type_node;
3138 tree val = build_int_cst (type, 7);
3139 greturn *stmt = gimple_build_return (val);
3140 verify_gimple_pp ("return 7;", stmt);
3142 ASSERT_EQ (GIMPLE_RETURN, gimple_code (stmt));
3143 ASSERT_EQ (NULL, gimple_get_lhs (stmt));
3144 ASSERT_EQ (val, gimple_return_retval (stmt));
3145 ASSERT_FALSE (gimple_assign_single_p (stmt));
3148 /* Build a GIMPLE_RETURN equivalent to
3149 return;
3150 and verify various properties of it. */
3152 static void
3153 test_return_without_value ()
3155 greturn *stmt = gimple_build_return (NULL);
3156 verify_gimple_pp ("return;", stmt);
3158 ASSERT_EQ (GIMPLE_RETURN, gimple_code (stmt));
3159 ASSERT_EQ (NULL, gimple_get_lhs (stmt));
3160 ASSERT_EQ (NULL, gimple_return_retval (stmt));
3161 ASSERT_FALSE (gimple_assign_single_p (stmt));
3164 /* Run all of the selftests within this file. */
3166 void
3167 gimple_c_tests ()
3169 test_assign_single ();
3170 test_assign_binop ();
3171 test_nop_stmt ();
3172 test_return_stmt ();
3173 test_return_without_value ();
3176 } // namespace selftest
3179 #endif /* CHECKING_P */