OSDN Git Service

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