1 /* Gimple IR support functions.
3 Copyright 2007, 2008, 2009, 2010 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
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
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/>. */
24 #include "coretypes.h"
29 #include "hard-reg-set.h"
30 #include "basic-block.h"
32 #include "diagnostic.h"
33 #include "tree-flow.h"
34 #include "value-prof.h"
38 #include "langhooks.h"
40 /* Global type table. FIXME lto, it should be possible to re-use some
41 of the type hashing routines in tree.c (type_hash_canon, type_hash_lookup,
42 etc), but those assume that types were built with the various
43 build_*_type routines which is not the case with the streamer. */
44 static GTY((if_marked ("ggc_marked_p"), param_is (union tree_node)))
46 static GTY((if_marked ("ggc_marked_p"), param_is (union tree_node)))
47 htab_t gimple_canonical_types;
48 static GTY((if_marked ("tree_int_map_marked_p"), param_is (struct tree_int_map)))
49 htab_t type_hash_cache;
51 /* Global type comparison cache. This is by TYPE_UID for space efficiency
52 and thus cannot use and does not need GC. */
53 static htab_t gtc_visited;
54 static struct obstack gtc_ob;
56 /* All the tuples have their operand vector (if present) at the very bottom
57 of the structure. Therefore, the offset required to find the
58 operands vector the size of the structure minus the size of the 1
59 element tree array at the end (see gimple_ops). */
60 #define DEFGSSTRUCT(SYM, STRUCT, HAS_TREE_OP) \
61 (HAS_TREE_OP ? sizeof (struct STRUCT) - sizeof (tree) : 0),
62 EXPORTED_CONST size_t gimple_ops_offset_[] = {
63 #include "gsstruct.def"
67 #define DEFGSSTRUCT(SYM, STRUCT, HAS_TREE_OP) sizeof(struct STRUCT),
68 static const size_t gsstruct_code_size[] = {
69 #include "gsstruct.def"
73 #define DEFGSCODE(SYM, NAME, GSSCODE) NAME,
74 const char *const gimple_code_name[] = {
79 #define DEFGSCODE(SYM, NAME, GSSCODE) GSSCODE,
80 EXPORTED_CONST enum gimple_statement_structure_enum gss_for_code_[] = {
85 #ifdef GATHER_STATISTICS
88 int gimple_alloc_counts[(int) gimple_alloc_kind_all];
89 int gimple_alloc_sizes[(int) gimple_alloc_kind_all];
91 /* Keep in sync with gimple.h:enum gimple_alloc_kind. */
92 static const char * const gimple_alloc_kind_names[] = {
100 #endif /* GATHER_STATISTICS */
102 /* A cache of gimple_seq objects. Sequences are created and destroyed
103 fairly often during gimplification. */
104 static GTY ((deletable)) struct gimple_seq_d *gimple_seq_cache;
106 /* Private API manipulation functions shared only with some
108 extern void gimple_set_stored_syms (gimple, bitmap, bitmap_obstack *);
109 extern void gimple_set_loaded_syms (gimple, bitmap, bitmap_obstack *);
111 /* Gimple tuple constructors.
112 Note: Any constructor taking a ``gimple_seq'' as a parameter, can
113 be passed a NULL to start with an empty sequence. */
115 /* Set the code for statement G to CODE. */
118 gimple_set_code (gimple g, enum gimple_code code)
120 g->gsbase.code = code;
123 /* Return the number of bytes needed to hold a GIMPLE statement with
127 gimple_size (enum gimple_code code)
129 return gsstruct_code_size[gss_for_code (code)];
132 /* Allocate memory for a GIMPLE statement with code CODE and NUM_OPS
136 gimple_alloc_stat (enum gimple_code code, unsigned num_ops MEM_STAT_DECL)
141 size = gimple_size (code);
143 size += sizeof (tree) * (num_ops - 1);
145 #ifdef GATHER_STATISTICS
147 enum gimple_alloc_kind kind = gimple_alloc_kind (code);
148 gimple_alloc_counts[(int) kind]++;
149 gimple_alloc_sizes[(int) kind] += size;
153 stmt = ggc_alloc_cleared_gimple_statement_d_stat (size PASS_MEM_STAT);
154 gimple_set_code (stmt, code);
155 gimple_set_num_ops (stmt, num_ops);
157 /* Do not call gimple_set_modified here as it has other side
158 effects and this tuple is still not completely built. */
159 stmt->gsbase.modified = 1;
164 /* Set SUBCODE to be the code of the expression computed by statement G. */
167 gimple_set_subcode (gimple g, unsigned subcode)
169 /* We only have 16 bits for the RHS code. Assert that we are not
171 gcc_assert (subcode < (1 << 16));
172 g->gsbase.subcode = subcode;
177 /* Build a tuple with operands. CODE is the statement to build (which
178 must be one of the GIMPLE_WITH_OPS tuples). SUBCODE is the sub-code
179 for the new tuple. NUM_OPS is the number of operands to allocate. */
181 #define gimple_build_with_ops(c, s, n) \
182 gimple_build_with_ops_stat (c, s, n MEM_STAT_INFO)
185 gimple_build_with_ops_stat (enum gimple_code code, unsigned subcode,
186 unsigned num_ops MEM_STAT_DECL)
188 gimple s = gimple_alloc_stat (code, num_ops PASS_MEM_STAT);
189 gimple_set_subcode (s, subcode);
195 /* Build a GIMPLE_RETURN statement returning RETVAL. */
198 gimple_build_return (tree retval)
200 gimple s = gimple_build_with_ops (GIMPLE_RETURN, ERROR_MARK, 1);
202 gimple_return_set_retval (s, retval);
206 /* Reset alias information on call S. */
209 gimple_call_reset_alias_info (gimple s)
211 if (gimple_call_flags (s) & ECF_CONST)
212 memset (gimple_call_use_set (s), 0, sizeof (struct pt_solution));
214 pt_solution_reset (gimple_call_use_set (s));
215 if (gimple_call_flags (s) & (ECF_CONST|ECF_PURE|ECF_NOVOPS))
216 memset (gimple_call_clobber_set (s), 0, sizeof (struct pt_solution));
218 pt_solution_reset (gimple_call_clobber_set (s));
221 /* Helper for gimple_build_call, gimple_build_call_vec and
222 gimple_build_call_from_tree. Build the basic components of a
223 GIMPLE_CALL statement to function FN with NARGS arguments. */
226 gimple_build_call_1 (tree fn, unsigned nargs)
228 gimple s = gimple_build_with_ops (GIMPLE_CALL, ERROR_MARK, nargs + 3);
229 if (TREE_CODE (fn) == FUNCTION_DECL)
230 fn = build_fold_addr_expr (fn);
231 gimple_set_op (s, 1, fn);
232 gimple_call_reset_alias_info (s);
237 /* Build a GIMPLE_CALL statement to function FN with the arguments
238 specified in vector ARGS. */
241 gimple_build_call_vec (tree fn, VEC(tree, heap) *args)
244 unsigned nargs = VEC_length (tree, args);
245 gimple call = gimple_build_call_1 (fn, nargs);
247 for (i = 0; i < nargs; i++)
248 gimple_call_set_arg (call, i, VEC_index (tree, args, i));
254 /* Build a GIMPLE_CALL statement to function FN. NARGS is the number of
255 arguments. The ... are the arguments. */
258 gimple_build_call (tree fn, unsigned nargs, ...)
264 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL || is_gimple_call_addr (fn));
266 call = gimple_build_call_1 (fn, nargs);
268 va_start (ap, nargs);
269 for (i = 0; i < nargs; i++)
270 gimple_call_set_arg (call, i, va_arg (ap, tree));
277 /* Build a GIMPLE_CALL statement from CALL_EXPR T. Note that T is
278 assumed to be in GIMPLE form already. Minimal checking is done of
282 gimple_build_call_from_tree (tree t)
286 tree fndecl = get_callee_fndecl (t);
288 gcc_assert (TREE_CODE (t) == CALL_EXPR);
290 nargs = call_expr_nargs (t);
291 call = gimple_build_call_1 (fndecl ? fndecl : CALL_EXPR_FN (t), nargs);
293 for (i = 0; i < nargs; i++)
294 gimple_call_set_arg (call, i, CALL_EXPR_ARG (t, i));
296 gimple_set_block (call, TREE_BLOCK (t));
298 /* Carry all the CALL_EXPR flags to the new GIMPLE_CALL. */
299 gimple_call_set_chain (call, CALL_EXPR_STATIC_CHAIN (t));
300 gimple_call_set_tail (call, CALL_EXPR_TAILCALL (t));
301 gimple_call_set_cannot_inline (call, CALL_CANNOT_INLINE_P (t));
302 gimple_call_set_return_slot_opt (call, CALL_EXPR_RETURN_SLOT_OPT (t));
303 gimple_call_set_from_thunk (call, CALL_FROM_THUNK_P (t));
304 gimple_call_set_va_arg_pack (call, CALL_EXPR_VA_ARG_PACK (t));
305 gimple_call_set_nothrow (call, TREE_NOTHROW (t));
306 gimple_set_no_warning (call, TREE_NO_WARNING (t));
312 /* Extract the operands and code for expression EXPR into *SUBCODE_P,
313 *OP1_P, *OP2_P and *OP3_P respectively. */
316 extract_ops_from_tree_1 (tree expr, enum tree_code *subcode_p, tree *op1_p,
317 tree *op2_p, tree *op3_p)
319 enum gimple_rhs_class grhs_class;
321 *subcode_p = TREE_CODE (expr);
322 grhs_class = get_gimple_rhs_class (*subcode_p);
324 if (grhs_class == GIMPLE_TERNARY_RHS)
326 *op1_p = TREE_OPERAND (expr, 0);
327 *op2_p = TREE_OPERAND (expr, 1);
328 *op3_p = TREE_OPERAND (expr, 2);
330 else if (grhs_class == GIMPLE_BINARY_RHS)
332 *op1_p = TREE_OPERAND (expr, 0);
333 *op2_p = TREE_OPERAND (expr, 1);
336 else if (grhs_class == GIMPLE_UNARY_RHS)
338 *op1_p = TREE_OPERAND (expr, 0);
342 else if (grhs_class == GIMPLE_SINGLE_RHS)
353 /* Build a GIMPLE_ASSIGN statement.
355 LHS of the assignment.
356 RHS of the assignment which can be unary or binary. */
359 gimple_build_assign_stat (tree lhs, tree rhs MEM_STAT_DECL)
361 enum tree_code subcode;
364 extract_ops_from_tree_1 (rhs, &subcode, &op1, &op2, &op3);
365 return gimple_build_assign_with_ops_stat (subcode, lhs, op1, op2, op3
370 /* Build a GIMPLE_ASSIGN statement with sub-code SUBCODE and operands
371 OP1 and OP2. If OP2 is NULL then SUBCODE must be of class
372 GIMPLE_UNARY_RHS or GIMPLE_SINGLE_RHS. */
375 gimple_build_assign_with_ops_stat (enum tree_code subcode, tree lhs, tree op1,
376 tree op2, tree op3 MEM_STAT_DECL)
381 /* Need 1 operand for LHS and 1 or 2 for the RHS (depending on the
383 num_ops = get_gimple_rhs_num_ops (subcode) + 1;
385 p = gimple_build_with_ops_stat (GIMPLE_ASSIGN, (unsigned)subcode, num_ops
387 gimple_assign_set_lhs (p, lhs);
388 gimple_assign_set_rhs1 (p, op1);
391 gcc_assert (num_ops > 2);
392 gimple_assign_set_rhs2 (p, op2);
397 gcc_assert (num_ops > 3);
398 gimple_assign_set_rhs3 (p, op3);
405 /* Build a new GIMPLE_ASSIGN tuple and append it to the end of *SEQ_P.
407 DST/SRC are the destination and source respectively. You can pass
408 ungimplified trees in DST or SRC, in which case they will be
409 converted to a gimple operand if necessary.
411 This function returns the newly created GIMPLE_ASSIGN tuple. */
414 gimplify_assign (tree dst, tree src, gimple_seq *seq_p)
416 tree t = build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, src);
417 gimplify_and_add (t, seq_p);
419 return gimple_seq_last_stmt (*seq_p);
423 /* Build a GIMPLE_COND statement.
425 PRED is the condition used to compare LHS and the RHS.
426 T_LABEL is the label to jump to if the condition is true.
427 F_LABEL is the label to jump to otherwise. */
430 gimple_build_cond (enum tree_code pred_code, tree lhs, tree rhs,
431 tree t_label, tree f_label)
435 gcc_assert (TREE_CODE_CLASS (pred_code) == tcc_comparison);
436 p = gimple_build_with_ops (GIMPLE_COND, pred_code, 4);
437 gimple_cond_set_lhs (p, lhs);
438 gimple_cond_set_rhs (p, rhs);
439 gimple_cond_set_true_label (p, t_label);
440 gimple_cond_set_false_label (p, f_label);
445 /* Extract operands for a GIMPLE_COND statement out of COND_EXPR tree COND. */
448 gimple_cond_get_ops_from_tree (tree cond, enum tree_code *code_p,
449 tree *lhs_p, tree *rhs_p)
451 gcc_assert (TREE_CODE_CLASS (TREE_CODE (cond)) == tcc_comparison
452 || TREE_CODE (cond) == TRUTH_NOT_EXPR
453 || is_gimple_min_invariant (cond)
454 || SSA_VAR_P (cond));
456 extract_ops_from_tree (cond, code_p, lhs_p, rhs_p);
458 /* Canonicalize conditionals of the form 'if (!VAL)'. */
459 if (*code_p == TRUTH_NOT_EXPR)
462 gcc_assert (*lhs_p && *rhs_p == NULL_TREE);
463 *rhs_p = build_zero_cst (TREE_TYPE (*lhs_p));
465 /* Canonicalize conditionals of the form 'if (VAL)' */
466 else if (TREE_CODE_CLASS (*code_p) != tcc_comparison)
469 gcc_assert (*lhs_p && *rhs_p == NULL_TREE);
470 *rhs_p = build_zero_cst (TREE_TYPE (*lhs_p));
475 /* Build a GIMPLE_COND statement from the conditional expression tree
476 COND. T_LABEL and F_LABEL are as in gimple_build_cond. */
479 gimple_build_cond_from_tree (tree cond, tree t_label, tree f_label)
484 gimple_cond_get_ops_from_tree (cond, &code, &lhs, &rhs);
485 return gimple_build_cond (code, lhs, rhs, t_label, f_label);
488 /* Set code, lhs, and rhs of a GIMPLE_COND from a suitable
489 boolean expression tree COND. */
492 gimple_cond_set_condition_from_tree (gimple stmt, tree cond)
497 gimple_cond_get_ops_from_tree (cond, &code, &lhs, &rhs);
498 gimple_cond_set_condition (stmt, code, lhs, rhs);
501 /* Build a GIMPLE_LABEL statement for LABEL. */
504 gimple_build_label (tree label)
506 gimple p = gimple_build_with_ops (GIMPLE_LABEL, ERROR_MARK, 1);
507 gimple_label_set_label (p, label);
511 /* Build a GIMPLE_GOTO statement to label DEST. */
514 gimple_build_goto (tree dest)
516 gimple p = gimple_build_with_ops (GIMPLE_GOTO, ERROR_MARK, 1);
517 gimple_goto_set_dest (p, dest);
522 /* Build a GIMPLE_NOP statement. */
525 gimple_build_nop (void)
527 return gimple_alloc (GIMPLE_NOP, 0);
531 /* Build a GIMPLE_BIND statement.
532 VARS are the variables in BODY.
533 BLOCK is the containing block. */
536 gimple_build_bind (tree vars, gimple_seq body, tree block)
538 gimple p = gimple_alloc (GIMPLE_BIND, 0);
539 gimple_bind_set_vars (p, vars);
541 gimple_bind_set_body (p, body);
543 gimple_bind_set_block (p, block);
547 /* Helper function to set the simple fields of a asm stmt.
549 STRING is a pointer to a string that is the asm blocks assembly code.
550 NINPUT is the number of register inputs.
551 NOUTPUT is the number of register outputs.
552 NCLOBBERS is the number of clobbered registers.
556 gimple_build_asm_1 (const char *string, unsigned ninputs, unsigned noutputs,
557 unsigned nclobbers, unsigned nlabels)
560 int size = strlen (string);
562 /* ASMs with labels cannot have outputs. This should have been
563 enforced by the front end. */
564 gcc_assert (nlabels == 0 || noutputs == 0);
566 p = gimple_build_with_ops (GIMPLE_ASM, ERROR_MARK,
567 ninputs + noutputs + nclobbers + nlabels);
569 p->gimple_asm.ni = ninputs;
570 p->gimple_asm.no = noutputs;
571 p->gimple_asm.nc = nclobbers;
572 p->gimple_asm.nl = nlabels;
573 p->gimple_asm.string = ggc_alloc_string (string, size);
575 #ifdef GATHER_STATISTICS
576 gimple_alloc_sizes[(int) gimple_alloc_kind (GIMPLE_ASM)] += size;
582 /* Build a GIMPLE_ASM statement.
584 STRING is the assembly code.
585 NINPUT is the number of register inputs.
586 NOUTPUT is the number of register outputs.
587 NCLOBBERS is the number of clobbered registers.
588 INPUTS is a vector of the input register parameters.
589 OUTPUTS is a vector of the output register parameters.
590 CLOBBERS is a vector of the clobbered register parameters.
591 LABELS is a vector of destination labels. */
594 gimple_build_asm_vec (const char *string, VEC(tree,gc)* inputs,
595 VEC(tree,gc)* outputs, VEC(tree,gc)* clobbers,
596 VEC(tree,gc)* labels)
601 p = gimple_build_asm_1 (string,
602 VEC_length (tree, inputs),
603 VEC_length (tree, outputs),
604 VEC_length (tree, clobbers),
605 VEC_length (tree, labels));
607 for (i = 0; i < VEC_length (tree, inputs); i++)
608 gimple_asm_set_input_op (p, i, VEC_index (tree, inputs, i));
610 for (i = 0; i < VEC_length (tree, outputs); i++)
611 gimple_asm_set_output_op (p, i, VEC_index (tree, outputs, i));
613 for (i = 0; i < VEC_length (tree, clobbers); i++)
614 gimple_asm_set_clobber_op (p, i, VEC_index (tree, clobbers, i));
616 for (i = 0; i < VEC_length (tree, labels); i++)
617 gimple_asm_set_label_op (p, i, VEC_index (tree, labels, i));
622 /* Build a GIMPLE_CATCH statement.
624 TYPES are the catch types.
625 HANDLER is the exception handler. */
628 gimple_build_catch (tree types, gimple_seq handler)
630 gimple p = gimple_alloc (GIMPLE_CATCH, 0);
631 gimple_catch_set_types (p, types);
633 gimple_catch_set_handler (p, handler);
638 /* Build a GIMPLE_EH_FILTER statement.
640 TYPES are the filter's types.
641 FAILURE is the filter's failure action. */
644 gimple_build_eh_filter (tree types, gimple_seq failure)
646 gimple p = gimple_alloc (GIMPLE_EH_FILTER, 0);
647 gimple_eh_filter_set_types (p, types);
649 gimple_eh_filter_set_failure (p, failure);
654 /* Build a GIMPLE_EH_MUST_NOT_THROW statement. */
657 gimple_build_eh_must_not_throw (tree decl)
659 gimple p = gimple_alloc (GIMPLE_EH_MUST_NOT_THROW, 0);
661 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
662 gcc_assert (flags_from_decl_or_type (decl) & ECF_NORETURN);
663 gimple_eh_must_not_throw_set_fndecl (p, decl);
668 /* Build a GIMPLE_TRY statement.
670 EVAL is the expression to evaluate.
671 CLEANUP is the cleanup expression.
672 KIND is either GIMPLE_TRY_CATCH or GIMPLE_TRY_FINALLY depending on
673 whether this is a try/catch or a try/finally respectively. */
676 gimple_build_try (gimple_seq eval, gimple_seq cleanup,
677 enum gimple_try_flags kind)
681 gcc_assert (kind == GIMPLE_TRY_CATCH || kind == GIMPLE_TRY_FINALLY);
682 p = gimple_alloc (GIMPLE_TRY, 0);
683 gimple_set_subcode (p, kind);
685 gimple_try_set_eval (p, eval);
687 gimple_try_set_cleanup (p, cleanup);
692 /* Construct a GIMPLE_WITH_CLEANUP_EXPR statement.
694 CLEANUP is the cleanup expression. */
697 gimple_build_wce (gimple_seq cleanup)
699 gimple p = gimple_alloc (GIMPLE_WITH_CLEANUP_EXPR, 0);
701 gimple_wce_set_cleanup (p, cleanup);
707 /* Build a GIMPLE_RESX statement. */
710 gimple_build_resx (int region)
712 gimple p = gimple_build_with_ops (GIMPLE_RESX, ERROR_MARK, 0);
713 p->gimple_eh_ctrl.region = region;
718 /* The helper for constructing a gimple switch statement.
719 INDEX is the switch's index.
720 NLABELS is the number of labels in the switch excluding the default.
721 DEFAULT_LABEL is the default label for the switch statement. */
724 gimple_build_switch_nlabels (unsigned nlabels, tree index, tree default_label)
726 /* nlabels + 1 default label + 1 index. */
727 gimple p = gimple_build_with_ops (GIMPLE_SWITCH, ERROR_MARK,
728 1 + (default_label != NULL) + nlabels);
729 gimple_switch_set_index (p, index);
731 gimple_switch_set_default_label (p, default_label);
736 /* Build a GIMPLE_SWITCH statement.
738 INDEX is the switch's index.
739 NLABELS is the number of labels in the switch excluding the DEFAULT_LABEL.
740 ... are the labels excluding the default. */
743 gimple_build_switch (unsigned nlabels, tree index, tree default_label, ...)
747 gimple p = gimple_build_switch_nlabels (nlabels, index, default_label);
749 /* Store the rest of the labels. */
750 va_start (al, default_label);
751 offset = (default_label != NULL);
752 for (i = 0; i < nlabels; i++)
753 gimple_switch_set_label (p, i + offset, va_arg (al, tree));
760 /* Build a GIMPLE_SWITCH statement.
762 INDEX is the switch's index.
763 DEFAULT_LABEL is the default label
764 ARGS is a vector of labels excluding the default. */
767 gimple_build_switch_vec (tree index, tree default_label, VEC(tree, heap) *args)
769 unsigned i, offset, nlabels = VEC_length (tree, args);
770 gimple p = gimple_build_switch_nlabels (nlabels, index, default_label);
772 /* Copy the labels from the vector to the switch statement. */
773 offset = (default_label != NULL);
774 for (i = 0; i < nlabels; i++)
775 gimple_switch_set_label (p, i + offset, VEC_index (tree, args, i));
780 /* Build a GIMPLE_EH_DISPATCH statement. */
783 gimple_build_eh_dispatch (int region)
785 gimple p = gimple_build_with_ops (GIMPLE_EH_DISPATCH, ERROR_MARK, 0);
786 p->gimple_eh_ctrl.region = region;
790 /* Build a new GIMPLE_DEBUG_BIND statement.
792 VAR is bound to VALUE; block and location are taken from STMT. */
795 gimple_build_debug_bind_stat (tree var, tree value, gimple stmt MEM_STAT_DECL)
797 gimple p = gimple_build_with_ops_stat (GIMPLE_DEBUG,
798 (unsigned)GIMPLE_DEBUG_BIND, 2
801 gimple_debug_bind_set_var (p, var);
802 gimple_debug_bind_set_value (p, value);
805 gimple_set_block (p, gimple_block (stmt));
806 gimple_set_location (p, gimple_location (stmt));
813 /* Build a GIMPLE_OMP_CRITICAL statement.
815 BODY is the sequence of statements for which only one thread can execute.
816 NAME is optional identifier for this critical block. */
819 gimple_build_omp_critical (gimple_seq body, tree name)
821 gimple p = gimple_alloc (GIMPLE_OMP_CRITICAL, 0);
822 gimple_omp_critical_set_name (p, name);
824 gimple_omp_set_body (p, body);
829 /* Build a GIMPLE_OMP_FOR statement.
831 BODY is sequence of statements inside the for loop.
832 CLAUSES, are any of the OMP loop construct's clauses: private, firstprivate,
833 lastprivate, reductions, ordered, schedule, and nowait.
834 COLLAPSE is the collapse count.
835 PRE_BODY is the sequence of statements that are loop invariant. */
838 gimple_build_omp_for (gimple_seq body, tree clauses, size_t collapse,
841 gimple p = gimple_alloc (GIMPLE_OMP_FOR, 0);
843 gimple_omp_set_body (p, body);
844 gimple_omp_for_set_clauses (p, clauses);
845 p->gimple_omp_for.collapse = collapse;
846 p->gimple_omp_for.iter
847 = ggc_alloc_cleared_vec_gimple_omp_for_iter (collapse);
849 gimple_omp_for_set_pre_body (p, pre_body);
855 /* Build a GIMPLE_OMP_PARALLEL statement.
857 BODY is sequence of statements which are executed in parallel.
858 CLAUSES, are the OMP parallel construct's clauses.
859 CHILD_FN is the function created for the parallel threads to execute.
860 DATA_ARG are the shared data argument(s). */
863 gimple_build_omp_parallel (gimple_seq body, tree clauses, tree child_fn,
866 gimple p = gimple_alloc (GIMPLE_OMP_PARALLEL, 0);
868 gimple_omp_set_body (p, body);
869 gimple_omp_parallel_set_clauses (p, clauses);
870 gimple_omp_parallel_set_child_fn (p, child_fn);
871 gimple_omp_parallel_set_data_arg (p, data_arg);
877 /* Build a GIMPLE_OMP_TASK statement.
879 BODY is sequence of statements which are executed by the explicit task.
880 CLAUSES, are the OMP parallel construct's clauses.
881 CHILD_FN is the function created for the parallel threads to execute.
882 DATA_ARG are the shared data argument(s).
883 COPY_FN is the optional function for firstprivate initialization.
884 ARG_SIZE and ARG_ALIGN are size and alignment of the data block. */
887 gimple_build_omp_task (gimple_seq body, tree clauses, tree child_fn,
888 tree data_arg, tree copy_fn, tree arg_size,
891 gimple p = gimple_alloc (GIMPLE_OMP_TASK, 0);
893 gimple_omp_set_body (p, body);
894 gimple_omp_task_set_clauses (p, clauses);
895 gimple_omp_task_set_child_fn (p, child_fn);
896 gimple_omp_task_set_data_arg (p, data_arg);
897 gimple_omp_task_set_copy_fn (p, copy_fn);
898 gimple_omp_task_set_arg_size (p, arg_size);
899 gimple_omp_task_set_arg_align (p, arg_align);
905 /* Build a GIMPLE_OMP_SECTION statement for a sections statement.
907 BODY is the sequence of statements in the section. */
910 gimple_build_omp_section (gimple_seq body)
912 gimple p = gimple_alloc (GIMPLE_OMP_SECTION, 0);
914 gimple_omp_set_body (p, body);
920 /* Build a GIMPLE_OMP_MASTER statement.
922 BODY is the sequence of statements to be executed by just the master. */
925 gimple_build_omp_master (gimple_seq body)
927 gimple p = gimple_alloc (GIMPLE_OMP_MASTER, 0);
929 gimple_omp_set_body (p, body);
935 /* Build a GIMPLE_OMP_CONTINUE statement.
937 CONTROL_DEF is the definition of the control variable.
938 CONTROL_USE is the use of the control variable. */
941 gimple_build_omp_continue (tree control_def, tree control_use)
943 gimple p = gimple_alloc (GIMPLE_OMP_CONTINUE, 0);
944 gimple_omp_continue_set_control_def (p, control_def);
945 gimple_omp_continue_set_control_use (p, control_use);
949 /* Build a GIMPLE_OMP_ORDERED statement.
951 BODY is the sequence of statements inside a loop that will executed in
955 gimple_build_omp_ordered (gimple_seq body)
957 gimple p = gimple_alloc (GIMPLE_OMP_ORDERED, 0);
959 gimple_omp_set_body (p, body);
965 /* Build a GIMPLE_OMP_RETURN statement.
966 WAIT_P is true if this is a non-waiting return. */
969 gimple_build_omp_return (bool wait_p)
971 gimple p = gimple_alloc (GIMPLE_OMP_RETURN, 0);
973 gimple_omp_return_set_nowait (p);
979 /* Build a GIMPLE_OMP_SECTIONS statement.
981 BODY is a sequence of section statements.
982 CLAUSES are any of the OMP sections contsruct's clauses: private,
983 firstprivate, lastprivate, reduction, and nowait. */
986 gimple_build_omp_sections (gimple_seq body, tree clauses)
988 gimple p = gimple_alloc (GIMPLE_OMP_SECTIONS, 0);
990 gimple_omp_set_body (p, body);
991 gimple_omp_sections_set_clauses (p, clauses);
997 /* Build a GIMPLE_OMP_SECTIONS_SWITCH. */
1000 gimple_build_omp_sections_switch (void)
1002 return gimple_alloc (GIMPLE_OMP_SECTIONS_SWITCH, 0);
1006 /* Build a GIMPLE_OMP_SINGLE statement.
1008 BODY is the sequence of statements that will be executed once.
1009 CLAUSES are any of the OMP single construct's clauses: private, firstprivate,
1010 copyprivate, nowait. */
1013 gimple_build_omp_single (gimple_seq body, tree clauses)
1015 gimple p = gimple_alloc (GIMPLE_OMP_SINGLE, 0);
1017 gimple_omp_set_body (p, body);
1018 gimple_omp_single_set_clauses (p, clauses);
1024 /* Build a GIMPLE_OMP_ATOMIC_LOAD statement. */
1027 gimple_build_omp_atomic_load (tree lhs, tree rhs)
1029 gimple p = gimple_alloc (GIMPLE_OMP_ATOMIC_LOAD, 0);
1030 gimple_omp_atomic_load_set_lhs (p, lhs);
1031 gimple_omp_atomic_load_set_rhs (p, rhs);
1035 /* Build a GIMPLE_OMP_ATOMIC_STORE statement.
1037 VAL is the value we are storing. */
1040 gimple_build_omp_atomic_store (tree val)
1042 gimple p = gimple_alloc (GIMPLE_OMP_ATOMIC_STORE, 0);
1043 gimple_omp_atomic_store_set_val (p, val);
1047 /* Build a GIMPLE_PREDICT statement. PREDICT is one of the predictors from
1048 predict.def, OUTCOME is NOT_TAKEN or TAKEN. */
1051 gimple_build_predict (enum br_predictor predictor, enum prediction outcome)
1053 gimple p = gimple_alloc (GIMPLE_PREDICT, 0);
1054 /* Ensure all the predictors fit into the lower bits of the subcode. */
1055 gcc_assert ((int) END_PREDICTORS <= GF_PREDICT_TAKEN);
1056 gimple_predict_set_predictor (p, predictor);
1057 gimple_predict_set_outcome (p, outcome);
1061 #if defined ENABLE_GIMPLE_CHECKING
1062 /* Complain of a gimple type mismatch and die. */
1065 gimple_check_failed (const_gimple gs, const char *file, int line,
1066 const char *function, enum gimple_code code,
1067 enum tree_code subcode)
1069 internal_error ("gimple check: expected %s(%s), have %s(%s) in %s, at %s:%d",
1070 gimple_code_name[code],
1071 tree_code_name[subcode],
1072 gimple_code_name[gimple_code (gs)],
1073 gs->gsbase.subcode > 0
1074 ? tree_code_name[gs->gsbase.subcode]
1076 function, trim_filename (file), line);
1078 #endif /* ENABLE_GIMPLE_CHECKING */
1081 /* Allocate a new GIMPLE sequence in GC memory and return it. If
1082 there are free sequences in GIMPLE_SEQ_CACHE return one of those
1086 gimple_seq_alloc (void)
1088 gimple_seq seq = gimple_seq_cache;
1091 gimple_seq_cache = gimple_seq_cache->next_free;
1092 gcc_assert (gimple_seq_cache != seq);
1093 memset (seq, 0, sizeof (*seq));
1097 seq = ggc_alloc_cleared_gimple_seq_d ();
1098 #ifdef GATHER_STATISTICS
1099 gimple_alloc_counts[(int) gimple_alloc_kind_seq]++;
1100 gimple_alloc_sizes[(int) gimple_alloc_kind_seq] += sizeof (*seq);
1107 /* Return SEQ to the free pool of GIMPLE sequences. */
1110 gimple_seq_free (gimple_seq seq)
1115 gcc_assert (gimple_seq_first (seq) == NULL);
1116 gcc_assert (gimple_seq_last (seq) == NULL);
1118 /* If this triggers, it's a sign that the same list is being freed
1120 gcc_assert (seq != gimple_seq_cache || gimple_seq_cache == NULL);
1122 /* Add SEQ to the pool of free sequences. */
1123 seq->next_free = gimple_seq_cache;
1124 gimple_seq_cache = seq;
1128 /* Link gimple statement GS to the end of the sequence *SEQ_P. If
1129 *SEQ_P is NULL, a new sequence is allocated. */
1132 gimple_seq_add_stmt (gimple_seq *seq_p, gimple gs)
1134 gimple_stmt_iterator si;
1140 *seq_p = gimple_seq_alloc ();
1142 si = gsi_last (*seq_p);
1143 gsi_insert_after (&si, gs, GSI_NEW_STMT);
1147 /* Append sequence SRC to the end of sequence *DST_P. If *DST_P is
1148 NULL, a new sequence is allocated. */
1151 gimple_seq_add_seq (gimple_seq *dst_p, gimple_seq src)
1153 gimple_stmt_iterator si;
1159 *dst_p = gimple_seq_alloc ();
1161 si = gsi_last (*dst_p);
1162 gsi_insert_seq_after (&si, src, GSI_NEW_STMT);
1166 /* Helper function of empty_body_p. Return true if STMT is an empty
1170 empty_stmt_p (gimple stmt)
1172 if (gimple_code (stmt) == GIMPLE_NOP)
1174 if (gimple_code (stmt) == GIMPLE_BIND)
1175 return empty_body_p (gimple_bind_body (stmt));
1180 /* Return true if BODY contains nothing but empty statements. */
1183 empty_body_p (gimple_seq body)
1185 gimple_stmt_iterator i;
1187 if (gimple_seq_empty_p (body))
1189 for (i = gsi_start (body); !gsi_end_p (i); gsi_next (&i))
1190 if (!empty_stmt_p (gsi_stmt (i))
1191 && !is_gimple_debug (gsi_stmt (i)))
1198 /* Perform a deep copy of sequence SRC and return the result. */
1201 gimple_seq_copy (gimple_seq src)
1203 gimple_stmt_iterator gsi;
1204 gimple_seq new_seq = gimple_seq_alloc ();
1207 for (gsi = gsi_start (src); !gsi_end_p (gsi); gsi_next (&gsi))
1209 stmt = gimple_copy (gsi_stmt (gsi));
1210 gimple_seq_add_stmt (&new_seq, stmt);
1217 /* Walk all the statements in the sequence SEQ calling walk_gimple_stmt
1218 on each one. WI is as in walk_gimple_stmt.
1220 If walk_gimple_stmt returns non-NULL, the walk is stopped, the
1221 value is stored in WI->CALLBACK_RESULT and the statement that
1222 produced the value is returned.
1224 Otherwise, all the statements are walked and NULL returned. */
1227 walk_gimple_seq (gimple_seq seq, walk_stmt_fn callback_stmt,
1228 walk_tree_fn callback_op, struct walk_stmt_info *wi)
1230 gimple_stmt_iterator gsi;
1232 for (gsi = gsi_start (seq); !gsi_end_p (gsi); gsi_next (&gsi))
1234 tree ret = walk_gimple_stmt (&gsi, callback_stmt, callback_op, wi);
1237 /* If CALLBACK_STMT or CALLBACK_OP return a value, WI must exist
1240 wi->callback_result = ret;
1241 return gsi_stmt (gsi);
1246 wi->callback_result = NULL_TREE;
1252 /* Helper function for walk_gimple_stmt. Walk operands of a GIMPLE_ASM. */
1255 walk_gimple_asm (gimple stmt, walk_tree_fn callback_op,
1256 struct walk_stmt_info *wi)
1260 const char **oconstraints;
1262 const char *constraint;
1263 bool allows_mem, allows_reg, is_inout;
1265 noutputs = gimple_asm_noutputs (stmt);
1266 oconstraints = (const char **) alloca ((noutputs) * sizeof (const char *));
1271 for (i = 0; i < noutputs; i++)
1273 op = gimple_asm_output_op (stmt, i);
1274 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (op)));
1275 oconstraints[i] = constraint;
1276 parse_output_constraint (&constraint, i, 0, 0, &allows_mem, &allows_reg,
1279 wi->val_only = (allows_reg || !allows_mem);
1280 ret = walk_tree (&TREE_VALUE (op), callback_op, wi, NULL);
1285 n = gimple_asm_ninputs (stmt);
1286 for (i = 0; i < n; i++)
1288 op = gimple_asm_input_op (stmt, i);
1289 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (op)));
1290 parse_input_constraint (&constraint, 0, 0, noutputs, 0,
1291 oconstraints, &allows_mem, &allows_reg);
1294 wi->val_only = (allows_reg || !allows_mem);
1295 /* Although input "m" is not really a LHS, we need a lvalue. */
1296 wi->is_lhs = !wi->val_only;
1298 ret = walk_tree (&TREE_VALUE (op), callback_op, wi, NULL);
1306 wi->val_only = true;
1309 n = gimple_asm_nlabels (stmt);
1310 for (i = 0; i < n; i++)
1312 op = gimple_asm_label_op (stmt, i);
1313 ret = walk_tree (&TREE_VALUE (op), callback_op, wi, NULL);
1322 /* Helper function of WALK_GIMPLE_STMT. Walk every tree operand in
1323 STMT. CALLBACK_OP and WI are as in WALK_GIMPLE_STMT.
1325 CALLBACK_OP is called on each operand of STMT via walk_tree.
1326 Additional parameters to walk_tree must be stored in WI. For each operand
1327 OP, walk_tree is called as:
1329 walk_tree (&OP, CALLBACK_OP, WI, WI->PSET)
1331 If CALLBACK_OP returns non-NULL for an operand, the remaining
1332 operands are not scanned.
1334 The return value is that returned by the last call to walk_tree, or
1335 NULL_TREE if no CALLBACK_OP is specified. */
1338 walk_gimple_op (gimple stmt, walk_tree_fn callback_op,
1339 struct walk_stmt_info *wi)
1341 struct pointer_set_t *pset = (wi) ? wi->pset : NULL;
1343 tree ret = NULL_TREE;
1345 switch (gimple_code (stmt))
1348 /* Walk the RHS operands. If the LHS is of a non-renamable type or
1349 is a register variable, we may use a COMPONENT_REF on the RHS. */
1352 tree lhs = gimple_assign_lhs (stmt);
1354 = (is_gimple_reg_type (TREE_TYPE (lhs)) && !is_gimple_reg (lhs))
1355 || !gimple_assign_single_p (stmt);
1358 for (i = 1; i < gimple_num_ops (stmt); i++)
1360 ret = walk_tree (gimple_op_ptr (stmt, i), callback_op, wi,
1366 /* Walk the LHS. If the RHS is appropriate for a memory, we
1367 may use a COMPONENT_REF on the LHS. */
1370 /* If the RHS has more than 1 operand, it is not appropriate
1372 wi->val_only = !is_gimple_mem_rhs (gimple_assign_rhs1 (stmt))
1373 || !gimple_assign_single_p (stmt);
1377 ret = walk_tree (gimple_op_ptr (stmt, 0), callback_op, wi, pset);
1383 wi->val_only = true;
1392 wi->val_only = true;
1395 ret = walk_tree (gimple_call_chain_ptr (stmt), callback_op, wi, pset);
1399 ret = walk_tree (gimple_call_fn_ptr (stmt), callback_op, wi, pset);
1403 for (i = 0; i < gimple_call_num_args (stmt); i++)
1406 wi->val_only = is_gimple_reg_type (gimple_call_arg (stmt, i));
1407 ret = walk_tree (gimple_call_arg_ptr (stmt, i), callback_op, wi,
1413 if (gimple_call_lhs (stmt))
1418 wi->val_only = is_gimple_reg_type (gimple_call_lhs (stmt));
1421 ret = walk_tree (gimple_call_lhs_ptr (stmt), callback_op, wi, pset);
1429 wi->val_only = true;
1434 ret = walk_tree (gimple_catch_types_ptr (stmt), callback_op, wi,
1440 case GIMPLE_EH_FILTER:
1441 ret = walk_tree (gimple_eh_filter_types_ptr (stmt), callback_op, wi,
1448 ret = walk_gimple_asm (stmt, callback_op, wi);
1453 case GIMPLE_OMP_CONTINUE:
1454 ret = walk_tree (gimple_omp_continue_control_def_ptr (stmt),
1455 callback_op, wi, pset);
1459 ret = walk_tree (gimple_omp_continue_control_use_ptr (stmt),
1460 callback_op, wi, pset);
1465 case GIMPLE_OMP_CRITICAL:
1466 ret = walk_tree (gimple_omp_critical_name_ptr (stmt), callback_op, wi,
1472 case GIMPLE_OMP_FOR:
1473 ret = walk_tree (gimple_omp_for_clauses_ptr (stmt), callback_op, wi,
1477 for (i = 0; i < gimple_omp_for_collapse (stmt); i++)
1479 ret = walk_tree (gimple_omp_for_index_ptr (stmt, i), callback_op,
1483 ret = walk_tree (gimple_omp_for_initial_ptr (stmt, i), callback_op,
1487 ret = walk_tree (gimple_omp_for_final_ptr (stmt, i), callback_op,
1491 ret = walk_tree (gimple_omp_for_incr_ptr (stmt, i), callback_op,
1498 case GIMPLE_OMP_PARALLEL:
1499 ret = walk_tree (gimple_omp_parallel_clauses_ptr (stmt), callback_op,
1503 ret = walk_tree (gimple_omp_parallel_child_fn_ptr (stmt), callback_op,
1507 ret = walk_tree (gimple_omp_parallel_data_arg_ptr (stmt), callback_op,
1513 case GIMPLE_OMP_TASK:
1514 ret = walk_tree (gimple_omp_task_clauses_ptr (stmt), callback_op,
1518 ret = walk_tree (gimple_omp_task_child_fn_ptr (stmt), callback_op,
1522 ret = walk_tree (gimple_omp_task_data_arg_ptr (stmt), callback_op,
1526 ret = walk_tree (gimple_omp_task_copy_fn_ptr (stmt), callback_op,
1530 ret = walk_tree (gimple_omp_task_arg_size_ptr (stmt), callback_op,
1534 ret = walk_tree (gimple_omp_task_arg_align_ptr (stmt), callback_op,
1540 case GIMPLE_OMP_SECTIONS:
1541 ret = walk_tree (gimple_omp_sections_clauses_ptr (stmt), callback_op,
1546 ret = walk_tree (gimple_omp_sections_control_ptr (stmt), callback_op,
1553 case GIMPLE_OMP_SINGLE:
1554 ret = walk_tree (gimple_omp_single_clauses_ptr (stmt), callback_op, wi,
1560 case GIMPLE_OMP_ATOMIC_LOAD:
1561 ret = walk_tree (gimple_omp_atomic_load_lhs_ptr (stmt), callback_op, wi,
1566 ret = walk_tree (gimple_omp_atomic_load_rhs_ptr (stmt), callback_op, wi,
1572 case GIMPLE_OMP_ATOMIC_STORE:
1573 ret = walk_tree (gimple_omp_atomic_store_val_ptr (stmt), callback_op,
1579 /* Tuples that do not have operands. */
1582 case GIMPLE_OMP_RETURN:
1583 case GIMPLE_PREDICT:
1588 enum gimple_statement_structure_enum gss;
1589 gss = gimple_statement_structure (stmt);
1590 if (gss == GSS_WITH_OPS || gss == GSS_WITH_MEM_OPS)
1591 for (i = 0; i < gimple_num_ops (stmt); i++)
1593 ret = walk_tree (gimple_op_ptr (stmt, i), callback_op, wi, pset);
1605 /* Walk the current statement in GSI (optionally using traversal state
1606 stored in WI). If WI is NULL, no state is kept during traversal.
1607 The callback CALLBACK_STMT is called. If CALLBACK_STMT indicates
1608 that it has handled all the operands of the statement, its return
1609 value is returned. Otherwise, the return value from CALLBACK_STMT
1610 is discarded and its operands are scanned.
1612 If CALLBACK_STMT is NULL or it didn't handle the operands,
1613 CALLBACK_OP is called on each operand of the statement via
1614 walk_gimple_op. If walk_gimple_op returns non-NULL for any
1615 operand, the remaining operands are not scanned. In this case, the
1616 return value from CALLBACK_OP is returned.
1618 In any other case, NULL_TREE is returned. */
1621 walk_gimple_stmt (gimple_stmt_iterator *gsi, walk_stmt_fn callback_stmt,
1622 walk_tree_fn callback_op, struct walk_stmt_info *wi)
1626 gimple stmt = gsi_stmt (*gsi);
1631 if (wi && wi->want_locations && gimple_has_location (stmt))
1632 input_location = gimple_location (stmt);
1636 /* Invoke the statement callback. Return if the callback handled
1637 all of STMT operands by itself. */
1640 bool handled_ops = false;
1641 tree_ret = callback_stmt (gsi, &handled_ops, wi);
1645 /* If CALLBACK_STMT did not handle operands, it should not have
1646 a value to return. */
1647 gcc_assert (tree_ret == NULL);
1649 /* Re-read stmt in case the callback changed it. */
1650 stmt = gsi_stmt (*gsi);
1653 /* If CALLBACK_OP is defined, invoke it on every operand of STMT. */
1656 tree_ret = walk_gimple_op (stmt, callback_op, wi);
1661 /* If STMT can have statements inside (e.g. GIMPLE_BIND), walk them. */
1662 switch (gimple_code (stmt))
1665 ret = walk_gimple_seq (gimple_bind_body (stmt), callback_stmt,
1668 return wi->callback_result;
1672 ret = walk_gimple_seq (gimple_catch_handler (stmt), callback_stmt,
1675 return wi->callback_result;
1678 case GIMPLE_EH_FILTER:
1679 ret = walk_gimple_seq (gimple_eh_filter_failure (stmt), callback_stmt,
1682 return wi->callback_result;
1686 ret = walk_gimple_seq (gimple_try_eval (stmt), callback_stmt, callback_op,
1689 return wi->callback_result;
1691 ret = walk_gimple_seq (gimple_try_cleanup (stmt), callback_stmt,
1694 return wi->callback_result;
1697 case GIMPLE_OMP_FOR:
1698 ret = walk_gimple_seq (gimple_omp_for_pre_body (stmt), callback_stmt,
1701 return wi->callback_result;
1704 case GIMPLE_OMP_CRITICAL:
1705 case GIMPLE_OMP_MASTER:
1706 case GIMPLE_OMP_ORDERED:
1707 case GIMPLE_OMP_SECTION:
1708 case GIMPLE_OMP_PARALLEL:
1709 case GIMPLE_OMP_TASK:
1710 case GIMPLE_OMP_SECTIONS:
1711 case GIMPLE_OMP_SINGLE:
1712 ret = walk_gimple_seq (gimple_omp_body (stmt), callback_stmt, callback_op,
1715 return wi->callback_result;
1718 case GIMPLE_WITH_CLEANUP_EXPR:
1719 ret = walk_gimple_seq (gimple_wce_cleanup (stmt), callback_stmt,
1722 return wi->callback_result;
1726 gcc_assert (!gimple_has_substatements (stmt));
1734 /* Set sequence SEQ to be the GIMPLE body for function FN. */
1737 gimple_set_body (tree fndecl, gimple_seq seq)
1739 struct function *fn = DECL_STRUCT_FUNCTION (fndecl);
1742 /* If FNDECL still does not have a function structure associated
1743 with it, then it does not make sense for it to receive a
1745 gcc_assert (seq == NULL);
1748 fn->gimple_body = seq;
1752 /* Return the body of GIMPLE statements for function FN. After the
1753 CFG pass, the function body doesn't exist anymore because it has
1754 been split up into basic blocks. In this case, it returns
1758 gimple_body (tree fndecl)
1760 struct function *fn = DECL_STRUCT_FUNCTION (fndecl);
1761 return fn ? fn->gimple_body : NULL;
1764 /* Return true when FNDECL has Gimple body either in unlowered
1767 gimple_has_body_p (tree fndecl)
1769 struct function *fn = DECL_STRUCT_FUNCTION (fndecl);
1770 return (gimple_body (fndecl) || (fn && fn->cfg));
1773 /* Detect flags from a GIMPLE_CALL. This is just like
1774 call_expr_flags, but for gimple tuples. */
1777 gimple_call_flags (const_gimple stmt)
1780 tree decl = gimple_call_fndecl (stmt);
1784 flags = flags_from_decl_or_type (decl);
1787 t = TREE_TYPE (gimple_call_fn (stmt));
1788 if (t && TREE_CODE (t) == POINTER_TYPE)
1789 flags = flags_from_decl_or_type (TREE_TYPE (t));
1794 if (stmt->gsbase.subcode & GF_CALL_NOTHROW)
1795 flags |= ECF_NOTHROW;
1800 /* Detects argument flags for argument number ARG on call STMT. */
1803 gimple_call_arg_flags (const_gimple stmt, unsigned arg)
1805 tree type = TREE_TYPE (TREE_TYPE (gimple_call_fn (stmt)));
1806 tree attr = lookup_attribute ("fn spec", TYPE_ATTRIBUTES (type));
1810 attr = TREE_VALUE (TREE_VALUE (attr));
1811 if (1 + arg >= (unsigned) TREE_STRING_LENGTH (attr))
1814 switch (TREE_STRING_POINTER (attr)[1 + arg])
1821 return EAF_DIRECT | EAF_NOCLOBBER | EAF_NOESCAPE;
1824 return EAF_NOCLOBBER | EAF_NOESCAPE;
1827 return EAF_DIRECT | EAF_NOESCAPE;
1830 return EAF_NOESCAPE;
1838 /* Detects return flags for the call STMT. */
1841 gimple_call_return_flags (const_gimple stmt)
1844 tree attr = NULL_TREE;
1846 if (gimple_call_flags (stmt) & ECF_MALLOC)
1849 type = TREE_TYPE (TREE_TYPE (gimple_call_fn (stmt)));
1850 attr = lookup_attribute ("fn spec", TYPE_ATTRIBUTES (type));
1854 attr = TREE_VALUE (TREE_VALUE (attr));
1855 if (TREE_STRING_LENGTH (attr) < 1)
1858 switch (TREE_STRING_POINTER (attr)[0])
1864 return ERF_RETURNS_ARG | (TREE_STRING_POINTER (attr)[0] - '1');
1876 /* Return true if GS is a copy assignment. */
1879 gimple_assign_copy_p (gimple gs)
1881 return (gimple_assign_single_p (gs)
1882 && is_gimple_val (gimple_op (gs, 1)));
1886 /* Return true if GS is a SSA_NAME copy assignment. */
1889 gimple_assign_ssa_name_copy_p (gimple gs)
1891 return (gimple_assign_single_p (gs)
1892 && TREE_CODE (gimple_assign_lhs (gs)) == SSA_NAME
1893 && TREE_CODE (gimple_assign_rhs1 (gs)) == SSA_NAME);
1897 /* Return true if GS is an assignment with a unary RHS, but the
1898 operator has no effect on the assigned value. The logic is adapted
1899 from STRIP_NOPS. This predicate is intended to be used in tuplifying
1900 instances in which STRIP_NOPS was previously applied to the RHS of
1903 NOTE: In the use cases that led to the creation of this function
1904 and of gimple_assign_single_p, it is typical to test for either
1905 condition and to proceed in the same manner. In each case, the
1906 assigned value is represented by the single RHS operand of the
1907 assignment. I suspect there may be cases where gimple_assign_copy_p,
1908 gimple_assign_single_p, or equivalent logic is used where a similar
1909 treatment of unary NOPs is appropriate. */
1912 gimple_assign_unary_nop_p (gimple gs)
1914 return (is_gimple_assign (gs)
1915 && (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (gs))
1916 || gimple_assign_rhs_code (gs) == NON_LVALUE_EXPR)
1917 && gimple_assign_rhs1 (gs) != error_mark_node
1918 && (TYPE_MODE (TREE_TYPE (gimple_assign_lhs (gs)))
1919 == TYPE_MODE (TREE_TYPE (gimple_assign_rhs1 (gs)))));
1922 /* Set BB to be the basic block holding G. */
1925 gimple_set_bb (gimple stmt, basic_block bb)
1927 stmt->gsbase.bb = bb;
1929 /* If the statement is a label, add the label to block-to-labels map
1930 so that we can speed up edge creation for GIMPLE_GOTOs. */
1931 if (cfun->cfg && gimple_code (stmt) == GIMPLE_LABEL)
1936 t = gimple_label_label (stmt);
1937 uid = LABEL_DECL_UID (t);
1940 unsigned old_len = VEC_length (basic_block, label_to_block_map);
1941 LABEL_DECL_UID (t) = uid = cfun->cfg->last_label_uid++;
1942 if (old_len <= (unsigned) uid)
1944 unsigned new_len = 3 * uid / 2 + 1;
1946 VEC_safe_grow_cleared (basic_block, gc, label_to_block_map,
1951 VEC_replace (basic_block, label_to_block_map, uid, bb);
1956 /* Modify the RHS of the assignment pointed-to by GSI using the
1957 operands in the expression tree EXPR.
1959 NOTE: The statement pointed-to by GSI may be reallocated if it
1960 did not have enough operand slots.
1962 This function is useful to convert an existing tree expression into
1963 the flat representation used for the RHS of a GIMPLE assignment.
1964 It will reallocate memory as needed to expand or shrink the number
1965 of operand slots needed to represent EXPR.
1967 NOTE: If you find yourself building a tree and then calling this
1968 function, you are most certainly doing it the slow way. It is much
1969 better to build a new assignment or to use the function
1970 gimple_assign_set_rhs_with_ops, which does not require an
1971 expression tree to be built. */
1974 gimple_assign_set_rhs_from_tree (gimple_stmt_iterator *gsi, tree expr)
1976 enum tree_code subcode;
1979 extract_ops_from_tree_1 (expr, &subcode, &op1, &op2, &op3);
1980 gimple_assign_set_rhs_with_ops_1 (gsi, subcode, op1, op2, op3);
1984 /* Set the RHS of assignment statement pointed-to by GSI to CODE with
1985 operands OP1, OP2 and OP3.
1987 NOTE: The statement pointed-to by GSI may be reallocated if it
1988 did not have enough operand slots. */
1991 gimple_assign_set_rhs_with_ops_1 (gimple_stmt_iterator *gsi, enum tree_code code,
1992 tree op1, tree op2, tree op3)
1994 unsigned new_rhs_ops = get_gimple_rhs_num_ops (code);
1995 gimple stmt = gsi_stmt (*gsi);
1997 /* If the new CODE needs more operands, allocate a new statement. */
1998 if (gimple_num_ops (stmt) < new_rhs_ops + 1)
2000 tree lhs = gimple_assign_lhs (stmt);
2001 gimple new_stmt = gimple_alloc (gimple_code (stmt), new_rhs_ops + 1);
2002 memcpy (new_stmt, stmt, gimple_size (gimple_code (stmt)));
2003 gsi_replace (gsi, new_stmt, true);
2006 /* The LHS needs to be reset as this also changes the SSA name
2008 gimple_assign_set_lhs (stmt, lhs);
2011 gimple_set_num_ops (stmt, new_rhs_ops + 1);
2012 gimple_set_subcode (stmt, code);
2013 gimple_assign_set_rhs1 (stmt, op1);
2014 if (new_rhs_ops > 1)
2015 gimple_assign_set_rhs2 (stmt, op2);
2016 if (new_rhs_ops > 2)
2017 gimple_assign_set_rhs3 (stmt, op3);
2021 /* Return the LHS of a statement that performs an assignment,
2022 either a GIMPLE_ASSIGN or a GIMPLE_CALL. Returns NULL_TREE
2023 for a call to a function that returns no value, or for a
2024 statement other than an assignment or a call. */
2027 gimple_get_lhs (const_gimple stmt)
2029 enum gimple_code code = gimple_code (stmt);
2031 if (code == GIMPLE_ASSIGN)
2032 return gimple_assign_lhs (stmt);
2033 else if (code == GIMPLE_CALL)
2034 return gimple_call_lhs (stmt);
2040 /* Set the LHS of a statement that performs an assignment,
2041 either a GIMPLE_ASSIGN or a GIMPLE_CALL. */
2044 gimple_set_lhs (gimple stmt, tree lhs)
2046 enum gimple_code code = gimple_code (stmt);
2048 if (code == GIMPLE_ASSIGN)
2049 gimple_assign_set_lhs (stmt, lhs);
2050 else if (code == GIMPLE_CALL)
2051 gimple_call_set_lhs (stmt, lhs);
2056 /* Replace the LHS of STMT, an assignment, either a GIMPLE_ASSIGN or a
2057 GIMPLE_CALL, with NLHS, in preparation for modifying the RHS to an
2058 expression with a different value.
2060 This will update any annotations (say debug bind stmts) referring
2061 to the original LHS, so that they use the RHS instead. This is
2062 done even if NLHS and LHS are the same, for it is understood that
2063 the RHS will be modified afterwards, and NLHS will not be assigned
2064 an equivalent value.
2066 Adjusting any non-annotation uses of the LHS, if needed, is a
2067 responsibility of the caller.
2069 The effect of this call should be pretty much the same as that of
2070 inserting a copy of STMT before STMT, and then removing the
2071 original stmt, at which time gsi_remove() would have update
2072 annotations, but using this function saves all the inserting,
2073 copying and removing. */
2076 gimple_replace_lhs (gimple stmt, tree nlhs)
2078 if (MAY_HAVE_DEBUG_STMTS)
2080 tree lhs = gimple_get_lhs (stmt);
2082 gcc_assert (SSA_NAME_DEF_STMT (lhs) == stmt);
2084 insert_debug_temp_for_var_def (NULL, lhs);
2087 gimple_set_lhs (stmt, nlhs);
2090 /* Return a deep copy of statement STMT. All the operands from STMT
2091 are reallocated and copied using unshare_expr. The DEF, USE, VDEF
2092 and VUSE operand arrays are set to empty in the new copy. */
2095 gimple_copy (gimple stmt)
2097 enum gimple_code code = gimple_code (stmt);
2098 unsigned num_ops = gimple_num_ops (stmt);
2099 gimple copy = gimple_alloc (code, num_ops);
2102 /* Shallow copy all the fields from STMT. */
2103 memcpy (copy, stmt, gimple_size (code));
2105 /* If STMT has sub-statements, deep-copy them as well. */
2106 if (gimple_has_substatements (stmt))
2111 switch (gimple_code (stmt))
2114 new_seq = gimple_seq_copy (gimple_bind_body (stmt));
2115 gimple_bind_set_body (copy, new_seq);
2116 gimple_bind_set_vars (copy, unshare_expr (gimple_bind_vars (stmt)));
2117 gimple_bind_set_block (copy, gimple_bind_block (stmt));
2121 new_seq = gimple_seq_copy (gimple_catch_handler (stmt));
2122 gimple_catch_set_handler (copy, new_seq);
2123 t = unshare_expr (gimple_catch_types (stmt));
2124 gimple_catch_set_types (copy, t);
2127 case GIMPLE_EH_FILTER:
2128 new_seq = gimple_seq_copy (gimple_eh_filter_failure (stmt));
2129 gimple_eh_filter_set_failure (copy, new_seq);
2130 t = unshare_expr (gimple_eh_filter_types (stmt));
2131 gimple_eh_filter_set_types (copy, t);
2135 new_seq = gimple_seq_copy (gimple_try_eval (stmt));
2136 gimple_try_set_eval (copy, new_seq);
2137 new_seq = gimple_seq_copy (gimple_try_cleanup (stmt));
2138 gimple_try_set_cleanup (copy, new_seq);
2141 case GIMPLE_OMP_FOR:
2142 new_seq = gimple_seq_copy (gimple_omp_for_pre_body (stmt));
2143 gimple_omp_for_set_pre_body (copy, new_seq);
2144 t = unshare_expr (gimple_omp_for_clauses (stmt));
2145 gimple_omp_for_set_clauses (copy, t);
2146 copy->gimple_omp_for.iter
2147 = ggc_alloc_vec_gimple_omp_for_iter
2148 (gimple_omp_for_collapse (stmt));
2149 for (i = 0; i < gimple_omp_for_collapse (stmt); i++)
2151 gimple_omp_for_set_cond (copy, i,
2152 gimple_omp_for_cond (stmt, i));
2153 gimple_omp_for_set_index (copy, i,
2154 gimple_omp_for_index (stmt, i));
2155 t = unshare_expr (gimple_omp_for_initial (stmt, i));
2156 gimple_omp_for_set_initial (copy, i, t);
2157 t = unshare_expr (gimple_omp_for_final (stmt, i));
2158 gimple_omp_for_set_final (copy, i, t);
2159 t = unshare_expr (gimple_omp_for_incr (stmt, i));
2160 gimple_omp_for_set_incr (copy, i, t);
2164 case GIMPLE_OMP_PARALLEL:
2165 t = unshare_expr (gimple_omp_parallel_clauses (stmt));
2166 gimple_omp_parallel_set_clauses (copy, t);
2167 t = unshare_expr (gimple_omp_parallel_child_fn (stmt));
2168 gimple_omp_parallel_set_child_fn (copy, t);
2169 t = unshare_expr (gimple_omp_parallel_data_arg (stmt));
2170 gimple_omp_parallel_set_data_arg (copy, t);
2173 case GIMPLE_OMP_TASK:
2174 t = unshare_expr (gimple_omp_task_clauses (stmt));
2175 gimple_omp_task_set_clauses (copy, t);
2176 t = unshare_expr (gimple_omp_task_child_fn (stmt));
2177 gimple_omp_task_set_child_fn (copy, t);
2178 t = unshare_expr (gimple_omp_task_data_arg (stmt));
2179 gimple_omp_task_set_data_arg (copy, t);
2180 t = unshare_expr (gimple_omp_task_copy_fn (stmt));
2181 gimple_omp_task_set_copy_fn (copy, t);
2182 t = unshare_expr (gimple_omp_task_arg_size (stmt));
2183 gimple_omp_task_set_arg_size (copy, t);
2184 t = unshare_expr (gimple_omp_task_arg_align (stmt));
2185 gimple_omp_task_set_arg_align (copy, t);
2188 case GIMPLE_OMP_CRITICAL:
2189 t = unshare_expr (gimple_omp_critical_name (stmt));
2190 gimple_omp_critical_set_name (copy, t);
2193 case GIMPLE_OMP_SECTIONS:
2194 t = unshare_expr (gimple_omp_sections_clauses (stmt));
2195 gimple_omp_sections_set_clauses (copy, t);
2196 t = unshare_expr (gimple_omp_sections_control (stmt));
2197 gimple_omp_sections_set_control (copy, t);
2200 case GIMPLE_OMP_SINGLE:
2201 case GIMPLE_OMP_SECTION:
2202 case GIMPLE_OMP_MASTER:
2203 case GIMPLE_OMP_ORDERED:
2205 new_seq = gimple_seq_copy (gimple_omp_body (stmt));
2206 gimple_omp_set_body (copy, new_seq);
2209 case GIMPLE_WITH_CLEANUP_EXPR:
2210 new_seq = gimple_seq_copy (gimple_wce_cleanup (stmt));
2211 gimple_wce_set_cleanup (copy, new_seq);
2219 /* Make copy of operands. */
2222 for (i = 0; i < num_ops; i++)
2223 gimple_set_op (copy, i, unshare_expr (gimple_op (stmt, i)));
2225 /* Clear out SSA operand vectors on COPY. */
2226 if (gimple_has_ops (stmt))
2228 gimple_set_def_ops (copy, NULL);
2229 gimple_set_use_ops (copy, NULL);
2232 if (gimple_has_mem_ops (stmt))
2234 gimple_set_vdef (copy, gimple_vdef (stmt));
2235 gimple_set_vuse (copy, gimple_vuse (stmt));
2238 /* SSA operands need to be updated. */
2239 gimple_set_modified (copy, true);
2246 /* Set the MODIFIED flag to MODIFIEDP, iff the gimple statement G has
2247 a MODIFIED field. */
2250 gimple_set_modified (gimple s, bool modifiedp)
2252 if (gimple_has_ops (s))
2254 s->gsbase.modified = (unsigned) modifiedp;
2258 && is_gimple_call (s)
2259 && gimple_call_noreturn_p (s))
2260 VEC_safe_push (gimple, gc, MODIFIED_NORETURN_CALLS (cfun), s);
2265 /* Return true if statement S has side-effects. We consider a
2266 statement to have side effects if:
2268 - It is a GIMPLE_CALL not marked with ECF_PURE or ECF_CONST.
2269 - Any of its operands are marked TREE_THIS_VOLATILE or TREE_SIDE_EFFECTS. */
2272 gimple_has_side_effects (const_gimple s)
2276 if (is_gimple_debug (s))
2279 /* We don't have to scan the arguments to check for
2280 volatile arguments, though, at present, we still
2281 do a scan to check for TREE_SIDE_EFFECTS. */
2282 if (gimple_has_volatile_ops (s))
2285 if (is_gimple_call (s))
2287 unsigned nargs = gimple_call_num_args (s);
2289 if (!(gimple_call_flags (s) & (ECF_CONST | ECF_PURE)))
2291 else if (gimple_call_flags (s) & ECF_LOOPING_CONST_OR_PURE)
2292 /* An infinite loop is considered a side effect. */
2295 if (gimple_call_lhs (s)
2296 && TREE_SIDE_EFFECTS (gimple_call_lhs (s)))
2298 gcc_assert (gimple_has_volatile_ops (s));
2302 if (TREE_SIDE_EFFECTS (gimple_call_fn (s)))
2305 for (i = 0; i < nargs; i++)
2306 if (TREE_SIDE_EFFECTS (gimple_call_arg (s, i)))
2308 gcc_assert (gimple_has_volatile_ops (s));
2316 for (i = 0; i < gimple_num_ops (s); i++)
2317 if (TREE_SIDE_EFFECTS (gimple_op (s, i)))
2319 gcc_assert (gimple_has_volatile_ops (s));
2327 /* Return true if the RHS of statement S has side effects.
2328 We may use it to determine if it is admissable to replace
2329 an assignment or call with a copy of a previously-computed
2330 value. In such cases, side-effects due the the LHS are
2334 gimple_rhs_has_side_effects (const_gimple s)
2338 if (is_gimple_call (s))
2340 unsigned nargs = gimple_call_num_args (s);
2342 if (!(gimple_call_flags (s) & (ECF_CONST | ECF_PURE)))
2345 /* We cannot use gimple_has_volatile_ops here,
2346 because we must ignore a volatile LHS. */
2347 if (TREE_SIDE_EFFECTS (gimple_call_fn (s))
2348 || TREE_THIS_VOLATILE (gimple_call_fn (s)))
2350 gcc_assert (gimple_has_volatile_ops (s));
2354 for (i = 0; i < nargs; i++)
2355 if (TREE_SIDE_EFFECTS (gimple_call_arg (s, i))
2356 || TREE_THIS_VOLATILE (gimple_call_arg (s, i)))
2361 else if (is_gimple_assign (s))
2363 /* Skip the first operand, the LHS. */
2364 for (i = 1; i < gimple_num_ops (s); i++)
2365 if (TREE_SIDE_EFFECTS (gimple_op (s, i))
2366 || TREE_THIS_VOLATILE (gimple_op (s, i)))
2368 gcc_assert (gimple_has_volatile_ops (s));
2372 else if (is_gimple_debug (s))
2376 /* For statements without an LHS, examine all arguments. */
2377 for (i = 0; i < gimple_num_ops (s); i++)
2378 if (TREE_SIDE_EFFECTS (gimple_op (s, i))
2379 || TREE_THIS_VOLATILE (gimple_op (s, i)))
2381 gcc_assert (gimple_has_volatile_ops (s));
2389 /* Helper for gimple_could_trap_p and gimple_assign_rhs_could_trap_p.
2390 Return true if S can trap. When INCLUDE_MEM is true, check whether
2391 the memory operations could trap. When INCLUDE_STORES is true and
2392 S is a GIMPLE_ASSIGN, the LHS of the assignment is also checked. */
2395 gimple_could_trap_p_1 (gimple s, bool include_mem, bool include_stores)
2397 tree t, div = NULL_TREE;
2402 unsigned i, start = (is_gimple_assign (s) && !include_stores) ? 1 : 0;
2404 for (i = start; i < gimple_num_ops (s); i++)
2405 if (tree_could_trap_p (gimple_op (s, i)))
2409 switch (gimple_code (s))
2412 return gimple_asm_volatile_p (s);
2415 t = gimple_call_fndecl (s);
2416 /* Assume that calls to weak functions may trap. */
2417 if (!t || !DECL_P (t) || DECL_WEAK (t))
2422 t = gimple_expr_type (s);
2423 op = gimple_assign_rhs_code (s);
2424 if (get_gimple_rhs_class (op) == GIMPLE_BINARY_RHS)
2425 div = gimple_assign_rhs2 (s);
2426 return (operation_could_trap_p (op, FLOAT_TYPE_P (t),
2427 (INTEGRAL_TYPE_P (t)
2428 && TYPE_OVERFLOW_TRAPS (t)),
2438 /* Return true if statement S can trap. */
2441 gimple_could_trap_p (gimple s)
2443 return gimple_could_trap_p_1 (s, true, true);
2446 /* Return true if RHS of a GIMPLE_ASSIGN S can trap. */
2449 gimple_assign_rhs_could_trap_p (gimple s)
2451 gcc_assert (is_gimple_assign (s));
2452 return gimple_could_trap_p_1 (s, true, false);
2456 /* Print debugging information for gimple stmts generated. */
2459 dump_gimple_statistics (void)
2461 #ifdef GATHER_STATISTICS
2462 int i, total_tuples = 0, total_bytes = 0;
2464 fprintf (stderr, "\nGIMPLE statements\n");
2465 fprintf (stderr, "Kind Stmts Bytes\n");
2466 fprintf (stderr, "---------------------------------------\n");
2467 for (i = 0; i < (int) gimple_alloc_kind_all; ++i)
2469 fprintf (stderr, "%-20s %7d %10d\n", gimple_alloc_kind_names[i],
2470 gimple_alloc_counts[i], gimple_alloc_sizes[i]);
2471 total_tuples += gimple_alloc_counts[i];
2472 total_bytes += gimple_alloc_sizes[i];
2474 fprintf (stderr, "---------------------------------------\n");
2475 fprintf (stderr, "%-20s %7d %10d\n", "Total", total_tuples, total_bytes);
2476 fprintf (stderr, "---------------------------------------\n");
2478 fprintf (stderr, "No gimple statistics\n");
2483 /* Return the number of operands needed on the RHS of a GIMPLE
2484 assignment for an expression with tree code CODE. */
2487 get_gimple_rhs_num_ops (enum tree_code code)
2489 enum gimple_rhs_class rhs_class = get_gimple_rhs_class (code);
2491 if (rhs_class == GIMPLE_UNARY_RHS || rhs_class == GIMPLE_SINGLE_RHS)
2493 else if (rhs_class == GIMPLE_BINARY_RHS)
2495 else if (rhs_class == GIMPLE_TERNARY_RHS)
2501 #define DEFTREECODE(SYM, STRING, TYPE, NARGS) \
2503 ((TYPE) == tcc_unary ? GIMPLE_UNARY_RHS \
2504 : ((TYPE) == tcc_binary \
2505 || (TYPE) == tcc_comparison) ? GIMPLE_BINARY_RHS \
2506 : ((TYPE) == tcc_constant \
2507 || (TYPE) == tcc_declaration \
2508 || (TYPE) == tcc_reference) ? GIMPLE_SINGLE_RHS \
2509 : ((SYM) == TRUTH_AND_EXPR \
2510 || (SYM) == TRUTH_OR_EXPR \
2511 || (SYM) == TRUTH_XOR_EXPR) ? GIMPLE_BINARY_RHS \
2512 : (SYM) == TRUTH_NOT_EXPR ? GIMPLE_UNARY_RHS \
2513 : ((SYM) == WIDEN_MULT_PLUS_EXPR \
2514 || (SYM) == WIDEN_MULT_MINUS_EXPR \
2515 || (SYM) == FMA_EXPR) ? GIMPLE_TERNARY_RHS \
2516 : ((SYM) == COND_EXPR \
2517 || (SYM) == CONSTRUCTOR \
2518 || (SYM) == OBJ_TYPE_REF \
2519 || (SYM) == ASSERT_EXPR \
2520 || (SYM) == ADDR_EXPR \
2521 || (SYM) == WITH_SIZE_EXPR \
2522 || (SYM) == SSA_NAME \
2523 || (SYM) == POLYNOMIAL_CHREC \
2524 || (SYM) == DOT_PROD_EXPR \
2525 || (SYM) == VEC_COND_EXPR \
2526 || (SYM) == REALIGN_LOAD_EXPR) ? GIMPLE_SINGLE_RHS \
2527 : GIMPLE_INVALID_RHS),
2528 #define END_OF_BASE_TREE_CODES (unsigned char) GIMPLE_INVALID_RHS,
2530 const unsigned char gimple_rhs_class_table[] = {
2531 #include "all-tree.def"
2535 #undef END_OF_BASE_TREE_CODES
2537 /* For the definitive definition of GIMPLE, see doc/tree-ssa.texi. */
2539 /* Validation of GIMPLE expressions. */
2541 /* Returns true iff T is a valid RHS for an assignment to a renamed
2542 user -- or front-end generated artificial -- variable. */
2545 is_gimple_reg_rhs (tree t)
2547 return get_gimple_rhs_class (TREE_CODE (t)) != GIMPLE_INVALID_RHS;
2550 /* Returns true iff T is a valid RHS for an assignment to an un-renamed
2551 LHS, or for a call argument. */
2554 is_gimple_mem_rhs (tree t)
2556 /* If we're dealing with a renamable type, either source or dest must be
2557 a renamed variable. */
2558 if (is_gimple_reg_type (TREE_TYPE (t)))
2559 return is_gimple_val (t);
2561 return is_gimple_val (t) || is_gimple_lvalue (t);
2564 /* Return true if T is a valid LHS for a GIMPLE assignment expression. */
2567 is_gimple_lvalue (tree t)
2569 return (is_gimple_addressable (t)
2570 || TREE_CODE (t) == WITH_SIZE_EXPR
2571 /* These are complex lvalues, but don't have addresses, so they
2573 || TREE_CODE (t) == BIT_FIELD_REF);
2576 /* Return true if T is a GIMPLE condition. */
2579 is_gimple_condexpr (tree t)
2581 return (is_gimple_val (t) || (COMPARISON_CLASS_P (t)
2582 && !tree_could_trap_p (t)
2583 && is_gimple_val (TREE_OPERAND (t, 0))
2584 && is_gimple_val (TREE_OPERAND (t, 1))));
2587 /* Return true if T is something whose address can be taken. */
2590 is_gimple_addressable (tree t)
2592 return (is_gimple_id (t) || handled_component_p (t)
2593 || TREE_CODE (t) == MEM_REF);
2596 /* Return true if T is a valid gimple constant. */
2599 is_gimple_constant (const_tree t)
2601 switch (TREE_CODE (t))
2611 /* Vector constant constructors are gimple invariant. */
2613 if (TREE_TYPE (t) && TREE_CODE (TREE_TYPE (t)) == VECTOR_TYPE)
2614 return TREE_CONSTANT (t);
2623 /* Return true if T is a gimple address. */
2626 is_gimple_address (const_tree t)
2630 if (TREE_CODE (t) != ADDR_EXPR)
2633 op = TREE_OPERAND (t, 0);
2634 while (handled_component_p (op))
2636 if ((TREE_CODE (op) == ARRAY_REF
2637 || TREE_CODE (op) == ARRAY_RANGE_REF)
2638 && !is_gimple_val (TREE_OPERAND (op, 1)))
2641 op = TREE_OPERAND (op, 0);
2644 if (CONSTANT_CLASS_P (op) || TREE_CODE (op) == MEM_REF)
2647 switch (TREE_CODE (op))
2662 /* Strip out all handled components that produce invariant
2666 strip_invariant_refs (const_tree op)
2668 while (handled_component_p (op))
2670 switch (TREE_CODE (op))
2673 case ARRAY_RANGE_REF:
2674 if (!is_gimple_constant (TREE_OPERAND (op, 1))
2675 || TREE_OPERAND (op, 2) != NULL_TREE
2676 || TREE_OPERAND (op, 3) != NULL_TREE)
2681 if (TREE_OPERAND (op, 2) != NULL_TREE)
2687 op = TREE_OPERAND (op, 0);
2693 /* Return true if T is a gimple invariant address. */
2696 is_gimple_invariant_address (const_tree t)
2700 if (TREE_CODE (t) != ADDR_EXPR)
2703 op = strip_invariant_refs (TREE_OPERAND (t, 0));
2707 if (TREE_CODE (op) == MEM_REF)
2709 const_tree op0 = TREE_OPERAND (op, 0);
2710 return (TREE_CODE (op0) == ADDR_EXPR
2711 && (CONSTANT_CLASS_P (TREE_OPERAND (op0, 0))
2712 || decl_address_invariant_p (TREE_OPERAND (op0, 0))));
2715 return CONSTANT_CLASS_P (op) || decl_address_invariant_p (op);
2718 /* Return true if T is a gimple invariant address at IPA level
2719 (so addresses of variables on stack are not allowed). */
2722 is_gimple_ip_invariant_address (const_tree t)
2726 if (TREE_CODE (t) != ADDR_EXPR)
2729 op = strip_invariant_refs (TREE_OPERAND (t, 0));
2731 return op && (CONSTANT_CLASS_P (op) || decl_address_ip_invariant_p (op));
2734 /* Return true if T is a GIMPLE minimal invariant. It's a restricted
2735 form of function invariant. */
2738 is_gimple_min_invariant (const_tree t)
2740 if (TREE_CODE (t) == ADDR_EXPR)
2741 return is_gimple_invariant_address (t);
2743 return is_gimple_constant (t);
2746 /* Return true if T is a GIMPLE interprocedural invariant. It's a restricted
2747 form of gimple minimal invariant. */
2750 is_gimple_ip_invariant (const_tree t)
2752 if (TREE_CODE (t) == ADDR_EXPR)
2753 return is_gimple_ip_invariant_address (t);
2755 return is_gimple_constant (t);
2758 /* Return true if T looks like a valid GIMPLE statement. */
2761 is_gimple_stmt (tree t)
2763 const enum tree_code code = TREE_CODE (t);
2768 /* The only valid NOP_EXPR is the empty statement. */
2769 return IS_EMPTY_STMT (t);
2773 /* These are only valid if they're void. */
2774 return TREE_TYPE (t) == NULL || VOID_TYPE_P (TREE_TYPE (t));
2780 case CASE_LABEL_EXPR:
2781 case TRY_CATCH_EXPR:
2782 case TRY_FINALLY_EXPR:
2783 case EH_FILTER_EXPR:
2786 case STATEMENT_LIST:
2796 /* These are always void. */
2802 /* These are valid regardless of their type. */
2810 /* Return true if T is a variable. */
2813 is_gimple_variable (tree t)
2815 return (TREE_CODE (t) == VAR_DECL
2816 || TREE_CODE (t) == PARM_DECL
2817 || TREE_CODE (t) == RESULT_DECL
2818 || TREE_CODE (t) == SSA_NAME);
2821 /* Return true if T is a GIMPLE identifier (something with an address). */
2824 is_gimple_id (tree t)
2826 return (is_gimple_variable (t)
2827 || TREE_CODE (t) == FUNCTION_DECL
2828 || TREE_CODE (t) == LABEL_DECL
2829 || TREE_CODE (t) == CONST_DECL
2830 /* Allow string constants, since they are addressable. */
2831 || TREE_CODE (t) == STRING_CST);
2834 /* Return true if TYPE is a suitable type for a scalar register variable. */
2837 is_gimple_reg_type (tree type)
2839 return !AGGREGATE_TYPE_P (type);
2842 /* Return true if T is a non-aggregate register variable. */
2845 is_gimple_reg (tree t)
2847 if (TREE_CODE (t) == SSA_NAME)
2848 t = SSA_NAME_VAR (t);
2850 if (!is_gimple_variable (t))
2853 if (!is_gimple_reg_type (TREE_TYPE (t)))
2856 /* A volatile decl is not acceptable because we can't reuse it as
2857 needed. We need to copy it into a temp first. */
2858 if (TREE_THIS_VOLATILE (t))
2861 /* We define "registers" as things that can be renamed as needed,
2862 which with our infrastructure does not apply to memory. */
2863 if (needs_to_live_in_memory (t))
2866 /* Hard register variables are an interesting case. For those that
2867 are call-clobbered, we don't know where all the calls are, since
2868 we don't (want to) take into account which operations will turn
2869 into libcalls at the rtl level. For those that are call-saved,
2870 we don't currently model the fact that calls may in fact change
2871 global hard registers, nor do we examine ASM_CLOBBERS at the tree
2872 level, and so miss variable changes that might imply. All around,
2873 it seems safest to not do too much optimization with these at the
2874 tree level at all. We'll have to rely on the rtl optimizers to
2875 clean this up, as there we've got all the appropriate bits exposed. */
2876 if (TREE_CODE (t) == VAR_DECL && DECL_HARD_REGISTER (t))
2879 /* Complex and vector values must have been put into SSA-like form.
2880 That is, no assignments to the individual components. */
2881 if (TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE
2882 || TREE_CODE (TREE_TYPE (t)) == VECTOR_TYPE)
2883 return DECL_GIMPLE_REG_P (t);
2889 /* Return true if T is a GIMPLE variable whose address is not needed. */
2892 is_gimple_non_addressable (tree t)
2894 if (TREE_CODE (t) == SSA_NAME)
2895 t = SSA_NAME_VAR (t);
2897 return (is_gimple_variable (t) && ! needs_to_live_in_memory (t));
2900 /* Return true if T is a GIMPLE rvalue, i.e. an identifier or a constant. */
2903 is_gimple_val (tree t)
2905 /* Make loads from volatiles and memory vars explicit. */
2906 if (is_gimple_variable (t)
2907 && is_gimple_reg_type (TREE_TYPE (t))
2908 && !is_gimple_reg (t))
2911 return (is_gimple_variable (t) || is_gimple_min_invariant (t));
2914 /* Similarly, but accept hard registers as inputs to asm statements. */
2917 is_gimple_asm_val (tree t)
2919 if (TREE_CODE (t) == VAR_DECL && DECL_HARD_REGISTER (t))
2922 return is_gimple_val (t);
2925 /* Return true if T is a GIMPLE minimal lvalue. */
2928 is_gimple_min_lval (tree t)
2930 if (!(t = CONST_CAST_TREE (strip_invariant_refs (t))))
2932 return (is_gimple_id (t) || TREE_CODE (t) == MEM_REF);
2935 /* Return true if T is a valid function operand of a CALL_EXPR. */
2938 is_gimple_call_addr (tree t)
2940 return (TREE_CODE (t) == OBJ_TYPE_REF || is_gimple_val (t));
2943 /* Return true if T is a valid address operand of a MEM_REF. */
2946 is_gimple_mem_ref_addr (tree t)
2948 return (is_gimple_reg (t)
2949 || TREE_CODE (t) == INTEGER_CST
2950 || (TREE_CODE (t) == ADDR_EXPR
2951 && (CONSTANT_CLASS_P (TREE_OPERAND (t, 0))
2952 || decl_address_invariant_p (TREE_OPERAND (t, 0)))));
2955 /* If T makes a function call, return the corresponding CALL_EXPR operand.
2956 Otherwise, return NULL_TREE. */
2959 get_call_expr_in (tree t)
2961 if (TREE_CODE (t) == MODIFY_EXPR)
2962 t = TREE_OPERAND (t, 1);
2963 if (TREE_CODE (t) == WITH_SIZE_EXPR)
2964 t = TREE_OPERAND (t, 0);
2965 if (TREE_CODE (t) == CALL_EXPR)
2971 /* Given a memory reference expression T, return its base address.
2972 The base address of a memory reference expression is the main
2973 object being referenced. For instance, the base address for
2974 'array[i].fld[j]' is 'array'. You can think of this as stripping
2975 away the offset part from a memory address.
2977 This function calls handled_component_p to strip away all the inner
2978 parts of the memory reference until it reaches the base object. */
2981 get_base_address (tree t)
2983 while (handled_component_p (t))
2984 t = TREE_OPERAND (t, 0);
2986 if ((TREE_CODE (t) == MEM_REF
2987 || TREE_CODE (t) == TARGET_MEM_REF)
2988 && TREE_CODE (TREE_OPERAND (t, 0)) == ADDR_EXPR)
2989 t = TREE_OPERAND (TREE_OPERAND (t, 0), 0);
2991 if (TREE_CODE (t) == SSA_NAME
2993 || TREE_CODE (t) == STRING_CST
2994 || TREE_CODE (t) == CONSTRUCTOR
2995 || INDIRECT_REF_P (t)
2996 || TREE_CODE (t) == MEM_REF
2997 || TREE_CODE (t) == TARGET_MEM_REF)
3004 recalculate_side_effects (tree t)
3006 enum tree_code code = TREE_CODE (t);
3007 int len = TREE_OPERAND_LENGTH (t);
3010 switch (TREE_CODE_CLASS (code))
3012 case tcc_expression:
3018 case PREDECREMENT_EXPR:
3019 case PREINCREMENT_EXPR:
3020 case POSTDECREMENT_EXPR:
3021 case POSTINCREMENT_EXPR:
3022 /* All of these have side-effects, no matter what their
3031 case tcc_comparison: /* a comparison expression */
3032 case tcc_unary: /* a unary arithmetic expression */
3033 case tcc_binary: /* a binary arithmetic expression */
3034 case tcc_reference: /* a reference */
3035 case tcc_vl_exp: /* a function call */
3036 TREE_SIDE_EFFECTS (t) = TREE_THIS_VOLATILE (t);
3037 for (i = 0; i < len; ++i)
3039 tree op = TREE_OPERAND (t, i);
3040 if (op && TREE_SIDE_EFFECTS (op))
3041 TREE_SIDE_EFFECTS (t) = 1;
3046 /* No side-effects. */
3054 /* Canonicalize a tree T for use in a COND_EXPR as conditional. Returns
3055 a canonicalized tree that is valid for a COND_EXPR or NULL_TREE, if
3056 we failed to create one. */
3059 canonicalize_cond_expr_cond (tree t)
3061 /* Strip conversions around boolean operations. */
3062 if (CONVERT_EXPR_P (t)
3063 && truth_value_p (TREE_CODE (TREE_OPERAND (t, 0))))
3064 t = TREE_OPERAND (t, 0);
3066 /* For (bool)x use x != 0. */
3067 if (CONVERT_EXPR_P (t)
3068 && TREE_CODE (TREE_TYPE (t)) == BOOLEAN_TYPE)
3070 tree top0 = TREE_OPERAND (t, 0);
3071 t = build2 (NE_EXPR, TREE_TYPE (t),
3072 top0, build_int_cst (TREE_TYPE (top0), 0));
3074 /* For !x use x == 0. */
3075 else if (TREE_CODE (t) == TRUTH_NOT_EXPR)
3077 tree top0 = TREE_OPERAND (t, 0);
3078 t = build2 (EQ_EXPR, TREE_TYPE (t),
3079 top0, build_int_cst (TREE_TYPE (top0), 0));
3081 /* For cmp ? 1 : 0 use cmp. */
3082 else if (TREE_CODE (t) == COND_EXPR
3083 && COMPARISON_CLASS_P (TREE_OPERAND (t, 0))
3084 && integer_onep (TREE_OPERAND (t, 1))
3085 && integer_zerop (TREE_OPERAND (t, 2)))
3087 tree top0 = TREE_OPERAND (t, 0);
3088 t = build2 (TREE_CODE (top0), TREE_TYPE (t),
3089 TREE_OPERAND (top0, 0), TREE_OPERAND (top0, 1));
3092 if (is_gimple_condexpr (t))
3098 /* Build a GIMPLE_CALL identical to STMT but skipping the arguments in
3099 the positions marked by the set ARGS_TO_SKIP. */
3102 gimple_call_copy_skip_args (gimple stmt, bitmap args_to_skip)
3105 tree fn = gimple_call_fn (stmt);
3106 int nargs = gimple_call_num_args (stmt);
3107 VEC(tree, heap) *vargs = VEC_alloc (tree, heap, nargs);
3110 for (i = 0; i < nargs; i++)
3111 if (!bitmap_bit_p (args_to_skip, i))
3112 VEC_quick_push (tree, vargs, gimple_call_arg (stmt, i));
3114 new_stmt = gimple_build_call_vec (fn, vargs);
3115 VEC_free (tree, heap, vargs);
3116 if (gimple_call_lhs (stmt))
3117 gimple_call_set_lhs (new_stmt, gimple_call_lhs (stmt));
3119 gimple_set_vuse (new_stmt, gimple_vuse (stmt));
3120 gimple_set_vdef (new_stmt, gimple_vdef (stmt));
3122 gimple_set_block (new_stmt, gimple_block (stmt));
3123 if (gimple_has_location (stmt))
3124 gimple_set_location (new_stmt, gimple_location (stmt));
3125 gimple_call_copy_flags (new_stmt, stmt);
3126 gimple_call_set_chain (new_stmt, gimple_call_chain (stmt));
3128 gimple_set_modified (new_stmt, true);
3134 static hashval_t gimple_type_hash (const void *);
3136 /* Structure used to maintain a cache of some type pairs compared by
3137 gimple_types_compatible_p when comparing aggregate types. There are
3138 three possible values for SAME_P:
3140 -2: The pair (T1, T2) has just been inserted in the table.
3141 0: T1 and T2 are different types.
3142 1: T1 and T2 are the same type.
3144 The two elements in the SAME_P array are indexed by the comparison
3151 signed char same_p[2];
3153 typedef struct type_pair_d *type_pair_t;
3155 DEF_VEC_P(type_pair_t);
3156 DEF_VEC_ALLOC_P(type_pair_t,heap);
3158 /* Return a hash value for the type pair pointed-to by P. */
3161 type_pair_hash (const void *p)
3163 const struct type_pair_d *pair = (const struct type_pair_d *) p;
3164 hashval_t val1 = pair->uid1;
3165 hashval_t val2 = pair->uid2;
3166 return (iterative_hash_hashval_t (val2, val1)
3167 ^ iterative_hash_hashval_t (val1, val2));
3170 /* Compare two type pairs pointed-to by P1 and P2. */
3173 type_pair_eq (const void *p1, const void *p2)
3175 const struct type_pair_d *pair1 = (const struct type_pair_d *) p1;
3176 const struct type_pair_d *pair2 = (const struct type_pair_d *) p2;
3177 return ((pair1->uid1 == pair2->uid1 && pair1->uid2 == pair2->uid2)
3178 || (pair1->uid1 == pair2->uid2 && pair1->uid2 == pair2->uid1));
3181 /* Lookup the pair of types T1 and T2 in *VISITED_P. Insert a new
3182 entry if none existed. */
3185 lookup_type_pair (tree t1, tree t2, htab_t *visited_p, struct obstack *ob_p)
3187 struct type_pair_d pair;
3191 if (*visited_p == NULL)
3193 *visited_p = htab_create (251, type_pair_hash, type_pair_eq, NULL);
3194 gcc_obstack_init (ob_p);
3197 pair.uid1 = TYPE_UID (t1);
3198 pair.uid2 = TYPE_UID (t2);
3199 slot = htab_find_slot (*visited_p, &pair, INSERT);
3202 p = *((type_pair_t *) slot);
3205 p = XOBNEW (ob_p, struct type_pair_d);
3206 p->uid1 = TYPE_UID (t1);
3207 p->uid2 = TYPE_UID (t2);
3216 /* Per pointer state for the SCC finding. The on_sccstack flag
3217 is not strictly required, it is true when there is no hash value
3218 recorded for the type and false otherwise. But querying that
3223 unsigned int dfsnum;
3232 static unsigned int next_dfs_num;
3233 static unsigned int gtc_next_dfs_num;
3236 /* GIMPLE type merging cache. A direct-mapped cache based on TYPE_UID. */
3238 typedef struct GTY(()) gimple_type_leader_entry_s {
3241 } gimple_type_leader_entry;
3243 #define GIMPLE_TYPE_LEADER_SIZE 16381
3244 static GTY((length("GIMPLE_TYPE_LEADER_SIZE"))) gimple_type_leader_entry
3245 *gimple_type_leader;
3247 /* Lookup an existing leader for T and return it or NULL_TREE, if
3248 there is none in the cache. */
3251 gimple_lookup_type_leader (tree t)
3253 gimple_type_leader_entry *leader;
3255 if (!gimple_type_leader)
3258 leader = &gimple_type_leader[TYPE_UID (t) % GIMPLE_TYPE_LEADER_SIZE];
3259 if (leader->type != t)
3262 return leader->leader;
3265 /* Return true if T1 and T2 have the same name. If FOR_COMPLETION_P is
3266 true then if any type has no name return false, otherwise return
3267 true if both types have no names. */
3270 compare_type_names_p (tree t1, tree t2, bool for_completion_p)
3272 tree name1 = TYPE_NAME (t1);
3273 tree name2 = TYPE_NAME (t2);
3275 /* Consider anonymous types all unique for completion. */
3276 if (for_completion_p
3277 && (!name1 || !name2))
3280 if (name1 && TREE_CODE (name1) == TYPE_DECL)
3282 name1 = DECL_NAME (name1);
3283 if (for_completion_p
3287 gcc_assert (!name1 || TREE_CODE (name1) == IDENTIFIER_NODE);
3289 if (name2 && TREE_CODE (name2) == TYPE_DECL)
3291 name2 = DECL_NAME (name2);
3292 if (for_completion_p
3296 gcc_assert (!name2 || TREE_CODE (name2) == IDENTIFIER_NODE);
3298 /* Identifiers can be compared with pointer equality rather
3299 than a string comparison. */
3306 /* Return true if the field decls F1 and F2 are at the same offset.
3308 This is intended to be used on GIMPLE types only. In order to
3309 compare GENERIC types, use fields_compatible_p instead. */
3312 gimple_compare_field_offset (tree f1, tree f2)
3314 if (DECL_OFFSET_ALIGN (f1) == DECL_OFFSET_ALIGN (f2))
3316 tree offset1 = DECL_FIELD_OFFSET (f1);
3317 tree offset2 = DECL_FIELD_OFFSET (f2);
3318 return ((offset1 == offset2
3319 /* Once gimplification is done, self-referential offsets are
3320 instantiated as operand #2 of the COMPONENT_REF built for
3321 each access and reset. Therefore, they are not relevant
3322 anymore and fields are interchangeable provided that they
3323 represent the same access. */
3324 || (TREE_CODE (offset1) == PLACEHOLDER_EXPR
3325 && TREE_CODE (offset2) == PLACEHOLDER_EXPR
3326 && (DECL_SIZE (f1) == DECL_SIZE (f2)
3327 || (TREE_CODE (DECL_SIZE (f1)) == PLACEHOLDER_EXPR
3328 && TREE_CODE (DECL_SIZE (f2)) == PLACEHOLDER_EXPR)
3329 || operand_equal_p (DECL_SIZE (f1), DECL_SIZE (f2), 0))
3330 && DECL_ALIGN (f1) == DECL_ALIGN (f2))
3331 || operand_equal_p (offset1, offset2, 0))
3332 && tree_int_cst_equal (DECL_FIELD_BIT_OFFSET (f1),
3333 DECL_FIELD_BIT_OFFSET (f2)));
3336 /* Fortran and C do not always agree on what DECL_OFFSET_ALIGN
3337 should be, so handle differing ones specially by decomposing
3338 the offset into a byte and bit offset manually. */
3339 if (host_integerp (DECL_FIELD_OFFSET (f1), 0)
3340 && host_integerp (DECL_FIELD_OFFSET (f2), 0))
3342 unsigned HOST_WIDE_INT byte_offset1, byte_offset2;
3343 unsigned HOST_WIDE_INT bit_offset1, bit_offset2;
3344 bit_offset1 = TREE_INT_CST_LOW (DECL_FIELD_BIT_OFFSET (f1));
3345 byte_offset1 = (TREE_INT_CST_LOW (DECL_FIELD_OFFSET (f1))
3346 + bit_offset1 / BITS_PER_UNIT);
3347 bit_offset2 = TREE_INT_CST_LOW (DECL_FIELD_BIT_OFFSET (f2));
3348 byte_offset2 = (TREE_INT_CST_LOW (DECL_FIELD_OFFSET (f2))
3349 + bit_offset2 / BITS_PER_UNIT);
3350 if (byte_offset1 != byte_offset2)
3352 return bit_offset1 % BITS_PER_UNIT == bit_offset2 % BITS_PER_UNIT;
3358 /* If the type T1 and the type T2 are a complete and an incomplete
3359 variant of the same type return true. */
3362 gimple_compatible_complete_and_incomplete_subtype_p (tree t1, tree t2)
3364 /* If one pointer points to an incomplete type variant of
3365 the other pointed-to type they are the same. */
3366 if (TREE_CODE (t1) == TREE_CODE (t2)
3367 && RECORD_OR_UNION_TYPE_P (t1)
3368 && (!COMPLETE_TYPE_P (t1)
3369 || !COMPLETE_TYPE_P (t2))
3370 && TYPE_QUALS (t1) == TYPE_QUALS (t2)
3371 && compare_type_names_p (TYPE_MAIN_VARIANT (t1),
3372 TYPE_MAIN_VARIANT (t2), true))
3378 gimple_types_compatible_p_1 (tree, tree, enum gtc_mode, type_pair_t,
3379 VEC(type_pair_t, heap) **,
3380 struct pointer_map_t *, struct obstack *);
3382 /* DFS visit the edge from the callers type pair with state *STATE to
3383 the pair T1, T2 while operating in FOR_MERGING_P mode.
3384 Update the merging status if it is not part of the SCC containing the
3385 callers pair and return it.
3386 SCCSTACK, SCCSTATE and SCCSTATE_OBSTACK are state for the DFS walk done. */
3389 gtc_visit (tree t1, tree t2, enum gtc_mode mode,
3391 VEC(type_pair_t, heap) **sccstack,
3392 struct pointer_map_t *sccstate,
3393 struct obstack *sccstate_obstack)
3395 struct sccs *cstate = NULL;
3399 /* Check first for the obvious case of pointer identity. */
3403 /* Check that we have two types to compare. */
3404 if (t1 == NULL_TREE || t2 == NULL_TREE)
3407 /* If the types have been previously registered and found equal
3409 if (mode == GTC_MERGE)
3411 tree leader1 = gimple_lookup_type_leader (t1);
3412 tree leader2 = gimple_lookup_type_leader (t2);
3415 || (leader1 && leader1 == leader2))
3418 else if (mode == GTC_DIAG)
3420 if (TYPE_CANONICAL (t1)
3421 && TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2))
3425 /* Can't be the same type if the types don't have the same code. */
3426 if (TREE_CODE (t1) != TREE_CODE (t2))
3429 /* Can't be the same type if they have different CV qualifiers. */
3430 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
3433 /* Void types are always the same. */
3434 if (TREE_CODE (t1) == VOID_TYPE)
3437 /* Do some simple checks before doing three hashtable queries. */
3438 if (INTEGRAL_TYPE_P (t1)
3439 || SCALAR_FLOAT_TYPE_P (t1)
3440 || FIXED_POINT_TYPE_P (t1)
3441 || TREE_CODE (t1) == VECTOR_TYPE
3442 || TREE_CODE (t1) == COMPLEX_TYPE
3443 || TREE_CODE (t1) == OFFSET_TYPE)
3445 /* Can't be the same type if they have different alignment,
3446 sign, precision or mode. */
3447 if (TYPE_ALIGN (t1) != TYPE_ALIGN (t2)
3448 || TYPE_PRECISION (t1) != TYPE_PRECISION (t2)
3449 || TYPE_MODE (t1) != TYPE_MODE (t2)
3450 || TYPE_UNSIGNED (t1) != TYPE_UNSIGNED (t2))
3453 if (TREE_CODE (t1) == INTEGER_TYPE
3454 && (TYPE_IS_SIZETYPE (t1) != TYPE_IS_SIZETYPE (t2)
3455 || TYPE_STRING_FLAG (t1) != TYPE_STRING_FLAG (t2)))
3458 /* That's all we need to check for float and fixed-point types. */
3459 if (SCALAR_FLOAT_TYPE_P (t1)
3460 || FIXED_POINT_TYPE_P (t1))
3463 /* For integral types fall thru to more complex checks. */
3466 else if (AGGREGATE_TYPE_P (t1) || POINTER_TYPE_P (t1))
3468 /* Can't be the same type if they have different alignment or mode. */
3469 if (TYPE_ALIGN (t1) != TYPE_ALIGN (t2)
3470 || TYPE_MODE (t1) != TYPE_MODE (t2))
3474 /* If the hash values of t1 and t2 are different the types can't
3475 possibly be the same. This helps keeping the type-pair hashtable
3476 small, only tracking comparisons for hash collisions. */
3477 if (gimple_type_hash (t1) != gimple_type_hash (t2))
3480 /* Allocate a new cache entry for this comparison. */
3481 p = lookup_type_pair (t1, t2, >c_visited, >c_ob);
3482 if (p->same_p[mode] == 0 || p->same_p[mode] == 1)
3484 /* We have already decided whether T1 and T2 are the
3485 same, return the cached result. */
3486 return p->same_p[mode] == 1;
3489 if ((slot = pointer_map_contains (sccstate, p)) != NULL)
3490 cstate = (struct sccs *)*slot;
3491 /* Not yet visited. DFS recurse. */
3494 gimple_types_compatible_p_1 (t1, t2, mode, p,
3495 sccstack, sccstate, sccstate_obstack);
3496 cstate = (struct sccs *)* pointer_map_contains (sccstate, p);
3497 state->low = MIN (state->low, cstate->low);
3499 /* If the type is still on the SCC stack adjust the parents low. */
3500 if (cstate->dfsnum < state->dfsnum
3501 && cstate->on_sccstack)
3502 state->low = MIN (cstate->dfsnum, state->low);
3504 /* Return the current lattice value. We start with an equality
3505 assumption so types part of a SCC will be optimistically
3506 treated equal unless proven otherwise. */
3507 return cstate->u.same_p;
3510 /* Worker for gimple_types_compatible.
3511 SCCSTACK, SCCSTATE and SCCSTATE_OBSTACK are state for the DFS walk done. */
3514 gimple_types_compatible_p_1 (tree t1, tree t2, enum gtc_mode mode,
3516 VEC(type_pair_t, heap) **sccstack,
3517 struct pointer_map_t *sccstate,
3518 struct obstack *sccstate_obstack)
3522 gcc_assert (p->same_p[mode] == -2);
3524 state = XOBNEW (sccstate_obstack, struct sccs);
3525 *pointer_map_insert (sccstate, p) = state;
3527 VEC_safe_push (type_pair_t, heap, *sccstack, p);
3528 state->dfsnum = gtc_next_dfs_num++;
3529 state->low = state->dfsnum;
3530 state->on_sccstack = true;
3531 /* Start with an equality assumption. As we DFS recurse into child
3532 SCCs this assumption may get revisited. */
3533 state->u.same_p = 1;
3535 /* If their attributes are not the same they can't be the same type. */
3536 if (!attribute_list_equal (TYPE_ATTRIBUTES (t1), TYPE_ATTRIBUTES (t2)))
3537 goto different_types;
3539 /* Do type-specific comparisons. */
3540 switch (TREE_CODE (t1))
3544 if (!gtc_visit (TREE_TYPE (t1), TREE_TYPE (t2), mode,
3545 state, sccstack, sccstate, sccstate_obstack))
3546 goto different_types;
3550 /* Array types are the same if the element types are the same and
3551 the number of elements are the same. */
3552 if (!gtc_visit (TREE_TYPE (t1), TREE_TYPE (t2), mode,
3553 state, sccstack, sccstate, sccstate_obstack)
3554 || TYPE_STRING_FLAG (t1) != TYPE_STRING_FLAG (t2)
3555 || TYPE_NONALIASED_COMPONENT (t1) != TYPE_NONALIASED_COMPONENT (t2))
3556 goto different_types;
3559 tree i1 = TYPE_DOMAIN (t1);
3560 tree i2 = TYPE_DOMAIN (t2);
3562 /* For an incomplete external array, the type domain can be
3563 NULL_TREE. Check this condition also. */
3564 if (i1 == NULL_TREE && i2 == NULL_TREE)
3566 else if (i1 == NULL_TREE || i2 == NULL_TREE)
3567 goto different_types;
3568 /* If for a complete array type the possibly gimplified sizes
3569 are different the types are different. */
3570 else if (((TYPE_SIZE (i1) != NULL) ^ (TYPE_SIZE (i2) != NULL))
3573 && !operand_equal_p (TYPE_SIZE (i1), TYPE_SIZE (i2), 0)))
3574 goto different_types;
3577 tree min1 = TYPE_MIN_VALUE (i1);
3578 tree min2 = TYPE_MIN_VALUE (i2);
3579 tree max1 = TYPE_MAX_VALUE (i1);
3580 tree max2 = TYPE_MAX_VALUE (i2);
3582 /* The minimum/maximum values have to be the same. */
3585 && ((TREE_CODE (min1) == PLACEHOLDER_EXPR
3586 && TREE_CODE (min2) == PLACEHOLDER_EXPR)
3587 || operand_equal_p (min1, min2, 0))))
3590 && ((TREE_CODE (max1) == PLACEHOLDER_EXPR
3591 && TREE_CODE (max2) == PLACEHOLDER_EXPR)
3592 || operand_equal_p (max1, max2, 0)))))
3595 goto different_types;
3600 /* Method types should belong to the same class. */
3601 if (!gtc_visit (TYPE_METHOD_BASETYPE (t1), TYPE_METHOD_BASETYPE (t2),
3602 mode, state, sccstack, sccstate, sccstate_obstack))
3603 goto different_types;
3608 /* Function types are the same if the return type and arguments types
3610 if ((mode != GTC_DIAG
3611 || !gimple_compatible_complete_and_incomplete_subtype_p
3612 (TREE_TYPE (t1), TREE_TYPE (t2)))
3613 && !gtc_visit (TREE_TYPE (t1), TREE_TYPE (t2), mode,
3614 state, sccstack, sccstate, sccstate_obstack))
3615 goto different_types;
3617 if (!targetm.comp_type_attributes (t1, t2))
3618 goto different_types;
3620 if (TYPE_ARG_TYPES (t1) == TYPE_ARG_TYPES (t2))
3624 tree parms1, parms2;
3626 for (parms1 = TYPE_ARG_TYPES (t1), parms2 = TYPE_ARG_TYPES (t2);
3628 parms1 = TREE_CHAIN (parms1), parms2 = TREE_CHAIN (parms2))
3630 if ((mode == GTC_MERGE
3631 || !gimple_compatible_complete_and_incomplete_subtype_p
3632 (TREE_VALUE (parms1), TREE_VALUE (parms2)))
3633 && !gtc_visit (TREE_VALUE (parms1), TREE_VALUE (parms2), mode,
3634 state, sccstack, sccstate, sccstate_obstack))
3635 goto different_types;
3638 if (parms1 || parms2)
3639 goto different_types;
3646 if (!gtc_visit (TREE_TYPE (t1), TREE_TYPE (t2), mode,
3647 state, sccstack, sccstate, sccstate_obstack)
3648 || !gtc_visit (TYPE_OFFSET_BASETYPE (t1),
3649 TYPE_OFFSET_BASETYPE (t2), mode,
3650 state, sccstack, sccstate, sccstate_obstack))
3651 goto different_types;
3657 case REFERENCE_TYPE:
3659 /* If the two pointers have different ref-all attributes,
3660 they can't be the same type. */
3661 if (TYPE_REF_CAN_ALIAS_ALL (t1) != TYPE_REF_CAN_ALIAS_ALL (t2))
3662 goto different_types;
3664 /* If one pointer points to an incomplete type variant of
3665 the other pointed-to type they are the same. */
3666 if (mode == GTC_DIAG
3667 && gimple_compatible_complete_and_incomplete_subtype_p
3668 (TREE_TYPE (t1), TREE_TYPE (t2)))
3671 /* Otherwise, pointer and reference types are the same if the
3672 pointed-to types are the same. */
3673 if (gtc_visit (TREE_TYPE (t1), TREE_TYPE (t2), mode,
3674 state, sccstack, sccstate, sccstate_obstack))
3677 goto different_types;
3681 /* There is only one decltype(nullptr). */
3687 tree min1 = TYPE_MIN_VALUE (t1);
3688 tree max1 = TYPE_MAX_VALUE (t1);
3689 tree min2 = TYPE_MIN_VALUE (t2);
3690 tree max2 = TYPE_MAX_VALUE (t2);
3691 bool min_equal_p = false;
3692 bool max_equal_p = false;
3694 /* If either type has a minimum value, the other type must
3696 if (min1 == NULL_TREE && min2 == NULL_TREE)
3698 else if (min1 && min2 && operand_equal_p (min1, min2, 0))
3701 /* Likewise, if either type has a maximum value, the other
3702 type must have the same. */
3703 if (max1 == NULL_TREE && max2 == NULL_TREE)
3705 else if (max1 && max2 && operand_equal_p (max1, max2, 0))
3708 if (!min_equal_p || !max_equal_p)
3709 goto different_types;
3716 /* FIXME lto, we cannot check bounds on enumeral types because
3717 different front ends will produce different values.
3718 In C, enumeral types are integers, while in C++ each element
3719 will have its own symbolic value. We should decide how enums
3720 are to be represented in GIMPLE and have each front end lower
3724 /* For enumeral types, all the values must be the same. */
3725 if (TYPE_VALUES (t1) == TYPE_VALUES (t2))
3728 for (v1 = TYPE_VALUES (t1), v2 = TYPE_VALUES (t2);
3730 v1 = TREE_CHAIN (v1), v2 = TREE_CHAIN (v2))
3732 tree c1 = TREE_VALUE (v1);
3733 tree c2 = TREE_VALUE (v2);
3735 if (TREE_CODE (c1) == CONST_DECL)
3736 c1 = DECL_INITIAL (c1);
3738 if (TREE_CODE (c2) == CONST_DECL)
3739 c2 = DECL_INITIAL (c2);
3741 if (tree_int_cst_equal (c1, c2) != 1)
3742 goto different_types;
3745 /* If one enumeration has more values than the other, they
3746 are not the same. */
3748 goto different_types;
3755 case QUAL_UNION_TYPE:
3759 /* The struct tags shall compare equal. */
3760 if (!compare_type_names_p (TYPE_MAIN_VARIANT (t1),
3761 TYPE_MAIN_VARIANT (t2), false))
3762 goto different_types;
3764 /* For aggregate types, all the fields must be the same. */
3765 for (f1 = TYPE_FIELDS (t1), f2 = TYPE_FIELDS (t2);
3767 f1 = TREE_CHAIN (f1), f2 = TREE_CHAIN (f2))
3769 /* The fields must have the same name, offset and type. */
3770 if (DECL_NAME (f1) != DECL_NAME (f2)
3771 || DECL_NONADDRESSABLE_P (f1) != DECL_NONADDRESSABLE_P (f2)
3772 || !gimple_compare_field_offset (f1, f2)
3773 || !gtc_visit (TREE_TYPE (f1), TREE_TYPE (f2), mode,
3774 state, sccstack, sccstate, sccstate_obstack))
3775 goto different_types;
3778 /* If one aggregate has more fields than the other, they
3779 are not the same. */
3781 goto different_types;
3790 /* Common exit path for types that are not compatible. */
3792 state->u.same_p = 0;
3795 /* Common exit path for types that are compatible. */
3797 gcc_assert (state->u.same_p == 1);
3800 if (state->low == state->dfsnum)
3804 /* Pop off the SCC and set its cache values to the final
3805 comparison result. */
3808 struct sccs *cstate;
3809 x = VEC_pop (type_pair_t, *sccstack);
3810 cstate = (struct sccs *)*pointer_map_contains (sccstate, x);
3811 cstate->on_sccstack = false;
3812 x->same_p[mode] = state->u.same_p;
3817 return state->u.same_p;
3820 /* Return true iff T1 and T2 are structurally identical. When
3821 FOR_MERGING_P is true the an incomplete type and a complete type
3822 are considered different, otherwise they are considered compatible. */
3825 gimple_types_compatible_p (tree t1, tree t2, enum gtc_mode mode)
3827 VEC(type_pair_t, heap) *sccstack = NULL;
3828 struct pointer_map_t *sccstate;
3829 struct obstack sccstate_obstack;
3830 type_pair_t p = NULL;
3833 /* Before starting to set up the SCC machinery handle simple cases. */
3835 /* Check first for the obvious case of pointer identity. */
3839 /* Check that we have two types to compare. */
3840 if (t1 == NULL_TREE || t2 == NULL_TREE)
3843 /* If the types have been previously registered and found equal
3845 if (mode == GTC_MERGE)
3847 tree leader1 = gimple_lookup_type_leader (t1);
3848 tree leader2 = gimple_lookup_type_leader (t2);
3851 || (leader1 && leader1 == leader2))
3854 else if (mode == GTC_DIAG)
3856 if (TYPE_CANONICAL (t1)
3857 && TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2))
3861 /* Can't be the same type if the types don't have the same code. */
3862 if (TREE_CODE (t1) != TREE_CODE (t2))
3865 /* Can't be the same type if they have different CV qualifiers. */
3866 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
3869 /* Void types are always the same. */
3870 if (TREE_CODE (t1) == VOID_TYPE)
3873 /* Do some simple checks before doing three hashtable queries. */
3874 if (INTEGRAL_TYPE_P (t1)
3875 || SCALAR_FLOAT_TYPE_P (t1)
3876 || FIXED_POINT_TYPE_P (t1)
3877 || TREE_CODE (t1) == VECTOR_TYPE
3878 || TREE_CODE (t1) == COMPLEX_TYPE
3879 || TREE_CODE (t1) == OFFSET_TYPE)
3881 /* Can't be the same type if they have different alignment,
3882 sign, precision or mode. */
3883 if (TYPE_ALIGN (t1) != TYPE_ALIGN (t2)
3884 || TYPE_PRECISION (t1) != TYPE_PRECISION (t2)
3885 || TYPE_MODE (t1) != TYPE_MODE (t2)
3886 || TYPE_UNSIGNED (t1) != TYPE_UNSIGNED (t2))
3889 if (TREE_CODE (t1) == INTEGER_TYPE
3890 && (TYPE_IS_SIZETYPE (t1) != TYPE_IS_SIZETYPE (t2)
3891 || TYPE_STRING_FLAG (t1) != TYPE_STRING_FLAG (t2)))
3894 /* That's all we need to check for float and fixed-point types. */
3895 if (SCALAR_FLOAT_TYPE_P (t1)
3896 || FIXED_POINT_TYPE_P (t1))
3899 /* For integral types fall thru to more complex checks. */
3902 else if (AGGREGATE_TYPE_P (t1) || POINTER_TYPE_P (t1))
3904 /* Can't be the same type if they have different alignment or mode. */
3905 if (TYPE_ALIGN (t1) != TYPE_ALIGN (t2)
3906 || TYPE_MODE (t1) != TYPE_MODE (t2))
3910 /* If the hash values of t1 and t2 are different the types can't
3911 possibly be the same. This helps keeping the type-pair hashtable
3912 small, only tracking comparisons for hash collisions. */
3913 if (gimple_type_hash (t1) != gimple_type_hash (t2))
3916 /* If we've visited this type pair before (in the case of aggregates
3917 with self-referential types), and we made a decision, return it. */
3918 p = lookup_type_pair (t1, t2, >c_visited, >c_ob);
3919 if (p->same_p[mode] == 0 || p->same_p[mode] == 1)
3921 /* We have already decided whether T1 and T2 are the
3922 same, return the cached result. */
3923 return p->same_p[mode] == 1;
3926 /* Now set up the SCC machinery for the comparison. */
3927 gtc_next_dfs_num = 1;
3928 sccstate = pointer_map_create ();
3929 gcc_obstack_init (&sccstate_obstack);
3930 res = gimple_types_compatible_p_1 (t1, t2, mode, p,
3931 &sccstack, sccstate, &sccstate_obstack);
3932 VEC_free (type_pair_t, heap, sccstack);
3933 pointer_map_destroy (sccstate);
3934 obstack_free (&sccstate_obstack, NULL);
3941 iterative_hash_gimple_type (tree, hashval_t, VEC(tree, heap) **,
3942 struct pointer_map_t *, struct obstack *);
3944 /* DFS visit the edge from the callers type with state *STATE to T.
3945 Update the callers type hash V with the hash for T if it is not part
3946 of the SCC containing the callers type and return it.
3947 SCCSTACK, SCCSTATE and SCCSTATE_OBSTACK are state for the DFS walk done. */
3950 visit (tree t, struct sccs *state, hashval_t v,
3951 VEC (tree, heap) **sccstack,
3952 struct pointer_map_t *sccstate,
3953 struct obstack *sccstate_obstack)
3955 struct sccs *cstate = NULL;
3956 struct tree_int_map m;
3959 /* If there is a hash value recorded for this type then it can't
3960 possibly be part of our parent SCC. Simply mix in its hash. */
3962 if ((slot = htab_find_slot (type_hash_cache, &m, NO_INSERT))
3964 return iterative_hash_hashval_t (((struct tree_int_map *) *slot)->to, v);
3966 if ((slot = pointer_map_contains (sccstate, t)) != NULL)
3967 cstate = (struct sccs *)*slot;
3971 /* Not yet visited. DFS recurse. */
3972 tem = iterative_hash_gimple_type (t, v,
3973 sccstack, sccstate, sccstate_obstack);
3975 cstate = (struct sccs *)* pointer_map_contains (sccstate, t);
3976 state->low = MIN (state->low, cstate->low);
3977 /* If the type is no longer on the SCC stack and thus is not part
3978 of the parents SCC mix in its hash value. Otherwise we will
3979 ignore the type for hashing purposes and return the unaltered
3981 if (!cstate->on_sccstack)
3984 if (cstate->dfsnum < state->dfsnum
3985 && cstate->on_sccstack)
3986 state->low = MIN (cstate->dfsnum, state->low);
3988 /* We are part of our parents SCC, skip this type during hashing
3989 and return the unaltered hash value. */
3993 /* Hash NAME with the previous hash value V and return it. */
3996 iterative_hash_name (tree name, hashval_t v)
4000 if (TREE_CODE (name) == TYPE_DECL)
4001 name = DECL_NAME (name);
4004 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
4005 return iterative_hash_object (IDENTIFIER_HASH_VALUE (name), v);
4008 /* Returning a hash value for gimple type TYPE combined with VAL.
4009 SCCSTACK, SCCSTATE and SCCSTATE_OBSTACK are state for the DFS walk done.
4011 To hash a type we end up hashing in types that are reachable.
4012 Through pointers we can end up with cycles which messes up the
4013 required property that we need to compute the same hash value
4014 for structurally equivalent types. To avoid this we have to
4015 hash all types in a cycle (the SCC) in a commutative way. The
4016 easiest way is to not mix in the hashes of the SCC members at
4017 all. To make this work we have to delay setting the hash
4018 values of the SCC until it is complete. */
4021 iterative_hash_gimple_type (tree type, hashval_t val,
4022 VEC(tree, heap) **sccstack,
4023 struct pointer_map_t *sccstate,
4024 struct obstack *sccstate_obstack)
4030 /* Not visited during this DFS walk. */
4031 gcc_checking_assert (!pointer_map_contains (sccstate, type));
4032 state = XOBNEW (sccstate_obstack, struct sccs);
4033 *pointer_map_insert (sccstate, type) = state;
4035 VEC_safe_push (tree, heap, *sccstack, type);
4036 state->dfsnum = next_dfs_num++;
4037 state->low = state->dfsnum;
4038 state->on_sccstack = true;
4040 /* Combine a few common features of types so that types are grouped into
4041 smaller sets; when searching for existing matching types to merge,
4042 only existing types having the same features as the new type will be
4044 v = iterative_hash_hashval_t (TREE_CODE (type), 0);
4045 v = iterative_hash_hashval_t (TYPE_QUALS (type), v);
4046 v = iterative_hash_hashval_t (TREE_ADDRESSABLE (type), v);
4048 /* Do not hash the types size as this will cause differences in
4049 hash values for the complete vs. the incomplete type variant. */
4051 /* Incorporate common features of numerical types. */
4052 if (INTEGRAL_TYPE_P (type)
4053 || SCALAR_FLOAT_TYPE_P (type)
4054 || FIXED_POINT_TYPE_P (type))
4056 v = iterative_hash_hashval_t (TYPE_PRECISION (type), v);
4057 v = iterative_hash_hashval_t (TYPE_MODE (type), v);
4058 v = iterative_hash_hashval_t (TYPE_UNSIGNED (type), v);
4061 /* For pointer and reference types, fold in information about the type
4062 pointed to but do not recurse into possibly incomplete types to
4063 avoid hash differences for complete vs. incomplete types. */
4064 if (POINTER_TYPE_P (type))
4066 if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (type)))
4068 v = iterative_hash_hashval_t (TREE_CODE (TREE_TYPE (type)), v);
4069 v = iterative_hash_name
4070 (TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (type))), v);
4073 v = visit (TREE_TYPE (type), state, v,
4074 sccstack, sccstate, sccstate_obstack);
4077 /* For integer types hash the types min/max values and the string flag. */
4078 if (TREE_CODE (type) == INTEGER_TYPE)
4080 /* OMP lowering can introduce error_mark_node in place of
4081 random local decls in types. */
4082 if (TYPE_MIN_VALUE (type) != error_mark_node)
4083 v = iterative_hash_expr (TYPE_MIN_VALUE (type), v);
4084 if (TYPE_MAX_VALUE (type) != error_mark_node)
4085 v = iterative_hash_expr (TYPE_MAX_VALUE (type), v);
4086 v = iterative_hash_hashval_t (TYPE_STRING_FLAG (type), v);
4089 /* For array types hash their domain and the string flag. */
4090 if (TREE_CODE (type) == ARRAY_TYPE
4091 && TYPE_DOMAIN (type))
4093 v = iterative_hash_hashval_t (TYPE_STRING_FLAG (type), v);
4094 v = visit (TYPE_DOMAIN (type), state, v,
4095 sccstack, sccstate, sccstate_obstack);
4098 /* Recurse for aggregates with a single element type. */
4099 if (TREE_CODE (type) == ARRAY_TYPE
4100 || TREE_CODE (type) == COMPLEX_TYPE
4101 || TREE_CODE (type) == VECTOR_TYPE)
4102 v = visit (TREE_TYPE (type), state, v,
4103 sccstack, sccstate, sccstate_obstack);
4105 /* Incorporate function return and argument types. */
4106 if (TREE_CODE (type) == FUNCTION_TYPE || TREE_CODE (type) == METHOD_TYPE)
4111 /* For method types also incorporate their parent class. */
4112 if (TREE_CODE (type) == METHOD_TYPE)
4113 v = visit (TYPE_METHOD_BASETYPE (type), state, v,
4114 sccstack, sccstate, sccstate_obstack);
4116 /* For result types allow mismatch in completeness. */
4117 if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (type)))
4119 v = iterative_hash_hashval_t (TREE_CODE (TREE_TYPE (type)), v);
4120 v = iterative_hash_name
4121 (TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (type))), v);
4124 v = visit (TREE_TYPE (type), state, v,
4125 sccstack, sccstate, sccstate_obstack);
4127 for (p = TYPE_ARG_TYPES (type), na = 0; p; p = TREE_CHAIN (p))
4129 /* For argument types allow mismatch in completeness. */
4130 if (RECORD_OR_UNION_TYPE_P (TREE_VALUE (p)))
4132 v = iterative_hash_hashval_t (TREE_CODE (TREE_VALUE (p)), v);
4133 v = iterative_hash_name
4134 (TYPE_NAME (TYPE_MAIN_VARIANT (TREE_VALUE (p))), v);
4137 v = visit (TREE_VALUE (p), state, v,
4138 sccstack, sccstate, sccstate_obstack);
4142 v = iterative_hash_hashval_t (na, v);
4145 if (TREE_CODE (type) == RECORD_TYPE
4146 || TREE_CODE (type) == UNION_TYPE
4147 || TREE_CODE (type) == QUAL_UNION_TYPE)
4152 v = iterative_hash_name (TYPE_NAME (TYPE_MAIN_VARIANT (type)), v);
4154 for (f = TYPE_FIELDS (type), nf = 0; f; f = TREE_CHAIN (f))
4156 v = iterative_hash_name (DECL_NAME (f), v);
4157 v = visit (TREE_TYPE (f), state, v,
4158 sccstack, sccstate, sccstate_obstack);
4162 v = iterative_hash_hashval_t (nf, v);
4165 /* Record hash for us. */
4168 /* See if we found an SCC. */
4169 if (state->low == state->dfsnum)
4173 /* Pop off the SCC and set its hash values. */
4176 struct sccs *cstate;
4177 struct tree_int_map *m = ggc_alloc_cleared_tree_int_map ();
4178 x = VEC_pop (tree, *sccstack);
4179 cstate = (struct sccs *)*pointer_map_contains (sccstate, x);
4180 cstate->on_sccstack = false;
4182 m->to = cstate->u.hash;
4183 slot = htab_find_slot (type_hash_cache, m, INSERT);
4184 gcc_assert (!*slot);
4190 return iterative_hash_hashval_t (v, val);
4194 /* Returns a hash value for P (assumed to be a type). The hash value
4195 is computed using some distinguishing features of the type. Note
4196 that we cannot use pointer hashing here as we may be dealing with
4197 two distinct instances of the same type.
4199 This function should produce the same hash value for two compatible
4200 types according to gimple_types_compatible_p. */
4203 gimple_type_hash (const void *p)
4205 const_tree t = (const_tree) p;
4206 VEC(tree, heap) *sccstack = NULL;
4207 struct pointer_map_t *sccstate;
4208 struct obstack sccstate_obstack;
4211 struct tree_int_map m;
4213 if (type_hash_cache == NULL)
4214 type_hash_cache = htab_create_ggc (512, tree_int_map_hash,
4215 tree_int_map_eq, NULL);
4217 m.base.from = CONST_CAST_TREE (t);
4218 if ((slot = htab_find_slot (type_hash_cache, &m, NO_INSERT))
4220 return iterative_hash_hashval_t (((struct tree_int_map *) *slot)->to, 0);
4222 /* Perform a DFS walk and pre-hash all reachable types. */
4224 sccstate = pointer_map_create ();
4225 gcc_obstack_init (&sccstate_obstack);
4226 val = iterative_hash_gimple_type (CONST_CAST_TREE (t), 0,
4227 &sccstack, sccstate, &sccstate_obstack);
4228 VEC_free (tree, heap, sccstack);
4229 pointer_map_destroy (sccstate);
4230 obstack_free (&sccstate_obstack, NULL);
4236 /* Returns nonzero if P1 and P2 are equal. */
4239 gimple_type_eq (const void *p1, const void *p2)
4241 const_tree t1 = (const_tree) p1;
4242 const_tree t2 = (const_tree) p2;
4243 return gimple_types_compatible_p (CONST_CAST_TREE (t1),
4244 CONST_CAST_TREE (t2), GTC_MERGE);
4248 /* Register type T in the global type table gimple_types.
4249 If another type T', compatible with T, already existed in
4250 gimple_types then return T', otherwise return T. This is used by
4251 LTO to merge identical types read from different TUs. */
4254 gimple_register_type (tree t)
4257 gimple_type_leader_entry *leader;
4258 tree mv_leader = NULL_TREE;
4260 gcc_assert (TYPE_P (t));
4262 if (!gimple_type_leader)
4263 gimple_type_leader = ggc_alloc_cleared_vec_gimple_type_leader_entry_s
4264 (GIMPLE_TYPE_LEADER_SIZE);
4265 /* If we registered this type before return the cached result. */
4266 leader = &gimple_type_leader[TYPE_UID (t) % GIMPLE_TYPE_LEADER_SIZE];
4267 if (leader->type == t)
4268 return leader->leader;
4270 /* Always register the main variant first. This is important so we
4271 pick up the non-typedef variants as canonical, otherwise we'll end
4272 up taking typedef ids for structure tags during comparison. */
4273 if (TYPE_MAIN_VARIANT (t) != t)
4274 mv_leader = gimple_register_type (TYPE_MAIN_VARIANT (t));
4276 if (gimple_types == NULL)
4277 gimple_types = htab_create_ggc (16381, gimple_type_hash, gimple_type_eq, 0);
4279 slot = htab_find_slot (gimple_types, t, INSERT);
4281 && *(tree *)slot != t)
4283 tree new_type = (tree) *((tree *) slot);
4285 /* Do not merge types with different addressability. */
4286 gcc_assert (TREE_ADDRESSABLE (t) == TREE_ADDRESSABLE (new_type));
4288 /* If t is not its main variant then make t unreachable from its
4289 main variant list. Otherwise we'd queue up a lot of duplicates
4291 if (t != TYPE_MAIN_VARIANT (t))
4293 tree tem = TYPE_MAIN_VARIANT (t);
4294 while (tem && TYPE_NEXT_VARIANT (tem) != t)
4295 tem = TYPE_NEXT_VARIANT (tem);
4297 TYPE_NEXT_VARIANT (tem) = TYPE_NEXT_VARIANT (t);
4298 TYPE_NEXT_VARIANT (t) = NULL_TREE;
4301 /* If we are a pointer then remove us from the pointer-to or
4302 reference-to chain. Otherwise we'd queue up a lot of duplicates
4304 if (TREE_CODE (t) == POINTER_TYPE)
4306 if (TYPE_POINTER_TO (TREE_TYPE (t)) == t)
4307 TYPE_POINTER_TO (TREE_TYPE (t)) = TYPE_NEXT_PTR_TO (t);
4310 tree tem = TYPE_POINTER_TO (TREE_TYPE (t));
4311 while (tem && TYPE_NEXT_PTR_TO (tem) != t)
4312 tem = TYPE_NEXT_PTR_TO (tem);
4314 TYPE_NEXT_PTR_TO (tem) = TYPE_NEXT_PTR_TO (t);
4316 TYPE_NEXT_PTR_TO (t) = NULL_TREE;
4318 else if (TREE_CODE (t) == REFERENCE_TYPE)
4320 if (TYPE_REFERENCE_TO (TREE_TYPE (t)) == t)
4321 TYPE_REFERENCE_TO (TREE_TYPE (t)) = TYPE_NEXT_REF_TO (t);
4324 tree tem = TYPE_REFERENCE_TO (TREE_TYPE (t));
4325 while (tem && TYPE_NEXT_REF_TO (tem) != t)
4326 tem = TYPE_NEXT_REF_TO (tem);
4328 TYPE_NEXT_REF_TO (tem) = TYPE_NEXT_REF_TO (t);
4330 TYPE_NEXT_REF_TO (t) = NULL_TREE;
4334 leader->leader = new_type;
4341 /* We're the type leader. Make our TYPE_MAIN_VARIANT valid. */
4342 if (TYPE_MAIN_VARIANT (t) != t
4343 && TYPE_MAIN_VARIANT (t) != mv_leader)
4345 /* Remove us from our main variant list as we are not the variant
4346 leader and the variant leader will change. */
4347 tree tem = TYPE_MAIN_VARIANT (t);
4348 while (tem && TYPE_NEXT_VARIANT (tem) != t)
4349 tem = TYPE_NEXT_VARIANT (tem);
4351 TYPE_NEXT_VARIANT (tem) = TYPE_NEXT_VARIANT (t);
4352 TYPE_NEXT_VARIANT (t) = NULL_TREE;
4353 /* Adjust our main variant. Linking us into its variant list
4354 will happen at fixup time. */
4355 TYPE_MAIN_VARIANT (t) = mv_leader;
4364 /* Returns nonzero if P1 and P2 are equal. */
4367 gimple_canonical_type_eq (const void *p1, const void *p2)
4369 const_tree t1 = (const_tree) p1;
4370 const_tree t2 = (const_tree) p2;
4371 return gimple_types_compatible_p (CONST_CAST_TREE (t1),
4372 CONST_CAST_TREE (t2), GTC_DIAG);
4375 /* Register type T in the global type table gimple_types.
4376 If another type T', compatible with T, already existed in
4377 gimple_types then return T', otherwise return T. This is used by
4378 LTO to merge identical types read from different TUs. */
4381 gimple_register_canonical_type (tree t)
4386 gcc_assert (TYPE_P (t));
4388 if (TYPE_CANONICAL (t))
4389 return TYPE_CANONICAL (t);
4391 /* Always register the type itself first so that if it turns out
4392 to be the canonical type it will be the one we merge to as well. */
4393 t = gimple_register_type (t);
4395 /* Always register the main variant first. This is important so we
4396 pick up the non-typedef variants as canonical, otherwise we'll end
4397 up taking typedef ids for structure tags during comparison. */
4398 if (TYPE_MAIN_VARIANT (t) != t)
4399 gimple_register_canonical_type (TYPE_MAIN_VARIANT (t));
4401 if (gimple_canonical_types == NULL)
4402 gimple_canonical_types = htab_create_ggc (16381, gimple_type_hash,
4403 gimple_canonical_type_eq, 0);
4405 slot = htab_find_slot (gimple_canonical_types, t, INSERT);
4407 && *(tree *)slot != t)
4409 tree new_type = (tree) *((tree *) slot);
4411 TYPE_CANONICAL (t) = new_type;
4416 TYPE_CANONICAL (t) = t;
4420 /* Also cache the canonical type in the non-leaders. */
4421 TYPE_CANONICAL (orig_t) = t;
4427 /* Show statistics on references to the global type table gimple_types. */
4430 print_gimple_types_stats (void)
4433 fprintf (stderr, "GIMPLE type table: size %ld, %ld elements, "
4434 "%ld searches, %ld collisions (ratio: %f)\n",
4435 (long) htab_size (gimple_types),
4436 (long) htab_elements (gimple_types),
4437 (long) gimple_types->searches,
4438 (long) gimple_types->collisions,
4439 htab_collisions (gimple_types));
4441 fprintf (stderr, "GIMPLE type table is empty\n");
4442 if (gimple_canonical_types)
4443 fprintf (stderr, "GIMPLE canonical type table: size %ld, %ld elements, "
4444 "%ld searches, %ld collisions (ratio: %f)\n",
4445 (long) htab_size (gimple_canonical_types),
4446 (long) htab_elements (gimple_canonical_types),
4447 (long) gimple_canonical_types->searches,
4448 (long) gimple_canonical_types->collisions,
4449 htab_collisions (gimple_canonical_types));
4451 fprintf (stderr, "GIMPLE canonical type table is empty\n");
4452 if (type_hash_cache)
4453 fprintf (stderr, "GIMPLE type hash table: size %ld, %ld elements, "
4454 "%ld searches, %ld collisions (ratio: %f)\n",
4455 (long) htab_size (type_hash_cache),
4456 (long) htab_elements (type_hash_cache),
4457 (long) type_hash_cache->searches,
4458 (long) type_hash_cache->collisions,
4459 htab_collisions (type_hash_cache));
4461 fprintf (stderr, "GIMPLE type hash table is empty\n");
4463 fprintf (stderr, "GIMPLE type comparison table: size %ld, %ld "
4464 "elements, %ld searches, %ld collisions (ratio: %f)\n",
4465 (long) htab_size (gtc_visited),
4466 (long) htab_elements (gtc_visited),
4467 (long) gtc_visited->searches,
4468 (long) gtc_visited->collisions,
4469 htab_collisions (gtc_visited));
4471 fprintf (stderr, "GIMPLE type comparison table is empty\n");
4474 /* Free the gimple type hashtables used for LTO type merging. */
4477 free_gimple_type_tables (void)
4479 /* Last chance to print stats for the tables. */
4480 if (flag_lto_report)
4481 print_gimple_types_stats ();
4485 htab_delete (gimple_types);
4486 gimple_types = NULL;
4488 if (gimple_canonical_types)
4490 htab_delete (gimple_canonical_types);
4491 gimple_canonical_types = NULL;
4493 if (type_hash_cache)
4495 htab_delete (type_hash_cache);
4496 type_hash_cache = NULL;
4500 htab_delete (gtc_visited);
4501 obstack_free (>c_ob, NULL);
4504 gimple_type_leader = NULL;
4508 /* Return a type the same as TYPE except unsigned or
4509 signed according to UNSIGNEDP. */
4512 gimple_signed_or_unsigned_type (bool unsignedp, tree type)
4516 type1 = TYPE_MAIN_VARIANT (type);
4517 if (type1 == signed_char_type_node
4518 || type1 == char_type_node
4519 || type1 == unsigned_char_type_node)
4520 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
4521 if (type1 == integer_type_node || type1 == unsigned_type_node)
4522 return unsignedp ? unsigned_type_node : integer_type_node;
4523 if (type1 == short_integer_type_node || type1 == short_unsigned_type_node)
4524 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
4525 if (type1 == long_integer_type_node || type1 == long_unsigned_type_node)
4526 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
4527 if (type1 == long_long_integer_type_node
4528 || type1 == long_long_unsigned_type_node)
4530 ? long_long_unsigned_type_node
4531 : long_long_integer_type_node;
4532 if (int128_integer_type_node && (type1 == int128_integer_type_node || type1 == int128_unsigned_type_node))
4534 ? int128_unsigned_type_node
4535 : int128_integer_type_node;
4536 #if HOST_BITS_PER_WIDE_INT >= 64
4537 if (type1 == intTI_type_node || type1 == unsigned_intTI_type_node)
4538 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
4540 if (type1 == intDI_type_node || type1 == unsigned_intDI_type_node)
4541 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
4542 if (type1 == intSI_type_node || type1 == unsigned_intSI_type_node)
4543 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
4544 if (type1 == intHI_type_node || type1 == unsigned_intHI_type_node)
4545 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
4546 if (type1 == intQI_type_node || type1 == unsigned_intQI_type_node)
4547 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
4549 #define GIMPLE_FIXED_TYPES(NAME) \
4550 if (type1 == short_ ## NAME ## _type_node \
4551 || type1 == unsigned_short_ ## NAME ## _type_node) \
4552 return unsignedp ? unsigned_short_ ## NAME ## _type_node \
4553 : short_ ## NAME ## _type_node; \
4554 if (type1 == NAME ## _type_node \
4555 || type1 == unsigned_ ## NAME ## _type_node) \
4556 return unsignedp ? unsigned_ ## NAME ## _type_node \
4557 : NAME ## _type_node; \
4558 if (type1 == long_ ## NAME ## _type_node \
4559 || type1 == unsigned_long_ ## NAME ## _type_node) \
4560 return unsignedp ? unsigned_long_ ## NAME ## _type_node \
4561 : long_ ## NAME ## _type_node; \
4562 if (type1 == long_long_ ## NAME ## _type_node \
4563 || type1 == unsigned_long_long_ ## NAME ## _type_node) \
4564 return unsignedp ? unsigned_long_long_ ## NAME ## _type_node \
4565 : long_long_ ## NAME ## _type_node;
4567 #define GIMPLE_FIXED_MODE_TYPES(NAME) \
4568 if (type1 == NAME ## _type_node \
4569 || type1 == u ## NAME ## _type_node) \
4570 return unsignedp ? u ## NAME ## _type_node \
4571 : NAME ## _type_node;
4573 #define GIMPLE_FIXED_TYPES_SAT(NAME) \
4574 if (type1 == sat_ ## short_ ## NAME ## _type_node \
4575 || type1 == sat_ ## unsigned_short_ ## NAME ## _type_node) \
4576 return unsignedp ? sat_ ## unsigned_short_ ## NAME ## _type_node \
4577 : sat_ ## short_ ## NAME ## _type_node; \
4578 if (type1 == sat_ ## NAME ## _type_node \
4579 || type1 == sat_ ## unsigned_ ## NAME ## _type_node) \
4580 return unsignedp ? sat_ ## unsigned_ ## NAME ## _type_node \
4581 : sat_ ## NAME ## _type_node; \
4582 if (type1 == sat_ ## long_ ## NAME ## _type_node \
4583 || type1 == sat_ ## unsigned_long_ ## NAME ## _type_node) \
4584 return unsignedp ? sat_ ## unsigned_long_ ## NAME ## _type_node \
4585 : sat_ ## long_ ## NAME ## _type_node; \
4586 if (type1 == sat_ ## long_long_ ## NAME ## _type_node \
4587 || type1 == sat_ ## unsigned_long_long_ ## NAME ## _type_node) \
4588 return unsignedp ? sat_ ## unsigned_long_long_ ## NAME ## _type_node \
4589 : sat_ ## long_long_ ## NAME ## _type_node;
4591 #define GIMPLE_FIXED_MODE_TYPES_SAT(NAME) \
4592 if (type1 == sat_ ## NAME ## _type_node \
4593 || type1 == sat_ ## u ## NAME ## _type_node) \
4594 return unsignedp ? sat_ ## u ## NAME ## _type_node \
4595 : sat_ ## NAME ## _type_node;
4597 GIMPLE_FIXED_TYPES (fract);
4598 GIMPLE_FIXED_TYPES_SAT (fract);
4599 GIMPLE_FIXED_TYPES (accum);
4600 GIMPLE_FIXED_TYPES_SAT (accum);
4602 GIMPLE_FIXED_MODE_TYPES (qq);
4603 GIMPLE_FIXED_MODE_TYPES (hq);
4604 GIMPLE_FIXED_MODE_TYPES (sq);
4605 GIMPLE_FIXED_MODE_TYPES (dq);
4606 GIMPLE_FIXED_MODE_TYPES (tq);
4607 GIMPLE_FIXED_MODE_TYPES_SAT (qq);
4608 GIMPLE_FIXED_MODE_TYPES_SAT (hq);
4609 GIMPLE_FIXED_MODE_TYPES_SAT (sq);
4610 GIMPLE_FIXED_MODE_TYPES_SAT (dq);
4611 GIMPLE_FIXED_MODE_TYPES_SAT (tq);
4612 GIMPLE_FIXED_MODE_TYPES (ha);
4613 GIMPLE_FIXED_MODE_TYPES (sa);
4614 GIMPLE_FIXED_MODE_TYPES (da);
4615 GIMPLE_FIXED_MODE_TYPES (ta);
4616 GIMPLE_FIXED_MODE_TYPES_SAT (ha);
4617 GIMPLE_FIXED_MODE_TYPES_SAT (sa);
4618 GIMPLE_FIXED_MODE_TYPES_SAT (da);
4619 GIMPLE_FIXED_MODE_TYPES_SAT (ta);
4621 /* For ENUMERAL_TYPEs in C++, must check the mode of the types, not
4622 the precision; they have precision set to match their range, but
4623 may use a wider mode to match an ABI. If we change modes, we may
4624 wind up with bad conversions. For INTEGER_TYPEs in C, must check
4625 the precision as well, so as to yield correct results for
4626 bit-field types. C++ does not have these separate bit-field
4627 types, and producing a signed or unsigned variant of an
4628 ENUMERAL_TYPE may cause other problems as well. */
4629 if (!INTEGRAL_TYPE_P (type)
4630 || TYPE_UNSIGNED (type) == unsignedp)
4633 #define TYPE_OK(node) \
4634 (TYPE_MODE (type) == TYPE_MODE (node) \
4635 && TYPE_PRECISION (type) == TYPE_PRECISION (node))
4636 if (TYPE_OK (signed_char_type_node))
4637 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
4638 if (TYPE_OK (integer_type_node))
4639 return unsignedp ? unsigned_type_node : integer_type_node;
4640 if (TYPE_OK (short_integer_type_node))
4641 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
4642 if (TYPE_OK (long_integer_type_node))
4643 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
4644 if (TYPE_OK (long_long_integer_type_node))
4646 ? long_long_unsigned_type_node
4647 : long_long_integer_type_node);
4648 if (int128_integer_type_node && TYPE_OK (int128_integer_type_node))
4650 ? int128_unsigned_type_node
4651 : int128_integer_type_node);
4653 #if HOST_BITS_PER_WIDE_INT >= 64
4654 if (TYPE_OK (intTI_type_node))
4655 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
4657 if (TYPE_OK (intDI_type_node))
4658 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
4659 if (TYPE_OK (intSI_type_node))
4660 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
4661 if (TYPE_OK (intHI_type_node))
4662 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
4663 if (TYPE_OK (intQI_type_node))
4664 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
4666 #undef GIMPLE_FIXED_TYPES
4667 #undef GIMPLE_FIXED_MODE_TYPES
4668 #undef GIMPLE_FIXED_TYPES_SAT
4669 #undef GIMPLE_FIXED_MODE_TYPES_SAT
4672 return build_nonstandard_integer_type (TYPE_PRECISION (type), unsignedp);
4676 /* Return an unsigned type the same as TYPE in other respects. */
4679 gimple_unsigned_type (tree type)
4681 return gimple_signed_or_unsigned_type (true, type);
4685 /* Return a signed type the same as TYPE in other respects. */
4688 gimple_signed_type (tree type)
4690 return gimple_signed_or_unsigned_type (false, type);
4694 /* Return the typed-based alias set for T, which may be an expression
4695 or a type. Return -1 if we don't do anything special. */
4698 gimple_get_alias_set (tree t)
4702 /* Permit type-punning when accessing a union, provided the access
4703 is directly through the union. For example, this code does not
4704 permit taking the address of a union member and then storing
4705 through it. Even the type-punning allowed here is a GCC
4706 extension, albeit a common and useful one; the C standard says
4707 that such accesses have implementation-defined behavior. */
4709 TREE_CODE (u) == COMPONENT_REF || TREE_CODE (u) == ARRAY_REF;
4710 u = TREE_OPERAND (u, 0))
4711 if (TREE_CODE (u) == COMPONENT_REF
4712 && TREE_CODE (TREE_TYPE (TREE_OPERAND (u, 0))) == UNION_TYPE)
4715 /* That's all the expressions we handle specially. */
4719 /* For convenience, follow the C standard when dealing with
4720 character types. Any object may be accessed via an lvalue that
4721 has character type. */
4722 if (t == char_type_node
4723 || t == signed_char_type_node
4724 || t == unsigned_char_type_node)
4727 /* Allow aliasing between signed and unsigned variants of the same
4728 type. We treat the signed variant as canonical. */
4729 if (TREE_CODE (t) == INTEGER_TYPE && TYPE_UNSIGNED (t))
4731 tree t1 = gimple_signed_type (t);
4733 /* t1 == t can happen for boolean nodes which are always unsigned. */
4735 return get_alias_set (t1);
4742 /* Data structure used to count the number of dereferences to PTR
4743 inside an expression. */
4747 unsigned num_stores;
4751 /* Helper for count_uses_and_derefs. Called by walk_tree to look for
4752 (ALIGN/MISALIGNED_)INDIRECT_REF nodes for the pointer passed in DATA. */
4755 count_ptr_derefs (tree *tp, int *walk_subtrees, void *data)
4757 struct walk_stmt_info *wi_p = (struct walk_stmt_info *) data;
4758 struct count_ptr_d *count_p = (struct count_ptr_d *) wi_p->info;
4760 /* Do not walk inside ADDR_EXPR nodes. In the expression &ptr->fld,
4761 pointer 'ptr' is *not* dereferenced, it is simply used to compute
4762 the address of 'fld' as 'ptr + offsetof(fld)'. */
4763 if (TREE_CODE (*tp) == ADDR_EXPR)
4769 if (TREE_CODE (*tp) == MEM_REF && TREE_OPERAND (*tp, 0) == count_p->ptr)
4772 count_p->num_stores++;
4774 count_p->num_loads++;
4780 /* Count the number of direct and indirect uses for pointer PTR in
4781 statement STMT. The number of direct uses is stored in
4782 *NUM_USES_P. Indirect references are counted separately depending
4783 on whether they are store or load operations. The counts are
4784 stored in *NUM_STORES_P and *NUM_LOADS_P. */
4787 count_uses_and_derefs (tree ptr, gimple stmt, unsigned *num_uses_p,
4788 unsigned *num_loads_p, unsigned *num_stores_p)
4797 /* Find out the total number of uses of PTR in STMT. */
4798 FOR_EACH_SSA_TREE_OPERAND (use, stmt, i, SSA_OP_USE)
4802 /* Now count the number of indirect references to PTR. This is
4803 truly awful, but we don't have much choice. There are no parent
4804 pointers inside INDIRECT_REFs, so an expression like
4805 '*x_1 = foo (x_1, *x_1)' needs to be traversed piece by piece to
4806 find all the indirect and direct uses of x_1 inside. The only
4807 shortcut we can take is the fact that GIMPLE only allows
4808 INDIRECT_REFs inside the expressions below. */
4809 if (is_gimple_assign (stmt)
4810 || gimple_code (stmt) == GIMPLE_RETURN
4811 || gimple_code (stmt) == GIMPLE_ASM
4812 || is_gimple_call (stmt))
4814 struct walk_stmt_info wi;
4815 struct count_ptr_d count;
4818 count.num_stores = 0;
4819 count.num_loads = 0;
4821 memset (&wi, 0, sizeof (wi));
4823 walk_gimple_op (stmt, count_ptr_derefs, &wi);
4825 *num_stores_p = count.num_stores;
4826 *num_loads_p = count.num_loads;
4829 gcc_assert (*num_uses_p >= *num_loads_p + *num_stores_p);
4832 /* From a tree operand OP return the base of a load or store operation
4833 or NULL_TREE if OP is not a load or a store. */
4836 get_base_loadstore (tree op)
4838 while (handled_component_p (op))
4839 op = TREE_OPERAND (op, 0);
4841 || INDIRECT_REF_P (op)
4842 || TREE_CODE (op) == MEM_REF
4843 || TREE_CODE (op) == TARGET_MEM_REF)
4848 /* For the statement STMT call the callbacks VISIT_LOAD, VISIT_STORE and
4849 VISIT_ADDR if non-NULL on loads, store and address-taken operands
4850 passing the STMT, the base of the operand and DATA to it. The base
4851 will be either a decl, an indirect reference (including TARGET_MEM_REF)
4852 or the argument of an address expression.
4853 Returns the results of these callbacks or'ed. */
4856 walk_stmt_load_store_addr_ops (gimple stmt, void *data,
4857 bool (*visit_load)(gimple, tree, void *),
4858 bool (*visit_store)(gimple, tree, void *),
4859 bool (*visit_addr)(gimple, tree, void *))
4863 if (gimple_assign_single_p (stmt))
4868 lhs = get_base_loadstore (gimple_assign_lhs (stmt));
4870 ret |= visit_store (stmt, lhs, data);
4872 rhs = gimple_assign_rhs1 (stmt);
4873 while (handled_component_p (rhs))
4874 rhs = TREE_OPERAND (rhs, 0);
4877 if (TREE_CODE (rhs) == ADDR_EXPR)
4878 ret |= visit_addr (stmt, TREE_OPERAND (rhs, 0), data);
4879 else if (TREE_CODE (rhs) == TARGET_MEM_REF
4880 && TREE_CODE (TMR_BASE (rhs)) == ADDR_EXPR)
4881 ret |= visit_addr (stmt, TREE_OPERAND (TMR_BASE (rhs), 0), data);
4882 else if (TREE_CODE (rhs) == OBJ_TYPE_REF
4883 && TREE_CODE (OBJ_TYPE_REF_OBJECT (rhs)) == ADDR_EXPR)
4884 ret |= visit_addr (stmt, TREE_OPERAND (OBJ_TYPE_REF_OBJECT (rhs),
4886 lhs = gimple_assign_lhs (stmt);
4887 if (TREE_CODE (lhs) == TARGET_MEM_REF
4888 && TREE_CODE (TMR_BASE (lhs)) == ADDR_EXPR)
4889 ret |= visit_addr (stmt, TREE_OPERAND (TMR_BASE (lhs), 0), data);
4893 rhs = get_base_loadstore (rhs);
4895 ret |= visit_load (stmt, rhs, data);
4899 && (is_gimple_assign (stmt)
4900 || gimple_code (stmt) == GIMPLE_COND))
4902 for (i = 0; i < gimple_num_ops (stmt); ++i)
4903 if (gimple_op (stmt, i)
4904 && TREE_CODE (gimple_op (stmt, i)) == ADDR_EXPR)
4905 ret |= visit_addr (stmt, TREE_OPERAND (gimple_op (stmt, i), 0), data);
4907 else if (is_gimple_call (stmt))
4911 tree lhs = gimple_call_lhs (stmt);
4914 lhs = get_base_loadstore (lhs);
4916 ret |= visit_store (stmt, lhs, data);
4919 if (visit_load || visit_addr)
4920 for (i = 0; i < gimple_call_num_args (stmt); ++i)
4922 tree rhs = gimple_call_arg (stmt, i);
4924 && TREE_CODE (rhs) == ADDR_EXPR)
4925 ret |= visit_addr (stmt, TREE_OPERAND (rhs, 0), data);
4926 else if (visit_load)
4928 rhs = get_base_loadstore (rhs);
4930 ret |= visit_load (stmt, rhs, data);
4934 && gimple_call_chain (stmt)
4935 && TREE_CODE (gimple_call_chain (stmt)) == ADDR_EXPR)
4936 ret |= visit_addr (stmt, TREE_OPERAND (gimple_call_chain (stmt), 0),
4939 && gimple_call_return_slot_opt_p (stmt)
4940 && gimple_call_lhs (stmt) != NULL_TREE
4941 && TREE_ADDRESSABLE (TREE_TYPE (gimple_call_lhs (stmt))))
4942 ret |= visit_addr (stmt, gimple_call_lhs (stmt), data);
4944 else if (gimple_code (stmt) == GIMPLE_ASM)
4947 const char *constraint;
4948 const char **oconstraints;
4949 bool allows_mem, allows_reg, is_inout;
4950 noutputs = gimple_asm_noutputs (stmt);
4951 oconstraints = XALLOCAVEC (const char *, noutputs);
4952 if (visit_store || visit_addr)
4953 for (i = 0; i < gimple_asm_noutputs (stmt); ++i)
4955 tree link = gimple_asm_output_op (stmt, i);
4956 tree op = get_base_loadstore (TREE_VALUE (link));
4957 if (op && visit_store)
4958 ret |= visit_store (stmt, op, data);
4961 constraint = TREE_STRING_POINTER
4962 (TREE_VALUE (TREE_PURPOSE (link)));
4963 oconstraints[i] = constraint;
4964 parse_output_constraint (&constraint, i, 0, 0, &allows_mem,
4965 &allows_reg, &is_inout);
4966 if (op && !allows_reg && allows_mem)
4967 ret |= visit_addr (stmt, op, data);
4970 if (visit_load || visit_addr)
4971 for (i = 0; i < gimple_asm_ninputs (stmt); ++i)
4973 tree link = gimple_asm_input_op (stmt, i);
4974 tree op = TREE_VALUE (link);
4976 && TREE_CODE (op) == ADDR_EXPR)
4977 ret |= visit_addr (stmt, TREE_OPERAND (op, 0), data);
4978 else if (visit_load || visit_addr)
4980 op = get_base_loadstore (op);
4984 ret |= visit_load (stmt, op, data);
4987 constraint = TREE_STRING_POINTER
4988 (TREE_VALUE (TREE_PURPOSE (link)));
4989 parse_input_constraint (&constraint, 0, 0, noutputs,
4991 &allows_mem, &allows_reg);
4992 if (!allows_reg && allows_mem)
4993 ret |= visit_addr (stmt, op, data);
4999 else if (gimple_code (stmt) == GIMPLE_RETURN)
5001 tree op = gimple_return_retval (stmt);
5005 && TREE_CODE (op) == ADDR_EXPR)
5006 ret |= visit_addr (stmt, TREE_OPERAND (op, 0), data);
5007 else if (visit_load)
5009 op = get_base_loadstore (op);
5011 ret |= visit_load (stmt, op, data);
5016 && gimple_code (stmt) == GIMPLE_PHI)
5018 for (i = 0; i < gimple_phi_num_args (stmt); ++i)
5020 tree op = PHI_ARG_DEF (stmt, i);
5021 if (TREE_CODE (op) == ADDR_EXPR)
5022 ret |= visit_addr (stmt, TREE_OPERAND (op, 0), data);
5029 /* Like walk_stmt_load_store_addr_ops but with NULL visit_addr. IPA-CP
5030 should make a faster clone for this case. */
5033 walk_stmt_load_store_ops (gimple stmt, void *data,
5034 bool (*visit_load)(gimple, tree, void *),
5035 bool (*visit_store)(gimple, tree, void *))
5037 return walk_stmt_load_store_addr_ops (stmt, data,
5038 visit_load, visit_store, NULL);
5041 /* Helper for gimple_ior_addresses_taken_1. */
5044 gimple_ior_addresses_taken_1 (gimple stmt ATTRIBUTE_UNUSED,
5045 tree addr, void *data)
5047 bitmap addresses_taken = (bitmap)data;
5048 addr = get_base_address (addr);
5052 bitmap_set_bit (addresses_taken, DECL_UID (addr));
5058 /* Set the bit for the uid of all decls that have their address taken
5059 in STMT in the ADDRESSES_TAKEN bitmap. Returns true if there
5060 were any in this stmt. */
5063 gimple_ior_addresses_taken (bitmap addresses_taken, gimple stmt)
5065 return walk_stmt_load_store_addr_ops (stmt, addresses_taken, NULL, NULL,
5066 gimple_ior_addresses_taken_1);
5070 /* Return a printable name for symbol DECL. */
5073 gimple_decl_printable_name (tree decl, int verbosity)
5075 if (!DECL_NAME (decl))
5078 if (DECL_ASSEMBLER_NAME_SET_P (decl))
5080 const char *str, *mangled_str;
5081 int dmgl_opts = DMGL_NO_OPTS;
5085 dmgl_opts = DMGL_VERBOSE
5089 if (TREE_CODE (decl) == FUNCTION_DECL)
5090 dmgl_opts |= DMGL_PARAMS;
5093 mangled_str = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
5094 str = cplus_demangle_v3 (mangled_str, dmgl_opts);
5095 return (str) ? str : mangled_str;
5098 return IDENTIFIER_POINTER (DECL_NAME (decl));
5101 /* Return true when STMT is builtins call to CODE. */
5104 gimple_call_builtin_p (gimple stmt, enum built_in_function code)
5107 return (is_gimple_call (stmt)
5108 && (fndecl = gimple_call_fndecl (stmt)) != NULL
5109 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
5110 && DECL_FUNCTION_CODE (fndecl) == code);
5113 #include "gt-gimple.h"