1 /* Gimple IR support functions.
3 Copyright 2007, 2008, 2009 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"
33 #include "diagnostic.h"
34 #include "tree-flow.h"
35 #include "value-prof.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 htab_t gimple_types;
45 static struct pointer_map_t *type_hash_cache;
47 /* Global type comparison cache. */
48 static htab_t gtc_visited;
50 /* All the tuples have their operand vector (if present) at the very bottom
51 of the structure. Therefore, the offset required to find the
52 operands vector the size of the structure minus the size of the 1
53 element tree array at the end (see gimple_ops). */
54 #define DEFGSSTRUCT(SYM, STRUCT, HAS_TREE_OP) \
55 (HAS_TREE_OP ? sizeof (struct STRUCT) - sizeof (tree) : 0),
56 EXPORTED_CONST size_t gimple_ops_offset_[] = {
57 #include "gsstruct.def"
61 #define DEFGSSTRUCT(SYM, STRUCT, HAS_TREE_OP) sizeof(struct STRUCT),
62 static const size_t gsstruct_code_size[] = {
63 #include "gsstruct.def"
67 #define DEFGSCODE(SYM, NAME, GSSCODE) NAME,
68 const char *const gimple_code_name[] = {
73 #define DEFGSCODE(SYM, NAME, GSSCODE) GSSCODE,
74 EXPORTED_CONST enum gimple_statement_structure_enum gss_for_code_[] = {
79 #ifdef GATHER_STATISTICS
82 int gimple_alloc_counts[(int) gimple_alloc_kind_all];
83 int gimple_alloc_sizes[(int) gimple_alloc_kind_all];
85 /* Keep in sync with gimple.h:enum gimple_alloc_kind. */
86 static const char * const gimple_alloc_kind_names[] = {
94 #endif /* GATHER_STATISTICS */
96 /* A cache of gimple_seq objects. Sequences are created and destroyed
97 fairly often during gimplification. */
98 static GTY ((deletable)) struct gimple_seq_d *gimple_seq_cache;
100 /* Private API manipulation functions shared only with some
102 extern void gimple_set_stored_syms (gimple, bitmap, bitmap_obstack *);
103 extern void gimple_set_loaded_syms (gimple, bitmap, bitmap_obstack *);
105 /* Gimple tuple constructors.
106 Note: Any constructor taking a ``gimple_seq'' as a parameter, can
107 be passed a NULL to start with an empty sequence. */
109 /* Set the code for statement G to CODE. */
112 gimple_set_code (gimple g, enum gimple_code code)
114 g->gsbase.code = code;
117 /* Return the number of bytes needed to hold a GIMPLE statement with
121 gimple_size (enum gimple_code code)
123 return gsstruct_code_size[gss_for_code (code)];
126 /* Allocate memory for a GIMPLE statement with code CODE and NUM_OPS
130 gimple_alloc_stat (enum gimple_code code, unsigned num_ops MEM_STAT_DECL)
135 size = gimple_size (code);
137 size += sizeof (tree) * (num_ops - 1);
139 #ifdef GATHER_STATISTICS
141 enum gimple_alloc_kind kind = gimple_alloc_kind (code);
142 gimple_alloc_counts[(int) kind]++;
143 gimple_alloc_sizes[(int) kind] += size;
147 stmt = (gimple) ggc_alloc_cleared_stat (size PASS_MEM_STAT);
148 gimple_set_code (stmt, code);
149 gimple_set_num_ops (stmt, num_ops);
151 /* Do not call gimple_set_modified here as it has other side
152 effects and this tuple is still not completely built. */
153 stmt->gsbase.modified = 1;
158 /* Set SUBCODE to be the code of the expression computed by statement G. */
161 gimple_set_subcode (gimple g, unsigned subcode)
163 /* We only have 16 bits for the RHS code. Assert that we are not
165 gcc_assert (subcode < (1 << 16));
166 g->gsbase.subcode = subcode;
171 /* Build a tuple with operands. CODE is the statement to build (which
172 must be one of the GIMPLE_WITH_OPS tuples). SUBCODE is the sub-code
173 for the new tuple. NUM_OPS is the number of operands to allocate. */
175 #define gimple_build_with_ops(c, s, n) \
176 gimple_build_with_ops_stat (c, s, n MEM_STAT_INFO)
179 gimple_build_with_ops_stat (enum gimple_code code, unsigned subcode,
180 unsigned num_ops MEM_STAT_DECL)
182 gimple s = gimple_alloc_stat (code, num_ops PASS_MEM_STAT);
183 gimple_set_subcode (s, subcode);
189 /* Build a GIMPLE_RETURN statement returning RETVAL. */
192 gimple_build_return (tree retval)
194 gimple s = gimple_build_with_ops (GIMPLE_RETURN, ERROR_MARK, 1);
196 gimple_return_set_retval (s, retval);
200 /* Helper for gimple_build_call, gimple_build_call_vec and
201 gimple_build_call_from_tree. Build the basic components of a
202 GIMPLE_CALL statement to function FN with NARGS arguments. */
205 gimple_build_call_1 (tree fn, unsigned nargs)
207 gimple s = gimple_build_with_ops (GIMPLE_CALL, ERROR_MARK, nargs + 3);
208 if (TREE_CODE (fn) == FUNCTION_DECL)
209 fn = build_fold_addr_expr (fn);
210 gimple_set_op (s, 1, fn);
215 /* Build a GIMPLE_CALL statement to function FN with the arguments
216 specified in vector ARGS. */
219 gimple_build_call_vec (tree fn, VEC(tree, heap) *args)
222 unsigned nargs = VEC_length (tree, args);
223 gimple call = gimple_build_call_1 (fn, nargs);
225 for (i = 0; i < nargs; i++)
226 gimple_call_set_arg (call, i, VEC_index (tree, args, i));
232 /* Build a GIMPLE_CALL statement to function FN. NARGS is the number of
233 arguments. The ... are the arguments. */
236 gimple_build_call (tree fn, unsigned nargs, ...)
242 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL || is_gimple_call_addr (fn));
244 call = gimple_build_call_1 (fn, nargs);
246 va_start (ap, nargs);
247 for (i = 0; i < nargs; i++)
248 gimple_call_set_arg (call, i, va_arg (ap, tree));
255 /* Build a GIMPLE_CALL statement from CALL_EXPR T. Note that T is
256 assumed to be in GIMPLE form already. Minimal checking is done of
260 gimple_build_call_from_tree (tree t)
264 tree fndecl = get_callee_fndecl (t);
266 gcc_assert (TREE_CODE (t) == CALL_EXPR);
268 nargs = call_expr_nargs (t);
269 call = gimple_build_call_1 (fndecl ? fndecl : CALL_EXPR_FN (t), nargs);
271 for (i = 0; i < nargs; i++)
272 gimple_call_set_arg (call, i, CALL_EXPR_ARG (t, i));
274 gimple_set_block (call, TREE_BLOCK (t));
276 /* Carry all the CALL_EXPR flags to the new GIMPLE_CALL. */
277 gimple_call_set_chain (call, CALL_EXPR_STATIC_CHAIN (t));
278 gimple_call_set_tail (call, CALL_EXPR_TAILCALL (t));
279 gimple_call_set_cannot_inline (call, CALL_CANNOT_INLINE_P (t));
280 gimple_call_set_return_slot_opt (call, CALL_EXPR_RETURN_SLOT_OPT (t));
281 gimple_call_set_from_thunk (call, CALL_FROM_THUNK_P (t));
282 gimple_call_set_va_arg_pack (call, CALL_EXPR_VA_ARG_PACK (t));
283 gimple_set_no_warning (call, TREE_NO_WARNING (t));
289 /* Extract the operands and code for expression EXPR into *SUBCODE_P,
290 *OP1_P and *OP2_P respectively. */
293 extract_ops_from_tree (tree expr, enum tree_code *subcode_p, tree *op1_p,
296 enum gimple_rhs_class grhs_class;
298 *subcode_p = TREE_CODE (expr);
299 grhs_class = get_gimple_rhs_class (*subcode_p);
301 if (grhs_class == GIMPLE_BINARY_RHS)
303 *op1_p = TREE_OPERAND (expr, 0);
304 *op2_p = TREE_OPERAND (expr, 1);
306 else if (grhs_class == GIMPLE_UNARY_RHS)
308 *op1_p = TREE_OPERAND (expr, 0);
311 else if (grhs_class == GIMPLE_SINGLE_RHS)
321 /* Build a GIMPLE_ASSIGN statement.
323 LHS of the assignment.
324 RHS of the assignment which can be unary or binary. */
327 gimple_build_assign_stat (tree lhs, tree rhs MEM_STAT_DECL)
329 enum tree_code subcode;
332 extract_ops_from_tree (rhs, &subcode, &op1, &op2);
333 return gimple_build_assign_with_ops_stat (subcode, lhs, op1, op2
338 /* Build a GIMPLE_ASSIGN statement with sub-code SUBCODE and operands
339 OP1 and OP2. If OP2 is NULL then SUBCODE must be of class
340 GIMPLE_UNARY_RHS or GIMPLE_SINGLE_RHS. */
343 gimple_build_assign_with_ops_stat (enum tree_code subcode, tree lhs, tree op1,
344 tree op2 MEM_STAT_DECL)
349 /* Need 1 operand for LHS and 1 or 2 for the RHS (depending on the
351 num_ops = get_gimple_rhs_num_ops (subcode) + 1;
353 p = gimple_build_with_ops_stat (GIMPLE_ASSIGN, (unsigned)subcode, num_ops
355 gimple_assign_set_lhs (p, lhs);
356 gimple_assign_set_rhs1 (p, op1);
359 gcc_assert (num_ops > 2);
360 gimple_assign_set_rhs2 (p, op2);
367 /* Build a new GIMPLE_ASSIGN tuple and append it to the end of *SEQ_P.
369 DST/SRC are the destination and source respectively. You can pass
370 ungimplified trees in DST or SRC, in which case they will be
371 converted to a gimple operand if necessary.
373 This function returns the newly created GIMPLE_ASSIGN tuple. */
376 gimplify_assign (tree dst, tree src, gimple_seq *seq_p)
378 tree t = build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, src);
379 gimplify_and_add (t, seq_p);
381 return gimple_seq_last_stmt (*seq_p);
385 /* Build a GIMPLE_COND statement.
387 PRED is the condition used to compare LHS and the RHS.
388 T_LABEL is the label to jump to if the condition is true.
389 F_LABEL is the label to jump to otherwise. */
392 gimple_build_cond (enum tree_code pred_code, tree lhs, tree rhs,
393 tree t_label, tree f_label)
397 gcc_assert (TREE_CODE_CLASS (pred_code) == tcc_comparison);
398 p = gimple_build_with_ops (GIMPLE_COND, pred_code, 4);
399 gimple_cond_set_lhs (p, lhs);
400 gimple_cond_set_rhs (p, rhs);
401 gimple_cond_set_true_label (p, t_label);
402 gimple_cond_set_false_label (p, f_label);
407 /* Extract operands for a GIMPLE_COND statement out of COND_EXPR tree COND. */
410 gimple_cond_get_ops_from_tree (tree cond, enum tree_code *code_p,
411 tree *lhs_p, tree *rhs_p)
413 location_t loc = EXPR_LOCATION (cond);
414 gcc_assert (TREE_CODE_CLASS (TREE_CODE (cond)) == tcc_comparison
415 || TREE_CODE (cond) == TRUTH_NOT_EXPR
416 || is_gimple_min_invariant (cond)
417 || SSA_VAR_P (cond));
419 extract_ops_from_tree (cond, code_p, lhs_p, rhs_p);
421 /* Canonicalize conditionals of the form 'if (!VAL)'. */
422 if (*code_p == TRUTH_NOT_EXPR)
425 gcc_assert (*lhs_p && *rhs_p == NULL_TREE);
426 *rhs_p = fold_convert_loc (loc, TREE_TYPE (*lhs_p), integer_zero_node);
428 /* Canonicalize conditionals of the form 'if (VAL)' */
429 else if (TREE_CODE_CLASS (*code_p) != tcc_comparison)
432 gcc_assert (*lhs_p && *rhs_p == NULL_TREE);
433 *rhs_p = fold_convert_loc (loc, TREE_TYPE (*lhs_p), integer_zero_node);
438 /* Build a GIMPLE_COND statement from the conditional expression tree
439 COND. T_LABEL and F_LABEL are as in gimple_build_cond. */
442 gimple_build_cond_from_tree (tree cond, tree t_label, tree f_label)
447 gimple_cond_get_ops_from_tree (cond, &code, &lhs, &rhs);
448 return gimple_build_cond (code, lhs, rhs, t_label, f_label);
451 /* Set code, lhs, and rhs of a GIMPLE_COND from a suitable
452 boolean expression tree COND. */
455 gimple_cond_set_condition_from_tree (gimple stmt, tree cond)
460 gimple_cond_get_ops_from_tree (cond, &code, &lhs, &rhs);
461 gimple_cond_set_condition (stmt, code, lhs, rhs);
464 /* Build a GIMPLE_LABEL statement for LABEL. */
467 gimple_build_label (tree label)
469 gimple p = gimple_build_with_ops (GIMPLE_LABEL, ERROR_MARK, 1);
470 gimple_label_set_label (p, label);
474 /* Build a GIMPLE_GOTO statement to label DEST. */
477 gimple_build_goto (tree dest)
479 gimple p = gimple_build_with_ops (GIMPLE_GOTO, ERROR_MARK, 1);
480 gimple_goto_set_dest (p, dest);
485 /* Build a GIMPLE_NOP statement. */
488 gimple_build_nop (void)
490 return gimple_alloc (GIMPLE_NOP, 0);
494 /* Build a GIMPLE_BIND statement.
495 VARS are the variables in BODY.
496 BLOCK is the containing block. */
499 gimple_build_bind (tree vars, gimple_seq body, tree block)
501 gimple p = gimple_alloc (GIMPLE_BIND, 0);
502 gimple_bind_set_vars (p, vars);
504 gimple_bind_set_body (p, body);
506 gimple_bind_set_block (p, block);
510 /* Helper function to set the simple fields of a asm stmt.
512 STRING is a pointer to a string that is the asm blocks assembly code.
513 NINPUT is the number of register inputs.
514 NOUTPUT is the number of register outputs.
515 NCLOBBERS is the number of clobbered registers.
519 gimple_build_asm_1 (const char *string, unsigned ninputs, unsigned noutputs,
520 unsigned nclobbers, unsigned nlabels)
523 int size = strlen (string);
525 /* ASMs with labels cannot have outputs. This should have been
526 enforced by the front end. */
527 gcc_assert (nlabels == 0 || noutputs == 0);
529 p = gimple_build_with_ops (GIMPLE_ASM, ERROR_MARK,
530 ninputs + noutputs + nclobbers + nlabels);
532 p->gimple_asm.ni = ninputs;
533 p->gimple_asm.no = noutputs;
534 p->gimple_asm.nc = nclobbers;
535 p->gimple_asm.nl = nlabels;
536 p->gimple_asm.string = ggc_alloc_string (string, size);
538 #ifdef GATHER_STATISTICS
539 gimple_alloc_sizes[(int) gimple_alloc_kind (GIMPLE_ASM)] += size;
545 /* Build a GIMPLE_ASM statement.
547 STRING is the assembly code.
548 NINPUT is the number of register inputs.
549 NOUTPUT is the number of register outputs.
550 NCLOBBERS is the number of clobbered registers.
551 INPUTS is a vector of the input register parameters.
552 OUTPUTS is a vector of the output register parameters.
553 CLOBBERS is a vector of the clobbered register parameters.
554 LABELS is a vector of destination labels. */
557 gimple_build_asm_vec (const char *string, VEC(tree,gc)* inputs,
558 VEC(tree,gc)* outputs, VEC(tree,gc)* clobbers,
559 VEC(tree,gc)* labels)
564 p = gimple_build_asm_1 (string,
565 VEC_length (tree, inputs),
566 VEC_length (tree, outputs),
567 VEC_length (tree, clobbers),
568 VEC_length (tree, labels));
570 for (i = 0; i < VEC_length (tree, inputs); i++)
571 gimple_asm_set_input_op (p, i, VEC_index (tree, inputs, i));
573 for (i = 0; i < VEC_length (tree, outputs); i++)
574 gimple_asm_set_output_op (p, i, VEC_index (tree, outputs, i));
576 for (i = 0; i < VEC_length (tree, clobbers); i++)
577 gimple_asm_set_clobber_op (p, i, VEC_index (tree, clobbers, i));
579 for (i = 0; i < VEC_length (tree, labels); i++)
580 gimple_asm_set_label_op (p, i, VEC_index (tree, labels, i));
585 /* Build a GIMPLE_CATCH statement.
587 TYPES are the catch types.
588 HANDLER is the exception handler. */
591 gimple_build_catch (tree types, gimple_seq handler)
593 gimple p = gimple_alloc (GIMPLE_CATCH, 0);
594 gimple_catch_set_types (p, types);
596 gimple_catch_set_handler (p, handler);
601 /* Build a GIMPLE_EH_FILTER statement.
603 TYPES are the filter's types.
604 FAILURE is the filter's failure action. */
607 gimple_build_eh_filter (tree types, gimple_seq failure)
609 gimple p = gimple_alloc (GIMPLE_EH_FILTER, 0);
610 gimple_eh_filter_set_types (p, types);
612 gimple_eh_filter_set_failure (p, failure);
617 /* Build a GIMPLE_EH_MUST_NOT_THROW statement. */
620 gimple_build_eh_must_not_throw (tree decl)
622 gimple p = gimple_alloc (GIMPLE_EH_MUST_NOT_THROW, 1);
624 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
625 gcc_assert (flags_from_decl_or_type (decl) & ECF_NORETURN);
626 gimple_eh_must_not_throw_set_fndecl (p, decl);
631 /* Build a GIMPLE_TRY statement.
633 EVAL is the expression to evaluate.
634 CLEANUP is the cleanup expression.
635 KIND is either GIMPLE_TRY_CATCH or GIMPLE_TRY_FINALLY depending on
636 whether this is a try/catch or a try/finally respectively. */
639 gimple_build_try (gimple_seq eval, gimple_seq cleanup,
640 enum gimple_try_flags kind)
644 gcc_assert (kind == GIMPLE_TRY_CATCH || kind == GIMPLE_TRY_FINALLY);
645 p = gimple_alloc (GIMPLE_TRY, 0);
646 gimple_set_subcode (p, kind);
648 gimple_try_set_eval (p, eval);
650 gimple_try_set_cleanup (p, cleanup);
655 /* Construct a GIMPLE_WITH_CLEANUP_EXPR statement.
657 CLEANUP is the cleanup expression. */
660 gimple_build_wce (gimple_seq cleanup)
662 gimple p = gimple_alloc (GIMPLE_WITH_CLEANUP_EXPR, 0);
664 gimple_wce_set_cleanup (p, cleanup);
670 /* Build a GIMPLE_RESX statement. */
673 gimple_build_resx (int region)
675 gimple p = gimple_build_with_ops (GIMPLE_RESX, ERROR_MARK, 0);
676 p->gimple_eh_ctrl.region = region;
681 /* The helper for constructing a gimple switch statement.
682 INDEX is the switch's index.
683 NLABELS is the number of labels in the switch excluding the default.
684 DEFAULT_LABEL is the default label for the switch statement. */
687 gimple_build_switch_nlabels (unsigned nlabels, tree index, tree default_label)
689 /* nlabels + 1 default label + 1 index. */
690 gimple p = gimple_build_with_ops (GIMPLE_SWITCH, ERROR_MARK,
691 1 + (default_label != NULL) + nlabels);
692 gimple_switch_set_index (p, index);
694 gimple_switch_set_default_label (p, default_label);
699 /* Build a GIMPLE_SWITCH statement.
701 INDEX is the switch's index.
702 NLABELS is the number of labels in the switch excluding the DEFAULT_LABEL.
703 ... are the labels excluding the default. */
706 gimple_build_switch (unsigned nlabels, tree index, tree default_label, ...)
710 gimple p = gimple_build_switch_nlabels (nlabels, index, default_label);
712 /* Store the rest of the labels. */
713 va_start (al, default_label);
714 offset = (default_label != NULL);
715 for (i = 0; i < nlabels; i++)
716 gimple_switch_set_label (p, i + offset, va_arg (al, tree));
723 /* Build a GIMPLE_SWITCH statement.
725 INDEX is the switch's index.
726 DEFAULT_LABEL is the default label
727 ARGS is a vector of labels excluding the default. */
730 gimple_build_switch_vec (tree index, tree default_label, VEC(tree, heap) *args)
732 unsigned i, offset, nlabels = VEC_length (tree, args);
733 gimple p = gimple_build_switch_nlabels (nlabels, index, default_label);
735 /* Copy the labels from the vector to the switch statement. */
736 offset = (default_label != NULL);
737 for (i = 0; i < nlabels; i++)
738 gimple_switch_set_label (p, i + offset, VEC_index (tree, args, i));
743 /* Build a GIMPLE_EH_DISPATCH statement. */
746 gimple_build_eh_dispatch (int region)
748 gimple p = gimple_build_with_ops (GIMPLE_EH_DISPATCH, ERROR_MARK, 0);
749 p->gimple_eh_ctrl.region = region;
753 /* Build a new GIMPLE_DEBUG_BIND statement.
755 VAR is bound to VALUE; block and location are taken from STMT. */
758 gimple_build_debug_bind_stat (tree var, tree value, gimple stmt MEM_STAT_DECL)
760 gimple p = gimple_build_with_ops_stat (GIMPLE_DEBUG,
761 (unsigned)GIMPLE_DEBUG_BIND, 2
764 gimple_debug_bind_set_var (p, var);
765 gimple_debug_bind_set_value (p, value);
768 gimple_set_block (p, gimple_block (stmt));
769 gimple_set_location (p, gimple_location (stmt));
776 /* Build a GIMPLE_OMP_CRITICAL statement.
778 BODY is the sequence of statements for which only one thread can execute.
779 NAME is optional identifier for this critical block. */
782 gimple_build_omp_critical (gimple_seq body, tree name)
784 gimple p = gimple_alloc (GIMPLE_OMP_CRITICAL, 0);
785 gimple_omp_critical_set_name (p, name);
787 gimple_omp_set_body (p, body);
792 /* Build a GIMPLE_OMP_FOR statement.
794 BODY is sequence of statements inside the for loop.
795 CLAUSES, are any of the OMP loop construct's clauses: private, firstprivate,
796 lastprivate, reductions, ordered, schedule, and nowait.
797 COLLAPSE is the collapse count.
798 PRE_BODY is the sequence of statements that are loop invariant. */
801 gimple_build_omp_for (gimple_seq body, tree clauses, size_t collapse,
804 gimple p = gimple_alloc (GIMPLE_OMP_FOR, 0);
806 gimple_omp_set_body (p, body);
807 gimple_omp_for_set_clauses (p, clauses);
808 p->gimple_omp_for.collapse = collapse;
809 p->gimple_omp_for.iter = GGC_CNEWVEC (struct gimple_omp_for_iter, collapse);
811 gimple_omp_for_set_pre_body (p, pre_body);
817 /* Build a GIMPLE_OMP_PARALLEL statement.
819 BODY is sequence of statements which are executed in parallel.
820 CLAUSES, are the OMP parallel construct's clauses.
821 CHILD_FN is the function created for the parallel threads to execute.
822 DATA_ARG are the shared data argument(s). */
825 gimple_build_omp_parallel (gimple_seq body, tree clauses, tree child_fn,
828 gimple p = gimple_alloc (GIMPLE_OMP_PARALLEL, 0);
830 gimple_omp_set_body (p, body);
831 gimple_omp_parallel_set_clauses (p, clauses);
832 gimple_omp_parallel_set_child_fn (p, child_fn);
833 gimple_omp_parallel_set_data_arg (p, data_arg);
839 /* Build a GIMPLE_OMP_TASK statement.
841 BODY is sequence of statements which are executed by the explicit task.
842 CLAUSES, are the OMP parallel construct's clauses.
843 CHILD_FN is the function created for the parallel threads to execute.
844 DATA_ARG are the shared data argument(s).
845 COPY_FN is the optional function for firstprivate initialization.
846 ARG_SIZE and ARG_ALIGN are size and alignment of the data block. */
849 gimple_build_omp_task (gimple_seq body, tree clauses, tree child_fn,
850 tree data_arg, tree copy_fn, tree arg_size,
853 gimple p = gimple_alloc (GIMPLE_OMP_TASK, 0);
855 gimple_omp_set_body (p, body);
856 gimple_omp_task_set_clauses (p, clauses);
857 gimple_omp_task_set_child_fn (p, child_fn);
858 gimple_omp_task_set_data_arg (p, data_arg);
859 gimple_omp_task_set_copy_fn (p, copy_fn);
860 gimple_omp_task_set_arg_size (p, arg_size);
861 gimple_omp_task_set_arg_align (p, arg_align);
867 /* Build a GIMPLE_OMP_SECTION statement for a sections statement.
869 BODY is the sequence of statements in the section. */
872 gimple_build_omp_section (gimple_seq body)
874 gimple p = gimple_alloc (GIMPLE_OMP_SECTION, 0);
876 gimple_omp_set_body (p, body);
882 /* Build a GIMPLE_OMP_MASTER statement.
884 BODY is the sequence of statements to be executed by just the master. */
887 gimple_build_omp_master (gimple_seq body)
889 gimple p = gimple_alloc (GIMPLE_OMP_MASTER, 0);
891 gimple_omp_set_body (p, body);
897 /* Build a GIMPLE_OMP_CONTINUE statement.
899 CONTROL_DEF is the definition of the control variable.
900 CONTROL_USE is the use of the control variable. */
903 gimple_build_omp_continue (tree control_def, tree control_use)
905 gimple p = gimple_alloc (GIMPLE_OMP_CONTINUE, 0);
906 gimple_omp_continue_set_control_def (p, control_def);
907 gimple_omp_continue_set_control_use (p, control_use);
911 /* Build a GIMPLE_OMP_ORDERED statement.
913 BODY is the sequence of statements inside a loop that will executed in
917 gimple_build_omp_ordered (gimple_seq body)
919 gimple p = gimple_alloc (GIMPLE_OMP_ORDERED, 0);
921 gimple_omp_set_body (p, body);
927 /* Build a GIMPLE_OMP_RETURN statement.
928 WAIT_P is true if this is a non-waiting return. */
931 gimple_build_omp_return (bool wait_p)
933 gimple p = gimple_alloc (GIMPLE_OMP_RETURN, 0);
935 gimple_omp_return_set_nowait (p);
941 /* Build a GIMPLE_OMP_SECTIONS statement.
943 BODY is a sequence of section statements.
944 CLAUSES are any of the OMP sections contsruct's clauses: private,
945 firstprivate, lastprivate, reduction, and nowait. */
948 gimple_build_omp_sections (gimple_seq body, tree clauses)
950 gimple p = gimple_alloc (GIMPLE_OMP_SECTIONS, 0);
952 gimple_omp_set_body (p, body);
953 gimple_omp_sections_set_clauses (p, clauses);
959 /* Build a GIMPLE_OMP_SECTIONS_SWITCH. */
962 gimple_build_omp_sections_switch (void)
964 return gimple_alloc (GIMPLE_OMP_SECTIONS_SWITCH, 0);
968 /* Build a GIMPLE_OMP_SINGLE statement.
970 BODY is the sequence of statements that will be executed once.
971 CLAUSES are any of the OMP single construct's clauses: private, firstprivate,
972 copyprivate, nowait. */
975 gimple_build_omp_single (gimple_seq body, tree clauses)
977 gimple p = gimple_alloc (GIMPLE_OMP_SINGLE, 0);
979 gimple_omp_set_body (p, body);
980 gimple_omp_single_set_clauses (p, clauses);
986 /* Build a GIMPLE_OMP_ATOMIC_LOAD statement. */
989 gimple_build_omp_atomic_load (tree lhs, tree rhs)
991 gimple p = gimple_alloc (GIMPLE_OMP_ATOMIC_LOAD, 0);
992 gimple_omp_atomic_load_set_lhs (p, lhs);
993 gimple_omp_atomic_load_set_rhs (p, rhs);
997 /* Build a GIMPLE_OMP_ATOMIC_STORE statement.
999 VAL is the value we are storing. */
1002 gimple_build_omp_atomic_store (tree val)
1004 gimple p = gimple_alloc (GIMPLE_OMP_ATOMIC_STORE, 0);
1005 gimple_omp_atomic_store_set_val (p, val);
1009 /* Build a GIMPLE_PREDICT statement. PREDICT is one of the predictors from
1010 predict.def, OUTCOME is NOT_TAKEN or TAKEN. */
1013 gimple_build_predict (enum br_predictor predictor, enum prediction outcome)
1015 gimple p = gimple_alloc (GIMPLE_PREDICT, 0);
1016 /* Ensure all the predictors fit into the lower bits of the subcode. */
1017 gcc_assert ((int) END_PREDICTORS <= GF_PREDICT_TAKEN);
1018 gimple_predict_set_predictor (p, predictor);
1019 gimple_predict_set_outcome (p, outcome);
1023 #if defined ENABLE_GIMPLE_CHECKING
1024 /* Complain of a gimple type mismatch and die. */
1027 gimple_check_failed (const_gimple gs, const char *file, int line,
1028 const char *function, enum gimple_code code,
1029 enum tree_code subcode)
1031 internal_error ("gimple check: expected %s(%s), have %s(%s) in %s, at %s:%d",
1032 gimple_code_name[code],
1033 tree_code_name[subcode],
1034 gimple_code_name[gimple_code (gs)],
1035 gs->gsbase.subcode > 0
1036 ? tree_code_name[gs->gsbase.subcode]
1038 function, trim_filename (file), line);
1040 #endif /* ENABLE_GIMPLE_CHECKING */
1043 /* Allocate a new GIMPLE sequence in GC memory and return it. If
1044 there are free sequences in GIMPLE_SEQ_CACHE return one of those
1048 gimple_seq_alloc (void)
1050 gimple_seq seq = gimple_seq_cache;
1053 gimple_seq_cache = gimple_seq_cache->next_free;
1054 gcc_assert (gimple_seq_cache != seq);
1055 memset (seq, 0, sizeof (*seq));
1059 seq = (gimple_seq) ggc_alloc_cleared (sizeof (*seq));
1060 #ifdef GATHER_STATISTICS
1061 gimple_alloc_counts[(int) gimple_alloc_kind_seq]++;
1062 gimple_alloc_sizes[(int) gimple_alloc_kind_seq] += sizeof (*seq);
1069 /* Return SEQ to the free pool of GIMPLE sequences. */
1072 gimple_seq_free (gimple_seq seq)
1077 gcc_assert (gimple_seq_first (seq) == NULL);
1078 gcc_assert (gimple_seq_last (seq) == NULL);
1080 /* If this triggers, it's a sign that the same list is being freed
1082 gcc_assert (seq != gimple_seq_cache || gimple_seq_cache == NULL);
1084 /* Add SEQ to the pool of free sequences. */
1085 seq->next_free = gimple_seq_cache;
1086 gimple_seq_cache = seq;
1090 /* Link gimple statement GS to the end of the sequence *SEQ_P. If
1091 *SEQ_P is NULL, a new sequence is allocated. */
1094 gimple_seq_add_stmt (gimple_seq *seq_p, gimple gs)
1096 gimple_stmt_iterator si;
1102 *seq_p = gimple_seq_alloc ();
1104 si = gsi_last (*seq_p);
1105 gsi_insert_after (&si, gs, GSI_NEW_STMT);
1109 /* Append sequence SRC to the end of sequence *DST_P. If *DST_P is
1110 NULL, a new sequence is allocated. */
1113 gimple_seq_add_seq (gimple_seq *dst_p, gimple_seq src)
1115 gimple_stmt_iterator si;
1121 *dst_p = gimple_seq_alloc ();
1123 si = gsi_last (*dst_p);
1124 gsi_insert_seq_after (&si, src, GSI_NEW_STMT);
1128 /* Helper function of empty_body_p. Return true if STMT is an empty
1132 empty_stmt_p (gimple stmt)
1134 if (gimple_code (stmt) == GIMPLE_NOP)
1136 if (gimple_code (stmt) == GIMPLE_BIND)
1137 return empty_body_p (gimple_bind_body (stmt));
1142 /* Return true if BODY contains nothing but empty statements. */
1145 empty_body_p (gimple_seq body)
1147 gimple_stmt_iterator i;
1149 if (gimple_seq_empty_p (body))
1151 for (i = gsi_start (body); !gsi_end_p (i); gsi_next (&i))
1152 if (!empty_stmt_p (gsi_stmt (i))
1153 && !is_gimple_debug (gsi_stmt (i)))
1160 /* Perform a deep copy of sequence SRC and return the result. */
1163 gimple_seq_copy (gimple_seq src)
1165 gimple_stmt_iterator gsi;
1166 gimple_seq new_seq = gimple_seq_alloc ();
1169 for (gsi = gsi_start (src); !gsi_end_p (gsi); gsi_next (&gsi))
1171 stmt = gimple_copy (gsi_stmt (gsi));
1172 gimple_seq_add_stmt (&new_seq, stmt);
1179 /* Walk all the statements in the sequence SEQ calling walk_gimple_stmt
1180 on each one. WI is as in walk_gimple_stmt.
1182 If walk_gimple_stmt returns non-NULL, the walk is stopped, the
1183 value is stored in WI->CALLBACK_RESULT and the statement that
1184 produced the value is returned.
1186 Otherwise, all the statements are walked and NULL returned. */
1189 walk_gimple_seq (gimple_seq seq, walk_stmt_fn callback_stmt,
1190 walk_tree_fn callback_op, struct walk_stmt_info *wi)
1192 gimple_stmt_iterator gsi;
1194 for (gsi = gsi_start (seq); !gsi_end_p (gsi); gsi_next (&gsi))
1196 tree ret = walk_gimple_stmt (&gsi, callback_stmt, callback_op, wi);
1199 /* If CALLBACK_STMT or CALLBACK_OP return a value, WI must exist
1202 wi->callback_result = ret;
1203 return gsi_stmt (gsi);
1208 wi->callback_result = NULL_TREE;
1214 /* Helper function for walk_gimple_stmt. Walk operands of a GIMPLE_ASM. */
1217 walk_gimple_asm (gimple stmt, walk_tree_fn callback_op,
1218 struct walk_stmt_info *wi)
1222 const char **oconstraints;
1224 const char *constraint;
1225 bool allows_mem, allows_reg, is_inout;
1227 noutputs = gimple_asm_noutputs (stmt);
1228 oconstraints = (const char **) alloca ((noutputs) * sizeof (const char *));
1233 for (i = 0; i < noutputs; i++)
1235 op = gimple_asm_output_op (stmt, i);
1236 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (op)));
1237 oconstraints[i] = constraint;
1238 parse_output_constraint (&constraint, i, 0, 0, &allows_mem, &allows_reg,
1241 wi->val_only = (allows_reg || !allows_mem);
1242 ret = walk_tree (&TREE_VALUE (op), callback_op, wi, NULL);
1247 n = gimple_asm_ninputs (stmt);
1248 for (i = 0; i < n; i++)
1250 op = gimple_asm_input_op (stmt, i);
1251 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (op)));
1252 parse_input_constraint (&constraint, 0, 0, noutputs, 0,
1253 oconstraints, &allows_mem, &allows_reg);
1256 wi->val_only = (allows_reg || !allows_mem);
1257 /* Although input "m" is not really a LHS, we need a lvalue. */
1258 wi->is_lhs = !wi->val_only;
1260 ret = walk_tree (&TREE_VALUE (op), callback_op, wi, NULL);
1268 wi->val_only = true;
1271 n = gimple_asm_nlabels (stmt);
1272 for (i = 0; i < n; i++)
1274 op = gimple_asm_label_op (stmt, i);
1275 ret = walk_tree (&TREE_VALUE (op), callback_op, wi, NULL);
1284 /* Helper function of WALK_GIMPLE_STMT. Walk every tree operand in
1285 STMT. CALLBACK_OP and WI are as in WALK_GIMPLE_STMT.
1287 CALLBACK_OP is called on each operand of STMT via walk_tree.
1288 Additional parameters to walk_tree must be stored in WI. For each operand
1289 OP, walk_tree is called as:
1291 walk_tree (&OP, CALLBACK_OP, WI, WI->PSET)
1293 If CALLBACK_OP returns non-NULL for an operand, the remaining
1294 operands are not scanned.
1296 The return value is that returned by the last call to walk_tree, or
1297 NULL_TREE if no CALLBACK_OP is specified. */
1300 walk_gimple_op (gimple stmt, walk_tree_fn callback_op,
1301 struct walk_stmt_info *wi)
1303 struct pointer_set_t *pset = (wi) ? wi->pset : NULL;
1305 tree ret = NULL_TREE;
1307 switch (gimple_code (stmt))
1310 /* Walk the RHS operands. A formal temporary LHS may use a
1311 COMPONENT_REF RHS. */
1313 wi->val_only = !is_gimple_reg (gimple_assign_lhs (stmt))
1314 || !gimple_assign_single_p (stmt);
1316 for (i = 1; i < gimple_num_ops (stmt); i++)
1318 ret = walk_tree (gimple_op_ptr (stmt, i), callback_op, wi,
1324 /* Walk the LHS. If the RHS is appropriate for a memory, we
1325 may use a COMPONENT_REF on the LHS. */
1328 /* If the RHS has more than 1 operand, it is not appropriate
1330 wi->val_only = !is_gimple_mem_rhs (gimple_assign_rhs1 (stmt))
1331 || !gimple_assign_single_p (stmt);
1335 ret = walk_tree (gimple_op_ptr (stmt, 0), callback_op, wi, pset);
1341 wi->val_only = true;
1350 ret = walk_tree (gimple_call_chain_ptr (stmt), callback_op, wi, pset);
1354 ret = walk_tree (gimple_call_fn_ptr (stmt), callback_op, wi, pset);
1358 for (i = 0; i < gimple_call_num_args (stmt); i++)
1360 ret = walk_tree (gimple_call_arg_ptr (stmt, i), callback_op, wi,
1369 ret = walk_tree (gimple_call_lhs_ptr (stmt), callback_op, wi, pset);
1378 ret = walk_tree (gimple_catch_types_ptr (stmt), callback_op, wi,
1384 case GIMPLE_EH_FILTER:
1385 ret = walk_tree (gimple_eh_filter_types_ptr (stmt), callback_op, wi,
1392 ret = walk_gimple_asm (stmt, callback_op, wi);
1397 case GIMPLE_OMP_CONTINUE:
1398 ret = walk_tree (gimple_omp_continue_control_def_ptr (stmt),
1399 callback_op, wi, pset);
1403 ret = walk_tree (gimple_omp_continue_control_use_ptr (stmt),
1404 callback_op, wi, pset);
1409 case GIMPLE_OMP_CRITICAL:
1410 ret = walk_tree (gimple_omp_critical_name_ptr (stmt), callback_op, wi,
1416 case GIMPLE_OMP_FOR:
1417 ret = walk_tree (gimple_omp_for_clauses_ptr (stmt), callback_op, wi,
1421 for (i = 0; i < gimple_omp_for_collapse (stmt); i++)
1423 ret = walk_tree (gimple_omp_for_index_ptr (stmt, i), callback_op,
1427 ret = walk_tree (gimple_omp_for_initial_ptr (stmt, i), callback_op,
1431 ret = walk_tree (gimple_omp_for_final_ptr (stmt, i), callback_op,
1435 ret = walk_tree (gimple_omp_for_incr_ptr (stmt, i), callback_op,
1442 case GIMPLE_OMP_PARALLEL:
1443 ret = walk_tree (gimple_omp_parallel_clauses_ptr (stmt), callback_op,
1447 ret = walk_tree (gimple_omp_parallel_child_fn_ptr (stmt), callback_op,
1451 ret = walk_tree (gimple_omp_parallel_data_arg_ptr (stmt), callback_op,
1457 case GIMPLE_OMP_TASK:
1458 ret = walk_tree (gimple_omp_task_clauses_ptr (stmt), callback_op,
1462 ret = walk_tree (gimple_omp_task_child_fn_ptr (stmt), callback_op,
1466 ret = walk_tree (gimple_omp_task_data_arg_ptr (stmt), callback_op,
1470 ret = walk_tree (gimple_omp_task_copy_fn_ptr (stmt), callback_op,
1474 ret = walk_tree (gimple_omp_task_arg_size_ptr (stmt), callback_op,
1478 ret = walk_tree (gimple_omp_task_arg_align_ptr (stmt), callback_op,
1484 case GIMPLE_OMP_SECTIONS:
1485 ret = walk_tree (gimple_omp_sections_clauses_ptr (stmt), callback_op,
1490 ret = walk_tree (gimple_omp_sections_control_ptr (stmt), callback_op,
1497 case GIMPLE_OMP_SINGLE:
1498 ret = walk_tree (gimple_omp_single_clauses_ptr (stmt), callback_op, wi,
1504 case GIMPLE_OMP_ATOMIC_LOAD:
1505 ret = walk_tree (gimple_omp_atomic_load_lhs_ptr (stmt), callback_op, wi,
1510 ret = walk_tree (gimple_omp_atomic_load_rhs_ptr (stmt), callback_op, wi,
1516 case GIMPLE_OMP_ATOMIC_STORE:
1517 ret = walk_tree (gimple_omp_atomic_store_val_ptr (stmt), callback_op,
1523 /* Tuples that do not have operands. */
1526 case GIMPLE_OMP_RETURN:
1527 case GIMPLE_PREDICT:
1532 enum gimple_statement_structure_enum gss;
1533 gss = gimple_statement_structure (stmt);
1534 if (gss == GSS_WITH_OPS || gss == GSS_WITH_MEM_OPS)
1535 for (i = 0; i < gimple_num_ops (stmt); i++)
1537 ret = walk_tree (gimple_op_ptr (stmt, i), callback_op, wi, pset);
1549 /* Walk the current statement in GSI (optionally using traversal state
1550 stored in WI). If WI is NULL, no state is kept during traversal.
1551 The callback CALLBACK_STMT is called. If CALLBACK_STMT indicates
1552 that it has handled all the operands of the statement, its return
1553 value is returned. Otherwise, the return value from CALLBACK_STMT
1554 is discarded and its operands are scanned.
1556 If CALLBACK_STMT is NULL or it didn't handle the operands,
1557 CALLBACK_OP is called on each operand of the statement via
1558 walk_gimple_op. If walk_gimple_op returns non-NULL for any
1559 operand, the remaining operands are not scanned. In this case, the
1560 return value from CALLBACK_OP is returned.
1562 In any other case, NULL_TREE is returned. */
1565 walk_gimple_stmt (gimple_stmt_iterator *gsi, walk_stmt_fn callback_stmt,
1566 walk_tree_fn callback_op, struct walk_stmt_info *wi)
1570 gimple stmt = gsi_stmt (*gsi);
1575 if (wi && wi->want_locations && gimple_has_location (stmt))
1576 input_location = gimple_location (stmt);
1580 /* Invoke the statement callback. Return if the callback handled
1581 all of STMT operands by itself. */
1584 bool handled_ops = false;
1585 tree_ret = callback_stmt (gsi, &handled_ops, wi);
1589 /* If CALLBACK_STMT did not handle operands, it should not have
1590 a value to return. */
1591 gcc_assert (tree_ret == NULL);
1593 /* Re-read stmt in case the callback changed it. */
1594 stmt = gsi_stmt (*gsi);
1597 /* If CALLBACK_OP is defined, invoke it on every operand of STMT. */
1600 tree_ret = walk_gimple_op (stmt, callback_op, wi);
1605 /* If STMT can have statements inside (e.g. GIMPLE_BIND), walk them. */
1606 switch (gimple_code (stmt))
1609 ret = walk_gimple_seq (gimple_bind_body (stmt), callback_stmt,
1612 return wi->callback_result;
1616 ret = walk_gimple_seq (gimple_catch_handler (stmt), callback_stmt,
1619 return wi->callback_result;
1622 case GIMPLE_EH_FILTER:
1623 ret = walk_gimple_seq (gimple_eh_filter_failure (stmt), callback_stmt,
1626 return wi->callback_result;
1630 ret = walk_gimple_seq (gimple_try_eval (stmt), callback_stmt, callback_op,
1633 return wi->callback_result;
1635 ret = walk_gimple_seq (gimple_try_cleanup (stmt), callback_stmt,
1638 return wi->callback_result;
1641 case GIMPLE_OMP_FOR:
1642 ret = walk_gimple_seq (gimple_omp_for_pre_body (stmt), callback_stmt,
1645 return wi->callback_result;
1648 case GIMPLE_OMP_CRITICAL:
1649 case GIMPLE_OMP_MASTER:
1650 case GIMPLE_OMP_ORDERED:
1651 case GIMPLE_OMP_SECTION:
1652 case GIMPLE_OMP_PARALLEL:
1653 case GIMPLE_OMP_TASK:
1654 case GIMPLE_OMP_SECTIONS:
1655 case GIMPLE_OMP_SINGLE:
1656 ret = walk_gimple_seq (gimple_omp_body (stmt), callback_stmt, callback_op,
1659 return wi->callback_result;
1662 case GIMPLE_WITH_CLEANUP_EXPR:
1663 ret = walk_gimple_seq (gimple_wce_cleanup (stmt), callback_stmt,
1666 return wi->callback_result;
1670 gcc_assert (!gimple_has_substatements (stmt));
1678 /* Set sequence SEQ to be the GIMPLE body for function FN. */
1681 gimple_set_body (tree fndecl, gimple_seq seq)
1683 struct function *fn = DECL_STRUCT_FUNCTION (fndecl);
1686 /* If FNDECL still does not have a function structure associated
1687 with it, then it does not make sense for it to receive a
1689 gcc_assert (seq == NULL);
1692 fn->gimple_body = seq;
1696 /* Return the body of GIMPLE statements for function FN. */
1699 gimple_body (tree fndecl)
1701 struct function *fn = DECL_STRUCT_FUNCTION (fndecl);
1702 return fn ? fn->gimple_body : NULL;
1705 /* Return true when FNDECL has Gimple body either in unlowered
1708 gimple_has_body_p (tree fndecl)
1710 struct function *fn = DECL_STRUCT_FUNCTION (fndecl);
1711 return (gimple_body (fndecl) || (fn && fn->cfg));
1714 /* Detect flags from a GIMPLE_CALL. This is just like
1715 call_expr_flags, but for gimple tuples. */
1718 gimple_call_flags (const_gimple stmt)
1721 tree decl = gimple_call_fndecl (stmt);
1725 flags = flags_from_decl_or_type (decl);
1728 t = TREE_TYPE (gimple_call_fn (stmt));
1729 if (t && TREE_CODE (t) == POINTER_TYPE)
1730 flags = flags_from_decl_or_type (TREE_TYPE (t));
1739 /* Return true if GS is a copy assignment. */
1742 gimple_assign_copy_p (gimple gs)
1744 return gimple_code (gs) == GIMPLE_ASSIGN
1745 && get_gimple_rhs_class (gimple_assign_rhs_code (gs))
1746 == GIMPLE_SINGLE_RHS
1747 && is_gimple_val (gimple_op (gs, 1));
1751 /* Return true if GS is a SSA_NAME copy assignment. */
1754 gimple_assign_ssa_name_copy_p (gimple gs)
1756 return (gimple_code (gs) == GIMPLE_ASSIGN
1757 && (get_gimple_rhs_class (gimple_assign_rhs_code (gs))
1758 == GIMPLE_SINGLE_RHS)
1759 && TREE_CODE (gimple_assign_lhs (gs)) == SSA_NAME
1760 && TREE_CODE (gimple_assign_rhs1 (gs)) == SSA_NAME);
1764 /* Return true if GS is an assignment with a singleton RHS, i.e.,
1765 there is no operator associated with the assignment itself.
1766 Unlike gimple_assign_copy_p, this predicate returns true for
1767 any RHS operand, including those that perform an operation
1768 and do not have the semantics of a copy, such as COND_EXPR. */
1771 gimple_assign_single_p (gimple gs)
1773 return (gimple_code (gs) == GIMPLE_ASSIGN
1774 && get_gimple_rhs_class (gimple_assign_rhs_code (gs))
1775 == GIMPLE_SINGLE_RHS);
1778 /* Return true if GS is an assignment with a unary RHS, but the
1779 operator has no effect on the assigned value. The logic is adapted
1780 from STRIP_NOPS. This predicate is intended to be used in tuplifying
1781 instances in which STRIP_NOPS was previously applied to the RHS of
1784 NOTE: In the use cases that led to the creation of this function
1785 and of gimple_assign_single_p, it is typical to test for either
1786 condition and to proceed in the same manner. In each case, the
1787 assigned value is represented by the single RHS operand of the
1788 assignment. I suspect there may be cases where gimple_assign_copy_p,
1789 gimple_assign_single_p, or equivalent logic is used where a similar
1790 treatment of unary NOPs is appropriate. */
1793 gimple_assign_unary_nop_p (gimple gs)
1795 return (gimple_code (gs) == GIMPLE_ASSIGN
1796 && (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (gs))
1797 || gimple_assign_rhs_code (gs) == NON_LVALUE_EXPR)
1798 && gimple_assign_rhs1 (gs) != error_mark_node
1799 && (TYPE_MODE (TREE_TYPE (gimple_assign_lhs (gs)))
1800 == TYPE_MODE (TREE_TYPE (gimple_assign_rhs1 (gs)))));
1803 /* Set BB to be the basic block holding G. */
1806 gimple_set_bb (gimple stmt, basic_block bb)
1808 stmt->gsbase.bb = bb;
1810 /* If the statement is a label, add the label to block-to-labels map
1811 so that we can speed up edge creation for GIMPLE_GOTOs. */
1812 if (cfun->cfg && gimple_code (stmt) == GIMPLE_LABEL)
1817 t = gimple_label_label (stmt);
1818 uid = LABEL_DECL_UID (t);
1821 unsigned old_len = VEC_length (basic_block, label_to_block_map);
1822 LABEL_DECL_UID (t) = uid = cfun->cfg->last_label_uid++;
1823 if (old_len <= (unsigned) uid)
1825 unsigned new_len = 3 * uid / 2 + 1;
1827 VEC_safe_grow_cleared (basic_block, gc, label_to_block_map,
1832 VEC_replace (basic_block, label_to_block_map, uid, bb);
1837 /* Fold the expression computed by STMT. If the expression can be
1838 folded, return the folded result, otherwise return NULL. STMT is
1842 gimple_fold (const_gimple stmt)
1844 location_t loc = gimple_location (stmt);
1845 switch (gimple_code (stmt))
1848 return fold_binary_loc (loc, gimple_cond_code (stmt),
1850 gimple_cond_lhs (stmt),
1851 gimple_cond_rhs (stmt));
1854 switch (get_gimple_rhs_class (gimple_assign_rhs_code (stmt)))
1856 case GIMPLE_UNARY_RHS:
1857 return fold_unary_loc (loc, gimple_assign_rhs_code (stmt),
1858 TREE_TYPE (gimple_assign_lhs (stmt)),
1859 gimple_assign_rhs1 (stmt));
1860 case GIMPLE_BINARY_RHS:
1861 return fold_binary_loc (loc, gimple_assign_rhs_code (stmt),
1862 TREE_TYPE (gimple_assign_lhs (stmt)),
1863 gimple_assign_rhs1 (stmt),
1864 gimple_assign_rhs2 (stmt));
1865 case GIMPLE_SINGLE_RHS:
1866 return fold (gimple_assign_rhs1 (stmt));
1872 return gimple_switch_index (stmt);
1885 /* Modify the RHS of the assignment pointed-to by GSI using the
1886 operands in the expression tree EXPR.
1888 NOTE: The statement pointed-to by GSI may be reallocated if it
1889 did not have enough operand slots.
1891 This function is useful to convert an existing tree expression into
1892 the flat representation used for the RHS of a GIMPLE assignment.
1893 It will reallocate memory as needed to expand or shrink the number
1894 of operand slots needed to represent EXPR.
1896 NOTE: If you find yourself building a tree and then calling this
1897 function, you are most certainly doing it the slow way. It is much
1898 better to build a new assignment or to use the function
1899 gimple_assign_set_rhs_with_ops, which does not require an
1900 expression tree to be built. */
1903 gimple_assign_set_rhs_from_tree (gimple_stmt_iterator *gsi, tree expr)
1905 enum tree_code subcode;
1908 extract_ops_from_tree (expr, &subcode, &op1, &op2);
1909 gimple_assign_set_rhs_with_ops (gsi, subcode, op1, op2);
1913 /* Set the RHS of assignment statement pointed-to by GSI to CODE with
1914 operands OP1 and OP2.
1916 NOTE: The statement pointed-to by GSI may be reallocated if it
1917 did not have enough operand slots. */
1920 gimple_assign_set_rhs_with_ops (gimple_stmt_iterator *gsi, enum tree_code code,
1923 unsigned new_rhs_ops = get_gimple_rhs_num_ops (code);
1924 gimple stmt = gsi_stmt (*gsi);
1926 /* If the new CODE needs more operands, allocate a new statement. */
1927 if (gimple_num_ops (stmt) < new_rhs_ops + 1)
1929 tree lhs = gimple_assign_lhs (stmt);
1930 gimple new_stmt = gimple_alloc (gimple_code (stmt), new_rhs_ops + 1);
1931 memcpy (new_stmt, stmt, gimple_size (gimple_code (stmt)));
1932 gsi_replace (gsi, new_stmt, true);
1935 /* The LHS needs to be reset as this also changes the SSA name
1937 gimple_assign_set_lhs (stmt, lhs);
1940 gimple_set_num_ops (stmt, new_rhs_ops + 1);
1941 gimple_set_subcode (stmt, code);
1942 gimple_assign_set_rhs1 (stmt, op1);
1943 if (new_rhs_ops > 1)
1944 gimple_assign_set_rhs2 (stmt, op2);
1948 /* Return the LHS of a statement that performs an assignment,
1949 either a GIMPLE_ASSIGN or a GIMPLE_CALL. Returns NULL_TREE
1950 for a call to a function that returns no value, or for a
1951 statement other than an assignment or a call. */
1954 gimple_get_lhs (const_gimple stmt)
1956 enum gimple_code code = gimple_code (stmt);
1958 if (code == GIMPLE_ASSIGN)
1959 return gimple_assign_lhs (stmt);
1960 else if (code == GIMPLE_CALL)
1961 return gimple_call_lhs (stmt);
1967 /* Set the LHS of a statement that performs an assignment,
1968 either a GIMPLE_ASSIGN or a GIMPLE_CALL. */
1971 gimple_set_lhs (gimple stmt, tree lhs)
1973 enum gimple_code code = gimple_code (stmt);
1975 if (code == GIMPLE_ASSIGN)
1976 gimple_assign_set_lhs (stmt, lhs);
1977 else if (code == GIMPLE_CALL)
1978 gimple_call_set_lhs (stmt, lhs);
1984 /* Return a deep copy of statement STMT. All the operands from STMT
1985 are reallocated and copied using unshare_expr. The DEF, USE, VDEF
1986 and VUSE operand arrays are set to empty in the new copy. */
1989 gimple_copy (gimple stmt)
1991 enum gimple_code code = gimple_code (stmt);
1992 unsigned num_ops = gimple_num_ops (stmt);
1993 gimple copy = gimple_alloc (code, num_ops);
1996 /* Shallow copy all the fields from STMT. */
1997 memcpy (copy, stmt, gimple_size (code));
1999 /* If STMT has sub-statements, deep-copy them as well. */
2000 if (gimple_has_substatements (stmt))
2005 switch (gimple_code (stmt))
2008 new_seq = gimple_seq_copy (gimple_bind_body (stmt));
2009 gimple_bind_set_body (copy, new_seq);
2010 gimple_bind_set_vars (copy, unshare_expr (gimple_bind_vars (stmt)));
2011 gimple_bind_set_block (copy, gimple_bind_block (stmt));
2015 new_seq = gimple_seq_copy (gimple_catch_handler (stmt));
2016 gimple_catch_set_handler (copy, new_seq);
2017 t = unshare_expr (gimple_catch_types (stmt));
2018 gimple_catch_set_types (copy, t);
2021 case GIMPLE_EH_FILTER:
2022 new_seq = gimple_seq_copy (gimple_eh_filter_failure (stmt));
2023 gimple_eh_filter_set_failure (copy, new_seq);
2024 t = unshare_expr (gimple_eh_filter_types (stmt));
2025 gimple_eh_filter_set_types (copy, t);
2029 new_seq = gimple_seq_copy (gimple_try_eval (stmt));
2030 gimple_try_set_eval (copy, new_seq);
2031 new_seq = gimple_seq_copy (gimple_try_cleanup (stmt));
2032 gimple_try_set_cleanup (copy, new_seq);
2035 case GIMPLE_OMP_FOR:
2036 new_seq = gimple_seq_copy (gimple_omp_for_pre_body (stmt));
2037 gimple_omp_for_set_pre_body (copy, new_seq);
2038 t = unshare_expr (gimple_omp_for_clauses (stmt));
2039 gimple_omp_for_set_clauses (copy, t);
2040 copy->gimple_omp_for.iter
2041 = GGC_NEWVEC (struct gimple_omp_for_iter,
2042 gimple_omp_for_collapse (stmt));
2043 for (i = 0; i < gimple_omp_for_collapse (stmt); i++)
2045 gimple_omp_for_set_cond (copy, i,
2046 gimple_omp_for_cond (stmt, i));
2047 gimple_omp_for_set_index (copy, i,
2048 gimple_omp_for_index (stmt, i));
2049 t = unshare_expr (gimple_omp_for_initial (stmt, i));
2050 gimple_omp_for_set_initial (copy, i, t);
2051 t = unshare_expr (gimple_omp_for_final (stmt, i));
2052 gimple_omp_for_set_final (copy, i, t);
2053 t = unshare_expr (gimple_omp_for_incr (stmt, i));
2054 gimple_omp_for_set_incr (copy, i, t);
2058 case GIMPLE_OMP_PARALLEL:
2059 t = unshare_expr (gimple_omp_parallel_clauses (stmt));
2060 gimple_omp_parallel_set_clauses (copy, t);
2061 t = unshare_expr (gimple_omp_parallel_child_fn (stmt));
2062 gimple_omp_parallel_set_child_fn (copy, t);
2063 t = unshare_expr (gimple_omp_parallel_data_arg (stmt));
2064 gimple_omp_parallel_set_data_arg (copy, t);
2067 case GIMPLE_OMP_TASK:
2068 t = unshare_expr (gimple_omp_task_clauses (stmt));
2069 gimple_omp_task_set_clauses (copy, t);
2070 t = unshare_expr (gimple_omp_task_child_fn (stmt));
2071 gimple_omp_task_set_child_fn (copy, t);
2072 t = unshare_expr (gimple_omp_task_data_arg (stmt));
2073 gimple_omp_task_set_data_arg (copy, t);
2074 t = unshare_expr (gimple_omp_task_copy_fn (stmt));
2075 gimple_omp_task_set_copy_fn (copy, t);
2076 t = unshare_expr (gimple_omp_task_arg_size (stmt));
2077 gimple_omp_task_set_arg_size (copy, t);
2078 t = unshare_expr (gimple_omp_task_arg_align (stmt));
2079 gimple_omp_task_set_arg_align (copy, t);
2082 case GIMPLE_OMP_CRITICAL:
2083 t = unshare_expr (gimple_omp_critical_name (stmt));
2084 gimple_omp_critical_set_name (copy, t);
2087 case GIMPLE_OMP_SECTIONS:
2088 t = unshare_expr (gimple_omp_sections_clauses (stmt));
2089 gimple_omp_sections_set_clauses (copy, t);
2090 t = unshare_expr (gimple_omp_sections_control (stmt));
2091 gimple_omp_sections_set_control (copy, t);
2094 case GIMPLE_OMP_SINGLE:
2095 case GIMPLE_OMP_SECTION:
2096 case GIMPLE_OMP_MASTER:
2097 case GIMPLE_OMP_ORDERED:
2099 new_seq = gimple_seq_copy (gimple_omp_body (stmt));
2100 gimple_omp_set_body (copy, new_seq);
2103 case GIMPLE_WITH_CLEANUP_EXPR:
2104 new_seq = gimple_seq_copy (gimple_wce_cleanup (stmt));
2105 gimple_wce_set_cleanup (copy, new_seq);
2113 /* Make copy of operands. */
2116 for (i = 0; i < num_ops; i++)
2117 gimple_set_op (copy, i, unshare_expr (gimple_op (stmt, i)));
2119 /* Clear out SSA operand vectors on COPY. */
2120 if (gimple_has_ops (stmt))
2122 gimple_set_def_ops (copy, NULL);
2123 gimple_set_use_ops (copy, NULL);
2126 if (gimple_has_mem_ops (stmt))
2128 gimple_set_vdef (copy, gimple_vdef (stmt));
2129 gimple_set_vuse (copy, gimple_vuse (stmt));
2132 /* SSA operands need to be updated. */
2133 gimple_set_modified (copy, true);
2140 /* Set the MODIFIED flag to MODIFIEDP, iff the gimple statement G has
2141 a MODIFIED field. */
2144 gimple_set_modified (gimple s, bool modifiedp)
2146 if (gimple_has_ops (s))
2148 s->gsbase.modified = (unsigned) modifiedp;
2152 && is_gimple_call (s)
2153 && gimple_call_noreturn_p (s))
2154 VEC_safe_push (gimple, gc, MODIFIED_NORETURN_CALLS (cfun), s);
2159 /* Return true if statement S has side-effects. We consider a
2160 statement to have side effects if:
2162 - It is a GIMPLE_CALL not marked with ECF_PURE or ECF_CONST.
2163 - Any of its operands are marked TREE_THIS_VOLATILE or TREE_SIDE_EFFECTS. */
2166 gimple_has_side_effects (const_gimple s)
2170 if (is_gimple_debug (s))
2173 /* We don't have to scan the arguments to check for
2174 volatile arguments, though, at present, we still
2175 do a scan to check for TREE_SIDE_EFFECTS. */
2176 if (gimple_has_volatile_ops (s))
2179 if (is_gimple_call (s))
2181 unsigned nargs = gimple_call_num_args (s);
2183 if (!(gimple_call_flags (s) & (ECF_CONST | ECF_PURE)))
2185 else if (gimple_call_flags (s) & ECF_LOOPING_CONST_OR_PURE)
2186 /* An infinite loop is considered a side effect. */
2189 if (gimple_call_lhs (s)
2190 && TREE_SIDE_EFFECTS (gimple_call_lhs (s)))
2192 gcc_assert (gimple_has_volatile_ops (s));
2196 if (TREE_SIDE_EFFECTS (gimple_call_fn (s)))
2199 for (i = 0; i < nargs; i++)
2200 if (TREE_SIDE_EFFECTS (gimple_call_arg (s, i)))
2202 gcc_assert (gimple_has_volatile_ops (s));
2210 for (i = 0; i < gimple_num_ops (s); i++)
2211 if (TREE_SIDE_EFFECTS (gimple_op (s, i)))
2213 gcc_assert (gimple_has_volatile_ops (s));
2221 /* Return true if the RHS of statement S has side effects.
2222 We may use it to determine if it is admissable to replace
2223 an assignment or call with a copy of a previously-computed
2224 value. In such cases, side-effects due the the LHS are
2228 gimple_rhs_has_side_effects (const_gimple s)
2232 if (is_gimple_call (s))
2234 unsigned nargs = gimple_call_num_args (s);
2236 if (!(gimple_call_flags (s) & (ECF_CONST | ECF_PURE)))
2239 /* We cannot use gimple_has_volatile_ops here,
2240 because we must ignore a volatile LHS. */
2241 if (TREE_SIDE_EFFECTS (gimple_call_fn (s))
2242 || TREE_THIS_VOLATILE (gimple_call_fn (s)))
2244 gcc_assert (gimple_has_volatile_ops (s));
2248 for (i = 0; i < nargs; i++)
2249 if (TREE_SIDE_EFFECTS (gimple_call_arg (s, i))
2250 || TREE_THIS_VOLATILE (gimple_call_arg (s, i)))
2255 else if (is_gimple_assign (s))
2257 /* Skip the first operand, the LHS. */
2258 for (i = 1; i < gimple_num_ops (s); i++)
2259 if (TREE_SIDE_EFFECTS (gimple_op (s, i))
2260 || TREE_THIS_VOLATILE (gimple_op (s, i)))
2262 gcc_assert (gimple_has_volatile_ops (s));
2266 else if (is_gimple_debug (s))
2270 /* For statements without an LHS, examine all arguments. */
2271 for (i = 0; i < gimple_num_ops (s); i++)
2272 if (TREE_SIDE_EFFECTS (gimple_op (s, i))
2273 || TREE_THIS_VOLATILE (gimple_op (s, i)))
2275 gcc_assert (gimple_has_volatile_ops (s));
2284 /* Helper for gimple_could_trap_p and gimple_assign_rhs_could_trap_p.
2285 Return true if S can trap. If INCLUDE_LHS is true and S is a
2286 GIMPLE_ASSIGN, the LHS of the assignment is also checked.
2287 Otherwise, only the RHS of the assignment is checked. */
2290 gimple_could_trap_p_1 (gimple s, bool include_lhs)
2293 tree t, div = NULL_TREE;
2296 start = (is_gimple_assign (s) && !include_lhs) ? 1 : 0;
2298 for (i = start; i < gimple_num_ops (s); i++)
2299 if (tree_could_trap_p (gimple_op (s, i)))
2302 switch (gimple_code (s))
2305 return gimple_asm_volatile_p (s);
2308 t = gimple_call_fndecl (s);
2309 /* Assume that calls to weak functions may trap. */
2310 if (!t || !DECL_P (t) || DECL_WEAK (t))
2315 t = gimple_expr_type (s);
2316 op = gimple_assign_rhs_code (s);
2317 if (get_gimple_rhs_class (op) == GIMPLE_BINARY_RHS)
2318 div = gimple_assign_rhs2 (s);
2319 return (operation_could_trap_p (op, FLOAT_TYPE_P (t),
2320 (INTEGRAL_TYPE_P (t)
2321 && TYPE_OVERFLOW_TRAPS (t)),
2333 /* Return true if statement S can trap. */
2336 gimple_could_trap_p (gimple s)
2338 return gimple_could_trap_p_1 (s, true);
2342 /* Return true if RHS of a GIMPLE_ASSIGN S can trap. */
2345 gimple_assign_rhs_could_trap_p (gimple s)
2347 gcc_assert (is_gimple_assign (s));
2348 return gimple_could_trap_p_1 (s, false);
2352 /* Print debugging information for gimple stmts generated. */
2355 dump_gimple_statistics (void)
2357 #ifdef GATHER_STATISTICS
2358 int i, total_tuples = 0, total_bytes = 0;
2360 fprintf (stderr, "\nGIMPLE statements\n");
2361 fprintf (stderr, "Kind Stmts Bytes\n");
2362 fprintf (stderr, "---------------------------------------\n");
2363 for (i = 0; i < (int) gimple_alloc_kind_all; ++i)
2365 fprintf (stderr, "%-20s %7d %10d\n", gimple_alloc_kind_names[i],
2366 gimple_alloc_counts[i], gimple_alloc_sizes[i]);
2367 total_tuples += gimple_alloc_counts[i];
2368 total_bytes += gimple_alloc_sizes[i];
2370 fprintf (stderr, "---------------------------------------\n");
2371 fprintf (stderr, "%-20s %7d %10d\n", "Total", total_tuples, total_bytes);
2372 fprintf (stderr, "---------------------------------------\n");
2374 fprintf (stderr, "No gimple statistics\n");
2379 /* Return the number of operands needed on the RHS of a GIMPLE
2380 assignment for an expression with tree code CODE. */
2383 get_gimple_rhs_num_ops (enum tree_code code)
2385 enum gimple_rhs_class rhs_class = get_gimple_rhs_class (code);
2387 if (rhs_class == GIMPLE_UNARY_RHS || rhs_class == GIMPLE_SINGLE_RHS)
2389 else if (rhs_class == GIMPLE_BINARY_RHS)
2395 #define DEFTREECODE(SYM, STRING, TYPE, NARGS) \
2397 ((TYPE) == tcc_unary ? GIMPLE_UNARY_RHS \
2398 : ((TYPE) == tcc_binary \
2399 || (TYPE) == tcc_comparison) ? GIMPLE_BINARY_RHS \
2400 : ((TYPE) == tcc_constant \
2401 || (TYPE) == tcc_declaration \
2402 || (TYPE) == tcc_reference) ? GIMPLE_SINGLE_RHS \
2403 : ((SYM) == TRUTH_AND_EXPR \
2404 || (SYM) == TRUTH_OR_EXPR \
2405 || (SYM) == TRUTH_XOR_EXPR) ? GIMPLE_BINARY_RHS \
2406 : (SYM) == TRUTH_NOT_EXPR ? GIMPLE_UNARY_RHS \
2407 : ((SYM) == COND_EXPR \
2408 || (SYM) == CONSTRUCTOR \
2409 || (SYM) == OBJ_TYPE_REF \
2410 || (SYM) == ASSERT_EXPR \
2411 || (SYM) == ADDR_EXPR \
2412 || (SYM) == WITH_SIZE_EXPR \
2413 || (SYM) == SSA_NAME \
2414 || (SYM) == POLYNOMIAL_CHREC \
2415 || (SYM) == DOT_PROD_EXPR \
2416 || (SYM) == VEC_COND_EXPR \
2417 || (SYM) == REALIGN_LOAD_EXPR) ? GIMPLE_SINGLE_RHS \
2418 : GIMPLE_INVALID_RHS),
2419 #define END_OF_BASE_TREE_CODES (unsigned char) GIMPLE_INVALID_RHS,
2421 const unsigned char gimple_rhs_class_table[] = {
2422 #include "all-tree.def"
2426 #undef END_OF_BASE_TREE_CODES
2428 /* For the definitive definition of GIMPLE, see doc/tree-ssa.texi. */
2430 /* Validation of GIMPLE expressions. */
2432 /* Return true if OP is an acceptable tree node to be used as a GIMPLE
2436 is_gimple_operand (const_tree op)
2438 return op && get_gimple_rhs_class (TREE_CODE (op)) == GIMPLE_SINGLE_RHS;
2441 /* Returns true iff T is a valid RHS for an assignment to a renamed
2442 user -- or front-end generated artificial -- variable. */
2445 is_gimple_reg_rhs (tree t)
2447 return get_gimple_rhs_class (TREE_CODE (t)) != GIMPLE_INVALID_RHS;
2450 /* Returns true iff T is a valid RHS for an assignment to an un-renamed
2451 LHS, or for a call argument. */
2454 is_gimple_mem_rhs (tree t)
2456 /* If we're dealing with a renamable type, either source or dest must be
2457 a renamed variable. */
2458 if (is_gimple_reg_type (TREE_TYPE (t)))
2459 return is_gimple_val (t);
2461 return is_gimple_val (t) || is_gimple_lvalue (t);
2464 /* Return true if T is a valid LHS for a GIMPLE assignment expression. */
2467 is_gimple_lvalue (tree t)
2469 return (is_gimple_addressable (t)
2470 || TREE_CODE (t) == WITH_SIZE_EXPR
2471 /* These are complex lvalues, but don't have addresses, so they
2473 || TREE_CODE (t) == BIT_FIELD_REF);
2476 /* Return true if T is a GIMPLE condition. */
2479 is_gimple_condexpr (tree t)
2481 return (is_gimple_val (t) || (COMPARISON_CLASS_P (t)
2482 && !tree_could_trap_p (t)
2483 && is_gimple_val (TREE_OPERAND (t, 0))
2484 && is_gimple_val (TREE_OPERAND (t, 1))));
2487 /* Return true if T is something whose address can be taken. */
2490 is_gimple_addressable (tree t)
2492 return (is_gimple_id (t) || handled_component_p (t) || INDIRECT_REF_P (t));
2495 /* Return true if T is a valid gimple constant. */
2498 is_gimple_constant (const_tree t)
2500 switch (TREE_CODE (t))
2510 /* Vector constant constructors are gimple invariant. */
2512 if (TREE_TYPE (t) && TREE_CODE (TREE_TYPE (t)) == VECTOR_TYPE)
2513 return TREE_CONSTANT (t);
2522 /* Return true if T is a gimple address. */
2525 is_gimple_address (const_tree t)
2529 if (TREE_CODE (t) != ADDR_EXPR)
2532 op = TREE_OPERAND (t, 0);
2533 while (handled_component_p (op))
2535 if ((TREE_CODE (op) == ARRAY_REF
2536 || TREE_CODE (op) == ARRAY_RANGE_REF)
2537 && !is_gimple_val (TREE_OPERAND (op, 1)))
2540 op = TREE_OPERAND (op, 0);
2543 if (CONSTANT_CLASS_P (op) || INDIRECT_REF_P (op))
2546 switch (TREE_CODE (op))
2561 /* Strip out all handled components that produce invariant
2565 strip_invariant_refs (const_tree op)
2567 while (handled_component_p (op))
2569 switch (TREE_CODE (op))
2572 case ARRAY_RANGE_REF:
2573 if (!is_gimple_constant (TREE_OPERAND (op, 1))
2574 || TREE_OPERAND (op, 2) != NULL_TREE
2575 || TREE_OPERAND (op, 3) != NULL_TREE)
2580 if (TREE_OPERAND (op, 2) != NULL_TREE)
2586 op = TREE_OPERAND (op, 0);
2592 /* Return true if T is a gimple invariant address. */
2595 is_gimple_invariant_address (const_tree t)
2599 if (TREE_CODE (t) != ADDR_EXPR)
2602 op = strip_invariant_refs (TREE_OPERAND (t, 0));
2604 return op && (CONSTANT_CLASS_P (op) || decl_address_invariant_p (op));
2607 /* Return true if T is a gimple invariant address at IPA level
2608 (so addresses of variables on stack are not allowed). */
2611 is_gimple_ip_invariant_address (const_tree t)
2615 if (TREE_CODE (t) != ADDR_EXPR)
2618 op = strip_invariant_refs (TREE_OPERAND (t, 0));
2620 return op && (CONSTANT_CLASS_P (op) || decl_address_ip_invariant_p (op));
2623 /* Return true if T is a GIMPLE minimal invariant. It's a restricted
2624 form of function invariant. */
2627 is_gimple_min_invariant (const_tree t)
2629 if (TREE_CODE (t) == ADDR_EXPR)
2630 return is_gimple_invariant_address (t);
2632 return is_gimple_constant (t);
2635 /* Return true if T is a GIMPLE interprocedural invariant. It's a restricted
2636 form of gimple minimal invariant. */
2639 is_gimple_ip_invariant (const_tree t)
2641 if (TREE_CODE (t) == ADDR_EXPR)
2642 return is_gimple_ip_invariant_address (t);
2644 return is_gimple_constant (t);
2647 /* Return true if T looks like a valid GIMPLE statement. */
2650 is_gimple_stmt (tree t)
2652 const enum tree_code code = TREE_CODE (t);
2657 /* The only valid NOP_EXPR is the empty statement. */
2658 return IS_EMPTY_STMT (t);
2662 /* These are only valid if they're void. */
2663 return TREE_TYPE (t) == NULL || VOID_TYPE_P (TREE_TYPE (t));
2669 case CASE_LABEL_EXPR:
2670 case TRY_CATCH_EXPR:
2671 case TRY_FINALLY_EXPR:
2672 case EH_FILTER_EXPR:
2675 case STATEMENT_LIST:
2685 /* These are always void. */
2691 /* These are valid regardless of their type. */
2699 /* Return true if T is a variable. */
2702 is_gimple_variable (tree t)
2704 return (TREE_CODE (t) == VAR_DECL
2705 || TREE_CODE (t) == PARM_DECL
2706 || TREE_CODE (t) == RESULT_DECL
2707 || TREE_CODE (t) == SSA_NAME);
2710 /* Return true if T is a GIMPLE identifier (something with an address). */
2713 is_gimple_id (tree t)
2715 return (is_gimple_variable (t)
2716 || TREE_CODE (t) == FUNCTION_DECL
2717 || TREE_CODE (t) == LABEL_DECL
2718 || TREE_CODE (t) == CONST_DECL
2719 /* Allow string constants, since they are addressable. */
2720 || TREE_CODE (t) == STRING_CST);
2723 /* Return true if TYPE is a suitable type for a scalar register variable. */
2726 is_gimple_reg_type (tree type)
2728 return !AGGREGATE_TYPE_P (type);
2731 /* Return true if T is a non-aggregate register variable. */
2734 is_gimple_reg (tree t)
2736 if (TREE_CODE (t) == SSA_NAME)
2737 t = SSA_NAME_VAR (t);
2739 if (!is_gimple_variable (t))
2742 if (!is_gimple_reg_type (TREE_TYPE (t)))
2745 /* A volatile decl is not acceptable because we can't reuse it as
2746 needed. We need to copy it into a temp first. */
2747 if (TREE_THIS_VOLATILE (t))
2750 /* We define "registers" as things that can be renamed as needed,
2751 which with our infrastructure does not apply to memory. */
2752 if (needs_to_live_in_memory (t))
2755 /* Hard register variables are an interesting case. For those that
2756 are call-clobbered, we don't know where all the calls are, since
2757 we don't (want to) take into account which operations will turn
2758 into libcalls at the rtl level. For those that are call-saved,
2759 we don't currently model the fact that calls may in fact change
2760 global hard registers, nor do we examine ASM_CLOBBERS at the tree
2761 level, and so miss variable changes that might imply. All around,
2762 it seems safest to not do too much optimization with these at the
2763 tree level at all. We'll have to rely on the rtl optimizers to
2764 clean this up, as there we've got all the appropriate bits exposed. */
2765 if (TREE_CODE (t) == VAR_DECL && DECL_HARD_REGISTER (t))
2768 /* Complex and vector values must have been put into SSA-like form.
2769 That is, no assignments to the individual components. */
2770 if (TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE
2771 || TREE_CODE (TREE_TYPE (t)) == VECTOR_TYPE)
2772 return DECL_GIMPLE_REG_P (t);
2778 /* Return true if T is a GIMPLE variable whose address is not needed. */
2781 is_gimple_non_addressable (tree t)
2783 if (TREE_CODE (t) == SSA_NAME)
2784 t = SSA_NAME_VAR (t);
2786 return (is_gimple_variable (t) && ! needs_to_live_in_memory (t));
2789 /* Return true if T is a GIMPLE rvalue, i.e. an identifier or a constant. */
2792 is_gimple_val (tree t)
2794 /* Make loads from volatiles and memory vars explicit. */
2795 if (is_gimple_variable (t)
2796 && is_gimple_reg_type (TREE_TYPE (t))
2797 && !is_gimple_reg (t))
2800 return (is_gimple_variable (t) || is_gimple_min_invariant (t));
2803 /* Similarly, but accept hard registers as inputs to asm statements. */
2806 is_gimple_asm_val (tree t)
2808 if (TREE_CODE (t) == VAR_DECL && DECL_HARD_REGISTER (t))
2811 return is_gimple_val (t);
2814 /* Return true if T is a GIMPLE minimal lvalue. */
2817 is_gimple_min_lval (tree t)
2819 if (!(t = CONST_CAST_TREE (strip_invariant_refs (t))))
2821 return (is_gimple_id (t) || TREE_CODE (t) == INDIRECT_REF);
2824 /* Return true if T is a typecast operation. */
2827 is_gimple_cast (tree t)
2829 return (CONVERT_EXPR_P (t)
2830 || TREE_CODE (t) == FIX_TRUNC_EXPR);
2833 /* Return true if T is a valid function operand of a CALL_EXPR. */
2836 is_gimple_call_addr (tree t)
2838 return (TREE_CODE (t) == OBJ_TYPE_REF || is_gimple_val (t));
2841 /* If T makes a function call, return the corresponding CALL_EXPR operand.
2842 Otherwise, return NULL_TREE. */
2845 get_call_expr_in (tree t)
2847 if (TREE_CODE (t) == MODIFY_EXPR)
2848 t = TREE_OPERAND (t, 1);
2849 if (TREE_CODE (t) == WITH_SIZE_EXPR)
2850 t = TREE_OPERAND (t, 0);
2851 if (TREE_CODE (t) == CALL_EXPR)
2857 /* Given a memory reference expression T, return its base address.
2858 The base address of a memory reference expression is the main
2859 object being referenced. For instance, the base address for
2860 'array[i].fld[j]' is 'array'. You can think of this as stripping
2861 away the offset part from a memory address.
2863 This function calls handled_component_p to strip away all the inner
2864 parts of the memory reference until it reaches the base object. */
2867 get_base_address (tree t)
2869 while (handled_component_p (t))
2870 t = TREE_OPERAND (t, 0);
2873 || TREE_CODE (t) == STRING_CST
2874 || TREE_CODE (t) == CONSTRUCTOR
2875 || INDIRECT_REF_P (t))
2882 recalculate_side_effects (tree t)
2884 enum tree_code code = TREE_CODE (t);
2885 int len = TREE_OPERAND_LENGTH (t);
2888 switch (TREE_CODE_CLASS (code))
2890 case tcc_expression:
2896 case PREDECREMENT_EXPR:
2897 case PREINCREMENT_EXPR:
2898 case POSTDECREMENT_EXPR:
2899 case POSTINCREMENT_EXPR:
2900 /* All of these have side-effects, no matter what their
2909 case tcc_comparison: /* a comparison expression */
2910 case tcc_unary: /* a unary arithmetic expression */
2911 case tcc_binary: /* a binary arithmetic expression */
2912 case tcc_reference: /* a reference */
2913 case tcc_vl_exp: /* a function call */
2914 TREE_SIDE_EFFECTS (t) = TREE_THIS_VOLATILE (t);
2915 for (i = 0; i < len; ++i)
2917 tree op = TREE_OPERAND (t, i);
2918 if (op && TREE_SIDE_EFFECTS (op))
2919 TREE_SIDE_EFFECTS (t) = 1;
2924 /* No side-effects. */
2932 /* Canonicalize a tree T for use in a COND_EXPR as conditional. Returns
2933 a canonicalized tree that is valid for a COND_EXPR or NULL_TREE, if
2934 we failed to create one. */
2937 canonicalize_cond_expr_cond (tree t)
2939 /* For (bool)x use x != 0. */
2940 if (TREE_CODE (t) == NOP_EXPR
2941 && TREE_TYPE (t) == boolean_type_node)
2943 tree top0 = TREE_OPERAND (t, 0);
2944 t = build2 (NE_EXPR, TREE_TYPE (t),
2945 top0, build_int_cst (TREE_TYPE (top0), 0));
2947 /* For !x use x == 0. */
2948 else if (TREE_CODE (t) == TRUTH_NOT_EXPR)
2950 tree top0 = TREE_OPERAND (t, 0);
2951 t = build2 (EQ_EXPR, TREE_TYPE (t),
2952 top0, build_int_cst (TREE_TYPE (top0), 0));
2954 /* For cmp ? 1 : 0 use cmp. */
2955 else if (TREE_CODE (t) == COND_EXPR
2956 && COMPARISON_CLASS_P (TREE_OPERAND (t, 0))
2957 && integer_onep (TREE_OPERAND (t, 1))
2958 && integer_zerop (TREE_OPERAND (t, 2)))
2960 tree top0 = TREE_OPERAND (t, 0);
2961 t = build2 (TREE_CODE (top0), TREE_TYPE (t),
2962 TREE_OPERAND (top0, 0), TREE_OPERAND (top0, 1));
2965 if (is_gimple_condexpr (t))
2971 /* Build a GIMPLE_CALL identical to STMT but skipping the arguments in
2972 the positions marked by the set ARGS_TO_SKIP. */
2975 gimple_call_copy_skip_args (gimple stmt, bitmap args_to_skip)
2978 tree fn = gimple_call_fn (stmt);
2979 int nargs = gimple_call_num_args (stmt);
2980 VEC(tree, heap) *vargs = VEC_alloc (tree, heap, nargs);
2983 for (i = 0; i < nargs; i++)
2984 if (!bitmap_bit_p (args_to_skip, i))
2985 VEC_quick_push (tree, vargs, gimple_call_arg (stmt, i));
2987 new_stmt = gimple_build_call_vec (fn, vargs);
2988 VEC_free (tree, heap, vargs);
2989 if (gimple_call_lhs (stmt))
2990 gimple_call_set_lhs (new_stmt, gimple_call_lhs (stmt));
2992 gimple_set_vuse (new_stmt, gimple_vuse (stmt));
2993 gimple_set_vdef (new_stmt, gimple_vdef (stmt));
2995 gimple_set_block (new_stmt, gimple_block (stmt));
2996 if (gimple_has_location (stmt))
2997 gimple_set_location (new_stmt, gimple_location (stmt));
2999 /* Carry all the flags to the new GIMPLE_CALL. */
3000 gimple_call_set_chain (new_stmt, gimple_call_chain (stmt));
3001 gimple_call_set_tail (new_stmt, gimple_call_tail_p (stmt));
3002 gimple_call_set_cannot_inline (new_stmt, gimple_call_cannot_inline_p (stmt));
3003 gimple_call_set_return_slot_opt (new_stmt, gimple_call_return_slot_opt_p (stmt));
3004 gimple_call_set_from_thunk (new_stmt, gimple_call_from_thunk_p (stmt));
3005 gimple_call_set_va_arg_pack (new_stmt, gimple_call_va_arg_pack_p (stmt));
3007 gimple_set_modified (new_stmt, true);
3013 static hashval_t gimple_type_hash (const void *);
3015 /* Structure used to maintain a cache of some type pairs compared by
3016 gimple_types_compatible_p when comparing aggregate types. There are
3017 four possible values for SAME_P:
3019 -2: The pair (T1, T2) has just been inserted in the table.
3020 -1: The pair (T1, T2) is currently being compared.
3021 0: T1 and T2 are different types.
3022 1: T1 and T2 are the same type.
3024 This table is only used when comparing aggregate types to avoid
3025 infinite recursion due to self-referential types. */
3032 typedef struct type_pair_d *type_pair_t;
3034 /* Return a hash value for the type pair pointed-to by P. */
3037 type_pair_hash (const void *p)
3039 const struct type_pair_d *pair = (const struct type_pair_d *) p;
3040 hashval_t val1 = iterative_hash_hashval_t (htab_hash_pointer (pair->t1), 0);
3041 hashval_t val2 = iterative_hash_hashval_t (htab_hash_pointer (pair->t2), 0);
3042 return (iterative_hash_hashval_t (val2, val1)
3043 ^ iterative_hash_hashval_t (val1, val2));
3046 /* Compare two type pairs pointed-to by P1 and P2. */
3049 type_pair_eq (const void *p1, const void *p2)
3051 const struct type_pair_d *pair1 = (const struct type_pair_d *) p1;
3052 const struct type_pair_d *pair2 = (const struct type_pair_d *) p2;
3053 return ((pair1->t1 == pair2->t1 && pair1->t2 == pair2->t2)
3054 || (pair1->t1 == pair2->t2 && pair1->t2 == pair2->t1));
3057 /* Lookup the pair of types T1 and T2 in *VISITED_P. Insert a new
3058 entry if none existed. */
3061 lookup_type_pair (tree t1, tree t2, htab_t *visited_p)
3063 struct type_pair_d pair;
3067 if (*visited_p == NULL)
3068 *visited_p = htab_create (251, type_pair_hash, type_pair_eq, free);
3072 slot = htab_find_slot (*visited_p, &pair, INSERT);
3075 p = *((type_pair_t *) slot);
3078 p = XNEW (struct type_pair_d);
3089 /* Force merging the type T2 into the type T1. */
3092 gimple_force_type_merge (tree t1, tree t2)
3097 /* There's no other way than copying t2 to t1 in this case.
3098 Yuck. We'll just call this "completing" t1. */
3099 memcpy (t1, t2, tree_size (t1));
3101 /* Adjust the hash value of T1 if it was computed already. Otherwise
3102 we would be forced to not hash fields of structs to match the
3103 hash value of an incomplete struct. */
3105 && (slot = pointer_map_contains (type_hash_cache, t1)) != NULL)
3107 gimple_type_hash (t2);
3108 *slot = *pointer_map_contains (type_hash_cache, t2);
3111 /* Adjust cached comparison results for T1 and T2 to make sure
3112 they now compare compatible. */
3113 p = lookup_type_pair (t1, t2, >c_visited);
3118 /* Return true if both types have the same name. */
3121 compare_type_names_p (tree t1, tree t2)
3123 tree name1 = TYPE_NAME (t1);
3124 tree name2 = TYPE_NAME (t2);
3126 /* Consider anonymous types all unique. */
3127 if (!name1 || !name2)
3130 if (TREE_CODE (name1) == TYPE_DECL)
3132 name1 = DECL_NAME (name1);
3136 gcc_assert (TREE_CODE (name1) == IDENTIFIER_NODE);
3138 if (TREE_CODE (name2) == TYPE_DECL)
3140 name2 = DECL_NAME (name2);
3144 gcc_assert (TREE_CODE (name2) == IDENTIFIER_NODE);
3146 /* Identifiers can be compared with pointer equality rather
3147 than a string comparison. */
3154 /* Return true if the field decls F1 and F2 are at the same offset. */
3157 compare_field_offset (tree f1, tree f2)
3159 if (DECL_OFFSET_ALIGN (f1) == DECL_OFFSET_ALIGN (f2))
3160 return (operand_equal_p (DECL_FIELD_OFFSET (f1),
3161 DECL_FIELD_OFFSET (f2), 0)
3162 && tree_int_cst_equal (DECL_FIELD_BIT_OFFSET (f1),
3163 DECL_FIELD_BIT_OFFSET (f2)));
3165 /* Fortran and C do not always agree on what DECL_OFFSET_ALIGN
3166 should be, so handle differing ones specially by decomposing
3167 the offset into a byte and bit offset manually. */
3168 if (host_integerp (DECL_FIELD_OFFSET (f1), 0)
3169 && host_integerp (DECL_FIELD_OFFSET (f2), 0))
3171 unsigned HOST_WIDE_INT byte_offset1, byte_offset2;
3172 unsigned HOST_WIDE_INT bit_offset1, bit_offset2;
3173 bit_offset1 = TREE_INT_CST_LOW (DECL_FIELD_BIT_OFFSET (f1));
3174 byte_offset1 = (TREE_INT_CST_LOW (DECL_FIELD_OFFSET (f1))
3175 + bit_offset1 / BITS_PER_UNIT);
3176 bit_offset2 = TREE_INT_CST_LOW (DECL_FIELD_BIT_OFFSET (f2));
3177 byte_offset2 = (TREE_INT_CST_LOW (DECL_FIELD_OFFSET (f2))
3178 + bit_offset2 / BITS_PER_UNIT);
3179 if (byte_offset1 != byte_offset2)
3181 return bit_offset1 % BITS_PER_UNIT == bit_offset2 % BITS_PER_UNIT;
3187 /* Return 1 iff T1 and T2 are structurally identical.
3188 Otherwise, return 0. */
3191 gimple_types_compatible_p (tree t1, tree t2)
3193 type_pair_t p = NULL;
3195 /* Check first for the obvious case of pointer identity. */
3199 /* Check that we have two types to compare. */
3200 if (t1 == NULL_TREE || t2 == NULL_TREE)
3201 goto different_types;
3203 /* Can't be the same type if the types don't have the same code. */
3204 if (TREE_CODE (t1) != TREE_CODE (t2))
3205 goto different_types;
3207 /* Void types are always the same. */
3208 if (TREE_CODE (t1) == VOID_TYPE)
3211 /* Can't be the same type if they have different CV qualifiers. */
3212 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
3213 goto different_types;
3215 /* If the hash values of t1 and t2 are different the types can't
3216 possibly be the same. This helps keeping the type-pair hashtable
3217 small, only tracking comparisons for hash collisions. */
3218 if (gimple_type_hash (t1) != gimple_type_hash (t2))
3221 /* If we've visited this type pair before (in the case of aggregates
3222 with self-referential types), and we made a decision, return it. */
3223 p = lookup_type_pair (t1, t2, >c_visited);
3224 if (p->same_p == 0 || p->same_p == 1)
3226 /* We have already decided whether T1 and T2 are the
3227 same, return the cached result. */
3228 return p->same_p == 1;
3230 else if (p->same_p == -1)
3232 /* We are currently comparing this pair of types, assume
3233 that they are the same and let the caller decide. */
3237 gcc_assert (p->same_p == -2);
3239 /* Mark the (T1, T2) comparison in progress. */
3242 /* If their attributes are not the same they can't be the same type. */
3243 if (!attribute_list_equal (TYPE_ATTRIBUTES (t1), TYPE_ATTRIBUTES (t2)))
3244 goto different_types;
3246 /* For numerical types, the bounds must coincide. */
3247 if (INTEGRAL_TYPE_P (t1)
3248 || SCALAR_FLOAT_TYPE_P (t1)
3249 || FIXED_POINT_TYPE_P (t1))
3251 /* Can't be the same type if they have different size, alignment,
3252 sign, precision or mode. Note that from now on, comparisons
3253 between *_CST nodes must be done using tree_int_cst_equal because
3254 we cannot assume that constants from T1 and T2 will be shared
3255 since T1 and T2 are distinct pointers. */
3256 if (!tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2))
3257 || !tree_int_cst_equal (TYPE_SIZE_UNIT (t1), TYPE_SIZE_UNIT (t2))
3258 || TYPE_ALIGN (t1) != TYPE_ALIGN (t2)
3259 || TYPE_PRECISION (t1) != TYPE_PRECISION (t2)
3260 || TYPE_MODE (t1) != TYPE_MODE (t2)
3261 || TYPE_UNSIGNED (t1) != TYPE_UNSIGNED (t2))
3262 goto different_types;
3264 /* For non-enumeral types, check type bounds. FIXME lto, we
3265 cannot check bounds on enumeral types because different front
3266 ends will produce different values. In C, enumeral types are
3267 integers, while in C++ each element will have its own
3268 symbolic value. We should decide how enums are to be
3269 represented in GIMPLE and have each front end lower to that. */
3270 if (TREE_CODE (t1) != ENUMERAL_TYPE)
3272 tree min1 = TYPE_MIN_VALUE (t1);
3273 tree max1 = TYPE_MAX_VALUE (t1);
3274 tree min2 = TYPE_MIN_VALUE (t2);
3275 tree max2 = TYPE_MAX_VALUE (t2);
3276 bool min_equal_p = false;
3277 bool max_equal_p = false;
3279 /* If either type has a minimum value, the other type must
3281 if (min1 == NULL_TREE && min2 == NULL_TREE)
3283 else if (min1 && min2 && operand_equal_p (min1, min2, 0))
3286 /* Likewise, if either type has a maximum value, the other
3287 type must have the same. */
3288 if (max1 == NULL_TREE && max2 == NULL_TREE)
3290 else if (max1 && max2 && operand_equal_p (max1, max2, 0))
3293 if (!min_equal_p || !max_equal_p)
3294 goto different_types;
3297 if (TREE_CODE (t1) == INTEGER_TYPE)
3299 if (TYPE_IS_SIZETYPE (t1) == TYPE_IS_SIZETYPE (t2)
3300 && TYPE_STRING_FLAG (t1) == TYPE_STRING_FLAG (t2))
3303 goto different_types;
3305 else if (TREE_CODE (t1) == BOOLEAN_TYPE)
3307 else if (TREE_CODE (t1) == REAL_TYPE)
3311 /* Do type-specific comparisons. */
3312 switch (TREE_CODE (t1))
3315 /* Array types are the same if the element types are the same and
3316 the number of elements are the same. */
3317 if (!gimple_types_compatible_p (TREE_TYPE (t1), TREE_TYPE (t2))
3318 || TYPE_STRING_FLAG (t1) != TYPE_STRING_FLAG (t2))
3319 goto different_types;
3322 tree i1 = TYPE_DOMAIN (t1);
3323 tree i2 = TYPE_DOMAIN (t2);
3325 /* For an incomplete external array, the type domain can be
3326 NULL_TREE. Check this condition also. */
3327 if (i1 == NULL_TREE && i2 == NULL_TREE)
3329 else if (i1 == NULL_TREE || i2 == NULL_TREE)
3330 goto different_types;
3331 /* If for a complete array type the possibly gimplified sizes
3332 are different the types are different. */
3333 else if (((TYPE_SIZE (i1) != NULL) ^ (TYPE_SIZE (i2) != NULL))
3336 && !operand_equal_p (TYPE_SIZE (i1), TYPE_SIZE (i2), 0)))
3337 goto different_types;
3340 tree min1 = TYPE_MIN_VALUE (i1);
3341 tree min2 = TYPE_MIN_VALUE (i2);
3342 tree max1 = TYPE_MAX_VALUE (i1);
3343 tree max2 = TYPE_MAX_VALUE (i2);
3345 /* The minimum/maximum values have to be the same. */
3347 || (min1 && min2 && operand_equal_p (min1, min2, 0)))
3349 || (max1 && max2 && operand_equal_p (max1, max2, 0))))
3352 goto different_types;
3357 /* Method types should belong to the same class. */
3358 if (!gimple_types_compatible_p (TYPE_METHOD_BASETYPE (t1),
3359 TYPE_METHOD_BASETYPE (t2)))
3360 goto different_types;
3365 /* Function types are the same if the return type and arguments types
3367 if (!gimple_types_compatible_p (TREE_TYPE (t1), TREE_TYPE (t2)))
3368 goto different_types;
3371 if (!targetm.comp_type_attributes (t1, t2))
3372 goto different_types;
3374 if (TYPE_ARG_TYPES (t1) == TYPE_ARG_TYPES (t2))
3378 tree parms1, parms2;
3380 for (parms1 = TYPE_ARG_TYPES (t1), parms2 = TYPE_ARG_TYPES (t2);
3382 parms1 = TREE_CHAIN (parms1), parms2 = TREE_CHAIN (parms2))
3384 if (!gimple_types_compatible_p (TREE_VALUE (parms1),
3385 TREE_VALUE (parms2)))
3386 goto different_types;
3389 if (parms1 || parms2)
3390 goto different_types;
3397 case REFERENCE_TYPE:
3399 /* If the two pointers have different ref-all attributes,
3400 they can't be the same type. */
3401 if (TYPE_REF_CAN_ALIAS_ALL (t1) != TYPE_REF_CAN_ALIAS_ALL (t2))
3402 goto different_types;
3404 /* If one pointer points to an incomplete type variant of
3405 the other pointed-to type they are the same. */
3406 if (TREE_CODE (TREE_TYPE (t1)) == TREE_CODE (TREE_TYPE (t2))
3407 && (!COMPLETE_TYPE_P (TREE_TYPE (t1))
3408 || !COMPLETE_TYPE_P (TREE_TYPE (t2)))
3409 && compare_type_names_p (TREE_TYPE (t1), TREE_TYPE (t2)))
3411 /* If t2 is complete we want to choose it instead of t1. */
3412 if (COMPLETE_TYPE_P (TREE_TYPE (t2)))
3413 gimple_force_type_merge (TREE_TYPE (t1), TREE_TYPE (t2));
3417 /* Otherwise, pointer and reference types are the same if the
3418 pointed-to types are the same. */
3419 if (gimple_types_compatible_p (TREE_TYPE (t1), TREE_TYPE (t2)))
3422 goto different_types;
3427 /* For enumeral types, all the values must be the same. */
3430 if (TYPE_VALUES (t1) == TYPE_VALUES (t2))
3433 for (v1 = TYPE_VALUES (t1), v2 = TYPE_VALUES (t2);
3435 v1 = TREE_CHAIN (v1), v2 = TREE_CHAIN (v2))
3437 tree c1 = TREE_VALUE (v1);
3438 tree c2 = TREE_VALUE (v2);
3440 if (TREE_CODE (c1) == CONST_DECL)
3441 c1 = DECL_INITIAL (c1);
3443 if (TREE_CODE (c2) == CONST_DECL)
3444 c2 = DECL_INITIAL (c2);
3446 if (tree_int_cst_equal (c1, c2) != 1)
3447 goto different_types;
3450 /* If one enumeration has more values than the other, they
3451 are not the same. */
3453 goto different_types;
3460 case QUAL_UNION_TYPE:
3462 /* For aggregate types, all the fields must be the same. */
3465 /* Compare every field. */
3466 for (f1 = TYPE_FIELDS (t1), f2 = TYPE_FIELDS (t2);
3468 f1 = TREE_CHAIN (f1), f2 = TREE_CHAIN (f2))
3470 /* The fields must have the same name, offset and type. */
3471 if (DECL_NAME (f1) != DECL_NAME (f2)
3472 || !compare_field_offset (f1, f2)
3473 || !gimple_types_compatible_p (TREE_TYPE (f1),
3475 goto different_types;
3478 /* If one aggregate has more fields than the other, they
3479 are not the same. */
3481 goto different_types;
3487 if (TYPE_VECTOR_SUBPARTS (t1) != TYPE_VECTOR_SUBPARTS (t2))
3488 goto different_types;
3492 if (!gimple_types_compatible_p (TREE_TYPE (t1), TREE_TYPE (t2)))
3493 goto different_types;
3497 goto different_types;
3500 /* Common exit path for types that are not compatible. */
3506 /* Common exit path for types that are compatible. */
3516 /* Per pointer state for the SCC finding. The on_sccstack flag
3517 is not strictly required, it is true when there is no hash value
3518 recorded for the type and false otherwise. But querying that
3523 unsigned int dfsnum;
3529 static unsigned int next_dfs_num;
3532 iterative_hash_gimple_type (tree, hashval_t, VEC(tree, heap) **,
3533 struct pointer_map_t *, struct obstack *);
3535 /* DFS visit the edge from the callers type with state *STATE to T.
3536 Update the callers type hash V with the hash for T if it is not part
3537 of the SCC containing the callers type and return it.
3538 SCCSTACK, SCCSTATE and SCCSTATE_OBSTACK are state for the DFS walk done. */
3541 visit (tree t, struct sccs *state, hashval_t v,
3542 VEC (tree, heap) **sccstack,
3543 struct pointer_map_t *sccstate,
3544 struct obstack *sccstate_obstack)
3546 struct sccs *cstate = NULL;
3549 /* If there is a hash value recorded for this type then it can't
3550 possibly be part of our parent SCC. Simply mix in its hash. */
3551 if ((slot = pointer_map_contains (type_hash_cache, t)))
3552 return iterative_hash_hashval_t ((hashval_t) (size_t) *slot, v);
3554 if ((slot = pointer_map_contains (sccstate, t)) != NULL)
3555 cstate = (struct sccs *)*slot;
3559 /* Not yet visited. DFS recurse. */
3560 tem = iterative_hash_gimple_type (t, v,
3561 sccstack, sccstate, sccstate_obstack);
3563 cstate = (struct sccs *)* pointer_map_contains (sccstate, t);
3564 state->low = MIN (state->low, cstate->low);
3565 /* If the type is no longer on the SCC stack and thus is not part
3566 of the parents SCC mix in its hash value. Otherwise we will
3567 ignore the type for hashing purposes and return the unaltered
3569 if (!cstate->on_sccstack)
3572 if (cstate->dfsnum < state->dfsnum
3573 && cstate->on_sccstack)
3574 state->low = MIN (cstate->dfsnum, state->low);
3576 /* We are part of our parents SCC, skip this type during hashing
3577 and return the unaltered hash value. */
3581 /* Hash the name of TYPE with the previous hash value V and return it. */
3584 iterative_hash_type_name (tree type, hashval_t v)
3586 tree name = TYPE_NAME (TYPE_MAIN_VARIANT (type));
3589 if (TREE_CODE (name) == TYPE_DECL)
3590 name = DECL_NAME (name);
3593 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
3594 /* Do not hash names of anonymous unions. At least the C++ FE insists
3595 to have a non-NULL TYPE_NAME for them. See cp/cp-tree.h for all
3597 #ifndef NO_DOT_IN_LABEL
3598 if (IDENTIFIER_POINTER (name)[0] == '.')
3601 #ifndef NO_DOLLAR_IN_LABEL
3602 if (IDENTIFIER_POINTER (name)[0] == '$')
3605 if (!strncmp (IDENTIFIER_POINTER (name), "__anon_", sizeof ("__anon_") - 1))
3609 return iterative_hash_object (IDENTIFIER_HASH_VALUE (name), v);
3612 /* Returning a hash value for gimple type TYPE combined with VAL.
3613 SCCSTACK, SCCSTATE and SCCSTATE_OBSTACK are state for the DFS walk done.
3615 To hash a type we end up hashing in types that are reachable.
3616 Through pointers we can end up with cycles which messes up the
3617 required property that we need to compute the same hash value
3618 for structurally equivalent types. To avoid this we have to
3619 hash all types in a cycle (the SCC) in a commutative way. The
3620 easiest way is to not mix in the hashes of the SCC members at
3621 all. To make this work we have to delay setting the hash
3622 values of the SCC until it is complete. */
3625 iterative_hash_gimple_type (tree type, hashval_t val,
3626 VEC(tree, heap) **sccstack,
3627 struct pointer_map_t *sccstate,
3628 struct obstack *sccstate_obstack)
3634 #ifdef ENABLE_CHECKING
3635 /* Not visited during this DFS walk nor during previous walks. */
3636 gcc_assert (!pointer_map_contains (type_hash_cache, type)
3637 && !pointer_map_contains (sccstate, type));
3639 state = XOBNEW (sccstate_obstack, struct sccs);
3640 *pointer_map_insert (sccstate, type) = state;
3642 VEC_safe_push (tree, heap, *sccstack, type);
3643 state->dfsnum = next_dfs_num++;
3644 state->low = state->dfsnum;
3645 state->on_sccstack = true;
3647 /* Combine a few common features of types so that types are grouped into
3648 smaller sets; when searching for existing matching types to merge,
3649 only existing types having the same features as the new type will be
3651 v = iterative_hash_hashval_t (TREE_CODE (type), 0);
3652 v = iterative_hash_hashval_t (TYPE_QUALS (type), v);
3653 v = iterative_hash_hashval_t (TREE_ADDRESSABLE (type), v);
3655 /* Do not hash the types size as this will cause differences in
3656 hash values for the complete vs. the incomplete type variant. */
3658 /* Incorporate common features of numerical types. */
3659 if (INTEGRAL_TYPE_P (type)
3660 || SCALAR_FLOAT_TYPE_P (type)
3661 || FIXED_POINT_TYPE_P (type))
3663 v = iterative_hash_hashval_t (TYPE_PRECISION (type), v);
3664 v = iterative_hash_hashval_t (TYPE_MODE (type), v);
3665 v = iterative_hash_hashval_t (TYPE_UNSIGNED (type), v);
3668 /* For pointer and reference types, fold in information about the type
3669 pointed to but do not recurse into possibly incomplete types to
3670 avoid hash differences for complete vs. incomplete types. */
3671 if (POINTER_TYPE_P (type))
3673 if (AGGREGATE_TYPE_P (TREE_TYPE (type)))
3675 v = iterative_hash_hashval_t (TREE_CODE (TREE_TYPE (type)), v);
3676 v = iterative_hash_type_name (type, v);
3679 v = visit (TREE_TYPE (type), state, v,
3680 sccstack, sccstate, sccstate_obstack);
3683 /* Recurse for aggregates with a single element. */
3684 if (TREE_CODE (type) == ARRAY_TYPE
3685 || TREE_CODE (type) == COMPLEX_TYPE
3686 || TREE_CODE (type) == VECTOR_TYPE)
3687 v = visit (TREE_TYPE (type), state, v,
3688 sccstack, sccstate, sccstate_obstack);
3690 /* Incorporate function return and argument types. */
3691 if (TREE_CODE (type) == FUNCTION_TYPE || TREE_CODE (type) == METHOD_TYPE)
3696 /* For method types also incorporate their parent class. */
3697 if (TREE_CODE (type) == METHOD_TYPE)
3698 v = visit (TYPE_METHOD_BASETYPE (type), state, v,
3699 sccstack, sccstate, sccstate_obstack);
3701 v = visit (TREE_TYPE (type), state, v,
3702 sccstack, sccstate, sccstate_obstack);
3704 for (p = TYPE_ARG_TYPES (type), na = 0; p; p = TREE_CHAIN (p))
3706 v = visit (TREE_VALUE (p), state, v,
3707 sccstack, sccstate, sccstate_obstack);
3711 v = iterative_hash_hashval_t (na, v);
3714 if (TREE_CODE (type) == RECORD_TYPE
3715 || TREE_CODE (type) == UNION_TYPE
3716 || TREE_CODE (type) == QUAL_UNION_TYPE)
3721 v = iterative_hash_type_name (type, v);
3723 for (f = TYPE_FIELDS (type), nf = 0; f; f = TREE_CHAIN (f))
3725 v = visit (TREE_TYPE (f), state, v,
3726 sccstack, sccstate, sccstate_obstack);
3730 v = iterative_hash_hashval_t (nf, v);
3733 /* Record hash for us. */
3736 /* See if we found an SCC. */
3737 if (state->low == state->dfsnum)
3741 /* Pop off the SCC and set its hash values. */
3744 struct sccs *cstate;
3745 x = VEC_pop (tree, *sccstack);
3746 gcc_assert (!pointer_map_contains (type_hash_cache, x));
3747 cstate = (struct sccs *)*pointer_map_contains (sccstate, x);
3748 cstate->on_sccstack = false;
3749 slot = pointer_map_insert (type_hash_cache, x);
3750 *slot = (void *) (size_t) cstate->hash;
3755 return iterative_hash_hashval_t (v, val);
3759 /* Returns a hash value for P (assumed to be a type). The hash value
3760 is computed using some distinguishing features of the type. Note
3761 that we cannot use pointer hashing here as we may be dealing with
3762 two distinct instances of the same type.
3764 This function should produce the same hash value for two compatible
3765 types according to gimple_types_compatible_p. */
3768 gimple_type_hash (const void *p)
3770 const_tree t = (const_tree) p;
3771 VEC(tree, heap) *sccstack = NULL;
3772 struct pointer_map_t *sccstate;
3773 struct obstack sccstate_obstack;
3777 if (type_hash_cache == NULL)
3778 type_hash_cache = pointer_map_create ();
3780 if ((slot = pointer_map_contains (type_hash_cache, p)) != NULL)
3781 return iterative_hash_hashval_t ((hashval_t) (size_t) *slot, 0);
3783 /* Perform a DFS walk and pre-hash all reachable types. */
3785 sccstate = pointer_map_create ();
3786 gcc_obstack_init (&sccstate_obstack);
3787 val = iterative_hash_gimple_type (CONST_CAST_TREE (t), 0,
3788 &sccstack, sccstate, &sccstate_obstack);
3789 VEC_free (tree, heap, sccstack);
3790 pointer_map_destroy (sccstate);
3791 obstack_free (&sccstate_obstack, NULL);
3797 /* Returns nonzero if P1 and P2 are equal. */
3800 gimple_type_eq (const void *p1, const void *p2)
3802 const_tree t1 = (const_tree) p1;
3803 const_tree t2 = (const_tree) p2;
3804 return gimple_types_compatible_p (CONST_CAST_TREE (t1), CONST_CAST_TREE (t2));
3808 /* Register type T in the global type table gimple_types.
3809 If another type T', compatible with T, already existed in
3810 gimple_types then return T', otherwise return T. This is used by
3811 LTO to merge identical types read from different TUs. */
3814 gimple_register_type (tree t)
3818 gcc_assert (TYPE_P (t));
3820 if (gimple_types == NULL)
3821 gimple_types = htab_create (16381, gimple_type_hash, gimple_type_eq, 0);
3823 slot = htab_find_slot (gimple_types, t, INSERT);
3825 && *(tree *)slot != t)
3827 tree new_type = (tree) *((tree *) slot);
3829 /* Do not merge types with different addressability. */
3830 gcc_assert (TREE_ADDRESSABLE (t) == TREE_ADDRESSABLE (new_type));
3832 /* If t is not its main variant then make t unreachable from its
3833 main variant list. Otherwise we'd queue up a lot of duplicates
3835 if (t != TYPE_MAIN_VARIANT (t))
3837 tree tem = TYPE_MAIN_VARIANT (t);
3838 while (tem && TYPE_NEXT_VARIANT (tem) != t)
3839 tem = TYPE_NEXT_VARIANT (tem);
3841 TYPE_NEXT_VARIANT (tem) = TYPE_NEXT_VARIANT (t);
3842 TYPE_NEXT_VARIANT (t) = NULL_TREE;
3845 /* If we are a pointer then remove us from the pointer-to or
3846 reference-to chain. Otherwise we'd queue up a lot of duplicates
3848 if (TREE_CODE (t) == POINTER_TYPE)
3850 if (TYPE_POINTER_TO (TREE_TYPE (t)) == t)
3851 TYPE_POINTER_TO (TREE_TYPE (t)) = TYPE_NEXT_PTR_TO (t);
3854 tree tem = TYPE_POINTER_TO (TREE_TYPE (t));
3855 while (tem && TYPE_NEXT_PTR_TO (tem) != t)
3856 tem = TYPE_NEXT_PTR_TO (tem);
3858 TYPE_NEXT_PTR_TO (tem) = TYPE_NEXT_PTR_TO (t);
3860 TYPE_NEXT_PTR_TO (t) = NULL_TREE;
3862 else if (TREE_CODE (t) == REFERENCE_TYPE)
3864 if (TYPE_REFERENCE_TO (TREE_TYPE (t)) == t)
3865 TYPE_REFERENCE_TO (TREE_TYPE (t)) = TYPE_NEXT_REF_TO (t);
3868 tree tem = TYPE_REFERENCE_TO (TREE_TYPE (t));
3869 while (tem && TYPE_NEXT_REF_TO (tem) != t)
3870 tem = TYPE_NEXT_REF_TO (tem);
3872 TYPE_NEXT_REF_TO (tem) = TYPE_NEXT_REF_TO (t);
3874 TYPE_NEXT_REF_TO (t) = NULL_TREE;
3886 /* Show statistics on references to the global type table gimple_types. */
3889 print_gimple_types_stats (void)
3892 fprintf (stderr, "GIMPLE type table: size %ld, %ld elements, "
3893 "%ld searches, %ld collisions (ratio: %f)\n",
3894 (long) htab_size (gimple_types),
3895 (long) htab_elements (gimple_types),
3896 (long) gimple_types->searches,
3897 (long) gimple_types->collisions,
3898 htab_collisions (gimple_types));
3900 fprintf (stderr, "GIMPLE type table is empty\n");
3902 fprintf (stderr, "GIMPLE type comparison table: size %ld, %ld elements, "
3903 "%ld searches, %ld collisions (ratio: %f)\n",
3904 (long) htab_size (gtc_visited),
3905 (long) htab_elements (gtc_visited),
3906 (long) gtc_visited->searches,
3907 (long) gtc_visited->collisions,
3908 htab_collisions (gtc_visited));
3910 fprintf (stderr, "GIMPLE type comparison table is empty\n");
3914 /* Return a type the same as TYPE except unsigned or
3915 signed according to UNSIGNEDP. */
3918 gimple_signed_or_unsigned_type (bool unsignedp, tree type)
3922 type1 = TYPE_MAIN_VARIANT (type);
3923 if (type1 == signed_char_type_node
3924 || type1 == char_type_node
3925 || type1 == unsigned_char_type_node)
3926 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3927 if (type1 == integer_type_node || type1 == unsigned_type_node)
3928 return unsignedp ? unsigned_type_node : integer_type_node;
3929 if (type1 == short_integer_type_node || type1 == short_unsigned_type_node)
3930 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3931 if (type1 == long_integer_type_node || type1 == long_unsigned_type_node)
3932 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3933 if (type1 == long_long_integer_type_node
3934 || type1 == long_long_unsigned_type_node)
3936 ? long_long_unsigned_type_node
3937 : long_long_integer_type_node;
3938 #if HOST_BITS_PER_WIDE_INT >= 64
3939 if (type1 == intTI_type_node || type1 == unsigned_intTI_type_node)
3940 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3942 if (type1 == intDI_type_node || type1 == unsigned_intDI_type_node)
3943 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3944 if (type1 == intSI_type_node || type1 == unsigned_intSI_type_node)
3945 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3946 if (type1 == intHI_type_node || type1 == unsigned_intHI_type_node)
3947 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3948 if (type1 == intQI_type_node || type1 == unsigned_intQI_type_node)
3949 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3951 #define GIMPLE_FIXED_TYPES(NAME) \
3952 if (type1 == short_ ## NAME ## _type_node \
3953 || type1 == unsigned_short_ ## NAME ## _type_node) \
3954 return unsignedp ? unsigned_short_ ## NAME ## _type_node \
3955 : short_ ## NAME ## _type_node; \
3956 if (type1 == NAME ## _type_node \
3957 || type1 == unsigned_ ## NAME ## _type_node) \
3958 return unsignedp ? unsigned_ ## NAME ## _type_node \
3959 : NAME ## _type_node; \
3960 if (type1 == long_ ## NAME ## _type_node \
3961 || type1 == unsigned_long_ ## NAME ## _type_node) \
3962 return unsignedp ? unsigned_long_ ## NAME ## _type_node \
3963 : long_ ## NAME ## _type_node; \
3964 if (type1 == long_long_ ## NAME ## _type_node \
3965 || type1 == unsigned_long_long_ ## NAME ## _type_node) \
3966 return unsignedp ? unsigned_long_long_ ## NAME ## _type_node \
3967 : long_long_ ## NAME ## _type_node;
3969 #define GIMPLE_FIXED_MODE_TYPES(NAME) \
3970 if (type1 == NAME ## _type_node \
3971 || type1 == u ## NAME ## _type_node) \
3972 return unsignedp ? u ## NAME ## _type_node \
3973 : NAME ## _type_node;
3975 #define GIMPLE_FIXED_TYPES_SAT(NAME) \
3976 if (type1 == sat_ ## short_ ## NAME ## _type_node \
3977 || type1 == sat_ ## unsigned_short_ ## NAME ## _type_node) \
3978 return unsignedp ? sat_ ## unsigned_short_ ## NAME ## _type_node \
3979 : sat_ ## short_ ## NAME ## _type_node; \
3980 if (type1 == sat_ ## NAME ## _type_node \
3981 || type1 == sat_ ## unsigned_ ## NAME ## _type_node) \
3982 return unsignedp ? sat_ ## unsigned_ ## NAME ## _type_node \
3983 : sat_ ## NAME ## _type_node; \
3984 if (type1 == sat_ ## long_ ## NAME ## _type_node \
3985 || type1 == sat_ ## unsigned_long_ ## NAME ## _type_node) \
3986 return unsignedp ? sat_ ## unsigned_long_ ## NAME ## _type_node \
3987 : sat_ ## long_ ## NAME ## _type_node; \
3988 if (type1 == sat_ ## long_long_ ## NAME ## _type_node \
3989 || type1 == sat_ ## unsigned_long_long_ ## NAME ## _type_node) \
3990 return unsignedp ? sat_ ## unsigned_long_long_ ## NAME ## _type_node \
3991 : sat_ ## long_long_ ## NAME ## _type_node;
3993 #define GIMPLE_FIXED_MODE_TYPES_SAT(NAME) \
3994 if (type1 == sat_ ## NAME ## _type_node \
3995 || type1 == sat_ ## u ## NAME ## _type_node) \
3996 return unsignedp ? sat_ ## u ## NAME ## _type_node \
3997 : sat_ ## NAME ## _type_node;
3999 GIMPLE_FIXED_TYPES (fract);
4000 GIMPLE_FIXED_TYPES_SAT (fract);
4001 GIMPLE_FIXED_TYPES (accum);
4002 GIMPLE_FIXED_TYPES_SAT (accum);
4004 GIMPLE_FIXED_MODE_TYPES (qq);
4005 GIMPLE_FIXED_MODE_TYPES (hq);
4006 GIMPLE_FIXED_MODE_TYPES (sq);
4007 GIMPLE_FIXED_MODE_TYPES (dq);
4008 GIMPLE_FIXED_MODE_TYPES (tq);
4009 GIMPLE_FIXED_MODE_TYPES_SAT (qq);
4010 GIMPLE_FIXED_MODE_TYPES_SAT (hq);
4011 GIMPLE_FIXED_MODE_TYPES_SAT (sq);
4012 GIMPLE_FIXED_MODE_TYPES_SAT (dq);
4013 GIMPLE_FIXED_MODE_TYPES_SAT (tq);
4014 GIMPLE_FIXED_MODE_TYPES (ha);
4015 GIMPLE_FIXED_MODE_TYPES (sa);
4016 GIMPLE_FIXED_MODE_TYPES (da);
4017 GIMPLE_FIXED_MODE_TYPES (ta);
4018 GIMPLE_FIXED_MODE_TYPES_SAT (ha);
4019 GIMPLE_FIXED_MODE_TYPES_SAT (sa);
4020 GIMPLE_FIXED_MODE_TYPES_SAT (da);
4021 GIMPLE_FIXED_MODE_TYPES_SAT (ta);
4023 /* For ENUMERAL_TYPEs in C++, must check the mode of the types, not
4024 the precision; they have precision set to match their range, but
4025 may use a wider mode to match an ABI. If we change modes, we may
4026 wind up with bad conversions. For INTEGER_TYPEs in C, must check
4027 the precision as well, so as to yield correct results for
4028 bit-field types. C++ does not have these separate bit-field
4029 types, and producing a signed or unsigned variant of an
4030 ENUMERAL_TYPE may cause other problems as well. */
4031 if (!INTEGRAL_TYPE_P (type)
4032 || TYPE_UNSIGNED (type) == unsignedp)
4035 #define TYPE_OK(node) \
4036 (TYPE_MODE (type) == TYPE_MODE (node) \
4037 && TYPE_PRECISION (type) == TYPE_PRECISION (node))
4038 if (TYPE_OK (signed_char_type_node))
4039 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
4040 if (TYPE_OK (integer_type_node))
4041 return unsignedp ? unsigned_type_node : integer_type_node;
4042 if (TYPE_OK (short_integer_type_node))
4043 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
4044 if (TYPE_OK (long_integer_type_node))
4045 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
4046 if (TYPE_OK (long_long_integer_type_node))
4048 ? long_long_unsigned_type_node
4049 : long_long_integer_type_node);
4051 #if HOST_BITS_PER_WIDE_INT >= 64
4052 if (TYPE_OK (intTI_type_node))
4053 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
4055 if (TYPE_OK (intDI_type_node))
4056 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
4057 if (TYPE_OK (intSI_type_node))
4058 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
4059 if (TYPE_OK (intHI_type_node))
4060 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
4061 if (TYPE_OK (intQI_type_node))
4062 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
4064 #undef GIMPLE_FIXED_TYPES
4065 #undef GIMPLE_FIXED_MODE_TYPES
4066 #undef GIMPLE_FIXED_TYPES_SAT
4067 #undef GIMPLE_FIXED_MODE_TYPES_SAT
4070 return build_nonstandard_integer_type (TYPE_PRECISION (type), unsignedp);
4074 /* Return an unsigned type the same as TYPE in other respects. */
4077 gimple_unsigned_type (tree type)
4079 return gimple_signed_or_unsigned_type (true, type);
4083 /* Return a signed type the same as TYPE in other respects. */
4086 gimple_signed_type (tree type)
4088 return gimple_signed_or_unsigned_type (false, type);
4092 /* Return the typed-based alias set for T, which may be an expression
4093 or a type. Return -1 if we don't do anything special. */
4096 gimple_get_alias_set (tree t)
4100 /* Permit type-punning when accessing a union, provided the access
4101 is directly through the union. For example, this code does not
4102 permit taking the address of a union member and then storing
4103 through it. Even the type-punning allowed here is a GCC
4104 extension, albeit a common and useful one; the C standard says
4105 that such accesses have implementation-defined behavior. */
4107 TREE_CODE (u) == COMPONENT_REF || TREE_CODE (u) == ARRAY_REF;
4108 u = TREE_OPERAND (u, 0))
4109 if (TREE_CODE (u) == COMPONENT_REF
4110 && TREE_CODE (TREE_TYPE (TREE_OPERAND (u, 0))) == UNION_TYPE)
4113 /* That's all the expressions we handle specially. */
4117 /* For convenience, follow the C standard when dealing with
4118 character types. Any object may be accessed via an lvalue that
4119 has character type. */
4120 if (t == char_type_node
4121 || t == signed_char_type_node
4122 || t == unsigned_char_type_node)
4125 /* Allow aliasing between signed and unsigned variants of the same
4126 type. We treat the signed variant as canonical. */
4127 if (TREE_CODE (t) == INTEGER_TYPE && TYPE_UNSIGNED (t))
4129 tree t1 = gimple_signed_type (t);
4131 /* t1 == t can happen for boolean nodes which are always unsigned. */
4133 return get_alias_set (t1);
4135 else if (POINTER_TYPE_P (t))
4139 /* Unfortunately, there is no canonical form of a pointer type.
4140 In particular, if we have `typedef int I', then `int *', and
4141 `I *' are different types. So, we have to pick a canonical
4142 representative. We do this below.
4144 Technically, this approach is actually more conservative that
4145 it needs to be. In particular, `const int *' and `int *'
4146 should be in different alias sets, according to the C and C++
4147 standard, since their types are not the same, and so,
4148 technically, an `int **' and `const int **' cannot point at
4151 But, the standard is wrong. In particular, this code is
4156 const int* const* cipp = ipp;
4157 And, it doesn't make sense for that to be legal unless you
4158 can dereference IPP and CIPP. So, we ignore cv-qualifiers on
4159 the pointed-to types. This issue has been reported to the
4161 t1 = build_type_no_quals (t);
4163 return get_alias_set (t1);
4170 /* Data structure used to count the number of dereferences to PTR
4171 inside an expression. */
4175 unsigned num_stores;
4179 /* Helper for count_uses_and_derefs. Called by walk_tree to look for
4180 (ALIGN/MISALIGNED_)INDIRECT_REF nodes for the pointer passed in DATA. */
4183 count_ptr_derefs (tree *tp, int *walk_subtrees, void *data)
4185 struct walk_stmt_info *wi_p = (struct walk_stmt_info *) data;
4186 struct count_ptr_d *count_p = (struct count_ptr_d *) wi_p->info;
4188 /* Do not walk inside ADDR_EXPR nodes. In the expression &ptr->fld,
4189 pointer 'ptr' is *not* dereferenced, it is simply used to compute
4190 the address of 'fld' as 'ptr + offsetof(fld)'. */
4191 if (TREE_CODE (*tp) == ADDR_EXPR)
4197 if (INDIRECT_REF_P (*tp) && TREE_OPERAND (*tp, 0) == count_p->ptr)
4200 count_p->num_stores++;
4202 count_p->num_loads++;
4208 /* Count the number of direct and indirect uses for pointer PTR in
4209 statement STMT. The number of direct uses is stored in
4210 *NUM_USES_P. Indirect references are counted separately depending
4211 on whether they are store or load operations. The counts are
4212 stored in *NUM_STORES_P and *NUM_LOADS_P. */
4215 count_uses_and_derefs (tree ptr, gimple stmt, unsigned *num_uses_p,
4216 unsigned *num_loads_p, unsigned *num_stores_p)
4225 /* Find out the total number of uses of PTR in STMT. */
4226 FOR_EACH_SSA_TREE_OPERAND (use, stmt, i, SSA_OP_USE)
4230 /* Now count the number of indirect references to PTR. This is
4231 truly awful, but we don't have much choice. There are no parent
4232 pointers inside INDIRECT_REFs, so an expression like
4233 '*x_1 = foo (x_1, *x_1)' needs to be traversed piece by piece to
4234 find all the indirect and direct uses of x_1 inside. The only
4235 shortcut we can take is the fact that GIMPLE only allows
4236 INDIRECT_REFs inside the expressions below. */
4237 if (is_gimple_assign (stmt)
4238 || gimple_code (stmt) == GIMPLE_RETURN
4239 || gimple_code (stmt) == GIMPLE_ASM
4240 || is_gimple_call (stmt))
4242 struct walk_stmt_info wi;
4243 struct count_ptr_d count;
4246 count.num_stores = 0;
4247 count.num_loads = 0;
4249 memset (&wi, 0, sizeof (wi));
4251 walk_gimple_op (stmt, count_ptr_derefs, &wi);
4253 *num_stores_p = count.num_stores;
4254 *num_loads_p = count.num_loads;
4257 gcc_assert (*num_uses_p >= *num_loads_p + *num_stores_p);
4260 /* From a tree operand OP return the base of a load or store operation
4261 or NULL_TREE if OP is not a load or a store. */
4264 get_base_loadstore (tree op)
4266 while (handled_component_p (op))
4267 op = TREE_OPERAND (op, 0);
4269 || INDIRECT_REF_P (op)
4270 || TREE_CODE (op) == TARGET_MEM_REF)
4275 /* For the statement STMT call the callbacks VISIT_LOAD, VISIT_STORE and
4276 VISIT_ADDR if non-NULL on loads, store and address-taken operands
4277 passing the STMT, the base of the operand and DATA to it. The base
4278 will be either a decl, an indirect reference (including TARGET_MEM_REF)
4279 or the argument of an address expression.
4280 Returns the results of these callbacks or'ed. */
4283 walk_stmt_load_store_addr_ops (gimple stmt, void *data,
4284 bool (*visit_load)(gimple, tree, void *),
4285 bool (*visit_store)(gimple, tree, void *),
4286 bool (*visit_addr)(gimple, tree, void *))
4290 if (gimple_assign_single_p (stmt))
4295 lhs = get_base_loadstore (gimple_assign_lhs (stmt));
4297 ret |= visit_store (stmt, lhs, data);
4299 rhs = gimple_assign_rhs1 (stmt);
4300 while (handled_component_p (rhs))
4301 rhs = TREE_OPERAND (rhs, 0);
4304 if (TREE_CODE (rhs) == ADDR_EXPR)
4305 ret |= visit_addr (stmt, TREE_OPERAND (rhs, 0), data);
4306 else if (TREE_CODE (rhs) == TARGET_MEM_REF
4307 && TMR_BASE (rhs) != NULL_TREE
4308 && TREE_CODE (TMR_BASE (rhs)) == ADDR_EXPR)
4309 ret |= visit_addr (stmt, TREE_OPERAND (TMR_BASE (rhs), 0), data);
4310 else if (TREE_CODE (rhs) == OBJ_TYPE_REF
4311 && TREE_CODE (OBJ_TYPE_REF_OBJECT (rhs)) == ADDR_EXPR)
4312 ret |= visit_addr (stmt, TREE_OPERAND (OBJ_TYPE_REF_OBJECT (rhs),
4314 lhs = gimple_assign_lhs (stmt);
4315 if (TREE_CODE (lhs) == TARGET_MEM_REF
4316 && TMR_BASE (lhs) != NULL_TREE
4317 && TREE_CODE (TMR_BASE (lhs)) == ADDR_EXPR)
4318 ret |= visit_addr (stmt, TREE_OPERAND (TMR_BASE (lhs), 0), data);
4322 rhs = get_base_loadstore (rhs);
4324 ret |= visit_load (stmt, rhs, data);
4328 && (is_gimple_assign (stmt)
4329 || gimple_code (stmt) == GIMPLE_COND))
4331 for (i = 0; i < gimple_num_ops (stmt); ++i)
4332 if (gimple_op (stmt, i)
4333 && TREE_CODE (gimple_op (stmt, i)) == ADDR_EXPR)
4334 ret |= visit_addr (stmt, TREE_OPERAND (gimple_op (stmt, i), 0), data);
4336 else if (is_gimple_call (stmt))
4340 tree lhs = gimple_call_lhs (stmt);
4343 lhs = get_base_loadstore (lhs);
4345 ret |= visit_store (stmt, lhs, data);
4348 if (visit_load || visit_addr)
4349 for (i = 0; i < gimple_call_num_args (stmt); ++i)
4351 tree rhs = gimple_call_arg (stmt, i);
4353 && TREE_CODE (rhs) == ADDR_EXPR)
4354 ret |= visit_addr (stmt, TREE_OPERAND (rhs, 0), data);
4355 else if (visit_load)
4357 rhs = get_base_loadstore (rhs);
4359 ret |= visit_load (stmt, rhs, data);
4363 && gimple_call_chain (stmt)
4364 && TREE_CODE (gimple_call_chain (stmt)) == ADDR_EXPR)
4365 ret |= visit_addr (stmt, TREE_OPERAND (gimple_call_chain (stmt), 0),
4368 && gimple_call_return_slot_opt_p (stmt)
4369 && gimple_call_lhs (stmt) != NULL_TREE
4370 && TREE_ADDRESSABLE (TREE_TYPE (gimple_call_lhs (stmt))))
4371 ret |= visit_addr (stmt, gimple_call_lhs (stmt), data);
4373 else if (gimple_code (stmt) == GIMPLE_ASM)
4376 const char *constraint;
4377 const char **oconstraints;
4378 bool allows_mem, allows_reg, is_inout;
4379 noutputs = gimple_asm_noutputs (stmt);
4380 oconstraints = XALLOCAVEC (const char *, noutputs);
4381 if (visit_store || visit_addr)
4382 for (i = 0; i < gimple_asm_noutputs (stmt); ++i)
4384 tree link = gimple_asm_output_op (stmt, i);
4385 tree op = get_base_loadstore (TREE_VALUE (link));
4386 if (op && visit_store)
4387 ret |= visit_store (stmt, op, data);
4390 constraint = TREE_STRING_POINTER
4391 (TREE_VALUE (TREE_PURPOSE (link)));
4392 oconstraints[i] = constraint;
4393 parse_output_constraint (&constraint, i, 0, 0, &allows_mem,
4394 &allows_reg, &is_inout);
4395 if (op && !allows_reg && allows_mem)
4396 ret |= visit_addr (stmt, op, data);
4399 if (visit_load || visit_addr)
4400 for (i = 0; i < gimple_asm_ninputs (stmt); ++i)
4402 tree link = gimple_asm_input_op (stmt, i);
4403 tree op = TREE_VALUE (link);
4405 && TREE_CODE (op) == ADDR_EXPR)
4406 ret |= visit_addr (stmt, TREE_OPERAND (op, 0), data);
4407 else if (visit_load || visit_addr)
4409 op = get_base_loadstore (op);
4413 ret |= visit_load (stmt, op, data);
4416 constraint = TREE_STRING_POINTER
4417 (TREE_VALUE (TREE_PURPOSE (link)));
4418 parse_input_constraint (&constraint, 0, 0, noutputs,
4420 &allows_mem, &allows_reg);
4421 if (!allows_reg && allows_mem)
4422 ret |= visit_addr (stmt, op, data);
4428 else if (gimple_code (stmt) == GIMPLE_RETURN)
4430 tree op = gimple_return_retval (stmt);
4434 && TREE_CODE (op) == ADDR_EXPR)
4435 ret |= visit_addr (stmt, TREE_OPERAND (op, 0), data);
4436 else if (visit_load)
4438 op = get_base_loadstore (op);
4440 ret |= visit_load (stmt, op, data);
4445 && gimple_code (stmt) == GIMPLE_PHI)
4447 for (i = 0; i < gimple_phi_num_args (stmt); ++i)
4449 tree op = PHI_ARG_DEF (stmt, i);
4450 if (TREE_CODE (op) == ADDR_EXPR)
4451 ret |= visit_addr (stmt, TREE_OPERAND (op, 0), data);
4458 /* Like walk_stmt_load_store_addr_ops but with NULL visit_addr. IPA-CP
4459 should make a faster clone for this case. */
4462 walk_stmt_load_store_ops (gimple stmt, void *data,
4463 bool (*visit_load)(gimple, tree, void *),
4464 bool (*visit_store)(gimple, tree, void *))
4466 return walk_stmt_load_store_addr_ops (stmt, data,
4467 visit_load, visit_store, NULL);
4470 /* Helper for gimple_ior_addresses_taken_1. */
4473 gimple_ior_addresses_taken_1 (gimple stmt ATTRIBUTE_UNUSED,
4474 tree addr, void *data)
4476 bitmap addresses_taken = (bitmap)data;
4477 while (handled_component_p (addr))
4478 addr = TREE_OPERAND (addr, 0);
4481 bitmap_set_bit (addresses_taken, DECL_UID (addr));
4487 /* Set the bit for the uid of all decls that have their address taken
4488 in STMT in the ADDRESSES_TAKEN bitmap. Returns true if there
4489 were any in this stmt. */
4492 gimple_ior_addresses_taken (bitmap addresses_taken, gimple stmt)
4494 return walk_stmt_load_store_addr_ops (stmt, addresses_taken, NULL, NULL,
4495 gimple_ior_addresses_taken_1);
4499 /* Return a printable name for symbol DECL. */
4502 gimple_decl_printable_name (tree decl, int verbosity)
4504 gcc_assert (decl && DECL_NAME (decl));
4506 if (DECL_ASSEMBLER_NAME_SET_P (decl))
4508 const char *str, *mangled_str;
4509 int dmgl_opts = DMGL_NO_OPTS;
4513 dmgl_opts = DMGL_VERBOSE
4517 if (TREE_CODE (decl) == FUNCTION_DECL)
4518 dmgl_opts |= DMGL_PARAMS;
4521 mangled_str = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
4522 str = cplus_demangle_v3 (mangled_str, dmgl_opts);
4523 return (str) ? str : mangled_str;
4526 return IDENTIFIER_POINTER (DECL_NAME (decl));
4530 /* Fold a OBJ_TYPE_REF expression to the address of a function.
4531 KNOWN_TYPE carries the true type of OBJ_TYPE_REF_OBJECT(REF). Adapted
4532 from cp_fold_obj_type_ref, but it tolerates types with no binfo
4536 gimple_fold_obj_type_ref (tree ref, tree known_type)
4538 HOST_WIDE_INT index;
4543 if (TYPE_BINFO (known_type) == NULL_TREE)
4546 v = BINFO_VIRTUALS (TYPE_BINFO (known_type));
4547 index = tree_low_cst (OBJ_TYPE_REF_TOKEN (ref), 1);
4551 i += (TARGET_VTABLE_USES_DESCRIPTORS
4552 ? TARGET_VTABLE_USES_DESCRIPTORS : 1);
4556 fndecl = TREE_VALUE (v);
4558 #ifdef ENABLE_CHECKING
4559 gcc_assert (tree_int_cst_equal (OBJ_TYPE_REF_TOKEN (ref),
4560 DECL_VINDEX (fndecl)));
4563 cgraph_node (fndecl)->local.vtable_method = true;
4565 return build_fold_addr_expr (fndecl);
4568 #include "gt-gimple.h"