OSDN Git Service

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