OSDN Git Service

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