re PR c++/16036 (Spurious "may be used uninitialized in this function" warning)
[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
950       /* ??? With complex control flow (usually involving abnormal edges),
951          we can wind up warning about an uninitialized value for this.  Due
952          to how this variable is constructed and initialized, this is never
953          true.  Give up and never warn.  */
954       TREE_NO_WARNING (result) = 1;
955
956       gimplify_ctxp->return_temp = result;
957     }
958
959   /* Smash the lhs of the MODIFY_EXPR to the temporary we plan to use.
960      Then gimplify the whole thing.  */
961   if (result != result_decl)
962     TREE_OPERAND (ret_expr, 0) = result;
963   gimplify_stmt (&TREE_OPERAND (stmt, 0));
964   append_to_statement_list (TREE_OPERAND (stmt, 0), pre_p);
965
966   /* If we didn't use a temporary, then the result is just the result_decl.
967      Otherwise we need a simple copy.  This should already be gimple.  */
968   if (result == result_decl)
969     ret_expr = result;
970   else
971     ret_expr = build (MODIFY_EXPR, TREE_TYPE (result), result_decl, result);
972   TREE_OPERAND (stmt, 0) = ret_expr;
973
974   return GS_ALL_DONE;
975 }
976
977 /* Gimplify a LOOP_EXPR.  Normally this just involves gimplifying the body
978    and replacing the LOOP_EXPR with goto, but if the loop contains an
979    EXIT_EXPR, we need to append a label for it to jump to.  */
980
981 static enum gimplify_status
982 gimplify_loop_expr (tree *expr_p, tree *pre_p)
983 {
984   tree saved_label = gimplify_ctxp->exit_label;
985   tree start_label = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
986   tree jump_stmt = build_and_jump (&LABEL_EXPR_LABEL (start_label));
987
988   append_to_statement_list (start_label, pre_p);
989
990   gimplify_ctxp->exit_label = NULL_TREE;
991
992   gimplify_stmt (&LOOP_EXPR_BODY (*expr_p));
993   append_to_statement_list (LOOP_EXPR_BODY (*expr_p), pre_p);
994
995   if (gimplify_ctxp->exit_label)
996     {
997       append_to_statement_list (jump_stmt, pre_p);
998       *expr_p = build1 (LABEL_EXPR, void_type_node, gimplify_ctxp->exit_label);
999     }
1000   else
1001     *expr_p = jump_stmt;
1002
1003   gimplify_ctxp->exit_label = saved_label;
1004
1005   return GS_ALL_DONE;
1006 }
1007
1008 /* Compare two case labels.  Because the front end should already have
1009    made sure that case ranges do not overlap, it is enough to only compare
1010    the CASE_LOW values of each case label.  */
1011
1012 static int
1013 compare_case_labels (const void *p1, const void *p2)
1014 {
1015   tree case1 = *(tree *)p1;
1016   tree case2 = *(tree *)p2;
1017
1018   return tree_int_cst_compare (CASE_LOW (case1), CASE_LOW (case2));
1019 }
1020
1021 /* Sort the case labels in LABEL_VEC in ascending order.  */
1022
1023 void
1024 sort_case_labels (tree label_vec)
1025 {
1026   size_t len = TREE_VEC_LENGTH (label_vec);
1027   tree default_case = TREE_VEC_ELT (label_vec, len - 1);
1028
1029   if (CASE_LOW (default_case))
1030     {
1031       size_t i;
1032
1033       /* The last label in the vector should be the default case
1034          but it is not.  */
1035       for (i = 0; i < len; ++i)
1036         {
1037           tree t = TREE_VEC_ELT (label_vec, i);
1038           if (!CASE_LOW (t))
1039             {
1040               default_case = t;
1041               TREE_VEC_ELT (label_vec, i) = TREE_VEC_ELT (label_vec, len - 1);
1042               TREE_VEC_ELT (label_vec, len - 1) = default_case;
1043               break;
1044             }
1045         }
1046     }
1047
1048   qsort (&TREE_VEC_ELT (label_vec, 0), len - 1, sizeof (tree),
1049          compare_case_labels);
1050 }
1051
1052 /* Gimplify a SWITCH_EXPR, and collect a TREE_VEC of the labels it can
1053    branch to.  */
1054
1055 static enum gimplify_status
1056 gimplify_switch_expr (tree *expr_p, tree *pre_p)
1057 {
1058   tree switch_expr = *expr_p;
1059   enum gimplify_status ret;
1060
1061   ret = gimplify_expr (&SWITCH_COND (switch_expr), pre_p, NULL,
1062                        is_gimple_val, fb_rvalue);
1063
1064   if (SWITCH_BODY (switch_expr))
1065     {
1066       varray_type labels, saved_labels;
1067       tree label_vec, default_case = NULL_TREE;
1068       size_t i, len;
1069
1070       /* If someone can be bothered to fill in the labels, they can
1071          be bothered to null out the body too.  */
1072       if (SWITCH_LABELS (switch_expr))
1073         abort ();
1074
1075       saved_labels = gimplify_ctxp->case_labels;
1076       VARRAY_TREE_INIT (gimplify_ctxp->case_labels, 8, "case_labels");
1077
1078       gimplify_to_stmt_list (&SWITCH_BODY (switch_expr));
1079
1080       labels = gimplify_ctxp->case_labels;
1081       gimplify_ctxp->case_labels = saved_labels;
1082
1083       len = VARRAY_ACTIVE_SIZE (labels);
1084
1085       for (i = 0; i < len; ++i)
1086         {
1087           tree t = VARRAY_TREE (labels, i);
1088           if (!CASE_LOW (t))
1089             {
1090               /* The default case must be the last label in the list.  */
1091               default_case = t;
1092               VARRAY_TREE (labels, i) = VARRAY_TREE (labels, len - 1);
1093               len--;
1094               break;
1095             }
1096         }
1097
1098       label_vec = make_tree_vec (len + 1);
1099       SWITCH_LABELS (*expr_p) = label_vec;
1100       append_to_statement_list (switch_expr, pre_p);
1101
1102       if (! default_case)
1103         {
1104           /* If the switch has no default label, add one, so that we jump
1105              around the switch body.  */
1106           default_case = build (CASE_LABEL_EXPR, void_type_node, NULL_TREE,
1107                                 NULL_TREE, create_artificial_label ());
1108           append_to_statement_list (SWITCH_BODY (switch_expr), pre_p);
1109           *expr_p = build (LABEL_EXPR, void_type_node,
1110                            CASE_LABEL (default_case));
1111         }
1112       else
1113         *expr_p = SWITCH_BODY (switch_expr);
1114
1115       for (i = 0; i < len; ++i)
1116         TREE_VEC_ELT (label_vec, i) = VARRAY_TREE (labels, i);
1117       TREE_VEC_ELT (label_vec, len) = default_case;
1118
1119       sort_case_labels (label_vec);
1120
1121       SWITCH_BODY (switch_expr) = NULL;
1122     }
1123   else if (!SWITCH_LABELS (switch_expr))
1124     abort ();
1125
1126   return ret;
1127 }
1128
1129 static enum gimplify_status
1130 gimplify_case_label_expr (tree *expr_p)
1131 {
1132   tree expr = *expr_p;
1133   if (gimplify_ctxp->case_labels)
1134     VARRAY_PUSH_TREE (gimplify_ctxp->case_labels, expr);
1135   else
1136     abort ();
1137   *expr_p = build (LABEL_EXPR, void_type_node, CASE_LABEL (expr));
1138   return GS_ALL_DONE;
1139 }
1140
1141 /* Gimplify a LABELED_BLOCK_EXPR into a LABEL_EXPR following
1142    a (possibly empty) body.  */
1143
1144 static enum gimplify_status
1145 gimplify_labeled_block_expr (tree *expr_p)
1146 {
1147   tree body = LABELED_BLOCK_BODY (*expr_p);
1148   tree label = LABELED_BLOCK_LABEL (*expr_p);
1149   tree t;
1150
1151   DECL_CONTEXT (label) = current_function_decl;
1152   t = build (LABEL_EXPR, void_type_node, label);
1153   if (body != NULL_TREE)
1154     t = build (COMPOUND_EXPR, void_type_node, body, t);
1155   *expr_p = t;
1156
1157   return GS_OK;
1158 }
1159
1160 /* Gimplify a EXIT_BLOCK_EXPR into a GOTO_EXPR.  */
1161
1162 static enum gimplify_status
1163 gimplify_exit_block_expr (tree *expr_p)
1164 {
1165   tree labeled_block = TREE_OPERAND (*expr_p, 0);
1166   tree label;
1167
1168   /* First operand must be a LABELED_BLOCK_EXPR, which should
1169      already be lowered (or partially lowered) when we get here.  */
1170 #if defined ENABLE_CHECKING
1171   if (TREE_CODE (labeled_block) != LABELED_BLOCK_EXPR)
1172     abort ();
1173 #endif
1174
1175   label = LABELED_BLOCK_LABEL (labeled_block);
1176   *expr_p = build1 (GOTO_EXPR, void_type_node, label);
1177
1178   return GS_OK;
1179 }
1180
1181 /* Build a GOTO to the LABEL_DECL pointed to by LABEL_P, building it first
1182    if necessary.  */
1183
1184 tree
1185 build_and_jump (tree *label_p)
1186 {
1187   if (label_p == NULL)
1188     /* If there's nowhere to jump, just fall through.  */
1189     return build_empty_stmt ();
1190
1191   if (*label_p == NULL_TREE)
1192     {
1193       tree label = create_artificial_label ();
1194       *label_p = label;
1195     }
1196
1197   return build1 (GOTO_EXPR, void_type_node, *label_p);
1198 }
1199
1200 /* Gimplify an EXIT_EXPR by converting to a GOTO_EXPR inside a COND_EXPR.
1201    This also involves building a label to jump to and communicating it to
1202    gimplify_loop_expr through gimplify_ctxp->exit_label.  */
1203
1204 static enum gimplify_status
1205 gimplify_exit_expr (tree *expr_p)
1206 {
1207   tree cond = TREE_OPERAND (*expr_p, 0);
1208   tree expr;
1209
1210   expr = build_and_jump (&gimplify_ctxp->exit_label);
1211   expr = build (COND_EXPR, void_type_node, cond, expr, build_empty_stmt ());
1212   *expr_p = expr;
1213
1214   return GS_OK;
1215 }
1216
1217 /* A helper function to be called via walk_tree.  Mark all labels under *TP
1218    as being forced.  To be called for DECL_INITIAL of static variables.  */
1219
1220 tree
1221 force_labels_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
1222 {
1223   if (TYPE_P (*tp))
1224     *walk_subtrees = 0;
1225   if (TREE_CODE (*tp) == LABEL_DECL)
1226     FORCED_LABEL (*tp) = 1;
1227
1228   return NULL_TREE;
1229 }
1230
1231 /* Break out elements of a constructor used as an initializer into separate
1232    MODIFY_EXPRs.
1233
1234    Note that we still need to clear any elements that don't have explicit
1235    initializers, so if not all elements are initialized we keep the
1236    original MODIFY_EXPR, we just remove all of the constructor elements.  */
1237
1238 static enum gimplify_status
1239 gimplify_init_constructor (tree *expr_p, tree *pre_p,
1240                            tree *post_p, int want_value)
1241 {
1242   tree object = TREE_OPERAND (*expr_p, 0);
1243   tree ctor = TREE_OPERAND (*expr_p, 1);
1244   tree type = TREE_TYPE (ctor);
1245   enum gimplify_status ret;
1246   tree elt_list;
1247
1248   if (TREE_CODE (ctor) != CONSTRUCTOR)
1249     return GS_UNHANDLED;
1250
1251   elt_list = CONSTRUCTOR_ELTS (ctor);
1252
1253   ret = GS_ALL_DONE;
1254   switch (TREE_CODE (type))
1255     {
1256     case RECORD_TYPE:
1257     case UNION_TYPE:
1258     case QUAL_UNION_TYPE:
1259     case ARRAY_TYPE:
1260       {
1261         HOST_WIDE_INT i, num_elements, num_nonzero_elements;
1262         HOST_WIDE_INT num_nonconstant_elements;
1263         bool cleared;
1264
1265         /* Aggregate types must lower constructors to initialization of
1266            individual elements.  The exception is that a CONSTRUCTOR node
1267            with no elements indicates zero-initialization of the whole.  */
1268         if (elt_list == NULL)
1269           {
1270             if (want_value)
1271               {
1272                 *expr_p = object;
1273                 return GS_OK;
1274               }
1275             else
1276               return GS_ALL_DONE;
1277           }
1278
1279         categorize_ctor_elements (ctor, &num_nonzero_elements,
1280                                   &num_nonconstant_elements);
1281         num_elements = count_type_elements (TREE_TYPE (ctor));
1282
1283         /* If a const aggregate variable is being initialized, then it
1284            should never be a lose to promote the variable to be static.  */
1285         if (num_nonconstant_elements == 0
1286             && TREE_READONLY (object)
1287             && TREE_CODE (object) == VAR_DECL)
1288           {
1289             DECL_INITIAL (object) = ctor;
1290             TREE_STATIC (object) = 1;
1291             if (!DECL_NAME (object))
1292               DECL_NAME (object) = create_tmp_var_name ("C");
1293             walk_tree (&DECL_INITIAL (object), force_labels_r, NULL, NULL);
1294
1295             /* ??? C++ doesn't automatically append a .<number> to the
1296                assembler name, and even when it does, it looks a FE private
1297                data structures to figure out what that number should be,
1298                which are not set for this variable.  I suppose this is
1299                important for local statics for inline functions, which aren't
1300                "local" in the object file sense.  So in order to get a unique
1301                TU-local symbol, we must invoke the lhd version now.  */
1302             lhd_set_decl_assembler_name (object);
1303
1304             *expr_p = build_empty_stmt ();
1305             break;
1306           }
1307
1308         /* If there are "lots" of initialized elements, and all of them
1309            are valid address constants, then the entire initializer can
1310            be dropped to memory, and then memcpy'd out.  */
1311         if (num_nonconstant_elements == 0)
1312           {
1313             HOST_WIDE_INT size = int_size_in_bytes (type);
1314             unsigned int align;
1315
1316             /* ??? We can still get unbounded array types, at least
1317                from the C++ front end.  This seems wrong, but attempt
1318                to work around it for now.  */
1319             if (size < 0)
1320               {
1321                 size = int_size_in_bytes (TREE_TYPE (object));
1322                 if (size >= 0)
1323                   TREE_TYPE (ctor) = type = TREE_TYPE (object);
1324               }
1325
1326             /* Find the maximum alignment we can assume for the object.  */
1327             /* ??? Make use of DECL_OFFSET_ALIGN.  */
1328             if (DECL_P (object))
1329               align = DECL_ALIGN (object);
1330             else
1331               align = TYPE_ALIGN (type);
1332
1333             if (size > 0 && !can_move_by_pieces (size, align))
1334               {
1335                 tree new = create_tmp_var_raw (type, "C");
1336                 gimple_add_tmp_var (new);
1337                 TREE_STATIC (new) = 1;
1338                 TREE_READONLY (new) = 1;
1339                 DECL_INITIAL (new) = ctor;
1340                 if (align > DECL_ALIGN (new))
1341                   {
1342                     DECL_ALIGN (new) = align;
1343                     DECL_USER_ALIGN (new) = 1;
1344                   }
1345                 walk_tree (&DECL_INITIAL (new), force_labels_r, NULL, NULL);
1346
1347                 TREE_OPERAND (*expr_p, 1) = new;
1348                 break;
1349               }
1350           }
1351
1352         /* If there are "lots" of initialized elements, even discounting
1353            those that are not address constants (and thus *must* be 
1354            computed at runtime), then partition the constructor into
1355            constant and non-constant parts.  Block copy the constant
1356            parts in, then generate code for the non-constant parts.  */
1357         /* TODO.  There's code in cp/typeck.c to do this.  */
1358
1359         /* If there are "lots" of zeros, then block clear the object first.  */
1360         cleared = false;
1361         if (num_elements - num_nonzero_elements > CLEAR_RATIO
1362             && num_nonzero_elements < num_elements/4)
1363           cleared = true;
1364
1365         /* ??? This bit ought not be needed.  For any element not present
1366            in the initializer, we should simply set them to zero.  Except
1367            we'd need to *find* the elements that are not present, and that
1368            requires trickery to avoid quadratic compile-time behavior in
1369            large cases or excessive memory use in small cases.  */
1370         else
1371           {
1372             HOST_WIDE_INT len = list_length (elt_list);
1373             if (TREE_CODE (type) == ARRAY_TYPE)
1374               {
1375                 tree nelts = array_type_nelts (type);
1376                 if (!host_integerp (nelts, 1)
1377                     || tree_low_cst (nelts, 1) != len)
1378                   cleared = 1;;
1379               }
1380             else if (len != fields_length (type))
1381               cleared = 1;
1382           }
1383
1384         if (cleared)
1385           {
1386             CONSTRUCTOR_ELTS (ctor) = NULL_TREE;
1387             append_to_statement_list (*expr_p, pre_p);
1388           }
1389
1390         for (i = 0; elt_list; i++, elt_list = TREE_CHAIN (elt_list))
1391           {
1392             tree purpose, value, cref, init;
1393
1394             purpose = TREE_PURPOSE (elt_list);
1395             value = TREE_VALUE (elt_list);
1396
1397             if (cleared && initializer_zerop (value))
1398               continue;
1399
1400             if (TREE_CODE (type) == ARRAY_TYPE)
1401               {
1402                 tree t = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (object)));
1403
1404                 /* ??? Here's to hoping the front end fills in all of the
1405                    indicies, so we don't have to figure out what's missing
1406                    ourselves.  */
1407                 if (!purpose)
1408                   abort ();
1409                 /* ??? Need to handle this.  */
1410                 if (TREE_CODE (purpose) == RANGE_EXPR)
1411                   abort ();
1412
1413                 cref = build (ARRAY_REF, t, object, purpose);
1414               }
1415             else
1416               {
1417                 cref = build (COMPONENT_REF, TREE_TYPE (purpose),
1418                               object, purpose);
1419               }
1420
1421             init = build (MODIFY_EXPR, TREE_TYPE (purpose), cref, value);
1422             /* Each member initialization is a full-expression.  */
1423             gimplify_stmt (&init);
1424             append_to_statement_list (init, pre_p);
1425           }
1426
1427         *expr_p = build_empty_stmt ();
1428       }
1429       break;
1430
1431     case COMPLEX_TYPE:
1432       {
1433         tree r, i;
1434
1435         /* Extract the real and imaginary parts out of the ctor.  */
1436         r = i = NULL_TREE;
1437         if (elt_list)
1438           {
1439             r = TREE_VALUE (elt_list);
1440             elt_list = TREE_CHAIN (elt_list);
1441             if (elt_list)
1442               {
1443                 i = TREE_VALUE (elt_list);
1444                 if (TREE_CHAIN (elt_list))
1445                   abort ();
1446               }
1447           }
1448         if (r == NULL || i == NULL)
1449           {
1450             tree zero = convert (TREE_TYPE (type), integer_zero_node);
1451             if (r == NULL)
1452               r = zero;
1453             if (i == NULL)
1454               i = zero;
1455           }
1456
1457         /* Complex types have either COMPLEX_CST or COMPLEX_EXPR to
1458            represent creation of a complex value.  */
1459         if (TREE_CONSTANT (r) && TREE_CONSTANT (i))
1460           {
1461             ctor = build_complex (type, r, i);
1462             TREE_OPERAND (*expr_p, 1) = ctor;
1463           }
1464         else
1465           {
1466             ctor = build (COMPLEX_EXPR, type, r, i);
1467             TREE_OPERAND (*expr_p, 1) = ctor;
1468             ret = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
1469                                  is_gimple_rhs, fb_rvalue);
1470           }
1471       }
1472       break;
1473
1474     case VECTOR_TYPE:
1475       /* Go ahead and simplify constant constructors to VECTOR_CST.  */
1476       if (TREE_CONSTANT (ctor))
1477         TREE_OPERAND (*expr_p, 1) = build_vector (type, elt_list);
1478       else
1479         {
1480           /* Vector types use CONSTRUCTOR all the way through gimple
1481              compilation as a general initializer.  */
1482           for (; elt_list; elt_list = TREE_CHAIN (elt_list))
1483             {
1484               enum gimplify_status tret;
1485               tret = gimplify_expr (&TREE_VALUE (elt_list), pre_p, post_p,
1486                                     is_gimple_constructor_elt, fb_rvalue);
1487               if (tret == GS_ERROR)
1488                 ret = GS_ERROR;
1489             }
1490         }
1491       break;
1492
1493     default:
1494       /* So how did we get a CONSTRUCTOR for a scalar type?  */
1495       abort ();
1496     }
1497
1498   if (ret == GS_ERROR)
1499     return GS_ERROR;
1500   else if (want_value)
1501     {
1502       append_to_statement_list (*expr_p, pre_p);
1503       *expr_p = object;
1504       return GS_OK;
1505     }
1506   else
1507     return GS_ALL_DONE;
1508 }
1509
1510 /* *EXPR_P is a COMPONENT_REF being used as an rvalue.  If its type is
1511    different from its canonical type, wrap the whole thing inside a
1512    NOP_EXPR and force the type of the COMPONENT_REF to be the canonical
1513    type.
1514
1515    The canonical type of a COMPONENT_REF is the type of the field being
1516    referenced--unless the field is a bit-field which can be read directly
1517    in a smaller mode, in which case the canonical type is the
1518    sign-appropriate type corresponding to that mode.  */
1519
1520 static void
1521 canonicalize_component_ref (tree *expr_p)
1522 {
1523   tree expr = *expr_p;
1524   tree type;
1525
1526   if (TREE_CODE (expr) != COMPONENT_REF)
1527     abort ();
1528
1529   if (INTEGRAL_TYPE_P (TREE_TYPE (expr)))
1530     type = TREE_TYPE (get_unwidened (expr, NULL_TREE));
1531   else
1532     type = TREE_TYPE (TREE_OPERAND (expr, 1));
1533
1534   if (TREE_TYPE (expr) != type)
1535     {
1536       tree old_type = TREE_TYPE (expr);
1537
1538       /* Set the type of the COMPONENT_REF to the underlying type.  */
1539       TREE_TYPE (expr) = type;
1540
1541       /* And wrap the whole thing inside a NOP_EXPR.  */
1542       expr = build1 (NOP_EXPR, old_type, expr);
1543
1544       *expr_p = expr;
1545     }
1546 }
1547
1548 /* If a NOP conversion is changing a pointer to array of foo to a pointer
1549    to foo, embed that change in the ADDR_EXPR.  Lest we perturb the type
1550    system too badly, we must take extra steps to ensure that the ADDR_EXPR
1551    and the addressed object continue to agree on types.  */
1552 /* ??? We might could do better if we recognize
1553         T array[N][M];
1554         (T *)&array
1555    ==>
1556         &array[0][0];
1557 */
1558
1559 static void
1560 canonicalize_addr_expr (tree* expr_p)
1561 {
1562   tree expr = *expr_p;
1563   tree ctype = TREE_TYPE (expr);
1564   tree addr_expr = TREE_OPERAND (expr, 0);
1565   tree atype = TREE_TYPE (addr_expr);
1566   tree dctype, datype, ddatype, otype, obj_expr;
1567
1568   /* Both cast and addr_expr types should be pointers.  */
1569   if (!POINTER_TYPE_P (ctype) || !POINTER_TYPE_P (atype))
1570     return;
1571
1572   /* The addr_expr type should be a pointer to an array.  */
1573   datype = TREE_TYPE (atype);
1574   if (TREE_CODE (datype) != ARRAY_TYPE)
1575     return;
1576
1577   /* Both cast and addr_expr types should address the same object type.  */
1578   dctype = TREE_TYPE (ctype);
1579   ddatype = TREE_TYPE (datype);
1580   if (!lang_hooks.types_compatible_p (ddatype, dctype))
1581     return;
1582
1583   /* The addr_expr and the object type should match.  */
1584   obj_expr = TREE_OPERAND (addr_expr, 0);
1585   otype = TREE_TYPE (obj_expr);
1586   if (!lang_hooks.types_compatible_p (otype, datype))
1587     return;
1588
1589   /* All checks succeeded.  Build a new node to merge the cast.  */
1590   *expr_p = build1 (ADDR_EXPR, ctype, obj_expr);
1591 }
1592
1593 /* *EXPR_P is a NOP_EXPR or CONVERT_EXPR.  Remove it and/or other conversions
1594    underneath as appropriate.  */
1595
1596 static enum gimplify_status
1597 gimplify_conversion (tree *expr_p)
1598 {  
1599   /* Strip away as many useless type conversions as possible
1600      at the toplevel.  */
1601   STRIP_USELESS_TYPE_CONVERSION (*expr_p);
1602
1603   /* If we still have a conversion at the toplevel, then strip
1604      away all but the outermost conversion.  */
1605   if (TREE_CODE (*expr_p) == NOP_EXPR || TREE_CODE (*expr_p) == CONVERT_EXPR)
1606     {
1607       STRIP_SIGN_NOPS (TREE_OPERAND (*expr_p, 0));
1608
1609       /* And remove the outermost conversion if it's useless.  */
1610       if (tree_ssa_useless_type_conversion (*expr_p))
1611         *expr_p = TREE_OPERAND (*expr_p, 0);
1612     }
1613
1614   /* If we still have a conversion at the toplevel,
1615      then canonicalize some constructs.  */
1616   if (TREE_CODE (*expr_p) == NOP_EXPR || TREE_CODE (*expr_p) == CONVERT_EXPR)
1617     {
1618       tree sub = TREE_OPERAND (*expr_p, 0);
1619
1620       /* If a NOP conversion is changing the type of a COMPONENT_REF
1621          expression, then canonicalize its type now in order to expose more
1622          redundant conversions.  */
1623       if (TREE_CODE (sub) == COMPONENT_REF)
1624         canonicalize_component_ref (&TREE_OPERAND (*expr_p, 0));
1625
1626       /* If a NOP conversion is changing a pointer to array of foo
1627          to a pointer to foo, embed that change in the ADDR_EXPR.  */
1628       else if (TREE_CODE (sub) == ADDR_EXPR)
1629         canonicalize_addr_expr (expr_p);
1630     }
1631
1632   return GS_OK;
1633 }
1634
1635 /* Reduce MIN/MAX_EXPR to a COND_EXPR for further gimplification.  */
1636
1637 static enum gimplify_status
1638 gimplify_minimax_expr (tree *expr_p, tree *pre_p, tree *post_p)
1639 {
1640   tree op1 = TREE_OPERAND (*expr_p, 0);
1641   tree op2 = TREE_OPERAND (*expr_p, 1);
1642   enum tree_code code;
1643   enum gimplify_status r0, r1;
1644
1645   if (TREE_CODE (*expr_p) == MIN_EXPR)
1646     code = LE_EXPR;
1647   else
1648     code = GE_EXPR;
1649
1650   r0 = gimplify_expr (&op1, pre_p, post_p, is_gimple_val, fb_rvalue);
1651   r1 = gimplify_expr (&op2, pre_p, post_p, is_gimple_val, fb_rvalue);
1652
1653   *expr_p = build (COND_EXPR, TREE_TYPE (*expr_p),
1654                    build (code, boolean_type_node, op1, op2),
1655                    op1, op2);
1656
1657   if (r0 == GS_ERROR || r1 == GS_ERROR)
1658     return GS_ERROR;
1659   else
1660     return GS_OK;
1661 }
1662
1663 /* Subroutine of gimplify_compound_lval and gimplify_array_ref.
1664    Converts an ARRAY_REF to the equivalent *(&array + offset) form.  */
1665
1666 static enum gimplify_status
1667 gimplify_array_ref_to_plus (tree *expr_p, tree *pre_p, tree *post_p)
1668 {
1669   tree array = TREE_OPERAND (*expr_p, 0);
1670   tree arrtype = TREE_TYPE (array);
1671   tree elttype = TREE_TYPE (arrtype);
1672   tree size = size_in_bytes (elttype);
1673   tree ptrtype = build_pointer_type (elttype);
1674   enum tree_code add_code = PLUS_EXPR;
1675   tree idx = TREE_OPERAND (*expr_p, 1);
1676   tree minidx, offset, addr, result;
1677   enum gimplify_status ret;
1678
1679   /* If the array domain does not start at zero, apply the offset.  */
1680   minidx = TYPE_DOMAIN (arrtype);
1681   if (minidx)
1682     {
1683       minidx = TYPE_MIN_VALUE (minidx);
1684       if (minidx && !integer_zerop (minidx))
1685         {
1686           idx = convert (TREE_TYPE (minidx), idx);
1687           idx = fold (build (MINUS_EXPR, TREE_TYPE (minidx), idx, minidx));
1688         }
1689     }
1690
1691   /* If the index is negative -- a technically invalid situation now
1692      that we've biased the index back to zero -- then casting it to
1693      unsigned has ill effects.  In particular, -1*4U/4U != -1.
1694      Represent this as a subtraction of a positive rather than addition
1695      of a negative.  This will prevent any conversion back to ARRAY_REF
1696      from getting the wrong results from the division.  */
1697   if (TREE_CODE (idx) == INTEGER_CST && tree_int_cst_sgn (idx) < 0)
1698     {
1699       idx = fold (build1 (NEGATE_EXPR, TREE_TYPE (idx), idx));
1700       add_code = MINUS_EXPR;
1701     }
1702
1703   /* Pointer arithmetic must be done in sizetype.  */
1704   idx = convert (sizetype, idx);
1705
1706   /* Convert the index to a byte offset.  */
1707   offset = size_binop (MULT_EXPR, size, idx);
1708
1709   ret = gimplify_expr (&array, pre_p, post_p, is_gimple_min_lval, fb_lvalue);
1710   if (ret == GS_ERROR)
1711     return ret;
1712
1713   addr = build_fold_addr_expr_with_type (array, ptrtype);
1714   result = fold (build (add_code, ptrtype, addr, offset));
1715   *expr_p = build1 (INDIRECT_REF, elttype, result);
1716
1717   return GS_OK;
1718 }
1719
1720 /* Gimplify the COMPONENT_REF, ARRAY_REF, REALPART_EXPR or IMAGPART_EXPR
1721    node pointed by EXPR_P.
1722
1723       compound_lval
1724               : min_lval '[' val ']'
1725               | min_lval '.' ID
1726               | compound_lval '[' val ']'
1727               | compound_lval '.' ID
1728
1729    This is not part of the original SIMPLE definition, which separates
1730    array and member references, but it seems reasonable to handle them
1731    together.  Also, this way we don't run into problems with union
1732    aliasing; gcc requires that for accesses through a union to alias, the
1733    union reference must be explicit, which was not always the case when we
1734    were splitting up array and member refs.
1735
1736    PRE_P points to the list where side effects that must happen before
1737      *EXPR_P should be stored.
1738
1739    POST_P points to the list where side effects that must happen after
1740      *EXPR_P should be stored.  */
1741
1742 static enum gimplify_status
1743 gimplify_compound_lval (tree *expr_p, tree *pre_p,
1744                         tree *post_p, int want_lvalue)
1745 {
1746   tree *p;
1747   enum tree_code code;
1748   varray_type stack;
1749   enum gimplify_status ret;
1750
1751 #if defined ENABLE_CHECKING
1752   if (TREE_CODE (*expr_p) != ARRAY_REF
1753       && TREE_CODE (*expr_p) != COMPONENT_REF
1754       && TREE_CODE (*expr_p) != REALPART_EXPR
1755       && TREE_CODE (*expr_p) != IMAGPART_EXPR)
1756     abort ();
1757 #endif
1758
1759   code = ERROR_MARK;    /* [GIMPLE] Avoid uninitialized use warning.  */
1760
1761   /* Create a stack of the subexpressions so later we can walk them in
1762      order from inner to outer.  */
1763   VARRAY_TREE_INIT (stack, 10, "stack");
1764
1765   for (p = expr_p;
1766        TREE_CODE (*p) == ARRAY_REF
1767        || TREE_CODE (*p) == COMPONENT_REF
1768        || TREE_CODE (*p) == REALPART_EXPR
1769        || TREE_CODE (*p) == IMAGPART_EXPR;
1770        p = &TREE_OPERAND (*p, 0))
1771     {
1772       code = TREE_CODE (*p);
1773       if (code == ARRAY_REF)
1774         {
1775           tree elttype = TREE_TYPE (TREE_TYPE (TREE_OPERAND (*p, 0)));
1776           if (!TREE_CONSTANT (TYPE_SIZE_UNIT (elttype)))
1777             /* If the size of the array elements is not constant,
1778                computing the offset is non-trivial, so expose it.  */
1779             break;
1780         }
1781       VARRAY_PUSH_TREE (stack, *p);
1782     }
1783
1784   /* Now 'p' points to the first bit that isn't a ref, 'code' is the
1785      TREE_CODE of the last bit that was, and 'stack' is a stack of pointers
1786      to all the refs we've walked through.
1787
1788      Gimplify the base, and then process each of the outer nodes from left
1789      to right.  */
1790   ret = gimplify_expr (p, pre_p, post_p, is_gimple_min_lval,
1791                        code != ARRAY_REF ? fb_either : fb_lvalue);
1792
1793   for (; VARRAY_ACTIVE_SIZE (stack) > 0; )
1794     {
1795       tree t = VARRAY_TOP_TREE (stack);
1796       if (TREE_CODE (t) == ARRAY_REF)
1797         {
1798           /* Gimplify the dimension.  */
1799           enum gimplify_status tret;
1800           /* Temporary fix for gcc.c-torture/execute/20040313-1.c.
1801              Gimplify non-constant array indices into a temporary
1802              variable.
1803              FIXME - The real fix is to gimplify post-modify
1804              expressions into a minimal gimple lvalue.  However, that
1805              exposes bugs in alias analysis.  The alias analyzer does
1806              not handle &PTR->FIELD very well.  Will fix after the
1807              branch is merged into mainline (dnovillo 2004-05-03).  */
1808           if (!is_gimple_min_invariant (TREE_OPERAND (t, 1)))
1809             {
1810               tret = gimplify_expr (&TREE_OPERAND (t, 1), pre_p, post_p,
1811                                     is_gimple_tmp_var, fb_rvalue);
1812               if (tret == GS_ERROR)
1813                 ret = GS_ERROR;
1814             }
1815         }
1816       recalculate_side_effects (t);
1817       VARRAY_POP (stack);
1818     }
1819
1820   /* If the outermost expression is a COMPONENT_REF, canonicalize its type.  */
1821   if (!want_lvalue && TREE_CODE (*expr_p) == COMPONENT_REF)
1822     {
1823       canonicalize_component_ref (expr_p);
1824       ret = MIN (ret, GS_OK);
1825     }
1826
1827   return ret;
1828 }
1829
1830 /*  Re-write the ARRAY_REF node pointed by EXPR_P.
1831
1832     PRE_P points to the list where side effects that must happen before
1833         *EXPR_P should be stored.
1834
1835     POST_P points to the list where side effects that must happen after
1836         *EXPR_P should be stored.
1837
1838     FIXME: ARRAY_REF currently doesn't accept a pointer as the array
1839     argument, so this gimplification uses an INDIRECT_REF of ARRAY_TYPE.
1840     ARRAY_REF should be extended.  */
1841
1842 static enum gimplify_status
1843 gimplify_array_ref (tree *expr_p, tree *pre_p,
1844                     tree *post_p, int want_lvalue)
1845 {
1846   tree elttype = TREE_TYPE (TREE_TYPE (TREE_OPERAND (*expr_p, 0)));
1847   if (!TREE_CONSTANT (TYPE_SIZE_UNIT (elttype)))
1848     /* If the size of the array elements is not constant,
1849        computing the offset is non-trivial, so expose it.  */
1850     return gimplify_array_ref_to_plus (expr_p, pre_p, post_p);
1851   else
1852     /* Handle array and member refs together for now.  When alias analysis
1853        improves, we may want to go back to handling them separately.  */
1854     return gimplify_compound_lval (expr_p, pre_p, post_p, want_lvalue);
1855 }
1856
1857 /*  Gimplify the self modifying expression pointed by EXPR_P (++, --, +=, -=).
1858
1859     PRE_P points to the list where side effects that must happen before
1860         *EXPR_P should be stored.
1861
1862     POST_P points to the list where side effects that must happen after
1863         *EXPR_P should be stored.
1864
1865     WANT_VALUE is nonzero iff we want to use the value of this expression
1866         in another expression.  */
1867
1868 static enum gimplify_status
1869 gimplify_self_mod_expr (tree *expr_p, tree *pre_p, tree *post_p,
1870                         int want_value)
1871 {
1872   enum tree_code code;
1873   tree lhs, lvalue, rhs, t1;
1874   bool postfix;
1875   enum tree_code arith_code;
1876   enum gimplify_status ret;
1877
1878   code = TREE_CODE (*expr_p);
1879
1880 #if defined ENABLE_CHECKING
1881   if (code != POSTINCREMENT_EXPR
1882       && code != POSTDECREMENT_EXPR
1883       && code != PREINCREMENT_EXPR
1884       && code != PREDECREMENT_EXPR)
1885     abort ();
1886 #endif
1887
1888   /* Prefix or postfix?  */
1889   if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
1890     /* Faster to treat as prefix if result is not used.  */
1891     postfix = want_value;
1892   else
1893     postfix = false;
1894
1895   /* Add or subtract?  */
1896   if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
1897     arith_code = PLUS_EXPR;
1898   else
1899     arith_code = MINUS_EXPR;
1900
1901   /* Gimplify the LHS into a GIMPLE lvalue.  */
1902   lvalue = TREE_OPERAND (*expr_p, 0);
1903   ret = gimplify_expr (&lvalue, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
1904   if (ret == GS_ERROR)
1905     return ret;
1906
1907   /* Extract the operands to the arithmetic operation.  */
1908   lhs = lvalue;
1909   rhs = TREE_OPERAND (*expr_p, 1);
1910
1911   /* For postfix operator, we evaluate the LHS to an rvalue and then use
1912      that as the result value and in the postqueue operation.  */
1913   if (postfix)
1914     {
1915       ret = gimplify_expr (&lhs, pre_p, post_p, is_gimple_val, fb_rvalue);
1916       if (ret == GS_ERROR)
1917         return ret;
1918     }
1919
1920   t1 = build (arith_code, TREE_TYPE (*expr_p), lhs, rhs);
1921   t1 = build (MODIFY_EXPR, TREE_TYPE (lvalue), lvalue, t1);
1922
1923   if (postfix)
1924     {
1925       gimplify_stmt (&t1);
1926       append_to_statement_list (t1, post_p);
1927       *expr_p = lhs;
1928       return GS_ALL_DONE;
1929     }
1930   else
1931     {
1932       *expr_p = t1;
1933       return GS_OK;
1934     }
1935 }
1936
1937 /*  Gimplify the CALL_EXPR node pointed by EXPR_P.
1938
1939       call_expr
1940               : ID '(' arglist ')'
1941
1942       arglist
1943               : arglist ',' val
1944               | val
1945
1946     PRE_P points to the list where side effects that must happen before
1947         *EXPR_P should be stored.  */
1948
1949 static enum gimplify_status
1950 gimplify_call_expr (tree *expr_p, tree *pre_p, bool (*gimple_test_f) (tree))
1951 {
1952   tree decl;
1953   tree arglist;
1954   enum gimplify_status ret;
1955
1956 #if defined ENABLE_CHECKING
1957   if (TREE_CODE (*expr_p) != CALL_EXPR)
1958     abort ();
1959 #endif
1960
1961   /* For reliable diagnostics during inlining, it is necessary that 
1962      every call_expr be annotated with file and line.  */
1963   if (!EXPR_LOCUS (*expr_p))
1964     annotate_with_locus (*expr_p, input_location);
1965
1966   /* This may be a call to a builtin function.
1967
1968      Builtin function calls may be transformed into different
1969      (and more efficient) builtin function calls under certain
1970      circumstances.  Unfortunately, gimplification can muck things
1971      up enough that the builtin expanders are not aware that certain
1972      transformations are still valid.
1973
1974      So we attempt transformation/gimplification of the call before
1975      we gimplify the CALL_EXPR.  At this time we do not manage to
1976      transform all calls in the same manner as the expanders do, but
1977      we do transform most of them.  */
1978   decl = get_callee_fndecl (*expr_p);
1979   if (decl && DECL_BUILT_IN (decl))
1980     {
1981       tree new;
1982
1983       /* If it is allocation of stack, record the need to restore the memory
1984          when the enclosing bind_expr is exited.  */
1985       if (DECL_FUNCTION_CODE (decl) == BUILT_IN_STACK_ALLOC)
1986         gimplify_ctxp->save_stack = true;
1987
1988       /* If it is restore of the stack, reset it, since it means we are
1989          regimplifying the bind_expr.  Note that we use the fact that
1990          for try_finally_expr, try part is processed first.  */
1991       if (DECL_FUNCTION_CODE (decl) == BUILT_IN_STACK_RESTORE)
1992         gimplify_ctxp->save_stack = false;
1993
1994       new = simplify_builtin (*expr_p, gimple_test_f == is_gimple_stmt);
1995
1996       if (new && new != *expr_p)
1997         {
1998           /* There was a transformation of this call which computes the
1999              same value, but in a more efficient way.  Return and try
2000              again.  */
2001           *expr_p = new;
2002           return GS_OK;
2003         }
2004     }
2005
2006   /* There is a sequence point before the call, so any side effects in
2007      the calling expression must occur before the actual call.  Force
2008      gimplify_expr to use an internal post queue.  */
2009   ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, NULL,
2010                        is_gimple_val, fb_rvalue);
2011
2012   if (PUSH_ARGS_REVERSED)
2013     TREE_OPERAND (*expr_p, 1) = nreverse (TREE_OPERAND (*expr_p, 1));
2014   for (arglist = TREE_OPERAND (*expr_p, 1); arglist;
2015        arglist = TREE_CHAIN (arglist))
2016     {
2017       enum gimplify_status t;
2018
2019       /* There is a sequence point before a function call.  Side effects in
2020          the argument list must occur before the actual call. So, when
2021          gimplifying arguments, force gimplify_expr to use an internal
2022          post queue which is then appended to the end of PRE_P.  */
2023       t = gimplify_expr (&TREE_VALUE (arglist), pre_p, NULL, is_gimple_val,
2024                          fb_rvalue);
2025
2026       if (t == GS_ERROR)
2027         ret = GS_ERROR;
2028     }
2029   if (PUSH_ARGS_REVERSED)
2030     TREE_OPERAND (*expr_p, 1) = nreverse (TREE_OPERAND (*expr_p, 1));
2031
2032   /* Try this again in case gimplification exposed something.  */
2033   if (ret != GS_ERROR && decl && DECL_BUILT_IN (decl))
2034     {
2035       tree new = simplify_builtin (*expr_p, gimple_test_f == is_gimple_stmt);
2036
2037       if (new && new != *expr_p)
2038         {
2039           /* There was a transformation of this call which computes the
2040              same value, but in a more efficient way.  Return and try
2041              again.  */
2042           *expr_p = new;
2043           return GS_OK;
2044         }
2045     }
2046
2047   /* If the function is "const" or "pure", then clear TREE_SIDE_EFFECTS on its
2048      decl.  This allows us to eliminate redundant or useless
2049      calls to "const" functions.  */
2050   if (TREE_CODE (*expr_p) == CALL_EXPR
2051       && (call_expr_flags (*expr_p) & (ECF_CONST | ECF_PURE)))
2052     TREE_SIDE_EFFECTS (*expr_p) = 0;
2053
2054   return ret;
2055 }
2056
2057 /* Handle shortcut semantics in the predicate operand of a COND_EXPR by
2058    rewriting it into multiple COND_EXPRs, and possibly GOTO_EXPRs.
2059
2060    TRUE_LABEL_P and FALSE_LABEL_P point to the labels to jump to if the
2061    condition is true or false, respectively.  If null, we should generate
2062    our own to skip over the evaluation of this specific expression.
2063
2064    This function is the tree equivalent of do_jump.
2065
2066    shortcut_cond_r should only be called by shortcut_cond_expr.  */
2067
2068 static tree
2069 shortcut_cond_r (tree pred, tree *true_label_p, tree *false_label_p)
2070 {
2071   tree local_label = NULL_TREE;
2072   tree t, expr = NULL;
2073
2074   /* OK, it's not a simple case; we need to pull apart the COND_EXPR to
2075      retain the shortcut semantics.  Just insert the gotos here;
2076      shortcut_cond_expr will append the real blocks later.  */
2077   if (TREE_CODE (pred) == TRUTH_ANDIF_EXPR)
2078     {
2079       /* Turn if (a && b) into
2080
2081          if (a); else goto no;
2082          if (b) goto yes; else goto no;
2083          (no:) */
2084
2085       if (false_label_p == NULL)
2086         false_label_p = &local_label;
2087
2088       t = shortcut_cond_r (TREE_OPERAND (pred, 0), NULL, false_label_p);
2089       append_to_statement_list (t, &expr);
2090
2091       t = shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p,
2092                            false_label_p);
2093       append_to_statement_list (t, &expr);
2094     }
2095   else if (TREE_CODE (pred) == TRUTH_ORIF_EXPR)
2096     {
2097       /* Turn if (a || b) into
2098
2099          if (a) goto yes;
2100          if (b) goto yes; else goto no;
2101          (yes:) */
2102
2103       if (true_label_p == NULL)
2104         true_label_p = &local_label;
2105
2106       t = shortcut_cond_r (TREE_OPERAND (pred, 0), true_label_p, NULL);
2107       append_to_statement_list (t, &expr);
2108
2109       t = shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p,
2110                            false_label_p);
2111       append_to_statement_list (t, &expr);
2112     }
2113   else if (TREE_CODE (pred) == COND_EXPR)
2114     {
2115       /* As long as we're messing with gotos, turn if (a ? b : c) into
2116          if (a)
2117            if (b) goto yes; else goto no;
2118          else
2119            if (c) goto yes; else goto no;  */
2120       expr = build (COND_EXPR, void_type_node, TREE_OPERAND (pred, 0),
2121                     shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p,
2122                                      false_label_p),
2123                     shortcut_cond_r (TREE_OPERAND (pred, 2), true_label_p,
2124                                      false_label_p));
2125     }
2126   else
2127     {
2128       expr = build (COND_EXPR, void_type_node, pred,
2129                     build_and_jump (true_label_p),
2130                     build_and_jump (false_label_p));
2131     }
2132
2133   if (local_label)
2134     {
2135       t = build1 (LABEL_EXPR, void_type_node, local_label);
2136       append_to_statement_list (t, &expr);
2137     }
2138
2139   return expr;
2140 }
2141
2142 static tree
2143 shortcut_cond_expr (tree expr)
2144 {
2145   tree pred = TREE_OPERAND (expr, 0);
2146   tree then_ = TREE_OPERAND (expr, 1);
2147   tree else_ = TREE_OPERAND (expr, 2);
2148   tree true_label, false_label, end_label, t;
2149   tree *true_label_p;
2150   tree *false_label_p;
2151   bool emit_end, emit_false;
2152
2153   /* First do simple transformations.  */
2154   if (!TREE_SIDE_EFFECTS (else_))
2155     {
2156       /* If there is no 'else', turn (a && b) into if (a) if (b).  */
2157       while (TREE_CODE (pred) == TRUTH_ANDIF_EXPR)
2158         {
2159           TREE_OPERAND (expr, 0) = TREE_OPERAND (pred, 1);
2160           then_ = shortcut_cond_expr (expr);
2161           pred = TREE_OPERAND (pred, 0);
2162           expr = build (COND_EXPR, void_type_node, pred, then_,
2163                         build_empty_stmt ());
2164         }
2165     }
2166   if (!TREE_SIDE_EFFECTS (then_))
2167     {
2168       /* If there is no 'then', turn
2169            if (a || b); else d
2170          into
2171            if (a); else if (b); else d.  */
2172       while (TREE_CODE (pred) == TRUTH_ORIF_EXPR)
2173         {
2174           TREE_OPERAND (expr, 0) = TREE_OPERAND (pred, 1);
2175           else_ = shortcut_cond_expr (expr);
2176           pred = TREE_OPERAND (pred, 0);
2177           expr = build (COND_EXPR, void_type_node, pred,
2178                         build_empty_stmt (), else_);
2179         }
2180     }
2181
2182   /* If we're done, great.  */
2183   if (TREE_CODE (pred) != TRUTH_ANDIF_EXPR
2184       && TREE_CODE (pred) != TRUTH_ORIF_EXPR)
2185     return expr;
2186
2187   /* Otherwise we need to mess with gotos.  Change
2188        if (a) c; else d;
2189      to
2190        if (a); else goto no;
2191        c; goto end;
2192        no: d; end:
2193      and recursively gimplify the condition.  */
2194
2195   true_label = false_label = end_label = NULL_TREE;
2196
2197   /* If our arms just jump somewhere, hijack those labels so we don't
2198      generate jumps to jumps.  */
2199
2200   if (TREE_CODE (then_) == GOTO_EXPR
2201       && TREE_CODE (GOTO_DESTINATION (then_)) == LABEL_DECL)
2202     {
2203       true_label = GOTO_DESTINATION (then_);
2204       then_ = build_empty_stmt ();
2205     }
2206
2207   if (TREE_CODE (else_) == GOTO_EXPR
2208       && TREE_CODE (GOTO_DESTINATION (else_)) == LABEL_DECL)
2209     {
2210       false_label = GOTO_DESTINATION (else_);
2211       else_ = build_empty_stmt ();
2212     }
2213
2214   /* If we aren't hijacking a label for the 'then' branch, it falls through.  */
2215   if (true_label)
2216     true_label_p = &true_label;
2217   else
2218     true_label_p = NULL;
2219
2220   /* The 'else' branch also needs a label if it contains interesting code.  */
2221   if (false_label || TREE_SIDE_EFFECTS (else_))
2222     false_label_p = &false_label;
2223   else
2224     false_label_p = NULL;
2225
2226   /* If there was nothing else in our arms, just forward the label(s).  */
2227   if (!TREE_SIDE_EFFECTS (then_) && !TREE_SIDE_EFFECTS (else_))
2228     return shortcut_cond_r (pred, true_label_p, false_label_p);
2229
2230   /* If our last subexpression already has a terminal label, reuse it.  */
2231   if (TREE_SIDE_EFFECTS (else_))
2232     expr = expr_last (else_);
2233   else
2234     expr = expr_last (then_);
2235   if (TREE_CODE (expr) == LABEL_EXPR)
2236     end_label = LABEL_EXPR_LABEL (expr);
2237
2238   /* If we don't care about jumping to the 'else' branch, jump to the end
2239      if the condition is false.  */
2240   if (!false_label_p)
2241     false_label_p = &end_label;
2242
2243   /* We only want to emit these labels if we aren't hijacking them.  */
2244   emit_end = (end_label == NULL_TREE);
2245   emit_false = (false_label == NULL_TREE);
2246
2247   pred = shortcut_cond_r (pred, true_label_p, false_label_p);
2248
2249   expr = NULL;
2250   append_to_statement_list (pred, &expr);
2251
2252   append_to_statement_list (then_, &expr);
2253   if (TREE_SIDE_EFFECTS (else_))
2254     {
2255       t = build_and_jump (&end_label);
2256       append_to_statement_list (t, &expr);
2257       if (emit_false)
2258         {
2259           t = build1 (LABEL_EXPR, void_type_node, false_label);
2260           append_to_statement_list (t, &expr);
2261         }
2262       append_to_statement_list (else_, &expr);
2263     }
2264   if (emit_end && end_label)
2265     {
2266       t = build1 (LABEL_EXPR, void_type_node, end_label);
2267       append_to_statement_list (t, &expr);
2268     }
2269
2270   return expr;
2271 }
2272
2273 /* EXPR is used in a boolean context; make sure it has BOOLEAN_TYPE.  */
2274
2275 static tree
2276 gimple_boolify (tree expr)
2277 {
2278   tree type = TREE_TYPE (expr);
2279
2280   if (TREE_CODE (type) == BOOLEAN_TYPE)
2281     return expr;
2282
2283   /* If this is the predicate of a COND_EXPR, it might not even be a
2284      truthvalue yet.  */
2285   expr = lang_hooks.truthvalue_conversion (expr);
2286
2287   switch (TREE_CODE (expr))
2288     {
2289     case TRUTH_AND_EXPR:
2290     case TRUTH_OR_EXPR:
2291     case TRUTH_XOR_EXPR:
2292     case TRUTH_ANDIF_EXPR:
2293     case TRUTH_ORIF_EXPR:
2294       /* Also boolify the arguments of truth exprs.  */
2295       TREE_OPERAND (expr, 1) = gimple_boolify (TREE_OPERAND (expr, 1));
2296       /* FALLTHRU */
2297
2298     case TRUTH_NOT_EXPR:
2299       TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
2300       /* FALLTHRU */
2301
2302     case EQ_EXPR: case NE_EXPR:
2303     case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
2304       /* These expressions always produce boolean results.  */
2305       TREE_TYPE (expr) = boolean_type_node;
2306       return expr;
2307       
2308     default:
2309       /* Other expressions that get here must have boolean values, but
2310          might need to be converted to the appropriate mode.  */
2311       return convert (boolean_type_node, expr);
2312     }
2313 }
2314
2315 /*  Convert the conditional expression pointed by EXPR_P '(p) ? a : b;'
2316     into
2317
2318     if (p)                      if (p)
2319       t1 = a;                     a;
2320     else                or      else
2321       t1 = b;                     b;
2322     t1;
2323
2324     The second form is used when *EXPR_P is of type void.
2325
2326     PRE_P points to the list where side effects that must happen before
2327         *EXPR_P should be stored.  */
2328
2329 static enum gimplify_status
2330 gimplify_cond_expr (tree *expr_p, tree *pre_p, tree target)
2331 {
2332   tree expr = *expr_p;
2333   tree tmp;
2334   enum gimplify_status ret;
2335
2336   /* If this COND_EXPR has a value, copy the values into a temporary within
2337      the arms.  */
2338   if (! VOID_TYPE_P (TREE_TYPE (expr)))
2339     {
2340       if (target)
2341         {
2342           tmp = target;
2343           ret = GS_OK;
2344         }
2345       else
2346         {
2347           tmp = create_tmp_var (TREE_TYPE (expr), "iftmp");
2348           ret = GS_ALL_DONE;
2349         }
2350
2351       /* Build the then clause, 't1 = a;'.  But don't build an assignment
2352          if this branch is void; in C++ it can be, if it's a throw.  */
2353       if (TREE_TYPE (TREE_OPERAND (expr, 1)) != void_type_node)
2354         TREE_OPERAND (expr, 1)
2355           = build (MODIFY_EXPR, void_type_node, tmp, TREE_OPERAND (expr, 1));
2356
2357       /* Build the else clause, 't1 = b;'.  */
2358       if (TREE_TYPE (TREE_OPERAND (expr, 2)) != void_type_node)
2359         TREE_OPERAND (expr, 2)
2360           = build (MODIFY_EXPR, void_type_node, tmp, TREE_OPERAND (expr, 2));
2361
2362       TREE_TYPE (expr) = void_type_node;
2363       recalculate_side_effects (expr);
2364
2365       /* Move the COND_EXPR to the prequeue and use the temp in its place.  */
2366       gimplify_stmt (&expr);
2367       append_to_statement_list (expr, pre_p);
2368       *expr_p = tmp;
2369
2370       return ret;
2371     }
2372
2373   /* Make sure the condition has BOOLEAN_TYPE.  */
2374   TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
2375
2376   /* Break apart && and || conditions.  */
2377   if (TREE_CODE (TREE_OPERAND (expr, 0)) == TRUTH_ANDIF_EXPR
2378       || TREE_CODE (TREE_OPERAND (expr, 0)) == TRUTH_ORIF_EXPR)
2379     {
2380       expr = shortcut_cond_expr (expr);
2381
2382       if (expr != *expr_p)
2383         {
2384           *expr_p = expr;
2385
2386           /* We can't rely on gimplify_expr to re-gimplify the expanded
2387              form properly, as cleanups might cause the target labels to be
2388              wrapped in a TRY_FINALLY_EXPR.  To prevent that, we need to
2389              set up a conditional context.  */
2390           gimple_push_condition ();
2391           gimplify_stmt (expr_p);
2392           gimple_pop_condition (pre_p);
2393
2394           return GS_ALL_DONE;
2395         }
2396     }
2397
2398   /* Now do the normal gimplification.  */
2399   ret = gimplify_expr (&TREE_OPERAND (expr, 0), pre_p, NULL,
2400                        is_gimple_condexpr, fb_rvalue);
2401
2402   gimple_push_condition ();
2403
2404   gimplify_to_stmt_list (&TREE_OPERAND (expr, 1));
2405   gimplify_to_stmt_list (&TREE_OPERAND (expr, 2));
2406   recalculate_side_effects (expr);
2407
2408   gimple_pop_condition (pre_p);
2409
2410   if (ret == GS_ERROR)
2411     ;
2412   else if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)))
2413     ret = GS_ALL_DONE;
2414   else if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 2)))
2415     /* Rewrite "if (a); else b" to "if (!a) b"  */
2416     {
2417       TREE_OPERAND (expr, 0) = invert_truthvalue (TREE_OPERAND (expr, 0));
2418       ret = gimplify_expr (&TREE_OPERAND (expr, 0), pre_p, NULL,
2419                            is_gimple_condexpr, fb_rvalue);
2420
2421       tmp = TREE_OPERAND (expr, 1);
2422       TREE_OPERAND (expr, 1) = TREE_OPERAND (expr, 2);
2423       TREE_OPERAND (expr, 2) = tmp;
2424     }
2425   else
2426     /* Both arms are empty; replace the COND_EXPR with its predicate.  */
2427     expr = TREE_OPERAND (expr, 0);
2428
2429   *expr_p = expr;
2430   return ret;
2431 }
2432
2433 /*  Gimplify the MODIFY_EXPR node pointed by EXPR_P.
2434
2435       modify_expr
2436               : varname '=' rhs
2437               | '*' ID '=' rhs
2438
2439     PRE_P points to the list where side effects that must happen before
2440         *EXPR_P should be stored.
2441
2442     POST_P points to the list where side effects that must happen after
2443         *EXPR_P should be stored.
2444
2445     WANT_VALUE is nonzero iff we want to use the value of this expression
2446         in another expression.  */
2447
2448 static enum gimplify_status
2449 gimplify_modify_expr (tree *expr_p, tree *pre_p, tree *post_p, bool want_value)
2450 {
2451   tree *from_p = &TREE_OPERAND (*expr_p, 1);
2452   tree *to_p = &TREE_OPERAND (*expr_p, 0);
2453   enum gimplify_status ret;
2454
2455 #if defined ENABLE_CHECKING
2456   if (TREE_CODE (*expr_p) != MODIFY_EXPR && TREE_CODE (*expr_p) != INIT_EXPR)
2457     abort ();
2458 #endif
2459
2460   /* The distinction between MODIFY_EXPR and INIT_EXPR is no longer useful.  */
2461   if (TREE_CODE (*expr_p) == INIT_EXPR)
2462     TREE_SET_CODE (*expr_p, MODIFY_EXPR);
2463
2464   ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
2465   if (ret == GS_ERROR)
2466     return ret;
2467
2468   /* If we are initializing something from a TARGET_EXPR, strip the
2469      TARGET_EXPR and initialize it directly.  */
2470   /* What about code that pulls out the temp and uses it elsewhere?  I
2471      think that such code never uses the TARGET_EXPR as an initializer.  If
2472      I'm wrong, we'll abort because the temp won't have any RTL.  In that
2473      case, I guess we'll need to replace references somehow.  */
2474   if (TREE_CODE (*from_p) == TARGET_EXPR)
2475     *from_p = TARGET_EXPR_INITIAL (*from_p);
2476
2477   /* If we're assigning from a ?: expression with ADDRESSABLE type, push
2478      the assignment down into the branches, since we can't generate a
2479      temporary of such a type.  */
2480   if (TREE_CODE (*from_p) == COND_EXPR
2481       && TREE_ADDRESSABLE (TREE_TYPE (*from_p)))
2482     {
2483       *expr_p = *from_p;
2484       return gimplify_cond_expr (expr_p, pre_p, *to_p);
2485     }
2486
2487   ret = gimplify_expr (from_p, pre_p, post_p, is_gimple_rhs, fb_rvalue);
2488   if (ret == GS_ERROR)
2489     return ret;
2490
2491   ret = gimplify_init_constructor (expr_p, pre_p, post_p, want_value);
2492   if (ret != GS_UNHANDLED)
2493     return ret;
2494
2495   /* If the destination is already simple, nothing else needed.  */
2496   if (is_gimple_tmp_var (*to_p))
2497     ret = GS_ALL_DONE;
2498   else
2499     {
2500       /* If the RHS of the MODIFY_EXPR may throw or make a nonlocal goto and
2501          the LHS is a user variable, then we need to introduce a temporary.
2502          ie temp = RHS; LHS = temp.
2503
2504          This way the optimizers can determine that the user variable is
2505          only modified if evaluation of the RHS does not throw.
2506
2507          FIXME this should be handled by the is_gimple_rhs predicate.  */
2508
2509       if (aggregate_value_p (TREE_TYPE (*from_p), NULL_TREE))
2510         /* Don't force a temp of a large aggregate type; the copy could be
2511            arbitrarily expensive.  Instead we will generate a V_MAY_DEF for
2512            the assignment.  */;
2513       else if (TREE_CODE (*from_p) == CALL_EXPR
2514                || (flag_non_call_exceptions && tree_could_trap_p (*from_p))
2515                /* If we're dealing with a renamable type, either source or dest
2516                   must be a renamed variable.  */
2517                || (is_gimple_reg_type (TREE_TYPE (*from_p))
2518                    && !is_gimple_reg (*to_p)))
2519         gimplify_expr (from_p, pre_p, post_p, is_gimple_val, fb_rvalue);
2520
2521       /* If the value being copied is of variable width, expose the length
2522          if the copy by converting the whole thing to a memcpy.  */
2523       /* ??? Except that we can't manage this with VA_ARG_EXPR.  Yes, this
2524          does leave us with an edge condition that doesn't work.  The only
2525          way out is to rearrange how VA_ARG_EXPR works.  */
2526       if (TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (*to_p))) != INTEGER_CST
2527           && TREE_CODE (*from_p) != VA_ARG_EXPR)
2528         {
2529           tree args, t, dest;
2530
2531           t = TYPE_SIZE_UNIT (TREE_TYPE (*to_p));
2532           t = unshare_expr (t);
2533           args = tree_cons (NULL, t, NULL);
2534           t = build_fold_addr_expr (*from_p);
2535           args = tree_cons (NULL, t, args);
2536           dest = build_fold_addr_expr (*to_p);
2537           args = tree_cons (NULL, dest, args);
2538           t = implicit_built_in_decls[BUILT_IN_MEMCPY];
2539           t = build_function_call_expr (t, args);
2540           if (want_value)
2541             {
2542               t = build1 (NOP_EXPR, TREE_TYPE (dest), t);
2543               t = build1 (INDIRECT_REF, TREE_TYPE (*to_p), t);
2544             }
2545           *expr_p = t;
2546
2547           return GS_OK;
2548         }
2549
2550       ret = want_value ? GS_OK : GS_ALL_DONE;
2551     }
2552
2553   if (want_value)
2554     {
2555       append_to_statement_list (*expr_p, pre_p);
2556       *expr_p = *to_p;
2557     }
2558
2559   return ret;
2560 }
2561
2562 /*  Gimplify TRUTH_ANDIF_EXPR and TRUTH_ORIF_EXPR expressions.  EXPR_P
2563     points to the expression to gimplify.
2564
2565     Expressions of the form 'a && b' are gimplified to:
2566
2567         a && b ? true : false
2568
2569     gimplify_cond_expr will do the rest.
2570
2571     PRE_P points to the list where side effects that must happen before
2572         *EXPR_P should be stored.  */
2573
2574 static enum gimplify_status
2575 gimplify_boolean_expr (tree *expr_p)
2576 {
2577   /* Preserve the original type of the expression.  */
2578   tree type = TREE_TYPE (*expr_p);
2579
2580   *expr_p = build (COND_EXPR, type, *expr_p,
2581                    convert (type, boolean_true_node),
2582                    convert (type, boolean_false_node));
2583
2584   return GS_OK;
2585 }
2586
2587 /* Gimplifies an expression sequence.  This function gimplifies each
2588    expression and re-writes the original expression with the last
2589    expression of the sequence in GIMPLE form.
2590
2591    PRE_P points to the list where the side effects for all the
2592        expressions in the sequence will be emitted.
2593     
2594    WANT_VALUE is true when the result of the last COMPOUND_EXPR is used.  */
2595 /* ??? Should rearrange to share the pre-queue with all the indirect
2596    invocations of gimplify_expr.  Would probably save on creations 
2597    of statement_list nodes.  */
2598
2599 static enum gimplify_status
2600 gimplify_compound_expr (tree *expr_p, tree *pre_p, bool want_value)
2601 {
2602   tree t = *expr_p;
2603
2604   do
2605     {
2606       tree *sub_p = &TREE_OPERAND (t, 0);
2607
2608       if (TREE_CODE (*sub_p) == COMPOUND_EXPR)
2609         gimplify_compound_expr (sub_p, pre_p, false);
2610       else
2611         gimplify_stmt (sub_p);
2612       append_to_statement_list (*sub_p, pre_p);
2613
2614       t = TREE_OPERAND (t, 1);
2615     }
2616   while (TREE_CODE (t) == COMPOUND_EXPR);
2617
2618   *expr_p = t;
2619   if (want_value)
2620     return GS_OK;
2621   else
2622     {
2623       gimplify_stmt (expr_p);
2624       return GS_ALL_DONE;
2625     }
2626 }
2627
2628 /* Gimplifies a statement list.  These may be created either by an
2629    enlightened front-end, or by shortcut_cond_expr.  */
2630
2631 static enum gimplify_status
2632 gimplify_statement_list (tree *expr_p)
2633 {
2634   tree_stmt_iterator i = tsi_start (*expr_p);
2635
2636   while (!tsi_end_p (i))
2637     {
2638       tree t;
2639
2640       gimplify_stmt (tsi_stmt_ptr (i));
2641
2642       t = tsi_stmt (i);
2643       if (TREE_CODE (t) == STATEMENT_LIST)
2644         {
2645           tsi_link_before (&i, t, TSI_SAME_STMT);
2646           tsi_delink (&i);
2647         }
2648       else
2649         tsi_next (&i);
2650     }
2651
2652   return GS_ALL_DONE;
2653 }
2654
2655 /*  Gimplify a SAVE_EXPR node.  EXPR_P points to the expression to
2656     gimplify.  After gimplification, EXPR_P will point to a new temporary
2657     that holds the original value of the SAVE_EXPR node.
2658
2659     PRE_P points to the list where side effects that must happen before
2660         *EXPR_P should be stored.  */
2661
2662 static enum gimplify_status
2663 gimplify_save_expr (tree *expr_p, tree *pre_p, tree *post_p)
2664 {
2665   enum gimplify_status ret = GS_ALL_DONE;
2666   tree val;
2667
2668 #if defined ENABLE_CHECKING
2669   if (TREE_CODE (*expr_p) != SAVE_EXPR)
2670     abort ();
2671 #endif
2672
2673   val = TREE_OPERAND (*expr_p, 0);
2674
2675   /* If the operand is already a GIMPLE temporary, just re-write the
2676      SAVE_EXPR node.  */
2677   if (is_gimple_tmp_var (val))
2678     *expr_p = val;
2679   /* The operand may be a void-valued expression such as SAVE_EXPRs
2680      generated by the Java frontend for class initialization.  It is
2681      being executed only for its side-effects.  */
2682   else if (TREE_TYPE (val) == void_type_node)
2683     {
2684       tree body = TREE_OPERAND (*expr_p, 0);
2685       ret = gimplify_expr (& body, pre_p, post_p, is_gimple_stmt, fb_none);
2686       append_to_statement_list (body, pre_p);
2687       *expr_p = build_empty_stmt ();
2688     }
2689   else
2690     *expr_p = TREE_OPERAND (*expr_p, 0)
2691       = get_initialized_tmp_var (val, pre_p, post_p);
2692
2693   return ret;
2694 }
2695
2696 /*  Re-write the ADDR_EXPR node pointed by EXPR_P
2697
2698       unary_expr
2699               : ...
2700               | '&' varname
2701               ...
2702
2703     PRE_P points to the list where side effects that must happen before
2704         *EXPR_P should be stored.
2705
2706     POST_P points to the list where side effects that must happen after
2707         *EXPR_P should be stored.  */
2708
2709 static enum gimplify_status
2710 gimplify_addr_expr (tree *expr_p, tree *pre_p, tree *post_p)
2711 {
2712   tree expr = *expr_p;
2713   tree op0 = TREE_OPERAND (expr, 0);
2714   enum gimplify_status ret;
2715
2716   switch (TREE_CODE (op0))
2717     {
2718     case INDIRECT_REF:
2719       /* Check if we are dealing with an expression of the form '&*ptr'.
2720          While the front end folds away '&*ptr' into 'ptr', these
2721          expressions may be generated internally by the compiler (e.g.,
2722          builtins like __builtin_va_end).  */
2723       *expr_p = TREE_OPERAND (op0, 0);
2724       ret = GS_OK;
2725       break;
2726
2727     case ARRAY_REF:
2728       /* Fold &a[6] to (&a + 6).  */
2729       ret = gimplify_array_ref_to_plus (&TREE_OPERAND (expr, 0),
2730                                         pre_p, post_p);
2731
2732       /* This added an INDIRECT_REF.  Fold it away.  */
2733       op0 = TREE_OPERAND (TREE_OPERAND (expr, 0), 0);
2734
2735       *expr_p = op0;
2736       break;
2737
2738     default:
2739       /* We use fb_either here because the C frontend sometimes takes
2740          the address of a call that returns a struct.  */
2741       ret = gimplify_expr (&TREE_OPERAND (expr, 0), pre_p, post_p,
2742                            is_gimple_addr_expr_arg, fb_either);
2743       if (ret != GS_ERROR)
2744         {
2745           /* At this point, the argument of the ADDR_EXPR should be
2746              sufficiently simple that there are never side effects.  */
2747           /* ??? Could split out the decision code from build1 to verify.  */
2748           TREE_SIDE_EFFECTS (expr) = 0;
2749
2750           /* Make sure TREE_INVARIANT/TREE_CONSTANT is set properly.  */
2751           recompute_tree_invarant_for_addr_expr (expr);
2752
2753           /* Mark the RHS addressable.  */
2754           lang_hooks.mark_addressable (TREE_OPERAND (expr, 0));
2755         }
2756       break;
2757     }
2758
2759   /* If the operand is gimplified into a _DECL, mark the address expression
2760      as TREE_INVARIANT.  */
2761   if (DECL_P (TREE_OPERAND (expr, 0)))
2762     TREE_INVARIANT (expr) = 1;
2763
2764   return ret;
2765 }
2766
2767 /* Gimplify the operands of an ASM_EXPR.  Input operands should be a gimple
2768    value; output operands should be a gimple lvalue.  */
2769
2770 static enum gimplify_status
2771 gimplify_asm_expr (tree *expr_p, tree *pre_p, tree *post_p)
2772 {
2773   tree expr = *expr_p;
2774   int noutputs = list_length (ASM_OUTPUTS (expr));
2775   const char **oconstraints
2776     = (const char **) alloca ((noutputs) * sizeof (const char *));
2777   int i;
2778   tree link;
2779   const char *constraint;
2780   bool allows_mem, allows_reg, is_inout;
2781   enum gimplify_status ret, tret;
2782
2783   ASM_STRING (expr)
2784     = resolve_asm_operand_names (ASM_STRING (expr), ASM_OUTPUTS (expr),
2785                                  ASM_INPUTS (expr));
2786
2787   ret = GS_ALL_DONE;
2788   for (i = 0, link = ASM_OUTPUTS (expr); link; ++i, link = TREE_CHAIN (link))
2789     {
2790       oconstraints[i] = constraint
2791         = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
2792
2793       parse_output_constraint (&constraint, i, 0, 0,
2794                                &allows_mem, &allows_reg, &is_inout);
2795
2796       if (!allows_reg && allows_mem)
2797         lang_hooks.mark_addressable (TREE_VALUE (link));
2798
2799       tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
2800                             is_inout ? is_gimple_min_lval : is_gimple_lvalue,
2801                             fb_lvalue | fb_mayfail);
2802       if (tret == GS_ERROR)
2803         {
2804           error ("invalid lvalue in asm output %d", i);
2805           ret = tret;
2806         }
2807
2808       if (is_inout)
2809         {
2810           /* An input/output operand.  To give the optimizers more
2811              flexibility, split it into separate input and output
2812              operands.  */
2813           tree input;
2814           char buf[10];
2815           size_t constraint_len = strlen (constraint);
2816
2817           /* Turn the in/out constraint into an output constraint.  */
2818           char *p = xstrdup (constraint);
2819           p[0] = '=';
2820           TREE_VALUE (TREE_PURPOSE (link)) = build_string (constraint_len, p);
2821           free (p);
2822
2823           /* And add a matching input constraint.  */
2824           if (allows_reg)
2825             {
2826               sprintf (buf, "%d", i);
2827               input = build_string (strlen (buf), buf);
2828             }
2829           else
2830             input = build_string (constraint_len - 1, constraint + 1);
2831           input = build_tree_list (build_tree_list (NULL_TREE, input),
2832                                    unshare_expr (TREE_VALUE (link)));
2833           ASM_INPUTS (expr) = chainon (ASM_INPUTS (expr), input);
2834         }
2835     }
2836
2837   for (link = ASM_INPUTS (expr); link; ++i, link = TREE_CHAIN (link))
2838     {
2839       constraint
2840         = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
2841       parse_input_constraint (&constraint, 0, 0, noutputs, 0,
2842                               oconstraints, &allows_mem, &allows_reg);
2843
2844       /* If the operand is a memory input, it should be an lvalue.  */
2845       if (!allows_reg && allows_mem)
2846         {
2847           lang_hooks.mark_addressable (TREE_VALUE (link));
2848           tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
2849                                 is_gimple_lvalue, fb_lvalue | fb_mayfail);
2850           if (tret == GS_ERROR)
2851             {
2852               error ("memory input %d is not directly addressable", i);
2853               ret = tret;
2854             }
2855         }
2856       else
2857         {
2858           tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
2859                                 is_gimple_val, fb_rvalue);
2860           if (tret == GS_ERROR)
2861             ret = tret;
2862         }
2863     }
2864
2865   return ret;
2866 }
2867
2868 /* Gimplify a CLEANUP_POINT_EXPR.  Currently this works by adding
2869    WITH_CLEANUP_EXPRs to the prequeue as we encounter cleanups while
2870    gimplifying the body, and converting them to TRY_FINALLY_EXPRs when we
2871    return to this function.
2872
2873    FIXME should we complexify the prequeue handling instead?  Or use flags
2874    for all the cleanups and let the optimizer tighten them up?  The current
2875    code seems pretty fragile; it will break on a cleanup within any
2876    non-conditional nesting.  But any such nesting would be broken, anyway;
2877    we can't write a TRY_FINALLY_EXPR that starts inside a nesting construct
2878    and continues out of it.  We can do that at the RTL level, though, so
2879    having an optimizer to tighten up try/finally regions would be a Good
2880    Thing.  */
2881
2882 static enum gimplify_status
2883 gimplify_cleanup_point_expr (tree *expr_p, tree *pre_p)
2884 {
2885   tree_stmt_iterator iter;
2886   tree body;
2887
2888   tree temp = voidify_wrapper_expr (*expr_p, NULL);
2889
2890   /* We only care about the number of conditions between the innermost
2891      CLEANUP_POINT_EXPR and the cleanup.  So save and reset the count.  */
2892   int old_conds = gimplify_ctxp->conditions;
2893   gimplify_ctxp->conditions = 0;
2894
2895   body = TREE_OPERAND (*expr_p, 0);
2896   gimplify_to_stmt_list (&body);
2897
2898   gimplify_ctxp->conditions = old_conds;
2899
2900   for (iter = tsi_start (body); !tsi_end_p (iter); )
2901     {
2902       tree *wce_p = tsi_stmt_ptr (iter);
2903       tree wce = *wce_p;
2904
2905       if (TREE_CODE (wce) == WITH_CLEANUP_EXPR)
2906         {
2907           if (tsi_one_before_end_p (iter))
2908             {
2909               tsi_link_before (&iter, TREE_OPERAND (wce, 1), TSI_SAME_STMT);
2910               tsi_delink (&iter);
2911               break;
2912             }
2913           else
2914             {
2915               tree sl, tfe;
2916
2917               sl = tsi_split_statement_list_after (&iter);
2918               tfe = build (TRY_FINALLY_EXPR, void_type_node, sl, NULL_TREE);
2919               append_to_statement_list (TREE_OPERAND (wce, 1),
2920                                      &TREE_OPERAND (tfe, 1));
2921               *wce_p = tfe;
2922               iter = tsi_start (sl);
2923             }
2924         }
2925       else
2926         tsi_next (&iter);
2927     }
2928
2929   if (temp)
2930     {
2931       *expr_p = temp;
2932       append_to_statement_list (body, pre_p);
2933       return GS_OK;
2934     }
2935   else
2936     {
2937       *expr_p = body;
2938       return GS_ALL_DONE;
2939     }
2940 }
2941
2942 /* Insert a cleanup marker for gimplify_cleanup_point_expr.  CLEANUP
2943    is the cleanup action required.  */
2944
2945 static void
2946 gimple_push_cleanup (tree var, tree cleanup, tree *pre_p)
2947 {
2948   tree wce;
2949
2950   /* Errors can result in improperly nested cleanups.  Which results in
2951      confusion when trying to resolve the WITH_CLEANUP_EXPR.  */
2952   if (errorcount || sorrycount)
2953     return;
2954
2955   if (gimple_conditional_context ())
2956     {
2957       /* If we're in a conditional context, this is more complex.  We only
2958          want to run the cleanup if we actually ran the initialization that
2959          necessitates it, but we want to run it after the end of the
2960          conditional context.  So we wrap the try/finally around the
2961          condition and use a flag to determine whether or not to actually
2962          run the destructor.  Thus
2963
2964            test ? f(A()) : 0
2965
2966          becomes (approximately)
2967
2968            flag = 0;
2969            try {
2970              if (test) { A::A(temp); flag = 1; val = f(temp); }
2971              else { val = 0; }
2972            } finally {
2973              if (flag) A::~A(temp);
2974            }
2975            val
2976       */
2977
2978       tree flag = create_tmp_var (boolean_type_node, "cleanup");
2979       tree ffalse = build (MODIFY_EXPR, void_type_node, flag,
2980                            boolean_false_node);
2981       tree ftrue = build (MODIFY_EXPR, void_type_node, flag,
2982                           boolean_true_node);
2983       cleanup = build (COND_EXPR, void_type_node, flag, cleanup,
2984                        build_empty_stmt ());
2985       wce = build (WITH_CLEANUP_EXPR, void_type_node, NULL_TREE,
2986                    cleanup, NULL_TREE);
2987       append_to_statement_list (ffalse, &gimplify_ctxp->conditional_cleanups);
2988       append_to_statement_list (wce, &gimplify_ctxp->conditional_cleanups);
2989       append_to_statement_list (ftrue, pre_p);
2990
2991       /* Because of this manipulation, and the EH edges that jump
2992          threading cannot redirect, the temporary (VAR) will appear
2993          to be used uninitialized.  Don't warn.  */
2994       TREE_NO_WARNING (var) = 1;
2995     }
2996   else
2997     {
2998       wce = build (WITH_CLEANUP_EXPR, void_type_node, NULL_TREE,
2999                    cleanup, NULL_TREE);
3000       append_to_statement_list (wce, pre_p);
3001     }
3002
3003   gimplify_stmt (&TREE_OPERAND (wce, 1));
3004 }
3005
3006 /* Gimplify a TARGET_EXPR which doesn't appear on the rhs of an INIT_EXPR.  */
3007
3008 static enum gimplify_status
3009 gimplify_target_expr (tree *expr_p, tree *pre_p, tree *post_p)
3010 {
3011   tree targ = *expr_p;
3012   tree temp = TARGET_EXPR_SLOT (targ);
3013   tree init = TARGET_EXPR_INITIAL (targ);
3014   enum gimplify_status ret;
3015
3016   if (init)
3017     {
3018       /* TARGET_EXPR temps aren't part of the enclosing block, so add it to the
3019          temps list.  */
3020       gimple_add_tmp_var (temp);
3021
3022       /* Build up the initialization and add it to pre_p.  Special handling
3023          for BIND_EXPR can result in fewer temporaries created.  */
3024       if (TREE_CODE (init) == BIND_EXPR)
3025         gimplify_bind_expr (&init, temp, pre_p);
3026       if (init != temp)
3027         {
3028           if (! VOID_TYPE_P (TREE_TYPE (init)))
3029             init = build (MODIFY_EXPR, void_type_node, temp, init);
3030           ret = gimplify_expr (&init, pre_p, post_p, is_gimple_stmt, fb_none);
3031           if (ret == GS_ERROR)
3032             return GS_ERROR;
3033         }
3034       append_to_statement_list (init, pre_p);
3035
3036       /* If needed, push the cleanup for the temp.  */
3037       if (TARGET_EXPR_CLEANUP (targ))
3038         {
3039           gimplify_stmt (&TARGET_EXPR_CLEANUP (targ));
3040           gimple_push_cleanup (temp, TARGET_EXPR_CLEANUP (targ), pre_p);
3041         }
3042
3043       /* Only expand this once.  */
3044       TREE_OPERAND (targ, 3) = init;
3045       TARGET_EXPR_INITIAL (targ) = NULL_TREE;
3046     }
3047   else if (!temp->decl.seen_in_bind_expr)
3048     /* We should have expanded this before.  */
3049     abort ();
3050
3051   *expr_p = temp;
3052   return GS_OK;
3053 }
3054
3055 /* Gimplification of expression trees.  */
3056
3057 /* Gimplify an expression which appears at statement context; usually, this
3058    means replacing it with a suitably gimple STATEMENT_LIST.  */
3059
3060 void
3061 gimplify_stmt (tree *stmt_p)
3062 {
3063   gimplify_expr (stmt_p, NULL, NULL, is_gimple_stmt, fb_none);
3064   if (!*stmt_p)
3065     *stmt_p = alloc_stmt_list ();
3066 }
3067
3068 /* Similarly, but force the result to be a STATEMENT_LIST.  */
3069
3070 void
3071 gimplify_to_stmt_list (tree *stmt_p)
3072 {
3073   gimplify_stmt (stmt_p);
3074   if (TREE_CODE (*stmt_p) != STATEMENT_LIST)
3075     {
3076       tree t = *stmt_p;
3077       *stmt_p = alloc_stmt_list ();
3078       append_to_statement_list (t, stmt_p);
3079     }
3080 }
3081
3082
3083 /*  Gimplifies the expression tree pointed by EXPR_P.  Return 0 if
3084     gimplification failed.
3085
3086     PRE_P points to the list where side effects that must happen before
3087         EXPR should be stored.
3088
3089     POST_P points to the list where side effects that must happen after
3090         EXPR should be stored, or NULL if there is no suitable list.  In
3091         that case, we copy the result to a temporary, emit the
3092         post-effects, and then return the temporary.
3093
3094     GIMPLE_TEST_F points to a function that takes a tree T and
3095         returns nonzero if T is in the GIMPLE form requested by the
3096         caller.  The GIMPLE predicates are in tree-gimple.c.
3097
3098         This test is used twice.  Before gimplification, the test is
3099         invoked to determine whether *EXPR_P is already gimple enough.  If
3100         that fails, *EXPR_P is gimplified according to its code and
3101         GIMPLE_TEST_F is called again.  If the test still fails, then a new
3102         temporary variable is created and assigned the value of the
3103         gimplified expression.
3104
3105     FALLBACK tells the function what sort of a temporary we want.  If the 1
3106         bit is set, an rvalue is OK.  If the 2 bit is set, an lvalue is OK.
3107         If both are set, either is OK, but an lvalue is preferable.
3108
3109     The return value is either GS_ERROR or GS_ALL_DONE, since this function
3110     iterates until solution.  */
3111
3112 enum gimplify_status
3113 gimplify_expr (tree *expr_p, tree *pre_p, tree *post_p,
3114                bool (* gimple_test_f) (tree), fallback_t fallback)
3115 {
3116   tree tmp;
3117   tree internal_pre = NULL_TREE;
3118   tree internal_post = NULL_TREE;
3119   tree save_expr;
3120   int is_statement = (pre_p == NULL);
3121   location_t *locus;
3122   location_t saved_location;
3123   enum gimplify_status ret;
3124
3125   save_expr = *expr_p;
3126   if (save_expr == NULL_TREE)
3127     return GS_ALL_DONE;
3128
3129   /* We used to check the predicate here and return immediately if it
3130      succeeds.  This is wrong; the design is for gimplification to be
3131      idempotent, and for the predicates to only test for valid forms, not
3132      whether they are fully simplified.  */
3133
3134   /* Set up our internal queues if needed.  */
3135   if (pre_p == NULL)
3136     pre_p = &internal_pre;
3137   if (post_p == NULL)
3138     post_p = &internal_post;
3139
3140   saved_location = input_location;
3141   if (save_expr == error_mark_node)
3142     locus = NULL;
3143   else
3144     locus = EXPR_LOCUS (save_expr);
3145   if (locus)
3146     input_location = *locus;
3147
3148   /* Loop over the specific gimplifiers until the toplevel node
3149      remains the same.  */
3150   do
3151     {
3152       /* Strip any uselessness.  */
3153       STRIP_MAIN_TYPE_NOPS (*expr_p);
3154
3155       /* Remember the expr.  */
3156       save_expr = *expr_p;
3157
3158       /* Die, die, die, my darling.  */
3159       if (save_expr == error_mark_node
3160           || TREE_TYPE (save_expr) == error_mark_node)
3161         {
3162           ret = GS_ERROR;
3163           break;
3164         }
3165
3166       /* Do any language-specific gimplification.  */
3167       ret = lang_hooks.gimplify_expr (expr_p, pre_p, post_p);
3168       if (ret == GS_OK)
3169         {
3170           if (*expr_p == NULL_TREE)
3171             break;
3172           if (*expr_p != save_expr)
3173             continue;
3174         }
3175       else if (ret != GS_UNHANDLED)
3176         break;
3177
3178       ret = GS_OK;
3179       switch (TREE_CODE (*expr_p))
3180         {
3181           /* First deal with the special cases.  */
3182
3183         case POSTINCREMENT_EXPR:
3184         case POSTDECREMENT_EXPR:
3185         case PREINCREMENT_EXPR:
3186         case PREDECREMENT_EXPR:
3187           ret = gimplify_self_mod_expr (expr_p, pre_p, post_p,
3188                                         fallback != fb_none);
3189           break;
3190
3191         case ARRAY_REF:
3192           ret = gimplify_array_ref (expr_p, pre_p, post_p,
3193                                     fallback & fb_lvalue);
3194           break;
3195
3196         case COMPONENT_REF:
3197           ret = gimplify_compound_lval (expr_p, pre_p, post_p,
3198                                         fallback & fb_lvalue);
3199           break;
3200
3201         case COND_EXPR:
3202           ret = gimplify_cond_expr (expr_p, pre_p, NULL_TREE);
3203           break;
3204
3205         case CALL_EXPR:
3206           ret = gimplify_call_expr (expr_p, pre_p, gimple_test_f);
3207           break;
3208
3209         case TREE_LIST:
3210           abort ();
3211
3212         case COMPOUND_EXPR:
3213           ret = gimplify_compound_expr (expr_p, pre_p, fallback != fb_none);
3214           break;
3215
3216         case REALPART_EXPR:
3217         case IMAGPART_EXPR:
3218           ret = gimplify_compound_lval (expr_p, pre_p, post_p,
3219                                         fallback & fb_lvalue);
3220           break;
3221
3222         case MODIFY_EXPR:
3223         case INIT_EXPR:
3224           ret = gimplify_modify_expr (expr_p, pre_p, post_p,
3225                                       fallback != fb_none);
3226           break;
3227
3228         case TRUTH_ANDIF_EXPR:
3229         case TRUTH_ORIF_EXPR:
3230           ret = gimplify_boolean_expr (expr_p);
3231           break;
3232
3233         case TRUTH_NOT_EXPR:
3234           TREE_OPERAND (*expr_p, 0)
3235             = gimple_boolify (TREE_OPERAND (*expr_p, 0));
3236           ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
3237                                is_gimple_val, fb_rvalue);
3238           recalculate_side_effects (*expr_p);
3239           break;
3240
3241         case ADDR_EXPR:
3242           ret = gimplify_addr_expr (expr_p, pre_p, post_p);
3243           break;
3244
3245         case VA_ARG_EXPR:
3246           ret = gimplify_va_arg_expr (expr_p, pre_p, post_p);
3247           break;
3248
3249         case CONVERT_EXPR:
3250         case NOP_EXPR:
3251           if (IS_EMPTY_STMT (*expr_p))
3252             {
3253               ret = GS_ALL_DONE;
3254               break;
3255             }
3256
3257           if (VOID_TYPE_P (TREE_TYPE (*expr_p))
3258               || fallback == fb_none)
3259             {
3260               /* Just strip a conversion to void (or in void context) and
3261                  try again.  */
3262               *expr_p = TREE_OPERAND (*expr_p, 0);
3263               break;
3264             }
3265
3266           ret = gimplify_conversion (expr_p);
3267           if (ret == GS_ERROR)
3268             break;
3269           if (*expr_p != save_expr)
3270             break;
3271           /* FALLTHRU */
3272
3273         case FIX_TRUNC_EXPR:
3274         case FIX_CEIL_EXPR:
3275         case FIX_FLOOR_EXPR:
3276         case FIX_ROUND_EXPR:
3277           /* unary_expr: ... | '(' cast ')' val | ...  */
3278           ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
3279                                is_gimple_val, fb_rvalue);
3280           recalculate_side_effects (*expr_p);
3281           break;
3282
3283         case INDIRECT_REF:
3284           ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
3285                                is_gimple_reg, fb_rvalue);
3286           recalculate_side_effects (*expr_p);
3287           break;
3288
3289           /* Constants need not be gimplified.  */
3290         case INTEGER_CST:
3291         case REAL_CST:
3292         case STRING_CST:
3293         case COMPLEX_CST:
3294         case VECTOR_CST:
3295           ret = GS_ALL_DONE;
3296           break;
3297
3298         case CONST_DECL:
3299           *expr_p = DECL_INITIAL (*expr_p);
3300           break;
3301
3302         case EXC_PTR_EXPR:
3303           /* FIXME make this a decl.  */
3304           ret = GS_ALL_DONE;
3305           break;
3306
3307         case BIND_EXPR:
3308           ret = gimplify_bind_expr (expr_p, NULL, pre_p);
3309           break;
3310
3311         case LOOP_EXPR:
3312           ret = gimplify_loop_expr (expr_p, pre_p);
3313           break;
3314
3315         case SWITCH_EXPR:
3316           ret = gimplify_switch_expr (expr_p, pre_p);
3317           break;
3318
3319         case LABELED_BLOCK_EXPR:
3320           ret = gimplify_labeled_block_expr (expr_p);
3321           break;
3322
3323         case EXIT_BLOCK_EXPR:
3324           ret = gimplify_exit_block_expr (expr_p);
3325           break;
3326
3327         case EXIT_EXPR:
3328           ret = gimplify_exit_expr (expr_p);
3329           break;
3330
3331         case GOTO_EXPR:
3332           /* If the target is not LABEL, then it is a computed jump
3333              and the target needs to be gimplified.  */
3334           if (TREE_CODE (GOTO_DESTINATION (*expr_p)) != LABEL_DECL)
3335             ret = gimplify_expr (&GOTO_DESTINATION (*expr_p), pre_p,
3336                                  NULL, is_gimple_val, fb_rvalue);
3337           break;
3338
3339         case LABEL_EXPR:
3340           ret = GS_ALL_DONE;
3341 #ifdef ENABLE_CHECKING
3342           if (decl_function_context (LABEL_EXPR_LABEL (*expr_p)) != current_function_decl)
3343             abort ();
3344 #endif
3345           break;
3346
3347         case CASE_LABEL_EXPR:
3348           ret = gimplify_case_label_expr (expr_p);
3349           break;
3350
3351         case RETURN_EXPR:
3352           ret = gimplify_return_expr (*expr_p, pre_p);
3353           break;
3354
3355         case CONSTRUCTOR:
3356           /* Don't reduce this in place; let gimplify_init_constructor work
3357              its magic.  */
3358           ret = GS_ALL_DONE;
3359           break;
3360
3361           /* The following are special cases that are not handled by the
3362              original GIMPLE grammar.  */
3363
3364           /* SAVE_EXPR nodes are converted into a GIMPLE identifier and
3365              eliminated.  */
3366         case SAVE_EXPR:
3367           ret = gimplify_save_expr (expr_p, pre_p, post_p);
3368           break;
3369
3370         case BIT_FIELD_REF:
3371           {
3372             enum gimplify_status r0, r1, r2;
3373
3374             r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
3375                                 is_gimple_min_lval, fb_either);
3376             r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
3377                                 is_gimple_val, fb_rvalue);
3378             r2 = gimplify_expr (&TREE_OPERAND (*expr_p, 2), pre_p, post_p,
3379                                 is_gimple_val, fb_rvalue);
3380             recalculate_side_effects (*expr_p);
3381
3382             ret = MIN (r0, MIN (r1, r2));
3383           }
3384           break;
3385
3386         case NON_LVALUE_EXPR:
3387           /* This should have been stripped above.  */
3388           abort ();
3389           break;
3390
3391         case ASM_EXPR:
3392           ret = gimplify_asm_expr (expr_p, pre_p, post_p);
3393           break;
3394
3395         case TRY_FINALLY_EXPR:
3396         case TRY_CATCH_EXPR:
3397           gimplify_to_stmt_list (&TREE_OPERAND (*expr_p, 0));
3398           gimplify_to_stmt_list (&TREE_OPERAND (*expr_p, 1));
3399           ret = GS_ALL_DONE;
3400           break;
3401
3402         case CLEANUP_POINT_EXPR:
3403           ret = gimplify_cleanup_point_expr (expr_p, pre_p);
3404           break;
3405
3406         case TARGET_EXPR:
3407           ret = gimplify_target_expr (expr_p, pre_p, post_p);
3408           break;
3409
3410         case CATCH_EXPR:
3411           gimplify_to_stmt_list (&CATCH_BODY (*expr_p));
3412           ret = GS_ALL_DONE;
3413           break;
3414
3415         case EH_FILTER_EXPR:
3416           gimplify_to_stmt_list (&EH_FILTER_FAILURE (*expr_p));
3417           ret = GS_ALL_DONE;
3418           break;
3419
3420         case VTABLE_REF:
3421           /* This moves much of the actual computation out of the
3422              VTABLE_REF.  Perhaps this should be revisited once we want to
3423              do clever things with VTABLE_REFs.  */
3424           ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
3425                                is_gimple_min_lval, fb_lvalue);
3426           break;
3427
3428         case MIN_EXPR:
3429         case MAX_EXPR:
3430           ret = gimplify_minimax_expr (expr_p, pre_p, post_p);
3431           break;
3432
3433         case LABEL_DECL:
3434           /* We get here when taking the address of a label.  We mark
3435              the label as "forced"; meaning it can never be removed and
3436              it is a potential target for any computed goto.  */
3437           FORCED_LABEL (*expr_p) = 1;
3438           ret = GS_ALL_DONE;
3439           break;
3440
3441         case STATEMENT_LIST:
3442           ret = gimplify_statement_list (expr_p);
3443           break;
3444
3445         case VAR_DECL:
3446           /* ??? If this is a local variable, and it has not been seen in any
3447              outer BIND_EXPR, then it's probably the result of a duplicate
3448              declaration, for which we've already issued an error.  It would
3449              be really nice if the front end wouldn't leak these at all. 
3450              Currently the only known culprit is C++ destructors, as seen
3451              in g++.old-deja/g++.jason/binding.C.  */
3452           tmp = *expr_p;
3453           if (!TREE_STATIC (tmp) && !DECL_EXTERNAL (tmp)
3454               && decl_function_context (tmp) == current_function_decl
3455               && !tmp->decl.seen_in_bind_expr)
3456             {
3457 #ifdef ENABLE_CHECKING
3458               if (!errorcount && !sorrycount)
3459                 abort ();
3460 #endif
3461               ret = GS_ERROR;
3462             }
3463           else
3464             ret = GS_ALL_DONE;
3465           break;
3466
3467         default:
3468           /* If *EXPR_P does not need to be special-cased, handle it
3469              according to its class.  */
3470           if (TREE_CODE_CLASS (TREE_CODE (*expr_p)) == '1')
3471             ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
3472                                  post_p, is_gimple_val, fb_rvalue);
3473           else if (TREE_CODE_CLASS (TREE_CODE (*expr_p)) == '2'
3474                    || TREE_CODE_CLASS (TREE_CODE (*expr_p)) == '<'
3475                    || TREE_CODE (*expr_p) == TRUTH_AND_EXPR
3476                    || TREE_CODE (*expr_p) == TRUTH_OR_EXPR
3477                    || TREE_CODE (*expr_p) == TRUTH_XOR_EXPR)
3478             {
3479               enum gimplify_status r0, r1;
3480
3481               r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
3482                                   post_p, is_gimple_val, fb_rvalue);
3483               r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
3484                                   post_p, is_gimple_val, fb_rvalue);
3485
3486               ret = MIN (r0, r1);
3487             }
3488           else if (TREE_CODE_CLASS (TREE_CODE (*expr_p)) == 'd'
3489                    || TREE_CODE_CLASS (TREE_CODE (*expr_p)) == 'c')
3490             {
3491               ret = GS_ALL_DONE;
3492               break;
3493             }
3494           else
3495             /* Fail if we don't know how to handle this tree code.  */
3496             abort ();
3497
3498           recalculate_side_effects (*expr_p);
3499           break;
3500         }
3501
3502       /* If we replaced *expr_p, gimplify again.  */
3503       if (ret == GS_OK && (*expr_p == NULL || *expr_p == save_expr))
3504         ret = GS_ALL_DONE;
3505     }
3506   while (ret == GS_OK);
3507
3508   /* If we encountered an error_mark somewhere nested inside, either
3509      stub out the statement or propagate the error back out.  */
3510   if (ret == GS_ERROR)
3511     {
3512       if (is_statement)
3513         *expr_p = build_empty_stmt ();
3514       goto out;
3515     }
3516
3517 #ifdef ENABLE_CHECKING
3518   /* This was only valid as a return value from the langhook, which
3519      we handled.  Make sure it doesn't escape from any other context.  */
3520   if (ret == GS_UNHANDLED)
3521     abort ();
3522 #endif
3523
3524   if (!*expr_p)
3525     *expr_p = build_empty_stmt ();
3526   if (fallback == fb_none && !is_gimple_stmt (*expr_p))
3527     {
3528       /* We aren't looking for a value, and we don't have a valid
3529          statement.  If it doesn't have side-effects, throw it away.  */
3530       if (!TREE_SIDE_EFFECTS (*expr_p))
3531         *expr_p = build_empty_stmt ();
3532       else if (!TREE_THIS_VOLATILE (*expr_p))
3533         /* We only handle volatiles here; anything else with side-effects
3534            must be converted to a valid statement before we get here.  */
3535         abort ();
3536       else if (COMPLETE_TYPE_P (TREE_TYPE (*expr_p)))
3537         {
3538           /* Historically, the compiler has treated a bare
3539              reference to a volatile lvalue as forcing a load.  */
3540           tree tmp = create_tmp_var (TREE_TYPE (*expr_p), "vol");
3541           *expr_p = build (MODIFY_EXPR, TREE_TYPE (tmp), tmp, *expr_p);
3542         }
3543       else
3544         /* We can't do anything useful with a volatile reference to
3545            incomplete type, so just throw it away.  */
3546         *expr_p = build_empty_stmt ();
3547     }
3548
3549   /* If we are gimplifying at the statement level, we're done.  Tack
3550      everything together and replace the original statement with the
3551      gimplified form.  */
3552   if (fallback == fb_none || is_statement)
3553     {
3554       if (internal_pre || internal_post)
3555         {
3556           append_to_statement_list (*expr_p, &internal_pre);
3557           append_to_statement_list (internal_post, &internal_pre);
3558           annotate_all_with_locus (&internal_pre, input_location);
3559           *expr_p = internal_pre;
3560         }
3561       goto out;
3562     }
3563
3564   /* Otherwise we're gimplifying a subexpression, so the resulting value is
3565      interesting.  */
3566
3567   /* If it's sufficiently simple already, we're done.  Unless we are
3568      handling some post-effects internally; if that's the case, we need to
3569      copy into a temp before adding the post-effects to the tree.  */
3570   if (!internal_post && (*gimple_test_f) (*expr_p))
3571     goto out;
3572
3573   /* Otherwise, we need to create a new temporary for the gimplified
3574      expression.  */
3575
3576   /* We can't return an lvalue if we have an internal postqueue.  The
3577      object the lvalue refers to would (probably) be modified by the
3578      postqueue; we need to copy the value out first, which means an
3579      rvalue.  */
3580   if ((fallback & fb_lvalue) && !internal_post
3581       && is_gimple_addr_expr_arg (*expr_p))
3582     {
3583       /* An lvalue will do.  Take the address of the expression, store it
3584          in a temporary, and replace the expression with an INDIRECT_REF of
3585          that temporary.  */
3586       tmp = build_fold_addr_expr (*expr_p);
3587       gimplify_expr (&tmp, pre_p, post_p, is_gimple_reg, fb_rvalue);
3588       *expr_p = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (tmp)), tmp);
3589     }
3590   else if ((fallback & fb_rvalue) && is_gimple_rhs (*expr_p))
3591     {
3592 #if defined ENABLE_CHECKING
3593       if (VOID_TYPE_P (TREE_TYPE (*expr_p)))
3594         abort ();
3595 #endif
3596
3597       /* An rvalue will do.  Assign the gimplified expression into a new
3598          temporary TMP and replace the original expression with TMP.  */
3599
3600       if (internal_post || (fallback & fb_lvalue))
3601         /* The postqueue might change the value of the expression between
3602            the initialization and use of the temporary, so we can't use a
3603            formal temp.  FIXME do we care?  */
3604         *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p);
3605       else
3606         *expr_p = get_formal_tmp_var (*expr_p, pre_p);
3607     }
3608   else if (fallback & fb_mayfail)
3609     {
3610       /* If this is an asm statement, and the user asked for the impossible,
3611          don't abort.  Fail and let gimplify_asm_expr issue an error.  */
3612       ret = GS_ERROR;
3613       goto out;
3614     }
3615   else
3616     {
3617       fprintf (stderr, "gimplification failed:\n");
3618       print_generic_expr (stderr, *expr_p, 0);
3619       debug_tree (*expr_p);
3620       abort ();
3621     }
3622
3623 #if defined ENABLE_CHECKING
3624   /* Make sure the temporary matches our predicate.  */
3625   if (!(*gimple_test_f) (*expr_p))
3626     abort ();
3627 #endif
3628
3629   if (internal_post)
3630     {
3631       annotate_all_with_locus (&internal_post, input_location);
3632       append_to_statement_list (internal_post, pre_p);
3633     }
3634
3635  out:
3636   input_location = saved_location;
3637   return ret;
3638 }
3639
3640 #ifdef ENABLE_CHECKING
3641 /* Compare types A and B for a "close enough" match.  */
3642
3643 static bool
3644 cpt_same_type (tree a, tree b)
3645 {
3646   if (lang_hooks.types_compatible_p (a, b))
3647     return true;
3648
3649   /* ??? The C++ FE decomposes METHOD_TYPES to FUNCTION_TYPES and doesn't
3650      link them together.  This routine is intended to catch type errors
3651      that will affect the optimizers, and the optimizers don't add new
3652      dereferences of function pointers, so ignore it.  */
3653   if ((TREE_CODE (a) == FUNCTION_TYPE || TREE_CODE (a) == METHOD_TYPE)
3654       && (TREE_CODE (b) == FUNCTION_TYPE || TREE_CODE (b) == METHOD_TYPE))
3655     return true;
3656
3657   /* ??? The C FE pushes type qualifiers after the fact into the type of
3658      the element from the type of the array.  See build_unary_op's handling
3659      of ADDR_EXPR.  This seems wrong -- if we were going to do this, we
3660      should have done it when creating the variable in the first place.
3661      Alternately, why aren't the two array types made variants?  */
3662   if (TREE_CODE (a) == ARRAY_TYPE && TREE_CODE (b) == ARRAY_TYPE)
3663     return cpt_same_type (TREE_TYPE (a), TREE_TYPE (b));
3664
3665   /* And because of those, we have to recurse down through pointers.  */
3666   if (POINTER_TYPE_P (a) && POINTER_TYPE_P (b))
3667     return cpt_same_type (TREE_TYPE (a), TREE_TYPE (b));
3668
3669   return false;
3670 }
3671
3672 /* Check for some cases of the front end missing cast expressions.
3673    The type of a dereference should correspond to the pointer type;
3674    similarly the type of an address should match its object.  */
3675
3676 static tree
3677 check_pointer_types_r (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
3678                        void *data ATTRIBUTE_UNUSED)
3679 {
3680   tree t = *tp;
3681   tree ptype, otype, dtype;
3682
3683   switch (TREE_CODE (t))
3684     {
3685     case INDIRECT_REF:
3686     case ARRAY_REF:
3687       otype = TREE_TYPE (t);
3688       ptype = TREE_TYPE (TREE_OPERAND (t, 0));
3689       dtype = TREE_TYPE (ptype);
3690       if (!cpt_same_type (otype, dtype))
3691         abort ();
3692       break;
3693
3694     case ADDR_EXPR:
3695       ptype = TREE_TYPE (t);
3696       otype = TREE_TYPE (TREE_OPERAND (t, 0));
3697       dtype = TREE_TYPE (ptype);
3698       if (!cpt_same_type (otype, dtype))
3699         {
3700           /* &array is allowed to produce a pointer to the element,
3701              rather than a pointer to the array type.  */
3702           if (TREE_CODE (otype) == ARRAY_TYPE
3703               && POINTER_TYPE_P (ptype)
3704               && cpt_same_type (TREE_TYPE (otype), dtype))
3705             break;
3706           abort ();
3707         }
3708       break;
3709
3710     default:
3711       return NULL_TREE;
3712     }
3713
3714
3715   return NULL_TREE;
3716 }
3717 #endif
3718
3719 /* Gimplify the body of statements pointed by BODY_P.  FNDECL is the
3720    function decl containing BODY.  */
3721
3722 void
3723 gimplify_body (tree *body_p, tree fndecl)
3724 {
3725   location_t saved_location = input_location;
3726   tree body;
3727
3728   timevar_push (TV_TREE_GIMPLIFY);
3729   push_gimplify_context ();
3730
3731   /* Unshare most shared trees in the body.  */
3732   unshare_all_trees (*body_p);
3733
3734   /* Make sure input_location isn't set to something wierd.  */
3735   input_location = DECL_SOURCE_LOCATION (fndecl);
3736
3737   /* Gimplify the function's body.  */
3738   gimplify_stmt (body_p);
3739   body = *body_p;
3740
3741   /* Unshare again, in case gimplification was sloppy.  */
3742   unshare_all_trees (body);
3743
3744   /* If there isn't an outer BIND_EXPR, add one.  */
3745   if (TREE_CODE (body) == STATEMENT_LIST)
3746     {
3747       tree t = expr_only (*body_p);
3748       if (t)
3749         body = t;
3750     }
3751   if (TREE_CODE (body) != BIND_EXPR)
3752     {
3753       tree b = build (BIND_EXPR, void_type_node, NULL_TREE,
3754                       NULL_TREE, NULL_TREE);
3755       TREE_SIDE_EFFECTS (b) = 1;
3756       append_to_statement_list_force (body, &BIND_EXPR_BODY (b));
3757       body = b;
3758     }
3759   *body_p = body;
3760
3761   pop_gimplify_context (body);
3762
3763 #ifdef ENABLE_CHECKING
3764   walk_tree (body_p, check_pointer_types_r, NULL, NULL);
3765 #endif
3766
3767   timevar_pop (TV_TREE_GIMPLIFY);
3768   input_location = saved_location;
3769 }
3770
3771 /* Entry point to the gimplification pass.  FNDECL is the FUNCTION_DECL
3772    node for the function we want to gimplify.  */
3773
3774 void
3775 gimplify_function_tree (tree fndecl)
3776 {
3777   tree oldfn;
3778
3779   oldfn = current_function_decl;
3780   current_function_decl = fndecl;
3781
3782   gimplify_body (&DECL_SAVED_TREE (fndecl), fndecl);
3783
3784   /* If we're instrumenting function entry/exit, then prepend the call to
3785      the entry hook and wrap the whole function in a TRY_FINALLY_EXPR to
3786      catch the exit hook.  */
3787   /* ??? Add some way to ignore exceptions for this TFE.  */
3788   if (flag_instrument_function_entry_exit
3789       && ! DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (fndecl))
3790     {
3791       tree tf, x, bind;
3792
3793       tf = build (TRY_FINALLY_EXPR, void_type_node, NULL, NULL);
3794       TREE_SIDE_EFFECTS (tf) = 1;
3795       x = DECL_SAVED_TREE (fndecl);
3796       append_to_statement_list (x, &TREE_OPERAND (tf, 0));
3797       x = implicit_built_in_decls[BUILT_IN_PROFILE_FUNC_EXIT];
3798       x = build_function_call_expr (x, NULL);
3799       append_to_statement_list (x, &TREE_OPERAND (tf, 1));
3800
3801       bind = build (BIND_EXPR, void_type_node, NULL, NULL, NULL);
3802       TREE_SIDE_EFFECTS (bind) = 1;
3803       x = implicit_built_in_decls[BUILT_IN_PROFILE_FUNC_ENTER];
3804       x = build_function_call_expr (x, NULL);
3805       append_to_statement_list (x, &BIND_EXPR_BODY (bind));
3806       append_to_statement_list (tf, &BIND_EXPR_BODY (bind));
3807
3808       DECL_SAVED_TREE (fndecl) = bind;
3809     }
3810
3811   current_function_decl = oldfn;
3812 }
3813
3814 #include "gt-gimplify.h"