Fix PR c++/79360
[official-gcc.git] / gcc / cp / cp-cilkplus.c
blobd147e7eac133f1ea9d81aa4e930811429eaf86ee
1 /* This file is part of the Intel(R) Cilk(TM) Plus support
2 This file contains routines to handle Cilk Plus specific
3 routines for the C++ Compiler.
4 Copyright (C) 2013-2017 Free Software Foundation, Inc.
5 Contributed by Aldy Hernandez <aldyh@redhat.com>.
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it
10 under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3, or (at your option)
12 any later version.
14 GCC is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "cp-tree.h"
27 #include "tree-iterator.h"
28 #include "cilk.h"
29 #include "c-family/c-common.h"
31 /* Return TRUE if T is a FUNCTION_DECL for a type-conversion operator. */
33 static bool
34 is_conversion_operator_function_decl_p (tree t)
36 if (TREE_CODE (t) != FUNCTION_DECL)
37 return false;
39 return DECL_NAME (t) && IDENTIFIER_TYPENAME_P (DECL_NAME (t));
42 /* Recursively traverse EXP to search for a CILK_SPAWN_STMT subtree.
43 Return the CILK_SPAWN_STMT subtree if found; otherwise, the last subtree
44 searched. */
46 static tree
47 find_spawn (tree exp)
49 /* Happens with C++ TARGET_EXPR. */
50 if (exp == NULL_TREE)
51 return exp;
53 if (cilk_ignorable_spawn_rhs_op (exp))
54 return find_spawn (TREE_OPERAND (exp, 0));
56 switch (TREE_CODE (exp))
58 case AGGR_INIT_EXPR:
60 /* Check for initialization via a constructor call that represents
61 implicit conversion. */
62 if (AGGR_INIT_VIA_CTOR_P (exp) && aggr_init_expr_nargs (exp) == 2)
63 return find_spawn (AGGR_INIT_EXPR_ARG (exp, 1));
65 /* Check for initialization via a call to a type-conversion
66 operator. */
67 tree fn = AGGR_INIT_EXPR_FN (exp);
68 if (TREE_CODE (fn) == ADDR_EXPR
69 && is_conversion_operator_function_decl_p (TREE_OPERAND (fn, 0))
70 && aggr_init_expr_nargs (exp) == 1)
71 return find_spawn (AGGR_INIT_EXPR_ARG (exp, 0));
73 break;
75 case CALL_EXPR:
77 /* Check for a call to a type-conversion operator. */
78 tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
79 if (is_conversion_operator_function_decl_p (fndecl)
80 && call_expr_nargs (exp) == 1)
81 return find_spawn (CALL_EXPR_ARG (exp, 0));
83 break;
85 case TARGET_EXPR:
86 return find_spawn (TARGET_EXPR_INITIAL (exp));
88 case CLEANUP_POINT_EXPR:
89 case COMPOUND_EXPR:
90 case EXPR_STMT:
91 return find_spawn (TREE_OPERAND (exp, 0));
93 default:
94 break;
97 return exp;
100 /* Return true if *EXP0 is a recognized form of spawn. Recognized forms
101 are, after conversion to void, a call expression at outer level or an
102 assignment at outer level with the right hand side being a spawned call.
103 In addition to this, it also unwraps the CILK_SPAWN_STMT cover from the
104 CALL_EXPR that is being spawned.
106 Note that `=' in C++ may turn into a CALL_EXPR rather than a
107 MODIFY_EXPR. */
109 bool
110 cilk_cp_detect_spawn_and_unwrap (tree *exp0)
112 tree exp = *exp0;
114 if (!TREE_SIDE_EFFECTS (exp))
115 return false;
117 /* Strip off any conversion to void. It does not affect whether spawn
118 is supported here. */
119 if (TREE_CODE (exp) == CONVERT_EXPR && VOID_TYPE_P (TREE_TYPE (exp)))
120 exp = TREE_OPERAND (exp, 0);
122 if (TREE_CODE (exp) == MODIFY_EXPR || TREE_CODE (exp) == INIT_EXPR)
123 exp = TREE_OPERAND (exp, 1);
125 exp = find_spawn (exp);
126 if (exp == NULL_TREE)
127 return false;
129 /* Now we should have a CALL_EXPR with a CILK_SPAWN_STMT wrapper around
130 it, or return false. */
131 return cilk_recognize_spawn (exp, exp0);
134 /* Callback for cp_walk_tree to validate the body of a pragma simd loop
135 or _cilk_for loop.
137 This function is passed in as a function pointer to walk_tree. *TP is
138 the current tree pointer, *WALK_SUBTREES is set to 0 by this function if
139 recursing into TP's subtrees is unnecessary. *DATA is a bool variable that
140 is set to false if an error has occurred. */
142 static tree
143 cpp_validate_cilk_plus_loop_aux (tree *tp, int *walk_subtrees, void *data)
145 bool *valid = (bool *) data;
147 if (!tp || !*tp)
148 return NULL_TREE;
150 location_t loc = EXPR_LOCATION (*tp);
151 if (TREE_CODE (*tp) == THROW_EXPR)
153 error_at (loc, "throw expressions are not allowed inside loops "
154 "marked with pragma simd");
155 *walk_subtrees = 0;
156 *valid = false;
158 else if (TREE_CODE (*tp) == TRY_BLOCK)
160 error_at (loc, "try statements are not allowed inside loops marked "
161 "with #pragma simd");
162 *valid = false;
163 *walk_subtrees = 0;
165 return NULL_TREE;
169 /* Walks through all the subtrees of BODY using walk_tree to make sure
170 invalid statements/expressions are not found inside BODY. Returns
171 false if any invalid statements are found. */
173 bool
174 cpp_validate_cilk_plus_loop (tree body)
176 bool valid = true;
177 cp_walk_tree (&body, cpp_validate_cilk_plus_loop_aux,
178 (void *) &valid, NULL);
179 return valid;
182 /* Sets the EXCEPTION bit (0x10) in the FRAME.flags field. */
184 static tree
185 set_cilk_except_flag (tree frame)
187 tree flags = cilk_dot (frame, CILK_TI_FRAME_FLAGS, 0);
189 flags = build2 (MODIFY_EXPR, void_type_node, flags,
190 build2 (BIT_IOR_EXPR, TREE_TYPE (flags), flags,
191 build_int_cst (TREE_TYPE (flags),
192 CILK_FRAME_EXCEPTING)));
193 return flags;
196 /* Sets the frame.EXCEPT_DATA field to the head of the exception pointer. */
198 static tree
199 set_cilk_except_data (tree frame)
201 tree except_data = cilk_dot (frame, CILK_TI_FRAME_EXCEPTION, 0);
202 tree uresume_fn = builtin_decl_implicit (BUILT_IN_EH_POINTER);
203 tree ret_expr;
204 uresume_fn = build_call_expr (uresume_fn, 1,
205 build_int_cst (integer_type_node, 0));
206 ret_expr = build2 (MODIFY_EXPR, void_type_node, except_data, uresume_fn);
207 return ret_expr;
210 /* Installs BODY into function FNDECL with appropriate exception handling
211 code. WD holds information of wrapper function used to pass into the
212 outlining function, cilk_outline. */
214 void
215 cilk_install_body_with_frame_cleanup (tree fndecl, tree orig_body, void *wd)
217 tree frame = make_cilk_frame (fndecl);
218 tree dtor = create_cilk_function_exit (frame, false, false);
219 add_local_decl (cfun, frame);
221 cfun->language = ggc_cleared_alloc<language_function> ();
223 location_t loc = EXPR_LOCATION (orig_body);
224 tree list = alloc_stmt_list ();
225 DECL_SAVED_TREE (fndecl) = list;
226 tree fptr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (frame)), frame);
227 tree body = cilk_install_body_pedigree_operations (fptr);
228 gcc_assert (TREE_CODE (body) == STATEMENT_LIST);
229 tree detach_expr = build_call_expr (cilk_detach_fndecl, 1, fptr);
230 append_to_statement_list (detach_expr, &body);
231 cilk_outline (fndecl, &orig_body, (struct wrapper_data *) wd);
232 append_to_statement_list (orig_body, &body);
233 if (flag_exceptions)
235 tree except_flag = set_cilk_except_flag (frame);
236 tree except_data = set_cilk_except_data (frame);
237 tree catch_list = alloc_stmt_list ();
238 append_to_statement_list (except_flag, &catch_list);
239 append_to_statement_list (except_data, &catch_list);
240 body = create_try_catch_expr (body, catch_list);
242 append_to_statement_list (build_stmt (loc, TRY_FINALLY_EXPR, body, dtor),
243 &list);