OSDN Git Service

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