OSDN Git Service

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