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