OSDN Git Service

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