OSDN Git Service

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