OSDN Git Service

2009-07-08 Manuel López-Ibáñez <manu@gcc.gnu.org>
[pf3gnuchains/gcc-fork.git] / gcc / gimple.c
1 /* Gimple IR support functions.
2
3    Copyright 2007, 2008, 2009 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 "tree.h"
27 #include "ggc.h"
28 #include "hard-reg-set.h"
29 #include "basic-block.h"
30 #include "gimple.h"
31 #include "toplev.h"
32 #include "diagnostic.h"
33 #include "tree-flow.h"
34 #include "value-prof.h"
35 #include "flags.h"
36
37 #define DEFGSCODE(SYM, NAME, STRUCT)    NAME,
38 const char *const gimple_code_name[] = {
39 #include "gimple.def"
40 };
41 #undef DEFGSCODE
42
43 /* All the tuples have their operand vector at the very bottom
44    of the structure.  Therefore, the offset required to find the
45    operands vector the size of the structure minus the size of the 1
46    element tree array at the end (see gimple_ops).  */
47 #define DEFGSCODE(SYM, NAME, STRUCT)    (sizeof (STRUCT) - sizeof (tree)),
48 EXPORTED_CONST size_t gimple_ops_offset_[] = {
49 #include "gimple.def"
50 };
51 #undef DEFGSCODE
52
53 #ifdef GATHER_STATISTICS
54 /* Gimple stats.  */
55
56 int gimple_alloc_counts[(int) gimple_alloc_kind_all];
57 int gimple_alloc_sizes[(int) gimple_alloc_kind_all];
58
59 /* Keep in sync with gimple.h:enum gimple_alloc_kind.  */
60 static const char * const gimple_alloc_kind_names[] = {
61     "assignments",
62     "phi nodes",
63     "conditionals",
64     "sequences",
65     "everything else"
66 };
67
68 #endif /* GATHER_STATISTICS */
69
70 /* A cache of gimple_seq objects.  Sequences are created and destroyed
71    fairly often during gimplification.  */
72 static GTY ((deletable)) struct gimple_seq_d *gimple_seq_cache;
73
74 /* Private API manipulation functions shared only with some
75    other files.  */
76 extern void gimple_set_stored_syms (gimple, bitmap, bitmap_obstack *);
77 extern void gimple_set_loaded_syms (gimple, bitmap, bitmap_obstack *);
78
79 /* Gimple tuple constructors.
80    Note: Any constructor taking a ``gimple_seq'' as a parameter, can
81    be passed a NULL to start with an empty sequence.  */
82
83 /* Set the code for statement G to CODE.  */
84
85 static inline void
86 gimple_set_code (gimple g, enum gimple_code code)
87 {
88   g->gsbase.code = code;
89 }
90
91
92 /* Return the GSS_* identifier for the given GIMPLE statement CODE.  */
93
94 static enum gimple_statement_structure_enum
95 gss_for_code (enum gimple_code code)
96 {
97   switch (code)
98     {
99     case GIMPLE_ASSIGN:
100     case GIMPLE_CALL:
101     case GIMPLE_RETURN:                 return GSS_WITH_MEM_OPS;
102     case GIMPLE_COND:
103     case GIMPLE_GOTO:
104     case GIMPLE_LABEL:
105     case GIMPLE_SWITCH:                 return GSS_WITH_OPS;
106     case GIMPLE_ASM:                    return GSS_ASM;
107     case GIMPLE_BIND:                   return GSS_BIND;
108     case GIMPLE_CATCH:                  return GSS_CATCH;
109     case GIMPLE_EH_FILTER:              return GSS_EH_FILTER;
110     case GIMPLE_NOP:                    return GSS_BASE;
111     case GIMPLE_PHI:                    return GSS_PHI;
112     case GIMPLE_RESX:                   return GSS_RESX;
113     case GIMPLE_TRY:                    return GSS_TRY;
114     case GIMPLE_WITH_CLEANUP_EXPR:      return GSS_WCE;
115     case GIMPLE_OMP_CRITICAL:           return GSS_OMP_CRITICAL;
116     case GIMPLE_OMP_FOR:                return GSS_OMP_FOR;
117     case GIMPLE_OMP_MASTER:             
118     case GIMPLE_OMP_ORDERED:
119     case GIMPLE_OMP_SECTION:            return GSS_OMP;
120     case GIMPLE_OMP_RETURN:
121     case GIMPLE_OMP_SECTIONS_SWITCH:    return GSS_BASE;
122     case GIMPLE_OMP_CONTINUE:           return GSS_OMP_CONTINUE;
123     case GIMPLE_OMP_PARALLEL:           return GSS_OMP_PARALLEL;
124     case GIMPLE_OMP_TASK:               return GSS_OMP_TASK;
125     case GIMPLE_OMP_SECTIONS:           return GSS_OMP_SECTIONS;
126     case GIMPLE_OMP_SINGLE:             return GSS_OMP_SINGLE;
127     case GIMPLE_OMP_ATOMIC_LOAD:        return GSS_OMP_ATOMIC_LOAD;
128     case GIMPLE_OMP_ATOMIC_STORE:       return GSS_OMP_ATOMIC_STORE;
129     case GIMPLE_PREDICT:                return GSS_BASE;
130     default:                            gcc_unreachable ();
131     }
132 }
133
134
135 /* Return the number of bytes needed to hold a GIMPLE statement with
136    code CODE.  */
137
138 static size_t
139 gimple_size (enum gimple_code code)
140 {
141   enum gimple_statement_structure_enum gss = gss_for_code (code);
142
143   if (gss == GSS_WITH_OPS)
144     return sizeof (struct gimple_statement_with_ops);
145   else if (gss == GSS_WITH_MEM_OPS)
146     return sizeof (struct gimple_statement_with_memory_ops);
147
148   switch (code)
149     {
150     case GIMPLE_ASM:
151       return sizeof (struct gimple_statement_asm);
152     case GIMPLE_NOP:
153       return sizeof (struct gimple_statement_base);
154     case GIMPLE_BIND:
155       return sizeof (struct gimple_statement_bind);
156     case GIMPLE_CATCH:
157       return sizeof (struct gimple_statement_catch);
158     case GIMPLE_EH_FILTER:
159       return sizeof (struct gimple_statement_eh_filter);
160     case GIMPLE_TRY:
161       return sizeof (struct gimple_statement_try);
162     case GIMPLE_RESX:
163       return sizeof (struct gimple_statement_resx);
164     case GIMPLE_OMP_CRITICAL:
165       return sizeof (struct gimple_statement_omp_critical);
166     case GIMPLE_OMP_FOR:
167       return sizeof (struct gimple_statement_omp_for);
168     case GIMPLE_OMP_PARALLEL:
169       return sizeof (struct gimple_statement_omp_parallel);
170     case GIMPLE_OMP_TASK:
171       return sizeof (struct gimple_statement_omp_task);
172     case GIMPLE_OMP_SECTION:
173     case GIMPLE_OMP_MASTER:
174     case GIMPLE_OMP_ORDERED:
175       return sizeof (struct gimple_statement_omp);
176     case GIMPLE_OMP_RETURN:
177       return sizeof (struct gimple_statement_base);
178     case GIMPLE_OMP_CONTINUE:
179       return sizeof (struct gimple_statement_omp_continue);
180     case GIMPLE_OMP_SECTIONS:
181       return sizeof (struct gimple_statement_omp_sections);
182     case GIMPLE_OMP_SECTIONS_SWITCH:
183       return sizeof (struct gimple_statement_base);
184     case GIMPLE_OMP_SINGLE:
185       return sizeof (struct gimple_statement_omp_single);
186     case GIMPLE_OMP_ATOMIC_LOAD:
187       return sizeof (struct gimple_statement_omp_atomic_load);
188     case GIMPLE_OMP_ATOMIC_STORE:
189       return sizeof (struct gimple_statement_omp_atomic_store);
190     case GIMPLE_WITH_CLEANUP_EXPR:
191       return sizeof (struct gimple_statement_wce);
192     case GIMPLE_PREDICT:
193       return sizeof (struct gimple_statement_base);
194     default:
195       break;
196     }
197
198   gcc_unreachable ();
199 }
200
201
202 /* Allocate memory for a GIMPLE statement with code CODE and NUM_OPS
203    operands.  */
204
205 #define gimple_alloc(c, n) gimple_alloc_stat (c, n MEM_STAT_INFO)
206 static gimple
207 gimple_alloc_stat (enum gimple_code code, unsigned num_ops MEM_STAT_DECL)
208 {
209   size_t size;
210   gimple stmt;
211
212   size = gimple_size (code);
213   if (num_ops > 0)
214     size += sizeof (tree) * (num_ops - 1);
215
216 #ifdef GATHER_STATISTICS
217   {
218     enum gimple_alloc_kind kind = gimple_alloc_kind (code);
219     gimple_alloc_counts[(int) kind]++;
220     gimple_alloc_sizes[(int) kind] += size;
221   }
222 #endif
223
224   stmt = (gimple) ggc_alloc_cleared_stat (size PASS_MEM_STAT);
225   gimple_set_code (stmt, code);
226   gimple_set_num_ops (stmt, num_ops);
227
228   /* Do not call gimple_set_modified here as it has other side
229      effects and this tuple is still not completely built.  */
230   stmt->gsbase.modified = 1;
231
232   return stmt;
233 }
234
235 /* Set SUBCODE to be the code of the expression computed by statement G.  */
236
237 static inline void
238 gimple_set_subcode (gimple g, unsigned subcode)
239 {
240   /* We only have 16 bits for the RHS code.  Assert that we are not
241      overflowing it.  */
242   gcc_assert (subcode < (1 << 16));
243   g->gsbase.subcode = subcode;
244 }
245
246
247
248 /* Build a tuple with operands.  CODE is the statement to build (which
249    must be one of the GIMPLE_WITH_OPS tuples).  SUBCODE is the sub-code
250    for the new tuple.  NUM_OPS is the number of operands to allocate.  */ 
251
252 #define gimple_build_with_ops(c, s, n) \
253   gimple_build_with_ops_stat (c, s, n MEM_STAT_INFO)
254
255 static gimple
256 gimple_build_with_ops_stat (enum gimple_code code, enum tree_code subcode,
257                             unsigned num_ops MEM_STAT_DECL)
258 {
259   gimple s = gimple_alloc_stat (code, num_ops PASS_MEM_STAT);
260   gimple_set_subcode (s, subcode);
261
262   return s;
263 }
264
265
266 /* Build a GIMPLE_RETURN statement returning RETVAL.  */
267
268 gimple
269 gimple_build_return (tree retval)
270 {
271   gimple s = gimple_build_with_ops (GIMPLE_RETURN, ERROR_MARK, 1);
272   if (retval)
273     gimple_return_set_retval (s, retval);
274   return s;
275 }
276
277 /* Helper for gimple_build_call, gimple_build_call_vec and
278    gimple_build_call_from_tree.  Build the basic components of a
279    GIMPLE_CALL statement to function FN with NARGS arguments.  */
280
281 static inline gimple
282 gimple_build_call_1 (tree fn, unsigned nargs)
283 {
284   gimple s = gimple_build_with_ops (GIMPLE_CALL, ERROR_MARK, nargs + 3);
285   if (TREE_CODE (fn) == FUNCTION_DECL)
286     fn = build_fold_addr_expr (fn);
287   gimple_set_op (s, 1, fn);
288   return s;
289 }
290
291
292 /* Build a GIMPLE_CALL statement to function FN with the arguments
293    specified in vector ARGS.  */
294
295 gimple
296 gimple_build_call_vec (tree fn, VEC(tree, heap) *args)
297 {
298   unsigned i;
299   unsigned nargs = VEC_length (tree, args);
300   gimple call = gimple_build_call_1 (fn, nargs);
301
302   for (i = 0; i < nargs; i++)
303     gimple_call_set_arg (call, i, VEC_index (tree, args, i));
304
305   return call;
306 }
307
308
309 /* Build a GIMPLE_CALL statement to function FN.  NARGS is the number of
310    arguments.  The ... are the arguments.  */
311
312 gimple
313 gimple_build_call (tree fn, unsigned nargs, ...)
314 {
315   va_list ap;
316   gimple call;
317   unsigned i;
318
319   gcc_assert (TREE_CODE (fn) == FUNCTION_DECL || is_gimple_call_addr (fn));
320
321   call = gimple_build_call_1 (fn, nargs);
322
323   va_start (ap, nargs);
324   for (i = 0; i < nargs; i++)
325     gimple_call_set_arg (call, i, va_arg (ap, tree));
326   va_end (ap);
327
328   return call;
329 }
330
331
332 /* Build a GIMPLE_CALL statement from CALL_EXPR T.  Note that T is
333    assumed to be in GIMPLE form already.  Minimal checking is done of
334    this fact.  */
335
336 gimple
337 gimple_build_call_from_tree (tree t)
338 {
339   unsigned i, nargs;
340   gimple call;
341   tree fndecl = get_callee_fndecl (t);
342
343   gcc_assert (TREE_CODE (t) == CALL_EXPR);
344
345   nargs = call_expr_nargs (t);
346   call = gimple_build_call_1 (fndecl ? fndecl : CALL_EXPR_FN (t), nargs);
347
348   for (i = 0; i < nargs; i++)
349     gimple_call_set_arg (call, i, CALL_EXPR_ARG (t, i));
350
351   gimple_set_block (call, TREE_BLOCK (t));
352
353   /* Carry all the CALL_EXPR flags to the new GIMPLE_CALL.  */
354   gimple_call_set_chain (call, CALL_EXPR_STATIC_CHAIN (t));
355   gimple_call_set_tail (call, CALL_EXPR_TAILCALL (t));
356   gimple_call_set_cannot_inline (call, CALL_CANNOT_INLINE_P (t));
357   gimple_call_set_return_slot_opt (call, CALL_EXPR_RETURN_SLOT_OPT (t));
358   gimple_call_set_from_thunk (call, CALL_FROM_THUNK_P (t));
359   gimple_call_set_va_arg_pack (call, CALL_EXPR_VA_ARG_PACK (t));
360   gimple_set_no_warning (call, TREE_NO_WARNING (t));
361
362   return call;
363 }
364
365
366 /* Extract the operands and code for expression EXPR into *SUBCODE_P,
367    *OP1_P and *OP2_P respectively.  */
368
369 void
370 extract_ops_from_tree (tree expr, enum tree_code *subcode_p, tree *op1_p,
371                        tree *op2_p)
372 {
373   enum gimple_rhs_class grhs_class;
374
375   *subcode_p = TREE_CODE (expr);
376   grhs_class = get_gimple_rhs_class (*subcode_p);
377
378   if (grhs_class == GIMPLE_BINARY_RHS)
379     {
380       *op1_p = TREE_OPERAND (expr, 0);
381       *op2_p = TREE_OPERAND (expr, 1);
382     }
383   else if (grhs_class == GIMPLE_UNARY_RHS)
384     {
385       *op1_p = TREE_OPERAND (expr, 0);
386       *op2_p = NULL_TREE;
387     }
388   else if (grhs_class == GIMPLE_SINGLE_RHS)
389     {
390       *op1_p = expr;
391       *op2_p = NULL_TREE;
392     }
393   else
394     gcc_unreachable ();
395 }
396
397
398 /* Build a GIMPLE_ASSIGN statement.
399
400    LHS of the assignment.
401    RHS of the assignment which can be unary or binary.  */
402
403 gimple
404 gimple_build_assign_stat (tree lhs, tree rhs MEM_STAT_DECL)
405 {
406   enum tree_code subcode;
407   tree op1, op2;
408
409   extract_ops_from_tree (rhs, &subcode, &op1, &op2);
410   return gimple_build_assign_with_ops_stat (subcode, lhs, op1, op2
411                                             PASS_MEM_STAT);
412 }
413
414
415 /* Build a GIMPLE_ASSIGN statement with sub-code SUBCODE and operands
416    OP1 and OP2.  If OP2 is NULL then SUBCODE must be of class
417    GIMPLE_UNARY_RHS or GIMPLE_SINGLE_RHS.  */
418
419 gimple
420 gimple_build_assign_with_ops_stat (enum tree_code subcode, tree lhs, tree op1,
421                                    tree op2 MEM_STAT_DECL)
422 {
423   unsigned num_ops;
424   gimple p;
425
426   /* Need 1 operand for LHS and 1 or 2 for the RHS (depending on the
427      code).  */
428   num_ops = get_gimple_rhs_num_ops (subcode) + 1;
429   
430   p = gimple_build_with_ops_stat (GIMPLE_ASSIGN, subcode, num_ops
431                                   PASS_MEM_STAT);
432   gimple_assign_set_lhs (p, lhs);
433   gimple_assign_set_rhs1 (p, op1);
434   if (op2)
435     {
436       gcc_assert (num_ops > 2);
437       gimple_assign_set_rhs2 (p, op2);
438     }
439
440   return p;
441 }
442
443
444 /* Build a new GIMPLE_ASSIGN tuple and append it to the end of *SEQ_P.
445
446    DST/SRC are the destination and source respectively.  You can pass
447    ungimplified trees in DST or SRC, in which case they will be
448    converted to a gimple operand if necessary.
449
450    This function returns the newly created GIMPLE_ASSIGN tuple.  */
451
452 gimple
453 gimplify_assign (tree dst, tree src, gimple_seq *seq_p)
454
455   tree t = build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, src);
456   gimplify_and_add (t, seq_p);
457   ggc_free (t);
458   return gimple_seq_last_stmt (*seq_p);
459 }
460
461
462 /* Build a GIMPLE_COND statement.
463
464    PRED is the condition used to compare LHS and the RHS.
465    T_LABEL is the label to jump to if the condition is true.
466    F_LABEL is the label to jump to otherwise.  */
467
468 gimple
469 gimple_build_cond (enum tree_code pred_code, tree lhs, tree rhs,
470                    tree t_label, tree f_label)
471 {
472   gimple p;
473
474   gcc_assert (TREE_CODE_CLASS (pred_code) == tcc_comparison);
475   p = gimple_build_with_ops (GIMPLE_COND, pred_code, 4);
476   gimple_cond_set_lhs (p, lhs);
477   gimple_cond_set_rhs (p, rhs);
478   gimple_cond_set_true_label (p, t_label);
479   gimple_cond_set_false_label (p, f_label);
480   return p;
481 }
482
483
484 /* Extract operands for a GIMPLE_COND statement out of COND_EXPR tree COND.  */
485
486 void
487 gimple_cond_get_ops_from_tree (tree cond, enum tree_code *code_p,
488                                tree *lhs_p, tree *rhs_p)
489 {
490   gcc_assert (TREE_CODE_CLASS (TREE_CODE (cond)) == tcc_comparison
491               || TREE_CODE (cond) == TRUTH_NOT_EXPR
492               || is_gimple_min_invariant (cond)
493               || SSA_VAR_P (cond));
494
495   extract_ops_from_tree (cond, code_p, lhs_p, rhs_p);
496
497   /* Canonicalize conditionals of the form 'if (!VAL)'.  */
498   if (*code_p == TRUTH_NOT_EXPR)
499     {
500       *code_p = EQ_EXPR;
501       gcc_assert (*lhs_p && *rhs_p == NULL_TREE);
502       *rhs_p = fold_convert (TREE_TYPE (*lhs_p), integer_zero_node);
503     }
504   /* Canonicalize conditionals of the form 'if (VAL)'  */
505   else if (TREE_CODE_CLASS (*code_p) != tcc_comparison)
506     {
507       *code_p = NE_EXPR;
508       gcc_assert (*lhs_p && *rhs_p == NULL_TREE);
509       *rhs_p = fold_convert (TREE_TYPE (*lhs_p), integer_zero_node);
510     }
511 }
512
513
514 /* Build a GIMPLE_COND statement from the conditional expression tree
515    COND.  T_LABEL and F_LABEL are as in gimple_build_cond.  */
516
517 gimple
518 gimple_build_cond_from_tree (tree cond, tree t_label, tree f_label)
519 {
520   enum tree_code code;
521   tree lhs, rhs;
522
523   gimple_cond_get_ops_from_tree (cond, &code, &lhs, &rhs);
524   return gimple_build_cond (code, lhs, rhs, t_label, f_label);
525 }
526
527 /* Set code, lhs, and rhs of a GIMPLE_COND from a suitable
528    boolean expression tree COND.  */
529
530 void
531 gimple_cond_set_condition_from_tree (gimple stmt, tree cond)
532 {
533   enum tree_code code;
534   tree lhs, rhs;
535
536   gimple_cond_get_ops_from_tree (cond, &code, &lhs, &rhs);
537   gimple_cond_set_condition (stmt, code, lhs, rhs);
538 }
539
540 /* Build a GIMPLE_LABEL statement for LABEL.  */
541
542 gimple
543 gimple_build_label (tree label)
544 {
545   gimple p = gimple_build_with_ops (GIMPLE_LABEL, ERROR_MARK, 1);
546   gimple_label_set_label (p, label);
547   return p;
548 }
549
550 /* Build a GIMPLE_GOTO statement to label DEST.  */
551
552 gimple
553 gimple_build_goto (tree dest)
554 {
555   gimple p = gimple_build_with_ops (GIMPLE_GOTO, ERROR_MARK, 1);
556   gimple_goto_set_dest (p, dest);
557   return p;
558 }
559
560
561 /* Build a GIMPLE_NOP statement.  */
562
563 gimple 
564 gimple_build_nop (void)
565 {
566   return gimple_alloc (GIMPLE_NOP, 0);
567 }
568
569
570 /* Build a GIMPLE_BIND statement.
571    VARS are the variables in BODY.
572    BLOCK is the containing block.  */
573
574 gimple
575 gimple_build_bind (tree vars, gimple_seq body, tree block)
576 {
577   gimple p = gimple_alloc (GIMPLE_BIND, 0);
578   gimple_bind_set_vars (p, vars);
579   if (body)
580     gimple_bind_set_body (p, body);
581   if (block)
582     gimple_bind_set_block (p, block);
583   return p;
584 }
585
586 /* Helper function to set the simple fields of a asm stmt.
587
588    STRING is a pointer to a string that is the asm blocks assembly code.
589    NINPUT is the number of register inputs.
590    NOUTPUT is the number of register outputs.
591    NCLOBBERS is the number of clobbered registers.
592    */
593
594 static inline gimple
595 gimple_build_asm_1 (const char *string, unsigned ninputs, unsigned noutputs, 
596                     unsigned nclobbers)
597 {
598   gimple p;
599   int size = strlen (string);
600
601   p = gimple_build_with_ops (GIMPLE_ASM, ERROR_MARK,
602                              ninputs + noutputs + nclobbers);
603
604   p->gimple_asm.ni = ninputs;
605   p->gimple_asm.no = noutputs;
606   p->gimple_asm.nc = nclobbers;
607   p->gimple_asm.string = ggc_alloc_string (string, size);
608
609 #ifdef GATHER_STATISTICS
610   gimple_alloc_sizes[(int) gimple_alloc_kind (GIMPLE_ASM)] += size;
611 #endif
612   
613   return p;
614 }
615
616 /* Build a GIMPLE_ASM statement.
617
618    STRING is the assembly code.
619    NINPUT is the number of register inputs.
620    NOUTPUT is the number of register outputs.
621    NCLOBBERS is the number of clobbered registers.
622    INPUTS is a vector of the input register parameters.
623    OUTPUTS is a vector of the output register parameters.
624    CLOBBERS is a vector of the clobbered register parameters.  */
625
626 gimple
627 gimple_build_asm_vec (const char *string, VEC(tree,gc)* inputs, 
628                       VEC(tree,gc)* outputs, VEC(tree,gc)* clobbers)
629 {
630   gimple p;
631   unsigned i;
632
633   p = gimple_build_asm_1 (string,
634                           VEC_length (tree, inputs),
635                           VEC_length (tree, outputs), 
636                           VEC_length (tree, clobbers));
637   
638   for (i = 0; i < VEC_length (tree, inputs); i++)
639     gimple_asm_set_input_op (p, i, VEC_index (tree, inputs, i));
640
641   for (i = 0; i < VEC_length (tree, outputs); i++)
642     gimple_asm_set_output_op (p, i, VEC_index (tree, outputs, i));
643
644   for (i = 0; i < VEC_length (tree, clobbers); i++)
645     gimple_asm_set_clobber_op (p, i, VEC_index (tree, clobbers, i));
646   
647   return p;
648 }
649
650 /* Build a GIMPLE_ASM statement.
651
652    STRING is the assembly code.
653    NINPUT is the number of register inputs.
654    NOUTPUT is the number of register outputs.
655    NCLOBBERS is the number of clobbered registers.
656    ... are trees for each input, output and clobbered register.  */
657
658 gimple
659 gimple_build_asm (const char *string, unsigned ninputs, unsigned noutputs, 
660                   unsigned nclobbers, ...)
661 {
662   gimple p;
663   unsigned i;
664   va_list ap;
665   
666   p = gimple_build_asm_1 (string, ninputs, noutputs, nclobbers);
667   
668   va_start (ap, nclobbers);
669
670   for (i = 0; i < ninputs; i++)
671     gimple_asm_set_input_op (p, i, va_arg (ap, tree));
672
673   for (i = 0; i < noutputs; i++)
674     gimple_asm_set_output_op (p, i, va_arg (ap, tree));
675
676   for (i = 0; i < nclobbers; i++)
677     gimple_asm_set_clobber_op (p, i, va_arg (ap, tree));
678
679   va_end (ap);
680   
681   return p;
682 }
683
684 /* Build a GIMPLE_CATCH statement.
685
686   TYPES are the catch types.
687   HANDLER is the exception handler.  */
688
689 gimple
690 gimple_build_catch (tree types, gimple_seq handler)
691 {
692   gimple p = gimple_alloc (GIMPLE_CATCH, 0);
693   gimple_catch_set_types (p, types);
694   if (handler)
695     gimple_catch_set_handler (p, handler);
696
697   return p;
698 }
699
700 /* Build a GIMPLE_EH_FILTER statement.
701
702    TYPES are the filter's types.
703    FAILURE is the filter's failure action.  */
704
705 gimple
706 gimple_build_eh_filter (tree types, gimple_seq failure)
707 {
708   gimple p = gimple_alloc (GIMPLE_EH_FILTER, 0);
709   gimple_eh_filter_set_types (p, types);
710   if (failure)
711     gimple_eh_filter_set_failure (p, failure);
712
713   return p;
714 }
715
716 /* Build a GIMPLE_TRY statement.
717
718    EVAL is the expression to evaluate.
719    CLEANUP is the cleanup expression.
720    KIND is either GIMPLE_TRY_CATCH or GIMPLE_TRY_FINALLY depending on
721    whether this is a try/catch or a try/finally respectively.  */
722
723 gimple
724 gimple_build_try (gimple_seq eval, gimple_seq cleanup,
725                   enum gimple_try_flags kind)
726 {
727   gimple p;
728
729   gcc_assert (kind == GIMPLE_TRY_CATCH || kind == GIMPLE_TRY_FINALLY);
730   p = gimple_alloc (GIMPLE_TRY, 0);
731   gimple_set_subcode (p, kind);
732   if (eval)
733     gimple_try_set_eval (p, eval);
734   if (cleanup)
735     gimple_try_set_cleanup (p, cleanup);
736
737   return p;
738 }
739
740 /* Construct a GIMPLE_WITH_CLEANUP_EXPR statement.
741
742    CLEANUP is the cleanup expression.  */
743
744 gimple
745 gimple_build_wce (gimple_seq cleanup)
746 {
747   gimple p = gimple_alloc (GIMPLE_WITH_CLEANUP_EXPR, 0);
748   if (cleanup)
749     gimple_wce_set_cleanup (p, cleanup);
750
751   return p;
752 }
753
754
755 /* Build a GIMPLE_RESX statement.
756
757    REGION is the region number from which this resx causes control flow to 
758    leave.  */
759
760 gimple
761 gimple_build_resx (int region)
762 {
763   gimple p = gimple_alloc (GIMPLE_RESX, 0);
764   gimple_resx_set_region (p, region);
765   return p;
766 }
767
768
769 /* The helper for constructing a gimple switch statement.
770    INDEX is the switch's index.
771    NLABELS is the number of labels in the switch excluding the default.
772    DEFAULT_LABEL is the default label for the switch statement.  */
773
774 static inline gimple 
775 gimple_build_switch_1 (unsigned nlabels, tree index, tree default_label)
776 {
777   /* nlabels + 1 default label + 1 index.  */
778   gimple p = gimple_build_with_ops (GIMPLE_SWITCH, ERROR_MARK,
779                                     nlabels + 1 + 1);
780   gimple_switch_set_index (p, index);
781   gimple_switch_set_default_label (p, default_label);
782   return p;
783 }
784
785
786 /* Build a GIMPLE_SWITCH statement.
787
788    INDEX is the switch's index.
789    NLABELS is the number of labels in the switch excluding the DEFAULT_LABEL. 
790    ... are the labels excluding the default.  */
791
792 gimple 
793 gimple_build_switch (unsigned nlabels, tree index, tree default_label, ...)
794 {
795   va_list al;
796   unsigned i;
797   gimple p;
798   
799   p = gimple_build_switch_1 (nlabels, index, default_label);
800
801   /* Store the rest of the labels.  */
802   va_start (al, default_label);
803   for (i = 1; i <= nlabels; i++)
804     gimple_switch_set_label (p, i, va_arg (al, tree));
805   va_end (al);
806
807   return p;
808 }
809
810
811 /* Build a GIMPLE_SWITCH statement.
812
813    INDEX is the switch's index.
814    DEFAULT_LABEL is the default label
815    ARGS is a vector of labels excluding the default.  */
816
817 gimple
818 gimple_build_switch_vec (tree index, tree default_label, VEC(tree, heap) *args)
819 {
820   unsigned i;
821   unsigned nlabels = VEC_length (tree, args);
822   gimple p = gimple_build_switch_1 (nlabels, index, default_label);
823
824   /*  Put labels in labels[1 - (nlabels + 1)].
825      Default label is in labels[0].  */
826   for (i = 1; i <= nlabels; i++)
827     gimple_switch_set_label (p, i, VEC_index (tree, args, i - 1));
828
829   return p;
830 }
831
832
833 /* Build a GIMPLE_OMP_CRITICAL statement.
834
835    BODY is the sequence of statements for which only one thread can execute.
836    NAME is optional identifier for this critical block.  */
837
838 gimple 
839 gimple_build_omp_critical (gimple_seq body, tree name)
840 {
841   gimple p = gimple_alloc (GIMPLE_OMP_CRITICAL, 0);
842   gimple_omp_critical_set_name (p, name);
843   if (body)
844     gimple_omp_set_body (p, body);
845
846   return p;
847 }
848
849 /* Build a GIMPLE_OMP_FOR statement.
850
851    BODY is sequence of statements inside the for loop.
852    CLAUSES, are any of the OMP loop construct's clauses: private, firstprivate, 
853    lastprivate, reductions, ordered, schedule, and nowait.
854    COLLAPSE is the collapse count.
855    PRE_BODY is the sequence of statements that are loop invariant.  */
856
857 gimple
858 gimple_build_omp_for (gimple_seq body, tree clauses, size_t collapse,
859                       gimple_seq pre_body)
860 {
861   gimple p = gimple_alloc (GIMPLE_OMP_FOR, 0);
862   if (body)
863     gimple_omp_set_body (p, body);
864   gimple_omp_for_set_clauses (p, clauses);
865   p->gimple_omp_for.collapse = collapse;
866   p->gimple_omp_for.iter = GGC_CNEWVEC (struct gimple_omp_for_iter, collapse);
867   if (pre_body)
868     gimple_omp_for_set_pre_body (p, pre_body);
869
870   return p;
871 }
872
873
874 /* Build a GIMPLE_OMP_PARALLEL statement.
875
876    BODY is sequence of statements which are executed in parallel.
877    CLAUSES, are the OMP parallel construct's clauses.
878    CHILD_FN is the function created for the parallel threads to execute.
879    DATA_ARG are the shared data argument(s).  */
880
881 gimple 
882 gimple_build_omp_parallel (gimple_seq body, tree clauses, tree child_fn, 
883                            tree data_arg)
884 {
885   gimple p = gimple_alloc (GIMPLE_OMP_PARALLEL, 0);
886   if (body)
887     gimple_omp_set_body (p, body);
888   gimple_omp_parallel_set_clauses (p, clauses);
889   gimple_omp_parallel_set_child_fn (p, child_fn);
890   gimple_omp_parallel_set_data_arg (p, data_arg);
891
892   return p;
893 }
894
895
896 /* Build a GIMPLE_OMP_TASK statement.
897
898    BODY is sequence of statements which are executed by the explicit task.
899    CLAUSES, are the OMP parallel construct's clauses.
900    CHILD_FN is the function created for the parallel threads to execute.
901    DATA_ARG are the shared data argument(s).
902    COPY_FN is the optional function for firstprivate initialization.
903    ARG_SIZE and ARG_ALIGN are size and alignment of the data block.  */
904
905 gimple 
906 gimple_build_omp_task (gimple_seq body, tree clauses, tree child_fn,
907                        tree data_arg, tree copy_fn, tree arg_size,
908                        tree arg_align)
909 {
910   gimple p = gimple_alloc (GIMPLE_OMP_TASK, 0);
911   if (body)
912     gimple_omp_set_body (p, body);
913   gimple_omp_task_set_clauses (p, clauses);
914   gimple_omp_task_set_child_fn (p, child_fn);
915   gimple_omp_task_set_data_arg (p, data_arg);
916   gimple_omp_task_set_copy_fn (p, copy_fn);
917   gimple_omp_task_set_arg_size (p, arg_size);
918   gimple_omp_task_set_arg_align (p, arg_align);
919
920   return p;
921 }
922
923
924 /* Build a GIMPLE_OMP_SECTION statement for a sections statement.
925
926    BODY is the sequence of statements in the section.  */
927
928 gimple
929 gimple_build_omp_section (gimple_seq body)
930 {
931   gimple p = gimple_alloc (GIMPLE_OMP_SECTION, 0);
932   if (body)
933     gimple_omp_set_body (p, body);
934
935   return p;
936 }
937
938
939 /* Build a GIMPLE_OMP_MASTER statement.
940
941    BODY is the sequence of statements to be executed by just the master.  */
942
943 gimple 
944 gimple_build_omp_master (gimple_seq body)
945 {
946   gimple p = gimple_alloc (GIMPLE_OMP_MASTER, 0);
947   if (body)
948     gimple_omp_set_body (p, body);
949
950   return p;
951 }
952
953
954 /* Build a GIMPLE_OMP_CONTINUE statement.
955
956    CONTROL_DEF is the definition of the control variable.
957    CONTROL_USE is the use of the control variable.  */
958
959 gimple 
960 gimple_build_omp_continue (tree control_def, tree control_use)
961 {
962   gimple p = gimple_alloc (GIMPLE_OMP_CONTINUE, 0);
963   gimple_omp_continue_set_control_def (p, control_def);
964   gimple_omp_continue_set_control_use (p, control_use);
965   return p;
966 }
967
968 /* Build a GIMPLE_OMP_ORDERED statement.
969
970    BODY is the sequence of statements inside a loop that will executed in
971    sequence.  */
972
973 gimple 
974 gimple_build_omp_ordered (gimple_seq body)
975 {
976   gimple p = gimple_alloc (GIMPLE_OMP_ORDERED, 0);
977   if (body)
978     gimple_omp_set_body (p, body);
979
980   return p;
981 }
982
983
984 /* Build a GIMPLE_OMP_RETURN statement.
985    WAIT_P is true if this is a non-waiting return.  */
986
987 gimple 
988 gimple_build_omp_return (bool wait_p)
989 {
990   gimple p = gimple_alloc (GIMPLE_OMP_RETURN, 0);
991   if (wait_p)
992     gimple_omp_return_set_nowait (p);
993
994   return p;
995 }
996
997
998 /* Build a GIMPLE_OMP_SECTIONS statement.
999
1000    BODY is a sequence of section statements.
1001    CLAUSES are any of the OMP sections contsruct's clauses: private,
1002    firstprivate, lastprivate, reduction, and nowait.  */
1003
1004 gimple 
1005 gimple_build_omp_sections (gimple_seq body, tree clauses)
1006 {
1007   gimple p = gimple_alloc (GIMPLE_OMP_SECTIONS, 0);
1008   if (body)
1009     gimple_omp_set_body (p, body);
1010   gimple_omp_sections_set_clauses (p, clauses);
1011
1012   return p;
1013 }
1014
1015
1016 /* Build a GIMPLE_OMP_SECTIONS_SWITCH.  */
1017
1018 gimple
1019 gimple_build_omp_sections_switch (void)
1020 {
1021   return gimple_alloc (GIMPLE_OMP_SECTIONS_SWITCH, 0);
1022 }
1023
1024
1025 /* Build a GIMPLE_OMP_SINGLE statement.
1026
1027    BODY is the sequence of statements that will be executed once.
1028    CLAUSES are any of the OMP single construct's clauses: private, firstprivate,
1029    copyprivate, nowait.  */
1030
1031 gimple 
1032 gimple_build_omp_single (gimple_seq body, tree clauses)
1033 {
1034   gimple p = gimple_alloc (GIMPLE_OMP_SINGLE, 0);
1035   if (body)
1036     gimple_omp_set_body (p, body);
1037   gimple_omp_single_set_clauses (p, clauses);
1038
1039   return p;
1040 }
1041
1042
1043 /* Build a GIMPLE_OMP_ATOMIC_LOAD statement.  */
1044
1045 gimple
1046 gimple_build_omp_atomic_load (tree lhs, tree rhs)
1047 {
1048   gimple p = gimple_alloc (GIMPLE_OMP_ATOMIC_LOAD, 0);
1049   gimple_omp_atomic_load_set_lhs (p, lhs);
1050   gimple_omp_atomic_load_set_rhs (p, rhs);
1051   return p;
1052 }
1053
1054 /* Build a GIMPLE_OMP_ATOMIC_STORE statement.
1055
1056    VAL is the value we are storing.  */
1057
1058 gimple
1059 gimple_build_omp_atomic_store (tree val)
1060 {
1061   gimple p = gimple_alloc (GIMPLE_OMP_ATOMIC_STORE, 0);
1062   gimple_omp_atomic_store_set_val (p, val);
1063   return p;
1064 }
1065
1066 /* Build a GIMPLE_PREDICT statement.  PREDICT is one of the predictors from
1067    predict.def, OUTCOME is NOT_TAKEN or TAKEN.  */
1068
1069 gimple
1070 gimple_build_predict (enum br_predictor predictor, enum prediction outcome)
1071 {
1072   gimple p = gimple_alloc (GIMPLE_PREDICT, 0);
1073   /* Ensure all the predictors fit into the lower bits of the subcode.  */
1074   gcc_assert ((int) END_PREDICTORS <= GF_PREDICT_TAKEN);
1075   gimple_predict_set_predictor (p, predictor);
1076   gimple_predict_set_outcome (p, outcome);
1077   return p;
1078 }
1079
1080 /* Return which gimple structure is used by T.  The enums here are defined
1081    in gsstruct.def.  */
1082
1083 enum gimple_statement_structure_enum
1084 gimple_statement_structure (gimple gs)
1085 {
1086   return gss_for_code (gimple_code (gs));
1087 }
1088
1089 #if defined ENABLE_GIMPLE_CHECKING
1090 /* Complain of a gimple type mismatch and die.  */
1091
1092 void
1093 gimple_check_failed (const_gimple gs, const char *file, int line,
1094                      const char *function, enum gimple_code code,
1095                      enum tree_code subcode)
1096 {
1097   internal_error ("gimple check: expected %s(%s), have %s(%s) in %s, at %s:%d",
1098                   gimple_code_name[code],
1099                   tree_code_name[subcode],
1100                   gimple_code_name[gimple_code (gs)],
1101                   gs->gsbase.subcode > 0
1102                     ? tree_code_name[gs->gsbase.subcode]
1103                     : "",
1104                   function, trim_filename (file), line);
1105 }
1106 #endif /* ENABLE_GIMPLE_CHECKING */
1107
1108
1109 /* Allocate a new GIMPLE sequence in GC memory and return it.  If
1110    there are free sequences in GIMPLE_SEQ_CACHE return one of those
1111    instead.  */
1112
1113 gimple_seq
1114 gimple_seq_alloc (void)
1115 {
1116   gimple_seq seq = gimple_seq_cache;
1117   if (seq)
1118     {
1119       gimple_seq_cache = gimple_seq_cache->next_free;
1120       gcc_assert (gimple_seq_cache != seq);
1121       memset (seq, 0, sizeof (*seq));
1122     }
1123   else
1124     {
1125       seq = (gimple_seq) ggc_alloc_cleared (sizeof (*seq));
1126 #ifdef GATHER_STATISTICS
1127       gimple_alloc_counts[(int) gimple_alloc_kind_seq]++;
1128       gimple_alloc_sizes[(int) gimple_alloc_kind_seq] += sizeof (*seq);
1129 #endif
1130     }
1131
1132   return seq;
1133 }
1134
1135 /* Return SEQ to the free pool of GIMPLE sequences.  */
1136
1137 void
1138 gimple_seq_free (gimple_seq seq)
1139 {
1140   if (seq == NULL)
1141     return;
1142
1143   gcc_assert (gimple_seq_first (seq) == NULL);
1144   gcc_assert (gimple_seq_last (seq) == NULL);
1145
1146   /* If this triggers, it's a sign that the same list is being freed
1147      twice.  */
1148   gcc_assert (seq != gimple_seq_cache || gimple_seq_cache == NULL);
1149   
1150   /* Add SEQ to the pool of free sequences.  */
1151   seq->next_free = gimple_seq_cache;
1152   gimple_seq_cache = seq;
1153 }
1154
1155
1156 /* Link gimple statement GS to the end of the sequence *SEQ_P.  If
1157    *SEQ_P is NULL, a new sequence is allocated.  */
1158
1159 void
1160 gimple_seq_add_stmt (gimple_seq *seq_p, gimple gs)
1161 {
1162   gimple_stmt_iterator si;
1163
1164   if (gs == NULL)
1165     return;
1166
1167   if (*seq_p == NULL)
1168     *seq_p = gimple_seq_alloc ();
1169
1170   si = gsi_last (*seq_p);
1171   gsi_insert_after (&si, gs, GSI_NEW_STMT);
1172 }
1173
1174
1175 /* Append sequence SRC to the end of sequence *DST_P.  If *DST_P is
1176    NULL, a new sequence is allocated.  */
1177
1178 void
1179 gimple_seq_add_seq (gimple_seq *dst_p, gimple_seq src)
1180 {
1181   gimple_stmt_iterator si;
1182
1183   if (src == NULL)
1184     return;
1185
1186   if (*dst_p == NULL)
1187     *dst_p = gimple_seq_alloc ();
1188
1189   si = gsi_last (*dst_p);
1190   gsi_insert_seq_after (&si, src, GSI_NEW_STMT);
1191 }
1192
1193
1194 /* Helper function of empty_body_p.  Return true if STMT is an empty
1195    statement.  */
1196
1197 static bool
1198 empty_stmt_p (gimple stmt)
1199 {
1200   if (gimple_code (stmt) == GIMPLE_NOP)
1201     return true;
1202   if (gimple_code (stmt) == GIMPLE_BIND)
1203     return empty_body_p (gimple_bind_body (stmt));
1204   return false;
1205 }
1206
1207
1208 /* Return true if BODY contains nothing but empty statements.  */
1209
1210 bool
1211 empty_body_p (gimple_seq body)
1212 {
1213   gimple_stmt_iterator i;
1214
1215
1216   if (gimple_seq_empty_p (body))
1217     return true;
1218   for (i = gsi_start (body); !gsi_end_p (i); gsi_next (&i))
1219     if (!empty_stmt_p (gsi_stmt (i)))
1220       return false;
1221
1222   return true;
1223 }
1224
1225
1226 /* Perform a deep copy of sequence SRC and return the result.  */
1227
1228 gimple_seq
1229 gimple_seq_copy (gimple_seq src)
1230 {
1231   gimple_stmt_iterator gsi;
1232   gimple_seq new_seq = gimple_seq_alloc ();
1233   gimple stmt;
1234
1235   for (gsi = gsi_start (src); !gsi_end_p (gsi); gsi_next (&gsi))
1236     {
1237       stmt = gimple_copy (gsi_stmt (gsi));
1238       gimple_seq_add_stmt (&new_seq, stmt);
1239     }
1240
1241   return new_seq;
1242 }
1243
1244
1245 /* Walk all the statements in the sequence SEQ calling walk_gimple_stmt
1246    on each one.  WI is as in walk_gimple_stmt.
1247    
1248    If walk_gimple_stmt returns non-NULL, the walk is stopped, the
1249    value is stored in WI->CALLBACK_RESULT and the statement that
1250    produced the value is returned.
1251
1252    Otherwise, all the statements are walked and NULL returned.  */
1253
1254 gimple
1255 walk_gimple_seq (gimple_seq seq, walk_stmt_fn callback_stmt,
1256                  walk_tree_fn callback_op, struct walk_stmt_info *wi)
1257 {
1258   gimple_stmt_iterator gsi;
1259
1260   for (gsi = gsi_start (seq); !gsi_end_p (gsi); gsi_next (&gsi))
1261     {
1262       tree ret = walk_gimple_stmt (&gsi, callback_stmt, callback_op, wi);
1263       if (ret)
1264         {
1265           /* If CALLBACK_STMT or CALLBACK_OP return a value, WI must exist
1266              to hold it.  */
1267           gcc_assert (wi);
1268           wi->callback_result = ret;
1269           return gsi_stmt (gsi);
1270         }
1271     }
1272
1273   if (wi)
1274     wi->callback_result = NULL_TREE;
1275
1276   return NULL;
1277 }
1278
1279
1280 /* Helper function for walk_gimple_stmt.  Walk operands of a GIMPLE_ASM.  */
1281
1282 static tree
1283 walk_gimple_asm (gimple stmt, walk_tree_fn callback_op,
1284                  struct walk_stmt_info *wi)
1285 {
1286   tree ret;
1287   unsigned noutputs;
1288   const char **oconstraints;
1289   unsigned i;
1290   const char *constraint;
1291   bool allows_mem, allows_reg, is_inout;
1292
1293   noutputs = gimple_asm_noutputs (stmt);
1294   oconstraints = (const char **) alloca ((noutputs) * sizeof (const char *));
1295
1296   if (wi)
1297     wi->is_lhs = true;
1298
1299   for (i = 0; i < noutputs; i++)
1300     {
1301       tree op = gimple_asm_output_op (stmt, i);
1302       constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (op)));
1303       oconstraints[i] = constraint;
1304       parse_output_constraint (&constraint, i, 0, 0, &allows_mem, &allows_reg,
1305                                &is_inout);
1306       if (wi)
1307         wi->val_only = (allows_reg || !allows_mem);
1308       ret = walk_tree (&TREE_VALUE (op), callback_op, wi, NULL);
1309       if (ret)
1310         return ret;
1311     }
1312
1313   for (i = 0; i < gimple_asm_ninputs (stmt); i++)
1314     {
1315       tree op = gimple_asm_input_op (stmt, i);
1316       constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (op)));
1317       parse_input_constraint (&constraint, 0, 0, noutputs, 0,
1318                               oconstraints, &allows_mem, &allows_reg);
1319       if (wi)
1320         wi->val_only = (allows_reg || !allows_mem);
1321
1322       /* Although input "m" is not really a LHS, we need a lvalue.  */
1323       if (wi)
1324         wi->is_lhs = !wi->val_only;
1325       ret = walk_tree (&TREE_VALUE (op), callback_op, wi, NULL);
1326       if (ret)
1327         return ret;
1328     }
1329
1330   if (wi)
1331     {
1332       wi->is_lhs = false;
1333       wi->val_only = true;
1334     }
1335
1336   return NULL_TREE;
1337 }
1338
1339
1340 /* Helper function of WALK_GIMPLE_STMT.  Walk every tree operand in
1341    STMT.  CALLBACK_OP and WI are as in WALK_GIMPLE_STMT.
1342
1343    CALLBACK_OP is called on each operand of STMT via walk_tree.
1344    Additional parameters to walk_tree must be stored in WI.  For each operand
1345    OP, walk_tree is called as:
1346
1347         walk_tree (&OP, CALLBACK_OP, WI, WI->PSET)
1348
1349    If CALLBACK_OP returns non-NULL for an operand, the remaining
1350    operands are not scanned.
1351
1352    The return value is that returned by the last call to walk_tree, or
1353    NULL_TREE if no CALLBACK_OP is specified.  */
1354
1355 inline tree
1356 walk_gimple_op (gimple stmt, walk_tree_fn callback_op,
1357                 struct walk_stmt_info *wi)
1358 {
1359   struct pointer_set_t *pset = (wi) ? wi->pset : NULL;
1360   unsigned i;
1361   tree ret = NULL_TREE;
1362
1363   switch (gimple_code (stmt))
1364     {
1365     case GIMPLE_ASSIGN:
1366       /* Walk the RHS operands.  A formal temporary LHS may use a
1367          COMPONENT_REF RHS.  */
1368       if (wi)
1369         wi->val_only = !is_gimple_reg (gimple_assign_lhs (stmt))
1370                        || !gimple_assign_single_p (stmt);
1371
1372       for (i = 1; i < gimple_num_ops (stmt); i++)
1373         {
1374           ret = walk_tree (gimple_op_ptr (stmt, i), callback_op, wi,
1375                            pset);
1376           if (ret)
1377             return ret;
1378         }
1379
1380       /* Walk the LHS.  If the RHS is appropriate for a memory, we
1381          may use a COMPONENT_REF on the LHS.  */
1382       if (wi)
1383         {
1384           /* If the RHS has more than 1 operand, it is not appropriate
1385              for the memory.  */
1386           wi->val_only = !is_gimple_mem_rhs (gimple_assign_rhs1 (stmt))
1387                          || !gimple_assign_single_p (stmt);
1388           wi->is_lhs = true;
1389         }
1390
1391       ret = walk_tree (gimple_op_ptr (stmt, 0), callback_op, wi, pset);
1392       if (ret)
1393         return ret;
1394
1395       if (wi)
1396         {
1397           wi->val_only = true;
1398           wi->is_lhs = false;
1399         }
1400       break;
1401
1402     case GIMPLE_CALL:
1403       if (wi)
1404         wi->is_lhs = false;
1405
1406       ret = walk_tree (gimple_call_chain_ptr (stmt), callback_op, wi, pset);
1407       if (ret)
1408         return ret;
1409
1410       ret = walk_tree (gimple_call_fn_ptr (stmt), callback_op, wi, pset);
1411       if (ret)
1412         return ret;
1413
1414       for (i = 0; i < gimple_call_num_args (stmt); i++)
1415         {
1416           ret = walk_tree (gimple_call_arg_ptr (stmt, i), callback_op, wi,
1417                            pset);
1418           if (ret)
1419             return ret;
1420         }
1421
1422       if (wi)
1423         wi->is_lhs = true;
1424
1425       ret = walk_tree (gimple_call_lhs_ptr (stmt), callback_op, wi, pset);
1426       if (ret)
1427         return ret;
1428
1429       if (wi)
1430         wi->is_lhs = false;
1431       break;
1432
1433     case GIMPLE_CATCH:
1434       ret = walk_tree (gimple_catch_types_ptr (stmt), callback_op, wi,
1435                        pset);
1436       if (ret)
1437         return ret;
1438       break;
1439
1440     case GIMPLE_EH_FILTER:
1441       ret = walk_tree (gimple_eh_filter_types_ptr (stmt), callback_op, wi,
1442                        pset);
1443       if (ret)
1444         return ret;
1445       break;
1446
1447     case GIMPLE_ASM:
1448       ret = walk_gimple_asm (stmt, callback_op, wi);
1449       if (ret)
1450         return ret;
1451       break;
1452
1453     case GIMPLE_OMP_CONTINUE:
1454       ret = walk_tree (gimple_omp_continue_control_def_ptr (stmt),
1455                        callback_op, wi, pset);
1456       if (ret)
1457         return ret;
1458
1459       ret = walk_tree (gimple_omp_continue_control_use_ptr (stmt),
1460                        callback_op, wi, pset);
1461       if (ret)
1462         return ret;
1463       break;
1464
1465     case GIMPLE_OMP_CRITICAL:
1466       ret = walk_tree (gimple_omp_critical_name_ptr (stmt), callback_op, wi,
1467                        pset);
1468       if (ret)
1469         return ret;
1470       break;
1471
1472     case GIMPLE_OMP_FOR:
1473       ret = walk_tree (gimple_omp_for_clauses_ptr (stmt), callback_op, wi,
1474                        pset);
1475       if (ret)
1476         return ret;
1477       for (i = 0; i < gimple_omp_for_collapse (stmt); i++)
1478         {
1479           ret = walk_tree (gimple_omp_for_index_ptr (stmt, i), callback_op,
1480                            wi, pset);
1481           if (ret)
1482             return ret;
1483           ret = walk_tree (gimple_omp_for_initial_ptr (stmt, i), callback_op,
1484                            wi, pset);
1485           if (ret)
1486             return ret;
1487           ret = walk_tree (gimple_omp_for_final_ptr (stmt, i), callback_op,
1488                            wi, pset);
1489           if (ret)
1490             return ret;
1491           ret = walk_tree (gimple_omp_for_incr_ptr (stmt, i), callback_op,
1492                            wi, pset);
1493         }
1494       if (ret)
1495         return ret;
1496       break;
1497
1498     case GIMPLE_OMP_PARALLEL:
1499       ret = walk_tree (gimple_omp_parallel_clauses_ptr (stmt), callback_op,
1500                        wi, pset);
1501       if (ret)
1502         return ret;
1503       ret = walk_tree (gimple_omp_parallel_child_fn_ptr (stmt), callback_op,
1504                        wi, pset);
1505       if (ret)
1506         return ret;
1507       ret = walk_tree (gimple_omp_parallel_data_arg_ptr (stmt), callback_op,
1508                        wi, pset);
1509       if (ret)
1510         return ret;
1511       break;
1512
1513     case GIMPLE_OMP_TASK:
1514       ret = walk_tree (gimple_omp_task_clauses_ptr (stmt), callback_op,
1515                        wi, pset);
1516       if (ret)
1517         return ret;
1518       ret = walk_tree (gimple_omp_task_child_fn_ptr (stmt), callback_op,
1519                        wi, pset);
1520       if (ret)
1521         return ret;
1522       ret = walk_tree (gimple_omp_task_data_arg_ptr (stmt), callback_op,
1523                        wi, pset);
1524       if (ret)
1525         return ret;
1526       ret = walk_tree (gimple_omp_task_copy_fn_ptr (stmt), callback_op,
1527                        wi, pset);
1528       if (ret)
1529         return ret;
1530       ret = walk_tree (gimple_omp_task_arg_size_ptr (stmt), callback_op,
1531                        wi, pset);
1532       if (ret)
1533         return ret;
1534       ret = walk_tree (gimple_omp_task_arg_align_ptr (stmt), callback_op,
1535                        wi, pset);
1536       if (ret)
1537         return ret;
1538       break;
1539
1540     case GIMPLE_OMP_SECTIONS:
1541       ret = walk_tree (gimple_omp_sections_clauses_ptr (stmt), callback_op,
1542                        wi, pset);
1543       if (ret)
1544         return ret;
1545
1546       ret = walk_tree (gimple_omp_sections_control_ptr (stmt), callback_op,
1547                        wi, pset);
1548       if (ret)
1549         return ret;
1550
1551       break;
1552
1553     case GIMPLE_OMP_SINGLE:
1554       ret = walk_tree (gimple_omp_single_clauses_ptr (stmt), callback_op, wi,
1555                        pset);
1556       if (ret)
1557         return ret;
1558       break;
1559
1560     case GIMPLE_OMP_ATOMIC_LOAD:
1561       ret = walk_tree (gimple_omp_atomic_load_lhs_ptr (stmt), callback_op, wi,
1562                        pset);
1563       if (ret)
1564         return ret;
1565
1566       ret = walk_tree (gimple_omp_atomic_load_rhs_ptr (stmt), callback_op, wi,
1567                        pset);
1568       if (ret)
1569         return ret;
1570       break;
1571
1572     case GIMPLE_OMP_ATOMIC_STORE:
1573       ret = walk_tree (gimple_omp_atomic_store_val_ptr (stmt), callback_op,
1574                        wi, pset);
1575       if (ret)
1576         return ret;
1577       break;
1578
1579       /* Tuples that do not have operands.  */
1580     case GIMPLE_NOP:
1581     case GIMPLE_RESX:
1582     case GIMPLE_OMP_RETURN:
1583     case GIMPLE_PREDICT:
1584       break;
1585
1586     default:
1587       {
1588         enum gimple_statement_structure_enum gss;
1589         gss = gimple_statement_structure (stmt);
1590         if (gss == GSS_WITH_OPS || gss == GSS_WITH_MEM_OPS)
1591           for (i = 0; i < gimple_num_ops (stmt); i++)
1592             {
1593               ret = walk_tree (gimple_op_ptr (stmt, i), callback_op, wi, pset);
1594               if (ret)
1595                 return ret;
1596             }
1597       }
1598       break;
1599     }
1600
1601   return NULL_TREE;
1602 }
1603
1604
1605 /* Walk the current statement in GSI (optionally using traversal state
1606    stored in WI).  If WI is NULL, no state is kept during traversal.
1607    The callback CALLBACK_STMT is called.  If CALLBACK_STMT indicates
1608    that it has handled all the operands of the statement, its return
1609    value is returned.  Otherwise, the return value from CALLBACK_STMT
1610    is discarded and its operands are scanned.
1611
1612    If CALLBACK_STMT is NULL or it didn't handle the operands,
1613    CALLBACK_OP is called on each operand of the statement via
1614    walk_gimple_op.  If walk_gimple_op returns non-NULL for any
1615    operand, the remaining operands are not scanned.  In this case, the
1616    return value from CALLBACK_OP is returned.
1617
1618    In any other case, NULL_TREE is returned.  */
1619
1620 tree
1621 walk_gimple_stmt (gimple_stmt_iterator *gsi, walk_stmt_fn callback_stmt,
1622                   walk_tree_fn callback_op, struct walk_stmt_info *wi)
1623 {
1624   gimple ret;
1625   tree tree_ret;
1626   gimple stmt = gsi_stmt (*gsi);
1627
1628   if (wi)
1629     wi->gsi = *gsi;
1630
1631   if (wi && wi->want_locations && gimple_has_location (stmt))
1632     input_location = gimple_location (stmt);
1633
1634   ret = NULL;
1635
1636   /* Invoke the statement callback.  Return if the callback handled
1637      all of STMT operands by itself.  */
1638   if (callback_stmt)
1639     {
1640       bool handled_ops = false;
1641       tree_ret = callback_stmt (gsi, &handled_ops, wi);
1642       if (handled_ops)
1643         return tree_ret;
1644
1645       /* If CALLBACK_STMT did not handle operands, it should not have
1646          a value to return.  */
1647       gcc_assert (tree_ret == NULL);
1648
1649       /* Re-read stmt in case the callback changed it.  */
1650       stmt = gsi_stmt (*gsi);
1651     }
1652
1653   /* If CALLBACK_OP is defined, invoke it on every operand of STMT.  */
1654   if (callback_op)
1655     {
1656       tree_ret = walk_gimple_op (stmt, callback_op, wi);
1657       if (tree_ret)
1658         return tree_ret;
1659     }
1660
1661   /* If STMT can have statements inside (e.g. GIMPLE_BIND), walk them.  */
1662   switch (gimple_code (stmt))
1663     {
1664     case GIMPLE_BIND:
1665       ret = walk_gimple_seq (gimple_bind_body (stmt), callback_stmt,
1666                              callback_op, wi);
1667       if (ret)
1668         return wi->callback_result;
1669       break;
1670
1671     case GIMPLE_CATCH:
1672       ret = walk_gimple_seq (gimple_catch_handler (stmt), callback_stmt,
1673                              callback_op, wi);
1674       if (ret)
1675         return wi->callback_result;
1676       break;
1677
1678     case GIMPLE_EH_FILTER:
1679       ret = walk_gimple_seq (gimple_eh_filter_failure (stmt), callback_stmt,
1680                              callback_op, wi);
1681       if (ret)
1682         return wi->callback_result;
1683       break;
1684
1685     case GIMPLE_TRY:
1686       ret = walk_gimple_seq (gimple_try_eval (stmt), callback_stmt, callback_op,
1687                              wi);
1688       if (ret)
1689         return wi->callback_result;
1690
1691       ret = walk_gimple_seq (gimple_try_cleanup (stmt), callback_stmt,
1692                              callback_op, wi);
1693       if (ret)
1694         return wi->callback_result;
1695       break;
1696
1697     case GIMPLE_OMP_FOR:
1698       ret = walk_gimple_seq (gimple_omp_for_pre_body (stmt), callback_stmt,
1699                              callback_op, wi);
1700       if (ret)
1701         return wi->callback_result;
1702
1703       /* FALL THROUGH.  */
1704     case GIMPLE_OMP_CRITICAL:
1705     case GIMPLE_OMP_MASTER:
1706     case GIMPLE_OMP_ORDERED:
1707     case GIMPLE_OMP_SECTION:
1708     case GIMPLE_OMP_PARALLEL:
1709     case GIMPLE_OMP_TASK:
1710     case GIMPLE_OMP_SECTIONS:
1711     case GIMPLE_OMP_SINGLE:
1712       ret = walk_gimple_seq (gimple_omp_body (stmt), callback_stmt, callback_op,
1713                              wi);
1714       if (ret)
1715         return wi->callback_result;
1716       break;
1717
1718     case GIMPLE_WITH_CLEANUP_EXPR:
1719       ret = walk_gimple_seq (gimple_wce_cleanup (stmt), callback_stmt,
1720                              callback_op, wi);
1721       if (ret)
1722         return wi->callback_result;
1723       break;
1724
1725     default:
1726       gcc_assert (!gimple_has_substatements (stmt));
1727       break;
1728     }
1729
1730   return NULL;
1731 }
1732
1733
1734 /* Set sequence SEQ to be the GIMPLE body for function FN.  */
1735
1736 void
1737 gimple_set_body (tree fndecl, gimple_seq seq)
1738 {
1739   struct function *fn = DECL_STRUCT_FUNCTION (fndecl);
1740   if (fn == NULL)
1741     {
1742       /* If FNDECL still does not have a function structure associated
1743          with it, then it does not make sense for it to receive a
1744          GIMPLE body.  */
1745       gcc_assert (seq == NULL);
1746     }
1747   else
1748     fn->gimple_body = seq;
1749 }
1750
1751
1752 /* Return the body of GIMPLE statements for function FN.  */
1753
1754 gimple_seq
1755 gimple_body (tree fndecl)
1756 {
1757   struct function *fn = DECL_STRUCT_FUNCTION (fndecl);
1758   return fn ? fn->gimple_body : NULL;
1759 }
1760
1761 /* Return true when FNDECL has Gimple body either in unlowered
1762    or CFG form.  */
1763 bool
1764 gimple_has_body_p (tree fndecl)
1765 {
1766   struct function *fn = DECL_STRUCT_FUNCTION (fndecl);
1767   return (gimple_body (fndecl) || (fn && fn->cfg));
1768 }
1769
1770 /* Detect flags from a GIMPLE_CALL.  This is just like
1771    call_expr_flags, but for gimple tuples.  */
1772
1773 int
1774 gimple_call_flags (const_gimple stmt)
1775 {
1776   int flags;
1777   tree decl = gimple_call_fndecl (stmt);
1778   tree t;
1779
1780   if (decl)
1781     flags = flags_from_decl_or_type (decl);
1782   else
1783     {
1784       t = TREE_TYPE (gimple_call_fn (stmt));
1785       if (t && TREE_CODE (t) == POINTER_TYPE)
1786         flags = flags_from_decl_or_type (TREE_TYPE (t));
1787       else
1788         flags = 0;
1789     }
1790
1791   return flags;
1792 }
1793
1794
1795 /* Return true if GS is a copy assignment.  */
1796
1797 bool
1798 gimple_assign_copy_p (gimple gs)
1799 {
1800   return gimple_code (gs) == GIMPLE_ASSIGN
1801          && get_gimple_rhs_class (gimple_assign_rhs_code (gs))
1802             == GIMPLE_SINGLE_RHS
1803          && is_gimple_val (gimple_op (gs, 1));
1804 }
1805
1806
1807 /* Return true if GS is a SSA_NAME copy assignment.  */
1808
1809 bool
1810 gimple_assign_ssa_name_copy_p (gimple gs)
1811 {
1812   return (gimple_code (gs) == GIMPLE_ASSIGN
1813           && (get_gimple_rhs_class (gimple_assign_rhs_code (gs))
1814               == GIMPLE_SINGLE_RHS)
1815           && TREE_CODE (gimple_assign_lhs (gs)) == SSA_NAME
1816           && TREE_CODE (gimple_assign_rhs1 (gs)) == SSA_NAME);
1817 }
1818
1819
1820 /* Return true if GS is an assignment with a singleton RHS, i.e.,
1821    there is no operator associated with the assignment itself.
1822    Unlike gimple_assign_copy_p, this predicate returns true for
1823    any RHS operand, including those that perform an operation
1824    and do not have the semantics of a copy, such as COND_EXPR.  */
1825
1826 bool
1827 gimple_assign_single_p (gimple gs)
1828 {
1829   return (gimple_code (gs) == GIMPLE_ASSIGN
1830           && get_gimple_rhs_class (gimple_assign_rhs_code (gs))
1831              == GIMPLE_SINGLE_RHS);
1832 }
1833
1834 /* Return true if GS is an assignment with a unary RHS, but the
1835    operator has no effect on the assigned value.  The logic is adapted
1836    from STRIP_NOPS.  This predicate is intended to be used in tuplifying
1837    instances in which STRIP_NOPS was previously applied to the RHS of
1838    an assignment.
1839
1840    NOTE: In the use cases that led to the creation of this function
1841    and of gimple_assign_single_p, it is typical to test for either
1842    condition and to proceed in the same manner.  In each case, the
1843    assigned value is represented by the single RHS operand of the
1844    assignment.  I suspect there may be cases where gimple_assign_copy_p,
1845    gimple_assign_single_p, or equivalent logic is used where a similar
1846    treatment of unary NOPs is appropriate.  */
1847    
1848 bool
1849 gimple_assign_unary_nop_p (gimple gs)
1850 {
1851   return (gimple_code (gs) == GIMPLE_ASSIGN
1852           && (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (gs))
1853               || gimple_assign_rhs_code (gs) == NON_LVALUE_EXPR)
1854           && gimple_assign_rhs1 (gs) != error_mark_node
1855           && (TYPE_MODE (TREE_TYPE (gimple_assign_lhs (gs)))
1856               == TYPE_MODE (TREE_TYPE (gimple_assign_rhs1 (gs)))));
1857 }
1858
1859 /* Set BB to be the basic block holding G.  */
1860
1861 void
1862 gimple_set_bb (gimple stmt, basic_block bb)
1863 {
1864   stmt->gsbase.bb = bb;
1865
1866   /* If the statement is a label, add the label to block-to-labels map
1867      so that we can speed up edge creation for GIMPLE_GOTOs.  */
1868   if (cfun->cfg && gimple_code (stmt) == GIMPLE_LABEL)
1869     {
1870       tree t;
1871       int uid;
1872
1873       t = gimple_label_label (stmt);
1874       uid = LABEL_DECL_UID (t);
1875       if (uid == -1)
1876         {
1877           unsigned old_len = VEC_length (basic_block, label_to_block_map);
1878           LABEL_DECL_UID (t) = uid = cfun->cfg->last_label_uid++;
1879           if (old_len <= (unsigned) uid)
1880             {
1881               unsigned new_len = 3 * uid / 2 + 1;
1882
1883               VEC_safe_grow_cleared (basic_block, gc, label_to_block_map,
1884                                      new_len);
1885             }
1886         }
1887
1888       VEC_replace (basic_block, label_to_block_map, uid, bb);
1889     }
1890 }
1891
1892
1893 /* Fold the expression computed by STMT.  If the expression can be
1894    folded, return the folded result, otherwise return NULL.  STMT is
1895    not modified.  */
1896
1897 tree
1898 gimple_fold (const_gimple stmt)
1899 {
1900   switch (gimple_code (stmt))
1901     {
1902     case GIMPLE_COND:
1903       return fold_binary (gimple_cond_code (stmt),
1904                           boolean_type_node,
1905                           gimple_cond_lhs (stmt),
1906                           gimple_cond_rhs (stmt));
1907
1908     case GIMPLE_ASSIGN:
1909       switch (get_gimple_rhs_class (gimple_assign_rhs_code (stmt)))
1910         {
1911         case GIMPLE_UNARY_RHS:
1912           return fold_unary (gimple_assign_rhs_code (stmt),
1913                              TREE_TYPE (gimple_assign_lhs (stmt)),
1914                              gimple_assign_rhs1 (stmt));
1915         case GIMPLE_BINARY_RHS:
1916           return fold_binary (gimple_assign_rhs_code (stmt),
1917                               TREE_TYPE (gimple_assign_lhs (stmt)),
1918                               gimple_assign_rhs1 (stmt),
1919                               gimple_assign_rhs2 (stmt));
1920         case GIMPLE_SINGLE_RHS:
1921           return fold (gimple_assign_rhs1 (stmt));
1922         default:;
1923         }
1924       break;
1925
1926     case GIMPLE_SWITCH:
1927       return gimple_switch_index (stmt);
1928
1929     case GIMPLE_CALL:
1930       return NULL_TREE;
1931
1932     default:
1933       break;
1934     }
1935
1936   gcc_unreachable ();
1937 }
1938
1939
1940 /* Modify the RHS of the assignment pointed-to by GSI using the
1941    operands in the expression tree EXPR.
1942
1943    NOTE: The statement pointed-to by GSI may be reallocated if it
1944    did not have enough operand slots.
1945
1946    This function is useful to convert an existing tree expression into
1947    the flat representation used for the RHS of a GIMPLE assignment.
1948    It will reallocate memory as needed to expand or shrink the number
1949    of operand slots needed to represent EXPR.
1950
1951    NOTE: If you find yourself building a tree and then calling this
1952    function, you are most certainly doing it the slow way.  It is much
1953    better to build a new assignment or to use the function
1954    gimple_assign_set_rhs_with_ops, which does not require an
1955    expression tree to be built.  */
1956
1957 void
1958 gimple_assign_set_rhs_from_tree (gimple_stmt_iterator *gsi, tree expr)
1959 {
1960   enum tree_code subcode;
1961   tree op1, op2;
1962
1963   extract_ops_from_tree (expr, &subcode, &op1, &op2);
1964   gimple_assign_set_rhs_with_ops (gsi, subcode, op1, op2);
1965 }
1966
1967
1968 /* Set the RHS of assignment statement pointed-to by GSI to CODE with
1969    operands OP1 and OP2.
1970
1971    NOTE: The statement pointed-to by GSI may be reallocated if it
1972    did not have enough operand slots.  */
1973
1974 void
1975 gimple_assign_set_rhs_with_ops (gimple_stmt_iterator *gsi, enum tree_code code,
1976                                 tree op1, tree op2)
1977 {
1978   unsigned new_rhs_ops = get_gimple_rhs_num_ops (code);
1979   gimple stmt = gsi_stmt (*gsi);
1980
1981   /* If the new CODE needs more operands, allocate a new statement.  */
1982   if (gimple_num_ops (stmt) < new_rhs_ops + 1)
1983     {
1984       tree lhs = gimple_assign_lhs (stmt);
1985       gimple new_stmt = gimple_alloc (gimple_code (stmt), new_rhs_ops + 1);
1986       memcpy (new_stmt, stmt, gimple_size (gimple_code (stmt)));
1987       gsi_replace (gsi, new_stmt, true);
1988       stmt = new_stmt;
1989
1990       /* The LHS needs to be reset as this also changes the SSA name
1991          on the LHS.  */
1992       gimple_assign_set_lhs (stmt, lhs);
1993     }
1994
1995   gimple_set_num_ops (stmt, new_rhs_ops + 1);
1996   gimple_set_subcode (stmt, code);
1997   gimple_assign_set_rhs1 (stmt, op1);
1998   if (new_rhs_ops > 1)
1999     gimple_assign_set_rhs2 (stmt, op2);
2000 }
2001
2002
2003 /* Return the LHS of a statement that performs an assignment,
2004    either a GIMPLE_ASSIGN or a GIMPLE_CALL.  Returns NULL_TREE
2005    for a call to a function that returns no value, or for a
2006    statement other than an assignment or a call.  */
2007
2008 tree
2009 gimple_get_lhs (const_gimple stmt)
2010 {
2011   enum gimple_code code = gimple_code (stmt);
2012
2013   if (code == GIMPLE_ASSIGN)
2014     return gimple_assign_lhs (stmt);
2015   else if (code == GIMPLE_CALL)
2016     return gimple_call_lhs (stmt);
2017   else
2018     return NULL_TREE;
2019 }
2020
2021
2022 /* Set the LHS of a statement that performs an assignment,
2023    either a GIMPLE_ASSIGN or a GIMPLE_CALL.  */
2024
2025 void
2026 gimple_set_lhs (gimple stmt, tree lhs)
2027 {
2028   enum gimple_code code = gimple_code (stmt);
2029
2030   if (code == GIMPLE_ASSIGN)
2031     gimple_assign_set_lhs (stmt, lhs);
2032   else if (code == GIMPLE_CALL)
2033     gimple_call_set_lhs (stmt, lhs);
2034   else
2035     gcc_unreachable();
2036 }
2037
2038
2039 /* Return a deep copy of statement STMT.  All the operands from STMT
2040    are reallocated and copied using unshare_expr.  The DEF, USE, VDEF
2041    and VUSE operand arrays are set to empty in the new copy.  */
2042
2043 gimple
2044 gimple_copy (gimple stmt)
2045 {
2046   enum gimple_code code = gimple_code (stmt);
2047   unsigned num_ops = gimple_num_ops (stmt);
2048   gimple copy = gimple_alloc (code, num_ops);
2049   unsigned i;
2050
2051   /* Shallow copy all the fields from STMT.  */
2052   memcpy (copy, stmt, gimple_size (code));
2053
2054   /* If STMT has sub-statements, deep-copy them as well.  */
2055   if (gimple_has_substatements (stmt))
2056     {
2057       gimple_seq new_seq;
2058       tree t;
2059
2060       switch (gimple_code (stmt))
2061         {
2062         case GIMPLE_BIND:
2063           new_seq = gimple_seq_copy (gimple_bind_body (stmt));
2064           gimple_bind_set_body (copy, new_seq);
2065           gimple_bind_set_vars (copy, unshare_expr (gimple_bind_vars (stmt)));
2066           gimple_bind_set_block (copy, gimple_bind_block (stmt));
2067           break;
2068
2069         case GIMPLE_CATCH:
2070           new_seq = gimple_seq_copy (gimple_catch_handler (stmt));
2071           gimple_catch_set_handler (copy, new_seq);
2072           t = unshare_expr (gimple_catch_types (stmt));
2073           gimple_catch_set_types (copy, t);
2074           break;
2075
2076         case GIMPLE_EH_FILTER:
2077           new_seq = gimple_seq_copy (gimple_eh_filter_failure (stmt));
2078           gimple_eh_filter_set_failure (copy, new_seq);
2079           t = unshare_expr (gimple_eh_filter_types (stmt));
2080           gimple_eh_filter_set_types (copy, t);
2081           break;
2082
2083         case GIMPLE_TRY:
2084           new_seq = gimple_seq_copy (gimple_try_eval (stmt));
2085           gimple_try_set_eval (copy, new_seq);
2086           new_seq = gimple_seq_copy (gimple_try_cleanup (stmt));
2087           gimple_try_set_cleanup (copy, new_seq);
2088           break;
2089
2090         case GIMPLE_OMP_FOR:
2091           new_seq = gimple_seq_copy (gimple_omp_for_pre_body (stmt));
2092           gimple_omp_for_set_pre_body (copy, new_seq);
2093           t = unshare_expr (gimple_omp_for_clauses (stmt));
2094           gimple_omp_for_set_clauses (copy, t);
2095           copy->gimple_omp_for.iter
2096             = GGC_NEWVEC (struct gimple_omp_for_iter,
2097                           gimple_omp_for_collapse (stmt));
2098           for (i = 0; i < gimple_omp_for_collapse (stmt); i++)
2099             {
2100               gimple_omp_for_set_cond (copy, i,
2101                                        gimple_omp_for_cond (stmt, i));
2102               gimple_omp_for_set_index (copy, i,
2103                                         gimple_omp_for_index (stmt, i));
2104               t = unshare_expr (gimple_omp_for_initial (stmt, i));
2105               gimple_omp_for_set_initial (copy, i, t);
2106               t = unshare_expr (gimple_omp_for_final (stmt, i));
2107               gimple_omp_for_set_final (copy, i, t);
2108               t = unshare_expr (gimple_omp_for_incr (stmt, i));
2109               gimple_omp_for_set_incr (copy, i, t);
2110             }
2111           goto copy_omp_body;
2112
2113         case GIMPLE_OMP_PARALLEL:
2114           t = unshare_expr (gimple_omp_parallel_clauses (stmt));
2115           gimple_omp_parallel_set_clauses (copy, t);
2116           t = unshare_expr (gimple_omp_parallel_child_fn (stmt));
2117           gimple_omp_parallel_set_child_fn (copy, t);
2118           t = unshare_expr (gimple_omp_parallel_data_arg (stmt));
2119           gimple_omp_parallel_set_data_arg (copy, t);
2120           goto copy_omp_body;
2121
2122         case GIMPLE_OMP_TASK:
2123           t = unshare_expr (gimple_omp_task_clauses (stmt));
2124           gimple_omp_task_set_clauses (copy, t);
2125           t = unshare_expr (gimple_omp_task_child_fn (stmt));
2126           gimple_omp_task_set_child_fn (copy, t);
2127           t = unshare_expr (gimple_omp_task_data_arg (stmt));
2128           gimple_omp_task_set_data_arg (copy, t);
2129           t = unshare_expr (gimple_omp_task_copy_fn (stmt));
2130           gimple_omp_task_set_copy_fn (copy, t);
2131           t = unshare_expr (gimple_omp_task_arg_size (stmt));
2132           gimple_omp_task_set_arg_size (copy, t);
2133           t = unshare_expr (gimple_omp_task_arg_align (stmt));
2134           gimple_omp_task_set_arg_align (copy, t);
2135           goto copy_omp_body;
2136
2137         case GIMPLE_OMP_CRITICAL:
2138           t = unshare_expr (gimple_omp_critical_name (stmt));
2139           gimple_omp_critical_set_name (copy, t);
2140           goto copy_omp_body;
2141
2142         case GIMPLE_OMP_SECTIONS:
2143           t = unshare_expr (gimple_omp_sections_clauses (stmt));
2144           gimple_omp_sections_set_clauses (copy, t);
2145           t = unshare_expr (gimple_omp_sections_control (stmt));
2146           gimple_omp_sections_set_control (copy, t);
2147           /* FALLTHRU  */
2148
2149         case GIMPLE_OMP_SINGLE:
2150         case GIMPLE_OMP_SECTION:
2151         case GIMPLE_OMP_MASTER:
2152         case GIMPLE_OMP_ORDERED:
2153         copy_omp_body:
2154           new_seq = gimple_seq_copy (gimple_omp_body (stmt));
2155           gimple_omp_set_body (copy, new_seq);
2156           break;
2157
2158         case GIMPLE_WITH_CLEANUP_EXPR:
2159           new_seq = gimple_seq_copy (gimple_wce_cleanup (stmt));
2160           gimple_wce_set_cleanup (copy, new_seq);
2161           break;
2162
2163         default:
2164           gcc_unreachable ();
2165         }
2166     }
2167
2168   /* Make copy of operands.  */
2169   if (num_ops > 0)
2170     {
2171       for (i = 0; i < num_ops; i++)
2172         gimple_set_op (copy, i, unshare_expr (gimple_op (stmt, i)));
2173
2174       /* Clear out SSA operand vectors on COPY.  */
2175       if (gimple_has_ops (stmt))
2176         {
2177           gimple_set_def_ops (copy, NULL);
2178           gimple_set_use_ops (copy, NULL);
2179         }
2180
2181       if (gimple_has_mem_ops (stmt))
2182         {
2183           gimple_set_vdef (copy, gimple_vdef (stmt));
2184           gimple_set_vuse (copy, gimple_vuse (stmt));
2185         }
2186
2187       /* SSA operands need to be updated.  */
2188       gimple_set_modified (copy, true);
2189     }
2190
2191   return copy;
2192 }
2193
2194
2195 /* Set the MODIFIED flag to MODIFIEDP, iff the gimple statement G has
2196    a MODIFIED field.  */
2197
2198 void
2199 gimple_set_modified (gimple s, bool modifiedp)
2200 {
2201   if (gimple_has_ops (s))
2202     {
2203       s->gsbase.modified = (unsigned) modifiedp;
2204
2205       if (modifiedp
2206           && cfun->gimple_df
2207           && is_gimple_call (s)
2208           && gimple_call_noreturn_p (s))
2209         VEC_safe_push (gimple, gc, MODIFIED_NORETURN_CALLS (cfun), s);
2210     }
2211 }
2212
2213
2214 /* Return true if statement S has side-effects.  We consider a
2215    statement to have side effects if:
2216
2217    - It is a GIMPLE_CALL not marked with ECF_PURE or ECF_CONST.
2218    - Any of its operands are marked TREE_THIS_VOLATILE or TREE_SIDE_EFFECTS.  */
2219
2220 bool
2221 gimple_has_side_effects (const_gimple s)
2222 {
2223   unsigned i;
2224
2225   /* We don't have to scan the arguments to check for
2226      volatile arguments, though, at present, we still
2227      do a scan to check for TREE_SIDE_EFFECTS.  */
2228   if (gimple_has_volatile_ops (s))
2229     return true;
2230
2231   if (is_gimple_call (s))
2232     {
2233       unsigned nargs = gimple_call_num_args (s);
2234
2235       if (!(gimple_call_flags (s) & (ECF_CONST | ECF_PURE)))
2236         return true;
2237       else if (gimple_call_flags (s) & ECF_LOOPING_CONST_OR_PURE)
2238         /* An infinite loop is considered a side effect.  */
2239         return true;
2240
2241       if (gimple_call_lhs (s)
2242           && TREE_SIDE_EFFECTS (gimple_call_lhs (s)))
2243         {
2244           gcc_assert (gimple_has_volatile_ops (s));
2245           return true;
2246         }
2247
2248       if (TREE_SIDE_EFFECTS (gimple_call_fn (s)))
2249         return true;
2250
2251       for (i = 0; i < nargs; i++)
2252         if (TREE_SIDE_EFFECTS (gimple_call_arg (s, i)))
2253           {
2254             gcc_assert (gimple_has_volatile_ops (s));
2255             return true;
2256           }
2257
2258       return false;
2259     }
2260   else
2261     {
2262       for (i = 0; i < gimple_num_ops (s); i++)
2263         if (TREE_SIDE_EFFECTS (gimple_op (s, i)))
2264           {
2265             gcc_assert (gimple_has_volatile_ops (s));
2266             return true;
2267           }
2268     }
2269
2270   return false;
2271 }
2272
2273 /* Return true if the RHS of statement S has side effects.
2274    We may use it to determine if it is admissable to replace
2275    an assignment or call with a copy of a previously-computed
2276    value.  In such cases, side-effects due the the LHS are
2277    preserved.  */
2278
2279 bool
2280 gimple_rhs_has_side_effects (const_gimple s)
2281 {
2282   unsigned i;
2283
2284   if (is_gimple_call (s))
2285     {
2286       unsigned nargs = gimple_call_num_args (s);
2287
2288       if (!(gimple_call_flags (s) & (ECF_CONST | ECF_PURE)))
2289         return true;
2290
2291       /* We cannot use gimple_has_volatile_ops here,
2292          because we must ignore a volatile LHS.  */
2293       if (TREE_SIDE_EFFECTS (gimple_call_fn (s))
2294           || TREE_THIS_VOLATILE (gimple_call_fn (s)))
2295         {
2296           gcc_assert (gimple_has_volatile_ops (s));
2297           return true;
2298         }
2299
2300       for (i = 0; i < nargs; i++)
2301         if (TREE_SIDE_EFFECTS (gimple_call_arg (s, i))
2302             || TREE_THIS_VOLATILE (gimple_call_arg (s, i)))
2303           return true;
2304
2305       return false;
2306     }
2307   else if (is_gimple_assign (s))
2308     {
2309       /* Skip the first operand, the LHS. */
2310       for (i = 1; i < gimple_num_ops (s); i++)
2311         if (TREE_SIDE_EFFECTS (gimple_op (s, i))
2312             || TREE_THIS_VOLATILE (gimple_op (s, i)))
2313           {
2314             gcc_assert (gimple_has_volatile_ops (s));
2315             return true;
2316           }
2317     }
2318   else
2319     {
2320       /* For statements without an LHS, examine all arguments.  */
2321       for (i = 0; i < gimple_num_ops (s); i++)
2322         if (TREE_SIDE_EFFECTS (gimple_op (s, i))
2323             || TREE_THIS_VOLATILE (gimple_op (s, i)))
2324           {
2325             gcc_assert (gimple_has_volatile_ops (s));
2326             return true;
2327           }
2328     }
2329
2330   return false;
2331 }
2332
2333
2334 /* Helper for gimple_could_trap_p and gimple_assign_rhs_could_trap_p.
2335    Return true if S can trap.  If INCLUDE_LHS is true and S is a
2336    GIMPLE_ASSIGN, the LHS of the assignment is also checked.
2337    Otherwise, only the RHS of the assignment is checked.  */
2338
2339 static bool
2340 gimple_could_trap_p_1 (gimple s, bool include_lhs)
2341 {
2342   unsigned i, start;
2343   tree t, div = NULL_TREE;
2344   enum tree_code op;
2345
2346   start = (is_gimple_assign (s) && !include_lhs) ? 1 : 0;
2347
2348   for (i = start; i < gimple_num_ops (s); i++)
2349     if (tree_could_trap_p (gimple_op (s, i)))
2350       return true;
2351
2352   switch (gimple_code (s))
2353     {
2354     case GIMPLE_ASM:
2355       return gimple_asm_volatile_p (s);
2356
2357     case GIMPLE_CALL:
2358       t = gimple_call_fndecl (s);
2359       /* Assume that calls to weak functions may trap.  */
2360       if (!t || !DECL_P (t) || DECL_WEAK (t))
2361         return true;
2362       return false;
2363
2364     case GIMPLE_ASSIGN:
2365       t = gimple_expr_type (s);
2366       op = gimple_assign_rhs_code (s);
2367       if (get_gimple_rhs_class (op) == GIMPLE_BINARY_RHS)
2368         div = gimple_assign_rhs2 (s);
2369       return (operation_could_trap_p (op, FLOAT_TYPE_P (t),
2370                                       (INTEGRAL_TYPE_P (t)
2371                                        && TYPE_OVERFLOW_TRAPS (t)),
2372                                       div));
2373
2374     default:
2375       break;
2376     }
2377
2378   return false;
2379
2380 }
2381
2382
2383 /* Return true if statement S can trap.  */
2384
2385 bool
2386 gimple_could_trap_p (gimple s)
2387 {
2388   return gimple_could_trap_p_1 (s, true);
2389 }
2390
2391
2392 /* Return true if RHS of a GIMPLE_ASSIGN S can trap.  */
2393
2394 bool
2395 gimple_assign_rhs_could_trap_p (gimple s)
2396 {
2397   gcc_assert (is_gimple_assign (s));
2398   return gimple_could_trap_p_1 (s, false);
2399 }
2400
2401
2402 /* Print debugging information for gimple stmts generated.  */
2403
2404 void
2405 dump_gimple_statistics (void)
2406 {
2407 #ifdef GATHER_STATISTICS
2408   int i, total_tuples = 0, total_bytes = 0;
2409
2410   fprintf (stderr, "\nGIMPLE statements\n");
2411   fprintf (stderr, "Kind                   Stmts      Bytes\n");
2412   fprintf (stderr, "---------------------------------------\n");
2413   for (i = 0; i < (int) gimple_alloc_kind_all; ++i)
2414     {
2415       fprintf (stderr, "%-20s %7d %10d\n", gimple_alloc_kind_names[i],
2416           gimple_alloc_counts[i], gimple_alloc_sizes[i]);
2417       total_tuples += gimple_alloc_counts[i];
2418       total_bytes += gimple_alloc_sizes[i];
2419     }
2420   fprintf (stderr, "---------------------------------------\n");
2421   fprintf (stderr, "%-20s %7d %10d\n", "Total", total_tuples, total_bytes);
2422   fprintf (stderr, "---------------------------------------\n");
2423 #else
2424   fprintf (stderr, "No gimple statistics\n");
2425 #endif
2426 }
2427
2428
2429 /* Return the number of operands needed on the RHS of a GIMPLE
2430    assignment for an expression with tree code CODE.  */
2431
2432 unsigned
2433 get_gimple_rhs_num_ops (enum tree_code code)
2434 {
2435   enum gimple_rhs_class rhs_class = get_gimple_rhs_class (code);
2436
2437   if (rhs_class == GIMPLE_UNARY_RHS || rhs_class == GIMPLE_SINGLE_RHS)
2438     return 1;
2439   else if (rhs_class == GIMPLE_BINARY_RHS)
2440     return 2;
2441   else
2442     gcc_unreachable ();
2443 }
2444
2445 #define DEFTREECODE(SYM, STRING, TYPE, NARGS)                               \
2446   (unsigned char)                                                           \
2447   ((TYPE) == tcc_unary ? GIMPLE_UNARY_RHS                                   \
2448    : ((TYPE) == tcc_binary                                                  \
2449       || (TYPE) == tcc_comparison) ? GIMPLE_BINARY_RHS                      \
2450    : ((TYPE) == tcc_constant                                                \
2451       || (TYPE) == tcc_declaration                                          \
2452       || (TYPE) == tcc_reference) ? GIMPLE_SINGLE_RHS                       \
2453    : ((SYM) == TRUTH_AND_EXPR                                               \
2454       || (SYM) == TRUTH_OR_EXPR                                             \
2455       || (SYM) == TRUTH_XOR_EXPR) ? GIMPLE_BINARY_RHS                       \
2456    : (SYM) == TRUTH_NOT_EXPR ? GIMPLE_UNARY_RHS                             \
2457    : ((SYM) == COND_EXPR                                                    \
2458       || (SYM) == CONSTRUCTOR                                               \
2459       || (SYM) == OBJ_TYPE_REF                                              \
2460       || (SYM) == ASSERT_EXPR                                               \
2461       || (SYM) == ADDR_EXPR                                                 \
2462       || (SYM) == WITH_SIZE_EXPR                                            \
2463       || (SYM) == EXC_PTR_EXPR                                              \
2464       || (SYM) == SSA_NAME                                                  \
2465       || (SYM) == FILTER_EXPR                                               \
2466       || (SYM) == POLYNOMIAL_CHREC                                          \
2467       || (SYM) == DOT_PROD_EXPR                                             \
2468       || (SYM) == VEC_COND_EXPR                                             \
2469       || (SYM) == REALIGN_LOAD_EXPR) ? GIMPLE_SINGLE_RHS                    \
2470    : GIMPLE_INVALID_RHS),
2471 #define END_OF_BASE_TREE_CODES (unsigned char) GIMPLE_INVALID_RHS,
2472
2473 const unsigned char gimple_rhs_class_table[] = {
2474 #include "all-tree.def"
2475 };
2476
2477 #undef DEFTREECODE
2478 #undef END_OF_BASE_TREE_CODES
2479
2480 /* For the definitive definition of GIMPLE, see doc/tree-ssa.texi.  */
2481
2482 /* Validation of GIMPLE expressions.  */
2483
2484 /* Return true if OP is an acceptable tree node to be used as a GIMPLE
2485    operand.  */
2486
2487 bool
2488 is_gimple_operand (const_tree op)
2489 {
2490   return op && get_gimple_rhs_class (TREE_CODE (op)) == GIMPLE_SINGLE_RHS;
2491 }
2492
2493 /* Returns true iff T is a valid RHS for an assignment to a renamed
2494    user -- or front-end generated artificial -- variable.  */
2495
2496 bool
2497 is_gimple_reg_rhs (tree t)
2498 {
2499   return get_gimple_rhs_class (TREE_CODE (t)) != GIMPLE_INVALID_RHS;
2500 }
2501
2502 /* Returns true iff T is a valid RHS for an assignment to an un-renamed
2503    LHS, or for a call argument.  */
2504
2505 bool
2506 is_gimple_mem_rhs (tree t)
2507 {
2508   /* If we're dealing with a renamable type, either source or dest must be
2509      a renamed variable.  */
2510   if (is_gimple_reg_type (TREE_TYPE (t)))
2511     return is_gimple_val (t);
2512   else
2513     return is_gimple_val (t) || is_gimple_lvalue (t);
2514 }
2515
2516 /*  Return true if T is a valid LHS for a GIMPLE assignment expression.  */
2517
2518 bool
2519 is_gimple_lvalue (tree t)
2520 {
2521   return (is_gimple_addressable (t)
2522           || TREE_CODE (t) == WITH_SIZE_EXPR
2523           /* These are complex lvalues, but don't have addresses, so they
2524              go here.  */
2525           || TREE_CODE (t) == BIT_FIELD_REF);
2526 }
2527
2528 /*  Return true if T is a GIMPLE condition.  */
2529
2530 bool
2531 is_gimple_condexpr (tree t)
2532 {
2533   return (is_gimple_val (t) || (COMPARISON_CLASS_P (t)
2534                                 && !tree_could_trap_p (t)
2535                                 && is_gimple_val (TREE_OPERAND (t, 0))
2536                                 && is_gimple_val (TREE_OPERAND (t, 1))));
2537 }
2538
2539 /*  Return true if T is something whose address can be taken.  */
2540
2541 bool
2542 is_gimple_addressable (tree t)
2543 {
2544   return (is_gimple_id (t) || handled_component_p (t) || INDIRECT_REF_P (t));
2545 }
2546
2547 /* Return true if T is a valid gimple constant.  */
2548
2549 bool
2550 is_gimple_constant (const_tree t)
2551 {
2552   switch (TREE_CODE (t))
2553     {
2554     case INTEGER_CST:
2555     case REAL_CST:
2556     case FIXED_CST:
2557     case STRING_CST:
2558     case COMPLEX_CST:
2559     case VECTOR_CST:
2560       return true;
2561
2562     /* Vector constant constructors are gimple invariant.  */
2563     case CONSTRUCTOR:
2564       if (TREE_TYPE (t) && TREE_CODE (TREE_TYPE (t)) == VECTOR_TYPE)
2565         return TREE_CONSTANT (t);
2566       else
2567         return false;
2568
2569     default:
2570       return false;
2571     }
2572 }
2573
2574 /* Return true if T is a gimple address.  */
2575
2576 bool
2577 is_gimple_address (const_tree t)
2578 {
2579   tree op;
2580
2581   if (TREE_CODE (t) != ADDR_EXPR)
2582     return false;
2583
2584   op = TREE_OPERAND (t, 0);
2585   while (handled_component_p (op))
2586     {
2587       if ((TREE_CODE (op) == ARRAY_REF
2588            || TREE_CODE (op) == ARRAY_RANGE_REF)
2589           && !is_gimple_val (TREE_OPERAND (op, 1)))
2590             return false;
2591
2592       op = TREE_OPERAND (op, 0);
2593     }
2594
2595   if (CONSTANT_CLASS_P (op) || INDIRECT_REF_P (op))
2596     return true;
2597
2598   switch (TREE_CODE (op))
2599     {
2600     case PARM_DECL:
2601     case RESULT_DECL:
2602     case LABEL_DECL:
2603     case FUNCTION_DECL:
2604     case VAR_DECL:
2605     case CONST_DECL:
2606       return true;
2607
2608     default:
2609       return false;
2610     }
2611 }
2612
2613 /* Strip out all handled components that produce invariant
2614    offsets.  */
2615
2616 static const_tree
2617 strip_invariant_refs (const_tree op)
2618 {
2619   while (handled_component_p (op))
2620     {
2621       switch (TREE_CODE (op))
2622         {
2623         case ARRAY_REF:
2624         case ARRAY_RANGE_REF:
2625           if (!is_gimple_constant (TREE_OPERAND (op, 1))
2626               || TREE_OPERAND (op, 2) != NULL_TREE
2627               || TREE_OPERAND (op, 3) != NULL_TREE)
2628             return NULL;
2629           break;
2630
2631         case COMPONENT_REF:
2632           if (TREE_OPERAND (op, 2) != NULL_TREE)
2633             return NULL;
2634           break;
2635
2636         default:;
2637         }
2638       op = TREE_OPERAND (op, 0);
2639     }
2640
2641   return op;
2642 }
2643
2644 /* Return true if T is a gimple invariant address.  */
2645
2646 bool
2647 is_gimple_invariant_address (const_tree t)
2648 {
2649   const_tree op;
2650
2651   if (TREE_CODE (t) != ADDR_EXPR)
2652     return false;
2653
2654   op = strip_invariant_refs (TREE_OPERAND (t, 0));
2655
2656   return op && (CONSTANT_CLASS_P (op) || decl_address_invariant_p (op));
2657 }
2658
2659 /* Return true if T is a gimple invariant address at IPA level
2660    (so addresses of variables on stack are not allowed).  */
2661
2662 bool
2663 is_gimple_ip_invariant_address (const_tree t)
2664 {
2665   const_tree op;
2666
2667   if (TREE_CODE (t) != ADDR_EXPR)
2668     return false;
2669
2670   op = strip_invariant_refs (TREE_OPERAND (t, 0));
2671
2672   return op && (CONSTANT_CLASS_P (op) || decl_address_ip_invariant_p (op));
2673 }
2674
2675 /* Return true if T is a GIMPLE minimal invariant.  It's a restricted
2676    form of function invariant.  */
2677
2678 bool
2679 is_gimple_min_invariant (const_tree t)
2680 {
2681   if (TREE_CODE (t) == ADDR_EXPR)
2682     return is_gimple_invariant_address (t);
2683
2684   return is_gimple_constant (t);
2685 }
2686
2687 /* Return true if T is a GIMPLE interprocedural invariant.  It's a restricted
2688    form of gimple minimal invariant.  */
2689
2690 bool
2691 is_gimple_ip_invariant (const_tree t)
2692 {
2693   if (TREE_CODE (t) == ADDR_EXPR)
2694     return is_gimple_ip_invariant_address (t);
2695
2696   return is_gimple_constant (t);
2697 }
2698
2699 /* Return true if T looks like a valid GIMPLE statement.  */
2700
2701 bool
2702 is_gimple_stmt (tree t)
2703 {
2704   const enum tree_code code = TREE_CODE (t);
2705
2706   switch (code)
2707     {
2708     case NOP_EXPR:
2709       /* The only valid NOP_EXPR is the empty statement.  */
2710       return IS_EMPTY_STMT (t);
2711
2712     case BIND_EXPR:
2713     case COND_EXPR:
2714       /* These are only valid if they're void.  */
2715       return TREE_TYPE (t) == NULL || VOID_TYPE_P (TREE_TYPE (t));
2716
2717     case SWITCH_EXPR:
2718     case GOTO_EXPR:
2719     case RETURN_EXPR:
2720     case LABEL_EXPR:
2721     case CASE_LABEL_EXPR:
2722     case TRY_CATCH_EXPR:
2723     case TRY_FINALLY_EXPR:
2724     case EH_FILTER_EXPR:
2725     case CATCH_EXPR:
2726     case ASM_EXPR:
2727     case RESX_EXPR:
2728     case STATEMENT_LIST:
2729     case OMP_PARALLEL:
2730     case OMP_FOR:
2731     case OMP_SECTIONS:
2732     case OMP_SECTION:
2733     case OMP_SINGLE:
2734     case OMP_MASTER:
2735     case OMP_ORDERED:
2736     case OMP_CRITICAL:
2737     case OMP_TASK:
2738       /* These are always void.  */
2739       return true;
2740
2741     case CALL_EXPR:
2742     case MODIFY_EXPR:
2743     case PREDICT_EXPR:
2744       /* These are valid regardless of their type.  */
2745       return true;
2746
2747     default:
2748       return false;
2749     }
2750 }
2751
2752 /* Return true if T is a variable.  */
2753
2754 bool
2755 is_gimple_variable (tree t)
2756 {
2757   return (TREE_CODE (t) == VAR_DECL
2758           || TREE_CODE (t) == PARM_DECL
2759           || TREE_CODE (t) == RESULT_DECL
2760           || TREE_CODE (t) == SSA_NAME);
2761 }
2762
2763 /*  Return true if T is a GIMPLE identifier (something with an address).  */
2764
2765 bool
2766 is_gimple_id (tree t)
2767 {
2768   return (is_gimple_variable (t)
2769           || TREE_CODE (t) == FUNCTION_DECL
2770           || TREE_CODE (t) == LABEL_DECL
2771           || TREE_CODE (t) == CONST_DECL
2772           /* Allow string constants, since they are addressable.  */
2773           || TREE_CODE (t) == STRING_CST);
2774 }
2775
2776 /* Return true if TYPE is a suitable type for a scalar register variable.  */
2777
2778 bool
2779 is_gimple_reg_type (tree type)
2780 {
2781   return !AGGREGATE_TYPE_P (type);
2782 }
2783
2784 /* Return true if T is a non-aggregate register variable.  */
2785
2786 bool
2787 is_gimple_reg (tree t)
2788 {
2789   if (TREE_CODE (t) == SSA_NAME)
2790     t = SSA_NAME_VAR (t);
2791
2792   if (!is_gimple_variable (t))
2793     return false;
2794
2795   if (!is_gimple_reg_type (TREE_TYPE (t)))
2796     return false;
2797
2798   /* A volatile decl is not acceptable because we can't reuse it as
2799      needed.  We need to copy it into a temp first.  */
2800   if (TREE_THIS_VOLATILE (t))
2801     return false;
2802
2803   /* We define "registers" as things that can be renamed as needed,
2804      which with our infrastructure does not apply to memory.  */
2805   if (needs_to_live_in_memory (t))
2806     return false;
2807
2808   /* Hard register variables are an interesting case.  For those that
2809      are call-clobbered, we don't know where all the calls are, since
2810      we don't (want to) take into account which operations will turn
2811      into libcalls at the rtl level.  For those that are call-saved,
2812      we don't currently model the fact that calls may in fact change
2813      global hard registers, nor do we examine ASM_CLOBBERS at the tree
2814      level, and so miss variable changes that might imply.  All around,
2815      it seems safest to not do too much optimization with these at the
2816      tree level at all.  We'll have to rely on the rtl optimizers to
2817      clean this up, as there we've got all the appropriate bits exposed.  */
2818   if (TREE_CODE (t) == VAR_DECL && DECL_HARD_REGISTER (t))
2819     return false;
2820
2821   /* Complex and vector values must have been put into SSA-like form.
2822      That is, no assignments to the individual components.  */
2823   if (TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE
2824       || TREE_CODE (TREE_TYPE (t)) == VECTOR_TYPE)
2825     return DECL_GIMPLE_REG_P (t);
2826
2827   return true;
2828 }
2829
2830
2831 /* Return true if T is a GIMPLE variable whose address is not needed.  */
2832
2833 bool
2834 is_gimple_non_addressable (tree t)
2835 {
2836   if (TREE_CODE (t) == SSA_NAME)
2837     t = SSA_NAME_VAR (t);
2838
2839   return (is_gimple_variable (t) && ! needs_to_live_in_memory (t));
2840 }
2841
2842 /* Return true if T is a GIMPLE rvalue, i.e. an identifier or a constant.  */
2843
2844 bool
2845 is_gimple_val (tree t)
2846 {
2847   /* Make loads from volatiles and memory vars explicit.  */
2848   if (is_gimple_variable (t)
2849       && is_gimple_reg_type (TREE_TYPE (t))
2850       && !is_gimple_reg (t))
2851     return false;
2852
2853   /* FIXME make these decls.  That can happen only when we expose the
2854      entire landing-pad construct at the tree level.  */
2855   if (TREE_CODE (t) == EXC_PTR_EXPR || TREE_CODE (t) == FILTER_EXPR)
2856     return true;
2857
2858   return (is_gimple_variable (t) || is_gimple_min_invariant (t));
2859 }
2860
2861 /* Similarly, but accept hard registers as inputs to asm statements.  */
2862
2863 bool
2864 is_gimple_asm_val (tree t)
2865 {
2866   if (TREE_CODE (t) == VAR_DECL && DECL_HARD_REGISTER (t))
2867     return true;
2868
2869   return is_gimple_val (t);
2870 }
2871
2872 /* Return true if T is a GIMPLE minimal lvalue.  */
2873
2874 bool
2875 is_gimple_min_lval (tree t)
2876 {
2877   if (!(t = CONST_CAST_TREE (strip_invariant_refs (t))))
2878     return false;
2879   return (is_gimple_id (t) || TREE_CODE (t) == INDIRECT_REF);
2880 }
2881
2882 /* Return true if T is a typecast operation.  */
2883
2884 bool
2885 is_gimple_cast (tree t)
2886 {
2887   return (CONVERT_EXPR_P (t)
2888           || TREE_CODE (t) == FIX_TRUNC_EXPR);
2889 }
2890
2891 /* Return true if T is a valid function operand of a CALL_EXPR.  */
2892
2893 bool
2894 is_gimple_call_addr (tree t)
2895 {
2896   return (TREE_CODE (t) == OBJ_TYPE_REF || is_gimple_val (t));
2897 }
2898
2899 /* If T makes a function call, return the corresponding CALL_EXPR operand.
2900    Otherwise, return NULL_TREE.  */
2901
2902 tree
2903 get_call_expr_in (tree t)
2904 {
2905   if (TREE_CODE (t) == MODIFY_EXPR)
2906     t = TREE_OPERAND (t, 1);
2907   if (TREE_CODE (t) == WITH_SIZE_EXPR)
2908     t = TREE_OPERAND (t, 0);
2909   if (TREE_CODE (t) == CALL_EXPR)
2910     return t;
2911   return NULL_TREE;
2912 }
2913
2914
2915 /* Given a memory reference expression T, return its base address.
2916    The base address of a memory reference expression is the main
2917    object being referenced.  For instance, the base address for
2918    'array[i].fld[j]' is 'array'.  You can think of this as stripping
2919    away the offset part from a memory address.
2920
2921    This function calls handled_component_p to strip away all the inner
2922    parts of the memory reference until it reaches the base object.  */
2923
2924 tree
2925 get_base_address (tree t)
2926 {
2927   while (handled_component_p (t))
2928     t = TREE_OPERAND (t, 0);
2929   
2930   if (SSA_VAR_P (t)
2931       || TREE_CODE (t) == STRING_CST
2932       || TREE_CODE (t) == CONSTRUCTOR
2933       || INDIRECT_REF_P (t))
2934     return t;
2935   else
2936     return NULL_TREE;
2937 }
2938
2939 void
2940 recalculate_side_effects (tree t)
2941 {
2942   enum tree_code code = TREE_CODE (t);
2943   int len = TREE_OPERAND_LENGTH (t);
2944   int i;
2945
2946   switch (TREE_CODE_CLASS (code))
2947     {
2948     case tcc_expression:
2949       switch (code)
2950         {
2951         case INIT_EXPR:
2952         case MODIFY_EXPR:
2953         case VA_ARG_EXPR:
2954         case PREDECREMENT_EXPR:
2955         case PREINCREMENT_EXPR:
2956         case POSTDECREMENT_EXPR:
2957         case POSTINCREMENT_EXPR:
2958           /* All of these have side-effects, no matter what their
2959              operands are.  */
2960           return;
2961
2962         default:
2963           break;
2964         }
2965       /* Fall through.  */
2966
2967     case tcc_comparison:  /* a comparison expression */
2968     case tcc_unary:       /* a unary arithmetic expression */
2969     case tcc_binary:      /* a binary arithmetic expression */
2970     case tcc_reference:   /* a reference */
2971     case tcc_vl_exp:        /* a function call */
2972       TREE_SIDE_EFFECTS (t) = TREE_THIS_VOLATILE (t);
2973       for (i = 0; i < len; ++i)
2974         {
2975           tree op = TREE_OPERAND (t, i);
2976           if (op && TREE_SIDE_EFFECTS (op))
2977             TREE_SIDE_EFFECTS (t) = 1;
2978         }
2979       break;
2980
2981     case tcc_constant:
2982       /* No side-effects.  */
2983       return;
2984
2985     default:
2986       gcc_unreachable ();
2987    }
2988 }
2989
2990 /* Canonicalize a tree T for use in a COND_EXPR as conditional.  Returns
2991    a canonicalized tree that is valid for a COND_EXPR or NULL_TREE, if
2992    we failed to create one.  */
2993
2994 tree
2995 canonicalize_cond_expr_cond (tree t)
2996 {
2997   /* For (bool)x use x != 0.  */
2998   if (TREE_CODE (t) == NOP_EXPR
2999       && TREE_TYPE (t) == boolean_type_node)
3000     {
3001       tree top0 = TREE_OPERAND (t, 0);
3002       t = build2 (NE_EXPR, TREE_TYPE (t),
3003                   top0, build_int_cst (TREE_TYPE (top0), 0));
3004     }
3005   /* For !x use x == 0.  */
3006   else if (TREE_CODE (t) == TRUTH_NOT_EXPR)
3007     {
3008       tree top0 = TREE_OPERAND (t, 0);
3009       t = build2 (EQ_EXPR, TREE_TYPE (t),
3010                   top0, build_int_cst (TREE_TYPE (top0), 0));
3011     }
3012   /* For cmp ? 1 : 0 use cmp.  */
3013   else if (TREE_CODE (t) == COND_EXPR
3014            && COMPARISON_CLASS_P (TREE_OPERAND (t, 0))
3015            && integer_onep (TREE_OPERAND (t, 1))
3016            && integer_zerop (TREE_OPERAND (t, 2)))
3017     {
3018       tree top0 = TREE_OPERAND (t, 0);
3019       t = build2 (TREE_CODE (top0), TREE_TYPE (t),
3020                   TREE_OPERAND (top0, 0), TREE_OPERAND (top0, 1));
3021     }
3022
3023   if (is_gimple_condexpr (t))
3024     return t;
3025
3026   return NULL_TREE;
3027 }
3028
3029 /* Build a GIMPLE_CALL identical to STMT but skipping the arguments in
3030    the positions marked by the set ARGS_TO_SKIP.  */
3031
3032 gimple
3033 gimple_call_copy_skip_args (gimple stmt, bitmap args_to_skip)
3034 {
3035   int i;
3036   tree fn = gimple_call_fn (stmt);
3037   int nargs = gimple_call_num_args (stmt);
3038   VEC(tree, heap) *vargs = VEC_alloc (tree, heap, nargs);
3039   gimple new_stmt;
3040
3041   for (i = 0; i < nargs; i++)
3042     if (!bitmap_bit_p (args_to_skip, i))
3043       VEC_quick_push (tree, vargs, gimple_call_arg (stmt, i));
3044
3045   new_stmt = gimple_build_call_vec (fn, vargs);
3046   VEC_free (tree, heap, vargs);
3047   if (gimple_call_lhs (stmt))
3048     gimple_call_set_lhs (new_stmt, gimple_call_lhs (stmt));
3049
3050   gimple_set_vuse (new_stmt, gimple_vuse (stmt));
3051   gimple_set_vdef (new_stmt, gimple_vdef (stmt));
3052
3053   gimple_set_block (new_stmt, gimple_block (stmt));
3054   if (gimple_has_location (stmt))
3055     gimple_set_location (new_stmt, gimple_location (stmt));
3056
3057   /* Carry all the flags to the new GIMPLE_CALL.  */
3058   gimple_call_set_chain (new_stmt, gimple_call_chain (stmt));
3059   gimple_call_set_tail (new_stmt, gimple_call_tail_p (stmt));
3060   gimple_call_set_cannot_inline (new_stmt, gimple_call_cannot_inline_p (stmt));
3061   gimple_call_set_return_slot_opt (new_stmt, gimple_call_return_slot_opt_p (stmt));
3062   gimple_call_set_from_thunk (new_stmt, gimple_call_from_thunk_p (stmt));
3063   gimple_call_set_va_arg_pack (new_stmt, gimple_call_va_arg_pack_p (stmt));
3064
3065   gimple_set_modified (new_stmt, true);
3066
3067   return new_stmt;
3068 }
3069
3070
3071 /* Data structure used to count the number of dereferences to PTR
3072    inside an expression.  */
3073 struct count_ptr_d
3074 {
3075   tree ptr;
3076   unsigned num_stores;
3077   unsigned num_loads;
3078 };
3079
3080 /* Helper for count_uses_and_derefs.  Called by walk_tree to look for
3081    (ALIGN/MISALIGNED_)INDIRECT_REF nodes for the pointer passed in DATA.  */
3082
3083 static tree
3084 count_ptr_derefs (tree *tp, int *walk_subtrees, void *data)
3085 {
3086   struct walk_stmt_info *wi_p = (struct walk_stmt_info *) data;
3087   struct count_ptr_d *count_p = (struct count_ptr_d *) wi_p->info;
3088
3089   /* Do not walk inside ADDR_EXPR nodes.  In the expression &ptr->fld,
3090      pointer 'ptr' is *not* dereferenced, it is simply used to compute
3091      the address of 'fld' as 'ptr + offsetof(fld)'.  */
3092   if (TREE_CODE (*tp) == ADDR_EXPR)
3093     {
3094       *walk_subtrees = 0;
3095       return NULL_TREE;
3096     }
3097
3098   if (INDIRECT_REF_P (*tp) && TREE_OPERAND (*tp, 0) == count_p->ptr)
3099     {
3100       if (wi_p->is_lhs)
3101         count_p->num_stores++;
3102       else
3103         count_p->num_loads++;
3104     }
3105
3106   return NULL_TREE;
3107 }
3108
3109 /* Count the number of direct and indirect uses for pointer PTR in
3110    statement STMT.  The number of direct uses is stored in
3111    *NUM_USES_P.  Indirect references are counted separately depending
3112    on whether they are store or load operations.  The counts are
3113    stored in *NUM_STORES_P and *NUM_LOADS_P.  */
3114
3115 void
3116 count_uses_and_derefs (tree ptr, gimple stmt, unsigned *num_uses_p,
3117                        unsigned *num_loads_p, unsigned *num_stores_p)
3118 {
3119   ssa_op_iter i;
3120   tree use;
3121
3122   *num_uses_p = 0;
3123   *num_loads_p = 0;
3124   *num_stores_p = 0;
3125
3126   /* Find out the total number of uses of PTR in STMT.  */
3127   FOR_EACH_SSA_TREE_OPERAND (use, stmt, i, SSA_OP_USE)
3128     if (use == ptr)
3129       (*num_uses_p)++;
3130
3131   /* Now count the number of indirect references to PTR.  This is
3132      truly awful, but we don't have much choice.  There are no parent
3133      pointers inside INDIRECT_REFs, so an expression like
3134      '*x_1 = foo (x_1, *x_1)' needs to be traversed piece by piece to
3135      find all the indirect and direct uses of x_1 inside.  The only
3136      shortcut we can take is the fact that GIMPLE only allows
3137      INDIRECT_REFs inside the expressions below.  */
3138   if (is_gimple_assign (stmt)
3139       || gimple_code (stmt) == GIMPLE_RETURN
3140       || gimple_code (stmt) == GIMPLE_ASM
3141       || is_gimple_call (stmt))
3142     {
3143       struct walk_stmt_info wi;
3144       struct count_ptr_d count;
3145
3146       count.ptr = ptr;
3147       count.num_stores = 0;
3148       count.num_loads = 0;
3149
3150       memset (&wi, 0, sizeof (wi));
3151       wi.info = &count;
3152       walk_gimple_op (stmt, count_ptr_derefs, &wi);
3153
3154       *num_stores_p = count.num_stores;
3155       *num_loads_p = count.num_loads;
3156     }
3157
3158   gcc_assert (*num_uses_p >= *num_loads_p + *num_stores_p);
3159 }
3160
3161 /* From a tree operand OP return the base of a load or store operation
3162    or NULL_TREE if OP is not a load or a store.  */
3163
3164 static tree
3165 get_base_loadstore (tree op)
3166 {
3167   while (handled_component_p (op))
3168     op = TREE_OPERAND (op, 0);
3169   if (DECL_P (op)
3170       || INDIRECT_REF_P (op)
3171       || TREE_CODE (op) == TARGET_MEM_REF)
3172     return op;
3173   return NULL_TREE;
3174 }
3175
3176 /* For the statement STMT call the callbacks VISIT_LOAD, VISIT_STORE and
3177    VISIT_ADDR if non-NULL on loads, store and address-taken operands
3178    passing the STMT, the base of the operand and DATA to it.  The base
3179    will be either a decl, an indirect reference (including TARGET_MEM_REF)
3180    or the argument of an address expression.
3181    Returns the results of these callbacks or'ed.  */
3182
3183 bool
3184 walk_stmt_load_store_addr_ops (gimple stmt, void *data,
3185                                bool (*visit_load)(gimple, tree, void *),
3186                                bool (*visit_store)(gimple, tree, void *),
3187                                bool (*visit_addr)(gimple, tree, void *))
3188 {
3189   bool ret = false;
3190   unsigned i;
3191   if (gimple_assign_single_p (stmt))
3192     {
3193       tree lhs, rhs;
3194       if (visit_store)
3195         {
3196           lhs = get_base_loadstore (gimple_assign_lhs (stmt));
3197           if (lhs)
3198             ret |= visit_store (stmt, lhs, data);
3199         }
3200       rhs = gimple_assign_rhs1 (stmt);
3201       while (handled_component_p (rhs))
3202         rhs = TREE_OPERAND (rhs, 0);
3203       if (visit_addr)
3204         {
3205           if (TREE_CODE (rhs) == ADDR_EXPR)
3206             ret |= visit_addr (stmt, TREE_OPERAND (rhs, 0), data);
3207           else if (TREE_CODE (rhs) == TARGET_MEM_REF
3208                    && TMR_BASE (rhs) != NULL_TREE
3209                    && TREE_CODE (TMR_BASE (rhs)) == ADDR_EXPR)
3210             ret |= visit_addr (stmt, TREE_OPERAND (TMR_BASE (rhs), 0), data);
3211           else if (TREE_CODE (rhs) == OBJ_TYPE_REF
3212                    && TREE_CODE (OBJ_TYPE_REF_OBJECT (rhs)) == ADDR_EXPR)
3213             ret |= visit_addr (stmt, TREE_OPERAND (OBJ_TYPE_REF_OBJECT (rhs),
3214                                                    0), data);
3215           lhs = gimple_assign_lhs (stmt);
3216           if (TREE_CODE (lhs) == TARGET_MEM_REF
3217               && TMR_BASE (lhs) != NULL_TREE
3218               && TREE_CODE (TMR_BASE (lhs)) == ADDR_EXPR)
3219             ret |= visit_addr (stmt, TREE_OPERAND (TMR_BASE (lhs), 0), data);
3220         }
3221       if (visit_load)
3222         {
3223           rhs = get_base_loadstore (rhs);
3224           if (rhs)
3225             ret |= visit_load (stmt, rhs, data);
3226         }
3227     }
3228   else if (visit_addr
3229            && (is_gimple_assign (stmt)
3230                || gimple_code (stmt) == GIMPLE_COND))
3231     {
3232       for (i = 0; i < gimple_num_ops (stmt); ++i)
3233         if (gimple_op (stmt, i)
3234             && TREE_CODE (gimple_op (stmt, i)) == ADDR_EXPR)
3235           ret |= visit_addr (stmt, TREE_OPERAND (gimple_op (stmt, i), 0), data);
3236     }
3237   else if (is_gimple_call (stmt))
3238     {
3239       if (visit_store)
3240         {
3241           tree lhs = gimple_call_lhs (stmt);
3242           if (lhs)
3243             {
3244               lhs = get_base_loadstore (lhs);
3245               if (lhs)
3246                 ret |= visit_store (stmt, lhs, data);
3247             }
3248         }
3249       if (visit_load || visit_addr)
3250         for (i = 0; i < gimple_call_num_args (stmt); ++i)
3251           {
3252             tree rhs = gimple_call_arg (stmt, i);
3253             if (visit_addr
3254                 && TREE_CODE (rhs) == ADDR_EXPR)
3255               ret |= visit_addr (stmt, TREE_OPERAND (rhs, 0), data);
3256             else if (visit_load)
3257               {
3258                 rhs = get_base_loadstore (rhs);
3259                 if (rhs)
3260                   ret |= visit_load (stmt, rhs, data);
3261               }
3262           }
3263       if (visit_addr
3264           && gimple_call_chain (stmt)
3265           && TREE_CODE (gimple_call_chain (stmt)) == ADDR_EXPR)
3266         ret |= visit_addr (stmt, TREE_OPERAND (gimple_call_chain (stmt), 0),
3267                            data);
3268       if (visit_addr
3269           && gimple_call_return_slot_opt_p (stmt)
3270           && gimple_call_lhs (stmt) != NULL_TREE
3271           && TREE_ADDRESSABLE (TREE_TYPE (gimple_call_lhs (stmt))))
3272         ret |= visit_addr (stmt, gimple_call_lhs (stmt), data);
3273     }
3274   else if (gimple_code (stmt) == GIMPLE_ASM)
3275     {
3276       unsigned noutputs;
3277       const char *constraint;
3278       const char **oconstraints;
3279       bool allows_mem, allows_reg, is_inout;
3280       noutputs = gimple_asm_noutputs (stmt);
3281       oconstraints = XALLOCAVEC (const char *, noutputs);
3282       if (visit_store || visit_addr)
3283         for (i = 0; i < gimple_asm_noutputs (stmt); ++i)
3284           {
3285             tree link = gimple_asm_output_op (stmt, i);
3286             tree op = get_base_loadstore (TREE_VALUE (link));
3287             if (op && visit_store)
3288               ret |= visit_store (stmt, op, data);
3289             if (visit_addr)
3290               {
3291                 constraint = TREE_STRING_POINTER
3292                     (TREE_VALUE (TREE_PURPOSE (link)));
3293                 oconstraints[i] = constraint;
3294                 parse_output_constraint (&constraint, i, 0, 0, &allows_mem,
3295                                          &allows_reg, &is_inout);
3296                 if (op && !allows_reg && allows_mem)
3297                   ret |= visit_addr (stmt, op, data);
3298               }
3299           }
3300       if (visit_load || visit_addr)
3301         for (i = 0; i < gimple_asm_ninputs (stmt); ++i)
3302           {
3303             tree link = gimple_asm_input_op (stmt, i);
3304             tree op = TREE_VALUE (link);
3305             if (visit_addr
3306                 && TREE_CODE (op) == ADDR_EXPR)
3307               ret |= visit_addr (stmt, TREE_OPERAND (op, 0), data);
3308             else if (visit_load || visit_addr)
3309               {
3310                 op = get_base_loadstore (op);
3311                 if (op)
3312                   {
3313                     if (visit_load)
3314                       ret |= visit_load (stmt, op, data);
3315                     if (visit_addr)
3316                       {
3317                         constraint = TREE_STRING_POINTER
3318                             (TREE_VALUE (TREE_PURPOSE (link)));
3319                         parse_input_constraint (&constraint, 0, 0, noutputs,
3320                                                 0, oconstraints,
3321                                                 &allows_mem, &allows_reg);
3322                         if (!allows_reg && allows_mem)
3323                           ret |= visit_addr (stmt, op, data);
3324                       }
3325                   }
3326               }
3327           }
3328     }
3329   else if (gimple_code (stmt) == GIMPLE_RETURN)
3330     {
3331       tree op = gimple_return_retval (stmt);
3332       if (op)
3333         {
3334           if (visit_addr
3335               && TREE_CODE (op) == ADDR_EXPR)
3336             ret |= visit_addr (stmt, TREE_OPERAND (op, 0), data);
3337           else if (visit_load)
3338             {
3339               op = get_base_loadstore (op);
3340               if (op)
3341                 ret |= visit_load (stmt, op, data);
3342             }
3343         }
3344     }
3345   else if (visit_addr
3346            && gimple_code (stmt) == GIMPLE_PHI)
3347     {
3348       for (i = 0; i < gimple_phi_num_args (stmt); ++i)
3349         {
3350           tree op = PHI_ARG_DEF (stmt, i);
3351           if (TREE_CODE (op) == ADDR_EXPR)
3352             ret |= visit_addr (stmt, TREE_OPERAND (op, 0), data);
3353         }
3354     }
3355
3356   return ret;
3357 }
3358
3359 /* Like walk_stmt_load_store_addr_ops but with NULL visit_addr.  IPA-CP
3360    should make a faster clone for this case.  */
3361
3362 bool
3363 walk_stmt_load_store_ops (gimple stmt, void *data,
3364                           bool (*visit_load)(gimple, tree, void *),
3365                           bool (*visit_store)(gimple, tree, void *))
3366 {
3367   return walk_stmt_load_store_addr_ops (stmt, data,
3368                                         visit_load, visit_store, NULL);
3369 }
3370
3371 /* Helper for gimple_ior_addresses_taken_1.  */
3372
3373 static bool
3374 gimple_ior_addresses_taken_1 (gimple stmt ATTRIBUTE_UNUSED,
3375                               tree addr, void *data)
3376 {
3377   bitmap addresses_taken = (bitmap)data;
3378   while (handled_component_p (addr))
3379     addr = TREE_OPERAND (addr, 0);
3380   if (DECL_P (addr))
3381     {
3382       bitmap_set_bit (addresses_taken, DECL_UID (addr));
3383       return true;
3384     }
3385   return false;
3386 }
3387
3388 /* Set the bit for the uid of all decls that have their address taken
3389    in STMT in the ADDRESSES_TAKEN bitmap.  Returns true if there
3390    were any in this stmt.  */
3391
3392 bool
3393 gimple_ior_addresses_taken (bitmap addresses_taken, gimple stmt)
3394 {
3395   return walk_stmt_load_store_addr_ops (stmt, addresses_taken, NULL, NULL,
3396                                         gimple_ior_addresses_taken_1);
3397 }
3398
3399 #include "gt-gimple.h"