OSDN Git Service

gcc/
[pf3gnuchains/gcc-fork.git] / gcc / gimplify.c
1 /* Tree lowering pass.  This pass converts the GENERIC functions-as-trees
2    tree representation into the GIMPLE form.
3    Copyright (C) 2002, 2003, 2004, 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 (variably_modified_type_p (TREE_TYPE (*to_p), NULL_TREE))
4421                 /* Always use the target and thus RSO for variable-sized types.
4422                    GIMPLE cannot deal with a variable-sized assignment
4423                    embedded in a call statement.  */
4424                 use_target = true;
4425               else if (TREE_CODE (*to_p) != SSA_NAME
4426                       && (!is_gimple_variable (*to_p)
4427                           || needs_to_live_in_memory (*to_p)))
4428                 /* Don't use the original target if it's already addressable;
4429                    if its address escapes, and the called function uses the
4430                    NRV optimization, a conforming program could see *to_p
4431                    change before the called function returns; see c++/19317.
4432                    When optimizing, the return_slot pass marks more functions
4433                    as safe after we have escape info.  */
4434                 use_target = false;
4435               else
4436                 use_target = true;
4437
4438               if (use_target)
4439                 {
4440                   CALL_EXPR_RETURN_SLOT_OPT (*from_p) = 1;
4441                   mark_addressable (*to_p);
4442                 }
4443             }
4444           break;
4445
4446         case WITH_SIZE_EXPR:
4447           /* Likewise for calls that return an aggregate of non-constant size,
4448              since we would not be able to generate a temporary at all.  */
4449           if (TREE_CODE (TREE_OPERAND (*from_p, 0)) == CALL_EXPR)
4450             {
4451               *from_p = TREE_OPERAND (*from_p, 0);
4452               /* We don't change ret in this case because the
4453                  WITH_SIZE_EXPR might have been added in
4454                  gimplify_modify_expr, so returning GS_OK would lead to an
4455                  infinite loop.  */
4456               changed = true;
4457             }
4458           break;
4459
4460           /* If we're initializing from a container, push the initialization
4461              inside it.  */
4462         case CLEANUP_POINT_EXPR:
4463         case BIND_EXPR:
4464         case STATEMENT_LIST:
4465           {
4466             tree wrap = *from_p;
4467             tree t;
4468
4469             ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_min_lval,
4470                                  fb_lvalue);
4471             if (ret != GS_ERROR)
4472               ret = GS_OK;
4473
4474             t = voidify_wrapper_expr (wrap, *expr_p);
4475             gcc_assert (t == *expr_p);
4476
4477             if (want_value)
4478               {
4479                 gimplify_and_add (wrap, pre_p);
4480                 *expr_p = unshare_expr (*to_p);
4481               }
4482             else
4483               *expr_p = wrap;
4484             return GS_OK;
4485           }
4486
4487         case COMPOUND_LITERAL_EXPR:
4488           {
4489             tree complit = TREE_OPERAND (*expr_p, 1);
4490             tree decl_s = COMPOUND_LITERAL_EXPR_DECL_EXPR (complit);
4491             tree decl = DECL_EXPR_DECL (decl_s);
4492             tree init = DECL_INITIAL (decl);
4493
4494             /* struct T x = (struct T) { 0, 1, 2 } can be optimized
4495                into struct T x = { 0, 1, 2 } if the address of the
4496                compound literal has never been taken.  */
4497             if (!TREE_ADDRESSABLE (complit)
4498                 && !TREE_ADDRESSABLE (decl)
4499                 && init)
4500               {
4501                 *expr_p = copy_node (*expr_p);
4502                 TREE_OPERAND (*expr_p, 1) = init;
4503                 return GS_OK;
4504               }
4505           }
4506
4507         default:
4508           break;
4509         }
4510     }
4511   while (changed);
4512
4513   return ret;
4514 }
4515
4516 /* Promote partial stores to COMPLEX variables to total stores.  *EXPR_P is
4517    a MODIFY_EXPR with a lhs of a REAL/IMAGPART_EXPR of a variable with
4518    DECL_GIMPLE_REG_P set.
4519
4520    IMPORTANT NOTE: This promotion is performed by introducing a load of the
4521    other, unmodified part of the complex object just before the total store.
4522    As a consequence, if the object is still uninitialized, an undefined value
4523    will be loaded into a register, which may result in a spurious exception
4524    if the register is floating-point and the value happens to be a signaling
4525    NaN for example.  Then the fully-fledged complex operations lowering pass
4526    followed by a DCE pass are necessary in order to fix things up.  */
4527
4528 static enum gimplify_status
4529 gimplify_modify_expr_complex_part (tree *expr_p, gimple_seq *pre_p,
4530                                    bool want_value)
4531 {
4532   enum tree_code code, ocode;
4533   tree lhs, rhs, new_rhs, other, realpart, imagpart;
4534
4535   lhs = TREE_OPERAND (*expr_p, 0);
4536   rhs = TREE_OPERAND (*expr_p, 1);
4537   code = TREE_CODE (lhs);
4538   lhs = TREE_OPERAND (lhs, 0);
4539
4540   ocode = code == REALPART_EXPR ? IMAGPART_EXPR : REALPART_EXPR;
4541   other = build1 (ocode, TREE_TYPE (rhs), lhs);
4542   TREE_NO_WARNING (other) = 1;
4543   other = get_formal_tmp_var (other, pre_p);
4544
4545   realpart = code == REALPART_EXPR ? rhs : other;
4546   imagpart = code == REALPART_EXPR ? other : rhs;
4547
4548   if (TREE_CONSTANT (realpart) && TREE_CONSTANT (imagpart))
4549     new_rhs = build_complex (TREE_TYPE (lhs), realpart, imagpart);
4550   else
4551     new_rhs = build2 (COMPLEX_EXPR, TREE_TYPE (lhs), realpart, imagpart);
4552
4553   gimplify_seq_add_stmt (pre_p, gimple_build_assign (lhs, new_rhs));
4554   *expr_p = (want_value) ? rhs : NULL_TREE;
4555
4556   return GS_ALL_DONE;
4557 }
4558
4559 /* Gimplify the MODIFY_EXPR node pointed to by EXPR_P.
4560
4561       modify_expr
4562               : varname '=' rhs
4563               | '*' ID '=' rhs
4564
4565     PRE_P points to the list where side effects that must happen before
4566         *EXPR_P should be stored.
4567
4568     POST_P points to the list where side effects that must happen after
4569         *EXPR_P should be stored.
4570
4571     WANT_VALUE is nonzero iff we want to use the value of this expression
4572         in another expression.  */
4573
4574 static enum gimplify_status
4575 gimplify_modify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
4576                       bool want_value)
4577 {
4578   tree *from_p = &TREE_OPERAND (*expr_p, 1);
4579   tree *to_p = &TREE_OPERAND (*expr_p, 0);
4580   enum gimplify_status ret = GS_UNHANDLED;
4581   gimple assign;
4582   location_t loc = EXPR_LOCATION (*expr_p);
4583
4584   gcc_assert (TREE_CODE (*expr_p) == MODIFY_EXPR
4585               || TREE_CODE (*expr_p) == INIT_EXPR);
4586
4587   /* Trying to simplify a clobber using normal logic doesn't work,
4588      so handle it here.  */
4589   if (TREE_CLOBBER_P (*from_p))
4590     {
4591       gcc_assert (!want_value && TREE_CODE (*to_p) == VAR_DECL);
4592       gimplify_seq_add_stmt (pre_p, gimple_build_assign (*to_p, *from_p));
4593       *expr_p = NULL;
4594       return GS_ALL_DONE;
4595     }
4596
4597   /* Insert pointer conversions required by the middle-end that are not
4598      required by the frontend.  This fixes middle-end type checking for
4599      for example gcc.dg/redecl-6.c.  */
4600   if (POINTER_TYPE_P (TREE_TYPE (*to_p)))
4601     {
4602       STRIP_USELESS_TYPE_CONVERSION (*from_p);
4603       if (!useless_type_conversion_p (TREE_TYPE (*to_p), TREE_TYPE (*from_p)))
4604         *from_p = fold_convert_loc (loc, TREE_TYPE (*to_p), *from_p);
4605     }
4606
4607   /* See if any simplifications can be done based on what the RHS is.  */
4608   ret = gimplify_modify_expr_rhs (expr_p, from_p, to_p, pre_p, post_p,
4609                                   want_value);
4610   if (ret != GS_UNHANDLED)
4611     return ret;
4612
4613   /* For zero sized types only gimplify the left hand side and right hand
4614      side as statements and throw away the assignment.  Do this after
4615      gimplify_modify_expr_rhs so we handle TARGET_EXPRs of addressable
4616      types properly.  */
4617   if (zero_sized_type (TREE_TYPE (*from_p)) && !want_value)
4618     {
4619       gimplify_stmt (from_p, pre_p);
4620       gimplify_stmt (to_p, pre_p);
4621       *expr_p = NULL_TREE;
4622       return GS_ALL_DONE;
4623     }
4624
4625   /* If the value being copied is of variable width, compute the length
4626      of the copy into a WITH_SIZE_EXPR.   Note that we need to do this
4627      before gimplifying any of the operands so that we can resolve any
4628      PLACEHOLDER_EXPRs in the size.  Also note that the RTL expander uses
4629      the size of the expression to be copied, not of the destination, so
4630      that is what we must do here.  */
4631   maybe_with_size_expr (from_p);
4632
4633   ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
4634   if (ret == GS_ERROR)
4635     return ret;
4636
4637   /* As a special case, we have to temporarily allow for assignments
4638      with a CALL_EXPR on the RHS.  Since in GIMPLE a function call is
4639      a toplevel statement, when gimplifying the GENERIC expression
4640      MODIFY_EXPR <a, CALL_EXPR <foo>>, we cannot create the tuple
4641      GIMPLE_ASSIGN <a, GIMPLE_CALL <foo>>.
4642
4643      Instead, we need to create the tuple GIMPLE_CALL <a, foo>.  To
4644      prevent gimplify_expr from trying to create a new temporary for
4645      foo's LHS, we tell it that it should only gimplify until it
4646      reaches the CALL_EXPR.  On return from gimplify_expr, the newly
4647      created GIMPLE_CALL <foo> will be the last statement in *PRE_P
4648      and all we need to do here is set 'a' to be its LHS.  */
4649   ret = gimplify_expr (from_p, pre_p, post_p, rhs_predicate_for (*to_p),
4650                        fb_rvalue);
4651   if (ret == GS_ERROR)
4652     return ret;
4653
4654   /* Now see if the above changed *from_p to something we handle specially.  */
4655   ret = gimplify_modify_expr_rhs (expr_p, from_p, to_p, pre_p, post_p,
4656                                   want_value);
4657   if (ret != GS_UNHANDLED)
4658     return ret;
4659
4660   /* If we've got a variable sized assignment between two lvalues (i.e. does
4661      not involve a call), then we can make things a bit more straightforward
4662      by converting the assignment to memcpy or memset.  */
4663   if (TREE_CODE (*from_p) == WITH_SIZE_EXPR)
4664     {
4665       tree from = TREE_OPERAND (*from_p, 0);
4666       tree size = TREE_OPERAND (*from_p, 1);
4667
4668       if (TREE_CODE (from) == CONSTRUCTOR)
4669         return gimplify_modify_expr_to_memset (expr_p, size, want_value, pre_p);
4670
4671       if (is_gimple_addressable (from))
4672         {
4673           *from_p = from;
4674           return gimplify_modify_expr_to_memcpy (expr_p, size, want_value,
4675                                                  pre_p);
4676         }
4677     }
4678
4679   /* Transform partial stores to non-addressable complex variables into
4680      total stores.  This allows us to use real instead of virtual operands
4681      for these variables, which improves optimization.  */
4682   if ((TREE_CODE (*to_p) == REALPART_EXPR
4683        || TREE_CODE (*to_p) == IMAGPART_EXPR)
4684       && is_gimple_reg (TREE_OPERAND (*to_p, 0)))
4685     return gimplify_modify_expr_complex_part (expr_p, pre_p, want_value);
4686
4687   /* Try to alleviate the effects of the gimplification creating artificial
4688      temporaries (see for example is_gimple_reg_rhs) on the debug info.  */
4689   if (!gimplify_ctxp->into_ssa
4690       && TREE_CODE (*from_p) == VAR_DECL
4691       && DECL_IGNORED_P (*from_p)
4692       && DECL_P (*to_p)
4693       && !DECL_IGNORED_P (*to_p))
4694     {
4695       if (!DECL_NAME (*from_p) && DECL_NAME (*to_p))
4696         DECL_NAME (*from_p)
4697           = create_tmp_var_name (IDENTIFIER_POINTER (DECL_NAME (*to_p)));
4698       DECL_DEBUG_EXPR_IS_FROM (*from_p) = 1;
4699       SET_DECL_DEBUG_EXPR (*from_p, *to_p);
4700    }
4701
4702   if (want_value && TREE_THIS_VOLATILE (*to_p))
4703     *from_p = get_initialized_tmp_var (*from_p, pre_p, post_p);
4704
4705   if (TREE_CODE (*from_p) == CALL_EXPR)
4706     {
4707       /* Since the RHS is a CALL_EXPR, we need to create a GIMPLE_CALL
4708          instead of a GIMPLE_ASSIGN.  */
4709       tree fnptrtype = TREE_TYPE (CALL_EXPR_FN (*from_p));
4710       CALL_EXPR_FN (*from_p) = TREE_OPERAND (CALL_EXPR_FN (*from_p), 0);
4711       STRIP_USELESS_TYPE_CONVERSION (CALL_EXPR_FN (*from_p));
4712       assign = gimple_build_call_from_tree (*from_p);
4713       gimple_call_set_fntype (assign, TREE_TYPE (fnptrtype));
4714       if (!gimple_call_noreturn_p (assign))
4715         gimple_call_set_lhs (assign, *to_p);
4716     }
4717   else
4718     {
4719       assign = gimple_build_assign (*to_p, *from_p);
4720       gimple_set_location (assign, EXPR_LOCATION (*expr_p));
4721     }
4722
4723   gimplify_seq_add_stmt (pre_p, assign);
4724
4725   if (gimplify_ctxp->into_ssa && is_gimple_reg (*to_p))
4726     {
4727       /* If we've somehow already got an SSA_NAME on the LHS, then
4728          we've probably modified it twice.  Not good.  */
4729       gcc_assert (TREE_CODE (*to_p) != SSA_NAME);
4730       *to_p = make_ssa_name (*to_p, assign);
4731       gimple_set_lhs (assign, *to_p);
4732     }
4733
4734   if (want_value)
4735     {
4736       *expr_p = TREE_THIS_VOLATILE (*to_p) ? *from_p : unshare_expr (*to_p);
4737       return GS_OK;
4738     }
4739   else
4740     *expr_p = NULL;
4741
4742   return GS_ALL_DONE;
4743 }
4744
4745 /* Gimplify a comparison between two variable-sized objects.  Do this
4746    with a call to BUILT_IN_MEMCMP.  */
4747
4748 static enum gimplify_status
4749 gimplify_variable_sized_compare (tree *expr_p)
4750 {
4751   location_t loc = EXPR_LOCATION (*expr_p);
4752   tree op0 = TREE_OPERAND (*expr_p, 0);
4753   tree op1 = TREE_OPERAND (*expr_p, 1);
4754   tree t, arg, dest, src, expr;
4755
4756   arg = TYPE_SIZE_UNIT (TREE_TYPE (op0));
4757   arg = unshare_expr (arg);
4758   arg = SUBSTITUTE_PLACEHOLDER_IN_EXPR (arg, op0);
4759   src = build_fold_addr_expr_loc (loc, op1);
4760   dest = build_fold_addr_expr_loc (loc, op0);
4761   t = builtin_decl_implicit (BUILT_IN_MEMCMP);
4762   t = build_call_expr_loc (loc, t, 3, dest, src, arg);
4763
4764   expr
4765     = build2 (TREE_CODE (*expr_p), TREE_TYPE (*expr_p), t, integer_zero_node);
4766   SET_EXPR_LOCATION (expr, loc);
4767   *expr_p = expr;
4768
4769   return GS_OK;
4770 }
4771
4772 /* Gimplify a comparison between two aggregate objects of integral scalar
4773    mode as a comparison between the bitwise equivalent scalar values.  */
4774
4775 static enum gimplify_status
4776 gimplify_scalar_mode_aggregate_compare (tree *expr_p)
4777 {
4778   location_t loc = EXPR_LOCATION (*expr_p);
4779   tree op0 = TREE_OPERAND (*expr_p, 0);
4780   tree op1 = TREE_OPERAND (*expr_p, 1);
4781
4782   tree type = TREE_TYPE (op0);
4783   tree scalar_type = lang_hooks.types.type_for_mode (TYPE_MODE (type), 1);
4784
4785   op0 = fold_build1_loc (loc, VIEW_CONVERT_EXPR, scalar_type, op0);
4786   op1 = fold_build1_loc (loc, VIEW_CONVERT_EXPR, scalar_type, op1);
4787
4788   *expr_p
4789     = fold_build2_loc (loc, TREE_CODE (*expr_p), TREE_TYPE (*expr_p), op0, op1);
4790
4791   return GS_OK;
4792 }
4793
4794 /* Gimplify an expression sequence.  This function gimplifies each
4795    expression and rewrites the original expression with the last
4796    expression of the sequence in GIMPLE form.
4797
4798    PRE_P points to the list where the side effects for all the
4799        expressions in the sequence will be emitted.
4800
4801    WANT_VALUE is true when the result of the last COMPOUND_EXPR is used.  */
4802
4803 static enum gimplify_status
4804 gimplify_compound_expr (tree *expr_p, gimple_seq *pre_p, bool want_value)
4805 {
4806   tree t = *expr_p;
4807
4808   do
4809     {
4810       tree *sub_p = &TREE_OPERAND (t, 0);
4811
4812       if (TREE_CODE (*sub_p) == COMPOUND_EXPR)
4813         gimplify_compound_expr (sub_p, pre_p, false);
4814       else
4815         gimplify_stmt (sub_p, pre_p);
4816
4817       t = TREE_OPERAND (t, 1);
4818     }
4819   while (TREE_CODE (t) == COMPOUND_EXPR);
4820
4821   *expr_p = t;
4822   if (want_value)
4823     return GS_OK;
4824   else
4825     {
4826       gimplify_stmt (expr_p, pre_p);
4827       return GS_ALL_DONE;
4828     }
4829 }
4830
4831 /* Gimplify a SAVE_EXPR node.  EXPR_P points to the expression to
4832    gimplify.  After gimplification, EXPR_P will point to a new temporary
4833    that holds the original value of the SAVE_EXPR node.
4834
4835    PRE_P points to the list where side effects that must happen before
4836    *EXPR_P should be stored.  */
4837
4838 static enum gimplify_status
4839 gimplify_save_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
4840 {
4841   enum gimplify_status ret = GS_ALL_DONE;
4842   tree val;
4843
4844   gcc_assert (TREE_CODE (*expr_p) == SAVE_EXPR);
4845   val = TREE_OPERAND (*expr_p, 0);
4846
4847   /* If the SAVE_EXPR has not been resolved, then evaluate it once.  */
4848   if (!SAVE_EXPR_RESOLVED_P (*expr_p))
4849     {
4850       /* The operand may be a void-valued expression such as SAVE_EXPRs
4851          generated by the Java frontend for class initialization.  It is
4852          being executed only for its side-effects.  */
4853       if (TREE_TYPE (val) == void_type_node)
4854         {
4855           ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
4856                                is_gimple_stmt, fb_none);
4857           val = NULL;
4858         }
4859       else
4860         val = get_initialized_tmp_var (val, pre_p, post_p);
4861
4862       TREE_OPERAND (*expr_p, 0) = val;
4863       SAVE_EXPR_RESOLVED_P (*expr_p) = 1;
4864     }
4865
4866   *expr_p = val;
4867
4868   return ret;
4869 }
4870
4871 /* Rewrite the ADDR_EXPR node pointed to by EXPR_P
4872
4873       unary_expr
4874               : ...
4875               | '&' varname
4876               ...
4877
4878     PRE_P points to the list where side effects that must happen before
4879         *EXPR_P should be stored.
4880
4881     POST_P points to the list where side effects that must happen after
4882         *EXPR_P should be stored.  */
4883
4884 static enum gimplify_status
4885 gimplify_addr_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
4886 {
4887   tree expr = *expr_p;
4888   tree op0 = TREE_OPERAND (expr, 0);
4889   enum gimplify_status ret;
4890   location_t loc = EXPR_LOCATION (*expr_p);
4891
4892   switch (TREE_CODE (op0))
4893     {
4894     case INDIRECT_REF:
4895     do_indirect_ref:
4896       /* Check if we are dealing with an expression of the form '&*ptr'.
4897          While the front end folds away '&*ptr' into 'ptr', these
4898          expressions may be generated internally by the compiler (e.g.,
4899          builtins like __builtin_va_end).  */
4900       /* Caution: the silent array decomposition semantics we allow for
4901          ADDR_EXPR means we can't always discard the pair.  */
4902       /* Gimplification of the ADDR_EXPR operand may drop
4903          cv-qualification conversions, so make sure we add them if
4904          needed.  */
4905       {
4906         tree op00 = TREE_OPERAND (op0, 0);
4907         tree t_expr = TREE_TYPE (expr);
4908         tree t_op00 = TREE_TYPE (op00);
4909
4910         if (!useless_type_conversion_p (t_expr, t_op00))
4911           op00 = fold_convert_loc (loc, TREE_TYPE (expr), op00);
4912         *expr_p = op00;
4913         ret = GS_OK;
4914       }
4915       break;
4916
4917     case VIEW_CONVERT_EXPR:
4918       /* Take the address of our operand and then convert it to the type of
4919          this ADDR_EXPR.
4920
4921          ??? The interactions of VIEW_CONVERT_EXPR and aliasing is not at
4922          all clear.  The impact of this transformation is even less clear.  */
4923
4924       /* If the operand is a useless conversion, look through it.  Doing so
4925          guarantees that the ADDR_EXPR and its operand will remain of the
4926          same type.  */
4927       if (tree_ssa_useless_type_conversion (TREE_OPERAND (op0, 0)))
4928         op0 = TREE_OPERAND (op0, 0);
4929
4930       *expr_p = fold_convert_loc (loc, TREE_TYPE (expr),
4931                                   build_fold_addr_expr_loc (loc,
4932                                                         TREE_OPERAND (op0, 0)));
4933       ret = GS_OK;
4934       break;
4935
4936     default:
4937       /* We use fb_either here because the C frontend sometimes takes
4938          the address of a call that returns a struct; see
4939          gcc.dg/c99-array-lval-1.c.  The gimplifier will correctly make
4940          the implied temporary explicit.  */
4941
4942       /* Make the operand addressable.  */
4943       ret = gimplify_expr (&TREE_OPERAND (expr, 0), pre_p, post_p,
4944                            is_gimple_addressable, fb_either);
4945       if (ret == GS_ERROR)
4946         break;
4947
4948       /* Then mark it.  Beware that it may not be possible to do so directly
4949          if a temporary has been created by the gimplification.  */
4950       prepare_gimple_addressable (&TREE_OPERAND (expr, 0), pre_p);
4951
4952       op0 = TREE_OPERAND (expr, 0);
4953
4954       /* For various reasons, the gimplification of the expression
4955          may have made a new INDIRECT_REF.  */
4956       if (TREE_CODE (op0) == INDIRECT_REF)
4957         goto do_indirect_ref;
4958
4959       mark_addressable (TREE_OPERAND (expr, 0));
4960
4961       /* The FEs may end up building ADDR_EXPRs early on a decl with
4962          an incomplete type.  Re-build ADDR_EXPRs in canonical form
4963          here.  */
4964       if (!types_compatible_p (TREE_TYPE (op0), TREE_TYPE (TREE_TYPE (expr))))
4965         *expr_p = build_fold_addr_expr (op0);
4966
4967       /* Make sure TREE_CONSTANT and TREE_SIDE_EFFECTS are set properly.  */
4968       recompute_tree_invariant_for_addr_expr (*expr_p);
4969
4970       /* If we re-built the ADDR_EXPR add a conversion to the original type
4971          if required.  */
4972       if (!useless_type_conversion_p (TREE_TYPE (expr), TREE_TYPE (*expr_p)))
4973         *expr_p = fold_convert (TREE_TYPE (expr), *expr_p);
4974
4975       break;
4976     }
4977
4978   return ret;
4979 }
4980
4981 /* Gimplify the operands of an ASM_EXPR.  Input operands should be a gimple
4982    value; output operands should be a gimple lvalue.  */
4983
4984 static enum gimplify_status
4985 gimplify_asm_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
4986 {
4987   tree expr;
4988   int noutputs;
4989   const char **oconstraints;
4990   int i;
4991   tree link;
4992   const char *constraint;
4993   bool allows_mem, allows_reg, is_inout;
4994   enum gimplify_status ret, tret;
4995   gimple stmt;
4996   VEC(tree, gc) *inputs;
4997   VEC(tree, gc) *outputs;
4998   VEC(tree, gc) *clobbers;
4999   VEC(tree, gc) *labels;
5000   tree link_next;
5001
5002   expr = *expr_p;
5003   noutputs = list_length (ASM_OUTPUTS (expr));
5004   oconstraints = (const char **) alloca ((noutputs) * sizeof (const char *));
5005
5006   inputs = outputs = clobbers = labels = NULL;
5007
5008   ret = GS_ALL_DONE;
5009   link_next = NULL_TREE;
5010   for (i = 0, link = ASM_OUTPUTS (expr); link; ++i, link = link_next)
5011     {
5012       bool ok;
5013       size_t constraint_len;
5014
5015       link_next = TREE_CHAIN (link);
5016
5017       oconstraints[i]
5018         = constraint
5019         = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
5020       constraint_len = strlen (constraint);
5021       if (constraint_len == 0)
5022         continue;
5023
5024       ok = parse_output_constraint (&constraint, i, 0, 0,
5025                                     &allows_mem, &allows_reg, &is_inout);
5026       if (!ok)
5027         {
5028           ret = GS_ERROR;
5029           is_inout = false;
5030         }
5031
5032       if (!allows_reg && allows_mem)
5033         mark_addressable (TREE_VALUE (link));
5034
5035       tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
5036                             is_inout ? is_gimple_min_lval : is_gimple_lvalue,
5037                             fb_lvalue | fb_mayfail);
5038       if (tret == GS_ERROR)
5039         {
5040           error ("invalid lvalue in asm output %d", i);
5041           ret = tret;
5042         }
5043
5044       VEC_safe_push (tree, gc, outputs, link);
5045       TREE_CHAIN (link) = NULL_TREE;
5046
5047       if (is_inout)
5048         {
5049           /* An input/output operand.  To give the optimizers more
5050              flexibility, split it into separate input and output
5051              operands.  */
5052           tree input;
5053           char buf[10];
5054
5055           /* Turn the in/out constraint into an output constraint.  */
5056           char *p = xstrdup (constraint);
5057           p[0] = '=';
5058           TREE_VALUE (TREE_PURPOSE (link)) = build_string (constraint_len, p);
5059
5060           /* And add a matching input constraint.  */
5061           if (allows_reg)
5062             {
5063               sprintf (buf, "%d", i);
5064
5065               /* If there are multiple alternatives in the constraint,
5066                  handle each of them individually.  Those that allow register
5067                  will be replaced with operand number, the others will stay
5068                  unchanged.  */
5069               if (strchr (p, ',') != NULL)
5070                 {
5071                   size_t len = 0, buflen = strlen (buf);
5072                   char *beg, *end, *str, *dst;
5073
5074                   for (beg = p + 1;;)
5075                     {
5076                       end = strchr (beg, ',');
5077                       if (end == NULL)
5078                         end = strchr (beg, '\0');
5079                       if ((size_t) (end - beg) < buflen)
5080                         len += buflen + 1;
5081                       else
5082                         len += end - beg + 1;
5083                       if (*end)
5084                         beg = end + 1;
5085                       else
5086                         break;
5087                     }
5088
5089                   str = (char *) alloca (len);
5090                   for (beg = p + 1, dst = str;;)
5091                     {
5092                       const char *tem;
5093                       bool mem_p, reg_p, inout_p;
5094
5095                       end = strchr (beg, ',');
5096                       if (end)
5097                         *end = '\0';
5098                       beg[-1] = '=';
5099                       tem = beg - 1;
5100                       parse_output_constraint (&tem, i, 0, 0,
5101                                                &mem_p, &reg_p, &inout_p);
5102                       if (dst != str)
5103                         *dst++ = ',';
5104                       if (reg_p)
5105                         {
5106                           memcpy (dst, buf, buflen);
5107                           dst += buflen;
5108                         }
5109                       else
5110                         {
5111                           if (end)
5112                             len = end - beg;
5113                           else
5114                             len = strlen (beg);
5115                           memcpy (dst, beg, len);
5116                           dst += len;
5117                         }
5118                       if (end)
5119                         beg = end + 1;
5120                       else
5121                         break;
5122                     }
5123                   *dst = '\0';
5124                   input = build_string (dst - str, str);
5125                 }
5126               else
5127                 input = build_string (strlen (buf), buf);
5128             }
5129           else
5130             input = build_string (constraint_len - 1, constraint + 1);
5131
5132           free (p);
5133
5134           input = build_tree_list (build_tree_list (NULL_TREE, input),
5135                                    unshare_expr (TREE_VALUE (link)));
5136           ASM_INPUTS (expr) = chainon (ASM_INPUTS (expr), input);
5137         }
5138     }
5139
5140   link_next = NULL_TREE;
5141   for (link = ASM_INPUTS (expr); link; ++i, link = link_next)
5142     {
5143       link_next = TREE_CHAIN (link);
5144       constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
5145       parse_input_constraint (&constraint, 0, 0, noutputs, 0,
5146                               oconstraints, &allows_mem, &allows_reg);
5147
5148       /* If we can't make copies, we can only accept memory.  */
5149       if (TREE_ADDRESSABLE (TREE_TYPE (TREE_VALUE (link))))
5150         {
5151           if (allows_mem)
5152             allows_reg = 0;
5153           else
5154             {
5155               error ("impossible constraint in %<asm%>");
5156               error ("non-memory input %d must stay in memory", i);
5157               return GS_ERROR;
5158             }
5159         }
5160
5161       /* If the operand is a memory input, it should be an lvalue.  */
5162       if (!allows_reg && allows_mem)
5163         {
5164           tree inputv = TREE_VALUE (link);
5165           STRIP_NOPS (inputv);
5166           if (TREE_CODE (inputv) == PREDECREMENT_EXPR
5167               || TREE_CODE (inputv) == PREINCREMENT_EXPR
5168               || TREE_CODE (inputv) == POSTDECREMENT_EXPR
5169               || TREE_CODE (inputv) == POSTINCREMENT_EXPR)
5170             TREE_VALUE (link) = error_mark_node;
5171           tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
5172                                 is_gimple_lvalue, fb_lvalue | fb_mayfail);
5173           mark_addressable (TREE_VALUE (link));
5174           if (tret == GS_ERROR)
5175             {
5176               if (EXPR_HAS_LOCATION (TREE_VALUE (link)))
5177                 input_location = EXPR_LOCATION (TREE_VALUE (link));
5178               error ("memory input %d is not directly addressable", i);
5179               ret = tret;
5180             }
5181         }
5182       else
5183         {
5184           tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
5185                                 is_gimple_asm_val, fb_rvalue);
5186           if (tret == GS_ERROR)
5187             ret = tret;
5188         }
5189
5190       TREE_CHAIN (link) = NULL_TREE;
5191       VEC_safe_push (tree, gc, inputs, link);
5192     }
5193
5194   for (link = ASM_CLOBBERS (expr); link; ++i, link = TREE_CHAIN (link))
5195     VEC_safe_push (tree, gc, clobbers, link);
5196
5197   for (link = ASM_LABELS (expr); link; ++i, link = TREE_CHAIN (link))
5198     VEC_safe_push (tree, gc, labels, link);
5199
5200   /* Do not add ASMs with errors to the gimple IL stream.  */
5201   if (ret != GS_ERROR)
5202     {
5203       stmt = gimple_build_asm_vec (TREE_STRING_POINTER (ASM_STRING (expr)),
5204                                    inputs, outputs, clobbers, labels);
5205
5206       gimple_asm_set_volatile (stmt, ASM_VOLATILE_P (expr));
5207       gimple_asm_set_input (stmt, ASM_INPUT_P (expr));
5208
5209       gimplify_seq_add_stmt (pre_p, stmt);
5210     }
5211
5212   return ret;
5213 }
5214
5215 /* Gimplify a CLEANUP_POINT_EXPR.  Currently this works by adding
5216    GIMPLE_WITH_CLEANUP_EXPRs to the prequeue as we encounter cleanups while
5217    gimplifying the body, and converting them to TRY_FINALLY_EXPRs when we
5218    return to this function.
5219
5220    FIXME should we complexify the prequeue handling instead?  Or use flags
5221    for all the cleanups and let the optimizer tighten them up?  The current
5222    code seems pretty fragile; it will break on a cleanup within any
5223    non-conditional nesting.  But any such nesting would be broken, anyway;
5224    we can't write a TRY_FINALLY_EXPR that starts inside a nesting construct
5225    and continues out of it.  We can do that at the RTL level, though, so
5226    having an optimizer to tighten up try/finally regions would be a Good
5227    Thing.  */
5228
5229 static enum gimplify_status
5230 gimplify_cleanup_point_expr (tree *expr_p, gimple_seq *pre_p)
5231 {
5232   gimple_stmt_iterator iter;
5233   gimple_seq body_sequence = NULL;
5234
5235   tree temp = voidify_wrapper_expr (*expr_p, NULL);
5236
5237   /* We only care about the number of conditions between the innermost
5238      CLEANUP_POINT_EXPR and the cleanup.  So save and reset the count and
5239      any cleanups collected outside the CLEANUP_POINT_EXPR.  */
5240   int old_conds = gimplify_ctxp->conditions;
5241   gimple_seq old_cleanups = gimplify_ctxp->conditional_cleanups;
5242   bool old_in_cleanup_point_expr = gimplify_ctxp->in_cleanup_point_expr;
5243   gimplify_ctxp->conditions = 0;
5244   gimplify_ctxp->conditional_cleanups = NULL;
5245   gimplify_ctxp->in_cleanup_point_expr = true;
5246
5247   gimplify_stmt (&TREE_OPERAND (*expr_p, 0), &body_sequence);
5248
5249   gimplify_ctxp->conditions = old_conds;
5250   gimplify_ctxp->conditional_cleanups = old_cleanups;
5251   gimplify_ctxp->in_cleanup_point_expr = old_in_cleanup_point_expr;
5252
5253   for (iter = gsi_start (body_sequence); !gsi_end_p (iter); )
5254     {
5255       gimple wce = gsi_stmt (iter);
5256
5257       if (gimple_code (wce) == GIMPLE_WITH_CLEANUP_EXPR)
5258         {
5259           if (gsi_one_before_end_p (iter))
5260             {
5261               /* Note that gsi_insert_seq_before and gsi_remove do not
5262                  scan operands, unlike some other sequence mutators.  */
5263               if (!gimple_wce_cleanup_eh_only (wce))
5264                 gsi_insert_seq_before_without_update (&iter,
5265                                                       gimple_wce_cleanup (wce),
5266                                                       GSI_SAME_STMT);
5267               gsi_remove (&iter, true);
5268               break;
5269             }
5270           else
5271             {
5272               gimple gtry;
5273               gimple_seq seq;
5274               enum gimple_try_flags kind;
5275
5276               if (gimple_wce_cleanup_eh_only (wce))
5277                 kind = GIMPLE_TRY_CATCH;
5278               else
5279                 kind = GIMPLE_TRY_FINALLY;
5280               seq = gsi_split_seq_after (iter);
5281
5282               gtry = gimple_build_try (seq, gimple_wce_cleanup (wce), kind);
5283               /* Do not use gsi_replace here, as it may scan operands.
5284                  We want to do a simple structural modification only.  */
5285               *gsi_stmt_ptr (&iter) = gtry;
5286               iter = gsi_start (seq);
5287             }
5288         }
5289       else
5290         gsi_next (&iter);
5291     }
5292
5293   gimplify_seq_add_seq (pre_p, body_sequence);
5294   if (temp)
5295     {
5296       *expr_p = temp;
5297       return GS_OK;
5298     }
5299   else
5300     {
5301       *expr_p = NULL;
5302       return GS_ALL_DONE;
5303     }
5304 }
5305
5306 /* Insert a cleanup marker for gimplify_cleanup_point_expr.  CLEANUP
5307    is the cleanup action required.  EH_ONLY is true if the cleanup should
5308    only be executed if an exception is thrown, not on normal exit.  */
5309
5310 static void
5311 gimple_push_cleanup (tree var, tree cleanup, bool eh_only, gimple_seq *pre_p)
5312 {
5313   gimple wce;
5314   gimple_seq cleanup_stmts = NULL;
5315
5316   /* Errors can result in improperly nested cleanups.  Which results in
5317      confusion when trying to resolve the GIMPLE_WITH_CLEANUP_EXPR.  */
5318   if (seen_error ())
5319     return;
5320
5321   if (gimple_conditional_context ())
5322     {
5323       /* If we're in a conditional context, this is more complex.  We only
5324          want to run the cleanup if we actually ran the initialization that
5325          necessitates it, but we want to run it after the end of the
5326          conditional context.  So we wrap the try/finally around the
5327          condition and use a flag to determine whether or not to actually
5328          run the destructor.  Thus
5329
5330            test ? f(A()) : 0
5331
5332          becomes (approximately)
5333
5334            flag = 0;
5335            try {
5336              if (test) { A::A(temp); flag = 1; val = f(temp); }
5337              else { val = 0; }
5338            } finally {
5339              if (flag) A::~A(temp);
5340            }
5341            val
5342       */
5343       tree flag = create_tmp_var (boolean_type_node, "cleanup");
5344       gimple ffalse = gimple_build_assign (flag, boolean_false_node);
5345       gimple ftrue = gimple_build_assign (flag, boolean_true_node);
5346
5347       cleanup = build3 (COND_EXPR, void_type_node, flag, cleanup, NULL);
5348       gimplify_stmt (&cleanup, &cleanup_stmts);
5349       wce = gimple_build_wce (cleanup_stmts);
5350
5351       gimplify_seq_add_stmt (&gimplify_ctxp->conditional_cleanups, ffalse);
5352       gimplify_seq_add_stmt (&gimplify_ctxp->conditional_cleanups, wce);
5353       gimplify_seq_add_stmt (pre_p, ftrue);
5354
5355       /* Because of this manipulation, and the EH edges that jump
5356          threading cannot redirect, the temporary (VAR) will appear
5357          to be used uninitialized.  Don't warn.  */
5358       TREE_NO_WARNING (var) = 1;
5359     }
5360   else
5361     {
5362       gimplify_stmt (&cleanup, &cleanup_stmts);
5363       wce = gimple_build_wce (cleanup_stmts);
5364       gimple_wce_set_cleanup_eh_only (wce, eh_only);
5365       gimplify_seq_add_stmt (pre_p, wce);
5366     }
5367 }
5368
5369 /* Gimplify a TARGET_EXPR which doesn't appear on the rhs of an INIT_EXPR.  */
5370
5371 static enum gimplify_status
5372 gimplify_target_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
5373 {
5374   tree targ = *expr_p;
5375   tree temp = TARGET_EXPR_SLOT (targ);
5376   tree init = TARGET_EXPR_INITIAL (targ);
5377   enum gimplify_status ret;
5378
5379   if (init)
5380     {
5381       tree cleanup = NULL_TREE;
5382
5383       /* TARGET_EXPR temps aren't part of the enclosing block, so add it
5384          to the temps list.  Handle also variable length TARGET_EXPRs.  */
5385       if (TREE_CODE (DECL_SIZE (temp)) != INTEGER_CST)
5386         {
5387           if (!TYPE_SIZES_GIMPLIFIED (TREE_TYPE (temp)))
5388             gimplify_type_sizes (TREE_TYPE (temp), pre_p);
5389           gimplify_vla_decl (temp, pre_p);
5390         }
5391       else
5392         gimple_add_tmp_var (temp);
5393
5394       /* If TARGET_EXPR_INITIAL is void, then the mere evaluation of the
5395          expression is supposed to initialize the slot.  */
5396       if (VOID_TYPE_P (TREE_TYPE (init)))
5397         ret = gimplify_expr (&init, pre_p, post_p, is_gimple_stmt, fb_none);
5398       else
5399         {
5400           tree init_expr = build2 (INIT_EXPR, void_type_node, temp, init);
5401           init = init_expr;
5402           ret = gimplify_expr (&init, pre_p, post_p, is_gimple_stmt, fb_none);
5403           init = NULL;
5404           ggc_free (init_expr);
5405         }
5406       if (ret == GS_ERROR)
5407         {
5408           /* PR c++/28266 Make sure this is expanded only once. */
5409           TARGET_EXPR_INITIAL (targ) = NULL_TREE;
5410           return GS_ERROR;
5411         }
5412       if (init)
5413         gimplify_and_add (init, pre_p);
5414
5415       /* If needed, push the cleanup for the temp.  */
5416       if (TARGET_EXPR_CLEANUP (targ))
5417         {
5418           if (CLEANUP_EH_ONLY (targ))
5419             gimple_push_cleanup (temp, TARGET_EXPR_CLEANUP (targ),
5420                                  CLEANUP_EH_ONLY (targ), pre_p);
5421           else
5422             cleanup = TARGET_EXPR_CLEANUP (targ);
5423         }
5424
5425       /* Add a clobber for the temporary going out of scope, like
5426          gimplify_bind_expr.  */
5427       if (gimplify_ctxp->in_cleanup_point_expr
5428           && needs_to_live_in_memory (temp))
5429         {
5430           tree clobber = build_constructor (TREE_TYPE (temp), NULL);
5431           TREE_THIS_VOLATILE (clobber) = true;
5432           clobber = build2 (MODIFY_EXPR, TREE_TYPE (temp), temp, clobber);
5433           if (cleanup)
5434             cleanup = build2 (COMPOUND_EXPR, void_type_node, cleanup,
5435                               clobber);
5436           else
5437             cleanup = clobber;
5438         }
5439
5440       if (cleanup)
5441         gimple_push_cleanup (temp, cleanup, false, pre_p);
5442
5443       /* Only expand this once.  */
5444       TREE_OPERAND (targ, 3) = init;
5445       TARGET_EXPR_INITIAL (targ) = NULL_TREE;
5446     }
5447   else
5448     /* We should have expanded this before.  */
5449     gcc_assert (DECL_SEEN_IN_BIND_EXPR_P (temp));
5450
5451   *expr_p = temp;
5452   return GS_OK;
5453 }
5454
5455 /* Gimplification of expression trees.  */
5456
5457 /* Gimplify an expression which appears at statement context.  The
5458    corresponding GIMPLE statements are added to *SEQ_P.  If *SEQ_P is
5459    NULL, a new sequence is allocated.
5460
5461    Return true if we actually added a statement to the queue.  */
5462
5463 bool
5464 gimplify_stmt (tree *stmt_p, gimple_seq *seq_p)
5465 {
5466   gimple_seq_node last;
5467
5468   if (!*seq_p)
5469     *seq_p = gimple_seq_alloc ();
5470
5471   last = gimple_seq_last (*seq_p);
5472   gimplify_expr (stmt_p, seq_p, NULL, is_gimple_stmt, fb_none);
5473   return last != gimple_seq_last (*seq_p);
5474 }
5475
5476 /* Add FIRSTPRIVATE entries for DECL in the OpenMP the surrounding parallels
5477    to CTX.  If entries already exist, force them to be some flavor of private.
5478    If there is no enclosing parallel, do nothing.  */
5479
5480 void
5481 omp_firstprivatize_variable (struct gimplify_omp_ctx *ctx, tree decl)
5482 {
5483   splay_tree_node n;
5484
5485   if (decl == NULL || !DECL_P (decl))
5486     return;
5487
5488   do
5489     {
5490       n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
5491       if (n != NULL)
5492         {
5493           if (n->value & GOVD_SHARED)
5494             n->value = GOVD_FIRSTPRIVATE | (n->value & GOVD_SEEN);
5495           else
5496             return;
5497         }
5498       else if (ctx->region_type != ORT_WORKSHARE)
5499         omp_add_variable (ctx, decl, GOVD_FIRSTPRIVATE);
5500
5501       ctx = ctx->outer_context;
5502     }
5503   while (ctx);
5504 }
5505
5506 /* Similarly for each of the type sizes of TYPE.  */
5507
5508 static void
5509 omp_firstprivatize_type_sizes (struct gimplify_omp_ctx *ctx, tree type)
5510 {
5511   if (type == NULL || type == error_mark_node)
5512     return;
5513   type = TYPE_MAIN_VARIANT (type);
5514
5515   if (pointer_set_insert (ctx->privatized_types, type))
5516     return;
5517
5518   switch (TREE_CODE (type))
5519     {
5520     case INTEGER_TYPE:
5521     case ENUMERAL_TYPE:
5522     case BOOLEAN_TYPE:
5523     case REAL_TYPE:
5524     case FIXED_POINT_TYPE:
5525       omp_firstprivatize_variable (ctx, TYPE_MIN_VALUE (type));
5526       omp_firstprivatize_variable (ctx, TYPE_MAX_VALUE (type));
5527       break;
5528
5529     case ARRAY_TYPE:
5530       omp_firstprivatize_type_sizes (ctx, TREE_TYPE (type));
5531       omp_firstprivatize_type_sizes (ctx, TYPE_DOMAIN (type));
5532       break;
5533
5534     case RECORD_TYPE:
5535     case UNION_TYPE:
5536     case QUAL_UNION_TYPE:
5537       {
5538         tree field;
5539         for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5540           if (TREE_CODE (field) == FIELD_DECL)
5541             {
5542               omp_firstprivatize_variable (ctx, DECL_FIELD_OFFSET (field));
5543               omp_firstprivatize_type_sizes (ctx, TREE_TYPE (field));
5544             }
5545       }
5546       break;
5547
5548     case POINTER_TYPE:
5549     case REFERENCE_TYPE:
5550       omp_firstprivatize_type_sizes (ctx, TREE_TYPE (type));
5551       break;
5552
5553     default:
5554       break;
5555     }
5556
5557   omp_firstprivatize_variable (ctx, TYPE_SIZE (type));
5558   omp_firstprivatize_variable (ctx, TYPE_SIZE_UNIT (type));
5559   lang_hooks.types.omp_firstprivatize_type_sizes (ctx, type);
5560 }
5561
5562 /* Add an entry for DECL in the OpenMP context CTX with FLAGS.  */
5563
5564 static void
5565 omp_add_variable (struct gimplify_omp_ctx *ctx, tree decl, unsigned int flags)
5566 {
5567   splay_tree_node n;
5568   unsigned int nflags;
5569   tree t;
5570
5571   if (error_operand_p (decl))
5572     return;
5573
5574   /* Never elide decls whose type has TREE_ADDRESSABLE set.  This means
5575      there are constructors involved somewhere.  */
5576   if (TREE_ADDRESSABLE (TREE_TYPE (decl))
5577       || TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl)))
5578     flags |= GOVD_SEEN;
5579
5580   n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
5581   if (n != NULL)
5582     {
5583       /* We shouldn't be re-adding the decl with the same data
5584          sharing class.  */
5585       gcc_assert ((n->value & GOVD_DATA_SHARE_CLASS & flags) == 0);
5586       /* The only combination of data sharing classes we should see is
5587          FIRSTPRIVATE and LASTPRIVATE.  */
5588       nflags = n->value | flags;
5589       gcc_assert ((nflags & GOVD_DATA_SHARE_CLASS)
5590                   == (GOVD_FIRSTPRIVATE | GOVD_LASTPRIVATE));
5591       n->value = nflags;
5592       return;
5593     }
5594
5595   /* When adding a variable-sized variable, we have to handle all sorts
5596      of additional bits of data: the pointer replacement variable, and
5597      the parameters of the type.  */
5598   if (DECL_SIZE (decl) && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
5599     {
5600       /* Add the pointer replacement variable as PRIVATE if the variable
5601          replacement is private, else FIRSTPRIVATE since we'll need the
5602          address of the original variable either for SHARED, or for the
5603          copy into or out of the context.  */
5604       if (!(flags & GOVD_LOCAL))
5605         {
5606           nflags = flags & GOVD_PRIVATE ? GOVD_PRIVATE : GOVD_FIRSTPRIVATE;
5607           nflags |= flags & GOVD_SEEN;
5608           t = DECL_VALUE_EXPR (decl);
5609           gcc_assert (TREE_CODE (t) == INDIRECT_REF);
5610           t = TREE_OPERAND (t, 0);
5611           gcc_assert (DECL_P (t));
5612           omp_add_variable (ctx, t, nflags);
5613         }
5614
5615       /* Add all of the variable and type parameters (which should have
5616          been gimplified to a formal temporary) as FIRSTPRIVATE.  */
5617       omp_firstprivatize_variable (ctx, DECL_SIZE_UNIT (decl));
5618       omp_firstprivatize_variable (ctx, DECL_SIZE (decl));
5619       omp_firstprivatize_type_sizes (ctx, TREE_TYPE (decl));
5620
5621       /* The variable-sized variable itself is never SHARED, only some form
5622          of PRIVATE.  The sharing would take place via the pointer variable
5623          which we remapped above.  */
5624       if (flags & GOVD_SHARED)
5625         flags = GOVD_PRIVATE | GOVD_DEBUG_PRIVATE
5626                 | (flags & (GOVD_SEEN | GOVD_EXPLICIT));
5627
5628       /* We're going to make use of the TYPE_SIZE_UNIT at least in the
5629          alloca statement we generate for the variable, so make sure it
5630          is available.  This isn't automatically needed for the SHARED
5631          case, since we won't be allocating local storage then.
5632          For local variables TYPE_SIZE_UNIT might not be gimplified yet,
5633          in this case omp_notice_variable will be called later
5634          on when it is gimplified.  */
5635       else if (! (flags & GOVD_LOCAL)
5636                && DECL_P (TYPE_SIZE_UNIT (TREE_TYPE (decl))))
5637         omp_notice_variable (ctx, TYPE_SIZE_UNIT (TREE_TYPE (decl)), true);
5638     }
5639   else if (lang_hooks.decls.omp_privatize_by_reference (decl))
5640     {
5641       gcc_assert ((flags & GOVD_LOCAL) == 0);
5642       omp_firstprivatize_type_sizes (ctx, TREE_TYPE (decl));
5643
5644       /* Similar to the direct variable sized case above, we'll need the
5645          size of references being privatized.  */
5646       if ((flags & GOVD_SHARED) == 0)
5647         {
5648           t = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (decl)));
5649           if (TREE_CODE (t) != INTEGER_CST)
5650             omp_notice_variable (ctx, t, true);
5651         }
5652     }
5653
5654   splay_tree_insert (ctx->variables, (splay_tree_key)decl, flags);
5655 }
5656
5657 /* Notice a threadprivate variable DECL used in OpenMP context CTX.
5658    This just prints out diagnostics about threadprivate variable uses
5659    in untied tasks.  If DECL2 is non-NULL, prevent this warning
5660    on that variable.  */
5661
5662 static bool
5663 omp_notice_threadprivate_variable (struct gimplify_omp_ctx *ctx, tree decl,
5664                                    tree decl2)
5665 {
5666   splay_tree_node n;
5667
5668   if (ctx->region_type != ORT_UNTIED_TASK)
5669     return false;
5670   n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
5671   if (n == NULL)
5672     {
5673       error ("threadprivate variable %qE used in untied task",
5674              DECL_NAME (decl));
5675       error_at (ctx->location, "enclosing task");
5676       splay_tree_insert (ctx->variables, (splay_tree_key)decl, 0);
5677     }
5678   if (decl2)
5679     splay_tree_insert (ctx->variables, (splay_tree_key)decl2, 0);
5680   return false;
5681 }
5682
5683 /* Record the fact that DECL was used within the OpenMP context CTX.
5684    IN_CODE is true when real code uses DECL, and false when we should
5685    merely emit default(none) errors.  Return true if DECL is going to
5686    be remapped and thus DECL shouldn't be gimplified into its
5687    DECL_VALUE_EXPR (if any).  */
5688
5689 static bool
5690 omp_notice_variable (struct gimplify_omp_ctx *ctx, tree decl, bool in_code)
5691 {
5692   splay_tree_node n;
5693   unsigned flags = in_code ? GOVD_SEEN : 0;
5694   bool ret = false, shared;
5695
5696   if (error_operand_p (decl))
5697     return false;
5698
5699   /* Threadprivate variables are predetermined.  */
5700   if (is_global_var (decl))
5701     {
5702       if (DECL_THREAD_LOCAL_P (decl))
5703         return omp_notice_threadprivate_variable (ctx, decl, NULL_TREE);
5704
5705       if (DECL_HAS_VALUE_EXPR_P (decl))
5706         {
5707           tree value = get_base_address (DECL_VALUE_EXPR (decl));
5708
5709           if (value && DECL_P (value) && DECL_THREAD_LOCAL_P (value))
5710             return omp_notice_threadprivate_variable (ctx, decl, value);
5711         }
5712     }
5713
5714   n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
5715   if (n == NULL)
5716     {
5717       enum omp_clause_default_kind default_kind, kind;
5718       struct gimplify_omp_ctx *octx;
5719
5720       if (ctx->region_type == ORT_WORKSHARE)
5721         goto do_outer;
5722
5723       /* ??? Some compiler-generated variables (like SAVE_EXPRs) could be
5724          remapped firstprivate instead of shared.  To some extent this is
5725          addressed in omp_firstprivatize_type_sizes, but not effectively.  */
5726       default_kind = ctx->default_kind;
5727       kind = lang_hooks.decls.omp_predetermined_sharing (decl);
5728       if (kind != OMP_CLAUSE_DEFAULT_UNSPECIFIED)
5729         default_kind = kind;
5730
5731       switch (default_kind)
5732         {
5733         case OMP_CLAUSE_DEFAULT_NONE:
5734           error ("%qE not specified in enclosing parallel",
5735                  DECL_NAME (lang_hooks.decls.omp_report_decl (decl)));
5736           if ((ctx->region_type & ORT_TASK) != 0)
5737             error_at (ctx->location, "enclosing task");
5738           else
5739             error_at (ctx->location, "enclosing parallel");
5740           /* FALLTHRU */
5741         case OMP_CLAUSE_DEFAULT_SHARED:
5742           flags |= GOVD_SHARED;
5743           break;
5744         case OMP_CLAUSE_DEFAULT_PRIVATE:
5745           flags |= GOVD_PRIVATE;
5746           break;
5747         case OMP_CLAUSE_DEFAULT_FIRSTPRIVATE:
5748           flags |= GOVD_FIRSTPRIVATE;
5749           break;
5750         case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
5751           /* decl will be either GOVD_FIRSTPRIVATE or GOVD_SHARED.  */
5752           gcc_assert ((ctx->region_type & ORT_TASK) != 0);
5753           if (ctx->outer_context)
5754             omp_notice_variable (ctx->outer_context, decl, in_code);
5755           for (octx = ctx->outer_context; octx; octx = octx->outer_context)
5756             {
5757               splay_tree_node n2;
5758
5759               n2 = splay_tree_lookup (octx->variables, (splay_tree_key) decl);
5760               if (n2 && (n2->value & GOVD_DATA_SHARE_CLASS) != GOVD_SHARED)
5761                 {
5762                   flags |= GOVD_FIRSTPRIVATE;
5763                   break;
5764                 }
5765               if ((octx->region_type & ORT_PARALLEL) != 0)
5766                 break;
5767             }
5768           if (flags & GOVD_FIRSTPRIVATE)
5769             break;
5770           if (octx == NULL
5771               && (TREE_CODE (decl) == PARM_DECL
5772                   || (!is_global_var (decl)
5773                       && DECL_CONTEXT (decl) == current_function_decl)))
5774             {
5775               flags |= GOVD_FIRSTPRIVATE;
5776               break;
5777             }
5778           flags |= GOVD_SHARED;
5779           break;
5780         default:
5781           gcc_unreachable ();
5782         }
5783
5784       if ((flags & GOVD_PRIVATE)
5785           && lang_hooks.decls.omp_private_outer_ref (decl))
5786         flags |= GOVD_PRIVATE_OUTER_REF;
5787
5788       omp_add_variable (ctx, decl, flags);
5789
5790       shared = (flags & GOVD_SHARED) != 0;
5791       ret = lang_hooks.decls.omp_disregard_value_expr (decl, shared);
5792       goto do_outer;
5793     }
5794
5795   if ((n->value & (GOVD_SEEN | GOVD_LOCAL)) == 0
5796       && (flags & (GOVD_SEEN | GOVD_LOCAL)) == GOVD_SEEN
5797       && DECL_SIZE (decl)
5798       && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
5799     {
5800       splay_tree_node n2;
5801       tree t = DECL_VALUE_EXPR (decl);
5802       gcc_assert (TREE_CODE (t) == INDIRECT_REF);
5803       t = TREE_OPERAND (t, 0);
5804       gcc_assert (DECL_P (t));
5805       n2 = splay_tree_lookup (ctx->variables, (splay_tree_key) t);
5806       n2->value |= GOVD_SEEN;
5807     }
5808
5809   shared = ((flags | n->value) & GOVD_SHARED) != 0;
5810   ret = lang_hooks.decls.omp_disregard_value_expr (decl, shared);
5811
5812   /* If nothing changed, there's nothing left to do.  */
5813   if ((n->value & flags) == flags)
5814     return ret;
5815   flags |= n->value;
5816   n->value = flags;
5817
5818  do_outer:
5819   /* If the variable is private in the current context, then we don't
5820      need to propagate anything to an outer context.  */
5821   if ((flags & GOVD_PRIVATE) && !(flags & GOVD_PRIVATE_OUTER_REF))
5822     return ret;
5823   if (ctx->outer_context
5824       && omp_notice_variable (ctx->outer_context, decl, in_code))
5825     return true;
5826   return ret;
5827 }
5828
5829 /* Verify that DECL is private within CTX.  If there's specific information
5830    to the contrary in the innermost scope, generate an error.  */
5831
5832 static bool
5833 omp_is_private (struct gimplify_omp_ctx *ctx, tree decl)
5834 {
5835   splay_tree_node n;
5836
5837   n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
5838   if (n != NULL)
5839     {
5840       if (n->value & GOVD_SHARED)
5841         {
5842           if (ctx == gimplify_omp_ctxp)
5843             {
5844               error ("iteration variable %qE should be private",
5845                      DECL_NAME (decl));
5846               n->value = GOVD_PRIVATE;
5847               return true;
5848             }
5849           else
5850             return false;
5851         }
5852       else if ((n->value & GOVD_EXPLICIT) != 0
5853                && (ctx == gimplify_omp_ctxp
5854                    || (ctx->region_type == ORT_COMBINED_PARALLEL
5855                        && gimplify_omp_ctxp->outer_context == ctx)))
5856         {
5857           if ((n->value & GOVD_FIRSTPRIVATE) != 0)
5858             error ("iteration variable %qE should not be firstprivate",
5859                    DECL_NAME (decl));
5860           else if ((n->value & GOVD_REDUCTION) != 0)
5861             error ("iteration variable %qE should not be reduction",
5862                    DECL_NAME (decl));
5863         }
5864       return (ctx == gimplify_omp_ctxp
5865               || (ctx->region_type == ORT_COMBINED_PARALLEL
5866                   && gimplify_omp_ctxp->outer_context == ctx));
5867     }
5868
5869   if (ctx->region_type != ORT_WORKSHARE)
5870     return false;
5871   else if (ctx->outer_context)
5872     return omp_is_private (ctx->outer_context, decl);
5873   return false;
5874 }
5875
5876 /* Return true if DECL is private within a parallel region
5877    that binds to the current construct's context or in parallel
5878    region's REDUCTION clause.  */
5879
5880 static bool
5881 omp_check_private (struct gimplify_omp_ctx *ctx, tree decl)
5882 {
5883   splay_tree_node n;
5884
5885   do
5886     {
5887       ctx = ctx->outer_context;
5888       if (ctx == NULL)
5889         return !(is_global_var (decl)
5890                  /* References might be private, but might be shared too.  */
5891                  || lang_hooks.decls.omp_privatize_by_reference (decl));
5892
5893       n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
5894       if (n != NULL)
5895         return (n->value & GOVD_SHARED) == 0;
5896     }
5897   while (ctx->region_type == ORT_WORKSHARE);
5898   return false;
5899 }
5900
5901 /* Scan the OpenMP clauses in *LIST_P, installing mappings into a new
5902    and previous omp contexts.  */
5903
5904 static void
5905 gimplify_scan_omp_clauses (tree *list_p, gimple_seq *pre_p,
5906                            enum omp_region_type region_type)
5907 {
5908   struct gimplify_omp_ctx *ctx, *outer_ctx;
5909   struct gimplify_ctx gctx;
5910   tree c;
5911
5912   ctx = new_omp_context (region_type);
5913   outer_ctx = ctx->outer_context;
5914
5915   while ((c = *list_p) != NULL)
5916     {
5917       bool remove = false;
5918       bool notice_outer = true;
5919       const char *check_non_private = NULL;
5920       unsigned int flags;
5921       tree decl;
5922
5923       switch (OMP_CLAUSE_CODE (c))
5924         {
5925         case OMP_CLAUSE_PRIVATE:
5926           flags = GOVD_PRIVATE | GOVD_EXPLICIT;
5927           if (lang_hooks.decls.omp_private_outer_ref (OMP_CLAUSE_DECL (c)))
5928             {
5929               flags |= GOVD_PRIVATE_OUTER_REF;
5930               OMP_CLAUSE_PRIVATE_OUTER_REF (c) = 1;
5931             }
5932           else
5933             notice_outer = false;
5934           goto do_add;
5935         case OMP_CLAUSE_SHARED:
5936           flags = GOVD_SHARED | GOVD_EXPLICIT;
5937           goto do_add;
5938         case OMP_CLAUSE_FIRSTPRIVATE:
5939           flags = GOVD_FIRSTPRIVATE | GOVD_EXPLICIT;
5940           check_non_private = "firstprivate";
5941           goto do_add;
5942         case OMP_CLAUSE_LASTPRIVATE:
5943           flags = GOVD_LASTPRIVATE | GOVD_SEEN | GOVD_EXPLICIT;
5944           check_non_private = "lastprivate";
5945           goto do_add;
5946         case OMP_CLAUSE_REDUCTION:
5947           flags = GOVD_REDUCTION | GOVD_SEEN | GOVD_EXPLICIT;
5948           check_non_private = "reduction";
5949           goto do_add;
5950
5951         do_add:
5952           decl = OMP_CLAUSE_DECL (c);
5953           if (error_operand_p (decl))
5954             {
5955               remove = true;
5956               break;
5957             }
5958           omp_add_variable (ctx, decl, flags);
5959           if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
5960               && OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
5961             {
5962               omp_add_variable (ctx, OMP_CLAUSE_REDUCTION_PLACEHOLDER (c),
5963                                 GOVD_LOCAL | GOVD_SEEN);
5964               gimplify_omp_ctxp = ctx;
5965               push_gimplify_context (&gctx);
5966
5967               OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c) = gimple_seq_alloc ();
5968               OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c) = gimple_seq_alloc ();
5969
5970               gimplify_and_add (OMP_CLAUSE_REDUCTION_INIT (c),
5971                                 &OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c));
5972               pop_gimplify_context
5973                 (gimple_seq_first_stmt (OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c)));
5974               push_gimplify_context (&gctx);
5975               gimplify_and_add (OMP_CLAUSE_REDUCTION_MERGE (c),
5976                                 &OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c));
5977               pop_gimplify_context
5978                 (gimple_seq_first_stmt (OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c)));
5979               OMP_CLAUSE_REDUCTION_INIT (c) = NULL_TREE;
5980               OMP_CLAUSE_REDUCTION_MERGE (c) = NULL_TREE;
5981
5982               gimplify_omp_ctxp = outer_ctx;
5983             }
5984           else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
5985                    && OMP_CLAUSE_LASTPRIVATE_STMT (c))
5986             {
5987               gimplify_omp_ctxp = ctx;
5988               push_gimplify_context (&gctx);
5989               if (TREE_CODE (OMP_CLAUSE_LASTPRIVATE_STMT (c)) != BIND_EXPR)
5990                 {
5991                   tree bind = build3 (BIND_EXPR, void_type_node, NULL,
5992                                       NULL, NULL);
5993                   TREE_SIDE_EFFECTS (bind) = 1;
5994                   BIND_EXPR_BODY (bind) = OMP_CLAUSE_LASTPRIVATE_STMT (c);
5995                   OMP_CLAUSE_LASTPRIVATE_STMT (c) = bind;
5996                 }
5997               gimplify_and_add (OMP_CLAUSE_LASTPRIVATE_STMT (c),
5998                                 &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c));
5999               pop_gimplify_context
6000                 (gimple_seq_first_stmt (OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c)));
6001               OMP_CLAUSE_LASTPRIVATE_STMT (c) = NULL_TREE;
6002
6003               gimplify_omp_ctxp = outer_ctx;
6004             }
6005           if (notice_outer)
6006             goto do_notice;
6007           break;
6008
6009         case OMP_CLAUSE_COPYIN:
6010         case OMP_CLAUSE_COPYPRIVATE:
6011           decl = OMP_CLAUSE_DECL (c);
6012           if (error_operand_p (decl))
6013             {
6014               remove = true;
6015               break;
6016             }
6017         do_notice:
6018           if (outer_ctx)
6019             omp_notice_variable (outer_ctx, decl, true);
6020           if (check_non_private
6021               && region_type == ORT_WORKSHARE
6022               && omp_check_private (ctx, decl))
6023             {
6024               error ("%s variable %qE is private in outer context",
6025                      check_non_private, DECL_NAME (decl));
6026               remove = true;
6027             }
6028           break;
6029
6030         case OMP_CLAUSE_FINAL:
6031         case OMP_CLAUSE_IF:
6032           OMP_CLAUSE_OPERAND (c, 0)
6033             = gimple_boolify (OMP_CLAUSE_OPERAND (c, 0));
6034           /* Fall through.  */
6035
6036         case OMP_CLAUSE_SCHEDULE:
6037         case OMP_CLAUSE_NUM_THREADS:
6038           if (gimplify_expr (&OMP_CLAUSE_OPERAND (c, 0), pre_p, NULL,
6039                              is_gimple_val, fb_rvalue) == GS_ERROR)
6040               remove = true;
6041           break;
6042
6043         case OMP_CLAUSE_NOWAIT:
6044         case OMP_CLAUSE_ORDERED:
6045         case OMP_CLAUSE_UNTIED:
6046         case OMP_CLAUSE_COLLAPSE:
6047         case OMP_CLAUSE_MERGEABLE:
6048           break;
6049
6050         case OMP_CLAUSE_DEFAULT:
6051           ctx->default_kind = OMP_CLAUSE_DEFAULT_KIND (c);
6052           break;
6053
6054         default:
6055           gcc_unreachable ();
6056         }
6057
6058       if (remove)
6059         *list_p = OMP_CLAUSE_CHAIN (c);
6060       else
6061         list_p = &OMP_CLAUSE_CHAIN (c);
6062     }
6063
6064   gimplify_omp_ctxp = ctx;
6065 }
6066
6067 /* For all variables that were not actually used within the context,
6068    remove PRIVATE, SHARED, and FIRSTPRIVATE clauses.  */
6069
6070 static int
6071 gimplify_adjust_omp_clauses_1 (splay_tree_node n, void *data)
6072 {
6073   tree *list_p = (tree *) data;
6074   tree decl = (tree) n->key;
6075   unsigned flags = n->value;
6076   enum omp_clause_code code;
6077   tree clause;
6078   bool private_debug;
6079
6080   if (flags & (GOVD_EXPLICIT | GOVD_LOCAL))
6081     return 0;
6082   if ((flags & GOVD_SEEN) == 0)
6083     return 0;
6084   if (flags & GOVD_DEBUG_PRIVATE)
6085     {
6086       gcc_assert ((flags & GOVD_DATA_SHARE_CLASS) == GOVD_PRIVATE);
6087       private_debug = true;
6088     }
6089   else
6090     private_debug
6091       = lang_hooks.decls.omp_private_debug_clause (decl,
6092                                                    !!(flags & GOVD_SHARED));
6093   if (private_debug)
6094     code = OMP_CLAUSE_PRIVATE;
6095   else if (flags & GOVD_SHARED)
6096     {
6097       if (is_global_var (decl))
6098         {
6099           struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp->outer_context;
6100           while (ctx != NULL)
6101             {
6102               splay_tree_node on
6103                 = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6104               if (on && (on->value & (GOVD_FIRSTPRIVATE | GOVD_LASTPRIVATE
6105                                       | GOVD_PRIVATE | GOVD_REDUCTION)) != 0)
6106                 break;
6107               ctx = ctx->outer_context;
6108             }
6109           if (ctx == NULL)
6110             return 0;
6111         }
6112       code = OMP_CLAUSE_SHARED;
6113     }
6114   else if (flags & GOVD_PRIVATE)
6115     code = OMP_CLAUSE_PRIVATE;
6116   else if (flags & GOVD_FIRSTPRIVATE)
6117     code = OMP_CLAUSE_FIRSTPRIVATE;
6118   else
6119     gcc_unreachable ();
6120
6121   clause = build_omp_clause (input_location, code);
6122   OMP_CLAUSE_DECL (clause) = decl;
6123   OMP_CLAUSE_CHAIN (clause) = *list_p;
6124   if (private_debug)
6125     OMP_CLAUSE_PRIVATE_DEBUG (clause) = 1;
6126   else if (code == OMP_CLAUSE_PRIVATE && (flags & GOVD_PRIVATE_OUTER_REF))
6127     OMP_CLAUSE_PRIVATE_OUTER_REF (clause) = 1;
6128   *list_p = clause;
6129   lang_hooks.decls.omp_finish_clause (clause);
6130
6131   return 0;
6132 }
6133
6134 static void
6135 gimplify_adjust_omp_clauses (tree *list_p)
6136 {
6137   struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
6138   tree c, decl;
6139
6140   while ((c = *list_p) != NULL)
6141     {
6142       splay_tree_node n;
6143       bool remove = false;
6144
6145       switch (OMP_CLAUSE_CODE (c))
6146         {
6147         case OMP_CLAUSE_PRIVATE:
6148         case OMP_CLAUSE_SHARED:
6149         case OMP_CLAUSE_FIRSTPRIVATE:
6150           decl = OMP_CLAUSE_DECL (c);
6151           n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6152           remove = !(n->value & GOVD_SEEN);
6153           if (! remove)
6154             {
6155               bool shared = OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED;
6156               if ((n->value & GOVD_DEBUG_PRIVATE)
6157                   || lang_hooks.decls.omp_private_debug_clause (decl, shared))
6158                 {
6159                   gcc_assert ((n->value & GOVD_DEBUG_PRIVATE) == 0
6160                               || ((n->value & GOVD_DATA_SHARE_CLASS)
6161                                   == GOVD_PRIVATE));
6162                   OMP_CLAUSE_SET_CODE (c, OMP_CLAUSE_PRIVATE);
6163                   OMP_CLAUSE_PRIVATE_DEBUG (c) = 1;
6164                 }
6165             }
6166           break;
6167
6168         case OMP_CLAUSE_LASTPRIVATE:
6169           /* Make sure OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE is set to
6170              accurately reflect the presence of a FIRSTPRIVATE clause.  */
6171           decl = OMP_CLAUSE_DECL (c);
6172           n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6173           OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c)
6174             = (n->value & GOVD_FIRSTPRIVATE) != 0;
6175           break;
6176
6177         case OMP_CLAUSE_REDUCTION:
6178         case OMP_CLAUSE_COPYIN:
6179         case OMP_CLAUSE_COPYPRIVATE:
6180         case OMP_CLAUSE_IF:
6181         case OMP_CLAUSE_NUM_THREADS:
6182         case OMP_CLAUSE_SCHEDULE:
6183         case OMP_CLAUSE_NOWAIT:
6184         case OMP_CLAUSE_ORDERED:
6185         case OMP_CLAUSE_DEFAULT:
6186         case OMP_CLAUSE_UNTIED:
6187         case OMP_CLAUSE_COLLAPSE:
6188         case OMP_CLAUSE_FINAL:
6189         case OMP_CLAUSE_MERGEABLE:
6190           break;
6191
6192         default:
6193           gcc_unreachable ();
6194         }
6195
6196       if (remove)
6197         *list_p = OMP_CLAUSE_CHAIN (c);
6198       else
6199         list_p = &OMP_CLAUSE_CHAIN (c);
6200     }
6201
6202   /* Add in any implicit data sharing.  */
6203   splay_tree_foreach (ctx->variables, gimplify_adjust_omp_clauses_1, list_p);
6204
6205   gimplify_omp_ctxp = ctx->outer_context;
6206   delete_omp_context (ctx);
6207 }
6208
6209 /* Gimplify the contents of an OMP_PARALLEL statement.  This involves
6210    gimplification of the body, as well as scanning the body for used
6211    variables.  We need to do this scan now, because variable-sized
6212    decls will be decomposed during gimplification.  */
6213
6214 static void
6215 gimplify_omp_parallel (tree *expr_p, gimple_seq *pre_p)
6216 {
6217   tree expr = *expr_p;
6218   gimple g;
6219   gimple_seq body = NULL;
6220   struct gimplify_ctx gctx;
6221
6222   gimplify_scan_omp_clauses (&OMP_PARALLEL_CLAUSES (expr), pre_p,
6223                              OMP_PARALLEL_COMBINED (expr)
6224                              ? ORT_COMBINED_PARALLEL
6225                              : ORT_PARALLEL);
6226
6227   push_gimplify_context (&gctx);
6228
6229   g = gimplify_and_return_first (OMP_PARALLEL_BODY (expr), &body);
6230   if (gimple_code (g) == GIMPLE_BIND)
6231     pop_gimplify_context (g);
6232   else
6233     pop_gimplify_context (NULL);
6234
6235   gimplify_adjust_omp_clauses (&OMP_PARALLEL_CLAUSES (expr));
6236
6237   g = gimple_build_omp_parallel (body,
6238                                  OMP_PARALLEL_CLAUSES (expr),
6239                                  NULL_TREE, NULL_TREE);
6240   if (OMP_PARALLEL_COMBINED (expr))
6241     gimple_omp_set_subcode (g, GF_OMP_PARALLEL_COMBINED);
6242   gimplify_seq_add_stmt (pre_p, g);
6243   *expr_p = NULL_TREE;
6244 }
6245
6246 /* Gimplify the contents of an OMP_TASK statement.  This involves
6247    gimplification of the body, as well as scanning the body for used
6248    variables.  We need to do this scan now, because variable-sized
6249    decls will be decomposed during gimplification.  */
6250
6251 static void
6252 gimplify_omp_task (tree *expr_p, gimple_seq *pre_p)
6253 {
6254   tree expr = *expr_p;
6255   gimple g;
6256   gimple_seq body = NULL;
6257   struct gimplify_ctx gctx;
6258
6259   gimplify_scan_omp_clauses (&OMP_TASK_CLAUSES (expr), pre_p,
6260                              find_omp_clause (OMP_TASK_CLAUSES (expr),
6261                                               OMP_CLAUSE_UNTIED)
6262                              ? ORT_UNTIED_TASK : ORT_TASK);
6263
6264   push_gimplify_context (&gctx);
6265
6266   g = gimplify_and_return_first (OMP_TASK_BODY (expr), &body);
6267   if (gimple_code (g) == GIMPLE_BIND)
6268     pop_gimplify_context (g);
6269   else
6270     pop_gimplify_context (NULL);
6271
6272   gimplify_adjust_omp_clauses (&OMP_TASK_CLAUSES (expr));
6273
6274   g = gimple_build_omp_task (body,
6275                              OMP_TASK_CLAUSES (expr),
6276                              NULL_TREE, NULL_TREE,
6277                              NULL_TREE, NULL_TREE, NULL_TREE);
6278   gimplify_seq_add_stmt (pre_p, g);
6279   *expr_p = NULL_TREE;
6280 }
6281
6282 /* Gimplify the gross structure of an OMP_FOR statement.  */
6283
6284 static enum gimplify_status
6285 gimplify_omp_for (tree *expr_p, gimple_seq *pre_p)
6286 {
6287   tree for_stmt, decl, var, t;
6288   enum gimplify_status ret = GS_ALL_DONE;
6289   enum gimplify_status tret;
6290   gimple gfor;
6291   gimple_seq for_body, for_pre_body;
6292   int i;
6293
6294   for_stmt = *expr_p;
6295
6296   gimplify_scan_omp_clauses (&OMP_FOR_CLAUSES (for_stmt), pre_p,
6297                              ORT_WORKSHARE);
6298
6299   /* Handle OMP_FOR_INIT.  */
6300   for_pre_body = NULL;
6301   gimplify_and_add (OMP_FOR_PRE_BODY (for_stmt), &for_pre_body);
6302   OMP_FOR_PRE_BODY (for_stmt) = NULL_TREE;
6303
6304   for_body = gimple_seq_alloc ();
6305   gcc_assert (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt))
6306               == TREE_VEC_LENGTH (OMP_FOR_COND (for_stmt)));
6307   gcc_assert (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt))
6308               == TREE_VEC_LENGTH (OMP_FOR_INCR (for_stmt)));
6309   for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
6310     {
6311       t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
6312       gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
6313       decl = TREE_OPERAND (t, 0);
6314       gcc_assert (DECL_P (decl));
6315       gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (decl))
6316                   || POINTER_TYPE_P (TREE_TYPE (decl)));
6317
6318       /* Make sure the iteration variable is private.  */
6319       if (omp_is_private (gimplify_omp_ctxp, decl))
6320         omp_notice_variable (gimplify_omp_ctxp, decl, true);
6321       else
6322         omp_add_variable (gimplify_omp_ctxp, decl, GOVD_PRIVATE | GOVD_SEEN);
6323
6324       /* If DECL is not a gimple register, create a temporary variable to act
6325          as an iteration counter.  This is valid, since DECL cannot be
6326          modified in the body of the loop.  */
6327       if (!is_gimple_reg (decl))
6328         {
6329           var = create_tmp_var (TREE_TYPE (decl), get_name (decl));
6330           TREE_OPERAND (t, 0) = var;
6331
6332           gimplify_seq_add_stmt (&for_body, gimple_build_assign (decl, var));
6333
6334           omp_add_variable (gimplify_omp_ctxp, var, GOVD_PRIVATE | GOVD_SEEN);
6335         }
6336       else
6337         var = decl;
6338
6339       tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
6340                             is_gimple_val, fb_rvalue);
6341       ret = MIN (ret, tret);
6342       if (ret == GS_ERROR)
6343         return ret;
6344
6345       /* Handle OMP_FOR_COND.  */
6346       t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), i);
6347       gcc_assert (COMPARISON_CLASS_P (t));
6348       gcc_assert (TREE_OPERAND (t, 0) == decl);
6349
6350       tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
6351                             is_gimple_val, fb_rvalue);
6352       ret = MIN (ret, tret);
6353
6354       /* Handle OMP_FOR_INCR.  */
6355       t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
6356       switch (TREE_CODE (t))
6357         {
6358         case PREINCREMENT_EXPR:
6359         case POSTINCREMENT_EXPR:
6360           t = build_int_cst (TREE_TYPE (decl), 1);
6361           t = build2 (PLUS_EXPR, TREE_TYPE (decl), var, t);
6362           t = build2 (MODIFY_EXPR, TREE_TYPE (var), var, t);
6363           TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i) = t;
6364           break;
6365
6366         case PREDECREMENT_EXPR:
6367         case POSTDECREMENT_EXPR:
6368           t = build_int_cst (TREE_TYPE (decl), -1);
6369           t = build2 (PLUS_EXPR, TREE_TYPE (decl), var, t);
6370           t = build2 (MODIFY_EXPR, TREE_TYPE (var), var, t);
6371           TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i) = t;
6372           break;
6373
6374         case MODIFY_EXPR:
6375           gcc_assert (TREE_OPERAND (t, 0) == decl);
6376           TREE_OPERAND (t, 0) = var;
6377
6378           t = TREE_OPERAND (t, 1);
6379           switch (TREE_CODE (t))
6380             {
6381             case PLUS_EXPR:
6382               if (TREE_OPERAND (t, 1) == decl)
6383                 {
6384                   TREE_OPERAND (t, 1) = TREE_OPERAND (t, 0);
6385                   TREE_OPERAND (t, 0) = var;
6386                   break;
6387                 }
6388
6389               /* Fallthru.  */
6390             case MINUS_EXPR:
6391             case POINTER_PLUS_EXPR:
6392               gcc_assert (TREE_OPERAND (t, 0) == decl);
6393               TREE_OPERAND (t, 0) = var;
6394               break;
6395             default:
6396               gcc_unreachable ();
6397             }
6398
6399           tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
6400                                 is_gimple_val, fb_rvalue);
6401           ret = MIN (ret, tret);
6402           break;
6403
6404         default:
6405           gcc_unreachable ();
6406         }
6407
6408       if (var != decl || TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) > 1)
6409         {
6410           tree c;
6411           for (c = OMP_FOR_CLAUSES (for_stmt); c ; c = OMP_CLAUSE_CHAIN (c))
6412             if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
6413                 && OMP_CLAUSE_DECL (c) == decl
6414                 && OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c) == NULL)
6415               {
6416                 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
6417                 gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
6418                 gcc_assert (TREE_OPERAND (t, 0) == var);
6419                 t = TREE_OPERAND (t, 1);
6420                 gcc_assert (TREE_CODE (t) == PLUS_EXPR
6421                             || TREE_CODE (t) == MINUS_EXPR
6422                             || TREE_CODE (t) == POINTER_PLUS_EXPR);
6423                 gcc_assert (TREE_OPERAND (t, 0) == var);
6424                 t = build2 (TREE_CODE (t), TREE_TYPE (decl), decl,
6425                             TREE_OPERAND (t, 1));
6426                 gimplify_assign (decl, t,
6427                                  &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c));
6428             }
6429         }
6430     }
6431
6432   gimplify_and_add (OMP_FOR_BODY (for_stmt), &for_body);
6433
6434   gimplify_adjust_omp_clauses (&OMP_FOR_CLAUSES (for_stmt));
6435
6436   gfor = gimple_build_omp_for (for_body, OMP_FOR_CLAUSES (for_stmt),
6437                                TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)),
6438                                for_pre_body);
6439
6440   for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
6441     {
6442       t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
6443       gimple_omp_for_set_index (gfor, i, TREE_OPERAND (t, 0));
6444       gimple_omp_for_set_initial (gfor, i, TREE_OPERAND (t, 1));
6445       t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), i);
6446       gimple_omp_for_set_cond (gfor, i, TREE_CODE (t));
6447       gimple_omp_for_set_final (gfor, i, TREE_OPERAND (t, 1));
6448       t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
6449       gimple_omp_for_set_incr (gfor, i, TREE_OPERAND (t, 1));
6450     }
6451
6452   gimplify_seq_add_stmt (pre_p, gfor);
6453   return ret == GS_ALL_DONE ? GS_ALL_DONE : GS_ERROR;
6454 }
6455
6456 /* Gimplify the gross structure of other OpenMP worksharing constructs.
6457    In particular, OMP_SECTIONS and OMP_SINGLE.  */
6458
6459 static void
6460 gimplify_omp_workshare (tree *expr_p, gimple_seq *pre_p)
6461 {
6462   tree expr = *expr_p;
6463   gimple stmt;
6464   gimple_seq body = NULL;
6465
6466   gimplify_scan_omp_clauses (&OMP_CLAUSES (expr), pre_p, ORT_WORKSHARE);
6467   gimplify_and_add (OMP_BODY (expr), &body);
6468   gimplify_adjust_omp_clauses (&OMP_CLAUSES (expr));
6469
6470   if (TREE_CODE (expr) == OMP_SECTIONS)
6471     stmt = gimple_build_omp_sections (body, OMP_CLAUSES (expr));
6472   else if (TREE_CODE (expr) == OMP_SINGLE)
6473     stmt = gimple_build_omp_single (body, OMP_CLAUSES (expr));
6474   else
6475     gcc_unreachable ();
6476
6477   gimplify_seq_add_stmt (pre_p, stmt);
6478 }
6479
6480 /* A subroutine of gimplify_omp_atomic.  The front end is supposed to have
6481    stabilized the lhs of the atomic operation as *ADDR.  Return true if
6482    EXPR is this stabilized form.  */
6483
6484 static bool
6485 goa_lhs_expr_p (tree expr, tree addr)
6486 {
6487   /* Also include casts to other type variants.  The C front end is fond
6488      of adding these for e.g. volatile variables.  This is like
6489      STRIP_TYPE_NOPS but includes the main variant lookup.  */
6490   STRIP_USELESS_TYPE_CONVERSION (expr);
6491
6492   if (TREE_CODE (expr) == INDIRECT_REF)
6493     {
6494       expr = TREE_OPERAND (expr, 0);
6495       while (expr != addr
6496              && (CONVERT_EXPR_P (expr)
6497                  || TREE_CODE (expr) == NON_LVALUE_EXPR)
6498              && TREE_CODE (expr) == TREE_CODE (addr)
6499              && types_compatible_p (TREE_TYPE (expr), TREE_TYPE (addr)))
6500         {
6501           expr = TREE_OPERAND (expr, 0);
6502           addr = TREE_OPERAND (addr, 0);
6503         }
6504       if (expr == addr)
6505         return true;
6506       return (TREE_CODE (addr) == ADDR_EXPR
6507               && TREE_CODE (expr) == ADDR_EXPR
6508               && TREE_OPERAND (addr, 0) == TREE_OPERAND (expr, 0));
6509     }
6510   if (TREE_CODE (addr) == ADDR_EXPR && expr == TREE_OPERAND (addr, 0))
6511     return true;
6512   return false;
6513 }
6514
6515 /* Walk *EXPR_P and replace appearances of *LHS_ADDR with LHS_VAR.  If an
6516    expression does not involve the lhs, evaluate it into a temporary.
6517    Return 1 if the lhs appeared as a subexpression, 0 if it did not,
6518    or -1 if an error was encountered.  */
6519
6520 static int
6521 goa_stabilize_expr (tree *expr_p, gimple_seq *pre_p, tree lhs_addr,
6522                     tree lhs_var)
6523 {
6524   tree expr = *expr_p;
6525   int saw_lhs;
6526
6527   if (goa_lhs_expr_p (expr, lhs_addr))
6528     {
6529       *expr_p = lhs_var;
6530       return 1;
6531     }
6532   if (is_gimple_val (expr))
6533     return 0;
6534
6535   saw_lhs = 0;
6536   switch (TREE_CODE_CLASS (TREE_CODE (expr)))
6537     {
6538     case tcc_binary:
6539     case tcc_comparison:
6540       saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 1), pre_p, lhs_addr,
6541                                      lhs_var);
6542     case tcc_unary:
6543       saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 0), pre_p, lhs_addr,
6544                                      lhs_var);
6545       break;
6546     case tcc_expression:
6547       switch (TREE_CODE (expr))
6548         {
6549         case TRUTH_ANDIF_EXPR:
6550         case TRUTH_ORIF_EXPR:
6551         case TRUTH_AND_EXPR:
6552         case TRUTH_OR_EXPR:
6553         case TRUTH_XOR_EXPR:
6554           saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 1), pre_p,
6555                                          lhs_addr, lhs_var);
6556         case TRUTH_NOT_EXPR:
6557           saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 0), pre_p,
6558                                          lhs_addr, lhs_var);
6559           break;
6560         case COMPOUND_EXPR:
6561           /* Break out any preevaluations from cp_build_modify_expr.  */
6562           for (; TREE_CODE (expr) == COMPOUND_EXPR;
6563                expr = TREE_OPERAND (expr, 1))
6564             gimplify_stmt (&TREE_OPERAND (expr, 0), pre_p);
6565           *expr_p = expr;
6566           return goa_stabilize_expr (expr_p, pre_p, lhs_addr, lhs_var);
6567         default:
6568           break;
6569         }
6570       break;
6571     default:
6572       break;
6573     }
6574
6575   if (saw_lhs == 0)
6576     {
6577       enum gimplify_status gs;
6578       gs = gimplify_expr (expr_p, pre_p, NULL, is_gimple_val, fb_rvalue);
6579       if (gs != GS_ALL_DONE)
6580         saw_lhs = -1;
6581     }
6582
6583   return saw_lhs;
6584 }
6585
6586 /* Gimplify an OMP_ATOMIC statement.  */
6587
6588 static enum gimplify_status
6589 gimplify_omp_atomic (tree *expr_p, gimple_seq *pre_p)
6590 {
6591   tree addr = TREE_OPERAND (*expr_p, 0);
6592   tree rhs = TREE_CODE (*expr_p) == OMP_ATOMIC_READ
6593              ? NULL : TREE_OPERAND (*expr_p, 1);
6594   tree type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (addr)));
6595   tree tmp_load;
6596   gimple loadstmt, storestmt;
6597
6598   tmp_load = create_tmp_reg (type, NULL);
6599   if (rhs && goa_stabilize_expr (&rhs, pre_p, addr, tmp_load) < 0)
6600     return GS_ERROR;
6601
6602   if (gimplify_expr (&addr, pre_p, NULL, is_gimple_val, fb_rvalue)
6603       != GS_ALL_DONE)
6604     return GS_ERROR;
6605
6606   loadstmt = gimple_build_omp_atomic_load (tmp_load, addr);
6607   gimplify_seq_add_stmt (pre_p, loadstmt);
6608   if (rhs && gimplify_expr (&rhs, pre_p, NULL, is_gimple_val, fb_rvalue)
6609       != GS_ALL_DONE)
6610     return GS_ERROR;
6611
6612   if (TREE_CODE (*expr_p) == OMP_ATOMIC_READ)
6613     rhs = tmp_load;
6614   storestmt = gimple_build_omp_atomic_store (rhs);
6615   gimplify_seq_add_stmt (pre_p, storestmt);
6616   switch (TREE_CODE (*expr_p))
6617     {
6618     case OMP_ATOMIC_READ:
6619     case OMP_ATOMIC_CAPTURE_OLD:
6620       *expr_p = tmp_load;
6621       gimple_omp_atomic_set_need_value (loadstmt);
6622       break;
6623     case OMP_ATOMIC_CAPTURE_NEW:
6624       *expr_p = rhs;
6625       gimple_omp_atomic_set_need_value (storestmt);
6626       break;
6627     default:
6628       *expr_p = NULL;
6629       break;
6630     }
6631
6632    return GS_ALL_DONE;
6633 }
6634
6635 /* Gimplify a TRANSACTION_EXPR.  This involves gimplification of the
6636    body, and adding some EH bits.  */
6637
6638 static enum gimplify_status
6639 gimplify_transaction (tree *expr_p, gimple_seq *pre_p)
6640 {
6641   tree expr = *expr_p, temp, tbody = TRANSACTION_EXPR_BODY (expr);
6642   gimple g;
6643   gimple_seq body = NULL;
6644   struct gimplify_ctx gctx;
6645   int subcode = 0;
6646
6647   /* Wrap the transaction body in a BIND_EXPR so we have a context
6648      where to put decls for OpenMP.  */
6649   if (TREE_CODE (tbody) != BIND_EXPR)
6650     {
6651       tree bind = build3 (BIND_EXPR, void_type_node, NULL, tbody, NULL);
6652       TREE_SIDE_EFFECTS (bind) = 1;
6653       SET_EXPR_LOCATION (bind, EXPR_LOCATION (tbody));
6654       TRANSACTION_EXPR_BODY (expr) = bind;
6655     }
6656
6657   push_gimplify_context (&gctx);
6658   temp = voidify_wrapper_expr (*expr_p, NULL);
6659
6660   g = gimplify_and_return_first (TRANSACTION_EXPR_BODY (expr), &body);
6661   pop_gimplify_context (g);
6662
6663   g = gimple_build_transaction (body, NULL);
6664   if (TRANSACTION_EXPR_OUTER (expr))
6665     subcode = GTMA_IS_OUTER;
6666   else if (TRANSACTION_EXPR_RELAXED (expr))
6667     subcode = GTMA_IS_RELAXED;
6668   gimple_transaction_set_subcode (g, subcode);
6669
6670   gimplify_seq_add_stmt (pre_p, g);
6671
6672   if (temp)
6673     {
6674       *expr_p = temp;
6675       return GS_OK;
6676     }
6677
6678   *expr_p = NULL_TREE;
6679   return GS_ALL_DONE;
6680 }
6681
6682 /* Convert the GENERIC expression tree *EXPR_P to GIMPLE.  If the
6683    expression produces a value to be used as an operand inside a GIMPLE
6684    statement, the value will be stored back in *EXPR_P.  This value will
6685    be a tree of class tcc_declaration, tcc_constant, tcc_reference or
6686    an SSA_NAME.  The corresponding sequence of GIMPLE statements is
6687    emitted in PRE_P and POST_P.
6688
6689    Additionally, this process may overwrite parts of the input
6690    expression during gimplification.  Ideally, it should be
6691    possible to do non-destructive gimplification.
6692
6693    EXPR_P points to the GENERIC expression to convert to GIMPLE.  If
6694       the expression needs to evaluate to a value to be used as
6695       an operand in a GIMPLE statement, this value will be stored in
6696       *EXPR_P on exit.  This happens when the caller specifies one
6697       of fb_lvalue or fb_rvalue fallback flags.
6698
6699    PRE_P will contain the sequence of GIMPLE statements corresponding
6700        to the evaluation of EXPR and all the side-effects that must
6701        be executed before the main expression.  On exit, the last
6702        statement of PRE_P is the core statement being gimplified.  For
6703        instance, when gimplifying 'if (++a)' the last statement in
6704        PRE_P will be 'if (t.1)' where t.1 is the result of
6705        pre-incrementing 'a'.
6706
6707    POST_P will contain the sequence of GIMPLE statements corresponding
6708        to the evaluation of all the side-effects that must be executed
6709        after the main expression.  If this is NULL, the post
6710        side-effects are stored at the end of PRE_P.
6711
6712        The reason why the output is split in two is to handle post
6713        side-effects explicitly.  In some cases, an expression may have
6714        inner and outer post side-effects which need to be emitted in
6715        an order different from the one given by the recursive
6716        traversal.  For instance, for the expression (*p--)++ the post
6717        side-effects of '--' must actually occur *after* the post
6718        side-effects of '++'.  However, gimplification will first visit
6719        the inner expression, so if a separate POST sequence was not
6720        used, the resulting sequence would be:
6721
6722             1   t.1 = *p
6723             2   p = p - 1
6724             3   t.2 = t.1 + 1
6725             4   *p = t.2
6726
6727        However, the post-decrement operation in line #2 must not be
6728        evaluated until after the store to *p at line #4, so the
6729        correct sequence should be:
6730
6731             1   t.1 = *p
6732             2   t.2 = t.1 + 1
6733             3   *p = t.2
6734             4   p = p - 1
6735
6736        So, by specifying a separate post queue, it is possible
6737        to emit the post side-effects in the correct order.
6738        If POST_P is NULL, an internal queue will be used.  Before
6739        returning to the caller, the sequence POST_P is appended to
6740        the main output sequence PRE_P.
6741
6742    GIMPLE_TEST_F points to a function that takes a tree T and
6743        returns nonzero if T is in the GIMPLE form requested by the
6744        caller.  The GIMPLE predicates are in gimple.c.
6745
6746    FALLBACK tells the function what sort of a temporary we want if
6747        gimplification cannot produce an expression that complies with
6748        GIMPLE_TEST_F.
6749
6750        fb_none means that no temporary should be generated
6751        fb_rvalue means that an rvalue is OK to generate
6752        fb_lvalue means that an lvalue is OK to generate
6753        fb_either means that either is OK, but an lvalue is preferable.
6754        fb_mayfail means that gimplification may fail (in which case
6755        GS_ERROR will be returned)
6756
6757    The return value is either GS_ERROR or GS_ALL_DONE, since this
6758    function iterates until EXPR is completely gimplified or an error
6759    occurs.  */
6760
6761 enum gimplify_status
6762 gimplify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
6763                bool (*gimple_test_f) (tree), fallback_t fallback)
6764 {
6765   tree tmp;
6766   gimple_seq internal_pre = NULL;
6767   gimple_seq internal_post = NULL;
6768   tree save_expr;
6769   bool is_statement;
6770   location_t saved_location;
6771   enum gimplify_status ret;
6772   gimple_stmt_iterator pre_last_gsi, post_last_gsi;
6773
6774   save_expr = *expr_p;
6775   if (save_expr == NULL_TREE)
6776     return GS_ALL_DONE;
6777
6778   /* If we are gimplifying a top-level statement, PRE_P must be valid.  */
6779   is_statement = gimple_test_f == is_gimple_stmt;
6780   if (is_statement)
6781     gcc_assert (pre_p);
6782
6783   /* Consistency checks.  */
6784   if (gimple_test_f == is_gimple_reg)
6785     gcc_assert (fallback & (fb_rvalue | fb_lvalue));
6786   else if (gimple_test_f == is_gimple_val
6787            || gimple_test_f == is_gimple_call_addr
6788            || gimple_test_f == is_gimple_condexpr
6789            || gimple_test_f == is_gimple_mem_rhs
6790            || gimple_test_f == is_gimple_mem_rhs_or_call
6791            || gimple_test_f == is_gimple_reg_rhs
6792            || gimple_test_f == is_gimple_reg_rhs_or_call
6793            || gimple_test_f == is_gimple_asm_val
6794            || gimple_test_f == is_gimple_mem_ref_addr)
6795     gcc_assert (fallback & fb_rvalue);
6796   else if (gimple_test_f == is_gimple_min_lval
6797            || gimple_test_f == is_gimple_lvalue)
6798     gcc_assert (fallback & fb_lvalue);
6799   else if (gimple_test_f == is_gimple_addressable)
6800     gcc_assert (fallback & fb_either);
6801   else if (gimple_test_f == is_gimple_stmt)
6802     gcc_assert (fallback == fb_none);
6803   else
6804     {
6805       /* We should have recognized the GIMPLE_TEST_F predicate to
6806          know what kind of fallback to use in case a temporary is
6807          needed to hold the value or address of *EXPR_P.  */
6808       gcc_unreachable ();
6809     }
6810
6811   /* We used to check the predicate here and return immediately if it
6812      succeeds.  This is wrong; the design is for gimplification to be
6813      idempotent, and for the predicates to only test for valid forms, not
6814      whether they are fully simplified.  */
6815   if (pre_p == NULL)
6816     pre_p = &internal_pre;
6817
6818   if (post_p == NULL)
6819     post_p = &internal_post;
6820
6821   /* Remember the last statements added to PRE_P and POST_P.  Every
6822      new statement added by the gimplification helpers needs to be
6823      annotated with location information.  To centralize the
6824      responsibility, we remember the last statement that had been
6825      added to both queues before gimplifying *EXPR_P.  If
6826      gimplification produces new statements in PRE_P and POST_P, those
6827      statements will be annotated with the same location information
6828      as *EXPR_P.  */
6829   pre_last_gsi = gsi_last (*pre_p);
6830   post_last_gsi = gsi_last (*post_p);
6831
6832   saved_location = input_location;
6833   if (save_expr != error_mark_node
6834       && EXPR_HAS_LOCATION (*expr_p))
6835     input_location = EXPR_LOCATION (*expr_p);
6836
6837   /* Loop over the specific gimplifiers until the toplevel node
6838      remains the same.  */
6839   do
6840     {
6841       /* Strip away as many useless type conversions as possible
6842          at the toplevel.  */
6843       STRIP_USELESS_TYPE_CONVERSION (*expr_p);
6844
6845       /* Remember the expr.  */
6846       save_expr = *expr_p;
6847
6848       /* Die, die, die, my darling.  */
6849       if (save_expr == error_mark_node
6850           || (TREE_TYPE (save_expr)
6851               && TREE_TYPE (save_expr) == error_mark_node))
6852         {
6853           ret = GS_ERROR;
6854           break;
6855         }
6856
6857       /* Do any language-specific gimplification.  */
6858       ret = ((enum gimplify_status)
6859              lang_hooks.gimplify_expr (expr_p, pre_p, post_p));
6860       if (ret == GS_OK)
6861         {
6862           if (*expr_p == NULL_TREE)
6863             break;
6864           if (*expr_p != save_expr)
6865             continue;
6866         }
6867       else if (ret != GS_UNHANDLED)
6868         break;
6869
6870       /* Make sure that all the cases set 'ret' appropriately.  */
6871       ret = GS_UNHANDLED;
6872       switch (TREE_CODE (*expr_p))
6873         {
6874           /* First deal with the special cases.  */
6875
6876         case POSTINCREMENT_EXPR:
6877         case POSTDECREMENT_EXPR:
6878         case PREINCREMENT_EXPR:
6879         case PREDECREMENT_EXPR:
6880           ret = gimplify_self_mod_expr (expr_p, pre_p, post_p,
6881                                         fallback != fb_none);
6882           break;
6883
6884         case ARRAY_REF:
6885         case ARRAY_RANGE_REF:
6886         case REALPART_EXPR:
6887         case IMAGPART_EXPR:
6888         case COMPONENT_REF:
6889         case VIEW_CONVERT_EXPR:
6890           ret = gimplify_compound_lval (expr_p, pre_p, post_p,
6891                                         fallback ? fallback : fb_rvalue);
6892           break;
6893
6894         case COND_EXPR:
6895           ret = gimplify_cond_expr (expr_p, pre_p, fallback);
6896
6897           /* C99 code may assign to an array in a structure value of a
6898              conditional expression, and this has undefined behavior
6899              only on execution, so create a temporary if an lvalue is
6900              required.  */
6901           if (fallback == fb_lvalue)
6902             {
6903               *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p);
6904               mark_addressable (*expr_p);
6905               ret = GS_OK;
6906             }
6907           break;
6908
6909         case CALL_EXPR:
6910           ret = gimplify_call_expr (expr_p, pre_p, fallback != fb_none);
6911
6912           /* C99 code may assign to an array in a structure returned
6913              from a function, and this has undefined behavior only on
6914              execution, so create a temporary if an lvalue is
6915              required.  */
6916           if (fallback == fb_lvalue)
6917             {
6918               *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p);
6919               mark_addressable (*expr_p);
6920               ret = GS_OK;
6921             }
6922           break;
6923
6924         case TREE_LIST:
6925           gcc_unreachable ();
6926
6927         case COMPOUND_EXPR:
6928           ret = gimplify_compound_expr (expr_p, pre_p, fallback != fb_none);
6929           break;
6930
6931         case COMPOUND_LITERAL_EXPR:
6932           ret = gimplify_compound_literal_expr (expr_p, pre_p);
6933           break;
6934
6935         case MODIFY_EXPR:
6936         case INIT_EXPR:
6937           ret = gimplify_modify_expr (expr_p, pre_p, post_p,
6938                                       fallback != fb_none);
6939           break;
6940
6941         case TRUTH_ANDIF_EXPR:
6942         case TRUTH_ORIF_EXPR:
6943           {
6944             /* Preserve the original type of the expression and the
6945                source location of the outer expression.  */
6946             tree org_type = TREE_TYPE (*expr_p);
6947             *expr_p = gimple_boolify (*expr_p);
6948             *expr_p = build3_loc (input_location, COND_EXPR,
6949                                   org_type, *expr_p,
6950                                   fold_convert_loc
6951                                     (input_location,
6952                                      org_type, boolean_true_node),
6953                                   fold_convert_loc
6954                                     (input_location,
6955                                      org_type, boolean_false_node));
6956             ret = GS_OK;
6957             break;
6958           }
6959
6960         case TRUTH_NOT_EXPR:
6961           {
6962             tree type = TREE_TYPE (*expr_p);
6963             /* The parsers are careful to generate TRUTH_NOT_EXPR
6964                only with operands that are always zero or one.
6965                We do not fold here but handle the only interesting case
6966                manually, as fold may re-introduce the TRUTH_NOT_EXPR.  */
6967             *expr_p = gimple_boolify (*expr_p);
6968             if (TYPE_PRECISION (TREE_TYPE (*expr_p)) == 1)
6969               *expr_p = build1_loc (input_location, BIT_NOT_EXPR,
6970                                     TREE_TYPE (*expr_p),
6971                                     TREE_OPERAND (*expr_p, 0));
6972             else
6973               *expr_p = build2_loc (input_location, BIT_XOR_EXPR,
6974                                     TREE_TYPE (*expr_p),
6975                                     TREE_OPERAND (*expr_p, 0),
6976                                     build_int_cst (TREE_TYPE (*expr_p), 1));
6977             if (!useless_type_conversion_p (type, TREE_TYPE (*expr_p)))
6978               *expr_p = fold_convert_loc (input_location, type, *expr_p);
6979             ret = GS_OK;
6980             break;
6981           }
6982
6983         case ADDR_EXPR:
6984           ret = gimplify_addr_expr (expr_p, pre_p, post_p);
6985           break;
6986
6987         case VA_ARG_EXPR:
6988           ret = gimplify_va_arg_expr (expr_p, pre_p, post_p);
6989           break;
6990
6991         CASE_CONVERT:
6992           if (IS_EMPTY_STMT (*expr_p))
6993             {
6994               ret = GS_ALL_DONE;
6995               break;
6996             }
6997
6998           if (VOID_TYPE_P (TREE_TYPE (*expr_p))
6999               || fallback == fb_none)
7000             {
7001               /* Just strip a conversion to void (or in void context) and
7002                  try again.  */
7003               *expr_p = TREE_OPERAND (*expr_p, 0);
7004               ret = GS_OK;
7005               break;
7006             }
7007
7008           ret = gimplify_conversion (expr_p);
7009           if (ret == GS_ERROR)
7010             break;
7011           if (*expr_p != save_expr)
7012             break;
7013           /* FALLTHRU */
7014
7015         case FIX_TRUNC_EXPR:
7016           /* unary_expr: ... | '(' cast ')' val | ...  */
7017           ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
7018                                is_gimple_val, fb_rvalue);
7019           recalculate_side_effects (*expr_p);
7020           break;
7021
7022         case INDIRECT_REF:
7023           {
7024             bool volatilep = TREE_THIS_VOLATILE (*expr_p);
7025             bool notrap = TREE_THIS_NOTRAP (*expr_p);
7026             tree saved_ptr_type = TREE_TYPE (TREE_OPERAND (*expr_p, 0));
7027
7028             *expr_p = fold_indirect_ref_loc (input_location, *expr_p);
7029             if (*expr_p != save_expr)
7030               {
7031                 ret = GS_OK;
7032                 break;
7033               }
7034
7035             ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
7036                                  is_gimple_reg, fb_rvalue);
7037             if (ret == GS_ERROR)
7038               break;
7039
7040             recalculate_side_effects (*expr_p);
7041             *expr_p = fold_build2_loc (input_location, MEM_REF,
7042                                        TREE_TYPE (*expr_p),
7043                                        TREE_OPERAND (*expr_p, 0),
7044                                        build_int_cst (saved_ptr_type, 0));
7045             TREE_THIS_VOLATILE (*expr_p) = volatilep;
7046             TREE_THIS_NOTRAP (*expr_p) = notrap;
7047             ret = GS_OK;
7048             break;
7049           }
7050
7051         /* We arrive here through the various re-gimplifcation paths.  */
7052         case MEM_REF:
7053           /* First try re-folding the whole thing.  */
7054           tmp = fold_binary (MEM_REF, TREE_TYPE (*expr_p),
7055                              TREE_OPERAND (*expr_p, 0),
7056                              TREE_OPERAND (*expr_p, 1));
7057           if (tmp)
7058             {
7059               *expr_p = tmp;
7060               recalculate_side_effects (*expr_p);
7061               ret = GS_OK;
7062               break;
7063             }
7064           ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
7065                                is_gimple_mem_ref_addr, fb_rvalue);
7066           if (ret == GS_ERROR)
7067             break;
7068           recalculate_side_effects (*expr_p);
7069           ret = GS_ALL_DONE;
7070           break;
7071
7072           /* Constants need not be gimplified.  */
7073         case INTEGER_CST:
7074         case REAL_CST:
7075         case FIXED_CST:
7076         case STRING_CST:
7077         case COMPLEX_CST:
7078         case VECTOR_CST:
7079           ret = GS_ALL_DONE;
7080           break;
7081
7082         case CONST_DECL:
7083           /* If we require an lvalue, such as for ADDR_EXPR, retain the
7084              CONST_DECL node.  Otherwise the decl is replaceable by its
7085              value.  */
7086           /* ??? Should be == fb_lvalue, but ADDR_EXPR passes fb_either.  */
7087           if (fallback & fb_lvalue)
7088             ret = GS_ALL_DONE;
7089           else
7090             {
7091               *expr_p = DECL_INITIAL (*expr_p);
7092               ret = GS_OK;
7093             }
7094           break;
7095
7096         case DECL_EXPR:
7097           ret = gimplify_decl_expr (expr_p, pre_p);
7098           break;
7099
7100         case BIND_EXPR:
7101           ret = gimplify_bind_expr (expr_p, pre_p);
7102           break;
7103
7104         case LOOP_EXPR:
7105           ret = gimplify_loop_expr (expr_p, pre_p);
7106           break;
7107
7108         case SWITCH_EXPR:
7109           ret = gimplify_switch_expr (expr_p, pre_p);
7110           break;
7111
7112         case EXIT_EXPR:
7113           ret = gimplify_exit_expr (expr_p);
7114           break;
7115
7116         case GOTO_EXPR:
7117           /* If the target is not LABEL, then it is a computed jump
7118              and the target needs to be gimplified.  */
7119           if (TREE_CODE (GOTO_DESTINATION (*expr_p)) != LABEL_DECL)
7120             {
7121               ret = gimplify_expr (&GOTO_DESTINATION (*expr_p), pre_p,
7122                                    NULL, is_gimple_val, fb_rvalue);
7123               if (ret == GS_ERROR)
7124                 break;
7125             }
7126           gimplify_seq_add_stmt (pre_p,
7127                           gimple_build_goto (GOTO_DESTINATION (*expr_p)));
7128           ret = GS_ALL_DONE;
7129           break;
7130
7131         case PREDICT_EXPR:
7132           gimplify_seq_add_stmt (pre_p,
7133                         gimple_build_predict (PREDICT_EXPR_PREDICTOR (*expr_p),
7134                                               PREDICT_EXPR_OUTCOME (*expr_p)));
7135           ret = GS_ALL_DONE;
7136           break;
7137
7138         case LABEL_EXPR:
7139           ret = GS_ALL_DONE;
7140           gcc_assert (decl_function_context (LABEL_EXPR_LABEL (*expr_p))
7141                       == current_function_decl);
7142           gimplify_seq_add_stmt (pre_p,
7143                           gimple_build_label (LABEL_EXPR_LABEL (*expr_p)));
7144           break;
7145
7146         case CASE_LABEL_EXPR:
7147           ret = gimplify_case_label_expr (expr_p, pre_p);
7148           break;
7149
7150         case RETURN_EXPR:
7151           ret = gimplify_return_expr (*expr_p, pre_p);
7152           break;
7153
7154         case CONSTRUCTOR:
7155           /* Don't reduce this in place; let gimplify_init_constructor work its
7156              magic.  Buf if we're just elaborating this for side effects, just
7157              gimplify any element that has side-effects.  */
7158           if (fallback == fb_none)
7159             {
7160               unsigned HOST_WIDE_INT ix;
7161               tree val;
7162               tree temp = NULL_TREE;
7163               FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (*expr_p), ix, val)
7164                 if (TREE_SIDE_EFFECTS (val))
7165                   append_to_statement_list (val, &temp);
7166
7167               *expr_p = temp;
7168               ret = temp ? GS_OK : GS_ALL_DONE;
7169             }
7170           /* C99 code may assign to an array in a constructed
7171              structure or union, and this has undefined behavior only
7172              on execution, so create a temporary if an lvalue is
7173              required.  */
7174           else if (fallback == fb_lvalue)
7175             {
7176               *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p);
7177               mark_addressable (*expr_p);
7178               ret = GS_OK;
7179             }
7180           else
7181             ret = GS_ALL_DONE;
7182           break;
7183
7184           /* The following are special cases that are not handled by the
7185              original GIMPLE grammar.  */
7186
7187           /* SAVE_EXPR nodes are converted into a GIMPLE identifier and
7188              eliminated.  */
7189         case SAVE_EXPR:
7190           ret = gimplify_save_expr (expr_p, pre_p, post_p);
7191           break;
7192
7193         case BIT_FIELD_REF:
7194           {
7195             enum gimplify_status r0, r1, r2;
7196
7197             r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
7198                                 post_p, is_gimple_lvalue, fb_either);
7199             r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
7200                                 post_p, is_gimple_val, fb_rvalue);
7201             r2 = gimplify_expr (&TREE_OPERAND (*expr_p, 2), pre_p,
7202                                 post_p, is_gimple_val, fb_rvalue);
7203             recalculate_side_effects (*expr_p);
7204
7205             ret = MIN (r0, MIN (r1, r2));
7206           }
7207           break;
7208
7209         case TARGET_MEM_REF:
7210           {
7211             enum gimplify_status r0 = GS_ALL_DONE, r1 = GS_ALL_DONE;
7212
7213             if (TMR_BASE (*expr_p))
7214               r0 = gimplify_expr (&TMR_BASE (*expr_p), pre_p,
7215                                   post_p, is_gimple_mem_ref_addr, fb_either);
7216             if (TMR_INDEX (*expr_p))
7217               r1 = gimplify_expr (&TMR_INDEX (*expr_p), pre_p,
7218                                   post_p, is_gimple_val, fb_rvalue);
7219             if (TMR_INDEX2 (*expr_p))
7220               r1 = gimplify_expr (&TMR_INDEX2 (*expr_p), pre_p,
7221                                   post_p, is_gimple_val, fb_rvalue);
7222             /* TMR_STEP and TMR_OFFSET are always integer constants.  */
7223             ret = MIN (r0, r1);
7224           }
7225           break;
7226
7227         case NON_LVALUE_EXPR:
7228           /* This should have been stripped above.  */
7229           gcc_unreachable ();
7230
7231         case ASM_EXPR:
7232           ret = gimplify_asm_expr (expr_p, pre_p, post_p);
7233           break;
7234
7235         case TRY_FINALLY_EXPR:
7236         case TRY_CATCH_EXPR:
7237           {
7238             gimple_seq eval, cleanup;
7239             gimple try_;
7240
7241             eval = cleanup = NULL;
7242             gimplify_and_add (TREE_OPERAND (*expr_p, 0), &eval);
7243             gimplify_and_add (TREE_OPERAND (*expr_p, 1), &cleanup);
7244             /* Don't create bogus GIMPLE_TRY with empty cleanup.  */
7245             if (gimple_seq_empty_p (cleanup))
7246               {
7247                 gimple_seq_add_seq (pre_p, eval);
7248                 ret = GS_ALL_DONE;
7249                 break;
7250               }
7251             try_ = gimple_build_try (eval, cleanup,
7252                                      TREE_CODE (*expr_p) == TRY_FINALLY_EXPR
7253                                      ? GIMPLE_TRY_FINALLY
7254                                      : GIMPLE_TRY_CATCH);
7255             if (TREE_CODE (*expr_p) == TRY_CATCH_EXPR)
7256               gimple_try_set_catch_is_cleanup (try_,
7257                                                TRY_CATCH_IS_CLEANUP (*expr_p));
7258             gimplify_seq_add_stmt (pre_p, try_);
7259             ret = GS_ALL_DONE;
7260             break;
7261           }
7262
7263         case CLEANUP_POINT_EXPR:
7264           ret = gimplify_cleanup_point_expr (expr_p, pre_p);
7265           break;
7266
7267         case TARGET_EXPR:
7268           ret = gimplify_target_expr (expr_p, pre_p, post_p);
7269           break;
7270
7271         case CATCH_EXPR:
7272           {
7273             gimple c;
7274             gimple_seq handler = NULL;
7275             gimplify_and_add (CATCH_BODY (*expr_p), &handler);
7276             c = gimple_build_catch (CATCH_TYPES (*expr_p), handler);
7277             gimplify_seq_add_stmt (pre_p, c);
7278             ret = GS_ALL_DONE;
7279             break;
7280           }
7281
7282         case EH_FILTER_EXPR:
7283           {
7284             gimple ehf;
7285             gimple_seq failure = NULL;
7286
7287             gimplify_and_add (EH_FILTER_FAILURE (*expr_p), &failure);
7288             ehf = gimple_build_eh_filter (EH_FILTER_TYPES (*expr_p), failure);
7289             gimple_set_no_warning (ehf, TREE_NO_WARNING (*expr_p));
7290             gimplify_seq_add_stmt (pre_p, ehf);
7291             ret = GS_ALL_DONE;
7292             break;
7293           }
7294
7295         case OBJ_TYPE_REF:
7296           {
7297             enum gimplify_status r0, r1;
7298             r0 = gimplify_expr (&OBJ_TYPE_REF_OBJECT (*expr_p), pre_p,
7299                                 post_p, is_gimple_val, fb_rvalue);
7300             r1 = gimplify_expr (&OBJ_TYPE_REF_EXPR (*expr_p), pre_p,
7301                                 post_p, is_gimple_val, fb_rvalue);
7302             TREE_SIDE_EFFECTS (*expr_p) = 0;
7303             ret = MIN (r0, r1);
7304           }
7305           break;
7306
7307         case LABEL_DECL:
7308           /* We get here when taking the address of a label.  We mark
7309              the label as "forced"; meaning it can never be removed and
7310              it is a potential target for any computed goto.  */
7311           FORCED_LABEL (*expr_p) = 1;
7312           ret = GS_ALL_DONE;
7313           break;
7314
7315         case STATEMENT_LIST:
7316           ret = gimplify_statement_list (expr_p, pre_p);
7317           break;
7318
7319         case WITH_SIZE_EXPR:
7320           {
7321             gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
7322                            post_p == &internal_post ? NULL : post_p,
7323                            gimple_test_f, fallback);
7324             gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
7325                            is_gimple_val, fb_rvalue);
7326             ret = GS_ALL_DONE;
7327           }
7328           break;
7329
7330         case VAR_DECL:
7331         case PARM_DECL:
7332           ret = gimplify_var_or_parm_decl (expr_p);
7333           break;
7334
7335         case RESULT_DECL:
7336           /* When within an OpenMP context, notice uses of variables.  */
7337           if (gimplify_omp_ctxp)
7338             omp_notice_variable (gimplify_omp_ctxp, *expr_p, true);
7339           ret = GS_ALL_DONE;
7340           break;
7341
7342         case SSA_NAME:
7343           /* Allow callbacks into the gimplifier during optimization.  */
7344           ret = GS_ALL_DONE;
7345           break;
7346
7347         case OMP_PARALLEL:
7348           gimplify_omp_parallel (expr_p, pre_p);
7349           ret = GS_ALL_DONE;
7350           break;
7351
7352         case OMP_TASK:
7353           gimplify_omp_task (expr_p, pre_p);
7354           ret = GS_ALL_DONE;
7355           break;
7356
7357         case OMP_FOR:
7358           ret = gimplify_omp_for (expr_p, pre_p);
7359           break;
7360
7361         case OMP_SECTIONS:
7362         case OMP_SINGLE:
7363           gimplify_omp_workshare (expr_p, pre_p);
7364           ret = GS_ALL_DONE;
7365           break;
7366
7367         case OMP_SECTION:
7368         case OMP_MASTER:
7369         case OMP_ORDERED:
7370         case OMP_CRITICAL:
7371           {
7372             gimple_seq body = NULL;
7373             gimple g;
7374
7375             gimplify_and_add (OMP_BODY (*expr_p), &body);
7376             switch (TREE_CODE (*expr_p))
7377               {
7378               case OMP_SECTION:
7379                 g = gimple_build_omp_section (body);
7380                 break;
7381               case OMP_MASTER:
7382                 g = gimple_build_omp_master (body);
7383                 break;
7384               case OMP_ORDERED:
7385                 g = gimple_build_omp_ordered (body);
7386                 break;
7387               case OMP_CRITICAL:
7388                 g = gimple_build_omp_critical (body,
7389                                                OMP_CRITICAL_NAME (*expr_p));
7390                 break;
7391               default:
7392                 gcc_unreachable ();
7393               }
7394             gimplify_seq_add_stmt (pre_p, g);
7395             ret = GS_ALL_DONE;
7396             break;
7397           }
7398
7399         case OMP_ATOMIC:
7400         case OMP_ATOMIC_READ:
7401         case OMP_ATOMIC_CAPTURE_OLD:
7402         case OMP_ATOMIC_CAPTURE_NEW:
7403           ret = gimplify_omp_atomic (expr_p, pre_p);
7404           break;
7405
7406         case TRANSACTION_EXPR:
7407           ret = gimplify_transaction (expr_p, pre_p);
7408           break;
7409
7410         case TRUTH_AND_EXPR:
7411         case TRUTH_OR_EXPR:
7412         case TRUTH_XOR_EXPR:
7413           {
7414             tree orig_type = TREE_TYPE (*expr_p);
7415             tree new_type, xop0, xop1;
7416             *expr_p = gimple_boolify (*expr_p);
7417             new_type = TREE_TYPE (*expr_p);
7418             if (!useless_type_conversion_p (orig_type, new_type))
7419               {
7420                 *expr_p = fold_convert_loc (input_location, orig_type, *expr_p);
7421                 ret = GS_OK;
7422                 break;
7423               }
7424
7425           /* Boolified binary truth expressions are semantically equivalent
7426              to bitwise binary expressions.  Canonicalize them to the
7427              bitwise variant.  */
7428             switch (TREE_CODE (*expr_p))
7429               {
7430               case TRUTH_AND_EXPR:
7431                 TREE_SET_CODE (*expr_p, BIT_AND_EXPR);
7432                 break;
7433               case TRUTH_OR_EXPR:
7434                 TREE_SET_CODE (*expr_p, BIT_IOR_EXPR);
7435                 break;
7436               case TRUTH_XOR_EXPR:
7437                 TREE_SET_CODE (*expr_p, BIT_XOR_EXPR);
7438                 break;
7439               default:
7440                 break;
7441               }
7442             /* Now make sure that operands have compatible type to
7443                expression's new_type.  */
7444             xop0 = TREE_OPERAND (*expr_p, 0);
7445             xop1 = TREE_OPERAND (*expr_p, 1);
7446             if (!useless_type_conversion_p (new_type, TREE_TYPE (xop0)))
7447               TREE_OPERAND (*expr_p, 0) = fold_convert_loc (input_location,
7448                                                             new_type,
7449                                                             xop0);
7450             if (!useless_type_conversion_p (new_type, TREE_TYPE (xop1)))
7451               TREE_OPERAND (*expr_p, 1) = fold_convert_loc (input_location,
7452                                                             new_type,
7453                                                             xop1);
7454             /* Continue classified as tcc_binary.  */
7455             goto expr_2;
7456           }
7457
7458         case FMA_EXPR:
7459         case VEC_PERM_EXPR:
7460           /* Classified as tcc_expression.  */
7461           goto expr_3;
7462
7463         case POINTER_PLUS_EXPR:
7464           {
7465             enum gimplify_status r0, r1;
7466             r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
7467                                 post_p, is_gimple_val, fb_rvalue);
7468             r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
7469                                 post_p, is_gimple_val, fb_rvalue);
7470             recalculate_side_effects (*expr_p);
7471             ret = MIN (r0, r1);
7472             /* Convert &X + CST to invariant &MEM[&X, CST].  Do this
7473                after gimplifying operands - this is similar to how
7474                it would be folding all gimplified stmts on creation
7475                to have them canonicalized, which is what we eventually
7476                should do anyway.  */
7477             if (TREE_CODE (TREE_OPERAND (*expr_p, 1)) == INTEGER_CST
7478                 && is_gimple_min_invariant (TREE_OPERAND (*expr_p, 0)))
7479               {
7480                 *expr_p = build_fold_addr_expr_with_type_loc
7481                    (input_location,
7482                     fold_build2 (MEM_REF, TREE_TYPE (TREE_TYPE (*expr_p)),
7483                                  TREE_OPERAND (*expr_p, 0),
7484                                  fold_convert (ptr_type_node,
7485                                                TREE_OPERAND (*expr_p, 1))),
7486                     TREE_TYPE (*expr_p));
7487                 ret = MIN (ret, GS_OK);
7488               }
7489             break;
7490           }
7491
7492         default:
7493           switch (TREE_CODE_CLASS (TREE_CODE (*expr_p)))
7494             {
7495             case tcc_comparison:
7496               /* Handle comparison of objects of non scalar mode aggregates
7497                  with a call to memcmp.  It would be nice to only have to do
7498                  this for variable-sized objects, but then we'd have to allow
7499                  the same nest of reference nodes we allow for MODIFY_EXPR and
7500                  that's too complex.
7501
7502                  Compare scalar mode aggregates as scalar mode values.  Using
7503                  memcmp for them would be very inefficient at best, and is
7504                  plain wrong if bitfields are involved.  */
7505                 {
7506                   tree type = TREE_TYPE (TREE_OPERAND (*expr_p, 1));
7507
7508                   /* Vector comparisons need no boolification.  */
7509                   if (TREE_CODE (type) == VECTOR_TYPE)
7510                     goto expr_2;
7511                   else if (!AGGREGATE_TYPE_P (type))
7512                     {
7513                       tree org_type = TREE_TYPE (*expr_p);
7514                       *expr_p = gimple_boolify (*expr_p);
7515                       if (!useless_type_conversion_p (org_type,
7516                                                       TREE_TYPE (*expr_p)))
7517                         {
7518                           *expr_p = fold_convert_loc (input_location,
7519                                                       org_type, *expr_p);
7520                           ret = GS_OK;
7521                         }
7522                       else
7523                         goto expr_2;
7524                     }
7525                   else if (TYPE_MODE (type) != BLKmode)
7526                     ret = gimplify_scalar_mode_aggregate_compare (expr_p);
7527                   else
7528                     ret = gimplify_variable_sized_compare (expr_p);
7529
7530                   break;
7531                 }
7532
7533             /* If *EXPR_P does not need to be special-cased, handle it
7534                according to its class.  */
7535             case tcc_unary:
7536               ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
7537                                    post_p, is_gimple_val, fb_rvalue);
7538               break;
7539
7540             case tcc_binary:
7541             expr_2:
7542               {
7543                 enum gimplify_status r0, r1;
7544
7545                 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
7546                                     post_p, is_gimple_val, fb_rvalue);
7547                 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
7548                                     post_p, is_gimple_val, fb_rvalue);
7549
7550                 ret = MIN (r0, r1);
7551                 break;
7552               }
7553
7554             expr_3:
7555               {
7556                 enum gimplify_status r0, r1, r2;
7557
7558                 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
7559                                     post_p, is_gimple_val, fb_rvalue);
7560                 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
7561                                     post_p, is_gimple_val, fb_rvalue);
7562                 r2 = gimplify_expr (&TREE_OPERAND (*expr_p, 2), pre_p,
7563                                     post_p, is_gimple_val, fb_rvalue);
7564
7565                 ret = MIN (MIN (r0, r1), r2);
7566                 break;
7567               }
7568
7569             case tcc_declaration:
7570             case tcc_constant:
7571               ret = GS_ALL_DONE;
7572               goto dont_recalculate;
7573
7574             default:
7575               gcc_unreachable ();
7576             }
7577
7578           recalculate_side_effects (*expr_p);
7579
7580         dont_recalculate:
7581           break;
7582         }
7583
7584       gcc_assert (*expr_p || ret != GS_OK);
7585     }
7586   while (ret == GS_OK);
7587
7588   /* If we encountered an error_mark somewhere nested inside, either
7589      stub out the statement or propagate the error back out.  */
7590   if (ret == GS_ERROR)
7591     {
7592       if (is_statement)
7593         *expr_p = NULL;
7594       goto out;
7595     }
7596
7597   /* This was only valid as a return value from the langhook, which
7598      we handled.  Make sure it doesn't escape from any other context.  */
7599   gcc_assert (ret != GS_UNHANDLED);
7600
7601   if (fallback == fb_none && *expr_p && !is_gimple_stmt (*expr_p))
7602     {
7603       /* We aren't looking for a value, and we don't have a valid
7604          statement.  If it doesn't have side-effects, throw it away.  */
7605       if (!TREE_SIDE_EFFECTS (*expr_p))
7606         *expr_p = NULL;
7607       else if (!TREE_THIS_VOLATILE (*expr_p))
7608         {
7609           /* This is probably a _REF that contains something nested that
7610              has side effects.  Recurse through the operands to find it.  */
7611           enum tree_code code = TREE_CODE (*expr_p);
7612
7613           switch (code)
7614             {
7615             case COMPONENT_REF:
7616             case REALPART_EXPR:
7617             case IMAGPART_EXPR:
7618             case VIEW_CONVERT_EXPR:
7619               gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
7620                              gimple_test_f, fallback);
7621               break;
7622
7623             case ARRAY_REF:
7624             case ARRAY_RANGE_REF:
7625               gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
7626                              gimple_test_f, fallback);
7627               gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
7628                              gimple_test_f, fallback);
7629               break;
7630
7631             default:
7632                /* Anything else with side-effects must be converted to
7633                   a valid statement before we get here.  */
7634               gcc_unreachable ();
7635             }
7636
7637           *expr_p = NULL;
7638         }
7639       else if (COMPLETE_TYPE_P (TREE_TYPE (*expr_p))
7640                && TYPE_MODE (TREE_TYPE (*expr_p)) != BLKmode)
7641         {
7642           /* Historically, the compiler has treated a bare reference
7643              to a non-BLKmode volatile lvalue as forcing a load.  */
7644           tree type = TYPE_MAIN_VARIANT (TREE_TYPE (*expr_p));
7645
7646           /* Normally, we do not want to create a temporary for a
7647              TREE_ADDRESSABLE type because such a type should not be
7648              copied by bitwise-assignment.  However, we make an
7649              exception here, as all we are doing here is ensuring that
7650              we read the bytes that make up the type.  We use
7651              create_tmp_var_raw because create_tmp_var will abort when
7652              given a TREE_ADDRESSABLE type.  */
7653           tree tmp = create_tmp_var_raw (type, "vol");
7654           gimple_add_tmp_var (tmp);
7655           gimplify_assign (tmp, *expr_p, pre_p);
7656           *expr_p = NULL;
7657         }
7658       else
7659         /* We can't do anything useful with a volatile reference to
7660            an incomplete type, so just throw it away.  Likewise for
7661            a BLKmode type, since any implicit inner load should
7662            already have been turned into an explicit one by the
7663            gimplification process.  */
7664         *expr_p = NULL;
7665     }
7666
7667   /* If we are gimplifying at the statement level, we're done.  Tack
7668      everything together and return.  */
7669   if (fallback == fb_none || is_statement)
7670     {
7671       /* Since *EXPR_P has been converted into a GIMPLE tuple, clear
7672          it out for GC to reclaim it.  */
7673       *expr_p = NULL_TREE;
7674
7675       if (!gimple_seq_empty_p (internal_pre)
7676           || !gimple_seq_empty_p (internal_post))
7677         {
7678           gimplify_seq_add_seq (&internal_pre, internal_post);
7679           gimplify_seq_add_seq (pre_p, internal_pre);
7680         }
7681
7682       /* The result of gimplifying *EXPR_P is going to be the last few
7683          statements in *PRE_P and *POST_P.  Add location information
7684          to all the statements that were added by the gimplification
7685          helpers.  */
7686       if (!gimple_seq_empty_p (*pre_p))
7687         annotate_all_with_location_after (*pre_p, pre_last_gsi, input_location);
7688
7689       if (!gimple_seq_empty_p (*post_p))
7690         annotate_all_with_location_after (*post_p, post_last_gsi,
7691                                           input_location);
7692
7693       goto out;
7694     }
7695
7696 #ifdef ENABLE_GIMPLE_CHECKING
7697   if (*expr_p)
7698     {
7699       enum tree_code code = TREE_CODE (*expr_p);
7700       /* These expressions should already be in gimple IR form.  */
7701       gcc_assert (code != MODIFY_EXPR
7702                   && code != ASM_EXPR
7703                   && code != BIND_EXPR
7704                   && code != CATCH_EXPR
7705                   && (code != COND_EXPR || gimplify_ctxp->allow_rhs_cond_expr)
7706                   && code != EH_FILTER_EXPR
7707                   && code != GOTO_EXPR
7708                   && code != LABEL_EXPR
7709                   && code != LOOP_EXPR
7710                   && code != SWITCH_EXPR
7711                   && code != TRY_FINALLY_EXPR
7712                   && code != OMP_CRITICAL
7713                   && code != OMP_FOR
7714                   && code != OMP_MASTER
7715                   && code != OMP_ORDERED
7716                   && code != OMP_PARALLEL
7717                   && code != OMP_SECTIONS
7718                   && code != OMP_SECTION
7719                   && code != OMP_SINGLE);
7720     }
7721 #endif
7722
7723   /* Otherwise we're gimplifying a subexpression, so the resulting
7724      value is interesting.  If it's a valid operand that matches
7725      GIMPLE_TEST_F, we're done. Unless we are handling some
7726      post-effects internally; if that's the case, we need to copy into
7727      a temporary before adding the post-effects to POST_P.  */
7728   if (gimple_seq_empty_p (internal_post) && (*gimple_test_f) (*expr_p))
7729     goto out;
7730
7731   /* Otherwise, we need to create a new temporary for the gimplified
7732      expression.  */
7733
7734   /* We can't return an lvalue if we have an internal postqueue.  The
7735      object the lvalue refers to would (probably) be modified by the
7736      postqueue; we need to copy the value out first, which means an
7737      rvalue.  */
7738   if ((fallback & fb_lvalue)
7739       && gimple_seq_empty_p (internal_post)
7740       && is_gimple_addressable (*expr_p))
7741     {
7742       /* An lvalue will do.  Take the address of the expression, store it
7743          in a temporary, and replace the expression with an INDIRECT_REF of
7744          that temporary.  */
7745       tmp = build_fold_addr_expr_loc (input_location, *expr_p);
7746       gimplify_expr (&tmp, pre_p, post_p, is_gimple_reg, fb_rvalue);
7747       *expr_p = build_simple_mem_ref (tmp);
7748     }
7749   else if ((fallback & fb_rvalue) && is_gimple_reg_rhs_or_call (*expr_p))
7750     {
7751       /* An rvalue will do.  Assign the gimplified expression into a
7752          new temporary TMP and replace the original expression with
7753          TMP.  First, make sure that the expression has a type so that
7754          it can be assigned into a temporary.  */
7755       gcc_assert (!VOID_TYPE_P (TREE_TYPE (*expr_p)));
7756
7757       if (!gimple_seq_empty_p (internal_post) || (fallback & fb_lvalue))
7758         /* The postqueue might change the value of the expression between
7759            the initialization and use of the temporary, so we can't use a
7760            formal temp.  FIXME do we care?  */
7761         {
7762           *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p);
7763           if (TREE_CODE (TREE_TYPE (*expr_p)) == COMPLEX_TYPE
7764               || TREE_CODE (TREE_TYPE (*expr_p)) == VECTOR_TYPE)
7765             DECL_GIMPLE_REG_P (*expr_p) = 1;
7766         }
7767       else
7768         *expr_p = get_formal_tmp_var (*expr_p, pre_p);
7769     }
7770   else
7771     {
7772 #ifdef ENABLE_GIMPLE_CHECKING
7773       if (!(fallback & fb_mayfail))
7774         {
7775           fprintf (stderr, "gimplification failed:\n");
7776           print_generic_expr (stderr, *expr_p, 0);
7777           debug_tree (*expr_p);
7778           internal_error ("gimplification failed");
7779         }
7780 #endif
7781       gcc_assert (fallback & fb_mayfail);
7782
7783       /* If this is an asm statement, and the user asked for the
7784          impossible, don't die.  Fail and let gimplify_asm_expr
7785          issue an error.  */
7786       ret = GS_ERROR;
7787       goto out;
7788     }
7789
7790   /* Make sure the temporary matches our predicate.  */
7791   gcc_assert ((*gimple_test_f) (*expr_p));
7792
7793   if (!gimple_seq_empty_p (internal_post))
7794     {
7795       annotate_all_with_location (internal_post, input_location);
7796       gimplify_seq_add_seq (pre_p, internal_post);
7797     }
7798
7799  out:
7800   input_location = saved_location;
7801   return ret;
7802 }
7803
7804 /* Look through TYPE for variable-sized objects and gimplify each such
7805    size that we find.  Add to LIST_P any statements generated.  */
7806
7807 void
7808 gimplify_type_sizes (tree type, gimple_seq *list_p)
7809 {
7810   tree field, t;
7811
7812   if (type == NULL || type == error_mark_node)
7813     return;
7814
7815   /* We first do the main variant, then copy into any other variants.  */
7816   type = TYPE_MAIN_VARIANT (type);
7817
7818   /* Avoid infinite recursion.  */
7819   if (TYPE_SIZES_GIMPLIFIED (type))
7820     return;
7821
7822   TYPE_SIZES_GIMPLIFIED (type) = 1;
7823
7824   switch (TREE_CODE (type))
7825     {
7826     case INTEGER_TYPE:
7827     case ENUMERAL_TYPE:
7828     case BOOLEAN_TYPE:
7829     case REAL_TYPE:
7830     case FIXED_POINT_TYPE:
7831       gimplify_one_sizepos (&TYPE_MIN_VALUE (type), list_p);
7832       gimplify_one_sizepos (&TYPE_MAX_VALUE (type), list_p);
7833
7834       for (t = TYPE_NEXT_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
7835         {
7836           TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (type);
7837           TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (type);
7838         }
7839       break;
7840
7841     case ARRAY_TYPE:
7842       /* These types may not have declarations, so handle them here.  */
7843       gimplify_type_sizes (TREE_TYPE (type), list_p);
7844       gimplify_type_sizes (TYPE_DOMAIN (type), list_p);
7845       /* Ensure VLA bounds aren't removed, for -O0 they should be variables
7846          with assigned stack slots, for -O1+ -g they should be tracked
7847          by VTA.  */
7848       if (!(TYPE_NAME (type)
7849             && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
7850             && DECL_IGNORED_P (TYPE_NAME (type)))
7851           && TYPE_DOMAIN (type)
7852           && INTEGRAL_TYPE_P (TYPE_DOMAIN (type)))
7853         {
7854           t = TYPE_MIN_VALUE (TYPE_DOMAIN (type));
7855           if (t && TREE_CODE (t) == VAR_DECL && DECL_ARTIFICIAL (t))
7856             DECL_IGNORED_P (t) = 0;
7857           t = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
7858           if (t && TREE_CODE (t) == VAR_DECL && DECL_ARTIFICIAL (t))
7859             DECL_IGNORED_P (t) = 0;
7860         }
7861       break;
7862
7863     case RECORD_TYPE:
7864     case UNION_TYPE:
7865     case QUAL_UNION_TYPE:
7866       for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
7867         if (TREE_CODE (field) == FIELD_DECL)
7868           {
7869             gimplify_one_sizepos (&DECL_FIELD_OFFSET (field), list_p);
7870             gimplify_one_sizepos (&DECL_SIZE (field), list_p);
7871             gimplify_one_sizepos (&DECL_SIZE_UNIT (field), list_p);
7872             gimplify_type_sizes (TREE_TYPE (field), list_p);
7873           }
7874       break;
7875
7876     case POINTER_TYPE:
7877     case REFERENCE_TYPE:
7878         /* We used to recurse on the pointed-to type here, which turned out to
7879            be incorrect because its definition might refer to variables not
7880            yet initialized at this point if a forward declaration is involved.
7881
7882            It was actually useful for anonymous pointed-to types to ensure
7883            that the sizes evaluation dominates every possible later use of the
7884            values.  Restricting to such types here would be safe since there
7885            is no possible forward declaration around, but would introduce an
7886            undesirable middle-end semantic to anonymity.  We then defer to
7887            front-ends the responsibility of ensuring that the sizes are
7888            evaluated both early and late enough, e.g. by attaching artificial
7889            type declarations to the tree.  */
7890       break;
7891
7892     default:
7893       break;
7894     }
7895
7896   gimplify_one_sizepos (&TYPE_SIZE (type), list_p);
7897   gimplify_one_sizepos (&TYPE_SIZE_UNIT (type), list_p);
7898
7899   for (t = TYPE_NEXT_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
7900     {
7901       TYPE_SIZE (t) = TYPE_SIZE (type);
7902       TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (type);
7903       TYPE_SIZES_GIMPLIFIED (t) = 1;
7904     }
7905 }
7906
7907 /* A subroutine of gimplify_type_sizes to make sure that *EXPR_P,
7908    a size or position, has had all of its SAVE_EXPRs evaluated.
7909    We add any required statements to *STMT_P.  */
7910
7911 void
7912 gimplify_one_sizepos (tree *expr_p, gimple_seq *stmt_p)
7913 {
7914   tree type, expr = *expr_p;
7915
7916   /* We don't do anything if the value isn't there, is constant, or contains
7917      A PLACEHOLDER_EXPR.  We also don't want to do anything if it's already
7918      a VAR_DECL.  If it's a VAR_DECL from another function, the gimplifier
7919      will want to replace it with a new variable, but that will cause problems
7920      if this type is from outside the function.  It's OK to have that here.  */
7921   if (expr == NULL_TREE || TREE_CONSTANT (expr)
7922       || TREE_CODE (expr) == VAR_DECL
7923       || CONTAINS_PLACEHOLDER_P (expr))
7924     return;
7925
7926   type = TREE_TYPE (expr);
7927   *expr_p = unshare_expr (expr);
7928
7929   gimplify_expr (expr_p, stmt_p, NULL, is_gimple_val, fb_rvalue);
7930   expr = *expr_p;
7931
7932   /* Verify that we've an exact type match with the original expression.
7933      In particular, we do not wish to drop a "sizetype" in favour of a
7934      type of similar dimensions.  We don't want to pollute the generic
7935      type-stripping code with this knowledge because it doesn't matter
7936      for the bulk of GENERIC/GIMPLE.  It only matters that TYPE_SIZE_UNIT
7937      and friends retain their "sizetype-ness".  */
7938   if (TREE_TYPE (expr) != type
7939       && TREE_CODE (type) == INTEGER_TYPE
7940       && TYPE_IS_SIZETYPE (type))
7941     {
7942       tree tmp;
7943       gimple stmt;
7944
7945       *expr_p = create_tmp_var (type, NULL);
7946       tmp = build1 (NOP_EXPR, type, expr);
7947       stmt = gimplify_assign (*expr_p, tmp, stmt_p);
7948       gimple_set_location (stmt, EXPR_LOC_OR_HERE (expr));
7949     }
7950 }
7951
7952 /* Gimplify the body of statements of FNDECL and return a GIMPLE_BIND node
7953    containing the sequence of corresponding GIMPLE statements.  If DO_PARMS
7954    is true, also gimplify the parameters.  */
7955
7956 gimple
7957 gimplify_body (tree fndecl, bool do_parms)
7958 {
7959   location_t saved_location = input_location;
7960   gimple_seq parm_stmts, seq;
7961   gimple outer_bind;
7962   struct gimplify_ctx gctx;
7963   struct cgraph_node *cgn;
7964
7965   timevar_push (TV_TREE_GIMPLIFY);
7966
7967   /* Initialize for optimize_insn_for_s{ize,peed}_p possibly called during
7968      gimplification.  */
7969   default_rtl_profile ();
7970
7971   gcc_assert (gimplify_ctxp == NULL);
7972   push_gimplify_context (&gctx);
7973
7974   /* Unshare most shared trees in the body and in that of any nested functions.
7975      It would seem we don't have to do this for nested functions because
7976      they are supposed to be output and then the outer function gimplified
7977      first, but the g++ front end doesn't always do it that way.  */
7978   unshare_body (fndecl);
7979   unvisit_body (fndecl);
7980
7981   cgn = cgraph_get_node (fndecl);
7982   if (cgn && cgn->origin)
7983     nonlocal_vlas = pointer_set_create ();
7984
7985   /* Make sure input_location isn't set to something weird.  */
7986   input_location = DECL_SOURCE_LOCATION (fndecl);
7987
7988   /* Resolve callee-copies.  This has to be done before processing
7989      the body so that DECL_VALUE_EXPR gets processed correctly.  */
7990   parm_stmts = do_parms ? gimplify_parameters () : NULL;
7991
7992   /* Gimplify the function's body.  */
7993   seq = NULL;
7994   gimplify_stmt (&DECL_SAVED_TREE (fndecl), &seq);
7995   outer_bind = gimple_seq_first_stmt (seq);
7996   if (!outer_bind)
7997     {
7998       outer_bind = gimple_build_nop ();
7999       gimplify_seq_add_stmt (&seq, outer_bind);
8000     }
8001
8002   /* The body must contain exactly one statement, a GIMPLE_BIND.  If this is
8003      not the case, wrap everything in a GIMPLE_BIND to make it so.  */
8004   if (gimple_code (outer_bind) == GIMPLE_BIND
8005       && gimple_seq_first (seq) == gimple_seq_last (seq))
8006     ;
8007   else
8008     outer_bind = gimple_build_bind (NULL_TREE, seq, NULL);
8009
8010   DECL_SAVED_TREE (fndecl) = NULL_TREE;
8011
8012   /* If we had callee-copies statements, insert them at the beginning
8013      of the function and clear DECL_VALUE_EXPR_P on the parameters.  */
8014   if (!gimple_seq_empty_p (parm_stmts))
8015     {
8016       tree parm;
8017
8018       gimplify_seq_add_seq (&parm_stmts, gimple_bind_body (outer_bind));
8019       gimple_bind_set_body (outer_bind, parm_stmts);
8020
8021       for (parm = DECL_ARGUMENTS (current_function_decl);
8022            parm; parm = DECL_CHAIN (parm))
8023         if (DECL_HAS_VALUE_EXPR_P (parm))
8024           {
8025             DECL_HAS_VALUE_EXPR_P (parm) = 0;
8026             DECL_IGNORED_P (parm) = 0;
8027           }
8028     }
8029
8030   if (nonlocal_vlas)
8031     {
8032       pointer_set_destroy (nonlocal_vlas);
8033       nonlocal_vlas = NULL;
8034     }
8035
8036   pop_gimplify_context (outer_bind);
8037   gcc_assert (gimplify_ctxp == NULL);
8038
8039   if (!seen_error ())
8040     verify_gimple_in_seq (gimple_bind_body (outer_bind));
8041
8042   timevar_pop (TV_TREE_GIMPLIFY);
8043   input_location = saved_location;
8044
8045   return outer_bind;
8046 }
8047
8048 typedef char *char_p; /* For DEF_VEC_P.  */
8049 DEF_VEC_P(char_p);
8050 DEF_VEC_ALLOC_P(char_p,heap);
8051
8052 /* Return whether we should exclude FNDECL from instrumentation.  */
8053
8054 static bool
8055 flag_instrument_functions_exclude_p (tree fndecl)
8056 {
8057   VEC(char_p,heap) *vec;
8058
8059   vec = (VEC(char_p,heap) *) flag_instrument_functions_exclude_functions;
8060   if (VEC_length (char_p, vec) > 0)
8061     {
8062       const char *name;
8063       int i;
8064       char *s;
8065
8066       name = lang_hooks.decl_printable_name (fndecl, 0);
8067       FOR_EACH_VEC_ELT (char_p, vec, i, s)
8068         if (strstr (name, s) != NULL)
8069           return true;
8070     }
8071
8072   vec = (VEC(char_p,heap) *) flag_instrument_functions_exclude_files;
8073   if (VEC_length (char_p, vec) > 0)
8074     {
8075       const char *name;
8076       int i;
8077       char *s;
8078
8079       name = DECL_SOURCE_FILE (fndecl);
8080       FOR_EACH_VEC_ELT (char_p, vec, i, s)
8081         if (strstr (name, s) != NULL)
8082           return true;
8083     }
8084
8085   return false;
8086 }
8087
8088 /* Entry point to the gimplification pass.  FNDECL is the FUNCTION_DECL
8089    node for the function we want to gimplify.
8090
8091    Return the sequence of GIMPLE statements corresponding to the body
8092    of FNDECL.  */
8093
8094 void
8095 gimplify_function_tree (tree fndecl)
8096 {
8097   tree oldfn, parm, ret;
8098   gimple_seq seq;
8099   gimple bind;
8100
8101   gcc_assert (!gimple_body (fndecl));
8102
8103   oldfn = current_function_decl;
8104   current_function_decl = fndecl;
8105   if (DECL_STRUCT_FUNCTION (fndecl))
8106     push_cfun (DECL_STRUCT_FUNCTION (fndecl));
8107   else
8108     push_struct_function (fndecl);
8109
8110   for (parm = DECL_ARGUMENTS (fndecl); parm ; parm = DECL_CHAIN (parm))
8111     {
8112       /* Preliminarily mark non-addressed complex variables as eligible
8113          for promotion to gimple registers.  We'll transform their uses
8114          as we find them.  */
8115       if ((TREE_CODE (TREE_TYPE (parm)) == COMPLEX_TYPE
8116            || TREE_CODE (TREE_TYPE (parm)) == VECTOR_TYPE)
8117           && !TREE_THIS_VOLATILE (parm)
8118           && !needs_to_live_in_memory (parm))
8119         DECL_GIMPLE_REG_P (parm) = 1;
8120     }
8121
8122   ret = DECL_RESULT (fndecl);
8123   if ((TREE_CODE (TREE_TYPE (ret)) == COMPLEX_TYPE
8124        || TREE_CODE (TREE_TYPE (ret)) == VECTOR_TYPE)
8125       && !needs_to_live_in_memory (ret))
8126     DECL_GIMPLE_REG_P (ret) = 1;
8127
8128   bind = gimplify_body (fndecl, true);
8129
8130   /* The tree body of the function is no longer needed, replace it
8131      with the new GIMPLE body.  */
8132   seq = gimple_seq_alloc ();
8133   gimple_seq_add_stmt (&seq, bind);
8134   gimple_set_body (fndecl, seq);
8135
8136   /* If we're instrumenting function entry/exit, then prepend the call to
8137      the entry hook and wrap the whole function in a TRY_FINALLY_EXPR to
8138      catch the exit hook.  */
8139   /* ??? Add some way to ignore exceptions for this TFE.  */
8140   if (flag_instrument_function_entry_exit
8141       && !DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (fndecl)
8142       && !flag_instrument_functions_exclude_p (fndecl))
8143     {
8144       tree x;
8145       gimple new_bind;
8146       gimple tf;
8147       gimple_seq cleanup = NULL, body = NULL;
8148       tree tmp_var;
8149       gimple call;
8150
8151       x = builtin_decl_implicit (BUILT_IN_RETURN_ADDRESS);
8152       call = gimple_build_call (x, 1, integer_zero_node);
8153       tmp_var = create_tmp_var (ptr_type_node, "return_addr");
8154       gimple_call_set_lhs (call, tmp_var);
8155       gimplify_seq_add_stmt (&cleanup, call);
8156       x = builtin_decl_implicit (BUILT_IN_PROFILE_FUNC_EXIT);
8157       call = gimple_build_call (x, 2,
8158                                 build_fold_addr_expr (current_function_decl),
8159                                 tmp_var);
8160       gimplify_seq_add_stmt (&cleanup, call);
8161       tf = gimple_build_try (seq, cleanup, GIMPLE_TRY_FINALLY);
8162
8163       x = builtin_decl_implicit (BUILT_IN_RETURN_ADDRESS);
8164       call = gimple_build_call (x, 1, integer_zero_node);
8165       tmp_var = create_tmp_var (ptr_type_node, "return_addr");
8166       gimple_call_set_lhs (call, tmp_var);
8167       gimplify_seq_add_stmt (&body, call);
8168       x = builtin_decl_implicit (BUILT_IN_PROFILE_FUNC_ENTER);
8169       call = gimple_build_call (x, 2,
8170                                 build_fold_addr_expr (current_function_decl),
8171                                 tmp_var);
8172       gimplify_seq_add_stmt (&body, call);
8173       gimplify_seq_add_stmt (&body, tf);
8174       new_bind = gimple_build_bind (NULL, body, gimple_bind_block (bind));
8175       /* Clear the block for BIND, since it is no longer directly inside
8176          the function, but within a try block.  */
8177       gimple_bind_set_block (bind, NULL);
8178
8179       /* Replace the current function body with the body
8180          wrapped in the try/finally TF.  */
8181       seq = gimple_seq_alloc ();
8182       gimple_seq_add_stmt (&seq, new_bind);
8183       gimple_set_body (fndecl, seq);
8184     }
8185
8186   DECL_SAVED_TREE (fndecl) = NULL_TREE;
8187   cfun->curr_properties = PROP_gimple_any;
8188
8189   current_function_decl = oldfn;
8190   pop_cfun ();
8191 }
8192
8193 /* Some transformations like inlining may invalidate the GIMPLE form
8194    for operands.  This function traverses all the operands in STMT and
8195    gimplifies anything that is not a valid gimple operand.  Any new
8196    GIMPLE statements are inserted before *GSI_P.  */
8197
8198 void
8199 gimple_regimplify_operands (gimple stmt, gimple_stmt_iterator *gsi_p)
8200 {
8201   size_t i, num_ops;
8202   tree orig_lhs = NULL_TREE, lhs, t;
8203   gimple_seq pre = NULL;
8204   gimple post_stmt = NULL;
8205   struct gimplify_ctx gctx;
8206
8207   push_gimplify_context (&gctx);
8208   gimplify_ctxp->into_ssa = gimple_in_ssa_p (cfun);
8209
8210   switch (gimple_code (stmt))
8211     {
8212     case GIMPLE_COND:
8213       gimplify_expr (gimple_cond_lhs_ptr (stmt), &pre, NULL,
8214                      is_gimple_val, fb_rvalue);
8215       gimplify_expr (gimple_cond_rhs_ptr (stmt), &pre, NULL,
8216                      is_gimple_val, fb_rvalue);
8217       break;
8218     case GIMPLE_SWITCH:
8219       gimplify_expr (gimple_switch_index_ptr (stmt), &pre, NULL,
8220                      is_gimple_val, fb_rvalue);
8221       break;
8222     case GIMPLE_OMP_ATOMIC_LOAD:
8223       gimplify_expr (gimple_omp_atomic_load_rhs_ptr (stmt), &pre, NULL,
8224                      is_gimple_val, fb_rvalue);
8225       break;
8226     case GIMPLE_ASM:
8227       {
8228         size_t i, noutputs = gimple_asm_noutputs (stmt);
8229         const char *constraint, **oconstraints;
8230         bool allows_mem, allows_reg, is_inout;
8231
8232         oconstraints
8233           = (const char **) alloca ((noutputs) * sizeof (const char *));
8234         for (i = 0; i < noutputs; i++)
8235           {
8236             tree op = gimple_asm_output_op (stmt, i);
8237             constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (op)));
8238             oconstraints[i] = constraint;
8239             parse_output_constraint (&constraint, i, 0, 0, &allows_mem,
8240                                      &allows_reg, &is_inout);
8241             gimplify_expr (&TREE_VALUE (op), &pre, NULL,
8242                            is_inout ? is_gimple_min_lval : is_gimple_lvalue,
8243                            fb_lvalue | fb_mayfail);
8244           }
8245         for (i = 0; i < gimple_asm_ninputs (stmt); i++)
8246           {
8247             tree op = gimple_asm_input_op (stmt, i);
8248             constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (op)));
8249             parse_input_constraint (&constraint, 0, 0, noutputs, 0,
8250                                     oconstraints, &allows_mem, &allows_reg);
8251             if (TREE_ADDRESSABLE (TREE_TYPE (TREE_VALUE (op))) && allows_mem)
8252               allows_reg = 0;
8253             if (!allows_reg && allows_mem)
8254               gimplify_expr (&TREE_VALUE (op), &pre, NULL,
8255                              is_gimple_lvalue, fb_lvalue | fb_mayfail);
8256             else
8257               gimplify_expr (&TREE_VALUE (op), &pre, NULL,
8258                              is_gimple_asm_val, fb_rvalue);
8259           }
8260       }
8261       break;
8262     default:
8263       /* NOTE: We start gimplifying operands from last to first to
8264          make sure that side-effects on the RHS of calls, assignments
8265          and ASMs are executed before the LHS.  The ordering is not
8266          important for other statements.  */
8267       num_ops = gimple_num_ops (stmt);
8268       orig_lhs = gimple_get_lhs (stmt);
8269       for (i = num_ops; i > 0; i--)
8270         {
8271           tree op = gimple_op (stmt, i - 1);
8272           if (op == NULL_TREE)
8273             continue;
8274           if (i == 1 && (is_gimple_call (stmt) || is_gimple_assign (stmt)))
8275             gimplify_expr (&op, &pre, NULL, is_gimple_lvalue, fb_lvalue);
8276           else if (i == 2
8277                    && is_gimple_assign (stmt)
8278                    && num_ops == 2
8279                    && get_gimple_rhs_class (gimple_expr_code (stmt))
8280                       == GIMPLE_SINGLE_RHS)
8281             gimplify_expr (&op, &pre, NULL,
8282                            rhs_predicate_for (gimple_assign_lhs (stmt)),
8283                            fb_rvalue);
8284           else if (i == 2 && is_gimple_call (stmt))
8285             {
8286               if (TREE_CODE (op) == FUNCTION_DECL)
8287                 continue;
8288               gimplify_expr (&op, &pre, NULL, is_gimple_call_addr, fb_rvalue);
8289             }
8290           else
8291             gimplify_expr (&op, &pre, NULL, is_gimple_val, fb_rvalue);
8292           gimple_set_op (stmt, i - 1, op);
8293         }
8294
8295       lhs = gimple_get_lhs (stmt);
8296       /* If the LHS changed it in a way that requires a simple RHS,
8297          create temporary.  */
8298       if (lhs && !is_gimple_reg (lhs))
8299         {
8300           bool need_temp = false;
8301
8302           if (is_gimple_assign (stmt)
8303               && num_ops == 2
8304               && get_gimple_rhs_class (gimple_expr_code (stmt))
8305                  == GIMPLE_SINGLE_RHS)
8306             gimplify_expr (gimple_assign_rhs1_ptr (stmt), &pre, NULL,
8307                            rhs_predicate_for (gimple_assign_lhs (stmt)),
8308                            fb_rvalue);
8309           else if (is_gimple_reg (lhs))
8310             {
8311               if (is_gimple_reg_type (TREE_TYPE (lhs)))
8312                 {
8313                   if (is_gimple_call (stmt))
8314                     {
8315                       i = gimple_call_flags (stmt);
8316                       if ((i & ECF_LOOPING_CONST_OR_PURE)
8317                           || !(i & (ECF_CONST | ECF_PURE)))
8318                         need_temp = true;
8319                     }
8320                   if (stmt_can_throw_internal (stmt))
8321                     need_temp = true;
8322                 }
8323             }
8324           else
8325             {
8326               if (is_gimple_reg_type (TREE_TYPE (lhs)))
8327                 need_temp = true;
8328               else if (TYPE_MODE (TREE_TYPE (lhs)) != BLKmode)
8329                 {
8330                   if (is_gimple_call (stmt))
8331                     {
8332                       tree fndecl = gimple_call_fndecl (stmt);
8333
8334                       if (!aggregate_value_p (TREE_TYPE (lhs), fndecl)
8335                           && !(fndecl && DECL_RESULT (fndecl)
8336                                && DECL_BY_REFERENCE (DECL_RESULT (fndecl))))
8337                         need_temp = true;
8338                     }
8339                   else
8340                     need_temp = true;
8341                 }
8342             }
8343           if (need_temp)
8344             {
8345               tree temp = create_tmp_reg (TREE_TYPE (lhs), NULL);
8346
8347               if (TREE_CODE (orig_lhs) == SSA_NAME)
8348                 orig_lhs = SSA_NAME_VAR (orig_lhs);
8349
8350               if (gimple_in_ssa_p (cfun))
8351                 temp = make_ssa_name (temp, NULL);
8352               gimple_set_lhs (stmt, temp);
8353               post_stmt = gimple_build_assign (lhs, temp);
8354               if (TREE_CODE (lhs) == SSA_NAME)
8355                 SSA_NAME_DEF_STMT (lhs) = post_stmt;
8356             }
8357         }
8358       break;
8359     }
8360
8361   if (gimple_referenced_vars (cfun))
8362     for (t = gimplify_ctxp->temps; t ; t = TREE_CHAIN (t))
8363       add_referenced_var (t);
8364
8365   if (!gimple_seq_empty_p (pre))
8366     {
8367       if (gimple_in_ssa_p (cfun))
8368         {
8369           gimple_stmt_iterator i;
8370
8371           for (i = gsi_start (pre); !gsi_end_p (i); gsi_next (&i))
8372             mark_symbols_for_renaming (gsi_stmt (i));
8373         }
8374       gsi_insert_seq_before (gsi_p, pre, GSI_SAME_STMT);
8375     }
8376   if (post_stmt)
8377     gsi_insert_after (gsi_p, post_stmt, GSI_NEW_STMT);
8378
8379   pop_gimplify_context (NULL);
8380 }
8381
8382 /* Expand EXPR to list of gimple statements STMTS.  GIMPLE_TEST_F specifies
8383    the predicate that will hold for the result.  If VAR is not NULL, make the
8384    base variable of the final destination be VAR if suitable.  */
8385
8386 tree
8387 force_gimple_operand_1 (tree expr, gimple_seq *stmts,
8388                         gimple_predicate gimple_test_f, tree var)
8389 {
8390   tree t;
8391   enum gimplify_status ret;
8392   struct gimplify_ctx gctx;
8393
8394   *stmts = NULL;
8395
8396   /* gimple_test_f might be more strict than is_gimple_val, make
8397      sure we pass both.  Just checking gimple_test_f doesn't work
8398      because most gimple predicates do not work recursively.  */
8399   if (is_gimple_val (expr)
8400       && (*gimple_test_f) (expr))
8401     return expr;
8402
8403   push_gimplify_context (&gctx);
8404   gimplify_ctxp->into_ssa = gimple_in_ssa_p (cfun);
8405   gimplify_ctxp->allow_rhs_cond_expr = true;
8406
8407   if (var)
8408     expr = build2 (MODIFY_EXPR, TREE_TYPE (var), var, expr);
8409
8410   if (TREE_CODE (expr) != MODIFY_EXPR
8411       && TREE_TYPE (expr) == void_type_node)
8412     {
8413       gimplify_and_add (expr, stmts);
8414       expr = NULL_TREE;
8415     }
8416   else
8417     {
8418       ret = gimplify_expr (&expr, stmts, NULL, gimple_test_f, fb_rvalue);
8419       gcc_assert (ret != GS_ERROR);
8420     }
8421
8422   if (gimple_referenced_vars (cfun))
8423     for (t = gimplify_ctxp->temps; t ; t = DECL_CHAIN (t))
8424       add_referenced_var (t);
8425
8426   pop_gimplify_context (NULL);
8427
8428   return expr;
8429 }
8430
8431 /* Expand EXPR to list of gimple statements STMTS.  If SIMPLE is true,
8432    force the result to be either ssa_name or an invariant, otherwise
8433    just force it to be a rhs expression.  If VAR is not NULL, make the
8434    base variable of the final destination be VAR if suitable.  */
8435
8436 tree
8437 force_gimple_operand (tree expr, gimple_seq *stmts, bool simple, tree var)
8438 {
8439   return force_gimple_operand_1 (expr, stmts,
8440                                  simple ? is_gimple_val : is_gimple_reg_rhs,
8441                                  var);
8442 }
8443
8444 /* Invoke force_gimple_operand_1 for EXPR with parameters GIMPLE_TEST_F
8445    and VAR.  If some statements are produced, emits them at GSI.
8446    If BEFORE is true.  the statements are appended before GSI, otherwise
8447    they are appended after it.  M specifies the way GSI moves after
8448    insertion (GSI_SAME_STMT or GSI_CONTINUE_LINKING are the usual values).  */
8449
8450 tree
8451 force_gimple_operand_gsi_1 (gimple_stmt_iterator *gsi, tree expr,
8452                             gimple_predicate gimple_test_f,
8453                             tree var, bool before,
8454                             enum gsi_iterator_update m)
8455 {
8456   gimple_seq stmts;
8457
8458   expr = force_gimple_operand_1 (expr, &stmts, gimple_test_f, var);
8459
8460   if (!gimple_seq_empty_p (stmts))
8461     {
8462       if (gimple_in_ssa_p (cfun))
8463         {
8464           gimple_stmt_iterator i;
8465
8466           for (i = gsi_start (stmts); !gsi_end_p (i); gsi_next (&i))
8467             mark_symbols_for_renaming (gsi_stmt (i));
8468         }
8469
8470       if (before)
8471         gsi_insert_seq_before (gsi, stmts, m);
8472       else
8473         gsi_insert_seq_after (gsi, stmts, m);
8474     }
8475
8476   return expr;
8477 }
8478
8479 /* Invoke force_gimple_operand_1 for EXPR with parameter VAR.
8480    If SIMPLE is true, force the result to be either ssa_name or an invariant,
8481    otherwise just force it to be a rhs expression.  If some statements are
8482    produced, emits them at GSI.  If BEFORE is true, the statements are
8483    appended before GSI, otherwise they are appended after it.  M specifies
8484    the way GSI moves after insertion (GSI_SAME_STMT or GSI_CONTINUE_LINKING
8485    are the usual values).  */
8486
8487 tree
8488 force_gimple_operand_gsi (gimple_stmt_iterator *gsi, tree expr,
8489                           bool simple_p, tree var, bool before,
8490                           enum gsi_iterator_update m)
8491 {
8492   return force_gimple_operand_gsi_1 (gsi, expr,
8493                                      simple_p
8494                                      ? is_gimple_val : is_gimple_reg_rhs,
8495                                      var, before, m);
8496 }
8497
8498
8499 #include "gt-gimplify.h"