OSDN Git Service

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