OSDN Git Service

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