OSDN Git Service

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