OSDN Git Service

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