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