OSDN Git Service

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