Introduce gimple_phi and use it in various places
[official-gcc.git] / gcc / cp / cp-array-notation.c
blob20f741aaebc37d63735f6752a4dbb3975f39941d
1 /* This file is part of the Intel(R) Cilk(TM) Plus support
2 It contains routines to handle Array Notation expression
3 handling routines in the C++ Compiler.
4 Copyright (C) 2013-2014 Free Software Foundation, Inc.
5 Contributed by Balaji V. Iyer <balaji.v.iyer@intel.com>,
6 Intel Corporation
8 This file is part of GCC.
10 GCC is free software; you can redistribute it and/or modify it
11 under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3, or (at your option)
13 any later version.
15 GCC is distributed in the hope that it will be useful, but
16 WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with GCC; see the file COPYING3. If not see
22 <http://www.gnu.org/licenses/>. */
24 /* The Array Notation Transformation Technique:
26 An array notation expression has 4 major components:
27 1. The array name
28 2. Start Index
29 3. Number of elements we need to access (we call it length)
30 4. Stride
32 So, if we have something like A[0:5:2], we are accessing A[0], A[2], A[4],
33 A[6] and A[8]. The user is responsible to make sure the access length does
34 not step outside the array's size.
36 In this section, I highlight the overall method on how array notations are
37 broken up into C/C++ code. Almost all the functions follows this step:
39 Let's say the user has used the array notation in a statement like this:
41 A[St1:Ln:Str1] = B[St2:Ln:Str2] + <NON ARRAY_NOT STMT>
43 where St{1,2} = Starting index, Ln = Number of elements we need to access,
44 and Str{1,2} = the stride.
45 Note: The length of both the array notation expressions must be the same.
47 The above expression is broken into the following:
49 for (Tmp_Var = 0; Tmp_Var < Ln; Tmp_Var++)
50 A[St1 + Tmp_Var * Str1] = B[St1 + Tmp_Var * Str2] + <NON_ARRAY_NOT_STMT>;
53 #include "config.h"
54 #include "system.h"
55 #include "coretypes.h"
56 #include "tree.h"
57 #include "cp-tree.h"
58 #include "c-family/c-common.h"
59 #include "diagnostic.h"
60 #include "tree-iterator.h"
61 #include "vec.h"
63 /* Creates a FOR_STMT with INIT, COND, INCR and BODY as the initializer,
64 condition, increment expression and the loop-body, respectively. */
66 static void
67 create_an_loop (tree init, tree cond, tree incr, tree body)
69 tree for_stmt;
71 finish_expr_stmt (init);
72 for_stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
73 finish_for_init_stmt (for_stmt);
74 finish_for_cond (cond, for_stmt, false);
75 finish_for_expr (incr, for_stmt);
76 finish_expr_stmt (body);
77 finish_for_stmt (for_stmt);
80 /* If *VALUE is not a constant integer, then this function replaces it with
81 a variable to make it loop invariant for array notations. */
83 static inline void
84 make_triplet_val_inv (tree *value)
86 if (TREE_CODE (*value) != INTEGER_CST
87 && TREE_CODE (*value) != PARM_DECL
88 && TREE_CODE (*value) != VAR_DECL)
89 *value = get_temp_regvar (ptrdiff_type_node, *value);
92 /* Returns a vector of size RANK that contains an ARRAY_REF. This vector is
93 created using array notation-triplet information stored in AN_INFO. The
94 induction var is taken from AN_LOOP_INFO.
96 For example: For an array notation A[5:10:2], the vector start will be
97 of size 1 holding '5', stride of same size as start but holding the value of
98 as 2, and is_vector as true. Let's assume VAR is 'x'
99 This function returns a vector of size 1 with the following data:
100 A[5 + (x * 2)] .
103 static vec<tree, va_gc> *
104 create_array_refs (location_t loc, vec<vec<an_parts> > an_info,
105 vec<an_loop_parts> an_loop_info, size_t size, size_t rank)
107 tree ind_mult, ind_incr;
108 vec<tree, va_gc> *array_operand = NULL;
110 for (size_t ii = 0; ii < size; ii++)
111 if (an_info[ii][0].is_vector)
113 tree array_opr = an_info[ii][rank - 1].value;
114 for (int s_jj = rank -1; s_jj >= 0; s_jj--)
116 tree start = cp_fold_convert (ptrdiff_type_node,
117 an_info[ii][s_jj].start);
118 tree stride = cp_fold_convert (ptrdiff_type_node,
119 an_info[ii][s_jj].stride);
120 tree var = cp_fold_convert (ptrdiff_type_node,
121 an_loop_info[s_jj].var);
123 ind_mult = build2 (MULT_EXPR, TREE_TYPE (var), var, stride);
124 ind_incr = build2 (PLUS_EXPR, TREE_TYPE (var), start, ind_mult);
125 /* Array [ start_index + (induction_var * stride)] */
126 array_opr = grok_array_decl (loc, array_opr, ind_incr, false);
128 vec_safe_push (array_operand, array_opr);
130 else
131 vec_safe_push (array_operand, integer_one_node);
132 return array_operand;
135 /* Populates the INCR and CMP fields in *NODE with the increment
136 (of type POSTINCREMENT) and comparison (of TYPE LT_EXPR) expressions, using
137 data from AN_INFO. */
139 void
140 create_cmp_incr (location_t loc, vec <an_loop_parts> *node, size_t rank,
141 vec<vec<an_parts> > an_info, tsubst_flags_t complain)
143 for (size_t ii = 0; ii < rank; ii++)
145 (*node)[ii].incr = build_x_unary_op (loc, POSTINCREMENT_EXPR,
146 (*node)[ii].var, complain);
147 (*node)[ii].cmp = build_x_binary_op (loc, LT_EXPR, (*node)[ii].var,
148 TREE_CODE ((*node)[ii].var),
149 an_info[0][ii].length,
150 TREE_CODE (an_info[0][ii].length),
151 NULL, complain);
155 /* Replaces all the scalar expressions in *NODE. Returns a STATEMENT LIST that
156 holds the NODE along with the variables that hold the results of the
157 invariant expressions. */
159 static tree
160 replace_invariant_exprs (tree *node)
162 size_t ix = 0;
163 tree node_list = NULL_TREE;
164 tree t = NULL_TREE, new_var = NULL_TREE;
165 struct inv_list data;
167 data.list_values = NULL;
168 data.replacement = NULL;
169 data.additional_tcodes = NULL;
170 cp_walk_tree (node, find_inv_trees, (void *) &data, NULL);
172 if (vec_safe_length (data.list_values))
174 node_list = push_stmt_list ();
175 for (ix = 0; vec_safe_iterate (data.list_values, ix, &t); ix++)
177 /* Sometimes, when comma_expr has a function call in it, it will
178 typecast it to void. Find_inv_trees finds those nodes and so
179 if it void type, then don't bother creating a new var to hold
180 the return value. */
181 if (VOID_TYPE_P (TREE_TYPE (t)))
183 finish_expr_stmt (t);
184 new_var = void_node;
186 else
187 new_var = get_temp_regvar (TREE_TYPE (t), t);
188 vec_safe_push (data.replacement, new_var);
190 cp_walk_tree (node, replace_inv_trees, (void *) &data, NULL);
191 node_list = pop_stmt_list (node_list);
193 return node_list;
196 /* Replace array notation's built-in function passed in AN_BUILTIN_FN with
197 the appropriate loop and computation (all stored in variable LOOP of type
198 tree node). The output of the function function is always a scalar and that
199 result is returned in *NEW_VAR. *NEW_VAR is NULL_TREE if the function is
200 __sec_reduce_mutating. */
202 static tree
203 expand_sec_reduce_builtin (tree an_builtin_fn, tree *new_var)
205 tree new_var_type = NULL_TREE, func_parm, new_yes_expr, new_no_expr;
206 tree array_ind_value = NULL_TREE, new_no_ind, new_yes_ind, new_no_list;
207 tree new_yes_list, new_cond_expr, new_expr = NULL_TREE;
208 vec<tree, va_gc> *array_list = NULL, *array_operand = NULL;
209 size_t list_size = 0, rank = 0, ii = 0;
210 tree body, an_init, loop_with_init = alloc_stmt_list ();
211 tree array_op0, comp_node = NULL_TREE;
212 tree call_fn = NULL_TREE, identity_value = NULL_TREE;
213 tree init = NULL_TREE, cond_init = NULL_TREE;
214 enum tree_code code = NOP_EXPR;
215 location_t location = UNKNOWN_LOCATION;
216 vec<vec<an_parts> > an_info = vNULL;
217 vec<an_loop_parts> an_loop_info = vNULL;
218 enum built_in_function an_type =
219 is_cilkplus_reduce_builtin (CALL_EXPR_FN (an_builtin_fn));
220 vec <tree, va_gc> *func_args;
222 if (an_type == BUILT_IN_NONE)
223 return NULL_TREE;
225 if (an_type != BUILT_IN_CILKPLUS_SEC_REDUCE
226 && an_type != BUILT_IN_CILKPLUS_SEC_REDUCE_MUTATING)
227 func_parm = CALL_EXPR_ARG (an_builtin_fn, 0);
228 else
230 call_fn = CALL_EXPR_ARG (an_builtin_fn, 2);
232 /* We need to do this because we are "faking" the builtin function types,
233 so the compiler does a bunch of typecasts and this will get rid of
234 all that! */
235 STRIP_NOPS (call_fn);
236 if (TREE_CODE (call_fn) != OVERLOAD
237 && TREE_CODE (call_fn) != FUNCTION_DECL)
238 call_fn = TREE_OPERAND (call_fn, 0);
239 identity_value = CALL_EXPR_ARG (an_builtin_fn, 0);
240 func_parm = CALL_EXPR_ARG (an_builtin_fn, 1);
241 STRIP_NOPS (identity_value);
243 STRIP_NOPS (func_parm);
245 location = EXPR_LOCATION (an_builtin_fn);
247 /* Note about using find_rank (): If find_rank returns false, then it must
248 have already reported an error, thus we just return an error_mark_node
249 without any doing any error emission. */
250 if (!find_rank (location, an_builtin_fn, an_builtin_fn, true, &rank))
251 return error_mark_node;
252 if (rank == 0)
254 error_at (location, "Invalid builtin arguments");
255 return error_mark_node;
257 else if (rank > 1
258 && (an_type == BUILT_IN_CILKPLUS_SEC_REDUCE_MAX_IND
259 || an_type == BUILT_IN_CILKPLUS_SEC_REDUCE_MIN_IND))
261 error_at (location, "__sec_reduce_min_ind or __sec_reduce_max_ind cannot "
262 "have arrays with dimension greater than 1");
263 return error_mark_node;
266 extract_array_notation_exprs (func_parm, true, &array_list);
267 list_size = vec_safe_length (array_list);
268 switch (an_type)
270 case BUILT_IN_CILKPLUS_SEC_REDUCE_ADD:
271 case BUILT_IN_CILKPLUS_SEC_REDUCE_MUL:
272 case BUILT_IN_CILKPLUS_SEC_REDUCE_MAX:
273 case BUILT_IN_CILKPLUS_SEC_REDUCE_MIN:
274 new_var_type = TREE_TYPE ((*array_list)[0]);
275 break;
276 case BUILT_IN_CILKPLUS_SEC_REDUCE_ALL_ZERO:
277 case BUILT_IN_CILKPLUS_SEC_REDUCE_ANY_ZERO:
278 case BUILT_IN_CILKPLUS_SEC_REDUCE_ANY_NONZERO:
279 case BUILT_IN_CILKPLUS_SEC_REDUCE_ALL_NONZERO:
280 new_var_type = boolean_type_node;
281 break;
282 case BUILT_IN_CILKPLUS_SEC_REDUCE_MAX_IND:
283 case BUILT_IN_CILKPLUS_SEC_REDUCE_MIN_IND:
284 new_var_type = size_type_node;
285 break;
286 case BUILT_IN_CILKPLUS_SEC_REDUCE:
287 if (call_fn && identity_value)
288 new_var_type = TREE_TYPE ((*array_list)[0]);
289 break;
290 case BUILT_IN_CILKPLUS_SEC_REDUCE_MUTATING:
291 new_var_type = NULL_TREE;
292 break;
293 default:
294 gcc_unreachable ();
297 if (new_var_type && TREE_CODE (new_var_type) == ARRAY_TYPE)
298 new_var_type = TREE_TYPE (new_var_type);
299 an_loop_info.safe_grow_cleared (rank);
301 an_init = push_stmt_list ();
303 /* Assign the array notation components to variable so that they can satisfy
304 the exec-once rule. */
305 for (ii = 0; ii < list_size; ii++)
306 if (TREE_CODE ((*array_list)[ii]) == ARRAY_NOTATION_REF)
308 tree anode = (*array_list)[ii];
309 make_triplet_val_inv (&ARRAY_NOTATION_START (anode));
310 make_triplet_val_inv (&ARRAY_NOTATION_LENGTH (anode));
311 make_triplet_val_inv (&ARRAY_NOTATION_STRIDE (anode));
313 cilkplus_extract_an_triplets (array_list, list_size, rank, &an_info);
314 for (ii = 0; ii < rank; ii++)
316 tree typ = ptrdiff_type_node;
318 /* In this place, we are using get_temp_regvar instead of
319 create_temporary_var if an_type is SEC_REDUCE_MAX/MIN_IND because
320 the array_ind_value depends on this value being initalized to 0. */
321 if (an_type == BUILT_IN_CILKPLUS_SEC_REDUCE_MAX_IND
322 || an_type == BUILT_IN_CILKPLUS_SEC_REDUCE_MIN_IND)
323 an_loop_info[ii].var = get_temp_regvar (typ, build_zero_cst (typ));
324 else
326 an_loop_info[ii].var = create_temporary_var (typ);
327 add_decl_expr (an_loop_info[ii].var);
329 an_loop_info[ii].ind_init =
330 build_x_modify_expr (location, an_loop_info[ii].var, INIT_EXPR,
331 build_zero_cst (typ), tf_warning_or_error);
333 array_operand = create_array_refs (location, an_info, an_loop_info,
334 list_size, rank);
335 replace_array_notations (&func_parm, true, array_list, array_operand);
337 if (!TREE_TYPE (func_parm))
338 TREE_TYPE (func_parm) = TREE_TYPE ((*array_list)[0]);
340 create_cmp_incr (location, &an_loop_info, rank, an_info, tf_warning_or_error);
341 if (an_type == BUILT_IN_CILKPLUS_SEC_REDUCE_MAX_IND
342 || an_type == BUILT_IN_CILKPLUS_SEC_REDUCE_MIN_IND)
343 array_ind_value = get_temp_regvar (TREE_TYPE (func_parm), func_parm);
345 array_op0 = (*array_operand)[0];
346 if (TREE_CODE (array_op0) == INDIRECT_REF)
347 array_op0 = TREE_OPERAND (array_op0, 0);
348 switch (an_type)
350 case BUILT_IN_CILKPLUS_SEC_REDUCE_ADD:
351 code = PLUS_EXPR;
352 init = build_zero_cst (new_var_type);
353 break;
354 case BUILT_IN_CILKPLUS_SEC_REDUCE_MUL:
355 code = MULT_EXPR;
356 init = build_one_cst (new_var_type);
357 break;
358 case BUILT_IN_CILKPLUS_SEC_REDUCE_ANY_ZERO:
359 case BUILT_IN_CILKPLUS_SEC_REDUCE_ANY_NONZERO:
360 code = ((an_type == BUILT_IN_CILKPLUS_SEC_REDUCE_ANY_ZERO) ? EQ_EXPR
361 : NE_EXPR);
362 init = build_zero_cst (new_var_type);
363 cond_init = build_one_cst (new_var_type);
364 comp_node = build_zero_cst (TREE_TYPE (func_parm));
365 break;
366 case BUILT_IN_CILKPLUS_SEC_REDUCE_ALL_ZERO:
367 case BUILT_IN_CILKPLUS_SEC_REDUCE_ALL_NONZERO:
368 code = ((an_type == BUILT_IN_CILKPLUS_SEC_REDUCE_ALL_ZERO) ? NE_EXPR
369 : EQ_EXPR);
370 init = build_one_cst (new_var_type);
371 cond_init = build_zero_cst (new_var_type);
372 comp_node = build_zero_cst (TREE_TYPE (func_parm));
373 break;
374 case BUILT_IN_CILKPLUS_SEC_REDUCE_MAX:
375 code = MAX_EXPR;
376 init = (TYPE_MIN_VALUE (new_var_type) ? TYPE_MIN_VALUE (new_var_type)
377 : func_parm);
378 break;
379 case BUILT_IN_CILKPLUS_SEC_REDUCE_MIN:
380 code = MIN_EXPR;
381 init = (TYPE_MAX_VALUE (new_var_type) ? TYPE_MAX_VALUE (new_var_type)
382 : func_parm);
383 break;
384 case BUILT_IN_CILKPLUS_SEC_REDUCE_MIN_IND:
385 case BUILT_IN_CILKPLUS_SEC_REDUCE_MAX_IND:
386 code = (an_type == BUILT_IN_CILKPLUS_SEC_REDUCE_MAX_IND ? LE_EXPR
387 : GE_EXPR);
388 init = an_loop_info[0].var;
389 break;
390 case BUILT_IN_CILKPLUS_SEC_REDUCE:
391 init = identity_value;
392 break;
393 case BUILT_IN_CILKPLUS_SEC_REDUCE_MUTATING:
394 init = NULL_TREE;
395 break;
396 default:
397 gcc_unreachable ();
400 if (an_type != BUILT_IN_CILKPLUS_SEC_REDUCE_MUTATING)
401 *new_var = get_temp_regvar (new_var_type, init);
402 else
403 *new_var = NULL_TREE;
405 switch (an_type)
407 case BUILT_IN_CILKPLUS_SEC_REDUCE_ADD:
408 case BUILT_IN_CILKPLUS_SEC_REDUCE_MUL:
409 new_expr = build_x_modify_expr (location, *new_var, code, func_parm,
410 tf_warning_or_error);
411 break;
412 case BUILT_IN_CILKPLUS_SEC_REDUCE_ALL_ZERO:
413 case BUILT_IN_CILKPLUS_SEC_REDUCE_ALL_NONZERO:
414 case BUILT_IN_CILKPLUS_SEC_REDUCE_ANY_ZERO:
415 case BUILT_IN_CILKPLUS_SEC_REDUCE_ANY_NONZERO:
416 /* In all these cases, assume the false case is true and as soon as
417 we find a true case, set the true flag on and latch it in. */
418 new_yes_expr = build_x_modify_expr (location, *new_var, NOP_EXPR,
419 cond_init, tf_warning_or_error);
420 new_no_expr = build_x_modify_expr (location, *new_var, NOP_EXPR,
421 *new_var, tf_warning_or_error);
422 new_cond_expr = build_x_binary_op
423 (location, code, func_parm, TREE_CODE (func_parm), comp_node,
424 TREE_CODE (comp_node), NULL, tf_warning_or_error);
425 new_expr = build_x_conditional_expr (location, new_cond_expr,
426 new_yes_expr, new_no_expr,
427 tf_warning_or_error);
428 break;
429 case BUILT_IN_CILKPLUS_SEC_REDUCE_MAX:
430 case BUILT_IN_CILKPLUS_SEC_REDUCE_MIN:
431 new_cond_expr = build_x_binary_op
432 (location, code, *new_var, TREE_CODE (*new_var), func_parm,
433 TREE_CODE (func_parm), NULL, tf_warning_or_error);
434 new_expr = build_x_modify_expr (location, *new_var, NOP_EXPR, func_parm,
435 tf_warning_or_error);
436 break;
437 case BUILT_IN_CILKPLUS_SEC_REDUCE_MAX_IND:
438 case BUILT_IN_CILKPLUS_SEC_REDUCE_MIN_IND:
439 new_yes_expr = build_x_modify_expr (location, array_ind_value, NOP_EXPR,
440 func_parm, tf_warning_or_error);
441 new_no_expr = build_x_modify_expr (location, array_ind_value, NOP_EXPR,
442 array_ind_value, tf_warning_or_error);
443 if (list_size > 1)
444 new_yes_ind = build_x_modify_expr (location, *new_var, NOP_EXPR,
445 an_loop_info[0].var,
446 tf_warning_or_error);
447 else
448 new_yes_ind = build_x_modify_expr (location, *new_var, NOP_EXPR,
449 TREE_OPERAND (array_op0, 1),
450 tf_warning_or_error);
451 new_no_ind = build_x_modify_expr (location, *new_var, NOP_EXPR, *new_var,
452 tf_warning_or_error);
453 new_yes_list = alloc_stmt_list ();
454 append_to_statement_list (new_yes_ind, &new_yes_list);
455 append_to_statement_list (new_yes_expr, &new_yes_list);
457 new_no_list = alloc_stmt_list ();
458 append_to_statement_list (new_no_ind, &new_no_list);
459 append_to_statement_list (new_no_expr, &new_no_list);
461 new_cond_expr = build_x_binary_op (location, code, array_ind_value,
462 TREE_CODE (array_ind_value), func_parm,
463 TREE_CODE (func_parm), NULL,
464 tf_warning_or_error);
465 new_expr = build_x_conditional_expr (location, new_cond_expr,
466 new_yes_list, new_no_list,
467 tf_warning_or_error);
468 break;
469 case BUILT_IN_CILKPLUS_SEC_REDUCE:
470 case BUILT_IN_CILKPLUS_SEC_REDUCE_MUTATING:
471 func_args = make_tree_vector ();
472 if (an_type == BUILT_IN_CILKPLUS_SEC_REDUCE)
473 vec_safe_push (func_args, *new_var);
474 else
475 vec_safe_push (func_args, identity_value);
476 vec_safe_push (func_args, func_parm);
478 new_expr = finish_call_expr (call_fn, &func_args, false, true,
479 tf_warning_or_error);
480 if (an_type == BUILT_IN_CILKPLUS_SEC_REDUCE)
481 new_expr = build_x_modify_expr (location, *new_var, NOP_EXPR, new_expr,
482 tf_warning_or_error);
483 release_tree_vector (func_args);
484 break;
485 default:
486 gcc_unreachable ();
488 an_init = pop_stmt_list (an_init);
489 append_to_statement_list (an_init, &loop_with_init);
490 body = new_expr;
492 for (ii = 0; ii < rank; ii++)
494 tree new_loop = push_stmt_list ();
495 create_an_loop (an_loop_info[ii].ind_init, an_loop_info[ii].cmp,
496 an_loop_info[ii].incr, body);
497 body = pop_stmt_list (new_loop);
499 append_to_statement_list (body, &loop_with_init);
501 an_info.release ();
502 an_loop_info.release ();
504 return loop_with_init;
507 /* Returns a loop with ARRAY_REF inside it with an appropriate modify expr.
508 The LHS and/or RHS will be array notation expressions that have a
509 MODIFYCODE. The location of the variable is specified by LOCATION. */
511 static tree
512 expand_an_in_modify_expr (location_t location, tree lhs,
513 enum tree_code modifycode, tree rhs,
514 tsubst_flags_t complain)
516 tree array_expr_lhs = NULL_TREE, array_expr_rhs = NULL_TREE;
517 tree array_expr = NULL_TREE;
518 tree body = NULL_TREE;
519 vec<tree> cond_expr = vNULL;
520 vec<tree, va_gc> *lhs_array_operand = NULL, *rhs_array_operand = NULL;
521 size_t lhs_rank = 0, rhs_rank = 0, ii = 0;
522 vec<tree, va_gc> *rhs_list = NULL, *lhs_list = NULL;
523 size_t rhs_list_size = 0, lhs_list_size = 0;
524 tree new_modify_expr, new_var = NULL_TREE, builtin_loop, scalar_mods;
525 bool found_builtin_fn = false;
526 tree an_init, loop_with_init = alloc_stmt_list ();
527 vec<vec<an_parts> > lhs_an_info = vNULL, rhs_an_info = vNULL;
528 vec<an_loop_parts> lhs_an_loop_info = vNULL, rhs_an_loop_info = vNULL;
530 if (!find_rank (location, rhs, rhs, false, &rhs_rank))
531 return error_mark_node;
532 extract_array_notation_exprs (rhs, false, &rhs_list);
533 rhs_list_size = vec_safe_length (rhs_list);
534 an_init = push_stmt_list ();
535 if (rhs_rank)
537 scalar_mods = replace_invariant_exprs (&rhs);
538 if (scalar_mods)
539 finish_expr_stmt (scalar_mods);
541 for (ii = 0; ii < rhs_list_size; ii++)
543 tree rhs_node = (*rhs_list)[ii];
544 if (TREE_CODE (rhs_node) == CALL_EXPR)
546 builtin_loop = expand_sec_reduce_builtin (rhs_node, &new_var);
547 if (builtin_loop == error_mark_node)
548 return error_mark_node;
549 else if (builtin_loop)
551 finish_expr_stmt (builtin_loop);
552 found_builtin_fn = true;
553 if (new_var)
555 vec <tree, va_gc> *rhs_sub_list = NULL, *new_var_list = NULL;
556 vec_safe_push (rhs_sub_list, rhs_node);
557 vec_safe_push (new_var_list, new_var);
558 replace_array_notations (&rhs, false, rhs_sub_list,
559 new_var_list);
564 lhs_rank = 0;
565 rhs_rank = 0;
566 if (!find_rank (location, lhs, lhs, true, &lhs_rank)
567 || !find_rank (location, rhs, rhs, true, &rhs_rank))
569 pop_stmt_list (an_init);
570 return error_mark_node;
573 /* If both are scalar, then the only reason why we will get this far is if
574 there is some array notations inside it and was using a builtin array
575 notation functions. If so, we have already broken those guys up and now
576 a simple build_x_modify_expr would do. */
577 if (lhs_rank == 0 && rhs_rank == 0)
579 if (found_builtin_fn)
581 new_modify_expr = build_x_modify_expr (location, lhs,
582 modifycode, rhs, complain);
583 finish_expr_stmt (new_modify_expr);
584 pop_stmt_list (an_init);
585 return an_init;
587 else
588 gcc_unreachable ();
591 /* If for some reason location is not set, then find if LHS or RHS has
592 location info. If so, then use that so we atleast have an idea. */
593 if (location == UNKNOWN_LOCATION)
595 if (EXPR_LOCATION (lhs) != UNKNOWN_LOCATION)
596 location = EXPR_LOCATION (lhs);
597 else if (EXPR_LOCATION (rhs) != UNKNOWN_LOCATION)
598 location = EXPR_LOCATION (rhs);
601 /* We need this when we have a scatter issue. */
602 extract_array_notation_exprs (lhs, true, &lhs_list);
603 rhs_list = NULL;
604 extract_array_notation_exprs (rhs, true, &rhs_list);
605 rhs_list_size = vec_safe_length (rhs_list);
606 lhs_list_size = vec_safe_length (lhs_list);
608 if (lhs_rank == 0 && rhs_rank != 0)
610 error_at (location, "%qE cannot be scalar when %qE is not", lhs, rhs);
611 return error_mark_node;
613 if (lhs_rank != 0 && rhs_rank != 0 && lhs_rank != rhs_rank)
615 error_at (location, "rank mismatch between %qE and %qE", lhs, rhs);
616 return error_mark_node;
619 /* Assign the array notation components to variable so that they can satisfy
620 the execute-once rule. */
621 for (ii = 0; ii < lhs_list_size; ii++)
623 tree anode = (*lhs_list)[ii];
624 make_triplet_val_inv (&ARRAY_NOTATION_START (anode));
625 make_triplet_val_inv (&ARRAY_NOTATION_LENGTH (anode));
626 make_triplet_val_inv (&ARRAY_NOTATION_STRIDE (anode));
628 for (ii = 0; ii < rhs_list_size; ii++)
629 if ((*rhs_list)[ii] && TREE_CODE ((*rhs_list)[ii]) == ARRAY_NOTATION_REF)
631 tree aa = (*rhs_list)[ii];
632 make_triplet_val_inv (&ARRAY_NOTATION_START (aa));
633 make_triplet_val_inv (&ARRAY_NOTATION_LENGTH (aa));
634 make_triplet_val_inv (&ARRAY_NOTATION_STRIDE (aa));
636 lhs_an_loop_info.safe_grow_cleared (lhs_rank);
638 if (rhs_rank)
639 rhs_an_loop_info.safe_grow_cleared (rhs_rank);
641 cond_expr.safe_grow_cleared (MAX (lhs_rank, rhs_rank));
642 cilkplus_extract_an_triplets (lhs_list, lhs_list_size, lhs_rank,
643 &lhs_an_info);
644 if (rhs_list)
645 cilkplus_extract_an_triplets (rhs_list, rhs_list_size, rhs_rank,
646 &rhs_an_info);
647 if (length_mismatch_in_expr_p (EXPR_LOCATION (lhs), lhs_an_info)
648 || (rhs_list && length_mismatch_in_expr_p (EXPR_LOCATION (rhs),
649 rhs_an_info)))
651 pop_stmt_list (an_init);
652 return error_mark_node;
654 tree rhs_len = ((rhs_list_size > 0 && rhs_rank > 0) ?
655 rhs_an_info[0][0].length : NULL_TREE);
656 tree lhs_len = ((lhs_list_size > 0 && lhs_rank > 0) ?
657 lhs_an_info[0][0].length : NULL_TREE);
658 if (lhs_list_size > 0 && rhs_list_size > 0 && lhs_rank > 0 && rhs_rank > 0
659 && TREE_CODE (lhs_len) == INTEGER_CST && rhs_len
660 && TREE_CODE (rhs_len) == INTEGER_CST
661 && !tree_int_cst_equal (rhs_len, lhs_len))
663 error_at (location, "length mismatch between LHS and RHS");
664 pop_stmt_list (an_init);
665 return error_mark_node;
667 for (ii = 0; ii < lhs_rank; ii++)
669 tree typ = ptrdiff_type_node;
670 lhs_an_loop_info[ii].var = create_temporary_var (typ);
671 add_decl_expr (lhs_an_loop_info[ii].var);
672 lhs_an_loop_info[ii].ind_init = build_x_modify_expr
673 (location, lhs_an_loop_info[ii].var, INIT_EXPR, build_zero_cst (typ),
674 complain);
677 if (rhs_list_size > 0)
679 rhs_array_operand = fix_sec_implicit_args (location, rhs_list,
680 lhs_an_loop_info, lhs_rank,
681 lhs);
682 if (!rhs_array_operand)
683 return error_mark_node;
685 replace_array_notations (&rhs, true, rhs_list, rhs_array_operand);
686 rhs_list_size = 0;
687 rhs_list = NULL;
688 extract_array_notation_exprs (rhs, true, &rhs_list);
689 rhs_list_size = vec_safe_length (rhs_list);
691 for (ii = 0; ii < rhs_rank; ii++)
693 tree typ = ptrdiff_type_node;
694 rhs_an_loop_info[ii].var = create_temporary_var (typ);
695 add_decl_expr (rhs_an_loop_info[ii].var);
696 rhs_an_loop_info[ii].ind_init = build_x_modify_expr
697 (location, rhs_an_loop_info[ii].var, INIT_EXPR, build_zero_cst (typ),
698 complain);
701 if (lhs_rank)
703 lhs_array_operand =
704 create_array_refs (location, lhs_an_info, lhs_an_loop_info,
705 lhs_list_size, lhs_rank);
706 replace_array_notations (&lhs, true, lhs_list, lhs_array_operand);
709 if (rhs_array_operand)
710 vec_safe_truncate (rhs_array_operand, 0);
711 if (rhs_rank)
713 rhs_array_operand = create_array_refs (location, rhs_an_info,
714 rhs_an_loop_info, rhs_list_size,
715 rhs_rank);
716 /* Replace all the array refs created by the above function because this
717 variable is blown away by the fix_sec_implicit_args function below. */
718 replace_array_notations (&rhs, true, rhs_list, rhs_array_operand);
719 vec_safe_truncate (rhs_array_operand , 0);
720 rhs_array_operand = fix_sec_implicit_args (location, rhs_list,
721 rhs_an_loop_info, rhs_rank,
722 rhs);
723 if (!rhs_array_operand)
724 return error_mark_node;
725 replace_array_notations (&rhs, true, rhs_list, rhs_array_operand);
728 array_expr_rhs = rhs;
729 array_expr_lhs = lhs;
731 array_expr = build_x_modify_expr (location, array_expr_lhs, modifycode,
732 array_expr_rhs, complain);
733 create_cmp_incr (location, &lhs_an_loop_info, lhs_rank, lhs_an_info,
734 complain);
735 if (rhs_rank)
736 create_cmp_incr (location, &rhs_an_loop_info, rhs_rank, rhs_an_info,
737 complain);
738 for (ii = 0; ii < MAX (rhs_rank, lhs_rank); ii++)
739 if (ii < lhs_rank && ii < rhs_rank)
740 cond_expr[ii] = build_x_binary_op
741 (location, TRUTH_ANDIF_EXPR, lhs_an_loop_info[ii].cmp,
742 TREE_CODE (lhs_an_loop_info[ii].cmp), rhs_an_loop_info[ii].cmp,
743 TREE_CODE (rhs_an_loop_info[ii].cmp), NULL, complain);
744 else if (ii < lhs_rank && ii >= rhs_rank)
745 cond_expr[ii] = lhs_an_loop_info[ii].cmp;
746 else
747 /* No need to compare ii < rhs_rank && ii >= lhs_rank because in a valid
748 Array notation expression, rank of RHS cannot be greater than LHS. */
749 gcc_unreachable ();
751 an_init = pop_stmt_list (an_init);
752 append_to_statement_list (an_init, &loop_with_init);
753 body = array_expr;
754 for (ii = 0; ii < MAX (lhs_rank, rhs_rank); ii++)
756 tree incr_list = alloc_stmt_list ();
757 tree init_list = alloc_stmt_list ();
758 tree new_loop = push_stmt_list ();
760 if (lhs_rank)
762 append_to_statement_list (lhs_an_loop_info[ii].ind_init, &init_list);
763 append_to_statement_list (lhs_an_loop_info[ii].incr, &incr_list);
765 if (rhs_rank)
767 append_to_statement_list (rhs_an_loop_info[ii].ind_init, &init_list);
768 append_to_statement_list (rhs_an_loop_info[ii].incr, &incr_list);
770 create_an_loop (init_list, cond_expr[ii], incr_list, body);
771 body = pop_stmt_list (new_loop);
773 append_to_statement_list (body, &loop_with_init);
775 lhs_an_info.release ();
776 lhs_an_loop_info.release ();
777 if (rhs_rank)
779 rhs_an_info.release ();
780 rhs_an_loop_info.release ();
782 cond_expr.release ();
784 return loop_with_init;
787 /* Helper function for expand_conditonal_array_notations. Encloses the
788 conditional statement passed in ORIG_STMT with a loop around it and
789 replaces the condition in STMT with a ARRAY_REF tree-node to the array.
790 The condition must have a ARRAY_NOTATION_REF tree. */
792 static tree
793 cp_expand_cond_array_notations (tree orig_stmt)
795 vec<tree, va_gc> *array_list = NULL, *array_operand = NULL;
796 size_t list_size = 0;
797 size_t rank = 0, ii = 0;
798 tree an_init, body, stmt = NULL_TREE;
799 tree builtin_loop, new_var = NULL_TREE;
800 tree loop_with_init = alloc_stmt_list ();
801 location_t location = UNKNOWN_LOCATION;
802 vec<vec<an_parts> > an_info = vNULL;
803 vec<an_loop_parts> an_loop_info = vNULL;
805 if (TREE_CODE (orig_stmt) == COND_EXPR)
807 size_t cond_rank = 0, yes_rank = 0, no_rank = 0;
808 tree yes_expr = COND_EXPR_THEN (orig_stmt);
809 tree no_expr = COND_EXPR_ELSE (orig_stmt);
810 tree cond = COND_EXPR_COND (orig_stmt);
811 if (!find_rank (EXPR_LOCATION (cond), cond, cond, true, &cond_rank)
812 || !find_rank (EXPR_LOCATION (yes_expr), yes_expr, yes_expr, true,
813 &yes_rank)
814 || find_rank (EXPR_LOCATION (no_expr), no_expr, no_expr, true,
815 &no_rank))
816 return error_mark_node;
817 /* If the condition has a zero rank, then handle array notations in body
818 separately. */
819 if (cond_rank == 0)
820 return orig_stmt;
821 if (cond_rank != yes_rank && yes_rank != 0)
823 error_at (EXPR_LOCATION (yes_expr), "rank mismatch with controlling"
824 " expression of parent if-statement");
825 return error_mark_node;
827 else if (cond_rank != no_rank && no_rank != 0)
829 error_at (EXPR_LOCATION (no_expr), "rank mismatch with controlling "
830 "expression of parent if-statement");
831 return error_mark_node;
834 else if (TREE_CODE (orig_stmt) == IF_STMT)
836 size_t cond_rank = 0, yes_rank = 0, no_rank = 0;
837 tree yes_expr = THEN_CLAUSE (orig_stmt);
838 tree no_expr = ELSE_CLAUSE (orig_stmt);
839 tree cond = IF_COND (orig_stmt);
840 if (!find_rank (EXPR_LOCATION (cond), cond, cond, true, &cond_rank)
841 || (yes_expr
842 && !find_rank (EXPR_LOCATION (yes_expr), yes_expr, yes_expr, true,
843 &yes_rank))
844 || (no_expr
845 && !find_rank (EXPR_LOCATION (no_expr), no_expr, no_expr, true,
846 &no_rank)))
847 return error_mark_node;
849 /* Same reasoning as for COND_EXPR. */
850 if (cond_rank == 0)
851 return orig_stmt;
852 else if (cond_rank != yes_rank && yes_rank != 0)
854 error_at (EXPR_LOCATION (yes_expr), "rank mismatch with controlling"
855 " expression of parent if-statement");
856 return error_mark_node;
858 else if (cond_rank != no_rank && no_rank != 0)
860 error_at (EXPR_LOCATION (no_expr), "rank mismatch with controlling "
861 "expression of parent if-statement");
862 return error_mark_node;
865 else if (truth_value_p (TREE_CODE (orig_stmt)))
867 size_t left_rank = 0, right_rank = 0;
868 tree left_expr = TREE_OPERAND (orig_stmt, 0);
869 tree right_expr = TREE_OPERAND (orig_stmt, 1);
870 if (!find_rank (EXPR_LOCATION (left_expr), left_expr, left_expr, true,
871 &left_rank)
872 || !find_rank (EXPR_LOCATION (right_expr), right_expr, right_expr,
873 true, &right_rank))
874 return error_mark_node;
875 if (right_rank == 0 && left_rank == 0)
876 return orig_stmt;
879 if (!find_rank (EXPR_LOCATION (orig_stmt), orig_stmt, orig_stmt, true,
880 &rank))
881 return error_mark_node;
882 if (rank == 0)
883 return orig_stmt;
885 extract_array_notation_exprs (orig_stmt, false, &array_list);
886 stmt = alloc_stmt_list ();
887 for (ii = 0; ii < vec_safe_length (array_list); ii++)
889 tree array_node = (*array_list)[ii];
890 if (TREE_CODE (array_node) == CALL_EXPR
891 || TREE_CODE (array_node) == AGGR_INIT_EXPR)
893 builtin_loop = expand_sec_reduce_builtin (array_node, &new_var);
894 if (builtin_loop == error_mark_node)
895 finish_expr_stmt (error_mark_node);
896 else if (new_var)
898 vec<tree, va_gc> *sub_list = NULL, *new_var_list = NULL;
899 vec_safe_push (sub_list, array_node);
900 vec_safe_push (new_var_list, new_var);
901 replace_array_notations (&orig_stmt, false, sub_list,
902 new_var_list);
903 append_to_statement_list (builtin_loop, &stmt);
907 append_to_statement_list (orig_stmt, &stmt);
908 rank = 0;
909 array_list = NULL;
910 if (!find_rank (EXPR_LOCATION (stmt), stmt, stmt, true, &rank))
911 return error_mark_node;
912 if (rank == 0)
913 return stmt;
915 extract_array_notation_exprs (stmt, true, &array_list);
916 list_size = vec_safe_length (array_list);
917 if (list_size == 0)
918 return stmt;
920 location = EXPR_LOCATION (orig_stmt);
921 list_size = vec_safe_length (array_list);
922 an_loop_info.safe_grow_cleared (rank);
924 an_init = push_stmt_list ();
926 /* Assign the array notation components to variable so that they can
927 satisfy the exec-once rule. */
928 for (ii = 0; ii < list_size; ii++)
930 tree anode = (*array_list)[ii];
931 make_triplet_val_inv (&ARRAY_NOTATION_START (anode));
932 make_triplet_val_inv (&ARRAY_NOTATION_LENGTH (anode));
933 make_triplet_val_inv (&ARRAY_NOTATION_STRIDE (anode));
935 cilkplus_extract_an_triplets (array_list, list_size, rank, &an_info);
937 for (ii = 0; ii < rank; ii++)
939 tree typ = ptrdiff_type_node;
940 an_loop_info[ii].var = create_temporary_var (typ);
941 add_decl_expr (an_loop_info[ii].var);
942 an_loop_info[ii].ind_init =
943 build_x_modify_expr (location, an_loop_info[ii].var, INIT_EXPR,
944 build_zero_cst (typ), tf_warning_or_error);
946 array_operand = create_array_refs (location, an_info, an_loop_info,
947 list_size, rank);
948 replace_array_notations (&stmt, true, array_list, array_operand);
949 create_cmp_incr (location, &an_loop_info, rank, an_info, tf_warning_or_error);
951 an_init = pop_stmt_list (an_init);
952 append_to_statement_list (an_init, &loop_with_init);
953 body = stmt;
955 for (ii = 0; ii < rank; ii++)
957 tree new_loop = push_stmt_list ();
958 create_an_loop (an_loop_info[ii].ind_init, an_loop_info[ii].cmp,
959 an_loop_info[ii].incr, body);
960 body = pop_stmt_list (new_loop);
962 append_to_statement_list (body, &loop_with_init);
964 an_info.release ();
965 an_loop_info.release ();
967 return loop_with_init;
970 /* Transforms array notations inside unary expression ORIG_STMT with an
971 appropriate loop and ARRAY_REF (and returns all this as a super-tree called
972 LOOP). */
974 static tree
975 expand_unary_array_notation_exprs (tree orig_stmt)
977 vec<tree, va_gc> *array_list = NULL, *array_operand = NULL;
978 size_t list_size = 0, rank = 0, ii = 0;
979 tree body;
980 tree builtin_loop, stmt = NULL_TREE, new_var = NULL_TREE;
981 location_t location = EXPR_LOCATION (orig_stmt);
982 tree an_init, loop_with_init = alloc_stmt_list ();
983 vec<vec<an_parts> > an_info = vNULL;
984 vec<an_loop_parts> an_loop_info = vNULL;
986 if (!find_rank (location, orig_stmt, orig_stmt, true, &rank))
987 return error_mark_node;
988 if (rank == 0)
989 return orig_stmt;
991 extract_array_notation_exprs (orig_stmt, false, &array_list);
992 list_size = vec_safe_length (array_list);
993 location = EXPR_LOCATION (orig_stmt);
994 stmt = NULL_TREE;
995 for (ii = 0; ii < list_size; ii++)
996 if (TREE_CODE ((*array_list)[ii]) == CALL_EXPR
997 || TREE_CODE ((*array_list)[ii]) == AGGR_INIT_EXPR)
999 tree list_node = (*array_list)[ii];
1000 builtin_loop = expand_sec_reduce_builtin (list_node, &new_var);
1001 if (builtin_loop == error_mark_node)
1002 return error_mark_node;
1003 else if (builtin_loop)
1005 vec<tree, va_gc> *sub_list = NULL, *new_var_list = NULL;
1006 stmt = alloc_stmt_list ();
1007 append_to_statement_list (builtin_loop, &stmt);
1008 vec_safe_push (sub_list, list_node);
1009 vec_safe_push (new_var_list, new_var);
1010 replace_array_notations (&orig_stmt, false, sub_list, new_var_list);
1013 if (stmt != NULL_TREE)
1014 append_to_statement_list (finish_expr_stmt (orig_stmt), &stmt);
1015 else
1016 stmt = orig_stmt;
1017 rank = 0;
1018 list_size = 0;
1019 array_list = NULL;
1020 extract_array_notation_exprs (stmt, true, &array_list);
1021 list_size = vec_safe_length (array_list);
1023 if (!find_rank (EXPR_LOCATION (stmt), stmt, stmt, true, &rank))
1024 return error_mark_node;
1025 if (rank == 0 || list_size == 0)
1026 return stmt;
1027 an_loop_info.safe_grow_cleared (rank);
1028 an_init = push_stmt_list ();
1029 /* Assign the array notation components to variable so that they can satisfy
1030 the exec-once rule. */
1031 for (ii = 0; ii < list_size; ii++)
1033 tree array_node = (*array_list)[ii];
1034 make_triplet_val_inv (&ARRAY_NOTATION_START (array_node));
1035 make_triplet_val_inv (&ARRAY_NOTATION_LENGTH (array_node));
1036 make_triplet_val_inv (&ARRAY_NOTATION_STRIDE (array_node));
1038 cilkplus_extract_an_triplets (array_list, list_size, rank, &an_info);
1040 for (ii = 0; ii < rank; ii++)
1042 tree typ = ptrdiff_type_node;
1043 an_loop_info[ii].var = create_temporary_var (typ);
1044 add_decl_expr (an_loop_info[ii].var);
1045 an_loop_info[ii].ind_init = build_x_modify_expr
1046 (location, an_loop_info[ii].var, INIT_EXPR, build_zero_cst (typ),
1047 tf_warning_or_error);
1049 array_operand = create_array_refs (location, an_info, an_loop_info,
1050 list_size, rank);
1051 replace_array_notations (&stmt, true, array_list, array_operand);
1052 create_cmp_incr (location, &an_loop_info, rank, an_info, tf_warning_or_error);
1054 an_init = pop_stmt_list (an_init);
1055 append_to_statement_list (an_init, &loop_with_init);
1056 body = stmt;
1058 for (ii = 0; ii < rank; ii++)
1060 tree new_loop = push_stmt_list ();
1061 create_an_loop (an_loop_info[ii].ind_init, an_loop_info[ii].cmp,
1062 an_loop_info[ii].incr, body);
1063 body = pop_stmt_list (new_loop);
1065 append_to_statement_list (body, &loop_with_init);
1067 an_info.release ();
1068 an_loop_info.release ();
1070 return loop_with_init;
1073 /* Expands the array notation's builtin reduction function in EXPR
1074 (of type RETURN_EXPR) and returns a STATEMENT_LIST that contains a loop
1075 with the builtin function expansion and a return statement at the end. */
1077 static tree
1078 expand_return_expr (tree expr)
1080 tree new_mod_list, new_var, new_mod, retval_expr;
1081 size_t rank = 0;
1082 location_t loc = EXPR_LOCATION (expr);
1083 if (TREE_CODE (expr) != RETURN_EXPR)
1084 return expr;
1086 if (!find_rank (loc, expr, expr, false, &rank))
1087 return error_mark_node;
1089 /* If the return expression contains array notations, then flag it as
1090 error. */
1091 if (rank >= 1)
1093 error_at (loc, "array notation expression cannot be used as a return "
1094 "value");
1095 return error_mark_node;
1098 new_mod_list = push_stmt_list ();
1099 retval_expr = TREE_OPERAND (expr, 0);
1100 new_var = create_temporary_var (TREE_TYPE (retval_expr));
1101 add_decl_expr (new_var);
1102 new_mod = expand_an_in_modify_expr (loc, new_var, NOP_EXPR,
1103 TREE_OPERAND (retval_expr, 1),
1104 tf_warning_or_error);
1105 TREE_OPERAND (retval_expr, 1) = new_var;
1106 TREE_OPERAND (expr, 0) = retval_expr;
1107 add_stmt (new_mod);
1108 add_stmt (expr);
1109 new_mod_list = pop_stmt_list (new_mod_list);
1110 return new_mod_list;
1113 /* Expands ARRAY_NOTATION_REF and builtin functions in a compound statement,
1114 STMT. Returns the STMT with expanded array notations. */
1116 tree
1117 expand_array_notation_exprs (tree t)
1119 enum tree_code code;
1120 bool is_expr;
1121 location_t loc = UNKNOWN_LOCATION;
1123 if (!t)
1124 return t;
1126 loc = EXPR_LOCATION (t);
1128 code = TREE_CODE (t);
1129 is_expr = IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code));
1130 switch (code)
1132 case ERROR_MARK:
1133 case IDENTIFIER_NODE:
1134 case VOID_CST:
1135 case INTEGER_CST:
1136 case REAL_CST:
1137 case FIXED_CST:
1138 case STRING_CST:
1139 case BLOCK:
1140 case PLACEHOLDER_EXPR:
1141 case FIELD_DECL:
1142 case VOID_TYPE:
1143 case REAL_TYPE:
1144 case SSA_NAME:
1145 case LABEL_DECL:
1146 case RESULT_DECL:
1147 case VAR_DECL:
1148 case PARM_DECL:
1149 case NON_LVALUE_EXPR:
1150 case NOP_EXPR:
1151 case ADDR_EXPR:
1152 case ARRAY_REF:
1153 case BIT_FIELD_REF:
1154 case VECTOR_CST:
1155 case COMPLEX_CST:
1156 return t;
1157 case INIT_EXPR:
1158 case MODIFY_EXPR:
1159 if (contains_array_notation_expr (t))
1160 t = expand_an_in_modify_expr (loc, TREE_OPERAND (t, 0), NOP_EXPR,
1161 TREE_OPERAND (t, 1),
1162 tf_warning_or_error);
1163 return t;
1164 case MODOP_EXPR:
1165 if (contains_array_notation_expr (t) && !processing_template_decl)
1166 t = expand_an_in_modify_expr
1167 (loc, TREE_OPERAND (t, 0), TREE_CODE (TREE_OPERAND (t, 1)),
1168 TREE_OPERAND (t, 2), tf_warning_or_error);
1169 return t;
1170 case CONSTRUCTOR:
1171 return t;
1172 case BIND_EXPR:
1174 BIND_EXPR_BODY (t) =
1175 expand_array_notation_exprs (BIND_EXPR_BODY (t));
1176 return t;
1178 case DECL_EXPR:
1179 if (contains_array_notation_expr (t))
1181 tree x = DECL_EXPR_DECL (t);
1182 if (DECL_INITIAL (x))
1184 location_t loc = DECL_SOURCE_LOCATION (x);
1185 tree lhs = x;
1186 tree rhs = DECL_INITIAL (x);
1187 DECL_INITIAL (x) = NULL;
1188 tree new_modify_expr = build_modify_expr (loc, lhs,
1189 TREE_TYPE (lhs),
1190 NOP_EXPR,
1191 loc, rhs,
1192 TREE_TYPE(rhs));
1193 t = expand_array_notation_exprs (new_modify_expr);
1196 return t;
1197 case STATEMENT_LIST:
1199 tree_stmt_iterator i;
1200 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
1201 *tsi_stmt_ptr (i) =
1202 expand_array_notation_exprs (*tsi_stmt_ptr (i));
1203 return t;
1206 case OMP_PARALLEL:
1207 case OMP_TASK:
1208 case OMP_FOR:
1209 case OMP_SINGLE:
1210 case OMP_SECTION:
1211 case OMP_SECTIONS:
1212 case OMP_MASTER:
1213 case OMP_TASKGROUP:
1214 case OMP_ORDERED:
1215 case OMP_CRITICAL:
1216 case OMP_ATOMIC:
1217 case OMP_CLAUSE:
1218 case TARGET_EXPR:
1219 case INTEGER_TYPE:
1220 case ENUMERAL_TYPE:
1221 case BOOLEAN_TYPE:
1222 case POINTER_TYPE:
1223 case ARRAY_TYPE:
1224 case RECORD_TYPE:
1225 case METHOD_TYPE:
1226 return t;
1227 case RETURN_EXPR:
1228 if (contains_array_notation_expr (t))
1229 t = expand_return_expr (t);
1230 return t;
1231 case PREDECREMENT_EXPR:
1232 case PREINCREMENT_EXPR:
1233 case POSTDECREMENT_EXPR:
1234 case POSTINCREMENT_EXPR:
1235 case AGGR_INIT_EXPR:
1236 case CALL_EXPR:
1237 t = expand_unary_array_notation_exprs (t);
1238 return t;
1239 case CONVERT_EXPR:
1240 case CLEANUP_POINT_EXPR:
1241 case EXPR_STMT:
1242 TREE_OPERAND (t, 0) = expand_array_notation_exprs (TREE_OPERAND (t, 0));
1243 /* It is not necessary to wrap error_mark_node in EXPR_STMT. */
1244 if (TREE_OPERAND (t, 0) == error_mark_node)
1245 return TREE_OPERAND (t, 0);
1246 return t;
1247 case TRUTH_ANDIF_EXPR:
1248 case TRUTH_ORIF_EXPR:
1249 case TRUTH_AND_EXPR:
1250 case TRUTH_OR_EXPR:
1251 case TRUTH_XOR_EXPR:
1252 case TRUTH_NOT_EXPR:
1253 case COND_EXPR:
1254 t = cp_expand_cond_array_notations (t);
1255 if (TREE_CODE (t) == COND_EXPR)
1257 COND_EXPR_THEN (t) =
1258 expand_array_notation_exprs (COND_EXPR_THEN (t));
1259 COND_EXPR_ELSE (t) =
1260 expand_array_notation_exprs (COND_EXPR_ELSE (t));
1262 return t;
1263 case FOR_STMT:
1264 if (contains_array_notation_expr (FOR_COND (t)))
1266 error_at (EXPR_LOCATION (FOR_COND (t)),
1267 "array notation cannot be used in a condition for "
1268 "a for-loop");
1269 return error_mark_node;
1271 /* FIXME: Add a check for CILK_FOR_STMT here when we add Cilk tasking
1272 keywords. */
1273 if (TREE_CODE (t) == FOR_STMT)
1275 FOR_BODY (t) = expand_array_notation_exprs (FOR_BODY (t));
1276 FOR_EXPR (t) = expand_array_notation_exprs (FOR_EXPR (t));
1278 else
1279 t = expand_array_notation_exprs (t);
1280 return t;
1281 case IF_STMT:
1282 t = cp_expand_cond_array_notations (t);
1283 /* If the above function added some extra instructions above the original
1284 if statement, then we can't assume it is still IF_STMT so we have to
1285 check again. */
1286 if (TREE_CODE (t) == IF_STMT)
1288 if (THEN_CLAUSE (t))
1289 THEN_CLAUSE (t) = expand_array_notation_exprs (THEN_CLAUSE (t));
1290 if (ELSE_CLAUSE (t))
1291 ELSE_CLAUSE (t) = expand_array_notation_exprs (ELSE_CLAUSE (t));
1293 else
1294 t = expand_array_notation_exprs (t);
1295 return t;
1296 case SWITCH_STMT:
1297 if (contains_array_notation_expr (SWITCH_STMT_COND (t)))
1299 error_at (EXPR_LOCATION (SWITCH_STMT_COND (t)),
1300 "array notation cannot be used as a condition for "
1301 "switch statement");
1302 return error_mark_node;
1304 if (SWITCH_STMT_BODY (t))
1305 SWITCH_STMT_BODY (t) =
1306 expand_array_notation_exprs (SWITCH_STMT_BODY (t));
1307 return t;
1308 case WHILE_STMT:
1309 if (contains_array_notation_expr (WHILE_COND (t)))
1311 if (EXPR_LOCATION (WHILE_COND (t)) != UNKNOWN_LOCATION)
1312 loc = EXPR_LOCATION (WHILE_COND (t));
1313 error_at (loc, "array notation cannot be used as a condition for "
1314 "while statement");
1315 return error_mark_node;
1317 if (WHILE_BODY (t))
1318 WHILE_BODY (t) = expand_array_notation_exprs (WHILE_BODY (t));
1319 return t;
1320 case DO_STMT:
1321 if (contains_array_notation_expr (DO_COND (t)))
1323 error_at (EXPR_LOCATION (DO_COND (t)),
1324 "array notation cannot be used as a condition for a "
1325 "do-while statement");
1326 return error_mark_node;
1328 if (DO_BODY (t))
1329 DO_BODY (t) = expand_array_notation_exprs (DO_BODY (t));
1330 return t;
1331 default:
1332 if (is_expr)
1334 int i, len;
1336 /* Walk over all the sub-trees of this operand. */
1337 len = TREE_CODE_LENGTH (code);
1339 /* Go through the subtrees. We need to do this in forward order so
1340 that the scope of a FOR_EXPR is handled properly. */
1341 for (i = 0; i < len; ++i)
1342 TREE_OPERAND (t, i) =
1343 expand_array_notation_exprs (TREE_OPERAND (t, i));
1345 return t;
1347 return t;
1350 /* Given the base of an array (ARRAY), the START (start_index), the number of
1351 elements to be accessed (LENGTH) and the STRIDE, construct an
1352 ARRAY_NOTATION_REF tree of type TYPE and return it. Restrictions on START,
1353 LENGTH and STRIDE are the same as that of index field passed into ARRAY_REF.
1354 The only additional restriction is that, unlike index in ARRAY_REF, stride,
1355 length and start_index cannot contain array notations. */
1357 tree
1358 build_array_notation_ref (location_t loc, tree array, tree start, tree length,
1359 tree stride, tree type)
1361 tree array_ntn_expr = NULL_TREE;
1363 /* If we enter the then-case of the if-statement below, we have hit a case
1364 like this: ARRAY [:]. */
1365 if (!start && !length)
1367 if (TREE_CODE (type) != ARRAY_TYPE)
1369 error_at (loc, "start-index and length fields necessary for "
1370 "using array notation in pointers or records");
1371 return error_mark_node;
1373 tree domain = TYPE_DOMAIN (type);
1374 if (!domain)
1376 error_at (loc, "start-index and length fields necessary for "
1377 "using array notation with array of unknown bound");
1378 return error_mark_node;
1380 start = cp_fold_convert (ptrdiff_type_node, TYPE_MINVAL (domain));
1381 length = size_binop (PLUS_EXPR, TYPE_MAXVAL (domain), size_one_node);
1382 length = cp_fold_convert (ptrdiff_type_node, length);
1385 if (!stride)
1386 stride = build_one_cst (ptrdiff_type_node);
1388 /* When dealing with templates, triplet type-checking will be done in pt.c
1389 after type substitution. */
1390 if (processing_template_decl
1391 && (type_dependent_expression_p (array)
1392 || type_dependent_expression_p (length)
1393 || type_dependent_expression_p (start)
1394 || type_dependent_expression_p (stride)))
1395 array_ntn_expr = build_min_nt_loc (loc, ARRAY_NOTATION_REF, array, start,
1396 length, stride, NULL_TREE);
1397 else
1399 if (!cilkplus_an_triplet_types_ok_p (loc, start, length, stride, type))
1400 return error_mark_node;
1401 array_ntn_expr = build4 (ARRAY_NOTATION_REF, NULL_TREE, array, start,
1402 length, stride);
1404 if (TREE_CODE (type) == ARRAY_TYPE || TREE_CODE (type) == POINTER_TYPE)
1405 TREE_TYPE (array_ntn_expr) = TREE_TYPE (type);
1406 else
1408 error_at (loc, "base of array section must be pointer or array type");
1409 return error_mark_node;
1412 SET_EXPR_LOCATION (array_ntn_expr, loc);
1413 return array_ntn_expr;
1416 /* Returns false if any of the Array notation triplet values: START_INDEX,
1417 LENGTH and STRIDE, are not of integral type and have a rank greater than
1418 zero. */
1420 bool
1421 cilkplus_an_triplet_types_ok_p (location_t loc, tree start_index, tree length,
1422 tree stride, tree type)
1424 size_t stride_rank = 0, length_rank = 0, start_rank = 0;
1425 if (!TREE_TYPE (start_index) || !INTEGRAL_TYPE_P (TREE_TYPE (start_index)))
1427 error_at (loc, "start-index of array notation triplet is not an integer");
1428 return false;
1430 if (!TREE_TYPE (length) || !INTEGRAL_TYPE_P (TREE_TYPE (length)))
1432 error_at (loc, "length of array notation triplet is not an integer");
1433 return false;
1435 if (!TREE_TYPE (stride) || !INTEGRAL_TYPE_P (TREE_TYPE (stride)))
1437 error_at (loc, "stride of array notation triplet is not an integer");
1438 return false;
1440 if (TREE_CODE (type) == FUNCTION_TYPE)
1442 error_at (loc, "array notation cannot be used with function type");
1443 return false;
1445 if (!find_rank (loc, start_index, start_index, false, &start_rank)
1446 || !find_rank (loc, length, length, false, &length_rank)
1447 || !find_rank (loc, stride, stride, false, &stride_rank))
1448 return false;
1450 if (start_rank != 0)
1452 error_at (loc, "rank of an array notation triplet%'s start-index is not "
1453 "zero");
1454 return false;
1456 if (length_rank != 0)
1458 error_at (loc, "rank of an array notation triplet%'s length is not zero");
1459 return false;
1461 if (stride_rank != 0)
1463 error_at (loc, "rank of array notation triplet%'s stride is not zero");
1464 return false;
1466 return true;