OSDN Git Service

4e0e16aad1282334a115da906bc094ad49d90680
[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, 2005, 2006, 2007
4    Free Software Foundation, Inc.
5    Major work done by Sebastian Pop <s.pop@laposte.net>,
6    Diego Novillo <dnovillo@redhat.com> and Jason Merrill <jason@redhat.com>.
7
8 This file is part of GCC.
9
10 GCC is free software; you can redistribute it and/or modify it under
11 the terms of the GNU General Public License as published by the Free
12 Software Foundation; either version 3, or (at your option) any later
13 version.
14
15 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
16 WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
18 for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with GCC; see the file COPYING3.  If not see
22 <http://www.gnu.org/licenses/>.  */
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 "varray.h"
31 #include "tree-gimple.h"
32 #include "tree-inline.h"
33 #include "diagnostic.h"
34 #include "langhooks.h"
35 #include "langhooks-def.h"
36 #include "tree-flow.h"
37 #include "cgraph.h"
38 #include "timevar.h"
39 #include "except.h"
40 #include "hashtab.h"
41 #include "flags.h"
42 #include "real.h"
43 #include "function.h"
44 #include "output.h"
45 #include "expr.h"
46 #include "ggc.h"
47 #include "toplev.h"
48 #include "target.h"
49 #include "optabs.h"
50 #include "pointer-set.h"
51 #include "splay-tree.h"
52
53
54 enum gimplify_omp_var_data
55 {
56   GOVD_SEEN = 1,
57   GOVD_EXPLICIT = 2,
58   GOVD_SHARED = 4,
59   GOVD_PRIVATE = 8,
60   GOVD_FIRSTPRIVATE = 16,
61   GOVD_LASTPRIVATE = 32,
62   GOVD_REDUCTION = 64,
63   GOVD_LOCAL = 128,
64   GOVD_DEBUG_PRIVATE = 256,
65   GOVD_DATA_SHARE_CLASS = (GOVD_SHARED | GOVD_PRIVATE | GOVD_FIRSTPRIVATE
66                            | GOVD_LASTPRIVATE | GOVD_REDUCTION | GOVD_LOCAL)
67 };
68
69 struct gimplify_omp_ctx
70 {
71   struct gimplify_omp_ctx *outer_context;
72   splay_tree variables;
73   struct pointer_set_t *privatized_types;
74   location_t location;
75   enum omp_clause_default_kind default_kind;
76   bool is_parallel;
77   bool is_combined_parallel;
78 };
79
80 struct gimplify_ctx
81 {
82   struct gimplify_ctx *prev_context;
83
84   tree current_bind_expr;
85   tree temps;
86   tree conditional_cleanups;
87   tree exit_label;
88   tree return_temp;
89   
90   VEC(tree,heap) *case_labels;
91   /* The formal temporary table.  Should this be persistent?  */
92   htab_t temp_htab;
93
94   int conditions;
95   bool save_stack;
96   bool into_ssa;
97 };
98
99 static struct gimplify_ctx *gimplify_ctxp;
100 static struct gimplify_omp_ctx *gimplify_omp_ctxp;
101
102
103
104 /* Formal (expression) temporary table handling: Multiple occurrences of
105    the same scalar expression are evaluated into the same temporary.  */
106
107 typedef struct gimple_temp_hash_elt
108 {
109   tree val;   /* Key */
110   tree temp;  /* Value */
111 } elt_t;
112
113 /* Forward declarations.  */
114 static enum gimplify_status gimplify_compound_expr (tree *, tree *, bool);
115 #ifdef ENABLE_CHECKING
116 static bool cpt_same_type (tree a, tree b);
117 #endif
118
119 /* Mark X addressable.  Unlike the langhook we expect X to be in gimple
120    form and we don't do any syntax checking.  */
121 static void
122 mark_addressable (tree x)
123 {
124   while (handled_component_p (x))
125     x = TREE_OPERAND (x, 0);
126   if (TREE_CODE (x) != VAR_DECL && TREE_CODE (x) != PARM_DECL)
127     return ;
128   TREE_ADDRESSABLE (x) = 1;
129 }
130
131 /* Return a hash value for a formal temporary table entry.  */
132
133 static hashval_t
134 gimple_tree_hash (const void *p)
135 {
136   tree t = ((const elt_t *) p)->val;
137   return iterative_hash_expr (t, 0);
138 }
139
140 /* Compare two formal temporary table entries.  */
141
142 static int
143 gimple_tree_eq (const void *p1, const void *p2)
144 {
145   tree t1 = ((const elt_t *) p1)->val;
146   tree t2 = ((const elt_t *) p2)->val;
147   enum tree_code code = TREE_CODE (t1);
148
149   if (TREE_CODE (t2) != code
150       || TREE_TYPE (t1) != TREE_TYPE (t2))
151     return 0;
152
153   if (!operand_equal_p (t1, t2, 0))
154     return 0;
155
156   /* Only allow them to compare equal if they also hash equal; otherwise
157      results are nondeterminate, and we fail bootstrap comparison.  */
158   gcc_assert (gimple_tree_hash (p1) == gimple_tree_hash (p2));
159
160   return 1;
161 }
162
163 /* Set up a context for the gimplifier.  */
164
165 void
166 push_gimplify_context (void)
167 {
168   struct gimplify_ctx *c;
169
170   c = (struct gimplify_ctx *) xcalloc (1, sizeof (struct gimplify_ctx));
171   c->prev_context = gimplify_ctxp;
172   if (optimize)
173     c->temp_htab = htab_create (1000, gimple_tree_hash, gimple_tree_eq, free);
174
175   gimplify_ctxp = c;
176 }
177
178 /* Tear down a context for the gimplifier.  If BODY is non-null, then
179    put the temporaries into the outer BIND_EXPR.  Otherwise, put them
180    in the unexpanded_var_list.  */
181
182 void
183 pop_gimplify_context (tree body)
184 {
185   struct gimplify_ctx *c = gimplify_ctxp;
186   tree t;
187
188   gcc_assert (c && !c->current_bind_expr);
189   gimplify_ctxp = c->prev_context;
190
191   for (t = c->temps; t ; t = TREE_CHAIN (t))
192     DECL_GIMPLE_FORMAL_TEMP_P (t) = 0;
193
194   if (body)
195     declare_vars (c->temps, body, false);
196   else
197     record_vars (c->temps);
198
199   if (optimize)
200     htab_delete (c->temp_htab);
201   free (c);
202 }
203
204 static void
205 gimple_push_bind_expr (tree bind)
206 {
207   TREE_CHAIN (bind) = gimplify_ctxp->current_bind_expr;
208   gimplify_ctxp->current_bind_expr = bind;
209 }
210
211 static void
212 gimple_pop_bind_expr (void)
213 {
214   gimplify_ctxp->current_bind_expr
215     = TREE_CHAIN (gimplify_ctxp->current_bind_expr);
216 }
217
218 tree
219 gimple_current_bind_expr (void)
220 {
221   return gimplify_ctxp->current_bind_expr;
222 }
223
224 /* Returns true iff there is a COND_EXPR between us and the innermost
225    CLEANUP_POINT_EXPR.  This info is used by gimple_push_cleanup.  */
226
227 static bool
228 gimple_conditional_context (void)
229 {
230   return gimplify_ctxp->conditions > 0;
231 }
232
233 /* Note that we've entered a COND_EXPR.  */
234
235 static void
236 gimple_push_condition (void)
237 {
238 #ifdef ENABLE_CHECKING
239   if (gimplify_ctxp->conditions == 0)
240     gcc_assert (!gimplify_ctxp->conditional_cleanups);
241 #endif
242   ++(gimplify_ctxp->conditions);
243 }
244
245 /* Note that we've left a COND_EXPR.  If we're back at unconditional scope
246    now, add any conditional cleanups we've seen to the prequeue.  */
247
248 static void
249 gimple_pop_condition (tree *pre_p)
250 {
251   int conds = --(gimplify_ctxp->conditions);
252
253   gcc_assert (conds >= 0);
254   if (conds == 0)
255     {
256       append_to_statement_list (gimplify_ctxp->conditional_cleanups, pre_p);
257       gimplify_ctxp->conditional_cleanups = NULL_TREE;
258     }
259 }
260
261 /* A stable comparison routine for use with splay trees and DECLs.  */
262
263 static int
264 splay_tree_compare_decl_uid (splay_tree_key xa, splay_tree_key xb)
265 {
266   tree a = (tree) xa;
267   tree b = (tree) xb;
268
269   return DECL_UID (a) - DECL_UID (b);
270 }
271
272 /* Create a new omp construct that deals with variable remapping.  */
273
274 static struct gimplify_omp_ctx *
275 new_omp_context (bool is_parallel, bool is_combined_parallel)
276 {
277   struct gimplify_omp_ctx *c;
278
279   c = XCNEW (struct gimplify_omp_ctx);
280   c->outer_context = gimplify_omp_ctxp;
281   c->variables = splay_tree_new (splay_tree_compare_decl_uid, 0, 0);
282   c->privatized_types = pointer_set_create ();
283   c->location = input_location;
284   c->is_parallel = is_parallel;
285   c->is_combined_parallel = is_combined_parallel;
286   c->default_kind = OMP_CLAUSE_DEFAULT_SHARED;
287
288   return c;
289 }
290
291 /* Destroy an omp construct that deals with variable remapping.  */
292
293 static void
294 delete_omp_context (struct gimplify_omp_ctx *c)
295 {
296   splay_tree_delete (c->variables);
297   pointer_set_destroy (c->privatized_types);
298   XDELETE (c);
299 }
300
301 static void omp_add_variable (struct gimplify_omp_ctx *, tree, unsigned int);
302 static bool omp_notice_variable (struct gimplify_omp_ctx *, tree, bool);
303
304 /* A subroutine of append_to_statement_list{,_force}.  T is not NULL.  */
305
306 static void
307 append_to_statement_list_1 (tree t, tree *list_p)
308 {
309   tree list = *list_p;
310   tree_stmt_iterator i;
311
312   if (!list)
313     {
314       if (t && TREE_CODE (t) == STATEMENT_LIST)
315         {
316           *list_p = t;
317           return;
318         }
319       *list_p = list = alloc_stmt_list ();
320     }
321
322   i = tsi_last (list);
323   tsi_link_after (&i, t, TSI_CONTINUE_LINKING);
324 }
325
326 /* Add T to the end of the list container pointed to by LIST_P.
327    If T is an expression with no effects, it is ignored.  */
328
329 void
330 append_to_statement_list (tree t, tree *list_p)
331 {
332   if (t && TREE_SIDE_EFFECTS (t))
333     append_to_statement_list_1 (t, list_p);
334 }
335
336 /* Similar, but the statement is always added, regardless of side effects.  */
337
338 void
339 append_to_statement_list_force (tree t, tree *list_p)
340 {
341   if (t != NULL_TREE)
342     append_to_statement_list_1 (t, list_p);
343 }
344
345 /* Both gimplify the statement T and append it to LIST_P.  */
346
347 void
348 gimplify_and_add (tree t, tree *list_p)
349 {
350   gimplify_stmt (&t);
351   append_to_statement_list (t, list_p);
352 }
353
354 /* Strip off a legitimate source ending from the input string NAME of
355    length LEN.  Rather than having to know the names used by all of
356    our front ends, we strip off an ending of a period followed by
357    up to five characters.  (Java uses ".class".)  */
358
359 static inline void
360 remove_suffix (char *name, int len)
361 {
362   int i;
363
364   for (i = 2;  i < 8 && len > i;  i++)
365     {
366       if (name[len - i] == '.')
367         {
368           name[len - i] = '\0';
369           break;
370         }
371     }
372 }
373
374 /* Create a nameless artificial label and put it in the current function
375    context.  Returns the newly created label.  */
376
377 tree
378 create_artificial_label (void)
379 {
380   tree lab = build_decl (LABEL_DECL, NULL_TREE, void_type_node);
381
382   DECL_ARTIFICIAL (lab) = 1;
383   DECL_IGNORED_P (lab) = 1;
384   DECL_CONTEXT (lab) = current_function_decl;
385   return lab;
386 }
387
388 /* Subroutine for find_single_pointer_decl.  */
389
390 static tree
391 find_single_pointer_decl_1 (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
392                             void *data)
393 {
394   tree *pdecl = (tree *) data;
395
396   if (DECL_P (*tp) && POINTER_TYPE_P (TREE_TYPE (*tp)))
397     {
398       if (*pdecl)
399         {
400           /* We already found a pointer decl; return anything other
401              than NULL_TREE to unwind from walk_tree signalling that
402              we have a duplicate.  */
403           return *tp;
404         }
405       *pdecl = *tp;
406     }
407
408   return NULL_TREE;
409 }
410
411 /* Find the single DECL of pointer type in the tree T and return it.
412    If there are zero or more than one such DECLs, return NULL.  */
413
414 static tree
415 find_single_pointer_decl (tree t)
416 {
417   tree decl = NULL_TREE;
418
419   if (walk_tree (&t, find_single_pointer_decl_1, &decl, NULL))
420     {
421       /* find_single_pointer_decl_1 returns a nonzero value, causing
422          walk_tree to return a nonzero value, to indicate that it
423          found more than one pointer DECL.  */
424       return NULL_TREE;
425     }
426
427   return decl;
428 }
429
430 /* Create a new temporary name with PREFIX.  Returns an identifier.  */
431
432 static GTY(()) unsigned int tmp_var_id_num;
433
434 tree
435 create_tmp_var_name (const char *prefix)
436 {
437   char *tmp_name;
438
439   if (prefix)
440     {
441       char *preftmp = ASTRDUP (prefix);
442
443       remove_suffix (preftmp, strlen (preftmp));
444       prefix = preftmp;
445     }
446
447   ASM_FORMAT_PRIVATE_NAME (tmp_name, prefix ? prefix : "T", tmp_var_id_num++);
448   return get_identifier (tmp_name);
449 }
450
451
452 /* Create a new temporary variable declaration of type TYPE.
453    Does NOT push it into the current binding.  */
454
455 tree
456 create_tmp_var_raw (tree type, const char *prefix)
457 {
458   tree tmp_var;
459   tree new_type;
460
461   /* Make the type of the variable writable.  */
462   new_type = build_type_variant (type, 0, 0);
463   TYPE_ATTRIBUTES (new_type) = TYPE_ATTRIBUTES (type);
464
465   tmp_var = build_decl (VAR_DECL, prefix ? create_tmp_var_name (prefix) : NULL,
466                         type);
467
468   /* The variable was declared by the compiler.  */
469   DECL_ARTIFICIAL (tmp_var) = 1;
470   /* And we don't want debug info for it.  */
471   DECL_IGNORED_P (tmp_var) = 1;
472
473   /* Make the variable writable.  */
474   TREE_READONLY (tmp_var) = 0;
475
476   DECL_EXTERNAL (tmp_var) = 0;
477   TREE_STATIC (tmp_var) = 0;
478   TREE_USED (tmp_var) = 1;
479
480   return tmp_var;
481 }
482
483 /* Create a new temporary variable declaration of type TYPE.  DOES push the
484    variable into the current binding.  Further, assume that this is called
485    only from gimplification or optimization, at which point the creation of
486    certain types are bugs.  */
487
488 tree
489 create_tmp_var (tree type, const char *prefix)
490 {
491   tree tmp_var;
492
493   /* We don't allow types that are addressable (meaning we can't make copies),
494      or incomplete.  We also used to reject every variable size objects here,
495      but now support those for which a constant upper bound can be obtained.
496      The processing for variable sizes is performed in gimple_add_tmp_var,
497      point at which it really matters and possibly reached via paths not going
498      through this function, e.g. after direct calls to create_tmp_var_raw.  */
499   gcc_assert (!TREE_ADDRESSABLE (type) && COMPLETE_TYPE_P (type));
500
501   tmp_var = create_tmp_var_raw (type, prefix);
502   gimple_add_tmp_var (tmp_var);
503   return tmp_var;
504 }
505
506 /*  Given a tree, try to return a useful variable name that we can use
507     to prefix a temporary that is being assigned the value of the tree.
508     I.E. given  <temp> = &A, return A.  */
509
510 const char *
511 get_name (tree t)
512 {
513   tree stripped_decl;
514
515   stripped_decl = t;
516   STRIP_NOPS (stripped_decl);
517   if (DECL_P (stripped_decl) && DECL_NAME (stripped_decl))
518     return IDENTIFIER_POINTER (DECL_NAME (stripped_decl));
519   else
520     {
521       switch (TREE_CODE (stripped_decl))
522         {
523         case ADDR_EXPR:
524           return get_name (TREE_OPERAND (stripped_decl, 0));
525         default:
526           return NULL;
527         }
528     }
529 }
530
531 /* Create a temporary with a name derived from VAL.  Subroutine of
532    lookup_tmp_var; nobody else should call this function.  */
533
534 static inline tree
535 create_tmp_from_val (tree val)
536 {
537   return create_tmp_var (TYPE_MAIN_VARIANT (TREE_TYPE (val)), get_name (val));
538 }
539
540 /* Create a temporary to hold the value of VAL.  If IS_FORMAL, try to reuse
541    an existing expression temporary.  */
542
543 static tree
544 lookup_tmp_var (tree val, bool is_formal)
545 {
546   tree ret;
547
548   /* If not optimizing, never really reuse a temporary.  local-alloc
549      won't allocate any variable that is used in more than one basic
550      block, which means it will go into memory, causing much extra
551      work in reload and final and poorer code generation, outweighing
552      the extra memory allocation here.  */
553   if (!optimize || !is_formal || TREE_SIDE_EFFECTS (val))
554     ret = create_tmp_from_val (val);
555   else
556     {
557       elt_t elt, *elt_p;
558       void **slot;
559
560       elt.val = val;
561       slot = htab_find_slot (gimplify_ctxp->temp_htab, (void *)&elt, INSERT);
562       if (*slot == NULL)
563         {
564           elt_p = XNEW (elt_t);
565           elt_p->val = val;
566           elt_p->temp = ret = create_tmp_from_val (val);
567           *slot = (void *) elt_p;
568         }
569       else
570         {
571           elt_p = (elt_t *) *slot;
572           ret = elt_p->temp;
573         }
574     }
575
576   if (is_formal)
577     DECL_GIMPLE_FORMAL_TEMP_P (ret) = 1;
578
579   return ret;
580 }
581
582 /* Returns a formal temporary variable initialized with VAL.  PRE_P is as
583    in gimplify_expr.  Only use this function if:
584
585    1) The value of the unfactored expression represented by VAL will not
586       change between the initialization and use of the temporary, and
587    2) The temporary will not be otherwise modified.
588
589    For instance, #1 means that this is inappropriate for SAVE_EXPR temps,
590    and #2 means it is inappropriate for && temps.
591
592    For other cases, use get_initialized_tmp_var instead.  */
593
594 static tree
595 internal_get_tmp_var (tree val, tree *pre_p, tree *post_p, bool is_formal)
596 {
597   tree t, mod;
598
599   gimplify_expr (&val, pre_p, post_p, is_gimple_formal_tmp_rhs, fb_rvalue);
600
601   t = lookup_tmp_var (val, is_formal);
602
603   if (is_formal)
604     {
605       tree u = find_single_pointer_decl (val);
606
607       if (u && TREE_CODE (u) == VAR_DECL && DECL_BASED_ON_RESTRICT_P (u))
608         u = DECL_GET_RESTRICT_BASE (u);
609       if (u && TYPE_RESTRICT (TREE_TYPE (u)))
610         {
611           if (DECL_BASED_ON_RESTRICT_P (t))
612             gcc_assert (u == DECL_GET_RESTRICT_BASE (t));
613           else
614             {
615               DECL_BASED_ON_RESTRICT_P (t) = 1;
616               SET_DECL_RESTRICT_BASE (t, u);
617             }
618         }
619     }
620
621   if (TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE
622       || TREE_CODE (TREE_TYPE (t)) == VECTOR_TYPE)
623     DECL_GIMPLE_REG_P (t) = 1;
624
625   mod = build2 (INIT_EXPR, TREE_TYPE (t), t, unshare_expr (val));
626
627   if (EXPR_HAS_LOCATION (val))
628     SET_EXPR_LOCUS (mod, EXPR_LOCUS (val));
629   else
630     SET_EXPR_LOCATION (mod, input_location);
631
632   /* gimplify_modify_expr might want to reduce this further.  */
633   gimplify_and_add (mod, pre_p);
634
635   /* If we're gimplifying into ssa, gimplify_modify_expr will have
636      given our temporary an ssa name.  Find and return it.  */
637   if (gimplify_ctxp->into_ssa)
638     t = TREE_OPERAND (mod, 0);
639
640   return t;
641 }
642
643 /* Returns a formal temporary variable initialized with VAL.  PRE_P
644    points to a statement list where side-effects needed to compute VAL
645    should be stored.  */
646
647 tree
648 get_formal_tmp_var (tree val, tree *pre_p)
649 {
650   return internal_get_tmp_var (val, pre_p, NULL, true);
651 }
652
653 /* Returns a temporary variable initialized with VAL.  PRE_P and POST_P
654    are as in gimplify_expr.  */
655
656 tree
657 get_initialized_tmp_var (tree val, tree *pre_p, tree *post_p)
658 {
659   return internal_get_tmp_var (val, pre_p, post_p, false);
660 }
661
662 /* Declares all the variables in VARS in SCOPE.  If DEBUG_INFO is
663    true, generate debug info for them; otherwise don't.  */
664
665 void
666 declare_vars (tree vars, tree scope, bool debug_info)
667 {
668   tree last = vars;
669   if (last)
670     {
671       tree temps, block;
672
673       /* C99 mode puts the default 'return 0;' for main outside the outer
674          braces.  So drill down until we find an actual scope.  */
675       while (TREE_CODE (scope) == COMPOUND_EXPR)
676         scope = TREE_OPERAND (scope, 0);
677
678       gcc_assert (TREE_CODE (scope) == BIND_EXPR);
679
680       temps = nreverse (last);
681
682       block = BIND_EXPR_BLOCK (scope);
683       if (!block || !debug_info)
684         {
685           TREE_CHAIN (last) = BIND_EXPR_VARS (scope);
686           BIND_EXPR_VARS (scope) = temps;
687         }
688       else
689         {
690           /* We need to attach the nodes both to the BIND_EXPR and to its
691              associated BLOCK for debugging purposes.  The key point here
692              is that the BLOCK_VARS of the BIND_EXPR_BLOCK of a BIND_EXPR
693              is a subchain of the BIND_EXPR_VARS of the BIND_EXPR.  */
694           if (BLOCK_VARS (block))
695             BLOCK_VARS (block) = chainon (BLOCK_VARS (block), temps);
696           else
697             {
698               BIND_EXPR_VARS (scope) = chainon (BIND_EXPR_VARS (scope), temps);
699               BLOCK_VARS (block) = temps;
700             }
701         }
702     }
703 }
704
705 /* For VAR a VAR_DECL of variable size, try to find a constant upper bound
706    for the size and adjust DECL_SIZE/DECL_SIZE_UNIT accordingly.  Abort if
707    no such upper bound can be obtained.  */
708
709 static void
710 force_constant_size (tree var)
711 {
712   /* The only attempt we make is by querying the maximum size of objects
713      of the variable's type.  */
714
715   HOST_WIDE_INT max_size;
716
717   gcc_assert (TREE_CODE (var) == VAR_DECL);
718
719   max_size = max_int_size_in_bytes (TREE_TYPE (var));
720
721   gcc_assert (max_size >= 0);
722
723   DECL_SIZE_UNIT (var)
724     = build_int_cst (TREE_TYPE (DECL_SIZE_UNIT (var)), max_size);
725   DECL_SIZE (var)
726     = build_int_cst (TREE_TYPE (DECL_SIZE (var)), max_size * BITS_PER_UNIT);
727 }
728
729 void
730 gimple_add_tmp_var (tree tmp)
731 {
732   gcc_assert (!TREE_CHAIN (tmp) && !DECL_SEEN_IN_BIND_EXPR_P (tmp));
733
734   /* Later processing assumes that the object size is constant, which might
735      not be true at this point.  Force the use of a constant upper bound in
736      this case.  */
737   if (!host_integerp (DECL_SIZE_UNIT (tmp), 1))
738     force_constant_size (tmp);
739
740   DECL_CONTEXT (tmp) = current_function_decl;
741   DECL_SEEN_IN_BIND_EXPR_P (tmp) = 1;
742
743   if (gimplify_ctxp)
744     {
745       TREE_CHAIN (tmp) = gimplify_ctxp->temps;
746       gimplify_ctxp->temps = tmp;
747
748       /* Mark temporaries local within the nearest enclosing parallel.  */
749       if (gimplify_omp_ctxp)
750         {
751           struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
752           while (ctx && !ctx->is_parallel)
753             ctx = ctx->outer_context;
754           if (ctx)
755             omp_add_variable (ctx, tmp, GOVD_LOCAL | GOVD_SEEN);
756         }
757     }
758   else if (cfun)
759     record_vars (tmp);
760   else
761     declare_vars (tmp, DECL_SAVED_TREE (current_function_decl), false);
762 }
763
764 /* Determines whether to assign a locus to the statement STMT.  */
765
766 static bool
767 should_carry_locus_p (tree stmt)
768 {
769   /* Don't emit a line note for a label.  We particularly don't want to
770      emit one for the break label, since it doesn't actually correspond
771      to the beginning of the loop/switch.  */
772   if (TREE_CODE (stmt) == LABEL_EXPR)
773     return false;
774
775   /* Do not annotate empty statements, since it confuses gcov.  */
776   if (!TREE_SIDE_EFFECTS (stmt))
777     return false;
778
779   return true;
780 }
781
782 static void
783 annotate_one_with_locus (tree t, location_t locus)
784 {
785   if (CAN_HAVE_LOCATION_P (t)
786       && ! EXPR_HAS_LOCATION (t) && should_carry_locus_p (t))
787     SET_EXPR_LOCATION (t, locus);
788 }
789
790 void
791 annotate_all_with_locus (tree *stmt_p, location_t locus)
792 {
793   tree_stmt_iterator i;
794
795   if (!*stmt_p)
796     return;
797
798   for (i = tsi_start (*stmt_p); !tsi_end_p (i); tsi_next (&i))
799     {
800       tree t = tsi_stmt (i);
801
802       /* Assuming we've already been gimplified, we shouldn't
803           see nested chaining constructs anymore.  */
804       gcc_assert (TREE_CODE (t) != STATEMENT_LIST
805                   && TREE_CODE (t) != COMPOUND_EXPR);
806
807       annotate_one_with_locus (t, locus);
808     }
809 }
810
811 /* Similar to copy_tree_r() but do not copy SAVE_EXPR or TARGET_EXPR nodes.
812    These nodes model computations that should only be done once.  If we
813    were to unshare something like SAVE_EXPR(i++), the gimplification
814    process would create wrong code.  */
815
816 static tree
817 mostly_copy_tree_r (tree *tp, int *walk_subtrees, void *data)
818 {
819   enum tree_code code = TREE_CODE (*tp);
820   /* Don't unshare types, decls, constants and SAVE_EXPR nodes.  */
821   if (TREE_CODE_CLASS (code) == tcc_type
822       || TREE_CODE_CLASS (code) == tcc_declaration
823       || TREE_CODE_CLASS (code) == tcc_constant
824       || code == SAVE_EXPR || code == TARGET_EXPR
825       /* We can't do anything sensible with a BLOCK used as an expression,
826          but we also can't just die when we see it because of non-expression
827          uses.  So just avert our eyes and cross our fingers.  Silly Java.  */
828       || code == BLOCK)
829     *walk_subtrees = 0;
830   else
831     {
832       gcc_assert (code != BIND_EXPR);
833       copy_tree_r (tp, walk_subtrees, data);
834     }
835
836   return NULL_TREE;
837 }
838
839 /* Callback for walk_tree to unshare most of the shared trees rooted at
840    *TP.  If *TP has been visited already (i.e., TREE_VISITED (*TP) == 1),
841    then *TP is deep copied by calling copy_tree_r.
842
843    This unshares the same trees as copy_tree_r with the exception of
844    SAVE_EXPR nodes.  These nodes model computations that should only be
845    done once.  If we were to unshare something like SAVE_EXPR(i++), the
846    gimplification process would create wrong code.  */
847
848 static tree
849 copy_if_shared_r (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
850                   void *data ATTRIBUTE_UNUSED)
851 {
852   tree t = *tp;
853   enum tree_code code = TREE_CODE (t);
854
855   /* Skip types, decls, and constants.  But we do want to look at their
856      types and the bounds of types.  Mark them as visited so we properly
857      unmark their subtrees on the unmark pass.  If we've already seen them,
858      don't look down further.  */
859   if (TREE_CODE_CLASS (code) == tcc_type
860       || TREE_CODE_CLASS (code) == tcc_declaration
861       || TREE_CODE_CLASS (code) == tcc_constant)
862     {
863       if (TREE_VISITED (t))
864         *walk_subtrees = 0;
865       else
866         TREE_VISITED (t) = 1;
867     }
868
869   /* If this node has been visited already, unshare it and don't look
870      any deeper.  */
871   else if (TREE_VISITED (t))
872     {
873       walk_tree (tp, mostly_copy_tree_r, NULL, NULL);
874       *walk_subtrees = 0;
875     }
876
877   /* Otherwise, mark the tree as visited and keep looking.  */
878   else
879     TREE_VISITED (t) = 1;
880
881   return NULL_TREE;
882 }
883
884 static tree
885 unmark_visited_r (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
886                   void *data ATTRIBUTE_UNUSED)
887 {
888   if (TREE_VISITED (*tp))
889     TREE_VISITED (*tp) = 0;
890   else
891     *walk_subtrees = 0;
892
893   return NULL_TREE;
894 }
895
896 /* Unshare all the trees in BODY_P, a pointer into the body of FNDECL, and the
897    bodies of any nested functions if we are unsharing the entire body of
898    FNDECL.  */
899
900 static void
901 unshare_body (tree *body_p, tree fndecl)
902 {
903   struct cgraph_node *cgn = cgraph_node (fndecl);
904
905   walk_tree (body_p, copy_if_shared_r, NULL, NULL);
906   if (body_p == &DECL_SAVED_TREE (fndecl))
907     for (cgn = cgn->nested; cgn; cgn = cgn->next_nested)
908       unshare_body (&DECL_SAVED_TREE (cgn->decl), cgn->decl);
909 }
910
911 /* Likewise, but mark all trees as not visited.  */
912
913 static void
914 unvisit_body (tree *body_p, tree fndecl)
915 {
916   struct cgraph_node *cgn = cgraph_node (fndecl);
917
918   walk_tree (body_p, unmark_visited_r, NULL, NULL);
919   if (body_p == &DECL_SAVED_TREE (fndecl))
920     for (cgn = cgn->nested; cgn; cgn = cgn->next_nested)
921       unvisit_body (&DECL_SAVED_TREE (cgn->decl), cgn->decl);
922 }
923
924 /* Unshare T and all the trees reached from T via TREE_CHAIN.  */
925
926 static void
927 unshare_all_trees (tree t)
928 {
929   walk_tree (&t, copy_if_shared_r, NULL, NULL);
930   walk_tree (&t, unmark_visited_r, NULL, NULL);
931 }
932
933 /* Unconditionally make an unshared copy of EXPR.  This is used when using
934    stored expressions which span multiple functions, such as BINFO_VTABLE,
935    as the normal unsharing process can't tell that they're shared.  */
936
937 tree
938 unshare_expr (tree expr)
939 {
940   walk_tree (&expr, mostly_copy_tree_r, NULL, NULL);
941   return expr;
942 }
943
944 /* A terser interface for building a representation of an exception
945    specification.  */
946
947 tree
948 gimple_build_eh_filter (tree body, tree allowed, tree failure)
949 {
950   tree t;
951
952   /* FIXME should the allowed types go in TREE_TYPE?  */
953   t = build2 (EH_FILTER_EXPR, void_type_node, allowed, NULL_TREE);
954   append_to_statement_list (failure, &EH_FILTER_FAILURE (t));
955
956   t = build2 (TRY_CATCH_EXPR, void_type_node, NULL_TREE, t);
957   append_to_statement_list (body, &TREE_OPERAND (t, 0));
958
959   return t;
960 }
961
962 \f
963 /* WRAPPER is a code such as BIND_EXPR or CLEANUP_POINT_EXPR which can both
964    contain statements and have a value.  Assign its value to a temporary
965    and give it void_type_node.  Returns the temporary, or NULL_TREE if
966    WRAPPER was already void.  */
967
968 tree
969 voidify_wrapper_expr (tree wrapper, tree temp)
970 {
971   tree type = TREE_TYPE (wrapper);
972   if (type && !VOID_TYPE_P (type))
973     {
974       tree *p;
975
976       /* Set p to point to the body of the wrapper.  Loop until we find
977          something that isn't a wrapper.  */
978       for (p = &wrapper; p && *p; )
979         {
980           switch (TREE_CODE (*p))
981             {
982             case BIND_EXPR:
983               TREE_SIDE_EFFECTS (*p) = 1;
984               TREE_TYPE (*p) = void_type_node;
985               /* For a BIND_EXPR, the body is operand 1.  */
986               p = &BIND_EXPR_BODY (*p);
987               break;
988
989             case CLEANUP_POINT_EXPR:
990             case TRY_FINALLY_EXPR:
991             case TRY_CATCH_EXPR:
992               TREE_SIDE_EFFECTS (*p) = 1;
993               TREE_TYPE (*p) = void_type_node;
994               p = &TREE_OPERAND (*p, 0);
995               break;
996
997             case STATEMENT_LIST:
998               {
999                 tree_stmt_iterator i = tsi_last (*p);
1000                 TREE_SIDE_EFFECTS (*p) = 1;
1001                 TREE_TYPE (*p) = void_type_node;
1002                 p = tsi_end_p (i) ? NULL : tsi_stmt_ptr (i);
1003               }
1004               break;
1005
1006             case COMPOUND_EXPR:
1007               /* Advance to the last statement.  Set all container types to void.  */
1008               for (; TREE_CODE (*p) == COMPOUND_EXPR; p = &TREE_OPERAND (*p, 1))
1009                 {
1010                   TREE_SIDE_EFFECTS (*p) = 1;
1011                   TREE_TYPE (*p) = void_type_node;
1012                 }
1013               break;
1014
1015             default:
1016               goto out;
1017             }
1018         }
1019
1020     out:
1021       if (p == NULL || IS_EMPTY_STMT (*p))
1022         temp = NULL_TREE;
1023       else if (temp)
1024         {
1025           /* The wrapper is on the RHS of an assignment that we're pushing
1026              down.  */
1027           gcc_assert (TREE_CODE (temp) == INIT_EXPR
1028                       || TREE_CODE (temp) == GIMPLE_MODIFY_STMT
1029                       || TREE_CODE (temp) == MODIFY_EXPR);
1030           GENERIC_TREE_OPERAND (temp, 1) = *p;
1031           *p = temp;
1032         }
1033       else
1034         {
1035           temp = create_tmp_var (type, "retval");
1036           *p = build2 (INIT_EXPR, type, temp, *p);
1037         }
1038
1039       return temp;
1040     }
1041
1042   return NULL_TREE;
1043 }
1044
1045 /* Prepare calls to builtins to SAVE and RESTORE the stack as well as
1046    a temporary through which they communicate.  */
1047
1048 static void
1049 build_stack_save_restore (tree *save, tree *restore)
1050 {
1051   tree save_call, tmp_var;
1052
1053   save_call =
1054     build_call_expr (implicit_built_in_decls[BUILT_IN_STACK_SAVE], 0);
1055   tmp_var = create_tmp_var (ptr_type_node, "saved_stack");
1056
1057   *save = build_gimple_modify_stmt (tmp_var, save_call);
1058   *restore =
1059     build_call_expr (implicit_built_in_decls[BUILT_IN_STACK_RESTORE],
1060                      1, tmp_var);
1061 }
1062
1063 /* Gimplify a BIND_EXPR.  Just voidify and recurse.  */
1064
1065 static enum gimplify_status
1066 gimplify_bind_expr (tree *expr_p, tree *pre_p)
1067 {
1068   tree bind_expr = *expr_p;
1069   bool old_save_stack = gimplify_ctxp->save_stack;
1070   tree t;
1071
1072   tree temp = voidify_wrapper_expr (bind_expr, NULL);
1073
1074   /* Mark variables seen in this bind expr.  */
1075   for (t = BIND_EXPR_VARS (bind_expr); t ; t = TREE_CHAIN (t))
1076     {
1077       if (TREE_CODE (t) == VAR_DECL)
1078         {
1079           struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
1080
1081           /* Mark variable as local.  */
1082           if (ctx && !is_global_var (t)
1083               && (! DECL_SEEN_IN_BIND_EXPR_P (t)
1084                   || splay_tree_lookup (ctx->variables,
1085                                         (splay_tree_key) t) == NULL))
1086             omp_add_variable (gimplify_omp_ctxp, t, GOVD_LOCAL | GOVD_SEEN);
1087
1088           DECL_SEEN_IN_BIND_EXPR_P (t) = 1;
1089         }
1090
1091       /* Preliminarily mark non-addressed complex variables as eligible
1092          for promotion to gimple registers.  We'll transform their uses
1093          as we find them.  */
1094       if ((TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE
1095            || TREE_CODE (TREE_TYPE (t)) == VECTOR_TYPE)
1096           && !TREE_THIS_VOLATILE (t)
1097           && (TREE_CODE (t) == VAR_DECL && !DECL_HARD_REGISTER (t))
1098           && !needs_to_live_in_memory (t))
1099         DECL_GIMPLE_REG_P (t) = 1;
1100     }
1101
1102   gimple_push_bind_expr (bind_expr);
1103   gimplify_ctxp->save_stack = false;
1104
1105   gimplify_to_stmt_list (&BIND_EXPR_BODY (bind_expr));
1106
1107   if (gimplify_ctxp->save_stack)
1108     {
1109       tree stack_save, stack_restore;
1110
1111       /* Save stack on entry and restore it on exit.  Add a try_finally
1112          block to achieve this.  Note that mudflap depends on the
1113          format of the emitted code: see mx_register_decls().  */
1114       build_stack_save_restore (&stack_save, &stack_restore);
1115
1116       t = build2 (TRY_FINALLY_EXPR, void_type_node,
1117                   BIND_EXPR_BODY (bind_expr), NULL_TREE);
1118       append_to_statement_list (stack_restore, &TREE_OPERAND (t, 1));
1119
1120       BIND_EXPR_BODY (bind_expr) = NULL_TREE;
1121       append_to_statement_list (stack_save, &BIND_EXPR_BODY (bind_expr));
1122       append_to_statement_list (t, &BIND_EXPR_BODY (bind_expr));
1123     }
1124
1125   gimplify_ctxp->save_stack = old_save_stack;
1126   gimple_pop_bind_expr ();
1127
1128   if (temp)
1129     {
1130       *expr_p = temp;
1131       append_to_statement_list (bind_expr, pre_p);
1132       return GS_OK;
1133     }
1134   else
1135     return GS_ALL_DONE;
1136 }
1137
1138 /* Gimplify a RETURN_EXPR.  If the expression to be returned is not a
1139    GIMPLE value, it is assigned to a new temporary and the statement is
1140    re-written to return the temporary.
1141
1142    PRE_P points to the list where side effects that must happen before
1143    STMT should be stored.  */
1144
1145 static enum gimplify_status
1146 gimplify_return_expr (tree stmt, tree *pre_p)
1147 {
1148   tree ret_expr = TREE_OPERAND (stmt, 0);
1149   tree result_decl, result;
1150
1151   if (!ret_expr || TREE_CODE (ret_expr) == RESULT_DECL
1152       || ret_expr == error_mark_node)
1153     return GS_ALL_DONE;
1154
1155   if (VOID_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl))))
1156     result_decl = NULL_TREE;
1157   else
1158     {
1159       result_decl = GENERIC_TREE_OPERAND (ret_expr, 0);
1160       if (TREE_CODE (result_decl) == INDIRECT_REF)
1161         /* See through a return by reference.  */
1162         result_decl = TREE_OPERAND (result_decl, 0);
1163
1164       gcc_assert ((TREE_CODE (ret_expr) == MODIFY_EXPR
1165                    || TREE_CODE (ret_expr) == GIMPLE_MODIFY_STMT
1166                    || TREE_CODE (ret_expr) == INIT_EXPR)
1167                   && TREE_CODE (result_decl) == RESULT_DECL);
1168     }
1169
1170   /* If aggregate_value_p is true, then we can return the bare RESULT_DECL.
1171      Recall that aggregate_value_p is FALSE for any aggregate type that is
1172      returned in registers.  If we're returning values in registers, then
1173      we don't want to extend the lifetime of the RESULT_DECL, particularly
1174      across another call.  In addition, for those aggregates for which
1175      hard_function_value generates a PARALLEL, we'll die during normal
1176      expansion of structure assignments; there's special code in expand_return
1177      to handle this case that does not exist in expand_expr.  */
1178   if (!result_decl
1179       || aggregate_value_p (result_decl, TREE_TYPE (current_function_decl)))
1180     result = result_decl;
1181   else if (gimplify_ctxp->return_temp)
1182     result = gimplify_ctxp->return_temp;
1183   else
1184     {
1185       result = create_tmp_var (TREE_TYPE (result_decl), NULL);
1186       if (TREE_CODE (TREE_TYPE (result)) == COMPLEX_TYPE
1187           || TREE_CODE (TREE_TYPE (result)) == VECTOR_TYPE)
1188         DECL_GIMPLE_REG_P (result) = 1;
1189
1190       /* ??? With complex control flow (usually involving abnormal edges),
1191          we can wind up warning about an uninitialized value for this.  Due
1192          to how this variable is constructed and initialized, this is never
1193          true.  Give up and never warn.  */
1194       TREE_NO_WARNING (result) = 1;
1195
1196       gimplify_ctxp->return_temp = result;
1197     }
1198
1199   /* Smash the lhs of the GIMPLE_MODIFY_STMT to the temporary we plan to use.
1200      Then gimplify the whole thing.  */
1201   if (result != result_decl)
1202     GENERIC_TREE_OPERAND (ret_expr, 0) = result;
1203
1204   gimplify_and_add (TREE_OPERAND (stmt, 0), pre_p);
1205
1206   /* If we didn't use a temporary, then the result is just the result_decl.
1207      Otherwise we need a simple copy.  This should already be gimple.  */
1208   if (result == result_decl)
1209     ret_expr = result;
1210   else
1211     ret_expr = build_gimple_modify_stmt (result_decl, result);
1212   TREE_OPERAND (stmt, 0) = ret_expr;
1213
1214   return GS_ALL_DONE;
1215 }
1216
1217 /* Gimplifies a DECL_EXPR node *STMT_P by making any necessary allocation
1218    and initialization explicit.  */
1219
1220 static enum gimplify_status
1221 gimplify_decl_expr (tree *stmt_p)
1222 {
1223   tree stmt = *stmt_p;
1224   tree decl = DECL_EXPR_DECL (stmt);
1225
1226   *stmt_p = NULL_TREE;
1227
1228   if (TREE_TYPE (decl) == error_mark_node)
1229     return GS_ERROR;
1230
1231   if ((TREE_CODE (decl) == TYPE_DECL
1232        || TREE_CODE (decl) == VAR_DECL)
1233       && !TYPE_SIZES_GIMPLIFIED (TREE_TYPE (decl)))
1234     gimplify_type_sizes (TREE_TYPE (decl), stmt_p);
1235
1236   if (TREE_CODE (decl) == VAR_DECL && !DECL_EXTERNAL (decl))
1237     {
1238       tree init = DECL_INITIAL (decl);
1239
1240       if (TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
1241         {
1242           /* This is a variable-sized decl.  Simplify its size and mark it
1243              for deferred expansion.  Note that mudflap depends on the format
1244              of the emitted code: see mx_register_decls().  */
1245           tree t, addr, ptr_type;
1246
1247           gimplify_one_sizepos (&DECL_SIZE (decl), stmt_p);
1248           gimplify_one_sizepos (&DECL_SIZE_UNIT (decl), stmt_p);
1249
1250           /* All occurrences of this decl in final gimplified code will be
1251              replaced by indirection.  Setting DECL_VALUE_EXPR does two
1252              things: First, it lets the rest of the gimplifier know what
1253              replacement to use.  Second, it lets the debug info know
1254              where to find the value.  */
1255           ptr_type = build_pointer_type (TREE_TYPE (decl));
1256           addr = create_tmp_var (ptr_type, get_name (decl));
1257           DECL_IGNORED_P (addr) = 0;
1258           t = build_fold_indirect_ref (addr);
1259           SET_DECL_VALUE_EXPR (decl, t);
1260           DECL_HAS_VALUE_EXPR_P (decl) = 1;
1261
1262           t = built_in_decls[BUILT_IN_ALLOCA];
1263           t = build_call_expr (t, 1, DECL_SIZE_UNIT (decl));
1264           t = fold_convert (ptr_type, t);
1265           t = build_gimple_modify_stmt (addr, t);
1266
1267           gimplify_and_add (t, stmt_p);
1268
1269           /* Indicate that we need to restore the stack level when the
1270              enclosing BIND_EXPR is exited.  */
1271           gimplify_ctxp->save_stack = true;
1272         }
1273
1274       if (init && init != error_mark_node)
1275         {
1276           if (!TREE_STATIC (decl))
1277             {
1278               DECL_INITIAL (decl) = NULL_TREE;
1279               init = build2 (INIT_EXPR, void_type_node, decl, init);
1280               gimplify_and_add (init, stmt_p);
1281             }
1282           else
1283             /* We must still examine initializers for static variables
1284                as they may contain a label address.  */
1285             walk_tree (&init, force_labels_r, NULL, NULL);
1286         }
1287
1288       /* Some front ends do not explicitly declare all anonymous
1289          artificial variables.  We compensate here by declaring the
1290          variables, though it would be better if the front ends would
1291          explicitly declare them.  */
1292       if (!DECL_SEEN_IN_BIND_EXPR_P (decl)
1293           && DECL_ARTIFICIAL (decl) && DECL_NAME (decl) == NULL_TREE)
1294         gimple_add_tmp_var (decl);
1295     }
1296
1297   return GS_ALL_DONE;
1298 }
1299
1300 /* Gimplify a LOOP_EXPR.  Normally this just involves gimplifying the body
1301    and replacing the LOOP_EXPR with goto, but if the loop contains an
1302    EXIT_EXPR, we need to append a label for it to jump to.  */
1303
1304 static enum gimplify_status
1305 gimplify_loop_expr (tree *expr_p, tree *pre_p)
1306 {
1307   tree saved_label = gimplify_ctxp->exit_label;
1308   tree start_label = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
1309   tree jump_stmt = build_and_jump (&LABEL_EXPR_LABEL (start_label));
1310
1311   append_to_statement_list (start_label, pre_p);
1312
1313   gimplify_ctxp->exit_label = NULL_TREE;
1314
1315   gimplify_and_add (LOOP_EXPR_BODY (*expr_p), pre_p);
1316
1317   if (gimplify_ctxp->exit_label)
1318     {
1319       append_to_statement_list (jump_stmt, pre_p);
1320       *expr_p = build1 (LABEL_EXPR, void_type_node, gimplify_ctxp->exit_label);
1321     }
1322   else
1323     *expr_p = jump_stmt;
1324
1325   gimplify_ctxp->exit_label = saved_label;
1326
1327   return GS_ALL_DONE;
1328 }
1329
1330 /* Compare two case labels.  Because the front end should already have
1331    made sure that case ranges do not overlap, it is enough to only compare
1332    the CASE_LOW values of each case label.  */
1333
1334 static int
1335 compare_case_labels (const void *p1, const void *p2)
1336 {
1337   const_tree const case1 = *(const_tree const*)p1;
1338   const_tree const case2 = *(const_tree const*)p2;
1339
1340   return tree_int_cst_compare (CASE_LOW (case1), CASE_LOW (case2));
1341 }
1342
1343 /* Sort the case labels in LABEL_VEC in place in ascending order.  */
1344
1345 void
1346 sort_case_labels (tree label_vec)
1347 {
1348   size_t len = TREE_VEC_LENGTH (label_vec);
1349   tree default_case = TREE_VEC_ELT (label_vec, len - 1);
1350
1351   if (CASE_LOW (default_case))
1352     {
1353       size_t i;
1354
1355       /* The last label in the vector should be the default case
1356          but it is not.  */
1357       for (i = 0; i < len; ++i)
1358         {
1359           tree t = TREE_VEC_ELT (label_vec, i);
1360           if (!CASE_LOW (t))
1361             {
1362               default_case = t;
1363               TREE_VEC_ELT (label_vec, i) = TREE_VEC_ELT (label_vec, len - 1);
1364               TREE_VEC_ELT (label_vec, len - 1) = default_case;
1365               break;
1366             }
1367         }
1368     }
1369
1370   qsort (&TREE_VEC_ELT (label_vec, 0), len - 1, sizeof (tree),
1371          compare_case_labels);
1372 }
1373
1374 /* Gimplify a SWITCH_EXPR, and collect a TREE_VEC of the labels it can
1375    branch to.  */
1376
1377 static enum gimplify_status
1378 gimplify_switch_expr (tree *expr_p, tree *pre_p)
1379 {
1380   tree switch_expr = *expr_p;
1381   enum gimplify_status ret;
1382
1383   ret = gimplify_expr (&SWITCH_COND (switch_expr), pre_p, NULL,
1384                        is_gimple_val, fb_rvalue);
1385
1386   if (SWITCH_BODY (switch_expr))
1387     {
1388       VEC(tree,heap) *labels, *saved_labels;
1389       tree label_vec, default_case = NULL_TREE;
1390       size_t i, len;
1391
1392       /* If someone can be bothered to fill in the labels, they can
1393          be bothered to null out the body too.  */
1394       gcc_assert (!SWITCH_LABELS (switch_expr));
1395
1396       saved_labels = gimplify_ctxp->case_labels;
1397       gimplify_ctxp->case_labels = VEC_alloc (tree, heap, 8);
1398
1399       gimplify_to_stmt_list (&SWITCH_BODY (switch_expr));
1400
1401       labels = gimplify_ctxp->case_labels;
1402       gimplify_ctxp->case_labels = saved_labels;
1403
1404       i = 0;
1405       while (i < VEC_length (tree, labels))
1406         {
1407           tree elt = VEC_index (tree, labels, i);
1408           tree low = CASE_LOW (elt);
1409           bool remove_element = FALSE;
1410
1411           if (low)
1412             {
1413               /* Discard empty ranges.  */
1414               tree high = CASE_HIGH (elt);
1415               if (high && INT_CST_LT (high, low))
1416                 remove_element = TRUE;
1417             }
1418           else
1419             {
1420               /* The default case must be the last label in the list.  */
1421               gcc_assert (!default_case);
1422               default_case = elt;
1423               remove_element = TRUE;
1424             }
1425
1426           if (remove_element)
1427             VEC_ordered_remove (tree, labels, i);
1428           else
1429             i++;
1430         }
1431       len = i;
1432
1433       label_vec = make_tree_vec (len + 1);
1434       SWITCH_LABELS (*expr_p) = label_vec;
1435       append_to_statement_list (switch_expr, pre_p);
1436
1437       if (! default_case)
1438         {
1439           /* If the switch has no default label, add one, so that we jump
1440              around the switch body.  */
1441           default_case = build3 (CASE_LABEL_EXPR, void_type_node, NULL_TREE,
1442                                  NULL_TREE, create_artificial_label ());
1443           append_to_statement_list (SWITCH_BODY (switch_expr), pre_p);
1444           *expr_p = build1 (LABEL_EXPR, void_type_node,
1445                             CASE_LABEL (default_case));
1446         }
1447       else
1448         *expr_p = SWITCH_BODY (switch_expr);
1449
1450       for (i = 0; i < len; ++i)
1451         TREE_VEC_ELT (label_vec, i) = VEC_index (tree, labels, i);
1452       TREE_VEC_ELT (label_vec, len) = default_case;
1453
1454       VEC_free (tree, heap, labels);
1455
1456       sort_case_labels (label_vec);
1457
1458       SWITCH_BODY (switch_expr) = NULL;
1459     }
1460   else
1461     gcc_assert (SWITCH_LABELS (switch_expr));
1462
1463   return ret;
1464 }
1465
1466 static enum gimplify_status
1467 gimplify_case_label_expr (tree *expr_p)
1468 {
1469   tree expr = *expr_p;
1470   struct gimplify_ctx *ctxp;
1471
1472   /* Invalid OpenMP programs can play Duff's Device type games with
1473      #pragma omp parallel.  At least in the C front end, we don't
1474      detect such invalid branches until after gimplification.  */
1475   for (ctxp = gimplify_ctxp; ; ctxp = ctxp->prev_context)
1476     if (ctxp->case_labels)
1477       break;
1478
1479   VEC_safe_push (tree, heap, ctxp->case_labels, expr);
1480   *expr_p = build1 (LABEL_EXPR, void_type_node, CASE_LABEL (expr));
1481   return GS_ALL_DONE;
1482 }
1483
1484 /* Build a GOTO to the LABEL_DECL pointed to by LABEL_P, building it first
1485    if necessary.  */
1486
1487 tree
1488 build_and_jump (tree *label_p)
1489 {
1490   if (label_p == NULL)
1491     /* If there's nowhere to jump, just fall through.  */
1492     return NULL_TREE;
1493
1494   if (*label_p == NULL_TREE)
1495     {
1496       tree label = create_artificial_label ();
1497       *label_p = label;
1498     }
1499
1500   return build1 (GOTO_EXPR, void_type_node, *label_p);
1501 }
1502
1503 /* Gimplify an EXIT_EXPR by converting to a GOTO_EXPR inside a COND_EXPR.
1504    This also involves building a label to jump to and communicating it to
1505    gimplify_loop_expr through gimplify_ctxp->exit_label.  */
1506
1507 static enum gimplify_status
1508 gimplify_exit_expr (tree *expr_p)
1509 {
1510   tree cond = TREE_OPERAND (*expr_p, 0);
1511   tree expr;
1512
1513   expr = build_and_jump (&gimplify_ctxp->exit_label);
1514   expr = build3 (COND_EXPR, void_type_node, cond, expr, NULL_TREE);
1515   *expr_p = expr;
1516
1517   return GS_OK;
1518 }
1519
1520 /* A helper function to be called via walk_tree.  Mark all labels under *TP
1521    as being forced.  To be called for DECL_INITIAL of static variables.  */
1522
1523 tree
1524 force_labels_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
1525 {
1526   if (TYPE_P (*tp))
1527     *walk_subtrees = 0;
1528   if (TREE_CODE (*tp) == LABEL_DECL)
1529     FORCED_LABEL (*tp) = 1;
1530
1531   return NULL_TREE;
1532 }
1533
1534 /* *EXPR_P is a COMPONENT_REF being used as an rvalue.  If its type is
1535    different from its canonical type, wrap the whole thing inside a
1536    NOP_EXPR and force the type of the COMPONENT_REF to be the canonical
1537    type.
1538
1539    The canonical type of a COMPONENT_REF is the type of the field being
1540    referenced--unless the field is a bit-field which can be read directly
1541    in a smaller mode, in which case the canonical type is the
1542    sign-appropriate type corresponding to that mode.  */
1543
1544 static void
1545 canonicalize_component_ref (tree *expr_p)
1546 {
1547   tree expr = *expr_p;
1548   tree type;
1549
1550   gcc_assert (TREE_CODE (expr) == COMPONENT_REF);
1551
1552   if (INTEGRAL_TYPE_P (TREE_TYPE (expr)))
1553     type = TREE_TYPE (get_unwidened (expr, NULL_TREE));
1554   else
1555     type = TREE_TYPE (TREE_OPERAND (expr, 1));
1556
1557   if (TREE_TYPE (expr) != type)
1558     {
1559       tree old_type = TREE_TYPE (expr);
1560
1561       /* Set the type of the COMPONENT_REF to the underlying type.  */
1562       TREE_TYPE (expr) = type;
1563
1564       /* And wrap the whole thing inside a NOP_EXPR.  */
1565       expr = build1 (NOP_EXPR, old_type, expr);
1566
1567       *expr_p = expr;
1568     }
1569 }
1570
1571 /* If a NOP conversion is changing a pointer to array of foo to a pointer
1572    to foo, embed that change in the ADDR_EXPR by converting
1573       T array[U];
1574       (T *)&array
1575    ==>
1576       &array[L]
1577    where L is the lower bound.  For simplicity, only do this for constant
1578    lower bound.  */
1579
1580 static void
1581 canonicalize_addr_expr (tree *expr_p)
1582 {
1583   tree expr = *expr_p;
1584   tree ctype = TREE_TYPE (expr);
1585   tree addr_expr = TREE_OPERAND (expr, 0);
1586   tree atype = TREE_TYPE (addr_expr);
1587   tree dctype, datype, ddatype, otype, obj_expr;
1588
1589   /* Both cast and addr_expr types should be pointers.  */
1590   if (!POINTER_TYPE_P (ctype) || !POINTER_TYPE_P (atype))
1591     return;
1592
1593   /* The addr_expr type should be a pointer to an array.  */
1594   datype = TREE_TYPE (atype);
1595   if (TREE_CODE (datype) != ARRAY_TYPE)
1596     return;
1597
1598   /* Both cast and addr_expr types should address the same object type.  */
1599   dctype = TREE_TYPE (ctype);
1600   ddatype = TREE_TYPE (datype);
1601   if (!useless_type_conversion_p (dctype, ddatype))
1602     return;
1603
1604   /* The addr_expr and the object type should match.  */
1605   obj_expr = TREE_OPERAND (addr_expr, 0);
1606   otype = TREE_TYPE (obj_expr);
1607   if (!useless_type_conversion_p (datype, otype))
1608     return;
1609
1610   /* The lower bound and element sizes must be constant.  */
1611   if (!TYPE_SIZE_UNIT (dctype)
1612       || TREE_CODE (TYPE_SIZE_UNIT (dctype)) != INTEGER_CST
1613       || !TYPE_DOMAIN (datype) || !TYPE_MIN_VALUE (TYPE_DOMAIN (datype))
1614       || TREE_CODE (TYPE_MIN_VALUE (TYPE_DOMAIN (datype))) != INTEGER_CST)
1615     return;
1616
1617   /* All checks succeeded.  Build a new node to merge the cast.  */
1618   *expr_p = build4 (ARRAY_REF, dctype, obj_expr,
1619                     TYPE_MIN_VALUE (TYPE_DOMAIN (datype)),
1620                     NULL_TREE, NULL_TREE);
1621   *expr_p = build1 (ADDR_EXPR, ctype, *expr_p);
1622 }
1623
1624 /* *EXPR_P is a NOP_EXPR or CONVERT_EXPR.  Remove it and/or other conversions
1625    underneath as appropriate.  */
1626
1627 static enum gimplify_status
1628 gimplify_conversion (tree *expr_p)
1629 {
1630   tree tem;
1631   gcc_assert (TREE_CODE (*expr_p) == NOP_EXPR
1632               || TREE_CODE (*expr_p) == CONVERT_EXPR);
1633   
1634   /* Then strip away all but the outermost conversion.  */
1635   STRIP_SIGN_NOPS (TREE_OPERAND (*expr_p, 0));
1636
1637   /* And remove the outermost conversion if it's useless.  */
1638   if (tree_ssa_useless_type_conversion (*expr_p))
1639     *expr_p = TREE_OPERAND (*expr_p, 0);
1640
1641   /* Attempt to avoid NOP_EXPR by producing reference to a subtype.
1642      For example this fold (subclass *)&A into &A->subclass avoiding
1643      a need for statement.  */
1644   if (TREE_CODE (*expr_p) == NOP_EXPR
1645       && POINTER_TYPE_P (TREE_TYPE (*expr_p))
1646       && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (*expr_p, 0)))
1647       && (tem = maybe_fold_offset_to_reference
1648                   (TREE_OPERAND (*expr_p, 0),
1649                    integer_zero_node, TREE_TYPE (TREE_TYPE (*expr_p)))))
1650     {
1651       tree ptr_type = build_pointer_type (TREE_TYPE (tem));
1652       if (useless_type_conversion_p (TREE_TYPE (*expr_p), ptr_type))
1653         *expr_p = build_fold_addr_expr_with_type (tem, ptr_type);
1654     }
1655
1656   /* If we still have a conversion at the toplevel,
1657      then canonicalize some constructs.  */
1658   if (TREE_CODE (*expr_p) == NOP_EXPR || TREE_CODE (*expr_p) == CONVERT_EXPR)
1659     {
1660       tree sub = TREE_OPERAND (*expr_p, 0);
1661
1662       /* If a NOP conversion is changing the type of a COMPONENT_REF
1663          expression, then canonicalize its type now in order to expose more
1664          redundant conversions.  */
1665       if (TREE_CODE (sub) == COMPONENT_REF)
1666         canonicalize_component_ref (&TREE_OPERAND (*expr_p, 0));
1667
1668       /* If a NOP conversion is changing a pointer to array of foo
1669          to a pointer to foo, embed that change in the ADDR_EXPR.  */
1670       else if (TREE_CODE (sub) == ADDR_EXPR)
1671         canonicalize_addr_expr (expr_p);
1672     }
1673
1674   return GS_OK;
1675 }
1676
1677 /* Gimplify a VAR_DECL or PARM_DECL.  Returns GS_OK if we expanded a 
1678    DECL_VALUE_EXPR, and it's worth re-examining things.  */
1679
1680 static enum gimplify_status
1681 gimplify_var_or_parm_decl (tree *expr_p)
1682 {
1683   tree decl = *expr_p;
1684
1685   /* ??? If this is a local variable, and it has not been seen in any
1686      outer BIND_EXPR, then it's probably the result of a duplicate
1687      declaration, for which we've already issued an error.  It would
1688      be really nice if the front end wouldn't leak these at all.
1689      Currently the only known culprit is C++ destructors, as seen
1690      in g++.old-deja/g++.jason/binding.C.  */
1691   if (TREE_CODE (decl) == VAR_DECL
1692       && !DECL_SEEN_IN_BIND_EXPR_P (decl)
1693       && !TREE_STATIC (decl) && !DECL_EXTERNAL (decl)
1694       && decl_function_context (decl) == current_function_decl)
1695     {
1696       gcc_assert (errorcount || sorrycount);
1697       return GS_ERROR;
1698     }
1699
1700   /* When within an OpenMP context, notice uses of variables.  */
1701   if (gimplify_omp_ctxp && omp_notice_variable (gimplify_omp_ctxp, decl, true))
1702     return GS_ALL_DONE;
1703
1704   /* If the decl is an alias for another expression, substitute it now.  */
1705   if (DECL_HAS_VALUE_EXPR_P (decl))
1706     {
1707       *expr_p = unshare_expr (DECL_VALUE_EXPR (decl));
1708       return GS_OK;
1709     }
1710
1711   return GS_ALL_DONE;
1712 }
1713
1714
1715 /* Gimplify the COMPONENT_REF, ARRAY_REF, REALPART_EXPR or IMAGPART_EXPR
1716    node pointed to by EXPR_P.
1717
1718       compound_lval
1719               : min_lval '[' val ']'
1720               | min_lval '.' ID
1721               | compound_lval '[' val ']'
1722               | compound_lval '.' ID
1723
1724    This is not part of the original SIMPLE definition, which separates
1725    array and member references, but it seems reasonable to handle them
1726    together.  Also, this way we don't run into problems with union
1727    aliasing; gcc requires that for accesses through a union to alias, the
1728    union reference must be explicit, which was not always the case when we
1729    were splitting up array and member refs.
1730
1731    PRE_P points to the list where side effects that must happen before
1732      *EXPR_P should be stored.
1733
1734    POST_P points to the list where side effects that must happen after
1735      *EXPR_P should be stored.  */
1736
1737 static enum gimplify_status
1738 gimplify_compound_lval (tree *expr_p, tree *pre_p,
1739                         tree *post_p, fallback_t fallback)
1740 {
1741   tree *p;
1742   VEC(tree,heap) *stack;
1743   enum gimplify_status ret = GS_OK, tret;
1744   int i;
1745
1746   /* Create a stack of the subexpressions so later we can walk them in
1747      order from inner to outer.  */
1748   stack = VEC_alloc (tree, heap, 10);
1749
1750   /* We can handle anything that get_inner_reference can deal with.  */
1751   for (p = expr_p; ; p = &TREE_OPERAND (*p, 0))
1752     {
1753     restart:
1754       /* Fold INDIRECT_REFs now to turn them into ARRAY_REFs.  */
1755       if (TREE_CODE (*p) == INDIRECT_REF)
1756         *p = fold_indirect_ref (*p);
1757
1758       if (handled_component_p (*p))
1759         ;
1760       /* Expand DECL_VALUE_EXPR now.  In some cases that may expose
1761          additional COMPONENT_REFs.  */
1762       else if ((TREE_CODE (*p) == VAR_DECL || TREE_CODE (*p) == PARM_DECL)
1763                && gimplify_var_or_parm_decl (p) == GS_OK)
1764         goto restart;
1765       else
1766         break;
1767                
1768       VEC_safe_push (tree, heap, stack, *p);
1769     }
1770
1771   gcc_assert (VEC_length (tree, stack));
1772
1773   /* Now STACK is a stack of pointers to all the refs we've walked through
1774      and P points to the innermost expression.
1775
1776      Java requires that we elaborated nodes in source order.  That
1777      means we must gimplify the inner expression followed by each of
1778      the indices, in order.  But we can't gimplify the inner
1779      expression until we deal with any variable bounds, sizes, or
1780      positions in order to deal with PLACEHOLDER_EXPRs.
1781
1782      So we do this in three steps.  First we deal with the annotations
1783      for any variables in the components, then we gimplify the base,
1784      then we gimplify any indices, from left to right.  */
1785   for (i = VEC_length (tree, stack) - 1; i >= 0; i--)
1786     {
1787       tree t = VEC_index (tree, stack, i);
1788
1789       if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
1790         {
1791           /* Gimplify the low bound and element type size and put them into
1792              the ARRAY_REF.  If these values are set, they have already been
1793              gimplified.  */
1794           if (!TREE_OPERAND (t, 2))
1795             {
1796               tree low = unshare_expr (array_ref_low_bound (t));
1797               if (!is_gimple_min_invariant (low))
1798                 {
1799                   TREE_OPERAND (t, 2) = low;
1800                   tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p, post_p,
1801                                         is_gimple_formal_tmp_reg, fb_rvalue);
1802                   ret = MIN (ret, tret);
1803                 }
1804             }
1805
1806           if (!TREE_OPERAND (t, 3))
1807             {
1808               tree elmt_type = TREE_TYPE (TREE_TYPE (TREE_OPERAND (t, 0)));
1809               tree elmt_size = unshare_expr (array_ref_element_size (t));
1810               tree factor = size_int (TYPE_ALIGN_UNIT (elmt_type));
1811
1812               /* Divide the element size by the alignment of the element
1813                  type (above).  */
1814               elmt_size = size_binop (EXACT_DIV_EXPR, elmt_size, factor);
1815
1816               if (!is_gimple_min_invariant (elmt_size))
1817                 {
1818                   TREE_OPERAND (t, 3) = elmt_size;
1819                   tret = gimplify_expr (&TREE_OPERAND (t, 3), pre_p, post_p,
1820                                         is_gimple_formal_tmp_reg, fb_rvalue);
1821                   ret = MIN (ret, tret);
1822                 }
1823             }
1824         }
1825       else if (TREE_CODE (t) == COMPONENT_REF)
1826         {
1827           /* Set the field offset into T and gimplify it.  */
1828           if (!TREE_OPERAND (t, 2))
1829             {
1830               tree offset = unshare_expr (component_ref_field_offset (t));
1831               tree field = TREE_OPERAND (t, 1);
1832               tree factor
1833                 = size_int (DECL_OFFSET_ALIGN (field) / BITS_PER_UNIT);
1834
1835               /* Divide the offset by its alignment.  */
1836               offset = size_binop (EXACT_DIV_EXPR, offset, factor);
1837
1838               if (!is_gimple_min_invariant (offset))
1839                 {
1840                   TREE_OPERAND (t, 2) = offset;
1841                   tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p, post_p,
1842                                         is_gimple_formal_tmp_reg, fb_rvalue);
1843                   ret = MIN (ret, tret);
1844                 }
1845             }
1846         }
1847     }
1848
1849   /* Step 2 is to gimplify the base expression.  Make sure lvalue is set
1850      so as to match the min_lval predicate.  Failure to do so may result
1851      in the creation of large aggregate temporaries.  */
1852   tret = gimplify_expr (p, pre_p, post_p, is_gimple_min_lval,
1853                         fallback | fb_lvalue);
1854   ret = MIN (ret, tret);
1855
1856   /* And finally, the indices and operands to BIT_FIELD_REF.  During this
1857      loop we also remove any useless conversions.  */
1858   for (; VEC_length (tree, stack) > 0; )
1859     {
1860       tree t = VEC_pop (tree, stack);
1861
1862       if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
1863         {
1864           /* Gimplify the dimension.
1865              Temporary fix for gcc.c-torture/execute/20040313-1.c.
1866              Gimplify non-constant array indices into a temporary
1867              variable.
1868              FIXME - The real fix is to gimplify post-modify
1869              expressions into a minimal gimple lvalue.  However, that
1870              exposes bugs in alias analysis.  The alias analyzer does
1871              not handle &PTR->FIELD very well.  Will fix after the
1872              branch is merged into mainline (dnovillo 2004-05-03).  */
1873           if (!is_gimple_min_invariant (TREE_OPERAND (t, 1)))
1874             {
1875               tret = gimplify_expr (&TREE_OPERAND (t, 1), pre_p, post_p,
1876                                     is_gimple_formal_tmp_reg, fb_rvalue);
1877               ret = MIN (ret, tret);
1878             }
1879         }
1880       else if (TREE_CODE (t) == BIT_FIELD_REF)
1881         {
1882           tret = gimplify_expr (&TREE_OPERAND (t, 1), pre_p, post_p,
1883                                 is_gimple_val, fb_rvalue);
1884           ret = MIN (ret, tret);
1885           tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p, post_p,
1886                                 is_gimple_val, fb_rvalue);
1887           ret = MIN (ret, tret);
1888         }
1889
1890       STRIP_USELESS_TYPE_CONVERSION (TREE_OPERAND (t, 0));
1891
1892       /* The innermost expression P may have originally had TREE_SIDE_EFFECTS
1893          set which would have caused all the outer expressions in EXPR_P
1894          leading to P to also have had TREE_SIDE_EFFECTS set.  */
1895       recalculate_side_effects (t);
1896     }
1897
1898   tret = gimplify_expr (p, pre_p, post_p, is_gimple_min_lval, fallback);
1899   ret = MIN (ret, tret);
1900
1901   /* If the outermost expression is a COMPONENT_REF, canonicalize its type.  */
1902   if ((fallback & fb_rvalue) && TREE_CODE (*expr_p) == COMPONENT_REF)
1903     {
1904       canonicalize_component_ref (expr_p);
1905       ret = MIN (ret, GS_OK);
1906     }
1907
1908   VEC_free (tree, heap, stack);
1909
1910   return ret;
1911 }
1912
1913 /*  Gimplify the self modifying expression pointed to by EXPR_P
1914     (++, --, +=, -=).
1915
1916     PRE_P points to the list where side effects that must happen before
1917         *EXPR_P should be stored.
1918
1919     POST_P points to the list where side effects that must happen after
1920         *EXPR_P should be stored.
1921
1922     WANT_VALUE is nonzero iff we want to use the value of this expression
1923         in another expression.  */
1924
1925 static enum gimplify_status
1926 gimplify_self_mod_expr (tree *expr_p, tree *pre_p, tree *post_p,
1927                         bool want_value)
1928 {
1929   enum tree_code code;
1930   tree lhs, lvalue, rhs, t1, post = NULL, *orig_post_p = post_p;
1931   bool postfix;
1932   enum tree_code arith_code;
1933   enum gimplify_status ret;
1934
1935   code = TREE_CODE (*expr_p);
1936
1937   gcc_assert (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR
1938               || code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR);
1939
1940   /* Prefix or postfix?  */
1941   if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
1942     /* Faster to treat as prefix if result is not used.  */
1943     postfix = want_value;
1944   else
1945     postfix = false;
1946
1947   /* For postfix, make sure the inner expression's post side effects
1948      are executed after side effects from this expression.  */
1949   if (postfix)
1950     post_p = &post;
1951
1952   /* Add or subtract?  */
1953   if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
1954     arith_code = PLUS_EXPR;
1955   else
1956     arith_code = MINUS_EXPR;
1957
1958   /* Gimplify the LHS into a GIMPLE lvalue.  */
1959   lvalue = TREE_OPERAND (*expr_p, 0);
1960   ret = gimplify_expr (&lvalue, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
1961   if (ret == GS_ERROR)
1962     return ret;
1963
1964   /* Extract the operands to the arithmetic operation.  */
1965   lhs = lvalue;
1966   rhs = TREE_OPERAND (*expr_p, 1);
1967
1968   /* For postfix operator, we evaluate the LHS to an rvalue and then use
1969      that as the result value and in the postqueue operation.  */
1970   if (postfix)
1971     {
1972       ret = gimplify_expr (&lhs, pre_p, post_p, is_gimple_val, fb_rvalue);
1973       if (ret == GS_ERROR)
1974         return ret;
1975     }
1976
1977   /* For POINTERs increment, use POINTER_PLUS_EXPR.  */
1978   if (POINTER_TYPE_P (TREE_TYPE (lhs)))
1979     {
1980       rhs = fold_convert (sizetype, rhs);
1981       if (arith_code == MINUS_EXPR)
1982         rhs = fold_build1 (NEGATE_EXPR, TREE_TYPE (rhs), rhs);
1983       arith_code = POINTER_PLUS_EXPR;
1984     }
1985
1986   t1 = build2 (arith_code, TREE_TYPE (*expr_p), lhs, rhs);
1987   t1 = build_gimple_modify_stmt (lvalue, t1);
1988
1989   if (postfix)
1990     {
1991       gimplify_and_add (t1, orig_post_p);
1992       append_to_statement_list (post, orig_post_p);
1993       *expr_p = lhs;
1994       return GS_ALL_DONE;
1995     }
1996   else
1997     {
1998       *expr_p = t1;
1999       return GS_OK;
2000     }
2001 }
2002
2003 /* If *EXPR_P has a variable sized type, wrap it in a WITH_SIZE_EXPR.  */
2004
2005 static void
2006 maybe_with_size_expr (tree *expr_p)
2007 {
2008   tree expr = *expr_p;
2009   tree type = TREE_TYPE (expr);
2010   tree size;
2011
2012   /* If we've already wrapped this or the type is error_mark_node, we can't do
2013      anything.  */
2014   if (TREE_CODE (expr) == WITH_SIZE_EXPR
2015       || type == error_mark_node)
2016     return;
2017
2018   /* If the size isn't known or is a constant, we have nothing to do.  */
2019   size = TYPE_SIZE_UNIT (type);
2020   if (!size || TREE_CODE (size) == INTEGER_CST)
2021     return;
2022
2023   /* Otherwise, make a WITH_SIZE_EXPR.  */
2024   size = unshare_expr (size);
2025   size = SUBSTITUTE_PLACEHOLDER_IN_EXPR (size, expr);
2026   *expr_p = build2 (WITH_SIZE_EXPR, type, expr, size);
2027 }
2028
2029 /* Subroutine of gimplify_call_expr:  Gimplify a single argument.  */
2030
2031 static enum gimplify_status
2032 gimplify_arg (tree *expr_p, tree *pre_p)
2033 {
2034   bool (*test) (tree);
2035   fallback_t fb;
2036
2037   /* In general, we allow lvalues for function arguments to avoid
2038      extra overhead of copying large aggregates out of even larger
2039      aggregates into temporaries only to copy the temporaries to
2040      the argument list.  Make optimizers happy by pulling out to
2041      temporaries those types that fit in registers.  */
2042   if (is_gimple_reg_type (TREE_TYPE (*expr_p)))
2043     test = is_gimple_val, fb = fb_rvalue;
2044   else
2045     test = is_gimple_lvalue, fb = fb_either;
2046
2047   /* If this is a variable sized type, we must remember the size.  */
2048   maybe_with_size_expr (expr_p);
2049
2050   /* There is a sequence point before a function call.  Side effects in
2051      the argument list must occur before the actual call. So, when
2052      gimplifying arguments, force gimplify_expr to use an internal
2053      post queue which is then appended to the end of PRE_P.  */
2054   return gimplify_expr (expr_p, pre_p, NULL, test, fb);
2055 }
2056
2057 /* Gimplify the CALL_EXPR node pointed to by EXPR_P.  PRE_P points to the
2058    list where side effects that must happen before *EXPR_P should be stored.
2059    WANT_VALUE is true if the result of the call is desired.  */
2060
2061 static enum gimplify_status
2062 gimplify_call_expr (tree *expr_p, tree *pre_p, bool want_value)
2063 {
2064   tree decl, parms, p;
2065   enum gimplify_status ret;
2066   int i, nargs;
2067
2068   gcc_assert (TREE_CODE (*expr_p) == CALL_EXPR);
2069
2070   /* For reliable diagnostics during inlining, it is necessary that
2071      every call_expr be annotated with file and line.  */
2072   if (! EXPR_HAS_LOCATION (*expr_p))
2073     SET_EXPR_LOCATION (*expr_p, input_location);
2074
2075   /* This may be a call to a builtin function.
2076
2077      Builtin function calls may be transformed into different
2078      (and more efficient) builtin function calls under certain
2079      circumstances.  Unfortunately, gimplification can muck things
2080      up enough that the builtin expanders are not aware that certain
2081      transformations are still valid.
2082
2083      So we attempt transformation/gimplification of the call before
2084      we gimplify the CALL_EXPR.  At this time we do not manage to
2085      transform all calls in the same manner as the expanders do, but
2086      we do transform most of them.  */
2087   decl = get_callee_fndecl (*expr_p);
2088   if (decl && DECL_BUILT_IN (decl))
2089     {
2090       tree new = fold_call_expr (*expr_p, !want_value);
2091
2092       if (new && new != *expr_p)
2093         {
2094           /* There was a transformation of this call which computes the
2095              same value, but in a more efficient way.  Return and try
2096              again.  */
2097           *expr_p = new;
2098           return GS_OK;
2099         }
2100
2101       if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL
2102           && DECL_FUNCTION_CODE (decl) == BUILT_IN_VA_START)
2103         {
2104           if (call_expr_nargs (*expr_p) < 2)
2105             {
2106               error ("too few arguments to function %<va_start%>");
2107               *expr_p = build_empty_stmt ();
2108               return GS_OK;
2109             }
2110           
2111           if (fold_builtin_next_arg (*expr_p, true))
2112             {
2113               *expr_p = build_empty_stmt ();
2114               return GS_OK;
2115             }
2116           /* Avoid gimplifying the second argument to va_start, which needs
2117              to be the plain PARM_DECL.  */
2118           return gimplify_arg (&CALL_EXPR_ARG (*expr_p, 0), pre_p);
2119         }
2120     }
2121
2122   /* There is a sequence point before the call, so any side effects in
2123      the calling expression must occur before the actual call.  Force
2124      gimplify_expr to use an internal post queue.  */
2125   ret = gimplify_expr (&CALL_EXPR_FN (*expr_p), pre_p, NULL,
2126                        is_gimple_call_addr, fb_rvalue);
2127
2128   nargs = call_expr_nargs (*expr_p);
2129
2130   /* Get argument types for verification.  */
2131   decl = get_callee_fndecl (*expr_p);
2132   parms = NULL_TREE;
2133   if (decl)
2134     parms = TYPE_ARG_TYPES (TREE_TYPE (decl));
2135   else if (POINTER_TYPE_P (TREE_TYPE (CALL_EXPR_FN (*expr_p))))
2136     parms = TYPE_ARG_TYPES (TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (*expr_p))));
2137
2138   /* Verify if the type of the argument matches that of the function
2139      declaration.  If we cannot verify this or there is a mismatch,
2140      mark the call expression so it doesn't get inlined later.  */
2141   if (decl && DECL_ARGUMENTS (decl))
2142     {
2143       for (i = 0, p = DECL_ARGUMENTS (decl); i < nargs;
2144            i++, p = TREE_CHAIN (p))
2145         {
2146           /* We cannot distinguish a varargs function from the case
2147              of excess parameters, still deferring the inlining decision
2148              to the callee is possible.  */
2149           if (!p)
2150             break;
2151           if (p == error_mark_node
2152               || CALL_EXPR_ARG (*expr_p, i) == error_mark_node
2153               || !fold_convertible_p (DECL_ARG_TYPE (p),
2154                                       CALL_EXPR_ARG (*expr_p, i)))
2155             {
2156               CALL_CANNOT_INLINE_P (*expr_p) = 1;
2157               break;
2158             }
2159         }
2160     }
2161   else if (parms)
2162     {
2163       for (i = 0, p = parms; i < nargs; i++, p = TREE_CHAIN (p))
2164         {
2165           /* If this is a varargs function defer inlining decision
2166              to callee.  */
2167           if (!p)
2168             break;
2169           if (TREE_VALUE (p) == error_mark_node
2170               || CALL_EXPR_ARG (*expr_p, i) == error_mark_node
2171               || TREE_CODE (TREE_VALUE (p)) == VOID_TYPE
2172               || !fold_convertible_p (TREE_VALUE (p),
2173                                       CALL_EXPR_ARG (*expr_p, i)))
2174             {
2175               CALL_CANNOT_INLINE_P (*expr_p) = 1;
2176               break;
2177             }
2178         }
2179     }
2180   else if (nargs != 0)
2181     CALL_CANNOT_INLINE_P (*expr_p) = 1;
2182
2183   /* Finally, gimplify the function arguments.  */
2184   for (i = (PUSH_ARGS_REVERSED ? nargs - 1 : 0);
2185        PUSH_ARGS_REVERSED ? i >= 0 : i < nargs;
2186        PUSH_ARGS_REVERSED ? i-- : i++)
2187     {
2188       enum gimplify_status t;
2189
2190       t = gimplify_arg (&CALL_EXPR_ARG (*expr_p, i), pre_p);
2191
2192       if (t == GS_ERROR)
2193         ret = GS_ERROR;
2194     }
2195
2196   /* Try this again in case gimplification exposed something.  */
2197   if (ret != GS_ERROR)
2198     {
2199       tree new = fold_call_expr (*expr_p, !want_value);
2200
2201       if (new && new != *expr_p)
2202         {
2203           /* There was a transformation of this call which computes the
2204              same value, but in a more efficient way.  Return and try
2205              again.  */
2206           *expr_p = new;
2207           return GS_OK;
2208         }
2209     }
2210
2211   /* If the function is "const" or "pure", then clear TREE_SIDE_EFFECTS on its
2212      decl.  This allows us to eliminate redundant or useless
2213      calls to "const" functions.  */
2214   if (TREE_CODE (*expr_p) == CALL_EXPR
2215       && (call_expr_flags (*expr_p) & (ECF_CONST | ECF_PURE)))
2216     TREE_SIDE_EFFECTS (*expr_p) = 0;
2217
2218   return ret;
2219 }
2220
2221 /* Handle shortcut semantics in the predicate operand of a COND_EXPR by
2222    rewriting it into multiple COND_EXPRs, and possibly GOTO_EXPRs.
2223
2224    TRUE_LABEL_P and FALSE_LABEL_P point to the labels to jump to if the
2225    condition is true or false, respectively.  If null, we should generate
2226    our own to skip over the evaluation of this specific expression.
2227
2228    This function is the tree equivalent of do_jump.
2229
2230    shortcut_cond_r should only be called by shortcut_cond_expr.  */
2231
2232 static tree
2233 shortcut_cond_r (tree pred, tree *true_label_p, tree *false_label_p)
2234 {
2235   tree local_label = NULL_TREE;
2236   tree t, expr = NULL;
2237
2238   /* OK, it's not a simple case; we need to pull apart the COND_EXPR to
2239      retain the shortcut semantics.  Just insert the gotos here;
2240      shortcut_cond_expr will append the real blocks later.  */
2241   if (TREE_CODE (pred) == TRUTH_ANDIF_EXPR)
2242     {
2243       /* Turn if (a && b) into
2244
2245          if (a); else goto no;
2246          if (b) goto yes; else goto no;
2247          (no:) */
2248
2249       if (false_label_p == NULL)
2250         false_label_p = &local_label;
2251
2252       t = shortcut_cond_r (TREE_OPERAND (pred, 0), NULL, false_label_p);
2253       append_to_statement_list (t, &expr);
2254
2255       t = shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p,
2256                            false_label_p);
2257       append_to_statement_list (t, &expr);
2258     }
2259   else if (TREE_CODE (pred) == TRUTH_ORIF_EXPR)
2260     {
2261       /* Turn if (a || b) into
2262
2263          if (a) goto yes;
2264          if (b) goto yes; else goto no;
2265          (yes:) */
2266
2267       if (true_label_p == NULL)
2268         true_label_p = &local_label;
2269
2270       t = shortcut_cond_r (TREE_OPERAND (pred, 0), true_label_p, NULL);
2271       append_to_statement_list (t, &expr);
2272
2273       t = shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p,
2274                            false_label_p);
2275       append_to_statement_list (t, &expr);
2276     }
2277   else if (TREE_CODE (pred) == COND_EXPR)
2278     {
2279       /* As long as we're messing with gotos, turn if (a ? b : c) into
2280          if (a)
2281            if (b) goto yes; else goto no;
2282          else
2283            if (c) goto yes; else goto no;  */
2284       expr = build3 (COND_EXPR, void_type_node, TREE_OPERAND (pred, 0),
2285                      shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p,
2286                                       false_label_p),
2287                      shortcut_cond_r (TREE_OPERAND (pred, 2), true_label_p,
2288                                       false_label_p));
2289     }
2290   else
2291     {
2292       expr = build3 (COND_EXPR, void_type_node, pred,
2293                      build_and_jump (true_label_p),
2294                      build_and_jump (false_label_p));
2295     }
2296
2297   if (local_label)
2298     {
2299       t = build1 (LABEL_EXPR, void_type_node, local_label);
2300       append_to_statement_list (t, &expr);
2301     }
2302
2303   return expr;
2304 }
2305
2306 static tree
2307 shortcut_cond_expr (tree expr)
2308 {
2309   tree pred = TREE_OPERAND (expr, 0);
2310   tree then_ = TREE_OPERAND (expr, 1);
2311   tree else_ = TREE_OPERAND (expr, 2);
2312   tree true_label, false_label, end_label, t;
2313   tree *true_label_p;
2314   tree *false_label_p;
2315   bool emit_end, emit_false, jump_over_else;
2316   bool then_se = then_ && TREE_SIDE_EFFECTS (then_);
2317   bool else_se = else_ && TREE_SIDE_EFFECTS (else_);
2318
2319   /* First do simple transformations.  */
2320   if (!else_se)
2321     {
2322       /* If there is no 'else', turn (a && b) into if (a) if (b).  */
2323       while (TREE_CODE (pred) == TRUTH_ANDIF_EXPR)
2324         {
2325           TREE_OPERAND (expr, 0) = TREE_OPERAND (pred, 1);
2326           then_ = shortcut_cond_expr (expr);
2327           then_se = then_ && TREE_SIDE_EFFECTS (then_);
2328           pred = TREE_OPERAND (pred, 0);
2329           expr = build3 (COND_EXPR, void_type_node, pred, then_, NULL_TREE);
2330         }
2331     }
2332   if (!then_se)
2333     {
2334       /* If there is no 'then', turn
2335            if (a || b); else d
2336          into
2337            if (a); else if (b); else d.  */
2338       while (TREE_CODE (pred) == TRUTH_ORIF_EXPR)
2339         {
2340           TREE_OPERAND (expr, 0) = TREE_OPERAND (pred, 1);
2341           else_ = shortcut_cond_expr (expr);
2342           else_se = else_ && TREE_SIDE_EFFECTS (else_);
2343           pred = TREE_OPERAND (pred, 0);
2344           expr = build3 (COND_EXPR, void_type_node, pred, NULL_TREE, else_);
2345         }
2346     }
2347
2348   /* If we're done, great.  */
2349   if (TREE_CODE (pred) != TRUTH_ANDIF_EXPR
2350       && TREE_CODE (pred) != TRUTH_ORIF_EXPR)
2351     return expr;
2352
2353   /* Otherwise we need to mess with gotos.  Change
2354        if (a) c; else d;
2355      to
2356        if (a); else goto no;
2357        c; goto end;
2358        no: d; end:
2359      and recursively gimplify the condition.  */
2360
2361   true_label = false_label = end_label = NULL_TREE;
2362
2363   /* If our arms just jump somewhere, hijack those labels so we don't
2364      generate jumps to jumps.  */
2365
2366   if (then_
2367       && TREE_CODE (then_) == GOTO_EXPR
2368       && TREE_CODE (GOTO_DESTINATION (then_)) == LABEL_DECL)
2369     {
2370       true_label = GOTO_DESTINATION (then_);
2371       then_ = NULL;
2372       then_se = false;
2373     }
2374
2375   if (else_
2376       && TREE_CODE (else_) == GOTO_EXPR
2377       && TREE_CODE (GOTO_DESTINATION (else_)) == LABEL_DECL)
2378     {
2379       false_label = GOTO_DESTINATION (else_);
2380       else_ = NULL;
2381       else_se = false;
2382     }
2383
2384   /* If we aren't hijacking a label for the 'then' branch, it falls through.  */
2385   if (true_label)
2386     true_label_p = &true_label;
2387   else
2388     true_label_p = NULL;
2389
2390   /* The 'else' branch also needs a label if it contains interesting code.  */
2391   if (false_label || else_se)
2392     false_label_p = &false_label;
2393   else
2394     false_label_p = NULL;
2395
2396   /* If there was nothing else in our arms, just forward the label(s).  */
2397   if (!then_se && !else_se)
2398     return shortcut_cond_r (pred, true_label_p, false_label_p);
2399
2400   /* If our last subexpression already has a terminal label, reuse it.  */
2401   if (else_se)
2402     expr = expr_last (else_);
2403   else if (then_se)
2404     expr = expr_last (then_);
2405   else
2406     expr = NULL;
2407   if (expr && TREE_CODE (expr) == LABEL_EXPR)
2408     end_label = LABEL_EXPR_LABEL (expr);
2409
2410   /* If we don't care about jumping to the 'else' branch, jump to the end
2411      if the condition is false.  */
2412   if (!false_label_p)
2413     false_label_p = &end_label;
2414
2415   /* We only want to emit these labels if we aren't hijacking them.  */
2416   emit_end = (end_label == NULL_TREE);
2417   emit_false = (false_label == NULL_TREE);
2418
2419   /* We only emit the jump over the else clause if we have to--if the
2420      then clause may fall through.  Otherwise we can wind up with a
2421      useless jump and a useless label at the end of gimplified code,
2422      which will cause us to think that this conditional as a whole
2423      falls through even if it doesn't.  If we then inline a function
2424      which ends with such a condition, that can cause us to issue an
2425      inappropriate warning about control reaching the end of a
2426      non-void function.  */
2427   jump_over_else = block_may_fallthru (then_);
2428
2429   pred = shortcut_cond_r (pred, true_label_p, false_label_p);
2430
2431   expr = NULL;
2432   append_to_statement_list (pred, &expr);
2433
2434   append_to_statement_list (then_, &expr);
2435   if (else_se)
2436     {
2437       if (jump_over_else)
2438         {
2439           t = build_and_jump (&end_label);
2440           append_to_statement_list (t, &expr);
2441         }
2442       if (emit_false)
2443         {
2444           t = build1 (LABEL_EXPR, void_type_node, false_label);
2445           append_to_statement_list (t, &expr);
2446         }
2447       append_to_statement_list (else_, &expr);
2448     }
2449   if (emit_end && end_label)
2450     {
2451       t = build1 (LABEL_EXPR, void_type_node, end_label);
2452       append_to_statement_list (t, &expr);
2453     }
2454
2455   return expr;
2456 }
2457
2458 /* EXPR is used in a boolean context; make sure it has BOOLEAN_TYPE.  */
2459
2460 tree
2461 gimple_boolify (tree expr)
2462 {
2463   tree type = TREE_TYPE (expr);
2464
2465   if (TREE_CODE (type) == BOOLEAN_TYPE)
2466     return expr;
2467
2468   switch (TREE_CODE (expr))
2469     {
2470     case TRUTH_AND_EXPR:
2471     case TRUTH_OR_EXPR:
2472     case TRUTH_XOR_EXPR:
2473     case TRUTH_ANDIF_EXPR:
2474     case TRUTH_ORIF_EXPR:
2475       /* Also boolify the arguments of truth exprs.  */
2476       TREE_OPERAND (expr, 1) = gimple_boolify (TREE_OPERAND (expr, 1));
2477       /* FALLTHRU */
2478
2479     case TRUTH_NOT_EXPR:
2480       TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
2481       /* FALLTHRU */
2482
2483     case EQ_EXPR: case NE_EXPR:
2484     case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
2485       /* These expressions always produce boolean results.  */
2486       TREE_TYPE (expr) = boolean_type_node;
2487       return expr;
2488
2489     default:
2490       /* Other expressions that get here must have boolean values, but
2491          might need to be converted to the appropriate mode.  */
2492       return fold_convert (boolean_type_node, expr);
2493     }
2494 }
2495
2496 /*  Convert the conditional expression pointed to by EXPR_P '(p) ? a : b;'
2497     into
2498
2499     if (p)                      if (p)
2500       t1 = a;                     a;
2501     else                or      else
2502       t1 = b;                     b;
2503     t1;
2504
2505     The second form is used when *EXPR_P is of type void.
2506
2507     TARGET is the tree for T1 above.
2508
2509     PRE_P points to the list where side effects that must happen before
2510       *EXPR_P should be stored.  */
2511
2512 static enum gimplify_status
2513 gimplify_cond_expr (tree *expr_p, tree *pre_p, fallback_t fallback)
2514 {
2515   tree expr = *expr_p;
2516   tree tmp, tmp2, type;
2517   enum gimplify_status ret;
2518
2519   type = TREE_TYPE (expr);
2520
2521   /* If this COND_EXPR has a value, copy the values into a temporary within
2522      the arms.  */
2523   if (! VOID_TYPE_P (type))
2524     {
2525       tree result;
2526
2527       if ((fallback & fb_lvalue) == 0)
2528         {
2529           result = tmp2 = tmp = create_tmp_var (TREE_TYPE (expr), "iftmp");
2530           ret = GS_ALL_DONE;
2531         }
2532       else
2533         {
2534           tree type = build_pointer_type (TREE_TYPE (expr));
2535
2536           if (TREE_TYPE (TREE_OPERAND (expr, 1)) != void_type_node)
2537             TREE_OPERAND (expr, 1) =
2538               build_fold_addr_expr (TREE_OPERAND (expr, 1));
2539
2540           if (TREE_TYPE (TREE_OPERAND (expr, 2)) != void_type_node)
2541             TREE_OPERAND (expr, 2) =
2542               build_fold_addr_expr (TREE_OPERAND (expr, 2));
2543           
2544           tmp2 = tmp = create_tmp_var (type, "iftmp");
2545
2546           expr = build3 (COND_EXPR, void_type_node, TREE_OPERAND (expr, 0),
2547                          TREE_OPERAND (expr, 1), TREE_OPERAND (expr, 2));
2548
2549           result = build_fold_indirect_ref (tmp);
2550           ret = GS_ALL_DONE;
2551         }
2552
2553       /* Build the then clause, 't1 = a;'.  But don't build an assignment
2554          if this branch is void; in C++ it can be, if it's a throw.  */
2555       if (TREE_TYPE (TREE_OPERAND (expr, 1)) != void_type_node)
2556         TREE_OPERAND (expr, 1)
2557           = build_gimple_modify_stmt (tmp, TREE_OPERAND (expr, 1));
2558
2559       /* Build the else clause, 't1 = b;'.  */
2560       if (TREE_TYPE (TREE_OPERAND (expr, 2)) != void_type_node)
2561         TREE_OPERAND (expr, 2)
2562           = build_gimple_modify_stmt (tmp2, TREE_OPERAND (expr, 2));
2563
2564       TREE_TYPE (expr) = void_type_node;
2565       recalculate_side_effects (expr);
2566
2567       /* Move the COND_EXPR to the prequeue.  */
2568       gimplify_and_add (expr, pre_p);
2569
2570       *expr_p = result;
2571       return ret;
2572     }
2573
2574   /* Make sure the condition has BOOLEAN_TYPE.  */
2575   TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
2576
2577   /* Break apart && and || conditions.  */
2578   if (TREE_CODE (TREE_OPERAND (expr, 0)) == TRUTH_ANDIF_EXPR
2579       || TREE_CODE (TREE_OPERAND (expr, 0)) == TRUTH_ORIF_EXPR)
2580     {
2581       expr = shortcut_cond_expr (expr);
2582
2583       if (expr != *expr_p)
2584         {
2585           *expr_p = expr;
2586
2587           /* We can't rely on gimplify_expr to re-gimplify the expanded
2588              form properly, as cleanups might cause the target labels to be
2589              wrapped in a TRY_FINALLY_EXPR.  To prevent that, we need to
2590              set up a conditional context.  */
2591           gimple_push_condition ();
2592           gimplify_stmt (expr_p);
2593           gimple_pop_condition (pre_p);
2594
2595           return GS_ALL_DONE;
2596         }
2597     }
2598
2599   /* Now do the normal gimplification.  */
2600   ret = gimplify_expr (&TREE_OPERAND (expr, 0), pre_p, NULL,
2601                        is_gimple_condexpr, fb_rvalue);
2602
2603   gimple_push_condition ();
2604
2605   gimplify_to_stmt_list (&TREE_OPERAND (expr, 1));
2606   gimplify_to_stmt_list (&TREE_OPERAND (expr, 2));
2607   recalculate_side_effects (expr);
2608
2609   gimple_pop_condition (pre_p);
2610
2611   if (ret == GS_ERROR)
2612     ;
2613   else if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)))
2614     ret = GS_ALL_DONE;
2615   else if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 2)))
2616     /* Rewrite "if (a); else b" to "if (!a) b"  */
2617     {
2618       TREE_OPERAND (expr, 0) = invert_truthvalue (TREE_OPERAND (expr, 0));
2619       ret = gimplify_expr (&TREE_OPERAND (expr, 0), pre_p, NULL,
2620                            is_gimple_condexpr, fb_rvalue);
2621
2622       tmp = TREE_OPERAND (expr, 1);
2623       TREE_OPERAND (expr, 1) = TREE_OPERAND (expr, 2);
2624       TREE_OPERAND (expr, 2) = tmp;
2625     }
2626   else
2627     /* Both arms are empty; replace the COND_EXPR with its predicate.  */
2628     expr = TREE_OPERAND (expr, 0);
2629
2630   *expr_p = expr;
2631   return ret;
2632 }
2633
2634 /* A subroutine of gimplify_modify_expr.  Replace a MODIFY_EXPR with
2635    a call to __builtin_memcpy.  */
2636
2637 static enum gimplify_status
2638 gimplify_modify_expr_to_memcpy (tree *expr_p, tree size, bool want_value)
2639 {
2640   tree t, to, to_ptr, from, from_ptr;
2641
2642   to = GENERIC_TREE_OPERAND (*expr_p, 0);
2643   from = GENERIC_TREE_OPERAND (*expr_p, 1);
2644
2645   from_ptr = build_fold_addr_expr (from);
2646
2647   to_ptr = build_fold_addr_expr (to);
2648   t = implicit_built_in_decls[BUILT_IN_MEMCPY];
2649   t = build_call_expr (t, 3, to_ptr, from_ptr, size);
2650
2651   if (want_value)
2652     {
2653       t = build1 (NOP_EXPR, TREE_TYPE (to_ptr), t);
2654       t = build1 (INDIRECT_REF, TREE_TYPE (to), t);
2655     }
2656
2657   *expr_p = t;
2658   return GS_OK;
2659 }
2660
2661 /* A subroutine of gimplify_modify_expr.  Replace a MODIFY_EXPR with
2662    a call to __builtin_memset.  In this case we know that the RHS is
2663    a CONSTRUCTOR with an empty element list.  */
2664
2665 static enum gimplify_status
2666 gimplify_modify_expr_to_memset (tree *expr_p, tree size, bool want_value)
2667 {
2668   tree t, to, to_ptr;
2669
2670   to = GENERIC_TREE_OPERAND (*expr_p, 0);
2671
2672   to_ptr = build_fold_addr_expr (to);
2673   t = implicit_built_in_decls[BUILT_IN_MEMSET];
2674   t = build_call_expr (t, 3, to_ptr, integer_zero_node, size);
2675
2676   if (want_value)
2677     {
2678       t = build1 (NOP_EXPR, TREE_TYPE (to_ptr), t);
2679       t = build1 (INDIRECT_REF, TREE_TYPE (to), t);
2680     }
2681
2682   *expr_p = t;
2683   return GS_OK;
2684 }
2685
2686 /* A subroutine of gimplify_init_ctor_preeval.  Called via walk_tree,
2687    determine, cautiously, if a CONSTRUCTOR overlaps the lhs of an
2688    assignment.  Returns non-null if we detect a potential overlap.  */
2689
2690 struct gimplify_init_ctor_preeval_data
2691 {
2692   /* The base decl of the lhs object.  May be NULL, in which case we
2693      have to assume the lhs is indirect.  */
2694   tree lhs_base_decl;
2695
2696   /* The alias set of the lhs object.  */
2697   int lhs_alias_set;
2698 };
2699
2700 static tree
2701 gimplify_init_ctor_preeval_1 (tree *tp, int *walk_subtrees, void *xdata)
2702 {
2703   struct gimplify_init_ctor_preeval_data *data
2704     = (struct gimplify_init_ctor_preeval_data *) xdata;
2705   tree t = *tp;
2706
2707   /* If we find the base object, obviously we have overlap.  */
2708   if (data->lhs_base_decl == t)
2709     return t;
2710
2711   /* If the constructor component is indirect, determine if we have a
2712      potential overlap with the lhs.  The only bits of information we
2713      have to go on at this point are addressability and alias sets.  */
2714   if (TREE_CODE (t) == INDIRECT_REF
2715       && (!data->lhs_base_decl || TREE_ADDRESSABLE (data->lhs_base_decl))
2716       && alias_sets_conflict_p (data->lhs_alias_set, get_alias_set (t)))
2717     return t;
2718
2719   /* If the constructor component is a call, determine if it can hide a
2720      potential overlap with the lhs through an INDIRECT_REF like above.  */
2721   if (TREE_CODE (t) == CALL_EXPR)
2722     {
2723       tree type, fntype = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (t)));
2724
2725       for (type = TYPE_ARG_TYPES (fntype); type; type = TREE_CHAIN (type))
2726         if (POINTER_TYPE_P (TREE_VALUE (type))
2727             && (!data->lhs_base_decl || TREE_ADDRESSABLE (data->lhs_base_decl))
2728             && alias_sets_conflict_p (data->lhs_alias_set,
2729                                       get_alias_set
2730                                         (TREE_TYPE (TREE_VALUE (type)))))
2731           return t;
2732     }
2733
2734   if (IS_TYPE_OR_DECL_P (t))
2735     *walk_subtrees = 0;
2736   return NULL;
2737 }
2738
2739 /* A subroutine of gimplify_init_constructor.  Pre-evaluate *EXPR_P,
2740    force values that overlap with the lhs (as described by *DATA)
2741    into temporaries.  */
2742
2743 static void
2744 gimplify_init_ctor_preeval (tree *expr_p, tree *pre_p, tree *post_p,
2745                             struct gimplify_init_ctor_preeval_data *data)
2746 {
2747   enum gimplify_status one;
2748
2749   /* If the value is invariant, then there's nothing to pre-evaluate.
2750      But ensure it doesn't have any side-effects since a SAVE_EXPR is
2751      invariant but has side effects and might contain a reference to
2752      the object we're initializing.  */
2753   if (TREE_INVARIANT (*expr_p) && !TREE_SIDE_EFFECTS (*expr_p))
2754     return;
2755
2756   /* If the type has non-trivial constructors, we can't pre-evaluate.  */
2757   if (TREE_ADDRESSABLE (TREE_TYPE (*expr_p)))
2758     return;
2759
2760   /* Recurse for nested constructors.  */
2761   if (TREE_CODE (*expr_p) == CONSTRUCTOR)
2762     {
2763       unsigned HOST_WIDE_INT ix;
2764       constructor_elt *ce;
2765       VEC(constructor_elt,gc) *v = CONSTRUCTOR_ELTS (*expr_p);
2766
2767       for (ix = 0; VEC_iterate (constructor_elt, v, ix, ce); ix++)
2768         gimplify_init_ctor_preeval (&ce->value, pre_p, post_p, data);
2769       return;
2770     }
2771
2772   /* If this is a variable sized type, we must remember the size.  */
2773   maybe_with_size_expr (expr_p);
2774
2775   /* Gimplify the constructor element to something appropriate for the rhs
2776      of a MODIFY_EXPR.  Given that we know the lhs is an aggregate, we know
2777      the gimplifier will consider this a store to memory.  Doing this
2778      gimplification now means that we won't have to deal with complicated
2779      language-specific trees, nor trees like SAVE_EXPR that can induce
2780      exponential search behavior.  */
2781   one = gimplify_expr (expr_p, pre_p, post_p, is_gimple_mem_rhs, fb_rvalue);
2782   if (one == GS_ERROR)
2783     {
2784       *expr_p = NULL;
2785       return;
2786     }
2787
2788   /* If we gimplified to a bare decl, we can be sure that it doesn't overlap
2789      with the lhs, since "a = { .x=a }" doesn't make sense.  This will
2790      always be true for all scalars, since is_gimple_mem_rhs insists on a
2791      temporary variable for them.  */
2792   if (DECL_P (*expr_p))
2793     return;
2794
2795   /* If this is of variable size, we have no choice but to assume it doesn't
2796      overlap since we can't make a temporary for it.  */
2797   if (TREE_CODE (TYPE_SIZE (TREE_TYPE (*expr_p))) != INTEGER_CST)
2798     return;
2799
2800   /* Otherwise, we must search for overlap ...  */
2801   if (!walk_tree (expr_p, gimplify_init_ctor_preeval_1, data, NULL))
2802     return;
2803
2804   /* ... and if found, force the value into a temporary.  */
2805   *expr_p = get_formal_tmp_var (*expr_p, pre_p);
2806 }
2807
2808 /* A subroutine of gimplify_init_ctor_eval.  Create a loop for
2809    a RANGE_EXPR in a CONSTRUCTOR for an array.
2810
2811       var = lower;
2812     loop_entry:
2813       object[var] = value;
2814       if (var == upper)
2815         goto loop_exit;
2816       var = var + 1;
2817       goto loop_entry;
2818     loop_exit:
2819
2820    We increment var _after_ the loop exit check because we might otherwise
2821    fail if upper == TYPE_MAX_VALUE (type for upper).
2822
2823    Note that we never have to deal with SAVE_EXPRs here, because this has
2824    already been taken care of for us, in gimplify_init_ctor_preeval().  */
2825
2826 static void gimplify_init_ctor_eval (tree, VEC(constructor_elt,gc) *,
2827                                      tree *, bool);
2828
2829 static void
2830 gimplify_init_ctor_eval_range (tree object, tree lower, tree upper,
2831                                tree value, tree array_elt_type,
2832                                tree *pre_p, bool cleared)
2833 {
2834   tree loop_entry_label, loop_exit_label;
2835   tree var, var_type, cref, tmp;
2836
2837   loop_entry_label = create_artificial_label ();
2838   loop_exit_label = create_artificial_label ();
2839
2840   /* Create and initialize the index variable.  */
2841   var_type = TREE_TYPE (upper);
2842   var = create_tmp_var (var_type, NULL);
2843   append_to_statement_list (build_gimple_modify_stmt (var, lower), pre_p);
2844
2845   /* Add the loop entry label.  */
2846   append_to_statement_list (build1 (LABEL_EXPR,
2847                                     void_type_node,
2848                                     loop_entry_label),
2849                             pre_p);
2850
2851   /* Build the reference.  */
2852   cref = build4 (ARRAY_REF, array_elt_type, unshare_expr (object),
2853                  var, NULL_TREE, NULL_TREE);
2854
2855   /* If we are a constructor, just call gimplify_init_ctor_eval to do
2856      the store.  Otherwise just assign value to the reference.  */
2857
2858   if (TREE_CODE (value) == CONSTRUCTOR)
2859     /* NB we might have to call ourself recursively through
2860        gimplify_init_ctor_eval if the value is a constructor.  */
2861     gimplify_init_ctor_eval (cref, CONSTRUCTOR_ELTS (value),
2862                              pre_p, cleared);
2863   else
2864     append_to_statement_list (build_gimple_modify_stmt (cref, value), pre_p);
2865
2866   /* We exit the loop when the index var is equal to the upper bound.  */
2867   gimplify_and_add (build3 (COND_EXPR, void_type_node,
2868                             build2 (EQ_EXPR, boolean_type_node,
2869                                     var, upper),
2870                             build1 (GOTO_EXPR,
2871                                     void_type_node,
2872                                     loop_exit_label),
2873                             NULL_TREE),
2874                     pre_p);
2875
2876   /* Otherwise, increment the index var...  */
2877   tmp = build2 (PLUS_EXPR, var_type, var,
2878                 fold_convert (var_type, integer_one_node));
2879   append_to_statement_list (build_gimple_modify_stmt (var, tmp), pre_p);
2880
2881   /* ...and jump back to the loop entry.  */
2882   append_to_statement_list (build1 (GOTO_EXPR,
2883                                     void_type_node,
2884                                     loop_entry_label),
2885                             pre_p);
2886
2887   /* Add the loop exit label.  */
2888   append_to_statement_list (build1 (LABEL_EXPR,
2889                                     void_type_node,
2890                                     loop_exit_label),
2891                             pre_p);
2892 }
2893
2894 /* Return true if FDECL is accessing a field that is zero sized.  */
2895    
2896 static bool
2897 zero_sized_field_decl (tree fdecl)
2898 {
2899   if (TREE_CODE (fdecl) == FIELD_DECL && DECL_SIZE (fdecl) 
2900       && integer_zerop (DECL_SIZE (fdecl)))
2901     return true;
2902   return false;
2903 }
2904
2905 /* Return true if TYPE is zero sized.  */
2906    
2907 static bool
2908 zero_sized_type (tree type)
2909 {
2910   if (AGGREGATE_TYPE_P (type) && TYPE_SIZE (type)
2911       && integer_zerop (TYPE_SIZE (type)))
2912     return true;
2913   return false;
2914 }
2915
2916 /* A subroutine of gimplify_init_constructor.  Generate individual
2917    MODIFY_EXPRs for a CONSTRUCTOR.  OBJECT is the LHS against which the
2918    assignments should happen.  ELTS is the CONSTRUCTOR_ELTS of the
2919    CONSTRUCTOR.  CLEARED is true if the entire LHS object has been
2920    zeroed first.  */
2921
2922 static void
2923 gimplify_init_ctor_eval (tree object, VEC(constructor_elt,gc) *elts,
2924                          tree *pre_p, bool cleared)
2925 {
2926   tree array_elt_type = NULL;
2927   unsigned HOST_WIDE_INT ix;
2928   tree purpose, value;
2929
2930   if (TREE_CODE (TREE_TYPE (object)) == ARRAY_TYPE)
2931     array_elt_type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (object)));
2932
2933   FOR_EACH_CONSTRUCTOR_ELT (elts, ix, purpose, value)
2934     {
2935       tree cref, init;
2936
2937       /* NULL values are created above for gimplification errors.  */
2938       if (value == NULL)
2939         continue;
2940
2941       if (cleared && initializer_zerop (value))
2942         continue;
2943
2944       /* ??? Here's to hoping the front end fills in all of the indices,
2945          so we don't have to figure out what's missing ourselves.  */
2946       gcc_assert (purpose);
2947
2948       /* Skip zero-sized fields, unless value has side-effects.  This can
2949          happen with calls to functions returning a zero-sized type, which
2950          we shouldn't discard.  As a number of downstream passes don't
2951          expect sets of zero-sized fields, we rely on the gimplification of
2952          the MODIFY_EXPR we make below to drop the assignment statement.  */
2953       if (! TREE_SIDE_EFFECTS (value) && zero_sized_field_decl (purpose))
2954         continue;
2955
2956       /* If we have a RANGE_EXPR, we have to build a loop to assign the
2957          whole range.  */
2958       if (TREE_CODE (purpose) == RANGE_EXPR)
2959         {
2960           tree lower = TREE_OPERAND (purpose, 0);
2961           tree upper = TREE_OPERAND (purpose, 1);
2962
2963           /* If the lower bound is equal to upper, just treat it as if
2964              upper was the index.  */
2965           if (simple_cst_equal (lower, upper))
2966             purpose = upper;
2967           else
2968             {
2969               gimplify_init_ctor_eval_range (object, lower, upper, value,
2970                                              array_elt_type, pre_p, cleared);
2971               continue;
2972             }
2973         }
2974
2975       if (array_elt_type)
2976         {
2977           cref = build4 (ARRAY_REF, array_elt_type, unshare_expr (object),
2978                          purpose, NULL_TREE, NULL_TREE);
2979         }
2980       else
2981         {
2982           gcc_assert (TREE_CODE (purpose) == FIELD_DECL);
2983           cref = build3 (COMPONENT_REF, TREE_TYPE (purpose),
2984                          unshare_expr (object), purpose, NULL_TREE);
2985         }
2986
2987       if (TREE_CODE (value) == CONSTRUCTOR
2988           && TREE_CODE (TREE_TYPE (value)) != VECTOR_TYPE)
2989         gimplify_init_ctor_eval (cref, CONSTRUCTOR_ELTS (value),
2990                                  pre_p, cleared);
2991       else
2992         {
2993           init = build2 (INIT_EXPR, TREE_TYPE (cref), cref, value);
2994           gimplify_and_add (init, pre_p);
2995         }
2996     }
2997 }
2998
2999 /* A subroutine of gimplify_modify_expr.  Break out elements of a
3000    CONSTRUCTOR used as an initializer into separate MODIFY_EXPRs.
3001
3002    Note that we still need to clear any elements that don't have explicit
3003    initializers, so if not all elements are initialized we keep the
3004    original MODIFY_EXPR, we just remove all of the constructor elements.  */
3005
3006 static enum gimplify_status
3007 gimplify_init_constructor (tree *expr_p, tree *pre_p,
3008                            tree *post_p, bool want_value)
3009 {
3010   tree object;
3011   tree ctor = GENERIC_TREE_OPERAND (*expr_p, 1);
3012   tree type = TREE_TYPE (ctor);
3013   enum gimplify_status ret;
3014   VEC(constructor_elt,gc) *elts;
3015
3016   if (TREE_CODE (ctor) != CONSTRUCTOR)
3017     return GS_UNHANDLED;
3018
3019   ret = gimplify_expr (&GENERIC_TREE_OPERAND (*expr_p, 0), pre_p, post_p,
3020                        is_gimple_lvalue, fb_lvalue);
3021   if (ret == GS_ERROR)
3022     return ret;
3023   object = GENERIC_TREE_OPERAND (*expr_p, 0);
3024
3025   elts = CONSTRUCTOR_ELTS (ctor);
3026
3027   ret = GS_ALL_DONE;
3028   switch (TREE_CODE (type))
3029     {
3030     case RECORD_TYPE:
3031     case UNION_TYPE:
3032     case QUAL_UNION_TYPE:
3033     case ARRAY_TYPE:
3034       {
3035         struct gimplify_init_ctor_preeval_data preeval_data;
3036         HOST_WIDE_INT num_type_elements, num_ctor_elements;
3037         HOST_WIDE_INT num_nonzero_elements;
3038         bool cleared, valid_const_initializer;
3039
3040         /* Aggregate types must lower constructors to initialization of
3041            individual elements.  The exception is that a CONSTRUCTOR node
3042            with no elements indicates zero-initialization of the whole.  */
3043         if (VEC_empty (constructor_elt, elts))
3044           break;
3045
3046         /* Fetch information about the constructor to direct later processing.
3047            We might want to make static versions of it in various cases, and
3048            can only do so if it known to be a valid constant initializer.  */
3049         valid_const_initializer
3050           = categorize_ctor_elements (ctor, &num_nonzero_elements,
3051                                       &num_ctor_elements, &cleared);
3052
3053         /* If a const aggregate variable is being initialized, then it
3054            should never be a lose to promote the variable to be static.  */
3055         if (valid_const_initializer
3056             && num_nonzero_elements > 1
3057             && TREE_READONLY (object)
3058             && TREE_CODE (object) == VAR_DECL)
3059           {
3060             DECL_INITIAL (object) = ctor;
3061             TREE_STATIC (object) = 1;
3062             if (!DECL_NAME (object))
3063               DECL_NAME (object) = create_tmp_var_name ("C");
3064             walk_tree (&DECL_INITIAL (object), force_labels_r, NULL, NULL);
3065
3066             /* ??? C++ doesn't automatically append a .<number> to the
3067                assembler name, and even when it does, it looks a FE private
3068                data structures to figure out what that number should be,
3069                which are not set for this variable.  I suppose this is
3070                important for local statics for inline functions, which aren't
3071                "local" in the object file sense.  So in order to get a unique
3072                TU-local symbol, we must invoke the lhd version now.  */
3073             lhd_set_decl_assembler_name (object);
3074
3075             *expr_p = NULL_TREE;
3076             break;
3077           }
3078
3079         /* If there are "lots" of initialized elements, even discounting
3080            those that are not address constants (and thus *must* be
3081            computed at runtime), then partition the constructor into
3082            constant and non-constant parts.  Block copy the constant
3083            parts in, then generate code for the non-constant parts.  */
3084         /* TODO.  There's code in cp/typeck.c to do this.  */
3085
3086         num_type_elements = count_type_elements (type, true);
3087
3088         /* If count_type_elements could not determine number of type elements
3089            for a constant-sized object, assume clearing is needed.
3090            Don't do this for variable-sized objects, as store_constructor
3091            will ignore the clearing of variable-sized objects.  */
3092         if (num_type_elements < 0 && int_size_in_bytes (type) >= 0)
3093           cleared = true;
3094         /* If there are "lots" of zeros, then block clear the object first.  */
3095         else if (num_type_elements - num_nonzero_elements > CLEAR_RATIO
3096                  && num_nonzero_elements < num_type_elements/4)
3097           cleared = true;
3098         /* ??? This bit ought not be needed.  For any element not present
3099            in the initializer, we should simply set them to zero.  Except
3100            we'd need to *find* the elements that are not present, and that
3101            requires trickery to avoid quadratic compile-time behavior in
3102            large cases or excessive memory use in small cases.  */
3103         else if (num_ctor_elements < num_type_elements)
3104           cleared = true;
3105
3106         /* If there are "lots" of initialized elements, and all of them
3107            are valid address constants, then the entire initializer can
3108            be dropped to memory, and then memcpy'd out.  Don't do this
3109            for sparse arrays, though, as it's more efficient to follow
3110            the standard CONSTRUCTOR behavior of memset followed by
3111            individual element initialization.  */
3112         if (valid_const_initializer && !cleared)
3113           {
3114             HOST_WIDE_INT size = int_size_in_bytes (type);
3115             unsigned int align;
3116
3117             /* ??? We can still get unbounded array types, at least
3118                from the C++ front end.  This seems wrong, but attempt
3119                to work around it for now.  */
3120             if (size < 0)
3121               {
3122                 size = int_size_in_bytes (TREE_TYPE (object));
3123                 if (size >= 0)
3124                   TREE_TYPE (ctor) = type = TREE_TYPE (object);
3125               }
3126
3127             /* Find the maximum alignment we can assume for the object.  */
3128             /* ??? Make use of DECL_OFFSET_ALIGN.  */
3129             if (DECL_P (object))
3130               align = DECL_ALIGN (object);
3131             else
3132               align = TYPE_ALIGN (type);
3133
3134             if (size > 0 && !can_move_by_pieces (size, align))
3135               {
3136                 tree new = create_tmp_var_raw (type, "C");
3137
3138                 gimple_add_tmp_var (new);
3139                 TREE_STATIC (new) = 1;
3140                 TREE_READONLY (new) = 1;
3141                 DECL_INITIAL (new) = ctor;
3142                 if (align > DECL_ALIGN (new))
3143                   {
3144                     DECL_ALIGN (new) = align;
3145                     DECL_USER_ALIGN (new) = 1;
3146                   }
3147                 walk_tree (&DECL_INITIAL (new), force_labels_r, NULL, NULL);
3148
3149                 GENERIC_TREE_OPERAND (*expr_p, 1) = new;
3150
3151                 /* This is no longer an assignment of a CONSTRUCTOR, but
3152                    we still may have processing to do on the LHS.  So
3153                    pretend we didn't do anything here to let that happen.  */
3154                 return GS_UNHANDLED;
3155               }
3156           }
3157
3158         /* If there are nonzero elements, pre-evaluate to capture elements
3159            overlapping with the lhs into temporaries.  We must do this before
3160            clearing to fetch the values before they are zeroed-out.  */
3161         if (num_nonzero_elements > 0)
3162           {
3163             preeval_data.lhs_base_decl = get_base_address (object);
3164             if (!DECL_P (preeval_data.lhs_base_decl))
3165               preeval_data.lhs_base_decl = NULL;
3166             preeval_data.lhs_alias_set = get_alias_set (object);
3167
3168             gimplify_init_ctor_preeval (&GENERIC_TREE_OPERAND (*expr_p, 1),
3169                                         pre_p, post_p, &preeval_data);
3170           }
3171
3172         if (cleared)
3173           {
3174             /* Zap the CONSTRUCTOR element list, which simplifies this case.
3175                Note that we still have to gimplify, in order to handle the
3176                case of variable sized types.  Avoid shared tree structures.  */
3177             CONSTRUCTOR_ELTS (ctor) = NULL;
3178             object = unshare_expr (object);
3179             gimplify_stmt (expr_p);
3180             append_to_statement_list (*expr_p, pre_p);
3181           }
3182
3183         /* If we have not block cleared the object, or if there are nonzero
3184            elements in the constructor, add assignments to the individual
3185            scalar fields of the object.  */
3186         if (!cleared || num_nonzero_elements > 0)
3187           gimplify_init_ctor_eval (object, elts, pre_p, cleared);
3188
3189         *expr_p = NULL_TREE;
3190       }
3191       break;
3192
3193     case COMPLEX_TYPE:
3194       {
3195         tree r, i;
3196
3197         /* Extract the real and imaginary parts out of the ctor.  */
3198         gcc_assert (VEC_length (constructor_elt, elts) == 2);
3199         r = VEC_index (constructor_elt, elts, 0)->value;
3200         i = VEC_index (constructor_elt, elts, 1)->value;
3201         if (r == NULL || i == NULL)
3202           {
3203             tree zero = fold_convert (TREE_TYPE (type), integer_zero_node);
3204             if (r == NULL)
3205               r = zero;
3206             if (i == NULL)
3207               i = zero;
3208           }
3209
3210         /* Complex types have either COMPLEX_CST or COMPLEX_EXPR to
3211            represent creation of a complex value.  */
3212         if (TREE_CONSTANT (r) && TREE_CONSTANT (i))
3213           {
3214             ctor = build_complex (type, r, i);
3215             TREE_OPERAND (*expr_p, 1) = ctor;
3216           }
3217         else
3218           {
3219             ctor = build2 (COMPLEX_EXPR, type, r, i);
3220             TREE_OPERAND (*expr_p, 1) = ctor;
3221             ret = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
3222                                  rhs_predicate_for (TREE_OPERAND (*expr_p, 0)),
3223                                  fb_rvalue);
3224           }
3225       }
3226       break;
3227
3228     case VECTOR_TYPE:
3229       {
3230         unsigned HOST_WIDE_INT ix;
3231         constructor_elt *ce;
3232
3233         /* Go ahead and simplify constant constructors to VECTOR_CST.  */
3234         if (TREE_CONSTANT (ctor))
3235           {
3236             bool constant_p = true;
3237             tree value;
3238
3239             /* Even when ctor is constant, it might contain non-*_CST
3240               elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
3241               belong into VECTOR_CST nodes.  */
3242             FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
3243               if (!CONSTANT_CLASS_P (value))
3244                 {
3245                   constant_p = false;
3246                   break;
3247                 }
3248
3249             if (constant_p)
3250               {
3251                 TREE_OPERAND (*expr_p, 1) = build_vector_from_ctor (type, elts);
3252                 break;
3253               }
3254
3255             /* Don't reduce a TREE_CONSTANT vector ctor even if we can't
3256                make a VECTOR_CST.  It won't do anything for us, and it'll
3257                prevent us from representing it as a single constant.  */
3258             break;
3259           }
3260
3261         /* Vector types use CONSTRUCTOR all the way through gimple
3262           compilation as a general initializer.  */
3263         for (ix = 0; VEC_iterate (constructor_elt, elts, ix, ce); ix++)
3264           {
3265             enum gimplify_status tret;
3266             tret = gimplify_expr (&ce->value, pre_p, post_p,
3267                                   is_gimple_val, fb_rvalue);
3268             if (tret == GS_ERROR)
3269               ret = GS_ERROR;
3270           }
3271         if (!is_gimple_reg (GENERIC_TREE_OPERAND (*expr_p, 0)))
3272           GENERIC_TREE_OPERAND (*expr_p, 1) = get_formal_tmp_var (ctor, pre_p);
3273       }
3274       break;
3275
3276     default:
3277       /* So how did we get a CONSTRUCTOR for a scalar type?  */
3278       gcc_unreachable ();
3279     }
3280
3281   if (ret == GS_ERROR)
3282     return GS_ERROR;
3283   else if (want_value)
3284     {
3285       append_to_statement_list (*expr_p, pre_p);
3286       *expr_p = object;
3287       return GS_OK;
3288     }
3289   else
3290     return GS_ALL_DONE;
3291 }
3292
3293 /* Given a pointer value OP0, return a simplified version of an
3294    indirection through OP0, or NULL_TREE if no simplification is
3295    possible.  This may only be applied to a rhs of an expression.
3296    Note that the resulting type may be different from the type pointed
3297    to in the sense that it is still compatible from the langhooks
3298    point of view. */
3299
3300 static tree
3301 fold_indirect_ref_rhs (tree t)
3302 {
3303   tree type = TREE_TYPE (TREE_TYPE (t));
3304   tree sub = t;
3305   tree subtype;
3306
3307   STRIP_USELESS_TYPE_CONVERSION (sub);
3308   subtype = TREE_TYPE (sub);
3309   if (!POINTER_TYPE_P (subtype))
3310     return NULL_TREE;
3311
3312   if (TREE_CODE (sub) == ADDR_EXPR)
3313     {
3314       tree op = TREE_OPERAND (sub, 0);
3315       tree optype = TREE_TYPE (op);
3316       /* *&p => p */
3317       if (useless_type_conversion_p (type, optype))
3318         return op;
3319       /* *(foo *)&fooarray => fooarray[0] */
3320       else if (TREE_CODE (optype) == ARRAY_TYPE
3321                && useless_type_conversion_p (type, TREE_TYPE (optype)))
3322        {
3323          tree type_domain = TYPE_DOMAIN (optype);
3324          tree min_val = size_zero_node;
3325          if (type_domain && TYPE_MIN_VALUE (type_domain))
3326            min_val = TYPE_MIN_VALUE (type_domain);
3327          return build4 (ARRAY_REF, type, op, min_val, NULL_TREE, NULL_TREE);
3328        }
3329     }
3330
3331   /* *(foo *)fooarrptr => (*fooarrptr)[0] */
3332   if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
3333       && useless_type_conversion_p (type, TREE_TYPE (TREE_TYPE (subtype))))
3334     {
3335       tree type_domain;
3336       tree min_val = size_zero_node;
3337       tree osub = sub;
3338       sub = fold_indirect_ref_rhs (sub);
3339       if (! sub)
3340         sub = build1 (INDIRECT_REF, TREE_TYPE (subtype), osub);
3341       type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
3342       if (type_domain && TYPE_MIN_VALUE (type_domain))
3343         min_val = TYPE_MIN_VALUE (type_domain);
3344       return build4 (ARRAY_REF, type, sub, min_val, NULL_TREE, NULL_TREE);
3345     }
3346
3347   return NULL_TREE;
3348 }
3349
3350 /* Subroutine of gimplify_modify_expr to do simplifications of MODIFY_EXPRs
3351    based on the code of the RHS.  We loop for as long as something changes.  */
3352
3353 static enum gimplify_status
3354 gimplify_modify_expr_rhs (tree *expr_p, tree *from_p, tree *to_p, tree *pre_p,
3355                           tree *post_p, bool want_value)
3356 {
3357   enum gimplify_status ret = GS_OK;
3358
3359   while (ret != GS_UNHANDLED)
3360     switch (TREE_CODE (*from_p))
3361       {
3362       case INDIRECT_REF:
3363         {
3364           /* If we have code like 
3365
3366                 *(const A*)(A*)&x
3367
3368              where the type of "x" is a (possibly cv-qualified variant
3369              of "A"), treat the entire expression as identical to "x".
3370              This kind of code arises in C++ when an object is bound
3371              to a const reference, and if "x" is a TARGET_EXPR we want
3372              to take advantage of the optimization below.  */
3373           tree t = fold_indirect_ref_rhs (TREE_OPERAND (*from_p, 0));
3374           if (t)
3375             {
3376               *from_p = t;
3377               ret = GS_OK;
3378             }
3379           else
3380             ret = GS_UNHANDLED;
3381           break;
3382         }
3383
3384       case TARGET_EXPR:
3385         {
3386           /* If we are initializing something from a TARGET_EXPR, strip the
3387              TARGET_EXPR and initialize it directly, if possible.  This can't
3388              be done if the initializer is void, since that implies that the
3389              temporary is set in some non-trivial way.
3390
3391              ??? What about code that pulls out the temp and uses it
3392              elsewhere? I think that such code never uses the TARGET_EXPR as
3393              an initializer.  If I'm wrong, we'll die because the temp won't
3394              have any RTL.  In that case, I guess we'll need to replace
3395              references somehow.  */
3396           tree init = TARGET_EXPR_INITIAL (*from_p);
3397
3398           if (!VOID_TYPE_P (TREE_TYPE (init)))
3399             {
3400               *from_p = init;
3401               ret = GS_OK;
3402             }
3403           else
3404             ret = GS_UNHANDLED;
3405         }
3406         break;
3407
3408       case COMPOUND_EXPR:
3409         /* Remove any COMPOUND_EXPR in the RHS so the following cases will be
3410            caught.  */
3411         gimplify_compound_expr (from_p, pre_p, true);
3412         ret = GS_OK;
3413         break;
3414
3415       case CONSTRUCTOR:
3416         /* If we're initializing from a CONSTRUCTOR, break this into
3417            individual MODIFY_EXPRs.  */
3418         return gimplify_init_constructor (expr_p, pre_p, post_p, want_value);
3419
3420       case COND_EXPR:
3421         /* If we're assigning to a non-register type, push the assignment
3422            down into the branches.  This is mandatory for ADDRESSABLE types,
3423            since we cannot generate temporaries for such, but it saves a
3424            copy in other cases as well.  */
3425         if (!is_gimple_reg_type (TREE_TYPE (*from_p)))
3426           {
3427             /* This code should mirror the code in gimplify_cond_expr. */
3428             enum tree_code code = TREE_CODE (*expr_p);
3429             tree cond = *from_p;
3430             tree result = *to_p;
3431
3432             ret = gimplify_expr (&result, pre_p, post_p,
3433                                  is_gimple_min_lval, fb_lvalue);
3434             if (ret != GS_ERROR)
3435               ret = GS_OK;
3436
3437             if (TREE_TYPE (TREE_OPERAND (cond, 1)) != void_type_node)
3438               TREE_OPERAND (cond, 1)
3439                 = build2 (code, void_type_node, result,
3440                           TREE_OPERAND (cond, 1));
3441             if (TREE_TYPE (TREE_OPERAND (cond, 2)) != void_type_node)
3442               TREE_OPERAND (cond, 2)
3443                 = build2 (code, void_type_node, unshare_expr (result),
3444                           TREE_OPERAND (cond, 2));
3445
3446             TREE_TYPE (cond) = void_type_node;
3447             recalculate_side_effects (cond);
3448
3449             if (want_value)
3450               {
3451                 gimplify_and_add (cond, pre_p);
3452                 *expr_p = unshare_expr (result);
3453               }
3454             else
3455               *expr_p = cond;
3456             return ret;
3457           }
3458         else
3459           ret = GS_UNHANDLED;
3460         break;
3461
3462       case CALL_EXPR:
3463         /* For calls that return in memory, give *to_p as the CALL_EXPR's
3464            return slot so that we don't generate a temporary.  */
3465         if (!CALL_EXPR_RETURN_SLOT_OPT (*from_p)
3466             && aggregate_value_p (*from_p, *from_p))
3467           {
3468             bool use_target;
3469
3470             if (!(rhs_predicate_for (*to_p))(*from_p))
3471               /* If we need a temporary, *to_p isn't accurate.  */
3472               use_target = false;
3473             else if (TREE_CODE (*to_p) == RESULT_DECL
3474                      && DECL_NAME (*to_p) == NULL_TREE
3475                      && needs_to_live_in_memory (*to_p))
3476               /* It's OK to use the return slot directly unless it's an NRV. */
3477               use_target = true;
3478             else if (is_gimple_reg_type (TREE_TYPE (*to_p))
3479                      || (DECL_P (*to_p) && DECL_REGISTER (*to_p)))
3480               /* Don't force regs into memory.  */
3481               use_target = false;
3482             else if (TREE_CODE (*to_p) == VAR_DECL
3483                      && DECL_GIMPLE_FORMAL_TEMP_P (*to_p))
3484               /* Don't use the original target if it's a formal temp; we
3485                  don't want to take their addresses.  */
3486               use_target = false;
3487             else if (TREE_CODE (*expr_p) == INIT_EXPR)
3488               /* It's OK to use the target directly if it's being
3489                  initialized. */
3490               use_target = true;
3491             else if (!is_gimple_non_addressable (*to_p))
3492               /* Don't use the original target if it's already addressable;
3493                  if its address escapes, and the called function uses the
3494                  NRV optimization, a conforming program could see *to_p
3495                  change before the called function returns; see c++/19317.
3496                  When optimizing, the return_slot pass marks more functions
3497                  as safe after we have escape info.  */
3498               use_target = false;
3499             else
3500               use_target = true;
3501
3502             if (use_target)
3503               {
3504                 CALL_EXPR_RETURN_SLOT_OPT (*from_p) = 1;
3505                 mark_addressable (*to_p);
3506               }
3507           }
3508
3509         ret = GS_UNHANDLED;
3510         break;
3511
3512         /* If we're initializing from a container, push the initialization
3513            inside it.  */
3514       case CLEANUP_POINT_EXPR:
3515       case BIND_EXPR:
3516       case STATEMENT_LIST:
3517         {
3518           tree wrap = *from_p;
3519           tree t;
3520
3521           ret = gimplify_expr (to_p, pre_p, post_p,
3522                                is_gimple_min_lval, fb_lvalue);
3523           if (ret != GS_ERROR)
3524             ret = GS_OK;
3525
3526           t = voidify_wrapper_expr (wrap, *expr_p);
3527           gcc_assert (t == *expr_p);
3528
3529           if (want_value)
3530             {
3531               gimplify_and_add (wrap, pre_p);
3532               *expr_p = unshare_expr (*to_p);
3533             }
3534           else
3535             *expr_p = wrap;
3536           return GS_OK;
3537         }
3538         
3539       default:
3540         ret = GS_UNHANDLED;
3541         break;
3542       }
3543
3544   return ret;
3545 }
3546
3547 /* Destructively convert the TREE pointer in TP into a gimple tuple if
3548    appropriate.  */
3549
3550 static void
3551 tree_to_gimple_tuple (tree *tp)
3552 {
3553
3554   switch (TREE_CODE (*tp))
3555     {
3556     case GIMPLE_MODIFY_STMT:
3557       return;
3558     case MODIFY_EXPR:
3559       {
3560         struct gimple_stmt *gs;
3561         tree lhs = TREE_OPERAND (*tp, 0);
3562         bool def_stmt_self_p = false;
3563
3564         if (TREE_CODE (lhs) == SSA_NAME)
3565           {
3566             if (SSA_NAME_DEF_STMT (lhs) == *tp)
3567               def_stmt_self_p = true;
3568           }
3569
3570         gs = &make_node (GIMPLE_MODIFY_STMT)->gstmt;
3571         gs->base = (*tp)->base;
3572         /* The set to base above overwrites the CODE.  */
3573         TREE_SET_CODE ((tree) gs, GIMPLE_MODIFY_STMT);
3574
3575         gs->locus = EXPR_LOCUS (*tp);
3576         gs->operands[0] = TREE_OPERAND (*tp, 0);
3577         gs->operands[1] = TREE_OPERAND (*tp, 1);
3578         gs->block = TREE_BLOCK (*tp);
3579         *tp = (tree)gs;
3580
3581         /* If we re-gimplify a set to an SSA_NAME, we must change the
3582            SSA name's DEF_STMT link.  */
3583         if (def_stmt_self_p)
3584           SSA_NAME_DEF_STMT (GIMPLE_STMT_OPERAND (*tp, 0)) = *tp;
3585
3586         return;
3587       }
3588     default:
3589       break;
3590     }
3591 }
3592
3593 /* Promote partial stores to COMPLEX variables to total stores.  *EXPR_P is
3594    a MODIFY_EXPR with a lhs of a REAL/IMAGPART_EXPR of a variable with
3595    DECL_GIMPLE_REG_P set.  */
3596
3597 static enum gimplify_status
3598 gimplify_modify_expr_complex_part (tree *expr_p, tree *pre_p, bool want_value)
3599 {
3600   enum tree_code code, ocode;
3601   tree lhs, rhs, new_rhs, other, realpart, imagpart;
3602
3603   lhs = GENERIC_TREE_OPERAND (*expr_p, 0);
3604   rhs = GENERIC_TREE_OPERAND (*expr_p, 1);
3605   code = TREE_CODE (lhs);
3606   lhs = TREE_OPERAND (lhs, 0);
3607
3608   ocode = code == REALPART_EXPR ? IMAGPART_EXPR : REALPART_EXPR;
3609   other = build1 (ocode, TREE_TYPE (rhs), lhs);
3610   other = get_formal_tmp_var (other, pre_p);
3611
3612   realpart = code == REALPART_EXPR ? rhs : other;
3613   imagpart = code == REALPART_EXPR ? other : rhs;
3614
3615   if (TREE_CONSTANT (realpart) && TREE_CONSTANT (imagpart))
3616     new_rhs = build_complex (TREE_TYPE (lhs), realpart, imagpart);
3617   else
3618     new_rhs = build2 (COMPLEX_EXPR, TREE_TYPE (lhs), realpart, imagpart);
3619
3620   GENERIC_TREE_OPERAND (*expr_p, 0) = lhs;
3621   GENERIC_TREE_OPERAND (*expr_p, 1) = new_rhs;
3622
3623   if (want_value)
3624     {
3625       tree_to_gimple_tuple (expr_p);
3626
3627       append_to_statement_list (*expr_p, pre_p);
3628       *expr_p = rhs;
3629     }
3630
3631   return GS_ALL_DONE;
3632 }
3633
3634 /* Gimplify the MODIFY_EXPR node pointed to by EXPR_P.
3635
3636       modify_expr
3637               : varname '=' rhs
3638               | '*' ID '=' rhs
3639
3640     PRE_P points to the list where side effects that must happen before
3641         *EXPR_P should be stored.
3642
3643     POST_P points to the list where side effects that must happen after
3644         *EXPR_P should be stored.
3645
3646     WANT_VALUE is nonzero iff we want to use the value of this expression
3647         in another expression.  */
3648
3649 static enum gimplify_status
3650 gimplify_modify_expr (tree *expr_p, tree *pre_p, tree *post_p, bool want_value)
3651 {
3652   tree *from_p = &GENERIC_TREE_OPERAND (*expr_p, 1);
3653   tree *to_p = &GENERIC_TREE_OPERAND (*expr_p, 0);
3654   enum gimplify_status ret = GS_UNHANDLED;
3655
3656   gcc_assert (TREE_CODE (*expr_p) == MODIFY_EXPR
3657               || TREE_CODE (*expr_p) == GIMPLE_MODIFY_STMT
3658               || TREE_CODE (*expr_p) == INIT_EXPR);
3659
3660   /* For zero sized types only gimplify the left hand side and right hand side
3661      as statements and throw away the assignment.  */
3662   if (zero_sized_type (TREE_TYPE (*from_p)))
3663     {
3664       gimplify_stmt (from_p);
3665       gimplify_stmt (to_p);
3666       append_to_statement_list (*from_p, pre_p);
3667       append_to_statement_list (*to_p, pre_p);
3668       *expr_p = NULL_TREE;
3669       return GS_ALL_DONE;
3670     }
3671
3672   /* See if any simplifications can be done based on what the RHS is.  */
3673   ret = gimplify_modify_expr_rhs (expr_p, from_p, to_p, pre_p, post_p,
3674                                   want_value);
3675   if (ret != GS_UNHANDLED)
3676     return ret;
3677
3678   /* If the value being copied is of variable width, compute the length
3679      of the copy into a WITH_SIZE_EXPR.   Note that we need to do this
3680      before gimplifying any of the operands so that we can resolve any
3681      PLACEHOLDER_EXPRs in the size.  Also note that the RTL expander uses
3682      the size of the expression to be copied, not of the destination, so
3683      that is what we must here.  */
3684   maybe_with_size_expr (from_p);
3685
3686   ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
3687   if (ret == GS_ERROR)
3688     return ret;
3689
3690   ret = gimplify_expr (from_p, pre_p, post_p,
3691                        rhs_predicate_for (*to_p), fb_rvalue);
3692   if (ret == GS_ERROR)
3693     return ret;
3694
3695   /* Now see if the above changed *from_p to something we handle specially.  */
3696   ret = gimplify_modify_expr_rhs (expr_p, from_p, to_p, pre_p, post_p,
3697                                   want_value);
3698   if (ret != GS_UNHANDLED)
3699     return ret;
3700
3701   /* If we've got a variable sized assignment between two lvalues (i.e. does
3702      not involve a call), then we can make things a bit more straightforward
3703      by converting the assignment to memcpy or memset.  */
3704   if (TREE_CODE (*from_p) == WITH_SIZE_EXPR)
3705     {
3706       tree from = TREE_OPERAND (*from_p, 0);
3707       tree size = TREE_OPERAND (*from_p, 1);
3708
3709       if (TREE_CODE (from) == CONSTRUCTOR)
3710         return gimplify_modify_expr_to_memset (expr_p, size, want_value);
3711       if (is_gimple_addressable (from))
3712         {
3713           *from_p = from;
3714           return gimplify_modify_expr_to_memcpy (expr_p, size, want_value);
3715         }
3716     }
3717
3718   /* Transform partial stores to non-addressable complex variables into
3719      total stores.  This allows us to use real instead of virtual operands
3720      for these variables, which improves optimization.  */
3721   if ((TREE_CODE (*to_p) == REALPART_EXPR
3722        || TREE_CODE (*to_p) == IMAGPART_EXPR)
3723       && is_gimple_reg (TREE_OPERAND (*to_p, 0)))
3724     return gimplify_modify_expr_complex_part (expr_p, pre_p, want_value);
3725
3726   if (gimplify_ctxp->into_ssa && is_gimple_reg (*to_p))
3727     {
3728       /* If we've somehow already got an SSA_NAME on the LHS, then
3729          we're probably modified it twice.  Not good.  */
3730       gcc_assert (TREE_CODE (*to_p) != SSA_NAME);
3731       *to_p = make_ssa_name (*to_p, *expr_p);
3732     }
3733
3734   /* Try to alleviate the effects of the gimplification creating artificial
3735      temporaries (see for example is_gimple_reg_rhs) on the debug info.  */
3736   if (!gimplify_ctxp->into_ssa
3737       && DECL_P (*from_p) && DECL_IGNORED_P (*from_p)
3738       && DECL_P (*to_p) && !DECL_IGNORED_P (*to_p))
3739     {
3740       if (!DECL_NAME (*from_p) && DECL_NAME (*to_p))
3741         DECL_NAME (*from_p)
3742           = create_tmp_var_name (IDENTIFIER_POINTER (DECL_NAME (*to_p)));
3743       DECL_DEBUG_EXPR_IS_FROM (*from_p) = 1;
3744       SET_DECL_DEBUG_EXPR (*from_p, *to_p);
3745     }
3746
3747   if (want_value)
3748     {
3749       tree_to_gimple_tuple (expr_p);
3750
3751       append_to_statement_list (*expr_p, pre_p);
3752       *expr_p = *to_p;
3753       return GS_OK;
3754     }
3755
3756   return GS_ALL_DONE;
3757 }
3758
3759 /*  Gimplify a comparison between two variable-sized objects.  Do this
3760     with a call to BUILT_IN_MEMCMP.  */
3761
3762 static enum gimplify_status
3763 gimplify_variable_sized_compare (tree *expr_p)
3764 {
3765   tree op0 = TREE_OPERAND (*expr_p, 0);
3766   tree op1 = TREE_OPERAND (*expr_p, 1);
3767   tree t, arg, dest, src;
3768
3769   arg = TYPE_SIZE_UNIT (TREE_TYPE (op0));
3770   arg = unshare_expr (arg);
3771   arg = SUBSTITUTE_PLACEHOLDER_IN_EXPR (arg, op0);
3772   src = build_fold_addr_expr (op1);
3773   dest = build_fold_addr_expr (op0);
3774   t = implicit_built_in_decls[BUILT_IN_MEMCMP];
3775   t = build_call_expr (t, 3, dest, src, arg);
3776   *expr_p
3777     = build2 (TREE_CODE (*expr_p), TREE_TYPE (*expr_p), t, integer_zero_node);
3778
3779   return GS_OK;
3780 }
3781
3782 /*  Gimplify a comparison between two aggregate objects of integral scalar
3783     mode as a comparison between the bitwise equivalent scalar values.  */
3784
3785 static enum gimplify_status
3786 gimplify_scalar_mode_aggregate_compare (tree *expr_p)
3787 {
3788   tree op0 = TREE_OPERAND (*expr_p, 0);
3789   tree op1 = TREE_OPERAND (*expr_p, 1);
3790
3791   tree type = TREE_TYPE (op0);
3792   tree scalar_type = lang_hooks.types.type_for_mode (TYPE_MODE (type), 1);
3793
3794   op0 = fold_build1 (VIEW_CONVERT_EXPR, scalar_type, op0);
3795   op1 = fold_build1 (VIEW_CONVERT_EXPR, scalar_type, op1);
3796
3797   *expr_p
3798     = fold_build2 (TREE_CODE (*expr_p), TREE_TYPE (*expr_p), op0, op1);
3799
3800   return GS_OK;
3801 }
3802
3803 /*  Gimplify TRUTH_ANDIF_EXPR and TRUTH_ORIF_EXPR expressions.  EXPR_P
3804     points to the expression to gimplify.
3805
3806     Expressions of the form 'a && b' are gimplified to:
3807
3808         a && b ? true : false
3809
3810     gimplify_cond_expr will do the rest.
3811
3812     PRE_P points to the list where side effects that must happen before
3813         *EXPR_P should be stored.  */
3814
3815 static enum gimplify_status
3816 gimplify_boolean_expr (tree *expr_p)
3817 {
3818   /* Preserve the original type of the expression.  */
3819   tree type = TREE_TYPE (*expr_p);
3820
3821   *expr_p = build3 (COND_EXPR, type, *expr_p,
3822                     fold_convert (type, boolean_true_node),
3823                     fold_convert (type, boolean_false_node));
3824
3825   return GS_OK;
3826 }
3827
3828 /* Gimplifies an expression sequence.  This function gimplifies each
3829    expression and re-writes the original expression with the last
3830    expression of the sequence in GIMPLE form.
3831
3832    PRE_P points to the list where the side effects for all the
3833        expressions in the sequence will be emitted.
3834
3835    WANT_VALUE is true when the result of the last COMPOUND_EXPR is used.  */
3836 /* ??? Should rearrange to share the pre-queue with all the indirect
3837    invocations of gimplify_expr.  Would probably save on creations
3838    of statement_list nodes.  */
3839
3840 static enum gimplify_status
3841 gimplify_compound_expr (tree *expr_p, tree *pre_p, bool want_value)
3842 {
3843   tree t = *expr_p;
3844
3845   do
3846     {
3847       tree *sub_p = &TREE_OPERAND (t, 0);
3848
3849       if (TREE_CODE (*sub_p) == COMPOUND_EXPR)
3850         gimplify_compound_expr (sub_p, pre_p, false);
3851       else
3852         gimplify_stmt (sub_p);
3853       append_to_statement_list (*sub_p, pre_p);
3854
3855       t = TREE_OPERAND (t, 1);
3856     }
3857   while (TREE_CODE (t) == COMPOUND_EXPR);
3858
3859   *expr_p = t;
3860   if (want_value)
3861     return GS_OK;
3862   else
3863     {
3864       gimplify_stmt (expr_p);
3865       return GS_ALL_DONE;
3866     }
3867 }
3868
3869 /* Gimplifies a statement list.  These may be created either by an
3870    enlightened front-end, or by shortcut_cond_expr.  */
3871
3872 static enum gimplify_status
3873 gimplify_statement_list (tree *expr_p, tree *pre_p)
3874 {
3875   tree temp = voidify_wrapper_expr (*expr_p, NULL);
3876
3877   tree_stmt_iterator i = tsi_start (*expr_p);
3878
3879   while (!tsi_end_p (i))
3880     {
3881       tree t;
3882
3883       gimplify_stmt (tsi_stmt_ptr (i));
3884
3885       t = tsi_stmt (i);
3886       if (t == NULL)
3887         tsi_delink (&i);
3888       else if (TREE_CODE (t) == STATEMENT_LIST)
3889         {
3890           tsi_link_before (&i, t, TSI_SAME_STMT);
3891           tsi_delink (&i);
3892         }
3893       else
3894         tsi_next (&i);
3895     }
3896
3897   if (temp)
3898     {
3899       append_to_statement_list (*expr_p, pre_p);
3900       *expr_p = temp;
3901       return GS_OK;
3902     }
3903
3904   return GS_ALL_DONE;
3905 }
3906
3907 /*  Gimplify a SAVE_EXPR node.  EXPR_P points to the expression to
3908     gimplify.  After gimplification, EXPR_P will point to a new temporary
3909     that holds the original value of the SAVE_EXPR node.
3910
3911     PRE_P points to the list where side effects that must happen before
3912         *EXPR_P should be stored.  */
3913
3914 static enum gimplify_status
3915 gimplify_save_expr (tree *expr_p, tree *pre_p, tree *post_p)
3916 {
3917   enum gimplify_status ret = GS_ALL_DONE;
3918   tree val;
3919
3920   gcc_assert (TREE_CODE (*expr_p) == SAVE_EXPR);
3921   val = TREE_OPERAND (*expr_p, 0);
3922
3923   /* If the SAVE_EXPR has not been resolved, then evaluate it once.  */
3924   if (!SAVE_EXPR_RESOLVED_P (*expr_p))
3925     {
3926       /* The operand may be a void-valued expression such as SAVE_EXPRs
3927          generated by the Java frontend for class initialization.  It is
3928          being executed only for its side-effects.  */
3929       if (TREE_TYPE (val) == void_type_node)
3930         {
3931           ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
3932                                is_gimple_stmt, fb_none);
3933           append_to_statement_list (TREE_OPERAND (*expr_p, 0), pre_p);
3934           val = NULL;
3935         }
3936       else
3937         val = get_initialized_tmp_var (val, pre_p, post_p);
3938
3939       TREE_OPERAND (*expr_p, 0) = val;
3940       SAVE_EXPR_RESOLVED_P (*expr_p) = 1;
3941     }
3942
3943   *expr_p = val;
3944
3945   return ret;
3946 }
3947
3948 /*  Re-write the ADDR_EXPR node pointed to by EXPR_P
3949
3950       unary_expr
3951               : ...
3952               | '&' varname
3953               ...
3954
3955     PRE_P points to the list where side effects that must happen before
3956         *EXPR_P should be stored.
3957
3958     POST_P points to the list where side effects that must happen after
3959         *EXPR_P should be stored.  */
3960
3961 static enum gimplify_status
3962 gimplify_addr_expr (tree *expr_p, tree *pre_p, tree *post_p)
3963 {
3964   tree expr = *expr_p;
3965   tree op0 = TREE_OPERAND (expr, 0);
3966   enum gimplify_status ret;
3967
3968   switch (TREE_CODE (op0))
3969     {
3970     case INDIRECT_REF:
3971     case MISALIGNED_INDIRECT_REF:
3972     do_indirect_ref:
3973       /* Check if we are dealing with an expression of the form '&*ptr'.
3974          While the front end folds away '&*ptr' into 'ptr', these
3975          expressions may be generated internally by the compiler (e.g.,
3976          builtins like __builtin_va_end).  */
3977       /* Caution: the silent array decomposition semantics we allow for
3978          ADDR_EXPR means we can't always discard the pair.  */
3979       /* Gimplification of the ADDR_EXPR operand may drop
3980          cv-qualification conversions, so make sure we add them if
3981          needed.  */
3982       {
3983         tree op00 = TREE_OPERAND (op0, 0);
3984         tree t_expr = TREE_TYPE (expr);
3985         tree t_op00 = TREE_TYPE (op00);
3986
3987         if (!useless_type_conversion_p (t_expr, t_op00))
3988           {
3989 #ifdef ENABLE_CHECKING
3990             tree t_op0 = TREE_TYPE (op0);
3991             gcc_assert (POINTER_TYPE_P (t_expr)
3992                         && (cpt_same_type (TREE_TYPE (t_expr), t_op0)
3993                             || (TREE_CODE (t_op0) == ARRAY_TYPE
3994                                 && cpt_same_type (TREE_TYPE (t_expr),
3995                                                   TREE_TYPE (t_op0))))
3996                         && POINTER_TYPE_P (t_op00)
3997                         && cpt_same_type (t_op0, TREE_TYPE (t_op00)));
3998 #endif
3999             op00 = fold_convert (TREE_TYPE (expr), op00);
4000           }
4001         *expr_p = op00;
4002         ret = GS_OK;
4003       }
4004       break;
4005
4006     case VIEW_CONVERT_EXPR:
4007       /* Take the address of our operand and then convert it to the type of
4008          this ADDR_EXPR.
4009
4010          ??? The interactions of VIEW_CONVERT_EXPR and aliasing is not at
4011          all clear.  The impact of this transformation is even less clear.  */
4012
4013       /* If the operand is a useless conversion, look through it.  Doing so
4014          guarantees that the ADDR_EXPR and its operand will remain of the
4015          same type.  */
4016       if (tree_ssa_useless_type_conversion (TREE_OPERAND (op0, 0)))
4017         op0 = TREE_OPERAND (op0, 0);
4018
4019       *expr_p = fold_convert (TREE_TYPE (expr),
4020                               build_fold_addr_expr (TREE_OPERAND (op0, 0)));
4021       ret = GS_OK;
4022       break;
4023
4024     default:
4025       /* We use fb_either here because the C frontend sometimes takes
4026          the address of a call that returns a struct; see
4027          gcc.dg/c99-array-lval-1.c.  The gimplifier will correctly make
4028          the implied temporary explicit.  */
4029
4030       /* Mark the RHS addressable.  */
4031       ret = gimplify_expr (&TREE_OPERAND (expr, 0), pre_p, post_p,
4032                            is_gimple_addressable, fb_either);
4033       if (ret != GS_ERROR)
4034         {
4035           op0 = TREE_OPERAND (expr, 0);
4036
4037           /* For various reasons, the gimplification of the expression
4038              may have made a new INDIRECT_REF.  */
4039           if (TREE_CODE (op0) == INDIRECT_REF)
4040             goto do_indirect_ref;
4041
4042           /* Make sure TREE_INVARIANT, TREE_CONSTANT, and TREE_SIDE_EFFECTS
4043              is set properly.  */
4044           recompute_tree_invariant_for_addr_expr (expr);
4045
4046           mark_addressable (TREE_OPERAND (expr, 0));
4047         }
4048       break;
4049     }
4050
4051   return ret;
4052 }
4053
4054 /* Gimplify the operands of an ASM_EXPR.  Input operands should be a gimple
4055    value; output operands should be a gimple lvalue.  */
4056
4057 static enum gimplify_status
4058 gimplify_asm_expr (tree *expr_p, tree *pre_p, tree *post_p)
4059 {
4060   tree expr = *expr_p;
4061   int noutputs = list_length (ASM_OUTPUTS (expr));
4062   const char **oconstraints
4063     = (const char **) alloca ((noutputs) * sizeof (const char *));
4064   int i;
4065   tree link;
4066   const char *constraint;
4067   bool allows_mem, allows_reg, is_inout;
4068   enum gimplify_status ret, tret;
4069
4070   ret = GS_ALL_DONE;
4071   for (i = 0, link = ASM_OUTPUTS (expr); link; ++i, link = TREE_CHAIN (link))
4072     {
4073       size_t constraint_len;
4074       oconstraints[i] = constraint
4075         = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
4076       constraint_len = strlen (constraint);
4077       if (constraint_len == 0)
4078         continue;
4079
4080       parse_output_constraint (&constraint, i, 0, 0,
4081                                &allows_mem, &allows_reg, &is_inout);
4082
4083       if (!allows_reg && allows_mem)
4084         mark_addressable (TREE_VALUE (link));
4085
4086       tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
4087                             is_inout ? is_gimple_min_lval : is_gimple_lvalue,
4088                             fb_lvalue | fb_mayfail);
4089       if (tret == GS_ERROR)
4090         {
4091           error ("invalid lvalue in asm output %d", i);
4092           ret = tret;
4093         }
4094
4095       if (is_inout)
4096         {
4097           /* An input/output operand.  To give the optimizers more
4098              flexibility, split it into separate input and output
4099              operands.  */
4100           tree input;
4101           char buf[10];
4102
4103           /* Turn the in/out constraint into an output constraint.  */
4104           char *p = xstrdup (constraint);
4105           p[0] = '=';
4106           TREE_VALUE (TREE_PURPOSE (link)) = build_string (constraint_len, p);
4107
4108           /* And add a matching input constraint.  */
4109           if (allows_reg)
4110             {
4111               sprintf (buf, "%d", i);
4112
4113               /* If there are multiple alternatives in the constraint,
4114                  handle each of them individually.  Those that allow register
4115                  will be replaced with operand number, the others will stay
4116                  unchanged.  */
4117               if (strchr (p, ',') != NULL)
4118                 {
4119                   size_t len = 0, buflen = strlen (buf);
4120                   char *beg, *end, *str, *dst;
4121
4122                   for (beg = p + 1;;)
4123                     {
4124                       end = strchr (beg, ',');
4125                       if (end == NULL)
4126                         end = strchr (beg, '\0');
4127                       if ((size_t) (end - beg) < buflen)
4128                         len += buflen + 1;
4129                       else
4130                         len += end - beg + 1;
4131                       if (*end)
4132                         beg = end + 1;
4133                       else
4134                         break;
4135                     }
4136
4137                   str = (char *) alloca (len);
4138                   for (beg = p + 1, dst = str;;)
4139                     {
4140                       const char *tem;
4141                       bool mem_p, reg_p, inout_p;
4142
4143                       end = strchr (beg, ',');
4144                       if (end)
4145                         *end = '\0';
4146                       beg[-1] = '=';
4147                       tem = beg - 1;
4148                       parse_output_constraint (&tem, i, 0, 0,
4149                                                &mem_p, &reg_p, &inout_p);
4150                       if (dst != str)
4151                         *dst++ = ',';
4152                       if (reg_p)
4153                         {
4154                           memcpy (dst, buf, buflen);
4155                           dst += buflen;
4156                         }
4157                       else
4158                         {
4159                           if (end)
4160                             len = end - beg;
4161                           else
4162                             len = strlen (beg);
4163                           memcpy (dst, beg, len);
4164                           dst += len;
4165                         }
4166                       if (end)
4167                         beg = end + 1;
4168                       else
4169                         break;
4170                     }
4171                   *dst = '\0';
4172                   input = build_string (dst - str, str);
4173                 }
4174               else
4175                 input = build_string (strlen (buf), buf);
4176             }
4177           else
4178             input = build_string (constraint_len - 1, constraint + 1);
4179
4180           free (p);
4181
4182           input = build_tree_list (build_tree_list (NULL_TREE, input),
4183                                    unshare_expr (TREE_VALUE (link)));
4184           ASM_INPUTS (expr) = chainon (ASM_INPUTS (expr), input);
4185         }
4186     }
4187
4188   for (link = ASM_INPUTS (expr); link; ++i, link = TREE_CHAIN (link))
4189     {
4190       constraint
4191         = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
4192       parse_input_constraint (&constraint, 0, 0, noutputs, 0,
4193                               oconstraints, &allows_mem, &allows_reg);
4194
4195       /* If we can't make copies, we can only accept memory.  */
4196       if (TREE_ADDRESSABLE (TREE_TYPE (TREE_VALUE (link))))
4197         {
4198           if (allows_mem)
4199             allows_reg = 0;
4200           else
4201             {
4202               error ("impossible constraint in %<asm%>");
4203               error ("non-memory input %d must stay in memory", i);
4204               return GS_ERROR;
4205             }
4206         }
4207
4208       /* If the operand is a memory input, it should be an lvalue.  */
4209       if (!allows_reg && allows_mem)
4210         {
4211           tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
4212                                 is_gimple_lvalue, fb_lvalue | fb_mayfail);
4213           mark_addressable (TREE_VALUE (link));
4214           if (tret == GS_ERROR)
4215             {
4216               error ("memory input %d is not directly addressable", i);
4217               ret = tret;
4218             }
4219         }
4220       else
4221         {
4222           tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
4223                                 is_gimple_asm_val, fb_rvalue);
4224           if (tret == GS_ERROR)
4225             ret = tret;
4226         }
4227     }
4228
4229   return ret;
4230 }
4231
4232 /* Gimplify a CLEANUP_POINT_EXPR.  Currently this works by adding
4233    WITH_CLEANUP_EXPRs to the prequeue as we encounter cleanups while
4234    gimplifying the body, and converting them to TRY_FINALLY_EXPRs when we
4235    return to this function.
4236
4237    FIXME should we complexify the prequeue handling instead?  Or use flags
4238    for all the cleanups and let the optimizer tighten them up?  The current
4239    code seems pretty fragile; it will break on a cleanup within any
4240    non-conditional nesting.  But any such nesting would be broken, anyway;
4241    we can't write a TRY_FINALLY_EXPR that starts inside a nesting construct
4242    and continues out of it.  We can do that at the RTL level, though, so
4243    having an optimizer to tighten up try/finally regions would be a Good
4244    Thing.  */
4245
4246 static enum gimplify_status
4247 gimplify_cleanup_point_expr (tree *expr_p, tree *pre_p)
4248 {
4249   tree_stmt_iterator iter;
4250   tree body;
4251
4252   tree temp = voidify_wrapper_expr (*expr_p, NULL);
4253
4254   /* We only care about the number of conditions between the innermost
4255      CLEANUP_POINT_EXPR and the cleanup.  So save and reset the count and
4256      any cleanups collected outside the CLEANUP_POINT_EXPR.  */
4257   int old_conds = gimplify_ctxp->conditions;
4258   tree old_cleanups = gimplify_ctxp->conditional_cleanups;
4259   gimplify_ctxp->conditions = 0;
4260   gimplify_ctxp->conditional_cleanups = NULL_TREE;
4261
4262   body = TREE_OPERAND (*expr_p, 0);
4263   gimplify_to_stmt_list (&body);
4264
4265   gimplify_ctxp->conditions = old_conds;
4266   gimplify_ctxp->conditional_cleanups = old_cleanups;
4267
4268   for (iter = tsi_start (body); !tsi_end_p (iter); )
4269     {
4270       tree *wce_p = tsi_stmt_ptr (iter);
4271       tree wce = *wce_p;
4272
4273       if (TREE_CODE (wce) == WITH_CLEANUP_EXPR)
4274         {
4275           if (tsi_one_before_end_p (iter))
4276             {
4277               tsi_link_before (&iter, TREE_OPERAND (wce, 0), TSI_SAME_STMT);
4278               tsi_delink (&iter);
4279               break;
4280             }
4281           else
4282             {
4283               tree sl, tfe;
4284               enum tree_code code;
4285
4286               if (CLEANUP_EH_ONLY (wce))
4287                 code = TRY_CATCH_EXPR;
4288               else
4289                 code = TRY_FINALLY_EXPR;
4290
4291               sl = tsi_split_statement_list_after (&iter);
4292               tfe = build2 (code, void_type_node, sl, NULL_TREE);
4293               append_to_statement_list (TREE_OPERAND (wce, 0),
4294                                         &TREE_OPERAND (tfe, 1));
4295               *wce_p = tfe;
4296               iter = tsi_start (sl);
4297             }
4298         }
4299       else
4300         tsi_next (&iter);
4301     }
4302
4303   if (temp)
4304     {
4305       *expr_p = temp;
4306       append_to_statement_list (body, pre_p);
4307       return GS_OK;
4308     }
4309   else
4310     {
4311       *expr_p = body;
4312       return GS_ALL_DONE;
4313     }
4314 }
4315
4316 /* Insert a cleanup marker for gimplify_cleanup_point_expr.  CLEANUP
4317    is the cleanup action required.  */
4318
4319 static void
4320 gimple_push_cleanup (tree var, tree cleanup, bool eh_only, tree *pre_p)
4321 {
4322   tree wce;
4323
4324   /* Errors can result in improperly nested cleanups.  Which results in
4325      confusion when trying to resolve the WITH_CLEANUP_EXPR.  */
4326   if (errorcount || sorrycount)
4327     return;
4328
4329   if (gimple_conditional_context ())
4330     {
4331       /* If we're in a conditional context, this is more complex.  We only
4332          want to run the cleanup if we actually ran the initialization that
4333          necessitates it, but we want to run it after the end of the
4334          conditional context.  So we wrap the try/finally around the
4335          condition and use a flag to determine whether or not to actually
4336          run the destructor.  Thus
4337
4338            test ? f(A()) : 0
4339
4340          becomes (approximately)
4341
4342            flag = 0;
4343            try {
4344              if (test) { A::A(temp); flag = 1; val = f(temp); }
4345              else { val = 0; }
4346            } finally {
4347              if (flag) A::~A(temp);
4348            }
4349            val
4350       */
4351
4352       tree flag = create_tmp_var (boolean_type_node, "cleanup");
4353       tree ffalse = build_gimple_modify_stmt (flag, boolean_false_node);
4354       tree ftrue = build_gimple_modify_stmt (flag, boolean_true_node);
4355       cleanup = build3 (COND_EXPR, void_type_node, flag, cleanup, NULL);
4356       wce = build1 (WITH_CLEANUP_EXPR, void_type_node, cleanup);
4357       append_to_statement_list (ffalse, &gimplify_ctxp->conditional_cleanups);
4358       append_to_statement_list (wce, &gimplify_ctxp->conditional_cleanups);
4359       append_to_statement_list (ftrue, pre_p);
4360
4361       /* Because of this manipulation, and the EH edges that jump
4362          threading cannot redirect, the temporary (VAR) will appear
4363          to be used uninitialized.  Don't warn.  */
4364       TREE_NO_WARNING (var) = 1;
4365     }
4366   else
4367     {
4368       wce = build1 (WITH_CLEANUP_EXPR, void_type_node, cleanup);
4369       CLEANUP_EH_ONLY (wce) = eh_only;
4370       append_to_statement_list (wce, pre_p);
4371     }
4372
4373   gimplify_stmt (&TREE_OPERAND (wce, 0));
4374 }
4375
4376 /* Gimplify a TARGET_EXPR which doesn't appear on the rhs of an INIT_EXPR.  */
4377
4378 static enum gimplify_status
4379 gimplify_target_expr (tree *expr_p, tree *pre_p, tree *post_p)
4380 {
4381   tree targ = *expr_p;
4382   tree temp = TARGET_EXPR_SLOT (targ);
4383   tree init = TARGET_EXPR_INITIAL (targ);
4384   enum gimplify_status ret;
4385
4386   if (init)
4387     {
4388       /* TARGET_EXPR temps aren't part of the enclosing block, so add it
4389          to the temps list.  */
4390       gimple_add_tmp_var (temp);
4391
4392       /* If TARGET_EXPR_INITIAL is void, then the mere evaluation of the
4393          expression is supposed to initialize the slot.  */
4394       if (VOID_TYPE_P (TREE_TYPE (init)))
4395         ret = gimplify_expr (&init, pre_p, post_p, is_gimple_stmt, fb_none);
4396       else
4397         {
4398           init = build2 (INIT_EXPR, void_type_node, temp, init);
4399           ret = gimplify_expr (&init, pre_p, post_p, is_gimple_stmt,
4400                                fb_none);
4401         }
4402       if (ret == GS_ERROR)
4403         {
4404           /* PR c++/28266 Make sure this is expanded only once. */
4405           TARGET_EXPR_INITIAL (targ) = NULL_TREE;
4406           return GS_ERROR;
4407         }
4408       append_to_statement_list (init, pre_p);
4409
4410       /* If needed, push the cleanup for the temp.  */
4411       if (TARGET_EXPR_CLEANUP (targ))
4412         {
4413           gimplify_stmt (&TARGET_EXPR_CLEANUP (targ));
4414           gimple_push_cleanup (temp, TARGET_EXPR_CLEANUP (targ),
4415                                CLEANUP_EH_ONLY (targ), pre_p);
4416         }
4417
4418       /* Only expand this once.  */
4419       TREE_OPERAND (targ, 3) = init;
4420       TARGET_EXPR_INITIAL (targ) = NULL_TREE;
4421     }
4422   else
4423     /* We should have expanded this before.  */
4424     gcc_assert (DECL_SEEN_IN_BIND_EXPR_P (temp));
4425
4426   *expr_p = temp;
4427   return GS_OK;
4428 }
4429
4430 /* Gimplification of expression trees.  */
4431
4432 /* Gimplify an expression which appears at statement context; usually, this
4433    means replacing it with a suitably gimple STATEMENT_LIST.  */
4434
4435 void
4436 gimplify_stmt (tree *stmt_p)
4437 {
4438   gimplify_expr (stmt_p, NULL, NULL, is_gimple_stmt, fb_none);
4439 }
4440
4441 /* Similarly, but force the result to be a STATEMENT_LIST.  */
4442
4443 void
4444 gimplify_to_stmt_list (tree *stmt_p)
4445 {
4446   gimplify_stmt (stmt_p);
4447   if (!*stmt_p)
4448     *stmt_p = alloc_stmt_list ();
4449   else if (TREE_CODE (*stmt_p) != STATEMENT_LIST)
4450     {
4451       tree t = *stmt_p;
4452       *stmt_p = alloc_stmt_list ();
4453       append_to_statement_list (t, stmt_p);
4454     }
4455 }
4456
4457
4458 /* Add FIRSTPRIVATE entries for DECL in the OpenMP the surrounding parallels
4459    to CTX.  If entries already exist, force them to be some flavor of private.
4460    If there is no enclosing parallel, do nothing.  */
4461
4462 void
4463 omp_firstprivatize_variable (struct gimplify_omp_ctx *ctx, tree decl)
4464 {
4465   splay_tree_node n;
4466
4467   if (decl == NULL || !DECL_P (decl))
4468     return;
4469
4470   do
4471     {
4472       n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
4473       if (n != NULL)
4474         {
4475           if (n->value & GOVD_SHARED)
4476             n->value = GOVD_FIRSTPRIVATE | (n->value & GOVD_SEEN);
4477           else
4478             return;
4479         }
4480       else if (ctx->is_parallel)
4481         omp_add_variable (ctx, decl, GOVD_FIRSTPRIVATE);
4482
4483       ctx = ctx->outer_context;
4484     }
4485   while (ctx);
4486 }
4487
4488 /* Similarly for each of the type sizes of TYPE.  */
4489
4490 static void
4491 omp_firstprivatize_type_sizes (struct gimplify_omp_ctx *ctx, tree type)
4492 {
4493   if (type == NULL || type == error_mark_node)
4494     return;
4495   type = TYPE_MAIN_VARIANT (type);
4496
4497   if (pointer_set_insert (ctx->privatized_types, type))
4498     return;
4499
4500   switch (TREE_CODE (type))
4501     {
4502     case INTEGER_TYPE:
4503     case ENUMERAL_TYPE:
4504     case BOOLEAN_TYPE:
4505     case REAL_TYPE:
4506       omp_firstprivatize_variable (ctx, TYPE_MIN_VALUE (type));
4507       omp_firstprivatize_variable (ctx, TYPE_MAX_VALUE (type));
4508       break;
4509
4510     case ARRAY_TYPE:
4511       omp_firstprivatize_type_sizes (ctx, TREE_TYPE (type));
4512       omp_firstprivatize_type_sizes (ctx, TYPE_DOMAIN (type));
4513       break;
4514
4515     case RECORD_TYPE:
4516     case UNION_TYPE:
4517     case QUAL_UNION_TYPE:
4518       {
4519         tree field;
4520         for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
4521           if (TREE_CODE (field) == FIELD_DECL)
4522             {
4523               omp_firstprivatize_variable (ctx, DECL_FIELD_OFFSET (field));
4524               omp_firstprivatize_type_sizes (ctx, TREE_TYPE (field));
4525             }
4526       }
4527       break;
4528
4529     case POINTER_TYPE:
4530     case REFERENCE_TYPE:
4531       omp_firstprivatize_type_sizes (ctx, TREE_TYPE (type));
4532       break;
4533
4534     default:
4535       break;
4536     }
4537
4538   omp_firstprivatize_variable (ctx, TYPE_SIZE (type));
4539   omp_firstprivatize_variable (ctx, TYPE_SIZE_UNIT (type));
4540   lang_hooks.types.omp_firstprivatize_type_sizes (ctx, type);
4541 }
4542
4543 /* Add an entry for DECL in the OpenMP context CTX with FLAGS.  */
4544
4545 static void
4546 omp_add_variable (struct gimplify_omp_ctx *ctx, tree decl, unsigned int flags)
4547 {
4548   splay_tree_node n;
4549   unsigned int nflags;
4550   tree t;
4551
4552   if (decl == error_mark_node || TREE_TYPE (decl) == error_mark_node)
4553     return;
4554
4555   /* Never elide decls whose type has TREE_ADDRESSABLE set.  This means
4556      there are constructors involved somewhere.  */
4557   if (TREE_ADDRESSABLE (TREE_TYPE (decl))
4558       || TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl)))
4559     flags |= GOVD_SEEN;
4560
4561   n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
4562   if (n != NULL)
4563     {
4564       /* We shouldn't be re-adding the decl with the same data
4565          sharing class.  */
4566       gcc_assert ((n->value & GOVD_DATA_SHARE_CLASS & flags) == 0);
4567       /* The only combination of data sharing classes we should see is
4568          FIRSTPRIVATE and LASTPRIVATE.  */
4569       nflags = n->value | flags;
4570       gcc_assert ((nflags & GOVD_DATA_SHARE_CLASS)
4571                   == (GOVD_FIRSTPRIVATE | GOVD_LASTPRIVATE));
4572       n->value = nflags;
4573       return;
4574     }
4575
4576   /* When adding a variable-sized variable, we have to handle all sorts
4577      of additional bits of data: the pointer replacement variable, and 
4578      the parameters of the type.  */
4579   if (DECL_SIZE (decl) && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
4580     {
4581       /* Add the pointer replacement variable as PRIVATE if the variable
4582          replacement is private, else FIRSTPRIVATE since we'll need the
4583          address of the original variable either for SHARED, or for the
4584          copy into or out of the context.  */
4585       if (!(flags & GOVD_LOCAL))
4586         {
4587           nflags = flags & GOVD_PRIVATE ? GOVD_PRIVATE : GOVD_FIRSTPRIVATE;
4588           nflags |= flags & GOVD_SEEN;
4589           t = DECL_VALUE_EXPR (decl);
4590           gcc_assert (TREE_CODE (t) == INDIRECT_REF);
4591           t = TREE_OPERAND (t, 0);
4592           gcc_assert (DECL_P (t));
4593           omp_add_variable (ctx, t, nflags);
4594         }
4595
4596       /* Add all of the variable and type parameters (which should have
4597          been gimplified to a formal temporary) as FIRSTPRIVATE.  */
4598       omp_firstprivatize_variable (ctx, DECL_SIZE_UNIT (decl));
4599       omp_firstprivatize_variable (ctx, DECL_SIZE (decl));
4600       omp_firstprivatize_type_sizes (ctx, TREE_TYPE (decl));
4601
4602       /* The variable-sized variable itself is never SHARED, only some form
4603          of PRIVATE.  The sharing would take place via the pointer variable
4604          which we remapped above.  */
4605       if (flags & GOVD_SHARED)
4606         flags = GOVD_PRIVATE | GOVD_DEBUG_PRIVATE
4607                 | (flags & (GOVD_SEEN | GOVD_EXPLICIT));
4608
4609       /* We're going to make use of the TYPE_SIZE_UNIT at least in the 
4610          alloca statement we generate for the variable, so make sure it
4611          is available.  This isn't automatically needed for the SHARED
4612          case, since we won't be allocating local storage then.
4613          For local variables TYPE_SIZE_UNIT might not be gimplified yet,
4614          in this case omp_notice_variable will be called later
4615          on when it is gimplified.  */
4616       else if (! (flags & GOVD_LOCAL))
4617         omp_notice_variable (ctx, TYPE_SIZE_UNIT (TREE_TYPE (decl)), true);
4618     }
4619   else if (lang_hooks.decls.omp_privatize_by_reference (decl))
4620     {
4621       gcc_assert ((flags & GOVD_LOCAL) == 0);
4622       omp_firstprivatize_type_sizes (ctx, TREE_TYPE (decl));
4623
4624       /* Similar to the direct variable sized case above, we'll need the
4625          size of references being privatized.  */
4626       if ((flags & GOVD_SHARED) == 0)
4627         {
4628           t = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (decl)));
4629           if (TREE_CODE (t) != INTEGER_CST)
4630             omp_notice_variable (ctx, t, true);
4631         }
4632     }
4633
4634   splay_tree_insert (ctx->variables, (splay_tree_key)decl, flags);
4635 }
4636
4637 /* Record the fact that DECL was used within the OpenMP context CTX.
4638    IN_CODE is true when real code uses DECL, and false when we should
4639    merely emit default(none) errors.  Return true if DECL is going to
4640    be remapped and thus DECL shouldn't be gimplified into its
4641    DECL_VALUE_EXPR (if any).  */
4642
4643 static bool
4644 omp_notice_variable (struct gimplify_omp_ctx *ctx, tree decl, bool in_code)
4645 {
4646   splay_tree_node n;
4647   unsigned flags = in_code ? GOVD_SEEN : 0;
4648   bool ret = false, shared;
4649
4650   if (decl == error_mark_node || TREE_TYPE (decl) == error_mark_node)
4651     return false;
4652
4653   /* Threadprivate variables are predetermined.  */
4654   if (is_global_var (decl))
4655     {
4656       if (DECL_THREAD_LOCAL_P (decl))
4657         return false;
4658
4659       if (DECL_HAS_VALUE_EXPR_P (decl))
4660         {
4661           tree value = get_base_address (DECL_VALUE_EXPR (decl));
4662
4663           if (value && DECL_P (value) && DECL_THREAD_LOCAL_P (value))
4664             return false;
4665         }
4666     }
4667
4668   n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
4669   if (n == NULL)
4670     {
4671       enum omp_clause_default_kind default_kind, kind;
4672
4673       if (!ctx->is_parallel)
4674         goto do_outer;
4675
4676       /* ??? Some compiler-generated variables (like SAVE_EXPRs) could be
4677          remapped firstprivate instead of shared.  To some extent this is
4678          addressed in omp_firstprivatize_type_sizes, but not effectively.  */
4679       default_kind = ctx->default_kind;
4680       kind = lang_hooks.decls.omp_predetermined_sharing (decl);
4681       if (kind != OMP_CLAUSE_DEFAULT_UNSPECIFIED)
4682         default_kind = kind;
4683
4684       switch (default_kind)
4685         {
4686         case OMP_CLAUSE_DEFAULT_NONE:
4687           error ("%qs not specified in enclosing parallel",
4688                  IDENTIFIER_POINTER (DECL_NAME (decl)));
4689           error ("%Henclosing parallel", &ctx->location);
4690           /* FALLTHRU */
4691         case OMP_CLAUSE_DEFAULT_SHARED:
4692           flags |= GOVD_SHARED;
4693           break;
4694         case OMP_CLAUSE_DEFAULT_PRIVATE:
4695           flags |= GOVD_PRIVATE;
4696           break;
4697         default:
4698           gcc_unreachable ();
4699         }
4700
4701       omp_add_variable (ctx, decl, flags);
4702
4703       shared = (flags & GOVD_SHARED) != 0;
4704       ret = lang_hooks.decls.omp_disregard_value_expr (decl, shared);
4705       goto do_outer;
4706     }
4707
4708   shared = ((flags | n->value) & GOVD_SHARED) != 0;
4709   ret = lang_hooks.decls.omp_disregard_value_expr (decl, shared);
4710
4711   /* If nothing changed, there's nothing left to do.  */
4712   if ((n->value & flags) == flags)
4713     return ret;
4714   flags |= n->value;
4715   n->value = flags;
4716
4717  do_outer:
4718   /* If the variable is private in the current context, then we don't
4719      need to propagate anything to an outer context.  */
4720   if (flags & GOVD_PRIVATE)
4721     return ret;
4722   if (ctx->outer_context
4723       && omp_notice_variable (ctx->outer_context, decl, in_code))
4724     return true;
4725   return ret;
4726 }
4727
4728 /* Verify that DECL is private within CTX.  If there's specific information
4729    to the contrary in the innermost scope, generate an error.  */
4730
4731 static bool
4732 omp_is_private (struct gimplify_omp_ctx *ctx, tree decl)
4733 {
4734   splay_tree_node n;
4735
4736   n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
4737   if (n != NULL)
4738     {
4739       if (n->value & GOVD_SHARED)
4740         {
4741           if (ctx == gimplify_omp_ctxp)
4742             {
4743               error ("iteration variable %qs should be private",
4744                      IDENTIFIER_POINTER (DECL_NAME (decl)));
4745               n->value = GOVD_PRIVATE;
4746               return true;
4747             }
4748           else
4749             return false;
4750         }
4751       else if ((n->value & GOVD_EXPLICIT) != 0
4752                && (ctx == gimplify_omp_ctxp
4753                    || (ctx->is_combined_parallel
4754                        && gimplify_omp_ctxp->outer_context == ctx)))
4755         {
4756           if ((n->value & GOVD_FIRSTPRIVATE) != 0)
4757             error ("iteration variable %qs should not be firstprivate",
4758                    IDENTIFIER_POINTER (DECL_NAME (decl)));
4759           else if ((n->value & GOVD_REDUCTION) != 0)
4760             error ("iteration variable %qs should not be reduction",
4761                    IDENTIFIER_POINTER (DECL_NAME (decl)));
4762         }
4763       return true;
4764     }
4765
4766   if (ctx->is_parallel)
4767     return false;
4768   else if (ctx->outer_context)
4769     return omp_is_private (ctx->outer_context, decl);
4770   else
4771     return !is_global_var (decl);
4772 }
4773
4774 /* Return true if DECL is private within a parallel region
4775    that binds to the current construct's context or in parallel
4776    region's REDUCTION clause.  */
4777
4778 static bool
4779 omp_check_private (struct gimplify_omp_ctx *ctx, tree decl)
4780 {
4781   splay_tree_node n;
4782
4783   do
4784     {
4785       ctx = ctx->outer_context;
4786       if (ctx == NULL)
4787         return !(is_global_var (decl)
4788                  /* References might be private, but might be shared too.  */
4789                  || lang_hooks.decls.omp_privatize_by_reference (decl));
4790
4791       n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
4792       if (n != NULL)
4793         return (n->value & GOVD_SHARED) == 0;
4794     }
4795   while (!ctx->is_parallel);
4796   return false;
4797 }
4798
4799 /* Scan the OpenMP clauses in *LIST_P, installing mappings into a new
4800    and previous omp contexts.  */
4801
4802 static void
4803 gimplify_scan_omp_clauses (tree *list_p, tree *pre_p, bool in_parallel,
4804                            bool in_combined_parallel)
4805 {
4806   struct gimplify_omp_ctx *ctx, *outer_ctx;
4807   tree c;
4808
4809   ctx = new_omp_context (in_parallel, in_combined_parallel);
4810   outer_ctx = ctx->outer_context;
4811
4812   while ((c = *list_p) != NULL)
4813     {
4814       enum gimplify_status gs;
4815       bool remove = false;
4816       bool notice_outer = true;
4817       const char *check_non_private = NULL;
4818       unsigned int flags;
4819       tree decl;
4820
4821       switch (OMP_CLAUSE_CODE (c))
4822         {
4823         case OMP_CLAUSE_PRIVATE:
4824           flags = GOVD_PRIVATE | GOVD_EXPLICIT;
4825           notice_outer = false;
4826           goto do_add;
4827         case OMP_CLAUSE_SHARED:
4828           flags = GOVD_SHARED | GOVD_EXPLICIT;
4829           goto do_add;
4830         case OMP_CLAUSE_FIRSTPRIVATE:
4831           flags = GOVD_FIRSTPRIVATE | GOVD_EXPLICIT;
4832           check_non_private = "firstprivate";
4833           goto do_add;
4834         case OMP_CLAUSE_LASTPRIVATE:
4835           flags = GOVD_LASTPRIVATE | GOVD_SEEN | GOVD_EXPLICIT;
4836           check_non_private = "lastprivate";
4837           goto do_add;
4838         case OMP_CLAUSE_REDUCTION:
4839           flags = GOVD_REDUCTION | GOVD_SEEN | GOVD_EXPLICIT;
4840           check_non_private = "reduction";
4841           goto do_add;
4842
4843         do_add:
4844           decl = OMP_CLAUSE_DECL (c);
4845           if (decl == error_mark_node || TREE_TYPE (decl) == error_mark_node)
4846             {
4847               remove = true;
4848               break;
4849             }
4850           omp_add_variable (ctx, decl, flags);
4851           if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
4852               && OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
4853             {
4854               omp_add_variable (ctx, OMP_CLAUSE_REDUCTION_PLACEHOLDER (c),
4855                                 GOVD_LOCAL | GOVD_SEEN);
4856               gimplify_omp_ctxp = ctx;
4857               push_gimplify_context ();
4858               gimplify_stmt (&OMP_CLAUSE_REDUCTION_INIT (c));
4859               pop_gimplify_context (OMP_CLAUSE_REDUCTION_INIT (c));
4860               push_gimplify_context ();
4861               gimplify_stmt (&OMP_CLAUSE_REDUCTION_MERGE (c));
4862               pop_gimplify_context (OMP_CLAUSE_REDUCTION_MERGE (c));
4863               gimplify_omp_ctxp = outer_ctx;
4864             }
4865           if (notice_outer)
4866             goto do_notice;
4867           break;
4868
4869         case OMP_CLAUSE_COPYIN:
4870         case OMP_CLAUSE_COPYPRIVATE:
4871           decl = OMP_CLAUSE_DECL (c);
4872           if (decl == error_mark_node || TREE_TYPE (decl) == error_mark_node)
4873             {
4874               remove = true;
4875               break;
4876             }
4877         do_notice:
4878           if (outer_ctx)
4879             omp_notice_variable (outer_ctx, decl, true);
4880           if (check_non_private
4881               && !in_parallel
4882               && omp_check_private (ctx, decl))
4883             {
4884               error ("%s variable %qs is private in outer context",
4885                      check_non_private, IDENTIFIER_POINTER (DECL_NAME (decl)));
4886               remove = true;
4887             }
4888           break;
4889
4890         case OMP_CLAUSE_IF:
4891           OMP_CLAUSE_OPERAND (c, 0)
4892             = gimple_boolify (OMP_CLAUSE_OPERAND (c, 0));
4893           /* Fall through.  */
4894
4895         case OMP_CLAUSE_SCHEDULE:
4896         case OMP_CLAUSE_NUM_THREADS:
4897           gs = gimplify_expr (&OMP_CLAUSE_OPERAND (c, 0), pre_p, NULL,
4898                               is_gimple_val, fb_rvalue);
4899           if (gs == GS_ERROR)
4900             remove = true;
4901           break;
4902
4903         case OMP_CLAUSE_NOWAIT:
4904         case OMP_CLAUSE_ORDERED:
4905           break;
4906
4907         case OMP_CLAUSE_DEFAULT:
4908           ctx->default_kind = OMP_CLAUSE_DEFAULT_KIND (c);
4909           break;
4910
4911         default:
4912           gcc_unreachable ();
4913         }
4914
4915       if (remove)
4916         *list_p = OMP_CLAUSE_CHAIN (c);
4917       else
4918         list_p = &OMP_CLAUSE_CHAIN (c);
4919     }
4920
4921   gimplify_omp_ctxp = ctx;
4922 }
4923
4924 /* For all variables that were not actually used within the context,
4925    remove PRIVATE, SHARED, and FIRSTPRIVATE clauses.  */
4926
4927 static int
4928 gimplify_adjust_omp_clauses_1 (splay_tree_node n, void *data)
4929 {
4930   tree *list_p = (tree *) data;
4931   tree decl = (tree) n->key;
4932   unsigned flags = n->value;
4933   enum omp_clause_code code;
4934   tree clause;
4935   bool private_debug;
4936
4937   if (flags & (GOVD_EXPLICIT | GOVD_LOCAL))
4938     return 0;
4939   if ((flags & GOVD_SEEN) == 0)
4940     return 0;
4941   if (flags & GOVD_DEBUG_PRIVATE)
4942     {
4943       gcc_assert ((flags & GOVD_DATA_SHARE_CLASS) == GOVD_PRIVATE);
4944       private_debug = true;
4945     }
4946   else
4947     private_debug
4948       = lang_hooks.decls.omp_private_debug_clause (decl,
4949                                                    !!(flags & GOVD_SHARED));
4950   if (private_debug)
4951     code = OMP_CLAUSE_PRIVATE;
4952   else if (flags & GOVD_SHARED)
4953     {
4954       if (is_global_var (decl))
4955         {
4956           struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp->outer_context;
4957           while (ctx != NULL)
4958             {
4959               splay_tree_node on
4960                 = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
4961               if (on && (on->value & (GOVD_FIRSTPRIVATE | GOVD_LASTPRIVATE
4962                                       | GOVD_PRIVATE | GOVD_REDUCTION)) != 0)
4963                 break;
4964               ctx = ctx->outer_context;
4965             }
4966           if (ctx == NULL)
4967             return 0;
4968         }
4969       code = OMP_CLAUSE_SHARED;
4970     }
4971   else if (flags & GOVD_PRIVATE)
4972     code = OMP_CLAUSE_PRIVATE;
4973   else if (flags & GOVD_FIRSTPRIVATE)
4974     code = OMP_CLAUSE_FIRSTPRIVATE;
4975   else
4976     gcc_unreachable ();
4977
4978   clause = build_omp_clause (code);
4979   OMP_CLAUSE_DECL (clause) = decl;
4980   OMP_CLAUSE_CHAIN (clause) = *list_p;
4981   if (private_debug)
4982     OMP_CLAUSE_PRIVATE_DEBUG (clause) = 1;
4983   *list_p = clause;
4984
4985   return 0;
4986 }
4987
4988 static void
4989 gimplify_adjust_omp_clauses (tree *list_p)
4990 {
4991   struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
4992   tree c, decl;
4993
4994   while ((c = *list_p) != NULL)
4995     {
4996       splay_tree_node n;
4997       bool remove = false;
4998
4999       switch (OMP_CLAUSE_CODE (c))
5000         {
5001         case OMP_CLAUSE_PRIVATE:
5002         case OMP_CLAUSE_SHARED:
5003         case OMP_CLAUSE_FIRSTPRIVATE:
5004           decl = OMP_CLAUSE_DECL (c);
5005           n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
5006           remove = !(n->value & GOVD_SEEN);
5007           if (! remove)
5008             {
5009               bool shared = OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED;
5010               if ((n->value & GOVD_DEBUG_PRIVATE)
5011                   || lang_hooks.decls.omp_private_debug_clause (decl, shared))
5012                 {
5013                   gcc_assert ((n->value & GOVD_DEBUG_PRIVATE) == 0
5014                               || ((n->value & GOVD_DATA_SHARE_CLASS)
5015                                   == GOVD_PRIVATE));
5016                   OMP_CLAUSE_SET_CODE (c, OMP_CLAUSE_PRIVATE);
5017                   OMP_CLAUSE_PRIVATE_DEBUG (c) = 1;
5018                 }
5019             }
5020           break;
5021
5022         case OMP_CLAUSE_LASTPRIVATE:
5023           /* Make sure OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE is set to
5024              accurately reflect the presence of a FIRSTPRIVATE clause.  */
5025           decl = OMP_CLAUSE_DECL (c);
5026           n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
5027           OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c)
5028             = (n->value & GOVD_FIRSTPRIVATE) != 0;
5029           break;
5030           
5031         case OMP_CLAUSE_REDUCTION:
5032         case OMP_CLAUSE_COPYIN:
5033         case OMP_CLAUSE_COPYPRIVATE:
5034         case OMP_CLAUSE_IF:
5035         case OMP_CLAUSE_NUM_THREADS:
5036         case OMP_CLAUSE_SCHEDULE:
5037         case OMP_CLAUSE_NOWAIT:
5038         case OMP_CLAUSE_ORDERED:
5039         case OMP_CLAUSE_DEFAULT:
5040           break;
5041
5042         default:
5043           gcc_unreachable ();
5044         }
5045
5046       if (remove)
5047         *list_p = OMP_CLAUSE_CHAIN (c);
5048       else
5049         list_p = &OMP_CLAUSE_CHAIN (c);
5050     }
5051
5052   /* Add in any implicit data sharing.  */
5053   splay_tree_foreach (ctx->variables, gimplify_adjust_omp_clauses_1, list_p);
5054   
5055   gimplify_omp_ctxp = ctx->outer_context;
5056   delete_omp_context (ctx);
5057 }
5058
5059 /* Gimplify the contents of an OMP_PARALLEL statement.  This involves
5060    gimplification of the body, as well as scanning the body for used
5061    variables.  We need to do this scan now, because variable-sized
5062    decls will be decomposed during gimplification.  */
5063
5064 static enum gimplify_status
5065 gimplify_omp_parallel (tree *expr_p, tree *pre_p)
5066 {
5067   tree expr = *expr_p;
5068
5069   gimplify_scan_omp_clauses (&OMP_PARALLEL_CLAUSES (expr), pre_p, true,
5070                              OMP_PARALLEL_COMBINED (expr));
5071
5072   push_gimplify_context ();
5073
5074   gimplify_stmt (&OMP_PARALLEL_BODY (expr));
5075
5076   if (TREE_CODE (OMP_PARALLEL_BODY (expr)) == BIND_EXPR)
5077     pop_gimplify_context (OMP_PARALLEL_BODY (expr));
5078   else
5079     pop_gimplify_context (NULL_TREE);
5080
5081   gimplify_adjust_omp_clauses (&OMP_PARALLEL_CLAUSES (expr));
5082
5083   return GS_ALL_DONE;
5084 }
5085
5086 /* Gimplify the gross structure of an OMP_FOR statement.  */
5087
5088 static enum gimplify_status
5089 gimplify_omp_for (tree *expr_p, tree *pre_p)
5090 {
5091   tree for_stmt, decl, t;
5092   enum gimplify_status ret = GS_OK;
5093
5094   for_stmt = *expr_p;
5095
5096   gimplify_scan_omp_clauses (&OMP_FOR_CLAUSES (for_stmt), pre_p, false, false);
5097
5098   t = OMP_FOR_INIT (for_stmt);
5099   gcc_assert (TREE_CODE (t) == MODIFY_EXPR
5100               || TREE_CODE (t) == GIMPLE_MODIFY_STMT);
5101   decl = GENERIC_TREE_OPERAND (t, 0);
5102   gcc_assert (DECL_P (decl));
5103   gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (decl)));
5104
5105   /* Make sure the iteration variable is private.  */
5106   if (omp_is_private (gimplify_omp_ctxp, decl))
5107     omp_notice_variable (gimplify_omp_ctxp, decl, true);
5108   else
5109     omp_add_variable (gimplify_omp_ctxp, decl, GOVD_PRIVATE | GOVD_SEEN);
5110
5111   ret |= gimplify_expr (&GENERIC_TREE_OPERAND (t, 1),
5112                         &OMP_FOR_PRE_BODY (for_stmt),
5113                         NULL, is_gimple_val, fb_rvalue);
5114
5115   tree_to_gimple_tuple (&OMP_FOR_INIT (for_stmt));
5116
5117   t = OMP_FOR_COND (for_stmt);
5118   gcc_assert (COMPARISON_CLASS_P (t));
5119   gcc_assert (GENERIC_TREE_OPERAND (t, 0) == decl);
5120
5121   ret |= gimplify_expr (&GENERIC_TREE_OPERAND (t, 1),
5122                         &OMP_FOR_PRE_BODY (for_stmt),
5123                         NULL, is_gimple_val, fb_rvalue);
5124
5125   tree_to_gimple_tuple (&OMP_FOR_INCR (for_stmt));
5126   t = OMP_FOR_INCR (for_stmt);
5127   switch (TREE_CODE (t))
5128     {
5129     case PREINCREMENT_EXPR:
5130     case POSTINCREMENT_EXPR:
5131       t = build_int_cst (TREE_TYPE (decl), 1);
5132       t = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, t);
5133       t = build_gimple_modify_stmt (decl, t);
5134       OMP_FOR_INCR (for_stmt) = t;
5135       break;
5136
5137     case PREDECREMENT_EXPR:
5138     case POSTDECREMENT_EXPR:
5139       t = build_int_cst (TREE_TYPE (decl), -1);
5140       t = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, t);
5141       t = build_gimple_modify_stmt (decl, t);
5142       OMP_FOR_INCR (for_stmt) = t;
5143       break;
5144       
5145     case GIMPLE_MODIFY_STMT:
5146       gcc_assert (GIMPLE_STMT_OPERAND (t, 0) == decl);
5147       t = GIMPLE_STMT_OPERAND (t, 1);
5148       switch (TREE_CODE (t))
5149         {
5150         case PLUS_EXPR:
5151           if (TREE_OPERAND (t, 1) == decl)
5152             {
5153               TREE_OPERAND (t, 1) = TREE_OPERAND (t, 0);
5154               TREE_OPERAND (t, 0) = decl;
5155               break;
5156             }
5157         case MINUS_EXPR:
5158           gcc_assert (TREE_OPERAND (t, 0) == decl);
5159           break;
5160         default:
5161           gcc_unreachable ();
5162         }
5163
5164       ret |= gimplify_expr (&TREE_OPERAND (t, 1), &OMP_FOR_PRE_BODY (for_stmt),
5165                             NULL, is_gimple_val, fb_rvalue);
5166       break;
5167
5168     default:
5169       gcc_unreachable ();
5170     }
5171
5172   gimplify_to_stmt_list (&OMP_FOR_BODY (for_stmt));
5173   gimplify_adjust_omp_clauses (&OMP_FOR_CLAUSES (for_stmt));
5174
5175   return ret == GS_ALL_DONE ? GS_ALL_DONE : GS_ERROR;
5176 }
5177
5178 /* Gimplify the gross structure of other OpenMP worksharing constructs.
5179    In particular, OMP_SECTIONS and OMP_SINGLE.  */
5180
5181 static enum gimplify_status
5182 gimplify_omp_workshare (tree *expr_p, tree *pre_p)
5183 {
5184   tree stmt = *expr_p;
5185
5186   gimplify_scan_omp_clauses (&OMP_CLAUSES (stmt), pre_p, false, false);
5187   gimplify_to_stmt_list (&OMP_BODY (stmt));
5188   gimplify_adjust_omp_clauses (&OMP_CLAUSES (stmt));
5189
5190   return GS_ALL_DONE;
5191 }
5192
5193 /* A subroutine of gimplify_omp_atomic.  The front end is supposed to have
5194    stabilized the lhs of the atomic operation as *ADDR.  Return true if 
5195    EXPR is this stabilized form.  */
5196
5197 static bool
5198 goa_lhs_expr_p (tree expr, tree addr)
5199 {
5200   /* Also include casts to other type variants.  The C front end is fond
5201      of adding these for e.g. volatile variables.  This is like 
5202      STRIP_TYPE_NOPS but includes the main variant lookup.  */
5203   while ((TREE_CODE (expr) == NOP_EXPR
5204           || TREE_CODE (expr) == CONVERT_EXPR
5205           || TREE_CODE (expr) == NON_LVALUE_EXPR)
5206          && TREE_OPERAND (expr, 0) != error_mark_node
5207          && (TYPE_MAIN_VARIANT (TREE_TYPE (expr))
5208              == TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (expr, 0)))))
5209     expr = TREE_OPERAND (expr, 0);
5210
5211   if (TREE_CODE (expr) == INDIRECT_REF && TREE_OPERAND (expr, 0) == addr)
5212     return true;
5213   if (TREE_CODE (addr) == ADDR_EXPR && expr == TREE_OPERAND (addr, 0))
5214     return true;
5215   return false;
5216 }
5217
5218 /* A subroutine of gimplify_omp_atomic.  Attempt to implement the atomic
5219    operation as a __sync_fetch_and_op builtin.  INDEX is log2 of the
5220    size of the data type, and thus usable to find the index of the builtin
5221    decl.  Returns GS_UNHANDLED if the expression is not of the proper form.  */
5222
5223 static enum gimplify_status
5224 gimplify_omp_atomic_fetch_op (tree *expr_p, tree addr, tree rhs, int index)
5225 {
5226   enum built_in_function base;
5227   tree decl, itype;
5228   enum insn_code *optab;
5229
5230   /* Check for one of the supported fetch-op operations.  */
5231   switch (TREE_CODE (rhs))
5232     {
5233     case POINTER_PLUS_EXPR:
5234     case PLUS_EXPR:
5235       base = BUILT_IN_FETCH_AND_ADD_N;
5236       optab = sync_add_optab;
5237       break;
5238     case MINUS_EXPR:
5239       base = BUILT_IN_FETCH_AND_SUB_N;
5240       optab = sync_add_optab;
5241       break;
5242     case BIT_AND_EXPR:
5243       base = BUILT_IN_FETCH_AND_AND_N;
5244       optab = sync_and_optab;
5245       break;
5246     case BIT_IOR_EXPR:
5247       base = BUILT_IN_FETCH_AND_OR_N;
5248       optab = sync_ior_optab;
5249       break;
5250     case BIT_XOR_EXPR:
5251       base = BUILT_IN_FETCH_AND_XOR_N;
5252       optab = sync_xor_optab;
5253       break;
5254     default:
5255       return GS_UNHANDLED;
5256     }
5257
5258   /* Make sure the expression is of the proper form.  */
5259   if (goa_lhs_expr_p (TREE_OPERAND (rhs, 0), addr))
5260     rhs = TREE_OPERAND (rhs, 1);
5261   else if (commutative_tree_code (TREE_CODE (rhs))
5262            && goa_lhs_expr_p (TREE_OPERAND (rhs, 1), addr))
5263     rhs = TREE_OPERAND (rhs, 0);
5264   else
5265     return GS_UNHANDLED;
5266
5267   decl = built_in_decls[base + index + 1];
5268   itype = TREE_TYPE (TREE_TYPE (decl));
5269
5270   if (optab[TYPE_MODE (itype)] == CODE_FOR_nothing)
5271     return GS_UNHANDLED;
5272
5273   *expr_p = build_call_expr (decl, 2, addr, fold_convert (itype, rhs));
5274   return GS_OK;
5275 }
5276
5277 /* A subroutine of gimplify_omp_atomic_pipeline.  Walk *EXPR_P and replace
5278    appearances of *LHS_ADDR with LHS_VAR.  If an expression does not involve
5279    the lhs, evaluate it into a temporary.  Return 1 if the lhs appeared as
5280    a subexpression, 0 if it did not, or -1 if an error was encountered.  */
5281
5282 static int
5283 goa_stabilize_expr (tree *expr_p, tree *pre_p, tree lhs_addr, tree lhs_var)
5284 {
5285   tree expr = *expr_p;
5286   int saw_lhs;
5287
5288   if (goa_lhs_expr_p (expr, lhs_addr))
5289     {
5290       *expr_p = lhs_var;
5291       return 1;
5292     }
5293   if (is_gimple_val (expr))
5294     return 0;
5295  
5296   saw_lhs = 0;
5297   switch (TREE_CODE_CLASS (TREE_CODE (expr)))
5298     {
5299     case tcc_binary:
5300       saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 1), pre_p,
5301                                      lhs_addr, lhs_var);
5302     case tcc_unary:
5303       saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 0), pre_p,
5304                                      lhs_addr, lhs_var);
5305       break;
5306     default:
5307       break;
5308     }
5309
5310   if (saw_lhs == 0)
5311     {
5312       enum gimplify_status gs;
5313       gs = gimplify_expr (expr_p, pre_p, NULL, is_gimple_val, fb_rvalue);
5314       if (gs != GS_ALL_DONE)
5315         saw_lhs = -1;
5316     }
5317
5318   return saw_lhs;
5319 }
5320
5321 /* A subroutine of gimplify_omp_atomic.  Implement the atomic operation as:
5322
5323         oldval = *addr;
5324       repeat:
5325         newval = rhs;   // with oldval replacing *addr in rhs
5326         oldval = __sync_val_compare_and_swap (addr, oldval, newval);
5327         if (oldval != newval)
5328           goto repeat;
5329
5330    INDEX is log2 of the size of the data type, and thus usable to find the
5331    index of the builtin decl.  */
5332
5333 static enum gimplify_status
5334 gimplify_omp_atomic_pipeline (tree *expr_p, tree *pre_p, tree addr,
5335                               tree rhs, int index)
5336 {
5337   tree oldval, oldival, oldival2, newval, newival, label;
5338   tree type, itype, cmpxchg, x, iaddr;
5339
5340   cmpxchg = built_in_decls[BUILT_IN_VAL_COMPARE_AND_SWAP_N + index + 1];
5341   type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (addr)));
5342   itype = TREE_TYPE (TREE_TYPE (cmpxchg));
5343
5344   if (sync_compare_and_swap[TYPE_MODE (itype)] == CODE_FOR_nothing)
5345     return GS_UNHANDLED;
5346
5347   oldval = create_tmp_var (type, NULL);
5348   newval = create_tmp_var (type, NULL);
5349
5350   /* Precompute as much of RHS as possible.  In the same walk, replace
5351      occurrences of the lhs value with our temporary.  */
5352   if (goa_stabilize_expr (&rhs, pre_p, addr, oldval) < 0)
5353     return GS_ERROR;
5354
5355   x = build_fold_indirect_ref (addr);
5356   x = build_gimple_modify_stmt (oldval, x);
5357   gimplify_and_add (x, pre_p);
5358
5359   /* For floating-point values, we'll need to view-convert them to integers
5360      so that we can perform the atomic compare and swap.  Simplify the 
5361      following code by always setting up the "i"ntegral variables.  */
5362   if (INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type))
5363     {
5364       oldival = oldval;
5365       newival = newval;
5366       iaddr = addr;
5367     }
5368   else
5369     {
5370       oldival = create_tmp_var (itype, NULL);
5371       newival = create_tmp_var (itype, NULL);
5372
5373       x = build1 (VIEW_CONVERT_EXPR, itype, oldval);
5374       x = build_gimple_modify_stmt (oldival, x);
5375       gimplify_and_add (x, pre_p);
5376       iaddr = fold_convert (build_pointer_type (itype), addr);
5377     }
5378
5379   oldival2 = create_tmp_var (itype, NULL);
5380
5381   label = create_artificial_label ();
5382   x = build1 (LABEL_EXPR, void_type_node, label);
5383   gimplify_and_add (x, pre_p);
5384
5385   x = build_gimple_modify_stmt (newval, rhs);
5386   gimplify_and_add (x, pre_p);
5387
5388   if (newval != newival)
5389     {
5390       x = build1 (VIEW_CONVERT_EXPR, itype, newval);
5391       x = build_gimple_modify_stmt (newival, x);
5392       gimplify_and_add (x, pre_p);
5393     }
5394
5395   x = build_gimple_modify_stmt (oldival2, fold_convert (itype, oldival));
5396   gimplify_and_add (x, pre_p);
5397
5398   x = build_call_expr (cmpxchg, 3, iaddr, fold_convert (itype, oldival),
5399                        fold_convert (itype, newival));
5400   if (oldval == oldival)
5401     x = fold_convert (type, x);
5402   x = build_gimple_modify_stmt (oldival, x);
5403   gimplify_and_add (x, pre_p);
5404
5405   /* For floating point, be prepared for the loop backedge.  */
5406   if (oldval != oldival)
5407     {
5408       x = build1 (VIEW_CONVERT_EXPR, type, oldival);
5409       x = build_gimple_modify_stmt (oldval, x);
5410       gimplify_and_add (x, pre_p);
5411     }
5412
5413   /* Note that we always perform the comparison as an integer, even for
5414      floating point.  This allows the atomic operation to properly 
5415      succeed even with NaNs and -0.0.  */
5416   x = build3 (COND_EXPR, void_type_node,
5417               build2 (NE_EXPR, boolean_type_node,
5418                       fold_convert (itype, oldival), oldival2),
5419               build1 (GOTO_EXPR, void_type_node, label), NULL);
5420   gimplify_and_add (x, pre_p);
5421
5422   *expr_p = NULL;
5423   return GS_ALL_DONE;
5424 }
5425
5426 /* A subroutine of gimplify_omp_atomic.  Implement the atomic operation as:
5427
5428         GOMP_atomic_start ();
5429         *addr = rhs;
5430         GOMP_atomic_end ();
5431
5432    The result is not globally atomic, but works so long as all parallel
5433    references are within #pragma omp atomic directives.  According to
5434    responses received from omp@openmp.org, appears to be within spec.
5435    Which makes sense, since that's how several other compilers handle
5436    this situation as well.  */
5437
5438 static enum gimplify_status
5439 gimplify_omp_atomic_mutex (tree *expr_p, tree *pre_p, tree addr, tree rhs)
5440 {
5441   tree t;
5442
5443   t = built_in_decls[BUILT_IN_GOMP_ATOMIC_START];
5444   t = build_call_expr (t, 0);
5445   gimplify_and_add (t, pre_p);
5446
5447   t = build_fold_indirect_ref (addr);
5448   t = build_gimple_modify_stmt (t, rhs);
5449   gimplify_and_add (t, pre_p);
5450   
5451   t = built_in_decls[BUILT_IN_GOMP_ATOMIC_END];
5452   t = build_call_expr (t, 0);
5453   gimplify_and_add (t, pre_p);
5454
5455   *expr_p = NULL;
5456   return GS_ALL_DONE;
5457 }
5458
5459 /* Gimplify an OMP_ATOMIC statement.  */
5460
5461 static enum gimplify_status
5462 gimplify_omp_atomic (tree *expr_p, tree *pre_p)
5463 {
5464   tree addr = TREE_OPERAND (*expr_p, 0);
5465   tree rhs = TREE_OPERAND (*expr_p, 1);
5466   tree type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (addr)));
5467   HOST_WIDE_INT index;
5468
5469   /* Make sure the type is one of the supported sizes.  */
5470   index = tree_low_cst (TYPE_SIZE_UNIT (type), 1);
5471   index = exact_log2 (index);
5472   if (index >= 0 && index <= 4)
5473     {
5474       enum gimplify_status gs;
5475       unsigned int align;
5476
5477       if (DECL_P (TREE_OPERAND (addr, 0)))
5478         align = DECL_ALIGN_UNIT (TREE_OPERAND (addr, 0));
5479       else if (TREE_CODE (TREE_OPERAND (addr, 0)) == COMPONENT_REF
5480                && TREE_CODE (TREE_OPERAND (TREE_OPERAND (addr, 0), 1))
5481                   == FIELD_DECL)
5482         align = DECL_ALIGN_UNIT (TREE_OPERAND (TREE_OPERAND (addr, 0), 1));
5483       else
5484         align = TYPE_ALIGN_UNIT (type);
5485
5486       /* __sync builtins require strict data alignment.  */
5487       if (exact_log2 (align) >= index)
5488         {
5489           /* When possible, use specialized atomic update functions.  */
5490           if (INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type))
5491             {
5492               gs = gimplify_omp_atomic_fetch_op (expr_p, addr, rhs, index);
5493               if (gs != GS_UNHANDLED)
5494                 return gs;
5495             }
5496
5497           /* If we don't have specialized __sync builtins, try and implement
5498              as a compare and swap loop.  */
5499           gs = gimplify_omp_atomic_pipeline (expr_p, pre_p, addr, rhs, index);
5500           if (gs != GS_UNHANDLED)
5501             return gs;
5502         }
5503     }
5504
5505   /* The ultimate fallback is wrapping the operation in a mutex.  */
5506   return gimplify_omp_atomic_mutex (expr_p, pre_p, addr, rhs);
5507 }
5508
5509 /*  Gimplifies the expression tree pointed to by EXPR_P.  Return 0 if
5510     gimplification failed.
5511
5512     PRE_P points to the list where side effects that must happen before
5513         EXPR should be stored.
5514
5515     POST_P points to the list where side effects that must happen after
5516         EXPR should be stored, or NULL if there is no suitable list.  In
5517         that case, we copy the result to a temporary, emit the
5518         post-effects, and then return the temporary.
5519
5520     GIMPLE_TEST_F points to a function that takes a tree T and
5521         returns nonzero if T is in the GIMPLE form requested by the
5522         caller.  The GIMPLE predicates are in tree-gimple.c.
5523
5524         This test is used twice.  Before gimplification, the test is
5525         invoked to determine whether *EXPR_P is already gimple enough.  If
5526         that fails, *EXPR_P is gimplified according to its code and
5527         GIMPLE_TEST_F is called again.  If the test still fails, then a new
5528         temporary variable is created and assigned the value of the
5529         gimplified expression.
5530
5531     FALLBACK tells the function what sort of a temporary we want.  If the 1
5532         bit is set, an rvalue is OK.  If the 2 bit is set, an lvalue is OK.
5533         If both are set, either is OK, but an lvalue is preferable.
5534
5535     The return value is either GS_ERROR or GS_ALL_DONE, since this function
5536     iterates until solution.  */
5537
5538 enum gimplify_status
5539 gimplify_expr (tree *expr_p, tree *pre_p, tree *post_p,
5540                bool (* gimple_test_f) (tree), fallback_t fallback)
5541 {
5542   tree tmp;
5543   tree internal_pre = NULL_TREE;
5544   tree internal_post = NULL_TREE;
5545   tree save_expr;
5546   int is_statement = (pre_p == NULL);
5547   location_t saved_location;
5548   enum gimplify_status ret;
5549
5550   save_expr = *expr_p;
5551   if (save_expr == NULL_TREE)
5552     return GS_ALL_DONE;
5553
5554   /* We used to check the predicate here and return immediately if it
5555      succeeds.  This is wrong; the design is for gimplification to be
5556      idempotent, and for the predicates to only test for valid forms, not
5557      whether they are fully simplified.  */
5558
5559   /* Set up our internal queues if needed.  */
5560   if (pre_p == NULL)
5561     pre_p = &internal_pre;
5562   if (post_p == NULL)
5563     post_p = &internal_post;
5564
5565   saved_location = input_location;
5566   if (save_expr != error_mark_node
5567       && EXPR_HAS_LOCATION (*expr_p))
5568     input_location = EXPR_LOCATION (*expr_p);
5569
5570   /* Loop over the specific gimplifiers until the toplevel node
5571      remains the same.  */
5572   do
5573     {
5574       /* Strip away as many useless type conversions as possible
5575          at the toplevel.  */
5576       STRIP_USELESS_TYPE_CONVERSION (*expr_p);
5577
5578       /* Remember the expr.  */
5579       save_expr = *expr_p;
5580
5581       /* Die, die, die, my darling.  */
5582       if (save_expr == error_mark_node
5583           || (!GIMPLE_STMT_P (save_expr)
5584               && TREE_TYPE (save_expr)
5585               && TREE_TYPE (save_expr) == error_mark_node))
5586         {
5587           ret = GS_ERROR;
5588           break;
5589         }
5590
5591       /* Do any language-specific gimplification.  */
5592       ret = lang_hooks.gimplify_expr (expr_p, pre_p, post_p);
5593       if (ret == GS_OK)
5594         {
5595           if (*expr_p == NULL_TREE)
5596             break;
5597           if (*expr_p != save_expr)
5598             continue;
5599         }
5600       else if (ret != GS_UNHANDLED)
5601         break;
5602
5603       ret = GS_OK;
5604       switch (TREE_CODE (*expr_p))
5605         {
5606           /* First deal with the special cases.  */
5607
5608         case POSTINCREMENT_EXPR:
5609         case POSTDECREMENT_EXPR:
5610         case PREINCREMENT_EXPR:
5611         case PREDECREMENT_EXPR:
5612           ret = gimplify_self_mod_expr (expr_p, pre_p, post_p,
5613                                         fallback != fb_none);
5614           break;
5615
5616         case ARRAY_REF:
5617         case ARRAY_RANGE_REF:
5618         case REALPART_EXPR:
5619         case IMAGPART_EXPR:
5620         case COMPONENT_REF:
5621         case VIEW_CONVERT_EXPR:
5622           ret = gimplify_compound_lval (expr_p, pre_p, post_p,
5623                                         fallback ? fallback : fb_rvalue);
5624           break;
5625
5626         case COND_EXPR:
5627           ret = gimplify_cond_expr (expr_p, pre_p, fallback);
5628           /* C99 code may assign to an array in a structure value of a
5629              conditional expression, and this has undefined behavior
5630              only on execution, so create a temporary if an lvalue is
5631              required.  */
5632           if (fallback == fb_lvalue)
5633             {
5634               *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p);
5635               mark_addressable (*expr_p);
5636             }
5637           break;
5638
5639         case CALL_EXPR:
5640           ret = gimplify_call_expr (expr_p, pre_p, fallback != fb_none);
5641           /* C99 code may assign to an array in a structure returned
5642              from a function, and this has undefined behavior only on
5643              execution, so create a temporary if an lvalue is
5644              required.  */
5645           if (fallback == fb_lvalue)
5646             {
5647               *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p);
5648               mark_addressable (*expr_p);
5649             }
5650           break;
5651
5652         case TREE_LIST:
5653           gcc_unreachable ();
5654
5655         case COMPOUND_EXPR:
5656           ret = gimplify_compound_expr (expr_p, pre_p, fallback != fb_none);
5657           break;
5658
5659         case MODIFY_EXPR:
5660         case GIMPLE_MODIFY_STMT:
5661         case INIT_EXPR:
5662           ret = gimplify_modify_expr (expr_p, pre_p, post_p,
5663                                       fallback != fb_none);
5664
5665           if (*expr_p)
5666             {
5667               /* The distinction between MODIFY_EXPR and INIT_EXPR is no longer
5668                  useful.  */
5669               if (TREE_CODE (*expr_p) == INIT_EXPR)
5670                 TREE_SET_CODE (*expr_p, MODIFY_EXPR);
5671
5672               /* Convert MODIFY_EXPR to GIMPLE_MODIFY_STMT.  */
5673               if (TREE_CODE (*expr_p) == MODIFY_EXPR)
5674                 tree_to_gimple_tuple (expr_p);
5675             }
5676
5677           break;
5678
5679         case TRUTH_ANDIF_EXPR:
5680         case TRUTH_ORIF_EXPR:
5681           ret = gimplify_boolean_expr (expr_p);
5682           break;
5683
5684         case TRUTH_NOT_EXPR:
5685           TREE_OPERAND (*expr_p, 0)
5686             = gimple_boolify (TREE_OPERAND (*expr_p, 0));
5687           ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
5688                                is_gimple_val, fb_rvalue);
5689           recalculate_side_effects (*expr_p);
5690           break;
5691
5692         case ADDR_EXPR:
5693           ret = gimplify_addr_expr (expr_p, pre_p, post_p);
5694           break;
5695
5696         case VA_ARG_EXPR:
5697           ret = gimplify_va_arg_expr (expr_p, pre_p, post_p);
5698           break;
5699
5700         case CONVERT_EXPR:
5701         case NOP_EXPR:
5702           if (IS_EMPTY_STMT (*expr_p))
5703             {
5704               ret = GS_ALL_DONE;
5705               break;
5706             }
5707
5708           if (VOID_TYPE_P (TREE_TYPE (*expr_p))
5709               || fallback == fb_none)
5710             {
5711               /* Just strip a conversion to void (or in void context) and
5712                  try again.  */
5713               *expr_p = TREE_OPERAND (*expr_p, 0);
5714               break;
5715             }
5716
5717           ret = gimplify_conversion (expr_p);
5718           if (ret == GS_ERROR)
5719             break;
5720           if (*expr_p != save_expr)
5721             break;
5722           /* FALLTHRU */
5723
5724         case FIX_TRUNC_EXPR:
5725           /* unary_expr: ... | '(' cast ')' val | ...  */
5726           ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
5727                                is_gimple_val, fb_rvalue);
5728           recalculate_side_effects (*expr_p);
5729           break;
5730
5731         case INDIRECT_REF:
5732           *expr_p = fold_indirect_ref (*expr_p);
5733           if (*expr_p != save_expr)
5734             break;
5735           /* else fall through.  */
5736         case ALIGN_INDIRECT_REF:
5737         case MISALIGNED_INDIRECT_REF:
5738           ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
5739                                is_gimple_reg, fb_rvalue);
5740           recalculate_side_effects (*expr_p);
5741           break;
5742
5743           /* Constants need not be gimplified.  */
5744         case INTEGER_CST:
5745         case REAL_CST:
5746         case STRING_CST:
5747         case COMPLEX_CST:
5748         case VECTOR_CST:
5749           ret = GS_ALL_DONE;
5750           break;
5751
5752         case CONST_DECL:
5753           /* If we require an lvalue, such as for ADDR_EXPR, retain the
5754              CONST_DECL node.  Otherwise the decl is replaceable by its
5755              value.  */
5756           /* ??? Should be == fb_lvalue, but ADDR_EXPR passes fb_either.  */
5757           if (fallback & fb_lvalue)
5758             ret = GS_ALL_DONE;
5759           else
5760             *expr_p = DECL_INITIAL (*expr_p);
5761           break;
5762
5763         case DECL_EXPR:
5764           ret = gimplify_decl_expr (expr_p);
5765           break;
5766
5767         case EXC_PTR_EXPR:
5768           /* FIXME make this a decl.  */
5769           ret = GS_ALL_DONE;
5770           break;
5771
5772         case BIND_EXPR:
5773           ret = gimplify_bind_expr (expr_p, pre_p);
5774           break;
5775
5776         case LOOP_EXPR:
5777           ret = gimplify_loop_expr (expr_p, pre_p);
5778           break;
5779
5780         case SWITCH_EXPR:
5781           ret = gimplify_switch_expr (expr_p, pre_p);
5782           break;
5783
5784         case EXIT_EXPR:
5785           ret = gimplify_exit_expr (expr_p);
5786           break;
5787
5788         case GOTO_EXPR:
5789           /* If the target is not LABEL, then it is a computed jump
5790              and the target needs to be gimplified.  */
5791           if (TREE_CODE (GOTO_DESTINATION (*expr_p)) != LABEL_DECL)
5792             ret = gimplify_expr (&GOTO_DESTINATION (*expr_p), pre_p,
5793                                  NULL, is_gimple_val, fb_rvalue);
5794           break;
5795
5796         case LABEL_EXPR:
5797           ret = GS_ALL_DONE;
5798           gcc_assert (decl_function_context (LABEL_EXPR_LABEL (*expr_p))
5799                       == current_function_decl);
5800           break;
5801
5802         case CASE_LABEL_EXPR:
5803           ret = gimplify_case_label_expr (expr_p);
5804           break;
5805
5806         case RETURN_EXPR:
5807           ret = gimplify_return_expr (*expr_p, pre_p);
5808           break;
5809
5810         case CONSTRUCTOR:
5811           /* Don't reduce this in place; let gimplify_init_constructor work its
5812              magic.  Buf if we're just elaborating this for side effects, just
5813              gimplify any element that has side-effects.  */
5814           if (fallback == fb_none)
5815             {
5816               unsigned HOST_WIDE_INT ix;
5817               constructor_elt *ce;
5818               tree temp = NULL_TREE;
5819               for (ix = 0;
5820                    VEC_iterate (constructor_elt, CONSTRUCTOR_ELTS (*expr_p),
5821                                 ix, ce);
5822                    ix++)
5823                 if (TREE_SIDE_EFFECTS (ce->value))
5824                   append_to_statement_list (ce->value, &temp);
5825
5826               *expr_p = temp;
5827               ret = GS_OK;
5828             }
5829           /* C99 code may assign to an array in a constructed
5830              structure or union, and this has undefined behavior only
5831              on execution, so create a temporary if an lvalue is
5832              required.  */
5833           else if (fallback == fb_lvalue)
5834             {
5835               *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p);
5836               mark_addressable (*expr_p);
5837             }
5838           else
5839             ret = GS_ALL_DONE;
5840           break;
5841
5842           /* The following are special cases that are not handled by the
5843              original GIMPLE grammar.  */
5844
5845           /* SAVE_EXPR nodes are converted into a GIMPLE identifier and
5846              eliminated.  */
5847         case SAVE_EXPR:
5848           ret = gimplify_save_expr (expr_p, pre_p, post_p);
5849           break;
5850
5851         case BIT_FIELD_REF:
5852           {
5853             enum gimplify_status r0, r1, r2;
5854
5855             r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
5856                                 is_gimple_lvalue, fb_either);
5857             r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
5858                                 is_gimple_val, fb_rvalue);
5859             r2 = gimplify_expr (&TREE_OPERAND (*expr_p, 2), pre_p, post_p,
5860                                 is_gimple_val, fb_rvalue);
5861             recalculate_side_effects (*expr_p);
5862
5863             ret = MIN (r0, MIN (r1, r2));
5864           }
5865           break;
5866
5867         case NON_LVALUE_EXPR:
5868           /* This should have been stripped above.  */
5869           gcc_unreachable ();
5870
5871         case ASM_EXPR:
5872           ret = gimplify_asm_expr (expr_p, pre_p, post_p);
5873           break;
5874
5875         case TRY_FINALLY_EXPR:
5876         case TRY_CATCH_EXPR:
5877           gimplify_to_stmt_list (&TREE_OPERAND (*expr_p, 0));
5878           gimplify_to_stmt_list (&TREE_OPERAND (*expr_p, 1));
5879           ret = GS_ALL_DONE;
5880           break;
5881
5882         case CLEANUP_POINT_EXPR:
5883           ret = gimplify_cleanup_point_expr (expr_p, pre_p);
5884           break;
5885
5886         case TARGET_EXPR:
5887           ret = gimplify_target_expr (expr_p, pre_p, post_p);
5888           break;
5889
5890         case CATCH_EXPR:
5891           gimplify_to_stmt_list (&CATCH_BODY (*expr_p));
5892           ret = GS_ALL_DONE;
5893           break;
5894
5895         case EH_FILTER_EXPR:
5896           gimplify_to_stmt_list (&EH_FILTER_FAILURE (*expr_p));
5897           ret = GS_ALL_DONE;
5898           break;
5899
5900         case CHANGE_DYNAMIC_TYPE_EXPR:
5901           ret = gimplify_expr (&CHANGE_DYNAMIC_TYPE_LOCATION (*expr_p),
5902                                pre_p, post_p, is_gimple_reg, fb_lvalue);
5903           break;
5904
5905         case OBJ_TYPE_REF:
5906           {
5907             enum gimplify_status r0, r1;
5908             r0 = gimplify_expr (&OBJ_TYPE_REF_OBJECT (*expr_p), pre_p, post_p,
5909                                 is_gimple_val, fb_rvalue);
5910             r1 = gimplify_expr (&OBJ_TYPE_REF_EXPR (*expr_p), pre_p, post_p,
5911                                 is_gimple_val, fb_rvalue);
5912             ret = MIN (r0, r1);
5913           }
5914           break;
5915
5916         case LABEL_DECL:
5917           /* We get here when taking the address of a label.  We mark
5918              the label as "forced"; meaning it can never be removed and
5919              it is a potential target for any computed goto.  */
5920           FORCED_LABEL (*expr_p) = 1;
5921           ret = GS_ALL_DONE;
5922           break;
5923
5924         case STATEMENT_LIST:
5925           ret = gimplify_statement_list (expr_p, pre_p);
5926           break;
5927
5928         case WITH_SIZE_EXPR:
5929           {
5930             gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
5931                            post_p == &internal_post ? NULL : post_p,
5932                            gimple_test_f, fallback);
5933             gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
5934                            is_gimple_val, fb_rvalue);
5935           }
5936           break;
5937
5938         case VAR_DECL:
5939         case PARM_DECL:
5940           ret = gimplify_var_or_parm_decl (expr_p);
5941           break;
5942
5943         case RESULT_DECL:
5944           /* When within an OpenMP context, notice uses of variables.  */
5945           if (gimplify_omp_ctxp)
5946             omp_notice_variable (gimplify_omp_ctxp, *expr_p, true);
5947           ret = GS_ALL_DONE;
5948           break;
5949
5950         case SSA_NAME:
5951           /* Allow callbacks into the gimplifier during optimization.  */
5952           ret = GS_ALL_DONE;
5953           break;
5954
5955         case OMP_PARALLEL:
5956           ret = gimplify_omp_parallel (expr_p, pre_p);
5957           break;
5958
5959         case OMP_FOR:
5960           ret = gimplify_omp_for (expr_p, pre_p);
5961           break;
5962
5963         case OMP_SECTIONS:
5964         case OMP_SINGLE:
5965           ret = gimplify_omp_workshare (expr_p, pre_p);
5966           break;
5967
5968         case OMP_SECTION:
5969         case OMP_MASTER:
5970         case OMP_ORDERED:
5971         case OMP_CRITICAL:
5972           gimplify_to_stmt_list (&OMP_BODY (*expr_p));
5973           break;
5974
5975         case OMP_ATOMIC:
5976           ret = gimplify_omp_atomic (expr_p, pre_p);
5977           break;
5978
5979         case OMP_RETURN:
5980         case OMP_CONTINUE:
5981           ret = GS_ALL_DONE;
5982           break;
5983
5984         case POINTER_PLUS_EXPR:
5985           /* Convert ((type *)A)+offset into &A->field_of_type_and_offset.
5986              The second is gimple immediate saving a need for extra statement.
5987            */
5988           if (TREE_CODE (TREE_OPERAND (*expr_p, 1)) == INTEGER_CST
5989               && (tmp = maybe_fold_offset_to_reference
5990                          (TREE_OPERAND (*expr_p, 0), TREE_OPERAND (*expr_p, 1),
5991                           TREE_TYPE (TREE_TYPE (*expr_p)))))
5992              {
5993                tree ptr_type = build_pointer_type (TREE_TYPE (tmp));
5994                if (useless_type_conversion_p (TREE_TYPE (*expr_p), ptr_type))
5995                  {
5996                    *expr_p = build_fold_addr_expr_with_type (tmp, ptr_type);
5997                    break;
5998                  }
5999              }
6000           /* Convert (void *)&a + 4 into (void *)&a[1].  */
6001           if (TREE_CODE (TREE_OPERAND (*expr_p, 0)) == NOP_EXPR
6002               && TREE_CODE (TREE_OPERAND (*expr_p, 1)) == INTEGER_CST
6003               && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (*expr_p,
6004                                                                         0),0)))
6005               && (tmp = maybe_fold_offset_to_reference
6006                          (TREE_OPERAND (TREE_OPERAND (*expr_p, 0), 0),
6007                           TREE_OPERAND (*expr_p, 1),
6008                           TREE_TYPE (TREE_TYPE
6009                                   (TREE_OPERAND (TREE_OPERAND (*expr_p, 0),
6010                                                  0))))))
6011              {
6012                tmp = build_fold_addr_expr (tmp);
6013                *expr_p = fold_convert (TREE_TYPE (*expr_p), tmp);
6014                break;
6015              }
6016           /* FALLTHRU */
6017         default:
6018           switch (TREE_CODE_CLASS (TREE_CODE (*expr_p)))
6019             {
6020             case tcc_comparison:
6021               /* Handle comparison of objects of non scalar mode aggregates
6022                  with a call to memcmp.  It would be nice to only have to do
6023                  this for variable-sized objects, but then we'd have to allow
6024                  the same nest of reference nodes we allow for MODIFY_EXPR and
6025                  that's too complex.
6026
6027                  Compare scalar mode aggregates as scalar mode values.  Using
6028                  memcmp for them would be very inefficient at best, and is
6029                  plain wrong if bitfields are involved.  */
6030
6031               {
6032                 tree type = TREE_TYPE (TREE_OPERAND (*expr_p, 1));
6033
6034                 if (!AGGREGATE_TYPE_P (type))
6035                   goto expr_2;
6036                 else if (TYPE_MODE (type) != BLKmode)
6037                   ret = gimplify_scalar_mode_aggregate_compare (expr_p);
6038                 else
6039                   ret = gimplify_variable_sized_compare (expr_p);
6040
6041                 break;
6042                 }
6043
6044             /* If *EXPR_P does not need to be special-cased, handle it
6045                according to its class.  */
6046             case tcc_unary:
6047               ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
6048                                    post_p, is_gimple_val, fb_rvalue);
6049               break;
6050
6051             case tcc_binary:
6052             expr_2:
6053               {
6054                 enum gimplify_status r0, r1;
6055
6056                 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
6057                                     post_p, is_gimple_val, fb_rvalue);
6058                 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
6059                                     post_p, is_gimple_val, fb_rvalue);
6060
6061                 ret = MIN (r0, r1);
6062                 break;
6063               }
6064
6065             case tcc_declaration:
6066             case tcc_constant:
6067               ret = GS_ALL_DONE;
6068               goto dont_recalculate;
6069
6070             default:
6071               gcc_assert (TREE_CODE (*expr_p) == TRUTH_AND_EXPR
6072                           || TREE_CODE (*expr_p) == TRUTH_OR_EXPR
6073                           || TREE_CODE (*expr_p) == TRUTH_XOR_EXPR);
6074               goto expr_2;
6075             }
6076
6077           recalculate_side_effects (*expr_p);
6078         dont_recalculate:
6079           break;
6080         }
6081
6082       /* If we replaced *expr_p, gimplify again.  */
6083       if (ret == GS_OK && (*expr_p == NULL || *expr_p == save_expr))
6084         ret = GS_ALL_DONE;
6085     }
6086   while (ret == GS_OK);
6087
6088   /* If we encountered an error_mark somewhere nested inside, either
6089      stub out the statement or propagate the error back out.  */
6090   if (ret == GS_ERROR)
6091     {
6092       if (is_statement)
6093         *expr_p = NULL;
6094       goto out;
6095     }
6096
6097   /* This was only valid as a return value from the langhook, which
6098      we handled.  Make sure it doesn't escape from any other context.  */
6099   gcc_assert (ret != GS_UNHANDLED);
6100
6101   if (fallback == fb_none && *expr_p && !is_gimple_stmt (*expr_p))
6102     {
6103       /* We aren't looking for a value, and we don't have a valid
6104          statement.  If it doesn't have side-effects, throw it away.  */
6105       if (!TREE_SIDE_EFFECTS (*expr_p))
6106         *expr_p = NULL;
6107       else if (!TREE_THIS_VOLATILE (*expr_p))
6108         {
6109           /* This is probably a _REF that contains something nested that
6110              has side effects.  Recurse through the operands to find it.  */
6111           enum tree_code code = TREE_CODE (*expr_p);
6112
6113           switch (code)
6114             {
6115             case COMPONENT_REF:
6116             case REALPART_EXPR:
6117             case IMAGPART_EXPR:
6118             case VIEW_CONVERT_EXPR:
6119               gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
6120                              gimple_test_f, fallback);
6121               break;
6122
6123             case ARRAY_REF:
6124             case ARRAY_RANGE_REF:
6125               gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
6126                              gimple_test_f, fallback);
6127               gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
6128                              gimple_test_f, fallback);
6129               break;
6130
6131             default:
6132                /* Anything else with side-effects must be converted to
6133                   a valid statement before we get here.  */
6134               gcc_unreachable ();
6135             }
6136
6137           *expr_p = NULL;
6138         }
6139       else if (COMPLETE_TYPE_P (TREE_TYPE (*expr_p))
6140                && TYPE_MODE (TREE_TYPE (*expr_p)) != BLKmode)
6141         {
6142           /* Historically, the compiler has treated a bare reference
6143              to a non-BLKmode volatile lvalue as forcing a load.  */
6144           tree type = TYPE_MAIN_VARIANT (TREE_TYPE (*expr_p));
6145           /* Normally, we do not want to create a temporary for a
6146              TREE_ADDRESSABLE type because such a type should not be
6147              copied by bitwise-assignment.  However, we make an
6148              exception here, as all we are doing here is ensuring that
6149              we read the bytes that make up the type.  We use
6150              create_tmp_var_raw because create_tmp_var will abort when
6151              given a TREE_ADDRESSABLE type.  */
6152           tree tmp = create_tmp_var_raw (type, "vol");
6153           gimple_add_tmp_var (tmp);
6154           *expr_p = build_gimple_modify_stmt (tmp, *expr_p);
6155         }
6156       else
6157         /* We can't do anything useful with a volatile reference to
6158            an incomplete type, so just throw it away.  Likewise for
6159            a BLKmode type, since any implicit inner load should
6160            already have been turned into an explicit one by the
6161            gimplification process.  */
6162         *expr_p = NULL;
6163     }
6164
6165   /* If we are gimplifying at the statement level, we're done.  Tack
6166      everything together and replace the original statement with the
6167      gimplified form.  */
6168   if (fallback == fb_none || is_statement)
6169     {
6170       if (internal_pre || internal_post)
6171         {
6172           append_to_statement_list (*expr_p, &internal_pre);
6173           append_to_statement_list (internal_post, &internal_pre);
6174           annotate_all_with_locus (&internal_pre, input_location);
6175           *expr_p = internal_pre;
6176         }
6177       else if (!*expr_p)
6178         ;
6179       else if (TREE_CODE (*expr_p) == STATEMENT_LIST)
6180         annotate_all_with_locus (expr_p, input_location);
6181       else
6182         annotate_one_with_locus (*expr_p, input_location);
6183       goto out;
6184     }
6185
6186   /* Otherwise we're gimplifying a subexpression, so the resulting value is
6187      interesting.  */
6188
6189   /* If it's sufficiently simple already, we're done.  Unless we are
6190      handling some post-effects internally; if that's the case, we need to
6191      copy into a temp before adding the post-effects to the tree.  */
6192   if (!internal_post && (*gimple_test_f) (*expr_p))
6193     goto out;
6194
6195   /* Otherwise, we need to create a new temporary for the gimplified
6196      expression.  */
6197
6198   /* We can't return an lvalue if we have an internal postqueue.  The
6199      object the lvalue refers to would (probably) be modified by the
6200      postqueue; we need to copy the value out first, which means an
6201      rvalue.  */
6202   if ((fallback & fb_lvalue) && !internal_post
6203       && is_gimple_addressable (*expr_p))
6204     {
6205       /* An lvalue will do.  Take the address of the expression, store it
6206          in a temporary, and replace the expression with an INDIRECT_REF of
6207          that temporary.  */
6208       tmp = build_fold_addr_expr (*expr_p);
6209       gimplify_expr (&tmp, pre_p, post_p, is_gimple_reg, fb_rvalue);
6210       *expr_p = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (tmp)), tmp);
6211     }
6212   else if ((fallback & fb_rvalue) && is_gimple_formal_tmp_rhs (*expr_p))
6213     {
6214       gcc_assert (!VOID_TYPE_P (TREE_TYPE (*expr_p)));
6215
6216       /* An rvalue will do.  Assign the gimplified expression into a new
6217          temporary TMP and replace the original expression with TMP.  */
6218
6219       if (internal_post || (fallback & fb_lvalue))
6220         /* The postqueue might change the value of the expression between
6221            the initialization and use of the temporary, so we can't use a
6222            formal temp.  FIXME do we care?  */
6223         *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p);
6224       else
6225         *expr_p = get_formal_tmp_var (*expr_p, pre_p);
6226
6227       if (TREE_CODE (*expr_p) != SSA_NAME)
6228         DECL_GIMPLE_FORMAL_TEMP_P (*expr_p) = 1;
6229     }
6230   else
6231     {
6232 #ifdef ENABLE_CHECKING
6233       if (!(fallback & fb_mayfail))
6234         {
6235           fprintf (stderr, "gimplification failed:\n");
6236           print_generic_expr (stderr, *expr_p, 0);
6237           debug_tree (*expr_p);
6238           internal_error ("gimplification failed");
6239         }
6240 #endif
6241       gcc_assert (fallback & fb_mayfail);
6242       /* If this is an asm statement, and the user asked for the
6243          impossible, don't die.  Fail and let gimplify_asm_expr
6244          issue an error.  */
6245       ret = GS_ERROR;
6246       goto out;
6247     }
6248
6249   /* Make sure the temporary matches our predicate.  */
6250   gcc_assert ((*gimple_test_f) (*expr_p));
6251
6252   if (internal_post)
6253     {
6254       annotate_all_with_locus (&internal_post, input_location);
6255       append_to_statement_list (internal_post, pre_p);
6256     }
6257
6258  out:
6259   input_location = saved_location;
6260   return ret;
6261 }
6262
6263 /* Look through TYPE for variable-sized objects and gimplify each such
6264    size that we find.  Add to LIST_P any statements generated.  */
6265
6266 void
6267 gimplify_type_sizes (tree type, tree *list_p)
6268 {
6269   tree field, t;
6270
6271   if (type == NULL || type == error_mark_node)
6272     return;
6273
6274   /* We first do the main variant, then copy into any other variants.  */
6275   type = TYPE_MAIN_VARIANT (type);
6276
6277   /* Avoid infinite recursion.  */
6278   if (TYPE_SIZES_GIMPLIFIED (type))
6279     return;
6280
6281   TYPE_SIZES_GIMPLIFIED (type) = 1;
6282
6283   switch (TREE_CODE (type))
6284     {
6285     case INTEGER_TYPE:
6286     case ENUMERAL_TYPE:
6287     case BOOLEAN_TYPE:
6288     case REAL_TYPE:
6289       gimplify_one_sizepos (&TYPE_MIN_VALUE (type), list_p);
6290       gimplify_one_sizepos (&TYPE_MAX_VALUE (type), list_p);
6291
6292       for (t = TYPE_NEXT_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
6293         {
6294           TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (type);
6295           TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (type);
6296         }
6297       break;
6298
6299     case ARRAY_TYPE:
6300       /* These types may not have declarations, so handle them here.  */
6301       gimplify_type_sizes (TREE_TYPE (type), list_p);
6302       gimplify_type_sizes (TYPE_DOMAIN (type), list_p);
6303       break;
6304
6305     case RECORD_TYPE:
6306     case UNION_TYPE:
6307     case QUAL_UNION_TYPE:
6308       for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
6309         if (TREE_CODE (field) == FIELD_DECL)
6310           {
6311             gimplify_one_sizepos (&DECL_FIELD_OFFSET (field), list_p);
6312             gimplify_type_sizes (TREE_TYPE (field), list_p);
6313           }
6314       break;
6315
6316     case POINTER_TYPE:
6317     case REFERENCE_TYPE:
6318         /* We used to recurse on the pointed-to type here, which turned out to
6319            be incorrect because its definition might refer to variables not
6320            yet initialized at this point if a forward declaration is involved.
6321
6322            It was actually useful for anonymous pointed-to types to ensure
6323            that the sizes evaluation dominates every possible later use of the
6324            values.  Restricting to such types here would be safe since there
6325            is no possible forward declaration around, but would introduce an
6326            undesirable middle-end semantic to anonymity.  We then defer to
6327            front-ends the responsibility of ensuring that the sizes are
6328            evaluated both early and late enough, e.g. by attaching artificial
6329            type declarations to the tree.  */
6330       break;
6331
6332     default:
6333       break;
6334     }
6335
6336   gimplify_one_sizepos (&TYPE_SIZE (type), list_p);
6337   gimplify_one_sizepos (&TYPE_SIZE_UNIT (type), list_p);
6338
6339   for (t = TYPE_NEXT_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
6340     {
6341       TYPE_SIZE (t) = TYPE_SIZE (type);
6342       TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (type);
6343       TYPE_SIZES_GIMPLIFIED (t) = 1;
6344     }
6345 }
6346
6347 /* A subroutine of gimplify_type_sizes to make sure that *EXPR_P,
6348    a size or position, has had all of its SAVE_EXPRs evaluated.
6349    We add any required statements to STMT_P.  */
6350
6351 void
6352 gimplify_one_sizepos (tree *expr_p, tree *stmt_p)
6353 {
6354   tree type, expr = *expr_p;
6355
6356   /* We don't do anything if the value isn't there, is constant, or contains
6357      A PLACEHOLDER_EXPR.  We also don't want to do anything if it's already
6358      a VAR_DECL.  If it's a VAR_DECL from another function, the gimplifier
6359      will want to replace it with a new variable, but that will cause problems
6360      if this type is from outside the function.  It's OK to have that here.  */
6361   if (expr == NULL_TREE || TREE_CONSTANT (expr)
6362       || TREE_CODE (expr) == VAR_DECL
6363       || CONTAINS_PLACEHOLDER_P (expr))
6364     return;
6365
6366   type = TREE_TYPE (expr);
6367   *expr_p = unshare_expr (expr);
6368
6369   gimplify_expr (expr_p, stmt_p, NULL, is_gimple_val, fb_rvalue);
6370   expr = *expr_p;
6371
6372   /* Verify that we've an exact type match with the original expression.
6373      In particular, we do not wish to drop a "sizetype" in favour of a
6374      type of similar dimensions.  We don't want to pollute the generic
6375      type-stripping code with this knowledge because it doesn't matter
6376      for the bulk of GENERIC/GIMPLE.  It only matters that TYPE_SIZE_UNIT
6377      and friends retain their "sizetype-ness".  */
6378   if (TREE_TYPE (expr) != type
6379       && TREE_CODE (type) == INTEGER_TYPE
6380       && TYPE_IS_SIZETYPE (type))
6381     {
6382       tree tmp;
6383
6384       *expr_p = create_tmp_var (type, NULL);
6385       tmp = build1 (NOP_EXPR, type, expr);
6386       tmp = build_gimple_modify_stmt (*expr_p, tmp);
6387       if (EXPR_HAS_LOCATION (expr))
6388         SET_EXPR_LOCUS (tmp, EXPR_LOCUS (expr));
6389       else
6390         SET_EXPR_LOCATION (tmp, input_location);
6391
6392       gimplify_and_add (tmp, stmt_p);
6393     }
6394 }
6395 \f
6396 #ifdef ENABLE_CHECKING
6397 /* Compare types A and B for a "close enough" match.  */
6398
6399 static bool
6400 cpt_same_type (tree a, tree b)
6401 {
6402   if (useless_type_conversion_p (a, b))
6403     return true;
6404
6405   /* ??? The C++ FE decomposes METHOD_TYPES to FUNCTION_TYPES and doesn't
6406      link them together.  This routine is intended to catch type errors
6407      that will affect the optimizers, and the optimizers don't add new
6408      dereferences of function pointers, so ignore it.  */
6409   if ((TREE_CODE (a) == FUNCTION_TYPE || TREE_CODE (a) == METHOD_TYPE)
6410       && (TREE_CODE (b) == FUNCTION_TYPE || TREE_CODE (b) == METHOD_TYPE))
6411     return true;
6412
6413   /* ??? The C FE pushes type qualifiers after the fact into the type of
6414      the element from the type of the array.  See build_unary_op's handling
6415      of ADDR_EXPR.  This seems wrong -- if we were going to do this, we
6416      should have done it when creating the variable in the first place.
6417      Alternately, why aren't the two array types made variants?  */
6418   if (TREE_CODE (a) == ARRAY_TYPE && TREE_CODE (b) == ARRAY_TYPE)
6419     return cpt_same_type (TREE_TYPE (a), TREE_TYPE (b));
6420
6421   /* And because of those, we have to recurse down through pointers.  */
6422   if (POINTER_TYPE_P (a) && POINTER_TYPE_P (b))
6423     return cpt_same_type (TREE_TYPE (a), TREE_TYPE (b));
6424
6425   return false;
6426 }
6427
6428 /* Check for some cases of the front end missing cast expressions.
6429    The type of a dereference should correspond to the pointer type;
6430    similarly the type of an address should match its object.  */
6431
6432 static tree
6433 check_pointer_types_r (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
6434                        void *data ATTRIBUTE_UNUSED)
6435 {
6436   tree t = *tp;
6437   tree ptype, otype, dtype;
6438
6439   switch (TREE_CODE (t))
6440     {
6441     case INDIRECT_REF:
6442     case ARRAY_REF:
6443       otype = TREE_TYPE (t);
6444       ptype = TREE_TYPE (TREE_OPERAND (t, 0));
6445       dtype = TREE_TYPE (ptype);
6446       gcc_assert (cpt_same_type (otype, dtype));
6447       break;
6448
6449     case ADDR_EXPR:
6450       ptype = TREE_TYPE (t);
6451       otype = TREE_TYPE (TREE_OPERAND (t, 0));
6452       dtype = TREE_TYPE (ptype);
6453       if (!cpt_same_type (dtype, otype))
6454         {
6455           /* &array is allowed to produce a pointer to the element, rather than
6456              a pointer to the array type.  We must allow this in order to
6457              properly represent assigning the address of an array in C into
6458              pointer to the element type.  */
6459           gcc_assert (TREE_CODE (otype) == ARRAY_TYPE
6460                       && POINTER_TYPE_P (ptype)
6461                       && cpt_same_type (dtype, TREE_TYPE (otype)));
6462           break;
6463         }
6464       break;
6465
6466     default:
6467       return NULL_TREE;
6468     }
6469
6470
6471   return NULL_TREE;
6472 }
6473 #endif
6474
6475 /* Gimplify the body of statements pointed to by BODY_P.  FNDECL is the
6476    function decl containing BODY.  */
6477
6478 void
6479 gimplify_body (tree *body_p, tree fndecl, bool do_parms)
6480 {
6481   location_t saved_location = input_location;
6482   tree body, parm_stmts;
6483
6484   timevar_push (TV_TREE_GIMPLIFY);
6485
6486   gcc_assert (gimplify_ctxp == NULL);
6487   push_gimplify_context ();
6488
6489   /* Unshare most shared trees in the body and in that of any nested functions.
6490      It would seem we don't have to do this for nested functions because
6491      they are supposed to be output and then the outer function gimplified
6492      first, but the g++ front end doesn't always do it that way.  */
6493   unshare_body (body_p, fndecl);
6494   unvisit_body (body_p, fndecl);
6495
6496   /* Make sure input_location isn't set to something wierd.  */
6497   input_location = DECL_SOURCE_LOCATION (fndecl);
6498
6499   /* Resolve callee-copies.  This has to be done before processing
6500      the body so that DECL_VALUE_EXPR gets processed correctly.  */
6501   parm_stmts = do_parms ? gimplify_parameters () : NULL;
6502
6503   /* Gimplify the function's body.  */
6504   gimplify_stmt (body_p);
6505   body = *body_p;
6506
6507   if (!body)
6508     body = alloc_stmt_list ();
6509   else if (TREE_CODE (body) == STATEMENT_LIST)
6510     {
6511       tree t = expr_only (*body_p);
6512       if (t)
6513         body = t;
6514     }
6515
6516   /* If there isn't an outer BIND_EXPR, add one.  */
6517   if (TREE_CODE (body) != BIND_EXPR)
6518     {
6519       tree b = build3 (BIND_EXPR, void_type_node, NULL_TREE,
6520                        NULL_TREE, NULL_TREE);
6521       TREE_SIDE_EFFECTS (b) = 1;
6522       append_to_statement_list_force (body, &BIND_EXPR_BODY (b));
6523       body = b;
6524     }
6525
6526   /* If we had callee-copies statements, insert them at the beginning
6527      of the function.  */
6528   if (parm_stmts)
6529     {
6530       append_to_statement_list_force (BIND_EXPR_BODY (body), &parm_stmts);
6531       BIND_EXPR_BODY (body) = parm_stmts;
6532     }
6533
6534   /* Unshare again, in case gimplification was sloppy.  */
6535   unshare_all_trees (body);
6536
6537   *body_p = body;
6538
6539   pop_gimplify_context (body);
6540   gcc_assert (gimplify_ctxp == NULL);
6541
6542 #ifdef ENABLE_CHECKING
6543   walk_tree (body_p, check_pointer_types_r, NULL, NULL);
6544 #endif
6545
6546   timevar_pop (TV_TREE_GIMPLIFY);
6547   input_location = saved_location;
6548 }
6549
6550 /* Entry point to the gimplification pass.  FNDECL is the FUNCTION_DECL
6551    node for the function we want to gimplify.  */
6552
6553 void
6554 gimplify_function_tree (tree fndecl)
6555 {
6556   tree oldfn, parm, ret;
6557
6558   oldfn = current_function_decl;
6559   current_function_decl = fndecl;
6560   cfun = DECL_STRUCT_FUNCTION (fndecl);
6561   if (cfun == NULL)
6562     allocate_struct_function (fndecl);
6563
6564   for (parm = DECL_ARGUMENTS (fndecl); parm ; parm = TREE_CHAIN (parm))
6565     {
6566       /* Preliminarily mark non-addressed complex variables as eligible
6567          for promotion to gimple registers.  We'll transform their uses
6568          as we find them.  */
6569       if ((TREE_CODE (TREE_TYPE (parm)) == COMPLEX_TYPE
6570            || TREE_CODE (TREE_TYPE (parm)) == VECTOR_TYPE)
6571           && !TREE_THIS_VOLATILE (parm)
6572           && !needs_to_live_in_memory (parm))
6573         DECL_GIMPLE_REG_P (parm) = 1;
6574     }
6575
6576   ret = DECL_RESULT (fndecl);
6577   if ((TREE_CODE (TREE_TYPE (ret)) == COMPLEX_TYPE
6578            || TREE_CODE (TREE_TYPE (ret)) == VECTOR_TYPE)
6579       && !needs_to_live_in_memory (ret))
6580     DECL_GIMPLE_REG_P (ret) = 1;
6581
6582   gimplify_body (&DECL_SAVED_TREE (fndecl), fndecl, true);
6583
6584   /* If we're instrumenting function entry/exit, then prepend the call to
6585      the entry hook and wrap the whole function in a TRY_FINALLY_EXPR to
6586      catch the exit hook.  */
6587   /* ??? Add some way to ignore exceptions for this TFE.  */
6588   if (flag_instrument_function_entry_exit
6589       && ! DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (fndecl))
6590     {
6591       tree tf, x, bind;
6592
6593       tf = build2 (TRY_FINALLY_EXPR, void_type_node, NULL, NULL);
6594       TREE_SIDE_EFFECTS (tf) = 1;
6595       x = DECL_SAVED_TREE (fndecl);
6596       append_to_statement_list (x, &TREE_OPERAND (tf, 0));
6597       x = implicit_built_in_decls[BUILT_IN_PROFILE_FUNC_EXIT];
6598       x = build_call_expr (x, 0);
6599       append_to_statement_list (x, &TREE_OPERAND (tf, 1));
6600
6601       bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
6602       TREE_SIDE_EFFECTS (bind) = 1;
6603       x = implicit_built_in_decls[BUILT_IN_PROFILE_FUNC_ENTER];
6604       x = build_call_expr (x, 0);
6605       append_to_statement_list (x, &BIND_EXPR_BODY (bind));
6606       append_to_statement_list (tf, &BIND_EXPR_BODY (bind));
6607
6608       DECL_SAVED_TREE (fndecl) = bind;
6609     }
6610
6611   cfun->gimplified = true;
6612   current_function_decl = oldfn;
6613   cfun = oldfn ? DECL_STRUCT_FUNCTION (oldfn) : NULL;
6614 }
6615 \f
6616 /* Expands EXPR to list of gimple statements STMTS.  If SIMPLE is true,
6617    force the result to be either ssa_name or an invariant, otherwise
6618    just force it to be a rhs expression.  If VAR is not NULL, make the
6619    base variable of the final destination be VAR if suitable.  */
6620
6621 tree
6622 force_gimple_operand (tree expr, tree *stmts, bool simple, tree var)
6623 {
6624   tree t;
6625   enum gimplify_status ret;
6626   gimple_predicate gimple_test_f;
6627
6628   *stmts = NULL_TREE;
6629
6630   if (is_gimple_val (expr))
6631     return expr;
6632
6633   gimple_test_f = simple ? is_gimple_val : is_gimple_reg_rhs;
6634
6635   push_gimplify_context ();
6636   gimplify_ctxp->into_ssa = gimple_in_ssa_p (cfun);
6637
6638   if (var)
6639     expr = build_gimple_modify_stmt (var, expr);
6640
6641   ret = gimplify_expr (&expr, stmts, NULL,
6642                        gimple_test_f, fb_rvalue);
6643   gcc_assert (ret != GS_ERROR);
6644
6645   if (gimple_referenced_vars (cfun))
6646     {
6647       for (t = gimplify_ctxp->temps; t ; t = TREE_CHAIN (t))
6648         add_referenced_var (t);
6649     }
6650
6651   pop_gimplify_context (NULL);
6652
6653   return expr;
6654 }
6655
6656 /* Invokes force_gimple_operand for EXPR with parameters SIMPLE_P and VAR.  If
6657    some statements are produced, emits them at BSI.  If BEFORE is true.
6658    the statements are appended before BSI, otherwise they are appended after
6659    it.  M specifies the way BSI moves after insertion (BSI_SAME_STMT or
6660    BSI_CONTINUE_LINKING are the usual values).  */
6661
6662 tree
6663 force_gimple_operand_bsi (block_stmt_iterator *bsi, tree expr,
6664                           bool simple_p, tree var, bool before,
6665                           enum bsi_iterator_update m)
6666 {
6667   tree stmts;
6668
6669   expr = force_gimple_operand (expr, &stmts, simple_p, var);
6670   if (stmts)
6671     {
6672       if (gimple_in_ssa_p (cfun))
6673         {
6674           tree_stmt_iterator tsi;
6675
6676           for (tsi = tsi_start (stmts); !tsi_end_p (tsi); tsi_next (&tsi))
6677             mark_symbols_for_renaming (tsi_stmt (tsi));
6678         }
6679
6680       if (before)
6681         bsi_insert_before (bsi, stmts, m);
6682       else
6683         bsi_insert_after (bsi, stmts, m);
6684     }
6685
6686   return expr;
6687 }
6688
6689 #include "gt-gimplify.h"