re PR tree-optimization/49018 (Inline assembly block executed outside conditional...
[gcc.git] / gcc / gimple.c
1 /* Gimple IR support functions.
2
3 Copyright 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
4 Contributed by Aldy Hernandez <aldyh@redhat.com>
5
6 This file is part of GCC.
7
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.
12
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.
17
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/>. */
21
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"
39
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;
52
53 /* Global type comparison cache. This is by TYPE_UID for space efficiency
54 and thus cannot use and does not need GC. */
55 static htab_t gtc_visited;
56 static struct obstack gtc_ob;
57
58 /* All the tuples have their operand vector (if present) at the very bottom
59 of the structure. Therefore, the offset required to find the
60 operands vector the size of the structure minus the size of the 1
61 element tree array at the end (see gimple_ops). */
62 #define DEFGSSTRUCT(SYM, STRUCT, HAS_TREE_OP) \
63 (HAS_TREE_OP ? sizeof (struct STRUCT) - sizeof (tree) : 0),
64 EXPORTED_CONST size_t gimple_ops_offset_[] = {
65 #include "gsstruct.def"
66 };
67 #undef DEFGSSTRUCT
68
69 #define DEFGSSTRUCT(SYM, STRUCT, HAS_TREE_OP) sizeof(struct STRUCT),
70 static const size_t gsstruct_code_size[] = {
71 #include "gsstruct.def"
72 };
73 #undef DEFGSSTRUCT
74
75 #define DEFGSCODE(SYM, NAME, GSSCODE) NAME,
76 const char *const gimple_code_name[] = {
77 #include "gimple.def"
78 };
79 #undef DEFGSCODE
80
81 #define DEFGSCODE(SYM, NAME, GSSCODE) GSSCODE,
82 EXPORTED_CONST enum gimple_statement_structure_enum gss_for_code_[] = {
83 #include "gimple.def"
84 };
85 #undef DEFGSCODE
86
87 #ifdef GATHER_STATISTICS
88 /* Gimple stats. */
89
90 int gimple_alloc_counts[(int) gimple_alloc_kind_all];
91 int gimple_alloc_sizes[(int) gimple_alloc_kind_all];
92
93 /* Keep in sync with gimple.h:enum gimple_alloc_kind. */
94 static const char * const gimple_alloc_kind_names[] = {
95 "assignments",
96 "phi nodes",
97 "conditionals",
98 "sequences",
99 "everything else"
100 };
101
102 #endif /* GATHER_STATISTICS */
103
104 /* A cache of gimple_seq objects. Sequences are created and destroyed
105 fairly often during gimplification. */
106 static GTY ((deletable)) struct gimple_seq_d *gimple_seq_cache;
107
108 /* Private API manipulation functions shared only with some
109 other files. */
110 extern void gimple_set_stored_syms (gimple, bitmap, bitmap_obstack *);
111 extern void gimple_set_loaded_syms (gimple, bitmap, bitmap_obstack *);
112
113 /* Gimple tuple constructors.
114 Note: Any constructor taking a ``gimple_seq'' as a parameter, can
115 be passed a NULL to start with an empty sequence. */
116
117 /* Set the code for statement G to CODE. */
118
119 static inline void
120 gimple_set_code (gimple g, enum gimple_code code)
121 {
122 g->gsbase.code = code;
123 }
124
125 /* Return the number of bytes needed to hold a GIMPLE statement with
126 code CODE. */
127
128 static inline size_t
129 gimple_size (enum gimple_code code)
130 {
131 return gsstruct_code_size[gss_for_code (code)];
132 }
133
134 /* Allocate memory for a GIMPLE statement with code CODE and NUM_OPS
135 operands. */
136
137 gimple
138 gimple_alloc_stat (enum gimple_code code, unsigned num_ops MEM_STAT_DECL)
139 {
140 size_t size;
141 gimple stmt;
142
143 size = gimple_size (code);
144 if (num_ops > 0)
145 size += sizeof (tree) * (num_ops - 1);
146
147 #ifdef GATHER_STATISTICS
148 {
149 enum gimple_alloc_kind kind = gimple_alloc_kind (code);
150 gimple_alloc_counts[(int) kind]++;
151 gimple_alloc_sizes[(int) kind] += size;
152 }
153 #endif
154
155 stmt = ggc_alloc_cleared_gimple_statement_d_stat (size PASS_MEM_STAT);
156 gimple_set_code (stmt, code);
157 gimple_set_num_ops (stmt, num_ops);
158
159 /* Do not call gimple_set_modified here as it has other side
160 effects and this tuple is still not completely built. */
161 stmt->gsbase.modified = 1;
162
163 return stmt;
164 }
165
166 /* Set SUBCODE to be the code of the expression computed by statement G. */
167
168 static inline void
169 gimple_set_subcode (gimple g, unsigned subcode)
170 {
171 /* We only have 16 bits for the RHS code. Assert that we are not
172 overflowing it. */
173 gcc_assert (subcode < (1 << 16));
174 g->gsbase.subcode = subcode;
175 }
176
177
178
179 /* Build a tuple with operands. CODE is the statement to build (which
180 must be one of the GIMPLE_WITH_OPS tuples). SUBCODE is the sub-code
181 for the new tuple. NUM_OPS is the number of operands to allocate. */
182
183 #define gimple_build_with_ops(c, s, n) \
184 gimple_build_with_ops_stat (c, s, n MEM_STAT_INFO)
185
186 static gimple
187 gimple_build_with_ops_stat (enum gimple_code code, unsigned subcode,
188 unsigned num_ops MEM_STAT_DECL)
189 {
190 gimple s = gimple_alloc_stat (code, num_ops PASS_MEM_STAT);
191 gimple_set_subcode (s, subcode);
192
193 return s;
194 }
195
196
197 /* Build a GIMPLE_RETURN statement returning RETVAL. */
198
199 gimple
200 gimple_build_return (tree retval)
201 {
202 gimple s = gimple_build_with_ops (GIMPLE_RETURN, ERROR_MARK, 1);
203 if (retval)
204 gimple_return_set_retval (s, retval);
205 return s;
206 }
207
208 /* Reset alias information on call S. */
209
210 void
211 gimple_call_reset_alias_info (gimple s)
212 {
213 if (gimple_call_flags (s) & ECF_CONST)
214 memset (gimple_call_use_set (s), 0, sizeof (struct pt_solution));
215 else
216 pt_solution_reset (gimple_call_use_set (s));
217 if (gimple_call_flags (s) & (ECF_CONST|ECF_PURE|ECF_NOVOPS))
218 memset (gimple_call_clobber_set (s), 0, sizeof (struct pt_solution));
219 else
220 pt_solution_reset (gimple_call_clobber_set (s));
221 }
222
223 /* Helper for gimple_build_call, gimple_build_call_vec and
224 gimple_build_call_from_tree. Build the basic components of a
225 GIMPLE_CALL statement to function FN with NARGS arguments. */
226
227 static inline gimple
228 gimple_build_call_1 (tree fn, unsigned nargs)
229 {
230 gimple s = gimple_build_with_ops (GIMPLE_CALL, ERROR_MARK, nargs + 3);
231 if (TREE_CODE (fn) == FUNCTION_DECL)
232 fn = build_fold_addr_expr (fn);
233 gimple_set_op (s, 1, fn);
234 gimple_call_set_fntype (s, TREE_TYPE (TREE_TYPE (fn)));
235 gimple_call_reset_alias_info (s);
236 return s;
237 }
238
239
240 /* Build a GIMPLE_CALL statement to function FN with the arguments
241 specified in vector ARGS. */
242
243 gimple
244 gimple_build_call_vec (tree fn, VEC(tree, heap) *args)
245 {
246 unsigned i;
247 unsigned nargs = VEC_length (tree, args);
248 gimple call = gimple_build_call_1 (fn, nargs);
249
250 for (i = 0; i < nargs; i++)
251 gimple_call_set_arg (call, i, VEC_index (tree, args, i));
252
253 return call;
254 }
255
256
257 /* Build a GIMPLE_CALL statement to function FN. NARGS is the number of
258 arguments. The ... are the arguments. */
259
260 gimple
261 gimple_build_call (tree fn, unsigned nargs, ...)
262 {
263 va_list ap;
264 gimple call;
265 unsigned i;
266
267 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL || is_gimple_call_addr (fn));
268
269 call = gimple_build_call_1 (fn, nargs);
270
271 va_start (ap, nargs);
272 for (i = 0; i < nargs; i++)
273 gimple_call_set_arg (call, i, va_arg (ap, tree));
274 va_end (ap);
275
276 return call;
277 }
278
279
280 /* Helper for gimple_build_call_internal and gimple_build_call_internal_vec.
281 Build the basic components of a GIMPLE_CALL statement to internal
282 function FN with NARGS arguments. */
283
284 static inline gimple
285 gimple_build_call_internal_1 (enum internal_fn fn, unsigned nargs)
286 {
287 gimple s = gimple_build_with_ops (GIMPLE_CALL, ERROR_MARK, nargs + 3);
288 s->gsbase.subcode |= GF_CALL_INTERNAL;
289 gimple_call_set_internal_fn (s, fn);
290 gimple_call_reset_alias_info (s);
291 return s;
292 }
293
294
295 /* Build a GIMPLE_CALL statement to internal function FN. NARGS is
296 the number of arguments. The ... are the arguments. */
297
298 gimple
299 gimple_build_call_internal (enum internal_fn fn, unsigned nargs, ...)
300 {
301 va_list ap;
302 gimple call;
303 unsigned i;
304
305 call = gimple_build_call_internal_1 (fn, nargs);
306 va_start (ap, nargs);
307 for (i = 0; i < nargs; i++)
308 gimple_call_set_arg (call, i, va_arg (ap, tree));
309 va_end (ap);
310
311 return call;
312 }
313
314
315 /* Build a GIMPLE_CALL statement to internal function FN with the arguments
316 specified in vector ARGS. */
317
318 gimple
319 gimple_build_call_internal_vec (enum internal_fn fn, VEC(tree, heap) *args)
320 {
321 unsigned i, nargs;
322 gimple call;
323
324 nargs = VEC_length (tree, args);
325 call = gimple_build_call_internal_1 (fn, nargs);
326 for (i = 0; i < nargs; i++)
327 gimple_call_set_arg (call, i, VEC_index (tree, args, i));
328
329 return call;
330 }
331
332
333 /* Build a GIMPLE_CALL statement from CALL_EXPR T. Note that T is
334 assumed to be in GIMPLE form already. Minimal checking is done of
335 this fact. */
336
337 gimple
338 gimple_build_call_from_tree (tree t)
339 {
340 unsigned i, nargs;
341 gimple call;
342 tree fndecl = get_callee_fndecl (t);
343
344 gcc_assert (TREE_CODE (t) == CALL_EXPR);
345
346 nargs = call_expr_nargs (t);
347 call = gimple_build_call_1 (fndecl ? fndecl : CALL_EXPR_FN (t), nargs);
348
349 for (i = 0; i < nargs; i++)
350 gimple_call_set_arg (call, i, CALL_EXPR_ARG (t, i));
351
352 gimple_set_block (call, TREE_BLOCK (t));
353
354 /* Carry all the CALL_EXPR flags to the new GIMPLE_CALL. */
355 gimple_call_set_chain (call, CALL_EXPR_STATIC_CHAIN (t));
356 gimple_call_set_tail (call, CALL_EXPR_TAILCALL (t));
357 gimple_call_set_cannot_inline (call, CALL_CANNOT_INLINE_P (t));
358 gimple_call_set_return_slot_opt (call, CALL_EXPR_RETURN_SLOT_OPT (t));
359 if (fndecl
360 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
361 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_ALLOCA)
362 gimple_call_set_alloca_for_var (call, CALL_ALLOCA_FOR_VAR_P (t));
363 else
364 gimple_call_set_from_thunk (call, CALL_FROM_THUNK_P (t));
365 gimple_call_set_va_arg_pack (call, CALL_EXPR_VA_ARG_PACK (t));
366 gimple_call_set_nothrow (call, TREE_NOTHROW (t));
367 gimple_set_no_warning (call, TREE_NO_WARNING (t));
368
369 return call;
370 }
371
372
373 /* Extract the operands and code for expression EXPR into *SUBCODE_P,
374 *OP1_P, *OP2_P and *OP3_P respectively. */
375
376 void
377 extract_ops_from_tree_1 (tree expr, enum tree_code *subcode_p, tree *op1_p,
378 tree *op2_p, tree *op3_p)
379 {
380 enum gimple_rhs_class grhs_class;
381
382 *subcode_p = TREE_CODE (expr);
383 grhs_class = get_gimple_rhs_class (*subcode_p);
384
385 if (grhs_class == GIMPLE_TERNARY_RHS)
386 {
387 *op1_p = TREE_OPERAND (expr, 0);
388 *op2_p = TREE_OPERAND (expr, 1);
389 *op3_p = TREE_OPERAND (expr, 2);
390 }
391 else if (grhs_class == GIMPLE_BINARY_RHS)
392 {
393 *op1_p = TREE_OPERAND (expr, 0);
394 *op2_p = TREE_OPERAND (expr, 1);
395 *op3_p = NULL_TREE;
396 }
397 else if (grhs_class == GIMPLE_UNARY_RHS)
398 {
399 *op1_p = TREE_OPERAND (expr, 0);
400 *op2_p = NULL_TREE;
401 *op3_p = NULL_TREE;
402 }
403 else if (grhs_class == GIMPLE_SINGLE_RHS)
404 {
405 *op1_p = expr;
406 *op2_p = NULL_TREE;
407 *op3_p = NULL_TREE;
408 }
409 else
410 gcc_unreachable ();
411 }
412
413
414 /* Build a GIMPLE_ASSIGN statement.
415
416 LHS of the assignment.
417 RHS of the assignment which can be unary or binary. */
418
419 gimple
420 gimple_build_assign_stat (tree lhs, tree rhs MEM_STAT_DECL)
421 {
422 enum tree_code subcode;
423 tree op1, op2, op3;
424
425 extract_ops_from_tree_1 (rhs, &subcode, &op1, &op2, &op3);
426 return gimple_build_assign_with_ops_stat (subcode, lhs, op1, op2, op3
427 PASS_MEM_STAT);
428 }
429
430
431 /* Build a GIMPLE_ASSIGN statement with sub-code SUBCODE and operands
432 OP1 and OP2. If OP2 is NULL then SUBCODE must be of class
433 GIMPLE_UNARY_RHS or GIMPLE_SINGLE_RHS. */
434
435 gimple
436 gimple_build_assign_with_ops_stat (enum tree_code subcode, tree lhs, tree op1,
437 tree op2, tree op3 MEM_STAT_DECL)
438 {
439 unsigned num_ops;
440 gimple p;
441
442 /* Need 1 operand for LHS and 1 or 2 for the RHS (depending on the
443 code). */
444 num_ops = get_gimple_rhs_num_ops (subcode) + 1;
445
446 p = gimple_build_with_ops_stat (GIMPLE_ASSIGN, (unsigned)subcode, num_ops
447 PASS_MEM_STAT);
448 gimple_assign_set_lhs (p, lhs);
449 gimple_assign_set_rhs1 (p, op1);
450 if (op2)
451 {
452 gcc_assert (num_ops > 2);
453 gimple_assign_set_rhs2 (p, op2);
454 }
455
456 if (op3)
457 {
458 gcc_assert (num_ops > 3);
459 gimple_assign_set_rhs3 (p, op3);
460 }
461
462 return p;
463 }
464
465
466 /* Build a new GIMPLE_ASSIGN tuple and append it to the end of *SEQ_P.
467
468 DST/SRC are the destination and source respectively. You can pass
469 ungimplified trees in DST or SRC, in which case they will be
470 converted to a gimple operand if necessary.
471
472 This function returns the newly created GIMPLE_ASSIGN tuple. */
473
474 gimple
475 gimplify_assign (tree dst, tree src, gimple_seq *seq_p)
476 {
477 tree t = build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, src);
478 gimplify_and_add (t, seq_p);
479 ggc_free (t);
480 return gimple_seq_last_stmt (*seq_p);
481 }
482
483
484 /* Build a GIMPLE_COND statement.
485
486 PRED is the condition used to compare LHS and the RHS.
487 T_LABEL is the label to jump to if the condition is true.
488 F_LABEL is the label to jump to otherwise. */
489
490 gimple
491 gimple_build_cond (enum tree_code pred_code, tree lhs, tree rhs,
492 tree t_label, tree f_label)
493 {
494 gimple p;
495
496 gcc_assert (TREE_CODE_CLASS (pred_code) == tcc_comparison);
497 p = gimple_build_with_ops (GIMPLE_COND, pred_code, 4);
498 gimple_cond_set_lhs (p, lhs);
499 gimple_cond_set_rhs (p, rhs);
500 gimple_cond_set_true_label (p, t_label);
501 gimple_cond_set_false_label (p, f_label);
502 return p;
503 }
504
505
506 /* Extract operands for a GIMPLE_COND statement out of COND_EXPR tree COND. */
507
508 void
509 gimple_cond_get_ops_from_tree (tree cond, enum tree_code *code_p,
510 tree *lhs_p, tree *rhs_p)
511 {
512 gcc_assert (TREE_CODE_CLASS (TREE_CODE (cond)) == tcc_comparison
513 || TREE_CODE (cond) == TRUTH_NOT_EXPR
514 || is_gimple_min_invariant (cond)
515 || SSA_VAR_P (cond));
516
517 extract_ops_from_tree (cond, code_p, lhs_p, rhs_p);
518
519 /* Canonicalize conditionals of the form 'if (!VAL)'. */
520 if (*code_p == TRUTH_NOT_EXPR)
521 {
522 *code_p = EQ_EXPR;
523 gcc_assert (*lhs_p && *rhs_p == NULL_TREE);
524 *rhs_p = build_zero_cst (TREE_TYPE (*lhs_p));
525 }
526 /* Canonicalize conditionals of the form 'if (VAL)' */
527 else if (TREE_CODE_CLASS (*code_p) != tcc_comparison)
528 {
529 *code_p = NE_EXPR;
530 gcc_assert (*lhs_p && *rhs_p == NULL_TREE);
531 *rhs_p = build_zero_cst (TREE_TYPE (*lhs_p));
532 }
533 }
534
535
536 /* Build a GIMPLE_COND statement from the conditional expression tree
537 COND. T_LABEL and F_LABEL are as in gimple_build_cond. */
538
539 gimple
540 gimple_build_cond_from_tree (tree cond, tree t_label, tree f_label)
541 {
542 enum tree_code code;
543 tree lhs, rhs;
544
545 gimple_cond_get_ops_from_tree (cond, &code, &lhs, &rhs);
546 return gimple_build_cond (code, lhs, rhs, t_label, f_label);
547 }
548
549 /* Set code, lhs, and rhs of a GIMPLE_COND from a suitable
550 boolean expression tree COND. */
551
552 void
553 gimple_cond_set_condition_from_tree (gimple stmt, tree cond)
554 {
555 enum tree_code code;
556 tree lhs, rhs;
557
558 gimple_cond_get_ops_from_tree (cond, &code, &lhs, &rhs);
559 gimple_cond_set_condition (stmt, code, lhs, rhs);
560 }
561
562 /* Build a GIMPLE_LABEL statement for LABEL. */
563
564 gimple
565 gimple_build_label (tree label)
566 {
567 gimple p = gimple_build_with_ops (GIMPLE_LABEL, ERROR_MARK, 1);
568 gimple_label_set_label (p, label);
569 return p;
570 }
571
572 /* Build a GIMPLE_GOTO statement to label DEST. */
573
574 gimple
575 gimple_build_goto (tree dest)
576 {
577 gimple p = gimple_build_with_ops (GIMPLE_GOTO, ERROR_MARK, 1);
578 gimple_goto_set_dest (p, dest);
579 return p;
580 }
581
582
583 /* Build a GIMPLE_NOP statement. */
584
585 gimple
586 gimple_build_nop (void)
587 {
588 return gimple_alloc (GIMPLE_NOP, 0);
589 }
590
591
592 /* Build a GIMPLE_BIND statement.
593 VARS are the variables in BODY.
594 BLOCK is the containing block. */
595
596 gimple
597 gimple_build_bind (tree vars, gimple_seq body, tree block)
598 {
599 gimple p = gimple_alloc (GIMPLE_BIND, 0);
600 gimple_bind_set_vars (p, vars);
601 if (body)
602 gimple_bind_set_body (p, body);
603 if (block)
604 gimple_bind_set_block (p, block);
605 return p;
606 }
607
608 /* Helper function to set the simple fields of a asm stmt.
609
610 STRING is a pointer to a string that is the asm blocks assembly code.
611 NINPUT is the number of register inputs.
612 NOUTPUT is the number of register outputs.
613 NCLOBBERS is the number of clobbered registers.
614 */
615
616 static inline gimple
617 gimple_build_asm_1 (const char *string, unsigned ninputs, unsigned noutputs,
618 unsigned nclobbers, unsigned nlabels)
619 {
620 gimple p;
621 int size = strlen (string);
622
623 /* ASMs with labels cannot have outputs. This should have been
624 enforced by the front end. */
625 gcc_assert (nlabels == 0 || noutputs == 0);
626
627 p = gimple_build_with_ops (GIMPLE_ASM, ERROR_MARK,
628 ninputs + noutputs + nclobbers + nlabels);
629
630 p->gimple_asm.ni = ninputs;
631 p->gimple_asm.no = noutputs;
632 p->gimple_asm.nc = nclobbers;
633 p->gimple_asm.nl = nlabels;
634 p->gimple_asm.string = ggc_alloc_string (string, size);
635
636 #ifdef GATHER_STATISTICS
637 gimple_alloc_sizes[(int) gimple_alloc_kind (GIMPLE_ASM)] += size;
638 #endif
639
640 return p;
641 }
642
643 /* Build a GIMPLE_ASM statement.
644
645 STRING is the assembly code.
646 NINPUT is the number of register inputs.
647 NOUTPUT is the number of register outputs.
648 NCLOBBERS is the number of clobbered registers.
649 INPUTS is a vector of the input register parameters.
650 OUTPUTS is a vector of the output register parameters.
651 CLOBBERS is a vector of the clobbered register parameters.
652 LABELS is a vector of destination labels. */
653
654 gimple
655 gimple_build_asm_vec (const char *string, VEC(tree,gc)* inputs,
656 VEC(tree,gc)* outputs, VEC(tree,gc)* clobbers,
657 VEC(tree,gc)* labels)
658 {
659 gimple p;
660 unsigned i;
661
662 p = gimple_build_asm_1 (string,
663 VEC_length (tree, inputs),
664 VEC_length (tree, outputs),
665 VEC_length (tree, clobbers),
666 VEC_length (tree, labels));
667
668 for (i = 0; i < VEC_length (tree, inputs); i++)
669 gimple_asm_set_input_op (p, i, VEC_index (tree, inputs, i));
670
671 for (i = 0; i < VEC_length (tree, outputs); i++)
672 gimple_asm_set_output_op (p, i, VEC_index (tree, outputs, i));
673
674 for (i = 0; i < VEC_length (tree, clobbers); i++)
675 gimple_asm_set_clobber_op (p, i, VEC_index (tree, clobbers, i));
676
677 for (i = 0; i < VEC_length (tree, labels); i++)
678 gimple_asm_set_label_op (p, i, VEC_index (tree, labels, i));
679
680 return p;
681 }
682
683 /* Build a GIMPLE_CATCH statement.
684
685 TYPES are the catch types.
686 HANDLER is the exception handler. */
687
688 gimple
689 gimple_build_catch (tree types, gimple_seq handler)
690 {
691 gimple p = gimple_alloc (GIMPLE_CATCH, 0);
692 gimple_catch_set_types (p, types);
693 if (handler)
694 gimple_catch_set_handler (p, handler);
695
696 return p;
697 }
698
699 /* Build a GIMPLE_EH_FILTER statement.
700
701 TYPES are the filter's types.
702 FAILURE is the filter's failure action. */
703
704 gimple
705 gimple_build_eh_filter (tree types, gimple_seq failure)
706 {
707 gimple p = gimple_alloc (GIMPLE_EH_FILTER, 0);
708 gimple_eh_filter_set_types (p, types);
709 if (failure)
710 gimple_eh_filter_set_failure (p, failure);
711
712 return p;
713 }
714
715 /* Build a GIMPLE_EH_MUST_NOT_THROW statement. */
716
717 gimple
718 gimple_build_eh_must_not_throw (tree decl)
719 {
720 gimple p = gimple_alloc (GIMPLE_EH_MUST_NOT_THROW, 0);
721
722 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
723 gcc_assert (flags_from_decl_or_type (decl) & ECF_NORETURN);
724 gimple_eh_must_not_throw_set_fndecl (p, decl);
725
726 return p;
727 }
728
729 /* Build a GIMPLE_TRY statement.
730
731 EVAL is the expression to evaluate.
732 CLEANUP is the cleanup expression.
733 KIND is either GIMPLE_TRY_CATCH or GIMPLE_TRY_FINALLY depending on
734 whether this is a try/catch or a try/finally respectively. */
735
736 gimple
737 gimple_build_try (gimple_seq eval, gimple_seq cleanup,
738 enum gimple_try_flags kind)
739 {
740 gimple p;
741
742 gcc_assert (kind == GIMPLE_TRY_CATCH || kind == GIMPLE_TRY_FINALLY);
743 p = gimple_alloc (GIMPLE_TRY, 0);
744 gimple_set_subcode (p, kind);
745 if (eval)
746 gimple_try_set_eval (p, eval);
747 if (cleanup)
748 gimple_try_set_cleanup (p, cleanup);
749
750 return p;
751 }
752
753 /* Construct a GIMPLE_WITH_CLEANUP_EXPR statement.
754
755 CLEANUP is the cleanup expression. */
756
757 gimple
758 gimple_build_wce (gimple_seq cleanup)
759 {
760 gimple p = gimple_alloc (GIMPLE_WITH_CLEANUP_EXPR, 0);
761 if (cleanup)
762 gimple_wce_set_cleanup (p, cleanup);
763
764 return p;
765 }
766
767
768 /* Build a GIMPLE_RESX statement. */
769
770 gimple
771 gimple_build_resx (int region)
772 {
773 gimple p = gimple_build_with_ops (GIMPLE_RESX, ERROR_MARK, 0);
774 p->gimple_eh_ctrl.region = region;
775 return p;
776 }
777
778
779 /* The helper for constructing a gimple switch statement.
780 INDEX is the switch's index.
781 NLABELS is the number of labels in the switch excluding the default.
782 DEFAULT_LABEL is the default label for the switch statement. */
783
784 gimple
785 gimple_build_switch_nlabels (unsigned nlabels, tree index, tree default_label)
786 {
787 /* nlabels + 1 default label + 1 index. */
788 gimple p = gimple_build_with_ops (GIMPLE_SWITCH, ERROR_MARK,
789 1 + (default_label != NULL) + nlabels);
790 gimple_switch_set_index (p, index);
791 if (default_label)
792 gimple_switch_set_default_label (p, default_label);
793 return p;
794 }
795
796
797 /* Build a GIMPLE_SWITCH statement.
798
799 INDEX is the switch's index.
800 NLABELS is the number of labels in the switch excluding the DEFAULT_LABEL.
801 ... are the labels excluding the default. */
802
803 gimple
804 gimple_build_switch (unsigned nlabels, tree index, tree default_label, ...)
805 {
806 va_list al;
807 unsigned i, offset;
808 gimple p = gimple_build_switch_nlabels (nlabels, index, default_label);
809
810 /* Store the rest of the labels. */
811 va_start (al, default_label);
812 offset = (default_label != NULL);
813 for (i = 0; i < nlabels; i++)
814 gimple_switch_set_label (p, i + offset, va_arg (al, tree));
815 va_end (al);
816
817 return p;
818 }
819
820
821 /* Build a GIMPLE_SWITCH statement.
822
823 INDEX is the switch's index.
824 DEFAULT_LABEL is the default label
825 ARGS is a vector of labels excluding the default. */
826
827 gimple
828 gimple_build_switch_vec (tree index, tree default_label, VEC(tree, heap) *args)
829 {
830 unsigned i, offset, nlabels = VEC_length (tree, args);
831 gimple p = gimple_build_switch_nlabels (nlabels, index, default_label);
832
833 /* Copy the labels from the vector to the switch statement. */
834 offset = (default_label != NULL);
835 for (i = 0; i < nlabels; i++)
836 gimple_switch_set_label (p, i + offset, VEC_index (tree, args, i));
837
838 return p;
839 }
840
841 /* Build a GIMPLE_EH_DISPATCH statement. */
842
843 gimple
844 gimple_build_eh_dispatch (int region)
845 {
846 gimple p = gimple_build_with_ops (GIMPLE_EH_DISPATCH, ERROR_MARK, 0);
847 p->gimple_eh_ctrl.region = region;
848 return p;
849 }
850
851 /* Build a new GIMPLE_DEBUG_BIND statement.
852
853 VAR is bound to VALUE; block and location are taken from STMT. */
854
855 gimple
856 gimple_build_debug_bind_stat (tree var, tree value, gimple stmt MEM_STAT_DECL)
857 {
858 gimple p = gimple_build_with_ops_stat (GIMPLE_DEBUG,
859 (unsigned)GIMPLE_DEBUG_BIND, 2
860 PASS_MEM_STAT);
861
862 gimple_debug_bind_set_var (p, var);
863 gimple_debug_bind_set_value (p, value);
864 if (stmt)
865 {
866 gimple_set_block (p, gimple_block (stmt));
867 gimple_set_location (p, gimple_location (stmt));
868 }
869
870 return p;
871 }
872
873
874 /* Build a GIMPLE_OMP_CRITICAL statement.
875
876 BODY is the sequence of statements for which only one thread can execute.
877 NAME is optional identifier for this critical block. */
878
879 gimple
880 gimple_build_omp_critical (gimple_seq body, tree name)
881 {
882 gimple p = gimple_alloc (GIMPLE_OMP_CRITICAL, 0);
883 gimple_omp_critical_set_name (p, name);
884 if (body)
885 gimple_omp_set_body (p, body);
886
887 return p;
888 }
889
890 /* Build a GIMPLE_OMP_FOR statement.
891
892 BODY is sequence of statements inside the for loop.
893 CLAUSES, are any of the OMP loop construct's clauses: private, firstprivate,
894 lastprivate, reductions, ordered, schedule, and nowait.
895 COLLAPSE is the collapse count.
896 PRE_BODY is the sequence of statements that are loop invariant. */
897
898 gimple
899 gimple_build_omp_for (gimple_seq body, tree clauses, size_t collapse,
900 gimple_seq pre_body)
901 {
902 gimple p = gimple_alloc (GIMPLE_OMP_FOR, 0);
903 if (body)
904 gimple_omp_set_body (p, body);
905 gimple_omp_for_set_clauses (p, clauses);
906 p->gimple_omp_for.collapse = collapse;
907 p->gimple_omp_for.iter
908 = ggc_alloc_cleared_vec_gimple_omp_for_iter (collapse);
909 if (pre_body)
910 gimple_omp_for_set_pre_body (p, pre_body);
911
912 return p;
913 }
914
915
916 /* Build a GIMPLE_OMP_PARALLEL statement.
917
918 BODY is sequence of statements which are executed in parallel.
919 CLAUSES, are the OMP parallel construct's clauses.
920 CHILD_FN is the function created for the parallel threads to execute.
921 DATA_ARG are the shared data argument(s). */
922
923 gimple
924 gimple_build_omp_parallel (gimple_seq body, tree clauses, tree child_fn,
925 tree data_arg)
926 {
927 gimple p = gimple_alloc (GIMPLE_OMP_PARALLEL, 0);
928 if (body)
929 gimple_omp_set_body (p, body);
930 gimple_omp_parallel_set_clauses (p, clauses);
931 gimple_omp_parallel_set_child_fn (p, child_fn);
932 gimple_omp_parallel_set_data_arg (p, data_arg);
933
934 return p;
935 }
936
937
938 /* Build a GIMPLE_OMP_TASK statement.
939
940 BODY is sequence of statements which are executed by the explicit task.
941 CLAUSES, are the OMP parallel construct's clauses.
942 CHILD_FN is the function created for the parallel threads to execute.
943 DATA_ARG are the shared data argument(s).
944 COPY_FN is the optional function for firstprivate initialization.
945 ARG_SIZE and ARG_ALIGN are size and alignment of the data block. */
946
947 gimple
948 gimple_build_omp_task (gimple_seq body, tree clauses, tree child_fn,
949 tree data_arg, tree copy_fn, tree arg_size,
950 tree arg_align)
951 {
952 gimple p = gimple_alloc (GIMPLE_OMP_TASK, 0);
953 if (body)
954 gimple_omp_set_body (p, body);
955 gimple_omp_task_set_clauses (p, clauses);
956 gimple_omp_task_set_child_fn (p, child_fn);
957 gimple_omp_task_set_data_arg (p, data_arg);
958 gimple_omp_task_set_copy_fn (p, copy_fn);
959 gimple_omp_task_set_arg_size (p, arg_size);
960 gimple_omp_task_set_arg_align (p, arg_align);
961
962 return p;
963 }
964
965
966 /* Build a GIMPLE_OMP_SECTION statement for a sections statement.
967
968 BODY is the sequence of statements in the section. */
969
970 gimple
971 gimple_build_omp_section (gimple_seq body)
972 {
973 gimple p = gimple_alloc (GIMPLE_OMP_SECTION, 0);
974 if (body)
975 gimple_omp_set_body (p, body);
976
977 return p;
978 }
979
980
981 /* Build a GIMPLE_OMP_MASTER statement.
982
983 BODY is the sequence of statements to be executed by just the master. */
984
985 gimple
986 gimple_build_omp_master (gimple_seq body)
987 {
988 gimple p = gimple_alloc (GIMPLE_OMP_MASTER, 0);
989 if (body)
990 gimple_omp_set_body (p, body);
991
992 return p;
993 }
994
995
996 /* Build a GIMPLE_OMP_CONTINUE statement.
997
998 CONTROL_DEF is the definition of the control variable.
999 CONTROL_USE is the use of the control variable. */
1000
1001 gimple
1002 gimple_build_omp_continue (tree control_def, tree control_use)
1003 {
1004 gimple p = gimple_alloc (GIMPLE_OMP_CONTINUE, 0);
1005 gimple_omp_continue_set_control_def (p, control_def);
1006 gimple_omp_continue_set_control_use (p, control_use);
1007 return p;
1008 }
1009
1010 /* Build a GIMPLE_OMP_ORDERED statement.
1011
1012 BODY is the sequence of statements inside a loop that will executed in
1013 sequence. */
1014
1015 gimple
1016 gimple_build_omp_ordered (gimple_seq body)
1017 {
1018 gimple p = gimple_alloc (GIMPLE_OMP_ORDERED, 0);
1019 if (body)
1020 gimple_omp_set_body (p, body);
1021
1022 return p;
1023 }
1024
1025
1026 /* Build a GIMPLE_OMP_RETURN statement.
1027 WAIT_P is true if this is a non-waiting return. */
1028
1029 gimple
1030 gimple_build_omp_return (bool wait_p)
1031 {
1032 gimple p = gimple_alloc (GIMPLE_OMP_RETURN, 0);
1033 if (wait_p)
1034 gimple_omp_return_set_nowait (p);
1035
1036 return p;
1037 }
1038
1039
1040 /* Build a GIMPLE_OMP_SECTIONS statement.
1041
1042 BODY is a sequence of section statements.
1043 CLAUSES are any of the OMP sections contsruct's clauses: private,
1044 firstprivate, lastprivate, reduction, and nowait. */
1045
1046 gimple
1047 gimple_build_omp_sections (gimple_seq body, tree clauses)
1048 {
1049 gimple p = gimple_alloc (GIMPLE_OMP_SECTIONS, 0);
1050 if (body)
1051 gimple_omp_set_body (p, body);
1052 gimple_omp_sections_set_clauses (p, clauses);
1053
1054 return p;
1055 }
1056
1057
1058 /* Build a GIMPLE_OMP_SECTIONS_SWITCH. */
1059
1060 gimple
1061 gimple_build_omp_sections_switch (void)
1062 {
1063 return gimple_alloc (GIMPLE_OMP_SECTIONS_SWITCH, 0);
1064 }
1065
1066
1067 /* Build a GIMPLE_OMP_SINGLE statement.
1068
1069 BODY is the sequence of statements that will be executed once.
1070 CLAUSES are any of the OMP single construct's clauses: private, firstprivate,
1071 copyprivate, nowait. */
1072
1073 gimple
1074 gimple_build_omp_single (gimple_seq body, tree clauses)
1075 {
1076 gimple p = gimple_alloc (GIMPLE_OMP_SINGLE, 0);
1077 if (body)
1078 gimple_omp_set_body (p, body);
1079 gimple_omp_single_set_clauses (p, clauses);
1080
1081 return p;
1082 }
1083
1084
1085 /* Build a GIMPLE_OMP_ATOMIC_LOAD statement. */
1086
1087 gimple
1088 gimple_build_omp_atomic_load (tree lhs, tree rhs)
1089 {
1090 gimple p = gimple_alloc (GIMPLE_OMP_ATOMIC_LOAD, 0);
1091 gimple_omp_atomic_load_set_lhs (p, lhs);
1092 gimple_omp_atomic_load_set_rhs (p, rhs);
1093 return p;
1094 }
1095
1096 /* Build a GIMPLE_OMP_ATOMIC_STORE statement.
1097
1098 VAL is the value we are storing. */
1099
1100 gimple
1101 gimple_build_omp_atomic_store (tree val)
1102 {
1103 gimple p = gimple_alloc (GIMPLE_OMP_ATOMIC_STORE, 0);
1104 gimple_omp_atomic_store_set_val (p, val);
1105 return p;
1106 }
1107
1108 /* Build a GIMPLE_PREDICT statement. PREDICT is one of the predictors from
1109 predict.def, OUTCOME is NOT_TAKEN or TAKEN. */
1110
1111 gimple
1112 gimple_build_predict (enum br_predictor predictor, enum prediction outcome)
1113 {
1114 gimple p = gimple_alloc (GIMPLE_PREDICT, 0);
1115 /* Ensure all the predictors fit into the lower bits of the subcode. */
1116 gcc_assert ((int) END_PREDICTORS <= GF_PREDICT_TAKEN);
1117 gimple_predict_set_predictor (p, predictor);
1118 gimple_predict_set_outcome (p, outcome);
1119 return p;
1120 }
1121
1122 #if defined ENABLE_GIMPLE_CHECKING
1123 /* Complain of a gimple type mismatch and die. */
1124
1125 void
1126 gimple_check_failed (const_gimple gs, const char *file, int line,
1127 const char *function, enum gimple_code code,
1128 enum tree_code subcode)
1129 {
1130 internal_error ("gimple check: expected %s(%s), have %s(%s) in %s, at %s:%d",
1131 gimple_code_name[code],
1132 tree_code_name[subcode],
1133 gimple_code_name[gimple_code (gs)],
1134 gs->gsbase.subcode > 0
1135 ? tree_code_name[gs->gsbase.subcode]
1136 : "",
1137 function, trim_filename (file), line);
1138 }
1139 #endif /* ENABLE_GIMPLE_CHECKING */
1140
1141
1142 /* Allocate a new GIMPLE sequence in GC memory and return it. If
1143 there are free sequences in GIMPLE_SEQ_CACHE return one of those
1144 instead. */
1145
1146 gimple_seq
1147 gimple_seq_alloc (void)
1148 {
1149 gimple_seq seq = gimple_seq_cache;
1150 if (seq)
1151 {
1152 gimple_seq_cache = gimple_seq_cache->next_free;
1153 gcc_assert (gimple_seq_cache != seq);
1154 memset (seq, 0, sizeof (*seq));
1155 }
1156 else
1157 {
1158 seq = ggc_alloc_cleared_gimple_seq_d ();
1159 #ifdef GATHER_STATISTICS
1160 gimple_alloc_counts[(int) gimple_alloc_kind_seq]++;
1161 gimple_alloc_sizes[(int) gimple_alloc_kind_seq] += sizeof (*seq);
1162 #endif
1163 }
1164
1165 return seq;
1166 }
1167
1168 /* Return SEQ to the free pool of GIMPLE sequences. */
1169
1170 void
1171 gimple_seq_free (gimple_seq seq)
1172 {
1173 if (seq == NULL)
1174 return;
1175
1176 gcc_assert (gimple_seq_first (seq) == NULL);
1177 gcc_assert (gimple_seq_last (seq) == NULL);
1178
1179 /* If this triggers, it's a sign that the same list is being freed
1180 twice. */
1181 gcc_assert (seq != gimple_seq_cache || gimple_seq_cache == NULL);
1182
1183 /* Add SEQ to the pool of free sequences. */
1184 seq->next_free = gimple_seq_cache;
1185 gimple_seq_cache = seq;
1186 }
1187
1188
1189 /* Link gimple statement GS to the end of the sequence *SEQ_P. If
1190 *SEQ_P is NULL, a new sequence is allocated. */
1191
1192 void
1193 gimple_seq_add_stmt (gimple_seq *seq_p, gimple gs)
1194 {
1195 gimple_stmt_iterator si;
1196
1197 if (gs == NULL)
1198 return;
1199
1200 if (*seq_p == NULL)
1201 *seq_p = gimple_seq_alloc ();
1202
1203 si = gsi_last (*seq_p);
1204 gsi_insert_after (&si, gs, GSI_NEW_STMT);
1205 }
1206
1207
1208 /* Append sequence SRC to the end of sequence *DST_P. If *DST_P is
1209 NULL, a new sequence is allocated. */
1210
1211 void
1212 gimple_seq_add_seq (gimple_seq *dst_p, gimple_seq src)
1213 {
1214 gimple_stmt_iterator si;
1215
1216 if (src == NULL)
1217 return;
1218
1219 if (*dst_p == NULL)
1220 *dst_p = gimple_seq_alloc ();
1221
1222 si = gsi_last (*dst_p);
1223 gsi_insert_seq_after (&si, src, GSI_NEW_STMT);
1224 }
1225
1226
1227 /* Helper function of empty_body_p. Return true if STMT is an empty
1228 statement. */
1229
1230 static bool
1231 empty_stmt_p (gimple stmt)
1232 {
1233 if (gimple_code (stmt) == GIMPLE_NOP)
1234 return true;
1235 if (gimple_code (stmt) == GIMPLE_BIND)
1236 return empty_body_p (gimple_bind_body (stmt));
1237 return false;
1238 }
1239
1240
1241 /* Return true if BODY contains nothing but empty statements. */
1242
1243 bool
1244 empty_body_p (gimple_seq body)
1245 {
1246 gimple_stmt_iterator i;
1247
1248 if (gimple_seq_empty_p (body))
1249 return true;
1250 for (i = gsi_start (body); !gsi_end_p (i); gsi_next (&i))
1251 if (!empty_stmt_p (gsi_stmt (i))
1252 && !is_gimple_debug (gsi_stmt (i)))
1253 return false;
1254
1255 return true;
1256 }
1257
1258
1259 /* Perform a deep copy of sequence SRC and return the result. */
1260
1261 gimple_seq
1262 gimple_seq_copy (gimple_seq src)
1263 {
1264 gimple_stmt_iterator gsi;
1265 gimple_seq new_seq = gimple_seq_alloc ();
1266 gimple stmt;
1267
1268 for (gsi = gsi_start (src); !gsi_end_p (gsi); gsi_next (&gsi))
1269 {
1270 stmt = gimple_copy (gsi_stmt (gsi));
1271 gimple_seq_add_stmt (&new_seq, stmt);
1272 }
1273
1274 return new_seq;
1275 }
1276
1277
1278 /* Walk all the statements in the sequence SEQ calling walk_gimple_stmt
1279 on each one. WI is as in walk_gimple_stmt.
1280
1281 If walk_gimple_stmt returns non-NULL, the walk is stopped, the
1282 value is stored in WI->CALLBACK_RESULT and the statement that
1283 produced the value is returned.
1284
1285 Otherwise, all the statements are walked and NULL returned. */
1286
1287 gimple
1288 walk_gimple_seq (gimple_seq seq, walk_stmt_fn callback_stmt,
1289 walk_tree_fn callback_op, struct walk_stmt_info *wi)
1290 {
1291 gimple_stmt_iterator gsi;
1292
1293 for (gsi = gsi_start (seq); !gsi_end_p (gsi); gsi_next (&gsi))
1294 {
1295 tree ret = walk_gimple_stmt (&gsi, callback_stmt, callback_op, wi);
1296 if (ret)
1297 {
1298 /* If CALLBACK_STMT or CALLBACK_OP return a value, WI must exist
1299 to hold it. */
1300 gcc_assert (wi);
1301 wi->callback_result = ret;
1302 return gsi_stmt (gsi);
1303 }
1304 }
1305
1306 if (wi)
1307 wi->callback_result = NULL_TREE;
1308
1309 return NULL;
1310 }
1311
1312
1313 /* Helper function for walk_gimple_stmt. Walk operands of a GIMPLE_ASM. */
1314
1315 static tree
1316 walk_gimple_asm (gimple stmt, walk_tree_fn callback_op,
1317 struct walk_stmt_info *wi)
1318 {
1319 tree ret, op;
1320 unsigned noutputs;
1321 const char **oconstraints;
1322 unsigned i, n;
1323 const char *constraint;
1324 bool allows_mem, allows_reg, is_inout;
1325
1326 noutputs = gimple_asm_noutputs (stmt);
1327 oconstraints = (const char **) alloca ((noutputs) * sizeof (const char *));
1328
1329 if (wi)
1330 wi->is_lhs = true;
1331
1332 for (i = 0; i < noutputs; i++)
1333 {
1334 op = gimple_asm_output_op (stmt, i);
1335 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (op)));
1336 oconstraints[i] = constraint;
1337 parse_output_constraint (&constraint, i, 0, 0, &allows_mem, &allows_reg,
1338 &is_inout);
1339 if (wi)
1340 wi->val_only = (allows_reg || !allows_mem);
1341 ret = walk_tree (&TREE_VALUE (op), callback_op, wi, NULL);
1342 if (ret)
1343 return ret;
1344 }
1345
1346 n = gimple_asm_ninputs (stmt);
1347 for (i = 0; i < n; i++)
1348 {
1349 op = gimple_asm_input_op (stmt, i);
1350 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (op)));
1351 parse_input_constraint (&constraint, 0, 0, noutputs, 0,
1352 oconstraints, &allows_mem, &allows_reg);
1353 if (wi)
1354 {
1355 wi->val_only = (allows_reg || !allows_mem);
1356 /* Although input "m" is not really a LHS, we need a lvalue. */
1357 wi->is_lhs = !wi->val_only;
1358 }
1359 ret = walk_tree (&TREE_VALUE (op), callback_op, wi, NULL);
1360 if (ret)
1361 return ret;
1362 }
1363
1364 if (wi)
1365 {
1366 wi->is_lhs = false;
1367 wi->val_only = true;
1368 }
1369
1370 n = gimple_asm_nlabels (stmt);
1371 for (i = 0; i < n; i++)
1372 {
1373 op = gimple_asm_label_op (stmt, i);
1374 ret = walk_tree (&TREE_VALUE (op), callback_op, wi, NULL);
1375 if (ret)
1376 return ret;
1377 }
1378
1379 return NULL_TREE;
1380 }
1381
1382
1383 /* Helper function of WALK_GIMPLE_STMT. Walk every tree operand in
1384 STMT. CALLBACK_OP and WI are as in WALK_GIMPLE_STMT.
1385
1386 CALLBACK_OP is called on each operand of STMT via walk_tree.
1387 Additional parameters to walk_tree must be stored in WI. For each operand
1388 OP, walk_tree is called as:
1389
1390 walk_tree (&OP, CALLBACK_OP, WI, WI->PSET)
1391
1392 If CALLBACK_OP returns non-NULL for an operand, the remaining
1393 operands are not scanned.
1394
1395 The return value is that returned by the last call to walk_tree, or
1396 NULL_TREE if no CALLBACK_OP is specified. */
1397
1398 tree
1399 walk_gimple_op (gimple stmt, walk_tree_fn callback_op,
1400 struct walk_stmt_info *wi)
1401 {
1402 struct pointer_set_t *pset = (wi) ? wi->pset : NULL;
1403 unsigned i;
1404 tree ret = NULL_TREE;
1405
1406 switch (gimple_code (stmt))
1407 {
1408 case GIMPLE_ASSIGN:
1409 /* Walk the RHS operands. If the LHS is of a non-renamable type or
1410 is a register variable, we may use a COMPONENT_REF on the RHS. */
1411 if (wi)
1412 {
1413 tree lhs = gimple_assign_lhs (stmt);
1414 wi->val_only
1415 = (is_gimple_reg_type (TREE_TYPE (lhs)) && !is_gimple_reg (lhs))
1416 || !gimple_assign_single_p (stmt);
1417 }
1418
1419 for (i = 1; i < gimple_num_ops (stmt); i++)
1420 {
1421 ret = walk_tree (gimple_op_ptr (stmt, i), callback_op, wi,
1422 pset);
1423 if (ret)
1424 return ret;
1425 }
1426
1427 /* Walk the LHS. If the RHS is appropriate for a memory, we
1428 may use a COMPONENT_REF on the LHS. */
1429 if (wi)
1430 {
1431 /* If the RHS has more than 1 operand, it is not appropriate
1432 for the memory. */
1433 wi->val_only = !is_gimple_mem_rhs (gimple_assign_rhs1 (stmt))
1434 || !gimple_assign_single_p (stmt);
1435 wi->is_lhs = true;
1436 }
1437
1438 ret = walk_tree (gimple_op_ptr (stmt, 0), callback_op, wi, pset);
1439 if (ret)
1440 return ret;
1441
1442 if (wi)
1443 {
1444 wi->val_only = true;
1445 wi->is_lhs = false;
1446 }
1447 break;
1448
1449 case GIMPLE_CALL:
1450 if (wi)
1451 {
1452 wi->is_lhs = false;
1453 wi->val_only = true;
1454 }
1455
1456 ret = walk_tree (gimple_call_chain_ptr (stmt), callback_op, wi, pset);
1457 if (ret)
1458 return ret;
1459
1460 ret = walk_tree (gimple_call_fn_ptr (stmt), callback_op, wi, pset);
1461 if (ret)
1462 return ret;
1463
1464 for (i = 0; i < gimple_call_num_args (stmt); i++)
1465 {
1466 if (wi)
1467 wi->val_only
1468 = is_gimple_reg_type (TREE_TYPE (gimple_call_arg (stmt, i)));
1469 ret = walk_tree (gimple_call_arg_ptr (stmt, i), callback_op, wi,
1470 pset);
1471 if (ret)
1472 return ret;
1473 }
1474
1475 if (gimple_call_lhs (stmt))
1476 {
1477 if (wi)
1478 {
1479 wi->is_lhs = true;
1480 wi->val_only
1481 = is_gimple_reg_type (TREE_TYPE (gimple_call_lhs (stmt)));
1482 }
1483
1484 ret = walk_tree (gimple_call_lhs_ptr (stmt), callback_op, wi, pset);
1485 if (ret)
1486 return ret;
1487 }
1488
1489 if (wi)
1490 {
1491 wi->is_lhs = false;
1492 wi->val_only = true;
1493 }
1494 break;
1495
1496 case GIMPLE_CATCH:
1497 ret = walk_tree (gimple_catch_types_ptr (stmt), callback_op, wi,
1498 pset);
1499 if (ret)
1500 return ret;
1501 break;
1502
1503 case GIMPLE_EH_FILTER:
1504 ret = walk_tree (gimple_eh_filter_types_ptr (stmt), callback_op, wi,
1505 pset);
1506 if (ret)
1507 return ret;
1508 break;
1509
1510 case GIMPLE_ASM:
1511 ret = walk_gimple_asm (stmt, callback_op, wi);
1512 if (ret)
1513 return ret;
1514 break;
1515
1516 case GIMPLE_OMP_CONTINUE:
1517 ret = walk_tree (gimple_omp_continue_control_def_ptr (stmt),
1518 callback_op, wi, pset);
1519 if (ret)
1520 return ret;
1521
1522 ret = walk_tree (gimple_omp_continue_control_use_ptr (stmt),
1523 callback_op, wi, pset);
1524 if (ret)
1525 return ret;
1526 break;
1527
1528 case GIMPLE_OMP_CRITICAL:
1529 ret = walk_tree (gimple_omp_critical_name_ptr (stmt), callback_op, wi,
1530 pset);
1531 if (ret)
1532 return ret;
1533 break;
1534
1535 case GIMPLE_OMP_FOR:
1536 ret = walk_tree (gimple_omp_for_clauses_ptr (stmt), callback_op, wi,
1537 pset);
1538 if (ret)
1539 return ret;
1540 for (i = 0; i < gimple_omp_for_collapse (stmt); i++)
1541 {
1542 ret = walk_tree (gimple_omp_for_index_ptr (stmt, i), callback_op,
1543 wi, pset);
1544 if (ret)
1545 return ret;
1546 ret = walk_tree (gimple_omp_for_initial_ptr (stmt, i), callback_op,
1547 wi, pset);
1548 if (ret)
1549 return ret;
1550 ret = walk_tree (gimple_omp_for_final_ptr (stmt, i), callback_op,
1551 wi, pset);
1552 if (ret)
1553 return ret;
1554 ret = walk_tree (gimple_omp_for_incr_ptr (stmt, i), callback_op,
1555 wi, pset);
1556 }
1557 if (ret)
1558 return ret;
1559 break;
1560
1561 case GIMPLE_OMP_PARALLEL:
1562 ret = walk_tree (gimple_omp_parallel_clauses_ptr (stmt), callback_op,
1563 wi, pset);
1564 if (ret)
1565 return ret;
1566 ret = walk_tree (gimple_omp_parallel_child_fn_ptr (stmt), callback_op,
1567 wi, pset);
1568 if (ret)
1569 return ret;
1570 ret = walk_tree (gimple_omp_parallel_data_arg_ptr (stmt), callback_op,
1571 wi, pset);
1572 if (ret)
1573 return ret;
1574 break;
1575
1576 case GIMPLE_OMP_TASK:
1577 ret = walk_tree (gimple_omp_task_clauses_ptr (stmt), callback_op,
1578 wi, pset);
1579 if (ret)
1580 return ret;
1581 ret = walk_tree (gimple_omp_task_child_fn_ptr (stmt), callback_op,
1582 wi, pset);
1583 if (ret)
1584 return ret;
1585 ret = walk_tree (gimple_omp_task_data_arg_ptr (stmt), callback_op,
1586 wi, pset);
1587 if (ret)
1588 return ret;
1589 ret = walk_tree (gimple_omp_task_copy_fn_ptr (stmt), callback_op,
1590 wi, pset);
1591 if (ret)
1592 return ret;
1593 ret = walk_tree (gimple_omp_task_arg_size_ptr (stmt), callback_op,
1594 wi, pset);
1595 if (ret)
1596 return ret;
1597 ret = walk_tree (gimple_omp_task_arg_align_ptr (stmt), callback_op,
1598 wi, pset);
1599 if (ret)
1600 return ret;
1601 break;
1602
1603 case GIMPLE_OMP_SECTIONS:
1604 ret = walk_tree (gimple_omp_sections_clauses_ptr (stmt), callback_op,
1605 wi, pset);
1606 if (ret)
1607 return ret;
1608
1609 ret = walk_tree (gimple_omp_sections_control_ptr (stmt), callback_op,
1610 wi, pset);
1611 if (ret)
1612 return ret;
1613
1614 break;
1615
1616 case GIMPLE_OMP_SINGLE:
1617 ret = walk_tree (gimple_omp_single_clauses_ptr (stmt), callback_op, wi,
1618 pset);
1619 if (ret)
1620 return ret;
1621 break;
1622
1623 case GIMPLE_OMP_ATOMIC_LOAD:
1624 ret = walk_tree (gimple_omp_atomic_load_lhs_ptr (stmt), callback_op, wi,
1625 pset);
1626 if (ret)
1627 return ret;
1628
1629 ret = walk_tree (gimple_omp_atomic_load_rhs_ptr (stmt), callback_op, wi,
1630 pset);
1631 if (ret)
1632 return ret;
1633 break;
1634
1635 case GIMPLE_OMP_ATOMIC_STORE:
1636 ret = walk_tree (gimple_omp_atomic_store_val_ptr (stmt), callback_op,
1637 wi, pset);
1638 if (ret)
1639 return ret;
1640 break;
1641
1642 /* Tuples that do not have operands. */
1643 case GIMPLE_NOP:
1644 case GIMPLE_RESX:
1645 case GIMPLE_OMP_RETURN:
1646 case GIMPLE_PREDICT:
1647 break;
1648
1649 default:
1650 {
1651 enum gimple_statement_structure_enum gss;
1652 gss = gimple_statement_structure (stmt);
1653 if (gss == GSS_WITH_OPS || gss == GSS_WITH_MEM_OPS)
1654 for (i = 0; i < gimple_num_ops (stmt); i++)
1655 {
1656 ret = walk_tree (gimple_op_ptr (stmt, i), callback_op, wi, pset);
1657 if (ret)
1658 return ret;
1659 }
1660 }
1661 break;
1662 }
1663
1664 return NULL_TREE;
1665 }
1666
1667
1668 /* Walk the current statement in GSI (optionally using traversal state
1669 stored in WI). If WI is NULL, no state is kept during traversal.
1670 The callback CALLBACK_STMT is called. If CALLBACK_STMT indicates
1671 that it has handled all the operands of the statement, its return
1672 value is returned. Otherwise, the return value from CALLBACK_STMT
1673 is discarded and its operands are scanned.
1674
1675 If CALLBACK_STMT is NULL or it didn't handle the operands,
1676 CALLBACK_OP is called on each operand of the statement via
1677 walk_gimple_op. If walk_gimple_op returns non-NULL for any
1678 operand, the remaining operands are not scanned. In this case, the
1679 return value from CALLBACK_OP is returned.
1680
1681 In any other case, NULL_TREE is returned. */
1682
1683 tree
1684 walk_gimple_stmt (gimple_stmt_iterator *gsi, walk_stmt_fn callback_stmt,
1685 walk_tree_fn callback_op, struct walk_stmt_info *wi)
1686 {
1687 gimple ret;
1688 tree tree_ret;
1689 gimple stmt = gsi_stmt (*gsi);
1690
1691 if (wi)
1692 wi->gsi = *gsi;
1693
1694 if (wi && wi->want_locations && gimple_has_location (stmt))
1695 input_location = gimple_location (stmt);
1696
1697 ret = NULL;
1698
1699 /* Invoke the statement callback. Return if the callback handled
1700 all of STMT operands by itself. */
1701 if (callback_stmt)
1702 {
1703 bool handled_ops = false;
1704 tree_ret = callback_stmt (gsi, &handled_ops, wi);
1705 if (handled_ops)
1706 return tree_ret;
1707
1708 /* If CALLBACK_STMT did not handle operands, it should not have
1709 a value to return. */
1710 gcc_assert (tree_ret == NULL);
1711
1712 /* Re-read stmt in case the callback changed it. */
1713 stmt = gsi_stmt (*gsi);
1714 }
1715
1716 /* If CALLBACK_OP is defined, invoke it on every operand of STMT. */
1717 if (callback_op)
1718 {
1719 tree_ret = walk_gimple_op (stmt, callback_op, wi);
1720 if (tree_ret)
1721 return tree_ret;
1722 }
1723
1724 /* If STMT can have statements inside (e.g. GIMPLE_BIND), walk them. */
1725 switch (gimple_code (stmt))
1726 {
1727 case GIMPLE_BIND:
1728 ret = walk_gimple_seq (gimple_bind_body (stmt), callback_stmt,
1729 callback_op, wi);
1730 if (ret)
1731 return wi->callback_result;
1732 break;
1733
1734 case GIMPLE_CATCH:
1735 ret = walk_gimple_seq (gimple_catch_handler (stmt), callback_stmt,
1736 callback_op, wi);
1737 if (ret)
1738 return wi->callback_result;
1739 break;
1740
1741 case GIMPLE_EH_FILTER:
1742 ret = walk_gimple_seq (gimple_eh_filter_failure (stmt), callback_stmt,
1743 callback_op, wi);
1744 if (ret)
1745 return wi->callback_result;
1746 break;
1747
1748 case GIMPLE_TRY:
1749 ret = walk_gimple_seq (gimple_try_eval (stmt), callback_stmt, callback_op,
1750 wi);
1751 if (ret)
1752 return wi->callback_result;
1753
1754 ret = walk_gimple_seq (gimple_try_cleanup (stmt), callback_stmt,
1755 callback_op, wi);
1756 if (ret)
1757 return wi->callback_result;
1758 break;
1759
1760 case GIMPLE_OMP_FOR:
1761 ret = walk_gimple_seq (gimple_omp_for_pre_body (stmt), callback_stmt,
1762 callback_op, wi);
1763 if (ret)
1764 return wi->callback_result;
1765
1766 /* FALL THROUGH. */
1767 case GIMPLE_OMP_CRITICAL:
1768 case GIMPLE_OMP_MASTER:
1769 case GIMPLE_OMP_ORDERED:
1770 case GIMPLE_OMP_SECTION:
1771 case GIMPLE_OMP_PARALLEL:
1772 case GIMPLE_OMP_TASK:
1773 case GIMPLE_OMP_SECTIONS:
1774 case GIMPLE_OMP_SINGLE:
1775 ret = walk_gimple_seq (gimple_omp_body (stmt), callback_stmt, callback_op,
1776 wi);
1777 if (ret)
1778 return wi->callback_result;
1779 break;
1780
1781 case GIMPLE_WITH_CLEANUP_EXPR:
1782 ret = walk_gimple_seq (gimple_wce_cleanup (stmt), callback_stmt,
1783 callback_op, wi);
1784 if (ret)
1785 return wi->callback_result;
1786 break;
1787
1788 default:
1789 gcc_assert (!gimple_has_substatements (stmt));
1790 break;
1791 }
1792
1793 return NULL;
1794 }
1795
1796
1797 /* Set sequence SEQ to be the GIMPLE body for function FN. */
1798
1799 void
1800 gimple_set_body (tree fndecl, gimple_seq seq)
1801 {
1802 struct function *fn = DECL_STRUCT_FUNCTION (fndecl);
1803 if (fn == NULL)
1804 {
1805 /* If FNDECL still does not have a function structure associated
1806 with it, then it does not make sense for it to receive a
1807 GIMPLE body. */
1808 gcc_assert (seq == NULL);
1809 }
1810 else
1811 fn->gimple_body = seq;
1812 }
1813
1814
1815 /* Return the body of GIMPLE statements for function FN. After the
1816 CFG pass, the function body doesn't exist anymore because it has
1817 been split up into basic blocks. In this case, it returns
1818 NULL. */
1819
1820 gimple_seq
1821 gimple_body (tree fndecl)
1822 {
1823 struct function *fn = DECL_STRUCT_FUNCTION (fndecl);
1824 return fn ? fn->gimple_body : NULL;
1825 }
1826
1827 /* Return true when FNDECL has Gimple body either in unlowered
1828 or CFG form. */
1829 bool
1830 gimple_has_body_p (tree fndecl)
1831 {
1832 struct function *fn = DECL_STRUCT_FUNCTION (fndecl);
1833 return (gimple_body (fndecl) || (fn && fn->cfg));
1834 }
1835
1836 /* Return true if calls C1 and C2 are known to go to the same function. */
1837
1838 bool
1839 gimple_call_same_target_p (const_gimple c1, const_gimple c2)
1840 {
1841 if (gimple_call_internal_p (c1))
1842 return (gimple_call_internal_p (c2)
1843 && gimple_call_internal_fn (c1) == gimple_call_internal_fn (c2));
1844 else
1845 return (gimple_call_fn (c1) == gimple_call_fn (c2)
1846 || (gimple_call_fndecl (c1)
1847 && gimple_call_fndecl (c1) == gimple_call_fndecl (c2)));
1848 }
1849
1850 /* Detect flags from a GIMPLE_CALL. This is just like
1851 call_expr_flags, but for gimple tuples. */
1852
1853 int
1854 gimple_call_flags (const_gimple stmt)
1855 {
1856 int flags;
1857 tree decl = gimple_call_fndecl (stmt);
1858
1859 if (decl)
1860 flags = flags_from_decl_or_type (decl);
1861 else if (gimple_call_internal_p (stmt))
1862 flags = internal_fn_flags (gimple_call_internal_fn (stmt));
1863 else
1864 flags = flags_from_decl_or_type (gimple_call_fntype (stmt));
1865
1866 if (stmt->gsbase.subcode & GF_CALL_NOTHROW)
1867 flags |= ECF_NOTHROW;
1868
1869 return flags;
1870 }
1871
1872 /* Return the "fn spec" string for call STMT. */
1873
1874 static tree
1875 gimple_call_fnspec (const_gimple stmt)
1876 {
1877 tree type, attr;
1878
1879 type = gimple_call_fntype (stmt);
1880 if (!type)
1881 return NULL_TREE;
1882
1883 attr = lookup_attribute ("fn spec", TYPE_ATTRIBUTES (type));
1884 if (!attr)
1885 return NULL_TREE;
1886
1887 return TREE_VALUE (TREE_VALUE (attr));
1888 }
1889
1890 /* Detects argument flags for argument number ARG on call STMT. */
1891
1892 int
1893 gimple_call_arg_flags (const_gimple stmt, unsigned arg)
1894 {
1895 tree attr = gimple_call_fnspec (stmt);
1896
1897 if (!attr || 1 + arg >= (unsigned) TREE_STRING_LENGTH (attr))
1898 return 0;
1899
1900 switch (TREE_STRING_POINTER (attr)[1 + arg])
1901 {
1902 case 'x':
1903 case 'X':
1904 return EAF_UNUSED;
1905
1906 case 'R':
1907 return EAF_DIRECT | EAF_NOCLOBBER | EAF_NOESCAPE;
1908
1909 case 'r':
1910 return EAF_NOCLOBBER | EAF_NOESCAPE;
1911
1912 case 'W':
1913 return EAF_DIRECT | EAF_NOESCAPE;
1914
1915 case 'w':
1916 return EAF_NOESCAPE;
1917
1918 case '.':
1919 default:
1920 return 0;
1921 }
1922 }
1923
1924 /* Detects return flags for the call STMT. */
1925
1926 int
1927 gimple_call_return_flags (const_gimple stmt)
1928 {
1929 tree attr;
1930
1931 if (gimple_call_flags (stmt) & ECF_MALLOC)
1932 return ERF_NOALIAS;
1933
1934 attr = gimple_call_fnspec (stmt);
1935 if (!attr || TREE_STRING_LENGTH (attr) < 1)
1936 return 0;
1937
1938 switch (TREE_STRING_POINTER (attr)[0])
1939 {
1940 case '1':
1941 case '2':
1942 case '3':
1943 case '4':
1944 return ERF_RETURNS_ARG | (TREE_STRING_POINTER (attr)[0] - '1');
1945
1946 case 'm':
1947 return ERF_NOALIAS;
1948
1949 case '.':
1950 default:
1951 return 0;
1952 }
1953 }
1954
1955
1956 /* Return true if GS is a copy assignment. */
1957
1958 bool
1959 gimple_assign_copy_p (gimple gs)
1960 {
1961 return (gimple_assign_single_p (gs)
1962 && is_gimple_val (gimple_op (gs, 1)));
1963 }
1964
1965
1966 /* Return true if GS is a SSA_NAME copy assignment. */
1967
1968 bool
1969 gimple_assign_ssa_name_copy_p (gimple gs)
1970 {
1971 return (gimple_assign_single_p (gs)
1972 && TREE_CODE (gimple_assign_lhs (gs)) == SSA_NAME
1973 && TREE_CODE (gimple_assign_rhs1 (gs)) == SSA_NAME);
1974 }
1975
1976
1977 /* Return true if GS is an assignment with a unary RHS, but the
1978 operator has no effect on the assigned value. The logic is adapted
1979 from STRIP_NOPS. This predicate is intended to be used in tuplifying
1980 instances in which STRIP_NOPS was previously applied to the RHS of
1981 an assignment.
1982
1983 NOTE: In the use cases that led to the creation of this function
1984 and of gimple_assign_single_p, it is typical to test for either
1985 condition and to proceed in the same manner. In each case, the
1986 assigned value is represented by the single RHS operand of the
1987 assignment. I suspect there may be cases where gimple_assign_copy_p,
1988 gimple_assign_single_p, or equivalent logic is used where a similar
1989 treatment of unary NOPs is appropriate. */
1990
1991 bool
1992 gimple_assign_unary_nop_p (gimple gs)
1993 {
1994 return (is_gimple_assign (gs)
1995 && (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (gs))
1996 || gimple_assign_rhs_code (gs) == NON_LVALUE_EXPR)
1997 && gimple_assign_rhs1 (gs) != error_mark_node
1998 && (TYPE_MODE (TREE_TYPE (gimple_assign_lhs (gs)))
1999 == TYPE_MODE (TREE_TYPE (gimple_assign_rhs1 (gs)))));
2000 }
2001
2002 /* Set BB to be the basic block holding G. */
2003
2004 void
2005 gimple_set_bb (gimple stmt, basic_block bb)
2006 {
2007 stmt->gsbase.bb = bb;
2008
2009 /* If the statement is a label, add the label to block-to-labels map
2010 so that we can speed up edge creation for GIMPLE_GOTOs. */
2011 if (cfun->cfg && gimple_code (stmt) == GIMPLE_LABEL)
2012 {
2013 tree t;
2014 int uid;
2015
2016 t = gimple_label_label (stmt);
2017 uid = LABEL_DECL_UID (t);
2018 if (uid == -1)
2019 {
2020 unsigned old_len = VEC_length (basic_block, label_to_block_map);
2021 LABEL_DECL_UID (t) = uid = cfun->cfg->last_label_uid++;
2022 if (old_len <= (unsigned) uid)
2023 {
2024 unsigned new_len = 3 * uid / 2 + 1;
2025
2026 VEC_safe_grow_cleared (basic_block, gc, label_to_block_map,
2027 new_len);
2028 }
2029 }
2030
2031 VEC_replace (basic_block, label_to_block_map, uid, bb);
2032 }
2033 }
2034
2035
2036 /* Modify the RHS of the assignment pointed-to by GSI using the
2037 operands in the expression tree EXPR.
2038
2039 NOTE: The statement pointed-to by GSI may be reallocated if it
2040 did not have enough operand slots.
2041
2042 This function is useful to convert an existing tree expression into
2043 the flat representation used for the RHS of a GIMPLE assignment.
2044 It will reallocate memory as needed to expand or shrink the number
2045 of operand slots needed to represent EXPR.
2046
2047 NOTE: If you find yourself building a tree and then calling this
2048 function, you are most certainly doing it the slow way. It is much
2049 better to build a new assignment or to use the function
2050 gimple_assign_set_rhs_with_ops, which does not require an
2051 expression tree to be built. */
2052
2053 void
2054 gimple_assign_set_rhs_from_tree (gimple_stmt_iterator *gsi, tree expr)
2055 {
2056 enum tree_code subcode;
2057 tree op1, op2, op3;
2058
2059 extract_ops_from_tree_1 (expr, &subcode, &op1, &op2, &op3);
2060 gimple_assign_set_rhs_with_ops_1 (gsi, subcode, op1, op2, op3);
2061 }
2062
2063
2064 /* Set the RHS of assignment statement pointed-to by GSI to CODE with
2065 operands OP1, OP2 and OP3.
2066
2067 NOTE: The statement pointed-to by GSI may be reallocated if it
2068 did not have enough operand slots. */
2069
2070 void
2071 gimple_assign_set_rhs_with_ops_1 (gimple_stmt_iterator *gsi, enum tree_code code,
2072 tree op1, tree op2, tree op3)
2073 {
2074 unsigned new_rhs_ops = get_gimple_rhs_num_ops (code);
2075 gimple stmt = gsi_stmt (*gsi);
2076
2077 /* If the new CODE needs more operands, allocate a new statement. */
2078 if (gimple_num_ops (stmt) < new_rhs_ops + 1)
2079 {
2080 tree lhs = gimple_assign_lhs (stmt);
2081 gimple new_stmt = gimple_alloc (gimple_code (stmt), new_rhs_ops + 1);
2082 memcpy (new_stmt, stmt, gimple_size (gimple_code (stmt)));
2083 gsi_replace (gsi, new_stmt, true);
2084 stmt = new_stmt;
2085
2086 /* The LHS needs to be reset as this also changes the SSA name
2087 on the LHS. */
2088 gimple_assign_set_lhs (stmt, lhs);
2089 }
2090
2091 gimple_set_num_ops (stmt, new_rhs_ops + 1);
2092 gimple_set_subcode (stmt, code);
2093 gimple_assign_set_rhs1 (stmt, op1);
2094 if (new_rhs_ops > 1)
2095 gimple_assign_set_rhs2 (stmt, op2);
2096 if (new_rhs_ops > 2)
2097 gimple_assign_set_rhs3 (stmt, op3);
2098 }
2099
2100
2101 /* Return the LHS of a statement that performs an assignment,
2102 either a GIMPLE_ASSIGN or a GIMPLE_CALL. Returns NULL_TREE
2103 for a call to a function that returns no value, or for a
2104 statement other than an assignment or a call. */
2105
2106 tree
2107 gimple_get_lhs (const_gimple stmt)
2108 {
2109 enum gimple_code code = gimple_code (stmt);
2110
2111 if (code == GIMPLE_ASSIGN)
2112 return gimple_assign_lhs (stmt);
2113 else if (code == GIMPLE_CALL)
2114 return gimple_call_lhs (stmt);
2115 else
2116 return NULL_TREE;
2117 }
2118
2119
2120 /* Set the LHS of a statement that performs an assignment,
2121 either a GIMPLE_ASSIGN or a GIMPLE_CALL. */
2122
2123 void
2124 gimple_set_lhs (gimple stmt, tree lhs)
2125 {
2126 enum gimple_code code = gimple_code (stmt);
2127
2128 if (code == GIMPLE_ASSIGN)
2129 gimple_assign_set_lhs (stmt, lhs);
2130 else if (code == GIMPLE_CALL)
2131 gimple_call_set_lhs (stmt, lhs);
2132 else
2133 gcc_unreachable();
2134 }
2135
2136 /* Replace the LHS of STMT, an assignment, either a GIMPLE_ASSIGN or a
2137 GIMPLE_CALL, with NLHS, in preparation for modifying the RHS to an
2138 expression with a different value.
2139
2140 This will update any annotations (say debug bind stmts) referring
2141 to the original LHS, so that they use the RHS instead. This is
2142 done even if NLHS and LHS are the same, for it is understood that
2143 the RHS will be modified afterwards, and NLHS will not be assigned
2144 an equivalent value.
2145
2146 Adjusting any non-annotation uses of the LHS, if needed, is a
2147 responsibility of the caller.
2148
2149 The effect of this call should be pretty much the same as that of
2150 inserting a copy of STMT before STMT, and then removing the
2151 original stmt, at which time gsi_remove() would have update
2152 annotations, but using this function saves all the inserting,
2153 copying and removing. */
2154
2155 void
2156 gimple_replace_lhs (gimple stmt, tree nlhs)
2157 {
2158 if (MAY_HAVE_DEBUG_STMTS)
2159 {
2160 tree lhs = gimple_get_lhs (stmt);
2161
2162 gcc_assert (SSA_NAME_DEF_STMT (lhs) == stmt);
2163
2164 insert_debug_temp_for_var_def (NULL, lhs);
2165 }
2166
2167 gimple_set_lhs (stmt, nlhs);
2168 }
2169
2170 /* Return a deep copy of statement STMT. All the operands from STMT
2171 are reallocated and copied using unshare_expr. The DEF, USE, VDEF
2172 and VUSE operand arrays are set to empty in the new copy. */
2173
2174 gimple
2175 gimple_copy (gimple stmt)
2176 {
2177 enum gimple_code code = gimple_code (stmt);
2178 unsigned num_ops = gimple_num_ops (stmt);
2179 gimple copy = gimple_alloc (code, num_ops);
2180 unsigned i;
2181
2182 /* Shallow copy all the fields from STMT. */
2183 memcpy (copy, stmt, gimple_size (code));
2184
2185 /* If STMT has sub-statements, deep-copy them as well. */
2186 if (gimple_has_substatements (stmt))
2187 {
2188 gimple_seq new_seq;
2189 tree t;
2190
2191 switch (gimple_code (stmt))
2192 {
2193 case GIMPLE_BIND:
2194 new_seq = gimple_seq_copy (gimple_bind_body (stmt));
2195 gimple_bind_set_body (copy, new_seq);
2196 gimple_bind_set_vars (copy, unshare_expr (gimple_bind_vars (stmt)));
2197 gimple_bind_set_block (copy, gimple_bind_block (stmt));
2198 break;
2199
2200 case GIMPLE_CATCH:
2201 new_seq = gimple_seq_copy (gimple_catch_handler (stmt));
2202 gimple_catch_set_handler (copy, new_seq);
2203 t = unshare_expr (gimple_catch_types (stmt));
2204 gimple_catch_set_types (copy, t);
2205 break;
2206
2207 case GIMPLE_EH_FILTER:
2208 new_seq = gimple_seq_copy (gimple_eh_filter_failure (stmt));
2209 gimple_eh_filter_set_failure (copy, new_seq);
2210 t = unshare_expr (gimple_eh_filter_types (stmt));
2211 gimple_eh_filter_set_types (copy, t);
2212 break;
2213
2214 case GIMPLE_TRY:
2215 new_seq = gimple_seq_copy (gimple_try_eval (stmt));
2216 gimple_try_set_eval (copy, new_seq);
2217 new_seq = gimple_seq_copy (gimple_try_cleanup (stmt));
2218 gimple_try_set_cleanup (copy, new_seq);
2219 break;
2220
2221 case GIMPLE_OMP_FOR:
2222 new_seq = gimple_seq_copy (gimple_omp_for_pre_body (stmt));
2223 gimple_omp_for_set_pre_body (copy, new_seq);
2224 t = unshare_expr (gimple_omp_for_clauses (stmt));
2225 gimple_omp_for_set_clauses (copy, t);
2226 copy->gimple_omp_for.iter
2227 = ggc_alloc_vec_gimple_omp_for_iter
2228 (gimple_omp_for_collapse (stmt));
2229 for (i = 0; i < gimple_omp_for_collapse (stmt); i++)
2230 {
2231 gimple_omp_for_set_cond (copy, i,
2232 gimple_omp_for_cond (stmt, i));
2233 gimple_omp_for_set_index (copy, i,
2234 gimple_omp_for_index (stmt, i));
2235 t = unshare_expr (gimple_omp_for_initial (stmt, i));
2236 gimple_omp_for_set_initial (copy, i, t);
2237 t = unshare_expr (gimple_omp_for_final (stmt, i));
2238 gimple_omp_for_set_final (copy, i, t);
2239 t = unshare_expr (gimple_omp_for_incr (stmt, i));
2240 gimple_omp_for_set_incr (copy, i, t);
2241 }
2242 goto copy_omp_body;
2243
2244 case GIMPLE_OMP_PARALLEL:
2245 t = unshare_expr (gimple_omp_parallel_clauses (stmt));
2246 gimple_omp_parallel_set_clauses (copy, t);
2247 t = unshare_expr (gimple_omp_parallel_child_fn (stmt));
2248 gimple_omp_parallel_set_child_fn (copy, t);
2249 t = unshare_expr (gimple_omp_parallel_data_arg (stmt));
2250 gimple_omp_parallel_set_data_arg (copy, t);
2251 goto copy_omp_body;
2252
2253 case GIMPLE_OMP_TASK:
2254 t = unshare_expr (gimple_omp_task_clauses (stmt));
2255 gimple_omp_task_set_clauses (copy, t);
2256 t = unshare_expr (gimple_omp_task_child_fn (stmt));
2257 gimple_omp_task_set_child_fn (copy, t);
2258 t = unshare_expr (gimple_omp_task_data_arg (stmt));
2259 gimple_omp_task_set_data_arg (copy, t);
2260 t = unshare_expr (gimple_omp_task_copy_fn (stmt));
2261 gimple_omp_task_set_copy_fn (copy, t);
2262 t = unshare_expr (gimple_omp_task_arg_size (stmt));
2263 gimple_omp_task_set_arg_size (copy, t);
2264 t = unshare_expr (gimple_omp_task_arg_align (stmt));
2265 gimple_omp_task_set_arg_align (copy, t);
2266 goto copy_omp_body;
2267
2268 case GIMPLE_OMP_CRITICAL:
2269 t = unshare_expr (gimple_omp_critical_name (stmt));
2270 gimple_omp_critical_set_name (copy, t);
2271 goto copy_omp_body;
2272
2273 case GIMPLE_OMP_SECTIONS:
2274 t = unshare_expr (gimple_omp_sections_clauses (stmt));
2275 gimple_omp_sections_set_clauses (copy, t);
2276 t = unshare_expr (gimple_omp_sections_control (stmt));
2277 gimple_omp_sections_set_control (copy, t);
2278 /* FALLTHRU */
2279
2280 case GIMPLE_OMP_SINGLE:
2281 case GIMPLE_OMP_SECTION:
2282 case GIMPLE_OMP_MASTER:
2283 case GIMPLE_OMP_ORDERED:
2284 copy_omp_body:
2285 new_seq = gimple_seq_copy (gimple_omp_body (stmt));
2286 gimple_omp_set_body (copy, new_seq);
2287 break;
2288
2289 case GIMPLE_WITH_CLEANUP_EXPR:
2290 new_seq = gimple_seq_copy (gimple_wce_cleanup (stmt));
2291 gimple_wce_set_cleanup (copy, new_seq);
2292 break;
2293
2294 default:
2295 gcc_unreachable ();
2296 }
2297 }
2298
2299 /* Make copy of operands. */
2300 if (num_ops > 0)
2301 {
2302 for (i = 0; i < num_ops; i++)
2303 gimple_set_op (copy, i, unshare_expr (gimple_op (stmt, i)));
2304
2305 /* Clear out SSA operand vectors on COPY. */
2306 if (gimple_has_ops (stmt))
2307 {
2308 gimple_set_def_ops (copy, NULL);
2309 gimple_set_use_ops (copy, NULL);
2310 }
2311
2312 if (gimple_has_mem_ops (stmt))
2313 {
2314 gimple_set_vdef (copy, gimple_vdef (stmt));
2315 gimple_set_vuse (copy, gimple_vuse (stmt));
2316 }
2317
2318 /* SSA operands need to be updated. */
2319 gimple_set_modified (copy, true);
2320 }
2321
2322 return copy;
2323 }
2324
2325
2326 /* Set the MODIFIED flag to MODIFIEDP, iff the gimple statement G has
2327 a MODIFIED field. */
2328
2329 void
2330 gimple_set_modified (gimple s, bool modifiedp)
2331 {
2332 if (gimple_has_ops (s))
2333 s->gsbase.modified = (unsigned) modifiedp;
2334 }
2335
2336
2337 /* Return true if statement S has side-effects. We consider a
2338 statement to have side effects if:
2339
2340 - It is a GIMPLE_CALL not marked with ECF_PURE or ECF_CONST.
2341 - Any of its operands are marked TREE_THIS_VOLATILE or TREE_SIDE_EFFECTS. */
2342
2343 bool
2344 gimple_has_side_effects (const_gimple s)
2345 {
2346 unsigned i;
2347
2348 if (is_gimple_debug (s))
2349 return false;
2350
2351 /* We don't have to scan the arguments to check for
2352 volatile arguments, though, at present, we still
2353 do a scan to check for TREE_SIDE_EFFECTS. */
2354 if (gimple_has_volatile_ops (s))
2355 return true;
2356
2357 if (gimple_code (s) == GIMPLE_ASM
2358 && gimple_asm_volatile_p (s))
2359 return true;
2360
2361 if (is_gimple_call (s))
2362 {
2363 unsigned nargs = gimple_call_num_args (s);
2364 tree fn;
2365
2366 if (!(gimple_call_flags (s) & (ECF_CONST | ECF_PURE)))
2367 return true;
2368 else if (gimple_call_flags (s) & ECF_LOOPING_CONST_OR_PURE)
2369 /* An infinite loop is considered a side effect. */
2370 return true;
2371
2372 if (gimple_call_lhs (s)
2373 && TREE_SIDE_EFFECTS (gimple_call_lhs (s)))
2374 {
2375 gcc_checking_assert (gimple_has_volatile_ops (s));
2376 return true;
2377 }
2378
2379 fn = gimple_call_fn (s);
2380 if (fn && TREE_SIDE_EFFECTS (fn))
2381 return true;
2382
2383 for (i = 0; i < nargs; i++)
2384 if (TREE_SIDE_EFFECTS (gimple_call_arg (s, i)))
2385 {
2386 gcc_checking_assert (gimple_has_volatile_ops (s));
2387 return true;
2388 }
2389
2390 return false;
2391 }
2392 else
2393 {
2394 for (i = 0; i < gimple_num_ops (s); i++)
2395 {
2396 tree op = gimple_op (s, i);
2397 if (op && TREE_SIDE_EFFECTS (op))
2398 {
2399 gcc_checking_assert (gimple_has_volatile_ops (s));
2400 return true;
2401 }
2402 }
2403 }
2404
2405 return false;
2406 }
2407
2408 /* Return true if the RHS of statement S has side effects.
2409 We may use it to determine if it is admissable to replace
2410 an assignment or call with a copy of a previously-computed
2411 value. In such cases, side-effects due to the LHS are
2412 preserved. */
2413
2414 bool
2415 gimple_rhs_has_side_effects (const_gimple s)
2416 {
2417 unsigned i;
2418
2419 if (is_gimple_call (s))
2420 {
2421 unsigned nargs = gimple_call_num_args (s);
2422 tree fn;
2423
2424 if (!(gimple_call_flags (s) & (ECF_CONST | ECF_PURE)))
2425 return true;
2426
2427 /* We cannot use gimple_has_volatile_ops here,
2428 because we must ignore a volatile LHS. */
2429 fn = gimple_call_fn (s);
2430 if (fn && (TREE_SIDE_EFFECTS (fn) || TREE_THIS_VOLATILE (fn)))
2431 {
2432 gcc_assert (gimple_has_volatile_ops (s));
2433 return true;
2434 }
2435
2436 for (i = 0; i < nargs; i++)
2437 if (TREE_SIDE_EFFECTS (gimple_call_arg (s, i))
2438 || TREE_THIS_VOLATILE (gimple_call_arg (s, i)))
2439 return true;
2440
2441 return false;
2442 }
2443 else if (is_gimple_assign (s))
2444 {
2445 /* Skip the first operand, the LHS. */
2446 for (i = 1; i < gimple_num_ops (s); i++)
2447 if (TREE_SIDE_EFFECTS (gimple_op (s, i))
2448 || TREE_THIS_VOLATILE (gimple_op (s, i)))
2449 {
2450 gcc_assert (gimple_has_volatile_ops (s));
2451 return true;
2452 }
2453 }
2454 else if (is_gimple_debug (s))
2455 return false;
2456 else
2457 {
2458 /* For statements without an LHS, examine all arguments. */
2459 for (i = 0; i < gimple_num_ops (s); i++)
2460 if (TREE_SIDE_EFFECTS (gimple_op (s, i))
2461 || TREE_THIS_VOLATILE (gimple_op (s, i)))
2462 {
2463 gcc_assert (gimple_has_volatile_ops (s));
2464 return true;
2465 }
2466 }
2467
2468 return false;
2469 }
2470
2471 /* Helper for gimple_could_trap_p and gimple_assign_rhs_could_trap_p.
2472 Return true if S can trap. When INCLUDE_MEM is true, check whether
2473 the memory operations could trap. When INCLUDE_STORES is true and
2474 S is a GIMPLE_ASSIGN, the LHS of the assignment is also checked. */
2475
2476 bool
2477 gimple_could_trap_p_1 (gimple s, bool include_mem, bool include_stores)
2478 {
2479 tree t, div = NULL_TREE;
2480 enum tree_code op;
2481
2482 if (include_mem)
2483 {
2484 unsigned i, start = (is_gimple_assign (s) && !include_stores) ? 1 : 0;
2485
2486 for (i = start; i < gimple_num_ops (s); i++)
2487 if (tree_could_trap_p (gimple_op (s, i)))
2488 return true;
2489 }
2490
2491 switch (gimple_code (s))
2492 {
2493 case GIMPLE_ASM:
2494 return gimple_asm_volatile_p (s);
2495
2496 case GIMPLE_CALL:
2497 t = gimple_call_fndecl (s);
2498 /* Assume that calls to weak functions may trap. */
2499 if (!t || !DECL_P (t) || DECL_WEAK (t))
2500 return true;
2501 return false;
2502
2503 case GIMPLE_ASSIGN:
2504 t = gimple_expr_type (s);
2505 op = gimple_assign_rhs_code (s);
2506 if (get_gimple_rhs_class (op) == GIMPLE_BINARY_RHS)
2507 div = gimple_assign_rhs2 (s);
2508 return (operation_could_trap_p (op, FLOAT_TYPE_P (t),
2509 (INTEGRAL_TYPE_P (t)
2510 && TYPE_OVERFLOW_TRAPS (t)),
2511 div));
2512
2513 default:
2514 break;
2515 }
2516
2517 return false;
2518 }
2519
2520 /* Return true if statement S can trap. */
2521
2522 bool
2523 gimple_could_trap_p (gimple s)
2524 {
2525 return gimple_could_trap_p_1 (s, true, true);
2526 }
2527
2528 /* Return true if RHS of a GIMPLE_ASSIGN S can trap. */
2529
2530 bool
2531 gimple_assign_rhs_could_trap_p (gimple s)
2532 {
2533 gcc_assert (is_gimple_assign (s));
2534 return gimple_could_trap_p_1 (s, true, false);
2535 }
2536
2537
2538 /* Print debugging information for gimple stmts generated. */
2539
2540 void
2541 dump_gimple_statistics (void)
2542 {
2543 #ifdef GATHER_STATISTICS
2544 int i, total_tuples = 0, total_bytes = 0;
2545
2546 fprintf (stderr, "\nGIMPLE statements\n");
2547 fprintf (stderr, "Kind Stmts Bytes\n");
2548 fprintf (stderr, "---------------------------------------\n");
2549 for (i = 0; i < (int) gimple_alloc_kind_all; ++i)
2550 {
2551 fprintf (stderr, "%-20s %7d %10d\n", gimple_alloc_kind_names[i],
2552 gimple_alloc_counts[i], gimple_alloc_sizes[i]);
2553 total_tuples += gimple_alloc_counts[i];
2554 total_bytes += gimple_alloc_sizes[i];
2555 }
2556 fprintf (stderr, "---------------------------------------\n");
2557 fprintf (stderr, "%-20s %7d %10d\n", "Total", total_tuples, total_bytes);
2558 fprintf (stderr, "---------------------------------------\n");
2559 #else
2560 fprintf (stderr, "No gimple statistics\n");
2561 #endif
2562 }
2563
2564
2565 /* Return the number of operands needed on the RHS of a GIMPLE
2566 assignment for an expression with tree code CODE. */
2567
2568 unsigned
2569 get_gimple_rhs_num_ops (enum tree_code code)
2570 {
2571 enum gimple_rhs_class rhs_class = get_gimple_rhs_class (code);
2572
2573 if (rhs_class == GIMPLE_UNARY_RHS || rhs_class == GIMPLE_SINGLE_RHS)
2574 return 1;
2575 else if (rhs_class == GIMPLE_BINARY_RHS)
2576 return 2;
2577 else if (rhs_class == GIMPLE_TERNARY_RHS)
2578 return 3;
2579 else
2580 gcc_unreachable ();
2581 }
2582
2583 #define DEFTREECODE(SYM, STRING, TYPE, NARGS) \
2584 (unsigned char) \
2585 ((TYPE) == tcc_unary ? GIMPLE_UNARY_RHS \
2586 : ((TYPE) == tcc_binary \
2587 || (TYPE) == tcc_comparison) ? GIMPLE_BINARY_RHS \
2588 : ((TYPE) == tcc_constant \
2589 || (TYPE) == tcc_declaration \
2590 || (TYPE) == tcc_reference) ? GIMPLE_SINGLE_RHS \
2591 : ((SYM) == TRUTH_AND_EXPR \
2592 || (SYM) == TRUTH_OR_EXPR \
2593 || (SYM) == TRUTH_XOR_EXPR) ? GIMPLE_BINARY_RHS \
2594 : (SYM) == TRUTH_NOT_EXPR ? GIMPLE_UNARY_RHS \
2595 : ((SYM) == WIDEN_MULT_PLUS_EXPR \
2596 || (SYM) == WIDEN_MULT_MINUS_EXPR \
2597 || (SYM) == DOT_PROD_EXPR \
2598 || (SYM) == REALIGN_LOAD_EXPR \
2599 || (SYM) == FMA_EXPR) ? GIMPLE_TERNARY_RHS \
2600 : ((SYM) == COND_EXPR \
2601 || (SYM) == CONSTRUCTOR \
2602 || (SYM) == OBJ_TYPE_REF \
2603 || (SYM) == ASSERT_EXPR \
2604 || (SYM) == ADDR_EXPR \
2605 || (SYM) == WITH_SIZE_EXPR \
2606 || (SYM) == SSA_NAME \
2607 || (SYM) == VEC_COND_EXPR) ? GIMPLE_SINGLE_RHS \
2608 : GIMPLE_INVALID_RHS),
2609 #define END_OF_BASE_TREE_CODES (unsigned char) GIMPLE_INVALID_RHS,
2610
2611 const unsigned char gimple_rhs_class_table[] = {
2612 #include "all-tree.def"
2613 };
2614
2615 #undef DEFTREECODE
2616 #undef END_OF_BASE_TREE_CODES
2617
2618 /* For the definitive definition of GIMPLE, see doc/tree-ssa.texi. */
2619
2620 /* Validation of GIMPLE expressions. */
2621
2622 /* Returns true iff T is a valid RHS for an assignment to a renamed
2623 user -- or front-end generated artificial -- variable. */
2624
2625 bool
2626 is_gimple_reg_rhs (tree t)
2627 {
2628 return get_gimple_rhs_class (TREE_CODE (t)) != GIMPLE_INVALID_RHS;
2629 }
2630
2631 /* Returns true iff T is a valid RHS for an assignment to an un-renamed
2632 LHS, or for a call argument. */
2633
2634 bool
2635 is_gimple_mem_rhs (tree t)
2636 {
2637 /* If we're dealing with a renamable type, either source or dest must be
2638 a renamed variable. */
2639 if (is_gimple_reg_type (TREE_TYPE (t)))
2640 return is_gimple_val (t);
2641 else
2642 return is_gimple_val (t) || is_gimple_lvalue (t);
2643 }
2644
2645 /* Return true if T is a valid LHS for a GIMPLE assignment expression. */
2646
2647 bool
2648 is_gimple_lvalue (tree t)
2649 {
2650 return (is_gimple_addressable (t)
2651 || TREE_CODE (t) == WITH_SIZE_EXPR
2652 /* These are complex lvalues, but don't have addresses, so they
2653 go here. */
2654 || TREE_CODE (t) == BIT_FIELD_REF);
2655 }
2656
2657 /* Return true if T is a GIMPLE condition. */
2658
2659 bool
2660 is_gimple_condexpr (tree t)
2661 {
2662 return (is_gimple_val (t) || (COMPARISON_CLASS_P (t)
2663 && !tree_could_throw_p (t)
2664 && is_gimple_val (TREE_OPERAND (t, 0))
2665 && is_gimple_val (TREE_OPERAND (t, 1))));
2666 }
2667
2668 /* Return true if T is something whose address can be taken. */
2669
2670 bool
2671 is_gimple_addressable (tree t)
2672 {
2673 return (is_gimple_id (t) || handled_component_p (t)
2674 || TREE_CODE (t) == MEM_REF);
2675 }
2676
2677 /* Return true if T is a valid gimple constant. */
2678
2679 bool
2680 is_gimple_constant (const_tree t)
2681 {
2682 switch (TREE_CODE (t))
2683 {
2684 case INTEGER_CST:
2685 case REAL_CST:
2686 case FIXED_CST:
2687 case STRING_CST:
2688 case COMPLEX_CST:
2689 case VECTOR_CST:
2690 return true;
2691
2692 /* Vector constant constructors are gimple invariant. */
2693 case CONSTRUCTOR:
2694 if (TREE_TYPE (t) && TREE_CODE (TREE_TYPE (t)) == VECTOR_TYPE)
2695 return TREE_CONSTANT (t);
2696 else
2697 return false;
2698
2699 default:
2700 return false;
2701 }
2702 }
2703
2704 /* Return true if T is a gimple address. */
2705
2706 bool
2707 is_gimple_address (const_tree t)
2708 {
2709 tree op;
2710
2711 if (TREE_CODE (t) != ADDR_EXPR)
2712 return false;
2713
2714 op = TREE_OPERAND (t, 0);
2715 while (handled_component_p (op))
2716 {
2717 if ((TREE_CODE (op) == ARRAY_REF
2718 || TREE_CODE (op) == ARRAY_RANGE_REF)
2719 && !is_gimple_val (TREE_OPERAND (op, 1)))
2720 return false;
2721
2722 op = TREE_OPERAND (op, 0);
2723 }
2724
2725 if (CONSTANT_CLASS_P (op) || TREE_CODE (op) == MEM_REF)
2726 return true;
2727
2728 switch (TREE_CODE (op))
2729 {
2730 case PARM_DECL:
2731 case RESULT_DECL:
2732 case LABEL_DECL:
2733 case FUNCTION_DECL:
2734 case VAR_DECL:
2735 case CONST_DECL:
2736 return true;
2737
2738 default:
2739 return false;
2740 }
2741 }
2742
2743 /* Strip out all handled components that produce invariant
2744 offsets. */
2745
2746 static const_tree
2747 strip_invariant_refs (const_tree op)
2748 {
2749 while (handled_component_p (op))
2750 {
2751 switch (TREE_CODE (op))
2752 {
2753 case ARRAY_REF:
2754 case ARRAY_RANGE_REF:
2755 if (!is_gimple_constant (TREE_OPERAND (op, 1))
2756 || TREE_OPERAND (op, 2) != NULL_TREE
2757 || TREE_OPERAND (op, 3) != NULL_TREE)
2758 return NULL;
2759 break;
2760
2761 case COMPONENT_REF:
2762 if (TREE_OPERAND (op, 2) != NULL_TREE)
2763 return NULL;
2764 break;
2765
2766 default:;
2767 }
2768 op = TREE_OPERAND (op, 0);
2769 }
2770
2771 return op;
2772 }
2773
2774 /* Return true if T is a gimple invariant address. */
2775
2776 bool
2777 is_gimple_invariant_address (const_tree t)
2778 {
2779 const_tree op;
2780
2781 if (TREE_CODE (t) != ADDR_EXPR)
2782 return false;
2783
2784 op = strip_invariant_refs (TREE_OPERAND (t, 0));
2785 if (!op)
2786 return false;
2787
2788 if (TREE_CODE (op) == MEM_REF)
2789 {
2790 const_tree op0 = TREE_OPERAND (op, 0);
2791 return (TREE_CODE (op0) == ADDR_EXPR
2792 && (CONSTANT_CLASS_P (TREE_OPERAND (op0, 0))
2793 || decl_address_invariant_p (TREE_OPERAND (op0, 0))));
2794 }
2795
2796 return CONSTANT_CLASS_P (op) || decl_address_invariant_p (op);
2797 }
2798
2799 /* Return true if T is a gimple invariant address at IPA level
2800 (so addresses of variables on stack are not allowed). */
2801
2802 bool
2803 is_gimple_ip_invariant_address (const_tree t)
2804 {
2805 const_tree op;
2806
2807 if (TREE_CODE (t) != ADDR_EXPR)
2808 return false;
2809
2810 op = strip_invariant_refs (TREE_OPERAND (t, 0));
2811
2812 return op && (CONSTANT_CLASS_P (op) || decl_address_ip_invariant_p (op));
2813 }
2814
2815 /* Return true if T is a GIMPLE minimal invariant. It's a restricted
2816 form of function invariant. */
2817
2818 bool
2819 is_gimple_min_invariant (const_tree t)
2820 {
2821 if (TREE_CODE (t) == ADDR_EXPR)
2822 return is_gimple_invariant_address (t);
2823
2824 return is_gimple_constant (t);
2825 }
2826
2827 /* Return true if T is a GIMPLE interprocedural invariant. It's a restricted
2828 form of gimple minimal invariant. */
2829
2830 bool
2831 is_gimple_ip_invariant (const_tree t)
2832 {
2833 if (TREE_CODE (t) == ADDR_EXPR)
2834 return is_gimple_ip_invariant_address (t);
2835
2836 return is_gimple_constant (t);
2837 }
2838
2839 /* Return true if T looks like a valid GIMPLE statement. */
2840
2841 bool
2842 is_gimple_stmt (tree t)
2843 {
2844 const enum tree_code code = TREE_CODE (t);
2845
2846 switch (code)
2847 {
2848 case NOP_EXPR:
2849 /* The only valid NOP_EXPR is the empty statement. */
2850 return IS_EMPTY_STMT (t);
2851
2852 case BIND_EXPR:
2853 case COND_EXPR:
2854 /* These are only valid if they're void. */
2855 return TREE_TYPE (t) == NULL || VOID_TYPE_P (TREE_TYPE (t));
2856
2857 case SWITCH_EXPR:
2858 case GOTO_EXPR:
2859 case RETURN_EXPR:
2860 case LABEL_EXPR:
2861 case CASE_LABEL_EXPR:
2862 case TRY_CATCH_EXPR:
2863 case TRY_FINALLY_EXPR:
2864 case EH_FILTER_EXPR:
2865 case CATCH_EXPR:
2866 case ASM_EXPR:
2867 case STATEMENT_LIST:
2868 case OMP_PARALLEL:
2869 case OMP_FOR:
2870 case OMP_SECTIONS:
2871 case OMP_SECTION:
2872 case OMP_SINGLE:
2873 case OMP_MASTER:
2874 case OMP_ORDERED:
2875 case OMP_CRITICAL:
2876 case OMP_TASK:
2877 /* These are always void. */
2878 return true;
2879
2880 case CALL_EXPR:
2881 case MODIFY_EXPR:
2882 case PREDICT_EXPR:
2883 /* These are valid regardless of their type. */
2884 return true;
2885
2886 default:
2887 return false;
2888 }
2889 }
2890
2891 /* Return true if T is a variable. */
2892
2893 bool
2894 is_gimple_variable (tree t)
2895 {
2896 return (TREE_CODE (t) == VAR_DECL
2897 || TREE_CODE (t) == PARM_DECL
2898 || TREE_CODE (t) == RESULT_DECL
2899 || TREE_CODE (t) == SSA_NAME);
2900 }
2901
2902 /* Return true if T is a GIMPLE identifier (something with an address). */
2903
2904 bool
2905 is_gimple_id (tree t)
2906 {
2907 return (is_gimple_variable (t)
2908 || TREE_CODE (t) == FUNCTION_DECL
2909 || TREE_CODE (t) == LABEL_DECL
2910 || TREE_CODE (t) == CONST_DECL
2911 /* Allow string constants, since they are addressable. */
2912 || TREE_CODE (t) == STRING_CST);
2913 }
2914
2915 /* Return true if TYPE is a suitable type for a scalar register variable. */
2916
2917 bool
2918 is_gimple_reg_type (tree type)
2919 {
2920 return !AGGREGATE_TYPE_P (type);
2921 }
2922
2923 /* Return true if T is a non-aggregate register variable. */
2924
2925 bool
2926 is_gimple_reg (tree t)
2927 {
2928 if (TREE_CODE (t) == SSA_NAME)
2929 t = SSA_NAME_VAR (t);
2930
2931 if (!is_gimple_variable (t))
2932 return false;
2933
2934 if (!is_gimple_reg_type (TREE_TYPE (t)))
2935 return false;
2936
2937 /* A volatile decl is not acceptable because we can't reuse it as
2938 needed. We need to copy it into a temp first. */
2939 if (TREE_THIS_VOLATILE (t))
2940 return false;
2941
2942 /* We define "registers" as things that can be renamed as needed,
2943 which with our infrastructure does not apply to memory. */
2944 if (needs_to_live_in_memory (t))
2945 return false;
2946
2947 /* Hard register variables are an interesting case. For those that
2948 are call-clobbered, we don't know where all the calls are, since
2949 we don't (want to) take into account which operations will turn
2950 into libcalls at the rtl level. For those that are call-saved,
2951 we don't currently model the fact that calls may in fact change
2952 global hard registers, nor do we examine ASM_CLOBBERS at the tree
2953 level, and so miss variable changes that might imply. All around,
2954 it seems safest to not do too much optimization with these at the
2955 tree level at all. We'll have to rely on the rtl optimizers to
2956 clean this up, as there we've got all the appropriate bits exposed. */
2957 if (TREE_CODE (t) == VAR_DECL && DECL_HARD_REGISTER (t))
2958 return false;
2959
2960 /* Complex and vector values must have been put into SSA-like form.
2961 That is, no assignments to the individual components. */
2962 if (TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE
2963 || TREE_CODE (TREE_TYPE (t)) == VECTOR_TYPE)
2964 return DECL_GIMPLE_REG_P (t);
2965
2966 return true;
2967 }
2968
2969
2970 /* Return true if T is a GIMPLE variable whose address is not needed. */
2971
2972 bool
2973 is_gimple_non_addressable (tree t)
2974 {
2975 if (TREE_CODE (t) == SSA_NAME)
2976 t = SSA_NAME_VAR (t);
2977
2978 return (is_gimple_variable (t) && ! needs_to_live_in_memory (t));
2979 }
2980
2981 /* Return true if T is a GIMPLE rvalue, i.e. an identifier or a constant. */
2982
2983 bool
2984 is_gimple_val (tree t)
2985 {
2986 /* Make loads from volatiles and memory vars explicit. */
2987 if (is_gimple_variable (t)
2988 && is_gimple_reg_type (TREE_TYPE (t))
2989 && !is_gimple_reg (t))
2990 return false;
2991
2992 return (is_gimple_variable (t) || is_gimple_min_invariant (t));
2993 }
2994
2995 /* Similarly, but accept hard registers as inputs to asm statements. */
2996
2997 bool
2998 is_gimple_asm_val (tree t)
2999 {
3000 if (TREE_CODE (t) == VAR_DECL && DECL_HARD_REGISTER (t))
3001 return true;
3002
3003 return is_gimple_val (t);
3004 }
3005
3006 /* Return true if T is a GIMPLE minimal lvalue. */
3007
3008 bool
3009 is_gimple_min_lval (tree t)
3010 {
3011 if (!(t = CONST_CAST_TREE (strip_invariant_refs (t))))
3012 return false;
3013 return (is_gimple_id (t) || TREE_CODE (t) == MEM_REF);
3014 }
3015
3016 /* Return true if T is a valid function operand of a CALL_EXPR. */
3017
3018 bool
3019 is_gimple_call_addr (tree t)
3020 {
3021 return (TREE_CODE (t) == OBJ_TYPE_REF || is_gimple_val (t));
3022 }
3023
3024 /* Return true if T is a valid address operand of a MEM_REF. */
3025
3026 bool
3027 is_gimple_mem_ref_addr (tree t)
3028 {
3029 return (is_gimple_reg (t)
3030 || TREE_CODE (t) == INTEGER_CST
3031 || (TREE_CODE (t) == ADDR_EXPR
3032 && (CONSTANT_CLASS_P (TREE_OPERAND (t, 0))
3033 || decl_address_invariant_p (TREE_OPERAND (t, 0)))));
3034 }
3035
3036 /* If T makes a function call, return the corresponding CALL_EXPR operand.
3037 Otherwise, return NULL_TREE. */
3038
3039 tree
3040 get_call_expr_in (tree t)
3041 {
3042 if (TREE_CODE (t) == MODIFY_EXPR)
3043 t = TREE_OPERAND (t, 1);
3044 if (TREE_CODE (t) == WITH_SIZE_EXPR)
3045 t = TREE_OPERAND (t, 0);
3046 if (TREE_CODE (t) == CALL_EXPR)
3047 return t;
3048 return NULL_TREE;
3049 }
3050
3051
3052 /* Given a memory reference expression T, return its base address.
3053 The base address of a memory reference expression is the main
3054 object being referenced. For instance, the base address for
3055 'array[i].fld[j]' is 'array'. You can think of this as stripping
3056 away the offset part from a memory address.
3057
3058 This function calls handled_component_p to strip away all the inner
3059 parts of the memory reference until it reaches the base object. */
3060
3061 tree
3062 get_base_address (tree t)
3063 {
3064 while (handled_component_p (t))
3065 t = TREE_OPERAND (t, 0);
3066
3067 if ((TREE_CODE (t) == MEM_REF
3068 || TREE_CODE (t) == TARGET_MEM_REF)
3069 && TREE_CODE (TREE_OPERAND (t, 0)) == ADDR_EXPR)
3070 t = TREE_OPERAND (TREE_OPERAND (t, 0), 0);
3071
3072 if (TREE_CODE (t) == SSA_NAME
3073 || DECL_P (t)
3074 || TREE_CODE (t) == STRING_CST
3075 || TREE_CODE (t) == CONSTRUCTOR
3076 || INDIRECT_REF_P (t)
3077 || TREE_CODE (t) == MEM_REF
3078 || TREE_CODE (t) == TARGET_MEM_REF)
3079 return t;
3080 else
3081 return NULL_TREE;
3082 }
3083
3084 void
3085 recalculate_side_effects (tree t)
3086 {
3087 enum tree_code code = TREE_CODE (t);
3088 int len = TREE_OPERAND_LENGTH (t);
3089 int i;
3090
3091 switch (TREE_CODE_CLASS (code))
3092 {
3093 case tcc_expression:
3094 switch (code)
3095 {
3096 case INIT_EXPR:
3097 case MODIFY_EXPR:
3098 case VA_ARG_EXPR:
3099 case PREDECREMENT_EXPR:
3100 case PREINCREMENT_EXPR:
3101 case POSTDECREMENT_EXPR:
3102 case POSTINCREMENT_EXPR:
3103 /* All of these have side-effects, no matter what their
3104 operands are. */
3105 return;
3106
3107 default:
3108 break;
3109 }
3110 /* Fall through. */
3111
3112 case tcc_comparison: /* a comparison expression */
3113 case tcc_unary: /* a unary arithmetic expression */
3114 case tcc_binary: /* a binary arithmetic expression */
3115 case tcc_reference: /* a reference */
3116 case tcc_vl_exp: /* a function call */
3117 TREE_SIDE_EFFECTS (t) = TREE_THIS_VOLATILE (t);
3118 for (i = 0; i < len; ++i)
3119 {
3120 tree op = TREE_OPERAND (t, i);
3121 if (op && TREE_SIDE_EFFECTS (op))
3122 TREE_SIDE_EFFECTS (t) = 1;
3123 }
3124 break;
3125
3126 case tcc_constant:
3127 /* No side-effects. */
3128 return;
3129
3130 default:
3131 gcc_unreachable ();
3132 }
3133 }
3134
3135 /* Canonicalize a tree T for use in a COND_EXPR as conditional. Returns
3136 a canonicalized tree that is valid for a COND_EXPR or NULL_TREE, if
3137 we failed to create one. */
3138
3139 tree
3140 canonicalize_cond_expr_cond (tree t)
3141 {
3142 /* Strip conversions around boolean operations. */
3143 if (CONVERT_EXPR_P (t)
3144 && truth_value_p (TREE_CODE (TREE_OPERAND (t, 0))))
3145 t = TREE_OPERAND (t, 0);
3146
3147 /* For (bool)x use x != 0. */
3148 if (CONVERT_EXPR_P (t)
3149 && TREE_CODE (TREE_TYPE (t)) == BOOLEAN_TYPE)
3150 {
3151 tree top0 = TREE_OPERAND (t, 0);
3152 t = build2 (NE_EXPR, TREE_TYPE (t),
3153 top0, build_int_cst (TREE_TYPE (top0), 0));
3154 }
3155 /* For !x use x == 0. */
3156 else if (TREE_CODE (t) == TRUTH_NOT_EXPR)
3157 {
3158 tree top0 = TREE_OPERAND (t, 0);
3159 t = build2 (EQ_EXPR, TREE_TYPE (t),
3160 top0, build_int_cst (TREE_TYPE (top0), 0));
3161 }
3162 /* For cmp ? 1 : 0 use cmp. */
3163 else if (TREE_CODE (t) == COND_EXPR
3164 && COMPARISON_CLASS_P (TREE_OPERAND (t, 0))
3165 && integer_onep (TREE_OPERAND (t, 1))
3166 && integer_zerop (TREE_OPERAND (t, 2)))
3167 {
3168 tree top0 = TREE_OPERAND (t, 0);
3169 t = build2 (TREE_CODE (top0), TREE_TYPE (t),
3170 TREE_OPERAND (top0, 0), TREE_OPERAND (top0, 1));
3171 }
3172
3173 if (is_gimple_condexpr (t))
3174 return t;
3175
3176 return NULL_TREE;
3177 }
3178
3179 /* Build a GIMPLE_CALL identical to STMT but skipping the arguments in
3180 the positions marked by the set ARGS_TO_SKIP. */
3181
3182 gimple
3183 gimple_call_copy_skip_args (gimple stmt, bitmap args_to_skip)
3184 {
3185 int i;
3186 int nargs = gimple_call_num_args (stmt);
3187 VEC(tree, heap) *vargs = VEC_alloc (tree, heap, nargs);
3188 gimple new_stmt;
3189
3190 for (i = 0; i < nargs; i++)
3191 if (!bitmap_bit_p (args_to_skip, i))
3192 VEC_quick_push (tree, vargs, gimple_call_arg (stmt, i));
3193
3194 if (gimple_call_internal_p (stmt))
3195 new_stmt = gimple_build_call_internal_vec (gimple_call_internal_fn (stmt),
3196 vargs);
3197 else
3198 new_stmt = gimple_build_call_vec (gimple_call_fn (stmt), vargs);
3199 VEC_free (tree, heap, vargs);
3200 if (gimple_call_lhs (stmt))
3201 gimple_call_set_lhs (new_stmt, gimple_call_lhs (stmt));
3202
3203 gimple_set_vuse (new_stmt, gimple_vuse (stmt));
3204 gimple_set_vdef (new_stmt, gimple_vdef (stmt));
3205
3206 gimple_set_block (new_stmt, gimple_block (stmt));
3207 if (gimple_has_location (stmt))
3208 gimple_set_location (new_stmt, gimple_location (stmt));
3209 gimple_call_copy_flags (new_stmt, stmt);
3210 gimple_call_set_chain (new_stmt, gimple_call_chain (stmt));
3211
3212 gimple_set_modified (new_stmt, true);
3213
3214 return new_stmt;
3215 }
3216
3217
3218 enum gtc_mode { GTC_MERGE = 0, GTC_DIAG = 1 };
3219
3220 static hashval_t gimple_type_hash (const void *);
3221
3222 /* Structure used to maintain a cache of some type pairs compared by
3223 gimple_types_compatible_p when comparing aggregate types. There are
3224 three possible values for SAME_P:
3225
3226 -2: The pair (T1, T2) has just been inserted in the table.
3227 0: T1 and T2 are different types.
3228 1: T1 and T2 are the same type.
3229
3230 The two elements in the SAME_P array are indexed by the comparison
3231 mode gtc_mode. */
3232
3233 struct type_pair_d
3234 {
3235 unsigned int uid1;
3236 unsigned int uid2;
3237 signed char same_p[2];
3238 };
3239 typedef struct type_pair_d *type_pair_t;
3240
3241 DEF_VEC_P(type_pair_t);
3242 DEF_VEC_ALLOC_P(type_pair_t,heap);
3243
3244 /* Return a hash value for the type pair pointed-to by P. */
3245
3246 static hashval_t
3247 type_pair_hash (const void *p)
3248 {
3249 const struct type_pair_d *pair = (const struct type_pair_d *) p;
3250 hashval_t val1 = pair->uid1;
3251 hashval_t val2 = pair->uid2;
3252 return iterative_hash_hashval_t (val1, val2);
3253 }
3254
3255 /* Compare two type pairs pointed-to by P1 and P2. */
3256
3257 static int
3258 type_pair_eq (const void *p1, const void *p2)
3259 {
3260 const struct type_pair_d *pair1 = (const struct type_pair_d *) p1;
3261 const struct type_pair_d *pair2 = (const struct type_pair_d *) p2;
3262 return (pair1->uid1 == pair2->uid1 && pair1->uid2 == pair2->uid2);
3263 }
3264
3265 /* Lookup the pair of types T1 and T2 in *VISITED_P. Insert a new
3266 entry if none existed. */
3267
3268 static type_pair_t
3269 lookup_type_pair (tree t1, tree t2, htab_t *visited_p, struct obstack *ob_p)
3270 {
3271 struct type_pair_d pair;
3272 type_pair_t p;
3273 void **slot;
3274
3275 if (*visited_p == NULL)
3276 {
3277 *visited_p = htab_create (251, type_pair_hash, type_pair_eq, NULL);
3278 gcc_obstack_init (ob_p);
3279 }
3280
3281 if (TYPE_UID (t1) < TYPE_UID (t2))
3282 {
3283 pair.uid1 = TYPE_UID (t1);
3284 pair.uid2 = TYPE_UID (t2);
3285 }
3286 else
3287 {
3288 pair.uid1 = TYPE_UID (t2);
3289 pair.uid2 = TYPE_UID (t1);
3290 }
3291 slot = htab_find_slot (*visited_p, &pair, INSERT);
3292
3293 if (*slot)
3294 p = *((type_pair_t *) slot);
3295 else
3296 {
3297 p = XOBNEW (ob_p, struct type_pair_d);
3298 p->uid1 = pair.uid1;
3299 p->uid2 = pair.uid2;
3300 p->same_p[0] = -2;
3301 p->same_p[1] = -2;
3302 *slot = (void *) p;
3303 }
3304
3305 return p;
3306 }
3307
3308 /* Per pointer state for the SCC finding. The on_sccstack flag
3309 is not strictly required, it is true when there is no hash value
3310 recorded for the type and false otherwise. But querying that
3311 is slower. */
3312
3313 struct sccs
3314 {
3315 unsigned int dfsnum;
3316 unsigned int low;
3317 bool on_sccstack;
3318 union {
3319 hashval_t hash;
3320 signed char same_p;
3321 } u;
3322 };
3323
3324 static unsigned int next_dfs_num;
3325 static unsigned int gtc_next_dfs_num;
3326
3327
3328 /* GIMPLE type merging cache. A direct-mapped cache based on TYPE_UID. */
3329
3330 typedef struct GTY(()) gimple_type_leader_entry_s {
3331 tree type;
3332 tree leader;
3333 } gimple_type_leader_entry;
3334
3335 #define GIMPLE_TYPE_LEADER_SIZE 16381
3336 static GTY((deletable, length("GIMPLE_TYPE_LEADER_SIZE")))
3337 gimple_type_leader_entry *gimple_type_leader;
3338
3339 /* Lookup an existing leader for T and return it or NULL_TREE, if
3340 there is none in the cache. */
3341
3342 static inline tree
3343 gimple_lookup_type_leader (tree t)
3344 {
3345 gimple_type_leader_entry *leader;
3346
3347 if (!gimple_type_leader)
3348 return NULL_TREE;
3349
3350 leader = &gimple_type_leader[TYPE_UID (t) % GIMPLE_TYPE_LEADER_SIZE];
3351 if (leader->type != t)
3352 return NULL_TREE;
3353
3354 return leader->leader;
3355 }
3356
3357 /* Return true if T1 and T2 have the same name. If FOR_COMPLETION_P is
3358 true then if any type has no name return false, otherwise return
3359 true if both types have no names. */
3360
3361 static bool
3362 compare_type_names_p (tree t1, tree t2, bool for_completion_p)
3363 {
3364 tree name1 = TYPE_NAME (t1);
3365 tree name2 = TYPE_NAME (t2);
3366
3367 /* Consider anonymous types all unique for completion. */
3368 if (for_completion_p
3369 && (!name1 || !name2))
3370 return false;
3371
3372 if (name1 && TREE_CODE (name1) == TYPE_DECL)
3373 {
3374 name1 = DECL_NAME (name1);
3375 if (for_completion_p
3376 && !name1)
3377 return false;
3378 }
3379 gcc_assert (!name1 || TREE_CODE (name1) == IDENTIFIER_NODE);
3380
3381 if (name2 && TREE_CODE (name2) == TYPE_DECL)
3382 {
3383 name2 = DECL_NAME (name2);
3384 if (for_completion_p
3385 && !name2)
3386 return false;
3387 }
3388 gcc_assert (!name2 || TREE_CODE (name2) == IDENTIFIER_NODE);
3389
3390 /* Identifiers can be compared with pointer equality rather
3391 than a string comparison. */
3392 if (name1 == name2)
3393 return true;
3394
3395 return false;
3396 }
3397
3398 /* Return true if the field decls F1 and F2 are at the same offset.
3399
3400 This is intended to be used on GIMPLE types only. */
3401
3402 bool
3403 gimple_compare_field_offset (tree f1, tree f2)
3404 {
3405 if (DECL_OFFSET_ALIGN (f1) == DECL_OFFSET_ALIGN (f2))
3406 {
3407 tree offset1 = DECL_FIELD_OFFSET (f1);
3408 tree offset2 = DECL_FIELD_OFFSET (f2);
3409 return ((offset1 == offset2
3410 /* Once gimplification is done, self-referential offsets are
3411 instantiated as operand #2 of the COMPONENT_REF built for
3412 each access and reset. Therefore, they are not relevant
3413 anymore and fields are interchangeable provided that they
3414 represent the same access. */
3415 || (TREE_CODE (offset1) == PLACEHOLDER_EXPR
3416 && TREE_CODE (offset2) == PLACEHOLDER_EXPR
3417 && (DECL_SIZE (f1) == DECL_SIZE (f2)
3418 || (TREE_CODE (DECL_SIZE (f1)) == PLACEHOLDER_EXPR
3419 && TREE_CODE (DECL_SIZE (f2)) == PLACEHOLDER_EXPR)
3420 || operand_equal_p (DECL_SIZE (f1), DECL_SIZE (f2), 0))
3421 && DECL_ALIGN (f1) == DECL_ALIGN (f2))
3422 || operand_equal_p (offset1, offset2, 0))
3423 && tree_int_cst_equal (DECL_FIELD_BIT_OFFSET (f1),
3424 DECL_FIELD_BIT_OFFSET (f2)));
3425 }
3426
3427 /* Fortran and C do not always agree on what DECL_OFFSET_ALIGN
3428 should be, so handle differing ones specially by decomposing
3429 the offset into a byte and bit offset manually. */
3430 if (host_integerp (DECL_FIELD_OFFSET (f1), 0)
3431 && host_integerp (DECL_FIELD_OFFSET (f2), 0))
3432 {
3433 unsigned HOST_WIDE_INT byte_offset1, byte_offset2;
3434 unsigned HOST_WIDE_INT bit_offset1, bit_offset2;
3435 bit_offset1 = TREE_INT_CST_LOW (DECL_FIELD_BIT_OFFSET (f1));
3436 byte_offset1 = (TREE_INT_CST_LOW (DECL_FIELD_OFFSET (f1))
3437 + bit_offset1 / BITS_PER_UNIT);
3438 bit_offset2 = TREE_INT_CST_LOW (DECL_FIELD_BIT_OFFSET (f2));
3439 byte_offset2 = (TREE_INT_CST_LOW (DECL_FIELD_OFFSET (f2))
3440 + bit_offset2 / BITS_PER_UNIT);
3441 if (byte_offset1 != byte_offset2)
3442 return false;
3443 return bit_offset1 % BITS_PER_UNIT == bit_offset2 % BITS_PER_UNIT;
3444 }
3445
3446 return false;
3447 }
3448
3449 /* If the type T1 and the type T2 are a complete and an incomplete
3450 variant of the same type return true. */
3451
3452 static bool
3453 gimple_compatible_complete_and_incomplete_subtype_p (tree t1, tree t2)
3454 {
3455 /* If one pointer points to an incomplete type variant of
3456 the other pointed-to type they are the same. */
3457 if (TREE_CODE (t1) == TREE_CODE (t2)
3458 && RECORD_OR_UNION_TYPE_P (t1)
3459 && (!COMPLETE_TYPE_P (t1)
3460 || !COMPLETE_TYPE_P (t2))
3461 && TYPE_QUALS (t1) == TYPE_QUALS (t2)
3462 && compare_type_names_p (TYPE_MAIN_VARIANT (t1),
3463 TYPE_MAIN_VARIANT (t2), true))
3464 return true;
3465 return false;
3466 }
3467
3468 static bool
3469 gimple_types_compatible_p_1 (tree, tree, type_pair_t,
3470 VEC(type_pair_t, heap) **,
3471 struct pointer_map_t *, struct obstack *);
3472
3473 /* DFS visit the edge from the callers type pair with state *STATE to
3474 the pair T1, T2 while operating in FOR_MERGING_P mode.
3475 Update the merging status if it is not part of the SCC containing the
3476 callers pair and return it.
3477 SCCSTACK, SCCSTATE and SCCSTATE_OBSTACK are state for the DFS walk done. */
3478
3479 static bool
3480 gtc_visit (tree t1, tree t2,
3481 struct sccs *state,
3482 VEC(type_pair_t, heap) **sccstack,
3483 struct pointer_map_t *sccstate,
3484 struct obstack *sccstate_obstack)
3485 {
3486 struct sccs *cstate = NULL;
3487 type_pair_t p;
3488 void **slot;
3489 tree leader1, leader2;
3490
3491 /* Check first for the obvious case of pointer identity. */
3492 if (t1 == t2)
3493 return true;
3494
3495 /* Check that we have two types to compare. */
3496 if (t1 == NULL_TREE || t2 == NULL_TREE)
3497 return false;
3498
3499 /* Can't be the same type if the types don't have the same code. */
3500 if (TREE_CODE (t1) != TREE_CODE (t2))
3501 return false;
3502
3503 /* Can't be the same type if they have different CV qualifiers. */
3504 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
3505 return false;
3506
3507 if (TREE_ADDRESSABLE (t1) != TREE_ADDRESSABLE (t2))
3508 return false;
3509
3510 /* Void types and nullptr types are always the same. */
3511 if (TREE_CODE (t1) == VOID_TYPE
3512 || TREE_CODE (t1) == NULLPTR_TYPE)
3513 return true;
3514
3515 /* Can't be the same type if they have different alignment or mode. */
3516 if (TYPE_ALIGN (t1) != TYPE_ALIGN (t2)
3517 || TYPE_MODE (t1) != TYPE_MODE (t2))
3518 return false;
3519
3520 /* Do some simple checks before doing three hashtable queries. */
3521 if (INTEGRAL_TYPE_P (t1)
3522 || SCALAR_FLOAT_TYPE_P (t1)
3523 || FIXED_POINT_TYPE_P (t1)
3524 || TREE_CODE (t1) == VECTOR_TYPE
3525 || TREE_CODE (t1) == COMPLEX_TYPE
3526 || TREE_CODE (t1) == OFFSET_TYPE
3527 || POINTER_TYPE_P (t1))
3528 {
3529 /* Can't be the same type if they have different sign or precision. */
3530 if (TYPE_PRECISION (t1) != TYPE_PRECISION (t2)
3531 || TYPE_UNSIGNED (t1) != TYPE_UNSIGNED (t2))
3532 return false;
3533
3534 if (TREE_CODE (t1) == INTEGER_TYPE
3535 && (TYPE_IS_SIZETYPE (t1) != TYPE_IS_SIZETYPE (t2)
3536 || TYPE_STRING_FLAG (t1) != TYPE_STRING_FLAG (t2)))
3537 return false;
3538
3539 /* That's all we need to check for float and fixed-point types. */
3540 if (SCALAR_FLOAT_TYPE_P (t1)
3541 || FIXED_POINT_TYPE_P (t1))
3542 return true;
3543
3544 /* For other types fall thru to more complex checks. */
3545 }
3546
3547 /* If the types have been previously registered and found equal
3548 they still are. */
3549 leader1 = gimple_lookup_type_leader (t1);
3550 leader2 = gimple_lookup_type_leader (t2);
3551 if (leader1 == t2
3552 || t1 == leader2
3553 || (leader1 && leader1 == leader2))
3554 return true;
3555
3556 /* If the hash values of t1 and t2 are different the types can't
3557 possibly be the same. This helps keeping the type-pair hashtable
3558 small, only tracking comparisons for hash collisions. */
3559 if (gimple_type_hash (t1) != gimple_type_hash (t2))
3560 return false;
3561
3562 /* Allocate a new cache entry for this comparison. */
3563 p = lookup_type_pair (t1, t2, &gtc_visited, &gtc_ob);
3564 if (p->same_p[GTC_MERGE] == 0 || p->same_p[GTC_MERGE] == 1)
3565 {
3566 /* We have already decided whether T1 and T2 are the
3567 same, return the cached result. */
3568 return p->same_p[GTC_MERGE] == 1;
3569 }
3570
3571 if ((slot = pointer_map_contains (sccstate, p)) != NULL)
3572 cstate = (struct sccs *)*slot;
3573 /* Not yet visited. DFS recurse. */
3574 if (!cstate)
3575 {
3576 gimple_types_compatible_p_1 (t1, t2, p,
3577 sccstack, sccstate, sccstate_obstack);
3578 cstate = (struct sccs *)* pointer_map_contains (sccstate, p);
3579 state->low = MIN (state->low, cstate->low);
3580 }
3581 /* If the type is still on the SCC stack adjust the parents low. */
3582 if (cstate->dfsnum < state->dfsnum
3583 && cstate->on_sccstack)
3584 state->low = MIN (cstate->dfsnum, state->low);
3585
3586 /* Return the current lattice value. We start with an equality
3587 assumption so types part of a SCC will be optimistically
3588 treated equal unless proven otherwise. */
3589 return cstate->u.same_p;
3590 }
3591
3592 /* Worker for gimple_types_compatible.
3593 SCCSTACK, SCCSTATE and SCCSTATE_OBSTACK are state for the DFS walk done. */
3594
3595 static bool
3596 gimple_types_compatible_p_1 (tree t1, tree t2, type_pair_t p,
3597 VEC(type_pair_t, heap) **sccstack,
3598 struct pointer_map_t *sccstate,
3599 struct obstack *sccstate_obstack)
3600 {
3601 struct sccs *state;
3602
3603 gcc_assert (p->same_p[GTC_MERGE] == -2);
3604
3605 state = XOBNEW (sccstate_obstack, struct sccs);
3606 *pointer_map_insert (sccstate, p) = state;
3607
3608 VEC_safe_push (type_pair_t, heap, *sccstack, p);
3609 state->dfsnum = gtc_next_dfs_num++;
3610 state->low = state->dfsnum;
3611 state->on_sccstack = true;
3612 /* Start with an equality assumption. As we DFS recurse into child
3613 SCCs this assumption may get revisited. */
3614 state->u.same_p = 1;
3615
3616 /* If their attributes are not the same they can't be the same type. */
3617 if (!attribute_list_equal (TYPE_ATTRIBUTES (t1), TYPE_ATTRIBUTES (t2)))
3618 goto different_types;
3619
3620 /* Do type-specific comparisons. */
3621 switch (TREE_CODE (t1))
3622 {
3623 case VECTOR_TYPE:
3624 case COMPLEX_TYPE:
3625 if (!gtc_visit (TREE_TYPE (t1), TREE_TYPE (t2),
3626 state, sccstack, sccstate, sccstate_obstack))
3627 goto different_types;
3628 goto same_types;
3629
3630 case ARRAY_TYPE:
3631 /* Array types are the same if the element types are the same and
3632 the number of elements are the same. */
3633 if (!gtc_visit (TREE_TYPE (t1), TREE_TYPE (t2),
3634 state, sccstack, sccstate, sccstate_obstack)
3635 || TYPE_STRING_FLAG (t1) != TYPE_STRING_FLAG (t2)
3636 || TYPE_NONALIASED_COMPONENT (t1) != TYPE_NONALIASED_COMPONENT (t2))
3637 goto different_types;
3638 else
3639 {
3640 tree i1 = TYPE_DOMAIN (t1);
3641 tree i2 = TYPE_DOMAIN (t2);
3642
3643 /* For an incomplete external array, the type domain can be
3644 NULL_TREE. Check this condition also. */
3645 if (i1 == NULL_TREE && i2 == NULL_TREE)
3646 goto same_types;
3647 else if (i1 == NULL_TREE || i2 == NULL_TREE)
3648 goto different_types;
3649 /* If for a complete array type the possibly gimplified sizes
3650 are different the types are different. */
3651 else if (((TYPE_SIZE (i1) != NULL) ^ (TYPE_SIZE (i2) != NULL))
3652 || (TYPE_SIZE (i1)
3653 && TYPE_SIZE (i2)
3654 && !operand_equal_p (TYPE_SIZE (i1), TYPE_SIZE (i2), 0)))
3655 goto different_types;
3656 else
3657 {
3658 tree min1 = TYPE_MIN_VALUE (i1);
3659 tree min2 = TYPE_MIN_VALUE (i2);
3660 tree max1 = TYPE_MAX_VALUE (i1);
3661 tree max2 = TYPE_MAX_VALUE (i2);
3662
3663 /* The minimum/maximum values have to be the same. */
3664 if ((min1 == min2
3665 || (min1 && min2
3666 && ((TREE_CODE (min1) == PLACEHOLDER_EXPR
3667 && TREE_CODE (min2) == PLACEHOLDER_EXPR)
3668 || operand_equal_p (min1, min2, 0))))
3669 && (max1 == max2
3670 || (max1 && max2
3671 && ((TREE_CODE (max1) == PLACEHOLDER_EXPR
3672 && TREE_CODE (max2) == PLACEHOLDER_EXPR)
3673 || operand_equal_p (max1, max2, 0)))))
3674 goto same_types;
3675 else
3676 goto different_types;
3677 }
3678 }
3679
3680 case METHOD_TYPE:
3681 /* Method types should belong to the same class. */
3682 if (!gtc_visit (TYPE_METHOD_BASETYPE (t1), TYPE_METHOD_BASETYPE (t2),
3683 state, sccstack, sccstate, sccstate_obstack))
3684 goto different_types;
3685
3686 /* Fallthru */
3687
3688 case FUNCTION_TYPE:
3689 /* Function types are the same if the return type and arguments types
3690 are the same. */
3691 if (!gtc_visit (TREE_TYPE (t1), TREE_TYPE (t2),
3692 state, sccstack, sccstate, sccstate_obstack))
3693 goto different_types;
3694
3695 if (!comp_type_attributes (t1, t2))
3696 goto different_types;
3697
3698 if (TYPE_ARG_TYPES (t1) == TYPE_ARG_TYPES (t2))
3699 goto same_types;
3700 else
3701 {
3702 tree parms1, parms2;
3703
3704 for (parms1 = TYPE_ARG_TYPES (t1), parms2 = TYPE_ARG_TYPES (t2);
3705 parms1 && parms2;
3706 parms1 = TREE_CHAIN (parms1), parms2 = TREE_CHAIN (parms2))
3707 {
3708 if (!gtc_visit (TREE_VALUE (parms1), TREE_VALUE (parms2),
3709 state, sccstack, sccstate, sccstate_obstack))
3710 goto different_types;
3711 }
3712
3713 if (parms1 || parms2)
3714 goto different_types;
3715
3716 goto same_types;
3717 }
3718
3719 case OFFSET_TYPE:
3720 {
3721 if (!gtc_visit (TREE_TYPE (t1), TREE_TYPE (t2),
3722 state, sccstack, sccstate, sccstate_obstack)
3723 || !gtc_visit (TYPE_OFFSET_BASETYPE (t1),
3724 TYPE_OFFSET_BASETYPE (t2),
3725 state, sccstack, sccstate, sccstate_obstack))
3726 goto different_types;
3727
3728 goto same_types;
3729 }
3730
3731 case POINTER_TYPE:
3732 case REFERENCE_TYPE:
3733 {
3734 /* If the two pointers have different ref-all attributes,
3735 they can't be the same type. */
3736 if (TYPE_REF_CAN_ALIAS_ALL (t1) != TYPE_REF_CAN_ALIAS_ALL (t2))
3737 goto different_types;
3738
3739 /* Otherwise, pointer and reference types are the same if the
3740 pointed-to types are the same. */
3741 if (gtc_visit (TREE_TYPE (t1), TREE_TYPE (t2),
3742 state, sccstack, sccstate, sccstate_obstack))
3743 goto same_types;
3744
3745 goto different_types;
3746 }
3747
3748 case INTEGER_TYPE:
3749 case BOOLEAN_TYPE:
3750 {
3751 tree min1 = TYPE_MIN_VALUE (t1);
3752 tree max1 = TYPE_MAX_VALUE (t1);
3753 tree min2 = TYPE_MIN_VALUE (t2);
3754 tree max2 = TYPE_MAX_VALUE (t2);
3755 bool min_equal_p = false;
3756 bool max_equal_p = false;
3757
3758 /* If either type has a minimum value, the other type must
3759 have the same. */
3760 if (min1 == NULL_TREE && min2 == NULL_TREE)
3761 min_equal_p = true;
3762 else if (min1 && min2 && operand_equal_p (min1, min2, 0))
3763 min_equal_p = true;
3764
3765 /* Likewise, if either type has a maximum value, the other
3766 type must have the same. */
3767 if (max1 == NULL_TREE && max2 == NULL_TREE)
3768 max_equal_p = true;
3769 else if (max1 && max2 && operand_equal_p (max1, max2, 0))
3770 max_equal_p = true;
3771
3772 if (!min_equal_p || !max_equal_p)
3773 goto different_types;
3774
3775 goto same_types;
3776 }
3777
3778 case ENUMERAL_TYPE:
3779 {
3780 /* FIXME lto, we cannot check bounds on enumeral types because
3781 different front ends will produce different values.
3782 In C, enumeral types are integers, while in C++ each element
3783 will have its own symbolic value. We should decide how enums
3784 are to be represented in GIMPLE and have each front end lower
3785 to that. */
3786 tree v1, v2;
3787
3788 /* For enumeral types, all the values must be the same. */
3789 if (TYPE_VALUES (t1) == TYPE_VALUES (t2))
3790 goto same_types;
3791
3792 for (v1 = TYPE_VALUES (t1), v2 = TYPE_VALUES (t2);
3793 v1 && v2;
3794 v1 = TREE_CHAIN (v1), v2 = TREE_CHAIN (v2))
3795 {
3796 tree c1 = TREE_VALUE (v1);
3797 tree c2 = TREE_VALUE (v2);
3798
3799 if (TREE_CODE (c1) == CONST_DECL)
3800 c1 = DECL_INITIAL (c1);
3801
3802 if (TREE_CODE (c2) == CONST_DECL)
3803 c2 = DECL_INITIAL (c2);
3804
3805 if (tree_int_cst_equal (c1, c2) != 1)
3806 goto different_types;
3807
3808 if (TREE_PURPOSE (v1) != TREE_PURPOSE (v2))
3809 goto different_types;
3810 }
3811
3812 /* If one enumeration has more values than the other, they
3813 are not the same. */
3814 if (v1 || v2)
3815 goto different_types;
3816
3817 goto same_types;
3818 }
3819
3820 case RECORD_TYPE:
3821 case UNION_TYPE:
3822 case QUAL_UNION_TYPE:
3823 {
3824 tree f1, f2;
3825
3826 /* The struct tags shall compare equal. */
3827 if (!compare_type_names_p (TYPE_MAIN_VARIANT (t1),
3828 TYPE_MAIN_VARIANT (t2), false))
3829 goto different_types;
3830
3831 /* For aggregate types, all the fields must be the same. */
3832 for (f1 = TYPE_FIELDS (t1), f2 = TYPE_FIELDS (t2);
3833 f1 && f2;
3834 f1 = TREE_CHAIN (f1), f2 = TREE_CHAIN (f2))
3835 {
3836 /* The fields must have the same name, offset and type. */
3837 if (DECL_NAME (f1) != DECL_NAME (f2)
3838 || DECL_NONADDRESSABLE_P (f1) != DECL_NONADDRESSABLE_P (f2)
3839 || !gimple_compare_field_offset (f1, f2)
3840 || !gtc_visit (TREE_TYPE (f1), TREE_TYPE (f2),
3841 state, sccstack, sccstate, sccstate_obstack))
3842 goto different_types;
3843 }
3844
3845 /* If one aggregate has more fields than the other, they
3846 are not the same. */
3847 if (f1 || f2)
3848 goto different_types;
3849
3850 goto same_types;
3851 }
3852
3853 default:
3854 gcc_unreachable ();
3855 }
3856
3857 /* Common exit path for types that are not compatible. */
3858 different_types:
3859 state->u.same_p = 0;
3860 goto pop;
3861
3862 /* Common exit path for types that are compatible. */
3863 same_types:
3864 gcc_assert (state->u.same_p == 1);
3865
3866 pop:
3867 if (state->low == state->dfsnum)
3868 {
3869 type_pair_t x;
3870
3871 /* Pop off the SCC and set its cache values to the final
3872 comparison result. */
3873 do
3874 {
3875 struct sccs *cstate;
3876 x = VEC_pop (type_pair_t, *sccstack);
3877 cstate = (struct sccs *)*pointer_map_contains (sccstate, x);
3878 cstate->on_sccstack = false;
3879 x->same_p[GTC_MERGE] = state->u.same_p;
3880 }
3881 while (x != p);
3882 }
3883
3884 return state->u.same_p;
3885 }
3886
3887 /* Return true iff T1 and T2 are structurally identical. When
3888 FOR_MERGING_P is true the an incomplete type and a complete type
3889 are considered different, otherwise they are considered compatible. */
3890
3891 static bool
3892 gimple_types_compatible_p (tree t1, tree t2)
3893 {
3894 VEC(type_pair_t, heap) *sccstack = NULL;
3895 struct pointer_map_t *sccstate;
3896 struct obstack sccstate_obstack;
3897 type_pair_t p = NULL;
3898 bool res;
3899 tree leader1, leader2;
3900
3901 /* Before starting to set up the SCC machinery handle simple cases. */
3902
3903 /* Check first for the obvious case of pointer identity. */
3904 if (t1 == t2)
3905 return true;
3906
3907 /* Check that we have two types to compare. */
3908 if (t1 == NULL_TREE || t2 == NULL_TREE)
3909 return false;
3910
3911 /* Can't be the same type if the types don't have the same code. */
3912 if (TREE_CODE (t1) != TREE_CODE (t2))
3913 return false;
3914
3915 /* Can't be the same type if they have different CV qualifiers. */
3916 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
3917 return false;
3918
3919 if (TREE_ADDRESSABLE (t1) != TREE_ADDRESSABLE (t2))
3920 return false;
3921
3922 /* Void types and nullptr types are always the same. */
3923 if (TREE_CODE (t1) == VOID_TYPE
3924 || TREE_CODE (t1) == NULLPTR_TYPE)
3925 return true;
3926
3927 /* Can't be the same type if they have different alignment or mode. */
3928 if (TYPE_ALIGN (t1) != TYPE_ALIGN (t2)
3929 || TYPE_MODE (t1) != TYPE_MODE (t2))
3930 return false;
3931
3932 /* Do some simple checks before doing three hashtable queries. */
3933 if (INTEGRAL_TYPE_P (t1)
3934 || SCALAR_FLOAT_TYPE_P (t1)
3935 || FIXED_POINT_TYPE_P (t1)
3936 || TREE_CODE (t1) == VECTOR_TYPE
3937 || TREE_CODE (t1) == COMPLEX_TYPE
3938 || TREE_CODE (t1) == OFFSET_TYPE
3939 || POINTER_TYPE_P (t1))
3940 {
3941 /* Can't be the same type if they have different sign or precision. */
3942 if (TYPE_PRECISION (t1) != TYPE_PRECISION (t2)
3943 || TYPE_UNSIGNED (t1) != TYPE_UNSIGNED (t2))
3944 return false;
3945
3946 if (TREE_CODE (t1) == INTEGER_TYPE
3947 && (TYPE_IS_SIZETYPE (t1) != TYPE_IS_SIZETYPE (t2)
3948 || TYPE_STRING_FLAG (t1) != TYPE_STRING_FLAG (t2)))
3949 return false;
3950
3951 /* That's all we need to check for float and fixed-point types. */
3952 if (SCALAR_FLOAT_TYPE_P (t1)
3953 || FIXED_POINT_TYPE_P (t1))
3954 return true;
3955
3956 /* For other types fall thru to more complex checks. */
3957 }
3958
3959 /* If the types have been previously registered and found equal
3960 they still are. */
3961 leader1 = gimple_lookup_type_leader (t1);
3962 leader2 = gimple_lookup_type_leader (t2);
3963 if (leader1 == t2
3964 || t1 == leader2
3965 || (leader1 && leader1 == leader2))
3966 return true;
3967
3968 /* If the hash values of t1 and t2 are different the types can't
3969 possibly be the same. This helps keeping the type-pair hashtable
3970 small, only tracking comparisons for hash collisions. */
3971 if (gimple_type_hash (t1) != gimple_type_hash (t2))
3972 return false;
3973
3974 /* If we've visited this type pair before (in the case of aggregates
3975 with self-referential types), and we made a decision, return it. */
3976 p = lookup_type_pair (t1, t2, &gtc_visited, &gtc_ob);
3977 if (p->same_p[GTC_MERGE] == 0 || p->same_p[GTC_MERGE] == 1)
3978 {
3979 /* We have already decided whether T1 and T2 are the
3980 same, return the cached result. */
3981 return p->same_p[GTC_MERGE] == 1;
3982 }
3983
3984 /* Now set up the SCC machinery for the comparison. */
3985 gtc_next_dfs_num = 1;
3986 sccstate = pointer_map_create ();
3987 gcc_obstack_init (&sccstate_obstack);
3988 res = gimple_types_compatible_p_1 (t1, t2, p,
3989 &sccstack, sccstate, &sccstate_obstack);
3990 VEC_free (type_pair_t, heap, sccstack);
3991 pointer_map_destroy (sccstate);
3992 obstack_free (&sccstate_obstack, NULL);
3993
3994 return res;
3995 }
3996
3997
3998 static hashval_t
3999 iterative_hash_gimple_type (tree, hashval_t, VEC(tree, heap) **,
4000 struct pointer_map_t *, struct obstack *);
4001
4002 /* DFS visit the edge from the callers type with state *STATE to T.
4003 Update the callers type hash V with the hash for T if it is not part
4004 of the SCC containing the callers type and return it.
4005 SCCSTACK, SCCSTATE and SCCSTATE_OBSTACK are state for the DFS walk done. */
4006
4007 static hashval_t
4008 visit (tree t, struct sccs *state, hashval_t v,
4009 VEC (tree, heap) **sccstack,
4010 struct pointer_map_t *sccstate,
4011 struct obstack *sccstate_obstack)
4012 {
4013 struct sccs *cstate = NULL;
4014 struct tree_int_map m;
4015 void **slot;
4016
4017 /* If there is a hash value recorded for this type then it can't
4018 possibly be part of our parent SCC. Simply mix in its hash. */
4019 m.base.from = t;
4020 if ((slot = htab_find_slot (type_hash_cache, &m, NO_INSERT))
4021 && *slot)
4022 return iterative_hash_hashval_t (((struct tree_int_map *) *slot)->to, v);
4023
4024 if ((slot = pointer_map_contains (sccstate, t)) != NULL)
4025 cstate = (struct sccs *)*slot;
4026 if (!cstate)
4027 {
4028 hashval_t tem;
4029 /* Not yet visited. DFS recurse. */
4030 tem = iterative_hash_gimple_type (t, v,
4031 sccstack, sccstate, sccstate_obstack);
4032 if (!cstate)
4033 cstate = (struct sccs *)* pointer_map_contains (sccstate, t);
4034 state->low = MIN (state->low, cstate->low);
4035 /* If the type is no longer on the SCC stack and thus is not part
4036 of the parents SCC mix in its hash value. Otherwise we will
4037 ignore the type for hashing purposes and return the unaltered
4038 hash value. */
4039 if (!cstate->on_sccstack)
4040 return tem;
4041 }
4042 if (cstate->dfsnum < state->dfsnum
4043 && cstate->on_sccstack)
4044 state->low = MIN (cstate->dfsnum, state->low);
4045
4046 /* We are part of our parents SCC, skip this type during hashing
4047 and return the unaltered hash value. */
4048 return v;
4049 }
4050
4051 /* Hash NAME with the previous hash value V and return it. */
4052
4053 static hashval_t
4054 iterative_hash_name (tree name, hashval_t v)
4055 {
4056 if (!name)
4057 return v;
4058 if (TREE_CODE (name) == TYPE_DECL)
4059 name = DECL_NAME (name);
4060 if (!name)
4061 return v;
4062 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
4063 return iterative_hash_object (IDENTIFIER_HASH_VALUE (name), v);
4064 }
4065
4066 /* A type, hashvalue pair for sorting SCC members. */
4067
4068 struct type_hash_pair {
4069 tree type;
4070 hashval_t hash;
4071 };
4072
4073 /* Compare two type, hashvalue pairs. */
4074
4075 static int
4076 type_hash_pair_compare (const void *p1_, const void *p2_)
4077 {
4078 const struct type_hash_pair *p1 = (const struct type_hash_pair *) p1_;
4079 const struct type_hash_pair *p2 = (const struct type_hash_pair *) p2_;
4080 if (p1->hash < p2->hash)
4081 return -1;
4082 else if (p1->hash > p2->hash)
4083 return 1;
4084 return 0;
4085 }
4086
4087 /* Returning a hash value for gimple type TYPE combined with VAL.
4088 SCCSTACK, SCCSTATE and SCCSTATE_OBSTACK are state for the DFS walk done.
4089
4090 To hash a type we end up hashing in types that are reachable.
4091 Through pointers we can end up with cycles which messes up the
4092 required property that we need to compute the same hash value
4093 for structurally equivalent types. To avoid this we have to
4094 hash all types in a cycle (the SCC) in a commutative way. The
4095 easiest way is to not mix in the hashes of the SCC members at
4096 all. To make this work we have to delay setting the hash
4097 values of the SCC until it is complete. */
4098
4099 static hashval_t
4100 iterative_hash_gimple_type (tree type, hashval_t val,
4101 VEC(tree, heap) **sccstack,
4102 struct pointer_map_t *sccstate,
4103 struct obstack *sccstate_obstack)
4104 {
4105 hashval_t v;
4106 void **slot;
4107 struct sccs *state;
4108
4109 /* Not visited during this DFS walk. */
4110 gcc_checking_assert (!pointer_map_contains (sccstate, type));
4111 state = XOBNEW (sccstate_obstack, struct sccs);
4112 *pointer_map_insert (sccstate, type) = state;
4113
4114 VEC_safe_push (tree, heap, *sccstack, type);
4115 state->dfsnum = next_dfs_num++;
4116 state->low = state->dfsnum;
4117 state->on_sccstack = true;
4118
4119 /* Combine a few common features of types so that types are grouped into
4120 smaller sets; when searching for existing matching types to merge,
4121 only existing types having the same features as the new type will be
4122 checked. */
4123 v = iterative_hash_hashval_t (TREE_CODE (type), 0);
4124 v = iterative_hash_hashval_t (TYPE_QUALS (type), v);
4125 v = iterative_hash_hashval_t (TREE_ADDRESSABLE (type), v);
4126
4127 /* Do not hash the types size as this will cause differences in
4128 hash values for the complete vs. the incomplete type variant. */
4129
4130 /* Incorporate common features of numerical types. */
4131 if (INTEGRAL_TYPE_P (type)
4132 || SCALAR_FLOAT_TYPE_P (type)
4133 || FIXED_POINT_TYPE_P (type))
4134 {
4135 v = iterative_hash_hashval_t (TYPE_PRECISION (type), v);
4136 v = iterative_hash_hashval_t (TYPE_MODE (type), v);
4137 v = iterative_hash_hashval_t (TYPE_UNSIGNED (type), v);
4138 }
4139
4140 /* For pointer and reference types, fold in information about the type
4141 pointed to. */
4142 if (POINTER_TYPE_P (type))
4143 v = visit (TREE_TYPE (type), state, v,
4144 sccstack, sccstate, sccstate_obstack);
4145
4146 /* For integer types hash the types min/max values and the string flag. */
4147 if (TREE_CODE (type) == INTEGER_TYPE)
4148 {
4149 /* OMP lowering can introduce error_mark_node in place of
4150 random local decls in types. */
4151 if (TYPE_MIN_VALUE (type) != error_mark_node)
4152 v = iterative_hash_expr (TYPE_MIN_VALUE (type), v);
4153 if (TYPE_MAX_VALUE (type) != error_mark_node)
4154 v = iterative_hash_expr (TYPE_MAX_VALUE (type), v);
4155 v = iterative_hash_hashval_t (TYPE_STRING_FLAG (type), v);
4156 }
4157
4158 /* For array types hash their domain and the string flag. */
4159 if (TREE_CODE (type) == ARRAY_TYPE
4160 && TYPE_DOMAIN (type))
4161 {
4162 v = iterative_hash_hashval_t (TYPE_STRING_FLAG (type), v);
4163 v = visit (TYPE_DOMAIN (type), state, v,
4164 sccstack, sccstate, sccstate_obstack);
4165 }
4166
4167 /* Recurse for aggregates with a single element type. */
4168 if (TREE_CODE (type) == ARRAY_TYPE
4169 || TREE_CODE (type) == COMPLEX_TYPE
4170 || TREE_CODE (type) == VECTOR_TYPE)
4171 v = visit (TREE_TYPE (type), state, v,
4172 sccstack, sccstate, sccstate_obstack);
4173
4174 /* Incorporate function return and argument types. */
4175 if (TREE_CODE (type) == FUNCTION_TYPE || TREE_CODE (type) == METHOD_TYPE)
4176 {
4177 unsigned na;
4178 tree p;
4179
4180 /* For method types also incorporate their parent class. */
4181 if (TREE_CODE (type) == METHOD_TYPE)
4182 v = visit (TYPE_METHOD_BASETYPE (type), state, v,
4183 sccstack, sccstate, sccstate_obstack);
4184
4185 /* Check result and argument types. */
4186 v = visit (TREE_TYPE (type), state, v,
4187 sccstack, sccstate, sccstate_obstack);
4188 for (p = TYPE_ARG_TYPES (type), na = 0; p; p = TREE_CHAIN (p))
4189 {
4190 v = visit (TREE_VALUE (p), state, v,
4191 sccstack, sccstate, sccstate_obstack);
4192 na++;
4193 }
4194
4195 v = iterative_hash_hashval_t (na, v);
4196 }
4197
4198 if (TREE_CODE (type) == RECORD_TYPE
4199 || TREE_CODE (type) == UNION_TYPE
4200 || TREE_CODE (type) == QUAL_UNION_TYPE)
4201 {
4202 unsigned nf;
4203 tree f;
4204
4205 v = iterative_hash_name (TYPE_NAME (TYPE_MAIN_VARIANT (type)), v);
4206
4207 for (f = TYPE_FIELDS (type), nf = 0; f; f = TREE_CHAIN (f))
4208 {
4209 v = iterative_hash_name (DECL_NAME (f), v);
4210 v = visit (TREE_TYPE (f), state, v,
4211 sccstack, sccstate, sccstate_obstack);
4212 nf++;
4213 }
4214
4215 v = iterative_hash_hashval_t (nf, v);
4216 }
4217
4218 /* Record hash for us. */
4219 state->u.hash = v;
4220
4221 /* See if we found an SCC. */
4222 if (state->low == state->dfsnum)
4223 {
4224 tree x;
4225 struct tree_int_map *m;
4226
4227 /* Pop off the SCC and set its hash values. */
4228 x = VEC_pop (tree, *sccstack);
4229 /* Optimize SCC size one. */
4230 if (x == type)
4231 {
4232 state->on_sccstack = false;
4233 m = ggc_alloc_cleared_tree_int_map ();
4234 m->base.from = x;
4235 m->to = v;
4236 slot = htab_find_slot (type_hash_cache, m, INSERT);
4237 gcc_assert (!*slot);
4238 *slot = (void *) m;
4239 }
4240 else
4241 {
4242 struct sccs *cstate;
4243 unsigned first, i, size, j;
4244 struct type_hash_pair *pairs;
4245 /* Pop off the SCC and build an array of type, hash pairs. */
4246 first = VEC_length (tree, *sccstack) - 1;
4247 while (VEC_index (tree, *sccstack, first) != type)
4248 --first;
4249 size = VEC_length (tree, *sccstack) - first + 1;
4250 pairs = XALLOCAVEC (struct type_hash_pair, size);
4251 i = 0;
4252 cstate = (struct sccs *)*pointer_map_contains (sccstate, x);
4253 cstate->on_sccstack = false;
4254 pairs[i].type = x;
4255 pairs[i].hash = cstate->u.hash;
4256 do
4257 {
4258 x = VEC_pop (tree, *sccstack);
4259 cstate = (struct sccs *)*pointer_map_contains (sccstate, x);
4260 cstate->on_sccstack = false;
4261 ++i;
4262 pairs[i].type = x;
4263 pairs[i].hash = cstate->u.hash;
4264 }
4265 while (x != type);
4266 gcc_assert (i + 1 == size);
4267 /* Sort the arrays of type, hash pairs so that when we mix in
4268 all members of the SCC the hash value becomes independent on
4269 the order we visited the SCC. Disregard hashes equal to
4270 the hash of the type we mix into because we cannot guarantee
4271 a stable sort for those across different TUs. */
4272 qsort (pairs, size, sizeof (struct type_hash_pair),
4273 type_hash_pair_compare);
4274 for (i = 0; i < size; ++i)
4275 {
4276 hashval_t hash;
4277 m = ggc_alloc_cleared_tree_int_map ();
4278 m->base.from = pairs[i].type;
4279 hash = pairs[i].hash;
4280 /* Skip same hashes. */
4281 for (j = i + 1; j < size && pairs[j].hash == pairs[i].hash; ++j)
4282 ;
4283 for (; j < size; ++j)
4284 hash = iterative_hash_hashval_t (pairs[j].hash, hash);
4285 for (j = 0; pairs[j].hash != pairs[i].hash; ++j)
4286 hash = iterative_hash_hashval_t (pairs[j].hash, hash);
4287 m->to = hash;
4288 if (pairs[i].type == type)
4289 v = hash;
4290 slot = htab_find_slot (type_hash_cache, m, INSERT);
4291 gcc_assert (!*slot);
4292 *slot = (void *) m;
4293 }
4294 }
4295 }
4296
4297 return iterative_hash_hashval_t (v, val);
4298 }
4299
4300
4301 /* Returns a hash value for P (assumed to be a type). The hash value
4302 is computed using some distinguishing features of the type. Note
4303 that we cannot use pointer hashing here as we may be dealing with
4304 two distinct instances of the same type.
4305
4306 This function should produce the same hash value for two compatible
4307 types according to gimple_types_compatible_p. */
4308
4309 static hashval_t
4310 gimple_type_hash (const void *p)
4311 {
4312 const_tree t = (const_tree) p;
4313 VEC(tree, heap) *sccstack = NULL;
4314 struct pointer_map_t *sccstate;
4315 struct obstack sccstate_obstack;
4316 hashval_t val;
4317 void **slot;
4318 struct tree_int_map m;
4319
4320 if (type_hash_cache == NULL)
4321 type_hash_cache = htab_create_ggc (512, tree_int_map_hash,
4322 tree_int_map_eq, NULL);
4323
4324 m.base.from = CONST_CAST_TREE (t);
4325 if ((slot = htab_find_slot (type_hash_cache, &m, NO_INSERT))
4326 && *slot)
4327 return iterative_hash_hashval_t (((struct tree_int_map *) *slot)->to, 0);
4328
4329 /* Perform a DFS walk and pre-hash all reachable types. */
4330 next_dfs_num = 1;
4331 sccstate = pointer_map_create ();
4332 gcc_obstack_init (&sccstate_obstack);
4333 val = iterative_hash_gimple_type (CONST_CAST_TREE (t), 0,
4334 &sccstack, sccstate, &sccstate_obstack);
4335 VEC_free (tree, heap, sccstack);
4336 pointer_map_destroy (sccstate);
4337 obstack_free (&sccstate_obstack, NULL);
4338
4339 return val;
4340 }
4341
4342 /* Returning a hash value for gimple type TYPE combined with VAL.
4343
4344 The hash value returned is equal for types considered compatible
4345 by gimple_canonical_types_compatible_p. */
4346
4347 static hashval_t
4348 iterative_hash_canonical_type (tree type, hashval_t val)
4349 {
4350 hashval_t v;
4351 void **slot;
4352 struct tree_int_map *mp, m;
4353
4354 m.base.from = type;
4355 if ((slot = htab_find_slot (canonical_type_hash_cache, &m, INSERT))
4356 && *slot)
4357 return iterative_hash_hashval_t (((struct tree_int_map *) *slot)->to, val);
4358
4359 /* Combine a few common features of types so that types are grouped into
4360 smaller sets; when searching for existing matching types to merge,
4361 only existing types having the same features as the new type will be
4362 checked. */
4363 v = iterative_hash_hashval_t (TREE_CODE (type), 0);
4364 v = iterative_hash_hashval_t (TREE_ADDRESSABLE (type), v);
4365 v = iterative_hash_hashval_t (TYPE_ALIGN (type), v);
4366 v = iterative_hash_hashval_t (TYPE_MODE (type), v);
4367
4368 /* Incorporate common features of numerical types. */
4369 if (INTEGRAL_TYPE_P (type)
4370 || SCALAR_FLOAT_TYPE_P (type)
4371 || FIXED_POINT_TYPE_P (type)
4372 || TREE_CODE (type) == VECTOR_TYPE
4373 || TREE_CODE (type) == COMPLEX_TYPE
4374 || TREE_CODE (type) == OFFSET_TYPE
4375 || POINTER_TYPE_P (type))
4376 {
4377 v = iterative_hash_hashval_t (TYPE_PRECISION (type), v);
4378 v = iterative_hash_hashval_t (TYPE_UNSIGNED (type), v);
4379 }
4380
4381 /* For pointer and reference types, fold in information about the type
4382 pointed to but do not recurse to the pointed-to type. */
4383 if (POINTER_TYPE_P (type))
4384 {
4385 v = iterative_hash_hashval_t (TYPE_REF_CAN_ALIAS_ALL (type), v);
4386 v = iterative_hash_hashval_t (TYPE_ADDR_SPACE (TREE_TYPE (type)), v);
4387 v = iterative_hash_hashval_t (TYPE_RESTRICT (type), v);
4388 v = iterative_hash_hashval_t (TREE_CODE (TREE_TYPE (type)), v);
4389 }
4390
4391 /* For integer types hash the types min/max values and the string flag. */
4392 if (TREE_CODE (type) == INTEGER_TYPE)
4393 {
4394 v = iterative_hash_hashval_t (TYPE_STRING_FLAG (type), v);
4395 v = iterative_hash_hashval_t (TYPE_IS_SIZETYPE (type), v);
4396 }
4397
4398 /* For array types hash their domain and the string flag. */
4399 if (TREE_CODE (type) == ARRAY_TYPE
4400 && TYPE_DOMAIN (type))
4401 {
4402 v = iterative_hash_hashval_t (TYPE_STRING_FLAG (type), v);
4403 v = iterative_hash_canonical_type (TYPE_DOMAIN (type), v);
4404 }
4405
4406 /* Recurse for aggregates with a single element type. */
4407 if (TREE_CODE (type) == ARRAY_TYPE
4408 || TREE_CODE (type) == COMPLEX_TYPE
4409 || TREE_CODE (type) == VECTOR_TYPE)
4410 v = iterative_hash_canonical_type (TREE_TYPE (type), v);
4411
4412 /* Incorporate function return and argument types. */
4413 if (TREE_CODE (type) == FUNCTION_TYPE || TREE_CODE (type) == METHOD_TYPE)
4414 {
4415 unsigned na;
4416 tree p;
4417
4418 /* For method types also incorporate their parent class. */
4419 if (TREE_CODE (type) == METHOD_TYPE)
4420 v = iterative_hash_canonical_type (TYPE_METHOD_BASETYPE (type), v);
4421
4422 /* For result types allow mismatch in completeness. */
4423 if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (type)))
4424 {
4425 v = iterative_hash_hashval_t (TREE_CODE (TREE_TYPE (type)), v);
4426 v = iterative_hash_name
4427 (TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (type))), v);
4428 }
4429 else
4430 v = iterative_hash_canonical_type (TREE_TYPE (type), v);
4431
4432 for (p = TYPE_ARG_TYPES (type), na = 0; p; p = TREE_CHAIN (p))
4433 {
4434 /* For argument types allow mismatch in completeness. */
4435 if (RECORD_OR_UNION_TYPE_P (TREE_VALUE (p)))
4436 {
4437 v = iterative_hash_hashval_t (TREE_CODE (TREE_VALUE (p)), v);
4438 v = iterative_hash_name
4439 (TYPE_NAME (TYPE_MAIN_VARIANT (TREE_VALUE (p))), v);
4440 }
4441 else
4442 v = iterative_hash_canonical_type (TREE_VALUE (p), v);
4443 na++;
4444 }
4445
4446 v = iterative_hash_hashval_t (na, v);
4447 }
4448
4449 if (TREE_CODE (type) == RECORD_TYPE
4450 || TREE_CODE (type) == UNION_TYPE
4451 || TREE_CODE (type) == QUAL_UNION_TYPE)
4452 {
4453 unsigned nf;
4454 tree f;
4455
4456 for (f = TYPE_FIELDS (type), nf = 0; f; f = TREE_CHAIN (f))
4457 {
4458 v = iterative_hash_canonical_type (TREE_TYPE (f), v);
4459 nf++;
4460 }
4461
4462 v = iterative_hash_hashval_t (nf, v);
4463 }
4464
4465 /* Cache the just computed hash value. */
4466 mp = ggc_alloc_cleared_tree_int_map ();
4467 mp->base.from = type;
4468 mp->to = v;
4469 *slot = (void *) mp;
4470
4471 return iterative_hash_hashval_t (v, val);
4472 }
4473
4474 static hashval_t
4475 gimple_canonical_type_hash (const void *p)
4476 {
4477 if (canonical_type_hash_cache == NULL)
4478 canonical_type_hash_cache = htab_create_ggc (512, tree_int_map_hash,
4479 tree_int_map_eq, NULL);
4480
4481 return iterative_hash_canonical_type (CONST_CAST_TREE ((const_tree) p), 0);
4482 }
4483
4484
4485 /* Returns nonzero if P1 and P2 are equal. */
4486
4487 static int
4488 gimple_type_eq (const void *p1, const void *p2)
4489 {
4490 const_tree t1 = (const_tree) p1;
4491 const_tree t2 = (const_tree) p2;
4492 return gimple_types_compatible_p (CONST_CAST_TREE (t1),
4493 CONST_CAST_TREE (t2));
4494 }
4495
4496
4497 /* Worker for gimple_register_type.
4498 Register type T in the global type table gimple_types.
4499 When REGISTERING_MV is false first recurse for the main variant of T. */
4500
4501 static tree
4502 gimple_register_type_1 (tree t, bool registering_mv)
4503 {
4504 void **slot;
4505 gimple_type_leader_entry *leader;
4506 tree mv_leader = NULL_TREE;
4507
4508 /* If we registered this type before return the cached result. */
4509 leader = &gimple_type_leader[TYPE_UID (t) % GIMPLE_TYPE_LEADER_SIZE];
4510 if (leader->type == t)
4511 return leader->leader;
4512
4513 /* Always register the main variant first. This is important so we
4514 pick up the non-typedef variants as canonical, otherwise we'll end
4515 up taking typedef ids for structure tags during comparison.
4516 It also makes sure that main variants will be merged to main variants.
4517 As we are operating on a possibly partially fixed up type graph
4518 do not bother to recurse more than once, otherwise we may end up
4519 walking in circles. */
4520 if (!registering_mv
4521 && TYPE_MAIN_VARIANT (t) != t)
4522 mv_leader = gimple_register_type_1 (TYPE_MAIN_VARIANT (t), true);
4523
4524 slot = htab_find_slot (gimple_types, t, INSERT);
4525 if (*slot
4526 && *(tree *)slot != t)
4527 {
4528 tree new_type = (tree) *((tree *) slot);
4529
4530 /* Do not merge types with different addressability. */
4531 gcc_assert (TREE_ADDRESSABLE (t) == TREE_ADDRESSABLE (new_type));
4532
4533 /* If t is not its main variant then make t unreachable from its
4534 main variant list. Otherwise we'd queue up a lot of duplicates
4535 there. */
4536 if (t != TYPE_MAIN_VARIANT (t))
4537 {
4538 tree tem = TYPE_MAIN_VARIANT (t);
4539 while (tem && TYPE_NEXT_VARIANT (tem) != t)
4540 tem = TYPE_NEXT_VARIANT (tem);
4541 if (tem)
4542 TYPE_NEXT_VARIANT (tem) = TYPE_NEXT_VARIANT (t);
4543 TYPE_NEXT_VARIANT (t) = NULL_TREE;
4544 }
4545
4546 /* If we are a pointer then remove us from the pointer-to or
4547 reference-to chain. Otherwise we'd queue up a lot of duplicates
4548 there. */
4549 if (TREE_CODE (t) == POINTER_TYPE)
4550 {
4551 if (TYPE_POINTER_TO (TREE_TYPE (t)) == t)
4552 TYPE_POINTER_TO (TREE_TYPE (t)) = TYPE_NEXT_PTR_TO (t);
4553 else
4554 {
4555 tree tem = TYPE_POINTER_TO (TREE_TYPE (t));
4556 while (tem && TYPE_NEXT_PTR_TO (tem) != t)
4557 tem = TYPE_NEXT_PTR_TO (tem);
4558 if (tem)
4559 TYPE_NEXT_PTR_TO (tem) = TYPE_NEXT_PTR_TO (t);
4560 }
4561 TYPE_NEXT_PTR_TO (t) = NULL_TREE;
4562 }
4563 else if (TREE_CODE (t) == REFERENCE_TYPE)
4564 {
4565 if (TYPE_REFERENCE_TO (TREE_TYPE (t)) == t)
4566 TYPE_REFERENCE_TO (TREE_TYPE (t)) = TYPE_NEXT_REF_TO (t);
4567 else
4568 {
4569 tree tem = TYPE_REFERENCE_TO (TREE_TYPE (t));
4570 while (tem && TYPE_NEXT_REF_TO (tem) != t)
4571 tem = TYPE_NEXT_REF_TO (tem);
4572 if (tem)
4573 TYPE_NEXT_REF_TO (tem) = TYPE_NEXT_REF_TO (t);
4574 }
4575 TYPE_NEXT_REF_TO (t) = NULL_TREE;
4576 }
4577
4578 leader->type = t;
4579 leader->leader = new_type;
4580 t = new_type;
4581 }
4582 else
4583 {
4584 leader->type = t;
4585 leader->leader = t;
4586 /* We're the type leader. Make our TYPE_MAIN_VARIANT valid. */
4587 if (TYPE_MAIN_VARIANT (t) != t
4588 && TYPE_MAIN_VARIANT (t) != mv_leader)
4589 {
4590 /* Remove us from our main variant list as we are not the variant
4591 leader and the variant leader will change. */
4592 tree tem = TYPE_MAIN_VARIANT (t);
4593 while (tem && TYPE_NEXT_VARIANT (tem) != t)
4594 tem = TYPE_NEXT_VARIANT (tem);
4595 if (tem)
4596 TYPE_NEXT_VARIANT (tem) = TYPE_NEXT_VARIANT (t);
4597 TYPE_NEXT_VARIANT (t) = NULL_TREE;
4598 /* Adjust our main variant. Linking us into its variant list
4599 will happen at fixup time. */
4600 TYPE_MAIN_VARIANT (t) = mv_leader;
4601 }
4602 *slot = (void *) t;
4603 }
4604
4605 return t;
4606 }
4607
4608 /* Register type T in the global type table gimple_types.
4609 If another type T', compatible with T, already existed in
4610 gimple_types then return T', otherwise return T. This is used by
4611 LTO to merge identical types read from different TUs. */
4612
4613 tree
4614 gimple_register_type (tree t)
4615 {
4616 gcc_assert (TYPE_P (t));
4617
4618 if (!gimple_type_leader)
4619 gimple_type_leader = ggc_alloc_cleared_vec_gimple_type_leader_entry_s
4620 (GIMPLE_TYPE_LEADER_SIZE);
4621
4622 if (gimple_types == NULL)
4623 gimple_types = htab_create_ggc (16381, gimple_type_hash, gimple_type_eq, 0);
4624
4625 return gimple_register_type_1 (t, false);
4626 }
4627
4628 /* The TYPE_CANONICAL merging machinery. It should closely resemble
4629 the middle-end types_compatible_p function. It needs to avoid
4630 claiming types are different for types that should be treated
4631 the same with respect to TBAA. Canonical types are also used
4632 for IL consistency checks via the useless_type_conversion_p
4633 predicate which does not handle all type kinds itself but falls
4634 back to pointer-comparison of TYPE_CANONICAL for aggregates
4635 for example. */
4636
4637 /* Return true iff T1 and T2 are structurally identical for what
4638 TBAA is concerned. */
4639
4640 static bool
4641 gimple_canonical_types_compatible_p (tree t1, tree t2)
4642 {
4643 /* Before starting to set up the SCC machinery handle simple cases. */
4644
4645 /* Check first for the obvious case of pointer identity. */
4646 if (t1 == t2)
4647 return true;
4648
4649 /* Check that we have two types to compare. */
4650 if (t1 == NULL_TREE || t2 == NULL_TREE)
4651 return false;
4652
4653 /* If the types have been previously registered and found equal
4654 they still are. */
4655 if (TYPE_CANONICAL (t1)
4656 && TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2))
4657 return true;
4658
4659 /* Can't be the same type if the types don't have the same code. */
4660 if (TREE_CODE (t1) != TREE_CODE (t2))
4661 return false;
4662
4663 if (TREE_ADDRESSABLE (t1) != TREE_ADDRESSABLE (t2))
4664 return false;
4665
4666 /* Qualifiers do not matter for canonical type comparison purposes. */
4667
4668 /* Void types and nullptr types are always the same. */
4669 if (TREE_CODE (t1) == VOID_TYPE
4670 || TREE_CODE (t1) == NULLPTR_TYPE)
4671 return true;
4672
4673 /* Can't be the same type if they have different alignment, or mode. */
4674 if (TYPE_ALIGN (t1) != TYPE_ALIGN (t2)
4675 || TYPE_MODE (t1) != TYPE_MODE (t2))
4676 return false;
4677
4678 /* Non-aggregate types can be handled cheaply. */
4679 if (INTEGRAL_TYPE_P (t1)
4680 || SCALAR_FLOAT_TYPE_P (t1)
4681 || FIXED_POINT_TYPE_P (t1)
4682 || TREE_CODE (t1) == VECTOR_TYPE
4683 || TREE_CODE (t1) == COMPLEX_TYPE
4684 || TREE_CODE (t1) == OFFSET_TYPE
4685 || POINTER_TYPE_P (t1))
4686 {
4687 /* Can't be the same type if they have different sign or precision. */
4688 if (TYPE_PRECISION (t1) != TYPE_PRECISION (t2)
4689 || TYPE_UNSIGNED (t1) != TYPE_UNSIGNED (t2))
4690 return false;
4691
4692 if (TREE_CODE (t1) == INTEGER_TYPE
4693 && (TYPE_IS_SIZETYPE (t1) != TYPE_IS_SIZETYPE (t2)
4694 || TYPE_STRING_FLAG (t1) != TYPE_STRING_FLAG (t2)))
4695 return false;
4696
4697 /* For canonical type comparisons we do not want to build SCCs
4698 so we cannot compare pointed-to types. But we can, for now,
4699 require the same pointed-to type kind and match what
4700 useless_type_conversion_p would do. */
4701 if (POINTER_TYPE_P (t1))
4702 {
4703 /* If the two pointers have different ref-all attributes,
4704 they can't be the same type. */
4705 if (TYPE_REF_CAN_ALIAS_ALL (t1) != TYPE_REF_CAN_ALIAS_ALL (t2))
4706 return false;
4707
4708 if (TYPE_ADDR_SPACE (TREE_TYPE (t1))
4709 != TYPE_ADDR_SPACE (TREE_TYPE (t2)))
4710 return false;
4711
4712 if (TYPE_RESTRICT (t1) != TYPE_RESTRICT (t2))
4713 return false;
4714
4715 if (TREE_CODE (TREE_TYPE (t1)) != TREE_CODE (TREE_TYPE (t2)))
4716 return false;
4717 }
4718
4719 /* Tail-recurse to components. */
4720 if (TREE_CODE (t1) == VECTOR_TYPE
4721 || TREE_CODE (t1) == COMPLEX_TYPE)
4722 return gimple_canonical_types_compatible_p (TREE_TYPE (t1),
4723 TREE_TYPE (t2));
4724
4725 return true;
4726 }
4727
4728 /* If their attributes are not the same they can't be the same type. */
4729 if (!attribute_list_equal (TYPE_ATTRIBUTES (t1), TYPE_ATTRIBUTES (t2)))
4730 return false;
4731
4732 /* Do type-specific comparisons. */
4733 switch (TREE_CODE (t1))
4734 {
4735 case ARRAY_TYPE:
4736 /* Array types are the same if the element types are the same and
4737 the number of elements are the same. */
4738 if (!gimple_canonical_types_compatible_p (TREE_TYPE (t1), TREE_TYPE (t2))
4739 || TYPE_STRING_FLAG (t1) != TYPE_STRING_FLAG (t2)
4740 || TYPE_NONALIASED_COMPONENT (t1) != TYPE_NONALIASED_COMPONENT (t2))
4741 return false;
4742 else
4743 {
4744 tree i1 = TYPE_DOMAIN (t1);
4745 tree i2 = TYPE_DOMAIN (t2);
4746
4747 /* For an incomplete external array, the type domain can be
4748 NULL_TREE. Check this condition also. */
4749 if (i1 == NULL_TREE && i2 == NULL_TREE)
4750 return true;
4751 else if (i1 == NULL_TREE || i2 == NULL_TREE)
4752 return false;
4753 /* If for a complete array type the possibly gimplified sizes
4754 are different the types are different. */
4755 else if (((TYPE_SIZE (i1) != NULL) ^ (TYPE_SIZE (i2) != NULL))
4756 || (TYPE_SIZE (i1)
4757 && TYPE_SIZE (i2)
4758 && !operand_equal_p (TYPE_SIZE (i1), TYPE_SIZE (i2), 0)))
4759 return false;
4760 else
4761 {
4762 tree min1 = TYPE_MIN_VALUE (i1);
4763 tree min2 = TYPE_MIN_VALUE (i2);
4764 tree max1 = TYPE_MAX_VALUE (i1);
4765 tree max2 = TYPE_MAX_VALUE (i2);
4766
4767 /* The minimum/maximum values have to be the same. */
4768 if ((min1 == min2
4769 || (min1 && min2
4770 && ((TREE_CODE (min1) == PLACEHOLDER_EXPR
4771 && TREE_CODE (min2) == PLACEHOLDER_EXPR)
4772 || operand_equal_p (min1, min2, 0))))
4773 && (max1 == max2
4774 || (max1 && max2
4775 && ((TREE_CODE (max1) == PLACEHOLDER_EXPR
4776 && TREE_CODE (max2) == PLACEHOLDER_EXPR)
4777 || operand_equal_p (max1, max2, 0)))))
4778 return true;
4779 else
4780 return false;
4781 }
4782 }
4783
4784 case METHOD_TYPE:
4785 /* Method types should belong to the same class. */
4786 if (!gimple_canonical_types_compatible_p
4787 (TYPE_METHOD_BASETYPE (t1), TYPE_METHOD_BASETYPE (t2)))
4788 return false;
4789
4790 /* Fallthru */
4791
4792 case FUNCTION_TYPE:
4793 /* Function types are the same if the return type and arguments types
4794 are the same. */
4795 if (!gimple_compatible_complete_and_incomplete_subtype_p
4796 (TREE_TYPE (t1), TREE_TYPE (t2))
4797 && !gimple_canonical_types_compatible_p
4798 (TREE_TYPE (t1), TREE_TYPE (t2)))
4799 return false;
4800
4801 if (!comp_type_attributes (t1, t2))
4802 return false;
4803
4804 if (TYPE_ARG_TYPES (t1) == TYPE_ARG_TYPES (t2))
4805 return true;
4806 else
4807 {
4808 tree parms1, parms2;
4809
4810 for (parms1 = TYPE_ARG_TYPES (t1), parms2 = TYPE_ARG_TYPES (t2);
4811 parms1 && parms2;
4812 parms1 = TREE_CHAIN (parms1), parms2 = TREE_CHAIN (parms2))
4813 {
4814 if (!gimple_compatible_complete_and_incomplete_subtype_p
4815 (TREE_VALUE (parms1), TREE_VALUE (parms2))
4816 && !gimple_canonical_types_compatible_p
4817 (TREE_VALUE (parms1), TREE_VALUE (parms2)))
4818 return false;
4819 }
4820
4821 if (parms1 || parms2)
4822 return false;
4823
4824 return true;
4825 }
4826
4827 case RECORD_TYPE:
4828 case UNION_TYPE:
4829 case QUAL_UNION_TYPE:
4830 {
4831 tree f1, f2;
4832
4833 /* For aggregate types, all the fields must be the same. */
4834 for (f1 = TYPE_FIELDS (t1), f2 = TYPE_FIELDS (t2);
4835 f1 && f2;
4836 f1 = TREE_CHAIN (f1), f2 = TREE_CHAIN (f2))
4837 {
4838 /* The fields must have the same name, offset and type. */
4839 if (DECL_NONADDRESSABLE_P (f1) != DECL_NONADDRESSABLE_P (f2)
4840 || !gimple_compare_field_offset (f1, f2)
4841 || !gimple_canonical_types_compatible_p
4842 (TREE_TYPE (f1), TREE_TYPE (f2)))
4843 return false;
4844 }
4845
4846 /* If one aggregate has more fields than the other, they
4847 are not the same. */
4848 if (f1 || f2)
4849 return false;
4850
4851 return true;
4852 }
4853
4854 default:
4855 gcc_unreachable ();
4856 }
4857 }
4858
4859
4860 /* Returns nonzero if P1 and P2 are equal. */
4861
4862 static int
4863 gimple_canonical_type_eq (const void *p1, const void *p2)
4864 {
4865 const_tree t1 = (const_tree) p1;
4866 const_tree t2 = (const_tree) p2;
4867 return gimple_canonical_types_compatible_p (CONST_CAST_TREE (t1),
4868 CONST_CAST_TREE (t2));
4869 }
4870
4871 /* Register type T in the global type table gimple_types.
4872 If another type T', compatible with T, already existed in
4873 gimple_types then return T', otherwise return T. This is used by
4874 LTO to merge identical types read from different TUs. */
4875
4876 tree
4877 gimple_register_canonical_type (tree t)
4878 {
4879 void **slot;
4880 tree orig_t = t;
4881
4882 gcc_assert (TYPE_P (t));
4883
4884 if (TYPE_CANONICAL (t))
4885 return TYPE_CANONICAL (t);
4886
4887 /* Use the leader of our main variant for determining our canonical
4888 type. The main variant leader is a type that will always
4889 prevail. */
4890 t = gimple_register_type (TYPE_MAIN_VARIANT (t));
4891
4892 if (TYPE_CANONICAL (t))
4893 return TYPE_CANONICAL (t);
4894
4895 if (gimple_canonical_types == NULL)
4896 gimple_canonical_types = htab_create_ggc (16381, gimple_canonical_type_hash,
4897 gimple_canonical_type_eq, 0);
4898
4899 slot = htab_find_slot (gimple_canonical_types, t, INSERT);
4900 if (*slot
4901 && *(tree *)slot != t)
4902 {
4903 tree new_type = (tree) *((tree *) slot);
4904
4905 TYPE_CANONICAL (t) = new_type;
4906 t = new_type;
4907 }
4908 else
4909 {
4910 TYPE_CANONICAL (t) = t;
4911 *slot = (void *) t;
4912 }
4913
4914 /* Also cache the canonical type in the non-leaders. */
4915 TYPE_CANONICAL (orig_t) = t;
4916
4917 return t;
4918 }
4919
4920
4921 /* Show statistics on references to the global type table gimple_types. */
4922
4923 void
4924 print_gimple_types_stats (void)
4925 {
4926 if (gimple_types)
4927 fprintf (stderr, "GIMPLE type table: size %ld, %ld elements, "
4928 "%ld searches, %ld collisions (ratio: %f)\n",
4929 (long) htab_size (gimple_types),
4930 (long) htab_elements (gimple_types),
4931 (long) gimple_types->searches,
4932 (long) gimple_types->collisions,
4933 htab_collisions (gimple_types));
4934 else
4935 fprintf (stderr, "GIMPLE type table is empty\n");
4936 if (type_hash_cache)
4937 fprintf (stderr, "GIMPLE type hash table: size %ld, %ld elements, "
4938 "%ld searches, %ld collisions (ratio: %f)\n",
4939 (long) htab_size (type_hash_cache),
4940 (long) htab_elements (type_hash_cache),
4941 (long) type_hash_cache->searches,
4942 (long) type_hash_cache->collisions,
4943 htab_collisions (type_hash_cache));
4944 else
4945 fprintf (stderr, "GIMPLE type hash table is empty\n");
4946 if (gimple_canonical_types)
4947 fprintf (stderr, "GIMPLE canonical type table: size %ld, %ld elements, "
4948 "%ld searches, %ld collisions (ratio: %f)\n",
4949 (long) htab_size (gimple_canonical_types),
4950 (long) htab_elements (gimple_canonical_types),
4951 (long) gimple_canonical_types->searches,
4952 (long) gimple_canonical_types->collisions,
4953 htab_collisions (gimple_canonical_types));
4954 else
4955 fprintf (stderr, "GIMPLE canonical type table is empty\n");
4956 if (canonical_type_hash_cache)
4957 fprintf (stderr, "GIMPLE canonical type hash table: size %ld, %ld elements, "
4958 "%ld searches, %ld collisions (ratio: %f)\n",
4959 (long) htab_size (canonical_type_hash_cache),
4960 (long) htab_elements (canonical_type_hash_cache),
4961 (long) canonical_type_hash_cache->searches,
4962 (long) canonical_type_hash_cache->collisions,
4963 htab_collisions (canonical_type_hash_cache));
4964 else
4965 fprintf (stderr, "GIMPLE canonical type hash table is empty\n");
4966 if (gtc_visited)
4967 fprintf (stderr, "GIMPLE type comparison table: size %ld, %ld "
4968 "elements, %ld searches, %ld collisions (ratio: %f)\n",
4969 (long) htab_size (gtc_visited),
4970 (long) htab_elements (gtc_visited),
4971 (long) gtc_visited->searches,
4972 (long) gtc_visited->collisions,
4973 htab_collisions (gtc_visited));
4974 else
4975 fprintf (stderr, "GIMPLE type comparison table is empty\n");
4976 }
4977
4978 /* Free the gimple type hashtables used for LTO type merging. */
4979
4980 void
4981 free_gimple_type_tables (void)
4982 {
4983 /* Last chance to print stats for the tables. */
4984 if (flag_lto_report)
4985 print_gimple_types_stats ();
4986
4987 if (gimple_types)
4988 {
4989 htab_delete (gimple_types);
4990 gimple_types = NULL;
4991 }
4992 if (gimple_canonical_types)
4993 {
4994 htab_delete (gimple_canonical_types);
4995 gimple_canonical_types = NULL;
4996 }
4997 if (type_hash_cache)
4998 {
4999 htab_delete (type_hash_cache);
5000 type_hash_cache = NULL;
5001 }
5002 if (canonical_type_hash_cache)
5003 {
5004 htab_delete (canonical_type_hash_cache);
5005 canonical_type_hash_cache = NULL;
5006 }
5007 if (gtc_visited)
5008 {
5009 htab_delete (gtc_visited);
5010 obstack_free (&gtc_ob, NULL);
5011 gtc_visited = NULL;
5012 }
5013 gimple_type_leader = NULL;
5014 }
5015
5016
5017 /* Return a type the same as TYPE except unsigned or
5018 signed according to UNSIGNEDP. */
5019
5020 static tree
5021 gimple_signed_or_unsigned_type (bool unsignedp, tree type)
5022 {
5023 tree type1;
5024
5025 type1 = TYPE_MAIN_VARIANT (type);
5026 if (type1 == signed_char_type_node
5027 || type1 == char_type_node
5028 || type1 == unsigned_char_type_node)
5029 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
5030 if (type1 == integer_type_node || type1 == unsigned_type_node)
5031 return unsignedp ? unsigned_type_node : integer_type_node;
5032 if (type1 == short_integer_type_node || type1 == short_unsigned_type_node)
5033 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
5034 if (type1 == long_integer_type_node || type1 == long_unsigned_type_node)
5035 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
5036 if (type1 == long_long_integer_type_node
5037 || type1 == long_long_unsigned_type_node)
5038 return unsignedp
5039 ? long_long_unsigned_type_node
5040 : long_long_integer_type_node;
5041 if (int128_integer_type_node && (type1 == int128_integer_type_node || type1 == int128_unsigned_type_node))
5042 return unsignedp
5043 ? int128_unsigned_type_node
5044 : int128_integer_type_node;
5045 #if HOST_BITS_PER_WIDE_INT >= 64
5046 if (type1 == intTI_type_node || type1 == unsigned_intTI_type_node)
5047 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
5048 #endif
5049 if (type1 == intDI_type_node || type1 == unsigned_intDI_type_node)
5050 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
5051 if (type1 == intSI_type_node || type1 == unsigned_intSI_type_node)
5052 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
5053 if (type1 == intHI_type_node || type1 == unsigned_intHI_type_node)
5054 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
5055 if (type1 == intQI_type_node || type1 == unsigned_intQI_type_node)
5056 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
5057
5058 #define GIMPLE_FIXED_TYPES(NAME) \
5059 if (type1 == short_ ## NAME ## _type_node \
5060 || type1 == unsigned_short_ ## NAME ## _type_node) \
5061 return unsignedp ? unsigned_short_ ## NAME ## _type_node \
5062 : short_ ## NAME ## _type_node; \
5063 if (type1 == NAME ## _type_node \
5064 || type1 == unsigned_ ## NAME ## _type_node) \
5065 return unsignedp ? unsigned_ ## NAME ## _type_node \
5066 : NAME ## _type_node; \
5067 if (type1 == long_ ## NAME ## _type_node \
5068 || type1 == unsigned_long_ ## NAME ## _type_node) \
5069 return unsignedp ? unsigned_long_ ## NAME ## _type_node \
5070 : long_ ## NAME ## _type_node; \
5071 if (type1 == long_long_ ## NAME ## _type_node \
5072 || type1 == unsigned_long_long_ ## NAME ## _type_node) \
5073 return unsignedp ? unsigned_long_long_ ## NAME ## _type_node \
5074 : long_long_ ## NAME ## _type_node;
5075
5076 #define GIMPLE_FIXED_MODE_TYPES(NAME) \
5077 if (type1 == NAME ## _type_node \
5078 || type1 == u ## NAME ## _type_node) \
5079 return unsignedp ? u ## NAME ## _type_node \
5080 : NAME ## _type_node;
5081
5082 #define GIMPLE_FIXED_TYPES_SAT(NAME) \
5083 if (type1 == sat_ ## short_ ## NAME ## _type_node \
5084 || type1 == sat_ ## unsigned_short_ ## NAME ## _type_node) \
5085 return unsignedp ? sat_ ## unsigned_short_ ## NAME ## _type_node \
5086 : sat_ ## short_ ## NAME ## _type_node; \
5087 if (type1 == sat_ ## NAME ## _type_node \
5088 || type1 == sat_ ## unsigned_ ## NAME ## _type_node) \
5089 return unsignedp ? sat_ ## unsigned_ ## NAME ## _type_node \
5090 : sat_ ## NAME ## _type_node; \
5091 if (type1 == sat_ ## long_ ## NAME ## _type_node \
5092 || type1 == sat_ ## unsigned_long_ ## NAME ## _type_node) \
5093 return unsignedp ? sat_ ## unsigned_long_ ## NAME ## _type_node \
5094 : sat_ ## long_ ## NAME ## _type_node; \
5095 if (type1 == sat_ ## long_long_ ## NAME ## _type_node \
5096 || type1 == sat_ ## unsigned_long_long_ ## NAME ## _type_node) \
5097 return unsignedp ? sat_ ## unsigned_long_long_ ## NAME ## _type_node \
5098 : sat_ ## long_long_ ## NAME ## _type_node;
5099
5100 #define GIMPLE_FIXED_MODE_TYPES_SAT(NAME) \
5101 if (type1 == sat_ ## NAME ## _type_node \
5102 || type1 == sat_ ## u ## NAME ## _type_node) \
5103 return unsignedp ? sat_ ## u ## NAME ## _type_node \
5104 : sat_ ## NAME ## _type_node;
5105
5106 GIMPLE_FIXED_TYPES (fract);
5107 GIMPLE_FIXED_TYPES_SAT (fract);
5108 GIMPLE_FIXED_TYPES (accum);
5109 GIMPLE_FIXED_TYPES_SAT (accum);
5110
5111 GIMPLE_FIXED_MODE_TYPES (qq);
5112 GIMPLE_FIXED_MODE_TYPES (hq);
5113 GIMPLE_FIXED_MODE_TYPES (sq);
5114 GIMPLE_FIXED_MODE_TYPES (dq);
5115 GIMPLE_FIXED_MODE_TYPES (tq);
5116 GIMPLE_FIXED_MODE_TYPES_SAT (qq);
5117 GIMPLE_FIXED_MODE_TYPES_SAT (hq);
5118 GIMPLE_FIXED_MODE_TYPES_SAT (sq);
5119 GIMPLE_FIXED_MODE_TYPES_SAT (dq);
5120 GIMPLE_FIXED_MODE_TYPES_SAT (tq);
5121 GIMPLE_FIXED_MODE_TYPES (ha);
5122 GIMPLE_FIXED_MODE_TYPES (sa);
5123 GIMPLE_FIXED_MODE_TYPES (da);
5124 GIMPLE_FIXED_MODE_TYPES (ta);
5125 GIMPLE_FIXED_MODE_TYPES_SAT (ha);
5126 GIMPLE_FIXED_MODE_TYPES_SAT (sa);
5127 GIMPLE_FIXED_MODE_TYPES_SAT (da);
5128 GIMPLE_FIXED_MODE_TYPES_SAT (ta);
5129
5130 /* For ENUMERAL_TYPEs in C++, must check the mode of the types, not
5131 the precision; they have precision set to match their range, but
5132 may use a wider mode to match an ABI. If we change modes, we may
5133 wind up with bad conversions. For INTEGER_TYPEs in C, must check
5134 the precision as well, so as to yield correct results for
5135 bit-field types. C++ does not have these separate bit-field
5136 types, and producing a signed or unsigned variant of an
5137 ENUMERAL_TYPE may cause other problems as well. */
5138 if (!INTEGRAL_TYPE_P (type)
5139 || TYPE_UNSIGNED (type) == unsignedp)
5140 return type;
5141
5142 #define TYPE_OK(node) \
5143 (TYPE_MODE (type) == TYPE_MODE (node) \
5144 && TYPE_PRECISION (type) == TYPE_PRECISION (node))
5145 if (TYPE_OK (signed_char_type_node))
5146 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
5147 if (TYPE_OK (integer_type_node))
5148 return unsignedp ? unsigned_type_node : integer_type_node;
5149 if (TYPE_OK (short_integer_type_node))
5150 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
5151 if (TYPE_OK (long_integer_type_node))
5152 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
5153 if (TYPE_OK (long_long_integer_type_node))
5154 return (unsignedp
5155 ? long_long_unsigned_type_node
5156 : long_long_integer_type_node);
5157 if (int128_integer_type_node && TYPE_OK (int128_integer_type_node))
5158 return (unsignedp
5159 ? int128_unsigned_type_node
5160 : int128_integer_type_node);
5161
5162 #if HOST_BITS_PER_WIDE_INT >= 64
5163 if (TYPE_OK (intTI_type_node))
5164 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
5165 #endif
5166 if (TYPE_OK (intDI_type_node))
5167 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
5168 if (TYPE_OK (intSI_type_node))
5169 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
5170 if (TYPE_OK (intHI_type_node))
5171 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
5172 if (TYPE_OK (intQI_type_node))
5173 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
5174
5175 #undef GIMPLE_FIXED_TYPES
5176 #undef GIMPLE_FIXED_MODE_TYPES
5177 #undef GIMPLE_FIXED_TYPES_SAT
5178 #undef GIMPLE_FIXED_MODE_TYPES_SAT
5179 #undef TYPE_OK
5180
5181 return build_nonstandard_integer_type (TYPE_PRECISION (type), unsignedp);
5182 }
5183
5184
5185 /* Return an unsigned type the same as TYPE in other respects. */
5186
5187 tree
5188 gimple_unsigned_type (tree type)
5189 {
5190 return gimple_signed_or_unsigned_type (true, type);
5191 }
5192
5193
5194 /* Return a signed type the same as TYPE in other respects. */
5195
5196 tree
5197 gimple_signed_type (tree type)
5198 {
5199 return gimple_signed_or_unsigned_type (false, type);
5200 }
5201
5202
5203 /* Return the typed-based alias set for T, which may be an expression
5204 or a type. Return -1 if we don't do anything special. */
5205
5206 alias_set_type
5207 gimple_get_alias_set (tree t)
5208 {
5209 tree u;
5210
5211 /* Permit type-punning when accessing a union, provided the access
5212 is directly through the union. For example, this code does not
5213 permit taking the address of a union member and then storing
5214 through it. Even the type-punning allowed here is a GCC
5215 extension, albeit a common and useful one; the C standard says
5216 that such accesses have implementation-defined behavior. */
5217 for (u = t;
5218 TREE_CODE (u) == COMPONENT_REF || TREE_CODE (u) == ARRAY_REF;
5219 u = TREE_OPERAND (u, 0))
5220 if (TREE_CODE (u) == COMPONENT_REF
5221 && TREE_CODE (TREE_TYPE (TREE_OPERAND (u, 0))) == UNION_TYPE)
5222 return 0;
5223
5224 /* That's all the expressions we handle specially. */
5225 if (!TYPE_P (t))
5226 return -1;
5227
5228 /* For convenience, follow the C standard when dealing with
5229 character types. Any object may be accessed via an lvalue that
5230 has character type. */
5231 if (t == char_type_node
5232 || t == signed_char_type_node
5233 || t == unsigned_char_type_node)
5234 return 0;
5235
5236 /* Allow aliasing between signed and unsigned variants of the same
5237 type. We treat the signed variant as canonical. */
5238 if (TREE_CODE (t) == INTEGER_TYPE && TYPE_UNSIGNED (t))
5239 {
5240 tree t1 = gimple_signed_type (t);
5241
5242 /* t1 == t can happen for boolean nodes which are always unsigned. */
5243 if (t1 != t)
5244 return get_alias_set (t1);
5245 }
5246
5247 return -1;
5248 }
5249
5250
5251 /* Data structure used to count the number of dereferences to PTR
5252 inside an expression. */
5253 struct count_ptr_d
5254 {
5255 tree ptr;
5256 unsigned num_stores;
5257 unsigned num_loads;
5258 };
5259
5260 /* Helper for count_uses_and_derefs. Called by walk_tree to look for
5261 (ALIGN/MISALIGNED_)INDIRECT_REF nodes for the pointer passed in DATA. */
5262
5263 static tree
5264 count_ptr_derefs (tree *tp, int *walk_subtrees, void *data)
5265 {
5266 struct walk_stmt_info *wi_p = (struct walk_stmt_info *) data;
5267 struct count_ptr_d *count_p = (struct count_ptr_d *) wi_p->info;
5268
5269 /* Do not walk inside ADDR_EXPR nodes. In the expression &ptr->fld,
5270 pointer 'ptr' is *not* dereferenced, it is simply used to compute
5271 the address of 'fld' as 'ptr + offsetof(fld)'. */
5272 if (TREE_CODE (*tp) == ADDR_EXPR)
5273 {
5274 *walk_subtrees = 0;
5275 return NULL_TREE;
5276 }
5277
5278 if (TREE_CODE (*tp) == MEM_REF && TREE_OPERAND (*tp, 0) == count_p->ptr)
5279 {
5280 if (wi_p->is_lhs)
5281 count_p->num_stores++;
5282 else
5283 count_p->num_loads++;
5284 }
5285
5286 return NULL_TREE;
5287 }
5288
5289 /* Count the number of direct and indirect uses for pointer PTR in
5290 statement STMT. The number of direct uses is stored in
5291 *NUM_USES_P. Indirect references are counted separately depending
5292 on whether they are store or load operations. The counts are
5293 stored in *NUM_STORES_P and *NUM_LOADS_P. */
5294
5295 void
5296 count_uses_and_derefs (tree ptr, gimple stmt, unsigned *num_uses_p,
5297 unsigned *num_loads_p, unsigned *num_stores_p)
5298 {
5299 ssa_op_iter i;
5300 tree use;
5301
5302 *num_uses_p = 0;
5303 *num_loads_p = 0;
5304 *num_stores_p = 0;
5305
5306 /* Find out the total number of uses of PTR in STMT. */
5307 FOR_EACH_SSA_TREE_OPERAND (use, stmt, i, SSA_OP_USE)
5308 if (use == ptr)
5309 (*num_uses_p)++;
5310
5311 /* Now count the number of indirect references to PTR. This is
5312 truly awful, but we don't have much choice. There are no parent
5313 pointers inside INDIRECT_REFs, so an expression like
5314 '*x_1 = foo (x_1, *x_1)' needs to be traversed piece by piece to
5315 find all the indirect and direct uses of x_1 inside. The only
5316 shortcut we can take is the fact that GIMPLE only allows
5317 INDIRECT_REFs inside the expressions below. */
5318 if (is_gimple_assign (stmt)
5319 || gimple_code (stmt) == GIMPLE_RETURN
5320 || gimple_code (stmt) == GIMPLE_ASM
5321 || is_gimple_call (stmt))
5322 {
5323 struct walk_stmt_info wi;
5324 struct count_ptr_d count;
5325
5326 count.ptr = ptr;
5327 count.num_stores = 0;
5328 count.num_loads = 0;
5329
5330 memset (&wi, 0, sizeof (wi));
5331 wi.info = &count;
5332 walk_gimple_op (stmt, count_ptr_derefs, &wi);
5333
5334 *num_stores_p = count.num_stores;
5335 *num_loads_p = count.num_loads;
5336 }
5337
5338 gcc_assert (*num_uses_p >= *num_loads_p + *num_stores_p);
5339 }
5340
5341 /* From a tree operand OP return the base of a load or store operation
5342 or NULL_TREE if OP is not a load or a store. */
5343
5344 static tree
5345 get_base_loadstore (tree op)
5346 {
5347 while (handled_component_p (op))
5348 op = TREE_OPERAND (op, 0);
5349 if (DECL_P (op)
5350 || INDIRECT_REF_P (op)
5351 || TREE_CODE (op) == MEM_REF
5352 || TREE_CODE (op) == TARGET_MEM_REF)
5353 return op;
5354 return NULL_TREE;
5355 }
5356
5357 /* For the statement STMT call the callbacks VISIT_LOAD, VISIT_STORE and
5358 VISIT_ADDR if non-NULL on loads, store and address-taken operands
5359 passing the STMT, the base of the operand and DATA to it. The base
5360 will be either a decl, an indirect reference (including TARGET_MEM_REF)
5361 or the argument of an address expression.
5362 Returns the results of these callbacks or'ed. */
5363
5364 bool
5365 walk_stmt_load_store_addr_ops (gimple stmt, void *data,
5366 bool (*visit_load)(gimple, tree, void *),
5367 bool (*visit_store)(gimple, tree, void *),
5368 bool (*visit_addr)(gimple, tree, void *))
5369 {
5370 bool ret = false;
5371 unsigned i;
5372 if (gimple_assign_single_p (stmt))
5373 {
5374 tree lhs, rhs;
5375 if (visit_store)
5376 {
5377 lhs = get_base_loadstore (gimple_assign_lhs (stmt));
5378 if (lhs)
5379 ret |= visit_store (stmt, lhs, data);
5380 }
5381 rhs = gimple_assign_rhs1 (stmt);
5382 while (handled_component_p (rhs))
5383 rhs = TREE_OPERAND (rhs, 0);
5384 if (visit_addr)
5385 {
5386 if (TREE_CODE (rhs) == ADDR_EXPR)
5387 ret |= visit_addr (stmt, TREE_OPERAND (rhs, 0), data);
5388 else if (TREE_CODE (rhs) == TARGET_MEM_REF
5389 && TREE_CODE (TMR_BASE (rhs)) == ADDR_EXPR)
5390 ret |= visit_addr (stmt, TREE_OPERAND (TMR_BASE (rhs), 0), data);
5391 else if (TREE_CODE (rhs) == OBJ_TYPE_REF
5392 && TREE_CODE (OBJ_TYPE_REF_OBJECT (rhs)) == ADDR_EXPR)
5393 ret |= visit_addr (stmt, TREE_OPERAND (OBJ_TYPE_REF_OBJECT (rhs),
5394 0), data);
5395 lhs = gimple_assign_lhs (stmt);
5396 if (TREE_CODE (lhs) == TARGET_MEM_REF
5397 && TREE_CODE (TMR_BASE (lhs)) == ADDR_EXPR)
5398 ret |= visit_addr (stmt, TREE_OPERAND (TMR_BASE (lhs), 0), data);
5399 }
5400 if (visit_load)
5401 {
5402 rhs = get_base_loadstore (rhs);
5403 if (rhs)
5404 ret |= visit_load (stmt, rhs, data);
5405 }
5406 }
5407 else if (visit_addr
5408 && (is_gimple_assign (stmt)
5409 || gimple_code (stmt) == GIMPLE_COND))
5410 {
5411 for (i = 0; i < gimple_num_ops (stmt); ++i)
5412 if (gimple_op (stmt, i)
5413 && TREE_CODE (gimple_op (stmt, i)) == ADDR_EXPR)
5414 ret |= visit_addr (stmt, TREE_OPERAND (gimple_op (stmt, i), 0), data);
5415 }
5416 else if (is_gimple_call (stmt))
5417 {
5418 if (visit_store)
5419 {
5420 tree lhs = gimple_call_lhs (stmt);
5421 if (lhs)
5422 {
5423 lhs = get_base_loadstore (lhs);
5424 if (lhs)
5425 ret |= visit_store (stmt, lhs, data);
5426 }
5427 }
5428 if (visit_load || visit_addr)
5429 for (i = 0; i < gimple_call_num_args (stmt); ++i)
5430 {
5431 tree rhs = gimple_call_arg (stmt, i);
5432 if (visit_addr
5433 && TREE_CODE (rhs) == ADDR_EXPR)
5434 ret |= visit_addr (stmt, TREE_OPERAND (rhs, 0), data);
5435 else if (visit_load)
5436 {
5437 rhs = get_base_loadstore (rhs);
5438 if (rhs)
5439 ret |= visit_load (stmt, rhs, data);
5440 }
5441 }
5442 if (visit_addr
5443 && gimple_call_chain (stmt)
5444 && TREE_CODE (gimple_call_chain (stmt)) == ADDR_EXPR)
5445 ret |= visit_addr (stmt, TREE_OPERAND (gimple_call_chain (stmt), 0),
5446 data);
5447 if (visit_addr
5448 && gimple_call_return_slot_opt_p (stmt)
5449 && gimple_call_lhs (stmt) != NULL_TREE
5450 && TREE_ADDRESSABLE (TREE_TYPE (gimple_call_lhs (stmt))))
5451 ret |= visit_addr (stmt, gimple_call_lhs (stmt), data);
5452 }
5453 else if (gimple_code (stmt) == GIMPLE_ASM)
5454 {
5455 unsigned noutputs;
5456 const char *constraint;
5457 const char **oconstraints;
5458 bool allows_mem, allows_reg, is_inout;
5459 noutputs = gimple_asm_noutputs (stmt);
5460 oconstraints = XALLOCAVEC (const char *, noutputs);
5461 if (visit_store || visit_addr)
5462 for (i = 0; i < gimple_asm_noutputs (stmt); ++i)
5463 {
5464 tree link = gimple_asm_output_op (stmt, i);
5465 tree op = get_base_loadstore (TREE_VALUE (link));
5466 if (op && visit_store)
5467 ret |= visit_store (stmt, op, data);
5468 if (visit_addr)
5469 {
5470 constraint = TREE_STRING_POINTER
5471 (TREE_VALUE (TREE_PURPOSE (link)));
5472 oconstraints[i] = constraint;
5473 parse_output_constraint (&constraint, i, 0, 0, &allows_mem,
5474 &allows_reg, &is_inout);
5475 if (op && !allows_reg && allows_mem)
5476 ret |= visit_addr (stmt, op, data);
5477 }
5478 }
5479 if (visit_load || visit_addr)
5480 for (i = 0; i < gimple_asm_ninputs (stmt); ++i)
5481 {
5482 tree link = gimple_asm_input_op (stmt, i);
5483 tree op = TREE_VALUE (link);
5484 if (visit_addr
5485 && TREE_CODE (op) == ADDR_EXPR)
5486 ret |= visit_addr (stmt, TREE_OPERAND (op, 0), data);
5487 else if (visit_load || visit_addr)
5488 {
5489 op = get_base_loadstore (op);
5490 if (op)
5491 {
5492 if (visit_load)
5493 ret |= visit_load (stmt, op, data);
5494 if (visit_addr)
5495 {
5496 constraint = TREE_STRING_POINTER
5497 (TREE_VALUE (TREE_PURPOSE (link)));
5498 parse_input_constraint (&constraint, 0, 0, noutputs,
5499 0, oconstraints,
5500 &allows_mem, &allows_reg);
5501 if (!allows_reg && allows_mem)
5502 ret |= visit_addr (stmt, op, data);
5503 }
5504 }
5505 }
5506 }
5507 }
5508 else if (gimple_code (stmt) == GIMPLE_RETURN)
5509 {
5510 tree op = gimple_return_retval (stmt);
5511 if (op)
5512 {
5513 if (visit_addr
5514 && TREE_CODE (op) == ADDR_EXPR)
5515 ret |= visit_addr (stmt, TREE_OPERAND (op, 0), data);
5516 else if (visit_load)
5517 {
5518 op = get_base_loadstore (op);
5519 if (op)
5520 ret |= visit_load (stmt, op, data);
5521 }
5522 }
5523 }
5524 else if (visit_addr
5525 && gimple_code (stmt) == GIMPLE_PHI)
5526 {
5527 for (i = 0; i < gimple_phi_num_args (stmt); ++i)
5528 {
5529 tree op = PHI_ARG_DEF (stmt, i);
5530 if (TREE_CODE (op) == ADDR_EXPR)
5531 ret |= visit_addr (stmt, TREE_OPERAND (op, 0), data);
5532 }
5533 }
5534
5535 return ret;
5536 }
5537
5538 /* Like walk_stmt_load_store_addr_ops but with NULL visit_addr. IPA-CP
5539 should make a faster clone for this case. */
5540
5541 bool
5542 walk_stmt_load_store_ops (gimple stmt, void *data,
5543 bool (*visit_load)(gimple, tree, void *),
5544 bool (*visit_store)(gimple, tree, void *))
5545 {
5546 return walk_stmt_load_store_addr_ops (stmt, data,
5547 visit_load, visit_store, NULL);
5548 }
5549
5550 /* Helper for gimple_ior_addresses_taken_1. */
5551
5552 static bool
5553 gimple_ior_addresses_taken_1 (gimple stmt ATTRIBUTE_UNUSED,
5554 tree addr, void *data)
5555 {
5556 bitmap addresses_taken = (bitmap)data;
5557 addr = get_base_address (addr);
5558 if (addr
5559 && DECL_P (addr))
5560 {
5561 bitmap_set_bit (addresses_taken, DECL_UID (addr));
5562 return true;
5563 }
5564 return false;
5565 }
5566
5567 /* Set the bit for the uid of all decls that have their address taken
5568 in STMT in the ADDRESSES_TAKEN bitmap. Returns true if there
5569 were any in this stmt. */
5570
5571 bool
5572 gimple_ior_addresses_taken (bitmap addresses_taken, gimple stmt)
5573 {
5574 return walk_stmt_load_store_addr_ops (stmt, addresses_taken, NULL, NULL,
5575 gimple_ior_addresses_taken_1);
5576 }
5577
5578
5579 /* Return a printable name for symbol DECL. */
5580
5581 const char *
5582 gimple_decl_printable_name (tree decl, int verbosity)
5583 {
5584 if (!DECL_NAME (decl))
5585 return NULL;
5586
5587 if (DECL_ASSEMBLER_NAME_SET_P (decl))
5588 {
5589 const char *str, *mangled_str;
5590 int dmgl_opts = DMGL_NO_OPTS;
5591
5592 if (verbosity >= 2)
5593 {
5594 dmgl_opts = DMGL_VERBOSE
5595 | DMGL_ANSI
5596 | DMGL_GNU_V3
5597 | DMGL_RET_POSTFIX;
5598 if (TREE_CODE (decl) == FUNCTION_DECL)
5599 dmgl_opts |= DMGL_PARAMS;
5600 }
5601
5602 mangled_str = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
5603 str = cplus_demangle_v3 (mangled_str, dmgl_opts);
5604 return (str) ? str : mangled_str;
5605 }
5606
5607 return IDENTIFIER_POINTER (DECL_NAME (decl));
5608 }
5609
5610 /* Return true when STMT is builtins call to CODE. */
5611
5612 bool
5613 gimple_call_builtin_p (gimple stmt, enum built_in_function code)
5614 {
5615 tree fndecl;
5616 return (is_gimple_call (stmt)
5617 && (fndecl = gimple_call_fndecl (stmt)) != NULL
5618 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
5619 && DECL_FUNCTION_CODE (fndecl) == code);
5620 }
5621
5622 /* Return true if STMT clobbers memory. STMT is required to be a
5623 GIMPLE_ASM. */
5624
5625 bool
5626 gimple_asm_clobbers_memory_p (const_gimple stmt)
5627 {
5628 unsigned i;
5629
5630 for (i = 0; i < gimple_asm_nclobbers (stmt); i++)
5631 {
5632 tree op = gimple_asm_clobber_op (stmt, i);
5633 if (strcmp (TREE_STRING_POINTER (TREE_VALUE (op)), "memory") == 0)
5634 return true;
5635 }
5636
5637 return false;
5638 }
5639 #include "gt-gimple.h"