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