OSDN Git Service

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