OSDN Git Service

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