OSDN Git Service

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