cp-gimplify.c (genericize_cp_loop): Use LOOP_EXPR.
[platform/upstream/gcc.git] / gcc / cp / cp-gimplify.c
1 /* C++-specific tree lowering bits; see also c-gimplify.c and tree-gimple.c.
2
3    Copyright (C) 2002-2014 Free Software Foundation, Inc.
4    Contributed by Jason Merrill <jason@redhat.com>
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
12
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3.  If not see
20 <http://www.gnu.org/licenses/>.  */
21
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "tree.h"
27 #include "stor-layout.h"
28 #include "cp-tree.h"
29 #include "c-family/c-common.h"
30 #include "tree-iterator.h"
31 #include "predict.h"
32 #include "vec.h"
33 #include "hashtab.h"
34 #include "hash-set.h"
35 #include "machmode.h"
36 #include "hard-reg-set.h"
37 #include "input.h"
38 #include "function.h"
39 #include "basic-block.h"
40 #include "tree-ssa-alias.h"
41 #include "internal-fn.h"
42 #include "gimple-expr.h"
43 #include "is-a.h"
44 #include "gimple.h"
45 #include "gimplify.h"
46 #include "flags.h"
47 #include "splay-tree.h"
48 #include "target.h"
49 #include "c-family/c-ubsan.h"
50 #include "cilk.h"
51
52 /* Forward declarations.  */
53
54 static tree cp_genericize_r (tree *, int *, void *);
55 static void cp_genericize_tree (tree*);
56
57 /* Local declarations.  */
58
59 enum bc_t { bc_break = 0, bc_continue = 1 };
60
61 /* Stack of labels which are targets for "break" or "continue",
62    linked through TREE_CHAIN.  */
63 static tree bc_label[2];
64
65 /* Begin a scope which can be exited by a break or continue statement.  BC
66    indicates which.
67
68    Just creates a label with location LOCATION and pushes it into the current
69    context.  */
70
71 static tree
72 begin_bc_block (enum bc_t bc, location_t location)
73 {
74   tree label = create_artificial_label (location);
75   DECL_CHAIN (label) = bc_label[bc];
76   bc_label[bc] = label;
77   return label;
78 }
79
80 /* Finish a scope which can be exited by a break or continue statement.
81    LABEL was returned from the most recent call to begin_bc_block.  BLOCK is
82    an expression for the contents of the scope.
83
84    If we saw a break (or continue) in the scope, append a LABEL_EXPR to
85    BLOCK.  Otherwise, just forget the label.  */
86
87 static void
88 finish_bc_block (tree *block, enum bc_t bc, tree label)
89 {
90   gcc_assert (label == bc_label[bc]);
91
92   if (TREE_USED (label))
93     append_to_statement_list (build1 (LABEL_EXPR, void_type_node, label),
94                               block);
95
96   bc_label[bc] = DECL_CHAIN (label);
97   DECL_CHAIN (label) = NULL_TREE;
98 }
99
100 /* Get the LABEL_EXPR to represent a break or continue statement
101    in the current block scope.  BC indicates which.  */
102
103 static tree
104 get_bc_label (enum bc_t bc)
105 {
106   tree label = bc_label[bc];
107
108   /* Mark the label used for finish_bc_block.  */
109   TREE_USED (label) = 1;
110   return label;
111 }
112
113 /* Genericize a TRY_BLOCK.  */
114
115 static void
116 genericize_try_block (tree *stmt_p)
117 {
118   tree body = TRY_STMTS (*stmt_p);
119   tree cleanup = TRY_HANDLERS (*stmt_p);
120
121   *stmt_p = build2 (TRY_CATCH_EXPR, void_type_node, body, cleanup);
122 }
123
124 /* Genericize a HANDLER by converting to a CATCH_EXPR.  */
125
126 static void
127 genericize_catch_block (tree *stmt_p)
128 {
129   tree type = HANDLER_TYPE (*stmt_p);
130   tree body = HANDLER_BODY (*stmt_p);
131
132   /* FIXME should the caught type go in TREE_TYPE?  */
133   *stmt_p = build2 (CATCH_EXPR, void_type_node, type, body);
134 }
135
136 /* A terser interface for building a representation of an exception
137    specification.  */
138
139 static tree
140 build_gimple_eh_filter_tree (tree body, tree allowed, tree failure)
141 {
142   tree t;
143
144   /* FIXME should the allowed types go in TREE_TYPE?  */
145   t = build2 (EH_FILTER_EXPR, void_type_node, allowed, NULL_TREE);
146   append_to_statement_list (failure, &EH_FILTER_FAILURE (t));
147
148   t = build2 (TRY_CATCH_EXPR, void_type_node, NULL_TREE, t);
149   append_to_statement_list (body, &TREE_OPERAND (t, 0));
150
151   return t;
152 }
153
154 /* Genericize an EH_SPEC_BLOCK by converting it to a
155    TRY_CATCH_EXPR/EH_FILTER_EXPR pair.  */
156
157 static void
158 genericize_eh_spec_block (tree *stmt_p)
159 {
160   tree body = EH_SPEC_STMTS (*stmt_p);
161   tree allowed = EH_SPEC_RAISES (*stmt_p);
162   tree failure = build_call_n (call_unexpected_node, 1, build_exc_ptr ());
163
164   *stmt_p = build_gimple_eh_filter_tree (body, allowed, failure);
165   TREE_NO_WARNING (*stmt_p) = true;
166   TREE_NO_WARNING (TREE_OPERAND (*stmt_p, 1)) = true;
167 }
168
169 /* Genericize an IF_STMT by turning it into a COND_EXPR.  */
170
171 static void
172 genericize_if_stmt (tree *stmt_p)
173 {
174   tree stmt, cond, then_, else_;
175   location_t locus = EXPR_LOCATION (*stmt_p);
176
177   stmt = *stmt_p;
178   cond = IF_COND (stmt);
179   then_ = THEN_CLAUSE (stmt);
180   else_ = ELSE_CLAUSE (stmt);
181
182   if (!then_)
183     then_ = build_empty_stmt (locus);
184   if (!else_)
185     else_ = build_empty_stmt (locus);
186
187   if (integer_nonzerop (cond) && !TREE_SIDE_EFFECTS (else_))
188     stmt = then_;
189   else if (integer_zerop (cond) && !TREE_SIDE_EFFECTS (then_))
190     stmt = else_;
191   else
192     stmt = build3 (COND_EXPR, void_type_node, cond, then_, else_);
193   if (CAN_HAVE_LOCATION_P (stmt) && !EXPR_HAS_LOCATION (stmt))
194     SET_EXPR_LOCATION (stmt, locus);
195   *stmt_p = stmt;
196 }
197
198 /* Build a generic representation of one of the C loop forms.  COND is the
199    loop condition or NULL_TREE.  BODY is the (possibly compound) statement
200    controlled by the loop.  INCR is the increment expression of a for-loop,
201    or NULL_TREE.  COND_IS_FIRST indicates whether the condition is
202    evaluated before the loop body as in while and for loops, or after the
203    loop body as in do-while loops.  */
204
205 static void
206 genericize_cp_loop (tree *stmt_p, location_t start_locus, tree cond, tree body,
207                     tree incr, bool cond_is_first, int *walk_subtrees,
208                     void *data)
209 {
210   tree blab, clab;
211   tree exit = NULL;
212   tree stmt_list = NULL;
213
214   blab = begin_bc_block (bc_break, start_locus);
215   clab = begin_bc_block (bc_continue, start_locus);
216
217   if (incr && EXPR_P (incr))
218     SET_EXPR_LOCATION (incr, start_locus);
219
220   cp_walk_tree (&cond, cp_genericize_r, data, NULL);
221   cp_walk_tree (&body, cp_genericize_r, data, NULL);
222   cp_walk_tree (&incr, cp_genericize_r, data, NULL);
223   *walk_subtrees = 0;
224
225   if (cond && TREE_CODE (cond) != INTEGER_CST)
226     {
227       /* If COND is constant, don't bother building an exit.  If it's false,
228          we won't build a loop.  If it's true, any exits are in the body.  */
229       location_t cloc = EXPR_LOC_OR_LOC (cond, start_locus);
230       exit = build1_loc (cloc, GOTO_EXPR, void_type_node,
231                          get_bc_label (bc_break));
232       exit = fold_build3_loc (cloc, COND_EXPR, void_type_node, cond,
233                               build_empty_stmt (cloc), exit);
234     }
235
236   if (exit && cond_is_first)
237     append_to_statement_list (exit, &stmt_list);
238   append_to_statement_list (body, &stmt_list);
239   finish_bc_block (&stmt_list, bc_continue, clab);
240   append_to_statement_list (incr, &stmt_list);
241   if (exit && !cond_is_first)
242     append_to_statement_list (exit, &stmt_list);
243
244   if (!stmt_list)
245     stmt_list = build_empty_stmt (start_locus);
246
247   tree loop;
248   if (cond && integer_zerop (cond))
249     {
250       if (cond_is_first)
251         loop = fold_build3_loc (start_locus, COND_EXPR,
252                                 void_type_node, cond, stmt_list,
253                                 build_empty_stmt (start_locus));
254       else
255         loop = stmt_list;
256     }
257   else
258     loop = build1_loc (start_locus, LOOP_EXPR, void_type_node, stmt_list);
259
260   stmt_list = NULL;
261   append_to_statement_list (loop, &stmt_list);
262   finish_bc_block (&stmt_list, bc_break, blab);
263   if (!stmt_list)
264     stmt_list = build_empty_stmt (start_locus);
265
266   *stmt_p = stmt_list;
267 }
268
269 /* Genericize a FOR_STMT node *STMT_P.  */
270
271 static void
272 genericize_for_stmt (tree *stmt_p, int *walk_subtrees, void *data)
273 {
274   tree stmt = *stmt_p;
275   tree expr = NULL;
276   tree loop;
277   tree init = FOR_INIT_STMT (stmt);
278
279   if (init)
280     {
281       cp_walk_tree (&init, cp_genericize_r, data, NULL);
282       append_to_statement_list (init, &expr);
283     }
284
285   genericize_cp_loop (&loop, EXPR_LOCATION (stmt), FOR_COND (stmt),
286                       FOR_BODY (stmt), FOR_EXPR (stmt), 1, walk_subtrees, data);
287   append_to_statement_list (loop, &expr);
288   if (expr == NULL_TREE)
289     expr = loop;
290   *stmt_p = expr;
291 }
292
293 /* Genericize a WHILE_STMT node *STMT_P.  */
294
295 static void
296 genericize_while_stmt (tree *stmt_p, int *walk_subtrees, void *data)
297 {
298   tree stmt = *stmt_p;
299   genericize_cp_loop (stmt_p, EXPR_LOCATION (stmt), WHILE_COND (stmt),
300                       WHILE_BODY (stmt), NULL_TREE, 1, walk_subtrees, data);
301 }
302
303 /* Genericize a DO_STMT node *STMT_P.  */
304
305 static void
306 genericize_do_stmt (tree *stmt_p, int *walk_subtrees, void *data)
307 {
308   tree stmt = *stmt_p;
309   genericize_cp_loop (stmt_p, EXPR_LOCATION (stmt), DO_COND (stmt),
310                       DO_BODY (stmt), NULL_TREE, 0, walk_subtrees, data);
311 }
312
313 /* Genericize a SWITCH_STMT node *STMT_P by turning it into a SWITCH_EXPR.  */
314
315 static void
316 genericize_switch_stmt (tree *stmt_p, int *walk_subtrees, void *data)
317 {
318   tree stmt = *stmt_p;
319   tree break_block, body, cond, type;
320   location_t stmt_locus = EXPR_LOCATION (stmt);
321
322   break_block = begin_bc_block (bc_break, stmt_locus);
323
324   body = SWITCH_STMT_BODY (stmt);
325   if (!body)
326     body = build_empty_stmt (stmt_locus);
327   cond = SWITCH_STMT_COND (stmt);
328   type = SWITCH_STMT_TYPE (stmt);
329
330   cp_walk_tree (&body, cp_genericize_r, data, NULL);
331   cp_walk_tree (&cond, cp_genericize_r, data, NULL);
332   cp_walk_tree (&type, cp_genericize_r, data, NULL);
333   *walk_subtrees = 0;
334
335   *stmt_p = build3_loc (stmt_locus, SWITCH_EXPR, type, cond, body, NULL_TREE);
336   finish_bc_block (stmt_p, bc_break, break_block);
337 }
338
339 /* Genericize a CONTINUE_STMT node *STMT_P.  */
340
341 static void
342 genericize_continue_stmt (tree *stmt_p)
343 {
344   tree stmt_list = NULL;
345   tree pred = build_predict_expr (PRED_CONTINUE, NOT_TAKEN);
346   tree label = get_bc_label (bc_continue);
347   location_t location = EXPR_LOCATION (*stmt_p);
348   tree jump = build1_loc (location, GOTO_EXPR, void_type_node, label);
349   append_to_statement_list (pred, &stmt_list);
350   append_to_statement_list (jump, &stmt_list);
351   *stmt_p = stmt_list;
352 }
353
354 /* Genericize a BREAK_STMT node *STMT_P.  */
355
356 static void
357 genericize_break_stmt (tree *stmt_p)
358 {
359   tree label = get_bc_label (bc_break);
360   location_t location = EXPR_LOCATION (*stmt_p);
361   *stmt_p = build1_loc (location, GOTO_EXPR, void_type_node, label);
362 }
363
364 /* Genericize a OMP_FOR node *STMT_P.  */
365
366 static void
367 genericize_omp_for_stmt (tree *stmt_p, int *walk_subtrees, void *data)
368 {
369   tree stmt = *stmt_p;
370   location_t locus = EXPR_LOCATION (stmt);
371   tree clab = begin_bc_block (bc_continue, locus);
372
373   cp_walk_tree (&OMP_FOR_BODY (stmt), cp_genericize_r, data, NULL);
374   cp_walk_tree (&OMP_FOR_CLAUSES (stmt), cp_genericize_r, data, NULL);
375   cp_walk_tree (&OMP_FOR_INIT (stmt), cp_genericize_r, data, NULL);
376   cp_walk_tree (&OMP_FOR_COND (stmt), cp_genericize_r, data, NULL);
377   cp_walk_tree (&OMP_FOR_INCR (stmt), cp_genericize_r, data, NULL);
378   cp_walk_tree (&OMP_FOR_PRE_BODY (stmt), cp_genericize_r, data, NULL);
379   *walk_subtrees = 0;
380
381   finish_bc_block (&OMP_FOR_BODY (stmt), bc_continue, clab);
382 }
383
384 /* Hook into the middle of gimplifying an OMP_FOR node.  */
385
386 static enum gimplify_status
387 cp_gimplify_omp_for (tree *expr_p, gimple_seq *pre_p)
388 {
389   tree for_stmt = *expr_p;
390   gimple_seq seq = NULL;
391
392   /* Protect ourselves from recursion.  */
393   if (OMP_FOR_GIMPLIFYING_P (for_stmt))
394     return GS_UNHANDLED;
395   OMP_FOR_GIMPLIFYING_P (for_stmt) = 1;
396
397   gimplify_and_add (for_stmt, &seq);
398   gimple_seq_add_seq (pre_p, seq);
399
400   OMP_FOR_GIMPLIFYING_P (for_stmt) = 0;
401
402   return GS_ALL_DONE;
403 }
404
405 /*  Gimplify an EXPR_STMT node.  */
406
407 static void
408 gimplify_expr_stmt (tree *stmt_p)
409 {
410   tree stmt = EXPR_STMT_EXPR (*stmt_p);
411
412   if (stmt == error_mark_node)
413     stmt = NULL;
414
415   /* Gimplification of a statement expression will nullify the
416      statement if all its side effects are moved to *PRE_P and *POST_P.
417
418      In this case we will not want to emit the gimplified statement.
419      However, we may still want to emit a warning, so we do that before
420      gimplification.  */
421   if (stmt && warn_unused_value)
422     {
423       if (!TREE_SIDE_EFFECTS (stmt))
424         {
425           if (!IS_EMPTY_STMT (stmt)
426               && !VOID_TYPE_P (TREE_TYPE (stmt))
427               && !TREE_NO_WARNING (stmt))
428             warning (OPT_Wunused_value, "statement with no effect");
429         }
430       else
431         warn_if_unused_value (stmt, input_location);
432     }
433
434   if (stmt == NULL_TREE)
435     stmt = alloc_stmt_list ();
436
437   *stmt_p = stmt;
438 }
439
440 /* Gimplify initialization from an AGGR_INIT_EXPR.  */
441
442 static void
443 cp_gimplify_init_expr (tree *expr_p)
444 {
445   tree from = TREE_OPERAND (*expr_p, 1);
446   tree to = TREE_OPERAND (*expr_p, 0);
447   tree t;
448
449   /* What about code that pulls out the temp and uses it elsewhere?  I
450      think that such code never uses the TARGET_EXPR as an initializer.  If
451      I'm wrong, we'll abort because the temp won't have any RTL.  In that
452      case, I guess we'll need to replace references somehow.  */
453   if (TREE_CODE (from) == TARGET_EXPR)
454     from = TARGET_EXPR_INITIAL (from);
455
456   /* Look through any COMPOUND_EXPRs, since build_compound_expr pushes them
457      inside the TARGET_EXPR.  */
458   for (t = from; t; )
459     {
460       tree sub = TREE_CODE (t) == COMPOUND_EXPR ? TREE_OPERAND (t, 0) : t;
461
462       /* If we are initializing from an AGGR_INIT_EXPR, drop the INIT_EXPR and
463          replace the slot operand with our target.
464
465          Should we add a target parm to gimplify_expr instead?  No, as in this
466          case we want to replace the INIT_EXPR.  */
467       if (TREE_CODE (sub) == AGGR_INIT_EXPR
468           || TREE_CODE (sub) == VEC_INIT_EXPR)
469         {
470           if (TREE_CODE (sub) == AGGR_INIT_EXPR)
471             AGGR_INIT_EXPR_SLOT (sub) = to;
472           else
473             VEC_INIT_EXPR_SLOT (sub) = to;
474           *expr_p = from;
475
476           /* The initialization is now a side-effect, so the container can
477              become void.  */
478           if (from != sub)
479             TREE_TYPE (from) = void_type_node;
480         }
481
482       if (cxx_dialect >= cxx14 && TREE_CODE (sub) == CONSTRUCTOR)
483         /* Handle aggregate NSDMI.  */
484         replace_placeholders (sub, to);
485
486       if (t == sub)
487         break;
488       else
489         t = TREE_OPERAND (t, 1);
490     }
491
492 }
493
494 /* Gimplify a MUST_NOT_THROW_EXPR.  */
495
496 static enum gimplify_status
497 gimplify_must_not_throw_expr (tree *expr_p, gimple_seq *pre_p)
498 {
499   tree stmt = *expr_p;
500   tree temp = voidify_wrapper_expr (stmt, NULL);
501   tree body = TREE_OPERAND (stmt, 0);
502   gimple_seq try_ = NULL;
503   gimple_seq catch_ = NULL;
504   gimple mnt;
505
506   gimplify_and_add (body, &try_);
507   mnt = gimple_build_eh_must_not_throw (terminate_node);
508   gimple_seq_add_stmt_without_update (&catch_, mnt);
509   mnt = gimple_build_try (try_, catch_, GIMPLE_TRY_CATCH);
510
511   gimple_seq_add_stmt_without_update (pre_p, mnt);
512   if (temp)
513     {
514       *expr_p = temp;
515       return GS_OK;
516     }
517
518   *expr_p = NULL;
519   return GS_ALL_DONE;
520 }
521
522 /* Do C++-specific gimplification.  Args are as for gimplify_expr.  */
523
524 int
525 cp_gimplify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
526 {
527   int saved_stmts_are_full_exprs_p = 0;
528   enum tree_code code = TREE_CODE (*expr_p);
529   enum gimplify_status ret;
530
531   if (STATEMENT_CODE_P (code))
532     {
533       saved_stmts_are_full_exprs_p = stmts_are_full_exprs_p ();
534       current_stmt_tree ()->stmts_are_full_exprs_p
535         = STMT_IS_FULL_EXPR_P (*expr_p);
536     }
537
538   switch (code)
539     {
540     case PTRMEM_CST:
541       *expr_p = cplus_expand_constant (*expr_p);
542       ret = GS_OK;
543       break;
544
545     case AGGR_INIT_EXPR:
546       simplify_aggr_init_expr (expr_p);
547       ret = GS_OK;
548       break;
549
550     case VEC_INIT_EXPR:
551       {
552         location_t loc = input_location;
553         tree init = VEC_INIT_EXPR_INIT (*expr_p);
554         int from_array = (init && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE);
555         gcc_assert (EXPR_HAS_LOCATION (*expr_p));
556         input_location = EXPR_LOCATION (*expr_p);
557         *expr_p = build_vec_init (VEC_INIT_EXPR_SLOT (*expr_p), NULL_TREE,
558                                   init, VEC_INIT_EXPR_VALUE_INIT (*expr_p),
559                                   from_array,
560                                   tf_warning_or_error);
561         cp_genericize_tree (expr_p);
562         ret = GS_OK;
563         input_location = loc;
564       }
565       break;
566
567     case THROW_EXPR:
568       /* FIXME communicate throw type to back end, probably by moving
569          THROW_EXPR into ../tree.def.  */
570       *expr_p = TREE_OPERAND (*expr_p, 0);
571       ret = GS_OK;
572       break;
573
574     case MUST_NOT_THROW_EXPR:
575       ret = gimplify_must_not_throw_expr (expr_p, pre_p);
576       break;
577
578       /* We used to do this for MODIFY_EXPR as well, but that's unsafe; the
579          LHS of an assignment might also be involved in the RHS, as in bug
580          25979.  */
581     case INIT_EXPR:
582       if (fn_contains_cilk_spawn_p (cfun)
583           && cilk_detect_spawn_and_unwrap (expr_p)
584           && !seen_error ())
585         return (enum gimplify_status) gimplify_cilk_spawn (expr_p);
586       cp_gimplify_init_expr (expr_p);
587       if (TREE_CODE (*expr_p) != INIT_EXPR)
588         return GS_OK;
589       /* Otherwise fall through.  */
590     case MODIFY_EXPR:
591       {
592         if (fn_contains_cilk_spawn_p (cfun)
593             && cilk_detect_spawn_and_unwrap (expr_p)
594             && !seen_error ())
595           return (enum gimplify_status) gimplify_cilk_spawn (expr_p);
596
597         /* If the back end isn't clever enough to know that the lhs and rhs
598            types are the same, add an explicit conversion.  */
599         tree op0 = TREE_OPERAND (*expr_p, 0);
600         tree op1 = TREE_OPERAND (*expr_p, 1);
601
602         if (!error_operand_p (op0)
603             && !error_operand_p (op1)
604             && (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (op0))
605                 || TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (op1)))
606             && !useless_type_conversion_p (TREE_TYPE (op1), TREE_TYPE (op0)))
607           TREE_OPERAND (*expr_p, 1) = build1 (VIEW_CONVERT_EXPR,
608                                               TREE_TYPE (op0), op1);
609
610         else if ((is_gimple_lvalue (op1) || INDIRECT_REF_P (op1)
611                   || (TREE_CODE (op1) == CONSTRUCTOR
612                       && CONSTRUCTOR_NELTS (op1) == 0
613                       && !TREE_CLOBBER_P (op1))
614                   || (TREE_CODE (op1) == CALL_EXPR
615                       && !CALL_EXPR_RETURN_SLOT_OPT (op1)))
616                  && is_really_empty_class (TREE_TYPE (op0)))
617           {
618             /* Remove any copies of empty classes.  We check that the RHS
619                has a simple form so that TARGET_EXPRs and non-empty
620                CONSTRUCTORs get reduced properly, and we leave the return
621                slot optimization alone because it isn't a copy (FIXME so it
622                shouldn't be represented as one).
623
624                Also drop volatile variables on the RHS to avoid infinite
625                recursion from gimplify_expr trying to load the value.  */
626             if (!TREE_SIDE_EFFECTS (op1))
627               *expr_p = op0;
628             else if (TREE_THIS_VOLATILE (op1)
629                      && (REFERENCE_CLASS_P (op1) || DECL_P (op1)))
630               *expr_p = build2 (COMPOUND_EXPR, TREE_TYPE (*expr_p),
631                                 build_fold_addr_expr (op1), op0);
632             else
633               *expr_p = build2 (COMPOUND_EXPR, TREE_TYPE (*expr_p),
634                                 op0, op1);
635           }
636       }
637       ret = GS_OK;
638       break;
639
640     case EMPTY_CLASS_EXPR:
641       /* We create an empty CONSTRUCTOR with RECORD_TYPE.  */
642       *expr_p = build_constructor (TREE_TYPE (*expr_p), NULL);
643       ret = GS_OK;
644       break;
645
646     case BASELINK:
647       *expr_p = BASELINK_FUNCTIONS (*expr_p);
648       ret = GS_OK;
649       break;
650
651     case TRY_BLOCK:
652       genericize_try_block (expr_p);
653       ret = GS_OK;
654       break;
655
656     case HANDLER:
657       genericize_catch_block (expr_p);
658       ret = GS_OK;
659       break;
660
661     case EH_SPEC_BLOCK:
662       genericize_eh_spec_block (expr_p);
663       ret = GS_OK;
664       break;
665
666     case USING_STMT:
667       gcc_unreachable ();
668
669     case FOR_STMT:
670     case WHILE_STMT:
671     case DO_STMT:
672     case SWITCH_STMT:
673     case CONTINUE_STMT:
674     case BREAK_STMT:
675       gcc_unreachable ();
676
677     case OMP_FOR:
678     case OMP_SIMD:
679     case OMP_DISTRIBUTE:
680       ret = cp_gimplify_omp_for (expr_p, pre_p);
681       break;
682
683     case EXPR_STMT:
684       gimplify_expr_stmt (expr_p);
685       ret = GS_OK;
686       break;
687
688     case UNARY_PLUS_EXPR:
689       {
690         tree arg = TREE_OPERAND (*expr_p, 0);
691         tree type = TREE_TYPE (*expr_p);
692         *expr_p = (TREE_TYPE (arg) != type) ? fold_convert (type, arg)
693                                             : arg;
694         ret = GS_OK;
695       }
696       break;
697
698     case CILK_SPAWN_STMT:
699       gcc_assert 
700         (fn_contains_cilk_spawn_p (cfun) 
701          && cilk_detect_spawn_and_unwrap (expr_p));
702
703       /* If errors are seen, then just process it as a CALL_EXPR.  */
704       if (!seen_error ())
705         return (enum gimplify_status) gimplify_cilk_spawn (expr_p);
706       
707     case CALL_EXPR:
708       if (fn_contains_cilk_spawn_p (cfun)
709           && cilk_detect_spawn_and_unwrap (expr_p)
710           && !seen_error ())
711         return (enum gimplify_status) gimplify_cilk_spawn (expr_p);
712
713       /* DR 1030 says that we need to evaluate the elements of an
714          initializer-list in forward order even when it's used as arguments to
715          a constructor.  So if the target wants to evaluate them in reverse
716          order and there's more than one argument other than 'this', gimplify
717          them in order.  */
718       ret = GS_OK;
719       if (PUSH_ARGS_REVERSED && CALL_EXPR_LIST_INIT_P (*expr_p)
720           && call_expr_nargs (*expr_p) > 2)
721         {
722           int nargs = call_expr_nargs (*expr_p);
723           location_t loc = EXPR_LOC_OR_LOC (*expr_p, input_location);
724           for (int i = 1; i < nargs; ++i)
725             {
726               enum gimplify_status t
727                 = gimplify_arg (&CALL_EXPR_ARG (*expr_p, i), pre_p, loc);
728               if (t == GS_ERROR)
729                 ret = GS_ERROR;
730             }
731         }
732       break;
733
734     default:
735       ret = (enum gimplify_status) c_gimplify_expr (expr_p, pre_p, post_p);
736       break;
737     }
738
739   /* Restore saved state.  */
740   if (STATEMENT_CODE_P (code))
741     current_stmt_tree ()->stmts_are_full_exprs_p
742       = saved_stmts_are_full_exprs_p;
743
744   return ret;
745 }
746
747 static inline bool
748 is_invisiref_parm (const_tree t)
749 {
750   return ((TREE_CODE (t) == PARM_DECL || TREE_CODE (t) == RESULT_DECL)
751           && DECL_BY_REFERENCE (t));
752 }
753
754 /* Return true if the uid in both int tree maps are equal.  */
755
756 bool
757 cxx_int_tree_map_hasher::equal (cxx_int_tree_map *a, cxx_int_tree_map *b)
758 {
759   return (a->uid == b->uid);
760 }
761
762 /* Hash a UID in a cxx_int_tree_map.  */
763
764 unsigned int
765 cxx_int_tree_map_hasher::hash (cxx_int_tree_map *item)
766 {
767   return item->uid;
768 }
769
770 /* A stable comparison routine for use with splay trees and DECLs.  */
771
772 static int
773 splay_tree_compare_decl_uid (splay_tree_key xa, splay_tree_key xb)
774 {
775   tree a = (tree) xa;
776   tree b = (tree) xb;
777
778   return DECL_UID (a) - DECL_UID (b);
779 }
780
781 /* OpenMP context during genericization.  */
782
783 struct cp_genericize_omp_taskreg
784 {
785   bool is_parallel;
786   bool default_shared;
787   struct cp_genericize_omp_taskreg *outer;
788   splay_tree variables;
789 };
790
791 /* Return true if genericization should try to determine if
792    DECL is firstprivate or shared within task regions.  */
793
794 static bool
795 omp_var_to_track (tree decl)
796 {
797   tree type = TREE_TYPE (decl);
798   if (is_invisiref_parm (decl))
799     type = TREE_TYPE (type);
800   while (TREE_CODE (type) == ARRAY_TYPE)
801     type = TREE_TYPE (type);
802   if (type == error_mark_node || !CLASS_TYPE_P (type))
803     return false;
804   if (VAR_P (decl) && DECL_THREAD_LOCAL_P (decl))
805     return false;
806   if (cxx_omp_predetermined_sharing (decl) != OMP_CLAUSE_DEFAULT_UNSPECIFIED)
807     return false;
808   return true;
809 }
810
811 /* Note DECL use in OpenMP region OMP_CTX during genericization.  */
812
813 static void
814 omp_cxx_notice_variable (struct cp_genericize_omp_taskreg *omp_ctx, tree decl)
815 {
816   splay_tree_node n = splay_tree_lookup (omp_ctx->variables,
817                                          (splay_tree_key) decl);
818   if (n == NULL)
819     {
820       int flags = OMP_CLAUSE_DEFAULT_SHARED;
821       if (omp_ctx->outer)
822         omp_cxx_notice_variable (omp_ctx->outer, decl);
823       if (!omp_ctx->default_shared)
824         {
825           struct cp_genericize_omp_taskreg *octx;
826
827           for (octx = omp_ctx->outer; octx; octx = octx->outer)
828             {
829               n = splay_tree_lookup (octx->variables, (splay_tree_key) decl);
830               if (n && n->value != OMP_CLAUSE_DEFAULT_SHARED)
831                 {
832                   flags = OMP_CLAUSE_DEFAULT_FIRSTPRIVATE;
833                   break;
834                 }
835               if (octx->is_parallel)
836                 break;
837             }
838           if (octx == NULL
839               && (TREE_CODE (decl) == PARM_DECL
840                   || (!(TREE_STATIC (decl) || DECL_EXTERNAL (decl))
841                       && DECL_CONTEXT (decl) == current_function_decl)))
842             flags = OMP_CLAUSE_DEFAULT_FIRSTPRIVATE;
843           if (flags == OMP_CLAUSE_DEFAULT_FIRSTPRIVATE)
844             {
845               /* DECL is implicitly determined firstprivate in
846                  the current task construct.  Ensure copy ctor and
847                  dtor are instantiated, because during gimplification
848                  it will be already too late.  */
849               tree type = TREE_TYPE (decl);
850               if (is_invisiref_parm (decl))
851                 type = TREE_TYPE (type);
852               while (TREE_CODE (type) == ARRAY_TYPE)
853                 type = TREE_TYPE (type);
854               get_copy_ctor (type, tf_none);
855               get_dtor (type, tf_none);
856             }
857         }
858       splay_tree_insert (omp_ctx->variables, (splay_tree_key) decl, flags);
859     }
860 }
861
862 /* Genericization context.  */
863
864 struct cp_genericize_data
865 {
866   hash_set<tree> *p_set;
867   vec<tree> bind_expr_stack;
868   struct cp_genericize_omp_taskreg *omp_ctx;
869 };
870
871 /* Perform any pre-gimplification lowering of C++ front end trees to
872    GENERIC.  */
873
874 static tree
875 cp_genericize_r (tree *stmt_p, int *walk_subtrees, void *data)
876 {
877   tree stmt = *stmt_p;
878   struct cp_genericize_data *wtd = (struct cp_genericize_data *) data;
879   hash_set<tree> *p_set = wtd->p_set;
880
881   /* If in an OpenMP context, note var uses.  */
882   if (__builtin_expect (wtd->omp_ctx != NULL, 0)
883       && (VAR_P (stmt)
884           || TREE_CODE (stmt) == PARM_DECL
885           || TREE_CODE (stmt) == RESULT_DECL)
886       && omp_var_to_track (stmt))
887     omp_cxx_notice_variable (wtd->omp_ctx, stmt);
888
889   if (is_invisiref_parm (stmt)
890       /* Don't dereference parms in a thunk, pass the references through. */
891       && !(DECL_THUNK_P (current_function_decl)
892            && TREE_CODE (stmt) == PARM_DECL))
893     {
894       *stmt_p = convert_from_reference (stmt);
895       *walk_subtrees = 0;
896       return NULL;
897     }
898
899   /* Map block scope extern declarations to visible declarations with the
900      same name and type in outer scopes if any.  */
901   if (cp_function_chain->extern_decl_map
902       && VAR_OR_FUNCTION_DECL_P (stmt)
903       && DECL_EXTERNAL (stmt))
904     {
905       struct cxx_int_tree_map *h, in;
906       in.uid = DECL_UID (stmt);
907       h = cp_function_chain->extern_decl_map->find_with_hash (&in, in.uid);
908       if (h)
909         {
910           *stmt_p = h->to;
911           *walk_subtrees = 0;
912           return NULL;
913         }
914     }
915
916   /* Other than invisiref parms, don't walk the same tree twice.  */
917   if (p_set->contains (stmt))
918     {
919       *walk_subtrees = 0;
920       return NULL_TREE;
921     }
922
923   if (TREE_CODE (stmt) == ADDR_EXPR
924       && is_invisiref_parm (TREE_OPERAND (stmt, 0)))
925     {
926       /* If in an OpenMP context, note var uses.  */
927       if (__builtin_expect (wtd->omp_ctx != NULL, 0)
928           && omp_var_to_track (TREE_OPERAND (stmt, 0)))
929         omp_cxx_notice_variable (wtd->omp_ctx, TREE_OPERAND (stmt, 0));
930       *stmt_p = convert (TREE_TYPE (stmt), TREE_OPERAND (stmt, 0));
931       *walk_subtrees = 0;
932     }
933   else if (TREE_CODE (stmt) == RETURN_EXPR
934            && TREE_OPERAND (stmt, 0)
935            && is_invisiref_parm (TREE_OPERAND (stmt, 0)))
936     /* Don't dereference an invisiref RESULT_DECL inside a RETURN_EXPR.  */
937     *walk_subtrees = 0;
938   else if (TREE_CODE (stmt) == OMP_CLAUSE)
939     switch (OMP_CLAUSE_CODE (stmt))
940       {
941       case OMP_CLAUSE_LASTPRIVATE:
942         /* Don't dereference an invisiref in OpenMP clauses.  */
943         if (is_invisiref_parm (OMP_CLAUSE_DECL (stmt)))
944           {
945             *walk_subtrees = 0;
946             if (OMP_CLAUSE_LASTPRIVATE_STMT (stmt))
947               cp_walk_tree (&OMP_CLAUSE_LASTPRIVATE_STMT (stmt),
948                             cp_genericize_r, data, NULL);
949           }
950         break;
951       case OMP_CLAUSE_PRIVATE:
952         /* Don't dereference an invisiref in OpenMP clauses.  */
953         if (is_invisiref_parm (OMP_CLAUSE_DECL (stmt)))
954           *walk_subtrees = 0;
955         else if (wtd->omp_ctx != NULL)
956           {
957             /* Private clause doesn't cause any references to the
958                var in outer contexts, avoid calling
959                omp_cxx_notice_variable for it.  */
960             struct cp_genericize_omp_taskreg *old = wtd->omp_ctx;
961             wtd->omp_ctx = NULL;
962             cp_walk_tree (&OMP_CLAUSE_DECL (stmt), cp_genericize_r,
963                           data, NULL);
964             wtd->omp_ctx = old;
965             *walk_subtrees = 0;
966           }
967         break;
968       case OMP_CLAUSE_SHARED:
969       case OMP_CLAUSE_FIRSTPRIVATE:
970       case OMP_CLAUSE_COPYIN:
971       case OMP_CLAUSE_COPYPRIVATE:
972         /* Don't dereference an invisiref in OpenMP clauses.  */
973         if (is_invisiref_parm (OMP_CLAUSE_DECL (stmt)))
974           *walk_subtrees = 0;
975         break;
976       case OMP_CLAUSE_REDUCTION:
977         /* Don't dereference an invisiref in reduction clause's
978            OMP_CLAUSE_DECL either.  OMP_CLAUSE_REDUCTION_{INIT,MERGE}
979            still needs to be genericized.  */
980         if (is_invisiref_parm (OMP_CLAUSE_DECL (stmt)))
981           {
982             *walk_subtrees = 0;
983             if (OMP_CLAUSE_REDUCTION_INIT (stmt))
984               cp_walk_tree (&OMP_CLAUSE_REDUCTION_INIT (stmt),
985                             cp_genericize_r, data, NULL);
986             if (OMP_CLAUSE_REDUCTION_MERGE (stmt))
987               cp_walk_tree (&OMP_CLAUSE_REDUCTION_MERGE (stmt),
988                             cp_genericize_r, data, NULL);
989           }
990         break;
991       default:
992         break;
993       }
994   else if (IS_TYPE_OR_DECL_P (stmt))
995     *walk_subtrees = 0;
996
997   /* Due to the way voidify_wrapper_expr is written, we don't get a chance
998      to lower this construct before scanning it, so we need to lower these
999      before doing anything else.  */
1000   else if (TREE_CODE (stmt) == CLEANUP_STMT)
1001     *stmt_p = build2_loc (EXPR_LOCATION (stmt),
1002                           CLEANUP_EH_ONLY (stmt) ? TRY_CATCH_EXPR
1003                                                  : TRY_FINALLY_EXPR,
1004                           void_type_node,
1005                           CLEANUP_BODY (stmt),
1006                           CLEANUP_EXPR (stmt));
1007
1008   else if (TREE_CODE (stmt) == IF_STMT)
1009     {
1010       genericize_if_stmt (stmt_p);
1011       /* *stmt_p has changed, tail recurse to handle it again.  */
1012       return cp_genericize_r (stmt_p, walk_subtrees, data);
1013     }
1014
1015   /* COND_EXPR might have incompatible types in branches if one or both
1016      arms are bitfields.  Fix it up now.  */
1017   else if (TREE_CODE (stmt) == COND_EXPR)
1018     {
1019       tree type_left
1020         = (TREE_OPERAND (stmt, 1)
1021            ? is_bitfield_expr_with_lowered_type (TREE_OPERAND (stmt, 1))
1022            : NULL_TREE);
1023       tree type_right
1024         = (TREE_OPERAND (stmt, 2)
1025            ? is_bitfield_expr_with_lowered_type (TREE_OPERAND (stmt, 2))
1026            : NULL_TREE);
1027       if (type_left
1028           && !useless_type_conversion_p (TREE_TYPE (stmt),
1029                                          TREE_TYPE (TREE_OPERAND (stmt, 1))))
1030         {
1031           TREE_OPERAND (stmt, 1)
1032             = fold_convert (type_left, TREE_OPERAND (stmt, 1));
1033           gcc_assert (useless_type_conversion_p (TREE_TYPE (stmt),
1034                                                  type_left));
1035         }
1036       if (type_right
1037           && !useless_type_conversion_p (TREE_TYPE (stmt),
1038                                          TREE_TYPE (TREE_OPERAND (stmt, 2))))
1039         {
1040           TREE_OPERAND (stmt, 2)
1041             = fold_convert (type_right, TREE_OPERAND (stmt, 2));
1042           gcc_assert (useless_type_conversion_p (TREE_TYPE (stmt),
1043                                                  type_right));
1044         }
1045     }
1046
1047   else if (TREE_CODE (stmt) == BIND_EXPR)
1048     {
1049       if (__builtin_expect (wtd->omp_ctx != NULL, 0))
1050         {
1051           tree decl;
1052           for (decl = BIND_EXPR_VARS (stmt); decl; decl = DECL_CHAIN (decl))
1053             if (VAR_P (decl)
1054                 && !DECL_EXTERNAL (decl)
1055                 && omp_var_to_track (decl))
1056               {
1057                 splay_tree_node n
1058                   = splay_tree_lookup (wtd->omp_ctx->variables,
1059                                        (splay_tree_key) decl);
1060                 if (n == NULL)
1061                   splay_tree_insert (wtd->omp_ctx->variables,
1062                                      (splay_tree_key) decl,
1063                                      TREE_STATIC (decl)
1064                                      ? OMP_CLAUSE_DEFAULT_SHARED
1065                                      : OMP_CLAUSE_DEFAULT_PRIVATE);
1066               }
1067         }
1068       wtd->bind_expr_stack.safe_push (stmt);
1069       cp_walk_tree (&BIND_EXPR_BODY (stmt),
1070                     cp_genericize_r, data, NULL);
1071       wtd->bind_expr_stack.pop ();
1072     }
1073
1074   else if (TREE_CODE (stmt) == USING_STMT)
1075     {
1076       tree block = NULL_TREE;
1077
1078       /* Get the innermost inclosing GIMPLE_BIND that has a non NULL
1079          BLOCK, and append an IMPORTED_DECL to its
1080          BLOCK_VARS chained list.  */
1081       if (wtd->bind_expr_stack.exists ())
1082         {
1083           int i;
1084           for (i = wtd->bind_expr_stack.length () - 1; i >= 0; i--)
1085             if ((block = BIND_EXPR_BLOCK (wtd->bind_expr_stack[i])))
1086               break;
1087         }
1088       if (block)
1089         {
1090           tree using_directive;
1091           gcc_assert (TREE_OPERAND (stmt, 0));
1092
1093           using_directive = make_node (IMPORTED_DECL);
1094           TREE_TYPE (using_directive) = void_type_node;
1095
1096           IMPORTED_DECL_ASSOCIATED_DECL (using_directive)
1097             = TREE_OPERAND (stmt, 0);
1098           DECL_CHAIN (using_directive) = BLOCK_VARS (block);
1099           BLOCK_VARS (block) = using_directive;
1100         }
1101       /* The USING_STMT won't appear in GENERIC.  */
1102       *stmt_p = build1 (NOP_EXPR, void_type_node, integer_zero_node);
1103       *walk_subtrees = 0;
1104     }
1105
1106   else if (TREE_CODE (stmt) == DECL_EXPR
1107            && TREE_CODE (DECL_EXPR_DECL (stmt)) == USING_DECL)
1108     {
1109       /* Using decls inside DECL_EXPRs are just dropped on the floor.  */
1110       *stmt_p = build1 (NOP_EXPR, void_type_node, integer_zero_node);
1111       *walk_subtrees = 0;
1112     }
1113   else if (TREE_CODE (stmt) == OMP_PARALLEL || TREE_CODE (stmt) == OMP_TASK)
1114     {
1115       struct cp_genericize_omp_taskreg omp_ctx;
1116       tree c, decl;
1117       splay_tree_node n;
1118
1119       *walk_subtrees = 0;
1120       cp_walk_tree (&OMP_CLAUSES (stmt), cp_genericize_r, data, NULL);
1121       omp_ctx.is_parallel = TREE_CODE (stmt) == OMP_PARALLEL;
1122       omp_ctx.default_shared = omp_ctx.is_parallel;
1123       omp_ctx.outer = wtd->omp_ctx;
1124       omp_ctx.variables = splay_tree_new (splay_tree_compare_decl_uid, 0, 0);
1125       wtd->omp_ctx = &omp_ctx;
1126       for (c = OMP_CLAUSES (stmt); c; c = OMP_CLAUSE_CHAIN (c))
1127         switch (OMP_CLAUSE_CODE (c))
1128           {
1129           case OMP_CLAUSE_SHARED:
1130           case OMP_CLAUSE_PRIVATE:
1131           case OMP_CLAUSE_FIRSTPRIVATE:
1132           case OMP_CLAUSE_LASTPRIVATE:
1133             decl = OMP_CLAUSE_DECL (c);
1134             if (decl == error_mark_node || !omp_var_to_track (decl))
1135               break;
1136             n = splay_tree_lookup (omp_ctx.variables, (splay_tree_key) decl);
1137             if (n != NULL)
1138               break;
1139             splay_tree_insert (omp_ctx.variables, (splay_tree_key) decl,
1140                                OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED
1141                                ? OMP_CLAUSE_DEFAULT_SHARED
1142                                : OMP_CLAUSE_DEFAULT_PRIVATE);
1143             if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_PRIVATE
1144                 && omp_ctx.outer)
1145               omp_cxx_notice_variable (omp_ctx.outer, decl);
1146             break;
1147           case OMP_CLAUSE_DEFAULT:
1148             if (OMP_CLAUSE_DEFAULT_KIND (c) == OMP_CLAUSE_DEFAULT_SHARED)
1149               omp_ctx.default_shared = true;
1150           default:
1151             break;
1152           }
1153       cp_walk_tree (&OMP_BODY (stmt), cp_genericize_r, data, NULL);
1154       wtd->omp_ctx = omp_ctx.outer;
1155       splay_tree_delete (omp_ctx.variables);
1156     }
1157   else if (TREE_CODE (stmt) == CONVERT_EXPR)
1158     gcc_assert (!CONVERT_EXPR_VBASE_PATH (stmt));
1159   else if (TREE_CODE (stmt) == FOR_STMT)
1160     genericize_for_stmt (stmt_p, walk_subtrees, data);
1161   else if (TREE_CODE (stmt) == WHILE_STMT)
1162     genericize_while_stmt (stmt_p, walk_subtrees, data);
1163   else if (TREE_CODE (stmt) == DO_STMT)
1164     genericize_do_stmt (stmt_p, walk_subtrees, data);
1165   else if (TREE_CODE (stmt) == SWITCH_STMT)
1166     genericize_switch_stmt (stmt_p, walk_subtrees, data);
1167   else if (TREE_CODE (stmt) == CONTINUE_STMT)
1168     genericize_continue_stmt (stmt_p);
1169   else if (TREE_CODE (stmt) == BREAK_STMT)
1170     genericize_break_stmt (stmt_p);
1171   else if (TREE_CODE (stmt) == OMP_FOR
1172            || TREE_CODE (stmt) == OMP_SIMD
1173            || TREE_CODE (stmt) == OMP_DISTRIBUTE)
1174     genericize_omp_for_stmt (stmt_p, walk_subtrees, data);
1175   else if (TREE_CODE (stmt) == SIZEOF_EXPR)
1176     {
1177       if (SIZEOF_EXPR_TYPE_P (stmt))
1178         *stmt_p
1179           = cxx_sizeof_or_alignof_type (TREE_TYPE (TREE_OPERAND (stmt, 0)),
1180                                         SIZEOF_EXPR, false);
1181       else if (TYPE_P (TREE_OPERAND (stmt, 0)))
1182         *stmt_p = cxx_sizeof_or_alignof_type (TREE_OPERAND (stmt, 0),
1183                                               SIZEOF_EXPR, false);
1184       else
1185         *stmt_p = cxx_sizeof_or_alignof_expr (TREE_OPERAND (stmt, 0),
1186                                               SIZEOF_EXPR, false);
1187       if (*stmt_p == error_mark_node)
1188         *stmt_p = size_one_node;
1189       return NULL;
1190     }    
1191   else if (flag_sanitize & (SANITIZE_NULL | SANITIZE_ALIGNMENT))
1192     {
1193       if (TREE_CODE (stmt) == NOP_EXPR
1194           && TREE_CODE (TREE_TYPE (stmt)) == REFERENCE_TYPE)
1195         ubsan_maybe_instrument_reference (stmt);
1196       else if (TREE_CODE (stmt) == CALL_EXPR)
1197         {
1198           tree fn = CALL_EXPR_FN (stmt);
1199           if (fn != NULL_TREE
1200               && !error_operand_p (fn)
1201               && POINTER_TYPE_P (TREE_TYPE (fn))
1202               && TREE_CODE (TREE_TYPE (TREE_TYPE (fn))) == METHOD_TYPE)
1203             {
1204               bool is_ctor
1205                 = TREE_CODE (fn) == ADDR_EXPR
1206                   && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL
1207                   && DECL_CONSTRUCTOR_P (TREE_OPERAND (fn, 0));
1208               ubsan_maybe_instrument_member_call (stmt, is_ctor);
1209             }
1210         }
1211     }
1212
1213   p_set->add (*stmt_p);
1214
1215   return NULL;
1216 }
1217
1218 /* Lower C++ front end trees to GENERIC in T_P.  */
1219
1220 static void
1221 cp_genericize_tree (tree* t_p)
1222 {
1223   struct cp_genericize_data wtd;
1224
1225   wtd.p_set = new hash_set<tree>;
1226   wtd.bind_expr_stack.create (0);
1227   wtd.omp_ctx = NULL;
1228   cp_walk_tree (t_p, cp_genericize_r, &wtd, NULL);
1229   delete wtd.p_set;
1230   wtd.bind_expr_stack.release ();
1231 }
1232
1233 /* If a function that should end with a return in non-void
1234    function doesn't obviously end with return, add ubsan
1235    instrumentation code to verify it at runtime.  */
1236
1237 static void
1238 cp_ubsan_maybe_instrument_return (tree fndecl)
1239 {
1240   if (VOID_TYPE_P (TREE_TYPE (TREE_TYPE (fndecl)))
1241       || DECL_CONSTRUCTOR_P (fndecl)
1242       || DECL_DESTRUCTOR_P (fndecl)
1243       || !targetm.warn_func_return (fndecl))
1244     return;
1245
1246   tree t = DECL_SAVED_TREE (fndecl);
1247   while (t)
1248     {
1249       switch (TREE_CODE (t))
1250         {
1251         case BIND_EXPR:
1252           t = BIND_EXPR_BODY (t);
1253           continue;
1254         case TRY_FINALLY_EXPR:
1255           t = TREE_OPERAND (t, 0);
1256           continue;
1257         case STATEMENT_LIST:
1258           {
1259             tree_stmt_iterator i = tsi_last (t);
1260             if (!tsi_end_p (i))
1261               {
1262                 t = tsi_stmt (i);
1263                 continue;
1264               }
1265           }
1266           break;
1267         case RETURN_EXPR:
1268           return;
1269         default:
1270           break;
1271         }
1272       break;
1273     }
1274   if (t == NULL_TREE)
1275     return;
1276   t = DECL_SAVED_TREE (fndecl);
1277   if (TREE_CODE (t) == BIND_EXPR
1278       && TREE_CODE (BIND_EXPR_BODY (t)) == STATEMENT_LIST)
1279     {
1280       tree_stmt_iterator i = tsi_last (BIND_EXPR_BODY (t));
1281       t = ubsan_instrument_return (DECL_SOURCE_LOCATION (fndecl));
1282       tsi_link_after (&i, t, TSI_NEW_STMT);
1283     }
1284 }
1285
1286 void
1287 cp_genericize (tree fndecl)
1288 {
1289   tree t;
1290
1291   /* Fix up the types of parms passed by invisible reference.  */
1292   for (t = DECL_ARGUMENTS (fndecl); t; t = DECL_CHAIN (t))
1293     if (TREE_ADDRESSABLE (TREE_TYPE (t)))
1294       {
1295         /* If a function's arguments are copied to create a thunk,
1296            then DECL_BY_REFERENCE will be set -- but the type of the
1297            argument will be a pointer type, so we will never get
1298            here.  */
1299         gcc_assert (!DECL_BY_REFERENCE (t));
1300         gcc_assert (DECL_ARG_TYPE (t) != TREE_TYPE (t));
1301         TREE_TYPE (t) = DECL_ARG_TYPE (t);
1302         DECL_BY_REFERENCE (t) = 1;
1303         TREE_ADDRESSABLE (t) = 0;
1304         relayout_decl (t);
1305       }
1306
1307   /* Do the same for the return value.  */
1308   if (TREE_ADDRESSABLE (TREE_TYPE (DECL_RESULT (fndecl))))
1309     {
1310       t = DECL_RESULT (fndecl);
1311       TREE_TYPE (t) = build_reference_type (TREE_TYPE (t));
1312       DECL_BY_REFERENCE (t) = 1;
1313       TREE_ADDRESSABLE (t) = 0;
1314       relayout_decl (t);
1315       if (DECL_NAME (t))
1316         {
1317           /* Adjust DECL_VALUE_EXPR of the original var.  */
1318           tree outer = outer_curly_brace_block (current_function_decl);
1319           tree var;
1320
1321           if (outer)
1322             for (var = BLOCK_VARS (outer); var; var = DECL_CHAIN (var))
1323               if (DECL_NAME (t) == DECL_NAME (var)
1324                   && DECL_HAS_VALUE_EXPR_P (var)
1325                   && DECL_VALUE_EXPR (var) == t)
1326                 {
1327                   tree val = convert_from_reference (t);
1328                   SET_DECL_VALUE_EXPR (var, val);
1329                   break;
1330                 }
1331         }
1332     }
1333
1334   /* If we're a clone, the body is already GIMPLE.  */
1335   if (DECL_CLONED_FUNCTION_P (fndecl))
1336     return;
1337
1338   /* Expand all the array notations here.  */
1339   if (flag_cilkplus 
1340       && contains_array_notation_expr (DECL_SAVED_TREE (fndecl)))
1341     DECL_SAVED_TREE (fndecl) = 
1342       expand_array_notation_exprs (DECL_SAVED_TREE (fndecl));
1343
1344   /* We do want to see every occurrence of the parms, so we can't just use
1345      walk_tree's hash functionality.  */
1346   cp_genericize_tree (&DECL_SAVED_TREE (fndecl));
1347
1348   if (flag_sanitize & SANITIZE_RETURN
1349       && current_function_decl != NULL_TREE
1350       && !lookup_attribute ("no_sanitize_undefined",
1351                             DECL_ATTRIBUTES (current_function_decl)))
1352     cp_ubsan_maybe_instrument_return (fndecl);
1353
1354   /* Do everything else.  */
1355   c_genericize (fndecl);
1356
1357   gcc_assert (bc_label[bc_break] == NULL);
1358   gcc_assert (bc_label[bc_continue] == NULL);
1359 }
1360 \f
1361 /* Build code to apply FN to each member of ARG1 and ARG2.  FN may be
1362    NULL if there is in fact nothing to do.  ARG2 may be null if FN
1363    actually only takes one argument.  */
1364
1365 static tree
1366 cxx_omp_clause_apply_fn (tree fn, tree arg1, tree arg2)
1367 {
1368   tree defparm, parm, t;
1369   int i = 0;
1370   int nargs;
1371   tree *argarray;
1372
1373   if (fn == NULL)
1374     return NULL;
1375
1376   nargs = list_length (DECL_ARGUMENTS (fn));
1377   argarray = XALLOCAVEC (tree, nargs);
1378
1379   defparm = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (fn)));
1380   if (arg2)
1381     defparm = TREE_CHAIN (defparm);
1382
1383   if (TREE_CODE (TREE_TYPE (arg1)) == ARRAY_TYPE)
1384     {
1385       tree inner_type = TREE_TYPE (arg1);
1386       tree start1, end1, p1;
1387       tree start2 = NULL, p2 = NULL;
1388       tree ret = NULL, lab;
1389
1390       start1 = arg1;
1391       start2 = arg2;
1392       do
1393         {
1394           inner_type = TREE_TYPE (inner_type);
1395           start1 = build4 (ARRAY_REF, inner_type, start1,
1396                            size_zero_node, NULL, NULL);
1397           if (arg2)
1398             start2 = build4 (ARRAY_REF, inner_type, start2,
1399                              size_zero_node, NULL, NULL);
1400         }
1401       while (TREE_CODE (inner_type) == ARRAY_TYPE);
1402       start1 = build_fold_addr_expr_loc (input_location, start1);
1403       if (arg2)
1404         start2 = build_fold_addr_expr_loc (input_location, start2);
1405
1406       end1 = TYPE_SIZE_UNIT (TREE_TYPE (arg1));
1407       end1 = fold_build_pointer_plus (start1, end1);
1408
1409       p1 = create_tmp_var (TREE_TYPE (start1), NULL);
1410       t = build2 (MODIFY_EXPR, TREE_TYPE (p1), p1, start1);
1411       append_to_statement_list (t, &ret);
1412
1413       if (arg2)
1414         {
1415           p2 = create_tmp_var (TREE_TYPE (start2), NULL);
1416           t = build2 (MODIFY_EXPR, TREE_TYPE (p2), p2, start2);
1417           append_to_statement_list (t, &ret);
1418         }
1419
1420       lab = create_artificial_label (input_location);
1421       t = build1 (LABEL_EXPR, void_type_node, lab);
1422       append_to_statement_list (t, &ret);
1423
1424       argarray[i++] = p1;
1425       if (arg2)
1426         argarray[i++] = p2;
1427       /* Handle default arguments.  */
1428       for (parm = defparm; parm && parm != void_list_node;
1429            parm = TREE_CHAIN (parm), i++)
1430         argarray[i] = convert_default_arg (TREE_VALUE (parm),
1431                                            TREE_PURPOSE (parm), fn, i,
1432                                            tf_warning_or_error);
1433       t = build_call_a (fn, i, argarray);
1434       t = fold_convert (void_type_node, t);
1435       t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
1436       append_to_statement_list (t, &ret);
1437
1438       t = fold_build_pointer_plus (p1, TYPE_SIZE_UNIT (inner_type));
1439       t = build2 (MODIFY_EXPR, TREE_TYPE (p1), p1, t);
1440       append_to_statement_list (t, &ret);
1441
1442       if (arg2)
1443         {
1444           t = fold_build_pointer_plus (p2, TYPE_SIZE_UNIT (inner_type));
1445           t = build2 (MODIFY_EXPR, TREE_TYPE (p2), p2, t);
1446           append_to_statement_list (t, &ret);
1447         }
1448
1449       t = build2 (NE_EXPR, boolean_type_node, p1, end1);
1450       t = build3 (COND_EXPR, void_type_node, t, build_and_jump (&lab), NULL);
1451       append_to_statement_list (t, &ret);
1452
1453       return ret;
1454     }
1455   else
1456     {
1457       argarray[i++] = build_fold_addr_expr_loc (input_location, arg1);
1458       if (arg2)
1459         argarray[i++] = build_fold_addr_expr_loc (input_location, arg2);
1460       /* Handle default arguments.  */
1461       for (parm = defparm; parm && parm != void_list_node;
1462            parm = TREE_CHAIN (parm), i++)
1463         argarray[i] = convert_default_arg (TREE_VALUE (parm),
1464                                            TREE_PURPOSE (parm),
1465                                            fn, i, tf_warning_or_error);
1466       t = build_call_a (fn, i, argarray);
1467       t = fold_convert (void_type_node, t);
1468       return fold_build_cleanup_point_expr (TREE_TYPE (t), t);
1469     }
1470 }
1471
1472 /* Return code to initialize DECL with its default constructor, or
1473    NULL if there's nothing to do.  */
1474
1475 tree
1476 cxx_omp_clause_default_ctor (tree clause, tree decl, tree /*outer*/)
1477 {
1478   tree info = CP_OMP_CLAUSE_INFO (clause);
1479   tree ret = NULL;
1480
1481   if (info)
1482     ret = cxx_omp_clause_apply_fn (TREE_VEC_ELT (info, 0), decl, NULL);
1483
1484   return ret;
1485 }
1486
1487 /* Return code to initialize DST with a copy constructor from SRC.  */
1488
1489 tree
1490 cxx_omp_clause_copy_ctor (tree clause, tree dst, tree src)
1491 {
1492   tree info = CP_OMP_CLAUSE_INFO (clause);
1493   tree ret = NULL;
1494
1495   if (info)
1496     ret = cxx_omp_clause_apply_fn (TREE_VEC_ELT (info, 0), dst, src);
1497   if (ret == NULL)
1498     ret = build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, src);
1499
1500   return ret;
1501 }
1502
1503 /* Similarly, except use an assignment operator instead.  */
1504
1505 tree
1506 cxx_omp_clause_assign_op (tree clause, tree dst, tree src)
1507 {
1508   tree info = CP_OMP_CLAUSE_INFO (clause);
1509   tree ret = NULL;
1510
1511   if (info)
1512     ret = cxx_omp_clause_apply_fn (TREE_VEC_ELT (info, 2), dst, src);
1513   if (ret == NULL)
1514     ret = build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, src);
1515
1516   return ret;
1517 }
1518
1519 /* Return code to destroy DECL.  */
1520
1521 tree
1522 cxx_omp_clause_dtor (tree clause, tree decl)
1523 {
1524   tree info = CP_OMP_CLAUSE_INFO (clause);
1525   tree ret = NULL;
1526
1527   if (info)
1528     ret = cxx_omp_clause_apply_fn (TREE_VEC_ELT (info, 1), decl, NULL);
1529
1530   return ret;
1531 }
1532
1533 /* True if OpenMP should privatize what this DECL points to rather
1534    than the DECL itself.  */
1535
1536 bool
1537 cxx_omp_privatize_by_reference (const_tree decl)
1538 {
1539   return (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE
1540           || is_invisiref_parm (decl));
1541 }
1542
1543 /* Return true if DECL is const qualified var having no mutable member.  */
1544 bool
1545 cxx_omp_const_qual_no_mutable (tree decl)
1546 {
1547   tree type = TREE_TYPE (decl);
1548   if (TREE_CODE (type) == REFERENCE_TYPE)
1549     {
1550       if (!is_invisiref_parm (decl))
1551         return false;
1552       type = TREE_TYPE (type);
1553
1554       if (TREE_CODE (decl) == RESULT_DECL && DECL_NAME (decl))
1555         {
1556           /* NVR doesn't preserve const qualification of the
1557              variable's type.  */
1558           tree outer = outer_curly_brace_block (current_function_decl);
1559           tree var;
1560
1561           if (outer)
1562             for (var = BLOCK_VARS (outer); var; var = DECL_CHAIN (var))
1563               if (DECL_NAME (decl) == DECL_NAME (var)
1564                   && (TYPE_MAIN_VARIANT (type)
1565                       == TYPE_MAIN_VARIANT (TREE_TYPE (var))))
1566                 {
1567                   if (TYPE_READONLY (TREE_TYPE (var)))
1568                     type = TREE_TYPE (var);
1569                   break;
1570                 }
1571         }
1572     }
1573
1574   if (type == error_mark_node)
1575     return false;
1576
1577   /* Variables with const-qualified type having no mutable member
1578      are predetermined shared.  */
1579   if (TYPE_READONLY (type) && !cp_has_mutable_p (type))
1580     return true;
1581
1582   return false;
1583 }
1584
1585 /* True if OpenMP sharing attribute of DECL is predetermined.  */
1586
1587 enum omp_clause_default_kind
1588 cxx_omp_predetermined_sharing (tree decl)
1589 {
1590   /* Static data members are predetermined shared.  */
1591   if (TREE_STATIC (decl))
1592     {
1593       tree ctx = CP_DECL_CONTEXT (decl);
1594       if (TYPE_P (ctx) && MAYBE_CLASS_TYPE_P (ctx))
1595         return OMP_CLAUSE_DEFAULT_SHARED;
1596     }
1597
1598   /* Const qualified vars having no mutable member are predetermined
1599      shared.  */
1600   if (cxx_omp_const_qual_no_mutable (decl))
1601     return OMP_CLAUSE_DEFAULT_SHARED;
1602
1603   return OMP_CLAUSE_DEFAULT_UNSPECIFIED;
1604 }
1605
1606 /* Finalize an implicitly determined clause.  */
1607
1608 void
1609 cxx_omp_finish_clause (tree c, gimple_seq *)
1610 {
1611   tree decl, inner_type;
1612   bool make_shared = false;
1613
1614   if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_FIRSTPRIVATE)
1615     return;
1616
1617   decl = OMP_CLAUSE_DECL (c);
1618   decl = require_complete_type (decl);
1619   inner_type = TREE_TYPE (decl);
1620   if (decl == error_mark_node)
1621     make_shared = true;
1622   else if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
1623     {
1624       if (is_invisiref_parm (decl))
1625         inner_type = TREE_TYPE (inner_type);
1626       else
1627         {
1628           error ("%qE implicitly determined as %<firstprivate%> has reference type",
1629                  decl);
1630           make_shared = true;
1631         }
1632     }
1633
1634   /* We're interested in the base element, not arrays.  */
1635   while (TREE_CODE (inner_type) == ARRAY_TYPE)
1636     inner_type = TREE_TYPE (inner_type);
1637
1638   /* Check for special function availability by building a call to one.
1639      Save the results, because later we won't be in the right context
1640      for making these queries.  */
1641   if (!make_shared
1642       && CLASS_TYPE_P (inner_type)
1643       && cxx_omp_create_clause_info (c, inner_type, false, true, false, true))
1644     make_shared = true;
1645
1646   if (make_shared)
1647     OMP_CLAUSE_CODE (c) = OMP_CLAUSE_SHARED;
1648 }