OSDN Git Service

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