OSDN Git Service

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