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