OSDN Git Service

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