OSDN Git Service

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