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