OSDN Git Service

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