PR libstdc++/54562
[official-gcc.git] / gcc / gimple.c
blob55dd09106bba595155ca22f6caa03bbf442211e7
1 /* Gimple IR support functions.
3 Copyright (C) 2007-2013 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 "tm.h"
26 #include "target.h"
27 #include "tree.h"
28 #include "ggc.h"
29 #include "hard-reg-set.h"
30 #include "basic-block.h"
31 #include "gimple.h"
32 #include "diagnostic.h"
33 #include "value-prof.h"
34 #include "flags.h"
35 #include "alias.h"
36 #include "demangle.h"
37 #include "langhooks.h"
38 #include "bitmap.h"
41 /* All the tuples have their operand vector (if present) at the very bottom
42 of the structure. Therefore, the offset required to find the
43 operands vector the size of the structure minus the size of the 1
44 element tree array at the end (see gimple_ops). */
45 #define DEFGSSTRUCT(SYM, STRUCT, HAS_TREE_OP) \
46 (HAS_TREE_OP ? sizeof (struct STRUCT) - sizeof (tree) : 0),
47 EXPORTED_CONST size_t gimple_ops_offset_[] = {
48 #include "gsstruct.def"
50 #undef DEFGSSTRUCT
52 #define DEFGSSTRUCT(SYM, STRUCT, HAS_TREE_OP) sizeof (struct STRUCT),
53 static const size_t gsstruct_code_size[] = {
54 #include "gsstruct.def"
56 #undef DEFGSSTRUCT
58 #define DEFGSCODE(SYM, NAME, GSSCODE) NAME,
59 const char *const gimple_code_name[] = {
60 #include "gimple.def"
62 #undef DEFGSCODE
64 #define DEFGSCODE(SYM, NAME, GSSCODE) GSSCODE,
65 EXPORTED_CONST enum gimple_statement_structure_enum gss_for_code_[] = {
66 #include "gimple.def"
68 #undef DEFGSCODE
70 /* Gimple stats. */
72 int gimple_alloc_counts[(int) gimple_alloc_kind_all];
73 int gimple_alloc_sizes[(int) gimple_alloc_kind_all];
75 /* Keep in sync with gimple.h:enum gimple_alloc_kind. */
76 static const char * const gimple_alloc_kind_names[] = {
77 "assignments",
78 "phi nodes",
79 "conditionals",
80 "everything else"
83 /* Private API manipulation functions shared only with some
84 other files. */
85 extern void gimple_set_stored_syms (gimple, bitmap, bitmap_obstack *);
86 extern void gimple_set_loaded_syms (gimple, bitmap, bitmap_obstack *);
88 /* Gimple tuple constructors.
89 Note: Any constructor taking a ``gimple_seq'' as a parameter, can
90 be passed a NULL to start with an empty sequence. */
92 /* Set the code for statement G to CODE. */
94 static inline void
95 gimple_set_code (gimple g, enum gimple_code code)
97 g->gsbase.code = code;
100 /* Return the number of bytes needed to hold a GIMPLE statement with
101 code CODE. */
103 static inline size_t
104 gimple_size (enum gimple_code code)
106 return gsstruct_code_size[gss_for_code (code)];
109 /* Allocate memory for a GIMPLE statement with code CODE and NUM_OPS
110 operands. */
112 gimple
113 gimple_alloc_stat (enum gimple_code code, unsigned num_ops MEM_STAT_DECL)
115 size_t size;
116 gimple stmt;
118 size = gimple_size (code);
119 if (num_ops > 0)
120 size += sizeof (tree) * (num_ops - 1);
122 if (GATHER_STATISTICS)
124 enum gimple_alloc_kind kind = gimple_alloc_kind (code);
125 gimple_alloc_counts[(int) kind]++;
126 gimple_alloc_sizes[(int) kind] += size;
129 stmt = ggc_alloc_cleared_gimple_statement_d_stat (size PASS_MEM_STAT);
130 gimple_set_code (stmt, code);
131 gimple_set_num_ops (stmt, num_ops);
133 /* Do not call gimple_set_modified here as it has other side
134 effects and this tuple is still not completely built. */
135 stmt->gsbase.modified = 1;
136 gimple_init_singleton (stmt);
138 return stmt;
141 /* Set SUBCODE to be the code of the expression computed by statement G. */
143 static inline void
144 gimple_set_subcode (gimple g, unsigned subcode)
146 /* We only have 16 bits for the RHS code. Assert that we are not
147 overflowing it. */
148 gcc_assert (subcode < (1 << 16));
149 g->gsbase.subcode = subcode;
154 /* Build a tuple with operands. CODE is the statement to build (which
155 must be one of the GIMPLE_WITH_OPS tuples). SUBCODE is the subcode
156 for the new tuple. NUM_OPS is the number of operands to allocate. */
158 #define gimple_build_with_ops(c, s, n) \
159 gimple_build_with_ops_stat (c, s, n MEM_STAT_INFO)
161 static gimple
162 gimple_build_with_ops_stat (enum gimple_code code, unsigned subcode,
163 unsigned num_ops MEM_STAT_DECL)
165 gimple s = gimple_alloc_stat (code, num_ops PASS_MEM_STAT);
166 gimple_set_subcode (s, subcode);
168 return s;
172 /* Build a GIMPLE_RETURN statement returning RETVAL. */
174 gimple
175 gimple_build_return (tree retval)
177 gimple s = gimple_build_with_ops (GIMPLE_RETURN, ERROR_MARK, 2);
178 if (retval)
179 gimple_return_set_retval (s, retval);
180 return s;
183 /* Reset alias information on call S. */
185 void
186 gimple_call_reset_alias_info (gimple s)
188 if (gimple_call_flags (s) & ECF_CONST)
189 memset (gimple_call_use_set (s), 0, sizeof (struct pt_solution));
190 else
191 pt_solution_reset (gimple_call_use_set (s));
192 if (gimple_call_flags (s) & (ECF_CONST|ECF_PURE|ECF_NOVOPS))
193 memset (gimple_call_clobber_set (s), 0, sizeof (struct pt_solution));
194 else
195 pt_solution_reset (gimple_call_clobber_set (s));
198 /* Helper for gimple_build_call, gimple_build_call_valist,
199 gimple_build_call_vec and gimple_build_call_from_tree. Build the basic
200 components of a GIMPLE_CALL statement to function FN with NARGS
201 arguments. */
203 static inline gimple
204 gimple_build_call_1 (tree fn, unsigned nargs)
206 gimple s = gimple_build_with_ops (GIMPLE_CALL, ERROR_MARK, nargs + 3);
207 if (TREE_CODE (fn) == FUNCTION_DECL)
208 fn = build_fold_addr_expr (fn);
209 gimple_set_op (s, 1, fn);
210 gimple_call_set_fntype (s, TREE_TYPE (TREE_TYPE (fn)));
211 gimple_call_reset_alias_info (s);
212 return s;
216 /* Build a GIMPLE_CALL statement to function FN with the arguments
217 specified in vector ARGS. */
219 gimple
220 gimple_build_call_vec (tree fn, vec<tree> args)
222 unsigned i;
223 unsigned nargs = args.length ();
224 gimple call = gimple_build_call_1 (fn, nargs);
226 for (i = 0; i < nargs; i++)
227 gimple_call_set_arg (call, i, args[i]);
229 return call;
233 /* Build a GIMPLE_CALL statement to function FN. NARGS is the number of
234 arguments. The ... are the arguments. */
236 gimple
237 gimple_build_call (tree fn, unsigned nargs, ...)
239 va_list ap;
240 gimple call;
241 unsigned i;
243 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL || is_gimple_call_addr (fn));
245 call = gimple_build_call_1 (fn, nargs);
247 va_start (ap, nargs);
248 for (i = 0; i < nargs; i++)
249 gimple_call_set_arg (call, i, va_arg (ap, tree));
250 va_end (ap);
252 return call;
256 /* Build a GIMPLE_CALL statement to function FN. NARGS is the number of
257 arguments. AP contains the arguments. */
259 gimple
260 gimple_build_call_valist (tree fn, unsigned nargs, va_list ap)
262 gimple call;
263 unsigned i;
265 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL || is_gimple_call_addr (fn));
267 call = gimple_build_call_1 (fn, nargs);
269 for (i = 0; i < nargs; i++)
270 gimple_call_set_arg (call, i, va_arg (ap, tree));
272 return call;
276 /* Helper for gimple_build_call_internal and gimple_build_call_internal_vec.
277 Build the basic components of a GIMPLE_CALL statement to internal
278 function FN with NARGS arguments. */
280 static inline gimple
281 gimple_build_call_internal_1 (enum internal_fn fn, unsigned nargs)
283 gimple s = gimple_build_with_ops (GIMPLE_CALL, ERROR_MARK, nargs + 3);
284 s->gsbase.subcode |= GF_CALL_INTERNAL;
285 gimple_call_set_internal_fn (s, fn);
286 gimple_call_reset_alias_info (s);
287 return s;
291 /* Build a GIMPLE_CALL statement to internal function FN. NARGS is
292 the number of arguments. The ... are the arguments. */
294 gimple
295 gimple_build_call_internal (enum internal_fn fn, unsigned nargs, ...)
297 va_list ap;
298 gimple call;
299 unsigned i;
301 call = gimple_build_call_internal_1 (fn, nargs);
302 va_start (ap, nargs);
303 for (i = 0; i < nargs; i++)
304 gimple_call_set_arg (call, i, va_arg (ap, tree));
305 va_end (ap);
307 return call;
311 /* Build a GIMPLE_CALL statement to internal function FN with the arguments
312 specified in vector ARGS. */
314 gimple
315 gimple_build_call_internal_vec (enum internal_fn fn, vec<tree> args)
317 unsigned i, nargs;
318 gimple call;
320 nargs = args.length ();
321 call = gimple_build_call_internal_1 (fn, nargs);
322 for (i = 0; i < nargs; i++)
323 gimple_call_set_arg (call, i, args[i]);
325 return call;
329 /* Build a GIMPLE_CALL statement from CALL_EXPR T. Note that T is
330 assumed to be in GIMPLE form already. Minimal checking is done of
331 this fact. */
333 gimple
334 gimple_build_call_from_tree (tree t)
336 unsigned i, nargs;
337 gimple call;
338 tree fndecl = get_callee_fndecl (t);
340 gcc_assert (TREE_CODE (t) == CALL_EXPR);
342 nargs = call_expr_nargs (t);
343 call = gimple_build_call_1 (fndecl ? fndecl : CALL_EXPR_FN (t), nargs);
345 for (i = 0; i < nargs; i++)
346 gimple_call_set_arg (call, i, CALL_EXPR_ARG (t, i));
348 gimple_set_block (call, TREE_BLOCK (t));
350 /* Carry all the CALL_EXPR flags to the new GIMPLE_CALL. */
351 gimple_call_set_chain (call, CALL_EXPR_STATIC_CHAIN (t));
352 gimple_call_set_tail (call, CALL_EXPR_TAILCALL (t));
353 gimple_call_set_return_slot_opt (call, CALL_EXPR_RETURN_SLOT_OPT (t));
354 if (fndecl
355 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
356 && (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_ALLOCA
357 || DECL_FUNCTION_CODE (fndecl) == BUILT_IN_ALLOCA_WITH_ALIGN))
358 gimple_call_set_alloca_for_var (call, CALL_ALLOCA_FOR_VAR_P (t));
359 else
360 gimple_call_set_from_thunk (call, CALL_FROM_THUNK_P (t));
361 gimple_call_set_va_arg_pack (call, CALL_EXPR_VA_ARG_PACK (t));
362 gimple_call_set_nothrow (call, TREE_NOTHROW (t));
363 gimple_set_no_warning (call, TREE_NO_WARNING (t));
365 return call;
369 /* Return index of INDEX's non bound argument of the call. */
371 unsigned
372 gimple_call_get_nobnd_arg_index (const_gimple gs, unsigned index)
374 unsigned num_args = gimple_call_num_args (gs);
375 for (unsigned n = 0; n < num_args; n++)
377 if (POINTER_BOUNDS_P (gimple_call_arg (gs, n)))
378 continue;
379 else if (index)
380 index--;
381 else
382 return n;
385 gcc_unreachable ();
389 /* Build a GIMPLE_ASSIGN statement.
391 LHS of the assignment.
392 RHS of the assignment which can be unary or binary. */
394 gimple
395 gimple_build_assign_stat (tree lhs, tree rhs MEM_STAT_DECL)
397 enum tree_code subcode;
398 tree op1, op2, op3;
400 extract_ops_from_tree_1 (rhs, &subcode, &op1, &op2, &op3);
401 return gimple_build_assign_with_ops (subcode, lhs, op1, op2, op3
402 PASS_MEM_STAT);
406 /* Build a GIMPLE_ASSIGN statement with subcode SUBCODE and operands
407 OP1 and OP2. If OP2 is NULL then SUBCODE must be of class
408 GIMPLE_UNARY_RHS or GIMPLE_SINGLE_RHS. */
410 gimple
411 gimple_build_assign_with_ops (enum tree_code subcode, tree lhs, tree op1,
412 tree op2, tree op3 MEM_STAT_DECL)
414 unsigned num_ops;
415 gimple p;
417 /* Need 1 operand for LHS and 1 or 2 for the RHS (depending on the
418 code). */
419 num_ops = get_gimple_rhs_num_ops (subcode) + 1;
421 p = gimple_build_with_ops_stat (GIMPLE_ASSIGN, (unsigned)subcode, num_ops
422 PASS_MEM_STAT);
423 gimple_assign_set_lhs (p, lhs);
424 gimple_assign_set_rhs1 (p, op1);
425 if (op2)
427 gcc_assert (num_ops > 2);
428 gimple_assign_set_rhs2 (p, op2);
431 if (op3)
433 gcc_assert (num_ops > 3);
434 gimple_assign_set_rhs3 (p, op3);
437 return p;
440 gimple
441 gimple_build_assign_with_ops (enum tree_code subcode, tree lhs, tree op1,
442 tree op2 MEM_STAT_DECL)
444 return gimple_build_assign_with_ops (subcode, lhs, op1, op2, NULL_TREE
445 PASS_MEM_STAT);
449 /* Build a new GIMPLE_ASSIGN tuple and append it to the end of *SEQ_P.
451 DST/SRC are the destination and source respectively. You can pass
452 ungimplified trees in DST or SRC, in which case they will be
453 converted to a gimple operand if necessary.
455 This function returns the newly created GIMPLE_ASSIGN tuple. */
457 gimple
458 gimplify_assign (tree dst, tree src, gimple_seq *seq_p)
460 tree t = build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, src);
461 gimplify_and_add (t, seq_p);
462 ggc_free (t);
463 return gimple_seq_last_stmt (*seq_p);
467 /* Build a GIMPLE_COND statement.
469 PRED is the condition used to compare LHS and the RHS.
470 T_LABEL is the label to jump to if the condition is true.
471 F_LABEL is the label to jump to otherwise. */
473 gimple
474 gimple_build_cond (enum tree_code pred_code, tree lhs, tree rhs,
475 tree t_label, tree f_label)
477 gimple p;
479 gcc_assert (TREE_CODE_CLASS (pred_code) == tcc_comparison);
480 p = gimple_build_with_ops (GIMPLE_COND, pred_code, 4);
481 gimple_cond_set_lhs (p, lhs);
482 gimple_cond_set_rhs (p, rhs);
483 gimple_cond_set_true_label (p, t_label);
484 gimple_cond_set_false_label (p, f_label);
485 return p;
488 /* Build a GIMPLE_COND statement from the conditional expression tree
489 COND. T_LABEL and F_LABEL are as in gimple_build_cond. */
491 gimple
492 gimple_build_cond_from_tree (tree cond, tree t_label, tree f_label)
494 enum tree_code code;
495 tree lhs, rhs;
497 gimple_cond_get_ops_from_tree (cond, &code, &lhs, &rhs);
498 return gimple_build_cond (code, lhs, rhs, t_label, f_label);
501 /* Set code, lhs, and rhs of a GIMPLE_COND from a suitable
502 boolean expression tree COND. */
504 void
505 gimple_cond_set_condition_from_tree (gimple stmt, tree cond)
507 enum tree_code code;
508 tree lhs, rhs;
510 gimple_cond_get_ops_from_tree (cond, &code, &lhs, &rhs);
511 gimple_cond_set_condition (stmt, code, lhs, rhs);
514 /* Build a GIMPLE_LABEL statement for LABEL. */
516 gimple
517 gimple_build_label (tree label)
519 gimple p = 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 gimple
527 gimple_build_goto (tree dest)
529 gimple p = gimple_build_with_ops (GIMPLE_GOTO, ERROR_MARK, 1);
530 gimple_goto_set_dest (p, dest);
531 return p;
535 /* Build a GIMPLE_NOP statement. */
537 gimple
538 gimple_build_nop (void)
540 return gimple_alloc (GIMPLE_NOP, 0);
544 /* Build a GIMPLE_BIND statement.
545 VARS are the variables in BODY.
546 BLOCK is the containing block. */
548 gimple
549 gimple_build_bind (tree vars, gimple_seq body, tree block)
551 gimple p = gimple_alloc (GIMPLE_BIND, 0);
552 gimple_bind_set_vars (p, vars);
553 if (body)
554 gimple_bind_set_body (p, body);
555 if (block)
556 gimple_bind_set_block (p, block);
557 return p;
560 /* Helper function to set the simple fields of a asm stmt.
562 STRING is a pointer to a string that is the asm blocks assembly code.
563 NINPUT is the number of register inputs.
564 NOUTPUT is the number of register outputs.
565 NCLOBBERS is the number of clobbered registers.
568 static inline gimple
569 gimple_build_asm_1 (const char *string, unsigned ninputs, unsigned noutputs,
570 unsigned nclobbers, unsigned nlabels)
572 gimple p;
573 int size = strlen (string);
575 /* ASMs with labels cannot have outputs. This should have been
576 enforced by the front end. */
577 gcc_assert (nlabels == 0 || noutputs == 0);
579 p = gimple_build_with_ops (GIMPLE_ASM, ERROR_MARK,
580 ninputs + noutputs + nclobbers + nlabels);
582 p->gimple_asm.ni = ninputs;
583 p->gimple_asm.no = noutputs;
584 p->gimple_asm.nc = nclobbers;
585 p->gimple_asm.nl = nlabels;
586 p->gimple_asm.string = ggc_alloc_string (string, size);
588 if (GATHER_STATISTICS)
589 gimple_alloc_sizes[(int) gimple_alloc_kind (GIMPLE_ASM)] += size;
591 return p;
594 /* Build a GIMPLE_ASM statement.
596 STRING is the assembly code.
597 NINPUT is the number of register inputs.
598 NOUTPUT is the number of register outputs.
599 NCLOBBERS is the number of clobbered registers.
600 INPUTS is a vector of the input register parameters.
601 OUTPUTS is a vector of the output register parameters.
602 CLOBBERS is a vector of the clobbered register parameters.
603 LABELS is a vector of destination labels. */
605 gimple
606 gimple_build_asm_vec (const char *string, vec<tree, va_gc> *inputs,
607 vec<tree, va_gc> *outputs, vec<tree, va_gc> *clobbers,
608 vec<tree, va_gc> *labels)
610 gimple p;
611 unsigned i;
613 p = gimple_build_asm_1 (string,
614 vec_safe_length (inputs),
615 vec_safe_length (outputs),
616 vec_safe_length (clobbers),
617 vec_safe_length (labels));
619 for (i = 0; i < vec_safe_length (inputs); i++)
620 gimple_asm_set_input_op (p, i, (*inputs)[i]);
622 for (i = 0; i < vec_safe_length (outputs); i++)
623 gimple_asm_set_output_op (p, i, (*outputs)[i]);
625 for (i = 0; i < vec_safe_length (clobbers); i++)
626 gimple_asm_set_clobber_op (p, i, (*clobbers)[i]);
628 for (i = 0; i < vec_safe_length (labels); i++)
629 gimple_asm_set_label_op (p, i, (*labels)[i]);
631 return p;
634 /* Build a GIMPLE_CATCH statement.
636 TYPES are the catch types.
637 HANDLER is the exception handler. */
639 gimple
640 gimple_build_catch (tree types, gimple_seq handler)
642 gimple p = gimple_alloc (GIMPLE_CATCH, 0);
643 gimple_catch_set_types (p, types);
644 if (handler)
645 gimple_catch_set_handler (p, handler);
647 return p;
650 /* Build a GIMPLE_EH_FILTER statement.
652 TYPES are the filter's types.
653 FAILURE is the filter's failure action. */
655 gimple
656 gimple_build_eh_filter (tree types, gimple_seq failure)
658 gimple p = gimple_alloc (GIMPLE_EH_FILTER, 0);
659 gimple_eh_filter_set_types (p, types);
660 if (failure)
661 gimple_eh_filter_set_failure (p, failure);
663 return p;
666 /* Build a GIMPLE_EH_MUST_NOT_THROW statement. */
668 gimple
669 gimple_build_eh_must_not_throw (tree decl)
671 gimple p = gimple_alloc (GIMPLE_EH_MUST_NOT_THROW, 0);
673 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
674 gcc_assert (flags_from_decl_or_type (decl) & ECF_NORETURN);
675 gimple_eh_must_not_throw_set_fndecl (p, decl);
677 return p;
680 /* Build a GIMPLE_EH_ELSE statement. */
682 gimple
683 gimple_build_eh_else (gimple_seq n_body, gimple_seq e_body)
685 gimple p = gimple_alloc (GIMPLE_EH_ELSE, 0);
686 gimple_eh_else_set_n_body (p, n_body);
687 gimple_eh_else_set_e_body (p, e_body);
688 return p;
691 /* Build a GIMPLE_TRY statement.
693 EVAL is the expression to evaluate.
694 CLEANUP is the cleanup expression.
695 KIND is either GIMPLE_TRY_CATCH or GIMPLE_TRY_FINALLY depending on
696 whether this is a try/catch or a try/finally respectively. */
698 gimple
699 gimple_build_try (gimple_seq eval, gimple_seq cleanup,
700 enum gimple_try_flags kind)
702 gimple p;
704 gcc_assert (kind == GIMPLE_TRY_CATCH || kind == GIMPLE_TRY_FINALLY);
705 p = gimple_alloc (GIMPLE_TRY, 0);
706 gimple_set_subcode (p, kind);
707 if (eval)
708 gimple_try_set_eval (p, eval);
709 if (cleanup)
710 gimple_try_set_cleanup (p, cleanup);
712 return p;
715 /* Construct a GIMPLE_WITH_CLEANUP_EXPR statement.
717 CLEANUP is the cleanup expression. */
719 gimple
720 gimple_build_wce (gimple_seq cleanup)
722 gimple p = gimple_alloc (GIMPLE_WITH_CLEANUP_EXPR, 0);
723 if (cleanup)
724 gimple_wce_set_cleanup (p, cleanup);
726 return p;
730 /* Build a GIMPLE_RESX statement. */
732 gimple
733 gimple_build_resx (int region)
735 gimple p = gimple_build_with_ops (GIMPLE_RESX, ERROR_MARK, 0);
736 p->gimple_eh_ctrl.region = region;
737 return p;
741 /* The helper for constructing a gimple switch statement.
742 INDEX is the switch's index.
743 NLABELS is the number of labels in the switch excluding the default.
744 DEFAULT_LABEL is the default label for the switch statement. */
746 gimple
747 gimple_build_switch_nlabels (unsigned nlabels, tree index, tree default_label)
749 /* nlabels + 1 default label + 1 index. */
750 gcc_checking_assert (default_label);
751 gimple p = gimple_build_with_ops (GIMPLE_SWITCH, ERROR_MARK,
752 1 + 1 + nlabels);
753 gimple_switch_set_index (p, index);
754 gimple_switch_set_default_label (p, default_label);
755 return p;
758 /* Build a GIMPLE_SWITCH statement.
760 INDEX is the switch's index.
761 DEFAULT_LABEL is the default label
762 ARGS is a vector of labels excluding the default. */
764 gimple
765 gimple_build_switch (tree index, tree default_label, vec<tree> args)
767 unsigned i, nlabels = args.length ();
769 gimple p = gimple_build_switch_nlabels (nlabels, index, default_label);
771 /* Copy the labels from the vector to the switch statement. */
772 for (i = 0; i < nlabels; i++)
773 gimple_switch_set_label (p, i + 1, args[i]);
775 return p;
778 /* Build a GIMPLE_EH_DISPATCH statement. */
780 gimple
781 gimple_build_eh_dispatch (int region)
783 gimple p = gimple_build_with_ops (GIMPLE_EH_DISPATCH, ERROR_MARK, 0);
784 p->gimple_eh_ctrl.region = region;
785 return p;
788 /* Build a new GIMPLE_DEBUG_BIND statement.
790 VAR is bound to VALUE; block and location are taken from STMT. */
792 gimple
793 gimple_build_debug_bind_stat (tree var, tree value, gimple stmt MEM_STAT_DECL)
795 gimple p = gimple_build_with_ops_stat (GIMPLE_DEBUG,
796 (unsigned)GIMPLE_DEBUG_BIND, 2
797 PASS_MEM_STAT);
799 gimple_debug_bind_set_var (p, var);
800 gimple_debug_bind_set_value (p, value);
801 if (stmt)
802 gimple_set_location (p, gimple_location (stmt));
804 return p;
808 /* Build a new GIMPLE_DEBUG_SOURCE_BIND statement.
810 VAR is bound to VALUE; block and location are taken from STMT. */
812 gimple
813 gimple_build_debug_source_bind_stat (tree var, tree value,
814 gimple stmt MEM_STAT_DECL)
816 gimple p = gimple_build_with_ops_stat (GIMPLE_DEBUG,
817 (unsigned)GIMPLE_DEBUG_SOURCE_BIND, 2
818 PASS_MEM_STAT);
820 gimple_debug_source_bind_set_var (p, var);
821 gimple_debug_source_bind_set_value (p, value);
822 if (stmt)
823 gimple_set_location (p, gimple_location (stmt));
825 return p;
829 /* Build a GIMPLE_OMP_CRITICAL statement.
831 BODY is the sequence of statements for which only one thread can execute.
832 NAME is optional identifier for this critical block. */
834 gimple
835 gimple_build_omp_critical (gimple_seq body, tree name)
837 gimple p = gimple_alloc (GIMPLE_OMP_CRITICAL, 0);
838 gimple_omp_critical_set_name (p, name);
839 if (body)
840 gimple_omp_set_body (p, body);
842 return p;
845 /* Build a GIMPLE_OMP_FOR statement.
847 BODY is sequence of statements inside the for loop.
848 KIND is the `for' variant.
849 CLAUSES, are any of the OMP loop construct's clauses: private, firstprivate,
850 lastprivate, reductions, ordered, schedule, and nowait.
851 COLLAPSE is the collapse count.
852 PRE_BODY is the sequence of statements that are loop invariant. */
854 gimple
855 gimple_build_omp_for (gimple_seq body, int kind, tree clauses, size_t collapse,
856 gimple_seq pre_body)
858 gimple p = gimple_alloc (GIMPLE_OMP_FOR, 0);
859 if (body)
860 gimple_omp_set_body (p, body);
861 gimple_omp_for_set_clauses (p, clauses);
862 gimple_omp_for_set_kind (p, kind);
863 p->gimple_omp_for.collapse = collapse;
864 p->gimple_omp_for.iter
865 = ggc_alloc_cleared_vec_gimple_omp_for_iter (collapse);
866 if (pre_body)
867 gimple_omp_for_set_pre_body (p, pre_body);
869 return p;
873 /* Build a GIMPLE_OMP_PARALLEL statement.
875 BODY is sequence of statements which are executed in parallel.
876 CLAUSES, are the OMP parallel construct's clauses.
877 CHILD_FN is the function created for the parallel threads to execute.
878 DATA_ARG are the shared data argument(s). */
880 gimple
881 gimple_build_omp_parallel (gimple_seq body, tree clauses, tree child_fn,
882 tree data_arg)
884 gimple p = gimple_alloc (GIMPLE_OMP_PARALLEL, 0);
885 if (body)
886 gimple_omp_set_body (p, body);
887 gimple_omp_parallel_set_clauses (p, clauses);
888 gimple_omp_parallel_set_child_fn (p, child_fn);
889 gimple_omp_parallel_set_data_arg (p, data_arg);
891 return p;
895 /* Build a GIMPLE_OMP_TASK statement.
897 BODY is sequence of statements which are executed by the explicit task.
898 CLAUSES, are the OMP parallel construct's clauses.
899 CHILD_FN is the function created for the parallel threads to execute.
900 DATA_ARG are the shared data argument(s).
901 COPY_FN is the optional function for firstprivate initialization.
902 ARG_SIZE and ARG_ALIGN are size and alignment of the data block. */
904 gimple
905 gimple_build_omp_task (gimple_seq body, tree clauses, tree child_fn,
906 tree data_arg, tree copy_fn, tree arg_size,
907 tree arg_align)
909 gimple p = gimple_alloc (GIMPLE_OMP_TASK, 0);
910 if (body)
911 gimple_omp_set_body (p, body);
912 gimple_omp_task_set_clauses (p, clauses);
913 gimple_omp_task_set_child_fn (p, child_fn);
914 gimple_omp_task_set_data_arg (p, data_arg);
915 gimple_omp_task_set_copy_fn (p, copy_fn);
916 gimple_omp_task_set_arg_size (p, arg_size);
917 gimple_omp_task_set_arg_align (p, arg_align);
919 return p;
923 /* Build a GIMPLE_OMP_SECTION statement for a sections statement.
925 BODY is the sequence of statements in the section. */
927 gimple
928 gimple_build_omp_section (gimple_seq body)
930 gimple p = gimple_alloc (GIMPLE_OMP_SECTION, 0);
931 if (body)
932 gimple_omp_set_body (p, body);
934 return p;
938 /* Build a GIMPLE_OMP_MASTER statement.
940 BODY is the sequence of statements to be executed by just the master. */
942 gimple
943 gimple_build_omp_master (gimple_seq body)
945 gimple p = gimple_alloc (GIMPLE_OMP_MASTER, 0);
946 if (body)
947 gimple_omp_set_body (p, body);
949 return p;
953 /* Build a GIMPLE_OMP_TASKGROUP statement.
955 BODY is the sequence of statements to be executed by the taskgroup
956 construct. */
958 gimple
959 gimple_build_omp_taskgroup (gimple_seq body)
961 gimple p = gimple_alloc (GIMPLE_OMP_TASKGROUP, 0);
962 if (body)
963 gimple_omp_set_body (p, body);
965 return p;
969 /* Build a GIMPLE_OMP_CONTINUE statement.
971 CONTROL_DEF is the definition of the control variable.
972 CONTROL_USE is the use of the control variable. */
974 gimple
975 gimple_build_omp_continue (tree control_def, tree control_use)
977 gimple p = gimple_alloc (GIMPLE_OMP_CONTINUE, 0);
978 gimple_omp_continue_set_control_def (p, control_def);
979 gimple_omp_continue_set_control_use (p, control_use);
980 return p;
983 /* Build a GIMPLE_OMP_ORDERED statement.
985 BODY is the sequence of statements inside a loop that will executed in
986 sequence. */
988 gimple
989 gimple_build_omp_ordered (gimple_seq body)
991 gimple p = gimple_alloc (GIMPLE_OMP_ORDERED, 0);
992 if (body)
993 gimple_omp_set_body (p, body);
995 return p;
999 /* Build a GIMPLE_OMP_RETURN statement.
1000 WAIT_P is true if this is a non-waiting return. */
1002 gimple
1003 gimple_build_omp_return (bool wait_p)
1005 gimple p = gimple_alloc (GIMPLE_OMP_RETURN, 0);
1006 if (wait_p)
1007 gimple_omp_return_set_nowait (p);
1009 return p;
1013 /* Build a GIMPLE_OMP_SECTIONS statement.
1015 BODY is a sequence of section statements.
1016 CLAUSES are any of the OMP sections contsruct's clauses: private,
1017 firstprivate, lastprivate, reduction, and nowait. */
1019 gimple
1020 gimple_build_omp_sections (gimple_seq body, tree clauses)
1022 gimple p = gimple_alloc (GIMPLE_OMP_SECTIONS, 0);
1023 if (body)
1024 gimple_omp_set_body (p, body);
1025 gimple_omp_sections_set_clauses (p, clauses);
1027 return p;
1031 /* Build a GIMPLE_OMP_SECTIONS_SWITCH. */
1033 gimple
1034 gimple_build_omp_sections_switch (void)
1036 return gimple_alloc (GIMPLE_OMP_SECTIONS_SWITCH, 0);
1040 /* Build a GIMPLE_OMP_SINGLE statement.
1042 BODY is the sequence of statements that will be executed once.
1043 CLAUSES are any of the OMP single construct's clauses: private, firstprivate,
1044 copyprivate, nowait. */
1046 gimple
1047 gimple_build_omp_single (gimple_seq body, tree clauses)
1049 gimple p = gimple_alloc (GIMPLE_OMP_SINGLE, 0);
1050 if (body)
1051 gimple_omp_set_body (p, body);
1052 gimple_omp_single_set_clauses (p, clauses);
1054 return p;
1058 /* Build a GIMPLE_OMP_TARGET statement.
1060 BODY is the sequence of statements that will be executed.
1061 CLAUSES are any of the OMP target construct's clauses. */
1063 gimple
1064 gimple_build_omp_target (gimple_seq body, int kind, tree clauses)
1066 gimple p = gimple_alloc (GIMPLE_OMP_TARGET, 0);
1067 if (body)
1068 gimple_omp_set_body (p, body);
1069 gimple_omp_target_set_clauses (p, clauses);
1070 gimple_omp_target_set_kind (p, kind);
1072 return p;
1076 /* Build a GIMPLE_OMP_TEAMS statement.
1078 BODY is the sequence of statements that will be executed.
1079 CLAUSES are any of the OMP teams construct's clauses. */
1081 gimple
1082 gimple_build_omp_teams (gimple_seq body, tree clauses)
1084 gimple p = gimple_alloc (GIMPLE_OMP_TEAMS, 0);
1085 if (body)
1086 gimple_omp_set_body (p, body);
1087 gimple_omp_teams_set_clauses (p, clauses);
1089 return p;
1093 /* Build a GIMPLE_OMP_ATOMIC_LOAD statement. */
1095 gimple
1096 gimple_build_omp_atomic_load (tree lhs, tree rhs)
1098 gimple p = gimple_alloc (GIMPLE_OMP_ATOMIC_LOAD, 0);
1099 gimple_omp_atomic_load_set_lhs (p, lhs);
1100 gimple_omp_atomic_load_set_rhs (p, rhs);
1101 return p;
1104 /* Build a GIMPLE_OMP_ATOMIC_STORE statement.
1106 VAL is the value we are storing. */
1108 gimple
1109 gimple_build_omp_atomic_store (tree val)
1111 gimple p = gimple_alloc (GIMPLE_OMP_ATOMIC_STORE, 0);
1112 gimple_omp_atomic_store_set_val (p, val);
1113 return p;
1116 /* Build a GIMPLE_TRANSACTION statement. */
1118 gimple
1119 gimple_build_transaction (gimple_seq body, tree label)
1121 gimple p = gimple_alloc (GIMPLE_TRANSACTION, 0);
1122 gimple_transaction_set_body (p, body);
1123 gimple_transaction_set_label (p, label);
1124 return p;
1127 /* Build a GIMPLE_PREDICT statement. PREDICT is one of the predictors from
1128 predict.def, OUTCOME is NOT_TAKEN or TAKEN. */
1130 gimple
1131 gimple_build_predict (enum br_predictor predictor, enum prediction outcome)
1133 gimple p = gimple_alloc (GIMPLE_PREDICT, 0);
1134 /* Ensure all the predictors fit into the lower bits of the subcode. */
1135 gcc_assert ((int) END_PREDICTORS <= GF_PREDICT_TAKEN);
1136 gimple_predict_set_predictor (p, predictor);
1137 gimple_predict_set_outcome (p, outcome);
1138 return p;
1141 #if defined ENABLE_GIMPLE_CHECKING
1142 /* Complain of a gimple type mismatch and die. */
1144 void
1145 gimple_check_failed (const_gimple gs, const char *file, int line,
1146 const char *function, enum gimple_code code,
1147 enum tree_code subcode)
1149 internal_error ("gimple check: expected %s(%s), have %s(%s) in %s, at %s:%d",
1150 gimple_code_name[code],
1151 get_tree_code_name (subcode),
1152 gimple_code_name[gimple_code (gs)],
1153 gs->gsbase.subcode > 0
1154 ? get_tree_code_name ((enum tree_code) gs->gsbase.subcode)
1155 : "",
1156 function, trim_filename (file), line);
1158 #endif /* ENABLE_GIMPLE_CHECKING */
1161 /* Link gimple statement GS to the end of the sequence *SEQ_P. If
1162 *SEQ_P is NULL, a new sequence is allocated. */
1164 void
1165 gimple_seq_add_stmt (gimple_seq *seq_p, gimple gs)
1167 gimple_stmt_iterator si;
1168 if (gs == NULL)
1169 return;
1171 si = gsi_last (*seq_p);
1172 gsi_insert_after (&si, gs, GSI_NEW_STMT);
1176 /* Append sequence SRC to the end of sequence *DST_P. If *DST_P is
1177 NULL, a new sequence is allocated. */
1179 void
1180 gimple_seq_add_seq (gimple_seq *dst_p, gimple_seq src)
1182 gimple_stmt_iterator si;
1183 if (src == NULL)
1184 return;
1186 si = gsi_last (*dst_p);
1187 gsi_insert_seq_after (&si, src, GSI_NEW_STMT);
1191 /* Helper function of empty_body_p. Return true if STMT is an empty
1192 statement. */
1194 static bool
1195 empty_stmt_p (gimple stmt)
1197 if (gimple_code (stmt) == GIMPLE_NOP)
1198 return true;
1199 if (gimple_code (stmt) == GIMPLE_BIND)
1200 return empty_body_p (gimple_bind_body (stmt));
1201 return false;
1205 /* Return true if BODY contains nothing but empty statements. */
1207 bool
1208 empty_body_p (gimple_seq body)
1210 gimple_stmt_iterator i;
1212 if (gimple_seq_empty_p (body))
1213 return true;
1214 for (i = gsi_start (body); !gsi_end_p (i); gsi_next (&i))
1215 if (!empty_stmt_p (gsi_stmt (i))
1216 && !is_gimple_debug (gsi_stmt (i)))
1217 return false;
1219 return true;
1223 /* Perform a deep copy of sequence SRC and return the result. */
1225 gimple_seq
1226 gimple_seq_copy (gimple_seq src)
1228 gimple_stmt_iterator gsi;
1229 gimple_seq new_seq = NULL;
1230 gimple stmt;
1232 for (gsi = gsi_start (src); !gsi_end_p (gsi); gsi_next (&gsi))
1234 stmt = gimple_copy (gsi_stmt (gsi));
1235 gimple_seq_add_stmt (&new_seq, stmt);
1238 return new_seq;
1242 /* Walk all the statements in the sequence *PSEQ calling walk_gimple_stmt
1243 on each one. WI is as in walk_gimple_stmt.
1245 If walk_gimple_stmt returns non-NULL, the walk is stopped, and the
1246 value is stored in WI->CALLBACK_RESULT. Also, the statement that
1247 produced the value is returned if this statement has not been
1248 removed by a callback (wi->removed_stmt). If the statement has
1249 been removed, NULL is returned.
1251 Otherwise, all the statements are walked and NULL returned. */
1253 gimple
1254 walk_gimple_seq_mod (gimple_seq *pseq, walk_stmt_fn callback_stmt,
1255 walk_tree_fn callback_op, struct walk_stmt_info *wi)
1257 gimple_stmt_iterator gsi;
1259 for (gsi = gsi_start (*pseq); !gsi_end_p (gsi); )
1261 tree ret = walk_gimple_stmt (&gsi, callback_stmt, callback_op, wi);
1262 if (ret)
1264 /* If CALLBACK_STMT or CALLBACK_OP return a value, WI must exist
1265 to hold it. */
1266 gcc_assert (wi);
1267 wi->callback_result = ret;
1269 return wi->removed_stmt ? NULL : gsi_stmt (gsi);
1272 if (!wi->removed_stmt)
1273 gsi_next (&gsi);
1276 if (wi)
1277 wi->callback_result = NULL_TREE;
1279 return NULL;
1283 /* Like walk_gimple_seq_mod, but ensure that the head of SEQ isn't
1284 changed by the callbacks. */
1286 gimple
1287 walk_gimple_seq (gimple_seq seq, walk_stmt_fn callback_stmt,
1288 walk_tree_fn callback_op, struct walk_stmt_info *wi)
1290 gimple_seq seq2 = seq;
1291 gimple ret = walk_gimple_seq_mod (&seq2, callback_stmt, callback_op, wi);
1292 gcc_assert (seq2 == seq);
1293 return ret;
1297 /* Helper function for walk_gimple_stmt. Walk operands of a GIMPLE_ASM. */
1299 static tree
1300 walk_gimple_asm (gimple stmt, walk_tree_fn callback_op,
1301 struct walk_stmt_info *wi)
1303 tree ret, op;
1304 unsigned noutputs;
1305 const char **oconstraints;
1306 unsigned i, n;
1307 const char *constraint;
1308 bool allows_mem, allows_reg, is_inout;
1310 noutputs = gimple_asm_noutputs (stmt);
1311 oconstraints = (const char **) alloca ((noutputs) * sizeof (const char *));
1313 if (wi)
1314 wi->is_lhs = true;
1316 for (i = 0; i < noutputs; i++)
1318 op = gimple_asm_output_op (stmt, i);
1319 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (op)));
1320 oconstraints[i] = constraint;
1321 parse_output_constraint (&constraint, i, 0, 0, &allows_mem, &allows_reg,
1322 &is_inout);
1323 if (wi)
1324 wi->val_only = (allows_reg || !allows_mem);
1325 ret = walk_tree (&TREE_VALUE (op), callback_op, wi, NULL);
1326 if (ret)
1327 return ret;
1330 n = gimple_asm_ninputs (stmt);
1331 for (i = 0; i < n; i++)
1333 op = gimple_asm_input_op (stmt, i);
1334 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (op)));
1335 parse_input_constraint (&constraint, 0, 0, noutputs, 0,
1336 oconstraints, &allows_mem, &allows_reg);
1337 if (wi)
1339 wi->val_only = (allows_reg || !allows_mem);
1340 /* Although input "m" is not really a LHS, we need a lvalue. */
1341 wi->is_lhs = !wi->val_only;
1343 ret = walk_tree (&TREE_VALUE (op), callback_op, wi, NULL);
1344 if (ret)
1345 return ret;
1348 if (wi)
1350 wi->is_lhs = false;
1351 wi->val_only = true;
1354 n = gimple_asm_nlabels (stmt);
1355 for (i = 0; i < n; i++)
1357 op = gimple_asm_label_op (stmt, i);
1358 ret = walk_tree (&TREE_VALUE (op), callback_op, wi, NULL);
1359 if (ret)
1360 return ret;
1363 return NULL_TREE;
1367 /* Helper function of WALK_GIMPLE_STMT. Walk every tree operand in
1368 STMT. CALLBACK_OP and WI are as in WALK_GIMPLE_STMT.
1370 CALLBACK_OP is called on each operand of STMT via walk_tree.
1371 Additional parameters to walk_tree must be stored in WI. For each operand
1372 OP, walk_tree is called as:
1374 walk_tree (&OP, CALLBACK_OP, WI, WI->PSET)
1376 If CALLBACK_OP returns non-NULL for an operand, the remaining
1377 operands are not scanned.
1379 The return value is that returned by the last call to walk_tree, or
1380 NULL_TREE if no CALLBACK_OP is specified. */
1382 tree
1383 walk_gimple_op (gimple stmt, walk_tree_fn callback_op,
1384 struct walk_stmt_info *wi)
1386 struct pointer_set_t *pset = (wi) ? wi->pset : NULL;
1387 unsigned i;
1388 tree ret = NULL_TREE;
1390 switch (gimple_code (stmt))
1392 case GIMPLE_ASSIGN:
1393 /* Walk the RHS operands. If the LHS is of a non-renamable type or
1394 is a register variable, we may use a COMPONENT_REF on the RHS. */
1395 if (wi)
1397 tree lhs = gimple_assign_lhs (stmt);
1398 wi->val_only
1399 = (is_gimple_reg_type (TREE_TYPE (lhs)) && !is_gimple_reg (lhs))
1400 || gimple_assign_rhs_class (stmt) != GIMPLE_SINGLE_RHS;
1403 for (i = 1; i < gimple_num_ops (stmt); i++)
1405 ret = walk_tree (gimple_op_ptr (stmt, i), callback_op, wi,
1406 pset);
1407 if (ret)
1408 return ret;
1411 /* Walk the LHS. If the RHS is appropriate for a memory, we
1412 may use a COMPONENT_REF on the LHS. */
1413 if (wi)
1415 /* If the RHS is of a non-renamable type or is a register variable,
1416 we may use a COMPONENT_REF on the LHS. */
1417 tree rhs1 = gimple_assign_rhs1 (stmt);
1418 wi->val_only
1419 = (is_gimple_reg_type (TREE_TYPE (rhs1)) && !is_gimple_reg (rhs1))
1420 || gimple_assign_rhs_class (stmt) != GIMPLE_SINGLE_RHS;
1421 wi->is_lhs = true;
1424 ret = walk_tree (gimple_op_ptr (stmt, 0), callback_op, wi, pset);
1425 if (ret)
1426 return ret;
1428 if (wi)
1430 wi->val_only = true;
1431 wi->is_lhs = false;
1433 break;
1435 case GIMPLE_CALL:
1436 if (wi)
1438 wi->is_lhs = false;
1439 wi->val_only = true;
1442 ret = walk_tree (gimple_call_chain_ptr (stmt), callback_op, wi, pset);
1443 if (ret)
1444 return ret;
1446 ret = walk_tree (gimple_call_fn_ptr (stmt), callback_op, wi, pset);
1447 if (ret)
1448 return ret;
1450 for (i = 0; i < gimple_call_num_args (stmt); i++)
1452 if (wi)
1453 wi->val_only
1454 = is_gimple_reg_type (TREE_TYPE (gimple_call_arg (stmt, i)));
1455 ret = walk_tree (gimple_call_arg_ptr (stmt, i), callback_op, wi,
1456 pset);
1457 if (ret)
1458 return ret;
1461 if (gimple_call_lhs (stmt))
1463 if (wi)
1465 wi->is_lhs = true;
1466 wi->val_only
1467 = is_gimple_reg_type (TREE_TYPE (gimple_call_lhs (stmt)));
1470 ret = walk_tree (gimple_call_lhs_ptr (stmt), callback_op, wi, pset);
1471 if (ret)
1472 return ret;
1475 if (wi)
1477 wi->is_lhs = false;
1478 wi->val_only = true;
1480 break;
1482 case GIMPLE_CATCH:
1483 ret = walk_tree (gimple_catch_types_ptr (stmt), callback_op, wi,
1484 pset);
1485 if (ret)
1486 return ret;
1487 break;
1489 case GIMPLE_EH_FILTER:
1490 ret = walk_tree (gimple_eh_filter_types_ptr (stmt), callback_op, wi,
1491 pset);
1492 if (ret)
1493 return ret;
1494 break;
1496 case GIMPLE_ASM:
1497 ret = walk_gimple_asm (stmt, callback_op, wi);
1498 if (ret)
1499 return ret;
1500 break;
1502 case GIMPLE_OMP_CONTINUE:
1503 ret = walk_tree (gimple_omp_continue_control_def_ptr (stmt),
1504 callback_op, wi, pset);
1505 if (ret)
1506 return ret;
1508 ret = walk_tree (gimple_omp_continue_control_use_ptr (stmt),
1509 callback_op, wi, pset);
1510 if (ret)
1511 return ret;
1512 break;
1514 case GIMPLE_OMP_CRITICAL:
1515 ret = walk_tree (gimple_omp_critical_name_ptr (stmt), callback_op, wi,
1516 pset);
1517 if (ret)
1518 return ret;
1519 break;
1521 case GIMPLE_OMP_FOR:
1522 ret = walk_tree (gimple_omp_for_clauses_ptr (stmt), callback_op, wi,
1523 pset);
1524 if (ret)
1525 return ret;
1526 for (i = 0; i < gimple_omp_for_collapse (stmt); i++)
1528 ret = walk_tree (gimple_omp_for_index_ptr (stmt, i), callback_op,
1529 wi, pset);
1530 if (ret)
1531 return ret;
1532 ret = walk_tree (gimple_omp_for_initial_ptr (stmt, i), callback_op,
1533 wi, pset);
1534 if (ret)
1535 return ret;
1536 ret = walk_tree (gimple_omp_for_final_ptr (stmt, i), callback_op,
1537 wi, pset);
1538 if (ret)
1539 return ret;
1540 ret = walk_tree (gimple_omp_for_incr_ptr (stmt, i), callback_op,
1541 wi, pset);
1543 if (ret)
1544 return ret;
1545 break;
1547 case GIMPLE_OMP_PARALLEL:
1548 ret = walk_tree (gimple_omp_parallel_clauses_ptr (stmt), callback_op,
1549 wi, pset);
1550 if (ret)
1551 return ret;
1552 ret = walk_tree (gimple_omp_parallel_child_fn_ptr (stmt), callback_op,
1553 wi, pset);
1554 if (ret)
1555 return ret;
1556 ret = walk_tree (gimple_omp_parallel_data_arg_ptr (stmt), callback_op,
1557 wi, pset);
1558 if (ret)
1559 return ret;
1560 break;
1562 case GIMPLE_OMP_TASK:
1563 ret = walk_tree (gimple_omp_task_clauses_ptr (stmt), callback_op,
1564 wi, pset);
1565 if (ret)
1566 return ret;
1567 ret = walk_tree (gimple_omp_task_child_fn_ptr (stmt), callback_op,
1568 wi, pset);
1569 if (ret)
1570 return ret;
1571 ret = walk_tree (gimple_omp_task_data_arg_ptr (stmt), callback_op,
1572 wi, pset);
1573 if (ret)
1574 return ret;
1575 ret = walk_tree (gimple_omp_task_copy_fn_ptr (stmt), callback_op,
1576 wi, pset);
1577 if (ret)
1578 return ret;
1579 ret = walk_tree (gimple_omp_task_arg_size_ptr (stmt), callback_op,
1580 wi, pset);
1581 if (ret)
1582 return ret;
1583 ret = walk_tree (gimple_omp_task_arg_align_ptr (stmt), callback_op,
1584 wi, pset);
1585 if (ret)
1586 return ret;
1587 break;
1589 case GIMPLE_OMP_SECTIONS:
1590 ret = walk_tree (gimple_omp_sections_clauses_ptr (stmt), callback_op,
1591 wi, pset);
1592 if (ret)
1593 return ret;
1595 ret = walk_tree (gimple_omp_sections_control_ptr (stmt), callback_op,
1596 wi, pset);
1597 if (ret)
1598 return ret;
1600 break;
1602 case GIMPLE_OMP_SINGLE:
1603 ret = walk_tree (gimple_omp_single_clauses_ptr (stmt), callback_op, wi,
1604 pset);
1605 if (ret)
1606 return ret;
1607 break;
1609 case GIMPLE_OMP_TARGET:
1610 ret = walk_tree (gimple_omp_target_clauses_ptr (stmt), callback_op, wi,
1611 pset);
1612 if (ret)
1613 return ret;
1614 break;
1616 case GIMPLE_OMP_TEAMS:
1617 ret = walk_tree (gimple_omp_teams_clauses_ptr (stmt), callback_op, wi,
1618 pset);
1619 if (ret)
1620 return ret;
1621 break;
1623 case GIMPLE_OMP_ATOMIC_LOAD:
1624 ret = walk_tree (gimple_omp_atomic_load_lhs_ptr (stmt), callback_op, wi,
1625 pset);
1626 if (ret)
1627 return ret;
1629 ret = walk_tree (gimple_omp_atomic_load_rhs_ptr (stmt), callback_op, wi,
1630 pset);
1631 if (ret)
1632 return ret;
1633 break;
1635 case GIMPLE_OMP_ATOMIC_STORE:
1636 ret = walk_tree (gimple_omp_atomic_store_val_ptr (stmt), callback_op,
1637 wi, pset);
1638 if (ret)
1639 return ret;
1640 break;
1642 case GIMPLE_TRANSACTION:
1643 ret = walk_tree (gimple_transaction_label_ptr (stmt), callback_op,
1644 wi, pset);
1645 if (ret)
1646 return ret;
1647 break;
1649 case GIMPLE_OMP_RETURN:
1650 ret = walk_tree (gimple_omp_return_lhs_ptr (stmt), callback_op, wi,
1651 pset);
1652 if (ret)
1653 return ret;
1654 break;
1656 /* Tuples that do not have operands. */
1657 case GIMPLE_NOP:
1658 case GIMPLE_RESX:
1659 case GIMPLE_PREDICT:
1660 break;
1662 default:
1664 enum gimple_statement_structure_enum gss;
1665 gss = gimple_statement_structure (stmt);
1666 if (gss == GSS_WITH_OPS || gss == GSS_WITH_MEM_OPS)
1667 for (i = 0; i < gimple_num_ops (stmt); i++)
1669 ret = walk_tree (gimple_op_ptr (stmt, i), callback_op, wi, pset);
1670 if (ret)
1671 return ret;
1674 break;
1677 return NULL_TREE;
1681 /* Walk the current statement in GSI (optionally using traversal state
1682 stored in WI). If WI is NULL, no state is kept during traversal.
1683 The callback CALLBACK_STMT is called. If CALLBACK_STMT indicates
1684 that it has handled all the operands of the statement, its return
1685 value is returned. Otherwise, the return value from CALLBACK_STMT
1686 is discarded and its operands are scanned.
1688 If CALLBACK_STMT is NULL or it didn't handle the operands,
1689 CALLBACK_OP is called on each operand of the statement via
1690 walk_gimple_op. If walk_gimple_op returns non-NULL for any
1691 operand, the remaining operands are not scanned. In this case, the
1692 return value from CALLBACK_OP is returned.
1694 In any other case, NULL_TREE is returned. */
1696 tree
1697 walk_gimple_stmt (gimple_stmt_iterator *gsi, walk_stmt_fn callback_stmt,
1698 walk_tree_fn callback_op, struct walk_stmt_info *wi)
1700 gimple ret;
1701 tree tree_ret;
1702 gimple stmt = gsi_stmt (*gsi);
1704 if (wi)
1706 wi->gsi = *gsi;
1707 wi->removed_stmt = false;
1709 if (wi->want_locations && gimple_has_location (stmt))
1710 input_location = gimple_location (stmt);
1713 ret = NULL;
1715 /* Invoke the statement callback. Return if the callback handled
1716 all of STMT operands by itself. */
1717 if (callback_stmt)
1719 bool handled_ops = false;
1720 tree_ret = callback_stmt (gsi, &handled_ops, wi);
1721 if (handled_ops)
1722 return tree_ret;
1724 /* If CALLBACK_STMT did not handle operands, it should not have
1725 a value to return. */
1726 gcc_assert (tree_ret == NULL);
1728 if (wi && wi->removed_stmt)
1729 return NULL;
1731 /* Re-read stmt in case the callback changed it. */
1732 stmt = gsi_stmt (*gsi);
1735 /* If CALLBACK_OP is defined, invoke it on every operand of STMT. */
1736 if (callback_op)
1738 tree_ret = walk_gimple_op (stmt, callback_op, wi);
1739 if (tree_ret)
1740 return tree_ret;
1743 /* If STMT can have statements inside (e.g. GIMPLE_BIND), walk them. */
1744 switch (gimple_code (stmt))
1746 case GIMPLE_BIND:
1747 ret = walk_gimple_seq_mod (gimple_bind_body_ptr (stmt), callback_stmt,
1748 callback_op, wi);
1749 if (ret)
1750 return wi->callback_result;
1751 break;
1753 case GIMPLE_CATCH:
1754 ret = walk_gimple_seq_mod (gimple_catch_handler_ptr (stmt), callback_stmt,
1755 callback_op, wi);
1756 if (ret)
1757 return wi->callback_result;
1758 break;
1760 case GIMPLE_EH_FILTER:
1761 ret = walk_gimple_seq_mod (gimple_eh_filter_failure_ptr (stmt), callback_stmt,
1762 callback_op, wi);
1763 if (ret)
1764 return wi->callback_result;
1765 break;
1767 case GIMPLE_EH_ELSE:
1768 ret = walk_gimple_seq_mod (gimple_eh_else_n_body_ptr (stmt),
1769 callback_stmt, callback_op, wi);
1770 if (ret)
1771 return wi->callback_result;
1772 ret = walk_gimple_seq_mod (gimple_eh_else_e_body_ptr (stmt),
1773 callback_stmt, callback_op, wi);
1774 if (ret)
1775 return wi->callback_result;
1776 break;
1778 case GIMPLE_TRY:
1779 ret = walk_gimple_seq_mod (gimple_try_eval_ptr (stmt), callback_stmt, callback_op,
1780 wi);
1781 if (ret)
1782 return wi->callback_result;
1784 ret = walk_gimple_seq_mod (gimple_try_cleanup_ptr (stmt), callback_stmt,
1785 callback_op, wi);
1786 if (ret)
1787 return wi->callback_result;
1788 break;
1790 case GIMPLE_OMP_FOR:
1791 ret = walk_gimple_seq_mod (gimple_omp_for_pre_body_ptr (stmt), callback_stmt,
1792 callback_op, wi);
1793 if (ret)
1794 return wi->callback_result;
1796 /* FALL THROUGH. */
1797 case GIMPLE_OMP_CRITICAL:
1798 case GIMPLE_OMP_MASTER:
1799 case GIMPLE_OMP_TASKGROUP:
1800 case GIMPLE_OMP_ORDERED:
1801 case GIMPLE_OMP_SECTION:
1802 case GIMPLE_OMP_PARALLEL:
1803 case GIMPLE_OMP_TASK:
1804 case GIMPLE_OMP_SECTIONS:
1805 case GIMPLE_OMP_SINGLE:
1806 case GIMPLE_OMP_TARGET:
1807 case GIMPLE_OMP_TEAMS:
1808 ret = walk_gimple_seq_mod (gimple_omp_body_ptr (stmt), callback_stmt,
1809 callback_op, wi);
1810 if (ret)
1811 return wi->callback_result;
1812 break;
1814 case GIMPLE_WITH_CLEANUP_EXPR:
1815 ret = walk_gimple_seq_mod (gimple_wce_cleanup_ptr (stmt), callback_stmt,
1816 callback_op, wi);
1817 if (ret)
1818 return wi->callback_result;
1819 break;
1821 case GIMPLE_TRANSACTION:
1822 ret = walk_gimple_seq_mod (gimple_transaction_body_ptr (stmt),
1823 callback_stmt, callback_op, wi);
1824 if (ret)
1825 return wi->callback_result;
1826 break;
1828 default:
1829 gcc_assert (!gimple_has_substatements (stmt));
1830 break;
1833 return NULL;
1837 /* Return true if calls C1 and C2 are known to go to the same function. */
1839 bool
1840 gimple_call_same_target_p (const_gimple c1, const_gimple c2)
1842 if (gimple_call_internal_p (c1))
1843 return (gimple_call_internal_p (c2)
1844 && gimple_call_internal_fn (c1) == gimple_call_internal_fn (c2));
1845 else
1846 return (gimple_call_fn (c1) == gimple_call_fn (c2)
1847 || (gimple_call_fndecl (c1)
1848 && gimple_call_fndecl (c1) == gimple_call_fndecl (c2)));
1851 /* Detect flags from a GIMPLE_CALL. This is just like
1852 call_expr_flags, but for gimple tuples. */
1855 gimple_call_flags (const_gimple stmt)
1857 int flags;
1858 tree decl = gimple_call_fndecl (stmt);
1860 if (decl)
1861 flags = flags_from_decl_or_type (decl);
1862 else if (gimple_call_internal_p (stmt))
1863 flags = internal_fn_flags (gimple_call_internal_fn (stmt));
1864 else
1865 flags = flags_from_decl_or_type (gimple_call_fntype (stmt));
1867 if (stmt->gsbase.subcode & GF_CALL_NOTHROW)
1868 flags |= ECF_NOTHROW;
1870 return flags;
1873 /* Return the "fn spec" string for call STMT. */
1875 static tree
1876 gimple_call_fnspec (const_gimple stmt)
1878 tree type, attr;
1880 type = gimple_call_fntype (stmt);
1881 if (!type)
1882 return NULL_TREE;
1884 attr = lookup_attribute ("fn spec", TYPE_ATTRIBUTES (type));
1885 if (!attr)
1886 return NULL_TREE;
1888 return TREE_VALUE (TREE_VALUE (attr));
1891 /* Detects argument flags for argument number ARG on call STMT. */
1894 gimple_call_arg_flags (const_gimple stmt, unsigned arg)
1896 tree attr = gimple_call_fnspec (stmt);
1898 if (!attr || 1 + arg >= (unsigned) TREE_STRING_LENGTH (attr))
1899 return 0;
1901 switch (TREE_STRING_POINTER (attr)[1 + arg])
1903 case 'x':
1904 case 'X':
1905 return EAF_UNUSED;
1907 case 'R':
1908 return EAF_DIRECT | EAF_NOCLOBBER | EAF_NOESCAPE;
1910 case 'r':
1911 return EAF_NOCLOBBER | EAF_NOESCAPE;
1913 case 'W':
1914 return EAF_DIRECT | EAF_NOESCAPE;
1916 case 'w':
1917 return EAF_NOESCAPE;
1919 case '.':
1920 default:
1921 return 0;
1925 /* Detects return flags for the call STMT. */
1928 gimple_call_return_flags (const_gimple stmt)
1930 tree attr;
1932 if (gimple_call_flags (stmt) & ECF_MALLOC)
1933 return ERF_NOALIAS;
1935 attr = gimple_call_fnspec (stmt);
1936 if (!attr || TREE_STRING_LENGTH (attr) < 1)
1937 return 0;
1939 switch (TREE_STRING_POINTER (attr)[0])
1941 case '1':
1942 case '2':
1943 case '3':
1944 case '4':
1945 return ERF_RETURNS_ARG | (TREE_STRING_POINTER (attr)[0] - '1');
1947 case 'm':
1948 return ERF_NOALIAS;
1950 case '.':
1951 default:
1952 return 0;
1957 /* Return true if GS is a copy assignment. */
1959 bool
1960 gimple_assign_copy_p (gimple gs)
1962 return (gimple_assign_single_p (gs)
1963 && is_gimple_val (gimple_op (gs, 1)));
1967 /* Return true if GS is a SSA_NAME copy assignment. */
1969 bool
1970 gimple_assign_ssa_name_copy_p (gimple gs)
1972 return (gimple_assign_single_p (gs)
1973 && TREE_CODE (gimple_assign_lhs (gs)) == SSA_NAME
1974 && TREE_CODE (gimple_assign_rhs1 (gs)) == SSA_NAME);
1978 /* Return true if GS is an assignment with a unary RHS, but the
1979 operator has no effect on the assigned value. The logic is adapted
1980 from STRIP_NOPS. This predicate is intended to be used in tuplifying
1981 instances in which STRIP_NOPS was previously applied to the RHS of
1982 an assignment.
1984 NOTE: In the use cases that led to the creation of this function
1985 and of gimple_assign_single_p, it is typical to test for either
1986 condition and to proceed in the same manner. In each case, the
1987 assigned value is represented by the single RHS operand of the
1988 assignment. I suspect there may be cases where gimple_assign_copy_p,
1989 gimple_assign_single_p, or equivalent logic is used where a similar
1990 treatment of unary NOPs is appropriate. */
1992 bool
1993 gimple_assign_unary_nop_p (gimple gs)
1995 return (is_gimple_assign (gs)
1996 && (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (gs))
1997 || gimple_assign_rhs_code (gs) == NON_LVALUE_EXPR)
1998 && gimple_assign_rhs1 (gs) != error_mark_node
1999 && (TYPE_MODE (TREE_TYPE (gimple_assign_lhs (gs)))
2000 == TYPE_MODE (TREE_TYPE (gimple_assign_rhs1 (gs)))));
2003 /* Set BB to be the basic block holding G. */
2005 void
2006 gimple_set_bb (gimple stmt, basic_block bb)
2008 stmt->gsbase.bb = bb;
2010 /* If the statement is a label, add the label to block-to-labels map
2011 so that we can speed up edge creation for GIMPLE_GOTOs. */
2012 if (cfun->cfg && gimple_code (stmt) == GIMPLE_LABEL)
2014 tree t;
2015 int uid;
2017 t = gimple_label_label (stmt);
2018 uid = LABEL_DECL_UID (t);
2019 if (uid == -1)
2021 unsigned old_len = vec_safe_length (label_to_block_map);
2022 LABEL_DECL_UID (t) = uid = cfun->cfg->last_label_uid++;
2023 if (old_len <= (unsigned) uid)
2025 unsigned new_len = 3 * uid / 2 + 1;
2027 vec_safe_grow_cleared (label_to_block_map, new_len);
2031 (*label_to_block_map)[uid] = bb;
2036 /* Modify the RHS of the assignment pointed-to by GSI using the
2037 operands in the expression tree EXPR.
2039 NOTE: The statement pointed-to by GSI may be reallocated if it
2040 did not have enough operand slots.
2042 This function is useful to convert an existing tree expression into
2043 the flat representation used for the RHS of a GIMPLE assignment.
2044 It will reallocate memory as needed to expand or shrink the number
2045 of operand slots needed to represent EXPR.
2047 NOTE: If you find yourself building a tree and then calling this
2048 function, you are most certainly doing it the slow way. It is much
2049 better to build a new assignment or to use the function
2050 gimple_assign_set_rhs_with_ops, which does not require an
2051 expression tree to be built. */
2053 void
2054 gimple_assign_set_rhs_from_tree (gimple_stmt_iterator *gsi, tree expr)
2056 enum tree_code subcode;
2057 tree op1, op2, op3;
2059 extract_ops_from_tree_1 (expr, &subcode, &op1, &op2, &op3);
2060 gimple_assign_set_rhs_with_ops_1 (gsi, subcode, op1, op2, op3);
2064 /* Set the RHS of assignment statement pointed-to by GSI to CODE with
2065 operands OP1, OP2 and OP3.
2067 NOTE: The statement pointed-to by GSI may be reallocated if it
2068 did not have enough operand slots. */
2070 void
2071 gimple_assign_set_rhs_with_ops_1 (gimple_stmt_iterator *gsi, enum tree_code code,
2072 tree op1, tree op2, tree op3)
2074 unsigned new_rhs_ops = get_gimple_rhs_num_ops (code);
2075 gimple stmt = gsi_stmt (*gsi);
2077 /* If the new CODE needs more operands, allocate a new statement. */
2078 if (gimple_num_ops (stmt) < new_rhs_ops + 1)
2080 tree lhs = gimple_assign_lhs (stmt);
2081 gimple new_stmt = gimple_alloc (gimple_code (stmt), new_rhs_ops + 1);
2082 memcpy (new_stmt, stmt, gimple_size (gimple_code (stmt)));
2083 gimple_init_singleton (new_stmt);
2084 gsi_replace (gsi, new_stmt, true);
2085 stmt = new_stmt;
2087 /* The LHS needs to be reset as this also changes the SSA name
2088 on the LHS. */
2089 gimple_assign_set_lhs (stmt, lhs);
2092 gimple_set_num_ops (stmt, new_rhs_ops + 1);
2093 gimple_set_subcode (stmt, code);
2094 gimple_assign_set_rhs1 (stmt, op1);
2095 if (new_rhs_ops > 1)
2096 gimple_assign_set_rhs2 (stmt, op2);
2097 if (new_rhs_ops > 2)
2098 gimple_assign_set_rhs3 (stmt, op3);
2102 /* Return the LHS of a statement that performs an assignment,
2103 either a GIMPLE_ASSIGN or a GIMPLE_CALL. Returns NULL_TREE
2104 for a call to a function that returns no value, or for a
2105 statement other than an assignment or a call. */
2107 tree
2108 gimple_get_lhs (const_gimple stmt)
2110 enum gimple_code code = gimple_code (stmt);
2112 if (code == GIMPLE_ASSIGN)
2113 return gimple_assign_lhs (stmt);
2114 else if (code == GIMPLE_CALL)
2115 return gimple_call_lhs (stmt);
2116 else
2117 return NULL_TREE;
2121 /* Set the LHS of a statement that performs an assignment,
2122 either a GIMPLE_ASSIGN or a GIMPLE_CALL. */
2124 void
2125 gimple_set_lhs (gimple stmt, tree lhs)
2127 enum gimple_code code = gimple_code (stmt);
2129 if (code == GIMPLE_ASSIGN)
2130 gimple_assign_set_lhs (stmt, lhs);
2131 else if (code == GIMPLE_CALL)
2132 gimple_call_set_lhs (stmt, lhs);
2133 else
2134 gcc_unreachable ();
2138 /* Return a deep copy of statement STMT. All the operands from STMT
2139 are reallocated and copied using unshare_expr. The DEF, USE, VDEF
2140 and VUSE operand arrays are set to empty in the new copy. The new
2141 copy isn't part of any sequence. */
2143 gimple
2144 gimple_copy (gimple stmt)
2146 enum gimple_code code = gimple_code (stmt);
2147 unsigned num_ops = gimple_num_ops (stmt);
2148 gimple copy = gimple_alloc (code, num_ops);
2149 unsigned i;
2151 /* Shallow copy all the fields from STMT. */
2152 memcpy (copy, stmt, gimple_size (code));
2153 gimple_init_singleton (copy);
2155 /* If STMT has sub-statements, deep-copy them as well. */
2156 if (gimple_has_substatements (stmt))
2158 gimple_seq new_seq;
2159 tree t;
2161 switch (gimple_code (stmt))
2163 case GIMPLE_BIND:
2164 new_seq = gimple_seq_copy (gimple_bind_body (stmt));
2165 gimple_bind_set_body (copy, new_seq);
2166 gimple_bind_set_vars (copy, unshare_expr (gimple_bind_vars (stmt)));
2167 gimple_bind_set_block (copy, gimple_bind_block (stmt));
2168 break;
2170 case GIMPLE_CATCH:
2171 new_seq = gimple_seq_copy (gimple_catch_handler (stmt));
2172 gimple_catch_set_handler (copy, new_seq);
2173 t = unshare_expr (gimple_catch_types (stmt));
2174 gimple_catch_set_types (copy, t);
2175 break;
2177 case GIMPLE_EH_FILTER:
2178 new_seq = gimple_seq_copy (gimple_eh_filter_failure (stmt));
2179 gimple_eh_filter_set_failure (copy, new_seq);
2180 t = unshare_expr (gimple_eh_filter_types (stmt));
2181 gimple_eh_filter_set_types (copy, t);
2182 break;
2184 case GIMPLE_EH_ELSE:
2185 new_seq = gimple_seq_copy (gimple_eh_else_n_body (stmt));
2186 gimple_eh_else_set_n_body (copy, new_seq);
2187 new_seq = gimple_seq_copy (gimple_eh_else_e_body (stmt));
2188 gimple_eh_else_set_e_body (copy, new_seq);
2189 break;
2191 case GIMPLE_TRY:
2192 new_seq = gimple_seq_copy (gimple_try_eval (stmt));
2193 gimple_try_set_eval (copy, new_seq);
2194 new_seq = gimple_seq_copy (gimple_try_cleanup (stmt));
2195 gimple_try_set_cleanup (copy, new_seq);
2196 break;
2198 case GIMPLE_OMP_FOR:
2199 new_seq = gimple_seq_copy (gimple_omp_for_pre_body (stmt));
2200 gimple_omp_for_set_pre_body (copy, new_seq);
2201 t = unshare_expr (gimple_omp_for_clauses (stmt));
2202 gimple_omp_for_set_clauses (copy, t);
2203 copy->gimple_omp_for.iter
2204 = ggc_alloc_vec_gimple_omp_for_iter
2205 (gimple_omp_for_collapse (stmt));
2206 for (i = 0; i < gimple_omp_for_collapse (stmt); i++)
2208 gimple_omp_for_set_cond (copy, i,
2209 gimple_omp_for_cond (stmt, i));
2210 gimple_omp_for_set_index (copy, i,
2211 gimple_omp_for_index (stmt, i));
2212 t = unshare_expr (gimple_omp_for_initial (stmt, i));
2213 gimple_omp_for_set_initial (copy, i, t);
2214 t = unshare_expr (gimple_omp_for_final (stmt, i));
2215 gimple_omp_for_set_final (copy, i, t);
2216 t = unshare_expr (gimple_omp_for_incr (stmt, i));
2217 gimple_omp_for_set_incr (copy, i, t);
2219 goto copy_omp_body;
2221 case GIMPLE_OMP_PARALLEL:
2222 t = unshare_expr (gimple_omp_parallel_clauses (stmt));
2223 gimple_omp_parallel_set_clauses (copy, t);
2224 t = unshare_expr (gimple_omp_parallel_child_fn (stmt));
2225 gimple_omp_parallel_set_child_fn (copy, t);
2226 t = unshare_expr (gimple_omp_parallel_data_arg (stmt));
2227 gimple_omp_parallel_set_data_arg (copy, t);
2228 goto copy_omp_body;
2230 case GIMPLE_OMP_TASK:
2231 t = unshare_expr (gimple_omp_task_clauses (stmt));
2232 gimple_omp_task_set_clauses (copy, t);
2233 t = unshare_expr (gimple_omp_task_child_fn (stmt));
2234 gimple_omp_task_set_child_fn (copy, t);
2235 t = unshare_expr (gimple_omp_task_data_arg (stmt));
2236 gimple_omp_task_set_data_arg (copy, t);
2237 t = unshare_expr (gimple_omp_task_copy_fn (stmt));
2238 gimple_omp_task_set_copy_fn (copy, t);
2239 t = unshare_expr (gimple_omp_task_arg_size (stmt));
2240 gimple_omp_task_set_arg_size (copy, t);
2241 t = unshare_expr (gimple_omp_task_arg_align (stmt));
2242 gimple_omp_task_set_arg_align (copy, t);
2243 goto copy_omp_body;
2245 case GIMPLE_OMP_CRITICAL:
2246 t = unshare_expr (gimple_omp_critical_name (stmt));
2247 gimple_omp_critical_set_name (copy, t);
2248 goto copy_omp_body;
2250 case GIMPLE_OMP_SECTIONS:
2251 t = unshare_expr (gimple_omp_sections_clauses (stmt));
2252 gimple_omp_sections_set_clauses (copy, t);
2253 t = unshare_expr (gimple_omp_sections_control (stmt));
2254 gimple_omp_sections_set_control (copy, t);
2255 /* FALLTHRU */
2257 case GIMPLE_OMP_SINGLE:
2258 case GIMPLE_OMP_TARGET:
2259 case GIMPLE_OMP_TEAMS:
2260 case GIMPLE_OMP_SECTION:
2261 case GIMPLE_OMP_MASTER:
2262 case GIMPLE_OMP_TASKGROUP:
2263 case GIMPLE_OMP_ORDERED:
2264 copy_omp_body:
2265 new_seq = gimple_seq_copy (gimple_omp_body (stmt));
2266 gimple_omp_set_body (copy, new_seq);
2267 break;
2269 case GIMPLE_TRANSACTION:
2270 new_seq = gimple_seq_copy (gimple_transaction_body (stmt));
2271 gimple_transaction_set_body (copy, new_seq);
2272 break;
2274 case GIMPLE_WITH_CLEANUP_EXPR:
2275 new_seq = gimple_seq_copy (gimple_wce_cleanup (stmt));
2276 gimple_wce_set_cleanup (copy, new_seq);
2277 break;
2279 default:
2280 gcc_unreachable ();
2284 /* Make copy of operands. */
2285 for (i = 0; i < num_ops; i++)
2286 gimple_set_op (copy, i, unshare_expr (gimple_op (stmt, i)));
2288 if (gimple_has_mem_ops (stmt))
2290 gimple_set_vdef (copy, gimple_vdef (stmt));
2291 gimple_set_vuse (copy, gimple_vuse (stmt));
2294 /* Clear out SSA operand vectors on COPY. */
2295 if (gimple_has_ops (stmt))
2297 gimple_set_use_ops (copy, NULL);
2299 /* SSA operands need to be updated. */
2300 gimple_set_modified (copy, true);
2303 return copy;
2307 /* Return true if statement S has side-effects. We consider a
2308 statement to have side effects if:
2310 - It is a GIMPLE_CALL not marked with ECF_PURE or ECF_CONST.
2311 - Any of its operands are marked TREE_THIS_VOLATILE or TREE_SIDE_EFFECTS. */
2313 bool
2314 gimple_has_side_effects (const_gimple s)
2316 if (is_gimple_debug (s))
2317 return false;
2319 /* We don't have to scan the arguments to check for
2320 volatile arguments, though, at present, we still
2321 do a scan to check for TREE_SIDE_EFFECTS. */
2322 if (gimple_has_volatile_ops (s))
2323 return true;
2325 if (gimple_code (s) == GIMPLE_ASM
2326 && gimple_asm_volatile_p (s))
2327 return true;
2329 if (is_gimple_call (s))
2331 int flags = gimple_call_flags (s);
2333 /* An infinite loop is considered a side effect. */
2334 if (!(flags & (ECF_CONST | ECF_PURE))
2335 || (flags & ECF_LOOPING_CONST_OR_PURE))
2336 return true;
2338 return false;
2341 return false;
2344 /* Helper for gimple_could_trap_p and gimple_assign_rhs_could_trap_p.
2345 Return true if S can trap. When INCLUDE_MEM is true, check whether
2346 the memory operations could trap. When INCLUDE_STORES is true and
2347 S is a GIMPLE_ASSIGN, the LHS of the assignment is also checked. */
2349 bool
2350 gimple_could_trap_p_1 (gimple s, bool include_mem, bool include_stores)
2352 tree t, div = NULL_TREE;
2353 enum tree_code op;
2355 if (include_mem)
2357 unsigned i, start = (is_gimple_assign (s) && !include_stores) ? 1 : 0;
2359 for (i = start; i < gimple_num_ops (s); i++)
2360 if (tree_could_trap_p (gimple_op (s, i)))
2361 return true;
2364 switch (gimple_code (s))
2366 case GIMPLE_ASM:
2367 return gimple_asm_volatile_p (s);
2369 case GIMPLE_CALL:
2370 t = gimple_call_fndecl (s);
2371 /* Assume that calls to weak functions may trap. */
2372 if (!t || !DECL_P (t) || DECL_WEAK (t))
2373 return true;
2374 return false;
2376 case GIMPLE_ASSIGN:
2377 t = gimple_expr_type (s);
2378 op = gimple_assign_rhs_code (s);
2379 if (get_gimple_rhs_class (op) == GIMPLE_BINARY_RHS)
2380 div = gimple_assign_rhs2 (s);
2381 return (operation_could_trap_p (op, FLOAT_TYPE_P (t),
2382 (INTEGRAL_TYPE_P (t)
2383 && TYPE_OVERFLOW_TRAPS (t)),
2384 div));
2386 default:
2387 break;
2390 return false;
2393 /* Return true if statement S can trap. */
2395 bool
2396 gimple_could_trap_p (gimple s)
2398 return gimple_could_trap_p_1 (s, true, true);
2401 /* Return true if RHS of a GIMPLE_ASSIGN S can trap. */
2403 bool
2404 gimple_assign_rhs_could_trap_p (gimple s)
2406 gcc_assert (is_gimple_assign (s));
2407 return gimple_could_trap_p_1 (s, true, false);
2411 /* Print debugging information for gimple stmts generated. */
2413 void
2414 dump_gimple_statistics (void)
2416 int i, total_tuples = 0, total_bytes = 0;
2418 if (! GATHER_STATISTICS)
2420 fprintf (stderr, "No gimple statistics\n");
2421 return;
2424 fprintf (stderr, "\nGIMPLE statements\n");
2425 fprintf (stderr, "Kind Stmts Bytes\n");
2426 fprintf (stderr, "---------------------------------------\n");
2427 for (i = 0; i < (int) gimple_alloc_kind_all; ++i)
2429 fprintf (stderr, "%-20s %7d %10d\n", gimple_alloc_kind_names[i],
2430 gimple_alloc_counts[i], gimple_alloc_sizes[i]);
2431 total_tuples += gimple_alloc_counts[i];
2432 total_bytes += gimple_alloc_sizes[i];
2434 fprintf (stderr, "---------------------------------------\n");
2435 fprintf (stderr, "%-20s %7d %10d\n", "Total", total_tuples, total_bytes);
2436 fprintf (stderr, "---------------------------------------\n");
2440 /* Return the number of operands needed on the RHS of a GIMPLE
2441 assignment for an expression with tree code CODE. */
2443 unsigned
2444 get_gimple_rhs_num_ops (enum tree_code code)
2446 enum gimple_rhs_class rhs_class = get_gimple_rhs_class (code);
2448 if (rhs_class == GIMPLE_UNARY_RHS || rhs_class == GIMPLE_SINGLE_RHS)
2449 return 1;
2450 else if (rhs_class == GIMPLE_BINARY_RHS)
2451 return 2;
2452 else if (rhs_class == GIMPLE_TERNARY_RHS)
2453 return 3;
2454 else
2455 gcc_unreachable ();
2458 #define DEFTREECODE(SYM, STRING, TYPE, NARGS) \
2459 (unsigned char) \
2460 ((TYPE) == tcc_unary ? GIMPLE_UNARY_RHS \
2461 : ((TYPE) == tcc_binary \
2462 || (TYPE) == tcc_comparison) ? GIMPLE_BINARY_RHS \
2463 : ((TYPE) == tcc_constant \
2464 || (TYPE) == tcc_declaration \
2465 || (TYPE) == tcc_reference) ? GIMPLE_SINGLE_RHS \
2466 : ((SYM) == TRUTH_AND_EXPR \
2467 || (SYM) == TRUTH_OR_EXPR \
2468 || (SYM) == TRUTH_XOR_EXPR) ? GIMPLE_BINARY_RHS \
2469 : (SYM) == TRUTH_NOT_EXPR ? GIMPLE_UNARY_RHS \
2470 : ((SYM) == COND_EXPR \
2471 || (SYM) == WIDEN_MULT_PLUS_EXPR \
2472 || (SYM) == WIDEN_MULT_MINUS_EXPR \
2473 || (SYM) == DOT_PROD_EXPR \
2474 || (SYM) == REALIGN_LOAD_EXPR \
2475 || (SYM) == VEC_COND_EXPR \
2476 || (SYM) == VEC_PERM_EXPR \
2477 || (SYM) == FMA_EXPR) ? GIMPLE_TERNARY_RHS \
2478 : ((SYM) == CONSTRUCTOR \
2479 || (SYM) == OBJ_TYPE_REF \
2480 || (SYM) == ASSERT_EXPR \
2481 || (SYM) == ADDR_EXPR \
2482 || (SYM) == WITH_SIZE_EXPR \
2483 || (SYM) == SSA_NAME) ? GIMPLE_SINGLE_RHS \
2484 : GIMPLE_INVALID_RHS),
2485 #define END_OF_BASE_TREE_CODES (unsigned char) GIMPLE_INVALID_RHS,
2487 const unsigned char gimple_rhs_class_table[] = {
2488 #include "all-tree.def"
2491 #undef DEFTREECODE
2492 #undef END_OF_BASE_TREE_CODES
2494 /* Given a memory reference expression T, return its base address.
2495 The base address of a memory reference expression is the main
2496 object being referenced. For instance, the base address for
2497 'array[i].fld[j]' is 'array'. You can think of this as stripping
2498 away the offset part from a memory address.
2500 This function calls handled_component_p to strip away all the inner
2501 parts of the memory reference until it reaches the base object. */
2503 tree
2504 get_base_address (tree t)
2506 while (handled_component_p (t))
2507 t = TREE_OPERAND (t, 0);
2509 if ((TREE_CODE (t) == MEM_REF
2510 || TREE_CODE (t) == TARGET_MEM_REF)
2511 && TREE_CODE (TREE_OPERAND (t, 0)) == ADDR_EXPR)
2512 t = TREE_OPERAND (TREE_OPERAND (t, 0), 0);
2514 /* ??? Either the alias oracle or all callers need to properly deal
2515 with WITH_SIZE_EXPRs before we can look through those. */
2516 if (TREE_CODE (t) == WITH_SIZE_EXPR)
2517 return NULL_TREE;
2519 return t;
2522 void
2523 recalculate_side_effects (tree t)
2525 enum tree_code code = TREE_CODE (t);
2526 int len = TREE_OPERAND_LENGTH (t);
2527 int i;
2529 switch (TREE_CODE_CLASS (code))
2531 case tcc_expression:
2532 switch (code)
2534 case INIT_EXPR:
2535 case MODIFY_EXPR:
2536 case VA_ARG_EXPR:
2537 case PREDECREMENT_EXPR:
2538 case PREINCREMENT_EXPR:
2539 case POSTDECREMENT_EXPR:
2540 case POSTINCREMENT_EXPR:
2541 /* All of these have side-effects, no matter what their
2542 operands are. */
2543 return;
2545 default:
2546 break;
2548 /* Fall through. */
2550 case tcc_comparison: /* a comparison expression */
2551 case tcc_unary: /* a unary arithmetic expression */
2552 case tcc_binary: /* a binary arithmetic expression */
2553 case tcc_reference: /* a reference */
2554 case tcc_vl_exp: /* a function call */
2555 TREE_SIDE_EFFECTS (t) = TREE_THIS_VOLATILE (t);
2556 for (i = 0; i < len; ++i)
2558 tree op = TREE_OPERAND (t, i);
2559 if (op && TREE_SIDE_EFFECTS (op))
2560 TREE_SIDE_EFFECTS (t) = 1;
2562 break;
2564 case tcc_constant:
2565 /* No side-effects. */
2566 return;
2568 default:
2569 gcc_unreachable ();
2573 /* Canonicalize a tree T for use in a COND_EXPR as conditional. Returns
2574 a canonicalized tree that is valid for a COND_EXPR or NULL_TREE, if
2575 we failed to create one. */
2577 tree
2578 canonicalize_cond_expr_cond (tree t)
2580 /* Strip conversions around boolean operations. */
2581 if (CONVERT_EXPR_P (t)
2582 && (truth_value_p (TREE_CODE (TREE_OPERAND (t, 0)))
2583 || TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0)))
2584 == BOOLEAN_TYPE))
2585 t = TREE_OPERAND (t, 0);
2587 /* For !x use x == 0. */
2588 if (TREE_CODE (t) == TRUTH_NOT_EXPR)
2590 tree top0 = TREE_OPERAND (t, 0);
2591 t = build2 (EQ_EXPR, TREE_TYPE (t),
2592 top0, build_int_cst (TREE_TYPE (top0), 0));
2594 /* For cmp ? 1 : 0 use cmp. */
2595 else if (TREE_CODE (t) == COND_EXPR
2596 && COMPARISON_CLASS_P (TREE_OPERAND (t, 0))
2597 && integer_onep (TREE_OPERAND (t, 1))
2598 && integer_zerop (TREE_OPERAND (t, 2)))
2600 tree top0 = TREE_OPERAND (t, 0);
2601 t = build2 (TREE_CODE (top0), TREE_TYPE (t),
2602 TREE_OPERAND (top0, 0), TREE_OPERAND (top0, 1));
2604 /* For x ^ y use x != y. */
2605 else if (TREE_CODE (t) == BIT_XOR_EXPR)
2606 t = build2 (NE_EXPR, TREE_TYPE (t),
2607 TREE_OPERAND (t, 0), TREE_OPERAND (t, 1));
2609 if (is_gimple_condexpr (t))
2610 return t;
2612 return NULL_TREE;
2615 /* Build a GIMPLE_CALL identical to STMT but skipping the arguments in
2616 the positions marked by the set ARGS_TO_SKIP. */
2618 gimple
2619 gimple_call_copy_skip_args (gimple stmt, bitmap args_to_skip)
2621 int i;
2622 int nargs = gimple_call_num_args (stmt);
2623 vec<tree> vargs;
2624 vargs.create (nargs);
2625 gimple new_stmt;
2627 for (i = 0; i < nargs; i++)
2628 if (!bitmap_bit_p (args_to_skip, i))
2629 vargs.quick_push (gimple_call_arg (stmt, i));
2631 if (gimple_call_internal_p (stmt))
2632 new_stmt = gimple_build_call_internal_vec (gimple_call_internal_fn (stmt),
2633 vargs);
2634 else
2635 new_stmt = gimple_build_call_vec (gimple_call_fn (stmt), vargs);
2636 vargs.release ();
2637 if (gimple_call_lhs (stmt))
2638 gimple_call_set_lhs (new_stmt, gimple_call_lhs (stmt));
2640 gimple_set_vuse (new_stmt, gimple_vuse (stmt));
2641 gimple_set_vdef (new_stmt, gimple_vdef (stmt));
2643 if (gimple_has_location (stmt))
2644 gimple_set_location (new_stmt, gimple_location (stmt));
2645 gimple_call_copy_flags (new_stmt, stmt);
2646 gimple_call_set_chain (new_stmt, gimple_call_chain (stmt));
2648 gimple_set_modified (new_stmt, true);
2650 return new_stmt;
2655 /* Return true if the field decls F1 and F2 are at the same offset.
2657 This is intended to be used on GIMPLE types only. */
2659 bool
2660 gimple_compare_field_offset (tree f1, tree f2)
2662 if (DECL_OFFSET_ALIGN (f1) == DECL_OFFSET_ALIGN (f2))
2664 tree offset1 = DECL_FIELD_OFFSET (f1);
2665 tree offset2 = DECL_FIELD_OFFSET (f2);
2666 return ((offset1 == offset2
2667 /* Once gimplification is done, self-referential offsets are
2668 instantiated as operand #2 of the COMPONENT_REF built for
2669 each access and reset. Therefore, they are not relevant
2670 anymore and fields are interchangeable provided that they
2671 represent the same access. */
2672 || (TREE_CODE (offset1) == PLACEHOLDER_EXPR
2673 && TREE_CODE (offset2) == PLACEHOLDER_EXPR
2674 && (DECL_SIZE (f1) == DECL_SIZE (f2)
2675 || (TREE_CODE (DECL_SIZE (f1)) == PLACEHOLDER_EXPR
2676 && TREE_CODE (DECL_SIZE (f2)) == PLACEHOLDER_EXPR)
2677 || operand_equal_p (DECL_SIZE (f1), DECL_SIZE (f2), 0))
2678 && DECL_ALIGN (f1) == DECL_ALIGN (f2))
2679 || operand_equal_p (offset1, offset2, 0))
2680 && tree_int_cst_equal (DECL_FIELD_BIT_OFFSET (f1),
2681 DECL_FIELD_BIT_OFFSET (f2)));
2684 /* Fortran and C do not always agree on what DECL_OFFSET_ALIGN
2685 should be, so handle differing ones specially by decomposing
2686 the offset into a byte and bit offset manually. */
2687 if (host_integerp (DECL_FIELD_OFFSET (f1), 0)
2688 && host_integerp (DECL_FIELD_OFFSET (f2), 0))
2690 unsigned HOST_WIDE_INT byte_offset1, byte_offset2;
2691 unsigned HOST_WIDE_INT bit_offset1, bit_offset2;
2692 bit_offset1 = TREE_INT_CST_LOW (DECL_FIELD_BIT_OFFSET (f1));
2693 byte_offset1 = (TREE_INT_CST_LOW (DECL_FIELD_OFFSET (f1))
2694 + bit_offset1 / BITS_PER_UNIT);
2695 bit_offset2 = TREE_INT_CST_LOW (DECL_FIELD_BIT_OFFSET (f2));
2696 byte_offset2 = (TREE_INT_CST_LOW (DECL_FIELD_OFFSET (f2))
2697 + bit_offset2 / BITS_PER_UNIT);
2698 if (byte_offset1 != byte_offset2)
2699 return false;
2700 return bit_offset1 % BITS_PER_UNIT == bit_offset2 % BITS_PER_UNIT;
2703 return false;
2707 /* Return a type the same as TYPE except unsigned or
2708 signed according to UNSIGNEDP. */
2710 static tree
2711 gimple_signed_or_unsigned_type (bool unsignedp, tree type)
2713 tree type1;
2715 type1 = TYPE_MAIN_VARIANT (type);
2716 if (type1 == signed_char_type_node
2717 || type1 == char_type_node
2718 || type1 == unsigned_char_type_node)
2719 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
2720 if (type1 == integer_type_node || type1 == unsigned_type_node)
2721 return unsignedp ? unsigned_type_node : integer_type_node;
2722 if (type1 == short_integer_type_node || type1 == short_unsigned_type_node)
2723 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
2724 if (type1 == long_integer_type_node || type1 == long_unsigned_type_node)
2725 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
2726 if (type1 == long_long_integer_type_node
2727 || type1 == long_long_unsigned_type_node)
2728 return unsignedp
2729 ? long_long_unsigned_type_node
2730 : long_long_integer_type_node;
2731 if (int128_integer_type_node && (type1 == int128_integer_type_node || type1 == int128_unsigned_type_node))
2732 return unsignedp
2733 ? int128_unsigned_type_node
2734 : int128_integer_type_node;
2735 #if HOST_BITS_PER_WIDE_INT >= 64
2736 if (type1 == intTI_type_node || type1 == unsigned_intTI_type_node)
2737 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
2738 #endif
2739 if (type1 == intDI_type_node || type1 == unsigned_intDI_type_node)
2740 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
2741 if (type1 == intSI_type_node || type1 == unsigned_intSI_type_node)
2742 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
2743 if (type1 == intHI_type_node || type1 == unsigned_intHI_type_node)
2744 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
2745 if (type1 == intQI_type_node || type1 == unsigned_intQI_type_node)
2746 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
2748 #define GIMPLE_FIXED_TYPES(NAME) \
2749 if (type1 == short_ ## NAME ## _type_node \
2750 || type1 == unsigned_short_ ## NAME ## _type_node) \
2751 return unsignedp ? unsigned_short_ ## NAME ## _type_node \
2752 : short_ ## NAME ## _type_node; \
2753 if (type1 == NAME ## _type_node \
2754 || type1 == unsigned_ ## NAME ## _type_node) \
2755 return unsignedp ? unsigned_ ## NAME ## _type_node \
2756 : NAME ## _type_node; \
2757 if (type1 == long_ ## NAME ## _type_node \
2758 || type1 == unsigned_long_ ## NAME ## _type_node) \
2759 return unsignedp ? unsigned_long_ ## NAME ## _type_node \
2760 : long_ ## NAME ## _type_node; \
2761 if (type1 == long_long_ ## NAME ## _type_node \
2762 || type1 == unsigned_long_long_ ## NAME ## _type_node) \
2763 return unsignedp ? unsigned_long_long_ ## NAME ## _type_node \
2764 : long_long_ ## NAME ## _type_node;
2766 #define GIMPLE_FIXED_MODE_TYPES(NAME) \
2767 if (type1 == NAME ## _type_node \
2768 || type1 == u ## NAME ## _type_node) \
2769 return unsignedp ? u ## NAME ## _type_node \
2770 : NAME ## _type_node;
2772 #define GIMPLE_FIXED_TYPES_SAT(NAME) \
2773 if (type1 == sat_ ## short_ ## NAME ## _type_node \
2774 || type1 == sat_ ## unsigned_short_ ## NAME ## _type_node) \
2775 return unsignedp ? sat_ ## unsigned_short_ ## NAME ## _type_node \
2776 : sat_ ## short_ ## NAME ## _type_node; \
2777 if (type1 == sat_ ## NAME ## _type_node \
2778 || type1 == sat_ ## unsigned_ ## NAME ## _type_node) \
2779 return unsignedp ? sat_ ## unsigned_ ## NAME ## _type_node \
2780 : sat_ ## NAME ## _type_node; \
2781 if (type1 == sat_ ## long_ ## NAME ## _type_node \
2782 || type1 == sat_ ## unsigned_long_ ## NAME ## _type_node) \
2783 return unsignedp ? sat_ ## unsigned_long_ ## NAME ## _type_node \
2784 : sat_ ## long_ ## NAME ## _type_node; \
2785 if (type1 == sat_ ## long_long_ ## NAME ## _type_node \
2786 || type1 == sat_ ## unsigned_long_long_ ## NAME ## _type_node) \
2787 return unsignedp ? sat_ ## unsigned_long_long_ ## NAME ## _type_node \
2788 : sat_ ## long_long_ ## NAME ## _type_node;
2790 #define GIMPLE_FIXED_MODE_TYPES_SAT(NAME) \
2791 if (type1 == sat_ ## NAME ## _type_node \
2792 || type1 == sat_ ## u ## NAME ## _type_node) \
2793 return unsignedp ? sat_ ## u ## NAME ## _type_node \
2794 : sat_ ## NAME ## _type_node;
2796 GIMPLE_FIXED_TYPES (fract);
2797 GIMPLE_FIXED_TYPES_SAT (fract);
2798 GIMPLE_FIXED_TYPES (accum);
2799 GIMPLE_FIXED_TYPES_SAT (accum);
2801 GIMPLE_FIXED_MODE_TYPES (qq);
2802 GIMPLE_FIXED_MODE_TYPES (hq);
2803 GIMPLE_FIXED_MODE_TYPES (sq);
2804 GIMPLE_FIXED_MODE_TYPES (dq);
2805 GIMPLE_FIXED_MODE_TYPES (tq);
2806 GIMPLE_FIXED_MODE_TYPES_SAT (qq);
2807 GIMPLE_FIXED_MODE_TYPES_SAT (hq);
2808 GIMPLE_FIXED_MODE_TYPES_SAT (sq);
2809 GIMPLE_FIXED_MODE_TYPES_SAT (dq);
2810 GIMPLE_FIXED_MODE_TYPES_SAT (tq);
2811 GIMPLE_FIXED_MODE_TYPES (ha);
2812 GIMPLE_FIXED_MODE_TYPES (sa);
2813 GIMPLE_FIXED_MODE_TYPES (da);
2814 GIMPLE_FIXED_MODE_TYPES (ta);
2815 GIMPLE_FIXED_MODE_TYPES_SAT (ha);
2816 GIMPLE_FIXED_MODE_TYPES_SAT (sa);
2817 GIMPLE_FIXED_MODE_TYPES_SAT (da);
2818 GIMPLE_FIXED_MODE_TYPES_SAT (ta);
2820 /* For ENUMERAL_TYPEs in C++, must check the mode of the types, not
2821 the precision; they have precision set to match their range, but
2822 may use a wider mode to match an ABI. If we change modes, we may
2823 wind up with bad conversions. For INTEGER_TYPEs in C, must check
2824 the precision as well, so as to yield correct results for
2825 bit-field types. C++ does not have these separate bit-field
2826 types, and producing a signed or unsigned variant of an
2827 ENUMERAL_TYPE may cause other problems as well. */
2828 if (!INTEGRAL_TYPE_P (type)
2829 || TYPE_UNSIGNED (type) == unsignedp)
2830 return type;
2832 #define TYPE_OK(node) \
2833 (TYPE_MODE (type) == TYPE_MODE (node) \
2834 && TYPE_PRECISION (type) == TYPE_PRECISION (node))
2835 if (TYPE_OK (signed_char_type_node))
2836 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
2837 if (TYPE_OK (integer_type_node))
2838 return unsignedp ? unsigned_type_node : integer_type_node;
2839 if (TYPE_OK (short_integer_type_node))
2840 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
2841 if (TYPE_OK (long_integer_type_node))
2842 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
2843 if (TYPE_OK (long_long_integer_type_node))
2844 return (unsignedp
2845 ? long_long_unsigned_type_node
2846 : long_long_integer_type_node);
2847 if (int128_integer_type_node && TYPE_OK (int128_integer_type_node))
2848 return (unsignedp
2849 ? int128_unsigned_type_node
2850 : int128_integer_type_node);
2852 #if HOST_BITS_PER_WIDE_INT >= 64
2853 if (TYPE_OK (intTI_type_node))
2854 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
2855 #endif
2856 if (TYPE_OK (intDI_type_node))
2857 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
2858 if (TYPE_OK (intSI_type_node))
2859 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
2860 if (TYPE_OK (intHI_type_node))
2861 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
2862 if (TYPE_OK (intQI_type_node))
2863 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
2865 #undef GIMPLE_FIXED_TYPES
2866 #undef GIMPLE_FIXED_MODE_TYPES
2867 #undef GIMPLE_FIXED_TYPES_SAT
2868 #undef GIMPLE_FIXED_MODE_TYPES_SAT
2869 #undef TYPE_OK
2871 return build_nonstandard_integer_type (TYPE_PRECISION (type), unsignedp);
2875 /* Return an unsigned type the same as TYPE in other respects. */
2877 tree
2878 gimple_unsigned_type (tree type)
2880 return gimple_signed_or_unsigned_type (true, type);
2884 /* Return a signed type the same as TYPE in other respects. */
2886 tree
2887 gimple_signed_type (tree type)
2889 return gimple_signed_or_unsigned_type (false, type);
2893 /* Return the typed-based alias set for T, which may be an expression
2894 or a type. Return -1 if we don't do anything special. */
2896 alias_set_type
2897 gimple_get_alias_set (tree t)
2899 tree u;
2901 /* Permit type-punning when accessing a union, provided the access
2902 is directly through the union. For example, this code does not
2903 permit taking the address of a union member and then storing
2904 through it. Even the type-punning allowed here is a GCC
2905 extension, albeit a common and useful one; the C standard says
2906 that such accesses have implementation-defined behavior. */
2907 for (u = t;
2908 TREE_CODE (u) == COMPONENT_REF || TREE_CODE (u) == ARRAY_REF;
2909 u = TREE_OPERAND (u, 0))
2910 if (TREE_CODE (u) == COMPONENT_REF
2911 && TREE_CODE (TREE_TYPE (TREE_OPERAND (u, 0))) == UNION_TYPE)
2912 return 0;
2914 /* That's all the expressions we handle specially. */
2915 if (!TYPE_P (t))
2916 return -1;
2918 /* For convenience, follow the C standard when dealing with
2919 character types. Any object may be accessed via an lvalue that
2920 has character type. */
2921 if (t == char_type_node
2922 || t == signed_char_type_node
2923 || t == unsigned_char_type_node)
2924 return 0;
2926 /* Allow aliasing between signed and unsigned variants of the same
2927 type. We treat the signed variant as canonical. */
2928 if (TREE_CODE (t) == INTEGER_TYPE && TYPE_UNSIGNED (t))
2930 tree t1 = gimple_signed_type (t);
2932 /* t1 == t can happen for boolean nodes which are always unsigned. */
2933 if (t1 != t)
2934 return get_alias_set (t1);
2937 return -1;
2941 /* From a tree operand OP return the base of a load or store operation
2942 or NULL_TREE if OP is not a load or a store. */
2944 static tree
2945 get_base_loadstore (tree op)
2947 while (handled_component_p (op))
2948 op = TREE_OPERAND (op, 0);
2949 if (DECL_P (op)
2950 || INDIRECT_REF_P (op)
2951 || TREE_CODE (op) == MEM_REF
2952 || TREE_CODE (op) == TARGET_MEM_REF)
2953 return op;
2954 return NULL_TREE;
2957 /* For the statement STMT call the callbacks VISIT_LOAD, VISIT_STORE and
2958 VISIT_ADDR if non-NULL on loads, store and address-taken operands
2959 passing the STMT, the base of the operand and DATA to it. The base
2960 will be either a decl, an indirect reference (including TARGET_MEM_REF)
2961 or the argument of an address expression.
2962 Returns the results of these callbacks or'ed. */
2964 bool
2965 walk_stmt_load_store_addr_ops (gimple stmt, void *data,
2966 bool (*visit_load)(gimple, tree, void *),
2967 bool (*visit_store)(gimple, tree, void *),
2968 bool (*visit_addr)(gimple, tree, void *))
2970 bool ret = false;
2971 unsigned i;
2972 if (gimple_assign_single_p (stmt))
2974 tree lhs, rhs;
2975 if (visit_store)
2977 lhs = get_base_loadstore (gimple_assign_lhs (stmt));
2978 if (lhs)
2979 ret |= visit_store (stmt, lhs, data);
2981 rhs = gimple_assign_rhs1 (stmt);
2982 while (handled_component_p (rhs))
2983 rhs = TREE_OPERAND (rhs, 0);
2984 if (visit_addr)
2986 if (TREE_CODE (rhs) == ADDR_EXPR)
2987 ret |= visit_addr (stmt, TREE_OPERAND (rhs, 0), data);
2988 else if (TREE_CODE (rhs) == TARGET_MEM_REF
2989 && TREE_CODE (TMR_BASE (rhs)) == ADDR_EXPR)
2990 ret |= visit_addr (stmt, TREE_OPERAND (TMR_BASE (rhs), 0), data);
2991 else if (TREE_CODE (rhs) == OBJ_TYPE_REF
2992 && TREE_CODE (OBJ_TYPE_REF_OBJECT (rhs)) == ADDR_EXPR)
2993 ret |= visit_addr (stmt, TREE_OPERAND (OBJ_TYPE_REF_OBJECT (rhs),
2994 0), data);
2995 else if (TREE_CODE (rhs) == CONSTRUCTOR)
2997 unsigned int ix;
2998 tree val;
3000 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (rhs), ix, val)
3001 if (TREE_CODE (val) == ADDR_EXPR)
3002 ret |= visit_addr (stmt, TREE_OPERAND (val, 0), data);
3003 else if (TREE_CODE (val) == OBJ_TYPE_REF
3004 && TREE_CODE (OBJ_TYPE_REF_OBJECT (val)) == ADDR_EXPR)
3005 ret |= visit_addr (stmt,
3006 TREE_OPERAND (OBJ_TYPE_REF_OBJECT (val),
3007 0), data);
3009 lhs = gimple_assign_lhs (stmt);
3010 if (TREE_CODE (lhs) == TARGET_MEM_REF
3011 && TREE_CODE (TMR_BASE (lhs)) == ADDR_EXPR)
3012 ret |= visit_addr (stmt, TREE_OPERAND (TMR_BASE (lhs), 0), data);
3014 if (visit_load)
3016 rhs = get_base_loadstore (rhs);
3017 if (rhs)
3018 ret |= visit_load (stmt, rhs, data);
3021 else if (visit_addr
3022 && (is_gimple_assign (stmt)
3023 || gimple_code (stmt) == GIMPLE_COND))
3025 for (i = 0; i < gimple_num_ops (stmt); ++i)
3027 tree op = gimple_op (stmt, i);
3028 if (op == NULL_TREE)
3030 else if (TREE_CODE (op) == ADDR_EXPR)
3031 ret |= visit_addr (stmt, TREE_OPERAND (op, 0), data);
3032 /* COND_EXPR and VCOND_EXPR rhs1 argument is a comparison
3033 tree with two operands. */
3034 else if (i == 1 && COMPARISON_CLASS_P (op))
3036 if (TREE_CODE (TREE_OPERAND (op, 0)) == ADDR_EXPR)
3037 ret |= visit_addr (stmt, TREE_OPERAND (TREE_OPERAND (op, 0),
3038 0), data);
3039 if (TREE_CODE (TREE_OPERAND (op, 1)) == ADDR_EXPR)
3040 ret |= visit_addr (stmt, TREE_OPERAND (TREE_OPERAND (op, 1),
3041 0), data);
3045 else if (is_gimple_call (stmt))
3047 if (visit_store)
3049 tree lhs = gimple_call_lhs (stmt);
3050 if (lhs)
3052 lhs = get_base_loadstore (lhs);
3053 if (lhs)
3054 ret |= visit_store (stmt, lhs, data);
3057 if (visit_load || visit_addr)
3058 for (i = 0; i < gimple_call_num_args (stmt); ++i)
3060 tree rhs = gimple_call_arg (stmt, i);
3061 if (visit_addr
3062 && TREE_CODE (rhs) == ADDR_EXPR)
3063 ret |= visit_addr (stmt, TREE_OPERAND (rhs, 0), data);
3064 else if (visit_load)
3066 rhs = get_base_loadstore (rhs);
3067 if (rhs)
3068 ret |= visit_load (stmt, rhs, data);
3071 if (visit_addr
3072 && gimple_call_chain (stmt)
3073 && TREE_CODE (gimple_call_chain (stmt)) == ADDR_EXPR)
3074 ret |= visit_addr (stmt, TREE_OPERAND (gimple_call_chain (stmt), 0),
3075 data);
3076 if (visit_addr
3077 && gimple_call_return_slot_opt_p (stmt)
3078 && gimple_call_lhs (stmt) != NULL_TREE
3079 && TREE_ADDRESSABLE (TREE_TYPE (gimple_call_lhs (stmt))))
3080 ret |= visit_addr (stmt, gimple_call_lhs (stmt), data);
3082 else if (gimple_code (stmt) == GIMPLE_ASM)
3084 unsigned noutputs;
3085 const char *constraint;
3086 const char **oconstraints;
3087 bool allows_mem, allows_reg, is_inout;
3088 noutputs = gimple_asm_noutputs (stmt);
3089 oconstraints = XALLOCAVEC (const char *, noutputs);
3090 if (visit_store || visit_addr)
3091 for (i = 0; i < gimple_asm_noutputs (stmt); ++i)
3093 tree link = gimple_asm_output_op (stmt, i);
3094 tree op = get_base_loadstore (TREE_VALUE (link));
3095 if (op && visit_store)
3096 ret |= visit_store (stmt, op, data);
3097 if (visit_addr)
3099 constraint = TREE_STRING_POINTER
3100 (TREE_VALUE (TREE_PURPOSE (link)));
3101 oconstraints[i] = constraint;
3102 parse_output_constraint (&constraint, i, 0, 0, &allows_mem,
3103 &allows_reg, &is_inout);
3104 if (op && !allows_reg && allows_mem)
3105 ret |= visit_addr (stmt, op, data);
3108 if (visit_load || visit_addr)
3109 for (i = 0; i < gimple_asm_ninputs (stmt); ++i)
3111 tree link = gimple_asm_input_op (stmt, i);
3112 tree op = TREE_VALUE (link);
3113 if (visit_addr
3114 && TREE_CODE (op) == ADDR_EXPR)
3115 ret |= visit_addr (stmt, TREE_OPERAND (op, 0), data);
3116 else if (visit_load || visit_addr)
3118 op = get_base_loadstore (op);
3119 if (op)
3121 if (visit_load)
3122 ret |= visit_load (stmt, op, data);
3123 if (visit_addr)
3125 constraint = TREE_STRING_POINTER
3126 (TREE_VALUE (TREE_PURPOSE (link)));
3127 parse_input_constraint (&constraint, 0, 0, noutputs,
3128 0, oconstraints,
3129 &allows_mem, &allows_reg);
3130 if (!allows_reg && allows_mem)
3131 ret |= visit_addr (stmt, op, data);
3137 else if (gimple_code (stmt) == GIMPLE_RETURN)
3139 tree op = gimple_return_retval (stmt);
3140 if (op)
3142 if (visit_addr
3143 && TREE_CODE (op) == ADDR_EXPR)
3144 ret |= visit_addr (stmt, TREE_OPERAND (op, 0), data);
3145 else if (visit_load)
3147 op = get_base_loadstore (op);
3148 if (op)
3149 ret |= visit_load (stmt, op, data);
3153 else if (visit_addr
3154 && gimple_code (stmt) == GIMPLE_PHI)
3156 for (i = 0; i < gimple_phi_num_args (stmt); ++i)
3158 tree op = gimple_phi_arg_def (stmt, i);
3159 if (TREE_CODE (op) == ADDR_EXPR)
3160 ret |= visit_addr (stmt, TREE_OPERAND (op, 0), data);
3163 else if (visit_addr
3164 && gimple_code (stmt) == GIMPLE_GOTO)
3166 tree op = gimple_goto_dest (stmt);
3167 if (TREE_CODE (op) == ADDR_EXPR)
3168 ret |= visit_addr (stmt, TREE_OPERAND (op, 0), data);
3171 return ret;
3174 /* Like walk_stmt_load_store_addr_ops but with NULL visit_addr. IPA-CP
3175 should make a faster clone for this case. */
3177 bool
3178 walk_stmt_load_store_ops (gimple stmt, void *data,
3179 bool (*visit_load)(gimple, tree, void *),
3180 bool (*visit_store)(gimple, tree, void *))
3182 return walk_stmt_load_store_addr_ops (stmt, data,
3183 visit_load, visit_store, NULL);
3186 /* Helper for gimple_ior_addresses_taken_1. */
3188 static bool
3189 gimple_ior_addresses_taken_1 (gimple stmt ATTRIBUTE_UNUSED,
3190 tree addr, void *data)
3192 bitmap addresses_taken = (bitmap)data;
3193 addr = get_base_address (addr);
3194 if (addr
3195 && DECL_P (addr))
3197 bitmap_set_bit (addresses_taken, DECL_UID (addr));
3198 return true;
3200 return false;
3203 /* Set the bit for the uid of all decls that have their address taken
3204 in STMT in the ADDRESSES_TAKEN bitmap. Returns true if there
3205 were any in this stmt. */
3207 bool
3208 gimple_ior_addresses_taken (bitmap addresses_taken, gimple stmt)
3210 return walk_stmt_load_store_addr_ops (stmt, addresses_taken, NULL, NULL,
3211 gimple_ior_addresses_taken_1);
3215 /* Return TRUE iff stmt is a call to a built-in function. */
3217 bool
3218 is_gimple_builtin_call (gimple stmt)
3220 tree callee;
3222 if (is_gimple_call (stmt)
3223 && (callee = gimple_call_fndecl (stmt))
3224 && is_builtin_fn (callee)
3225 && DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL)
3226 return true;
3228 return false;
3231 /* Return true when STMTs arguments match those of FNDECL. */
3233 static bool
3234 validate_call (gimple stmt, tree fndecl)
3236 tree targs = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
3237 unsigned nargs = gimple_call_num_args (stmt);
3238 for (unsigned i = 0; i < nargs; ++i)
3240 /* Variadic args follow. */
3241 if (!targs)
3242 return true;
3243 tree arg = gimple_call_arg (stmt, i);
3244 if (INTEGRAL_TYPE_P (TREE_TYPE (arg))
3245 && INTEGRAL_TYPE_P (TREE_VALUE (targs)))
3247 else if (POINTER_TYPE_P (TREE_TYPE (arg))
3248 && POINTER_TYPE_P (TREE_VALUE (targs)))
3250 else if (TREE_CODE (TREE_TYPE (arg))
3251 != TREE_CODE (TREE_VALUE (targs)))
3252 return false;
3253 targs = TREE_CHAIN (targs);
3255 if (targs && !VOID_TYPE_P (TREE_VALUE (targs)))
3256 return false;
3257 return true;
3260 /* Return true when STMT is builtins call to CLASS. */
3262 bool
3263 gimple_call_builtin_p (gimple stmt, enum built_in_class klass)
3265 tree fndecl;
3266 if (is_gimple_call (stmt)
3267 && (fndecl = gimple_call_fndecl (stmt)) != NULL_TREE
3268 && DECL_BUILT_IN_CLASS (fndecl) == klass)
3269 return validate_call (stmt, fndecl);
3270 return false;
3273 /* Return true when STMT is builtins call to CODE of CLASS. */
3275 bool
3276 gimple_call_builtin_p (gimple stmt, enum built_in_function code)
3278 tree fndecl;
3279 if (is_gimple_call (stmt)
3280 && (fndecl = gimple_call_fndecl (stmt)) != NULL_TREE
3281 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
3282 && DECL_FUNCTION_CODE (fndecl) == code)
3283 return validate_call (stmt, fndecl);
3284 return false;
3287 /* Return true if STMT clobbers memory. STMT is required to be a
3288 GIMPLE_ASM. */
3290 bool
3291 gimple_asm_clobbers_memory_p (const_gimple stmt)
3293 unsigned i;
3295 for (i = 0; i < gimple_asm_nclobbers (stmt); i++)
3297 tree op = gimple_asm_clobber_op (stmt, i);
3298 if (strcmp (TREE_STRING_POINTER (TREE_VALUE (op)), "memory") == 0)
3299 return true;
3302 return false;
3305 /* Dump bitmap SET (assumed to contain VAR_DECLs) to FILE. */
3307 void
3308 dump_decl_set (FILE *file, bitmap set)
3310 if (set)
3312 bitmap_iterator bi;
3313 unsigned i;
3315 fprintf (file, "{ ");
3317 EXECUTE_IF_SET_IN_BITMAP (set, 0, i, bi)
3319 fprintf (file, "D.%u", i);
3320 fprintf (file, " ");
3323 fprintf (file, "}");
3325 else
3326 fprintf (file, "NIL");
3329 /* Return true when CALL is a call stmt that definitely doesn't
3330 free any memory or makes it unavailable otherwise. */
3331 bool
3332 nonfreeing_call_p (gimple call)
3334 if (gimple_call_builtin_p (call, BUILT_IN_NORMAL)
3335 && gimple_call_flags (call) & ECF_LEAF)
3336 switch (DECL_FUNCTION_CODE (gimple_call_fndecl (call)))
3338 /* Just in case these become ECF_LEAF in the future. */
3339 case BUILT_IN_FREE:
3340 case BUILT_IN_TM_FREE:
3341 case BUILT_IN_REALLOC:
3342 case BUILT_IN_STACK_RESTORE:
3343 return false;
3344 default:
3345 return true;
3348 return false;
3351 /* Callback for walk_stmt_load_store_ops.
3353 Return TRUE if OP will dereference the tree stored in DATA, FALSE
3354 otherwise.
3356 This routine only makes a superficial check for a dereference. Thus
3357 it must only be used if it is safe to return a false negative. */
3358 static bool
3359 check_loadstore (gimple stmt ATTRIBUTE_UNUSED, tree op, void *data)
3361 if ((TREE_CODE (op) == MEM_REF || TREE_CODE (op) == TARGET_MEM_REF)
3362 && operand_equal_p (TREE_OPERAND (op, 0), (tree)data, 0))
3363 return true;
3364 return false;
3367 /* If OP can be inferred to be non-zero after STMT executes, return true. */
3369 bool
3370 infer_nonnull_range (gimple stmt, tree op)
3372 /* We can only assume that a pointer dereference will yield
3373 non-NULL if -fdelete-null-pointer-checks is enabled. */
3374 if (!flag_delete_null_pointer_checks
3375 || !POINTER_TYPE_P (TREE_TYPE (op))
3376 || gimple_code (stmt) == GIMPLE_ASM)
3377 return false;
3379 if (walk_stmt_load_store_ops (stmt, (void *)op,
3380 check_loadstore, check_loadstore))
3381 return true;
3383 if (is_gimple_call (stmt) && !gimple_call_internal_p (stmt))
3385 tree fntype = gimple_call_fntype (stmt);
3386 tree attrs = TYPE_ATTRIBUTES (fntype);
3387 for (; attrs; attrs = TREE_CHAIN (attrs))
3389 attrs = lookup_attribute ("nonnull", attrs);
3391 /* If "nonnull" wasn't specified, we know nothing about
3392 the argument. */
3393 if (attrs == NULL_TREE)
3394 return false;
3396 /* If "nonnull" applies to all the arguments, then ARG
3397 is non-null if it's in the argument list. */
3398 if (TREE_VALUE (attrs) == NULL_TREE)
3400 for (unsigned int i = 0; i < gimple_call_num_args (stmt); i++)
3402 if (operand_equal_p (op, gimple_call_arg (stmt, i), 0)
3403 && POINTER_TYPE_P (TREE_TYPE (gimple_call_arg (stmt, i))))
3404 return true;
3406 return false;
3409 /* Now see if op appears in the nonnull list. */
3410 for (tree t = TREE_VALUE (attrs); t; t = TREE_CHAIN (t))
3412 int idx = TREE_INT_CST_LOW (TREE_VALUE (t)) - 1;
3413 tree arg = gimple_call_arg (stmt, idx);
3414 if (operand_equal_p (op, arg, 0))
3415 return true;
3420 /* If this function is marked as returning non-null, then we can
3421 infer OP is non-null if it is used in the return statement. */
3422 if (gimple_code (stmt) == GIMPLE_RETURN
3423 && gimple_return_retval (stmt)
3424 && operand_equal_p (gimple_return_retval (stmt), op, 0)
3425 && lookup_attribute ("returns_nonnull",
3426 TYPE_ATTRIBUTES (TREE_TYPE (current_function_decl))))
3427 return true;
3429 return false;