OSDN Git Service

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