OSDN Git Service

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