* configure.ac (*-*-sysv4*): Don't enable libgomp.
[official-gcc.git] / gcc / gimple.c
blob7f29bd01e4053502acba900af76578c4a0e59d19
1 /* Gimple IR support functions.
3 Copyright 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
4 Contributed by Aldy Hernandez <aldyh@redhat.com>
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "target.h"
27 #include "tree.h"
28 #include "ggc.h"
29 #include "hard-reg-set.h"
30 #include "basic-block.h"
31 #include "gimple.h"
32 #include "diagnostic.h"
33 #include "tree-flow.h"
34 #include "value-prof.h"
35 #include "flags.h"
36 #include "alias.h"
37 #include "demangle.h"
38 #include "langhooks.h"
40 /* Global type table. FIXME lto, it should be possible to re-use some
41 of the type hashing routines in tree.c (type_hash_canon, type_hash_lookup,
42 etc), but those assume that types were built with the various
43 build_*_type routines which is not the case with the streamer. */
44 static GTY((if_marked ("ggc_marked_p"), param_is (union tree_node)))
45 htab_t gimple_types;
46 static GTY((if_marked ("ggc_marked_p"), param_is (union tree_node)))
47 htab_t gimple_canonical_types;
48 static GTY((if_marked ("tree_int_map_marked_p"), param_is (struct tree_int_map)))
49 htab_t type_hash_cache;
50 static GTY((if_marked ("tree_int_map_marked_p"), param_is (struct tree_int_map)))
51 htab_t canonical_type_hash_cache;
53 /* Global type comparison cache. This is by TYPE_UID for space efficiency
54 and thus cannot use and does not need GC. */
55 static htab_t gtc_visited;
56 static struct obstack gtc_ob;
58 /* All the tuples have their operand vector (if present) at the very bottom
59 of the structure. Therefore, the offset required to find the
60 operands vector the size of the structure minus the size of the 1
61 element tree array at the end (see gimple_ops). */
62 #define DEFGSSTRUCT(SYM, STRUCT, HAS_TREE_OP) \
63 (HAS_TREE_OP ? sizeof (struct STRUCT) - sizeof (tree) : 0),
64 EXPORTED_CONST size_t gimple_ops_offset_[] = {
65 #include "gsstruct.def"
67 #undef DEFGSSTRUCT
69 #define DEFGSSTRUCT(SYM, STRUCT, HAS_TREE_OP) sizeof(struct STRUCT),
70 static const size_t gsstruct_code_size[] = {
71 #include "gsstruct.def"
73 #undef DEFGSSTRUCT
75 #define DEFGSCODE(SYM, NAME, GSSCODE) NAME,
76 const char *const gimple_code_name[] = {
77 #include "gimple.def"
79 #undef DEFGSCODE
81 #define DEFGSCODE(SYM, NAME, GSSCODE) GSSCODE,
82 EXPORTED_CONST enum gimple_statement_structure_enum gss_for_code_[] = {
83 #include "gimple.def"
85 #undef DEFGSCODE
87 #ifdef GATHER_STATISTICS
88 /* Gimple stats. */
90 int gimple_alloc_counts[(int) gimple_alloc_kind_all];
91 int gimple_alloc_sizes[(int) gimple_alloc_kind_all];
93 /* Keep in sync with gimple.h:enum gimple_alloc_kind. */
94 static const char * const gimple_alloc_kind_names[] = {
95 "assignments",
96 "phi nodes",
97 "conditionals",
98 "sequences",
99 "everything else"
102 #endif /* GATHER_STATISTICS */
104 /* A cache of gimple_seq objects. Sequences are created and destroyed
105 fairly often during gimplification. */
106 static GTY ((deletable)) struct gimple_seq_d *gimple_seq_cache;
108 /* Private API manipulation functions shared only with some
109 other files. */
110 extern void gimple_set_stored_syms (gimple, bitmap, bitmap_obstack *);
111 extern void gimple_set_loaded_syms (gimple, bitmap, bitmap_obstack *);
113 /* Gimple tuple constructors.
114 Note: Any constructor taking a ``gimple_seq'' as a parameter, can
115 be passed a NULL to start with an empty sequence. */
117 /* Set the code for statement G to CODE. */
119 static inline void
120 gimple_set_code (gimple g, enum gimple_code code)
122 g->gsbase.code = code;
125 /* Return the number of bytes needed to hold a GIMPLE statement with
126 code CODE. */
128 static inline size_t
129 gimple_size (enum gimple_code code)
131 return gsstruct_code_size[gss_for_code (code)];
134 /* Allocate memory for a GIMPLE statement with code CODE and NUM_OPS
135 operands. */
137 gimple
138 gimple_alloc_stat (enum gimple_code code, unsigned num_ops MEM_STAT_DECL)
140 size_t size;
141 gimple stmt;
143 size = gimple_size (code);
144 if (num_ops > 0)
145 size += sizeof (tree) * (num_ops - 1);
147 #ifdef GATHER_STATISTICS
149 enum gimple_alloc_kind kind = gimple_alloc_kind (code);
150 gimple_alloc_counts[(int) kind]++;
151 gimple_alloc_sizes[(int) kind] += size;
153 #endif
155 stmt = ggc_alloc_cleared_gimple_statement_d_stat (size PASS_MEM_STAT);
156 gimple_set_code (stmt, code);
157 gimple_set_num_ops (stmt, num_ops);
159 /* Do not call gimple_set_modified here as it has other side
160 effects and this tuple is still not completely built. */
161 stmt->gsbase.modified = 1;
163 return stmt;
166 /* Set SUBCODE to be the code of the expression computed by statement G. */
168 static inline void
169 gimple_set_subcode (gimple g, unsigned subcode)
171 /* We only have 16 bits for the RHS code. Assert that we are not
172 overflowing it. */
173 gcc_assert (subcode < (1 << 16));
174 g->gsbase.subcode = subcode;
179 /* Build a tuple with operands. CODE is the statement to build (which
180 must be one of the GIMPLE_WITH_OPS tuples). SUBCODE is the sub-code
181 for the new tuple. NUM_OPS is the number of operands to allocate. */
183 #define gimple_build_with_ops(c, s, n) \
184 gimple_build_with_ops_stat (c, s, n MEM_STAT_INFO)
186 static gimple
187 gimple_build_with_ops_stat (enum gimple_code code, unsigned subcode,
188 unsigned num_ops MEM_STAT_DECL)
190 gimple s = gimple_alloc_stat (code, num_ops PASS_MEM_STAT);
191 gimple_set_subcode (s, subcode);
193 return s;
197 /* Build a GIMPLE_RETURN statement returning RETVAL. */
199 gimple
200 gimple_build_return (tree retval)
202 gimple s = gimple_build_with_ops (GIMPLE_RETURN, ERROR_MARK, 1);
203 if (retval)
204 gimple_return_set_retval (s, retval);
205 return s;
208 /* Reset alias information on call S. */
210 void
211 gimple_call_reset_alias_info (gimple s)
213 if (gimple_call_flags (s) & ECF_CONST)
214 memset (gimple_call_use_set (s), 0, sizeof (struct pt_solution));
215 else
216 pt_solution_reset (gimple_call_use_set (s));
217 if (gimple_call_flags (s) & (ECF_CONST|ECF_PURE|ECF_NOVOPS))
218 memset (gimple_call_clobber_set (s), 0, sizeof (struct pt_solution));
219 else
220 pt_solution_reset (gimple_call_clobber_set (s));
223 /* Helper for gimple_build_call, gimple_build_call_vec and
224 gimple_build_call_from_tree. Build the basic components of a
225 GIMPLE_CALL statement to function FN with NARGS arguments. */
227 static inline gimple
228 gimple_build_call_1 (tree fn, unsigned nargs)
230 gimple s = gimple_build_with_ops (GIMPLE_CALL, ERROR_MARK, nargs + 3);
231 if (TREE_CODE (fn) == FUNCTION_DECL)
232 fn = build_fold_addr_expr (fn);
233 gimple_set_op (s, 1, fn);
234 gimple_call_reset_alias_info (s);
235 return s;
239 /* Build a GIMPLE_CALL statement to function FN with the arguments
240 specified in vector ARGS. */
242 gimple
243 gimple_build_call_vec (tree fn, VEC(tree, heap) *args)
245 unsigned i;
246 unsigned nargs = VEC_length (tree, args);
247 gimple call = gimple_build_call_1 (fn, nargs);
249 for (i = 0; i < nargs; i++)
250 gimple_call_set_arg (call, i, VEC_index (tree, args, i));
252 return call;
256 /* Build a GIMPLE_CALL statement to function FN. NARGS is the number of
257 arguments. The ... are the arguments. */
259 gimple
260 gimple_build_call (tree fn, unsigned nargs, ...)
262 va_list ap;
263 gimple call;
264 unsigned i;
266 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL || is_gimple_call_addr (fn));
268 call = gimple_build_call_1 (fn, nargs);
270 va_start (ap, nargs);
271 for (i = 0; i < nargs; i++)
272 gimple_call_set_arg (call, i, va_arg (ap, tree));
273 va_end (ap);
275 return call;
279 /* Build a GIMPLE_CALL statement from CALL_EXPR T. Note that T is
280 assumed to be in GIMPLE form already. Minimal checking is done of
281 this fact. */
283 gimple
284 gimple_build_call_from_tree (tree t)
286 unsigned i, nargs;
287 gimple call;
288 tree fndecl = get_callee_fndecl (t);
290 gcc_assert (TREE_CODE (t) == CALL_EXPR);
292 nargs = call_expr_nargs (t);
293 call = gimple_build_call_1 (fndecl ? fndecl : CALL_EXPR_FN (t), nargs);
295 for (i = 0; i < nargs; i++)
296 gimple_call_set_arg (call, i, CALL_EXPR_ARG (t, i));
298 gimple_set_block (call, TREE_BLOCK (t));
300 /* Carry all the CALL_EXPR flags to the new GIMPLE_CALL. */
301 gimple_call_set_chain (call, CALL_EXPR_STATIC_CHAIN (t));
302 gimple_call_set_tail (call, CALL_EXPR_TAILCALL (t));
303 gimple_call_set_cannot_inline (call, CALL_CANNOT_INLINE_P (t));
304 gimple_call_set_return_slot_opt (call, CALL_EXPR_RETURN_SLOT_OPT (t));
305 gimple_call_set_from_thunk (call, CALL_FROM_THUNK_P (t));
306 gimple_call_set_va_arg_pack (call, CALL_EXPR_VA_ARG_PACK (t));
307 gimple_call_set_nothrow (call, TREE_NOTHROW (t));
308 gimple_set_no_warning (call, TREE_NO_WARNING (t));
310 return call;
314 /* Extract the operands and code for expression EXPR into *SUBCODE_P,
315 *OP1_P, *OP2_P and *OP3_P respectively. */
317 void
318 extract_ops_from_tree_1 (tree expr, enum tree_code *subcode_p, tree *op1_p,
319 tree *op2_p, tree *op3_p)
321 enum gimple_rhs_class grhs_class;
323 *subcode_p = TREE_CODE (expr);
324 grhs_class = get_gimple_rhs_class (*subcode_p);
326 if (grhs_class == GIMPLE_TERNARY_RHS)
328 *op1_p = TREE_OPERAND (expr, 0);
329 *op2_p = TREE_OPERAND (expr, 1);
330 *op3_p = TREE_OPERAND (expr, 2);
332 else if (grhs_class == GIMPLE_BINARY_RHS)
334 *op1_p = TREE_OPERAND (expr, 0);
335 *op2_p = TREE_OPERAND (expr, 1);
336 *op3_p = NULL_TREE;
338 else if (grhs_class == GIMPLE_UNARY_RHS)
340 *op1_p = TREE_OPERAND (expr, 0);
341 *op2_p = NULL_TREE;
342 *op3_p = NULL_TREE;
344 else if (grhs_class == GIMPLE_SINGLE_RHS)
346 *op1_p = expr;
347 *op2_p = NULL_TREE;
348 *op3_p = NULL_TREE;
350 else
351 gcc_unreachable ();
355 /* Build a GIMPLE_ASSIGN statement.
357 LHS of the assignment.
358 RHS of the assignment which can be unary or binary. */
360 gimple
361 gimple_build_assign_stat (tree lhs, tree rhs MEM_STAT_DECL)
363 enum tree_code subcode;
364 tree op1, op2, op3;
366 extract_ops_from_tree_1 (rhs, &subcode, &op1, &op2, &op3);
367 return gimple_build_assign_with_ops_stat (subcode, lhs, op1, op2, op3
368 PASS_MEM_STAT);
372 /* Build a GIMPLE_ASSIGN statement with sub-code SUBCODE and operands
373 OP1 and OP2. If OP2 is NULL then SUBCODE must be of class
374 GIMPLE_UNARY_RHS or GIMPLE_SINGLE_RHS. */
376 gimple
377 gimple_build_assign_with_ops_stat (enum tree_code subcode, tree lhs, tree op1,
378 tree op2, tree op3 MEM_STAT_DECL)
380 unsigned num_ops;
381 gimple p;
383 /* Need 1 operand for LHS and 1 or 2 for the RHS (depending on the
384 code). */
385 num_ops = get_gimple_rhs_num_ops (subcode) + 1;
387 p = gimple_build_with_ops_stat (GIMPLE_ASSIGN, (unsigned)subcode, num_ops
388 PASS_MEM_STAT);
389 gimple_assign_set_lhs (p, lhs);
390 gimple_assign_set_rhs1 (p, op1);
391 if (op2)
393 gcc_assert (num_ops > 2);
394 gimple_assign_set_rhs2 (p, op2);
397 if (op3)
399 gcc_assert (num_ops > 3);
400 gimple_assign_set_rhs3 (p, op3);
403 return p;
407 /* Build a new GIMPLE_ASSIGN tuple and append it to the end of *SEQ_P.
409 DST/SRC are the destination and source respectively. You can pass
410 ungimplified trees in DST or SRC, in which case they will be
411 converted to a gimple operand if necessary.
413 This function returns the newly created GIMPLE_ASSIGN tuple. */
415 gimple
416 gimplify_assign (tree dst, tree src, gimple_seq *seq_p)
418 tree t = build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, src);
419 gimplify_and_add (t, seq_p);
420 ggc_free (t);
421 return gimple_seq_last_stmt (*seq_p);
425 /* Build a GIMPLE_COND statement.
427 PRED is the condition used to compare LHS and the RHS.
428 T_LABEL is the label to jump to if the condition is true.
429 F_LABEL is the label to jump to otherwise. */
431 gimple
432 gimple_build_cond (enum tree_code pred_code, tree lhs, tree rhs,
433 tree t_label, tree f_label)
435 gimple p;
437 gcc_assert (TREE_CODE_CLASS (pred_code) == tcc_comparison);
438 p = gimple_build_with_ops (GIMPLE_COND, pred_code, 4);
439 gimple_cond_set_lhs (p, lhs);
440 gimple_cond_set_rhs (p, rhs);
441 gimple_cond_set_true_label (p, t_label);
442 gimple_cond_set_false_label (p, f_label);
443 return p;
447 /* Extract operands for a GIMPLE_COND statement out of COND_EXPR tree COND. */
449 void
450 gimple_cond_get_ops_from_tree (tree cond, enum tree_code *code_p,
451 tree *lhs_p, tree *rhs_p)
453 gcc_assert (TREE_CODE_CLASS (TREE_CODE (cond)) == tcc_comparison
454 || TREE_CODE (cond) == TRUTH_NOT_EXPR
455 || is_gimple_min_invariant (cond)
456 || SSA_VAR_P (cond));
458 extract_ops_from_tree (cond, code_p, lhs_p, rhs_p);
460 /* Canonicalize conditionals of the form 'if (!VAL)'. */
461 if (*code_p == TRUTH_NOT_EXPR)
463 *code_p = EQ_EXPR;
464 gcc_assert (*lhs_p && *rhs_p == NULL_TREE);
465 *rhs_p = build_zero_cst (TREE_TYPE (*lhs_p));
467 /* Canonicalize conditionals of the form 'if (VAL)' */
468 else if (TREE_CODE_CLASS (*code_p) != tcc_comparison)
470 *code_p = NE_EXPR;
471 gcc_assert (*lhs_p && *rhs_p == NULL_TREE);
472 *rhs_p = build_zero_cst (TREE_TYPE (*lhs_p));
477 /* Build a GIMPLE_COND statement from the conditional expression tree
478 COND. T_LABEL and F_LABEL are as in gimple_build_cond. */
480 gimple
481 gimple_build_cond_from_tree (tree cond, tree t_label, tree f_label)
483 enum tree_code code;
484 tree lhs, rhs;
486 gimple_cond_get_ops_from_tree (cond, &code, &lhs, &rhs);
487 return gimple_build_cond (code, lhs, rhs, t_label, f_label);
490 /* Set code, lhs, and rhs of a GIMPLE_COND from a suitable
491 boolean expression tree COND. */
493 void
494 gimple_cond_set_condition_from_tree (gimple stmt, tree cond)
496 enum tree_code code;
497 tree lhs, rhs;
499 gimple_cond_get_ops_from_tree (cond, &code, &lhs, &rhs);
500 gimple_cond_set_condition (stmt, code, lhs, rhs);
503 /* Build a GIMPLE_LABEL statement for LABEL. */
505 gimple
506 gimple_build_label (tree label)
508 gimple p = gimple_build_with_ops (GIMPLE_LABEL, ERROR_MARK, 1);
509 gimple_label_set_label (p, label);
510 return p;
513 /* Build a GIMPLE_GOTO statement to label DEST. */
515 gimple
516 gimple_build_goto (tree dest)
518 gimple p = gimple_build_with_ops (GIMPLE_GOTO, ERROR_MARK, 1);
519 gimple_goto_set_dest (p, dest);
520 return p;
524 /* Build a GIMPLE_NOP statement. */
526 gimple
527 gimple_build_nop (void)
529 return gimple_alloc (GIMPLE_NOP, 0);
533 /* Build a GIMPLE_BIND statement.
534 VARS are the variables in BODY.
535 BLOCK is the containing block. */
537 gimple
538 gimple_build_bind (tree vars, gimple_seq body, tree block)
540 gimple p = gimple_alloc (GIMPLE_BIND, 0);
541 gimple_bind_set_vars (p, vars);
542 if (body)
543 gimple_bind_set_body (p, body);
544 if (block)
545 gimple_bind_set_block (p, block);
546 return p;
549 /* Helper function to set the simple fields of a asm stmt.
551 STRING is a pointer to a string that is the asm blocks assembly code.
552 NINPUT is the number of register inputs.
553 NOUTPUT is the number of register outputs.
554 NCLOBBERS is the number of clobbered registers.
557 static inline gimple
558 gimple_build_asm_1 (const char *string, unsigned ninputs, unsigned noutputs,
559 unsigned nclobbers, unsigned nlabels)
561 gimple p;
562 int size = strlen (string);
564 /* ASMs with labels cannot have outputs. This should have been
565 enforced by the front end. */
566 gcc_assert (nlabels == 0 || noutputs == 0);
568 p = gimple_build_with_ops (GIMPLE_ASM, ERROR_MARK,
569 ninputs + noutputs + nclobbers + nlabels);
571 p->gimple_asm.ni = ninputs;
572 p->gimple_asm.no = noutputs;
573 p->gimple_asm.nc = nclobbers;
574 p->gimple_asm.nl = nlabels;
575 p->gimple_asm.string = ggc_alloc_string (string, size);
577 #ifdef GATHER_STATISTICS
578 gimple_alloc_sizes[(int) gimple_alloc_kind (GIMPLE_ASM)] += size;
579 #endif
581 return p;
584 /* Build a GIMPLE_ASM statement.
586 STRING is the assembly code.
587 NINPUT is the number of register inputs.
588 NOUTPUT is the number of register outputs.
589 NCLOBBERS is the number of clobbered registers.
590 INPUTS is a vector of the input register parameters.
591 OUTPUTS is a vector of the output register parameters.
592 CLOBBERS is a vector of the clobbered register parameters.
593 LABELS is a vector of destination labels. */
595 gimple
596 gimple_build_asm_vec (const char *string, VEC(tree,gc)* inputs,
597 VEC(tree,gc)* outputs, VEC(tree,gc)* clobbers,
598 VEC(tree,gc)* labels)
600 gimple p;
601 unsigned i;
603 p = gimple_build_asm_1 (string,
604 VEC_length (tree, inputs),
605 VEC_length (tree, outputs),
606 VEC_length (tree, clobbers),
607 VEC_length (tree, labels));
609 for (i = 0; i < VEC_length (tree, inputs); i++)
610 gimple_asm_set_input_op (p, i, VEC_index (tree, inputs, i));
612 for (i = 0; i < VEC_length (tree, outputs); i++)
613 gimple_asm_set_output_op (p, i, VEC_index (tree, outputs, i));
615 for (i = 0; i < VEC_length (tree, clobbers); i++)
616 gimple_asm_set_clobber_op (p, i, VEC_index (tree, clobbers, i));
618 for (i = 0; i < VEC_length (tree, labels); i++)
619 gimple_asm_set_label_op (p, i, VEC_index (tree, labels, i));
621 return p;
624 /* Build a GIMPLE_CATCH statement.
626 TYPES are the catch types.
627 HANDLER is the exception handler. */
629 gimple
630 gimple_build_catch (tree types, gimple_seq handler)
632 gimple p = gimple_alloc (GIMPLE_CATCH, 0);
633 gimple_catch_set_types (p, types);
634 if (handler)
635 gimple_catch_set_handler (p, handler);
637 return p;
640 /* Build a GIMPLE_EH_FILTER statement.
642 TYPES are the filter's types.
643 FAILURE is the filter's failure action. */
645 gimple
646 gimple_build_eh_filter (tree types, gimple_seq failure)
648 gimple p = gimple_alloc (GIMPLE_EH_FILTER, 0);
649 gimple_eh_filter_set_types (p, types);
650 if (failure)
651 gimple_eh_filter_set_failure (p, failure);
653 return p;
656 /* Build a GIMPLE_EH_MUST_NOT_THROW statement. */
658 gimple
659 gimple_build_eh_must_not_throw (tree decl)
661 gimple p = gimple_alloc (GIMPLE_EH_MUST_NOT_THROW, 0);
663 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
664 gcc_assert (flags_from_decl_or_type (decl) & ECF_NORETURN);
665 gimple_eh_must_not_throw_set_fndecl (p, decl);
667 return p;
670 /* Build a GIMPLE_TRY statement.
672 EVAL is the expression to evaluate.
673 CLEANUP is the cleanup expression.
674 KIND is either GIMPLE_TRY_CATCH or GIMPLE_TRY_FINALLY depending on
675 whether this is a try/catch or a try/finally respectively. */
677 gimple
678 gimple_build_try (gimple_seq eval, gimple_seq cleanup,
679 enum gimple_try_flags kind)
681 gimple p;
683 gcc_assert (kind == GIMPLE_TRY_CATCH || kind == GIMPLE_TRY_FINALLY);
684 p = gimple_alloc (GIMPLE_TRY, 0);
685 gimple_set_subcode (p, kind);
686 if (eval)
687 gimple_try_set_eval (p, eval);
688 if (cleanup)
689 gimple_try_set_cleanup (p, cleanup);
691 return p;
694 /* Construct a GIMPLE_WITH_CLEANUP_EXPR statement.
696 CLEANUP is the cleanup expression. */
698 gimple
699 gimple_build_wce (gimple_seq cleanup)
701 gimple p = gimple_alloc (GIMPLE_WITH_CLEANUP_EXPR, 0);
702 if (cleanup)
703 gimple_wce_set_cleanup (p, cleanup);
705 return p;
709 /* Build a GIMPLE_RESX statement. */
711 gimple
712 gimple_build_resx (int region)
714 gimple p = gimple_build_with_ops (GIMPLE_RESX, ERROR_MARK, 0);
715 p->gimple_eh_ctrl.region = region;
716 return p;
720 /* The helper for constructing a gimple switch statement.
721 INDEX is the switch's index.
722 NLABELS is the number of labels in the switch excluding the default.
723 DEFAULT_LABEL is the default label for the switch statement. */
725 gimple
726 gimple_build_switch_nlabels (unsigned nlabels, tree index, tree default_label)
728 /* nlabels + 1 default label + 1 index. */
729 gimple p = gimple_build_with_ops (GIMPLE_SWITCH, ERROR_MARK,
730 1 + (default_label != NULL) + nlabels);
731 gimple_switch_set_index (p, index);
732 if (default_label)
733 gimple_switch_set_default_label (p, default_label);
734 return p;
738 /* Build a GIMPLE_SWITCH statement.
740 INDEX is the switch's index.
741 NLABELS is the number of labels in the switch excluding the DEFAULT_LABEL.
742 ... are the labels excluding the default. */
744 gimple
745 gimple_build_switch (unsigned nlabels, tree index, tree default_label, ...)
747 va_list al;
748 unsigned i, offset;
749 gimple p = gimple_build_switch_nlabels (nlabels, index, default_label);
751 /* Store the rest of the labels. */
752 va_start (al, default_label);
753 offset = (default_label != NULL);
754 for (i = 0; i < nlabels; i++)
755 gimple_switch_set_label (p, i + offset, va_arg (al, tree));
756 va_end (al);
758 return p;
762 /* Build a GIMPLE_SWITCH statement.
764 INDEX is the switch's index.
765 DEFAULT_LABEL is the default label
766 ARGS is a vector of labels excluding the default. */
768 gimple
769 gimple_build_switch_vec (tree index, tree default_label, VEC(tree, heap) *args)
771 unsigned i, offset, nlabels = VEC_length (tree, args);
772 gimple p = gimple_build_switch_nlabels (nlabels, index, default_label);
774 /* Copy the labels from the vector to the switch statement. */
775 offset = (default_label != NULL);
776 for (i = 0; i < nlabels; i++)
777 gimple_switch_set_label (p, i + offset, VEC_index (tree, args, i));
779 return p;
782 /* Build a GIMPLE_EH_DISPATCH statement. */
784 gimple
785 gimple_build_eh_dispatch (int region)
787 gimple p = gimple_build_with_ops (GIMPLE_EH_DISPATCH, ERROR_MARK, 0);
788 p->gimple_eh_ctrl.region = region;
789 return p;
792 /* Build a new GIMPLE_DEBUG_BIND statement.
794 VAR is bound to VALUE; block and location are taken from STMT. */
796 gimple
797 gimple_build_debug_bind_stat (tree var, tree value, gimple stmt MEM_STAT_DECL)
799 gimple p = gimple_build_with_ops_stat (GIMPLE_DEBUG,
800 (unsigned)GIMPLE_DEBUG_BIND, 2
801 PASS_MEM_STAT);
803 gimple_debug_bind_set_var (p, var);
804 gimple_debug_bind_set_value (p, value);
805 if (stmt)
807 gimple_set_block (p, gimple_block (stmt));
808 gimple_set_location (p, gimple_location (stmt));
811 return p;
815 /* Build a GIMPLE_OMP_CRITICAL statement.
817 BODY is the sequence of statements for which only one thread can execute.
818 NAME is optional identifier for this critical block. */
820 gimple
821 gimple_build_omp_critical (gimple_seq body, tree name)
823 gimple p = gimple_alloc (GIMPLE_OMP_CRITICAL, 0);
824 gimple_omp_critical_set_name (p, name);
825 if (body)
826 gimple_omp_set_body (p, body);
828 return p;
831 /* Build a GIMPLE_OMP_FOR statement.
833 BODY is sequence of statements inside the for loop.
834 CLAUSES, are any of the OMP loop construct's clauses: private, firstprivate,
835 lastprivate, reductions, ordered, schedule, and nowait.
836 COLLAPSE is the collapse count.
837 PRE_BODY is the sequence of statements that are loop invariant. */
839 gimple
840 gimple_build_omp_for (gimple_seq body, tree clauses, size_t collapse,
841 gimple_seq pre_body)
843 gimple p = gimple_alloc (GIMPLE_OMP_FOR, 0);
844 if (body)
845 gimple_omp_set_body (p, body);
846 gimple_omp_for_set_clauses (p, clauses);
847 p->gimple_omp_for.collapse = collapse;
848 p->gimple_omp_for.iter
849 = ggc_alloc_cleared_vec_gimple_omp_for_iter (collapse);
850 if (pre_body)
851 gimple_omp_for_set_pre_body (p, pre_body);
853 return p;
857 /* Build a GIMPLE_OMP_PARALLEL statement.
859 BODY is sequence of statements which are executed in parallel.
860 CLAUSES, are the OMP parallel construct's clauses.
861 CHILD_FN is the function created for the parallel threads to execute.
862 DATA_ARG are the shared data argument(s). */
864 gimple
865 gimple_build_omp_parallel (gimple_seq body, tree clauses, tree child_fn,
866 tree data_arg)
868 gimple p = gimple_alloc (GIMPLE_OMP_PARALLEL, 0);
869 if (body)
870 gimple_omp_set_body (p, body);
871 gimple_omp_parallel_set_clauses (p, clauses);
872 gimple_omp_parallel_set_child_fn (p, child_fn);
873 gimple_omp_parallel_set_data_arg (p, data_arg);
875 return p;
879 /* Build a GIMPLE_OMP_TASK statement.
881 BODY is sequence of statements which are executed by the explicit task.
882 CLAUSES, are the OMP parallel construct's clauses.
883 CHILD_FN is the function created for the parallel threads to execute.
884 DATA_ARG are the shared data argument(s).
885 COPY_FN is the optional function for firstprivate initialization.
886 ARG_SIZE and ARG_ALIGN are size and alignment of the data block. */
888 gimple
889 gimple_build_omp_task (gimple_seq body, tree clauses, tree child_fn,
890 tree data_arg, tree copy_fn, tree arg_size,
891 tree arg_align)
893 gimple p = gimple_alloc (GIMPLE_OMP_TASK, 0);
894 if (body)
895 gimple_omp_set_body (p, body);
896 gimple_omp_task_set_clauses (p, clauses);
897 gimple_omp_task_set_child_fn (p, child_fn);
898 gimple_omp_task_set_data_arg (p, data_arg);
899 gimple_omp_task_set_copy_fn (p, copy_fn);
900 gimple_omp_task_set_arg_size (p, arg_size);
901 gimple_omp_task_set_arg_align (p, arg_align);
903 return p;
907 /* Build a GIMPLE_OMP_SECTION statement for a sections statement.
909 BODY is the sequence of statements in the section. */
911 gimple
912 gimple_build_omp_section (gimple_seq body)
914 gimple p = gimple_alloc (GIMPLE_OMP_SECTION, 0);
915 if (body)
916 gimple_omp_set_body (p, body);
918 return p;
922 /* Build a GIMPLE_OMP_MASTER statement.
924 BODY is the sequence of statements to be executed by just the master. */
926 gimple
927 gimple_build_omp_master (gimple_seq body)
929 gimple p = gimple_alloc (GIMPLE_OMP_MASTER, 0);
930 if (body)
931 gimple_omp_set_body (p, body);
933 return p;
937 /* Build a GIMPLE_OMP_CONTINUE statement.
939 CONTROL_DEF is the definition of the control variable.
940 CONTROL_USE is the use of the control variable. */
942 gimple
943 gimple_build_omp_continue (tree control_def, tree control_use)
945 gimple p = gimple_alloc (GIMPLE_OMP_CONTINUE, 0);
946 gimple_omp_continue_set_control_def (p, control_def);
947 gimple_omp_continue_set_control_use (p, control_use);
948 return p;
951 /* Build a GIMPLE_OMP_ORDERED statement.
953 BODY is the sequence of statements inside a loop that will executed in
954 sequence. */
956 gimple
957 gimple_build_omp_ordered (gimple_seq body)
959 gimple p = gimple_alloc (GIMPLE_OMP_ORDERED, 0);
960 if (body)
961 gimple_omp_set_body (p, body);
963 return p;
967 /* Build a GIMPLE_OMP_RETURN statement.
968 WAIT_P is true if this is a non-waiting return. */
970 gimple
971 gimple_build_omp_return (bool wait_p)
973 gimple p = gimple_alloc (GIMPLE_OMP_RETURN, 0);
974 if (wait_p)
975 gimple_omp_return_set_nowait (p);
977 return p;
981 /* Build a GIMPLE_OMP_SECTIONS statement.
983 BODY is a sequence of section statements.
984 CLAUSES are any of the OMP sections contsruct's clauses: private,
985 firstprivate, lastprivate, reduction, and nowait. */
987 gimple
988 gimple_build_omp_sections (gimple_seq body, tree clauses)
990 gimple p = gimple_alloc (GIMPLE_OMP_SECTIONS, 0);
991 if (body)
992 gimple_omp_set_body (p, body);
993 gimple_omp_sections_set_clauses (p, clauses);
995 return p;
999 /* Build a GIMPLE_OMP_SECTIONS_SWITCH. */
1001 gimple
1002 gimple_build_omp_sections_switch (void)
1004 return gimple_alloc (GIMPLE_OMP_SECTIONS_SWITCH, 0);
1008 /* Build a GIMPLE_OMP_SINGLE statement.
1010 BODY is the sequence of statements that will be executed once.
1011 CLAUSES are any of the OMP single construct's clauses: private, firstprivate,
1012 copyprivate, nowait. */
1014 gimple
1015 gimple_build_omp_single (gimple_seq body, tree clauses)
1017 gimple p = gimple_alloc (GIMPLE_OMP_SINGLE, 0);
1018 if (body)
1019 gimple_omp_set_body (p, body);
1020 gimple_omp_single_set_clauses (p, clauses);
1022 return p;
1026 /* Build a GIMPLE_OMP_ATOMIC_LOAD statement. */
1028 gimple
1029 gimple_build_omp_atomic_load (tree lhs, tree rhs)
1031 gimple p = gimple_alloc (GIMPLE_OMP_ATOMIC_LOAD, 0);
1032 gimple_omp_atomic_load_set_lhs (p, lhs);
1033 gimple_omp_atomic_load_set_rhs (p, rhs);
1034 return p;
1037 /* Build a GIMPLE_OMP_ATOMIC_STORE statement.
1039 VAL is the value we are storing. */
1041 gimple
1042 gimple_build_omp_atomic_store (tree val)
1044 gimple p = gimple_alloc (GIMPLE_OMP_ATOMIC_STORE, 0);
1045 gimple_omp_atomic_store_set_val (p, val);
1046 return p;
1049 /* Build a GIMPLE_PREDICT statement. PREDICT is one of the predictors from
1050 predict.def, OUTCOME is NOT_TAKEN or TAKEN. */
1052 gimple
1053 gimple_build_predict (enum br_predictor predictor, enum prediction outcome)
1055 gimple p = gimple_alloc (GIMPLE_PREDICT, 0);
1056 /* Ensure all the predictors fit into the lower bits of the subcode. */
1057 gcc_assert ((int) END_PREDICTORS <= GF_PREDICT_TAKEN);
1058 gimple_predict_set_predictor (p, predictor);
1059 gimple_predict_set_outcome (p, outcome);
1060 return p;
1063 #if defined ENABLE_GIMPLE_CHECKING
1064 /* Complain of a gimple type mismatch and die. */
1066 void
1067 gimple_check_failed (const_gimple gs, const char *file, int line,
1068 const char *function, enum gimple_code code,
1069 enum tree_code subcode)
1071 internal_error ("gimple check: expected %s(%s), have %s(%s) in %s, at %s:%d",
1072 gimple_code_name[code],
1073 tree_code_name[subcode],
1074 gimple_code_name[gimple_code (gs)],
1075 gs->gsbase.subcode > 0
1076 ? tree_code_name[gs->gsbase.subcode]
1077 : "",
1078 function, trim_filename (file), line);
1080 #endif /* ENABLE_GIMPLE_CHECKING */
1083 /* Allocate a new GIMPLE sequence in GC memory and return it. If
1084 there are free sequences in GIMPLE_SEQ_CACHE return one of those
1085 instead. */
1087 gimple_seq
1088 gimple_seq_alloc (void)
1090 gimple_seq seq = gimple_seq_cache;
1091 if (seq)
1093 gimple_seq_cache = gimple_seq_cache->next_free;
1094 gcc_assert (gimple_seq_cache != seq);
1095 memset (seq, 0, sizeof (*seq));
1097 else
1099 seq = ggc_alloc_cleared_gimple_seq_d ();
1100 #ifdef GATHER_STATISTICS
1101 gimple_alloc_counts[(int) gimple_alloc_kind_seq]++;
1102 gimple_alloc_sizes[(int) gimple_alloc_kind_seq] += sizeof (*seq);
1103 #endif
1106 return seq;
1109 /* Return SEQ to the free pool of GIMPLE sequences. */
1111 void
1112 gimple_seq_free (gimple_seq seq)
1114 if (seq == NULL)
1115 return;
1117 gcc_assert (gimple_seq_first (seq) == NULL);
1118 gcc_assert (gimple_seq_last (seq) == NULL);
1120 /* If this triggers, it's a sign that the same list is being freed
1121 twice. */
1122 gcc_assert (seq != gimple_seq_cache || gimple_seq_cache == NULL);
1124 /* Add SEQ to the pool of free sequences. */
1125 seq->next_free = gimple_seq_cache;
1126 gimple_seq_cache = seq;
1130 /* Link gimple statement GS to the end of the sequence *SEQ_P. If
1131 *SEQ_P is NULL, a new sequence is allocated. */
1133 void
1134 gimple_seq_add_stmt (gimple_seq *seq_p, gimple gs)
1136 gimple_stmt_iterator si;
1138 if (gs == NULL)
1139 return;
1141 if (*seq_p == NULL)
1142 *seq_p = gimple_seq_alloc ();
1144 si = gsi_last (*seq_p);
1145 gsi_insert_after (&si, gs, GSI_NEW_STMT);
1149 /* Append sequence SRC to the end of sequence *DST_P. If *DST_P is
1150 NULL, a new sequence is allocated. */
1152 void
1153 gimple_seq_add_seq (gimple_seq *dst_p, gimple_seq src)
1155 gimple_stmt_iterator si;
1157 if (src == NULL)
1158 return;
1160 if (*dst_p == NULL)
1161 *dst_p = gimple_seq_alloc ();
1163 si = gsi_last (*dst_p);
1164 gsi_insert_seq_after (&si, src, GSI_NEW_STMT);
1168 /* Helper function of empty_body_p. Return true if STMT is an empty
1169 statement. */
1171 static bool
1172 empty_stmt_p (gimple stmt)
1174 if (gimple_code (stmt) == GIMPLE_NOP)
1175 return true;
1176 if (gimple_code (stmt) == GIMPLE_BIND)
1177 return empty_body_p (gimple_bind_body (stmt));
1178 return false;
1182 /* Return true if BODY contains nothing but empty statements. */
1184 bool
1185 empty_body_p (gimple_seq body)
1187 gimple_stmt_iterator i;
1189 if (gimple_seq_empty_p (body))
1190 return true;
1191 for (i = gsi_start (body); !gsi_end_p (i); gsi_next (&i))
1192 if (!empty_stmt_p (gsi_stmt (i))
1193 && !is_gimple_debug (gsi_stmt (i)))
1194 return false;
1196 return true;
1200 /* Perform a deep copy of sequence SRC and return the result. */
1202 gimple_seq
1203 gimple_seq_copy (gimple_seq src)
1205 gimple_stmt_iterator gsi;
1206 gimple_seq new_seq = gimple_seq_alloc ();
1207 gimple stmt;
1209 for (gsi = gsi_start (src); !gsi_end_p (gsi); gsi_next (&gsi))
1211 stmt = gimple_copy (gsi_stmt (gsi));
1212 gimple_seq_add_stmt (&new_seq, stmt);
1215 return new_seq;
1219 /* Walk all the statements in the sequence SEQ calling walk_gimple_stmt
1220 on each one. WI is as in walk_gimple_stmt.
1222 If walk_gimple_stmt returns non-NULL, the walk is stopped, the
1223 value is stored in WI->CALLBACK_RESULT and the statement that
1224 produced the value is returned.
1226 Otherwise, all the statements are walked and NULL returned. */
1228 gimple
1229 walk_gimple_seq (gimple_seq seq, walk_stmt_fn callback_stmt,
1230 walk_tree_fn callback_op, struct walk_stmt_info *wi)
1232 gimple_stmt_iterator gsi;
1234 for (gsi = gsi_start (seq); !gsi_end_p (gsi); gsi_next (&gsi))
1236 tree ret = walk_gimple_stmt (&gsi, callback_stmt, callback_op, wi);
1237 if (ret)
1239 /* If CALLBACK_STMT or CALLBACK_OP return a value, WI must exist
1240 to hold it. */
1241 gcc_assert (wi);
1242 wi->callback_result = ret;
1243 return gsi_stmt (gsi);
1247 if (wi)
1248 wi->callback_result = NULL_TREE;
1250 return NULL;
1254 /* Helper function for walk_gimple_stmt. Walk operands of a GIMPLE_ASM. */
1256 static tree
1257 walk_gimple_asm (gimple stmt, walk_tree_fn callback_op,
1258 struct walk_stmt_info *wi)
1260 tree ret, op;
1261 unsigned noutputs;
1262 const char **oconstraints;
1263 unsigned i, n;
1264 const char *constraint;
1265 bool allows_mem, allows_reg, is_inout;
1267 noutputs = gimple_asm_noutputs (stmt);
1268 oconstraints = (const char **) alloca ((noutputs) * sizeof (const char *));
1270 if (wi)
1271 wi->is_lhs = true;
1273 for (i = 0; i < noutputs; i++)
1275 op = gimple_asm_output_op (stmt, i);
1276 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (op)));
1277 oconstraints[i] = constraint;
1278 parse_output_constraint (&constraint, i, 0, 0, &allows_mem, &allows_reg,
1279 &is_inout);
1280 if (wi)
1281 wi->val_only = (allows_reg || !allows_mem);
1282 ret = walk_tree (&TREE_VALUE (op), callback_op, wi, NULL);
1283 if (ret)
1284 return ret;
1287 n = gimple_asm_ninputs (stmt);
1288 for (i = 0; i < n; i++)
1290 op = gimple_asm_input_op (stmt, i);
1291 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (op)));
1292 parse_input_constraint (&constraint, 0, 0, noutputs, 0,
1293 oconstraints, &allows_mem, &allows_reg);
1294 if (wi)
1296 wi->val_only = (allows_reg || !allows_mem);
1297 /* Although input "m" is not really a LHS, we need a lvalue. */
1298 wi->is_lhs = !wi->val_only;
1300 ret = walk_tree (&TREE_VALUE (op), callback_op, wi, NULL);
1301 if (ret)
1302 return ret;
1305 if (wi)
1307 wi->is_lhs = false;
1308 wi->val_only = true;
1311 n = gimple_asm_nlabels (stmt);
1312 for (i = 0; i < n; i++)
1314 op = gimple_asm_label_op (stmt, i);
1315 ret = walk_tree (&TREE_VALUE (op), callback_op, wi, NULL);
1316 if (ret)
1317 return ret;
1320 return NULL_TREE;
1324 /* Helper function of WALK_GIMPLE_STMT. Walk every tree operand in
1325 STMT. CALLBACK_OP and WI are as in WALK_GIMPLE_STMT.
1327 CALLBACK_OP is called on each operand of STMT via walk_tree.
1328 Additional parameters to walk_tree must be stored in WI. For each operand
1329 OP, walk_tree is called as:
1331 walk_tree (&OP, CALLBACK_OP, WI, WI->PSET)
1333 If CALLBACK_OP returns non-NULL for an operand, the remaining
1334 operands are not scanned.
1336 The return value is that returned by the last call to walk_tree, or
1337 NULL_TREE if no CALLBACK_OP is specified. */
1339 tree
1340 walk_gimple_op (gimple stmt, walk_tree_fn callback_op,
1341 struct walk_stmt_info *wi)
1343 struct pointer_set_t *pset = (wi) ? wi->pset : NULL;
1344 unsigned i;
1345 tree ret = NULL_TREE;
1347 switch (gimple_code (stmt))
1349 case GIMPLE_ASSIGN:
1350 /* Walk the RHS operands. If the LHS is of a non-renamable type or
1351 is a register variable, we may use a COMPONENT_REF on the RHS. */
1352 if (wi)
1354 tree lhs = gimple_assign_lhs (stmt);
1355 wi->val_only
1356 = (is_gimple_reg_type (TREE_TYPE (lhs)) && !is_gimple_reg (lhs))
1357 || !gimple_assign_single_p (stmt);
1360 for (i = 1; i < gimple_num_ops (stmt); i++)
1362 ret = walk_tree (gimple_op_ptr (stmt, i), callback_op, wi,
1363 pset);
1364 if (ret)
1365 return ret;
1368 /* Walk the LHS. If the RHS is appropriate for a memory, we
1369 may use a COMPONENT_REF on the LHS. */
1370 if (wi)
1372 /* If the RHS has more than 1 operand, it is not appropriate
1373 for the memory. */
1374 wi->val_only = !is_gimple_mem_rhs (gimple_assign_rhs1 (stmt))
1375 || !gimple_assign_single_p (stmt);
1376 wi->is_lhs = true;
1379 ret = walk_tree (gimple_op_ptr (stmt, 0), callback_op, wi, pset);
1380 if (ret)
1381 return ret;
1383 if (wi)
1385 wi->val_only = true;
1386 wi->is_lhs = false;
1388 break;
1390 case GIMPLE_CALL:
1391 if (wi)
1393 wi->is_lhs = false;
1394 wi->val_only = true;
1397 ret = walk_tree (gimple_call_chain_ptr (stmt), callback_op, wi, pset);
1398 if (ret)
1399 return ret;
1401 ret = walk_tree (gimple_call_fn_ptr (stmt), callback_op, wi, pset);
1402 if (ret)
1403 return ret;
1405 for (i = 0; i < gimple_call_num_args (stmt); i++)
1407 if (wi)
1408 wi->val_only = is_gimple_reg_type (gimple_call_arg (stmt, i));
1409 ret = walk_tree (gimple_call_arg_ptr (stmt, i), callback_op, wi,
1410 pset);
1411 if (ret)
1412 return ret;
1415 if (gimple_call_lhs (stmt))
1417 if (wi)
1419 wi->is_lhs = true;
1420 wi->val_only = is_gimple_reg_type (gimple_call_lhs (stmt));
1423 ret = walk_tree (gimple_call_lhs_ptr (stmt), callback_op, wi, pset);
1424 if (ret)
1425 return ret;
1428 if (wi)
1430 wi->is_lhs = false;
1431 wi->val_only = true;
1433 break;
1435 case GIMPLE_CATCH:
1436 ret = walk_tree (gimple_catch_types_ptr (stmt), callback_op, wi,
1437 pset);
1438 if (ret)
1439 return ret;
1440 break;
1442 case GIMPLE_EH_FILTER:
1443 ret = walk_tree (gimple_eh_filter_types_ptr (stmt), callback_op, wi,
1444 pset);
1445 if (ret)
1446 return ret;
1447 break;
1449 case GIMPLE_ASM:
1450 ret = walk_gimple_asm (stmt, callback_op, wi);
1451 if (ret)
1452 return ret;
1453 break;
1455 case GIMPLE_OMP_CONTINUE:
1456 ret = walk_tree (gimple_omp_continue_control_def_ptr (stmt),
1457 callback_op, wi, pset);
1458 if (ret)
1459 return ret;
1461 ret = walk_tree (gimple_omp_continue_control_use_ptr (stmt),
1462 callback_op, wi, pset);
1463 if (ret)
1464 return ret;
1465 break;
1467 case GIMPLE_OMP_CRITICAL:
1468 ret = walk_tree (gimple_omp_critical_name_ptr (stmt), callback_op, wi,
1469 pset);
1470 if (ret)
1471 return ret;
1472 break;
1474 case GIMPLE_OMP_FOR:
1475 ret = walk_tree (gimple_omp_for_clauses_ptr (stmt), callback_op, wi,
1476 pset);
1477 if (ret)
1478 return ret;
1479 for (i = 0; i < gimple_omp_for_collapse (stmt); i++)
1481 ret = walk_tree (gimple_omp_for_index_ptr (stmt, i), callback_op,
1482 wi, pset);
1483 if (ret)
1484 return ret;
1485 ret = walk_tree (gimple_omp_for_initial_ptr (stmt, i), callback_op,
1486 wi, pset);
1487 if (ret)
1488 return ret;
1489 ret = walk_tree (gimple_omp_for_final_ptr (stmt, i), callback_op,
1490 wi, pset);
1491 if (ret)
1492 return ret;
1493 ret = walk_tree (gimple_omp_for_incr_ptr (stmt, i), callback_op,
1494 wi, pset);
1496 if (ret)
1497 return ret;
1498 break;
1500 case GIMPLE_OMP_PARALLEL:
1501 ret = walk_tree (gimple_omp_parallel_clauses_ptr (stmt), callback_op,
1502 wi, pset);
1503 if (ret)
1504 return ret;
1505 ret = walk_tree (gimple_omp_parallel_child_fn_ptr (stmt), callback_op,
1506 wi, pset);
1507 if (ret)
1508 return ret;
1509 ret = walk_tree (gimple_omp_parallel_data_arg_ptr (stmt), callback_op,
1510 wi, pset);
1511 if (ret)
1512 return ret;
1513 break;
1515 case GIMPLE_OMP_TASK:
1516 ret = walk_tree (gimple_omp_task_clauses_ptr (stmt), callback_op,
1517 wi, pset);
1518 if (ret)
1519 return ret;
1520 ret = walk_tree (gimple_omp_task_child_fn_ptr (stmt), callback_op,
1521 wi, pset);
1522 if (ret)
1523 return ret;
1524 ret = walk_tree (gimple_omp_task_data_arg_ptr (stmt), callback_op,
1525 wi, pset);
1526 if (ret)
1527 return ret;
1528 ret = walk_tree (gimple_omp_task_copy_fn_ptr (stmt), callback_op,
1529 wi, pset);
1530 if (ret)
1531 return ret;
1532 ret = walk_tree (gimple_omp_task_arg_size_ptr (stmt), callback_op,
1533 wi, pset);
1534 if (ret)
1535 return ret;
1536 ret = walk_tree (gimple_omp_task_arg_align_ptr (stmt), callback_op,
1537 wi, pset);
1538 if (ret)
1539 return ret;
1540 break;
1542 case GIMPLE_OMP_SECTIONS:
1543 ret = walk_tree (gimple_omp_sections_clauses_ptr (stmt), callback_op,
1544 wi, pset);
1545 if (ret)
1546 return ret;
1548 ret = walk_tree (gimple_omp_sections_control_ptr (stmt), callback_op,
1549 wi, pset);
1550 if (ret)
1551 return ret;
1553 break;
1555 case GIMPLE_OMP_SINGLE:
1556 ret = walk_tree (gimple_omp_single_clauses_ptr (stmt), callback_op, wi,
1557 pset);
1558 if (ret)
1559 return ret;
1560 break;
1562 case GIMPLE_OMP_ATOMIC_LOAD:
1563 ret = walk_tree (gimple_omp_atomic_load_lhs_ptr (stmt), callback_op, wi,
1564 pset);
1565 if (ret)
1566 return ret;
1568 ret = walk_tree (gimple_omp_atomic_load_rhs_ptr (stmt), callback_op, wi,
1569 pset);
1570 if (ret)
1571 return ret;
1572 break;
1574 case GIMPLE_OMP_ATOMIC_STORE:
1575 ret = walk_tree (gimple_omp_atomic_store_val_ptr (stmt), callback_op,
1576 wi, pset);
1577 if (ret)
1578 return ret;
1579 break;
1581 /* Tuples that do not have operands. */
1582 case GIMPLE_NOP:
1583 case GIMPLE_RESX:
1584 case GIMPLE_OMP_RETURN:
1585 case GIMPLE_PREDICT:
1586 break;
1588 default:
1590 enum gimple_statement_structure_enum gss;
1591 gss = gimple_statement_structure (stmt);
1592 if (gss == GSS_WITH_OPS || gss == GSS_WITH_MEM_OPS)
1593 for (i = 0; i < gimple_num_ops (stmt); i++)
1595 ret = walk_tree (gimple_op_ptr (stmt, i), callback_op, wi, pset);
1596 if (ret)
1597 return ret;
1600 break;
1603 return NULL_TREE;
1607 /* Walk the current statement in GSI (optionally using traversal state
1608 stored in WI). If WI is NULL, no state is kept during traversal.
1609 The callback CALLBACK_STMT is called. If CALLBACK_STMT indicates
1610 that it has handled all the operands of the statement, its return
1611 value is returned. Otherwise, the return value from CALLBACK_STMT
1612 is discarded and its operands are scanned.
1614 If CALLBACK_STMT is NULL or it didn't handle the operands,
1615 CALLBACK_OP is called on each operand of the statement via
1616 walk_gimple_op. If walk_gimple_op returns non-NULL for any
1617 operand, the remaining operands are not scanned. In this case, the
1618 return value from CALLBACK_OP is returned.
1620 In any other case, NULL_TREE is returned. */
1622 tree
1623 walk_gimple_stmt (gimple_stmt_iterator *gsi, walk_stmt_fn callback_stmt,
1624 walk_tree_fn callback_op, struct walk_stmt_info *wi)
1626 gimple ret;
1627 tree tree_ret;
1628 gimple stmt = gsi_stmt (*gsi);
1630 if (wi)
1631 wi->gsi = *gsi;
1633 if (wi && wi->want_locations && gimple_has_location (stmt))
1634 input_location = gimple_location (stmt);
1636 ret = NULL;
1638 /* Invoke the statement callback. Return if the callback handled
1639 all of STMT operands by itself. */
1640 if (callback_stmt)
1642 bool handled_ops = false;
1643 tree_ret = callback_stmt (gsi, &handled_ops, wi);
1644 if (handled_ops)
1645 return tree_ret;
1647 /* If CALLBACK_STMT did not handle operands, it should not have
1648 a value to return. */
1649 gcc_assert (tree_ret == NULL);
1651 /* Re-read stmt in case the callback changed it. */
1652 stmt = gsi_stmt (*gsi);
1655 /* If CALLBACK_OP is defined, invoke it on every operand of STMT. */
1656 if (callback_op)
1658 tree_ret = walk_gimple_op (stmt, callback_op, wi);
1659 if (tree_ret)
1660 return tree_ret;
1663 /* If STMT can have statements inside (e.g. GIMPLE_BIND), walk them. */
1664 switch (gimple_code (stmt))
1666 case GIMPLE_BIND:
1667 ret = walk_gimple_seq (gimple_bind_body (stmt), callback_stmt,
1668 callback_op, wi);
1669 if (ret)
1670 return wi->callback_result;
1671 break;
1673 case GIMPLE_CATCH:
1674 ret = walk_gimple_seq (gimple_catch_handler (stmt), callback_stmt,
1675 callback_op, wi);
1676 if (ret)
1677 return wi->callback_result;
1678 break;
1680 case GIMPLE_EH_FILTER:
1681 ret = walk_gimple_seq (gimple_eh_filter_failure (stmt), callback_stmt,
1682 callback_op, wi);
1683 if (ret)
1684 return wi->callback_result;
1685 break;
1687 case GIMPLE_TRY:
1688 ret = walk_gimple_seq (gimple_try_eval (stmt), callback_stmt, callback_op,
1689 wi);
1690 if (ret)
1691 return wi->callback_result;
1693 ret = walk_gimple_seq (gimple_try_cleanup (stmt), callback_stmt,
1694 callback_op, wi);
1695 if (ret)
1696 return wi->callback_result;
1697 break;
1699 case GIMPLE_OMP_FOR:
1700 ret = walk_gimple_seq (gimple_omp_for_pre_body (stmt), callback_stmt,
1701 callback_op, wi);
1702 if (ret)
1703 return wi->callback_result;
1705 /* FALL THROUGH. */
1706 case GIMPLE_OMP_CRITICAL:
1707 case GIMPLE_OMP_MASTER:
1708 case GIMPLE_OMP_ORDERED:
1709 case GIMPLE_OMP_SECTION:
1710 case GIMPLE_OMP_PARALLEL:
1711 case GIMPLE_OMP_TASK:
1712 case GIMPLE_OMP_SECTIONS:
1713 case GIMPLE_OMP_SINGLE:
1714 ret = walk_gimple_seq (gimple_omp_body (stmt), callback_stmt, callback_op,
1715 wi);
1716 if (ret)
1717 return wi->callback_result;
1718 break;
1720 case GIMPLE_WITH_CLEANUP_EXPR:
1721 ret = walk_gimple_seq (gimple_wce_cleanup (stmt), callback_stmt,
1722 callback_op, wi);
1723 if (ret)
1724 return wi->callback_result;
1725 break;
1727 default:
1728 gcc_assert (!gimple_has_substatements (stmt));
1729 break;
1732 return NULL;
1736 /* Set sequence SEQ to be the GIMPLE body for function FN. */
1738 void
1739 gimple_set_body (tree fndecl, gimple_seq seq)
1741 struct function *fn = DECL_STRUCT_FUNCTION (fndecl);
1742 if (fn == NULL)
1744 /* If FNDECL still does not have a function structure associated
1745 with it, then it does not make sense for it to receive a
1746 GIMPLE body. */
1747 gcc_assert (seq == NULL);
1749 else
1750 fn->gimple_body = seq;
1754 /* Return the body of GIMPLE statements for function FN. After the
1755 CFG pass, the function body doesn't exist anymore because it has
1756 been split up into basic blocks. In this case, it returns
1757 NULL. */
1759 gimple_seq
1760 gimple_body (tree fndecl)
1762 struct function *fn = DECL_STRUCT_FUNCTION (fndecl);
1763 return fn ? fn->gimple_body : NULL;
1766 /* Return true when FNDECL has Gimple body either in unlowered
1767 or CFG form. */
1768 bool
1769 gimple_has_body_p (tree fndecl)
1771 struct function *fn = DECL_STRUCT_FUNCTION (fndecl);
1772 return (gimple_body (fndecl) || (fn && fn->cfg));
1775 /* Detect flags from a GIMPLE_CALL. This is just like
1776 call_expr_flags, but for gimple tuples. */
1779 gimple_call_flags (const_gimple stmt)
1781 int flags;
1782 tree decl = gimple_call_fndecl (stmt);
1783 tree t;
1785 if (decl)
1786 flags = flags_from_decl_or_type (decl);
1787 else
1789 t = TREE_TYPE (gimple_call_fn (stmt));
1790 if (t && TREE_CODE (t) == POINTER_TYPE)
1791 flags = flags_from_decl_or_type (TREE_TYPE (t));
1792 else
1793 flags = 0;
1796 if (stmt->gsbase.subcode & GF_CALL_NOTHROW)
1797 flags |= ECF_NOTHROW;
1799 return flags;
1802 /* Detects argument flags for argument number ARG on call STMT. */
1805 gimple_call_arg_flags (const_gimple stmt, unsigned arg)
1807 tree type = TREE_TYPE (TREE_TYPE (gimple_call_fn (stmt)));
1808 tree attr = lookup_attribute ("fn spec", TYPE_ATTRIBUTES (type));
1809 if (!attr)
1810 return 0;
1812 attr = TREE_VALUE (TREE_VALUE (attr));
1813 if (1 + arg >= (unsigned) TREE_STRING_LENGTH (attr))
1814 return 0;
1816 switch (TREE_STRING_POINTER (attr)[1 + arg])
1818 case 'x':
1819 case 'X':
1820 return EAF_UNUSED;
1822 case 'R':
1823 return EAF_DIRECT | EAF_NOCLOBBER | EAF_NOESCAPE;
1825 case 'r':
1826 return EAF_NOCLOBBER | EAF_NOESCAPE;
1828 case 'W':
1829 return EAF_DIRECT | EAF_NOESCAPE;
1831 case 'w':
1832 return EAF_NOESCAPE;
1834 case '.':
1835 default:
1836 return 0;
1840 /* Detects return flags for the call STMT. */
1843 gimple_call_return_flags (const_gimple stmt)
1845 tree type;
1846 tree attr = NULL_TREE;
1848 if (gimple_call_flags (stmt) & ECF_MALLOC)
1849 return ERF_NOALIAS;
1851 type = TREE_TYPE (TREE_TYPE (gimple_call_fn (stmt)));
1852 attr = lookup_attribute ("fn spec", TYPE_ATTRIBUTES (type));
1853 if (!attr)
1854 return 0;
1856 attr = TREE_VALUE (TREE_VALUE (attr));
1857 if (TREE_STRING_LENGTH (attr) < 1)
1858 return 0;
1860 switch (TREE_STRING_POINTER (attr)[0])
1862 case '1':
1863 case '2':
1864 case '3':
1865 case '4':
1866 return ERF_RETURNS_ARG | (TREE_STRING_POINTER (attr)[0] - '1');
1868 case 'm':
1869 return ERF_NOALIAS;
1871 case '.':
1872 default:
1873 return 0;
1878 /* Return true if GS is a copy assignment. */
1880 bool
1881 gimple_assign_copy_p (gimple gs)
1883 return (gimple_assign_single_p (gs)
1884 && is_gimple_val (gimple_op (gs, 1)));
1888 /* Return true if GS is a SSA_NAME copy assignment. */
1890 bool
1891 gimple_assign_ssa_name_copy_p (gimple gs)
1893 return (gimple_assign_single_p (gs)
1894 && TREE_CODE (gimple_assign_lhs (gs)) == SSA_NAME
1895 && TREE_CODE (gimple_assign_rhs1 (gs)) == SSA_NAME);
1899 /* Return true if GS is an assignment with a unary RHS, but the
1900 operator has no effect on the assigned value. The logic is adapted
1901 from STRIP_NOPS. This predicate is intended to be used in tuplifying
1902 instances in which STRIP_NOPS was previously applied to the RHS of
1903 an assignment.
1905 NOTE: In the use cases that led to the creation of this function
1906 and of gimple_assign_single_p, it is typical to test for either
1907 condition and to proceed in the same manner. In each case, the
1908 assigned value is represented by the single RHS operand of the
1909 assignment. I suspect there may be cases where gimple_assign_copy_p,
1910 gimple_assign_single_p, or equivalent logic is used where a similar
1911 treatment of unary NOPs is appropriate. */
1913 bool
1914 gimple_assign_unary_nop_p (gimple gs)
1916 return (is_gimple_assign (gs)
1917 && (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (gs))
1918 || gimple_assign_rhs_code (gs) == NON_LVALUE_EXPR)
1919 && gimple_assign_rhs1 (gs) != error_mark_node
1920 && (TYPE_MODE (TREE_TYPE (gimple_assign_lhs (gs)))
1921 == TYPE_MODE (TREE_TYPE (gimple_assign_rhs1 (gs)))));
1924 /* Set BB to be the basic block holding G. */
1926 void
1927 gimple_set_bb (gimple stmt, basic_block bb)
1929 stmt->gsbase.bb = bb;
1931 /* If the statement is a label, add the label to block-to-labels map
1932 so that we can speed up edge creation for GIMPLE_GOTOs. */
1933 if (cfun->cfg && gimple_code (stmt) == GIMPLE_LABEL)
1935 tree t;
1936 int uid;
1938 t = gimple_label_label (stmt);
1939 uid = LABEL_DECL_UID (t);
1940 if (uid == -1)
1942 unsigned old_len = VEC_length (basic_block, label_to_block_map);
1943 LABEL_DECL_UID (t) = uid = cfun->cfg->last_label_uid++;
1944 if (old_len <= (unsigned) uid)
1946 unsigned new_len = 3 * uid / 2 + 1;
1948 VEC_safe_grow_cleared (basic_block, gc, label_to_block_map,
1949 new_len);
1953 VEC_replace (basic_block, label_to_block_map, uid, bb);
1958 /* Modify the RHS of the assignment pointed-to by GSI using the
1959 operands in the expression tree EXPR.
1961 NOTE: The statement pointed-to by GSI may be reallocated if it
1962 did not have enough operand slots.
1964 This function is useful to convert an existing tree expression into
1965 the flat representation used for the RHS of a GIMPLE assignment.
1966 It will reallocate memory as needed to expand or shrink the number
1967 of operand slots needed to represent EXPR.
1969 NOTE: If you find yourself building a tree and then calling this
1970 function, you are most certainly doing it the slow way. It is much
1971 better to build a new assignment or to use the function
1972 gimple_assign_set_rhs_with_ops, which does not require an
1973 expression tree to be built. */
1975 void
1976 gimple_assign_set_rhs_from_tree (gimple_stmt_iterator *gsi, tree expr)
1978 enum tree_code subcode;
1979 tree op1, op2, op3;
1981 extract_ops_from_tree_1 (expr, &subcode, &op1, &op2, &op3);
1982 gimple_assign_set_rhs_with_ops_1 (gsi, subcode, op1, op2, op3);
1986 /* Set the RHS of assignment statement pointed-to by GSI to CODE with
1987 operands OP1, OP2 and OP3.
1989 NOTE: The statement pointed-to by GSI may be reallocated if it
1990 did not have enough operand slots. */
1992 void
1993 gimple_assign_set_rhs_with_ops_1 (gimple_stmt_iterator *gsi, enum tree_code code,
1994 tree op1, tree op2, tree op3)
1996 unsigned new_rhs_ops = get_gimple_rhs_num_ops (code);
1997 gimple stmt = gsi_stmt (*gsi);
1999 /* If the new CODE needs more operands, allocate a new statement. */
2000 if (gimple_num_ops (stmt) < new_rhs_ops + 1)
2002 tree lhs = gimple_assign_lhs (stmt);
2003 gimple new_stmt = gimple_alloc (gimple_code (stmt), new_rhs_ops + 1);
2004 memcpy (new_stmt, stmt, gimple_size (gimple_code (stmt)));
2005 gsi_replace (gsi, new_stmt, true);
2006 stmt = new_stmt;
2008 /* The LHS needs to be reset as this also changes the SSA name
2009 on the LHS. */
2010 gimple_assign_set_lhs (stmt, lhs);
2013 gimple_set_num_ops (stmt, new_rhs_ops + 1);
2014 gimple_set_subcode (stmt, code);
2015 gimple_assign_set_rhs1 (stmt, op1);
2016 if (new_rhs_ops > 1)
2017 gimple_assign_set_rhs2 (stmt, op2);
2018 if (new_rhs_ops > 2)
2019 gimple_assign_set_rhs3 (stmt, op3);
2023 /* Return the LHS of a statement that performs an assignment,
2024 either a GIMPLE_ASSIGN or a GIMPLE_CALL. Returns NULL_TREE
2025 for a call to a function that returns no value, or for a
2026 statement other than an assignment or a call. */
2028 tree
2029 gimple_get_lhs (const_gimple stmt)
2031 enum gimple_code code = gimple_code (stmt);
2033 if (code == GIMPLE_ASSIGN)
2034 return gimple_assign_lhs (stmt);
2035 else if (code == GIMPLE_CALL)
2036 return gimple_call_lhs (stmt);
2037 else
2038 return NULL_TREE;
2042 /* Set the LHS of a statement that performs an assignment,
2043 either a GIMPLE_ASSIGN or a GIMPLE_CALL. */
2045 void
2046 gimple_set_lhs (gimple stmt, tree lhs)
2048 enum gimple_code code = gimple_code (stmt);
2050 if (code == GIMPLE_ASSIGN)
2051 gimple_assign_set_lhs (stmt, lhs);
2052 else if (code == GIMPLE_CALL)
2053 gimple_call_set_lhs (stmt, lhs);
2054 else
2055 gcc_unreachable();
2058 /* Replace the LHS of STMT, an assignment, either a GIMPLE_ASSIGN or a
2059 GIMPLE_CALL, with NLHS, in preparation for modifying the RHS to an
2060 expression with a different value.
2062 This will update any annotations (say debug bind stmts) referring
2063 to the original LHS, so that they use the RHS instead. This is
2064 done even if NLHS and LHS are the same, for it is understood that
2065 the RHS will be modified afterwards, and NLHS will not be assigned
2066 an equivalent value.
2068 Adjusting any non-annotation uses of the LHS, if needed, is a
2069 responsibility of the caller.
2071 The effect of this call should be pretty much the same as that of
2072 inserting a copy of STMT before STMT, and then removing the
2073 original stmt, at which time gsi_remove() would have update
2074 annotations, but using this function saves all the inserting,
2075 copying and removing. */
2077 void
2078 gimple_replace_lhs (gimple stmt, tree nlhs)
2080 if (MAY_HAVE_DEBUG_STMTS)
2082 tree lhs = gimple_get_lhs (stmt);
2084 gcc_assert (SSA_NAME_DEF_STMT (lhs) == stmt);
2086 insert_debug_temp_for_var_def (NULL, lhs);
2089 gimple_set_lhs (stmt, nlhs);
2092 /* Return a deep copy of statement STMT. All the operands from STMT
2093 are reallocated and copied using unshare_expr. The DEF, USE, VDEF
2094 and VUSE operand arrays are set to empty in the new copy. */
2096 gimple
2097 gimple_copy (gimple stmt)
2099 enum gimple_code code = gimple_code (stmt);
2100 unsigned num_ops = gimple_num_ops (stmt);
2101 gimple copy = gimple_alloc (code, num_ops);
2102 unsigned i;
2104 /* Shallow copy all the fields from STMT. */
2105 memcpy (copy, stmt, gimple_size (code));
2107 /* If STMT has sub-statements, deep-copy them as well. */
2108 if (gimple_has_substatements (stmt))
2110 gimple_seq new_seq;
2111 tree t;
2113 switch (gimple_code (stmt))
2115 case GIMPLE_BIND:
2116 new_seq = gimple_seq_copy (gimple_bind_body (stmt));
2117 gimple_bind_set_body (copy, new_seq);
2118 gimple_bind_set_vars (copy, unshare_expr (gimple_bind_vars (stmt)));
2119 gimple_bind_set_block (copy, gimple_bind_block (stmt));
2120 break;
2122 case GIMPLE_CATCH:
2123 new_seq = gimple_seq_copy (gimple_catch_handler (stmt));
2124 gimple_catch_set_handler (copy, new_seq);
2125 t = unshare_expr (gimple_catch_types (stmt));
2126 gimple_catch_set_types (copy, t);
2127 break;
2129 case GIMPLE_EH_FILTER:
2130 new_seq = gimple_seq_copy (gimple_eh_filter_failure (stmt));
2131 gimple_eh_filter_set_failure (copy, new_seq);
2132 t = unshare_expr (gimple_eh_filter_types (stmt));
2133 gimple_eh_filter_set_types (copy, t);
2134 break;
2136 case GIMPLE_TRY:
2137 new_seq = gimple_seq_copy (gimple_try_eval (stmt));
2138 gimple_try_set_eval (copy, new_seq);
2139 new_seq = gimple_seq_copy (gimple_try_cleanup (stmt));
2140 gimple_try_set_cleanup (copy, new_seq);
2141 break;
2143 case GIMPLE_OMP_FOR:
2144 new_seq = gimple_seq_copy (gimple_omp_for_pre_body (stmt));
2145 gimple_omp_for_set_pre_body (copy, new_seq);
2146 t = unshare_expr (gimple_omp_for_clauses (stmt));
2147 gimple_omp_for_set_clauses (copy, t);
2148 copy->gimple_omp_for.iter
2149 = ggc_alloc_vec_gimple_omp_for_iter
2150 (gimple_omp_for_collapse (stmt));
2151 for (i = 0; i < gimple_omp_for_collapse (stmt); i++)
2153 gimple_omp_for_set_cond (copy, i,
2154 gimple_omp_for_cond (stmt, i));
2155 gimple_omp_for_set_index (copy, i,
2156 gimple_omp_for_index (stmt, i));
2157 t = unshare_expr (gimple_omp_for_initial (stmt, i));
2158 gimple_omp_for_set_initial (copy, i, t);
2159 t = unshare_expr (gimple_omp_for_final (stmt, i));
2160 gimple_omp_for_set_final (copy, i, t);
2161 t = unshare_expr (gimple_omp_for_incr (stmt, i));
2162 gimple_omp_for_set_incr (copy, i, t);
2164 goto copy_omp_body;
2166 case GIMPLE_OMP_PARALLEL:
2167 t = unshare_expr (gimple_omp_parallel_clauses (stmt));
2168 gimple_omp_parallel_set_clauses (copy, t);
2169 t = unshare_expr (gimple_omp_parallel_child_fn (stmt));
2170 gimple_omp_parallel_set_child_fn (copy, t);
2171 t = unshare_expr (gimple_omp_parallel_data_arg (stmt));
2172 gimple_omp_parallel_set_data_arg (copy, t);
2173 goto copy_omp_body;
2175 case GIMPLE_OMP_TASK:
2176 t = unshare_expr (gimple_omp_task_clauses (stmt));
2177 gimple_omp_task_set_clauses (copy, t);
2178 t = unshare_expr (gimple_omp_task_child_fn (stmt));
2179 gimple_omp_task_set_child_fn (copy, t);
2180 t = unshare_expr (gimple_omp_task_data_arg (stmt));
2181 gimple_omp_task_set_data_arg (copy, t);
2182 t = unshare_expr (gimple_omp_task_copy_fn (stmt));
2183 gimple_omp_task_set_copy_fn (copy, t);
2184 t = unshare_expr (gimple_omp_task_arg_size (stmt));
2185 gimple_omp_task_set_arg_size (copy, t);
2186 t = unshare_expr (gimple_omp_task_arg_align (stmt));
2187 gimple_omp_task_set_arg_align (copy, t);
2188 goto copy_omp_body;
2190 case GIMPLE_OMP_CRITICAL:
2191 t = unshare_expr (gimple_omp_critical_name (stmt));
2192 gimple_omp_critical_set_name (copy, t);
2193 goto copy_omp_body;
2195 case GIMPLE_OMP_SECTIONS:
2196 t = unshare_expr (gimple_omp_sections_clauses (stmt));
2197 gimple_omp_sections_set_clauses (copy, t);
2198 t = unshare_expr (gimple_omp_sections_control (stmt));
2199 gimple_omp_sections_set_control (copy, t);
2200 /* FALLTHRU */
2202 case GIMPLE_OMP_SINGLE:
2203 case GIMPLE_OMP_SECTION:
2204 case GIMPLE_OMP_MASTER:
2205 case GIMPLE_OMP_ORDERED:
2206 copy_omp_body:
2207 new_seq = gimple_seq_copy (gimple_omp_body (stmt));
2208 gimple_omp_set_body (copy, new_seq);
2209 break;
2211 case GIMPLE_WITH_CLEANUP_EXPR:
2212 new_seq = gimple_seq_copy (gimple_wce_cleanup (stmt));
2213 gimple_wce_set_cleanup (copy, new_seq);
2214 break;
2216 default:
2217 gcc_unreachable ();
2221 /* Make copy of operands. */
2222 if (num_ops > 0)
2224 for (i = 0; i < num_ops; i++)
2225 gimple_set_op (copy, i, unshare_expr (gimple_op (stmt, i)));
2227 /* Clear out SSA operand vectors on COPY. */
2228 if (gimple_has_ops (stmt))
2230 gimple_set_def_ops (copy, NULL);
2231 gimple_set_use_ops (copy, NULL);
2234 if (gimple_has_mem_ops (stmt))
2236 gimple_set_vdef (copy, gimple_vdef (stmt));
2237 gimple_set_vuse (copy, gimple_vuse (stmt));
2240 /* SSA operands need to be updated. */
2241 gimple_set_modified (copy, true);
2244 return copy;
2248 /* Set the MODIFIED flag to MODIFIEDP, iff the gimple statement G has
2249 a MODIFIED field. */
2251 void
2252 gimple_set_modified (gimple s, bool modifiedp)
2254 if (gimple_has_ops (s))
2256 s->gsbase.modified = (unsigned) modifiedp;
2258 if (modifiedp
2259 && cfun->gimple_df
2260 && is_gimple_call (s)
2261 && gimple_call_noreturn_p (s))
2262 VEC_safe_push (gimple, gc, MODIFIED_NORETURN_CALLS (cfun), s);
2267 /* Return true if statement S has side-effects. We consider a
2268 statement to have side effects if:
2270 - It is a GIMPLE_CALL not marked with ECF_PURE or ECF_CONST.
2271 - Any of its operands are marked TREE_THIS_VOLATILE or TREE_SIDE_EFFECTS. */
2273 bool
2274 gimple_has_side_effects (const_gimple s)
2276 unsigned i;
2278 if (is_gimple_debug (s))
2279 return false;
2281 /* We don't have to scan the arguments to check for
2282 volatile arguments, though, at present, we still
2283 do a scan to check for TREE_SIDE_EFFECTS. */
2284 if (gimple_has_volatile_ops (s))
2285 return true;
2287 if (is_gimple_call (s))
2289 unsigned nargs = gimple_call_num_args (s);
2291 if (!(gimple_call_flags (s) & (ECF_CONST | ECF_PURE)))
2292 return true;
2293 else if (gimple_call_flags (s) & ECF_LOOPING_CONST_OR_PURE)
2294 /* An infinite loop is considered a side effect. */
2295 return true;
2297 if (gimple_call_lhs (s)
2298 && TREE_SIDE_EFFECTS (gimple_call_lhs (s)))
2300 gcc_assert (gimple_has_volatile_ops (s));
2301 return true;
2304 if (TREE_SIDE_EFFECTS (gimple_call_fn (s)))
2305 return true;
2307 for (i = 0; i < nargs; i++)
2308 if (TREE_SIDE_EFFECTS (gimple_call_arg (s, i)))
2310 gcc_assert (gimple_has_volatile_ops (s));
2311 return true;
2314 return false;
2316 else
2318 for (i = 0; i < gimple_num_ops (s); i++)
2319 if (TREE_SIDE_EFFECTS (gimple_op (s, i)))
2321 gcc_assert (gimple_has_volatile_ops (s));
2322 return true;
2326 return false;
2329 /* Return true if the RHS of statement S has side effects.
2330 We may use it to determine if it is admissable to replace
2331 an assignment or call with a copy of a previously-computed
2332 value. In such cases, side-effects due the the LHS are
2333 preserved. */
2335 bool
2336 gimple_rhs_has_side_effects (const_gimple s)
2338 unsigned i;
2340 if (is_gimple_call (s))
2342 unsigned nargs = gimple_call_num_args (s);
2344 if (!(gimple_call_flags (s) & (ECF_CONST | ECF_PURE)))
2345 return true;
2347 /* We cannot use gimple_has_volatile_ops here,
2348 because we must ignore a volatile LHS. */
2349 if (TREE_SIDE_EFFECTS (gimple_call_fn (s))
2350 || TREE_THIS_VOLATILE (gimple_call_fn (s)))
2352 gcc_assert (gimple_has_volatile_ops (s));
2353 return true;
2356 for (i = 0; i < nargs; i++)
2357 if (TREE_SIDE_EFFECTS (gimple_call_arg (s, i))
2358 || TREE_THIS_VOLATILE (gimple_call_arg (s, i)))
2359 return true;
2361 return false;
2363 else if (is_gimple_assign (s))
2365 /* Skip the first operand, the LHS. */
2366 for (i = 1; i < gimple_num_ops (s); i++)
2367 if (TREE_SIDE_EFFECTS (gimple_op (s, i))
2368 || TREE_THIS_VOLATILE (gimple_op (s, i)))
2370 gcc_assert (gimple_has_volatile_ops (s));
2371 return true;
2374 else if (is_gimple_debug (s))
2375 return false;
2376 else
2378 /* For statements without an LHS, examine all arguments. */
2379 for (i = 0; i < gimple_num_ops (s); i++)
2380 if (TREE_SIDE_EFFECTS (gimple_op (s, i))
2381 || TREE_THIS_VOLATILE (gimple_op (s, i)))
2383 gcc_assert (gimple_has_volatile_ops (s));
2384 return true;
2388 return false;
2391 /* Helper for gimple_could_trap_p and gimple_assign_rhs_could_trap_p.
2392 Return true if S can trap. When INCLUDE_MEM is true, check whether
2393 the memory operations could trap. When INCLUDE_STORES is true and
2394 S is a GIMPLE_ASSIGN, the LHS of the assignment is also checked. */
2396 bool
2397 gimple_could_trap_p_1 (gimple s, bool include_mem, bool include_stores)
2399 tree t, div = NULL_TREE;
2400 enum tree_code op;
2402 if (include_mem)
2404 unsigned i, start = (is_gimple_assign (s) && !include_stores) ? 1 : 0;
2406 for (i = start; i < gimple_num_ops (s); i++)
2407 if (tree_could_trap_p (gimple_op (s, i)))
2408 return true;
2411 switch (gimple_code (s))
2413 case GIMPLE_ASM:
2414 return gimple_asm_volatile_p (s);
2416 case GIMPLE_CALL:
2417 t = gimple_call_fndecl (s);
2418 /* Assume that calls to weak functions may trap. */
2419 if (!t || !DECL_P (t) || DECL_WEAK (t))
2420 return true;
2421 return false;
2423 case GIMPLE_ASSIGN:
2424 t = gimple_expr_type (s);
2425 op = gimple_assign_rhs_code (s);
2426 if (get_gimple_rhs_class (op) == GIMPLE_BINARY_RHS)
2427 div = gimple_assign_rhs2 (s);
2428 return (operation_could_trap_p (op, FLOAT_TYPE_P (t),
2429 (INTEGRAL_TYPE_P (t)
2430 && TYPE_OVERFLOW_TRAPS (t)),
2431 div));
2433 default:
2434 break;
2437 return false;
2440 /* Return true if statement S can trap. */
2442 bool
2443 gimple_could_trap_p (gimple s)
2445 return gimple_could_trap_p_1 (s, true, true);
2448 /* Return true if RHS of a GIMPLE_ASSIGN S can trap. */
2450 bool
2451 gimple_assign_rhs_could_trap_p (gimple s)
2453 gcc_assert (is_gimple_assign (s));
2454 return gimple_could_trap_p_1 (s, true, false);
2458 /* Print debugging information for gimple stmts generated. */
2460 void
2461 dump_gimple_statistics (void)
2463 #ifdef GATHER_STATISTICS
2464 int i, total_tuples = 0, total_bytes = 0;
2466 fprintf (stderr, "\nGIMPLE statements\n");
2467 fprintf (stderr, "Kind Stmts Bytes\n");
2468 fprintf (stderr, "---------------------------------------\n");
2469 for (i = 0; i < (int) gimple_alloc_kind_all; ++i)
2471 fprintf (stderr, "%-20s %7d %10d\n", gimple_alloc_kind_names[i],
2472 gimple_alloc_counts[i], gimple_alloc_sizes[i]);
2473 total_tuples += gimple_alloc_counts[i];
2474 total_bytes += gimple_alloc_sizes[i];
2476 fprintf (stderr, "---------------------------------------\n");
2477 fprintf (stderr, "%-20s %7d %10d\n", "Total", total_tuples, total_bytes);
2478 fprintf (stderr, "---------------------------------------\n");
2479 #else
2480 fprintf (stderr, "No gimple statistics\n");
2481 #endif
2485 /* Return the number of operands needed on the RHS of a GIMPLE
2486 assignment for an expression with tree code CODE. */
2488 unsigned
2489 get_gimple_rhs_num_ops (enum tree_code code)
2491 enum gimple_rhs_class rhs_class = get_gimple_rhs_class (code);
2493 if (rhs_class == GIMPLE_UNARY_RHS || rhs_class == GIMPLE_SINGLE_RHS)
2494 return 1;
2495 else if (rhs_class == GIMPLE_BINARY_RHS)
2496 return 2;
2497 else if (rhs_class == GIMPLE_TERNARY_RHS)
2498 return 3;
2499 else
2500 gcc_unreachable ();
2503 #define DEFTREECODE(SYM, STRING, TYPE, NARGS) \
2504 (unsigned char) \
2505 ((TYPE) == tcc_unary ? GIMPLE_UNARY_RHS \
2506 : ((TYPE) == tcc_binary \
2507 || (TYPE) == tcc_comparison) ? GIMPLE_BINARY_RHS \
2508 : ((TYPE) == tcc_constant \
2509 || (TYPE) == tcc_declaration \
2510 || (TYPE) == tcc_reference) ? GIMPLE_SINGLE_RHS \
2511 : ((SYM) == TRUTH_AND_EXPR \
2512 || (SYM) == TRUTH_OR_EXPR \
2513 || (SYM) == TRUTH_XOR_EXPR) ? GIMPLE_BINARY_RHS \
2514 : (SYM) == TRUTH_NOT_EXPR ? GIMPLE_UNARY_RHS \
2515 : ((SYM) == WIDEN_MULT_PLUS_EXPR \
2516 || (SYM) == WIDEN_MULT_MINUS_EXPR \
2517 || (SYM) == DOT_PROD_EXPR \
2518 || (SYM) == REALIGN_LOAD_EXPR \
2519 || (SYM) == FMA_EXPR) ? GIMPLE_TERNARY_RHS \
2520 : ((SYM) == COND_EXPR \
2521 || (SYM) == CONSTRUCTOR \
2522 || (SYM) == OBJ_TYPE_REF \
2523 || (SYM) == ASSERT_EXPR \
2524 || (SYM) == ADDR_EXPR \
2525 || (SYM) == WITH_SIZE_EXPR \
2526 || (SYM) == SSA_NAME \
2527 || (SYM) == VEC_COND_EXPR) ? GIMPLE_SINGLE_RHS \
2528 : GIMPLE_INVALID_RHS),
2529 #define END_OF_BASE_TREE_CODES (unsigned char) GIMPLE_INVALID_RHS,
2531 const unsigned char gimple_rhs_class_table[] = {
2532 #include "all-tree.def"
2535 #undef DEFTREECODE
2536 #undef END_OF_BASE_TREE_CODES
2538 /* For the definitive definition of GIMPLE, see doc/tree-ssa.texi. */
2540 /* Validation of GIMPLE expressions. */
2542 /* Returns true iff T is a valid RHS for an assignment to a renamed
2543 user -- or front-end generated artificial -- variable. */
2545 bool
2546 is_gimple_reg_rhs (tree t)
2548 return get_gimple_rhs_class (TREE_CODE (t)) != GIMPLE_INVALID_RHS;
2551 /* Returns true iff T is a valid RHS for an assignment to an un-renamed
2552 LHS, or for a call argument. */
2554 bool
2555 is_gimple_mem_rhs (tree t)
2557 /* If we're dealing with a renamable type, either source or dest must be
2558 a renamed variable. */
2559 if (is_gimple_reg_type (TREE_TYPE (t)))
2560 return is_gimple_val (t);
2561 else
2562 return is_gimple_val (t) || is_gimple_lvalue (t);
2565 /* Return true if T is a valid LHS for a GIMPLE assignment expression. */
2567 bool
2568 is_gimple_lvalue (tree t)
2570 return (is_gimple_addressable (t)
2571 || TREE_CODE (t) == WITH_SIZE_EXPR
2572 /* These are complex lvalues, but don't have addresses, so they
2573 go here. */
2574 || TREE_CODE (t) == BIT_FIELD_REF);
2577 /* Return true if T is a GIMPLE condition. */
2579 bool
2580 is_gimple_condexpr (tree t)
2582 return (is_gimple_val (t) || (COMPARISON_CLASS_P (t)
2583 && !tree_could_throw_p (t)
2584 && is_gimple_val (TREE_OPERAND (t, 0))
2585 && is_gimple_val (TREE_OPERAND (t, 1))));
2588 /* Return true if T is something whose address can be taken. */
2590 bool
2591 is_gimple_addressable (tree t)
2593 return (is_gimple_id (t) || handled_component_p (t)
2594 || TREE_CODE (t) == MEM_REF);
2597 /* Return true if T is a valid gimple constant. */
2599 bool
2600 is_gimple_constant (const_tree t)
2602 switch (TREE_CODE (t))
2604 case INTEGER_CST:
2605 case REAL_CST:
2606 case FIXED_CST:
2607 case STRING_CST:
2608 case COMPLEX_CST:
2609 case VECTOR_CST:
2610 return true;
2612 /* Vector constant constructors are gimple invariant. */
2613 case CONSTRUCTOR:
2614 if (TREE_TYPE (t) && TREE_CODE (TREE_TYPE (t)) == VECTOR_TYPE)
2615 return TREE_CONSTANT (t);
2616 else
2617 return false;
2619 default:
2620 return false;
2624 /* Return true if T is a gimple address. */
2626 bool
2627 is_gimple_address (const_tree t)
2629 tree op;
2631 if (TREE_CODE (t) != ADDR_EXPR)
2632 return false;
2634 op = TREE_OPERAND (t, 0);
2635 while (handled_component_p (op))
2637 if ((TREE_CODE (op) == ARRAY_REF
2638 || TREE_CODE (op) == ARRAY_RANGE_REF)
2639 && !is_gimple_val (TREE_OPERAND (op, 1)))
2640 return false;
2642 op = TREE_OPERAND (op, 0);
2645 if (CONSTANT_CLASS_P (op) || TREE_CODE (op) == MEM_REF)
2646 return true;
2648 switch (TREE_CODE (op))
2650 case PARM_DECL:
2651 case RESULT_DECL:
2652 case LABEL_DECL:
2653 case FUNCTION_DECL:
2654 case VAR_DECL:
2655 case CONST_DECL:
2656 return true;
2658 default:
2659 return false;
2663 /* Strip out all handled components that produce invariant
2664 offsets. */
2666 static const_tree
2667 strip_invariant_refs (const_tree op)
2669 while (handled_component_p (op))
2671 switch (TREE_CODE (op))
2673 case ARRAY_REF:
2674 case ARRAY_RANGE_REF:
2675 if (!is_gimple_constant (TREE_OPERAND (op, 1))
2676 || TREE_OPERAND (op, 2) != NULL_TREE
2677 || TREE_OPERAND (op, 3) != NULL_TREE)
2678 return NULL;
2679 break;
2681 case COMPONENT_REF:
2682 if (TREE_OPERAND (op, 2) != NULL_TREE)
2683 return NULL;
2684 break;
2686 default:;
2688 op = TREE_OPERAND (op, 0);
2691 return op;
2694 /* Return true if T is a gimple invariant address. */
2696 bool
2697 is_gimple_invariant_address (const_tree t)
2699 const_tree op;
2701 if (TREE_CODE (t) != ADDR_EXPR)
2702 return false;
2704 op = strip_invariant_refs (TREE_OPERAND (t, 0));
2705 if (!op)
2706 return false;
2708 if (TREE_CODE (op) == MEM_REF)
2710 const_tree op0 = TREE_OPERAND (op, 0);
2711 return (TREE_CODE (op0) == ADDR_EXPR
2712 && (CONSTANT_CLASS_P (TREE_OPERAND (op0, 0))
2713 || decl_address_invariant_p (TREE_OPERAND (op0, 0))));
2716 return CONSTANT_CLASS_P (op) || decl_address_invariant_p (op);
2719 /* Return true if T is a gimple invariant address at IPA level
2720 (so addresses of variables on stack are not allowed). */
2722 bool
2723 is_gimple_ip_invariant_address (const_tree t)
2725 const_tree op;
2727 if (TREE_CODE (t) != ADDR_EXPR)
2728 return false;
2730 op = strip_invariant_refs (TREE_OPERAND (t, 0));
2732 return op && (CONSTANT_CLASS_P (op) || decl_address_ip_invariant_p (op));
2735 /* Return true if T is a GIMPLE minimal invariant. It's a restricted
2736 form of function invariant. */
2738 bool
2739 is_gimple_min_invariant (const_tree t)
2741 if (TREE_CODE (t) == ADDR_EXPR)
2742 return is_gimple_invariant_address (t);
2744 return is_gimple_constant (t);
2747 /* Return true if T is a GIMPLE interprocedural invariant. It's a restricted
2748 form of gimple minimal invariant. */
2750 bool
2751 is_gimple_ip_invariant (const_tree t)
2753 if (TREE_CODE (t) == ADDR_EXPR)
2754 return is_gimple_ip_invariant_address (t);
2756 return is_gimple_constant (t);
2759 /* Return true if T looks like a valid GIMPLE statement. */
2761 bool
2762 is_gimple_stmt (tree t)
2764 const enum tree_code code = TREE_CODE (t);
2766 switch (code)
2768 case NOP_EXPR:
2769 /* The only valid NOP_EXPR is the empty statement. */
2770 return IS_EMPTY_STMT (t);
2772 case BIND_EXPR:
2773 case COND_EXPR:
2774 /* These are only valid if they're void. */
2775 return TREE_TYPE (t) == NULL || VOID_TYPE_P (TREE_TYPE (t));
2777 case SWITCH_EXPR:
2778 case GOTO_EXPR:
2779 case RETURN_EXPR:
2780 case LABEL_EXPR:
2781 case CASE_LABEL_EXPR:
2782 case TRY_CATCH_EXPR:
2783 case TRY_FINALLY_EXPR:
2784 case EH_FILTER_EXPR:
2785 case CATCH_EXPR:
2786 case ASM_EXPR:
2787 case STATEMENT_LIST:
2788 case OMP_PARALLEL:
2789 case OMP_FOR:
2790 case OMP_SECTIONS:
2791 case OMP_SECTION:
2792 case OMP_SINGLE:
2793 case OMP_MASTER:
2794 case OMP_ORDERED:
2795 case OMP_CRITICAL:
2796 case OMP_TASK:
2797 /* These are always void. */
2798 return true;
2800 case CALL_EXPR:
2801 case MODIFY_EXPR:
2802 case PREDICT_EXPR:
2803 /* These are valid regardless of their type. */
2804 return true;
2806 default:
2807 return false;
2811 /* Return true if T is a variable. */
2813 bool
2814 is_gimple_variable (tree t)
2816 return (TREE_CODE (t) == VAR_DECL
2817 || TREE_CODE (t) == PARM_DECL
2818 || TREE_CODE (t) == RESULT_DECL
2819 || TREE_CODE (t) == SSA_NAME);
2822 /* Return true if T is a GIMPLE identifier (something with an address). */
2824 bool
2825 is_gimple_id (tree t)
2827 return (is_gimple_variable (t)
2828 || TREE_CODE (t) == FUNCTION_DECL
2829 || TREE_CODE (t) == LABEL_DECL
2830 || TREE_CODE (t) == CONST_DECL
2831 /* Allow string constants, since they are addressable. */
2832 || TREE_CODE (t) == STRING_CST);
2835 /* Return true if TYPE is a suitable type for a scalar register variable. */
2837 bool
2838 is_gimple_reg_type (tree type)
2840 return !AGGREGATE_TYPE_P (type);
2843 /* Return true if T is a non-aggregate register variable. */
2845 bool
2846 is_gimple_reg (tree t)
2848 if (TREE_CODE (t) == SSA_NAME)
2849 t = SSA_NAME_VAR (t);
2851 if (!is_gimple_variable (t))
2852 return false;
2854 if (!is_gimple_reg_type (TREE_TYPE (t)))
2855 return false;
2857 /* A volatile decl is not acceptable because we can't reuse it as
2858 needed. We need to copy it into a temp first. */
2859 if (TREE_THIS_VOLATILE (t))
2860 return false;
2862 /* We define "registers" as things that can be renamed as needed,
2863 which with our infrastructure does not apply to memory. */
2864 if (needs_to_live_in_memory (t))
2865 return false;
2867 /* Hard register variables are an interesting case. For those that
2868 are call-clobbered, we don't know where all the calls are, since
2869 we don't (want to) take into account which operations will turn
2870 into libcalls at the rtl level. For those that are call-saved,
2871 we don't currently model the fact that calls may in fact change
2872 global hard registers, nor do we examine ASM_CLOBBERS at the tree
2873 level, and so miss variable changes that might imply. All around,
2874 it seems safest to not do too much optimization with these at the
2875 tree level at all. We'll have to rely on the rtl optimizers to
2876 clean this up, as there we've got all the appropriate bits exposed. */
2877 if (TREE_CODE (t) == VAR_DECL && DECL_HARD_REGISTER (t))
2878 return false;
2880 /* Complex and vector values must have been put into SSA-like form.
2881 That is, no assignments to the individual components. */
2882 if (TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE
2883 || TREE_CODE (TREE_TYPE (t)) == VECTOR_TYPE)
2884 return DECL_GIMPLE_REG_P (t);
2886 return true;
2890 /* Return true if T is a GIMPLE variable whose address is not needed. */
2892 bool
2893 is_gimple_non_addressable (tree t)
2895 if (TREE_CODE (t) == SSA_NAME)
2896 t = SSA_NAME_VAR (t);
2898 return (is_gimple_variable (t) && ! needs_to_live_in_memory (t));
2901 /* Return true if T is a GIMPLE rvalue, i.e. an identifier or a constant. */
2903 bool
2904 is_gimple_val (tree t)
2906 /* Make loads from volatiles and memory vars explicit. */
2907 if (is_gimple_variable (t)
2908 && is_gimple_reg_type (TREE_TYPE (t))
2909 && !is_gimple_reg (t))
2910 return false;
2912 return (is_gimple_variable (t) || is_gimple_min_invariant (t));
2915 /* Similarly, but accept hard registers as inputs to asm statements. */
2917 bool
2918 is_gimple_asm_val (tree t)
2920 if (TREE_CODE (t) == VAR_DECL && DECL_HARD_REGISTER (t))
2921 return true;
2923 return is_gimple_val (t);
2926 /* Return true if T is a GIMPLE minimal lvalue. */
2928 bool
2929 is_gimple_min_lval (tree t)
2931 if (!(t = CONST_CAST_TREE (strip_invariant_refs (t))))
2932 return false;
2933 return (is_gimple_id (t) || TREE_CODE (t) == MEM_REF);
2936 /* Return true if T is a valid function operand of a CALL_EXPR. */
2938 bool
2939 is_gimple_call_addr (tree t)
2941 return (TREE_CODE (t) == OBJ_TYPE_REF || is_gimple_val (t));
2944 /* Return true if T is a valid address operand of a MEM_REF. */
2946 bool
2947 is_gimple_mem_ref_addr (tree t)
2949 return (is_gimple_reg (t)
2950 || TREE_CODE (t) == INTEGER_CST
2951 || (TREE_CODE (t) == ADDR_EXPR
2952 && (CONSTANT_CLASS_P (TREE_OPERAND (t, 0))
2953 || decl_address_invariant_p (TREE_OPERAND (t, 0)))));
2956 /* If T makes a function call, return the corresponding CALL_EXPR operand.
2957 Otherwise, return NULL_TREE. */
2959 tree
2960 get_call_expr_in (tree t)
2962 if (TREE_CODE (t) == MODIFY_EXPR)
2963 t = TREE_OPERAND (t, 1);
2964 if (TREE_CODE (t) == WITH_SIZE_EXPR)
2965 t = TREE_OPERAND (t, 0);
2966 if (TREE_CODE (t) == CALL_EXPR)
2967 return t;
2968 return NULL_TREE;
2972 /* Given a memory reference expression T, return its base address.
2973 The base address of a memory reference expression is the main
2974 object being referenced. For instance, the base address for
2975 'array[i].fld[j]' is 'array'. You can think of this as stripping
2976 away the offset part from a memory address.
2978 This function calls handled_component_p to strip away all the inner
2979 parts of the memory reference until it reaches the base object. */
2981 tree
2982 get_base_address (tree t)
2984 while (handled_component_p (t))
2985 t = TREE_OPERAND (t, 0);
2987 if ((TREE_CODE (t) == MEM_REF
2988 || TREE_CODE (t) == TARGET_MEM_REF)
2989 && TREE_CODE (TREE_OPERAND (t, 0)) == ADDR_EXPR)
2990 t = TREE_OPERAND (TREE_OPERAND (t, 0), 0);
2992 if (TREE_CODE (t) == SSA_NAME
2993 || DECL_P (t)
2994 || TREE_CODE (t) == STRING_CST
2995 || TREE_CODE (t) == CONSTRUCTOR
2996 || INDIRECT_REF_P (t)
2997 || TREE_CODE (t) == MEM_REF
2998 || TREE_CODE (t) == TARGET_MEM_REF)
2999 return t;
3000 else
3001 return NULL_TREE;
3004 void
3005 recalculate_side_effects (tree t)
3007 enum tree_code code = TREE_CODE (t);
3008 int len = TREE_OPERAND_LENGTH (t);
3009 int i;
3011 switch (TREE_CODE_CLASS (code))
3013 case tcc_expression:
3014 switch (code)
3016 case INIT_EXPR:
3017 case MODIFY_EXPR:
3018 case VA_ARG_EXPR:
3019 case PREDECREMENT_EXPR:
3020 case PREINCREMENT_EXPR:
3021 case POSTDECREMENT_EXPR:
3022 case POSTINCREMENT_EXPR:
3023 /* All of these have side-effects, no matter what their
3024 operands are. */
3025 return;
3027 default:
3028 break;
3030 /* Fall through. */
3032 case tcc_comparison: /* a comparison expression */
3033 case tcc_unary: /* a unary arithmetic expression */
3034 case tcc_binary: /* a binary arithmetic expression */
3035 case tcc_reference: /* a reference */
3036 case tcc_vl_exp: /* a function call */
3037 TREE_SIDE_EFFECTS (t) = TREE_THIS_VOLATILE (t);
3038 for (i = 0; i < len; ++i)
3040 tree op = TREE_OPERAND (t, i);
3041 if (op && TREE_SIDE_EFFECTS (op))
3042 TREE_SIDE_EFFECTS (t) = 1;
3044 break;
3046 case tcc_constant:
3047 /* No side-effects. */
3048 return;
3050 default:
3051 gcc_unreachable ();
3055 /* Canonicalize a tree T for use in a COND_EXPR as conditional. Returns
3056 a canonicalized tree that is valid for a COND_EXPR or NULL_TREE, if
3057 we failed to create one. */
3059 tree
3060 canonicalize_cond_expr_cond (tree t)
3062 /* Strip conversions around boolean operations. */
3063 if (CONVERT_EXPR_P (t)
3064 && truth_value_p (TREE_CODE (TREE_OPERAND (t, 0))))
3065 t = TREE_OPERAND (t, 0);
3067 /* For (bool)x use x != 0. */
3068 if (CONVERT_EXPR_P (t)
3069 && TREE_CODE (TREE_TYPE (t)) == BOOLEAN_TYPE)
3071 tree top0 = TREE_OPERAND (t, 0);
3072 t = build2 (NE_EXPR, TREE_TYPE (t),
3073 top0, build_int_cst (TREE_TYPE (top0), 0));
3075 /* For !x use x == 0. */
3076 else if (TREE_CODE (t) == TRUTH_NOT_EXPR)
3078 tree top0 = TREE_OPERAND (t, 0);
3079 t = build2 (EQ_EXPR, TREE_TYPE (t),
3080 top0, build_int_cst (TREE_TYPE (top0), 0));
3082 /* For cmp ? 1 : 0 use cmp. */
3083 else if (TREE_CODE (t) == COND_EXPR
3084 && COMPARISON_CLASS_P (TREE_OPERAND (t, 0))
3085 && integer_onep (TREE_OPERAND (t, 1))
3086 && integer_zerop (TREE_OPERAND (t, 2)))
3088 tree top0 = TREE_OPERAND (t, 0);
3089 t = build2 (TREE_CODE (top0), TREE_TYPE (t),
3090 TREE_OPERAND (top0, 0), TREE_OPERAND (top0, 1));
3093 if (is_gimple_condexpr (t))
3094 return t;
3096 return NULL_TREE;
3099 /* Build a GIMPLE_CALL identical to STMT but skipping the arguments in
3100 the positions marked by the set ARGS_TO_SKIP. */
3102 gimple
3103 gimple_call_copy_skip_args (gimple stmt, bitmap args_to_skip)
3105 int i;
3106 tree fn = gimple_call_fn (stmt);
3107 int nargs = gimple_call_num_args (stmt);
3108 VEC(tree, heap) *vargs = VEC_alloc (tree, heap, nargs);
3109 gimple new_stmt;
3111 for (i = 0; i < nargs; i++)
3112 if (!bitmap_bit_p (args_to_skip, i))
3113 VEC_quick_push (tree, vargs, gimple_call_arg (stmt, i));
3115 new_stmt = gimple_build_call_vec (fn, vargs);
3116 VEC_free (tree, heap, vargs);
3117 if (gimple_call_lhs (stmt))
3118 gimple_call_set_lhs (new_stmt, gimple_call_lhs (stmt));
3120 gimple_set_vuse (new_stmt, gimple_vuse (stmt));
3121 gimple_set_vdef (new_stmt, gimple_vdef (stmt));
3123 gimple_set_block (new_stmt, gimple_block (stmt));
3124 if (gimple_has_location (stmt))
3125 gimple_set_location (new_stmt, gimple_location (stmt));
3126 gimple_call_copy_flags (new_stmt, stmt);
3127 gimple_call_set_chain (new_stmt, gimple_call_chain (stmt));
3129 gimple_set_modified (new_stmt, true);
3131 return new_stmt;
3135 static hashval_t gimple_type_hash_1 (const void *, enum gtc_mode);
3137 /* Structure used to maintain a cache of some type pairs compared by
3138 gimple_types_compatible_p when comparing aggregate types. There are
3139 three possible values for SAME_P:
3141 -2: The pair (T1, T2) has just been inserted in the table.
3142 0: T1 and T2 are different types.
3143 1: T1 and T2 are the same type.
3145 The two elements in the SAME_P array are indexed by the comparison
3146 mode gtc_mode. */
3148 struct type_pair_d
3150 unsigned int uid1;
3151 unsigned int uid2;
3152 signed char same_p[2];
3154 typedef struct type_pair_d *type_pair_t;
3156 DEF_VEC_P(type_pair_t);
3157 DEF_VEC_ALLOC_P(type_pair_t,heap);
3159 /* Return a hash value for the type pair pointed-to by P. */
3161 static hashval_t
3162 type_pair_hash (const void *p)
3164 const struct type_pair_d *pair = (const struct type_pair_d *) p;
3165 hashval_t val1 = pair->uid1;
3166 hashval_t val2 = pair->uid2;
3167 return (iterative_hash_hashval_t (val2, val1)
3168 ^ iterative_hash_hashval_t (val1, val2));
3171 /* Compare two type pairs pointed-to by P1 and P2. */
3173 static int
3174 type_pair_eq (const void *p1, const void *p2)
3176 const struct type_pair_d *pair1 = (const struct type_pair_d *) p1;
3177 const struct type_pair_d *pair2 = (const struct type_pair_d *) p2;
3178 return ((pair1->uid1 == pair2->uid1 && pair1->uid2 == pair2->uid2)
3179 || (pair1->uid1 == pair2->uid2 && pair1->uid2 == pair2->uid1));
3182 /* Lookup the pair of types T1 and T2 in *VISITED_P. Insert a new
3183 entry if none existed. */
3185 static type_pair_t
3186 lookup_type_pair (tree t1, tree t2, htab_t *visited_p, struct obstack *ob_p)
3188 struct type_pair_d pair;
3189 type_pair_t p;
3190 void **slot;
3192 if (*visited_p == NULL)
3194 *visited_p = htab_create (251, type_pair_hash, type_pair_eq, NULL);
3195 gcc_obstack_init (ob_p);
3198 pair.uid1 = TYPE_UID (t1);
3199 pair.uid2 = TYPE_UID (t2);
3200 slot = htab_find_slot (*visited_p, &pair, INSERT);
3202 if (*slot)
3203 p = *((type_pair_t *) slot);
3204 else
3206 p = XOBNEW (ob_p, struct type_pair_d);
3207 p->uid1 = TYPE_UID (t1);
3208 p->uid2 = TYPE_UID (t2);
3209 p->same_p[0] = -2;
3210 p->same_p[1] = -2;
3211 *slot = (void *) p;
3214 return p;
3217 /* Per pointer state for the SCC finding. The on_sccstack flag
3218 is not strictly required, it is true when there is no hash value
3219 recorded for the type and false otherwise. But querying that
3220 is slower. */
3222 struct sccs
3224 unsigned int dfsnum;
3225 unsigned int low;
3226 bool on_sccstack;
3227 union {
3228 hashval_t hash;
3229 signed char same_p;
3230 } u;
3233 static unsigned int next_dfs_num;
3234 static unsigned int gtc_next_dfs_num;
3237 /* GIMPLE type merging cache. A direct-mapped cache based on TYPE_UID. */
3239 typedef struct GTY(()) gimple_type_leader_entry_s {
3240 tree type;
3241 tree leader;
3242 } gimple_type_leader_entry;
3244 #define GIMPLE_TYPE_LEADER_SIZE 16381
3245 static GTY((length("GIMPLE_TYPE_LEADER_SIZE"))) gimple_type_leader_entry
3246 *gimple_type_leader;
3248 /* Lookup an existing leader for T and return it or NULL_TREE, if
3249 there is none in the cache. */
3251 static tree
3252 gimple_lookup_type_leader (tree t)
3254 gimple_type_leader_entry *leader;
3256 if (!gimple_type_leader)
3257 return NULL_TREE;
3259 leader = &gimple_type_leader[TYPE_UID (t) % GIMPLE_TYPE_LEADER_SIZE];
3260 if (leader->type != t)
3261 return NULL_TREE;
3263 return leader->leader;
3266 /* Return true if T1 and T2 have the same name. If FOR_COMPLETION_P is
3267 true then if any type has no name return false, otherwise return
3268 true if both types have no names. */
3270 static bool
3271 compare_type_names_p (tree t1, tree t2, bool for_completion_p)
3273 tree name1 = TYPE_NAME (t1);
3274 tree name2 = TYPE_NAME (t2);
3276 /* Consider anonymous types all unique for completion. */
3277 if (for_completion_p
3278 && (!name1 || !name2))
3279 return false;
3281 if (name1 && TREE_CODE (name1) == TYPE_DECL)
3283 name1 = DECL_NAME (name1);
3284 if (for_completion_p
3285 && !name1)
3286 return false;
3288 gcc_assert (!name1 || TREE_CODE (name1) == IDENTIFIER_NODE);
3290 if (name2 && TREE_CODE (name2) == TYPE_DECL)
3292 name2 = DECL_NAME (name2);
3293 if (for_completion_p
3294 && !name2)
3295 return false;
3297 gcc_assert (!name2 || TREE_CODE (name2) == IDENTIFIER_NODE);
3299 /* Identifiers can be compared with pointer equality rather
3300 than a string comparison. */
3301 if (name1 == name2)
3302 return true;
3304 return false;
3307 /* Return true if the field decls F1 and F2 are at the same offset.
3309 This is intended to be used on GIMPLE types only. In order to
3310 compare GENERIC types, use fields_compatible_p instead. */
3312 bool
3313 gimple_compare_field_offset (tree f1, tree f2)
3315 if (DECL_OFFSET_ALIGN (f1) == DECL_OFFSET_ALIGN (f2))
3317 tree offset1 = DECL_FIELD_OFFSET (f1);
3318 tree offset2 = DECL_FIELD_OFFSET (f2);
3319 return ((offset1 == offset2
3320 /* Once gimplification is done, self-referential offsets are
3321 instantiated as operand #2 of the COMPONENT_REF built for
3322 each access and reset. Therefore, they are not relevant
3323 anymore and fields are interchangeable provided that they
3324 represent the same access. */
3325 || (TREE_CODE (offset1) == PLACEHOLDER_EXPR
3326 && TREE_CODE (offset2) == PLACEHOLDER_EXPR
3327 && (DECL_SIZE (f1) == DECL_SIZE (f2)
3328 || (TREE_CODE (DECL_SIZE (f1)) == PLACEHOLDER_EXPR
3329 && TREE_CODE (DECL_SIZE (f2)) == PLACEHOLDER_EXPR)
3330 || operand_equal_p (DECL_SIZE (f1), DECL_SIZE (f2), 0))
3331 && DECL_ALIGN (f1) == DECL_ALIGN (f2))
3332 || operand_equal_p (offset1, offset2, 0))
3333 && tree_int_cst_equal (DECL_FIELD_BIT_OFFSET (f1),
3334 DECL_FIELD_BIT_OFFSET (f2)));
3337 /* Fortran and C do not always agree on what DECL_OFFSET_ALIGN
3338 should be, so handle differing ones specially by decomposing
3339 the offset into a byte and bit offset manually. */
3340 if (host_integerp (DECL_FIELD_OFFSET (f1), 0)
3341 && host_integerp (DECL_FIELD_OFFSET (f2), 0))
3343 unsigned HOST_WIDE_INT byte_offset1, byte_offset2;
3344 unsigned HOST_WIDE_INT bit_offset1, bit_offset2;
3345 bit_offset1 = TREE_INT_CST_LOW (DECL_FIELD_BIT_OFFSET (f1));
3346 byte_offset1 = (TREE_INT_CST_LOW (DECL_FIELD_OFFSET (f1))
3347 + bit_offset1 / BITS_PER_UNIT);
3348 bit_offset2 = TREE_INT_CST_LOW (DECL_FIELD_BIT_OFFSET (f2));
3349 byte_offset2 = (TREE_INT_CST_LOW (DECL_FIELD_OFFSET (f2))
3350 + bit_offset2 / BITS_PER_UNIT);
3351 if (byte_offset1 != byte_offset2)
3352 return false;
3353 return bit_offset1 % BITS_PER_UNIT == bit_offset2 % BITS_PER_UNIT;
3356 return false;
3359 /* If the type T1 and the type T2 are a complete and an incomplete
3360 variant of the same type return true. */
3362 static bool
3363 gimple_compatible_complete_and_incomplete_subtype_p (tree t1, tree t2)
3365 /* If one pointer points to an incomplete type variant of
3366 the other pointed-to type they are the same. */
3367 if (TREE_CODE (t1) == TREE_CODE (t2)
3368 && RECORD_OR_UNION_TYPE_P (t1)
3369 && (!COMPLETE_TYPE_P (t1)
3370 || !COMPLETE_TYPE_P (t2))
3371 && TYPE_QUALS (t1) == TYPE_QUALS (t2)
3372 && compare_type_names_p (TYPE_MAIN_VARIANT (t1),
3373 TYPE_MAIN_VARIANT (t2), true))
3374 return true;
3375 return false;
3378 static bool
3379 gimple_types_compatible_p_1 (tree, tree, enum gtc_mode, type_pair_t,
3380 VEC(type_pair_t, heap) **,
3381 struct pointer_map_t *, struct obstack *);
3383 /* DFS visit the edge from the callers type pair with state *STATE to
3384 the pair T1, T2 while operating in FOR_MERGING_P mode.
3385 Update the merging status if it is not part of the SCC containing the
3386 callers pair and return it.
3387 SCCSTACK, SCCSTATE and SCCSTATE_OBSTACK are state for the DFS walk done. */
3389 static bool
3390 gtc_visit (tree t1, tree t2, enum gtc_mode mode,
3391 struct sccs *state,
3392 VEC(type_pair_t, heap) **sccstack,
3393 struct pointer_map_t *sccstate,
3394 struct obstack *sccstate_obstack)
3396 struct sccs *cstate = NULL;
3397 type_pair_t p;
3398 void **slot;
3400 /* Check first for the obvious case of pointer identity. */
3401 if (t1 == t2)
3402 return true;
3404 /* Check that we have two types to compare. */
3405 if (t1 == NULL_TREE || t2 == NULL_TREE)
3406 return false;
3408 /* If the types have been previously registered and found equal
3409 they still are. */
3410 if (mode == GTC_MERGE)
3412 tree leader1 = gimple_lookup_type_leader (t1);
3413 tree leader2 = gimple_lookup_type_leader (t2);
3414 if (leader1 == t2
3415 || t1 == leader2
3416 || (leader1 && leader1 == leader2))
3417 return true;
3419 else if (mode == GTC_DIAG)
3421 if (TYPE_CANONICAL (t1)
3422 && TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2))
3423 return true;
3426 /* Can't be the same type if the types don't have the same code. */
3427 if (TREE_CODE (t1) != TREE_CODE (t2))
3428 return false;
3430 /* Can't be the same type if they have different CV qualifiers. */
3431 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
3432 return false;
3434 /* Void types are always the same. */
3435 if (TREE_CODE (t1) == VOID_TYPE)
3436 return true;
3438 /* Do some simple checks before doing three hashtable queries. */
3439 if (INTEGRAL_TYPE_P (t1)
3440 || SCALAR_FLOAT_TYPE_P (t1)
3441 || FIXED_POINT_TYPE_P (t1)
3442 || TREE_CODE (t1) == VECTOR_TYPE
3443 || TREE_CODE (t1) == COMPLEX_TYPE
3444 || TREE_CODE (t1) == OFFSET_TYPE)
3446 /* Can't be the same type if they have different alignment,
3447 sign, precision or mode. */
3448 if (TYPE_ALIGN (t1) != TYPE_ALIGN (t2)
3449 || TYPE_PRECISION (t1) != TYPE_PRECISION (t2)
3450 || TYPE_MODE (t1) != TYPE_MODE (t2)
3451 || TYPE_UNSIGNED (t1) != TYPE_UNSIGNED (t2))
3452 return false;
3454 if (TREE_CODE (t1) == INTEGER_TYPE
3455 && (TYPE_IS_SIZETYPE (t1) != TYPE_IS_SIZETYPE (t2)
3456 || TYPE_STRING_FLAG (t1) != TYPE_STRING_FLAG (t2)))
3457 return false;
3459 /* That's all we need to check for float and fixed-point types. */
3460 if (SCALAR_FLOAT_TYPE_P (t1)
3461 || FIXED_POINT_TYPE_P (t1))
3462 return true;
3464 /* For integral types fall thru to more complex checks. */
3467 else if (AGGREGATE_TYPE_P (t1) || POINTER_TYPE_P (t1))
3469 /* Can't be the same type if they have different alignment or mode. */
3470 if (TYPE_ALIGN (t1) != TYPE_ALIGN (t2)
3471 || TYPE_MODE (t1) != TYPE_MODE (t2))
3472 return false;
3475 /* If the hash values of t1 and t2 are different the types can't
3476 possibly be the same. This helps keeping the type-pair hashtable
3477 small, only tracking comparisons for hash collisions. */
3478 if (gimple_type_hash_1 (t1, mode) != gimple_type_hash_1 (t2, mode))
3479 return false;
3481 /* Allocate a new cache entry for this comparison. */
3482 p = lookup_type_pair (t1, t2, &gtc_visited, &gtc_ob);
3483 if (p->same_p[mode] == 0 || p->same_p[mode] == 1)
3485 /* We have already decided whether T1 and T2 are the
3486 same, return the cached result. */
3487 return p->same_p[mode] == 1;
3490 if ((slot = pointer_map_contains (sccstate, p)) != NULL)
3491 cstate = (struct sccs *)*slot;
3492 /* Not yet visited. DFS recurse. */
3493 if (!cstate)
3495 gimple_types_compatible_p_1 (t1, t2, mode, p,
3496 sccstack, sccstate, sccstate_obstack);
3497 cstate = (struct sccs *)* pointer_map_contains (sccstate, p);
3498 state->low = MIN (state->low, cstate->low);
3500 /* If the type is still on the SCC stack adjust the parents low. */
3501 if (cstate->dfsnum < state->dfsnum
3502 && cstate->on_sccstack)
3503 state->low = MIN (cstate->dfsnum, state->low);
3505 /* Return the current lattice value. We start with an equality
3506 assumption so types part of a SCC will be optimistically
3507 treated equal unless proven otherwise. */
3508 return cstate->u.same_p;
3511 /* Worker for gimple_types_compatible.
3512 SCCSTACK, SCCSTATE and SCCSTATE_OBSTACK are state for the DFS walk done. */
3514 static bool
3515 gimple_types_compatible_p_1 (tree t1, tree t2, enum gtc_mode mode,
3516 type_pair_t p,
3517 VEC(type_pair_t, heap) **sccstack,
3518 struct pointer_map_t *sccstate,
3519 struct obstack *sccstate_obstack)
3521 struct sccs *state;
3523 gcc_assert (p->same_p[mode] == -2);
3525 state = XOBNEW (sccstate_obstack, struct sccs);
3526 *pointer_map_insert (sccstate, p) = state;
3528 VEC_safe_push (type_pair_t, heap, *sccstack, p);
3529 state->dfsnum = gtc_next_dfs_num++;
3530 state->low = state->dfsnum;
3531 state->on_sccstack = true;
3532 /* Start with an equality assumption. As we DFS recurse into child
3533 SCCs this assumption may get revisited. */
3534 state->u.same_p = 1;
3536 /* If their attributes are not the same they can't be the same type. */
3537 if (!attribute_list_equal (TYPE_ATTRIBUTES (t1), TYPE_ATTRIBUTES (t2)))
3538 goto different_types;
3540 /* Do type-specific comparisons. */
3541 switch (TREE_CODE (t1))
3543 case VECTOR_TYPE:
3544 case COMPLEX_TYPE:
3545 if (!gtc_visit (TREE_TYPE (t1), TREE_TYPE (t2), mode,
3546 state, sccstack, sccstate, sccstate_obstack))
3547 goto different_types;
3548 goto same_types;
3550 case ARRAY_TYPE:
3551 /* Array types are the same if the element types are the same and
3552 the number of elements are the same. */
3553 if (!gtc_visit (TREE_TYPE (t1), TREE_TYPE (t2), mode,
3554 state, sccstack, sccstate, sccstate_obstack)
3555 || TYPE_STRING_FLAG (t1) != TYPE_STRING_FLAG (t2)
3556 || TYPE_NONALIASED_COMPONENT (t1) != TYPE_NONALIASED_COMPONENT (t2))
3557 goto different_types;
3558 else
3560 tree i1 = TYPE_DOMAIN (t1);
3561 tree i2 = TYPE_DOMAIN (t2);
3563 /* For an incomplete external array, the type domain can be
3564 NULL_TREE. Check this condition also. */
3565 if (i1 == NULL_TREE && i2 == NULL_TREE)
3566 goto same_types;
3567 else if (i1 == NULL_TREE || i2 == NULL_TREE)
3568 goto different_types;
3569 /* If for a complete array type the possibly gimplified sizes
3570 are different the types are different. */
3571 else if (((TYPE_SIZE (i1) != NULL) ^ (TYPE_SIZE (i2) != NULL))
3572 || (TYPE_SIZE (i1)
3573 && TYPE_SIZE (i2)
3574 && !operand_equal_p (TYPE_SIZE (i1), TYPE_SIZE (i2), 0)))
3575 goto different_types;
3576 else
3578 tree min1 = TYPE_MIN_VALUE (i1);
3579 tree min2 = TYPE_MIN_VALUE (i2);
3580 tree max1 = TYPE_MAX_VALUE (i1);
3581 tree max2 = TYPE_MAX_VALUE (i2);
3583 /* The minimum/maximum values have to be the same. */
3584 if ((min1 == min2
3585 || (min1 && min2
3586 && ((TREE_CODE (min1) == PLACEHOLDER_EXPR
3587 && TREE_CODE (min2) == PLACEHOLDER_EXPR)
3588 || operand_equal_p (min1, min2, 0))))
3589 && (max1 == max2
3590 || (max1 && max2
3591 && ((TREE_CODE (max1) == PLACEHOLDER_EXPR
3592 && TREE_CODE (max2) == PLACEHOLDER_EXPR)
3593 || operand_equal_p (max1, max2, 0)))))
3594 goto same_types;
3595 else
3596 goto different_types;
3600 case METHOD_TYPE:
3601 /* Method types should belong to the same class. */
3602 if (!gtc_visit (TYPE_METHOD_BASETYPE (t1), TYPE_METHOD_BASETYPE (t2),
3603 mode, state, sccstack, sccstate, sccstate_obstack))
3604 goto different_types;
3606 /* Fallthru */
3608 case FUNCTION_TYPE:
3609 /* Function types are the same if the return type and arguments types
3610 are the same. */
3611 if ((mode != GTC_DIAG
3612 || !gimple_compatible_complete_and_incomplete_subtype_p
3613 (TREE_TYPE (t1), TREE_TYPE (t2)))
3614 && !gtc_visit (TREE_TYPE (t1), TREE_TYPE (t2), mode,
3615 state, sccstack, sccstate, sccstate_obstack))
3616 goto different_types;
3618 if (!comp_type_attributes (t1, t2))
3619 goto different_types;
3621 if (TYPE_ARG_TYPES (t1) == TYPE_ARG_TYPES (t2))
3622 goto same_types;
3623 else
3625 tree parms1, parms2;
3627 for (parms1 = TYPE_ARG_TYPES (t1), parms2 = TYPE_ARG_TYPES (t2);
3628 parms1 && parms2;
3629 parms1 = TREE_CHAIN (parms1), parms2 = TREE_CHAIN (parms2))
3631 if ((mode == GTC_MERGE
3632 || !gimple_compatible_complete_and_incomplete_subtype_p
3633 (TREE_VALUE (parms1), TREE_VALUE (parms2)))
3634 && !gtc_visit (TREE_VALUE (parms1), TREE_VALUE (parms2), mode,
3635 state, sccstack, sccstate, sccstate_obstack))
3636 goto different_types;
3639 if (parms1 || parms2)
3640 goto different_types;
3642 goto same_types;
3645 case OFFSET_TYPE:
3647 if (!gtc_visit (TREE_TYPE (t1), TREE_TYPE (t2), mode,
3648 state, sccstack, sccstate, sccstate_obstack)
3649 || !gtc_visit (TYPE_OFFSET_BASETYPE (t1),
3650 TYPE_OFFSET_BASETYPE (t2), mode,
3651 state, sccstack, sccstate, sccstate_obstack))
3652 goto different_types;
3654 goto same_types;
3657 case POINTER_TYPE:
3658 case REFERENCE_TYPE:
3660 /* If the two pointers have different ref-all attributes,
3661 they can't be the same type. */
3662 if (TYPE_REF_CAN_ALIAS_ALL (t1) != TYPE_REF_CAN_ALIAS_ALL (t2))
3663 goto different_types;
3665 /* If one pointer points to an incomplete type variant of
3666 the other pointed-to type they are the same. */
3667 if (mode == GTC_DIAG
3668 && gimple_compatible_complete_and_incomplete_subtype_p
3669 (TREE_TYPE (t1), TREE_TYPE (t2)))
3670 goto same_types;
3672 /* Otherwise, pointer and reference types are the same if the
3673 pointed-to types are the same. */
3674 if (gtc_visit (TREE_TYPE (t1), TREE_TYPE (t2), mode,
3675 state, sccstack, sccstate, sccstate_obstack))
3676 goto same_types;
3678 goto different_types;
3681 case NULLPTR_TYPE:
3682 /* There is only one decltype(nullptr). */
3683 goto same_types;
3685 case INTEGER_TYPE:
3686 case BOOLEAN_TYPE:
3688 tree min1 = TYPE_MIN_VALUE (t1);
3689 tree max1 = TYPE_MAX_VALUE (t1);
3690 tree min2 = TYPE_MIN_VALUE (t2);
3691 tree max2 = TYPE_MAX_VALUE (t2);
3692 bool min_equal_p = false;
3693 bool max_equal_p = false;
3695 /* If either type has a minimum value, the other type must
3696 have the same. */
3697 if (min1 == NULL_TREE && min2 == NULL_TREE)
3698 min_equal_p = true;
3699 else if (min1 && min2 && operand_equal_p (min1, min2, 0))
3700 min_equal_p = true;
3702 /* Likewise, if either type has a maximum value, the other
3703 type must have the same. */
3704 if (max1 == NULL_TREE && max2 == NULL_TREE)
3705 max_equal_p = true;
3706 else if (max1 && max2 && operand_equal_p (max1, max2, 0))
3707 max_equal_p = true;
3709 if (!min_equal_p || !max_equal_p)
3710 goto different_types;
3712 goto same_types;
3715 case ENUMERAL_TYPE:
3717 /* FIXME lto, we cannot check bounds on enumeral types because
3718 different front ends will produce different values.
3719 In C, enumeral types are integers, while in C++ each element
3720 will have its own symbolic value. We should decide how enums
3721 are to be represented in GIMPLE and have each front end lower
3722 to that. */
3723 tree v1, v2;
3725 /* For enumeral types, all the values must be the same. */
3726 if (TYPE_VALUES (t1) == TYPE_VALUES (t2))
3727 goto same_types;
3729 for (v1 = TYPE_VALUES (t1), v2 = TYPE_VALUES (t2);
3730 v1 && v2;
3731 v1 = TREE_CHAIN (v1), v2 = TREE_CHAIN (v2))
3733 tree c1 = TREE_VALUE (v1);
3734 tree c2 = TREE_VALUE (v2);
3736 if (TREE_CODE (c1) == CONST_DECL)
3737 c1 = DECL_INITIAL (c1);
3739 if (TREE_CODE (c2) == CONST_DECL)
3740 c2 = DECL_INITIAL (c2);
3742 if (tree_int_cst_equal (c1, c2) != 1)
3743 goto different_types;
3746 /* If one enumeration has more values than the other, they
3747 are not the same. */
3748 if (v1 || v2)
3749 goto different_types;
3751 goto same_types;
3754 case RECORD_TYPE:
3755 case UNION_TYPE:
3756 case QUAL_UNION_TYPE:
3758 tree f1, f2;
3760 /* The struct tags shall compare equal. */
3761 if (mode == GTC_MERGE
3762 && !compare_type_names_p (TYPE_MAIN_VARIANT (t1),
3763 TYPE_MAIN_VARIANT (t2), false))
3764 goto different_types;
3766 /* For aggregate types, all the fields must be the same. */
3767 for (f1 = TYPE_FIELDS (t1), f2 = TYPE_FIELDS (t2);
3768 f1 && f2;
3769 f1 = TREE_CHAIN (f1), f2 = TREE_CHAIN (f2))
3771 /* The fields must have the same name, offset and type. */
3772 if ((mode == GTC_MERGE
3773 && DECL_NAME (f1) != DECL_NAME (f2))
3774 || DECL_NONADDRESSABLE_P (f1) != DECL_NONADDRESSABLE_P (f2)
3775 || !gimple_compare_field_offset (f1, f2)
3776 || !gtc_visit (TREE_TYPE (f1), TREE_TYPE (f2), mode,
3777 state, sccstack, sccstate, sccstate_obstack))
3778 goto different_types;
3781 /* If one aggregate has more fields than the other, they
3782 are not the same. */
3783 if (f1 || f2)
3784 goto different_types;
3786 goto same_types;
3789 default:
3790 gcc_unreachable ();
3793 /* Common exit path for types that are not compatible. */
3794 different_types:
3795 state->u.same_p = 0;
3796 goto pop;
3798 /* Common exit path for types that are compatible. */
3799 same_types:
3800 gcc_assert (state->u.same_p == 1);
3802 pop:
3803 if (state->low == state->dfsnum)
3805 type_pair_t x;
3807 /* Pop off the SCC and set its cache values to the final
3808 comparison result. */
3811 struct sccs *cstate;
3812 x = VEC_pop (type_pair_t, *sccstack);
3813 cstate = (struct sccs *)*pointer_map_contains (sccstate, x);
3814 cstate->on_sccstack = false;
3815 x->same_p[mode] = state->u.same_p;
3817 while (x != p);
3820 return state->u.same_p;
3823 /* Return true iff T1 and T2 are structurally identical. When
3824 FOR_MERGING_P is true the an incomplete type and a complete type
3825 are considered different, otherwise they are considered compatible. */
3827 bool
3828 gimple_types_compatible_p (tree t1, tree t2, enum gtc_mode mode)
3830 VEC(type_pair_t, heap) *sccstack = NULL;
3831 struct pointer_map_t *sccstate;
3832 struct obstack sccstate_obstack;
3833 type_pair_t p = NULL;
3834 bool res;
3836 /* Before starting to set up the SCC machinery handle simple cases. */
3838 /* Check first for the obvious case of pointer identity. */
3839 if (t1 == t2)
3840 return true;
3842 /* Check that we have two types to compare. */
3843 if (t1 == NULL_TREE || t2 == NULL_TREE)
3844 return false;
3846 /* If the types have been previously registered and found equal
3847 they still are. */
3848 if (mode == GTC_MERGE)
3850 tree leader1 = gimple_lookup_type_leader (t1);
3851 tree leader2 = gimple_lookup_type_leader (t2);
3852 if (leader1 == t2
3853 || t1 == leader2
3854 || (leader1 && leader1 == leader2))
3855 return true;
3857 else if (mode == GTC_DIAG)
3859 if (TYPE_CANONICAL (t1)
3860 && TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2))
3861 return true;
3864 /* Can't be the same type if the types don't have the same code. */
3865 if (TREE_CODE (t1) != TREE_CODE (t2))
3866 return false;
3868 /* Can't be the same type if they have different CV qualifiers. */
3869 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
3870 return false;
3872 /* Void types are always the same. */
3873 if (TREE_CODE (t1) == VOID_TYPE)
3874 return true;
3876 /* Do some simple checks before doing three hashtable queries. */
3877 if (INTEGRAL_TYPE_P (t1)
3878 || SCALAR_FLOAT_TYPE_P (t1)
3879 || FIXED_POINT_TYPE_P (t1)
3880 || TREE_CODE (t1) == VECTOR_TYPE
3881 || TREE_CODE (t1) == COMPLEX_TYPE
3882 || TREE_CODE (t1) == OFFSET_TYPE)
3884 /* Can't be the same type if they have different alignment,
3885 sign, precision or mode. */
3886 if (TYPE_ALIGN (t1) != TYPE_ALIGN (t2)
3887 || TYPE_PRECISION (t1) != TYPE_PRECISION (t2)
3888 || TYPE_MODE (t1) != TYPE_MODE (t2)
3889 || TYPE_UNSIGNED (t1) != TYPE_UNSIGNED (t2))
3890 return false;
3892 if (TREE_CODE (t1) == INTEGER_TYPE
3893 && (TYPE_IS_SIZETYPE (t1) != TYPE_IS_SIZETYPE (t2)
3894 || TYPE_STRING_FLAG (t1) != TYPE_STRING_FLAG (t2)))
3895 return false;
3897 /* That's all we need to check for float and fixed-point types. */
3898 if (SCALAR_FLOAT_TYPE_P (t1)
3899 || FIXED_POINT_TYPE_P (t1))
3900 return true;
3902 /* For integral types fall thru to more complex checks. */
3905 else if (AGGREGATE_TYPE_P (t1) || POINTER_TYPE_P (t1))
3907 /* Can't be the same type if they have different alignment or mode. */
3908 if (TYPE_ALIGN (t1) != TYPE_ALIGN (t2)
3909 || TYPE_MODE (t1) != TYPE_MODE (t2))
3910 return false;
3913 /* If the hash values of t1 and t2 are different the types can't
3914 possibly be the same. This helps keeping the type-pair hashtable
3915 small, only tracking comparisons for hash collisions. */
3916 if (gimple_type_hash_1 (t1, mode) != gimple_type_hash_1 (t2, mode))
3917 return false;
3919 /* If we've visited this type pair before (in the case of aggregates
3920 with self-referential types), and we made a decision, return it. */
3921 p = lookup_type_pair (t1, t2, &gtc_visited, &gtc_ob);
3922 if (p->same_p[mode] == 0 || p->same_p[mode] == 1)
3924 /* We have already decided whether T1 and T2 are the
3925 same, return the cached result. */
3926 return p->same_p[mode] == 1;
3929 /* Now set up the SCC machinery for the comparison. */
3930 gtc_next_dfs_num = 1;
3931 sccstate = pointer_map_create ();
3932 gcc_obstack_init (&sccstate_obstack);
3933 res = gimple_types_compatible_p_1 (t1, t2, mode, p,
3934 &sccstack, sccstate, &sccstate_obstack);
3935 VEC_free (type_pair_t, heap, sccstack);
3936 pointer_map_destroy (sccstate);
3937 obstack_free (&sccstate_obstack, NULL);
3939 return res;
3943 static hashval_t
3944 iterative_hash_gimple_type (tree, hashval_t, VEC(tree, heap) **,
3945 struct pointer_map_t *, struct obstack *,
3946 enum gtc_mode);
3948 /* DFS visit the edge from the callers type with state *STATE to T.
3949 Update the callers type hash V with the hash for T if it is not part
3950 of the SCC containing the callers type and return it.
3951 SCCSTACK, SCCSTATE and SCCSTATE_OBSTACK are state for the DFS walk done. */
3953 static hashval_t
3954 visit (tree t, struct sccs *state, hashval_t v,
3955 VEC (tree, heap) **sccstack,
3956 struct pointer_map_t *sccstate,
3957 struct obstack *sccstate_obstack, enum gtc_mode mode)
3959 struct sccs *cstate = NULL;
3960 struct tree_int_map m;
3961 void **slot;
3963 /* If there is a hash value recorded for this type then it can't
3964 possibly be part of our parent SCC. Simply mix in its hash. */
3965 m.base.from = t;
3966 if ((slot = htab_find_slot (mode == GTC_MERGE
3967 ? type_hash_cache : canonical_type_hash_cache,
3968 &m, NO_INSERT))
3969 && *slot)
3970 return iterative_hash_hashval_t (((struct tree_int_map *) *slot)->to, v);
3972 if ((slot = pointer_map_contains (sccstate, t)) != NULL)
3973 cstate = (struct sccs *)*slot;
3974 if (!cstate)
3976 hashval_t tem;
3977 /* Not yet visited. DFS recurse. */
3978 tem = iterative_hash_gimple_type (t, v,
3979 sccstack, sccstate, sccstate_obstack,
3980 mode);
3981 if (!cstate)
3982 cstate = (struct sccs *)* pointer_map_contains (sccstate, t);
3983 state->low = MIN (state->low, cstate->low);
3984 /* If the type is no longer on the SCC stack and thus is not part
3985 of the parents SCC mix in its hash value. Otherwise we will
3986 ignore the type for hashing purposes and return the unaltered
3987 hash value. */
3988 if (!cstate->on_sccstack)
3989 return tem;
3991 if (cstate->dfsnum < state->dfsnum
3992 && cstate->on_sccstack)
3993 state->low = MIN (cstate->dfsnum, state->low);
3995 /* We are part of our parents SCC, skip this type during hashing
3996 and return the unaltered hash value. */
3997 return v;
4000 /* Hash NAME with the previous hash value V and return it. */
4002 static hashval_t
4003 iterative_hash_name (tree name, hashval_t v)
4005 if (!name)
4006 return v;
4007 if (TREE_CODE (name) == TYPE_DECL)
4008 name = DECL_NAME (name);
4009 if (!name)
4010 return v;
4011 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
4012 return iterative_hash_object (IDENTIFIER_HASH_VALUE (name), v);
4015 /* Returning a hash value for gimple type TYPE combined with VAL.
4016 SCCSTACK, SCCSTATE and SCCSTATE_OBSTACK are state for the DFS walk done.
4018 To hash a type we end up hashing in types that are reachable.
4019 Through pointers we can end up with cycles which messes up the
4020 required property that we need to compute the same hash value
4021 for structurally equivalent types. To avoid this we have to
4022 hash all types in a cycle (the SCC) in a commutative way. The
4023 easiest way is to not mix in the hashes of the SCC members at
4024 all. To make this work we have to delay setting the hash
4025 values of the SCC until it is complete. */
4027 static hashval_t
4028 iterative_hash_gimple_type (tree type, hashval_t val,
4029 VEC(tree, heap) **sccstack,
4030 struct pointer_map_t *sccstate,
4031 struct obstack *sccstate_obstack,
4032 enum gtc_mode mode)
4034 hashval_t v;
4035 void **slot;
4036 struct sccs *state;
4038 /* Not visited during this DFS walk. */
4039 gcc_checking_assert (!pointer_map_contains (sccstate, type));
4040 state = XOBNEW (sccstate_obstack, struct sccs);
4041 *pointer_map_insert (sccstate, type) = state;
4043 VEC_safe_push (tree, heap, *sccstack, type);
4044 state->dfsnum = next_dfs_num++;
4045 state->low = state->dfsnum;
4046 state->on_sccstack = true;
4048 /* Combine a few common features of types so that types are grouped into
4049 smaller sets; when searching for existing matching types to merge,
4050 only existing types having the same features as the new type will be
4051 checked. */
4052 v = iterative_hash_hashval_t (TREE_CODE (type), 0);
4053 v = iterative_hash_hashval_t (TYPE_QUALS (type), v);
4054 v = iterative_hash_hashval_t (TREE_ADDRESSABLE (type), v);
4056 /* Do not hash the types size as this will cause differences in
4057 hash values for the complete vs. the incomplete type variant. */
4059 /* Incorporate common features of numerical types. */
4060 if (INTEGRAL_TYPE_P (type)
4061 || SCALAR_FLOAT_TYPE_P (type)
4062 || FIXED_POINT_TYPE_P (type))
4064 v = iterative_hash_hashval_t (TYPE_PRECISION (type), v);
4065 v = iterative_hash_hashval_t (TYPE_MODE (type), v);
4066 v = iterative_hash_hashval_t (TYPE_UNSIGNED (type), v);
4069 /* For pointer and reference types, fold in information about the type
4070 pointed to but do not recurse into possibly incomplete types to
4071 avoid hash differences for complete vs. incomplete types. */
4072 if (POINTER_TYPE_P (type))
4074 if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (type)))
4076 v = iterative_hash_hashval_t (TREE_CODE (TREE_TYPE (type)), v);
4077 v = iterative_hash_name
4078 (TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (type))), v);
4080 else
4081 v = visit (TREE_TYPE (type), state, v,
4082 sccstack, sccstate, sccstate_obstack, mode);
4085 /* For integer types hash the types min/max values and the string flag. */
4086 if (TREE_CODE (type) == INTEGER_TYPE)
4088 /* OMP lowering can introduce error_mark_node in place of
4089 random local decls in types. */
4090 if (TYPE_MIN_VALUE (type) != error_mark_node)
4091 v = iterative_hash_expr (TYPE_MIN_VALUE (type), v);
4092 if (TYPE_MAX_VALUE (type) != error_mark_node)
4093 v = iterative_hash_expr (TYPE_MAX_VALUE (type), v);
4094 v = iterative_hash_hashval_t (TYPE_STRING_FLAG (type), v);
4097 /* For array types hash their domain and the string flag. */
4098 if (TREE_CODE (type) == ARRAY_TYPE
4099 && TYPE_DOMAIN (type))
4101 v = iterative_hash_hashval_t (TYPE_STRING_FLAG (type), v);
4102 v = visit (TYPE_DOMAIN (type), state, v,
4103 sccstack, sccstate, sccstate_obstack, mode);
4106 /* Recurse for aggregates with a single element type. */
4107 if (TREE_CODE (type) == ARRAY_TYPE
4108 || TREE_CODE (type) == COMPLEX_TYPE
4109 || TREE_CODE (type) == VECTOR_TYPE)
4110 v = visit (TREE_TYPE (type), state, v,
4111 sccstack, sccstate, sccstate_obstack, mode);
4113 /* Incorporate function return and argument types. */
4114 if (TREE_CODE (type) == FUNCTION_TYPE || TREE_CODE (type) == METHOD_TYPE)
4116 unsigned na;
4117 tree p;
4119 /* For method types also incorporate their parent class. */
4120 if (TREE_CODE (type) == METHOD_TYPE)
4121 v = visit (TYPE_METHOD_BASETYPE (type), state, v,
4122 sccstack, sccstate, sccstate_obstack, mode);
4124 /* For result types allow mismatch in completeness. */
4125 if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (type)))
4127 v = iterative_hash_hashval_t (TREE_CODE (TREE_TYPE (type)), v);
4128 v = iterative_hash_name
4129 (TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (type))), v);
4131 else
4132 v = visit (TREE_TYPE (type), state, v,
4133 sccstack, sccstate, sccstate_obstack, mode);
4135 for (p = TYPE_ARG_TYPES (type), na = 0; p; p = TREE_CHAIN (p))
4137 /* For argument types allow mismatch in completeness. */
4138 if (RECORD_OR_UNION_TYPE_P (TREE_VALUE (p)))
4140 v = iterative_hash_hashval_t (TREE_CODE (TREE_VALUE (p)), v);
4141 v = iterative_hash_name
4142 (TYPE_NAME (TYPE_MAIN_VARIANT (TREE_VALUE (p))), v);
4144 else
4145 v = visit (TREE_VALUE (p), state, v,
4146 sccstack, sccstate, sccstate_obstack, mode);
4147 na++;
4150 v = iterative_hash_hashval_t (na, v);
4153 if (TREE_CODE (type) == RECORD_TYPE
4154 || TREE_CODE (type) == UNION_TYPE
4155 || TREE_CODE (type) == QUAL_UNION_TYPE)
4157 unsigned nf;
4158 tree f;
4160 if (mode == GTC_MERGE)
4161 v = iterative_hash_name (TYPE_NAME (TYPE_MAIN_VARIANT (type)), v);
4163 for (f = TYPE_FIELDS (type), nf = 0; f; f = TREE_CHAIN (f))
4165 if (mode == GTC_MERGE)
4166 v = iterative_hash_name (DECL_NAME (f), v);
4167 v = visit (TREE_TYPE (f), state, v,
4168 sccstack, sccstate, sccstate_obstack, mode);
4169 nf++;
4172 v = iterative_hash_hashval_t (nf, v);
4175 /* Record hash for us. */
4176 state->u.hash = v;
4178 /* See if we found an SCC. */
4179 if (state->low == state->dfsnum)
4181 tree x;
4183 /* Pop off the SCC and set its hash values. */
4186 struct sccs *cstate;
4187 struct tree_int_map *m = ggc_alloc_cleared_tree_int_map ();
4188 x = VEC_pop (tree, *sccstack);
4189 cstate = (struct sccs *)*pointer_map_contains (sccstate, x);
4190 cstate->on_sccstack = false;
4191 m->base.from = x;
4192 m->to = cstate->u.hash;
4193 slot = htab_find_slot (mode == GTC_MERGE
4194 ? type_hash_cache : canonical_type_hash_cache,
4195 m, INSERT);
4196 gcc_assert (!*slot);
4197 *slot = (void *) m;
4199 while (x != type);
4202 return iterative_hash_hashval_t (v, val);
4206 /* Returns a hash value for P (assumed to be a type). The hash value
4207 is computed using some distinguishing features of the type. Note
4208 that we cannot use pointer hashing here as we may be dealing with
4209 two distinct instances of the same type.
4211 This function should produce the same hash value for two compatible
4212 types according to gimple_types_compatible_p. */
4214 static hashval_t
4215 gimple_type_hash_1 (const void *p, enum gtc_mode mode)
4217 const_tree t = (const_tree) p;
4218 VEC(tree, heap) *sccstack = NULL;
4219 struct pointer_map_t *sccstate;
4220 struct obstack sccstate_obstack;
4221 hashval_t val;
4222 void **slot;
4223 struct tree_int_map m;
4225 if (mode == GTC_MERGE
4226 && type_hash_cache == NULL)
4227 type_hash_cache = htab_create_ggc (512, tree_int_map_hash,
4228 tree_int_map_eq, NULL);
4229 else if (mode == GTC_DIAG
4230 && canonical_type_hash_cache == NULL)
4231 canonical_type_hash_cache = htab_create_ggc (512, tree_int_map_hash,
4232 tree_int_map_eq, NULL);
4234 m.base.from = CONST_CAST_TREE (t);
4235 if ((slot = htab_find_slot (mode == GTC_MERGE
4236 ? type_hash_cache : canonical_type_hash_cache,
4237 &m, NO_INSERT))
4238 && *slot)
4239 return iterative_hash_hashval_t (((struct tree_int_map *) *slot)->to, 0);
4241 /* Perform a DFS walk and pre-hash all reachable types. */
4242 next_dfs_num = 1;
4243 sccstate = pointer_map_create ();
4244 gcc_obstack_init (&sccstate_obstack);
4245 val = iterative_hash_gimple_type (CONST_CAST_TREE (t), 0,
4246 &sccstack, sccstate, &sccstate_obstack,
4247 mode);
4248 VEC_free (tree, heap, sccstack);
4249 pointer_map_destroy (sccstate);
4250 obstack_free (&sccstate_obstack, NULL);
4252 return val;
4255 static hashval_t
4256 gimple_type_hash (const void *p)
4258 return gimple_type_hash_1 (p, GTC_MERGE);
4261 static hashval_t
4262 gimple_canonical_type_hash (const void *p)
4264 return gimple_type_hash_1 (p, GTC_DIAG);
4268 /* Returns nonzero if P1 and P2 are equal. */
4270 static int
4271 gimple_type_eq (const void *p1, const void *p2)
4273 const_tree t1 = (const_tree) p1;
4274 const_tree t2 = (const_tree) p2;
4275 return gimple_types_compatible_p (CONST_CAST_TREE (t1),
4276 CONST_CAST_TREE (t2), GTC_MERGE);
4280 /* Register type T in the global type table gimple_types.
4281 If another type T', compatible with T, already existed in
4282 gimple_types then return T', otherwise return T. This is used by
4283 LTO to merge identical types read from different TUs. */
4285 tree
4286 gimple_register_type (tree t)
4288 void **slot;
4289 gimple_type_leader_entry *leader;
4290 tree mv_leader = NULL_TREE;
4292 gcc_assert (TYPE_P (t));
4294 if (!gimple_type_leader)
4295 gimple_type_leader = ggc_alloc_cleared_vec_gimple_type_leader_entry_s
4296 (GIMPLE_TYPE_LEADER_SIZE);
4297 /* If we registered this type before return the cached result. */
4298 leader = &gimple_type_leader[TYPE_UID (t) % GIMPLE_TYPE_LEADER_SIZE];
4299 if (leader->type == t)
4300 return leader->leader;
4302 /* Always register the main variant first. This is important so we
4303 pick up the non-typedef variants as canonical, otherwise we'll end
4304 up taking typedef ids for structure tags during comparison. */
4305 if (TYPE_MAIN_VARIANT (t) != t)
4306 mv_leader = gimple_register_type (TYPE_MAIN_VARIANT (t));
4308 if (gimple_types == NULL)
4309 gimple_types = htab_create_ggc (16381, gimple_type_hash, gimple_type_eq, 0);
4311 slot = htab_find_slot (gimple_types, t, INSERT);
4312 if (*slot
4313 && *(tree *)slot != t)
4315 tree new_type = (tree) *((tree *) slot);
4317 /* Do not merge types with different addressability. */
4318 gcc_assert (TREE_ADDRESSABLE (t) == TREE_ADDRESSABLE (new_type));
4320 /* If t is not its main variant then make t unreachable from its
4321 main variant list. Otherwise we'd queue up a lot of duplicates
4322 there. */
4323 if (t != TYPE_MAIN_VARIANT (t))
4325 tree tem = TYPE_MAIN_VARIANT (t);
4326 while (tem && TYPE_NEXT_VARIANT (tem) != t)
4327 tem = TYPE_NEXT_VARIANT (tem);
4328 if (tem)
4329 TYPE_NEXT_VARIANT (tem) = TYPE_NEXT_VARIANT (t);
4330 TYPE_NEXT_VARIANT (t) = NULL_TREE;
4333 /* If we are a pointer then remove us from the pointer-to or
4334 reference-to chain. Otherwise we'd queue up a lot of duplicates
4335 there. */
4336 if (TREE_CODE (t) == POINTER_TYPE)
4338 if (TYPE_POINTER_TO (TREE_TYPE (t)) == t)
4339 TYPE_POINTER_TO (TREE_TYPE (t)) = TYPE_NEXT_PTR_TO (t);
4340 else
4342 tree tem = TYPE_POINTER_TO (TREE_TYPE (t));
4343 while (tem && TYPE_NEXT_PTR_TO (tem) != t)
4344 tem = TYPE_NEXT_PTR_TO (tem);
4345 if (tem)
4346 TYPE_NEXT_PTR_TO (tem) = TYPE_NEXT_PTR_TO (t);
4348 TYPE_NEXT_PTR_TO (t) = NULL_TREE;
4350 else if (TREE_CODE (t) == REFERENCE_TYPE)
4352 if (TYPE_REFERENCE_TO (TREE_TYPE (t)) == t)
4353 TYPE_REFERENCE_TO (TREE_TYPE (t)) = TYPE_NEXT_REF_TO (t);
4354 else
4356 tree tem = TYPE_REFERENCE_TO (TREE_TYPE (t));
4357 while (tem && TYPE_NEXT_REF_TO (tem) != t)
4358 tem = TYPE_NEXT_REF_TO (tem);
4359 if (tem)
4360 TYPE_NEXT_REF_TO (tem) = TYPE_NEXT_REF_TO (t);
4362 TYPE_NEXT_REF_TO (t) = NULL_TREE;
4365 leader->type = t;
4366 leader->leader = new_type;
4367 t = new_type;
4369 else
4371 leader->type = t;
4372 leader->leader = t;
4373 /* We're the type leader. Make our TYPE_MAIN_VARIANT valid. */
4374 if (TYPE_MAIN_VARIANT (t) != t
4375 && TYPE_MAIN_VARIANT (t) != mv_leader)
4377 /* Remove us from our main variant list as we are not the variant
4378 leader and the variant leader will change. */
4379 tree tem = TYPE_MAIN_VARIANT (t);
4380 while (tem && TYPE_NEXT_VARIANT (tem) != t)
4381 tem = TYPE_NEXT_VARIANT (tem);
4382 if (tem)
4383 TYPE_NEXT_VARIANT (tem) = TYPE_NEXT_VARIANT (t);
4384 TYPE_NEXT_VARIANT (t) = NULL_TREE;
4385 /* Adjust our main variant. Linking us into its variant list
4386 will happen at fixup time. */
4387 TYPE_MAIN_VARIANT (t) = mv_leader;
4389 *slot = (void *) t;
4392 return t;
4396 /* Returns nonzero if P1 and P2 are equal. */
4398 static int
4399 gimple_canonical_type_eq (const void *p1, const void *p2)
4401 const_tree t1 = (const_tree) p1;
4402 const_tree t2 = (const_tree) p2;
4403 return gimple_types_compatible_p (CONST_CAST_TREE (t1),
4404 CONST_CAST_TREE (t2), GTC_DIAG);
4407 /* Register type T in the global type table gimple_types.
4408 If another type T', compatible with T, already existed in
4409 gimple_types then return T', otherwise return T. This is used by
4410 LTO to merge identical types read from different TUs. */
4412 tree
4413 gimple_register_canonical_type (tree t)
4415 void **slot;
4416 tree orig_t = t;
4418 gcc_assert (TYPE_P (t));
4420 if (TYPE_CANONICAL (t))
4421 return TYPE_CANONICAL (t);
4423 /* Always register the type itself first so that if it turns out
4424 to be the canonical type it will be the one we merge to as well. */
4425 t = gimple_register_type (t);
4427 /* Always register the main variant first. This is important so we
4428 pick up the non-typedef variants as canonical, otherwise we'll end
4429 up taking typedef ids for structure tags during comparison. */
4430 if (TYPE_MAIN_VARIANT (t) != t)
4431 gimple_register_canonical_type (TYPE_MAIN_VARIANT (t));
4433 if (gimple_canonical_types == NULL)
4434 gimple_canonical_types = htab_create_ggc (16381, gimple_canonical_type_hash,
4435 gimple_canonical_type_eq, 0);
4437 slot = htab_find_slot (gimple_canonical_types, t, INSERT);
4438 if (*slot
4439 && *(tree *)slot != t)
4441 tree new_type = (tree) *((tree *) slot);
4443 TYPE_CANONICAL (t) = new_type;
4444 t = new_type;
4446 else
4448 TYPE_CANONICAL (t) = t;
4449 *slot = (void *) t;
4452 /* Also cache the canonical type in the non-leaders. */
4453 TYPE_CANONICAL (orig_t) = t;
4455 return t;
4459 /* Show statistics on references to the global type table gimple_types. */
4461 void
4462 print_gimple_types_stats (void)
4464 if (gimple_types)
4465 fprintf (stderr, "GIMPLE type table: size %ld, %ld elements, "
4466 "%ld searches, %ld collisions (ratio: %f)\n",
4467 (long) htab_size (gimple_types),
4468 (long) htab_elements (gimple_types),
4469 (long) gimple_types->searches,
4470 (long) gimple_types->collisions,
4471 htab_collisions (gimple_types));
4472 else
4473 fprintf (stderr, "GIMPLE type table is empty\n");
4474 if (type_hash_cache)
4475 fprintf (stderr, "GIMPLE type hash table: size %ld, %ld elements, "
4476 "%ld searches, %ld collisions (ratio: %f)\n",
4477 (long) htab_size (type_hash_cache),
4478 (long) htab_elements (type_hash_cache),
4479 (long) type_hash_cache->searches,
4480 (long) type_hash_cache->collisions,
4481 htab_collisions (type_hash_cache));
4482 else
4483 fprintf (stderr, "GIMPLE type hash table is empty\n");
4484 if (gimple_canonical_types)
4485 fprintf (stderr, "GIMPLE canonical type table: size %ld, %ld elements, "
4486 "%ld searches, %ld collisions (ratio: %f)\n",
4487 (long) htab_size (gimple_canonical_types),
4488 (long) htab_elements (gimple_canonical_types),
4489 (long) gimple_canonical_types->searches,
4490 (long) gimple_canonical_types->collisions,
4491 htab_collisions (gimple_canonical_types));
4492 else
4493 fprintf (stderr, "GIMPLE canonical type table is empty\n");
4494 if (canonical_type_hash_cache)
4495 fprintf (stderr, "GIMPLE canonical type hash table: size %ld, %ld elements, "
4496 "%ld searches, %ld collisions (ratio: %f)\n",
4497 (long) htab_size (canonical_type_hash_cache),
4498 (long) htab_elements (canonical_type_hash_cache),
4499 (long) canonical_type_hash_cache->searches,
4500 (long) canonical_type_hash_cache->collisions,
4501 htab_collisions (canonical_type_hash_cache));
4502 else
4503 fprintf (stderr, "GIMPLE canonical type hash table is empty\n");
4504 if (gtc_visited)
4505 fprintf (stderr, "GIMPLE type comparison table: size %ld, %ld "
4506 "elements, %ld searches, %ld collisions (ratio: %f)\n",
4507 (long) htab_size (gtc_visited),
4508 (long) htab_elements (gtc_visited),
4509 (long) gtc_visited->searches,
4510 (long) gtc_visited->collisions,
4511 htab_collisions (gtc_visited));
4512 else
4513 fprintf (stderr, "GIMPLE type comparison table is empty\n");
4516 /* Free the gimple type hashtables used for LTO type merging. */
4518 void
4519 free_gimple_type_tables (void)
4521 /* Last chance to print stats for the tables. */
4522 if (flag_lto_report)
4523 print_gimple_types_stats ();
4525 if (gimple_types)
4527 htab_delete (gimple_types);
4528 gimple_types = NULL;
4530 if (gimple_canonical_types)
4532 htab_delete (gimple_canonical_types);
4533 gimple_canonical_types = NULL;
4535 if (type_hash_cache)
4537 htab_delete (type_hash_cache);
4538 type_hash_cache = NULL;
4540 if (canonical_type_hash_cache)
4542 htab_delete (canonical_type_hash_cache);
4543 canonical_type_hash_cache = NULL;
4545 if (gtc_visited)
4547 htab_delete (gtc_visited);
4548 obstack_free (&gtc_ob, NULL);
4549 gtc_visited = NULL;
4551 gimple_type_leader = NULL;
4555 /* Return a type the same as TYPE except unsigned or
4556 signed according to UNSIGNEDP. */
4558 static tree
4559 gimple_signed_or_unsigned_type (bool unsignedp, tree type)
4561 tree type1;
4563 type1 = TYPE_MAIN_VARIANT (type);
4564 if (type1 == signed_char_type_node
4565 || type1 == char_type_node
4566 || type1 == unsigned_char_type_node)
4567 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
4568 if (type1 == integer_type_node || type1 == unsigned_type_node)
4569 return unsignedp ? unsigned_type_node : integer_type_node;
4570 if (type1 == short_integer_type_node || type1 == short_unsigned_type_node)
4571 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
4572 if (type1 == long_integer_type_node || type1 == long_unsigned_type_node)
4573 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
4574 if (type1 == long_long_integer_type_node
4575 || type1 == long_long_unsigned_type_node)
4576 return unsignedp
4577 ? long_long_unsigned_type_node
4578 : long_long_integer_type_node;
4579 if (int128_integer_type_node && (type1 == int128_integer_type_node || type1 == int128_unsigned_type_node))
4580 return unsignedp
4581 ? int128_unsigned_type_node
4582 : int128_integer_type_node;
4583 #if HOST_BITS_PER_WIDE_INT >= 64
4584 if (type1 == intTI_type_node || type1 == unsigned_intTI_type_node)
4585 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
4586 #endif
4587 if (type1 == intDI_type_node || type1 == unsigned_intDI_type_node)
4588 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
4589 if (type1 == intSI_type_node || type1 == unsigned_intSI_type_node)
4590 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
4591 if (type1 == intHI_type_node || type1 == unsigned_intHI_type_node)
4592 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
4593 if (type1 == intQI_type_node || type1 == unsigned_intQI_type_node)
4594 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
4596 #define GIMPLE_FIXED_TYPES(NAME) \
4597 if (type1 == short_ ## NAME ## _type_node \
4598 || type1 == unsigned_short_ ## NAME ## _type_node) \
4599 return unsignedp ? unsigned_short_ ## NAME ## _type_node \
4600 : short_ ## NAME ## _type_node; \
4601 if (type1 == NAME ## _type_node \
4602 || type1 == unsigned_ ## NAME ## _type_node) \
4603 return unsignedp ? unsigned_ ## NAME ## _type_node \
4604 : NAME ## _type_node; \
4605 if (type1 == long_ ## NAME ## _type_node \
4606 || type1 == unsigned_long_ ## NAME ## _type_node) \
4607 return unsignedp ? unsigned_long_ ## NAME ## _type_node \
4608 : long_ ## NAME ## _type_node; \
4609 if (type1 == long_long_ ## NAME ## _type_node \
4610 || type1 == unsigned_long_long_ ## NAME ## _type_node) \
4611 return unsignedp ? unsigned_long_long_ ## NAME ## _type_node \
4612 : long_long_ ## NAME ## _type_node;
4614 #define GIMPLE_FIXED_MODE_TYPES(NAME) \
4615 if (type1 == NAME ## _type_node \
4616 || type1 == u ## NAME ## _type_node) \
4617 return unsignedp ? u ## NAME ## _type_node \
4618 : NAME ## _type_node;
4620 #define GIMPLE_FIXED_TYPES_SAT(NAME) \
4621 if (type1 == sat_ ## short_ ## NAME ## _type_node \
4622 || type1 == sat_ ## unsigned_short_ ## NAME ## _type_node) \
4623 return unsignedp ? sat_ ## unsigned_short_ ## NAME ## _type_node \
4624 : sat_ ## short_ ## NAME ## _type_node; \
4625 if (type1 == sat_ ## NAME ## _type_node \
4626 || type1 == sat_ ## unsigned_ ## NAME ## _type_node) \
4627 return unsignedp ? sat_ ## unsigned_ ## NAME ## _type_node \
4628 : sat_ ## NAME ## _type_node; \
4629 if (type1 == sat_ ## long_ ## NAME ## _type_node \
4630 || type1 == sat_ ## unsigned_long_ ## NAME ## _type_node) \
4631 return unsignedp ? sat_ ## unsigned_long_ ## NAME ## _type_node \
4632 : sat_ ## long_ ## NAME ## _type_node; \
4633 if (type1 == sat_ ## long_long_ ## NAME ## _type_node \
4634 || type1 == sat_ ## unsigned_long_long_ ## NAME ## _type_node) \
4635 return unsignedp ? sat_ ## unsigned_long_long_ ## NAME ## _type_node \
4636 : sat_ ## long_long_ ## NAME ## _type_node;
4638 #define GIMPLE_FIXED_MODE_TYPES_SAT(NAME) \
4639 if (type1 == sat_ ## NAME ## _type_node \
4640 || type1 == sat_ ## u ## NAME ## _type_node) \
4641 return unsignedp ? sat_ ## u ## NAME ## _type_node \
4642 : sat_ ## NAME ## _type_node;
4644 GIMPLE_FIXED_TYPES (fract);
4645 GIMPLE_FIXED_TYPES_SAT (fract);
4646 GIMPLE_FIXED_TYPES (accum);
4647 GIMPLE_FIXED_TYPES_SAT (accum);
4649 GIMPLE_FIXED_MODE_TYPES (qq);
4650 GIMPLE_FIXED_MODE_TYPES (hq);
4651 GIMPLE_FIXED_MODE_TYPES (sq);
4652 GIMPLE_FIXED_MODE_TYPES (dq);
4653 GIMPLE_FIXED_MODE_TYPES (tq);
4654 GIMPLE_FIXED_MODE_TYPES_SAT (qq);
4655 GIMPLE_FIXED_MODE_TYPES_SAT (hq);
4656 GIMPLE_FIXED_MODE_TYPES_SAT (sq);
4657 GIMPLE_FIXED_MODE_TYPES_SAT (dq);
4658 GIMPLE_FIXED_MODE_TYPES_SAT (tq);
4659 GIMPLE_FIXED_MODE_TYPES (ha);
4660 GIMPLE_FIXED_MODE_TYPES (sa);
4661 GIMPLE_FIXED_MODE_TYPES (da);
4662 GIMPLE_FIXED_MODE_TYPES (ta);
4663 GIMPLE_FIXED_MODE_TYPES_SAT (ha);
4664 GIMPLE_FIXED_MODE_TYPES_SAT (sa);
4665 GIMPLE_FIXED_MODE_TYPES_SAT (da);
4666 GIMPLE_FIXED_MODE_TYPES_SAT (ta);
4668 /* For ENUMERAL_TYPEs in C++, must check the mode of the types, not
4669 the precision; they have precision set to match their range, but
4670 may use a wider mode to match an ABI. If we change modes, we may
4671 wind up with bad conversions. For INTEGER_TYPEs in C, must check
4672 the precision as well, so as to yield correct results for
4673 bit-field types. C++ does not have these separate bit-field
4674 types, and producing a signed or unsigned variant of an
4675 ENUMERAL_TYPE may cause other problems as well. */
4676 if (!INTEGRAL_TYPE_P (type)
4677 || TYPE_UNSIGNED (type) == unsignedp)
4678 return type;
4680 #define TYPE_OK(node) \
4681 (TYPE_MODE (type) == TYPE_MODE (node) \
4682 && TYPE_PRECISION (type) == TYPE_PRECISION (node))
4683 if (TYPE_OK (signed_char_type_node))
4684 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
4685 if (TYPE_OK (integer_type_node))
4686 return unsignedp ? unsigned_type_node : integer_type_node;
4687 if (TYPE_OK (short_integer_type_node))
4688 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
4689 if (TYPE_OK (long_integer_type_node))
4690 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
4691 if (TYPE_OK (long_long_integer_type_node))
4692 return (unsignedp
4693 ? long_long_unsigned_type_node
4694 : long_long_integer_type_node);
4695 if (int128_integer_type_node && TYPE_OK (int128_integer_type_node))
4696 return (unsignedp
4697 ? int128_unsigned_type_node
4698 : int128_integer_type_node);
4700 #if HOST_BITS_PER_WIDE_INT >= 64
4701 if (TYPE_OK (intTI_type_node))
4702 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
4703 #endif
4704 if (TYPE_OK (intDI_type_node))
4705 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
4706 if (TYPE_OK (intSI_type_node))
4707 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
4708 if (TYPE_OK (intHI_type_node))
4709 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
4710 if (TYPE_OK (intQI_type_node))
4711 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
4713 #undef GIMPLE_FIXED_TYPES
4714 #undef GIMPLE_FIXED_MODE_TYPES
4715 #undef GIMPLE_FIXED_TYPES_SAT
4716 #undef GIMPLE_FIXED_MODE_TYPES_SAT
4717 #undef TYPE_OK
4719 return build_nonstandard_integer_type (TYPE_PRECISION (type), unsignedp);
4723 /* Return an unsigned type the same as TYPE in other respects. */
4725 tree
4726 gimple_unsigned_type (tree type)
4728 return gimple_signed_or_unsigned_type (true, type);
4732 /* Return a signed type the same as TYPE in other respects. */
4734 tree
4735 gimple_signed_type (tree type)
4737 return gimple_signed_or_unsigned_type (false, type);
4741 /* Return the typed-based alias set for T, which may be an expression
4742 or a type. Return -1 if we don't do anything special. */
4744 alias_set_type
4745 gimple_get_alias_set (tree t)
4747 tree u;
4749 /* Permit type-punning when accessing a union, provided the access
4750 is directly through the union. For example, this code does not
4751 permit taking the address of a union member and then storing
4752 through it. Even the type-punning allowed here is a GCC
4753 extension, albeit a common and useful one; the C standard says
4754 that such accesses have implementation-defined behavior. */
4755 for (u = t;
4756 TREE_CODE (u) == COMPONENT_REF || TREE_CODE (u) == ARRAY_REF;
4757 u = TREE_OPERAND (u, 0))
4758 if (TREE_CODE (u) == COMPONENT_REF
4759 && TREE_CODE (TREE_TYPE (TREE_OPERAND (u, 0))) == UNION_TYPE)
4760 return 0;
4762 /* That's all the expressions we handle specially. */
4763 if (!TYPE_P (t))
4764 return -1;
4766 /* For convenience, follow the C standard when dealing with
4767 character types. Any object may be accessed via an lvalue that
4768 has character type. */
4769 if (t == char_type_node
4770 || t == signed_char_type_node
4771 || t == unsigned_char_type_node)
4772 return 0;
4774 /* Allow aliasing between signed and unsigned variants of the same
4775 type. We treat the signed variant as canonical. */
4776 if (TREE_CODE (t) == INTEGER_TYPE && TYPE_UNSIGNED (t))
4778 tree t1 = gimple_signed_type (t);
4780 /* t1 == t can happen for boolean nodes which are always unsigned. */
4781 if (t1 != t)
4782 return get_alias_set (t1);
4785 return -1;
4789 /* Data structure used to count the number of dereferences to PTR
4790 inside an expression. */
4791 struct count_ptr_d
4793 tree ptr;
4794 unsigned num_stores;
4795 unsigned num_loads;
4798 /* Helper for count_uses_and_derefs. Called by walk_tree to look for
4799 (ALIGN/MISALIGNED_)INDIRECT_REF nodes for the pointer passed in DATA. */
4801 static tree
4802 count_ptr_derefs (tree *tp, int *walk_subtrees, void *data)
4804 struct walk_stmt_info *wi_p = (struct walk_stmt_info *) data;
4805 struct count_ptr_d *count_p = (struct count_ptr_d *) wi_p->info;
4807 /* Do not walk inside ADDR_EXPR nodes. In the expression &ptr->fld,
4808 pointer 'ptr' is *not* dereferenced, it is simply used to compute
4809 the address of 'fld' as 'ptr + offsetof(fld)'. */
4810 if (TREE_CODE (*tp) == ADDR_EXPR)
4812 *walk_subtrees = 0;
4813 return NULL_TREE;
4816 if (TREE_CODE (*tp) == MEM_REF && TREE_OPERAND (*tp, 0) == count_p->ptr)
4818 if (wi_p->is_lhs)
4819 count_p->num_stores++;
4820 else
4821 count_p->num_loads++;
4824 return NULL_TREE;
4827 /* Count the number of direct and indirect uses for pointer PTR in
4828 statement STMT. The number of direct uses is stored in
4829 *NUM_USES_P. Indirect references are counted separately depending
4830 on whether they are store or load operations. The counts are
4831 stored in *NUM_STORES_P and *NUM_LOADS_P. */
4833 void
4834 count_uses_and_derefs (tree ptr, gimple stmt, unsigned *num_uses_p,
4835 unsigned *num_loads_p, unsigned *num_stores_p)
4837 ssa_op_iter i;
4838 tree use;
4840 *num_uses_p = 0;
4841 *num_loads_p = 0;
4842 *num_stores_p = 0;
4844 /* Find out the total number of uses of PTR in STMT. */
4845 FOR_EACH_SSA_TREE_OPERAND (use, stmt, i, SSA_OP_USE)
4846 if (use == ptr)
4847 (*num_uses_p)++;
4849 /* Now count the number of indirect references to PTR. This is
4850 truly awful, but we don't have much choice. There are no parent
4851 pointers inside INDIRECT_REFs, so an expression like
4852 '*x_1 = foo (x_1, *x_1)' needs to be traversed piece by piece to
4853 find all the indirect and direct uses of x_1 inside. The only
4854 shortcut we can take is the fact that GIMPLE only allows
4855 INDIRECT_REFs inside the expressions below. */
4856 if (is_gimple_assign (stmt)
4857 || gimple_code (stmt) == GIMPLE_RETURN
4858 || gimple_code (stmt) == GIMPLE_ASM
4859 || is_gimple_call (stmt))
4861 struct walk_stmt_info wi;
4862 struct count_ptr_d count;
4864 count.ptr = ptr;
4865 count.num_stores = 0;
4866 count.num_loads = 0;
4868 memset (&wi, 0, sizeof (wi));
4869 wi.info = &count;
4870 walk_gimple_op (stmt, count_ptr_derefs, &wi);
4872 *num_stores_p = count.num_stores;
4873 *num_loads_p = count.num_loads;
4876 gcc_assert (*num_uses_p >= *num_loads_p + *num_stores_p);
4879 /* From a tree operand OP return the base of a load or store operation
4880 or NULL_TREE if OP is not a load or a store. */
4882 static tree
4883 get_base_loadstore (tree op)
4885 while (handled_component_p (op))
4886 op = TREE_OPERAND (op, 0);
4887 if (DECL_P (op)
4888 || INDIRECT_REF_P (op)
4889 || TREE_CODE (op) == MEM_REF
4890 || TREE_CODE (op) == TARGET_MEM_REF)
4891 return op;
4892 return NULL_TREE;
4895 /* For the statement STMT call the callbacks VISIT_LOAD, VISIT_STORE and
4896 VISIT_ADDR if non-NULL on loads, store and address-taken operands
4897 passing the STMT, the base of the operand and DATA to it. The base
4898 will be either a decl, an indirect reference (including TARGET_MEM_REF)
4899 or the argument of an address expression.
4900 Returns the results of these callbacks or'ed. */
4902 bool
4903 walk_stmt_load_store_addr_ops (gimple stmt, void *data,
4904 bool (*visit_load)(gimple, tree, void *),
4905 bool (*visit_store)(gimple, tree, void *),
4906 bool (*visit_addr)(gimple, tree, void *))
4908 bool ret = false;
4909 unsigned i;
4910 if (gimple_assign_single_p (stmt))
4912 tree lhs, rhs;
4913 if (visit_store)
4915 lhs = get_base_loadstore (gimple_assign_lhs (stmt));
4916 if (lhs)
4917 ret |= visit_store (stmt, lhs, data);
4919 rhs = gimple_assign_rhs1 (stmt);
4920 while (handled_component_p (rhs))
4921 rhs = TREE_OPERAND (rhs, 0);
4922 if (visit_addr)
4924 if (TREE_CODE (rhs) == ADDR_EXPR)
4925 ret |= visit_addr (stmt, TREE_OPERAND (rhs, 0), data);
4926 else if (TREE_CODE (rhs) == TARGET_MEM_REF
4927 && TREE_CODE (TMR_BASE (rhs)) == ADDR_EXPR)
4928 ret |= visit_addr (stmt, TREE_OPERAND (TMR_BASE (rhs), 0), data);
4929 else if (TREE_CODE (rhs) == OBJ_TYPE_REF
4930 && TREE_CODE (OBJ_TYPE_REF_OBJECT (rhs)) == ADDR_EXPR)
4931 ret |= visit_addr (stmt, TREE_OPERAND (OBJ_TYPE_REF_OBJECT (rhs),
4932 0), data);
4933 lhs = gimple_assign_lhs (stmt);
4934 if (TREE_CODE (lhs) == TARGET_MEM_REF
4935 && TREE_CODE (TMR_BASE (lhs)) == ADDR_EXPR)
4936 ret |= visit_addr (stmt, TREE_OPERAND (TMR_BASE (lhs), 0), data);
4938 if (visit_load)
4940 rhs = get_base_loadstore (rhs);
4941 if (rhs)
4942 ret |= visit_load (stmt, rhs, data);
4945 else if (visit_addr
4946 && (is_gimple_assign (stmt)
4947 || gimple_code (stmt) == GIMPLE_COND))
4949 for (i = 0; i < gimple_num_ops (stmt); ++i)
4950 if (gimple_op (stmt, i)
4951 && TREE_CODE (gimple_op (stmt, i)) == ADDR_EXPR)
4952 ret |= visit_addr (stmt, TREE_OPERAND (gimple_op (stmt, i), 0), data);
4954 else if (is_gimple_call (stmt))
4956 if (visit_store)
4958 tree lhs = gimple_call_lhs (stmt);
4959 if (lhs)
4961 lhs = get_base_loadstore (lhs);
4962 if (lhs)
4963 ret |= visit_store (stmt, lhs, data);
4966 if (visit_load || visit_addr)
4967 for (i = 0; i < gimple_call_num_args (stmt); ++i)
4969 tree rhs = gimple_call_arg (stmt, i);
4970 if (visit_addr
4971 && TREE_CODE (rhs) == ADDR_EXPR)
4972 ret |= visit_addr (stmt, TREE_OPERAND (rhs, 0), data);
4973 else if (visit_load)
4975 rhs = get_base_loadstore (rhs);
4976 if (rhs)
4977 ret |= visit_load (stmt, rhs, data);
4980 if (visit_addr
4981 && gimple_call_chain (stmt)
4982 && TREE_CODE (gimple_call_chain (stmt)) == ADDR_EXPR)
4983 ret |= visit_addr (stmt, TREE_OPERAND (gimple_call_chain (stmt), 0),
4984 data);
4985 if (visit_addr
4986 && gimple_call_return_slot_opt_p (stmt)
4987 && gimple_call_lhs (stmt) != NULL_TREE
4988 && TREE_ADDRESSABLE (TREE_TYPE (gimple_call_lhs (stmt))))
4989 ret |= visit_addr (stmt, gimple_call_lhs (stmt), data);
4991 else if (gimple_code (stmt) == GIMPLE_ASM)
4993 unsigned noutputs;
4994 const char *constraint;
4995 const char **oconstraints;
4996 bool allows_mem, allows_reg, is_inout;
4997 noutputs = gimple_asm_noutputs (stmt);
4998 oconstraints = XALLOCAVEC (const char *, noutputs);
4999 if (visit_store || visit_addr)
5000 for (i = 0; i < gimple_asm_noutputs (stmt); ++i)
5002 tree link = gimple_asm_output_op (stmt, i);
5003 tree op = get_base_loadstore (TREE_VALUE (link));
5004 if (op && visit_store)
5005 ret |= visit_store (stmt, op, data);
5006 if (visit_addr)
5008 constraint = TREE_STRING_POINTER
5009 (TREE_VALUE (TREE_PURPOSE (link)));
5010 oconstraints[i] = constraint;
5011 parse_output_constraint (&constraint, i, 0, 0, &allows_mem,
5012 &allows_reg, &is_inout);
5013 if (op && !allows_reg && allows_mem)
5014 ret |= visit_addr (stmt, op, data);
5017 if (visit_load || visit_addr)
5018 for (i = 0; i < gimple_asm_ninputs (stmt); ++i)
5020 tree link = gimple_asm_input_op (stmt, i);
5021 tree op = TREE_VALUE (link);
5022 if (visit_addr
5023 && TREE_CODE (op) == ADDR_EXPR)
5024 ret |= visit_addr (stmt, TREE_OPERAND (op, 0), data);
5025 else if (visit_load || visit_addr)
5027 op = get_base_loadstore (op);
5028 if (op)
5030 if (visit_load)
5031 ret |= visit_load (stmt, op, data);
5032 if (visit_addr)
5034 constraint = TREE_STRING_POINTER
5035 (TREE_VALUE (TREE_PURPOSE (link)));
5036 parse_input_constraint (&constraint, 0, 0, noutputs,
5037 0, oconstraints,
5038 &allows_mem, &allows_reg);
5039 if (!allows_reg && allows_mem)
5040 ret |= visit_addr (stmt, op, data);
5046 else if (gimple_code (stmt) == GIMPLE_RETURN)
5048 tree op = gimple_return_retval (stmt);
5049 if (op)
5051 if (visit_addr
5052 && TREE_CODE (op) == ADDR_EXPR)
5053 ret |= visit_addr (stmt, TREE_OPERAND (op, 0), data);
5054 else if (visit_load)
5056 op = get_base_loadstore (op);
5057 if (op)
5058 ret |= visit_load (stmt, op, data);
5062 else if (visit_addr
5063 && gimple_code (stmt) == GIMPLE_PHI)
5065 for (i = 0; i < gimple_phi_num_args (stmt); ++i)
5067 tree op = PHI_ARG_DEF (stmt, i);
5068 if (TREE_CODE (op) == ADDR_EXPR)
5069 ret |= visit_addr (stmt, TREE_OPERAND (op, 0), data);
5073 return ret;
5076 /* Like walk_stmt_load_store_addr_ops but with NULL visit_addr. IPA-CP
5077 should make a faster clone for this case. */
5079 bool
5080 walk_stmt_load_store_ops (gimple stmt, void *data,
5081 bool (*visit_load)(gimple, tree, void *),
5082 bool (*visit_store)(gimple, tree, void *))
5084 return walk_stmt_load_store_addr_ops (stmt, data,
5085 visit_load, visit_store, NULL);
5088 /* Helper for gimple_ior_addresses_taken_1. */
5090 static bool
5091 gimple_ior_addresses_taken_1 (gimple stmt ATTRIBUTE_UNUSED,
5092 tree addr, void *data)
5094 bitmap addresses_taken = (bitmap)data;
5095 addr = get_base_address (addr);
5096 if (addr
5097 && DECL_P (addr))
5099 bitmap_set_bit (addresses_taken, DECL_UID (addr));
5100 return true;
5102 return false;
5105 /* Set the bit for the uid of all decls that have their address taken
5106 in STMT in the ADDRESSES_TAKEN bitmap. Returns true if there
5107 were any in this stmt. */
5109 bool
5110 gimple_ior_addresses_taken (bitmap addresses_taken, gimple stmt)
5112 return walk_stmt_load_store_addr_ops (stmt, addresses_taken, NULL, NULL,
5113 gimple_ior_addresses_taken_1);
5117 /* Return a printable name for symbol DECL. */
5119 const char *
5120 gimple_decl_printable_name (tree decl, int verbosity)
5122 if (!DECL_NAME (decl))
5123 return NULL;
5125 if (DECL_ASSEMBLER_NAME_SET_P (decl))
5127 const char *str, *mangled_str;
5128 int dmgl_opts = DMGL_NO_OPTS;
5130 if (verbosity >= 2)
5132 dmgl_opts = DMGL_VERBOSE
5133 | DMGL_ANSI
5134 | DMGL_GNU_V3
5135 | DMGL_RET_POSTFIX;
5136 if (TREE_CODE (decl) == FUNCTION_DECL)
5137 dmgl_opts |= DMGL_PARAMS;
5140 mangled_str = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
5141 str = cplus_demangle_v3 (mangled_str, dmgl_opts);
5142 return (str) ? str : mangled_str;
5145 return IDENTIFIER_POINTER (DECL_NAME (decl));
5148 /* Return true when STMT is builtins call to CODE. */
5150 bool
5151 gimple_call_builtin_p (gimple stmt, enum built_in_function code)
5153 tree fndecl;
5154 return (is_gimple_call (stmt)
5155 && (fndecl = gimple_call_fndecl (stmt)) != NULL
5156 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
5157 && DECL_FUNCTION_CODE (fndecl) == code);
5160 #include "gt-gimple.h"