OSDN Git Service

* gimple-low.c (struct lower_data): Add the_return_label and
[pf3gnuchains/gcc-fork.git] / gcc / gimplify.c
1 /* Tree lowering pass.  This pass converts the GENERIC functions-as-trees
2    tree representation into the GIMPLE form.
3
4    Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc.
5    Major work done by Sebastian Pop <s.pop@laposte.net>,
6    Diego Novillo <dnovillo@redhat.com> and Jason Merrill <jason@redhat.com>.
7
8 This file is part of GCC.
9
10 GCC is free software; you can redistribute it and/or modify it under
11 the terms of the GNU General Public License as published by the Free
12 Software Foundation; either version 2, or (at your option) any later
13 version.
14
15 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
16 WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
18 for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with GCC; see the file COPYING.  If not, write to the Free
22 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
23 02111-1307, USA.  */
24
25 #include "config.h"
26 #include "system.h"
27 #include "coretypes.h"
28 #include "tm.h"
29 #include "tree.h"
30 #include "rtl.h"
31 #include "errors.h"
32 #include "varray.h"
33 #include "tree-gimple.h"
34 #include "tree-inline.h"
35 #include "diagnostic.h"
36 #include "langhooks.h"
37 #include "langhooks-def.h"
38 #include "tree-flow.h"
39 #include "timevar.h"
40 #include "except.h"
41 #include "hashtab.h"
42 #include "flags.h"
43 #include "real.h"
44 #include "function.h"
45 #include "output.h"
46 #include "expr.h"
47 #include "ggc.h"
48
49 static struct gimplify_ctx
50 {
51   tree current_bind_expr;
52   bool save_stack;
53   tree temps;
54   tree conditional_cleanups;
55   int conditions;
56   tree exit_label;
57   varray_type case_labels;
58   /* The formal temporary table.  Should this be persistent?  */
59   htab_t temp_htab;
60 } *gimplify_ctxp;
61
62
63 /* Formal (expression) temporary table handling: Multiple occurrences of
64    the same scalar expression are evaluated into the same temporary.  */
65
66 typedef struct gimple_temp_hash_elt
67 {
68   tree val;   /* Key */
69   tree temp;  /* Value */
70 } elt_t;
71
72 /* Return a hash value for a formal temporary table entry.  */
73
74 static hashval_t
75 gimple_tree_hash (const void *p)
76 {
77   tree t = ((const elt_t *)p)->val;
78   return iterative_hash_expr (t, 0);
79 }
80
81 /* Compare two formal temporary table entries.  */
82
83 static int
84 gimple_tree_eq (const void *p1, const void *p2)
85 {
86   tree t1 = ((const elt_t *)p1)->val;
87   tree t2 = ((const elt_t *)p2)->val;
88   enum tree_code code = TREE_CODE (t1);
89
90   if (TREE_CODE (t2) != code
91       || TREE_TYPE (t1) != TREE_TYPE (t2))
92     return 0;
93
94   if (!operand_equal_p (t1, t2, 0))
95     return 0;
96
97   /* Only allow them to compare equal if they also hash equal; otherwise
98      results are nondeterminate, and we fail bootstrap comparison.  */
99   if (gimple_tree_hash (p1) != gimple_tree_hash (p2))
100     abort ();
101
102   return 1;
103 }
104
105 /* Set up a context for the gimplifier.  */
106
107 void
108 push_gimplify_context (void)
109 {
110   if (gimplify_ctxp)
111     abort ();
112   gimplify_ctxp
113     = (struct gimplify_ctx *) xcalloc (1, sizeof (struct gimplify_ctx));
114   gimplify_ctxp->temp_htab
115     = htab_create (1000, gimple_tree_hash, gimple_tree_eq, free);
116 }
117
118 /* Tear down a context for the gimplifier.  If BODY is non-null, then
119    put the temporaries into the outer BIND_EXPR.  Otherwise, put them
120    in the unexpanded_var_list.  */
121
122 void
123 pop_gimplify_context (tree body)
124 {
125   if (!gimplify_ctxp || gimplify_ctxp->current_bind_expr)
126     abort ();
127
128   if (body)
129     declare_tmp_vars (gimplify_ctxp->temps, body);
130   else
131     record_vars (gimplify_ctxp->temps);
132
133 #if 0
134   if (!quiet_flag)
135     fprintf (stderr, " collisions: %f ",
136              htab_collisions (gimplify_ctxp->temp_htab));
137 #endif
138
139   htab_delete (gimplify_ctxp->temp_htab);
140   free (gimplify_ctxp);
141   gimplify_ctxp = NULL;
142 }
143
144 void
145 gimple_push_bind_expr (tree bind)
146 {
147   TREE_CHAIN (bind) = gimplify_ctxp->current_bind_expr;
148   gimplify_ctxp->current_bind_expr = bind;
149 }
150
151 void
152 gimple_pop_bind_expr (void)
153 {
154   gimplify_ctxp->current_bind_expr
155     = TREE_CHAIN (gimplify_ctxp->current_bind_expr);
156 }
157
158 tree
159 gimple_current_bind_expr (void)
160 {
161   return gimplify_ctxp->current_bind_expr;
162 }
163
164 /* Returns true iff there is a COND_EXPR between us and the innermost
165    CLEANUP_POINT_EXPR.  This info is used by gimple_push_cleanup.  */
166
167 static bool
168 gimple_conditional_context (void)
169 {
170   return gimplify_ctxp->conditions > 0;
171 }
172
173 /* Note that we've entered a COND_EXPR.  */
174
175 static void
176 gimple_push_condition (void)
177 {
178   ++(gimplify_ctxp->conditions);
179 }
180
181 /* Note that we've left a COND_EXPR.  If we're back at unconditional scope
182    now, add any conditional cleanups we've seen to the prequeue.  */
183
184 static void
185 gimple_pop_condition (tree *pre_p)
186 {
187   int conds = --(gimplify_ctxp->conditions);
188   if (conds == 0)
189     {
190       append_to_statement_list (gimplify_ctxp->conditional_cleanups, pre_p);
191       gimplify_ctxp->conditional_cleanups = NULL_TREE;
192     }
193   else if (conds < 0)
194     abort ();
195 }
196
197 /* A subroutine of append_to_statement_list{,_force}.  */
198
199 static void
200 append_to_statement_list_1 (tree t, tree *list_p, bool side_effects)
201 {
202   tree list = *list_p;
203   tree_stmt_iterator i;
204
205   if (!side_effects)
206     return;
207
208   if (!list)
209     {
210       if (t && TREE_CODE (t) == STATEMENT_LIST)
211         {
212           *list_p = t;
213           return;
214         }
215       *list_p = list = alloc_stmt_list ();
216     }
217
218   i = tsi_last (list);
219   tsi_link_after (&i, t, TSI_CONTINUE_LINKING);
220 }
221
222 /* Add T to the end of the list container pointed by LIST_P.
223    If T is an expression with no effects, it is ignored.  */
224
225 void
226 append_to_statement_list (tree t, tree *list_p)
227 {
228   append_to_statement_list_1 (t, list_p, t ? TREE_SIDE_EFFECTS (t) : false);
229 }
230
231 /* Similar, but the statement is always added, regardless of side effects.  */
232
233 void
234 append_to_statement_list_force (tree t, tree *list_p)
235 {
236   append_to_statement_list_1 (t, list_p, t != NULL);
237 }
238
239 /* Add T to the end of a COMPOUND_EXPR pointed by LIST_P.  The type
240    of the result is the type of T.  */
241
242 void
243 append_to_compound_expr (tree t, tree *list_p)
244 {
245   if (!t)
246     return;
247   if (!*list_p)
248     *list_p = t;
249   else
250     *list_p = build (COMPOUND_EXPR, TREE_TYPE (t), *list_p, t);
251 }
252
253 /* Strip off a legitimate source ending from the input string NAME of
254    length LEN.  Rather than having to know the names used by all of
255    our front ends, we strip off an ending of a period followed by
256    up to five characters.  (Java uses ".class".)  */
257
258 static inline void
259 remove_suffix (char *name, int len)
260 {
261   int i;
262
263   for (i = 2;  i < 8 && len > i;  i++)
264     {
265       if (name[len - i] == '.')
266         {
267           name[len - i] = '\0';
268           break;
269         }
270     }
271 }
272
273 /* Create a nameless artificial label and put it in the current function
274    context.  Returns the newly created label.  */
275
276 tree
277 create_artificial_label (void)
278 {
279   tree lab = build_decl (LABEL_DECL, NULL_TREE, void_type_node);
280   DECL_ARTIFICIAL (lab) = 1;
281   DECL_CONTEXT (lab) = current_function_decl;
282   return lab;
283 }
284
285 /* Create a new temporary name with PREFIX.  Returns an identifier.  */
286
287 static GTY(()) unsigned int tmp_var_id_num;
288
289 tree
290 create_tmp_var_name (const char *prefix)
291 {
292   char *tmp_name;
293
294   if (prefix)
295     {
296       char *preftmp = ASTRDUP (prefix);
297       remove_suffix (preftmp, strlen (preftmp));
298       prefix = preftmp;
299     }
300
301   ASM_FORMAT_PRIVATE_NAME (tmp_name, prefix ? prefix : "T", tmp_var_id_num++);
302   return get_identifier (tmp_name);
303 }
304
305
306 /* Create a new temporary variable declaration of type TYPE.
307    Does NOT push it into the current binding.  */
308
309 tree
310 create_tmp_var_raw (tree type, const char *prefix)
311 {
312   tree tmp_var;
313   tree new_type;
314
315   /* Make the type of the variable writable.  */
316   new_type = build_type_variant (type, 0, 0);
317   TYPE_ATTRIBUTES (new_type) = TYPE_ATTRIBUTES (type);
318
319   tmp_var = build_decl (VAR_DECL, create_tmp_var_name (prefix), type);
320
321   /* The variable was declared by the compiler.  */
322   DECL_ARTIFICIAL (tmp_var) = 1;
323   /* And we don't want debug info for it.  */
324   DECL_IGNORED_P (tmp_var) = 1;
325
326   /* Make the variable writable.  */
327   TREE_READONLY (tmp_var) = 0;
328
329   DECL_EXTERNAL (tmp_var) = 0;
330   TREE_STATIC (tmp_var) = 0;
331   TREE_USED (tmp_var) = 1;
332
333   return tmp_var;
334 }
335
336 /* Create a new temporary variable declaration of type TYPE.  DOES push the
337    variable into the current binding.  Further, assume that this is called
338    only from gimplification or optimization, at which point the creation of
339    certain types are bugs.  */
340
341 tree
342 create_tmp_var (tree type, const char *prefix)
343 {
344   tree tmp_var;
345
346 #if defined ENABLE_CHECKING
347   /* If the type is an array or a type which must be created by the
348      frontend, something is wrong.  */
349   if (TREE_CODE (type) == ARRAY_TYPE || TREE_ADDRESSABLE (type))
350     abort ();
351   if (!COMPLETE_TYPE_P (type))
352     abort ();
353   /* Variable sized types require lots of machinery to create; the
354      optimizers shouldn't be doing anything of the sort.  */
355   if (TREE_CODE (TYPE_SIZE_UNIT (type)) != INTEGER_CST)
356     abort ();
357 #endif
358
359   tmp_var = create_tmp_var_raw (type, prefix);
360   gimple_add_tmp_var (tmp_var);
361   return tmp_var;
362 }
363
364 /*  Given a tree, try to return a useful variable name that we can use
365     to prefix a temporary that is being assigned the value of the tree.
366     I.E. given  <temp> = &A, return A.  */
367
368 const char *
369 get_name (tree t)
370 {
371   tree stripped_decl;
372
373   stripped_decl = t;
374   STRIP_NOPS (stripped_decl);
375   if (DECL_P (stripped_decl) && DECL_NAME (stripped_decl))
376     return IDENTIFIER_POINTER (DECL_NAME (stripped_decl));
377   else
378     {
379       switch (TREE_CODE (stripped_decl))
380         {
381         case ADDR_EXPR:
382           return get_name (TREE_OPERAND (stripped_decl, 0));
383           break;
384         default:
385           return NULL;
386         }
387     }
388 }
389
390 /* Create a temporary with a name derived from VAL.  Subroutine of
391    lookup_tmp_var; nobody else should call this function.  */
392
393 static inline tree
394 create_tmp_from_val (tree val)
395 {
396   return create_tmp_var (TREE_TYPE (val), get_name (val));
397 }
398
399 /* Create a temporary to hold the value of VAL.  If IS_FORMAL, try to reuse
400    an existing expression temporary.  */
401
402 static tree
403 lookup_tmp_var (tree val, bool is_formal)
404 {
405   if (!is_formal || TREE_SIDE_EFFECTS (val))
406     return create_tmp_from_val (val);
407   else
408     {
409       elt_t elt, *elt_p;
410       void **slot;
411
412       elt.val = val;
413       slot = htab_find_slot (gimplify_ctxp->temp_htab, (void *)&elt, INSERT);
414       if (*slot == NULL)
415         {
416           elt_p = xmalloc (sizeof (*elt_p));
417           elt_p->val = val;
418           elt_p->temp = create_tmp_from_val (val);
419           *slot = (void *)elt_p;
420         }
421       else
422         elt_p = (elt_t *) *slot;
423
424       return elt_p->temp;
425     }
426 }
427
428 /* Returns a formal temporary variable initialized with VAL.  PRE_P is as
429    in gimplify_expr.  Only use this function if:
430
431    1) The value of the unfactored expression represented by VAL will not
432       change between the initialization and use of the temporary, and
433    2) The temporary will not be otherwise modified.
434
435    For instance, #1 means that this is inappropriate for SAVE_EXPR temps,
436    and #2 means it is inappropriate for && temps.
437
438    For other cases, use get_initialized_tmp_var instead.  */
439
440 static tree
441 internal_get_tmp_var (tree val, tree *pre_p, tree *post_p, bool is_formal)
442 {
443   tree t, mod;
444   char class;
445
446   gimplify_expr (&val, pre_p, post_p, is_gimple_rhs, fb_rvalue);
447
448   t = lookup_tmp_var (val, is_formal);
449
450   mod = build (MODIFY_EXPR, TREE_TYPE (t), t, val);
451
452   class = TREE_CODE_CLASS (TREE_CODE (val));
453   if (EXPR_LOCUS (val))
454     SET_EXPR_LOCUS (mod, EXPR_LOCUS (val));
455   else
456     annotate_with_locus (mod, input_location);
457   /* gimplify_modify_expr might want to reduce this further.  */
458   gimplify_stmt (&mod);
459   append_to_statement_list (mod, pre_p);
460
461   return t;
462 }
463
464 tree
465 get_formal_tmp_var (tree val, tree *pre_p)
466 {
467   return internal_get_tmp_var (val, pre_p, NULL, true);
468 }
469
470 /* Returns a temporary variable initialized with VAL.  PRE_P and POST_P
471    are as in gimplify_expr.  */
472
473 tree
474 get_initialized_tmp_var (tree val, tree *pre_p, tree *post_p)
475 {
476   return internal_get_tmp_var (val, pre_p, post_p, false);
477 }
478
479 /*  Returns true if T is a GIMPLE temporary variable, false otherwise.  */
480
481 bool
482 is_gimple_tmp_var (tree t)
483 {
484   /* FIXME this could trigger for other local artificials, too.  */
485   return (TREE_CODE (t) == VAR_DECL && DECL_ARTIFICIAL (t)
486           && !TREE_STATIC (t) && !DECL_EXTERNAL (t));
487 }
488
489 /* Declares all the variables in VARS in SCOPE.  Returns the last
490    DECL_STMT emitted.  */
491
492 void
493 declare_tmp_vars (tree vars, tree scope)
494 {
495   tree last = vars;
496   if (last)
497     {
498       tree temps;
499
500       /* C99 mode puts the default 'return 0;' for main() outside the outer
501          braces.  So drill down until we find an actual scope.  */
502       while (TREE_CODE (scope) == COMPOUND_EXPR)
503         scope = TREE_OPERAND (scope, 0);
504
505       if (TREE_CODE (scope) != BIND_EXPR)
506         abort ();
507
508       temps = nreverse (last);
509       TREE_CHAIN (last) = BIND_EXPR_VARS (scope);
510       BIND_EXPR_VARS (scope) = temps;
511
512       /* We don't add the temps to the block for this BIND_EXPR, as we're
513          not interested in debugging info for them.  */
514     }
515 }
516
517 void
518 gimple_add_tmp_var (tree tmp)
519 {
520   if (TREE_CHAIN (tmp) || tmp->decl.seen_in_bind_expr)
521     abort ();
522
523   DECL_CONTEXT (tmp) = current_function_decl;
524   tmp->decl.seen_in_bind_expr = 1;
525
526   if (gimplify_ctxp)
527     {
528       TREE_CHAIN (tmp) = gimplify_ctxp->temps;
529       gimplify_ctxp->temps = tmp;
530     }
531   else if (cfun)
532     record_vars (tmp);
533   else
534     declare_tmp_vars (tmp, DECL_SAVED_TREE (current_function_decl));
535 }
536
537 /* Determines whether to assign a locus to the statement STMT.  */
538
539 static bool
540 should_carry_locus_p (tree stmt)
541 {
542   /* Don't emit a line note for a label.  We particularly don't want to
543      emit one for the break label, since it doesn't actually correspond
544      to the beginning of the loop/switch.  */
545   if (TREE_CODE (stmt) == LABEL_EXPR)
546     return false;
547
548   /* Do not annotate empty statements, since it confuses gcov.  */
549   if (!TREE_SIDE_EFFECTS (stmt))
550     return false;
551
552   return true;
553 }
554
555 void
556 annotate_all_with_locus (tree *stmt_p, location_t locus)
557 {
558   tree_stmt_iterator i;
559
560   if (!*stmt_p)
561     return;
562
563   for (i = tsi_start (*stmt_p); !tsi_end_p (i); tsi_next (&i))
564     {
565       tree t = tsi_stmt (i);
566
567 #ifdef ENABLE_CHECKING
568           /* Assuming we've already been gimplified, we shouldn't
569              see nested chaining constructs anymore.  */
570           if (TREE_CODE (t) == STATEMENT_LIST
571               || TREE_CODE (t) == COMPOUND_EXPR)
572             abort ();
573 #endif
574
575       if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (t)))
576           && ! EXPR_HAS_LOCATION (t)
577           && should_carry_locus_p (t))
578         annotate_with_locus (t, locus);
579     }
580 }
581
582 /* Similar to copy_tree_r() but do not copy SAVE_EXPR or TARGET_EXPR nodes.
583    These nodes model computations that should only be done once.  If we
584    were to unshare something like SAVE_EXPR(i++), the gimplification
585    process would create wrong code.  */
586
587 static tree
588 mostly_copy_tree_r (tree *tp, int *walk_subtrees, void *data)
589 {
590   enum tree_code code = TREE_CODE (*tp);
591   /* Don't unshare types, decls, constants and SAVE_EXPR nodes.  */
592   if (TREE_CODE_CLASS (code) == 't'
593       || TREE_CODE_CLASS (code) == 'd'
594       || TREE_CODE_CLASS (code) == 'c'
595       || code == SAVE_EXPR || code == TARGET_EXPR
596       /* We can't do anything sensible with a BLOCK used as an expression,
597          but we also can't abort when we see it because of non-expression
598          uses.  So just avert our eyes and cross our fingers.  Silly Java.  */
599       || code == BLOCK)
600     *walk_subtrees = 0;
601   else if (code == BIND_EXPR)
602     abort ();
603   else
604     copy_tree_r (tp, walk_subtrees, data);
605
606   return NULL_TREE;
607 }
608
609 /* Mark all the _DECL nodes under *TP as volatile.  FIXME: This must die
610    after VA_ARG_EXPRs are properly lowered.  */
611
612 static tree
613 mark_decls_volatile_r (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
614                        void *data ATTRIBUTE_UNUSED)
615 {
616   if (SSA_VAR_P (*tp))
617     TREE_THIS_VOLATILE (*tp) = 1;
618
619   return NULL_TREE;
620 }
621
622
623 /* Callback for walk_tree to unshare most of the shared trees rooted at
624    *TP.  If *TP has been visited already (i.e., TREE_VISITED (*TP) == 1),
625    then *TP is deep copied by calling copy_tree_r.
626
627    This unshares the same trees as copy_tree_r with the exception of
628    SAVE_EXPR nodes.  These nodes model computations that should only be
629    done once.  If we were to unshare something like SAVE_EXPR(i++), the
630    gimplification process would create wrong code.  */
631
632 static tree
633 copy_if_shared_r (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
634                   void *data ATTRIBUTE_UNUSED)
635 {
636   tree t = *tp;
637   enum tree_code code = TREE_CODE (t);
638
639   /* Skip types, decls, and constants.  */
640   if (TREE_CODE_CLASS (code) == 't'
641       || TREE_CODE_CLASS (code) == 'd'
642       || TREE_CODE_CLASS (code) == 'c')
643     *walk_subtrees = 0;
644
645   /* Special-case BIND_EXPR.  We should never be copying these, therefore
646      we can omit examining BIND_EXPR_VARS.  Which also avoids problems with
647      double processing of the DECL_INITIAL, which could be seen via both
648      the BIND_EXPR_VARS and a DECL_STMT.  */
649   else if (code == BIND_EXPR)
650     {
651       if (TREE_VISITED (t))
652         abort ();
653       TREE_VISITED (t) = 1;
654       *walk_subtrees = 0;
655       walk_tree (&BIND_EXPR_BODY (t), copy_if_shared_r, NULL, NULL);
656     }
657
658   /* If this node has been visited already, unshare it and don't look
659      any deeper.  */
660   else if (TREE_VISITED (t))
661     {
662       walk_tree (tp, mostly_copy_tree_r, NULL, NULL);
663       *walk_subtrees = 0;
664     }
665
666   /* Otherwise, mark the tree as visited and keep looking.  */
667   else
668     TREE_VISITED (t) = 1;
669
670   return NULL_TREE;
671 }
672
673 static tree
674 unmark_visited_r (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
675                   void *data ATTRIBUTE_UNUSED)
676 {
677   if (TREE_VISITED (*tp))
678     TREE_VISITED (*tp) = 0;
679   else
680     *walk_subtrees = 0;
681
682   return NULL_TREE;
683 }
684
685 /* Unshare T and all the trees reached from T via TREE_CHAIN.  */
686
687 void
688 unshare_all_trees (tree t)
689 {
690   walk_tree (&t, copy_if_shared_r, NULL, NULL);
691   walk_tree (&t, unmark_visited_r, NULL, NULL);
692 }
693
694 /* Unconditionally make an unshared copy of EXPR.  This is used when using
695    stored expressions which span multiple functions, such as BINFO_VTABLE,
696    as the normal unsharing process can't tell that they're shared.  */
697
698 tree
699 unshare_expr (tree expr)
700 {
701   walk_tree (&expr, mostly_copy_tree_r, NULL, NULL);
702   return expr;
703 }
704
705 /* A terser interface for building a representation of a exception
706    specification.  */
707
708 tree
709 gimple_build_eh_filter (tree body, tree allowed, tree failure)
710 {
711   tree t;
712
713   /* FIXME should the allowed types go in TREE_TYPE?  */
714   t = build (EH_FILTER_EXPR, void_type_node, allowed, NULL_TREE);
715   append_to_statement_list (failure, &EH_FILTER_FAILURE (t));
716
717   t = build (TRY_CATCH_EXPR, void_type_node, NULL_TREE, t);
718   append_to_statement_list (body, &TREE_OPERAND (t, 0));
719
720   return t;
721 }
722
723 \f
724 /* WRAPPER is a code such as BIND_EXPR or CLEANUP_POINT_EXPR which can both
725    contain statements and have a value.  Assign its value to a temporary
726    and give it void_type_node.  Returns the temporary, or NULL_TREE if
727    WRAPPER was already void.  */
728
729 tree
730 voidify_wrapper_expr (tree wrapper)
731 {
732   if (!VOID_TYPE_P (TREE_TYPE (wrapper)))
733     {
734       tree *p;
735       tree temp;
736
737       /* Set p to point to the body of the wrapper.  */
738       switch (TREE_CODE (wrapper))
739         {
740         case BIND_EXPR:
741           /* For a BIND_EXPR, the body is operand 1.  */
742           p = &BIND_EXPR_BODY (wrapper);
743           break;
744
745         default:
746           p = &TREE_OPERAND (wrapper, 0);
747           break;
748         }
749
750       /* Advance to the last statement.  Set all container types to void.  */
751       if (TREE_CODE (*p) == STATEMENT_LIST)
752         {
753           tree_stmt_iterator i = tsi_last (*p);
754           p = tsi_end_p (i) ? NULL : tsi_stmt_ptr (i);
755         }
756       else
757         { 
758           for (; TREE_CODE (*p) == COMPOUND_EXPR; p = &TREE_OPERAND (*p, 1))
759             {
760               TREE_SIDE_EFFECTS (*p) = 1;
761               TREE_TYPE (*p) = void_type_node;
762             }
763         }
764
765       if (p && TREE_CODE (*p) == INIT_EXPR)
766         {
767           /* The C++ frontend already did this for us.  */;
768           temp = TREE_OPERAND (*p, 0);
769         }
770       else if (p && TREE_CODE (*p) == INDIRECT_REF)
771         {
772           /* If we're returning a dereference, move the dereference outside
773              the wrapper.  */
774           tree ptr = TREE_OPERAND (*p, 0);
775           temp = create_tmp_var (TREE_TYPE (ptr), "retval");
776           *p = build (MODIFY_EXPR, TREE_TYPE (ptr), temp, ptr);
777           temp = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (temp)), temp);
778           /* If this is a BIND_EXPR for a const inline function, it might not
779              have TREE_SIDE_EFFECTS set.  That is no longer accurate.  */
780           TREE_SIDE_EFFECTS (wrapper) = 1;
781         }
782       else
783         {
784           temp = create_tmp_var (TREE_TYPE (wrapper), "retval");
785           if (p && !IS_EMPTY_STMT (*p))
786             {
787               *p = build (MODIFY_EXPR, TREE_TYPE (temp), temp, *p);
788               TREE_SIDE_EFFECTS (wrapper) = 1;
789             }
790         }
791
792       TREE_TYPE (wrapper) = void_type_node;
793       return temp;
794     }
795
796   return NULL_TREE;
797 }
798
799 /* Prepare calls to builtins to SAVE and RESTORE the stack as well as
800    a temporary through which they communicate.  */
801
802 static void
803 build_stack_save_restore (tree *save, tree *restore)
804 {
805   tree save_call, tmp_var;
806
807   save_call =
808       build_function_call_expr (implicit_built_in_decls[BUILT_IN_STACK_SAVE],
809                                 NULL_TREE);
810   tmp_var = create_tmp_var (ptr_type_node, "saved_stack");
811
812   *save = build (MODIFY_EXPR, ptr_type_node, tmp_var, save_call);
813   *restore =
814     build_function_call_expr (implicit_built_in_decls[BUILT_IN_STACK_RESTORE],
815                               tree_cons (NULL_TREE, tmp_var, NULL_TREE));
816 }
817
818 /* Gimplify a BIND_EXPR.  Just voidify and recurse.  */
819
820 static enum gimplify_status
821 gimplify_bind_expr (tree *expr_p, tree *pre_p)
822 {
823   tree bind_expr = *expr_p;
824   tree temp = voidify_wrapper_expr (bind_expr);
825   bool old_save_stack = gimplify_ctxp->save_stack;
826   tree t;
827
828   /* Mark variables seen in this bind expr.  */
829   for (t = BIND_EXPR_VARS (bind_expr); t ; t = TREE_CHAIN (t))
830     t->decl.seen_in_bind_expr = 1;
831
832   gimple_push_bind_expr (bind_expr);
833   gimplify_ctxp->save_stack = false;
834
835   gimplify_to_stmt_list (&BIND_EXPR_BODY (bind_expr));
836
837   if (gimplify_ctxp->save_stack)
838     {
839       tree stack_save, stack_restore;
840
841       /* Save stack on entry and restore it on exit.  Add a try_finally
842          block to achieve this.  Note that mudflap depends on the
843          format of the emitted code: see mx_register_decls().  */
844       build_stack_save_restore (&stack_save, &stack_restore);
845
846       t = build (TRY_FINALLY_EXPR, void_type_node,
847                  BIND_EXPR_BODY (bind_expr), NULL_TREE);
848       append_to_statement_list (stack_restore, &TREE_OPERAND (t, 1));
849
850       BIND_EXPR_BODY (bind_expr) = NULL_TREE;
851       append_to_statement_list (stack_save, &BIND_EXPR_BODY (bind_expr));
852       append_to_statement_list (t, &BIND_EXPR_BODY (bind_expr));
853     }
854
855   gimplify_ctxp->save_stack = old_save_stack;
856   gimple_pop_bind_expr ();
857
858   if (temp)
859     {
860       *expr_p = temp;
861       append_to_statement_list (bind_expr, pre_p);
862       return GS_OK;
863     }
864   else
865     return GS_ALL_DONE;
866 }
867
868 /* Gimplify a RETURN_EXPR.  If the expression to be returned is not a
869    GIMPLE value, it is assigned to a new temporary and the statement is
870    re-written to return the temporary.
871
872    PRE_P points to the list where side effects that must happen before
873    STMT should be stored.  */
874
875 static enum gimplify_status
876 gimplify_return_expr (tree stmt, tree *pre_p)
877 {
878   tree ret_expr = TREE_OPERAND (stmt, 0);
879   tree result;
880
881   if (!ret_expr || TREE_CODE (ret_expr) == RESULT_DECL)
882     return GS_ALL_DONE;
883
884   if (ret_expr == error_mark_node)
885     return GS_ERROR;
886
887   if (VOID_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl))))
888     result = NULL_TREE;
889   else
890     {
891       result = TREE_OPERAND (ret_expr, 0);
892 #ifdef ENABLE_CHECKING
893       if ((TREE_CODE (ret_expr) != MODIFY_EXPR
894            && TREE_CODE (ret_expr) != INIT_EXPR)
895           || TREE_CODE (result) != RESULT_DECL)
896         abort ();
897 #endif
898     }
899
900   /* We need to pass the full MODIFY_EXPR down so that special handling
901      can replace it with something else.  */
902   gimplify_stmt (&TREE_OPERAND (stmt, 0));
903   append_to_statement_list (TREE_OPERAND (stmt, 0), pre_p);
904
905   TREE_OPERAND (stmt, 0) = result;
906
907   return GS_ALL_DONE;
908 }
909
910 /* Gimplify a LOOP_EXPR.  Normally this just involves gimplifying the body
911    and replacing the LOOP_EXPR with goto, but if the loop contains an
912    EXIT_EXPR, we need to append a label for it to jump to.  */
913
914 static enum gimplify_status
915 gimplify_loop_expr (tree *expr_p, tree *pre_p)
916 {
917   tree saved_label = gimplify_ctxp->exit_label;
918   tree start_label = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
919   tree jump_stmt = build_and_jump (&LABEL_EXPR_LABEL (start_label));
920
921   append_to_statement_list (start_label, pre_p);
922
923   gimplify_ctxp->exit_label = NULL_TREE;
924
925   gimplify_stmt (&LOOP_EXPR_BODY (*expr_p));
926   append_to_statement_list (LOOP_EXPR_BODY (*expr_p), pre_p);
927
928   if (gimplify_ctxp->exit_label)
929     {
930       append_to_statement_list (jump_stmt, pre_p);
931       *expr_p = build1 (LABEL_EXPR, void_type_node, gimplify_ctxp->exit_label);
932     }
933   else
934     *expr_p = jump_stmt;
935
936   gimplify_ctxp->exit_label = saved_label;
937
938   return GS_ALL_DONE;
939 }
940
941 /* Compare two case labels.  Because the front end should already have
942    made sure that case ranges do not overlap, it is enough to only compare
943    the CASE_LOW values of each case label.  */
944
945 static int
946 compare_case_labels (const void *p1, const void *p2)
947 {
948   tree case1 = *(tree *)p1;
949   tree case2 = *(tree *)p2;
950
951   return tree_int_cst_compare (CASE_LOW (case1), CASE_LOW (case2));
952 }
953
954 /* Sort the case labels in LABEL_VEC in ascending order.  */
955
956 void
957 sort_case_labels (tree label_vec)
958 {
959   size_t len = TREE_VEC_LENGTH (label_vec);
960   tree default_case = TREE_VEC_ELT (label_vec, len - 1);
961
962   if (CASE_LOW (default_case))
963     {
964       size_t i;
965
966       /* The last label in the vector should be the default case
967          but it is not.  */
968       for (i = 0; i < len; ++i)
969         {
970           tree t = TREE_VEC_ELT (label_vec, i);
971           if (!CASE_LOW (t))
972             {
973               default_case = t;
974               TREE_VEC_ELT (label_vec, i) = TREE_VEC_ELT (label_vec, len - 1);
975               TREE_VEC_ELT (label_vec, len - 1) = default_case;
976               break;
977             }
978         }
979     }
980
981   qsort (&TREE_VEC_ELT (label_vec, 0), len - 1, sizeof (tree),
982          compare_case_labels);
983 }
984
985 /* Gimplify a SWITCH_EXPR, and collect a TREE_VEC of the labels it can
986    branch to.  */
987
988 static enum gimplify_status
989 gimplify_switch_expr (tree *expr_p, tree *pre_p)
990 {
991   tree switch_expr = *expr_p;
992   enum gimplify_status ret;
993
994   ret = gimplify_expr (&SWITCH_COND (switch_expr), pre_p, NULL,
995                        is_gimple_val, fb_rvalue);
996
997   if (SWITCH_BODY (switch_expr))
998     {
999       varray_type labels, saved_labels;
1000       tree label_vec, default_case = NULL_TREE;
1001       size_t i, len;
1002
1003       /* If someone can be bothered to fill in the labels, they can
1004          be bothered to null out the body too.  */
1005       if (SWITCH_LABELS (switch_expr))
1006         abort ();
1007
1008       saved_labels = gimplify_ctxp->case_labels;
1009       VARRAY_TREE_INIT (gimplify_ctxp->case_labels, 8, "case_labels");
1010
1011       gimplify_to_stmt_list (&SWITCH_BODY (switch_expr));
1012
1013       labels = gimplify_ctxp->case_labels;
1014       gimplify_ctxp->case_labels = saved_labels;
1015
1016       len = VARRAY_ACTIVE_SIZE (labels);
1017
1018       for (i = 0; i < len; ++i)
1019         {
1020           tree t = VARRAY_TREE (labels, i);
1021           if (!CASE_LOW (t))
1022             {
1023               /* The default case must be the last label in the list.  */
1024               default_case = t;
1025               VARRAY_TREE (labels, i) = VARRAY_TREE (labels, len - 1);
1026               len--;
1027               break;
1028             }
1029         }
1030
1031       label_vec = make_tree_vec (len + 1);
1032       SWITCH_LABELS (*expr_p) = label_vec;
1033       append_to_statement_list (switch_expr, pre_p);
1034
1035       if (! default_case)
1036         {
1037           /* If the switch has no default label, add one, so that we jump
1038              around the switch body.  */
1039           default_case = build (CASE_LABEL_EXPR, void_type_node, NULL_TREE,
1040                                 NULL_TREE, create_artificial_label ());
1041           append_to_statement_list (SWITCH_BODY (switch_expr), pre_p);
1042           *expr_p = build (LABEL_EXPR, void_type_node,
1043                            CASE_LABEL (default_case));
1044         }
1045       else
1046         *expr_p = SWITCH_BODY (switch_expr);
1047
1048       for (i = 0; i < len; ++i)
1049         TREE_VEC_ELT (label_vec, i) = VARRAY_TREE (labels, i);
1050       TREE_VEC_ELT (label_vec, len) = default_case;
1051
1052       sort_case_labels (label_vec);
1053
1054       SWITCH_BODY (switch_expr) = NULL;
1055     }
1056   else if (!SWITCH_LABELS (switch_expr))
1057     abort ();
1058
1059   return ret;
1060 }
1061
1062 static enum gimplify_status
1063 gimplify_case_label_expr (tree *expr_p)
1064 {
1065   tree expr = *expr_p;
1066   if (gimplify_ctxp->case_labels)
1067     VARRAY_PUSH_TREE (gimplify_ctxp->case_labels, expr);
1068   else
1069     abort ();
1070   *expr_p = build (LABEL_EXPR, void_type_node, CASE_LABEL (expr));
1071   return GS_ALL_DONE;
1072 }
1073
1074 /* Gimplify a LABELED_BLOCK_EXPR into a LABEL_EXPR following
1075    a (possibly empty) body.  */
1076
1077 static enum gimplify_status
1078 gimplify_labeled_block_expr (tree *expr_p)
1079 {
1080   tree body = LABELED_BLOCK_BODY (*expr_p);
1081   tree label = LABELED_BLOCK_LABEL (*expr_p);
1082   tree t;
1083
1084   DECL_CONTEXT (label) = current_function_decl;
1085   t = build (LABEL_EXPR, void_type_node, label);
1086   if (body != NULL_TREE)
1087     t = build (COMPOUND_EXPR, void_type_node, body, t);
1088   *expr_p = t;
1089
1090   return GS_OK;
1091 }
1092
1093 /* Gimplify a EXIT_BLOCK_EXPR into a GOTO_EXPR.  */
1094
1095 static enum gimplify_status
1096 gimplify_exit_block_expr (tree *expr_p)
1097 {
1098   tree labeled_block = TREE_OPERAND (*expr_p, 0);
1099   tree label;
1100
1101   /* First operand must be a LABELED_BLOCK_EXPR, which should
1102      already be lowered (or partially lowered) when we get here.  */
1103 #if defined ENABLE_CHECKING
1104   if (TREE_CODE (labeled_block) != LABELED_BLOCK_EXPR)
1105     abort ();
1106 #endif
1107
1108   label = LABELED_BLOCK_LABEL (labeled_block);
1109   *expr_p = build1 (GOTO_EXPR, void_type_node, label);
1110
1111   return GS_OK;
1112 }
1113
1114 /* Build a GOTO to the LABEL_DECL pointed to by LABEL_P, building it first
1115    if necessary.  */
1116
1117 tree
1118 build_and_jump (tree *label_p)
1119 {
1120   if (label_p == NULL)
1121     /* If there's nowhere to jump, just fall through.  */
1122     return build_empty_stmt ();
1123
1124   if (*label_p == NULL_TREE)
1125     {
1126       tree label = create_artificial_label ();
1127       *label_p = label;
1128     }
1129
1130   return build1 (GOTO_EXPR, void_type_node, *label_p);
1131 }
1132
1133 /* Gimplify an EXIT_EXPR by converting to a GOTO_EXPR inside a COND_EXPR.
1134    This also involves building a label to jump to and communicating it to
1135    gimplify_loop_expr through gimplify_ctxp->exit_label.  */
1136
1137 static enum gimplify_status
1138 gimplify_exit_expr (tree *expr_p)
1139 {
1140   tree cond = TREE_OPERAND (*expr_p, 0);
1141   tree expr;
1142
1143   expr = build_and_jump (&gimplify_ctxp->exit_label);
1144   expr = build (COND_EXPR, void_type_node, cond, expr, build_empty_stmt ());
1145   *expr_p = expr;
1146
1147   return GS_OK;
1148 }
1149
1150 /* A helper function to be called via walk_tree.  Mark all labels under *TP
1151    as being forced.  To be called for DECL_INITIAL of static variables.  */
1152
1153 tree
1154 force_labels_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
1155 {
1156   if (TYPE_P (*tp))
1157     *walk_subtrees = 0;
1158   if (TREE_CODE (*tp) == LABEL_DECL)
1159     FORCED_LABEL (*tp) = 1;
1160
1161   return NULL_TREE;
1162 }
1163
1164 /* Break out elements of a constructor used as an initializer into separate
1165    MODIFY_EXPRs.
1166
1167    Note that we still need to clear any elements that don't have explicit
1168    initializers, so if not all elements are initialized we keep the
1169    original MODIFY_EXPR, we just remove all of the constructor elements.  */
1170
1171 static enum gimplify_status
1172 gimplify_init_constructor (tree *expr_p, tree *pre_p,
1173                            tree *post_p, int want_value)
1174 {
1175   tree object = TREE_OPERAND (*expr_p, 0);
1176   tree ctor = TREE_OPERAND (*expr_p, 1);
1177   tree type = TREE_TYPE (ctor);
1178   enum gimplify_status ret;
1179   tree elt_list;
1180
1181   if (TREE_CODE (ctor) != CONSTRUCTOR)
1182     return GS_UNHANDLED;
1183
1184   elt_list = CONSTRUCTOR_ELTS (ctor);
1185
1186   ret = GS_ALL_DONE;
1187   switch (TREE_CODE (type))
1188     {
1189     case RECORD_TYPE:
1190     case UNION_TYPE:
1191     case QUAL_UNION_TYPE:
1192     case ARRAY_TYPE:
1193       {
1194         HOST_WIDE_INT i, num_elements, num_nonzero_elements;
1195         HOST_WIDE_INT num_nonconstant_elements;
1196         bool cleared;
1197
1198         /* Aggregate types must lower constructors to initialization of
1199            individual elements.  The exception is that a CONSTRUCTOR node
1200            with no elements indicates zero-initialization of the whole.  */
1201         if (elt_list == NULL)
1202           {
1203             if (want_value)
1204               {
1205                 *expr_p = object;
1206                 return GS_OK;
1207               }
1208             else
1209               return GS_ALL_DONE;
1210           }
1211
1212         categorize_ctor_elements (ctor, &num_nonzero_elements,
1213                                   &num_nonconstant_elements);
1214         num_elements = count_type_elements (TREE_TYPE (ctor));
1215
1216         /* If a const aggregate variable is being initialized, then it
1217            should never be a lose to promote the variable to be static.  */
1218         if (num_nonconstant_elements == 0
1219             && TREE_READONLY (object)
1220             && TREE_CODE (object) == VAR_DECL)
1221           {
1222             DECL_INITIAL (object) = ctor;
1223             TREE_STATIC (object) = 1;
1224             if (!DECL_NAME (object))
1225               DECL_NAME (object) = create_tmp_var_name ("C");
1226             walk_tree (&DECL_INITIAL (object), force_labels_r, NULL, NULL);
1227
1228             /* ??? C++ doesn't automatically append a .<number> to the
1229                assembler name, and even when it does, it looks a FE private
1230                data structures to figure out what that number should be,
1231                which are not set for this variable.  I suppose this is
1232                important for local statics for inline functions, which aren't
1233                "local" in the object file sense.  So in order to get a unique
1234                TU-local symbol, we must invoke the lhd version now.  */
1235             lhd_set_decl_assembler_name (object);
1236
1237             *expr_p = build_empty_stmt ();
1238             break;
1239           }
1240
1241         /* If there are "lots" of initialized elements, and all of them
1242            are valid address constants, then the entire initializer can
1243            be dropped to memory, and then memcpy'd out.  */
1244         if (num_nonconstant_elements == 0)
1245           {
1246             HOST_WIDE_INT size = int_size_in_bytes (type);
1247             unsigned int align;
1248
1249             /* ??? We can still get unbounded array types, at least
1250                from the C++ front end.  This seems wrong, but attempt
1251                to work around it for now.  */
1252             if (size < 0)
1253               {
1254                 size = int_size_in_bytes (TREE_TYPE (object));
1255                 if (size >= 0)
1256                   TREE_TYPE (ctor) = type = TREE_TYPE (object);
1257               }
1258
1259             /* Find the maximum alignment we can assume for the object.  */
1260             /* ??? Make use of DECL_OFFSET_ALIGN.  */
1261             if (DECL_P (object))
1262               align = DECL_ALIGN (object);
1263             else
1264               align = TYPE_ALIGN (type);
1265
1266             if (size > 0 && !can_move_by_pieces (size, align))
1267               {
1268                 tree new = create_tmp_var_raw (type, "C");
1269                 gimple_add_tmp_var (new);
1270                 TREE_STATIC (new) = 1;
1271                 TREE_READONLY (new) = 1;
1272                 DECL_INITIAL (new) = ctor;
1273                 if (align > DECL_ALIGN (new))
1274                   {
1275                     DECL_ALIGN (new) = align;
1276                     DECL_USER_ALIGN (new) = 1;
1277                   }
1278                 walk_tree (&DECL_INITIAL (new), force_labels_r, NULL, NULL);
1279
1280                 TREE_OPERAND (*expr_p, 1) = new;
1281                 break;
1282               }
1283           }
1284
1285         /* If there are "lots" of initialized elements, even discounting
1286            those that are not address constants (and thus *must* be 
1287            computed at runtime), then partition the constructor into
1288            constant and non-constant parts.  Block copy the constant
1289            parts in, then generate code for the non-constant parts.  */
1290         /* TODO.  There's code in cp/typeck.c to do this.  */
1291
1292         /* If there are "lots" of zeros, then block clear the object first.  */
1293         cleared = false;
1294         if (num_elements - num_nonzero_elements > CLEAR_RATIO
1295             && num_nonzero_elements < num_elements/4)
1296           cleared = true;
1297
1298         /* ??? This bit ought not be needed.  For any element not present
1299            in the initializer, we should simply set them to zero.  Except
1300            we'd need to *find* the elements that are not present, and that
1301            requires trickery to avoid quadratic compile-time behavior in
1302            large cases or excessive memory use in small cases.  */
1303         else
1304           {
1305             HOST_WIDE_INT len = list_length (elt_list);
1306             if (TREE_CODE (type) == ARRAY_TYPE)
1307               {
1308                 tree nelts = array_type_nelts (type);
1309                 if (!host_integerp (nelts, 1)
1310                     || tree_low_cst (nelts, 1) != len)
1311                   cleared = 1;;
1312               }
1313             else if (len != fields_length (type))
1314               cleared = 1;
1315           }
1316
1317         if (cleared)
1318           {
1319             CONSTRUCTOR_ELTS (ctor) = NULL_TREE;
1320             append_to_statement_list (*expr_p, pre_p);
1321           }
1322
1323         for (i = 0; elt_list; i++, elt_list = TREE_CHAIN (elt_list))
1324           {
1325             tree purpose, value, cref, init;
1326
1327             purpose = TREE_PURPOSE (elt_list);
1328             value = TREE_VALUE (elt_list);
1329
1330             if (cleared && initializer_zerop (value))
1331               continue;
1332
1333             if (TREE_CODE (type) == ARRAY_TYPE)
1334               {
1335                 tree t = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (object)));
1336
1337                 /* ??? Here's to hoping the front end fills in all of the
1338                    indicies, so we don't have to figure out what's missing
1339                    ourselves.  */
1340                 if (!purpose)
1341                   abort ();
1342                 /* ??? Need to handle this.  */
1343                 if (TREE_CODE (purpose) == RANGE_EXPR)
1344                   abort ();
1345
1346                 cref = build (ARRAY_REF, t, object, purpose);
1347               }
1348             else
1349               {
1350                 cref = build (COMPONENT_REF, TREE_TYPE (purpose),
1351                               object, purpose);
1352               }
1353
1354             init = build (MODIFY_EXPR, TREE_TYPE (purpose), cref, value);
1355             /* Each member initialization is a full-expression.  */
1356             gimplify_stmt (&init);
1357             append_to_statement_list (init, pre_p);
1358           }
1359
1360         *expr_p = build_empty_stmt ();
1361       }
1362       break;
1363
1364     case COMPLEX_TYPE:
1365       {
1366         tree r, i;
1367
1368         /* Extract the real and imaginary parts out of the ctor.  */
1369         r = i = NULL_TREE;
1370         if (elt_list)
1371           {
1372             r = TREE_VALUE (elt_list);
1373             elt_list = TREE_CHAIN (elt_list);
1374             if (elt_list)
1375               {
1376                 i = TREE_VALUE (elt_list);
1377                 if (TREE_CHAIN (elt_list))
1378                   abort ();
1379               }
1380           }
1381         if (r == NULL || i == NULL)
1382           {
1383             tree zero = convert (TREE_TYPE (type), integer_zero_node);
1384             if (r == NULL)
1385               r = zero;
1386             if (i == NULL)
1387               i = zero;
1388           }
1389
1390         /* Complex types have either COMPLEX_CST or COMPLEX_EXPR to
1391            represent creation of a complex value.  */
1392         if (TREE_CONSTANT (r) && TREE_CONSTANT (i))
1393           {
1394             ctor = build_complex (type, r, i);
1395             TREE_OPERAND (*expr_p, 1) = ctor;
1396           }
1397         else
1398           {
1399             ctor = build (COMPLEX_EXPR, type, r, i);
1400             TREE_OPERAND (*expr_p, 1) = ctor;
1401             ret = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
1402                                  is_gimple_rhs, fb_rvalue);
1403           }
1404       }
1405       break;
1406
1407     case VECTOR_TYPE:
1408       /* Go ahead and simplify constant constructors to VECTOR_CST.  */
1409       if (TREE_CONSTANT (ctor))
1410         TREE_OPERAND (*expr_p, 1) = build_vector (type, elt_list);
1411       else
1412         {
1413           /* Vector types use CONSTRUCTOR all the way through gimple
1414              compilation as a general initializer.  */
1415           for (; elt_list; elt_list = TREE_CHAIN (elt_list))
1416             {
1417               enum gimplify_status tret;
1418               tret = gimplify_expr (&TREE_VALUE (elt_list), pre_p, post_p,
1419                                     is_gimple_constructor_elt, fb_rvalue);
1420               if (tret == GS_ERROR)
1421                 ret = GS_ERROR;
1422             }
1423         }
1424       break;
1425
1426     default:
1427       /* So how did we get a CONSTRUCTOR for a scalar type?  */
1428       abort ();
1429     }
1430
1431   if (ret == GS_ERROR)
1432     return GS_ERROR;
1433   else if (want_value)
1434     {
1435       append_to_statement_list (*expr_p, pre_p);
1436       *expr_p = object;
1437       return GS_OK;
1438     }
1439   else
1440     return GS_ALL_DONE;
1441 }
1442
1443 /* *EXPR_P is a COMPONENT_REF being used as an rvalue.  If its type is
1444    different from its canonical type, wrap the whole thing inside a
1445    NOP_EXPR and force the type of the COMPONENT_REF to be the canonical
1446    type.
1447
1448    The canonical type of a COMPONENT_REF is the type of the field being
1449    referenced--unless the field is a bit-field which can be read directly
1450    in a smaller mode, in which case the canonical type is the
1451    sign-appropriate type corresponding to that mode.  */
1452
1453 static void
1454 canonicalize_component_ref (tree *expr_p)
1455 {
1456   tree expr = *expr_p;
1457   tree type;
1458
1459   if (TREE_CODE (expr) != COMPONENT_REF)
1460     abort ();
1461
1462   if (INTEGRAL_TYPE_P (TREE_TYPE (expr)))
1463     type = TREE_TYPE (get_unwidened (expr, NULL_TREE));
1464   else
1465     type = TREE_TYPE (TREE_OPERAND (expr, 1));
1466
1467   if (TREE_TYPE (expr) != type)
1468     {
1469       tree old_type = TREE_TYPE (expr);
1470
1471       /* Set the type of the COMPONENT_REF to the underlying type.  */
1472       TREE_TYPE (expr) = type;
1473
1474       /* And wrap the whole thing inside a NOP_EXPR.  */
1475       expr = build1 (NOP_EXPR, old_type, expr);
1476
1477       *expr_p = expr;
1478     }
1479 }
1480
1481 /* If a NOP conversion is changing a pointer to array of foo to a pointer
1482    to foo, embed that change in the ADDR_EXPR.  Lest we perturb the type
1483    system too badly, we must take extra steps to ensure that the ADDR_EXPR
1484    and the addressed object continue to agree on types.  */
1485 /* ??? We might could do better if we recognize
1486         T array[N][M];
1487         (T *)&array
1488    ==>
1489         &array[0][0];
1490 */
1491
1492 static void
1493 canonicalize_addr_expr (tree* expr_p)
1494 {
1495   tree expr = *expr_p;
1496   tree ctype = TREE_TYPE (expr);
1497   tree addr_expr = TREE_OPERAND (expr, 0);
1498   tree atype = TREE_TYPE (addr_expr);
1499   tree dctype, datype, ddatype, otype, obj_expr;
1500
1501   /* Both cast and addr_expr types should be pointers.  */
1502   if (!POINTER_TYPE_P (ctype) || !POINTER_TYPE_P (atype))
1503     return;
1504
1505   /* The addr_expr type should be a pointer to an array.  */
1506   datype = TREE_TYPE (atype);
1507   if (TREE_CODE (datype) != ARRAY_TYPE)
1508     return;
1509
1510   /* Both cast and addr_expr types should address the same object type.  */
1511   dctype = TREE_TYPE (ctype);
1512   ddatype = TREE_TYPE (datype);
1513   if (!lang_hooks.types_compatible_p (ddatype, dctype))
1514     return;
1515
1516   /* The addr_expr and the object type should match.  */
1517   obj_expr = TREE_OPERAND (addr_expr, 0);
1518   otype = TREE_TYPE (obj_expr);
1519   if (!lang_hooks.types_compatible_p (otype, datype))
1520     return;
1521
1522   /* All checks succeeded.  Build a new node to merge the cast.  */
1523   *expr_p = build1 (ADDR_EXPR, ctype, obj_expr);
1524 }
1525
1526 /* *EXPR_P is a NOP_EXPR or CONVERT_EXPR.  Remove it and/or other conversions
1527    underneath as appropriate.  */
1528
1529 static enum gimplify_status
1530 gimplify_conversion (tree *expr_p)
1531 {  
1532   /* Strip away as many useless type conversions as possible
1533      at the toplevel.  */
1534   STRIP_USELESS_TYPE_CONVERSION (*expr_p);
1535
1536   /* If we still have a conversion at the toplevel, then strip
1537      away all but the outermost conversion.  */
1538   if (TREE_CODE (*expr_p) == NOP_EXPR || TREE_CODE (*expr_p) == CONVERT_EXPR)
1539     {
1540       STRIP_SIGN_NOPS (TREE_OPERAND (*expr_p, 0));
1541
1542       /* And remove the outermost conversion if it's useless.  */
1543       if (tree_ssa_useless_type_conversion (*expr_p))
1544         *expr_p = TREE_OPERAND (*expr_p, 0);
1545     }
1546
1547   /* If we still have a conversion at the toplevel,
1548      then canonicalize some constructs.  */
1549   if (TREE_CODE (*expr_p) == NOP_EXPR || TREE_CODE (*expr_p) == CONVERT_EXPR)
1550     {
1551       tree sub = TREE_OPERAND (*expr_p, 0);
1552
1553       /* If a NOP conversion is changing the type of a COMPONENT_REF
1554          expression, then canonicalize its type now in order to expose more
1555          redundant conversions.  */
1556       if (TREE_CODE (sub) == COMPONENT_REF)
1557         canonicalize_component_ref (&TREE_OPERAND (*expr_p, 0));
1558
1559       /* If a NOP conversion is changing a pointer to array of foo
1560          to a pointer to foo, embed that change in the ADDR_EXPR.  */
1561       else if (TREE_CODE (sub) == ADDR_EXPR)
1562         canonicalize_addr_expr (expr_p);
1563     }
1564
1565   return GS_OK;
1566 }
1567
1568 /* Reduce MIN/MAX_EXPR to a COND_EXPR for further gimplification.  */
1569
1570 static enum gimplify_status
1571 gimplify_minimax_expr (tree *expr_p, tree *pre_p, tree *post_p)
1572 {
1573   tree op1 = TREE_OPERAND (*expr_p, 0);
1574   tree op2 = TREE_OPERAND (*expr_p, 1);
1575   enum tree_code code;
1576   enum gimplify_status r0, r1;
1577
1578   if (TREE_CODE (*expr_p) == MIN_EXPR)
1579     code = LE_EXPR;
1580   else
1581     code = GE_EXPR;
1582
1583   r0 = gimplify_expr (&op1, pre_p, post_p, is_gimple_val, fb_rvalue);
1584   r1 = gimplify_expr (&op2, pre_p, post_p, is_gimple_val, fb_rvalue);
1585
1586   *expr_p = build (COND_EXPR, TREE_TYPE (*expr_p),
1587                    build (code, boolean_type_node, op1, op2),
1588                    op1, op2);
1589
1590   if (r0 == GS_ERROR || r1 == GS_ERROR)
1591     return GS_ERROR;
1592   else
1593     return GS_OK;
1594 }
1595
1596 /*  Build an expression for the address of T.  Folds away INDIRECT_REF to
1597     avoid confusing the gimplify process.  */
1598
1599 static tree
1600 build_addr_expr_with_type (tree t, tree ptrtype)
1601 {
1602   if (TREE_CODE (t) == INDIRECT_REF)
1603     {
1604       t = TREE_OPERAND (t, 0);
1605       if (TREE_TYPE (t) != ptrtype)
1606         t = build1 (NOP_EXPR, ptrtype, t);
1607     }
1608   else
1609     {
1610       tree base = t;
1611       while (TREE_CODE (base) == COMPONENT_REF
1612              || TREE_CODE (base) == ARRAY_REF)
1613         base = TREE_OPERAND (base, 0);
1614       if (DECL_P (base))
1615         TREE_ADDRESSABLE (base) = 1;
1616
1617       t = build1 (ADDR_EXPR, ptrtype, t);
1618     }
1619
1620   return t;
1621 }
1622
1623 static tree
1624 build_addr_expr (tree t)
1625 {
1626   return build_addr_expr_with_type (t, build_pointer_type (TREE_TYPE (t)));
1627 }
1628
1629 /* Subroutine of gimplify_compound_lval and gimplify_array_ref.
1630    Converts an ARRAY_REF to the equivalent *(&array + offset) form.  */
1631
1632 static enum gimplify_status
1633 gimplify_array_ref_to_plus (tree *expr_p, tree *pre_p, tree *post_p)
1634 {
1635   tree array = TREE_OPERAND (*expr_p, 0);
1636   tree arrtype = TREE_TYPE (array);
1637   tree elttype = TREE_TYPE (arrtype);
1638   tree size = size_in_bytes (elttype);
1639   tree ptrtype = build_pointer_type (elttype);
1640   enum tree_code add_code = PLUS_EXPR;
1641   tree idx = TREE_OPERAND (*expr_p, 1);
1642   tree minidx, offset, addr, result;
1643   enum gimplify_status ret;
1644
1645   /* If the array domain does not start at zero, apply the offset.  */
1646   minidx = TYPE_DOMAIN (arrtype);
1647   if (minidx)
1648     {
1649       minidx = TYPE_MIN_VALUE (minidx);
1650       if (minidx && !integer_zerop (minidx))
1651         {
1652           idx = convert (TREE_TYPE (minidx), idx);
1653           idx = fold (build (MINUS_EXPR, TREE_TYPE (minidx), idx, minidx));
1654         }
1655     }
1656
1657   /* If the index is negative -- a technically invalid situation now
1658      that we've biased the index back to zero -- then casting it to
1659      unsigned has ill effects.  In particular, -1*4U/4U != -1.
1660      Represent this as a subtraction of a positive rather than addition
1661      of a negative.  This will prevent any conversion back to ARRAY_REF
1662      from getting the wrong results from the division.  */
1663   if (TREE_CODE (idx) == INTEGER_CST && tree_int_cst_sgn (idx) < 0)
1664     {
1665       idx = fold (build1 (NEGATE_EXPR, TREE_TYPE (idx), idx));
1666       add_code = MINUS_EXPR;
1667     }
1668
1669   /* Pointer arithmetic must be done in sizetype.  */
1670   idx = convert (sizetype, idx);
1671
1672   /* Convert the index to a byte offset.  */
1673   offset = size_binop (MULT_EXPR, size, idx);
1674
1675   ret = gimplify_expr (&array, pre_p, post_p, is_gimple_min_lval, fb_lvalue);
1676   if (ret == GS_ERROR)
1677     return ret;
1678
1679   addr = build_addr_expr_with_type (array, ptrtype);
1680   result = fold (build (add_code, ptrtype, addr, offset));
1681   *expr_p = build1 (INDIRECT_REF, elttype, result);
1682
1683   return GS_OK;
1684 }
1685
1686 /* Gimplify the COMPONENT_REF, ARRAY_REF, REALPART_EXPR or IMAGPART_EXPR
1687    node pointed by EXPR_P.
1688
1689       compound_lval
1690               : min_lval '[' val ']'
1691               | min_lval '.' ID
1692               | compound_lval '[' val ']'
1693               | compound_lval '.' ID
1694
1695    This is not part of the original SIMPLE definition, which separates
1696    array and member references, but it seems reasonable to handle them
1697    together.  Also, this way we don't run into problems with union
1698    aliasing; gcc requires that for accesses through a union to alias, the
1699    union reference must be explicit, which was not always the case when we
1700    were splitting up array and member refs.
1701
1702    PRE_P points to the list where side effects that must happen before
1703      *EXPR_P should be stored.
1704
1705    POST_P points to the list where side effects that must happen after
1706      *EXPR_P should be stored.  */
1707
1708 static enum gimplify_status
1709 gimplify_compound_lval (tree *expr_p, tree *pre_p,
1710                         tree *post_p, int want_lvalue)
1711 {
1712   tree *p;
1713   enum tree_code code;
1714   varray_type stack;
1715   enum gimplify_status ret;
1716
1717 #if defined ENABLE_CHECKING
1718   if (TREE_CODE (*expr_p) != ARRAY_REF
1719       && TREE_CODE (*expr_p) != COMPONENT_REF
1720       && TREE_CODE (*expr_p) != REALPART_EXPR
1721       && TREE_CODE (*expr_p) != IMAGPART_EXPR)
1722     abort ();
1723 #endif
1724
1725   code = ERROR_MARK;    /* [GIMPLE] Avoid uninitialized use warning.  */
1726
1727   /* Create a stack of the subexpressions so later we can walk them in
1728      order from inner to outer.  */
1729   VARRAY_TREE_INIT (stack, 10, "stack");
1730
1731   for (p = expr_p;
1732        TREE_CODE (*p) == ARRAY_REF
1733        || TREE_CODE (*p) == COMPONENT_REF
1734        || TREE_CODE (*p) == REALPART_EXPR
1735        || TREE_CODE (*p) == IMAGPART_EXPR;
1736        p = &TREE_OPERAND (*p, 0))
1737     {
1738       code = TREE_CODE (*p);
1739       if (code == ARRAY_REF)
1740         {
1741           tree elttype = TREE_TYPE (TREE_TYPE (TREE_OPERAND (*p, 0)));
1742           if (!TREE_CONSTANT (TYPE_SIZE_UNIT (elttype)))
1743             /* If the size of the array elements is not constant,
1744                computing the offset is non-trivial, so expose it.  */
1745             break;
1746         }
1747       VARRAY_PUSH_TREE (stack, *p);
1748     }
1749
1750   /* Now 'p' points to the first bit that isn't a ref, 'code' is the
1751      TREE_CODE of the last bit that was, and 'stack' is a stack of pointers
1752      to all the refs we've walked through.
1753
1754      Gimplify the base, and then process each of the outer nodes from left
1755      to right.  */
1756   ret = gimplify_expr (p, pre_p, post_p, is_gimple_min_lval,
1757                        code != ARRAY_REF ? fb_either : fb_lvalue);
1758
1759   for (; VARRAY_ACTIVE_SIZE (stack) > 0; )
1760     {
1761       tree t = VARRAY_TOP_TREE (stack);
1762       if (TREE_CODE (t) == ARRAY_REF)
1763         {
1764           /* Gimplify the dimension.  */
1765           enum gimplify_status tret;
1766           /* Temporary fix for gcc.c-torture/execute/20040313-1.c.
1767              Gimplify non-constant array indices into a temporary
1768              variable.
1769              FIXME - The real fix is to gimplify post-modify
1770              expressions into a minimal gimple lvalue.  However, that
1771              exposes bugs in alias analysis.  The alias analyzer does
1772              not handle &PTR->FIELD very well.  Will fix after the
1773              branch is merged into mainline (dnovillo 2004-05-03).  */
1774           if (!is_gimple_min_invariant (TREE_OPERAND (t, 1)))
1775             {
1776               tret = gimplify_expr (&TREE_OPERAND (t, 1), pre_p, post_p,
1777                                     is_gimple_tmp_var, fb_rvalue);
1778               if (tret == GS_ERROR)
1779                 ret = GS_ERROR;
1780             }
1781         }
1782       recalculate_side_effects (t);
1783       VARRAY_POP (stack);
1784     }
1785
1786   /* If the outermost expression is a COMPONENT_REF, canonicalize its type.  */
1787   if (!want_lvalue && TREE_CODE (*expr_p) == COMPONENT_REF)
1788     {
1789       canonicalize_component_ref (expr_p);
1790       ret = MIN (ret, GS_OK);
1791     }
1792
1793   return ret;
1794 }
1795
1796 /*  Re-write the ARRAY_REF node pointed by EXPR_P.
1797
1798     PRE_P points to the list where side effects that must happen before
1799         *EXPR_P should be stored.
1800
1801     POST_P points to the list where side effects that must happen after
1802         *EXPR_P should be stored.
1803
1804     FIXME: ARRAY_REF currently doesn't accept a pointer as the array
1805     argument, so this gimplification uses an INDIRECT_REF of ARRAY_TYPE.
1806     ARRAY_REF should be extended.  */
1807
1808 static enum gimplify_status
1809 gimplify_array_ref (tree *expr_p, tree *pre_p,
1810                     tree *post_p, int want_lvalue)
1811 {
1812   tree elttype = TREE_TYPE (TREE_TYPE (TREE_OPERAND (*expr_p, 0)));
1813   if (!TREE_CONSTANT (TYPE_SIZE_UNIT (elttype)))
1814     /* If the size of the array elements is not constant,
1815        computing the offset is non-trivial, so expose it.  */
1816     return gimplify_array_ref_to_plus (expr_p, pre_p, post_p);
1817   else
1818     /* Handle array and member refs together for now.  When alias analysis
1819        improves, we may want to go back to handling them separately.  */
1820     return gimplify_compound_lval (expr_p, pre_p, post_p, want_lvalue);
1821 }
1822
1823 /*  Gimplify the self modifying expression pointed by EXPR_P (++, --, +=, -=).
1824
1825     PRE_P points to the list where side effects that must happen before
1826         *EXPR_P should be stored.
1827
1828     POST_P points to the list where side effects that must happen after
1829         *EXPR_P should be stored.
1830
1831     WANT_VALUE is nonzero iff we want to use the value of this expression
1832         in another expression.  */
1833
1834 static enum gimplify_status
1835 gimplify_self_mod_expr (tree *expr_p, tree *pre_p, tree *post_p,
1836                         int want_value)
1837 {
1838   enum tree_code code;
1839   tree lhs, lvalue, rhs, t1;
1840   bool postfix;
1841   enum tree_code arith_code;
1842   enum gimplify_status ret;
1843
1844   code = TREE_CODE (*expr_p);
1845
1846 #if defined ENABLE_CHECKING
1847   if (code != POSTINCREMENT_EXPR
1848       && code != POSTDECREMENT_EXPR
1849       && code != PREINCREMENT_EXPR
1850       && code != PREDECREMENT_EXPR)
1851     abort ();
1852 #endif
1853
1854   /* Prefix or postfix?  */
1855   if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
1856     /* Faster to treat as prefix if result is not used.  */
1857     postfix = want_value;
1858   else
1859     postfix = false;
1860
1861   /* Add or subtract?  */
1862   if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
1863     arith_code = PLUS_EXPR;
1864   else
1865     arith_code = MINUS_EXPR;
1866
1867   /* Gimplify the LHS into a GIMPLE lvalue.  */
1868   lvalue = TREE_OPERAND (*expr_p, 0);
1869   ret = gimplify_expr (&lvalue, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
1870   if (ret == GS_ERROR)
1871     return ret;
1872
1873   /* Extract the operands to the arithmetic operation.  */
1874   lhs = lvalue;
1875   rhs = TREE_OPERAND (*expr_p, 1);
1876
1877   /* For postfix operator, we evaluate the LHS to an rvalue and then use
1878      that as the result value and in the postqueue operation.  */
1879   if (postfix)
1880     {
1881       ret = gimplify_expr (&lhs, pre_p, post_p, is_gimple_val, fb_rvalue);
1882       if (ret == GS_ERROR)
1883         return ret;
1884     }
1885
1886   t1 = build (arith_code, TREE_TYPE (*expr_p), lhs, rhs);
1887   t1 = build (MODIFY_EXPR, TREE_TYPE (lvalue), lvalue, t1);
1888
1889   if (postfix)
1890     {
1891       gimplify_stmt (&t1);
1892       append_to_statement_list (t1, post_p);
1893       *expr_p = lhs;
1894       return GS_ALL_DONE;
1895     }
1896   else
1897     {
1898       *expr_p = t1;
1899       return GS_OK;
1900     }
1901 }
1902
1903 /*  Gimplify the CALL_EXPR node pointed by EXPR_P.
1904
1905       call_expr
1906               : ID '(' arglist ')'
1907
1908       arglist
1909               : arglist ',' val
1910               | val
1911
1912     PRE_P points to the list where side effects that must happen before
1913         *EXPR_P should be stored.  */
1914
1915 static enum gimplify_status
1916 gimplify_call_expr (tree *expr_p, tree *pre_p, bool (*gimple_test_f) (tree))
1917 {
1918   tree decl;
1919   tree arglist;
1920   enum gimplify_status ret;
1921
1922 #if defined ENABLE_CHECKING
1923   if (TREE_CODE (*expr_p) != CALL_EXPR)
1924     abort ();
1925 #endif
1926
1927   /* For reliable diagnostics during inlining, it is necessary that 
1928      every call_expr be annotated with file and line.  */
1929   if (!EXPR_LOCUS (*expr_p))
1930     annotate_with_locus (*expr_p, input_location);
1931
1932   /* This may be a call to a builtin function.
1933
1934      Builtin function calls may be transformed into different
1935      (and more efficient) builtin function calls under certain
1936      circumstances.  Unfortunately, gimplification can muck things
1937      up enough that the builtin expanders are not aware that certain
1938      transformations are still valid.
1939
1940      So we attempt transformation/gimplification of the call before
1941      we gimplify the CALL_EXPR.  At this time we do not manage to
1942      transform all calls in the same manner as the expanders do, but
1943      we do transform most of them.  */
1944   decl = get_callee_fndecl (*expr_p);
1945   if (decl && DECL_BUILT_IN (decl))
1946     {
1947       tree new;
1948
1949       /* If it is allocation of stack, record the need to restore the memory
1950          when the enclosing bind_expr is exited.  */
1951       if (DECL_FUNCTION_CODE (decl) == BUILT_IN_STACK_ALLOC)
1952         gimplify_ctxp->save_stack = true;
1953
1954       /* If it is restore of the stack, reset it, since it means we are
1955          regimplifying the bind_expr.  Note that we use the fact that
1956          for try_finally_expr, try part is processed first.  */
1957       if (DECL_FUNCTION_CODE (decl) == BUILT_IN_STACK_RESTORE)
1958         gimplify_ctxp->save_stack = false;
1959
1960       new = simplify_builtin (*expr_p, gimple_test_f == is_gimple_stmt);
1961
1962       if (new && new != *expr_p)
1963         {
1964           /* There was a transformation of this call which computes the
1965              same value, but in a more efficient way.  Return and try
1966              again.  */
1967           *expr_p = new;
1968           return GS_OK;
1969         }
1970     }
1971
1972   /* There is a sequence point before the call, so any side effects in
1973      the calling expression must occur before the actual call.  Force
1974      gimplify_expr to use an internal post queue.  */
1975   ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, NULL,
1976                        is_gimple_val, fb_rvalue);
1977
1978   if (PUSH_ARGS_REVERSED)
1979     TREE_OPERAND (*expr_p, 1) = nreverse (TREE_OPERAND (*expr_p, 1));
1980   for (arglist = TREE_OPERAND (*expr_p, 1); arglist;
1981        arglist = TREE_CHAIN (arglist))
1982     {
1983       enum gimplify_status t;
1984
1985       /* There is a sequence point before a function call.  Side effects in
1986          the argument list must occur before the actual call. So, when
1987          gimplifying arguments, force gimplify_expr to use an internal
1988          post queue which is then appended to the end of PRE_P.  */
1989       t = gimplify_expr (&TREE_VALUE (arglist), pre_p, NULL, is_gimple_val,
1990                          fb_rvalue);
1991
1992       if (t == GS_ERROR)
1993         ret = GS_ERROR;
1994     }
1995   if (PUSH_ARGS_REVERSED)
1996     TREE_OPERAND (*expr_p, 1) = nreverse (TREE_OPERAND (*expr_p, 1));
1997
1998   /* Try this again in case gimplification exposed something.  */
1999   if (ret != GS_ERROR && decl && DECL_BUILT_IN (decl))
2000     {
2001       tree new = simplify_builtin (*expr_p, gimple_test_f == is_gimple_stmt);
2002
2003       if (new && new != *expr_p)
2004         {
2005           /* There was a transformation of this call which computes the
2006              same value, but in a more efficient way.  Return and try
2007              again.  */
2008           *expr_p = new;
2009           return GS_OK;
2010         }
2011     }
2012
2013   /* If the function is "const" or "pure", then clear TREE_SIDE_EFFECTS on its
2014      decl.  This allows us to eliminate redundant or useless
2015      calls to "const" functions.  */
2016   if (TREE_CODE (*expr_p) == CALL_EXPR
2017       && (call_expr_flags (*expr_p) & (ECF_CONST | ECF_PURE)))
2018     TREE_SIDE_EFFECTS (*expr_p) = 0;
2019
2020   return ret;
2021 }
2022
2023 /* Handle shortcut semantics in the predicate operand of a COND_EXPR by
2024    rewriting it into multiple COND_EXPRs, and possibly GOTO_EXPRs.
2025
2026    TRUE_LABEL_P and FALSE_LABEL_P point to the labels to jump to if the
2027    condition is true or false, respectively.  If null, we should generate
2028    our own to skip over the evaluation of this specific expression.
2029
2030    This function is the tree equivalent of do_jump.
2031
2032    shortcut_cond_r should only be called by shortcut_cond_expr.  */
2033
2034 static tree
2035 shortcut_cond_r (tree pred, tree *true_label_p, tree *false_label_p)
2036 {
2037   tree local_label = NULL_TREE;
2038   tree t, expr = NULL;
2039
2040   /* OK, it's not a simple case; we need to pull apart the COND_EXPR to
2041      retain the shortcut semantics.  Just insert the gotos here;
2042      shortcut_cond_expr will append the real blocks later.  */
2043   if (TREE_CODE (pred) == TRUTH_ANDIF_EXPR)
2044     {
2045       /* Turn if (a && b) into
2046
2047          if (a); else goto no;
2048          if (b) goto yes; else goto no;
2049          (no:) */
2050
2051       if (false_label_p == NULL)
2052         false_label_p = &local_label;
2053
2054       t = shortcut_cond_r (TREE_OPERAND (pred, 0), NULL, false_label_p);
2055       append_to_statement_list (t, &expr);
2056
2057       t = shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p,
2058                            false_label_p);
2059       append_to_statement_list (t, &expr);
2060     }
2061   else if (TREE_CODE (pred) == TRUTH_ORIF_EXPR)
2062     {
2063       /* Turn if (a || b) into
2064
2065          if (a) goto yes;
2066          if (b) goto yes; else goto no;
2067          (yes:) */
2068
2069       if (true_label_p == NULL)
2070         true_label_p = &local_label;
2071
2072       t = shortcut_cond_r (TREE_OPERAND (pred, 0), true_label_p, NULL);
2073       append_to_statement_list (t, &expr);
2074
2075       t = shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p,
2076                            false_label_p);
2077       append_to_statement_list (t, &expr);
2078     }
2079   else if (TREE_CODE (pred) == COND_EXPR)
2080     {
2081       /* As long as we're messing with gotos, turn if (a ? b : c) into
2082          if (a)
2083            if (b) goto yes; else goto no;
2084          else
2085            if (c) goto yes; else goto no;  */
2086       expr = build (COND_EXPR, void_type_node, TREE_OPERAND (pred, 0),
2087                     shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p,
2088                                      false_label_p),
2089                     shortcut_cond_r (TREE_OPERAND (pred, 2), true_label_p,
2090                                      false_label_p));
2091     }
2092   else
2093     {
2094       expr = build (COND_EXPR, void_type_node, pred,
2095                     build_and_jump (true_label_p),
2096                     build_and_jump (false_label_p));
2097     }
2098
2099   if (local_label)
2100     {
2101       t = build1 (LABEL_EXPR, void_type_node, local_label);
2102       append_to_statement_list (t, &expr);
2103     }
2104
2105   return expr;
2106 }
2107
2108 static tree
2109 shortcut_cond_expr (tree expr)
2110 {
2111   tree pred = TREE_OPERAND (expr, 0);
2112   tree then_ = TREE_OPERAND (expr, 1);
2113   tree else_ = TREE_OPERAND (expr, 2);
2114   tree true_label, false_label, end_label, t;
2115   tree *true_label_p;
2116   tree *false_label_p;
2117   bool emit_end, emit_false;
2118
2119   /* First do simple transformations.  */
2120   if (!TREE_SIDE_EFFECTS (else_))
2121     {
2122       /* If there is no 'else', turn (a && b) into if (a) if (b).  */
2123       while (TREE_CODE (pred) == TRUTH_ANDIF_EXPR)
2124         {
2125           TREE_OPERAND (expr, 0) = TREE_OPERAND (pred, 1);
2126           then_ = shortcut_cond_expr (expr);
2127           pred = TREE_OPERAND (pred, 0);
2128           expr = build (COND_EXPR, void_type_node, pred, then_,
2129                         build_empty_stmt ());
2130         }
2131     }
2132   if (!TREE_SIDE_EFFECTS (then_))
2133     {
2134       /* If there is no 'then', turn
2135            if (a || b); else d
2136          into
2137            if (a); else if (b); else d.  */
2138       while (TREE_CODE (pred) == TRUTH_ORIF_EXPR)
2139         {
2140           TREE_OPERAND (expr, 0) = TREE_OPERAND (pred, 1);
2141           else_ = shortcut_cond_expr (expr);
2142           pred = TREE_OPERAND (pred, 0);
2143           expr = build (COND_EXPR, void_type_node, pred,
2144                         build_empty_stmt (), else_);
2145         }
2146     }
2147
2148   /* If we're done, great.  */
2149   if (TREE_CODE (pred) != TRUTH_ANDIF_EXPR
2150       && TREE_CODE (pred) != TRUTH_ORIF_EXPR)
2151     return expr;
2152
2153   /* Otherwise we need to mess with gotos.  Change
2154        if (a) c; else d;
2155      to
2156        if (a); else goto no;
2157        c; goto end;
2158        no: d; end:
2159      and recursively gimplify the condition.  */
2160
2161   true_label = false_label = end_label = NULL_TREE;
2162
2163   /* If our arms just jump somewhere, hijack those labels so we don't
2164      generate jumps to jumps.  */
2165
2166   if (TREE_CODE (then_) == GOTO_EXPR
2167       && TREE_CODE (GOTO_DESTINATION (then_)) == LABEL_DECL)
2168     {
2169       true_label = GOTO_DESTINATION (then_);
2170       then_ = build_empty_stmt ();
2171     }
2172
2173   if (TREE_CODE (else_) == GOTO_EXPR
2174       && TREE_CODE (GOTO_DESTINATION (else_)) == LABEL_DECL)
2175     {
2176       false_label = GOTO_DESTINATION (else_);
2177       else_ = build_empty_stmt ();
2178     }
2179
2180   /* If we aren't hijacking a label for the 'then' branch, it falls through.  */
2181   if (true_label)
2182     true_label_p = &true_label;
2183   else
2184     true_label_p = NULL;
2185
2186   /* The 'else' branch also needs a label if it contains interesting code.  */
2187   if (false_label || TREE_SIDE_EFFECTS (else_))
2188     false_label_p = &false_label;
2189   else
2190     false_label_p = NULL;
2191
2192   /* If there was nothing else in our arms, just forward the label(s).  */
2193   if (!TREE_SIDE_EFFECTS (then_) && !TREE_SIDE_EFFECTS (else_))
2194     return shortcut_cond_r (pred, true_label_p, false_label_p);
2195
2196   /* If our last subexpression already has a terminal label, reuse it.  */
2197   if (TREE_SIDE_EFFECTS (else_))
2198     expr = expr_last (else_);
2199   else
2200     expr = expr_last (then_);
2201   if (TREE_CODE (expr) == LABEL_EXPR)
2202     end_label = LABEL_EXPR_LABEL (expr);
2203
2204   /* If we don't care about jumping to the 'else' branch, jump to the end
2205      if the condition is false.  */
2206   if (!false_label_p)
2207     false_label_p = &end_label;
2208
2209   /* We only want to emit these labels if we aren't hijacking them.  */
2210   emit_end = (end_label == NULL_TREE);
2211   emit_false = (false_label == NULL_TREE);
2212
2213   pred = shortcut_cond_r (pred, true_label_p, false_label_p);
2214
2215   expr = NULL;
2216   append_to_statement_list (pred, &expr);
2217
2218   append_to_statement_list (then_, &expr);
2219   if (TREE_SIDE_EFFECTS (else_))
2220     {
2221       t = build_and_jump (&end_label);
2222       append_to_statement_list (t, &expr);
2223       if (emit_false)
2224         {
2225           t = build1 (LABEL_EXPR, void_type_node, false_label);
2226           append_to_statement_list (t, &expr);
2227         }
2228       append_to_statement_list (else_, &expr);
2229     }
2230   if (emit_end && end_label)
2231     {
2232       t = build1 (LABEL_EXPR, void_type_node, end_label);
2233       append_to_statement_list (t, &expr);
2234     }
2235
2236   return expr;
2237 }
2238
2239 /* EXPR is used in a boolean context; make sure it has BOOLEAN_TYPE.  */
2240
2241 static tree
2242 gimple_boolify (tree expr)
2243 {
2244   tree type = TREE_TYPE (expr);
2245
2246   if (TREE_CODE (type) == BOOLEAN_TYPE)
2247     return expr;
2248
2249   /* If this is the predicate of a COND_EXPR, it might not even be a
2250      truthvalue yet.  */
2251   expr = lang_hooks.truthvalue_conversion (expr);
2252
2253   switch (TREE_CODE (expr))
2254     {
2255     case TRUTH_AND_EXPR:
2256     case TRUTH_OR_EXPR:
2257     case TRUTH_XOR_EXPR:
2258     case TRUTH_ANDIF_EXPR:
2259     case TRUTH_ORIF_EXPR:
2260       /* Also boolify the arguments of truth exprs.  */
2261       TREE_OPERAND (expr, 1) = gimple_boolify (TREE_OPERAND (expr, 1));
2262       /* FALLTHRU */
2263
2264     case TRUTH_NOT_EXPR:
2265       TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
2266       /* FALLTHRU */
2267
2268     case EQ_EXPR: case NE_EXPR:
2269     case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
2270       /* These expressions always produce boolean results.  */
2271       TREE_TYPE (expr) = boolean_type_node;
2272       return expr;
2273       
2274     default:
2275       /* Other expressions that get here must have boolean values, but
2276          might need to be converted to the appropriate mode.  */
2277       return convert (boolean_type_node, expr);
2278     }
2279 }
2280
2281 /*  Convert the conditional expression pointed by EXPR_P '(p) ? a : b;'
2282     into
2283
2284     if (p)                      if (p)
2285       t1 = a;                     a;
2286     else                or      else
2287       t1 = b;                     b;
2288     t1;
2289
2290     The second form is used when *EXPR_P is of type void.
2291
2292     PRE_P points to the list where side effects that must happen before
2293         *EXPR_P should be stored.  */
2294
2295 static enum gimplify_status
2296 gimplify_cond_expr (tree *expr_p, tree *pre_p, tree target)
2297 {
2298   tree expr = *expr_p;
2299   tree tmp;
2300   enum gimplify_status ret;
2301
2302   /* If this COND_EXPR has a value, copy the values into a temporary within
2303      the arms.  */
2304   if (! VOID_TYPE_P (TREE_TYPE (expr)))
2305     {
2306       if (target)
2307         {
2308           tmp = target;
2309           ret = GS_OK;
2310         }
2311       else
2312         {
2313           tmp = create_tmp_var (TREE_TYPE (expr), "iftmp");
2314           ret = GS_ALL_DONE;
2315         }
2316
2317       /* Build the then clause, 't1 = a;'.  But don't build an assignment
2318          if this branch is void; in C++ it can be, if it's a throw.  */
2319       if (TREE_TYPE (TREE_OPERAND (expr, 1)) != void_type_node)
2320         TREE_OPERAND (expr, 1)
2321           = build (MODIFY_EXPR, void_type_node, tmp, TREE_OPERAND (expr, 1));
2322
2323       /* Build the else clause, 't1 = b;'.  */
2324       if (TREE_TYPE (TREE_OPERAND (expr, 2)) != void_type_node)
2325         TREE_OPERAND (expr, 2)
2326           = build (MODIFY_EXPR, void_type_node, tmp, TREE_OPERAND (expr, 2));
2327
2328       TREE_TYPE (expr) = void_type_node;
2329       recalculate_side_effects (expr);
2330
2331       /* Move the COND_EXPR to the prequeue and use the temp in its place.  */
2332       gimplify_stmt (&expr);
2333       append_to_statement_list (expr, pre_p);
2334       *expr_p = tmp;
2335
2336       return ret;
2337     }
2338
2339   /* Make sure the condition has BOOLEAN_TYPE.  */
2340   TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
2341
2342   /* Break apart && and || conditions.  */
2343   if (TREE_CODE (TREE_OPERAND (expr, 0)) == TRUTH_ANDIF_EXPR
2344       || TREE_CODE (TREE_OPERAND (expr, 0)) == TRUTH_ORIF_EXPR)
2345     {
2346       expr = shortcut_cond_expr (expr);
2347
2348       if (expr != *expr_p)
2349         {
2350           *expr_p = expr;
2351
2352           /* We can't rely on gimplify_expr to re-gimplify the expanded
2353              form properly, as cleanups might cause the target labels to be
2354              wrapped in a TRY_FINALLY_EXPR.  To prevent that, we need to
2355              set up a conditional context.  */
2356           gimple_push_condition ();
2357           gimplify_stmt (expr_p);
2358           gimple_pop_condition (pre_p);
2359
2360           return GS_ALL_DONE;
2361         }
2362     }
2363
2364   /* Now do the normal gimplification.  */
2365   ret = gimplify_expr (&TREE_OPERAND (expr, 0), pre_p, NULL,
2366                        is_gimple_condexpr, fb_rvalue);
2367
2368   gimple_push_condition ();
2369
2370   gimplify_to_stmt_list (&TREE_OPERAND (expr, 1));
2371   gimplify_to_stmt_list (&TREE_OPERAND (expr, 2));
2372   recalculate_side_effects (expr);
2373
2374   gimple_pop_condition (pre_p);
2375
2376   if (ret == GS_ERROR)
2377     ;
2378   else if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)))
2379     ret = GS_ALL_DONE;
2380   else if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 2)))
2381     /* Rewrite "if (a); else b" to "if (!a) b"  */
2382     {
2383       TREE_OPERAND (expr, 0) = invert_truthvalue (TREE_OPERAND (expr, 0));
2384       ret = gimplify_expr (&TREE_OPERAND (expr, 0), pre_p, NULL,
2385                            is_gimple_condexpr, fb_rvalue);
2386
2387       tmp = TREE_OPERAND (expr, 1);
2388       TREE_OPERAND (expr, 1) = TREE_OPERAND (expr, 2);
2389       TREE_OPERAND (expr, 2) = tmp;
2390     }
2391   else
2392     /* Both arms are empty; replace the COND_EXPR with its predicate.  */
2393     expr = TREE_OPERAND (expr, 0);
2394
2395   *expr_p = expr;
2396   return ret;
2397 }
2398
2399 /*  Gimplify the MODIFY_EXPR node pointed by EXPR_P.
2400
2401       modify_expr
2402               : varname '=' rhs
2403               | '*' ID '=' rhs
2404
2405     PRE_P points to the list where side effects that must happen before
2406         *EXPR_P should be stored.
2407
2408     POST_P points to the list where side effects that must happen after
2409         *EXPR_P should be stored.
2410
2411     WANT_VALUE is nonzero iff we want to use the value of this expression
2412         in another expression.  */
2413
2414 static enum gimplify_status
2415 gimplify_modify_expr (tree *expr_p, tree *pre_p, tree *post_p, bool want_value)
2416 {
2417   tree *from_p = &TREE_OPERAND (*expr_p, 1);
2418   tree *to_p = &TREE_OPERAND (*expr_p, 0);
2419   enum gimplify_status ret;
2420
2421 #if defined ENABLE_CHECKING
2422   if (TREE_CODE (*expr_p) != MODIFY_EXPR && TREE_CODE (*expr_p) != INIT_EXPR)
2423     abort ();
2424 #endif
2425
2426   /* The distinction between MODIFY_EXPR and INIT_EXPR is no longer useful.  */
2427   if (TREE_CODE (*expr_p) == INIT_EXPR)
2428     TREE_SET_CODE (*expr_p, MODIFY_EXPR);
2429
2430   ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
2431   if (ret == GS_ERROR)
2432     return ret;
2433
2434   /* If we are initializing something from a TARGET_EXPR, strip the
2435      TARGET_EXPR and initialize it directly.  */
2436   /* What about code that pulls out the temp and uses it elsewhere?  I
2437      think that such code never uses the TARGET_EXPR as an initializer.  If
2438      I'm wrong, we'll abort because the temp won't have any RTL.  In that
2439      case, I guess we'll need to replace references somehow.  */
2440   if (TREE_CODE (*from_p) == TARGET_EXPR)
2441     *from_p = TARGET_EXPR_INITIAL (*from_p);
2442
2443   /* If we're assigning from a ?: expression with ADDRESSABLE type, push
2444      the assignment down into the branches, since we can't generate a
2445      temporary of such a type.  */
2446   if (TREE_CODE (*from_p) == COND_EXPR
2447       && TREE_ADDRESSABLE (TREE_TYPE (*from_p)))
2448     {
2449       *expr_p = *from_p;
2450       return gimplify_cond_expr (expr_p, pre_p, *to_p);
2451     }
2452
2453   ret = gimplify_expr (from_p, pre_p, post_p, is_gimple_rhs, fb_rvalue);
2454   if (ret == GS_ERROR)
2455     return ret;
2456
2457   ret = gimplify_init_constructor (expr_p, pre_p, post_p, want_value);
2458   if (ret != GS_UNHANDLED)
2459     return ret;
2460
2461   /* If the destination is already simple, nothing else needed.  */
2462   if (is_gimple_tmp_var (*to_p))
2463     ret = GS_ALL_DONE;
2464   else
2465     {
2466       /* If the RHS of the MODIFY_EXPR may throw or make a nonlocal goto and
2467          the LHS is a user variable, then we need to introduce a temporary.
2468          ie temp = RHS; LHS = temp.
2469
2470          This way the optimizers can determine that the user variable is
2471          only modified if evaluation of the RHS does not throw.
2472
2473          FIXME this should be handled by the is_gimple_rhs predicate.  */
2474
2475       if (TREE_CODE (*from_p) == CALL_EXPR
2476           || (flag_non_call_exceptions && tree_could_trap_p (*from_p))
2477           /* If we're dealing with a renamable type, either source or dest
2478              must be a renamed variable.  */
2479           || (is_gimple_reg_type (TREE_TYPE (*from_p))
2480               && !is_gimple_reg (*to_p)))
2481         gimplify_expr (from_p, pre_p, post_p, is_gimple_val, fb_rvalue);
2482
2483       /* If the value being copied is of variable width, expose the length
2484          if the copy by converting the whole thing to a memcpy.  */
2485       /* ??? Except that we can't manage this with VA_ARG_EXPR.  Yes, this
2486          does leave us with an edge condition that doesn't work.  The only
2487          way out is to rearrange how VA_ARG_EXPR works.  */
2488       if (TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (*to_p))) != INTEGER_CST
2489           && TREE_CODE (*from_p) != VA_ARG_EXPR)
2490         {
2491           tree args, t, dest;
2492
2493           t = TYPE_SIZE_UNIT (TREE_TYPE (*to_p));
2494           t = unshare_expr (t);
2495           args = tree_cons (NULL, t, NULL);
2496           t = build_addr_expr (*from_p);
2497           args = tree_cons (NULL, t, args);
2498           dest = build_addr_expr (*to_p);
2499           args = tree_cons (NULL, dest, args);
2500           t = implicit_built_in_decls[BUILT_IN_MEMCPY];
2501           t = build_function_call_expr (t, args);
2502           if (want_value)
2503             {
2504               t = build1 (NOP_EXPR, TREE_TYPE (dest), t);
2505               t = build1 (INDIRECT_REF, TREE_TYPE (*to_p), t);
2506             }
2507           *expr_p = t;
2508
2509           return GS_OK;
2510         }
2511
2512       ret = want_value ? GS_OK : GS_ALL_DONE;
2513     }
2514
2515   if (want_value)
2516     {
2517       append_to_statement_list (*expr_p, pre_p);
2518       *expr_p = *to_p;
2519     }
2520
2521   return ret;
2522 }
2523
2524 /*  Gimplify TRUTH_ANDIF_EXPR and TRUTH_ORIF_EXPR expressions.  EXPR_P
2525     points to the expression to gimplify.
2526
2527     Expressions of the form 'a && b' are gimplified to:
2528
2529         a && b ? true : false
2530
2531     gimplify_cond_expr will do the rest.
2532
2533     PRE_P points to the list where side effects that must happen before
2534         *EXPR_P should be stored.  */
2535
2536 static enum gimplify_status
2537 gimplify_boolean_expr (tree *expr_p)
2538 {
2539   /* Preserve the original type of the expression.  */
2540   tree type = TREE_TYPE (*expr_p);
2541
2542   *expr_p = build (COND_EXPR, type, *expr_p,
2543                    convert (type, boolean_true_node),
2544                    convert (type, boolean_false_node));
2545
2546   return GS_OK;
2547 }
2548
2549 /* Gimplifies an expression sequence.  This function gimplifies each
2550    expression and re-writes the original expression with the last
2551    expression of the sequence in GIMPLE form.
2552
2553    PRE_P points to the list where the side effects for all the
2554        expressions in the sequence will be emitted.
2555     
2556    WANT_VALUE is true when the result of the last COMPOUND_EXPR is used.  */
2557 /* ??? Should rearrange to share the pre-queue with all the indirect
2558    invocations of gimplify_expr.  Would probably save on creations 
2559    of statement_list nodes.  */
2560
2561 static enum gimplify_status
2562 gimplify_compound_expr (tree *expr_p, tree *pre_p, bool want_value)
2563 {
2564   tree t = *expr_p;
2565
2566   do
2567     {
2568       tree *sub_p = &TREE_OPERAND (t, 0);
2569
2570       if (TREE_CODE (*sub_p) == COMPOUND_EXPR)
2571         gimplify_compound_expr (sub_p, pre_p, false);
2572       else
2573         gimplify_stmt (sub_p);
2574       append_to_statement_list (*sub_p, pre_p);
2575
2576       t = TREE_OPERAND (t, 1);
2577     }
2578   while (TREE_CODE (t) == COMPOUND_EXPR);
2579
2580   *expr_p = t;
2581   if (want_value)
2582     return GS_OK;
2583   else
2584     {
2585       gimplify_stmt (expr_p);
2586       return GS_ALL_DONE;
2587     }
2588 }
2589
2590 /* Gimplifies a statement list.  These may be created either by an
2591    enlightened front-end, or by shortcut_cond_expr.  */
2592
2593 static enum gimplify_status
2594 gimplify_statement_list (tree *expr_p)
2595 {
2596   tree_stmt_iterator i = tsi_start (*expr_p);
2597
2598   while (!tsi_end_p (i))
2599     {
2600       tree t;
2601
2602       gimplify_stmt (tsi_stmt_ptr (i));
2603
2604       t = tsi_stmt (i);
2605       if (TREE_CODE (t) == STATEMENT_LIST)
2606         {
2607           tsi_link_before (&i, t, TSI_SAME_STMT);
2608           tsi_delink (&i);
2609         }
2610       else
2611         tsi_next (&i);
2612     }
2613
2614   return GS_ALL_DONE;
2615 }
2616
2617 /*  Gimplify a SAVE_EXPR node.  EXPR_P points to the expression to
2618     gimplify.  After gimplification, EXPR_P will point to a new temporary
2619     that holds the original value of the SAVE_EXPR node.
2620
2621     PRE_P points to the list where side effects that must happen before
2622         *EXPR_P should be stored.  */
2623
2624 static enum gimplify_status
2625 gimplify_save_expr (tree *expr_p, tree *pre_p, tree *post_p)
2626 {
2627   enum gimplify_status ret = GS_ALL_DONE;
2628   tree val;
2629
2630 #if defined ENABLE_CHECKING
2631   if (TREE_CODE (*expr_p) != SAVE_EXPR)
2632     abort ();
2633 #endif
2634
2635   val = TREE_OPERAND (*expr_p, 0);
2636
2637   /* If the operand is already a GIMPLE temporary, just re-write the
2638      SAVE_EXPR node.  */
2639   if (is_gimple_tmp_var (val))
2640     *expr_p = val;
2641   /* The operand may be a void-valued expression such as SAVE_EXPRs
2642      generated by the Java frontend for class initialization.  It is
2643      being executed only for its side-effects.  */
2644   else if (TREE_TYPE (val) == void_type_node)
2645     {
2646       tree body = TREE_OPERAND (*expr_p, 0);
2647       ret = gimplify_expr (& body, pre_p, post_p, is_gimple_stmt, fb_none);
2648       append_to_statement_list (body, pre_p);
2649       *expr_p = build_empty_stmt ();
2650     }
2651   else
2652     *expr_p = TREE_OPERAND (*expr_p, 0)
2653       = get_initialized_tmp_var (val, pre_p, post_p);
2654
2655   return ret;
2656 }
2657
2658 /*  Re-write the ADDR_EXPR node pointed by EXPR_P
2659
2660       unary_expr
2661               : ...
2662               | '&' varname
2663               ...
2664
2665     PRE_P points to the list where side effects that must happen before
2666         *EXPR_P should be stored.
2667
2668     POST_P points to the list where side effects that must happen after
2669         *EXPR_P should be stored.  */
2670
2671 static enum gimplify_status
2672 gimplify_addr_expr (tree *expr_p, tree *pre_p, tree *post_p)
2673 {
2674   tree expr = *expr_p;
2675   tree op0 = TREE_OPERAND (expr, 0);
2676   enum gimplify_status ret;
2677
2678   switch (TREE_CODE (op0))
2679     {
2680     case INDIRECT_REF:
2681       /* Check if we are dealing with an expression of the form '&*ptr'.
2682          While the front end folds away '&*ptr' into 'ptr', these
2683          expressions may be generated internally by the compiler (e.g.,
2684          builtins like __builtin_va_end).  */
2685       *expr_p = TREE_OPERAND (op0, 0);
2686       ret = GS_OK;
2687       break;
2688
2689     case ARRAY_REF:
2690       /* Fold &a[6] to (&a + 6).  */
2691       ret = gimplify_array_ref_to_plus (&TREE_OPERAND (expr, 0),
2692                                         pre_p, post_p);
2693
2694       /* This added an INDIRECT_REF.  Fold it away.  */
2695       op0 = TREE_OPERAND (TREE_OPERAND (expr, 0), 0);
2696
2697       *expr_p = op0;
2698       break;
2699
2700     default:
2701       /* We use fb_either here because the C frontend sometimes takes
2702          the address of a call that returns a struct.  */
2703       ret = gimplify_expr (&TREE_OPERAND (expr, 0), pre_p, post_p,
2704                            is_gimple_addr_expr_arg, fb_either);
2705       if (ret != GS_ERROR)
2706         {
2707           /* At this point, the argument of the ADDR_EXPR should be
2708              sufficiently simple that there are never side effects.  */
2709           /* ??? Could split out the decision code from build1 to verify.  */
2710           TREE_SIDE_EFFECTS (expr) = 0;
2711
2712           /* Make sure TREE_INVARIANT/TREE_CONSTANT is set properly.  */
2713           recompute_tree_invarant_for_addr_expr (expr);
2714
2715           /* Mark the RHS addressable.  */
2716           lang_hooks.mark_addressable (TREE_OPERAND (expr, 0));
2717         }
2718       break;
2719     }
2720
2721   /* If the operand is gimplified into a _DECL, mark the address expression
2722      as TREE_INVARIANT.  */
2723   if (DECL_P (TREE_OPERAND (expr, 0)))
2724     TREE_INVARIANT (expr) = 1;
2725
2726   return ret;
2727 }
2728
2729 /* Gimplify the operands of an ASM_EXPR.  Input operands should be a gimple
2730    value; output operands should be a gimple lvalue.  */
2731
2732 static enum gimplify_status
2733 gimplify_asm_expr (tree *expr_p, tree *pre_p, tree *post_p)
2734 {
2735   tree expr = *expr_p;
2736   int noutputs = list_length (ASM_OUTPUTS (expr));
2737   const char **oconstraints
2738     = (const char **) alloca ((noutputs) * sizeof (const char *));
2739   int i;
2740   tree link;
2741   const char *constraint;
2742   bool allows_mem, allows_reg, is_inout;
2743   enum gimplify_status ret, tret;
2744
2745   ASM_STRING (expr)
2746     = resolve_asm_operand_names (ASM_STRING (expr), ASM_OUTPUTS (expr),
2747                                  ASM_INPUTS (expr));
2748
2749   ret = GS_ALL_DONE;
2750   for (i = 0, link = ASM_OUTPUTS (expr); link; ++i, link = TREE_CHAIN (link))
2751     {
2752       oconstraints[i] = constraint
2753         = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
2754
2755       parse_output_constraint (&constraint, i, 0, 0,
2756                                &allows_mem, &allows_reg, &is_inout);
2757
2758       if (!allows_reg && allows_mem)
2759         lang_hooks.mark_addressable (TREE_VALUE (link));
2760
2761       tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
2762                             is_inout ? is_gimple_min_lval : is_gimple_lvalue,
2763                             fb_lvalue | fb_mayfail);
2764       if (tret == GS_ERROR)
2765         {
2766           error ("invalid lvalue in asm output %d", i);
2767           ret = tret;
2768         }
2769
2770       if (is_inout)
2771         {
2772           /* An input/output operand.  To give the optimizers more
2773              flexibility, split it into separate input and output
2774              operands.  */
2775           tree input;
2776           char buf[10];
2777           size_t constraint_len = strlen (constraint);
2778
2779           /* Turn the in/out constraint into an output constraint.  */
2780           char *p = xstrdup (constraint);
2781           p[0] = '=';
2782           TREE_VALUE (TREE_PURPOSE (link)) = build_string (constraint_len, p);
2783           free (p);
2784
2785           /* And add a matching input constraint.  */
2786           if (allows_reg)
2787             {
2788               sprintf (buf, "%d", i);
2789               input = build_string (strlen (buf), buf);
2790             }
2791           else
2792             input = build_string (constraint_len - 1, constraint + 1);
2793           input = build_tree_list (build_tree_list (NULL_TREE, input),
2794                                    unshare_expr (TREE_VALUE (link)));
2795           ASM_INPUTS (expr) = chainon (ASM_INPUTS (expr), input);
2796         }
2797     }
2798
2799   for (link = ASM_INPUTS (expr); link; ++i, link = TREE_CHAIN (link))
2800     {
2801       constraint
2802         = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
2803       parse_input_constraint (&constraint, 0, 0, noutputs, 0,
2804                               oconstraints, &allows_mem, &allows_reg);
2805
2806       /* If the operand is a memory input, it should be an lvalue.  */
2807       if (!allows_reg && allows_mem)
2808         {
2809           lang_hooks.mark_addressable (TREE_VALUE (link));
2810           tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
2811                                 is_gimple_lvalue, fb_lvalue | fb_mayfail);
2812           if (tret == GS_ERROR)
2813             {
2814               error ("memory input %d is not directly addressable", i);
2815               ret = tret;
2816             }
2817         }
2818       else
2819         {
2820           tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
2821                                 is_gimple_val, fb_rvalue);
2822           if (tret == GS_ERROR)
2823             ret = tret;
2824         }
2825     }
2826
2827   return ret;
2828 }
2829
2830 /* Gimplify a CLEANUP_POINT_EXPR.  Currently this works by adding
2831    WITH_CLEANUP_EXPRs to the prequeue as we encounter cleanups while
2832    gimplifying the body, and converting them to TRY_FINALLY_EXPRs when we
2833    return to this function.
2834
2835    FIXME should we complexify the prequeue handling instead?  Or use flags
2836    for all the cleanups and let the optimizer tighten them up?  The current
2837    code seems pretty fragile; it will break on a cleanup within any
2838    non-conditional nesting.  But any such nesting would be broken, anyway;
2839    we can't write a TRY_FINALLY_EXPR that starts inside a nesting construct
2840    and continues out of it.  We can do that at the RTL level, though, so
2841    having an optimizer to tighten up try/finally regions would be a Good
2842    Thing.  */
2843
2844 static enum gimplify_status
2845 gimplify_cleanup_point_expr (tree *expr_p, tree *pre_p)
2846 {
2847   tree_stmt_iterator iter;
2848   tree body;
2849
2850   tree temp = voidify_wrapper_expr (*expr_p);
2851
2852   /* We only care about the number of conditions between the innermost
2853      CLEANUP_POINT_EXPR and the cleanup.  So save and reset the count.  */
2854   int old_conds = gimplify_ctxp->conditions;
2855   gimplify_ctxp->conditions = 0;
2856
2857   body = TREE_OPERAND (*expr_p, 0);
2858   gimplify_to_stmt_list (&body);
2859
2860   gimplify_ctxp->conditions = old_conds;
2861
2862   for (iter = tsi_start (body); !tsi_end_p (iter); )
2863     {
2864       tree *wce_p = tsi_stmt_ptr (iter);
2865       tree wce = *wce_p;
2866
2867       if (TREE_CODE (wce) == WITH_CLEANUP_EXPR)
2868         {
2869           if (tsi_one_before_end_p (iter))
2870             {
2871               tsi_link_before (&iter, TREE_OPERAND (wce, 1), TSI_SAME_STMT);
2872               tsi_delink (&iter);
2873               break;
2874             }
2875           else
2876             {
2877               tree sl, tfe;
2878
2879               sl = tsi_split_statement_list_after (&iter);
2880               tfe = build (TRY_FINALLY_EXPR, void_type_node, sl, NULL_TREE);
2881               append_to_statement_list (TREE_OPERAND (wce, 1),
2882                                      &TREE_OPERAND (tfe, 1));
2883               *wce_p = tfe;
2884               iter = tsi_start (sl);
2885             }
2886         }
2887       else
2888         tsi_next (&iter);
2889     }
2890
2891   if (temp)
2892     {
2893       *expr_p = temp;
2894       append_to_statement_list (body, pre_p);
2895       return GS_OK;
2896     }
2897   else
2898     {
2899       *expr_p = body;
2900       return GS_ALL_DONE;
2901     }
2902 }
2903
2904 /* Insert a cleanup marker for gimplify_cleanup_point_expr.  CLEANUP
2905    is the cleanup action required.  */
2906
2907 static void
2908 gimple_push_cleanup (tree var, tree cleanup, tree *pre_p)
2909 {
2910   tree wce;
2911
2912   /* Errors can result in improperly nested cleanups.  Which results in
2913      confusion when trying to resolve the WITH_CLEANUP_EXPR.  */
2914   if (errorcount || sorrycount)
2915     return;
2916
2917   if (gimple_conditional_context ())
2918     {
2919       /* If we're in a conditional context, this is more complex.  We only
2920          want to run the cleanup if we actually ran the initialization that
2921          necessitates it, but we want to run it after the end of the
2922          conditional context.  So we wrap the try/finally around the
2923          condition and use a flag to determine whether or not to actually
2924          run the destructor.  Thus
2925
2926            test ? f(A()) : 0
2927
2928          becomes (approximately)
2929
2930            flag = 0;
2931            try {
2932              if (test) { A::A(temp); flag = 1; val = f(temp); }
2933              else { val = 0; }
2934            } finally {
2935              if (flag) A::~A(temp);
2936            }
2937            val
2938       */
2939
2940       tree flag = create_tmp_var (boolean_type_node, "cleanup");
2941       tree ffalse = build (MODIFY_EXPR, void_type_node, flag,
2942                            boolean_false_node);
2943       tree ftrue = build (MODIFY_EXPR, void_type_node, flag,
2944                           boolean_true_node);
2945       cleanup = build (COND_EXPR, void_type_node, flag, cleanup,
2946                        build_empty_stmt ());
2947       wce = build (WITH_CLEANUP_EXPR, void_type_node, NULL_TREE,
2948                    cleanup, NULL_TREE);
2949       append_to_statement_list (ffalse, &gimplify_ctxp->conditional_cleanups);
2950       append_to_statement_list (wce, &gimplify_ctxp->conditional_cleanups);
2951       append_to_statement_list (ftrue, pre_p);
2952
2953       /* Because of this manipulation, and the EH edges that jump
2954          threading cannot redirect, the temporary (VAR) will appear
2955          to be used uninitialized.  Don't warn.  */
2956       TREE_NO_WARNING (var) = 1;
2957     }
2958   else
2959     {
2960       wce = build (WITH_CLEANUP_EXPR, void_type_node, NULL_TREE,
2961                    cleanup, NULL_TREE);
2962       append_to_statement_list (wce, pre_p);
2963     }
2964
2965   gimplify_stmt (&TREE_OPERAND (wce, 1));
2966 }
2967
2968 /* Gimplify a TARGET_EXPR which doesn't appear on the rhs of an INIT_EXPR.  */
2969
2970 static enum gimplify_status
2971 gimplify_target_expr (tree *expr_p, tree *pre_p, tree *post_p)
2972 {
2973   tree targ = *expr_p;
2974   tree temp = TARGET_EXPR_SLOT (targ);
2975   tree init = TARGET_EXPR_INITIAL (targ);
2976   enum gimplify_status ret;
2977
2978   if (init)
2979     {
2980       /* TARGET_EXPR temps aren't part of the enclosing block, so add it to the
2981          temps list.  */
2982       gimple_add_tmp_var (temp);
2983
2984       /* Build up the initialization and add it to pre_p.  */
2985       init = build (MODIFY_EXPR, void_type_node, temp, init);
2986       ret = gimplify_expr (&init, pre_p, post_p, is_gimple_stmt, fb_none);
2987       if (ret == GS_ERROR)
2988         return GS_ERROR;
2989
2990       append_to_statement_list (init, pre_p);
2991
2992       /* If needed, push the cleanup for the temp.  */
2993       if (TARGET_EXPR_CLEANUP (targ))
2994         {
2995           gimplify_stmt (&TARGET_EXPR_CLEANUP (targ));
2996           gimple_push_cleanup (temp, TARGET_EXPR_CLEANUP (targ), pre_p);
2997         }
2998
2999       /* Only expand this once.  */
3000       TREE_OPERAND (targ, 3) = init;
3001       TARGET_EXPR_INITIAL (targ) = NULL_TREE;
3002     }
3003   else if (!temp->decl.seen_in_bind_expr)
3004     /* We should have expanded this before.  */
3005     abort ();
3006
3007   *expr_p = temp;
3008   return GS_OK;
3009 }
3010
3011 /* Gimplification of expression trees.  */
3012
3013 /* Gimplify an expression which appears at statement context; usually, this
3014    means replacing it with a suitably gimple STATEMENT_LIST.  */
3015
3016 void
3017 gimplify_stmt (tree *stmt_p)
3018 {
3019   gimplify_expr (stmt_p, NULL, NULL, is_gimple_stmt, fb_none);
3020   if (!*stmt_p)
3021     *stmt_p = alloc_stmt_list ();
3022 }
3023
3024 /* Similarly, but force the result to be a STATEMENT_LIST.  */
3025
3026 void
3027 gimplify_to_stmt_list (tree *stmt_p)
3028 {
3029   gimplify_stmt (stmt_p);
3030   if (TREE_CODE (*stmt_p) != STATEMENT_LIST)
3031     {
3032       tree t = *stmt_p;
3033       *stmt_p = alloc_stmt_list ();
3034       append_to_statement_list (t, stmt_p);
3035     }
3036 }
3037
3038
3039 /*  Gimplifies the expression tree pointed by EXPR_P.  Return 0 if
3040     gimplification failed.
3041
3042     PRE_P points to the list where side effects that must happen before
3043         EXPR should be stored.
3044
3045     POST_P points to the list where side effects that must happen after
3046         EXPR should be stored, or NULL if there is no suitable list.  In
3047         that case, we copy the result to a temporary, emit the
3048         post-effects, and then return the temporary.
3049
3050     GIMPLE_TEST_F points to a function that takes a tree T and
3051         returns nonzero if T is in the GIMPLE form requested by the
3052         caller.  The GIMPLE predicates are in tree-gimple.c.
3053
3054         This test is used twice.  Before gimplification, the test is
3055         invoked to determine whether *EXPR_P is already gimple enough.  If
3056         that fails, *EXPR_P is gimplified according to its code and
3057         GIMPLE_TEST_F is called again.  If the test still fails, then a new
3058         temporary variable is created and assigned the value of the
3059         gimplified expression.
3060
3061     FALLBACK tells the function what sort of a temporary we want.  If the 1
3062         bit is set, an rvalue is OK.  If the 2 bit is set, an lvalue is OK.
3063         If both are set, either is OK, but an lvalue is preferable.
3064
3065     The return value is either GS_ERROR or GS_ALL_DONE, since this function
3066     iterates until solution.  */
3067
3068 enum gimplify_status
3069 gimplify_expr (tree *expr_p, tree *pre_p, tree *post_p,
3070                bool (* gimple_test_f) (tree), fallback_t fallback)
3071 {
3072   tree tmp;
3073   tree internal_pre = NULL_TREE;
3074   tree internal_post = NULL_TREE;
3075   tree save_expr;
3076   int is_statement = (pre_p == NULL);
3077   location_t *locus;
3078   location_t saved_location;
3079   enum gimplify_status ret;
3080
3081   save_expr = *expr_p;
3082   if (save_expr == NULL_TREE)
3083     return GS_ALL_DONE;
3084
3085   /* We used to check the predicate here and return immediately if it
3086      succeeds.  This is wrong; the design is for gimplification to be
3087      idempotent, and for the predicates to only test for valid forms, not
3088      whether they are fully simplified.  */
3089
3090   /* Set up our internal queues if needed.  */
3091   if (pre_p == NULL)
3092     pre_p = &internal_pre;
3093   if (post_p == NULL)
3094     post_p = &internal_post;
3095
3096   saved_location = input_location;
3097   if (save_expr == error_mark_node)
3098     locus = NULL;
3099   else
3100     locus = EXPR_LOCUS (save_expr);
3101   if (locus)
3102     input_location = *locus;
3103
3104   /* Loop over the specific gimplifiers until the toplevel node
3105      remains the same.  */
3106   do
3107     {
3108       /* Strip any uselessness.  */
3109       STRIP_MAIN_TYPE_NOPS (*expr_p);
3110
3111       /* Remember the expr.  */
3112       save_expr = *expr_p;
3113
3114       /* Die, die, die, my darling.  */
3115       if (save_expr == error_mark_node
3116           || TREE_TYPE (save_expr) == error_mark_node)
3117         {
3118           ret = GS_ERROR;
3119           break;
3120         }
3121
3122       /* Do any language-specific gimplification.  */
3123       ret = lang_hooks.gimplify_expr (expr_p, pre_p, post_p);
3124       if (ret == GS_OK)
3125         {
3126           if (*expr_p == NULL_TREE)
3127             break;
3128           if (*expr_p != save_expr)
3129             continue;
3130         }
3131       else if (ret != GS_UNHANDLED)
3132         break;
3133
3134       ret = GS_OK;
3135       switch (TREE_CODE (*expr_p))
3136         {
3137           /* First deal with the special cases.  */
3138
3139         case POSTINCREMENT_EXPR:
3140         case POSTDECREMENT_EXPR:
3141         case PREINCREMENT_EXPR:
3142         case PREDECREMENT_EXPR:
3143           ret = gimplify_self_mod_expr (expr_p, pre_p, post_p,
3144                                         fallback != fb_none);
3145           break;
3146
3147         case ARRAY_REF:
3148           ret = gimplify_array_ref (expr_p, pre_p, post_p,
3149                                     fallback & fb_lvalue);
3150           break;
3151
3152         case COMPONENT_REF:
3153           ret = gimplify_compound_lval (expr_p, pre_p, post_p,
3154                                         fallback & fb_lvalue);
3155           break;
3156
3157         case COND_EXPR:
3158           ret = gimplify_cond_expr (expr_p, pre_p, NULL_TREE);
3159           break;
3160
3161         case CALL_EXPR:
3162           ret = gimplify_call_expr (expr_p, pre_p, gimple_test_f);
3163           break;
3164
3165         case TREE_LIST:
3166           abort ();
3167
3168         case COMPOUND_EXPR:
3169           ret = gimplify_compound_expr (expr_p, pre_p, fallback != fb_none);
3170           break;
3171
3172         case REALPART_EXPR:
3173         case IMAGPART_EXPR:
3174           ret = gimplify_compound_lval (expr_p, pre_p, post_p,
3175                                         fallback & fb_lvalue);
3176           break;
3177
3178         case MODIFY_EXPR:
3179         case INIT_EXPR:
3180           ret = gimplify_modify_expr (expr_p, pre_p, post_p,
3181                                       fallback != fb_none);
3182           break;
3183
3184         case TRUTH_ANDIF_EXPR:
3185         case TRUTH_ORIF_EXPR:
3186           ret = gimplify_boolean_expr (expr_p);
3187           break;
3188
3189         case TRUTH_NOT_EXPR:
3190           TREE_OPERAND (*expr_p, 0)
3191             = gimple_boolify (TREE_OPERAND (*expr_p, 0));
3192           ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
3193                                is_gimple_val, fb_rvalue);
3194           recalculate_side_effects (*expr_p);
3195           break;
3196
3197         case ADDR_EXPR:
3198           ret = gimplify_addr_expr (expr_p, pre_p, post_p);
3199           break;
3200
3201         case VA_ARG_EXPR:
3202           /* Mark any _DECL inside the operand as volatile to avoid the
3203              optimizers messing around with it. FIXME: Remove this once
3204              VA_ARG_EXPRs are properly lowered.  */
3205           walk_tree (&TREE_OPERAND (*expr_p, 0), mark_decls_volatile_r,
3206                      NULL, NULL);
3207
3208           /* va_arg expressions are in GIMPLE form already.  */
3209           ret = GS_ALL_DONE;
3210           break;
3211
3212         case CONVERT_EXPR:
3213         case NOP_EXPR:
3214           if (IS_EMPTY_STMT (*expr_p))
3215             {
3216               ret = GS_ALL_DONE;
3217               break;
3218             }
3219
3220           if (VOID_TYPE_P (TREE_TYPE (*expr_p))
3221               || fallback == fb_none)
3222             {
3223               /* Just strip a conversion to void (or in void context) and
3224                  try again.  */
3225               *expr_p = TREE_OPERAND (*expr_p, 0);
3226               break;
3227             }
3228
3229           ret = gimplify_conversion (expr_p);
3230           if (ret == GS_ERROR)
3231             break;
3232           if (*expr_p != save_expr)
3233             break;
3234           /* FALLTHRU */
3235
3236         case FIX_TRUNC_EXPR:
3237         case FIX_CEIL_EXPR:
3238         case FIX_FLOOR_EXPR:
3239         case FIX_ROUND_EXPR:
3240           /* unary_expr: ... | '(' cast ')' val | ...  */
3241           ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
3242                                is_gimple_val, fb_rvalue);
3243           recalculate_side_effects (*expr_p);
3244           break;
3245
3246         case INDIRECT_REF:
3247           ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
3248                                is_gimple_reg, fb_rvalue);
3249           recalculate_side_effects (*expr_p);
3250           break;
3251
3252           /* Constants need not be gimplified.  */
3253         case INTEGER_CST:
3254         case REAL_CST:
3255         case STRING_CST:
3256         case COMPLEX_CST:
3257         case VECTOR_CST:
3258           ret = GS_ALL_DONE;
3259           break;
3260
3261         case CONST_DECL:
3262           *expr_p = DECL_INITIAL (*expr_p);
3263           break;
3264
3265         case EXC_PTR_EXPR:
3266           /* FIXME make this a decl.  */
3267           ret = GS_ALL_DONE;
3268           break;
3269
3270         case BIND_EXPR:
3271           ret = gimplify_bind_expr (expr_p, pre_p);
3272           break;
3273
3274         case LOOP_EXPR:
3275           ret = gimplify_loop_expr (expr_p, pre_p);
3276           break;
3277
3278         case SWITCH_EXPR:
3279           ret = gimplify_switch_expr (expr_p, pre_p);
3280           break;
3281
3282         case LABELED_BLOCK_EXPR:
3283           ret = gimplify_labeled_block_expr (expr_p);
3284           break;
3285
3286         case EXIT_BLOCK_EXPR:
3287           ret = gimplify_exit_block_expr (expr_p);
3288           break;
3289
3290         case EXIT_EXPR:
3291           ret = gimplify_exit_expr (expr_p);
3292           break;
3293
3294         case GOTO_EXPR:
3295           /* If the target is not LABEL, then it is a computed jump
3296              and the target needs to be gimplified.  */
3297           if (TREE_CODE (GOTO_DESTINATION (*expr_p)) != LABEL_DECL)
3298             ret = gimplify_expr (&GOTO_DESTINATION (*expr_p), pre_p,
3299                                  NULL, is_gimple_val, fb_rvalue);
3300           break;
3301
3302         case LABEL_EXPR:
3303           ret = GS_ALL_DONE;
3304 #ifdef ENABLE_CHECKING
3305           if (decl_function_context (LABEL_EXPR_LABEL (*expr_p)) != current_function_decl)
3306             abort ();
3307 #endif
3308           break;
3309
3310         case CASE_LABEL_EXPR:
3311           ret = gimplify_case_label_expr (expr_p);
3312           break;
3313
3314         case RETURN_EXPR:
3315           ret = gimplify_return_expr (*expr_p, pre_p);
3316           break;
3317
3318         case CONSTRUCTOR:
3319           /* Don't reduce this in place; let gimplify_init_constructor work
3320              its magic.  */
3321           ret = GS_ALL_DONE;
3322           break;
3323
3324           /* The following are special cases that are not handled by the
3325              original GIMPLE grammar.  */
3326
3327           /* SAVE_EXPR nodes are converted into a GIMPLE identifier and
3328              eliminated.  */
3329         case SAVE_EXPR:
3330           ret = gimplify_save_expr (expr_p, pre_p, post_p);
3331           break;
3332
3333         case BIT_FIELD_REF:
3334           {
3335             enum gimplify_status r0, r1, r2;
3336
3337             r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
3338                                 is_gimple_min_lval, fb_either);
3339             r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
3340                                 is_gimple_val, fb_rvalue);
3341             r2 = gimplify_expr (&TREE_OPERAND (*expr_p, 2), pre_p, post_p,
3342                                 is_gimple_val, fb_rvalue);
3343             recalculate_side_effects (*expr_p);
3344
3345             ret = MIN (r0, MIN (r1, r2));
3346           }
3347           break;
3348
3349         case NON_LVALUE_EXPR:
3350           /* This should have been stripped above.  */
3351           abort ();
3352           break;
3353
3354         case ASM_EXPR:
3355           ret = gimplify_asm_expr (expr_p, pre_p, post_p);
3356           break;
3357
3358         case TRY_FINALLY_EXPR:
3359         case TRY_CATCH_EXPR:
3360           gimplify_to_stmt_list (&TREE_OPERAND (*expr_p, 0));
3361           gimplify_to_stmt_list (&TREE_OPERAND (*expr_p, 1));
3362           ret = GS_ALL_DONE;
3363           break;
3364
3365         case CLEANUP_POINT_EXPR:
3366           ret = gimplify_cleanup_point_expr (expr_p, pre_p);
3367           break;
3368
3369         case TARGET_EXPR:
3370           ret = gimplify_target_expr (expr_p, pre_p, post_p);
3371           break;
3372
3373         case CATCH_EXPR:
3374           gimplify_to_stmt_list (&CATCH_BODY (*expr_p));
3375           ret = GS_ALL_DONE;
3376           break;
3377
3378         case EH_FILTER_EXPR:
3379           gimplify_to_stmt_list (&EH_FILTER_FAILURE (*expr_p));
3380           ret = GS_ALL_DONE;
3381           break;
3382
3383         case VTABLE_REF:
3384           /* This moves much of the actual computation out of the
3385              VTABLE_REF.  Perhaps this should be revisited once we want to
3386              do clever things with VTABLE_REFs.  */
3387           ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
3388                                is_gimple_min_lval, fb_lvalue);
3389           break;
3390
3391         case MIN_EXPR:
3392         case MAX_EXPR:
3393           ret = gimplify_minimax_expr (expr_p, pre_p, post_p);
3394           break;
3395
3396         case LABEL_DECL:
3397           /* We get here when taking the address of a label.  We mark
3398              the label as "forced"; meaning it can never be removed and
3399              it is a potential target for any computed goto.  */
3400           FORCED_LABEL (*expr_p) = 1;
3401           ret = GS_ALL_DONE;
3402           break;
3403
3404         case STATEMENT_LIST:
3405           ret = gimplify_statement_list (expr_p);
3406           break;
3407
3408         case VAR_DECL:
3409           /* ??? If this is a local variable, and it has not been seen in any
3410              outer BIND_EXPR, then it's probably the result of a duplicate
3411              declaration, for which we've already issued an error.  It would
3412              be really nice if the front end wouldn't leak these at all. 
3413              Currently the only known culprit is C++ destructors, as seen
3414              in g++.old-deja/g++.jason/binding.C.  */
3415           tmp = *expr_p;
3416           if (!TREE_STATIC (tmp) && !DECL_EXTERNAL (tmp)
3417               && decl_function_context (tmp) == current_function_decl
3418               && !tmp->decl.seen_in_bind_expr)
3419             {
3420 #ifdef ENABLE_CHECKING
3421               if (!errorcount && !sorrycount)
3422                 abort ();
3423 #endif
3424               ret = GS_ERROR;
3425             }
3426           else
3427             ret = GS_ALL_DONE;
3428           break;
3429
3430         default:
3431           /* If *EXPR_P does not need to be special-cased, handle it
3432              according to its class.  */
3433           if (TREE_CODE_CLASS (TREE_CODE (*expr_p)) == '1')
3434             ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
3435                                  post_p, is_gimple_val, fb_rvalue);
3436           else if (TREE_CODE_CLASS (TREE_CODE (*expr_p)) == '2'
3437                    || TREE_CODE_CLASS (TREE_CODE (*expr_p)) == '<'
3438                    || TREE_CODE (*expr_p) == TRUTH_AND_EXPR
3439                    || TREE_CODE (*expr_p) == TRUTH_OR_EXPR
3440                    || TREE_CODE (*expr_p) == TRUTH_XOR_EXPR)
3441             {
3442               enum gimplify_status r0, r1;
3443
3444               r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
3445                                   post_p, is_gimple_val, fb_rvalue);
3446               r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
3447                                   post_p, is_gimple_val, fb_rvalue);
3448
3449               ret = MIN (r0, r1);
3450             }
3451           else if (TREE_CODE_CLASS (TREE_CODE (*expr_p)) == 'd'
3452                    || TREE_CODE_CLASS (TREE_CODE (*expr_p)) == 'c')
3453             {
3454               ret = GS_ALL_DONE;
3455               break;
3456             }
3457           else
3458             /* Fail if we don't know how to handle this tree code.  */
3459             abort ();
3460
3461           recalculate_side_effects (*expr_p);
3462           break;
3463         }
3464
3465       /* If we replaced *expr_p, gimplify again.  */
3466       if (ret == GS_OK && (*expr_p == NULL || *expr_p == save_expr))
3467         ret = GS_ALL_DONE;
3468     }
3469   while (ret == GS_OK);
3470
3471   /* If we encountered an error_mark somewhere nested inside, either
3472      stub out the statement or propagate the error back out.  */
3473   if (ret == GS_ERROR)
3474     {
3475       if (is_statement)
3476         *expr_p = build_empty_stmt ();
3477       goto out;
3478     }
3479
3480 #ifdef ENABLE_CHECKING
3481   /* This was only valid as a return value from the langhook, which
3482      we handled.  Make sure it doesn't escape from any other context.  */
3483   if (ret == GS_UNHANDLED)
3484     abort ();
3485 #endif
3486
3487   if (!*expr_p)
3488     *expr_p = build_empty_stmt ();
3489   if (fallback == fb_none && !is_gimple_stmt (*expr_p))
3490     {
3491       /* We aren't looking for a value, and we don't have a valid
3492          statement.  If it doesn't have side-effects, throw it away.  */
3493       if (!TREE_SIDE_EFFECTS (*expr_p))
3494         *expr_p = build_empty_stmt ();
3495       else if (!TREE_THIS_VOLATILE (*expr_p))
3496         /* We only handle volatiles here; anything else with side-effects
3497            must be converted to a valid statement before we get here.  */
3498         abort ();
3499       else if (COMPLETE_TYPE_P (TREE_TYPE (*expr_p)))
3500         {
3501           /* Historically, the compiler has treated a bare
3502              reference to a volatile lvalue as forcing a load.  */
3503           tree tmp = create_tmp_var (TREE_TYPE (*expr_p), "vol");
3504           *expr_p = build (MODIFY_EXPR, TREE_TYPE (tmp), tmp, *expr_p);
3505         }
3506       else
3507         /* We can't do anything useful with a volatile reference to
3508            incomplete type, so just throw it away.  */
3509         *expr_p = build_empty_stmt ();
3510     }
3511
3512   /* If we are gimplifying at the statement level, we're done.  Tack
3513      everything together and replace the original statement with the
3514      gimplified form.  */
3515   if (is_statement)
3516     {
3517       if (internal_pre || internal_post)
3518         {
3519           append_to_statement_list (*expr_p, &internal_pre);
3520           append_to_statement_list (internal_post, &internal_pre);
3521           annotate_all_with_locus (&internal_pre, input_location);
3522           *expr_p = internal_pre;
3523         }
3524       goto out;
3525     }
3526
3527   /* Otherwise we're gimplifying a subexpression, so the resulting value is
3528      interesting.  */
3529
3530   /* If it's sufficiently simple already, we're done.  Unless we are
3531      handling some post-effects internally; if that's the case, we need to
3532      copy into a temp before adding the post-effects to the tree.  */
3533   if (!internal_post && (*gimple_test_f) (*expr_p))
3534     goto out;
3535
3536   /* Otherwise, we need to create a new temporary for the gimplified
3537      expression.  */
3538
3539   /* We can't return an lvalue if we have an internal postqueue.  The
3540      object the lvalue refers to would (probably) be modified by the
3541      postqueue; we need to copy the value out first, which means an
3542      rvalue.  */
3543   if ((fallback & fb_lvalue) && !internal_post
3544       && is_gimple_addr_expr_arg (*expr_p))
3545     {
3546       /* An lvalue will do.  Take the address of the expression, store it
3547          in a temporary, and replace the expression with an INDIRECT_REF of
3548          that temporary.  */
3549       tmp = build_addr_expr (*expr_p);
3550       gimplify_expr (&tmp, pre_p, post_p, is_gimple_reg, fb_rvalue);
3551       *expr_p = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (tmp)), tmp);
3552     }
3553   else if ((fallback & fb_rvalue) && is_gimple_rhs (*expr_p))
3554     {
3555 #if defined ENABLE_CHECKING
3556       if (VOID_TYPE_P (TREE_TYPE (*expr_p)))
3557         abort ();
3558 #endif
3559
3560       /* An rvalue will do.  Assign the gimplified expression into a new
3561          temporary TMP and replace the original expression with TMP.  */
3562
3563       if (internal_post || (fallback & fb_lvalue))
3564         /* The postqueue might change the value of the expression between
3565            the initialization and use of the temporary, so we can't use a
3566            formal temp.  FIXME do we care?  */
3567         *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p);
3568       else
3569         *expr_p = get_formal_tmp_var (*expr_p, pre_p);
3570     }
3571   else if (fallback & fb_mayfail)
3572     {
3573       /* If this is an asm statement, and the user asked for the impossible,
3574          don't abort.  Fail and let gimplify_asm_expr issue an error.  */
3575       ret = GS_ERROR;
3576       goto out;
3577     }
3578   else
3579     {
3580       fprintf (stderr, "gimplification failed:\n");
3581       print_generic_expr (stderr, *expr_p, 0);
3582       debug_tree (*expr_p);
3583       abort ();
3584     }
3585
3586 #if defined ENABLE_CHECKING
3587   /* Make sure the temporary matches our predicate.  */
3588   if (!(*gimple_test_f) (*expr_p))
3589     abort ();
3590 #endif
3591
3592   if (internal_post)
3593     {
3594       annotate_all_with_locus (&internal_post, input_location);
3595       append_to_statement_list (internal_post, pre_p);
3596     }
3597
3598  out:
3599   input_location = saved_location;
3600   return ret;
3601 }
3602
3603 #ifdef ENABLE_CHECKING
3604 /* Compare types A and B for a "close enough" match.  */
3605
3606 static bool
3607 cpt_same_type (tree a, tree b)
3608 {
3609   if (lang_hooks.types_compatible_p (a, b))
3610     return true;
3611
3612   /* ??? The C++ FE decomposes METHOD_TYPES to FUNCTION_TYPES and doesn't
3613      link them together.  This routine is intended to catch type errors
3614      that will affect the optimizers, and the optimizers don't add new
3615      dereferences of function pointers, so ignore it.  */
3616   if ((TREE_CODE (a) == FUNCTION_TYPE || TREE_CODE (a) == METHOD_TYPE)
3617       && (TREE_CODE (b) == FUNCTION_TYPE || TREE_CODE (b) == METHOD_TYPE))
3618     return true;
3619
3620   /* ??? The C FE pushes type qualifiers after the fact into the type of
3621      the element from the type of the array.  See build_unary_op's handling
3622      of ADDR_EXPR.  This seems wrong -- if we were going to do this, we
3623      should have done it when creating the variable in the first place.
3624      Alternately, why aren't the two array types made variants?  */
3625   if (TREE_CODE (a) == ARRAY_TYPE && TREE_CODE (b) == ARRAY_TYPE)
3626     return cpt_same_type (TREE_TYPE (a), TREE_TYPE (b));
3627
3628   /* And because of those, we have to recurse down through pointers.  */
3629   if (POINTER_TYPE_P (a) && POINTER_TYPE_P (b))
3630     return cpt_same_type (TREE_TYPE (a), TREE_TYPE (b));
3631
3632   return false;
3633 }
3634
3635 /* Check for some cases of the front end missing cast expressions.
3636    The type of a dereference should correspond to the pointer type;
3637    similarly the type of an address should match its object.  */
3638
3639 static tree
3640 check_pointer_types_r (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
3641                        void *data ATTRIBUTE_UNUSED)
3642 {
3643   tree t = *tp;
3644   tree ptype, otype, dtype;
3645
3646   switch (TREE_CODE (t))
3647     {
3648     case INDIRECT_REF:
3649     case ARRAY_REF:
3650       otype = TREE_TYPE (t);
3651       ptype = TREE_TYPE (TREE_OPERAND (t, 0));
3652       dtype = TREE_TYPE (ptype);
3653       if (!cpt_same_type (otype, dtype))
3654         abort ();
3655       break;
3656
3657     case ADDR_EXPR:
3658       ptype = TREE_TYPE (t);
3659       otype = TREE_TYPE (TREE_OPERAND (t, 0));
3660       dtype = TREE_TYPE (ptype);
3661       if (!cpt_same_type (otype, dtype))
3662         {
3663           /* &array is allowed to produce a pointer to the element,
3664              rather than a pointer to the array type.  */
3665           if (TREE_CODE (otype) == ARRAY_TYPE
3666               && POINTER_TYPE_P (ptype)
3667               && cpt_same_type (TREE_TYPE (otype), dtype))
3668             break;
3669           abort ();
3670         }
3671       break;
3672
3673     default:
3674       return NULL_TREE;
3675     }
3676
3677
3678   return NULL_TREE;
3679 }
3680 #endif
3681
3682 /* Gimplify the body of statements pointed by BODY_P.  FNDECL is the
3683    function decl containing BODY.  */
3684
3685 void
3686 gimplify_body (tree *body_p, tree fndecl)
3687 {
3688   location_t saved_location = input_location;
3689   tree body;
3690
3691   timevar_push (TV_TREE_GIMPLIFY);
3692   push_gimplify_context ();
3693
3694   /* Unshare most shared trees in the body.  */
3695   unshare_all_trees (*body_p);
3696
3697   /* Make sure input_location isn't set to something wierd.  */
3698   input_location = DECL_SOURCE_LOCATION (fndecl);
3699
3700   /* Gimplify the function's body.  */
3701   gimplify_stmt (body_p);
3702   body = *body_p;
3703
3704   /* Unshare again, in case gimplification was sloppy.  */
3705   unshare_all_trees (body);
3706
3707   /* If there isn't an outer BIND_EXPR, add one.  */
3708   if (TREE_CODE (body) == STATEMENT_LIST)
3709     {
3710       tree t = expr_only (*body_p);
3711       if (t)
3712         body = t;
3713     }
3714   if (TREE_CODE (body) != BIND_EXPR)
3715     {
3716       tree b = build (BIND_EXPR, void_type_node, NULL_TREE,
3717                       NULL_TREE, NULL_TREE);
3718       TREE_SIDE_EFFECTS (b) = 1;
3719       append_to_statement_list_force (body, &BIND_EXPR_BODY (b));
3720       body = b;
3721     }
3722   *body_p = body;
3723
3724   pop_gimplify_context (body);
3725
3726 #ifdef ENABLE_CHECKING
3727   walk_tree (body_p, check_pointer_types_r, NULL, NULL);
3728 #endif
3729
3730   timevar_pop (TV_TREE_GIMPLIFY);
3731   input_location = saved_location;
3732 }
3733
3734 /* Entry point to the gimplification pass.  FNDECL is the FUNCTION_DECL
3735    node for the function we want to gimplify.  */
3736
3737 void
3738 gimplify_function_tree (tree fndecl)
3739 {
3740   tree oldfn;
3741
3742   oldfn = current_function_decl;
3743   current_function_decl = fndecl;
3744
3745   gimplify_body (&DECL_SAVED_TREE (fndecl), fndecl);
3746
3747   /* If we're instrumenting function entry/exit, then prepend the call to
3748      the entry hook and wrap the whole function in a TRY_FINALLY_EXPR to
3749      catch the exit hook.  */
3750   /* ??? Add some way to ignore exceptions for this TFE.  */
3751   if (flag_instrument_function_entry_exit
3752       && ! DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (fndecl))
3753     {
3754       tree tf, x, bind;
3755
3756       tf = build (TRY_FINALLY_EXPR, void_type_node, NULL, NULL);
3757       TREE_SIDE_EFFECTS (tf) = 1;
3758       x = DECL_SAVED_TREE (fndecl);
3759       append_to_statement_list (x, &TREE_OPERAND (tf, 0));
3760       x = implicit_built_in_decls[BUILT_IN_PROFILE_FUNC_EXIT];
3761       x = build_function_call_expr (x, NULL);
3762       append_to_statement_list (x, &TREE_OPERAND (tf, 1));
3763
3764       bind = build (BIND_EXPR, void_type_node, NULL, NULL, NULL);
3765       TREE_SIDE_EFFECTS (bind) = 1;
3766       x = implicit_built_in_decls[BUILT_IN_PROFILE_FUNC_ENTER];
3767       x = build_function_call_expr (x, NULL);
3768       append_to_statement_list (x, &BIND_EXPR_BODY (bind));
3769       append_to_statement_list (tf, &BIND_EXPR_BODY (bind));
3770
3771       DECL_SAVED_TREE (fndecl) = bind;
3772     }
3773
3774   current_function_decl = oldfn;
3775 }
3776
3777 #include "gt-gimplify.h"