OSDN Git Service

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