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