OSDN Git Service

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