PR c++/53989
[official-gcc.git] / gcc / gimple.c
blob4d2a0f5ffdee7a156c08a30ff3f7800521b6e18f
1 /* Gimple IR support functions.
3 Copyright 2007, 2008, 2009, 2010, 2011 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 /* All the tuples have their operand vector (if present) at the very bottom
54 of the structure. Therefore, the offset required to find the
55 operands vector the size of the structure minus the size of the 1
56 element tree array at the end (see gimple_ops). */
57 #define DEFGSSTRUCT(SYM, STRUCT, HAS_TREE_OP) \
58 (HAS_TREE_OP ? sizeof (struct STRUCT) - sizeof (tree) : 0),
59 EXPORTED_CONST size_t gimple_ops_offset_[] = {
60 #include "gsstruct.def"
62 #undef DEFGSSTRUCT
64 #define DEFGSSTRUCT(SYM, STRUCT, HAS_TREE_OP) sizeof(struct STRUCT),
65 static const size_t gsstruct_code_size[] = {
66 #include "gsstruct.def"
68 #undef DEFGSSTRUCT
70 #define DEFGSCODE(SYM, NAME, GSSCODE) NAME,
71 const char *const gimple_code_name[] = {
72 #include "gimple.def"
74 #undef DEFGSCODE
76 #define DEFGSCODE(SYM, NAME, GSSCODE) GSSCODE,
77 EXPORTED_CONST enum gimple_statement_structure_enum gss_for_code_[] = {
78 #include "gimple.def"
80 #undef DEFGSCODE
82 #ifdef GATHER_STATISTICS
83 /* Gimple stats. */
85 int gimple_alloc_counts[(int) gimple_alloc_kind_all];
86 int gimple_alloc_sizes[(int) gimple_alloc_kind_all];
88 /* Keep in sync with gimple.h:enum gimple_alloc_kind. */
89 static const char * const gimple_alloc_kind_names[] = {
90 "assignments",
91 "phi nodes",
92 "conditionals",
93 "everything else"
96 #endif /* GATHER_STATISTICS */
98 /* Private API manipulation functions shared only with some
99 other files. */
100 extern void gimple_set_stored_syms (gimple, bitmap, bitmap_obstack *);
101 extern void gimple_set_loaded_syms (gimple, bitmap, bitmap_obstack *);
103 /* Gimple tuple constructors.
104 Note: Any constructor taking a ``gimple_seq'' as a parameter, can
105 be passed a NULL to start with an empty sequence. */
107 /* Set the code for statement G to CODE. */
109 static inline void
110 gimple_set_code (gimple g, enum gimple_code code)
112 g->gsbase.code = code;
115 /* Return the number of bytes needed to hold a GIMPLE statement with
116 code CODE. */
118 static inline size_t
119 gimple_size (enum gimple_code code)
121 return gsstruct_code_size[gss_for_code (code)];
124 /* Allocate memory for a GIMPLE statement with code CODE and NUM_OPS
125 operands. */
127 gimple
128 gimple_alloc_stat (enum gimple_code code, unsigned num_ops MEM_STAT_DECL)
130 size_t size;
131 gimple stmt;
133 size = gimple_size (code);
134 if (num_ops > 0)
135 size += sizeof (tree) * (num_ops - 1);
137 #ifdef GATHER_STATISTICS
139 enum gimple_alloc_kind kind = gimple_alloc_kind (code);
140 gimple_alloc_counts[(int) kind]++;
141 gimple_alloc_sizes[(int) kind] += size;
143 #endif
145 stmt = ggc_alloc_cleared_gimple_statement_d_stat (size PASS_MEM_STAT);
146 gimple_set_code (stmt, code);
147 gimple_set_num_ops (stmt, num_ops);
149 /* Do not call gimple_set_modified here as it has other side
150 effects and this tuple is still not completely built. */
151 stmt->gsbase.modified = 1;
152 gimple_init_singleton (stmt);
154 return stmt;
157 /* Set SUBCODE to be the code of the expression computed by statement G. */
159 static inline void
160 gimple_set_subcode (gimple g, unsigned subcode)
162 /* We only have 16 bits for the RHS code. Assert that we are not
163 overflowing it. */
164 gcc_assert (subcode < (1 << 16));
165 g->gsbase.subcode = subcode;
170 /* Build a tuple with operands. CODE is the statement to build (which
171 must be one of the GIMPLE_WITH_OPS tuples). SUBCODE is the sub-code
172 for the new tuple. NUM_OPS is the number of operands to allocate. */
174 #define gimple_build_with_ops(c, s, n) \
175 gimple_build_with_ops_stat (c, s, n MEM_STAT_INFO)
177 static gimple
178 gimple_build_with_ops_stat (enum gimple_code code, unsigned subcode,
179 unsigned num_ops MEM_STAT_DECL)
181 gimple s = gimple_alloc_stat (code, num_ops PASS_MEM_STAT);
182 gimple_set_subcode (s, subcode);
184 return s;
188 /* Build a GIMPLE_RETURN statement returning RETVAL. */
190 gimple
191 gimple_build_return (tree retval)
193 gimple s = gimple_build_with_ops (GIMPLE_RETURN, ERROR_MARK, 1);
194 if (retval)
195 gimple_return_set_retval (s, retval);
196 return s;
199 /* Reset alias information on call S. */
201 void
202 gimple_call_reset_alias_info (gimple s)
204 if (gimple_call_flags (s) & ECF_CONST)
205 memset (gimple_call_use_set (s), 0, sizeof (struct pt_solution));
206 else
207 pt_solution_reset (gimple_call_use_set (s));
208 if (gimple_call_flags (s) & (ECF_CONST|ECF_PURE|ECF_NOVOPS))
209 memset (gimple_call_clobber_set (s), 0, sizeof (struct pt_solution));
210 else
211 pt_solution_reset (gimple_call_clobber_set (s));
214 /* Helper for gimple_build_call, gimple_build_call_valist,
215 gimple_build_call_vec and gimple_build_call_from_tree. Build the basic
216 components of a GIMPLE_CALL statement to function FN with NARGS
217 arguments. */
219 static inline gimple
220 gimple_build_call_1 (tree fn, unsigned nargs)
222 gimple s = gimple_build_with_ops (GIMPLE_CALL, ERROR_MARK, nargs + 3);
223 if (TREE_CODE (fn) == FUNCTION_DECL)
224 fn = build_fold_addr_expr (fn);
225 gimple_set_op (s, 1, fn);
226 gimple_call_set_fntype (s, TREE_TYPE (TREE_TYPE (fn)));
227 gimple_call_reset_alias_info (s);
228 return s;
232 /* Build a GIMPLE_CALL statement to function FN with the arguments
233 specified in vector ARGS. */
235 gimple
236 gimple_build_call_vec (tree fn, VEC(tree, heap) *args)
238 unsigned i;
239 unsigned nargs = VEC_length (tree, args);
240 gimple call = gimple_build_call_1 (fn, nargs);
242 for (i = 0; i < nargs; i++)
243 gimple_call_set_arg (call, i, VEC_index (tree, args, i));
245 return call;
249 /* Build a GIMPLE_CALL statement to function FN. NARGS is the number of
250 arguments. The ... are the arguments. */
252 gimple
253 gimple_build_call (tree fn, unsigned nargs, ...)
255 va_list ap;
256 gimple call;
257 unsigned i;
259 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL || is_gimple_call_addr (fn));
261 call = gimple_build_call_1 (fn, nargs);
263 va_start (ap, nargs);
264 for (i = 0; i < nargs; i++)
265 gimple_call_set_arg (call, i, va_arg (ap, tree));
266 va_end (ap);
268 return call;
272 /* Build a GIMPLE_CALL statement to function FN. NARGS is the number of
273 arguments. AP contains the arguments. */
275 gimple
276 gimple_build_call_valist (tree fn, unsigned nargs, va_list ap)
278 gimple call;
279 unsigned i;
281 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL || is_gimple_call_addr (fn));
283 call = gimple_build_call_1 (fn, nargs);
285 for (i = 0; i < nargs; i++)
286 gimple_call_set_arg (call, i, va_arg (ap, tree));
288 return call;
292 /* Helper for gimple_build_call_internal and gimple_build_call_internal_vec.
293 Build the basic components of a GIMPLE_CALL statement to internal
294 function FN with NARGS arguments. */
296 static inline gimple
297 gimple_build_call_internal_1 (enum internal_fn fn, unsigned nargs)
299 gimple s = gimple_build_with_ops (GIMPLE_CALL, ERROR_MARK, nargs + 3);
300 s->gsbase.subcode |= GF_CALL_INTERNAL;
301 gimple_call_set_internal_fn (s, fn);
302 gimple_call_reset_alias_info (s);
303 return s;
307 /* Build a GIMPLE_CALL statement to internal function FN. NARGS is
308 the number of arguments. The ... are the arguments. */
310 gimple
311 gimple_build_call_internal (enum internal_fn fn, unsigned nargs, ...)
313 va_list ap;
314 gimple call;
315 unsigned i;
317 call = gimple_build_call_internal_1 (fn, nargs);
318 va_start (ap, nargs);
319 for (i = 0; i < nargs; i++)
320 gimple_call_set_arg (call, i, va_arg (ap, tree));
321 va_end (ap);
323 return call;
327 /* Build a GIMPLE_CALL statement to internal function FN with the arguments
328 specified in vector ARGS. */
330 gimple
331 gimple_build_call_internal_vec (enum internal_fn fn, VEC(tree, heap) *args)
333 unsigned i, nargs;
334 gimple call;
336 nargs = VEC_length (tree, args);
337 call = gimple_build_call_internal_1 (fn, nargs);
338 for (i = 0; i < nargs; i++)
339 gimple_call_set_arg (call, i, VEC_index (tree, args, i));
341 return call;
345 /* Build a GIMPLE_CALL statement from CALL_EXPR T. Note that T is
346 assumed to be in GIMPLE form already. Minimal checking is done of
347 this fact. */
349 gimple
350 gimple_build_call_from_tree (tree t)
352 unsigned i, nargs;
353 gimple call;
354 tree fndecl = get_callee_fndecl (t);
356 gcc_assert (TREE_CODE (t) == CALL_EXPR);
358 nargs = call_expr_nargs (t);
359 call = gimple_build_call_1 (fndecl ? fndecl : CALL_EXPR_FN (t), nargs);
361 for (i = 0; i < nargs; i++)
362 gimple_call_set_arg (call, i, CALL_EXPR_ARG (t, i));
364 gimple_set_block (call, TREE_BLOCK (t));
366 /* Carry all the CALL_EXPR flags to the new GIMPLE_CALL. */
367 gimple_call_set_chain (call, CALL_EXPR_STATIC_CHAIN (t));
368 gimple_call_set_tail (call, CALL_EXPR_TAILCALL (t));
369 gimple_call_set_return_slot_opt (call, CALL_EXPR_RETURN_SLOT_OPT (t));
370 if (fndecl
371 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
372 && (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_ALLOCA
373 || DECL_FUNCTION_CODE (fndecl) == BUILT_IN_ALLOCA_WITH_ALIGN))
374 gimple_call_set_alloca_for_var (call, CALL_ALLOCA_FOR_VAR_P (t));
375 else
376 gimple_call_set_from_thunk (call, CALL_FROM_THUNK_P (t));
377 gimple_call_set_va_arg_pack (call, CALL_EXPR_VA_ARG_PACK (t));
378 gimple_call_set_nothrow (call, TREE_NOTHROW (t));
379 gimple_set_no_warning (call, TREE_NO_WARNING (t));
381 return call;
385 /* Extract the operands and code for expression EXPR into *SUBCODE_P,
386 *OP1_P, *OP2_P and *OP3_P respectively. */
388 void
389 extract_ops_from_tree_1 (tree expr, enum tree_code *subcode_p, tree *op1_p,
390 tree *op2_p, tree *op3_p)
392 enum gimple_rhs_class grhs_class;
394 *subcode_p = TREE_CODE (expr);
395 grhs_class = get_gimple_rhs_class (*subcode_p);
397 if (grhs_class == GIMPLE_TERNARY_RHS)
399 *op1_p = TREE_OPERAND (expr, 0);
400 *op2_p = TREE_OPERAND (expr, 1);
401 *op3_p = TREE_OPERAND (expr, 2);
403 else if (grhs_class == GIMPLE_BINARY_RHS)
405 *op1_p = TREE_OPERAND (expr, 0);
406 *op2_p = TREE_OPERAND (expr, 1);
407 *op3_p = NULL_TREE;
409 else if (grhs_class == GIMPLE_UNARY_RHS)
411 *op1_p = TREE_OPERAND (expr, 0);
412 *op2_p = NULL_TREE;
413 *op3_p = NULL_TREE;
415 else if (grhs_class == GIMPLE_SINGLE_RHS)
417 *op1_p = expr;
418 *op2_p = NULL_TREE;
419 *op3_p = NULL_TREE;
421 else
422 gcc_unreachable ();
426 /* Build a GIMPLE_ASSIGN statement.
428 LHS of the assignment.
429 RHS of the assignment which can be unary or binary. */
431 gimple
432 gimple_build_assign_stat (tree lhs, tree rhs MEM_STAT_DECL)
434 enum tree_code subcode;
435 tree op1, op2, op3;
437 extract_ops_from_tree_1 (rhs, &subcode, &op1, &op2, &op3);
438 return gimple_build_assign_with_ops_stat (subcode, lhs, op1, op2, op3
439 PASS_MEM_STAT);
443 /* Build a GIMPLE_ASSIGN statement with sub-code SUBCODE and operands
444 OP1 and OP2. If OP2 is NULL then SUBCODE must be of class
445 GIMPLE_UNARY_RHS or GIMPLE_SINGLE_RHS. */
447 gimple
448 gimple_build_assign_with_ops_stat (enum tree_code subcode, tree lhs, tree op1,
449 tree op2, tree op3 MEM_STAT_DECL)
451 unsigned num_ops;
452 gimple p;
454 /* Need 1 operand for LHS and 1 or 2 for the RHS (depending on the
455 code). */
456 num_ops = get_gimple_rhs_num_ops (subcode) + 1;
458 p = gimple_build_with_ops_stat (GIMPLE_ASSIGN, (unsigned)subcode, num_ops
459 PASS_MEM_STAT);
460 gimple_assign_set_lhs (p, lhs);
461 gimple_assign_set_rhs1 (p, op1);
462 if (op2)
464 gcc_assert (num_ops > 2);
465 gimple_assign_set_rhs2 (p, op2);
468 if (op3)
470 gcc_assert (num_ops > 3);
471 gimple_assign_set_rhs3 (p, op3);
474 return p;
478 /* Build a new GIMPLE_ASSIGN tuple and append it to the end of *SEQ_P.
480 DST/SRC are the destination and source respectively. You can pass
481 ungimplified trees in DST or SRC, in which case they will be
482 converted to a gimple operand if necessary.
484 This function returns the newly created GIMPLE_ASSIGN tuple. */
486 gimple
487 gimplify_assign (tree dst, tree src, gimple_seq *seq_p)
489 tree t = build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, src);
490 gimplify_and_add (t, seq_p);
491 ggc_free (t);
492 return gimple_seq_last_stmt (*seq_p);
496 /* Build a GIMPLE_COND statement.
498 PRED is the condition used to compare LHS and the RHS.
499 T_LABEL is the label to jump to if the condition is true.
500 F_LABEL is the label to jump to otherwise. */
502 gimple
503 gimple_build_cond (enum tree_code pred_code, tree lhs, tree rhs,
504 tree t_label, tree f_label)
506 gimple p;
508 gcc_assert (TREE_CODE_CLASS (pred_code) == tcc_comparison);
509 p = gimple_build_with_ops (GIMPLE_COND, pred_code, 4);
510 gimple_cond_set_lhs (p, lhs);
511 gimple_cond_set_rhs (p, rhs);
512 gimple_cond_set_true_label (p, t_label);
513 gimple_cond_set_false_label (p, f_label);
514 return p;
518 /* Extract operands for a GIMPLE_COND statement out of COND_EXPR tree COND. */
520 void
521 gimple_cond_get_ops_from_tree (tree cond, enum tree_code *code_p,
522 tree *lhs_p, tree *rhs_p)
524 gcc_assert (TREE_CODE_CLASS (TREE_CODE (cond)) == tcc_comparison
525 || TREE_CODE (cond) == TRUTH_NOT_EXPR
526 || is_gimple_min_invariant (cond)
527 || SSA_VAR_P (cond));
529 extract_ops_from_tree (cond, code_p, lhs_p, rhs_p);
531 /* Canonicalize conditionals of the form 'if (!VAL)'. */
532 if (*code_p == TRUTH_NOT_EXPR)
534 *code_p = EQ_EXPR;
535 gcc_assert (*lhs_p && *rhs_p == NULL_TREE);
536 *rhs_p = build_zero_cst (TREE_TYPE (*lhs_p));
538 /* Canonicalize conditionals of the form 'if (VAL)' */
539 else if (TREE_CODE_CLASS (*code_p) != tcc_comparison)
541 *code_p = NE_EXPR;
542 gcc_assert (*lhs_p && *rhs_p == NULL_TREE);
543 *rhs_p = build_zero_cst (TREE_TYPE (*lhs_p));
548 /* Build a GIMPLE_COND statement from the conditional expression tree
549 COND. T_LABEL and F_LABEL are as in gimple_build_cond. */
551 gimple
552 gimple_build_cond_from_tree (tree cond, tree t_label, tree f_label)
554 enum tree_code code;
555 tree lhs, rhs;
557 gimple_cond_get_ops_from_tree (cond, &code, &lhs, &rhs);
558 return gimple_build_cond (code, lhs, rhs, t_label, f_label);
561 /* Set code, lhs, and rhs of a GIMPLE_COND from a suitable
562 boolean expression tree COND. */
564 void
565 gimple_cond_set_condition_from_tree (gimple stmt, tree cond)
567 enum tree_code code;
568 tree lhs, rhs;
570 gimple_cond_get_ops_from_tree (cond, &code, &lhs, &rhs);
571 gimple_cond_set_condition (stmt, code, lhs, rhs);
574 /* Build a GIMPLE_LABEL statement for LABEL. */
576 gimple
577 gimple_build_label (tree label)
579 gimple p = gimple_build_with_ops (GIMPLE_LABEL, ERROR_MARK, 1);
580 gimple_label_set_label (p, label);
581 return p;
584 /* Build a GIMPLE_GOTO statement to label DEST. */
586 gimple
587 gimple_build_goto (tree dest)
589 gimple p = gimple_build_with_ops (GIMPLE_GOTO, ERROR_MARK, 1);
590 gimple_goto_set_dest (p, dest);
591 return p;
595 /* Build a GIMPLE_NOP statement. */
597 gimple
598 gimple_build_nop (void)
600 return gimple_alloc (GIMPLE_NOP, 0);
604 /* Build a GIMPLE_BIND statement.
605 VARS are the variables in BODY.
606 BLOCK is the containing block. */
608 gimple
609 gimple_build_bind (tree vars, gimple_seq body, tree block)
611 gimple p = gimple_alloc (GIMPLE_BIND, 0);
612 gimple_bind_set_vars (p, vars);
613 if (body)
614 gimple_bind_set_body (p, body);
615 if (block)
616 gimple_bind_set_block (p, block);
617 return p;
620 /* Helper function to set the simple fields of a asm stmt.
622 STRING is a pointer to a string that is the asm blocks assembly code.
623 NINPUT is the number of register inputs.
624 NOUTPUT is the number of register outputs.
625 NCLOBBERS is the number of clobbered registers.
628 static inline gimple
629 gimple_build_asm_1 (const char *string, unsigned ninputs, unsigned noutputs,
630 unsigned nclobbers, unsigned nlabels)
632 gimple p;
633 int size = strlen (string);
635 /* ASMs with labels cannot have outputs. This should have been
636 enforced by the front end. */
637 gcc_assert (nlabels == 0 || noutputs == 0);
639 p = gimple_build_with_ops (GIMPLE_ASM, ERROR_MARK,
640 ninputs + noutputs + nclobbers + nlabels);
642 p->gimple_asm.ni = ninputs;
643 p->gimple_asm.no = noutputs;
644 p->gimple_asm.nc = nclobbers;
645 p->gimple_asm.nl = nlabels;
646 p->gimple_asm.string = ggc_alloc_string (string, size);
648 #ifdef GATHER_STATISTICS
649 gimple_alloc_sizes[(int) gimple_alloc_kind (GIMPLE_ASM)] += size;
650 #endif
652 return p;
655 /* Build a GIMPLE_ASM statement.
657 STRING is the assembly code.
658 NINPUT is the number of register inputs.
659 NOUTPUT is the number of register outputs.
660 NCLOBBERS is the number of clobbered registers.
661 INPUTS is a vector of the input register parameters.
662 OUTPUTS is a vector of the output register parameters.
663 CLOBBERS is a vector of the clobbered register parameters.
664 LABELS is a vector of destination labels. */
666 gimple
667 gimple_build_asm_vec (const char *string, VEC(tree,gc)* inputs,
668 VEC(tree,gc)* outputs, VEC(tree,gc)* clobbers,
669 VEC(tree,gc)* labels)
671 gimple p;
672 unsigned i;
674 p = gimple_build_asm_1 (string,
675 VEC_length (tree, inputs),
676 VEC_length (tree, outputs),
677 VEC_length (tree, clobbers),
678 VEC_length (tree, labels));
680 for (i = 0; i < VEC_length (tree, inputs); i++)
681 gimple_asm_set_input_op (p, i, VEC_index (tree, inputs, i));
683 for (i = 0; i < VEC_length (tree, outputs); i++)
684 gimple_asm_set_output_op (p, i, VEC_index (tree, outputs, i));
686 for (i = 0; i < VEC_length (tree, clobbers); i++)
687 gimple_asm_set_clobber_op (p, i, VEC_index (tree, clobbers, i));
689 for (i = 0; i < VEC_length (tree, labels); i++)
690 gimple_asm_set_label_op (p, i, VEC_index (tree, labels, i));
692 return p;
695 /* Build a GIMPLE_CATCH statement.
697 TYPES are the catch types.
698 HANDLER is the exception handler. */
700 gimple
701 gimple_build_catch (tree types, gimple_seq handler)
703 gimple p = gimple_alloc (GIMPLE_CATCH, 0);
704 gimple_catch_set_types (p, types);
705 if (handler)
706 gimple_catch_set_handler (p, handler);
708 return p;
711 /* Build a GIMPLE_EH_FILTER statement.
713 TYPES are the filter's types.
714 FAILURE is the filter's failure action. */
716 gimple
717 gimple_build_eh_filter (tree types, gimple_seq failure)
719 gimple p = gimple_alloc (GIMPLE_EH_FILTER, 0);
720 gimple_eh_filter_set_types (p, types);
721 if (failure)
722 gimple_eh_filter_set_failure (p, failure);
724 return p;
727 /* Build a GIMPLE_EH_MUST_NOT_THROW statement. */
729 gimple
730 gimple_build_eh_must_not_throw (tree decl)
732 gimple p = gimple_alloc (GIMPLE_EH_MUST_NOT_THROW, 0);
734 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
735 gcc_assert (flags_from_decl_or_type (decl) & ECF_NORETURN);
736 gimple_eh_must_not_throw_set_fndecl (p, decl);
738 return p;
741 /* Build a GIMPLE_EH_ELSE statement. */
743 gimple
744 gimple_build_eh_else (gimple_seq n_body, gimple_seq e_body)
746 gimple p = gimple_alloc (GIMPLE_EH_ELSE, 0);
747 gimple_eh_else_set_n_body (p, n_body);
748 gimple_eh_else_set_e_body (p, e_body);
749 return p;
752 /* Build a GIMPLE_TRY statement.
754 EVAL is the expression to evaluate.
755 CLEANUP is the cleanup expression.
756 KIND is either GIMPLE_TRY_CATCH or GIMPLE_TRY_FINALLY depending on
757 whether this is a try/catch or a try/finally respectively. */
759 gimple
760 gimple_build_try (gimple_seq eval, gimple_seq cleanup,
761 enum gimple_try_flags kind)
763 gimple p;
765 gcc_assert (kind == GIMPLE_TRY_CATCH || kind == GIMPLE_TRY_FINALLY);
766 p = gimple_alloc (GIMPLE_TRY, 0);
767 gimple_set_subcode (p, kind);
768 if (eval)
769 gimple_try_set_eval (p, eval);
770 if (cleanup)
771 gimple_try_set_cleanup (p, cleanup);
773 return p;
776 /* Construct a GIMPLE_WITH_CLEANUP_EXPR statement.
778 CLEANUP is the cleanup expression. */
780 gimple
781 gimple_build_wce (gimple_seq cleanup)
783 gimple p = gimple_alloc (GIMPLE_WITH_CLEANUP_EXPR, 0);
784 if (cleanup)
785 gimple_wce_set_cleanup (p, cleanup);
787 return p;
791 /* Build a GIMPLE_RESX statement. */
793 gimple
794 gimple_build_resx (int region)
796 gimple p = gimple_build_with_ops (GIMPLE_RESX, ERROR_MARK, 0);
797 p->gimple_eh_ctrl.region = region;
798 return p;
802 /* The helper for constructing a gimple switch statement.
803 INDEX is the switch's index.
804 NLABELS is the number of labels in the switch excluding the default.
805 DEFAULT_LABEL is the default label for the switch statement. */
807 gimple
808 gimple_build_switch_nlabels (unsigned nlabels, tree index, tree default_label)
810 /* nlabels + 1 default label + 1 index. */
811 gimple p = gimple_build_with_ops (GIMPLE_SWITCH, ERROR_MARK,
812 1 + (default_label != NULL) + nlabels);
813 gimple_switch_set_index (p, index);
814 if (default_label)
815 gimple_switch_set_default_label (p, default_label);
816 return p;
820 /* Build a GIMPLE_SWITCH statement.
822 INDEX is the switch's index.
823 NLABELS is the number of labels in the switch excluding the DEFAULT_LABEL.
824 ... are the labels excluding the default. */
826 gimple
827 gimple_build_switch (unsigned nlabels, tree index, tree default_label, ...)
829 va_list al;
830 unsigned i, offset;
831 gimple p = gimple_build_switch_nlabels (nlabels, index, default_label);
833 /* Store the rest of the labels. */
834 va_start (al, default_label);
835 offset = (default_label != NULL);
836 for (i = 0; i < nlabels; i++)
837 gimple_switch_set_label (p, i + offset, va_arg (al, tree));
838 va_end (al);
840 return p;
844 /* Build a GIMPLE_SWITCH statement.
846 INDEX is the switch's index.
847 DEFAULT_LABEL is the default label
848 ARGS is a vector of labels excluding the default. */
850 gimple
851 gimple_build_switch_vec (tree index, tree default_label, VEC(tree, heap) *args)
853 unsigned i, offset, nlabels = VEC_length (tree, args);
854 gimple p = gimple_build_switch_nlabels (nlabels, index, default_label);
856 /* Copy the labels from the vector to the switch statement. */
857 offset = (default_label != NULL);
858 for (i = 0; i < nlabels; i++)
859 gimple_switch_set_label (p, i + offset, VEC_index (tree, args, i));
861 return p;
864 /* Build a GIMPLE_EH_DISPATCH statement. */
866 gimple
867 gimple_build_eh_dispatch (int region)
869 gimple p = gimple_build_with_ops (GIMPLE_EH_DISPATCH, ERROR_MARK, 0);
870 p->gimple_eh_ctrl.region = region;
871 return p;
874 /* Build a new GIMPLE_DEBUG_BIND statement.
876 VAR is bound to VALUE; block and location are taken from STMT. */
878 gimple
879 gimple_build_debug_bind_stat (tree var, tree value, gimple stmt MEM_STAT_DECL)
881 gimple p = gimple_build_with_ops_stat (GIMPLE_DEBUG,
882 (unsigned)GIMPLE_DEBUG_BIND, 2
883 PASS_MEM_STAT);
885 gimple_debug_bind_set_var (p, var);
886 gimple_debug_bind_set_value (p, value);
887 if (stmt)
889 gimple_set_block (p, gimple_block (stmt));
890 gimple_set_location (p, gimple_location (stmt));
893 return p;
897 /* Build a new GIMPLE_DEBUG_SOURCE_BIND statement.
899 VAR is bound to VALUE; block and location are taken from STMT. */
901 gimple
902 gimple_build_debug_source_bind_stat (tree var, tree value,
903 gimple stmt MEM_STAT_DECL)
905 gimple p = gimple_build_with_ops_stat (GIMPLE_DEBUG,
906 (unsigned)GIMPLE_DEBUG_SOURCE_BIND, 2
907 PASS_MEM_STAT);
909 gimple_debug_source_bind_set_var (p, var);
910 gimple_debug_source_bind_set_value (p, value);
911 if (stmt)
913 gimple_set_block (p, gimple_block (stmt));
914 gimple_set_location (p, gimple_location (stmt));
917 return p;
921 /* Build a GIMPLE_OMP_CRITICAL statement.
923 BODY is the sequence of statements for which only one thread can execute.
924 NAME is optional identifier for this critical block. */
926 gimple
927 gimple_build_omp_critical (gimple_seq body, tree name)
929 gimple p = gimple_alloc (GIMPLE_OMP_CRITICAL, 0);
930 gimple_omp_critical_set_name (p, name);
931 if (body)
932 gimple_omp_set_body (p, body);
934 return p;
937 /* Build a GIMPLE_OMP_FOR statement.
939 BODY is sequence of statements inside the for loop.
940 CLAUSES, are any of the OMP loop construct's clauses: private, firstprivate,
941 lastprivate, reductions, ordered, schedule, and nowait.
942 COLLAPSE is the collapse count.
943 PRE_BODY is the sequence of statements that are loop invariant. */
945 gimple
946 gimple_build_omp_for (gimple_seq body, tree clauses, size_t collapse,
947 gimple_seq pre_body)
949 gimple p = gimple_alloc (GIMPLE_OMP_FOR, 0);
950 if (body)
951 gimple_omp_set_body (p, body);
952 gimple_omp_for_set_clauses (p, clauses);
953 p->gimple_omp_for.collapse = collapse;
954 p->gimple_omp_for.iter
955 = ggc_alloc_cleared_vec_gimple_omp_for_iter (collapse);
956 if (pre_body)
957 gimple_omp_for_set_pre_body (p, pre_body);
959 return p;
963 /* Build a GIMPLE_OMP_PARALLEL statement.
965 BODY is sequence of statements which are executed in parallel.
966 CLAUSES, are the OMP parallel construct's clauses.
967 CHILD_FN is the function created for the parallel threads to execute.
968 DATA_ARG are the shared data argument(s). */
970 gimple
971 gimple_build_omp_parallel (gimple_seq body, tree clauses, tree child_fn,
972 tree data_arg)
974 gimple p = gimple_alloc (GIMPLE_OMP_PARALLEL, 0);
975 if (body)
976 gimple_omp_set_body (p, body);
977 gimple_omp_parallel_set_clauses (p, clauses);
978 gimple_omp_parallel_set_child_fn (p, child_fn);
979 gimple_omp_parallel_set_data_arg (p, data_arg);
981 return p;
985 /* Build a GIMPLE_OMP_TASK statement.
987 BODY is sequence of statements which are executed by the explicit task.
988 CLAUSES, are the OMP parallel construct's clauses.
989 CHILD_FN is the function created for the parallel threads to execute.
990 DATA_ARG are the shared data argument(s).
991 COPY_FN is the optional function for firstprivate initialization.
992 ARG_SIZE and ARG_ALIGN are size and alignment of the data block. */
994 gimple
995 gimple_build_omp_task (gimple_seq body, tree clauses, tree child_fn,
996 tree data_arg, tree copy_fn, tree arg_size,
997 tree arg_align)
999 gimple p = gimple_alloc (GIMPLE_OMP_TASK, 0);
1000 if (body)
1001 gimple_omp_set_body (p, body);
1002 gimple_omp_task_set_clauses (p, clauses);
1003 gimple_omp_task_set_child_fn (p, child_fn);
1004 gimple_omp_task_set_data_arg (p, data_arg);
1005 gimple_omp_task_set_copy_fn (p, copy_fn);
1006 gimple_omp_task_set_arg_size (p, arg_size);
1007 gimple_omp_task_set_arg_align (p, arg_align);
1009 return p;
1013 /* Build a GIMPLE_OMP_SECTION statement for a sections statement.
1015 BODY is the sequence of statements in the section. */
1017 gimple
1018 gimple_build_omp_section (gimple_seq body)
1020 gimple p = gimple_alloc (GIMPLE_OMP_SECTION, 0);
1021 if (body)
1022 gimple_omp_set_body (p, body);
1024 return p;
1028 /* Build a GIMPLE_OMP_MASTER statement.
1030 BODY is the sequence of statements to be executed by just the master. */
1032 gimple
1033 gimple_build_omp_master (gimple_seq body)
1035 gimple p = gimple_alloc (GIMPLE_OMP_MASTER, 0);
1036 if (body)
1037 gimple_omp_set_body (p, body);
1039 return p;
1043 /* Build a GIMPLE_OMP_CONTINUE statement.
1045 CONTROL_DEF is the definition of the control variable.
1046 CONTROL_USE is the use of the control variable. */
1048 gimple
1049 gimple_build_omp_continue (tree control_def, tree control_use)
1051 gimple p = gimple_alloc (GIMPLE_OMP_CONTINUE, 0);
1052 gimple_omp_continue_set_control_def (p, control_def);
1053 gimple_omp_continue_set_control_use (p, control_use);
1054 return p;
1057 /* Build a GIMPLE_OMP_ORDERED statement.
1059 BODY is the sequence of statements inside a loop that will executed in
1060 sequence. */
1062 gimple
1063 gimple_build_omp_ordered (gimple_seq body)
1065 gimple p = gimple_alloc (GIMPLE_OMP_ORDERED, 0);
1066 if (body)
1067 gimple_omp_set_body (p, body);
1069 return p;
1073 /* Build a GIMPLE_OMP_RETURN statement.
1074 WAIT_P is true if this is a non-waiting return. */
1076 gimple
1077 gimple_build_omp_return (bool wait_p)
1079 gimple p = gimple_alloc (GIMPLE_OMP_RETURN, 0);
1080 if (wait_p)
1081 gimple_omp_return_set_nowait (p);
1083 return p;
1087 /* Build a GIMPLE_OMP_SECTIONS statement.
1089 BODY is a sequence of section statements.
1090 CLAUSES are any of the OMP sections contsruct's clauses: private,
1091 firstprivate, lastprivate, reduction, and nowait. */
1093 gimple
1094 gimple_build_omp_sections (gimple_seq body, tree clauses)
1096 gimple p = gimple_alloc (GIMPLE_OMP_SECTIONS, 0);
1097 if (body)
1098 gimple_omp_set_body (p, body);
1099 gimple_omp_sections_set_clauses (p, clauses);
1101 return p;
1105 /* Build a GIMPLE_OMP_SECTIONS_SWITCH. */
1107 gimple
1108 gimple_build_omp_sections_switch (void)
1110 return gimple_alloc (GIMPLE_OMP_SECTIONS_SWITCH, 0);
1114 /* Build a GIMPLE_OMP_SINGLE statement.
1116 BODY is the sequence of statements that will be executed once.
1117 CLAUSES are any of the OMP single construct's clauses: private, firstprivate,
1118 copyprivate, nowait. */
1120 gimple
1121 gimple_build_omp_single (gimple_seq body, tree clauses)
1123 gimple p = gimple_alloc (GIMPLE_OMP_SINGLE, 0);
1124 if (body)
1125 gimple_omp_set_body (p, body);
1126 gimple_omp_single_set_clauses (p, clauses);
1128 return p;
1132 /* Build a GIMPLE_OMP_ATOMIC_LOAD statement. */
1134 gimple
1135 gimple_build_omp_atomic_load (tree lhs, tree rhs)
1137 gimple p = gimple_alloc (GIMPLE_OMP_ATOMIC_LOAD, 0);
1138 gimple_omp_atomic_load_set_lhs (p, lhs);
1139 gimple_omp_atomic_load_set_rhs (p, rhs);
1140 return p;
1143 /* Build a GIMPLE_OMP_ATOMIC_STORE statement.
1145 VAL is the value we are storing. */
1147 gimple
1148 gimple_build_omp_atomic_store (tree val)
1150 gimple p = gimple_alloc (GIMPLE_OMP_ATOMIC_STORE, 0);
1151 gimple_omp_atomic_store_set_val (p, val);
1152 return p;
1155 /* Build a GIMPLE_TRANSACTION statement. */
1157 gimple
1158 gimple_build_transaction (gimple_seq body, tree label)
1160 gimple p = gimple_alloc (GIMPLE_TRANSACTION, 0);
1161 gimple_transaction_set_body (p, body);
1162 gimple_transaction_set_label (p, label);
1163 return p;
1166 /* Build a GIMPLE_PREDICT statement. PREDICT is one of the predictors from
1167 predict.def, OUTCOME is NOT_TAKEN or TAKEN. */
1169 gimple
1170 gimple_build_predict (enum br_predictor predictor, enum prediction outcome)
1172 gimple p = gimple_alloc (GIMPLE_PREDICT, 0);
1173 /* Ensure all the predictors fit into the lower bits of the subcode. */
1174 gcc_assert ((int) END_PREDICTORS <= GF_PREDICT_TAKEN);
1175 gimple_predict_set_predictor (p, predictor);
1176 gimple_predict_set_outcome (p, outcome);
1177 return p;
1180 #if defined ENABLE_GIMPLE_CHECKING
1181 /* Complain of a gimple type mismatch and die. */
1183 void
1184 gimple_check_failed (const_gimple gs, const char *file, int line,
1185 const char *function, enum gimple_code code,
1186 enum tree_code subcode)
1188 internal_error ("gimple check: expected %s(%s), have %s(%s) in %s, at %s:%d",
1189 gimple_code_name[code],
1190 tree_code_name[subcode],
1191 gimple_code_name[gimple_code (gs)],
1192 gs->gsbase.subcode > 0
1193 ? tree_code_name[gs->gsbase.subcode]
1194 : "",
1195 function, trim_filename (file), line);
1197 #endif /* ENABLE_GIMPLE_CHECKING */
1200 /* Link gimple statement GS to the end of the sequence *SEQ_P. If
1201 *SEQ_P is NULL, a new sequence is allocated. */
1203 void
1204 gimple_seq_add_stmt (gimple_seq *seq_p, gimple gs)
1206 gimple_stmt_iterator si;
1207 if (gs == NULL)
1208 return;
1210 si = gsi_last (*seq_p);
1211 gsi_insert_after (&si, gs, GSI_NEW_STMT);
1215 /* Append sequence SRC to the end of sequence *DST_P. If *DST_P is
1216 NULL, a new sequence is allocated. */
1218 void
1219 gimple_seq_add_seq (gimple_seq *dst_p, gimple_seq src)
1221 gimple_stmt_iterator si;
1222 if (src == NULL)
1223 return;
1225 si = gsi_last (*dst_p);
1226 gsi_insert_seq_after (&si, src, GSI_NEW_STMT);
1230 /* Helper function of empty_body_p. Return true if STMT is an empty
1231 statement. */
1233 static bool
1234 empty_stmt_p (gimple stmt)
1236 if (gimple_code (stmt) == GIMPLE_NOP)
1237 return true;
1238 if (gimple_code (stmt) == GIMPLE_BIND)
1239 return empty_body_p (gimple_bind_body (stmt));
1240 return false;
1244 /* Return true if BODY contains nothing but empty statements. */
1246 bool
1247 empty_body_p (gimple_seq body)
1249 gimple_stmt_iterator i;
1251 if (gimple_seq_empty_p (body))
1252 return true;
1253 for (i = gsi_start (body); !gsi_end_p (i); gsi_next (&i))
1254 if (!empty_stmt_p (gsi_stmt (i))
1255 && !is_gimple_debug (gsi_stmt (i)))
1256 return false;
1258 return true;
1262 /* Perform a deep copy of sequence SRC and return the result. */
1264 gimple_seq
1265 gimple_seq_copy (gimple_seq src)
1267 gimple_stmt_iterator gsi;
1268 gimple_seq new_seq = NULL;
1269 gimple stmt;
1271 for (gsi = gsi_start (src); !gsi_end_p (gsi); gsi_next (&gsi))
1273 stmt = gimple_copy (gsi_stmt (gsi));
1274 gimple_seq_add_stmt (&new_seq, stmt);
1277 return new_seq;
1281 /* Walk all the statements in the sequence *PSEQ calling walk_gimple_stmt
1282 on each one. WI is as in walk_gimple_stmt.
1284 If walk_gimple_stmt returns non-NULL, the walk is stopped, and the
1285 value is stored in WI->CALLBACK_RESULT. Also, the statement that
1286 produced the value is returned if this statement has not been
1287 removed by a callback (wi->removed_stmt). If the statement has
1288 been removed, NULL is returned.
1290 Otherwise, all the statements are walked and NULL returned. */
1292 gimple
1293 walk_gimple_seq_mod (gimple_seq *pseq, walk_stmt_fn callback_stmt,
1294 walk_tree_fn callback_op, struct walk_stmt_info *wi)
1296 gimple_stmt_iterator gsi;
1298 for (gsi = gsi_start (*pseq); !gsi_end_p (gsi); )
1300 tree ret = walk_gimple_stmt (&gsi, callback_stmt, callback_op, wi);
1301 if (ret)
1303 /* If CALLBACK_STMT or CALLBACK_OP return a value, WI must exist
1304 to hold it. */
1305 gcc_assert (wi);
1306 wi->callback_result = ret;
1308 return wi->removed_stmt ? NULL : gsi_stmt (gsi);
1311 if (!wi->removed_stmt)
1312 gsi_next (&gsi);
1315 if (wi)
1316 wi->callback_result = NULL_TREE;
1318 return NULL;
1322 /* Like walk_gimple_seq_mod, but ensure that the head of SEQ isn't
1323 changed by the callbacks. */
1325 gimple
1326 walk_gimple_seq (gimple_seq seq, walk_stmt_fn callback_stmt,
1327 walk_tree_fn callback_op, struct walk_stmt_info *wi)
1329 gimple_seq seq2 = seq;
1330 gimple ret = walk_gimple_seq_mod (&seq2, callback_stmt, callback_op, wi);
1331 gcc_assert (seq2 == seq);
1332 return ret;
1336 /* Helper function for walk_gimple_stmt. Walk operands of a GIMPLE_ASM. */
1338 static tree
1339 walk_gimple_asm (gimple stmt, walk_tree_fn callback_op,
1340 struct walk_stmt_info *wi)
1342 tree ret, op;
1343 unsigned noutputs;
1344 const char **oconstraints;
1345 unsigned i, n;
1346 const char *constraint;
1347 bool allows_mem, allows_reg, is_inout;
1349 noutputs = gimple_asm_noutputs (stmt);
1350 oconstraints = (const char **) alloca ((noutputs) * sizeof (const char *));
1352 if (wi)
1353 wi->is_lhs = true;
1355 for (i = 0; i < noutputs; i++)
1357 op = gimple_asm_output_op (stmt, i);
1358 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (op)));
1359 oconstraints[i] = constraint;
1360 parse_output_constraint (&constraint, i, 0, 0, &allows_mem, &allows_reg,
1361 &is_inout);
1362 if (wi)
1363 wi->val_only = (allows_reg || !allows_mem);
1364 ret = walk_tree (&TREE_VALUE (op), callback_op, wi, NULL);
1365 if (ret)
1366 return ret;
1369 n = gimple_asm_ninputs (stmt);
1370 for (i = 0; i < n; i++)
1372 op = gimple_asm_input_op (stmt, i);
1373 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (op)));
1374 parse_input_constraint (&constraint, 0, 0, noutputs, 0,
1375 oconstraints, &allows_mem, &allows_reg);
1376 if (wi)
1378 wi->val_only = (allows_reg || !allows_mem);
1379 /* Although input "m" is not really a LHS, we need a lvalue. */
1380 wi->is_lhs = !wi->val_only;
1382 ret = walk_tree (&TREE_VALUE (op), callback_op, wi, NULL);
1383 if (ret)
1384 return ret;
1387 if (wi)
1389 wi->is_lhs = false;
1390 wi->val_only = true;
1393 n = gimple_asm_nlabels (stmt);
1394 for (i = 0; i < n; i++)
1396 op = gimple_asm_label_op (stmt, i);
1397 ret = walk_tree (&TREE_VALUE (op), callback_op, wi, NULL);
1398 if (ret)
1399 return ret;
1402 return NULL_TREE;
1406 /* Helper function of WALK_GIMPLE_STMT. Walk every tree operand in
1407 STMT. CALLBACK_OP and WI are as in WALK_GIMPLE_STMT.
1409 CALLBACK_OP is called on each operand of STMT via walk_tree.
1410 Additional parameters to walk_tree must be stored in WI. For each operand
1411 OP, walk_tree is called as:
1413 walk_tree (&OP, CALLBACK_OP, WI, WI->PSET)
1415 If CALLBACK_OP returns non-NULL for an operand, the remaining
1416 operands are not scanned.
1418 The return value is that returned by the last call to walk_tree, or
1419 NULL_TREE if no CALLBACK_OP is specified. */
1421 tree
1422 walk_gimple_op (gimple stmt, walk_tree_fn callback_op,
1423 struct walk_stmt_info *wi)
1425 struct pointer_set_t *pset = (wi) ? wi->pset : NULL;
1426 unsigned i;
1427 tree ret = NULL_TREE;
1429 switch (gimple_code (stmt))
1431 case GIMPLE_ASSIGN:
1432 /* Walk the RHS operands. If the LHS is of a non-renamable type or
1433 is a register variable, we may use a COMPONENT_REF on the RHS. */
1434 if (wi)
1436 tree lhs = gimple_assign_lhs (stmt);
1437 wi->val_only
1438 = (is_gimple_reg_type (TREE_TYPE (lhs)) && !is_gimple_reg (lhs))
1439 || gimple_assign_rhs_class (stmt) != GIMPLE_SINGLE_RHS;
1442 for (i = 1; i < gimple_num_ops (stmt); i++)
1444 ret = walk_tree (gimple_op_ptr (stmt, i), callback_op, wi,
1445 pset);
1446 if (ret)
1447 return ret;
1450 /* Walk the LHS. If the RHS is appropriate for a memory, we
1451 may use a COMPONENT_REF on the LHS. */
1452 if (wi)
1454 /* If the RHS is of a non-renamable type or is a register variable,
1455 we may use a COMPONENT_REF on the LHS. */
1456 tree rhs1 = gimple_assign_rhs1 (stmt);
1457 wi->val_only
1458 = (is_gimple_reg_type (TREE_TYPE (rhs1)) && !is_gimple_reg (rhs1))
1459 || gimple_assign_rhs_class (stmt) != GIMPLE_SINGLE_RHS;
1460 wi->is_lhs = true;
1463 ret = walk_tree (gimple_op_ptr (stmt, 0), callback_op, wi, pset);
1464 if (ret)
1465 return ret;
1467 if (wi)
1469 wi->val_only = true;
1470 wi->is_lhs = false;
1472 break;
1474 case GIMPLE_CALL:
1475 if (wi)
1477 wi->is_lhs = false;
1478 wi->val_only = true;
1481 ret = walk_tree (gimple_call_chain_ptr (stmt), callback_op, wi, pset);
1482 if (ret)
1483 return ret;
1485 ret = walk_tree (gimple_call_fn_ptr (stmt), callback_op, wi, pset);
1486 if (ret)
1487 return ret;
1489 for (i = 0; i < gimple_call_num_args (stmt); i++)
1491 if (wi)
1492 wi->val_only
1493 = is_gimple_reg_type (TREE_TYPE (gimple_call_arg (stmt, i)));
1494 ret = walk_tree (gimple_call_arg_ptr (stmt, i), callback_op, wi,
1495 pset);
1496 if (ret)
1497 return ret;
1500 if (gimple_call_lhs (stmt))
1502 if (wi)
1504 wi->is_lhs = true;
1505 wi->val_only
1506 = is_gimple_reg_type (TREE_TYPE (gimple_call_lhs (stmt)));
1509 ret = walk_tree (gimple_call_lhs_ptr (stmt), callback_op, wi, pset);
1510 if (ret)
1511 return ret;
1514 if (wi)
1516 wi->is_lhs = false;
1517 wi->val_only = true;
1519 break;
1521 case GIMPLE_CATCH:
1522 ret = walk_tree (gimple_catch_types_ptr (stmt), callback_op, wi,
1523 pset);
1524 if (ret)
1525 return ret;
1526 break;
1528 case GIMPLE_EH_FILTER:
1529 ret = walk_tree (gimple_eh_filter_types_ptr (stmt), callback_op, wi,
1530 pset);
1531 if (ret)
1532 return ret;
1533 break;
1535 case GIMPLE_ASM:
1536 ret = walk_gimple_asm (stmt, callback_op, wi);
1537 if (ret)
1538 return ret;
1539 break;
1541 case GIMPLE_OMP_CONTINUE:
1542 ret = walk_tree (gimple_omp_continue_control_def_ptr (stmt),
1543 callback_op, wi, pset);
1544 if (ret)
1545 return ret;
1547 ret = walk_tree (gimple_omp_continue_control_use_ptr (stmt),
1548 callback_op, wi, pset);
1549 if (ret)
1550 return ret;
1551 break;
1553 case GIMPLE_OMP_CRITICAL:
1554 ret = walk_tree (gimple_omp_critical_name_ptr (stmt), callback_op, wi,
1555 pset);
1556 if (ret)
1557 return ret;
1558 break;
1560 case GIMPLE_OMP_FOR:
1561 ret = walk_tree (gimple_omp_for_clauses_ptr (stmt), callback_op, wi,
1562 pset);
1563 if (ret)
1564 return ret;
1565 for (i = 0; i < gimple_omp_for_collapse (stmt); i++)
1567 ret = walk_tree (gimple_omp_for_index_ptr (stmt, i), callback_op,
1568 wi, pset);
1569 if (ret)
1570 return ret;
1571 ret = walk_tree (gimple_omp_for_initial_ptr (stmt, i), callback_op,
1572 wi, pset);
1573 if (ret)
1574 return ret;
1575 ret = walk_tree (gimple_omp_for_final_ptr (stmt, i), callback_op,
1576 wi, pset);
1577 if (ret)
1578 return ret;
1579 ret = walk_tree (gimple_omp_for_incr_ptr (stmt, i), callback_op,
1580 wi, pset);
1582 if (ret)
1583 return ret;
1584 break;
1586 case GIMPLE_OMP_PARALLEL:
1587 ret = walk_tree (gimple_omp_parallel_clauses_ptr (stmt), callback_op,
1588 wi, pset);
1589 if (ret)
1590 return ret;
1591 ret = walk_tree (gimple_omp_parallel_child_fn_ptr (stmt), callback_op,
1592 wi, pset);
1593 if (ret)
1594 return ret;
1595 ret = walk_tree (gimple_omp_parallel_data_arg_ptr (stmt), callback_op,
1596 wi, pset);
1597 if (ret)
1598 return ret;
1599 break;
1601 case GIMPLE_OMP_TASK:
1602 ret = walk_tree (gimple_omp_task_clauses_ptr (stmt), callback_op,
1603 wi, pset);
1604 if (ret)
1605 return ret;
1606 ret = walk_tree (gimple_omp_task_child_fn_ptr (stmt), callback_op,
1607 wi, pset);
1608 if (ret)
1609 return ret;
1610 ret = walk_tree (gimple_omp_task_data_arg_ptr (stmt), callback_op,
1611 wi, pset);
1612 if (ret)
1613 return ret;
1614 ret = walk_tree (gimple_omp_task_copy_fn_ptr (stmt), callback_op,
1615 wi, pset);
1616 if (ret)
1617 return ret;
1618 ret = walk_tree (gimple_omp_task_arg_size_ptr (stmt), callback_op,
1619 wi, pset);
1620 if (ret)
1621 return ret;
1622 ret = walk_tree (gimple_omp_task_arg_align_ptr (stmt), callback_op,
1623 wi, pset);
1624 if (ret)
1625 return ret;
1626 break;
1628 case GIMPLE_OMP_SECTIONS:
1629 ret = walk_tree (gimple_omp_sections_clauses_ptr (stmt), callback_op,
1630 wi, pset);
1631 if (ret)
1632 return ret;
1634 ret = walk_tree (gimple_omp_sections_control_ptr (stmt), callback_op,
1635 wi, pset);
1636 if (ret)
1637 return ret;
1639 break;
1641 case GIMPLE_OMP_SINGLE:
1642 ret = walk_tree (gimple_omp_single_clauses_ptr (stmt), callback_op, wi,
1643 pset);
1644 if (ret)
1645 return ret;
1646 break;
1648 case GIMPLE_OMP_ATOMIC_LOAD:
1649 ret = walk_tree (gimple_omp_atomic_load_lhs_ptr (stmt), callback_op, wi,
1650 pset);
1651 if (ret)
1652 return ret;
1654 ret = walk_tree (gimple_omp_atomic_load_rhs_ptr (stmt), callback_op, wi,
1655 pset);
1656 if (ret)
1657 return ret;
1658 break;
1660 case GIMPLE_OMP_ATOMIC_STORE:
1661 ret = walk_tree (gimple_omp_atomic_store_val_ptr (stmt), callback_op,
1662 wi, pset);
1663 if (ret)
1664 return ret;
1665 break;
1667 case GIMPLE_TRANSACTION:
1668 ret = walk_tree (gimple_transaction_label_ptr (stmt), callback_op,
1669 wi, pset);
1670 if (ret)
1671 return ret;
1672 break;
1674 /* Tuples that do not have operands. */
1675 case GIMPLE_NOP:
1676 case GIMPLE_RESX:
1677 case GIMPLE_OMP_RETURN:
1678 case GIMPLE_PREDICT:
1679 break;
1681 default:
1683 enum gimple_statement_structure_enum gss;
1684 gss = gimple_statement_structure (stmt);
1685 if (gss == GSS_WITH_OPS || gss == GSS_WITH_MEM_OPS)
1686 for (i = 0; i < gimple_num_ops (stmt); i++)
1688 ret = walk_tree (gimple_op_ptr (stmt, i), callback_op, wi, pset);
1689 if (ret)
1690 return ret;
1693 break;
1696 return NULL_TREE;
1700 /* Walk the current statement in GSI (optionally using traversal state
1701 stored in WI). If WI is NULL, no state is kept during traversal.
1702 The callback CALLBACK_STMT is called. If CALLBACK_STMT indicates
1703 that it has handled all the operands of the statement, its return
1704 value is returned. Otherwise, the return value from CALLBACK_STMT
1705 is discarded and its operands are scanned.
1707 If CALLBACK_STMT is NULL or it didn't handle the operands,
1708 CALLBACK_OP is called on each operand of the statement via
1709 walk_gimple_op. If walk_gimple_op returns non-NULL for any
1710 operand, the remaining operands are not scanned. In this case, the
1711 return value from CALLBACK_OP is returned.
1713 In any other case, NULL_TREE is returned. */
1715 tree
1716 walk_gimple_stmt (gimple_stmt_iterator *gsi, walk_stmt_fn callback_stmt,
1717 walk_tree_fn callback_op, struct walk_stmt_info *wi)
1719 gimple ret;
1720 tree tree_ret;
1721 gimple stmt = gsi_stmt (*gsi);
1723 if (wi)
1725 wi->gsi = *gsi;
1726 wi->removed_stmt = false;
1728 if (wi->want_locations && gimple_has_location (stmt))
1729 input_location = gimple_location (stmt);
1732 ret = NULL;
1734 /* Invoke the statement callback. Return if the callback handled
1735 all of STMT operands by itself. */
1736 if (callback_stmt)
1738 bool handled_ops = false;
1739 tree_ret = callback_stmt (gsi, &handled_ops, wi);
1740 if (handled_ops)
1741 return tree_ret;
1743 /* If CALLBACK_STMT did not handle operands, it should not have
1744 a value to return. */
1745 gcc_assert (tree_ret == NULL);
1747 if (wi && wi->removed_stmt)
1748 return NULL;
1750 /* Re-read stmt in case the callback changed it. */
1751 stmt = gsi_stmt (*gsi);
1754 /* If CALLBACK_OP is defined, invoke it on every operand of STMT. */
1755 if (callback_op)
1757 tree_ret = walk_gimple_op (stmt, callback_op, wi);
1758 if (tree_ret)
1759 return tree_ret;
1762 /* If STMT can have statements inside (e.g. GIMPLE_BIND), walk them. */
1763 switch (gimple_code (stmt))
1765 case GIMPLE_BIND:
1766 ret = walk_gimple_seq_mod (gimple_bind_body_ptr (stmt), callback_stmt,
1767 callback_op, wi);
1768 if (ret)
1769 return wi->callback_result;
1770 break;
1772 case GIMPLE_CATCH:
1773 ret = walk_gimple_seq_mod (gimple_catch_handler_ptr (stmt), callback_stmt,
1774 callback_op, wi);
1775 if (ret)
1776 return wi->callback_result;
1777 break;
1779 case GIMPLE_EH_FILTER:
1780 ret = walk_gimple_seq_mod (gimple_eh_filter_failure_ptr (stmt), callback_stmt,
1781 callback_op, wi);
1782 if (ret)
1783 return wi->callback_result;
1784 break;
1786 case GIMPLE_EH_ELSE:
1787 ret = walk_gimple_seq_mod (gimple_eh_else_n_body_ptr (stmt),
1788 callback_stmt, callback_op, wi);
1789 if (ret)
1790 return wi->callback_result;
1791 ret = walk_gimple_seq_mod (gimple_eh_else_e_body_ptr (stmt),
1792 callback_stmt, callback_op, wi);
1793 if (ret)
1794 return wi->callback_result;
1795 break;
1797 case GIMPLE_TRY:
1798 ret = walk_gimple_seq_mod (gimple_try_eval_ptr (stmt), callback_stmt, callback_op,
1799 wi);
1800 if (ret)
1801 return wi->callback_result;
1803 ret = walk_gimple_seq_mod (gimple_try_cleanup_ptr (stmt), callback_stmt,
1804 callback_op, wi);
1805 if (ret)
1806 return wi->callback_result;
1807 break;
1809 case GIMPLE_OMP_FOR:
1810 ret = walk_gimple_seq_mod (gimple_omp_for_pre_body_ptr (stmt), callback_stmt,
1811 callback_op, wi);
1812 if (ret)
1813 return wi->callback_result;
1815 /* FALL THROUGH. */
1816 case GIMPLE_OMP_CRITICAL:
1817 case GIMPLE_OMP_MASTER:
1818 case GIMPLE_OMP_ORDERED:
1819 case GIMPLE_OMP_SECTION:
1820 case GIMPLE_OMP_PARALLEL:
1821 case GIMPLE_OMP_TASK:
1822 case GIMPLE_OMP_SECTIONS:
1823 case GIMPLE_OMP_SINGLE:
1824 ret = walk_gimple_seq_mod (gimple_omp_body_ptr (stmt), callback_stmt,
1825 callback_op, wi);
1826 if (ret)
1827 return wi->callback_result;
1828 break;
1830 case GIMPLE_WITH_CLEANUP_EXPR:
1831 ret = walk_gimple_seq_mod (gimple_wce_cleanup_ptr (stmt), callback_stmt,
1832 callback_op, wi);
1833 if (ret)
1834 return wi->callback_result;
1835 break;
1837 case GIMPLE_TRANSACTION:
1838 ret = walk_gimple_seq_mod (gimple_transaction_body_ptr (stmt),
1839 callback_stmt, callback_op, wi);
1840 if (ret)
1841 return wi->callback_result;
1842 break;
1844 default:
1845 gcc_assert (!gimple_has_substatements (stmt));
1846 break;
1849 return NULL;
1853 /* Set sequence SEQ to be the GIMPLE body for function FN. */
1855 void
1856 gimple_set_body (tree fndecl, gimple_seq seq)
1858 struct function *fn = DECL_STRUCT_FUNCTION (fndecl);
1859 if (fn == NULL)
1861 /* If FNDECL still does not have a function structure associated
1862 with it, then it does not make sense for it to receive a
1863 GIMPLE body. */
1864 gcc_assert (seq == NULL);
1866 else
1867 fn->gimple_body = seq;
1871 /* Return the body of GIMPLE statements for function FN. After the
1872 CFG pass, the function body doesn't exist anymore because it has
1873 been split up into basic blocks. In this case, it returns
1874 NULL. */
1876 gimple_seq
1877 gimple_body (tree fndecl)
1879 struct function *fn = DECL_STRUCT_FUNCTION (fndecl);
1880 return fn ? fn->gimple_body : NULL;
1883 /* Return true when FNDECL has Gimple body either in unlowered
1884 or CFG form. */
1885 bool
1886 gimple_has_body_p (tree fndecl)
1888 struct function *fn = DECL_STRUCT_FUNCTION (fndecl);
1889 return (gimple_body (fndecl) || (fn && fn->cfg));
1892 /* Return true if calls C1 and C2 are known to go to the same function. */
1894 bool
1895 gimple_call_same_target_p (const_gimple c1, const_gimple c2)
1897 if (gimple_call_internal_p (c1))
1898 return (gimple_call_internal_p (c2)
1899 && gimple_call_internal_fn (c1) == gimple_call_internal_fn (c2));
1900 else
1901 return (gimple_call_fn (c1) == gimple_call_fn (c2)
1902 || (gimple_call_fndecl (c1)
1903 && gimple_call_fndecl (c1) == gimple_call_fndecl (c2)));
1906 /* Detect flags from a GIMPLE_CALL. This is just like
1907 call_expr_flags, but for gimple tuples. */
1910 gimple_call_flags (const_gimple stmt)
1912 int flags;
1913 tree decl = gimple_call_fndecl (stmt);
1915 if (decl)
1916 flags = flags_from_decl_or_type (decl);
1917 else if (gimple_call_internal_p (stmt))
1918 flags = internal_fn_flags (gimple_call_internal_fn (stmt));
1919 else
1920 flags = flags_from_decl_or_type (gimple_call_fntype (stmt));
1922 if (stmt->gsbase.subcode & GF_CALL_NOTHROW)
1923 flags |= ECF_NOTHROW;
1925 return flags;
1928 /* Return the "fn spec" string for call STMT. */
1930 static tree
1931 gimple_call_fnspec (const_gimple stmt)
1933 tree type, attr;
1935 type = gimple_call_fntype (stmt);
1936 if (!type)
1937 return NULL_TREE;
1939 attr = lookup_attribute ("fn spec", TYPE_ATTRIBUTES (type));
1940 if (!attr)
1941 return NULL_TREE;
1943 return TREE_VALUE (TREE_VALUE (attr));
1946 /* Detects argument flags for argument number ARG on call STMT. */
1949 gimple_call_arg_flags (const_gimple stmt, unsigned arg)
1951 tree attr = gimple_call_fnspec (stmt);
1953 if (!attr || 1 + arg >= (unsigned) TREE_STRING_LENGTH (attr))
1954 return 0;
1956 switch (TREE_STRING_POINTER (attr)[1 + arg])
1958 case 'x':
1959 case 'X':
1960 return EAF_UNUSED;
1962 case 'R':
1963 return EAF_DIRECT | EAF_NOCLOBBER | EAF_NOESCAPE;
1965 case 'r':
1966 return EAF_NOCLOBBER | EAF_NOESCAPE;
1968 case 'W':
1969 return EAF_DIRECT | EAF_NOESCAPE;
1971 case 'w':
1972 return EAF_NOESCAPE;
1974 case '.':
1975 default:
1976 return 0;
1980 /* Detects return flags for the call STMT. */
1983 gimple_call_return_flags (const_gimple stmt)
1985 tree attr;
1987 if (gimple_call_flags (stmt) & ECF_MALLOC)
1988 return ERF_NOALIAS;
1990 attr = gimple_call_fnspec (stmt);
1991 if (!attr || TREE_STRING_LENGTH (attr) < 1)
1992 return 0;
1994 switch (TREE_STRING_POINTER (attr)[0])
1996 case '1':
1997 case '2':
1998 case '3':
1999 case '4':
2000 return ERF_RETURNS_ARG | (TREE_STRING_POINTER (attr)[0] - '1');
2002 case 'm':
2003 return ERF_NOALIAS;
2005 case '.':
2006 default:
2007 return 0;
2012 /* Return true if GS is a copy assignment. */
2014 bool
2015 gimple_assign_copy_p (gimple gs)
2017 return (gimple_assign_single_p (gs)
2018 && is_gimple_val (gimple_op (gs, 1)));
2022 /* Return true if GS is a SSA_NAME copy assignment. */
2024 bool
2025 gimple_assign_ssa_name_copy_p (gimple gs)
2027 return (gimple_assign_single_p (gs)
2028 && TREE_CODE (gimple_assign_lhs (gs)) == SSA_NAME
2029 && TREE_CODE (gimple_assign_rhs1 (gs)) == SSA_NAME);
2033 /* Return true if GS is an assignment with a unary RHS, but the
2034 operator has no effect on the assigned value. The logic is adapted
2035 from STRIP_NOPS. This predicate is intended to be used in tuplifying
2036 instances in which STRIP_NOPS was previously applied to the RHS of
2037 an assignment.
2039 NOTE: In the use cases that led to the creation of this function
2040 and of gimple_assign_single_p, it is typical to test for either
2041 condition and to proceed in the same manner. In each case, the
2042 assigned value is represented by the single RHS operand of the
2043 assignment. I suspect there may be cases where gimple_assign_copy_p,
2044 gimple_assign_single_p, or equivalent logic is used where a similar
2045 treatment of unary NOPs is appropriate. */
2047 bool
2048 gimple_assign_unary_nop_p (gimple gs)
2050 return (is_gimple_assign (gs)
2051 && (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (gs))
2052 || gimple_assign_rhs_code (gs) == NON_LVALUE_EXPR)
2053 && gimple_assign_rhs1 (gs) != error_mark_node
2054 && (TYPE_MODE (TREE_TYPE (gimple_assign_lhs (gs)))
2055 == TYPE_MODE (TREE_TYPE (gimple_assign_rhs1 (gs)))));
2058 /* Set BB to be the basic block holding G. */
2060 void
2061 gimple_set_bb (gimple stmt, basic_block bb)
2063 stmt->gsbase.bb = bb;
2065 /* If the statement is a label, add the label to block-to-labels map
2066 so that we can speed up edge creation for GIMPLE_GOTOs. */
2067 if (cfun->cfg && gimple_code (stmt) == GIMPLE_LABEL)
2069 tree t;
2070 int uid;
2072 t = gimple_label_label (stmt);
2073 uid = LABEL_DECL_UID (t);
2074 if (uid == -1)
2076 unsigned old_len = VEC_length (basic_block, label_to_block_map);
2077 LABEL_DECL_UID (t) = uid = cfun->cfg->last_label_uid++;
2078 if (old_len <= (unsigned) uid)
2080 unsigned new_len = 3 * uid / 2 + 1;
2082 VEC_safe_grow_cleared (basic_block, gc, label_to_block_map,
2083 new_len);
2087 VEC_replace (basic_block, label_to_block_map, uid, bb);
2092 /* Modify the RHS of the assignment pointed-to by GSI using the
2093 operands in the expression tree EXPR.
2095 NOTE: The statement pointed-to by GSI may be reallocated if it
2096 did not have enough operand slots.
2098 This function is useful to convert an existing tree expression into
2099 the flat representation used for the RHS of a GIMPLE assignment.
2100 It will reallocate memory as needed to expand or shrink the number
2101 of operand slots needed to represent EXPR.
2103 NOTE: If you find yourself building a tree and then calling this
2104 function, you are most certainly doing it the slow way. It is much
2105 better to build a new assignment or to use the function
2106 gimple_assign_set_rhs_with_ops, which does not require an
2107 expression tree to be built. */
2109 void
2110 gimple_assign_set_rhs_from_tree (gimple_stmt_iterator *gsi, tree expr)
2112 enum tree_code subcode;
2113 tree op1, op2, op3;
2115 extract_ops_from_tree_1 (expr, &subcode, &op1, &op2, &op3);
2116 gimple_assign_set_rhs_with_ops_1 (gsi, subcode, op1, op2, op3);
2120 /* Set the RHS of assignment statement pointed-to by GSI to CODE with
2121 operands OP1, OP2 and OP3.
2123 NOTE: The statement pointed-to by GSI may be reallocated if it
2124 did not have enough operand slots. */
2126 void
2127 gimple_assign_set_rhs_with_ops_1 (gimple_stmt_iterator *gsi, enum tree_code code,
2128 tree op1, tree op2, tree op3)
2130 unsigned new_rhs_ops = get_gimple_rhs_num_ops (code);
2131 gimple stmt = gsi_stmt (*gsi);
2133 /* If the new CODE needs more operands, allocate a new statement. */
2134 if (gimple_num_ops (stmt) < new_rhs_ops + 1)
2136 tree lhs = gimple_assign_lhs (stmt);
2137 gimple new_stmt = gimple_alloc (gimple_code (stmt), new_rhs_ops + 1);
2138 memcpy (new_stmt, stmt, gimple_size (gimple_code (stmt)));
2139 gimple_init_singleton (new_stmt);
2140 gsi_replace (gsi, new_stmt, true);
2141 stmt = new_stmt;
2143 /* The LHS needs to be reset as this also changes the SSA name
2144 on the LHS. */
2145 gimple_assign_set_lhs (stmt, lhs);
2148 gimple_set_num_ops (stmt, new_rhs_ops + 1);
2149 gimple_set_subcode (stmt, code);
2150 gimple_assign_set_rhs1 (stmt, op1);
2151 if (new_rhs_ops > 1)
2152 gimple_assign_set_rhs2 (stmt, op2);
2153 if (new_rhs_ops > 2)
2154 gimple_assign_set_rhs3 (stmt, op3);
2158 /* Return the LHS of a statement that performs an assignment,
2159 either a GIMPLE_ASSIGN or a GIMPLE_CALL. Returns NULL_TREE
2160 for a call to a function that returns no value, or for a
2161 statement other than an assignment or a call. */
2163 tree
2164 gimple_get_lhs (const_gimple stmt)
2166 enum gimple_code code = gimple_code (stmt);
2168 if (code == GIMPLE_ASSIGN)
2169 return gimple_assign_lhs (stmt);
2170 else if (code == GIMPLE_CALL)
2171 return gimple_call_lhs (stmt);
2172 else
2173 return NULL_TREE;
2177 /* Set the LHS of a statement that performs an assignment,
2178 either a GIMPLE_ASSIGN or a GIMPLE_CALL. */
2180 void
2181 gimple_set_lhs (gimple stmt, tree lhs)
2183 enum gimple_code code = gimple_code (stmt);
2185 if (code == GIMPLE_ASSIGN)
2186 gimple_assign_set_lhs (stmt, lhs);
2187 else if (code == GIMPLE_CALL)
2188 gimple_call_set_lhs (stmt, lhs);
2189 else
2190 gcc_unreachable();
2193 /* Replace the LHS of STMT, an assignment, either a GIMPLE_ASSIGN or a
2194 GIMPLE_CALL, with NLHS, in preparation for modifying the RHS to an
2195 expression with a different value.
2197 This will update any annotations (say debug bind stmts) referring
2198 to the original LHS, so that they use the RHS instead. This is
2199 done even if NLHS and LHS are the same, for it is understood that
2200 the RHS will be modified afterwards, and NLHS will not be assigned
2201 an equivalent value.
2203 Adjusting any non-annotation uses of the LHS, if needed, is a
2204 responsibility of the caller.
2206 The effect of this call should be pretty much the same as that of
2207 inserting a copy of STMT before STMT, and then removing the
2208 original stmt, at which time gsi_remove() would have update
2209 annotations, but using this function saves all the inserting,
2210 copying and removing. */
2212 void
2213 gimple_replace_lhs (gimple stmt, tree nlhs)
2215 if (MAY_HAVE_DEBUG_STMTS)
2217 tree lhs = gimple_get_lhs (stmt);
2219 gcc_assert (SSA_NAME_DEF_STMT (lhs) == stmt);
2221 insert_debug_temp_for_var_def (NULL, lhs);
2224 gimple_set_lhs (stmt, nlhs);
2227 /* Return a deep copy of statement STMT. All the operands from STMT
2228 are reallocated and copied using unshare_expr. The DEF, USE, VDEF
2229 and VUSE operand arrays are set to empty in the new copy. The new
2230 copy isn't part of any sequence. */
2232 gimple
2233 gimple_copy (gimple stmt)
2235 enum gimple_code code = gimple_code (stmt);
2236 unsigned num_ops = gimple_num_ops (stmt);
2237 gimple copy = gimple_alloc (code, num_ops);
2238 unsigned i;
2240 /* Shallow copy all the fields from STMT. */
2241 memcpy (copy, stmt, gimple_size (code));
2242 gimple_init_singleton (copy);
2244 /* If STMT has sub-statements, deep-copy them as well. */
2245 if (gimple_has_substatements (stmt))
2247 gimple_seq new_seq;
2248 tree t;
2250 switch (gimple_code (stmt))
2252 case GIMPLE_BIND:
2253 new_seq = gimple_seq_copy (gimple_bind_body (stmt));
2254 gimple_bind_set_body (copy, new_seq);
2255 gimple_bind_set_vars (copy, unshare_expr (gimple_bind_vars (stmt)));
2256 gimple_bind_set_block (copy, gimple_bind_block (stmt));
2257 break;
2259 case GIMPLE_CATCH:
2260 new_seq = gimple_seq_copy (gimple_catch_handler (stmt));
2261 gimple_catch_set_handler (copy, new_seq);
2262 t = unshare_expr (gimple_catch_types (stmt));
2263 gimple_catch_set_types (copy, t);
2264 break;
2266 case GIMPLE_EH_FILTER:
2267 new_seq = gimple_seq_copy (gimple_eh_filter_failure (stmt));
2268 gimple_eh_filter_set_failure (copy, new_seq);
2269 t = unshare_expr (gimple_eh_filter_types (stmt));
2270 gimple_eh_filter_set_types (copy, t);
2271 break;
2273 case GIMPLE_EH_ELSE:
2274 new_seq = gimple_seq_copy (gimple_eh_else_n_body (stmt));
2275 gimple_eh_else_set_n_body (copy, new_seq);
2276 new_seq = gimple_seq_copy (gimple_eh_else_e_body (stmt));
2277 gimple_eh_else_set_e_body (copy, new_seq);
2278 break;
2280 case GIMPLE_TRY:
2281 new_seq = gimple_seq_copy (gimple_try_eval (stmt));
2282 gimple_try_set_eval (copy, new_seq);
2283 new_seq = gimple_seq_copy (gimple_try_cleanup (stmt));
2284 gimple_try_set_cleanup (copy, new_seq);
2285 break;
2287 case GIMPLE_OMP_FOR:
2288 new_seq = gimple_seq_copy (gimple_omp_for_pre_body (stmt));
2289 gimple_omp_for_set_pre_body (copy, new_seq);
2290 t = unshare_expr (gimple_omp_for_clauses (stmt));
2291 gimple_omp_for_set_clauses (copy, t);
2292 copy->gimple_omp_for.iter
2293 = ggc_alloc_vec_gimple_omp_for_iter
2294 (gimple_omp_for_collapse (stmt));
2295 for (i = 0; i < gimple_omp_for_collapse (stmt); i++)
2297 gimple_omp_for_set_cond (copy, i,
2298 gimple_omp_for_cond (stmt, i));
2299 gimple_omp_for_set_index (copy, i,
2300 gimple_omp_for_index (stmt, i));
2301 t = unshare_expr (gimple_omp_for_initial (stmt, i));
2302 gimple_omp_for_set_initial (copy, i, t);
2303 t = unshare_expr (gimple_omp_for_final (stmt, i));
2304 gimple_omp_for_set_final (copy, i, t);
2305 t = unshare_expr (gimple_omp_for_incr (stmt, i));
2306 gimple_omp_for_set_incr (copy, i, t);
2308 goto copy_omp_body;
2310 case GIMPLE_OMP_PARALLEL:
2311 t = unshare_expr (gimple_omp_parallel_clauses (stmt));
2312 gimple_omp_parallel_set_clauses (copy, t);
2313 t = unshare_expr (gimple_omp_parallel_child_fn (stmt));
2314 gimple_omp_parallel_set_child_fn (copy, t);
2315 t = unshare_expr (gimple_omp_parallel_data_arg (stmt));
2316 gimple_omp_parallel_set_data_arg (copy, t);
2317 goto copy_omp_body;
2319 case GIMPLE_OMP_TASK:
2320 t = unshare_expr (gimple_omp_task_clauses (stmt));
2321 gimple_omp_task_set_clauses (copy, t);
2322 t = unshare_expr (gimple_omp_task_child_fn (stmt));
2323 gimple_omp_task_set_child_fn (copy, t);
2324 t = unshare_expr (gimple_omp_task_data_arg (stmt));
2325 gimple_omp_task_set_data_arg (copy, t);
2326 t = unshare_expr (gimple_omp_task_copy_fn (stmt));
2327 gimple_omp_task_set_copy_fn (copy, t);
2328 t = unshare_expr (gimple_omp_task_arg_size (stmt));
2329 gimple_omp_task_set_arg_size (copy, t);
2330 t = unshare_expr (gimple_omp_task_arg_align (stmt));
2331 gimple_omp_task_set_arg_align (copy, t);
2332 goto copy_omp_body;
2334 case GIMPLE_OMP_CRITICAL:
2335 t = unshare_expr (gimple_omp_critical_name (stmt));
2336 gimple_omp_critical_set_name (copy, t);
2337 goto copy_omp_body;
2339 case GIMPLE_OMP_SECTIONS:
2340 t = unshare_expr (gimple_omp_sections_clauses (stmt));
2341 gimple_omp_sections_set_clauses (copy, t);
2342 t = unshare_expr (gimple_omp_sections_control (stmt));
2343 gimple_omp_sections_set_control (copy, t);
2344 /* FALLTHRU */
2346 case GIMPLE_OMP_SINGLE:
2347 case GIMPLE_OMP_SECTION:
2348 case GIMPLE_OMP_MASTER:
2349 case GIMPLE_OMP_ORDERED:
2350 copy_omp_body:
2351 new_seq = gimple_seq_copy (gimple_omp_body (stmt));
2352 gimple_omp_set_body (copy, new_seq);
2353 break;
2355 case GIMPLE_TRANSACTION:
2356 new_seq = gimple_seq_copy (gimple_transaction_body (stmt));
2357 gimple_transaction_set_body (copy, new_seq);
2358 break;
2360 case GIMPLE_WITH_CLEANUP_EXPR:
2361 new_seq = gimple_seq_copy (gimple_wce_cleanup (stmt));
2362 gimple_wce_set_cleanup (copy, new_seq);
2363 break;
2365 default:
2366 gcc_unreachable ();
2370 /* Make copy of operands. */
2371 if (num_ops > 0)
2373 for (i = 0; i < num_ops; i++)
2374 gimple_set_op (copy, i, unshare_expr (gimple_op (stmt, i)));
2376 /* Clear out SSA operand vectors on COPY. */
2377 if (gimple_has_ops (stmt))
2379 gimple_set_def_ops (copy, NULL);
2380 gimple_set_use_ops (copy, NULL);
2383 if (gimple_has_mem_ops (stmt))
2385 gimple_set_vdef (copy, gimple_vdef (stmt));
2386 gimple_set_vuse (copy, gimple_vuse (stmt));
2389 /* SSA operands need to be updated. */
2390 gimple_set_modified (copy, true);
2393 return copy;
2397 /* Return true if statement S has side-effects. We consider a
2398 statement to have side effects if:
2400 - It is a GIMPLE_CALL not marked with ECF_PURE or ECF_CONST.
2401 - Any of its operands are marked TREE_THIS_VOLATILE or TREE_SIDE_EFFECTS. */
2403 bool
2404 gimple_has_side_effects (const_gimple s)
2406 if (is_gimple_debug (s))
2407 return false;
2409 /* We don't have to scan the arguments to check for
2410 volatile arguments, though, at present, we still
2411 do a scan to check for TREE_SIDE_EFFECTS. */
2412 if (gimple_has_volatile_ops (s))
2413 return true;
2415 if (gimple_code (s) == GIMPLE_ASM
2416 && gimple_asm_volatile_p (s))
2417 return true;
2419 if (is_gimple_call (s))
2421 int flags = gimple_call_flags (s);
2423 /* An infinite loop is considered a side effect. */
2424 if (!(flags & (ECF_CONST | ECF_PURE))
2425 || (flags & ECF_LOOPING_CONST_OR_PURE))
2426 return true;
2428 return false;
2431 return false;
2434 /* Helper for gimple_could_trap_p and gimple_assign_rhs_could_trap_p.
2435 Return true if S can trap. When INCLUDE_MEM is true, check whether
2436 the memory operations could trap. When INCLUDE_STORES is true and
2437 S is a GIMPLE_ASSIGN, the LHS of the assignment is also checked. */
2439 bool
2440 gimple_could_trap_p_1 (gimple s, bool include_mem, bool include_stores)
2442 tree t, div = NULL_TREE;
2443 enum tree_code op;
2445 if (include_mem)
2447 unsigned i, start = (is_gimple_assign (s) && !include_stores) ? 1 : 0;
2449 for (i = start; i < gimple_num_ops (s); i++)
2450 if (tree_could_trap_p (gimple_op (s, i)))
2451 return true;
2454 switch (gimple_code (s))
2456 case GIMPLE_ASM:
2457 return gimple_asm_volatile_p (s);
2459 case GIMPLE_CALL:
2460 t = gimple_call_fndecl (s);
2461 /* Assume that calls to weak functions may trap. */
2462 if (!t || !DECL_P (t) || DECL_WEAK (t))
2463 return true;
2464 return false;
2466 case GIMPLE_ASSIGN:
2467 t = gimple_expr_type (s);
2468 op = gimple_assign_rhs_code (s);
2469 if (get_gimple_rhs_class (op) == GIMPLE_BINARY_RHS)
2470 div = gimple_assign_rhs2 (s);
2471 return (operation_could_trap_p (op, FLOAT_TYPE_P (t),
2472 (INTEGRAL_TYPE_P (t)
2473 && TYPE_OVERFLOW_TRAPS (t)),
2474 div));
2476 default:
2477 break;
2480 return false;
2483 /* Return true if statement S can trap. */
2485 bool
2486 gimple_could_trap_p (gimple s)
2488 return gimple_could_trap_p_1 (s, true, true);
2491 /* Return true if RHS of a GIMPLE_ASSIGN S can trap. */
2493 bool
2494 gimple_assign_rhs_could_trap_p (gimple s)
2496 gcc_assert (is_gimple_assign (s));
2497 return gimple_could_trap_p_1 (s, true, false);
2501 /* Print debugging information for gimple stmts generated. */
2503 void
2504 dump_gimple_statistics (void)
2506 #ifdef GATHER_STATISTICS
2507 int i, total_tuples = 0, total_bytes = 0;
2509 fprintf (stderr, "\nGIMPLE statements\n");
2510 fprintf (stderr, "Kind Stmts Bytes\n");
2511 fprintf (stderr, "---------------------------------------\n");
2512 for (i = 0; i < (int) gimple_alloc_kind_all; ++i)
2514 fprintf (stderr, "%-20s %7d %10d\n", gimple_alloc_kind_names[i],
2515 gimple_alloc_counts[i], gimple_alloc_sizes[i]);
2516 total_tuples += gimple_alloc_counts[i];
2517 total_bytes += gimple_alloc_sizes[i];
2519 fprintf (stderr, "---------------------------------------\n");
2520 fprintf (stderr, "%-20s %7d %10d\n", "Total", total_tuples, total_bytes);
2521 fprintf (stderr, "---------------------------------------\n");
2522 #else
2523 fprintf (stderr, "No gimple statistics\n");
2524 #endif
2528 /* Return the number of operands needed on the RHS of a GIMPLE
2529 assignment for an expression with tree code CODE. */
2531 unsigned
2532 get_gimple_rhs_num_ops (enum tree_code code)
2534 enum gimple_rhs_class rhs_class = get_gimple_rhs_class (code);
2536 if (rhs_class == GIMPLE_UNARY_RHS || rhs_class == GIMPLE_SINGLE_RHS)
2537 return 1;
2538 else if (rhs_class == GIMPLE_BINARY_RHS)
2539 return 2;
2540 else if (rhs_class == GIMPLE_TERNARY_RHS)
2541 return 3;
2542 else
2543 gcc_unreachable ();
2546 #define DEFTREECODE(SYM, STRING, TYPE, NARGS) \
2547 (unsigned char) \
2548 ((TYPE) == tcc_unary ? GIMPLE_UNARY_RHS \
2549 : ((TYPE) == tcc_binary \
2550 || (TYPE) == tcc_comparison) ? GIMPLE_BINARY_RHS \
2551 : ((TYPE) == tcc_constant \
2552 || (TYPE) == tcc_declaration \
2553 || (TYPE) == tcc_reference) ? GIMPLE_SINGLE_RHS \
2554 : ((SYM) == TRUTH_AND_EXPR \
2555 || (SYM) == TRUTH_OR_EXPR \
2556 || (SYM) == TRUTH_XOR_EXPR) ? GIMPLE_BINARY_RHS \
2557 : (SYM) == TRUTH_NOT_EXPR ? GIMPLE_UNARY_RHS \
2558 : ((SYM) == COND_EXPR \
2559 || (SYM) == WIDEN_MULT_PLUS_EXPR \
2560 || (SYM) == WIDEN_MULT_MINUS_EXPR \
2561 || (SYM) == DOT_PROD_EXPR \
2562 || (SYM) == REALIGN_LOAD_EXPR \
2563 || (SYM) == VEC_COND_EXPR \
2564 || (SYM) == VEC_PERM_EXPR \
2565 || (SYM) == FMA_EXPR) ? GIMPLE_TERNARY_RHS \
2566 : ((SYM) == CONSTRUCTOR \
2567 || (SYM) == OBJ_TYPE_REF \
2568 || (SYM) == ASSERT_EXPR \
2569 || (SYM) == ADDR_EXPR \
2570 || (SYM) == WITH_SIZE_EXPR \
2571 || (SYM) == SSA_NAME) ? GIMPLE_SINGLE_RHS \
2572 : GIMPLE_INVALID_RHS),
2573 #define END_OF_BASE_TREE_CODES (unsigned char) GIMPLE_INVALID_RHS,
2575 const unsigned char gimple_rhs_class_table[] = {
2576 #include "all-tree.def"
2579 #undef DEFTREECODE
2580 #undef END_OF_BASE_TREE_CODES
2582 /* For the definitive definition of GIMPLE, see doc/tree-ssa.texi. */
2584 /* Validation of GIMPLE expressions. */
2586 /* Return true if T is a valid LHS for a GIMPLE assignment expression. */
2588 bool
2589 is_gimple_lvalue (tree t)
2591 return (is_gimple_addressable (t)
2592 || TREE_CODE (t) == WITH_SIZE_EXPR
2593 /* These are complex lvalues, but don't have addresses, so they
2594 go here. */
2595 || TREE_CODE (t) == BIT_FIELD_REF);
2598 /* Return true if T is a GIMPLE condition. */
2600 bool
2601 is_gimple_condexpr (tree t)
2603 return (is_gimple_val (t) || (COMPARISON_CLASS_P (t)
2604 && !tree_could_throw_p (t)
2605 && is_gimple_val (TREE_OPERAND (t, 0))
2606 && is_gimple_val (TREE_OPERAND (t, 1))));
2609 /* Return true if T is something whose address can be taken. */
2611 bool
2612 is_gimple_addressable (tree t)
2614 return (is_gimple_id (t) || handled_component_p (t)
2615 || TREE_CODE (t) == MEM_REF);
2618 /* Return true if T is a valid gimple constant. */
2620 bool
2621 is_gimple_constant (const_tree t)
2623 switch (TREE_CODE (t))
2625 case INTEGER_CST:
2626 case REAL_CST:
2627 case FIXED_CST:
2628 case STRING_CST:
2629 case COMPLEX_CST:
2630 case VECTOR_CST:
2631 return true;
2633 /* Vector constant constructors are gimple invariant. */
2634 case CONSTRUCTOR:
2635 if (TREE_TYPE (t) && TREE_CODE (TREE_TYPE (t)) == VECTOR_TYPE)
2636 return TREE_CONSTANT (t);
2637 else
2638 return false;
2640 default:
2641 return false;
2645 /* Return true if T is a gimple address. */
2647 bool
2648 is_gimple_address (const_tree t)
2650 tree op;
2652 if (TREE_CODE (t) != ADDR_EXPR)
2653 return false;
2655 op = TREE_OPERAND (t, 0);
2656 while (handled_component_p (op))
2658 if ((TREE_CODE (op) == ARRAY_REF
2659 || TREE_CODE (op) == ARRAY_RANGE_REF)
2660 && !is_gimple_val (TREE_OPERAND (op, 1)))
2661 return false;
2663 op = TREE_OPERAND (op, 0);
2666 if (CONSTANT_CLASS_P (op) || TREE_CODE (op) == MEM_REF)
2667 return true;
2669 switch (TREE_CODE (op))
2671 case PARM_DECL:
2672 case RESULT_DECL:
2673 case LABEL_DECL:
2674 case FUNCTION_DECL:
2675 case VAR_DECL:
2676 case CONST_DECL:
2677 return true;
2679 default:
2680 return false;
2684 /* Return true if T is a gimple invariant address. */
2686 bool
2687 is_gimple_invariant_address (const_tree t)
2689 const_tree op;
2691 if (TREE_CODE (t) != ADDR_EXPR)
2692 return false;
2694 op = strip_invariant_refs (TREE_OPERAND (t, 0));
2695 if (!op)
2696 return false;
2698 if (TREE_CODE (op) == MEM_REF)
2700 const_tree op0 = TREE_OPERAND (op, 0);
2701 return (TREE_CODE (op0) == ADDR_EXPR
2702 && (CONSTANT_CLASS_P (TREE_OPERAND (op0, 0))
2703 || decl_address_invariant_p (TREE_OPERAND (op0, 0))));
2706 return CONSTANT_CLASS_P (op) || decl_address_invariant_p (op);
2709 /* Return true if T is a gimple invariant address at IPA level
2710 (so addresses of variables on stack are not allowed). */
2712 bool
2713 is_gimple_ip_invariant_address (const_tree t)
2715 const_tree op;
2717 if (TREE_CODE (t) != ADDR_EXPR)
2718 return false;
2720 op = strip_invariant_refs (TREE_OPERAND (t, 0));
2721 if (!op)
2722 return false;
2724 if (TREE_CODE (op) == MEM_REF)
2726 const_tree op0 = TREE_OPERAND (op, 0);
2727 return (TREE_CODE (op0) == ADDR_EXPR
2728 && (CONSTANT_CLASS_P (TREE_OPERAND (op0, 0))
2729 || decl_address_ip_invariant_p (TREE_OPERAND (op0, 0))));
2732 return 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 is a variable. */
2761 bool
2762 is_gimple_variable (tree t)
2764 return (TREE_CODE (t) == VAR_DECL
2765 || TREE_CODE (t) == PARM_DECL
2766 || TREE_CODE (t) == RESULT_DECL
2767 || TREE_CODE (t) == SSA_NAME);
2770 /* Return true if T is a GIMPLE identifier (something with an address). */
2772 bool
2773 is_gimple_id (tree t)
2775 return (is_gimple_variable (t)
2776 || TREE_CODE (t) == FUNCTION_DECL
2777 || TREE_CODE (t) == LABEL_DECL
2778 || TREE_CODE (t) == CONST_DECL
2779 /* Allow string constants, since they are addressable. */
2780 || TREE_CODE (t) == STRING_CST);
2783 /* Return true if T is a non-aggregate register variable. */
2785 bool
2786 is_gimple_reg (tree t)
2788 if (TREE_CODE (t) == SSA_NAME)
2790 t = SSA_NAME_VAR (t);
2791 if (TREE_CODE (t) == VAR_DECL
2792 && VAR_DECL_IS_VIRTUAL_OPERAND (t))
2793 return false;
2794 return true;
2797 if (TREE_CODE (t) == VAR_DECL
2798 && VAR_DECL_IS_VIRTUAL_OPERAND (t))
2799 return false;
2801 if (!is_gimple_variable (t))
2802 return false;
2804 if (!is_gimple_reg_type (TREE_TYPE (t)))
2805 return false;
2807 /* A volatile decl is not acceptable because we can't reuse it as
2808 needed. We need to copy it into a temp first. */
2809 if (TREE_THIS_VOLATILE (t))
2810 return false;
2812 /* We define "registers" as things that can be renamed as needed,
2813 which with our infrastructure does not apply to memory. */
2814 if (needs_to_live_in_memory (t))
2815 return false;
2817 /* Hard register variables are an interesting case. For those that
2818 are call-clobbered, we don't know where all the calls are, since
2819 we don't (want to) take into account which operations will turn
2820 into libcalls at the rtl level. For those that are call-saved,
2821 we don't currently model the fact that calls may in fact change
2822 global hard registers, nor do we examine ASM_CLOBBERS at the tree
2823 level, and so miss variable changes that might imply. All around,
2824 it seems safest to not do too much optimization with these at the
2825 tree level at all. We'll have to rely on the rtl optimizers to
2826 clean this up, as there we've got all the appropriate bits exposed. */
2827 if (TREE_CODE (t) == VAR_DECL && DECL_HARD_REGISTER (t))
2828 return false;
2830 /* Complex and vector values must have been put into SSA-like form.
2831 That is, no assignments to the individual components. */
2832 if (TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE
2833 || TREE_CODE (TREE_TYPE (t)) == VECTOR_TYPE)
2834 return DECL_GIMPLE_REG_P (t);
2836 return true;
2840 /* Return true if T is a GIMPLE rvalue, i.e. an identifier or a constant. */
2842 bool
2843 is_gimple_val (tree t)
2845 /* Make loads from volatiles and memory vars explicit. */
2846 if (is_gimple_variable (t)
2847 && is_gimple_reg_type (TREE_TYPE (t))
2848 && !is_gimple_reg (t))
2849 return false;
2851 return (is_gimple_variable (t) || is_gimple_min_invariant (t));
2854 /* Similarly, but accept hard registers as inputs to asm statements. */
2856 bool
2857 is_gimple_asm_val (tree t)
2859 if (TREE_CODE (t) == VAR_DECL && DECL_HARD_REGISTER (t))
2860 return true;
2862 return is_gimple_val (t);
2865 /* Return true if T is a GIMPLE minimal lvalue. */
2867 bool
2868 is_gimple_min_lval (tree t)
2870 if (!(t = CONST_CAST_TREE (strip_invariant_refs (t))))
2871 return false;
2872 return (is_gimple_id (t) || TREE_CODE (t) == MEM_REF);
2875 /* Return true if T is a valid function operand of a CALL_EXPR. */
2877 bool
2878 is_gimple_call_addr (tree t)
2880 return (TREE_CODE (t) == OBJ_TYPE_REF || is_gimple_val (t));
2883 /* Return true if T is a valid address operand of a MEM_REF. */
2885 bool
2886 is_gimple_mem_ref_addr (tree t)
2888 return (is_gimple_reg (t)
2889 || TREE_CODE (t) == INTEGER_CST
2890 || (TREE_CODE (t) == ADDR_EXPR
2891 && (CONSTANT_CLASS_P (TREE_OPERAND (t, 0))
2892 || decl_address_invariant_p (TREE_OPERAND (t, 0)))));
2896 /* Given a memory reference expression T, return its base address.
2897 The base address of a memory reference expression is the main
2898 object being referenced. For instance, the base address for
2899 'array[i].fld[j]' is 'array'. You can think of this as stripping
2900 away the offset part from a memory address.
2902 This function calls handled_component_p to strip away all the inner
2903 parts of the memory reference until it reaches the base object. */
2905 tree
2906 get_base_address (tree t)
2908 while (handled_component_p (t))
2909 t = TREE_OPERAND (t, 0);
2911 if ((TREE_CODE (t) == MEM_REF
2912 || TREE_CODE (t) == TARGET_MEM_REF)
2913 && TREE_CODE (TREE_OPERAND (t, 0)) == ADDR_EXPR)
2914 t = TREE_OPERAND (TREE_OPERAND (t, 0), 0);
2916 if (TREE_CODE (t) == SSA_NAME
2917 || DECL_P (t)
2918 || TREE_CODE (t) == STRING_CST
2919 || TREE_CODE (t) == CONSTRUCTOR
2920 || INDIRECT_REF_P (t)
2921 || TREE_CODE (t) == MEM_REF
2922 || TREE_CODE (t) == TARGET_MEM_REF)
2923 return t;
2924 else
2925 return NULL_TREE;
2928 void
2929 recalculate_side_effects (tree t)
2931 enum tree_code code = TREE_CODE (t);
2932 int len = TREE_OPERAND_LENGTH (t);
2933 int i;
2935 switch (TREE_CODE_CLASS (code))
2937 case tcc_expression:
2938 switch (code)
2940 case INIT_EXPR:
2941 case MODIFY_EXPR:
2942 case VA_ARG_EXPR:
2943 case PREDECREMENT_EXPR:
2944 case PREINCREMENT_EXPR:
2945 case POSTDECREMENT_EXPR:
2946 case POSTINCREMENT_EXPR:
2947 /* All of these have side-effects, no matter what their
2948 operands are. */
2949 return;
2951 default:
2952 break;
2954 /* Fall through. */
2956 case tcc_comparison: /* a comparison expression */
2957 case tcc_unary: /* a unary arithmetic expression */
2958 case tcc_binary: /* a binary arithmetic expression */
2959 case tcc_reference: /* a reference */
2960 case tcc_vl_exp: /* a function call */
2961 TREE_SIDE_EFFECTS (t) = TREE_THIS_VOLATILE (t);
2962 for (i = 0; i < len; ++i)
2964 tree op = TREE_OPERAND (t, i);
2965 if (op && TREE_SIDE_EFFECTS (op))
2966 TREE_SIDE_EFFECTS (t) = 1;
2968 break;
2970 case tcc_constant:
2971 /* No side-effects. */
2972 return;
2974 default:
2975 gcc_unreachable ();
2979 /* Canonicalize a tree T for use in a COND_EXPR as conditional. Returns
2980 a canonicalized tree that is valid for a COND_EXPR or NULL_TREE, if
2981 we failed to create one. */
2983 tree
2984 canonicalize_cond_expr_cond (tree t)
2986 /* Strip conversions around boolean operations. */
2987 if (CONVERT_EXPR_P (t)
2988 && (truth_value_p (TREE_CODE (TREE_OPERAND (t, 0)))
2989 || TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0)))
2990 == BOOLEAN_TYPE))
2991 t = TREE_OPERAND (t, 0);
2993 /* For !x use x == 0. */
2994 if (TREE_CODE (t) == TRUTH_NOT_EXPR)
2996 tree top0 = TREE_OPERAND (t, 0);
2997 t = build2 (EQ_EXPR, TREE_TYPE (t),
2998 top0, build_int_cst (TREE_TYPE (top0), 0));
3000 /* For cmp ? 1 : 0 use cmp. */
3001 else if (TREE_CODE (t) == COND_EXPR
3002 && COMPARISON_CLASS_P (TREE_OPERAND (t, 0))
3003 && integer_onep (TREE_OPERAND (t, 1))
3004 && integer_zerop (TREE_OPERAND (t, 2)))
3006 tree top0 = TREE_OPERAND (t, 0);
3007 t = build2 (TREE_CODE (top0), TREE_TYPE (t),
3008 TREE_OPERAND (top0, 0), TREE_OPERAND (top0, 1));
3011 if (is_gimple_condexpr (t))
3012 return t;
3014 return NULL_TREE;
3017 /* Build a GIMPLE_CALL identical to STMT but skipping the arguments in
3018 the positions marked by the set ARGS_TO_SKIP. */
3020 gimple
3021 gimple_call_copy_skip_args (gimple stmt, bitmap args_to_skip)
3023 int i;
3024 int nargs = gimple_call_num_args (stmt);
3025 VEC(tree, heap) *vargs = VEC_alloc (tree, heap, nargs);
3026 gimple new_stmt;
3028 for (i = 0; i < nargs; i++)
3029 if (!bitmap_bit_p (args_to_skip, i))
3030 VEC_quick_push (tree, vargs, gimple_call_arg (stmt, i));
3032 if (gimple_call_internal_p (stmt))
3033 new_stmt = gimple_build_call_internal_vec (gimple_call_internal_fn (stmt),
3034 vargs);
3035 else
3036 new_stmt = gimple_build_call_vec (gimple_call_fn (stmt), vargs);
3037 VEC_free (tree, heap, vargs);
3038 if (gimple_call_lhs (stmt))
3039 gimple_call_set_lhs (new_stmt, gimple_call_lhs (stmt));
3041 gimple_set_vuse (new_stmt, gimple_vuse (stmt));
3042 gimple_set_vdef (new_stmt, gimple_vdef (stmt));
3044 gimple_set_block (new_stmt, gimple_block (stmt));
3045 if (gimple_has_location (stmt))
3046 gimple_set_location (new_stmt, gimple_location (stmt));
3047 gimple_call_copy_flags (new_stmt, stmt);
3048 gimple_call_set_chain (new_stmt, gimple_call_chain (stmt));
3050 gimple_set_modified (new_stmt, true);
3052 return new_stmt;
3056 enum gtc_mode { GTC_MERGE = 0, GTC_DIAG = 1 };
3058 static hashval_t gimple_type_hash (const void *);
3060 /* Structure used to maintain a cache of some type pairs compared by
3061 gimple_types_compatible_p when comparing aggregate types. There are
3062 three possible values for SAME_P:
3064 -2: The pair (T1, T2) has just been inserted in the table.
3065 0: T1 and T2 are different types.
3066 1: T1 and T2 are the same type.
3068 The two elements in the SAME_P array are indexed by the comparison
3069 mode gtc_mode. */
3071 struct type_pair_d
3073 unsigned int uid1;
3074 unsigned int uid2;
3075 signed char same_p[2];
3077 typedef struct type_pair_d *type_pair_t;
3078 DEF_VEC_P(type_pair_t);
3079 DEF_VEC_ALLOC_P(type_pair_t,heap);
3081 #define GIMPLE_TYPE_PAIR_SIZE 16381
3082 struct type_pair_d *type_pair_cache;
3085 /* Lookup the pair of types T1 and T2 in *VISITED_P. Insert a new
3086 entry if none existed. */
3088 static inline type_pair_t
3089 lookup_type_pair (tree t1, tree t2)
3091 unsigned int index;
3092 unsigned int uid1, uid2;
3094 if (type_pair_cache == NULL)
3095 type_pair_cache = XCNEWVEC (struct type_pair_d, GIMPLE_TYPE_PAIR_SIZE);
3097 if (TYPE_UID (t1) < TYPE_UID (t2))
3099 uid1 = TYPE_UID (t1);
3100 uid2 = TYPE_UID (t2);
3102 else
3104 uid1 = TYPE_UID (t2);
3105 uid2 = TYPE_UID (t1);
3107 gcc_checking_assert (uid1 != uid2);
3109 /* iterative_hash_hashval_t imply an function calls.
3110 We know that UIDS are in limited range. */
3111 index = ((((unsigned HOST_WIDE_INT)uid1 << HOST_BITS_PER_WIDE_INT / 2) + uid2)
3112 % GIMPLE_TYPE_PAIR_SIZE);
3113 if (type_pair_cache [index].uid1 == uid1
3114 && type_pair_cache [index].uid2 == uid2)
3115 return &type_pair_cache[index];
3117 type_pair_cache [index].uid1 = uid1;
3118 type_pair_cache [index].uid2 = uid2;
3119 type_pair_cache [index].same_p[0] = -2;
3120 type_pair_cache [index].same_p[1] = -2;
3122 return &type_pair_cache[index];
3125 /* Per pointer state for the SCC finding. The on_sccstack flag
3126 is not strictly required, it is true when there is no hash value
3127 recorded for the type and false otherwise. But querying that
3128 is slower. */
3130 struct sccs
3132 unsigned int dfsnum;
3133 unsigned int low;
3134 bool on_sccstack;
3135 union {
3136 hashval_t hash;
3137 signed char same_p;
3138 } u;
3141 static unsigned int next_dfs_num;
3142 static unsigned int gtc_next_dfs_num;
3145 /* GIMPLE type merging cache. A direct-mapped cache based on TYPE_UID. */
3147 typedef struct GTY(()) gimple_type_leader_entry_s {
3148 tree type;
3149 tree leader;
3150 } gimple_type_leader_entry;
3152 #define GIMPLE_TYPE_LEADER_SIZE 16381
3153 static GTY((deletable, length("GIMPLE_TYPE_LEADER_SIZE")))
3154 gimple_type_leader_entry *gimple_type_leader;
3156 /* Lookup an existing leader for T and return it or NULL_TREE, if
3157 there is none in the cache. */
3159 static inline tree
3160 gimple_lookup_type_leader (tree t)
3162 gimple_type_leader_entry *leader;
3164 if (!gimple_type_leader)
3165 return NULL_TREE;
3167 leader = &gimple_type_leader[TYPE_UID (t) % GIMPLE_TYPE_LEADER_SIZE];
3168 if (leader->type != t)
3169 return NULL_TREE;
3171 return leader->leader;
3174 /* Return true if T1 and T2 have the same name. If FOR_COMPLETION_P is
3175 true then if any type has no name return false, otherwise return
3176 true if both types have no names. */
3178 static bool
3179 compare_type_names_p (tree t1, tree t2)
3181 tree name1 = TYPE_NAME (t1);
3182 tree name2 = TYPE_NAME (t2);
3184 if ((name1 != NULL_TREE) != (name2 != NULL_TREE))
3185 return false;
3187 if (name1 == NULL_TREE)
3188 return true;
3190 /* Either both should be a TYPE_DECL or both an IDENTIFIER_NODE. */
3191 if (TREE_CODE (name1) != TREE_CODE (name2))
3192 return false;
3194 if (TREE_CODE (name1) == TYPE_DECL)
3195 name1 = DECL_NAME (name1);
3196 gcc_checking_assert (!name1 || TREE_CODE (name1) == IDENTIFIER_NODE);
3198 if (TREE_CODE (name2) == TYPE_DECL)
3199 name2 = DECL_NAME (name2);
3200 gcc_checking_assert (!name2 || TREE_CODE (name2) == IDENTIFIER_NODE);
3202 /* Identifiers can be compared with pointer equality rather
3203 than a string comparison. */
3204 if (name1 == name2)
3205 return true;
3207 return false;
3210 /* Return true if the field decls F1 and F2 are at the same offset.
3212 This is intended to be used on GIMPLE types only. */
3214 bool
3215 gimple_compare_field_offset (tree f1, tree f2)
3217 if (DECL_OFFSET_ALIGN (f1) == DECL_OFFSET_ALIGN (f2))
3219 tree offset1 = DECL_FIELD_OFFSET (f1);
3220 tree offset2 = DECL_FIELD_OFFSET (f2);
3221 return ((offset1 == offset2
3222 /* Once gimplification is done, self-referential offsets are
3223 instantiated as operand #2 of the COMPONENT_REF built for
3224 each access and reset. Therefore, they are not relevant
3225 anymore and fields are interchangeable provided that they
3226 represent the same access. */
3227 || (TREE_CODE (offset1) == PLACEHOLDER_EXPR
3228 && TREE_CODE (offset2) == PLACEHOLDER_EXPR
3229 && (DECL_SIZE (f1) == DECL_SIZE (f2)
3230 || (TREE_CODE (DECL_SIZE (f1)) == PLACEHOLDER_EXPR
3231 && TREE_CODE (DECL_SIZE (f2)) == PLACEHOLDER_EXPR)
3232 || operand_equal_p (DECL_SIZE (f1), DECL_SIZE (f2), 0))
3233 && DECL_ALIGN (f1) == DECL_ALIGN (f2))
3234 || operand_equal_p (offset1, offset2, 0))
3235 && tree_int_cst_equal (DECL_FIELD_BIT_OFFSET (f1),
3236 DECL_FIELD_BIT_OFFSET (f2)));
3239 /* Fortran and C do not always agree on what DECL_OFFSET_ALIGN
3240 should be, so handle differing ones specially by decomposing
3241 the offset into a byte and bit offset manually. */
3242 if (host_integerp (DECL_FIELD_OFFSET (f1), 0)
3243 && host_integerp (DECL_FIELD_OFFSET (f2), 0))
3245 unsigned HOST_WIDE_INT byte_offset1, byte_offset2;
3246 unsigned HOST_WIDE_INT bit_offset1, bit_offset2;
3247 bit_offset1 = TREE_INT_CST_LOW (DECL_FIELD_BIT_OFFSET (f1));
3248 byte_offset1 = (TREE_INT_CST_LOW (DECL_FIELD_OFFSET (f1))
3249 + bit_offset1 / BITS_PER_UNIT);
3250 bit_offset2 = TREE_INT_CST_LOW (DECL_FIELD_BIT_OFFSET (f2));
3251 byte_offset2 = (TREE_INT_CST_LOW (DECL_FIELD_OFFSET (f2))
3252 + bit_offset2 / BITS_PER_UNIT);
3253 if (byte_offset1 != byte_offset2)
3254 return false;
3255 return bit_offset1 % BITS_PER_UNIT == bit_offset2 % BITS_PER_UNIT;
3258 return false;
3261 static bool
3262 gimple_types_compatible_p_1 (tree, tree, type_pair_t,
3263 VEC(type_pair_t, heap) **,
3264 struct pointer_map_t *, struct obstack *);
3266 /* DFS visit the edge from the callers type pair with state *STATE to
3267 the pair T1, T2 while operating in FOR_MERGING_P mode.
3268 Update the merging status if it is not part of the SCC containing the
3269 callers pair and return it.
3270 SCCSTACK, SCCSTATE and SCCSTATE_OBSTACK are state for the DFS walk done. */
3272 static bool
3273 gtc_visit (tree t1, tree t2,
3274 struct sccs *state,
3275 VEC(type_pair_t, heap) **sccstack,
3276 struct pointer_map_t *sccstate,
3277 struct obstack *sccstate_obstack)
3279 struct sccs *cstate = NULL;
3280 type_pair_t p;
3281 void **slot;
3282 tree leader1, leader2;
3284 /* Check first for the obvious case of pointer identity. */
3285 if (t1 == t2)
3286 return true;
3288 /* Check that we have two types to compare. */
3289 if (t1 == NULL_TREE || t2 == NULL_TREE)
3290 return false;
3292 /* Can't be the same type if the types don't have the same code. */
3293 if (TREE_CODE (t1) != TREE_CODE (t2))
3294 return false;
3296 /* Can't be the same type if they have different CV qualifiers. */
3297 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
3298 return false;
3300 if (TREE_ADDRESSABLE (t1) != TREE_ADDRESSABLE (t2))
3301 return false;
3303 /* Void types and nullptr types are always the same. */
3304 if (TREE_CODE (t1) == VOID_TYPE
3305 || TREE_CODE (t1) == NULLPTR_TYPE)
3306 return true;
3308 /* Can't be the same type if they have different alignment or mode. */
3309 if (TYPE_ALIGN (t1) != TYPE_ALIGN (t2)
3310 || TYPE_MODE (t1) != TYPE_MODE (t2))
3311 return false;
3313 /* Do some simple checks before doing three hashtable queries. */
3314 if (INTEGRAL_TYPE_P (t1)
3315 || SCALAR_FLOAT_TYPE_P (t1)
3316 || FIXED_POINT_TYPE_P (t1)
3317 || TREE_CODE (t1) == VECTOR_TYPE
3318 || TREE_CODE (t1) == COMPLEX_TYPE
3319 || TREE_CODE (t1) == OFFSET_TYPE
3320 || POINTER_TYPE_P (t1))
3322 /* Can't be the same type if they have different sign or precision. */
3323 if (TYPE_PRECISION (t1) != TYPE_PRECISION (t2)
3324 || TYPE_UNSIGNED (t1) != TYPE_UNSIGNED (t2))
3325 return false;
3327 if (TREE_CODE (t1) == INTEGER_TYPE
3328 && TYPE_STRING_FLAG (t1) != TYPE_STRING_FLAG (t2))
3329 return false;
3331 /* That's all we need to check for float and fixed-point types. */
3332 if (SCALAR_FLOAT_TYPE_P (t1)
3333 || FIXED_POINT_TYPE_P (t1))
3334 return true;
3336 /* For other types fall through to more complex checks. */
3339 /* If the types have been previously registered and found equal
3340 they still are. */
3341 leader1 = gimple_lookup_type_leader (t1);
3342 leader2 = gimple_lookup_type_leader (t2);
3343 if (leader1 == t2
3344 || t1 == leader2
3345 || (leader1 && leader1 == leader2))
3346 return true;
3348 /* If the hash values of t1 and t2 are different the types can't
3349 possibly be the same. This helps keeping the type-pair hashtable
3350 small, only tracking comparisons for hash collisions. */
3351 if (gimple_type_hash (t1) != gimple_type_hash (t2))
3352 return false;
3354 /* Allocate a new cache entry for this comparison. */
3355 p = lookup_type_pair (t1, t2);
3356 if (p->same_p[GTC_MERGE] == 0 || p->same_p[GTC_MERGE] == 1)
3358 /* We have already decided whether T1 and T2 are the
3359 same, return the cached result. */
3360 return p->same_p[GTC_MERGE] == 1;
3363 if ((slot = pointer_map_contains (sccstate, p)) != NULL)
3364 cstate = (struct sccs *)*slot;
3365 /* Not yet visited. DFS recurse. */
3366 if (!cstate)
3368 gimple_types_compatible_p_1 (t1, t2, p,
3369 sccstack, sccstate, sccstate_obstack);
3370 cstate = (struct sccs *)* pointer_map_contains (sccstate, p);
3371 state->low = MIN (state->low, cstate->low);
3373 /* If the type is still on the SCC stack adjust the parents low. */
3374 if (cstate->dfsnum < state->dfsnum
3375 && cstate->on_sccstack)
3376 state->low = MIN (cstate->dfsnum, state->low);
3378 /* Return the current lattice value. We start with an equality
3379 assumption so types part of a SCC will be optimistically
3380 treated equal unless proven otherwise. */
3381 return cstate->u.same_p;
3384 /* Worker for gimple_types_compatible.
3385 SCCSTACK, SCCSTATE and SCCSTATE_OBSTACK are state for the DFS walk done. */
3387 static bool
3388 gimple_types_compatible_p_1 (tree t1, tree t2, type_pair_t p,
3389 VEC(type_pair_t, heap) **sccstack,
3390 struct pointer_map_t *sccstate,
3391 struct obstack *sccstate_obstack)
3393 struct sccs *state;
3395 gcc_assert (p->same_p[GTC_MERGE] == -2);
3397 state = XOBNEW (sccstate_obstack, struct sccs);
3398 *pointer_map_insert (sccstate, p) = state;
3400 VEC_safe_push (type_pair_t, heap, *sccstack, p);
3401 state->dfsnum = gtc_next_dfs_num++;
3402 state->low = state->dfsnum;
3403 state->on_sccstack = true;
3404 /* Start with an equality assumption. As we DFS recurse into child
3405 SCCs this assumption may get revisited. */
3406 state->u.same_p = 1;
3408 /* The struct tags shall compare equal. */
3409 if (!compare_type_names_p (t1, t2))
3410 goto different_types;
3412 /* We may not merge typedef types to the same type in different
3413 contexts. */
3414 if (TYPE_NAME (t1)
3415 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
3416 && DECL_CONTEXT (TYPE_NAME (t1))
3417 && TYPE_P (DECL_CONTEXT (TYPE_NAME (t1))))
3419 if (!gtc_visit (DECL_CONTEXT (TYPE_NAME (t1)),
3420 DECL_CONTEXT (TYPE_NAME (t2)),
3421 state, sccstack, sccstate, sccstate_obstack))
3422 goto different_types;
3425 /* If their attributes are not the same they can't be the same type. */
3426 if (!attribute_list_equal (TYPE_ATTRIBUTES (t1), TYPE_ATTRIBUTES (t2)))
3427 goto different_types;
3429 /* Do type-specific comparisons. */
3430 switch (TREE_CODE (t1))
3432 case VECTOR_TYPE:
3433 case COMPLEX_TYPE:
3434 if (!gtc_visit (TREE_TYPE (t1), TREE_TYPE (t2),
3435 state, sccstack, sccstate, sccstate_obstack))
3436 goto different_types;
3437 goto same_types;
3439 case ARRAY_TYPE:
3440 /* Array types are the same if the element types are the same and
3441 the number of elements are the same. */
3442 if (!gtc_visit (TREE_TYPE (t1), TREE_TYPE (t2),
3443 state, sccstack, sccstate, sccstate_obstack)
3444 || TYPE_STRING_FLAG (t1) != TYPE_STRING_FLAG (t2)
3445 || TYPE_NONALIASED_COMPONENT (t1) != TYPE_NONALIASED_COMPONENT (t2))
3446 goto different_types;
3447 else
3449 tree i1 = TYPE_DOMAIN (t1);
3450 tree i2 = TYPE_DOMAIN (t2);
3452 /* For an incomplete external array, the type domain can be
3453 NULL_TREE. Check this condition also. */
3454 if (i1 == NULL_TREE && i2 == NULL_TREE)
3455 goto same_types;
3456 else if (i1 == NULL_TREE || i2 == NULL_TREE)
3457 goto different_types;
3458 else
3460 tree min1 = TYPE_MIN_VALUE (i1);
3461 tree min2 = TYPE_MIN_VALUE (i2);
3462 tree max1 = TYPE_MAX_VALUE (i1);
3463 tree max2 = TYPE_MAX_VALUE (i2);
3465 /* The minimum/maximum values have to be the same. */
3466 if ((min1 == min2
3467 || (min1 && min2
3468 && ((TREE_CODE (min1) == PLACEHOLDER_EXPR
3469 && TREE_CODE (min2) == PLACEHOLDER_EXPR)
3470 || operand_equal_p (min1, min2, 0))))
3471 && (max1 == max2
3472 || (max1 && max2
3473 && ((TREE_CODE (max1) == PLACEHOLDER_EXPR
3474 && TREE_CODE (max2) == PLACEHOLDER_EXPR)
3475 || operand_equal_p (max1, max2, 0)))))
3476 goto same_types;
3477 else
3478 goto different_types;
3482 case METHOD_TYPE:
3483 /* Method types should belong to the same class. */
3484 if (!gtc_visit (TYPE_METHOD_BASETYPE (t1), TYPE_METHOD_BASETYPE (t2),
3485 state, sccstack, sccstate, sccstate_obstack))
3486 goto different_types;
3488 /* Fallthru */
3490 case FUNCTION_TYPE:
3491 /* Function types are the same if the return type and arguments types
3492 are the same. */
3493 if (!gtc_visit (TREE_TYPE (t1), TREE_TYPE (t2),
3494 state, sccstack, sccstate, sccstate_obstack))
3495 goto different_types;
3497 if (!comp_type_attributes (t1, t2))
3498 goto different_types;
3500 if (TYPE_ARG_TYPES (t1) == TYPE_ARG_TYPES (t2))
3501 goto same_types;
3502 else
3504 tree parms1, parms2;
3506 for (parms1 = TYPE_ARG_TYPES (t1), parms2 = TYPE_ARG_TYPES (t2);
3507 parms1 && parms2;
3508 parms1 = TREE_CHAIN (parms1), parms2 = TREE_CHAIN (parms2))
3510 if (!gtc_visit (TREE_VALUE (parms1), TREE_VALUE (parms2),
3511 state, sccstack, sccstate, sccstate_obstack))
3512 goto different_types;
3515 if (parms1 || parms2)
3516 goto different_types;
3518 goto same_types;
3521 case OFFSET_TYPE:
3523 if (!gtc_visit (TREE_TYPE (t1), TREE_TYPE (t2),
3524 state, sccstack, sccstate, sccstate_obstack)
3525 || !gtc_visit (TYPE_OFFSET_BASETYPE (t1),
3526 TYPE_OFFSET_BASETYPE (t2),
3527 state, sccstack, sccstate, sccstate_obstack))
3528 goto different_types;
3530 goto same_types;
3533 case POINTER_TYPE:
3534 case REFERENCE_TYPE:
3536 /* If the two pointers have different ref-all attributes,
3537 they can't be the same type. */
3538 if (TYPE_REF_CAN_ALIAS_ALL (t1) != TYPE_REF_CAN_ALIAS_ALL (t2))
3539 goto different_types;
3541 /* Otherwise, pointer and reference types are the same if the
3542 pointed-to types are the same. */
3543 if (gtc_visit (TREE_TYPE (t1), TREE_TYPE (t2),
3544 state, sccstack, sccstate, sccstate_obstack))
3545 goto same_types;
3547 goto different_types;
3550 case INTEGER_TYPE:
3551 case BOOLEAN_TYPE:
3553 tree min1 = TYPE_MIN_VALUE (t1);
3554 tree max1 = TYPE_MAX_VALUE (t1);
3555 tree min2 = TYPE_MIN_VALUE (t2);
3556 tree max2 = TYPE_MAX_VALUE (t2);
3557 bool min_equal_p = false;
3558 bool max_equal_p = false;
3560 /* If either type has a minimum value, the other type must
3561 have the same. */
3562 if (min1 == NULL_TREE && min2 == NULL_TREE)
3563 min_equal_p = true;
3564 else if (min1 && min2 && operand_equal_p (min1, min2, 0))
3565 min_equal_p = true;
3567 /* Likewise, if either type has a maximum value, the other
3568 type must have the same. */
3569 if (max1 == NULL_TREE && max2 == NULL_TREE)
3570 max_equal_p = true;
3571 else if (max1 && max2 && operand_equal_p (max1, max2, 0))
3572 max_equal_p = true;
3574 if (!min_equal_p || !max_equal_p)
3575 goto different_types;
3577 goto same_types;
3580 case ENUMERAL_TYPE:
3582 /* FIXME lto, we cannot check bounds on enumeral types because
3583 different front ends will produce different values.
3584 In C, enumeral types are integers, while in C++ each element
3585 will have its own symbolic value. We should decide how enums
3586 are to be represented in GIMPLE and have each front end lower
3587 to that. */
3588 tree v1, v2;
3590 /* For enumeral types, all the values must be the same. */
3591 if (TYPE_VALUES (t1) == TYPE_VALUES (t2))
3592 goto same_types;
3594 for (v1 = TYPE_VALUES (t1), v2 = TYPE_VALUES (t2);
3595 v1 && v2;
3596 v1 = TREE_CHAIN (v1), v2 = TREE_CHAIN (v2))
3598 tree c1 = TREE_VALUE (v1);
3599 tree c2 = TREE_VALUE (v2);
3601 if (TREE_CODE (c1) == CONST_DECL)
3602 c1 = DECL_INITIAL (c1);
3604 if (TREE_CODE (c2) == CONST_DECL)
3605 c2 = DECL_INITIAL (c2);
3607 if (tree_int_cst_equal (c1, c2) != 1)
3608 goto different_types;
3610 if (TREE_PURPOSE (v1) != TREE_PURPOSE (v2))
3611 goto different_types;
3614 /* If one enumeration has more values than the other, they
3615 are not the same. */
3616 if (v1 || v2)
3617 goto different_types;
3619 goto same_types;
3622 case RECORD_TYPE:
3623 case UNION_TYPE:
3624 case QUAL_UNION_TYPE:
3626 tree f1, f2;
3628 /* For aggregate types, all the fields must be the same. */
3629 for (f1 = TYPE_FIELDS (t1), f2 = TYPE_FIELDS (t2);
3630 f1 && f2;
3631 f1 = TREE_CHAIN (f1), f2 = TREE_CHAIN (f2))
3633 /* Different field kinds are not compatible. */
3634 if (TREE_CODE (f1) != TREE_CODE (f2))
3635 goto different_types;
3636 /* Field decls must have the same name and offset. */
3637 if (TREE_CODE (f1) == FIELD_DECL
3638 && (DECL_NONADDRESSABLE_P (f1) != DECL_NONADDRESSABLE_P (f2)
3639 || !gimple_compare_field_offset (f1, f2)))
3640 goto different_types;
3641 /* All entities should have the same name and type. */
3642 if (DECL_NAME (f1) != DECL_NAME (f2)
3643 || !gtc_visit (TREE_TYPE (f1), TREE_TYPE (f2),
3644 state, sccstack, sccstate, sccstate_obstack))
3645 goto different_types;
3648 /* If one aggregate has more fields than the other, they
3649 are not the same. */
3650 if (f1 || f2)
3651 goto different_types;
3653 goto same_types;
3656 default:
3657 gcc_unreachable ();
3660 /* Common exit path for types that are not compatible. */
3661 different_types:
3662 state->u.same_p = 0;
3663 goto pop;
3665 /* Common exit path for types that are compatible. */
3666 same_types:
3667 gcc_assert (state->u.same_p == 1);
3669 pop:
3670 if (state->low == state->dfsnum)
3672 type_pair_t x;
3674 /* Pop off the SCC and set its cache values to the final
3675 comparison result. */
3678 struct sccs *cstate;
3679 x = VEC_pop (type_pair_t, *sccstack);
3680 cstate = (struct sccs *)*pointer_map_contains (sccstate, x);
3681 cstate->on_sccstack = false;
3682 x->same_p[GTC_MERGE] = state->u.same_p;
3684 while (x != p);
3687 return state->u.same_p;
3690 /* Return true iff T1 and T2 are structurally identical. When
3691 FOR_MERGING_P is true the an incomplete type and a complete type
3692 are considered different, otherwise they are considered compatible. */
3694 static bool
3695 gimple_types_compatible_p (tree t1, tree t2)
3697 VEC(type_pair_t, heap) *sccstack = NULL;
3698 struct pointer_map_t *sccstate;
3699 struct obstack sccstate_obstack;
3700 type_pair_t p = NULL;
3701 bool res;
3702 tree leader1, leader2;
3704 /* Before starting to set up the SCC machinery handle simple cases. */
3706 /* Check first for the obvious case of pointer identity. */
3707 if (t1 == t2)
3708 return true;
3710 /* Check that we have two types to compare. */
3711 if (t1 == NULL_TREE || t2 == NULL_TREE)
3712 return false;
3714 /* Can't be the same type if the types don't have the same code. */
3715 if (TREE_CODE (t1) != TREE_CODE (t2))
3716 return false;
3718 /* Can't be the same type if they have different CV qualifiers. */
3719 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
3720 return false;
3722 if (TREE_ADDRESSABLE (t1) != TREE_ADDRESSABLE (t2))
3723 return false;
3725 /* Void types and nullptr types are always the same. */
3726 if (TREE_CODE (t1) == VOID_TYPE
3727 || TREE_CODE (t1) == NULLPTR_TYPE)
3728 return true;
3730 /* Can't be the same type if they have different alignment or mode. */
3731 if (TYPE_ALIGN (t1) != TYPE_ALIGN (t2)
3732 || TYPE_MODE (t1) != TYPE_MODE (t2))
3733 return false;
3735 /* Do some simple checks before doing three hashtable queries. */
3736 if (INTEGRAL_TYPE_P (t1)
3737 || SCALAR_FLOAT_TYPE_P (t1)
3738 || FIXED_POINT_TYPE_P (t1)
3739 || TREE_CODE (t1) == VECTOR_TYPE
3740 || TREE_CODE (t1) == COMPLEX_TYPE
3741 || TREE_CODE (t1) == OFFSET_TYPE
3742 || POINTER_TYPE_P (t1))
3744 /* Can't be the same type if they have different sign or precision. */
3745 if (TYPE_PRECISION (t1) != TYPE_PRECISION (t2)
3746 || TYPE_UNSIGNED (t1) != TYPE_UNSIGNED (t2))
3747 return false;
3749 if (TREE_CODE (t1) == INTEGER_TYPE
3750 && TYPE_STRING_FLAG (t1) != TYPE_STRING_FLAG (t2))
3751 return false;
3753 /* That's all we need to check for float and fixed-point types. */
3754 if (SCALAR_FLOAT_TYPE_P (t1)
3755 || FIXED_POINT_TYPE_P (t1))
3756 return true;
3758 /* For other types fall through to more complex checks. */
3761 /* If the types have been previously registered and found equal
3762 they still are. */
3763 leader1 = gimple_lookup_type_leader (t1);
3764 leader2 = gimple_lookup_type_leader (t2);
3765 if (leader1 == t2
3766 || t1 == leader2
3767 || (leader1 && leader1 == leader2))
3768 return true;
3770 /* If the hash values of t1 and t2 are different the types can't
3771 possibly be the same. This helps keeping the type-pair hashtable
3772 small, only tracking comparisons for hash collisions. */
3773 if (gimple_type_hash (t1) != gimple_type_hash (t2))
3774 return false;
3776 /* If we've visited this type pair before (in the case of aggregates
3777 with self-referential types), and we made a decision, return it. */
3778 p = lookup_type_pair (t1, t2);
3779 if (p->same_p[GTC_MERGE] == 0 || p->same_p[GTC_MERGE] == 1)
3781 /* We have already decided whether T1 and T2 are the
3782 same, return the cached result. */
3783 return p->same_p[GTC_MERGE] == 1;
3786 /* Now set up the SCC machinery for the comparison. */
3787 gtc_next_dfs_num = 1;
3788 sccstate = pointer_map_create ();
3789 gcc_obstack_init (&sccstate_obstack);
3790 res = gimple_types_compatible_p_1 (t1, t2, p,
3791 &sccstack, sccstate, &sccstate_obstack);
3792 VEC_free (type_pair_t, heap, sccstack);
3793 pointer_map_destroy (sccstate);
3794 obstack_free (&sccstate_obstack, NULL);
3796 return res;
3800 static hashval_t
3801 iterative_hash_gimple_type (tree, hashval_t, VEC(tree, heap) **,
3802 struct pointer_map_t *, struct obstack *);
3804 /* DFS visit the edge from the callers type with state *STATE to T.
3805 Update the callers type hash V with the hash for T if it is not part
3806 of the SCC containing the callers type and return it.
3807 SCCSTACK, SCCSTATE and SCCSTATE_OBSTACK are state for the DFS walk done. */
3809 static hashval_t
3810 visit (tree t, struct sccs *state, hashval_t v,
3811 VEC (tree, heap) **sccstack,
3812 struct pointer_map_t *sccstate,
3813 struct obstack *sccstate_obstack)
3815 struct sccs *cstate = NULL;
3816 struct tree_int_map m;
3817 void **slot;
3819 /* If there is a hash value recorded for this type then it can't
3820 possibly be part of our parent SCC. Simply mix in its hash. */
3821 m.base.from = t;
3822 if ((slot = htab_find_slot (type_hash_cache, &m, NO_INSERT))
3823 && *slot)
3824 return iterative_hash_hashval_t (((struct tree_int_map *) *slot)->to, v);
3826 if ((slot = pointer_map_contains (sccstate, t)) != NULL)
3827 cstate = (struct sccs *)*slot;
3828 if (!cstate)
3830 hashval_t tem;
3831 /* Not yet visited. DFS recurse. */
3832 tem = iterative_hash_gimple_type (t, v,
3833 sccstack, sccstate, sccstate_obstack);
3834 if (!cstate)
3835 cstate = (struct sccs *)* pointer_map_contains (sccstate, t);
3836 state->low = MIN (state->low, cstate->low);
3837 /* If the type is no longer on the SCC stack and thus is not part
3838 of the parents SCC mix in its hash value. Otherwise we will
3839 ignore the type for hashing purposes and return the unaltered
3840 hash value. */
3841 if (!cstate->on_sccstack)
3842 return tem;
3844 if (cstate->dfsnum < state->dfsnum
3845 && cstate->on_sccstack)
3846 state->low = MIN (cstate->dfsnum, state->low);
3848 /* We are part of our parents SCC, skip this type during hashing
3849 and return the unaltered hash value. */
3850 return v;
3853 /* Hash NAME with the previous hash value V and return it. */
3855 static hashval_t
3856 iterative_hash_name (tree name, hashval_t v)
3858 if (!name)
3859 return v;
3860 v = iterative_hash_hashval_t (TREE_CODE (name), v);
3861 if (TREE_CODE (name) == TYPE_DECL)
3862 name = DECL_NAME (name);
3863 if (!name)
3864 return v;
3865 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
3866 return iterative_hash_object (IDENTIFIER_HASH_VALUE (name), v);
3869 /* A type, hashvalue pair for sorting SCC members. */
3871 struct type_hash_pair {
3872 tree type;
3873 hashval_t hash;
3876 /* Compare two type, hashvalue pairs. */
3878 static int
3879 type_hash_pair_compare (const void *p1_, const void *p2_)
3881 const struct type_hash_pair *p1 = (const struct type_hash_pair *) p1_;
3882 const struct type_hash_pair *p2 = (const struct type_hash_pair *) p2_;
3883 if (p1->hash < p2->hash)
3884 return -1;
3885 else if (p1->hash > p2->hash)
3886 return 1;
3887 return 0;
3890 /* Returning a hash value for gimple type TYPE combined with VAL.
3891 SCCSTACK, SCCSTATE and SCCSTATE_OBSTACK are state for the DFS walk done.
3893 To hash a type we end up hashing in types that are reachable.
3894 Through pointers we can end up with cycles which messes up the
3895 required property that we need to compute the same hash value
3896 for structurally equivalent types. To avoid this we have to
3897 hash all types in a cycle (the SCC) in a commutative way. The
3898 easiest way is to not mix in the hashes of the SCC members at
3899 all. To make this work we have to delay setting the hash
3900 values of the SCC until it is complete. */
3902 static hashval_t
3903 iterative_hash_gimple_type (tree type, hashval_t val,
3904 VEC(tree, heap) **sccstack,
3905 struct pointer_map_t *sccstate,
3906 struct obstack *sccstate_obstack)
3908 hashval_t v;
3909 void **slot;
3910 struct sccs *state;
3912 /* Not visited during this DFS walk. */
3913 gcc_checking_assert (!pointer_map_contains (sccstate, type));
3914 state = XOBNEW (sccstate_obstack, struct sccs);
3915 *pointer_map_insert (sccstate, type) = state;
3917 VEC_safe_push (tree, heap, *sccstack, type);
3918 state->dfsnum = next_dfs_num++;
3919 state->low = state->dfsnum;
3920 state->on_sccstack = true;
3922 /* Combine a few common features of types so that types are grouped into
3923 smaller sets; when searching for existing matching types to merge,
3924 only existing types having the same features as the new type will be
3925 checked. */
3926 v = iterative_hash_name (TYPE_NAME (type), 0);
3927 if (TYPE_NAME (type)
3928 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
3929 && DECL_CONTEXT (TYPE_NAME (type))
3930 && TYPE_P (DECL_CONTEXT (TYPE_NAME (type))))
3931 v = visit (DECL_CONTEXT (TYPE_NAME (type)), state, v,
3932 sccstack, sccstate, sccstate_obstack);
3933 v = iterative_hash_hashval_t (TREE_CODE (type), v);
3934 v = iterative_hash_hashval_t (TYPE_QUALS (type), v);
3935 v = iterative_hash_hashval_t (TREE_ADDRESSABLE (type), v);
3937 /* Do not hash the types size as this will cause differences in
3938 hash values for the complete vs. the incomplete type variant. */
3940 /* Incorporate common features of numerical types. */
3941 if (INTEGRAL_TYPE_P (type)
3942 || SCALAR_FLOAT_TYPE_P (type)
3943 || FIXED_POINT_TYPE_P (type))
3945 v = iterative_hash_hashval_t (TYPE_PRECISION (type), v);
3946 v = iterative_hash_hashval_t (TYPE_MODE (type), v);
3947 v = iterative_hash_hashval_t (TYPE_UNSIGNED (type), v);
3950 /* For pointer and reference types, fold in information about the type
3951 pointed to. */
3952 if (POINTER_TYPE_P (type))
3953 v = visit (TREE_TYPE (type), state, v,
3954 sccstack, sccstate, sccstate_obstack);
3956 /* For integer types hash the types min/max values and the string flag. */
3957 if (TREE_CODE (type) == INTEGER_TYPE)
3959 /* OMP lowering can introduce error_mark_node in place of
3960 random local decls in types. */
3961 if (TYPE_MIN_VALUE (type) != error_mark_node)
3962 v = iterative_hash_expr (TYPE_MIN_VALUE (type), v);
3963 if (TYPE_MAX_VALUE (type) != error_mark_node)
3964 v = iterative_hash_expr (TYPE_MAX_VALUE (type), v);
3965 v = iterative_hash_hashval_t (TYPE_STRING_FLAG (type), v);
3968 /* For array types hash the domain and the string flag. */
3969 if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
3971 v = iterative_hash_hashval_t (TYPE_STRING_FLAG (type), v);
3972 v = visit (TYPE_DOMAIN (type), state, v,
3973 sccstack, sccstate, sccstate_obstack);
3976 /* Recurse for aggregates with a single element type. */
3977 if (TREE_CODE (type) == ARRAY_TYPE
3978 || TREE_CODE (type) == COMPLEX_TYPE
3979 || TREE_CODE (type) == VECTOR_TYPE)
3980 v = visit (TREE_TYPE (type), state, v,
3981 sccstack, sccstate, sccstate_obstack);
3983 /* Incorporate function return and argument types. */
3984 if (TREE_CODE (type) == FUNCTION_TYPE || TREE_CODE (type) == METHOD_TYPE)
3986 unsigned na;
3987 tree p;
3989 /* For method types also incorporate their parent class. */
3990 if (TREE_CODE (type) == METHOD_TYPE)
3991 v = visit (TYPE_METHOD_BASETYPE (type), state, v,
3992 sccstack, sccstate, sccstate_obstack);
3994 /* Check result and argument types. */
3995 v = visit (TREE_TYPE (type), state, v,
3996 sccstack, sccstate, sccstate_obstack);
3997 for (p = TYPE_ARG_TYPES (type), na = 0; p; p = TREE_CHAIN (p))
3999 v = visit (TREE_VALUE (p), state, v,
4000 sccstack, sccstate, sccstate_obstack);
4001 na++;
4004 v = iterative_hash_hashval_t (na, v);
4007 if (RECORD_OR_UNION_TYPE_P (type))
4009 unsigned nf;
4010 tree f;
4012 for (f = TYPE_FIELDS (type), nf = 0; f; f = TREE_CHAIN (f))
4014 v = iterative_hash_name (DECL_NAME (f), v);
4015 v = visit (TREE_TYPE (f), state, v,
4016 sccstack, sccstate, sccstate_obstack);
4017 nf++;
4020 v = iterative_hash_hashval_t (nf, v);
4023 /* Record hash for us. */
4024 state->u.hash = v;
4026 /* See if we found an SCC. */
4027 if (state->low == state->dfsnum)
4029 tree x;
4030 struct tree_int_map *m;
4032 /* Pop off the SCC and set its hash values. */
4033 x = VEC_pop (tree, *sccstack);
4034 /* Optimize SCC size one. */
4035 if (x == type)
4037 state->on_sccstack = false;
4038 m = ggc_alloc_cleared_tree_int_map ();
4039 m->base.from = x;
4040 m->to = v;
4041 slot = htab_find_slot (type_hash_cache, m, INSERT);
4042 gcc_assert (!*slot);
4043 *slot = (void *) m;
4045 else
4047 struct sccs *cstate;
4048 unsigned first, i, size, j;
4049 struct type_hash_pair *pairs;
4050 /* Pop off the SCC and build an array of type, hash pairs. */
4051 first = VEC_length (tree, *sccstack) - 1;
4052 while (VEC_index (tree, *sccstack, first) != type)
4053 --first;
4054 size = VEC_length (tree, *sccstack) - first + 1;
4055 pairs = XALLOCAVEC (struct type_hash_pair, size);
4056 i = 0;
4057 cstate = (struct sccs *)*pointer_map_contains (sccstate, x);
4058 cstate->on_sccstack = false;
4059 pairs[i].type = x;
4060 pairs[i].hash = cstate->u.hash;
4063 x = VEC_pop (tree, *sccstack);
4064 cstate = (struct sccs *)*pointer_map_contains (sccstate, x);
4065 cstate->on_sccstack = false;
4066 ++i;
4067 pairs[i].type = x;
4068 pairs[i].hash = cstate->u.hash;
4070 while (x != type);
4071 gcc_assert (i + 1 == size);
4072 /* Sort the arrays of type, hash pairs so that when we mix in
4073 all members of the SCC the hash value becomes independent on
4074 the order we visited the SCC. Disregard hashes equal to
4075 the hash of the type we mix into because we cannot guarantee
4076 a stable sort for those across different TUs. */
4077 qsort (pairs, size, sizeof (struct type_hash_pair),
4078 type_hash_pair_compare);
4079 for (i = 0; i < size; ++i)
4081 hashval_t hash;
4082 m = ggc_alloc_cleared_tree_int_map ();
4083 m->base.from = pairs[i].type;
4084 hash = pairs[i].hash;
4085 /* Skip same hashes. */
4086 for (j = i + 1; j < size && pairs[j].hash == pairs[i].hash; ++j)
4088 for (; j < size; ++j)
4089 hash = iterative_hash_hashval_t (pairs[j].hash, hash);
4090 for (j = 0; pairs[j].hash != pairs[i].hash; ++j)
4091 hash = iterative_hash_hashval_t (pairs[j].hash, hash);
4092 m->to = hash;
4093 if (pairs[i].type == type)
4094 v = hash;
4095 slot = htab_find_slot (type_hash_cache, m, INSERT);
4096 gcc_assert (!*slot);
4097 *slot = (void *) m;
4102 return iterative_hash_hashval_t (v, val);
4106 /* Returns a hash value for P (assumed to be a type). The hash value
4107 is computed using some distinguishing features of the type. Note
4108 that we cannot use pointer hashing here as we may be dealing with
4109 two distinct instances of the same type.
4111 This function should produce the same hash value for two compatible
4112 types according to gimple_types_compatible_p. */
4114 static hashval_t
4115 gimple_type_hash (const void *p)
4117 const_tree t = (const_tree) p;
4118 VEC(tree, heap) *sccstack = NULL;
4119 struct pointer_map_t *sccstate;
4120 struct obstack sccstate_obstack;
4121 hashval_t val;
4122 void **slot;
4123 struct tree_int_map m;
4125 if (type_hash_cache == NULL)
4126 type_hash_cache = htab_create_ggc (512, tree_int_map_hash,
4127 tree_int_map_eq, NULL);
4129 m.base.from = CONST_CAST_TREE (t);
4130 if ((slot = htab_find_slot (type_hash_cache, &m, NO_INSERT))
4131 && *slot)
4132 return iterative_hash_hashval_t (((struct tree_int_map *) *slot)->to, 0);
4134 /* Perform a DFS walk and pre-hash all reachable types. */
4135 next_dfs_num = 1;
4136 sccstate = pointer_map_create ();
4137 gcc_obstack_init (&sccstate_obstack);
4138 val = iterative_hash_gimple_type (CONST_CAST_TREE (t), 0,
4139 &sccstack, sccstate, &sccstate_obstack);
4140 VEC_free (tree, heap, sccstack);
4141 pointer_map_destroy (sccstate);
4142 obstack_free (&sccstate_obstack, NULL);
4144 return val;
4147 /* Returning a hash value for gimple type TYPE combined with VAL.
4149 The hash value returned is equal for types considered compatible
4150 by gimple_canonical_types_compatible_p. */
4152 static hashval_t
4153 iterative_hash_canonical_type (tree type, hashval_t val)
4155 hashval_t v;
4156 void **slot;
4157 struct tree_int_map *mp, m;
4159 m.base.from = type;
4160 if ((slot = htab_find_slot (canonical_type_hash_cache, &m, INSERT))
4161 && *slot)
4162 return iterative_hash_hashval_t (((struct tree_int_map *) *slot)->to, val);
4164 /* Combine a few common features of types so that types are grouped into
4165 smaller sets; when searching for existing matching types to merge,
4166 only existing types having the same features as the new type will be
4167 checked. */
4168 v = iterative_hash_hashval_t (TREE_CODE (type), 0);
4169 v = iterative_hash_hashval_t (TREE_ADDRESSABLE (type), v);
4170 v = iterative_hash_hashval_t (TYPE_ALIGN (type), v);
4171 v = iterative_hash_hashval_t (TYPE_MODE (type), v);
4173 /* Incorporate common features of numerical types. */
4174 if (INTEGRAL_TYPE_P (type)
4175 || SCALAR_FLOAT_TYPE_P (type)
4176 || FIXED_POINT_TYPE_P (type)
4177 || TREE_CODE (type) == VECTOR_TYPE
4178 || TREE_CODE (type) == COMPLEX_TYPE
4179 || TREE_CODE (type) == OFFSET_TYPE
4180 || POINTER_TYPE_P (type))
4182 v = iterative_hash_hashval_t (TYPE_PRECISION (type), v);
4183 v = iterative_hash_hashval_t (TYPE_UNSIGNED (type), v);
4186 /* For pointer and reference types, fold in information about the type
4187 pointed to but do not recurse to the pointed-to type. */
4188 if (POINTER_TYPE_P (type))
4190 v = iterative_hash_hashval_t (TYPE_REF_CAN_ALIAS_ALL (type), v);
4191 v = iterative_hash_hashval_t (TYPE_ADDR_SPACE (TREE_TYPE (type)), v);
4192 v = iterative_hash_hashval_t (TYPE_RESTRICT (type), v);
4193 v = iterative_hash_hashval_t (TREE_CODE (TREE_TYPE (type)), v);
4196 /* For integer types hash only the string flag. */
4197 if (TREE_CODE (type) == INTEGER_TYPE)
4198 v = iterative_hash_hashval_t (TYPE_STRING_FLAG (type), v);
4200 /* For array types hash the domain bounds and the string flag. */
4201 if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
4203 v = iterative_hash_hashval_t (TYPE_STRING_FLAG (type), v);
4204 /* OMP lowering can introduce error_mark_node in place of
4205 random local decls in types. */
4206 if (TYPE_MIN_VALUE (TYPE_DOMAIN (type)) != error_mark_node)
4207 v = iterative_hash_expr (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), v);
4208 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != error_mark_node)
4209 v = iterative_hash_expr (TYPE_MAX_VALUE (TYPE_DOMAIN (type)), v);
4212 /* Recurse for aggregates with a single element type. */
4213 if (TREE_CODE (type) == ARRAY_TYPE
4214 || TREE_CODE (type) == COMPLEX_TYPE
4215 || TREE_CODE (type) == VECTOR_TYPE)
4216 v = iterative_hash_canonical_type (TREE_TYPE (type), v);
4218 /* Incorporate function return and argument types. */
4219 if (TREE_CODE (type) == FUNCTION_TYPE || TREE_CODE (type) == METHOD_TYPE)
4221 unsigned na;
4222 tree p;
4224 /* For method types also incorporate their parent class. */
4225 if (TREE_CODE (type) == METHOD_TYPE)
4226 v = iterative_hash_canonical_type (TYPE_METHOD_BASETYPE (type), v);
4228 v = iterative_hash_canonical_type (TREE_TYPE (type), v);
4230 for (p = TYPE_ARG_TYPES (type), na = 0; p; p = TREE_CHAIN (p))
4232 v = iterative_hash_canonical_type (TREE_VALUE (p), v);
4233 na++;
4236 v = iterative_hash_hashval_t (na, v);
4239 if (RECORD_OR_UNION_TYPE_P (type))
4241 unsigned nf;
4242 tree f;
4244 for (f = TYPE_FIELDS (type), nf = 0; f; f = TREE_CHAIN (f))
4245 if (TREE_CODE (f) == FIELD_DECL)
4247 v = iterative_hash_canonical_type (TREE_TYPE (f), v);
4248 nf++;
4251 v = iterative_hash_hashval_t (nf, v);
4254 /* Cache the just computed hash value. */
4255 mp = ggc_alloc_cleared_tree_int_map ();
4256 mp->base.from = type;
4257 mp->to = v;
4258 *slot = (void *) mp;
4260 return iterative_hash_hashval_t (v, val);
4263 static hashval_t
4264 gimple_canonical_type_hash (const void *p)
4266 if (canonical_type_hash_cache == NULL)
4267 canonical_type_hash_cache = htab_create_ggc (512, tree_int_map_hash,
4268 tree_int_map_eq, NULL);
4270 return iterative_hash_canonical_type (CONST_CAST_TREE ((const_tree) p), 0);
4274 /* Returns nonzero if P1 and P2 are equal. */
4276 static int
4277 gimple_type_eq (const void *p1, const void *p2)
4279 const_tree t1 = (const_tree) p1;
4280 const_tree t2 = (const_tree) p2;
4281 return gimple_types_compatible_p (CONST_CAST_TREE (t1),
4282 CONST_CAST_TREE (t2));
4286 /* Worker for gimple_register_type.
4287 Register type T in the global type table gimple_types.
4288 When REGISTERING_MV is false first recurse for the main variant of T. */
4290 static tree
4291 gimple_register_type_1 (tree t, bool registering_mv)
4293 void **slot;
4294 gimple_type_leader_entry *leader;
4296 /* If we registered this type before return the cached result. */
4297 leader = &gimple_type_leader[TYPE_UID (t) % GIMPLE_TYPE_LEADER_SIZE];
4298 if (leader->type == t)
4299 return leader->leader;
4301 /* Always register the main variant first. This is important so we
4302 pick up the non-typedef variants as canonical, otherwise we'll end
4303 up taking typedef ids for structure tags during comparison.
4304 It also makes sure that main variants will be merged to main variants.
4305 As we are operating on a possibly partially fixed up type graph
4306 do not bother to recurse more than once, otherwise we may end up
4307 walking in circles.
4308 If we are registering a main variant it will either remain its
4309 own main variant or it will be merged to something else in which
4310 case we do not care for the main variant leader. */
4311 if (!registering_mv
4312 && TYPE_MAIN_VARIANT (t) != t)
4313 gimple_register_type_1 (TYPE_MAIN_VARIANT (t), true);
4315 /* See if we already have an equivalent type registered. */
4316 slot = htab_find_slot (gimple_types, t, INSERT);
4317 if (*slot
4318 && *(tree *)slot != t)
4320 tree new_type = (tree) *((tree *) slot);
4321 leader->type = t;
4322 leader->leader = new_type;
4323 return new_type;
4326 /* If not, insert it to the cache and the hash. */
4327 leader->type = t;
4328 leader->leader = t;
4329 *slot = (void *) t;
4330 return t;
4333 /* Register type T in the global type table gimple_types.
4334 If another type T', compatible with T, already existed in
4335 gimple_types then return T', otherwise return T. This is used by
4336 LTO to merge identical types read from different TUs. */
4338 tree
4339 gimple_register_type (tree t)
4341 gcc_assert (TYPE_P (t));
4343 if (!gimple_type_leader)
4344 gimple_type_leader = ggc_alloc_cleared_vec_gimple_type_leader_entry_s
4345 (GIMPLE_TYPE_LEADER_SIZE);
4347 if (gimple_types == NULL)
4348 gimple_types = htab_create_ggc (16381, gimple_type_hash, gimple_type_eq, 0);
4350 return gimple_register_type_1 (t, false);
4353 /* The TYPE_CANONICAL merging machinery. It should closely resemble
4354 the middle-end types_compatible_p function. It needs to avoid
4355 claiming types are different for types that should be treated
4356 the same with respect to TBAA. Canonical types are also used
4357 for IL consistency checks via the useless_type_conversion_p
4358 predicate which does not handle all type kinds itself but falls
4359 back to pointer-comparison of TYPE_CANONICAL for aggregates
4360 for example. */
4362 /* Return true iff T1 and T2 are structurally identical for what
4363 TBAA is concerned. */
4365 static bool
4366 gimple_canonical_types_compatible_p (tree t1, tree t2)
4368 /* Before starting to set up the SCC machinery handle simple cases. */
4370 /* Check first for the obvious case of pointer identity. */
4371 if (t1 == t2)
4372 return true;
4374 /* Check that we have two types to compare. */
4375 if (t1 == NULL_TREE || t2 == NULL_TREE)
4376 return false;
4378 /* If the types have been previously registered and found equal
4379 they still are. */
4380 if (TYPE_CANONICAL (t1)
4381 && TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2))
4382 return true;
4384 /* Can't be the same type if the types don't have the same code. */
4385 if (TREE_CODE (t1) != TREE_CODE (t2))
4386 return false;
4388 if (TREE_ADDRESSABLE (t1) != TREE_ADDRESSABLE (t2))
4389 return false;
4391 /* Qualifiers do not matter for canonical type comparison purposes. */
4393 /* Void types and nullptr types are always the same. */
4394 if (TREE_CODE (t1) == VOID_TYPE
4395 || TREE_CODE (t1) == NULLPTR_TYPE)
4396 return true;
4398 /* Can't be the same type if they have different alignment, or mode. */
4399 if (TYPE_ALIGN (t1) != TYPE_ALIGN (t2)
4400 || TYPE_MODE (t1) != TYPE_MODE (t2))
4401 return false;
4403 /* Non-aggregate types can be handled cheaply. */
4404 if (INTEGRAL_TYPE_P (t1)
4405 || SCALAR_FLOAT_TYPE_P (t1)
4406 || FIXED_POINT_TYPE_P (t1)
4407 || TREE_CODE (t1) == VECTOR_TYPE
4408 || TREE_CODE (t1) == COMPLEX_TYPE
4409 || TREE_CODE (t1) == OFFSET_TYPE
4410 || POINTER_TYPE_P (t1))
4412 /* Can't be the same type if they have different sign or precision. */
4413 if (TYPE_PRECISION (t1) != TYPE_PRECISION (t2)
4414 || TYPE_UNSIGNED (t1) != TYPE_UNSIGNED (t2))
4415 return false;
4417 if (TREE_CODE (t1) == INTEGER_TYPE
4418 && TYPE_STRING_FLAG (t1) != TYPE_STRING_FLAG (t2))
4419 return false;
4421 /* For canonical type comparisons we do not want to build SCCs
4422 so we cannot compare pointed-to types. But we can, for now,
4423 require the same pointed-to type kind and match what
4424 useless_type_conversion_p would do. */
4425 if (POINTER_TYPE_P (t1))
4427 /* If the two pointers have different ref-all attributes,
4428 they can't be the same type. */
4429 if (TYPE_REF_CAN_ALIAS_ALL (t1) != TYPE_REF_CAN_ALIAS_ALL (t2))
4430 return false;
4432 if (TYPE_ADDR_SPACE (TREE_TYPE (t1))
4433 != TYPE_ADDR_SPACE (TREE_TYPE (t2)))
4434 return false;
4436 if (TYPE_RESTRICT (t1) != TYPE_RESTRICT (t2))
4437 return false;
4439 if (TREE_CODE (TREE_TYPE (t1)) != TREE_CODE (TREE_TYPE (t2)))
4440 return false;
4443 /* Tail-recurse to components. */
4444 if (TREE_CODE (t1) == VECTOR_TYPE
4445 || TREE_CODE (t1) == COMPLEX_TYPE)
4446 return gimple_canonical_types_compatible_p (TREE_TYPE (t1),
4447 TREE_TYPE (t2));
4449 return true;
4452 /* Do type-specific comparisons. */
4453 switch (TREE_CODE (t1))
4455 case ARRAY_TYPE:
4456 /* Array types are the same if the element types are the same and
4457 the number of elements are the same. */
4458 if (!gimple_canonical_types_compatible_p (TREE_TYPE (t1), TREE_TYPE (t2))
4459 || TYPE_STRING_FLAG (t1) != TYPE_STRING_FLAG (t2)
4460 || TYPE_NONALIASED_COMPONENT (t1) != TYPE_NONALIASED_COMPONENT (t2))
4461 return false;
4462 else
4464 tree i1 = TYPE_DOMAIN (t1);
4465 tree i2 = TYPE_DOMAIN (t2);
4467 /* For an incomplete external array, the type domain can be
4468 NULL_TREE. Check this condition also. */
4469 if (i1 == NULL_TREE && i2 == NULL_TREE)
4470 return true;
4471 else if (i1 == NULL_TREE || i2 == NULL_TREE)
4472 return false;
4473 else
4475 tree min1 = TYPE_MIN_VALUE (i1);
4476 tree min2 = TYPE_MIN_VALUE (i2);
4477 tree max1 = TYPE_MAX_VALUE (i1);
4478 tree max2 = TYPE_MAX_VALUE (i2);
4480 /* The minimum/maximum values have to be the same. */
4481 if ((min1 == min2
4482 || (min1 && min2
4483 && ((TREE_CODE (min1) == PLACEHOLDER_EXPR
4484 && TREE_CODE (min2) == PLACEHOLDER_EXPR)
4485 || operand_equal_p (min1, min2, 0))))
4486 && (max1 == max2
4487 || (max1 && max2
4488 && ((TREE_CODE (max1) == PLACEHOLDER_EXPR
4489 && TREE_CODE (max2) == PLACEHOLDER_EXPR)
4490 || operand_equal_p (max1, max2, 0)))))
4491 return true;
4492 else
4493 return false;
4497 case METHOD_TYPE:
4498 case FUNCTION_TYPE:
4499 /* Function types are the same if the return type and arguments types
4500 are the same. */
4501 if (!gimple_canonical_types_compatible_p (TREE_TYPE (t1), TREE_TYPE (t2)))
4502 return false;
4504 if (!comp_type_attributes (t1, t2))
4505 return false;
4507 if (TYPE_ARG_TYPES (t1) == TYPE_ARG_TYPES (t2))
4508 return true;
4509 else
4511 tree parms1, parms2;
4513 for (parms1 = TYPE_ARG_TYPES (t1), parms2 = TYPE_ARG_TYPES (t2);
4514 parms1 && parms2;
4515 parms1 = TREE_CHAIN (parms1), parms2 = TREE_CHAIN (parms2))
4517 if (!gimple_canonical_types_compatible_p
4518 (TREE_VALUE (parms1), TREE_VALUE (parms2)))
4519 return false;
4522 if (parms1 || parms2)
4523 return false;
4525 return true;
4528 case RECORD_TYPE:
4529 case UNION_TYPE:
4530 case QUAL_UNION_TYPE:
4532 tree f1, f2;
4534 /* For aggregate types, all the fields must be the same. */
4535 for (f1 = TYPE_FIELDS (t1), f2 = TYPE_FIELDS (t2);
4536 f1 || f2;
4537 f1 = TREE_CHAIN (f1), f2 = TREE_CHAIN (f2))
4539 /* Skip non-fields. */
4540 while (f1 && TREE_CODE (f1) != FIELD_DECL)
4541 f1 = TREE_CHAIN (f1);
4542 while (f2 && TREE_CODE (f2) != FIELD_DECL)
4543 f2 = TREE_CHAIN (f2);
4544 if (!f1 || !f2)
4545 break;
4546 /* The fields must have the same name, offset and type. */
4547 if (DECL_NONADDRESSABLE_P (f1) != DECL_NONADDRESSABLE_P (f2)
4548 || !gimple_compare_field_offset (f1, f2)
4549 || !gimple_canonical_types_compatible_p
4550 (TREE_TYPE (f1), TREE_TYPE (f2)))
4551 return false;
4554 /* If one aggregate has more fields than the other, they
4555 are not the same. */
4556 if (f1 || f2)
4557 return false;
4559 return true;
4562 default:
4563 gcc_unreachable ();
4568 /* Returns nonzero if P1 and P2 are equal. */
4570 static int
4571 gimple_canonical_type_eq (const void *p1, const void *p2)
4573 const_tree t1 = (const_tree) p1;
4574 const_tree t2 = (const_tree) p2;
4575 return gimple_canonical_types_compatible_p (CONST_CAST_TREE (t1),
4576 CONST_CAST_TREE (t2));
4579 /* Register type T in the global type table gimple_types.
4580 If another type T', compatible with T, already existed in
4581 gimple_types then return T', otherwise return T. This is used by
4582 LTO to merge identical types read from different TUs.
4584 ??? This merging does not exactly match how the tree.c middle-end
4585 functions will assign TYPE_CANONICAL when new types are created
4586 during optimization (which at least happens for pointer and array
4587 types). */
4589 tree
4590 gimple_register_canonical_type (tree t)
4592 void **slot;
4594 gcc_assert (TYPE_P (t));
4596 if (TYPE_CANONICAL (t))
4597 return TYPE_CANONICAL (t);
4599 if (gimple_canonical_types == NULL)
4600 gimple_canonical_types = htab_create_ggc (16381, gimple_canonical_type_hash,
4601 gimple_canonical_type_eq, 0);
4603 slot = htab_find_slot (gimple_canonical_types, t, INSERT);
4604 if (*slot
4605 && *(tree *)slot != t)
4607 tree new_type = (tree) *((tree *) slot);
4609 TYPE_CANONICAL (t) = new_type;
4610 t = new_type;
4612 else
4614 TYPE_CANONICAL (t) = t;
4615 *slot = (void *) t;
4618 return t;
4622 /* Show statistics on references to the global type table gimple_types. */
4624 void
4625 print_gimple_types_stats (void)
4627 if (gimple_types)
4628 fprintf (stderr, "GIMPLE type table: size %ld, %ld elements, "
4629 "%ld searches, %ld collisions (ratio: %f)\n",
4630 (long) htab_size (gimple_types),
4631 (long) htab_elements (gimple_types),
4632 (long) gimple_types->searches,
4633 (long) gimple_types->collisions,
4634 htab_collisions (gimple_types));
4635 else
4636 fprintf (stderr, "GIMPLE type table is empty\n");
4637 if (type_hash_cache)
4638 fprintf (stderr, "GIMPLE type hash table: size %ld, %ld elements, "
4639 "%ld searches, %ld collisions (ratio: %f)\n",
4640 (long) htab_size (type_hash_cache),
4641 (long) htab_elements (type_hash_cache),
4642 (long) type_hash_cache->searches,
4643 (long) type_hash_cache->collisions,
4644 htab_collisions (type_hash_cache));
4645 else
4646 fprintf (stderr, "GIMPLE type hash table is empty\n");
4647 if (gimple_canonical_types)
4648 fprintf (stderr, "GIMPLE canonical type table: size %ld, %ld elements, "
4649 "%ld searches, %ld collisions (ratio: %f)\n",
4650 (long) htab_size (gimple_canonical_types),
4651 (long) htab_elements (gimple_canonical_types),
4652 (long) gimple_canonical_types->searches,
4653 (long) gimple_canonical_types->collisions,
4654 htab_collisions (gimple_canonical_types));
4655 else
4656 fprintf (stderr, "GIMPLE canonical type table is empty\n");
4657 if (canonical_type_hash_cache)
4658 fprintf (stderr, "GIMPLE canonical type hash table: size %ld, %ld elements, "
4659 "%ld searches, %ld collisions (ratio: %f)\n",
4660 (long) htab_size (canonical_type_hash_cache),
4661 (long) htab_elements (canonical_type_hash_cache),
4662 (long) canonical_type_hash_cache->searches,
4663 (long) canonical_type_hash_cache->collisions,
4664 htab_collisions (canonical_type_hash_cache));
4665 else
4666 fprintf (stderr, "GIMPLE canonical type hash table is empty\n");
4669 /* Free the gimple type hashtables used for LTO type merging. */
4671 void
4672 free_gimple_type_tables (void)
4674 /* Last chance to print stats for the tables. */
4675 if (flag_lto_report)
4676 print_gimple_types_stats ();
4678 if (gimple_types)
4680 htab_delete (gimple_types);
4681 gimple_types = NULL;
4683 if (gimple_canonical_types)
4685 htab_delete (gimple_canonical_types);
4686 gimple_canonical_types = NULL;
4688 if (type_hash_cache)
4690 htab_delete (type_hash_cache);
4691 type_hash_cache = NULL;
4693 if (canonical_type_hash_cache)
4695 htab_delete (canonical_type_hash_cache);
4696 canonical_type_hash_cache = NULL;
4698 if (type_pair_cache)
4700 free (type_pair_cache);
4701 type_pair_cache = NULL;
4703 gimple_type_leader = NULL;
4707 /* Return a type the same as TYPE except unsigned or
4708 signed according to UNSIGNEDP. */
4710 static tree
4711 gimple_signed_or_unsigned_type (bool unsignedp, tree type)
4713 tree type1;
4715 type1 = TYPE_MAIN_VARIANT (type);
4716 if (type1 == signed_char_type_node
4717 || type1 == char_type_node
4718 || type1 == unsigned_char_type_node)
4719 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
4720 if (type1 == integer_type_node || type1 == unsigned_type_node)
4721 return unsignedp ? unsigned_type_node : integer_type_node;
4722 if (type1 == short_integer_type_node || type1 == short_unsigned_type_node)
4723 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
4724 if (type1 == long_integer_type_node || type1 == long_unsigned_type_node)
4725 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
4726 if (type1 == long_long_integer_type_node
4727 || type1 == long_long_unsigned_type_node)
4728 return unsignedp
4729 ? long_long_unsigned_type_node
4730 : long_long_integer_type_node;
4731 if (int128_integer_type_node && (type1 == int128_integer_type_node || type1 == int128_unsigned_type_node))
4732 return unsignedp
4733 ? int128_unsigned_type_node
4734 : int128_integer_type_node;
4735 #if HOST_BITS_PER_WIDE_INT >= 64
4736 if (type1 == intTI_type_node || type1 == unsigned_intTI_type_node)
4737 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
4738 #endif
4739 if (type1 == intDI_type_node || type1 == unsigned_intDI_type_node)
4740 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
4741 if (type1 == intSI_type_node || type1 == unsigned_intSI_type_node)
4742 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
4743 if (type1 == intHI_type_node || type1 == unsigned_intHI_type_node)
4744 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
4745 if (type1 == intQI_type_node || type1 == unsigned_intQI_type_node)
4746 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
4748 #define GIMPLE_FIXED_TYPES(NAME) \
4749 if (type1 == short_ ## NAME ## _type_node \
4750 || type1 == unsigned_short_ ## NAME ## _type_node) \
4751 return unsignedp ? unsigned_short_ ## NAME ## _type_node \
4752 : short_ ## NAME ## _type_node; \
4753 if (type1 == NAME ## _type_node \
4754 || type1 == unsigned_ ## NAME ## _type_node) \
4755 return unsignedp ? unsigned_ ## NAME ## _type_node \
4756 : NAME ## _type_node; \
4757 if (type1 == long_ ## NAME ## _type_node \
4758 || type1 == unsigned_long_ ## NAME ## _type_node) \
4759 return unsignedp ? unsigned_long_ ## NAME ## _type_node \
4760 : long_ ## NAME ## _type_node; \
4761 if (type1 == long_long_ ## NAME ## _type_node \
4762 || type1 == unsigned_long_long_ ## NAME ## _type_node) \
4763 return unsignedp ? unsigned_long_long_ ## NAME ## _type_node \
4764 : long_long_ ## NAME ## _type_node;
4766 #define GIMPLE_FIXED_MODE_TYPES(NAME) \
4767 if (type1 == NAME ## _type_node \
4768 || type1 == u ## NAME ## _type_node) \
4769 return unsignedp ? u ## NAME ## _type_node \
4770 : NAME ## _type_node;
4772 #define GIMPLE_FIXED_TYPES_SAT(NAME) \
4773 if (type1 == sat_ ## short_ ## NAME ## _type_node \
4774 || type1 == sat_ ## unsigned_short_ ## NAME ## _type_node) \
4775 return unsignedp ? sat_ ## unsigned_short_ ## NAME ## _type_node \
4776 : sat_ ## short_ ## NAME ## _type_node; \
4777 if (type1 == sat_ ## NAME ## _type_node \
4778 || type1 == sat_ ## unsigned_ ## NAME ## _type_node) \
4779 return unsignedp ? sat_ ## unsigned_ ## NAME ## _type_node \
4780 : sat_ ## NAME ## _type_node; \
4781 if (type1 == sat_ ## long_ ## NAME ## _type_node \
4782 || type1 == sat_ ## unsigned_long_ ## NAME ## _type_node) \
4783 return unsignedp ? sat_ ## unsigned_long_ ## NAME ## _type_node \
4784 : sat_ ## long_ ## NAME ## _type_node; \
4785 if (type1 == sat_ ## long_long_ ## NAME ## _type_node \
4786 || type1 == sat_ ## unsigned_long_long_ ## NAME ## _type_node) \
4787 return unsignedp ? sat_ ## unsigned_long_long_ ## NAME ## _type_node \
4788 : sat_ ## long_long_ ## NAME ## _type_node;
4790 #define GIMPLE_FIXED_MODE_TYPES_SAT(NAME) \
4791 if (type1 == sat_ ## NAME ## _type_node \
4792 || type1 == sat_ ## u ## NAME ## _type_node) \
4793 return unsignedp ? sat_ ## u ## NAME ## _type_node \
4794 : sat_ ## NAME ## _type_node;
4796 GIMPLE_FIXED_TYPES (fract);
4797 GIMPLE_FIXED_TYPES_SAT (fract);
4798 GIMPLE_FIXED_TYPES (accum);
4799 GIMPLE_FIXED_TYPES_SAT (accum);
4801 GIMPLE_FIXED_MODE_TYPES (qq);
4802 GIMPLE_FIXED_MODE_TYPES (hq);
4803 GIMPLE_FIXED_MODE_TYPES (sq);
4804 GIMPLE_FIXED_MODE_TYPES (dq);
4805 GIMPLE_FIXED_MODE_TYPES (tq);
4806 GIMPLE_FIXED_MODE_TYPES_SAT (qq);
4807 GIMPLE_FIXED_MODE_TYPES_SAT (hq);
4808 GIMPLE_FIXED_MODE_TYPES_SAT (sq);
4809 GIMPLE_FIXED_MODE_TYPES_SAT (dq);
4810 GIMPLE_FIXED_MODE_TYPES_SAT (tq);
4811 GIMPLE_FIXED_MODE_TYPES (ha);
4812 GIMPLE_FIXED_MODE_TYPES (sa);
4813 GIMPLE_FIXED_MODE_TYPES (da);
4814 GIMPLE_FIXED_MODE_TYPES (ta);
4815 GIMPLE_FIXED_MODE_TYPES_SAT (ha);
4816 GIMPLE_FIXED_MODE_TYPES_SAT (sa);
4817 GIMPLE_FIXED_MODE_TYPES_SAT (da);
4818 GIMPLE_FIXED_MODE_TYPES_SAT (ta);
4820 /* For ENUMERAL_TYPEs in C++, must check the mode of the types, not
4821 the precision; they have precision set to match their range, but
4822 may use a wider mode to match an ABI. If we change modes, we may
4823 wind up with bad conversions. For INTEGER_TYPEs in C, must check
4824 the precision as well, so as to yield correct results for
4825 bit-field types. C++ does not have these separate bit-field
4826 types, and producing a signed or unsigned variant of an
4827 ENUMERAL_TYPE may cause other problems as well. */
4828 if (!INTEGRAL_TYPE_P (type)
4829 || TYPE_UNSIGNED (type) == unsignedp)
4830 return type;
4832 #define TYPE_OK(node) \
4833 (TYPE_MODE (type) == TYPE_MODE (node) \
4834 && TYPE_PRECISION (type) == TYPE_PRECISION (node))
4835 if (TYPE_OK (signed_char_type_node))
4836 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
4837 if (TYPE_OK (integer_type_node))
4838 return unsignedp ? unsigned_type_node : integer_type_node;
4839 if (TYPE_OK (short_integer_type_node))
4840 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
4841 if (TYPE_OK (long_integer_type_node))
4842 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
4843 if (TYPE_OK (long_long_integer_type_node))
4844 return (unsignedp
4845 ? long_long_unsigned_type_node
4846 : long_long_integer_type_node);
4847 if (int128_integer_type_node && TYPE_OK (int128_integer_type_node))
4848 return (unsignedp
4849 ? int128_unsigned_type_node
4850 : int128_integer_type_node);
4852 #if HOST_BITS_PER_WIDE_INT >= 64
4853 if (TYPE_OK (intTI_type_node))
4854 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
4855 #endif
4856 if (TYPE_OK (intDI_type_node))
4857 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
4858 if (TYPE_OK (intSI_type_node))
4859 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
4860 if (TYPE_OK (intHI_type_node))
4861 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
4862 if (TYPE_OK (intQI_type_node))
4863 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
4865 #undef GIMPLE_FIXED_TYPES
4866 #undef GIMPLE_FIXED_MODE_TYPES
4867 #undef GIMPLE_FIXED_TYPES_SAT
4868 #undef GIMPLE_FIXED_MODE_TYPES_SAT
4869 #undef TYPE_OK
4871 return build_nonstandard_integer_type (TYPE_PRECISION (type), unsignedp);
4875 /* Return an unsigned type the same as TYPE in other respects. */
4877 tree
4878 gimple_unsigned_type (tree type)
4880 return gimple_signed_or_unsigned_type (true, type);
4884 /* Return a signed type the same as TYPE in other respects. */
4886 tree
4887 gimple_signed_type (tree type)
4889 return gimple_signed_or_unsigned_type (false, type);
4893 /* Return the typed-based alias set for T, which may be an expression
4894 or a type. Return -1 if we don't do anything special. */
4896 alias_set_type
4897 gimple_get_alias_set (tree t)
4899 tree u;
4901 /* Permit type-punning when accessing a union, provided the access
4902 is directly through the union. For example, this code does not
4903 permit taking the address of a union member and then storing
4904 through it. Even the type-punning allowed here is a GCC
4905 extension, albeit a common and useful one; the C standard says
4906 that such accesses have implementation-defined behavior. */
4907 for (u = t;
4908 TREE_CODE (u) == COMPONENT_REF || TREE_CODE (u) == ARRAY_REF;
4909 u = TREE_OPERAND (u, 0))
4910 if (TREE_CODE (u) == COMPONENT_REF
4911 && TREE_CODE (TREE_TYPE (TREE_OPERAND (u, 0))) == UNION_TYPE)
4912 return 0;
4914 /* That's all the expressions we handle specially. */
4915 if (!TYPE_P (t))
4916 return -1;
4918 /* For convenience, follow the C standard when dealing with
4919 character types. Any object may be accessed via an lvalue that
4920 has character type. */
4921 if (t == char_type_node
4922 || t == signed_char_type_node
4923 || t == unsigned_char_type_node)
4924 return 0;
4926 /* Allow aliasing between signed and unsigned variants of the same
4927 type. We treat the signed variant as canonical. */
4928 if (TREE_CODE (t) == INTEGER_TYPE && TYPE_UNSIGNED (t))
4930 tree t1 = gimple_signed_type (t);
4932 /* t1 == t can happen for boolean nodes which are always unsigned. */
4933 if (t1 != t)
4934 return get_alias_set (t1);
4937 return -1;
4941 /* Data structure used to count the number of dereferences to PTR
4942 inside an expression. */
4943 struct count_ptr_d
4945 tree ptr;
4946 unsigned num_stores;
4947 unsigned num_loads;
4950 /* Helper for count_uses_and_derefs. Called by walk_tree to look for
4951 (ALIGN/MISALIGNED_)INDIRECT_REF nodes for the pointer passed in DATA. */
4953 static tree
4954 count_ptr_derefs (tree *tp, int *walk_subtrees, void *data)
4956 struct walk_stmt_info *wi_p = (struct walk_stmt_info *) data;
4957 struct count_ptr_d *count_p = (struct count_ptr_d *) wi_p->info;
4959 /* Do not walk inside ADDR_EXPR nodes. In the expression &ptr->fld,
4960 pointer 'ptr' is *not* dereferenced, it is simply used to compute
4961 the address of 'fld' as 'ptr + offsetof(fld)'. */
4962 if (TREE_CODE (*tp) == ADDR_EXPR)
4964 *walk_subtrees = 0;
4965 return NULL_TREE;
4968 if (TREE_CODE (*tp) == MEM_REF && TREE_OPERAND (*tp, 0) == count_p->ptr)
4970 if (wi_p->is_lhs)
4971 count_p->num_stores++;
4972 else
4973 count_p->num_loads++;
4976 return NULL_TREE;
4979 /* Count the number of direct and indirect uses for pointer PTR in
4980 statement STMT. The number of direct uses is stored in
4981 *NUM_USES_P. Indirect references are counted separately depending
4982 on whether they are store or load operations. The counts are
4983 stored in *NUM_STORES_P and *NUM_LOADS_P. */
4985 void
4986 count_uses_and_derefs (tree ptr, gimple stmt, unsigned *num_uses_p,
4987 unsigned *num_loads_p, unsigned *num_stores_p)
4989 ssa_op_iter i;
4990 tree use;
4992 *num_uses_p = 0;
4993 *num_loads_p = 0;
4994 *num_stores_p = 0;
4996 /* Find out the total number of uses of PTR in STMT. */
4997 FOR_EACH_SSA_TREE_OPERAND (use, stmt, i, SSA_OP_USE)
4998 if (use == ptr)
4999 (*num_uses_p)++;
5001 /* Now count the number of indirect references to PTR. This is
5002 truly awful, but we don't have much choice. There are no parent
5003 pointers inside INDIRECT_REFs, so an expression like
5004 '*x_1 = foo (x_1, *x_1)' needs to be traversed piece by piece to
5005 find all the indirect and direct uses of x_1 inside. The only
5006 shortcut we can take is the fact that GIMPLE only allows
5007 INDIRECT_REFs inside the expressions below. */
5008 if (is_gimple_assign (stmt)
5009 || gimple_code (stmt) == GIMPLE_RETURN
5010 || gimple_code (stmt) == GIMPLE_ASM
5011 || is_gimple_call (stmt))
5013 struct walk_stmt_info wi;
5014 struct count_ptr_d count;
5016 count.ptr = ptr;
5017 count.num_stores = 0;
5018 count.num_loads = 0;
5020 memset (&wi, 0, sizeof (wi));
5021 wi.info = &count;
5022 walk_gimple_op (stmt, count_ptr_derefs, &wi);
5024 *num_stores_p = count.num_stores;
5025 *num_loads_p = count.num_loads;
5028 gcc_assert (*num_uses_p >= *num_loads_p + *num_stores_p);
5031 /* From a tree operand OP return the base of a load or store operation
5032 or NULL_TREE if OP is not a load or a store. */
5034 static tree
5035 get_base_loadstore (tree op)
5037 while (handled_component_p (op))
5038 op = TREE_OPERAND (op, 0);
5039 if (DECL_P (op)
5040 || INDIRECT_REF_P (op)
5041 || TREE_CODE (op) == MEM_REF
5042 || TREE_CODE (op) == TARGET_MEM_REF)
5043 return op;
5044 return NULL_TREE;
5047 /* For the statement STMT call the callbacks VISIT_LOAD, VISIT_STORE and
5048 VISIT_ADDR if non-NULL on loads, store and address-taken operands
5049 passing the STMT, the base of the operand and DATA to it. The base
5050 will be either a decl, an indirect reference (including TARGET_MEM_REF)
5051 or the argument of an address expression.
5052 Returns the results of these callbacks or'ed. */
5054 bool
5055 walk_stmt_load_store_addr_ops (gimple stmt, void *data,
5056 bool (*visit_load)(gimple, tree, void *),
5057 bool (*visit_store)(gimple, tree, void *),
5058 bool (*visit_addr)(gimple, tree, void *))
5060 bool ret = false;
5061 unsigned i;
5062 if (gimple_assign_single_p (stmt))
5064 tree lhs, rhs;
5065 if (visit_store)
5067 lhs = get_base_loadstore (gimple_assign_lhs (stmt));
5068 if (lhs)
5069 ret |= visit_store (stmt, lhs, data);
5071 rhs = gimple_assign_rhs1 (stmt);
5072 while (handled_component_p (rhs))
5073 rhs = TREE_OPERAND (rhs, 0);
5074 if (visit_addr)
5076 if (TREE_CODE (rhs) == ADDR_EXPR)
5077 ret |= visit_addr (stmt, TREE_OPERAND (rhs, 0), data);
5078 else if (TREE_CODE (rhs) == TARGET_MEM_REF
5079 && TREE_CODE (TMR_BASE (rhs)) == ADDR_EXPR)
5080 ret |= visit_addr (stmt, TREE_OPERAND (TMR_BASE (rhs), 0), data);
5081 else if (TREE_CODE (rhs) == OBJ_TYPE_REF
5082 && TREE_CODE (OBJ_TYPE_REF_OBJECT (rhs)) == ADDR_EXPR)
5083 ret |= visit_addr (stmt, TREE_OPERAND (OBJ_TYPE_REF_OBJECT (rhs),
5084 0), data);
5085 else if (TREE_CODE (rhs) == CONSTRUCTOR)
5087 unsigned int ix;
5088 tree val;
5090 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (rhs), ix, val)
5091 if (TREE_CODE (val) == ADDR_EXPR)
5092 ret |= visit_addr (stmt, TREE_OPERAND (val, 0), data);
5093 else if (TREE_CODE (val) == OBJ_TYPE_REF
5094 && TREE_CODE (OBJ_TYPE_REF_OBJECT (val)) == ADDR_EXPR)
5095 ret |= visit_addr (stmt,
5096 TREE_OPERAND (OBJ_TYPE_REF_OBJECT (val),
5097 0), data);
5099 lhs = gimple_assign_lhs (stmt);
5100 if (TREE_CODE (lhs) == TARGET_MEM_REF
5101 && TREE_CODE (TMR_BASE (lhs)) == ADDR_EXPR)
5102 ret |= visit_addr (stmt, TREE_OPERAND (TMR_BASE (lhs), 0), data);
5104 if (visit_load)
5106 rhs = get_base_loadstore (rhs);
5107 if (rhs)
5108 ret |= visit_load (stmt, rhs, data);
5111 else if (visit_addr
5112 && (is_gimple_assign (stmt)
5113 || gimple_code (stmt) == GIMPLE_COND))
5115 for (i = 0; i < gimple_num_ops (stmt); ++i)
5117 tree op = gimple_op (stmt, i);
5118 if (op == NULL_TREE)
5120 else if (TREE_CODE (op) == ADDR_EXPR)
5121 ret |= visit_addr (stmt, TREE_OPERAND (op, 0), data);
5122 /* COND_EXPR and VCOND_EXPR rhs1 argument is a comparison
5123 tree with two operands. */
5124 else if (i == 1 && COMPARISON_CLASS_P (op))
5126 if (TREE_CODE (TREE_OPERAND (op, 0)) == ADDR_EXPR)
5127 ret |= visit_addr (stmt, TREE_OPERAND (TREE_OPERAND (op, 0),
5128 0), data);
5129 if (TREE_CODE (TREE_OPERAND (op, 1)) == ADDR_EXPR)
5130 ret |= visit_addr (stmt, TREE_OPERAND (TREE_OPERAND (op, 1),
5131 0), data);
5135 else if (is_gimple_call (stmt))
5137 if (visit_store)
5139 tree lhs = gimple_call_lhs (stmt);
5140 if (lhs)
5142 lhs = get_base_loadstore (lhs);
5143 if (lhs)
5144 ret |= visit_store (stmt, lhs, data);
5147 if (visit_load || visit_addr)
5148 for (i = 0; i < gimple_call_num_args (stmt); ++i)
5150 tree rhs = gimple_call_arg (stmt, i);
5151 if (visit_addr
5152 && TREE_CODE (rhs) == ADDR_EXPR)
5153 ret |= visit_addr (stmt, TREE_OPERAND (rhs, 0), data);
5154 else if (visit_load)
5156 rhs = get_base_loadstore (rhs);
5157 if (rhs)
5158 ret |= visit_load (stmt, rhs, data);
5161 if (visit_addr
5162 && gimple_call_chain (stmt)
5163 && TREE_CODE (gimple_call_chain (stmt)) == ADDR_EXPR)
5164 ret |= visit_addr (stmt, TREE_OPERAND (gimple_call_chain (stmt), 0),
5165 data);
5166 if (visit_addr
5167 && gimple_call_return_slot_opt_p (stmt)
5168 && gimple_call_lhs (stmt) != NULL_TREE
5169 && TREE_ADDRESSABLE (TREE_TYPE (gimple_call_lhs (stmt))))
5170 ret |= visit_addr (stmt, gimple_call_lhs (stmt), data);
5172 else if (gimple_code (stmt) == GIMPLE_ASM)
5174 unsigned noutputs;
5175 const char *constraint;
5176 const char **oconstraints;
5177 bool allows_mem, allows_reg, is_inout;
5178 noutputs = gimple_asm_noutputs (stmt);
5179 oconstraints = XALLOCAVEC (const char *, noutputs);
5180 if (visit_store || visit_addr)
5181 for (i = 0; i < gimple_asm_noutputs (stmt); ++i)
5183 tree link = gimple_asm_output_op (stmt, i);
5184 tree op = get_base_loadstore (TREE_VALUE (link));
5185 if (op && visit_store)
5186 ret |= visit_store (stmt, op, data);
5187 if (visit_addr)
5189 constraint = TREE_STRING_POINTER
5190 (TREE_VALUE (TREE_PURPOSE (link)));
5191 oconstraints[i] = constraint;
5192 parse_output_constraint (&constraint, i, 0, 0, &allows_mem,
5193 &allows_reg, &is_inout);
5194 if (op && !allows_reg && allows_mem)
5195 ret |= visit_addr (stmt, op, data);
5198 if (visit_load || visit_addr)
5199 for (i = 0; i < gimple_asm_ninputs (stmt); ++i)
5201 tree link = gimple_asm_input_op (stmt, i);
5202 tree op = TREE_VALUE (link);
5203 if (visit_addr
5204 && TREE_CODE (op) == ADDR_EXPR)
5205 ret |= visit_addr (stmt, TREE_OPERAND (op, 0), data);
5206 else if (visit_load || visit_addr)
5208 op = get_base_loadstore (op);
5209 if (op)
5211 if (visit_load)
5212 ret |= visit_load (stmt, op, data);
5213 if (visit_addr)
5215 constraint = TREE_STRING_POINTER
5216 (TREE_VALUE (TREE_PURPOSE (link)));
5217 parse_input_constraint (&constraint, 0, 0, noutputs,
5218 0, oconstraints,
5219 &allows_mem, &allows_reg);
5220 if (!allows_reg && allows_mem)
5221 ret |= visit_addr (stmt, op, data);
5227 else if (gimple_code (stmt) == GIMPLE_RETURN)
5229 tree op = gimple_return_retval (stmt);
5230 if (op)
5232 if (visit_addr
5233 && TREE_CODE (op) == ADDR_EXPR)
5234 ret |= visit_addr (stmt, TREE_OPERAND (op, 0), data);
5235 else if (visit_load)
5237 op = get_base_loadstore (op);
5238 if (op)
5239 ret |= visit_load (stmt, op, data);
5243 else if (visit_addr
5244 && gimple_code (stmt) == GIMPLE_PHI)
5246 for (i = 0; i < gimple_phi_num_args (stmt); ++i)
5248 tree op = PHI_ARG_DEF (stmt, i);
5249 if (TREE_CODE (op) == ADDR_EXPR)
5250 ret |= visit_addr (stmt, TREE_OPERAND (op, 0), data);
5254 return ret;
5257 /* Like walk_stmt_load_store_addr_ops but with NULL visit_addr. IPA-CP
5258 should make a faster clone for this case. */
5260 bool
5261 walk_stmt_load_store_ops (gimple stmt, void *data,
5262 bool (*visit_load)(gimple, tree, void *),
5263 bool (*visit_store)(gimple, tree, void *))
5265 return walk_stmt_load_store_addr_ops (stmt, data,
5266 visit_load, visit_store, NULL);
5269 /* Helper for gimple_ior_addresses_taken_1. */
5271 static bool
5272 gimple_ior_addresses_taken_1 (gimple stmt ATTRIBUTE_UNUSED,
5273 tree addr, void *data)
5275 bitmap addresses_taken = (bitmap)data;
5276 addr = get_base_address (addr);
5277 if (addr
5278 && DECL_P (addr))
5280 bitmap_set_bit (addresses_taken, DECL_UID (addr));
5281 return true;
5283 return false;
5286 /* Set the bit for the uid of all decls that have their address taken
5287 in STMT in the ADDRESSES_TAKEN bitmap. Returns true if there
5288 were any in this stmt. */
5290 bool
5291 gimple_ior_addresses_taken (bitmap addresses_taken, gimple stmt)
5293 return walk_stmt_load_store_addr_ops (stmt, addresses_taken, NULL, NULL,
5294 gimple_ior_addresses_taken_1);
5298 /* Return a printable name for symbol DECL. */
5300 const char *
5301 gimple_decl_printable_name (tree decl, int verbosity)
5303 if (!DECL_NAME (decl))
5304 return NULL;
5306 if (DECL_ASSEMBLER_NAME_SET_P (decl))
5308 const char *str, *mangled_str;
5309 int dmgl_opts = DMGL_NO_OPTS;
5311 if (verbosity >= 2)
5313 dmgl_opts = DMGL_VERBOSE
5314 | DMGL_ANSI
5315 | DMGL_GNU_V3
5316 | DMGL_RET_POSTFIX;
5317 if (TREE_CODE (decl) == FUNCTION_DECL)
5318 dmgl_opts |= DMGL_PARAMS;
5321 mangled_str = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
5322 str = cplus_demangle_v3 (mangled_str, dmgl_opts);
5323 return (str) ? str : mangled_str;
5326 return IDENTIFIER_POINTER (DECL_NAME (decl));
5329 /* Return true when STMT is builtins call to CODE. */
5331 bool
5332 gimple_call_builtin_p (gimple stmt, enum built_in_function code)
5334 tree fndecl;
5335 return (is_gimple_call (stmt)
5336 && (fndecl = gimple_call_fndecl (stmt)) != NULL
5337 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
5338 && DECL_FUNCTION_CODE (fndecl) == code);
5341 /* Return true if STMT clobbers memory. STMT is required to be a
5342 GIMPLE_ASM. */
5344 bool
5345 gimple_asm_clobbers_memory_p (const_gimple stmt)
5347 unsigned i;
5349 for (i = 0; i < gimple_asm_nclobbers (stmt); i++)
5351 tree op = gimple_asm_clobber_op (stmt, i);
5352 if (strcmp (TREE_STRING_POINTER (TREE_VALUE (op)), "memory") == 0)
5353 return true;
5356 return false;
5358 #include "gt-gimple.h"