2015-09-21 Steven G. Kargl <kargl@gcc.gnu.org>
[official-gcc.git] / gcc / cp / cp-array-notation.c
blob2b7d4164379a64c83cf3ae68b779f9c01b99b6d0
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-2015 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 "alias.h"
57 #include "tree.h"
58 #include "options.h"
59 #include "cp-tree.h"
60 #include "c-family/c-common.h"
61 #include "diagnostic.h"
62 #include "tree-iterator.h"
64 /* Creates a FOR_STMT with INIT, COND, INCR and BODY as the initializer,
65 condition, increment expression and the loop-body, respectively. */
67 static void
68 create_an_loop (tree init, tree cond, tree incr, tree body)
70 tree for_stmt;
72 finish_expr_stmt (init);
73 for_stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
74 finish_for_init_stmt (for_stmt);
75 finish_for_cond (cond, for_stmt, false);
76 finish_for_expr (incr, for_stmt);
77 finish_expr_stmt (body);
78 finish_for_stmt (for_stmt);
81 /* If *VALUE is not a constant integer, then this function replaces it with
82 a variable to make it loop invariant for array notations. */
84 static inline void
85 make_triplet_val_inv (tree *value)
87 if (TREE_CODE (*value) != INTEGER_CST
88 && TREE_CODE (*value) != PARM_DECL
89 && !VAR_P (*value))
90 *value = get_temp_regvar (ptrdiff_type_node, *value);
93 /* Returns a vector of size RANK that contains an ARRAY_REF. This vector is
94 created using array notation-triplet information stored in AN_INFO. The
95 induction var is taken from AN_LOOP_INFO.
97 For example: For an array notation A[5:10:2], the vector start will be
98 of size 1 holding '5', stride of same size as start but holding the value of
99 as 2, and is_vector as true. Let's assume VAR is 'x'
100 This function returns a vector of size 1 with the following data:
101 A[5 + (x * 2)] .
104 static vec<tree, va_gc> *
105 create_array_refs (location_t loc, vec<vec<an_parts> > an_info,
106 vec<an_loop_parts> an_loop_info, size_t size, size_t rank)
108 tree ind_mult, ind_incr;
109 vec<tree, va_gc> *array_operand = NULL;
111 for (size_t ii = 0; ii < size; ii++)
112 if (an_info[ii][0].is_vector)
114 tree array_opr = an_info[ii][rank - 1].value;
115 for (int s_jj = rank -1; s_jj >= 0; s_jj--)
117 tree start = cp_fold_convert (ptrdiff_type_node,
118 an_info[ii][s_jj].start);
119 tree stride = cp_fold_convert (ptrdiff_type_node,
120 an_info[ii][s_jj].stride);
121 tree var = cp_fold_convert (ptrdiff_type_node,
122 an_loop_info[s_jj].var);
124 ind_mult = build2 (MULT_EXPR, TREE_TYPE (var), var, stride);
125 ind_incr = build2 (PLUS_EXPR, TREE_TYPE (var), start, ind_mult);
126 /* Array [ start_index + (induction_var * stride)] */
127 array_opr = grok_array_decl (loc, array_opr, ind_incr, false);
129 vec_safe_push (array_operand, array_opr);
131 else
132 vec_safe_push (array_operand, integer_one_node);
133 return array_operand;
136 /* Populates the INCR and CMP fields in *NODE with the increment
137 (of type POSTINCREMENT) and comparison (of TYPE LT_EXPR) expressions, using
138 data from AN_INFO. */
140 void
141 create_cmp_incr (location_t loc, vec <an_loop_parts> *node, size_t rank,
142 vec<vec<an_parts> > an_info, tsubst_flags_t complain)
144 for (size_t ii = 0; ii < rank; ii++)
146 (*node)[ii].incr = build_x_unary_op (loc, POSTINCREMENT_EXPR,
147 (*node)[ii].var, complain);
148 (*node)[ii].cmp = build_x_binary_op (loc, LT_EXPR, (*node)[ii].var,
149 TREE_CODE ((*node)[ii].var),
150 an_info[0][ii].length,
151 TREE_CODE (an_info[0][ii].length),
152 NULL, complain);
156 /* Replaces all the scalar expressions in *NODE. Returns a STATEMENT LIST that
157 holds the NODE along with the variables that hold the results of the
158 invariant expressions. */
160 static tree
161 replace_invariant_exprs (tree *node)
163 size_t ix = 0;
164 tree node_list = NULL_TREE;
165 tree t = NULL_TREE, new_var = NULL_TREE;
166 struct inv_list data;
168 data.list_values = NULL;
169 data.replacement = NULL;
170 data.additional_tcodes = NULL;
171 cp_walk_tree (node, find_inv_trees, (void *) &data, NULL);
173 if (vec_safe_length (data.list_values))
175 node_list = push_stmt_list ();
176 for (ix = 0; vec_safe_iterate (data.list_values, ix, &t); ix++)
178 /* Sometimes, when comma_expr has a function call in it, it will
179 typecast it to void. Find_inv_trees finds those nodes and so
180 if it void type, then don't bother creating a new var to hold
181 the return value. */
182 if (VOID_TYPE_P (TREE_TYPE (t)))
184 finish_expr_stmt (t);
185 new_var = void_node;
187 else
188 new_var = get_temp_regvar (TREE_TYPE (t), t);
189 vec_safe_push (data.replacement, new_var);
191 cp_walk_tree (node, replace_inv_trees, (void *) &data, NULL);
192 node_list = pop_stmt_list (node_list);
194 return node_list;
197 /* Replace array notation's built-in function passed in AN_BUILTIN_FN with
198 the appropriate loop and computation (all stored in variable LOOP of type
199 tree node). The output of the function is always a scalar and that
200 result is returned in *NEW_VAR. *NEW_VAR is NULL_TREE if the function is
201 __sec_reduce_mutating. */
203 static tree
204 expand_sec_reduce_builtin (tree an_builtin_fn, tree *new_var)
206 tree new_var_type = NULL_TREE, func_parm, new_yes_expr, new_no_expr;
207 tree array_ind_value = NULL_TREE, new_no_ind, new_yes_ind, new_no_list;
208 tree new_yes_list, new_cond_expr, new_expr = NULL_TREE;
209 vec<tree, va_gc> *array_list = NULL, *array_operand = NULL;
210 size_t list_size = 0, rank = 0, ii = 0;
211 tree body, an_init, loop_with_init = alloc_stmt_list ();
212 tree array_op0, comp_node = NULL_TREE;
213 tree call_fn = NULL_TREE, identity_value = NULL_TREE;
214 tree init = NULL_TREE, cond_init = NULL_TREE;
215 enum tree_code code = NOP_EXPR;
216 location_t location = UNKNOWN_LOCATION;
217 vec<vec<an_parts> > an_info = vNULL;
218 vec<an_loop_parts> an_loop_info = vNULL;
219 enum built_in_function an_type =
220 is_cilkplus_reduce_builtin (CALL_EXPR_FN (an_builtin_fn));
221 vec <tree, va_gc> *func_args;
223 if (an_type == BUILT_IN_NONE)
224 return NULL_TREE;
226 if (an_type != BUILT_IN_CILKPLUS_SEC_REDUCE
227 && an_type != BUILT_IN_CILKPLUS_SEC_REDUCE_MUTATING)
228 func_parm = CALL_EXPR_ARG (an_builtin_fn, 0);
229 else
231 call_fn = CALL_EXPR_ARG (an_builtin_fn, 2);
233 /* We need to do this because we are "faking" the builtin function types,
234 so the compiler does a bunch of typecasts and this will get rid of
235 all that! */
236 STRIP_NOPS (call_fn);
237 if (TREE_CODE (call_fn) != OVERLOAD
238 && TREE_CODE (call_fn) != FUNCTION_DECL)
239 call_fn = TREE_OPERAND (call_fn, 0);
240 identity_value = CALL_EXPR_ARG (an_builtin_fn, 0);
241 func_parm = CALL_EXPR_ARG (an_builtin_fn, 1);
242 STRIP_NOPS (identity_value);
244 STRIP_NOPS (func_parm);
246 location = EXPR_LOCATION (an_builtin_fn);
248 /* Note about using find_rank (): If find_rank returns false, then it must
249 have already reported an error, thus we just return an error_mark_node
250 without any doing any error emission. */
251 if (!find_rank (location, an_builtin_fn, an_builtin_fn, true, &rank))
252 return error_mark_node;
253 if (rank == 0)
255 error_at (location, "Invalid builtin arguments");
256 return error_mark_node;
258 else if (rank > 1
259 && (an_type == BUILT_IN_CILKPLUS_SEC_REDUCE_MAX_IND
260 || an_type == BUILT_IN_CILKPLUS_SEC_REDUCE_MIN_IND))
262 error_at (location, "__sec_reduce_min_ind or __sec_reduce_max_ind cannot "
263 "have arrays with dimension greater than 1");
264 return error_mark_node;
267 extract_array_notation_exprs (func_parm, true, &array_list);
268 list_size = vec_safe_length (array_list);
269 switch (an_type)
271 case BUILT_IN_CILKPLUS_SEC_REDUCE_ADD:
272 case BUILT_IN_CILKPLUS_SEC_REDUCE_MUL:
273 case BUILT_IN_CILKPLUS_SEC_REDUCE_MAX:
274 case BUILT_IN_CILKPLUS_SEC_REDUCE_MIN:
275 new_var_type = TREE_TYPE ((*array_list)[0]);
276 break;
277 case BUILT_IN_CILKPLUS_SEC_REDUCE_ALL_ZERO:
278 case BUILT_IN_CILKPLUS_SEC_REDUCE_ANY_ZERO:
279 case BUILT_IN_CILKPLUS_SEC_REDUCE_ANY_NONZERO:
280 case BUILT_IN_CILKPLUS_SEC_REDUCE_ALL_NONZERO:
281 new_var_type = boolean_type_node;
282 break;
283 case BUILT_IN_CILKPLUS_SEC_REDUCE_MAX_IND:
284 case BUILT_IN_CILKPLUS_SEC_REDUCE_MIN_IND:
285 new_var_type = size_type_node;
286 break;
287 case BUILT_IN_CILKPLUS_SEC_REDUCE:
288 if (call_fn && identity_value)
289 new_var_type = TREE_TYPE ((*array_list)[0]);
290 break;
291 case BUILT_IN_CILKPLUS_SEC_REDUCE_MUTATING:
292 new_var_type = NULL_TREE;
293 break;
294 default:
295 gcc_unreachable ();
298 if (new_var_type && TREE_CODE (new_var_type) == ARRAY_TYPE)
299 new_var_type = TREE_TYPE (new_var_type);
300 an_loop_info.safe_grow_cleared (rank);
302 an_init = push_stmt_list ();
304 /* Assign the array notation components to variable so that they can satisfy
305 the exec-once rule. */
306 for (ii = 0; ii < list_size; ii++)
307 if (TREE_CODE ((*array_list)[ii]) == ARRAY_NOTATION_REF)
309 tree anode = (*array_list)[ii];
310 make_triplet_val_inv (&ARRAY_NOTATION_START (anode));
311 make_triplet_val_inv (&ARRAY_NOTATION_LENGTH (anode));
312 make_triplet_val_inv (&ARRAY_NOTATION_STRIDE (anode));
314 cilkplus_extract_an_triplets (array_list, list_size, rank, &an_info);
315 for (ii = 0; ii < rank; ii++)
317 tree typ = ptrdiff_type_node;
319 /* In this place, we are using get_temp_regvar instead of
320 create_temporary_var if an_type is SEC_REDUCE_MAX/MIN_IND because
321 the array_ind_value depends on this value being initalized to 0. */
322 if (an_type == BUILT_IN_CILKPLUS_SEC_REDUCE_MAX_IND
323 || an_type == BUILT_IN_CILKPLUS_SEC_REDUCE_MIN_IND)
324 an_loop_info[ii].var = get_temp_regvar (typ, build_zero_cst (typ));
325 else
327 an_loop_info[ii].var = create_temporary_var (typ);
328 add_decl_expr (an_loop_info[ii].var);
330 an_loop_info[ii].ind_init =
331 build_x_modify_expr (location, an_loop_info[ii].var, INIT_EXPR,
332 build_zero_cst (typ), tf_warning_or_error);
334 array_operand = create_array_refs (location, an_info, an_loop_info,
335 list_size, rank);
336 replace_array_notations (&func_parm, true, array_list, array_operand);
338 if (!TREE_TYPE (func_parm))
339 TREE_TYPE (func_parm) = TREE_TYPE ((*array_list)[0]);
341 create_cmp_incr (location, &an_loop_info, rank, an_info, tf_warning_or_error);
342 if (an_type == BUILT_IN_CILKPLUS_SEC_REDUCE_MAX_IND
343 || an_type == BUILT_IN_CILKPLUS_SEC_REDUCE_MIN_IND)
344 array_ind_value = get_temp_regvar (TREE_TYPE (func_parm), func_parm);
346 array_op0 = (*array_operand)[0];
347 if (INDIRECT_REF_P (array_op0))
348 array_op0 = TREE_OPERAND (array_op0, 0);
349 switch (an_type)
351 case BUILT_IN_CILKPLUS_SEC_REDUCE_ADD:
352 code = PLUS_EXPR;
353 init = build_zero_cst (new_var_type);
354 break;
355 case BUILT_IN_CILKPLUS_SEC_REDUCE_MUL:
356 code = MULT_EXPR;
357 init = build_one_cst (new_var_type);
358 break;
359 case BUILT_IN_CILKPLUS_SEC_REDUCE_ANY_ZERO:
360 case BUILT_IN_CILKPLUS_SEC_REDUCE_ANY_NONZERO:
361 code = ((an_type == BUILT_IN_CILKPLUS_SEC_REDUCE_ANY_ZERO) ? EQ_EXPR
362 : NE_EXPR);
363 init = build_zero_cst (new_var_type);
364 cond_init = build_one_cst (new_var_type);
365 comp_node = build_zero_cst (TREE_TYPE (func_parm));
366 break;
367 case BUILT_IN_CILKPLUS_SEC_REDUCE_ALL_ZERO:
368 case BUILT_IN_CILKPLUS_SEC_REDUCE_ALL_NONZERO:
369 code = ((an_type == BUILT_IN_CILKPLUS_SEC_REDUCE_ALL_ZERO) ? NE_EXPR
370 : EQ_EXPR);
371 init = build_one_cst (new_var_type);
372 cond_init = build_zero_cst (new_var_type);
373 comp_node = build_zero_cst (TREE_TYPE (func_parm));
374 break;
375 case BUILT_IN_CILKPLUS_SEC_REDUCE_MAX:
376 code = MAX_EXPR;
377 init = (TYPE_MIN_VALUE (new_var_type) ? TYPE_MIN_VALUE (new_var_type)
378 : func_parm);
379 break;
380 case BUILT_IN_CILKPLUS_SEC_REDUCE_MIN:
381 code = MIN_EXPR;
382 init = (TYPE_MAX_VALUE (new_var_type) ? TYPE_MAX_VALUE (new_var_type)
383 : func_parm);
384 break;
385 case BUILT_IN_CILKPLUS_SEC_REDUCE_MIN_IND:
386 case BUILT_IN_CILKPLUS_SEC_REDUCE_MAX_IND:
387 code = (an_type == BUILT_IN_CILKPLUS_SEC_REDUCE_MAX_IND ? LE_EXPR
388 : GE_EXPR);
389 init = an_loop_info[0].var;
390 break;
391 case BUILT_IN_CILKPLUS_SEC_REDUCE:
392 init = identity_value;
393 break;
394 case BUILT_IN_CILKPLUS_SEC_REDUCE_MUTATING:
395 init = NULL_TREE;
396 break;
397 default:
398 gcc_unreachable ();
401 if (an_type != BUILT_IN_CILKPLUS_SEC_REDUCE_MUTATING)
402 *new_var = get_temp_regvar (new_var_type, init);
403 else
404 *new_var = NULL_TREE;
406 switch (an_type)
408 case BUILT_IN_CILKPLUS_SEC_REDUCE_ADD:
409 case BUILT_IN_CILKPLUS_SEC_REDUCE_MUL:
410 new_expr = build_x_modify_expr (location, *new_var, code, func_parm,
411 tf_warning_or_error);
412 break;
413 case BUILT_IN_CILKPLUS_SEC_REDUCE_ALL_ZERO:
414 case BUILT_IN_CILKPLUS_SEC_REDUCE_ALL_NONZERO:
415 case BUILT_IN_CILKPLUS_SEC_REDUCE_ANY_ZERO:
416 case BUILT_IN_CILKPLUS_SEC_REDUCE_ANY_NONZERO:
417 /* In all these cases, assume the false case is true and as soon as
418 we find a true case, set the true flag on and latch it in. */
419 new_yes_expr = build_x_modify_expr (location, *new_var, NOP_EXPR,
420 cond_init, tf_warning_or_error);
421 new_no_expr = build_x_modify_expr (location, *new_var, NOP_EXPR,
422 *new_var, tf_warning_or_error);
423 new_cond_expr = build_x_binary_op
424 (location, code, func_parm, TREE_CODE (func_parm), comp_node,
425 TREE_CODE (comp_node), NULL, tf_warning_or_error);
426 new_expr = build_x_conditional_expr (location, new_cond_expr,
427 new_yes_expr, new_no_expr,
428 tf_warning_or_error);
429 break;
430 case BUILT_IN_CILKPLUS_SEC_REDUCE_MAX:
431 case BUILT_IN_CILKPLUS_SEC_REDUCE_MIN:
432 new_cond_expr = build_x_binary_op
433 (location, code, *new_var, TREE_CODE (*new_var), func_parm,
434 TREE_CODE (func_parm), NULL, tf_warning_or_error);
435 new_expr = build_x_modify_expr (location, *new_var, NOP_EXPR, func_parm,
436 tf_warning_or_error);
437 break;
438 case BUILT_IN_CILKPLUS_SEC_REDUCE_MAX_IND:
439 case BUILT_IN_CILKPLUS_SEC_REDUCE_MIN_IND:
440 new_yes_expr = build_x_modify_expr (location, array_ind_value, NOP_EXPR,
441 func_parm, tf_warning_or_error);
442 new_no_expr = build_x_modify_expr (location, array_ind_value, NOP_EXPR,
443 array_ind_value, tf_warning_or_error);
444 if (list_size > 1)
445 new_yes_ind = build_x_modify_expr (location, *new_var, NOP_EXPR,
446 an_loop_info[0].var,
447 tf_warning_or_error);
448 else
449 new_yes_ind = build_x_modify_expr (location, *new_var, NOP_EXPR,
450 TREE_OPERAND (array_op0, 1),
451 tf_warning_or_error);
452 new_no_ind = build_x_modify_expr (location, *new_var, NOP_EXPR, *new_var,
453 tf_warning_or_error);
454 new_yes_list = alloc_stmt_list ();
455 append_to_statement_list (new_yes_ind, &new_yes_list);
456 append_to_statement_list (new_yes_expr, &new_yes_list);
458 new_no_list = alloc_stmt_list ();
459 append_to_statement_list (new_no_ind, &new_no_list);
460 append_to_statement_list (new_no_expr, &new_no_list);
462 new_cond_expr = build_x_binary_op (location, code, array_ind_value,
463 TREE_CODE (array_ind_value), func_parm,
464 TREE_CODE (func_parm), NULL,
465 tf_warning_or_error);
466 new_expr = build_x_conditional_expr (location, new_cond_expr,
467 new_yes_list, new_no_list,
468 tf_warning_or_error);
469 break;
470 case BUILT_IN_CILKPLUS_SEC_REDUCE:
471 case BUILT_IN_CILKPLUS_SEC_REDUCE_MUTATING:
472 func_args = make_tree_vector ();
473 if (an_type == BUILT_IN_CILKPLUS_SEC_REDUCE)
474 vec_safe_push (func_args, *new_var);
475 else
476 vec_safe_push (func_args, identity_value);
477 vec_safe_push (func_args, func_parm);
479 new_expr = finish_call_expr (call_fn, &func_args, false, true,
480 tf_warning_or_error);
481 if (an_type == BUILT_IN_CILKPLUS_SEC_REDUCE)
482 new_expr = build_x_modify_expr (location, *new_var, NOP_EXPR, new_expr,
483 tf_warning_or_error);
484 release_tree_vector (func_args);
485 break;
486 default:
487 gcc_unreachable ();
489 an_init = pop_stmt_list (an_init);
490 append_to_statement_list (an_init, &loop_with_init);
491 body = new_expr;
493 for (ii = 0; ii < rank; ii++)
495 tree new_loop = push_stmt_list ();
496 create_an_loop (an_loop_info[ii].ind_init, an_loop_info[ii].cmp,
497 an_loop_info[ii].incr, body);
498 body = pop_stmt_list (new_loop);
500 append_to_statement_list (body, &loop_with_init);
502 an_info.release ();
503 an_loop_info.release ();
505 return loop_with_init;
508 /* Returns a loop with ARRAY_REF inside it with an appropriate modify expr.
509 The LHS and/or RHS will be array notation expressions that have a
510 MODIFYCODE. The location of the variable is specified by LOCATION. */
512 static tree
513 expand_an_in_modify_expr (location_t location, tree lhs,
514 enum tree_code modifycode, tree rhs,
515 tsubst_flags_t complain)
517 tree array_expr_lhs = NULL_TREE, array_expr_rhs = NULL_TREE;
518 tree array_expr = NULL_TREE;
519 tree body = NULL_TREE;
520 vec<tree> cond_expr = vNULL;
521 vec<tree, va_gc> *lhs_array_operand = NULL, *rhs_array_operand = NULL;
522 size_t lhs_rank = 0, rhs_rank = 0, ii = 0;
523 vec<tree, va_gc> *rhs_list = NULL, *lhs_list = NULL;
524 size_t rhs_list_size = 0, lhs_list_size = 0;
525 tree new_modify_expr, new_var = NULL_TREE, builtin_loop, scalar_mods;
526 bool found_builtin_fn = false;
527 tree an_init, loop_with_init = alloc_stmt_list ();
528 vec<vec<an_parts> > lhs_an_info = vNULL, rhs_an_info = vNULL;
529 vec<an_loop_parts> lhs_an_loop_info = vNULL, rhs_an_loop_info = vNULL;
531 if (!find_rank (location, rhs, rhs, false, &rhs_rank))
532 return error_mark_node;
533 extract_array_notation_exprs (rhs, false, &rhs_list);
534 rhs_list_size = vec_safe_length (rhs_list);
535 an_init = push_stmt_list ();
536 if (rhs_rank)
538 scalar_mods = replace_invariant_exprs (&rhs);
539 if (scalar_mods)
540 finish_expr_stmt (scalar_mods);
542 for (ii = 0; ii < rhs_list_size; ii++)
544 tree rhs_node = (*rhs_list)[ii];
545 if (TREE_CODE (rhs_node) == CALL_EXPR)
547 builtin_loop = expand_sec_reduce_builtin (rhs_node, &new_var);
548 if (builtin_loop == error_mark_node)
549 return error_mark_node;
550 else if (builtin_loop)
552 finish_expr_stmt (builtin_loop);
553 found_builtin_fn = true;
554 if (new_var)
556 vec <tree, va_gc> *rhs_sub_list = NULL, *new_var_list = NULL;
557 vec_safe_push (rhs_sub_list, rhs_node);
558 vec_safe_push (new_var_list, new_var);
559 replace_array_notations (&rhs, false, rhs_sub_list,
560 new_var_list);
565 lhs_rank = 0;
566 rhs_rank = 0;
567 if (!find_rank (location, lhs, lhs, true, &lhs_rank)
568 || !find_rank (location, rhs, rhs, true, &rhs_rank))
570 pop_stmt_list (an_init);
571 return error_mark_node;
574 /* If both are scalar, then the only reason why we will get this far is if
575 there is some array notations inside it and was using a builtin array
576 notation functions. If so, we have already broken those guys up and now
577 a simple build_x_modify_expr would do. */
578 if (lhs_rank == 0 && rhs_rank == 0)
580 if (found_builtin_fn)
582 new_modify_expr = build_x_modify_expr (location, lhs,
583 modifycode, rhs, complain);
584 finish_expr_stmt (new_modify_expr);
585 pop_stmt_list (an_init);
586 return an_init;
588 else
589 gcc_unreachable ();
592 /* If for some reason location is not set, then find if LHS or RHS has
593 location info. If so, then use that so we atleast have an idea. */
594 if (location == UNKNOWN_LOCATION)
596 if (EXPR_LOCATION (lhs) != UNKNOWN_LOCATION)
597 location = EXPR_LOCATION (lhs);
598 else if (EXPR_LOCATION (rhs) != UNKNOWN_LOCATION)
599 location = EXPR_LOCATION (rhs);
602 /* We need this when we have a scatter issue. */
603 extract_array_notation_exprs (lhs, true, &lhs_list);
604 rhs_list = NULL;
605 extract_array_notation_exprs (rhs, true, &rhs_list);
606 rhs_list_size = vec_safe_length (rhs_list);
607 lhs_list_size = vec_safe_length (lhs_list);
609 if (lhs_rank == 0 && rhs_rank != 0)
611 error_at (location, "%qE cannot be scalar when %qE is not", lhs, rhs);
612 return error_mark_node;
614 if (lhs_rank != 0 && rhs_rank != 0 && lhs_rank != rhs_rank)
616 error_at (location, "rank mismatch between %qE and %qE", lhs, rhs);
617 return error_mark_node;
620 /* Assign the array notation components to variable so that they can satisfy
621 the execute-once rule. */
622 for (ii = 0; ii < lhs_list_size; ii++)
624 tree anode = (*lhs_list)[ii];
625 make_triplet_val_inv (&ARRAY_NOTATION_START (anode));
626 make_triplet_val_inv (&ARRAY_NOTATION_LENGTH (anode));
627 make_triplet_val_inv (&ARRAY_NOTATION_STRIDE (anode));
629 for (ii = 0; ii < rhs_list_size; ii++)
630 if ((*rhs_list)[ii] && TREE_CODE ((*rhs_list)[ii]) == ARRAY_NOTATION_REF)
632 tree aa = (*rhs_list)[ii];
633 make_triplet_val_inv (&ARRAY_NOTATION_START (aa));
634 make_triplet_val_inv (&ARRAY_NOTATION_LENGTH (aa));
635 make_triplet_val_inv (&ARRAY_NOTATION_STRIDE (aa));
637 lhs_an_loop_info.safe_grow_cleared (lhs_rank);
639 if (rhs_rank)
640 rhs_an_loop_info.safe_grow_cleared (rhs_rank);
642 cond_expr.safe_grow_cleared (MAX (lhs_rank, rhs_rank));
643 cilkplus_extract_an_triplets (lhs_list, lhs_list_size, lhs_rank,
644 &lhs_an_info);
645 if (rhs_list)
646 cilkplus_extract_an_triplets (rhs_list, rhs_list_size, rhs_rank,
647 &rhs_an_info);
648 if (length_mismatch_in_expr_p (EXPR_LOCATION (lhs), lhs_an_info)
649 || (rhs_list && length_mismatch_in_expr_p (EXPR_LOCATION (rhs),
650 rhs_an_info)))
652 pop_stmt_list (an_init);
653 return error_mark_node;
655 tree rhs_len = ((rhs_list_size > 0 && rhs_rank > 0) ?
656 rhs_an_info[0][0].length : NULL_TREE);
657 tree lhs_len = ((lhs_list_size > 0 && lhs_rank > 0) ?
658 lhs_an_info[0][0].length : NULL_TREE);
659 if (lhs_list_size > 0 && rhs_list_size > 0 && lhs_rank > 0 && rhs_rank > 0
660 && TREE_CODE (lhs_len) == INTEGER_CST && rhs_len
661 && TREE_CODE (rhs_len) == INTEGER_CST
662 && !tree_int_cst_equal (rhs_len, lhs_len))
664 error_at (location, "length mismatch between LHS and RHS");
665 pop_stmt_list (an_init);
666 return error_mark_node;
668 for (ii = 0; ii < lhs_rank; ii++)
670 tree typ = ptrdiff_type_node;
671 lhs_an_loop_info[ii].var = create_temporary_var (typ);
672 add_decl_expr (lhs_an_loop_info[ii].var);
673 lhs_an_loop_info[ii].ind_init = build_x_modify_expr
674 (location, lhs_an_loop_info[ii].var, INIT_EXPR, build_zero_cst (typ),
675 complain);
678 if (rhs_list_size > 0)
680 rhs_array_operand = fix_sec_implicit_args (location, rhs_list,
681 lhs_an_loop_info, lhs_rank,
682 lhs);
683 if (!rhs_array_operand)
684 return error_mark_node;
686 replace_array_notations (&rhs, true, rhs_list, rhs_array_operand);
687 rhs_list_size = 0;
688 rhs_list = NULL;
689 extract_array_notation_exprs (rhs, true, &rhs_list);
690 rhs_list_size = vec_safe_length (rhs_list);
692 for (ii = 0; ii < rhs_rank; ii++)
694 tree typ = ptrdiff_type_node;
695 rhs_an_loop_info[ii].var = create_temporary_var (typ);
696 add_decl_expr (rhs_an_loop_info[ii].var);
697 rhs_an_loop_info[ii].ind_init = build_x_modify_expr
698 (location, rhs_an_loop_info[ii].var, INIT_EXPR, build_zero_cst (typ),
699 complain);
702 if (lhs_rank)
704 lhs_array_operand =
705 create_array_refs (location, lhs_an_info, lhs_an_loop_info,
706 lhs_list_size, lhs_rank);
707 replace_array_notations (&lhs, true, lhs_list, lhs_array_operand);
710 if (rhs_array_operand)
711 vec_safe_truncate (rhs_array_operand, 0);
712 if (rhs_rank)
714 rhs_array_operand = create_array_refs (location, rhs_an_info,
715 rhs_an_loop_info, rhs_list_size,
716 rhs_rank);
717 /* Replace all the array refs created by the above function because this
718 variable is blown away by the fix_sec_implicit_args function below. */
719 replace_array_notations (&rhs, true, rhs_list, rhs_array_operand);
720 vec_safe_truncate (rhs_array_operand , 0);
721 rhs_array_operand = fix_sec_implicit_args (location, rhs_list,
722 rhs_an_loop_info, rhs_rank,
723 rhs);
724 if (!rhs_array_operand)
725 return error_mark_node;
726 replace_array_notations (&rhs, true, rhs_list, rhs_array_operand);
729 array_expr_rhs = rhs;
730 array_expr_lhs = lhs;
732 array_expr = build_x_modify_expr (location, array_expr_lhs, modifycode,
733 array_expr_rhs, complain);
734 create_cmp_incr (location, &lhs_an_loop_info, lhs_rank, lhs_an_info,
735 complain);
736 if (rhs_rank)
737 create_cmp_incr (location, &rhs_an_loop_info, rhs_rank, rhs_an_info,
738 complain);
739 for (ii = 0; ii < MAX (rhs_rank, lhs_rank); ii++)
740 if (ii < lhs_rank && ii < rhs_rank)
741 cond_expr[ii] = build_x_binary_op
742 (location, TRUTH_ANDIF_EXPR, lhs_an_loop_info[ii].cmp,
743 TREE_CODE (lhs_an_loop_info[ii].cmp), rhs_an_loop_info[ii].cmp,
744 TREE_CODE (rhs_an_loop_info[ii].cmp), NULL, complain);
745 else if (ii < lhs_rank && ii >= rhs_rank)
746 cond_expr[ii] = lhs_an_loop_info[ii].cmp;
747 else
748 /* No need to compare ii < rhs_rank && ii >= lhs_rank because in a valid
749 Array notation expression, rank of RHS cannot be greater than LHS. */
750 gcc_unreachable ();
752 an_init = pop_stmt_list (an_init);
753 append_to_statement_list (an_init, &loop_with_init);
754 body = array_expr;
755 for (ii = 0; ii < MAX (lhs_rank, rhs_rank); ii++)
757 tree incr_list = alloc_stmt_list ();
758 tree init_list = alloc_stmt_list ();
759 tree new_loop = push_stmt_list ();
761 if (lhs_rank)
763 append_to_statement_list (lhs_an_loop_info[ii].ind_init, &init_list);
764 append_to_statement_list (lhs_an_loop_info[ii].incr, &incr_list);
766 if (rhs_rank)
768 append_to_statement_list (rhs_an_loop_info[ii].ind_init, &init_list);
769 append_to_statement_list (rhs_an_loop_info[ii].incr, &incr_list);
771 create_an_loop (init_list, cond_expr[ii], incr_list, body);
772 body = pop_stmt_list (new_loop);
774 append_to_statement_list (body, &loop_with_init);
776 lhs_an_info.release ();
777 lhs_an_loop_info.release ();
778 if (rhs_rank)
780 rhs_an_info.release ();
781 rhs_an_loop_info.release ();
783 cond_expr.release ();
785 return loop_with_init;
788 /* Helper function for expand_conditonal_array_notations. Encloses the
789 conditional statement passed in ORIG_STMT with a loop around it and
790 replaces the condition in STMT with a ARRAY_REF tree-node to the array.
791 The condition must have a ARRAY_NOTATION_REF tree. */
793 static tree
794 cp_expand_cond_array_notations (tree orig_stmt)
796 vec<tree, va_gc> *array_list = NULL, *array_operand = NULL;
797 size_t list_size = 0;
798 size_t rank = 0, ii = 0;
799 tree an_init, body, stmt = NULL_TREE;
800 tree builtin_loop, new_var = NULL_TREE;
801 tree loop_with_init = alloc_stmt_list ();
802 location_t location = UNKNOWN_LOCATION;
803 vec<vec<an_parts> > an_info = vNULL;
804 vec<an_loop_parts> an_loop_info = vNULL;
806 if (TREE_CODE (orig_stmt) == COND_EXPR)
808 size_t cond_rank = 0, yes_rank = 0, no_rank = 0;
809 tree yes_expr = COND_EXPR_THEN (orig_stmt);
810 tree no_expr = COND_EXPR_ELSE (orig_stmt);
811 tree cond = COND_EXPR_COND (orig_stmt);
812 if (!find_rank (EXPR_LOCATION (cond), cond, cond, true, &cond_rank)
813 || !find_rank (EXPR_LOCATION (yes_expr), yes_expr, yes_expr, true,
814 &yes_rank)
815 || find_rank (EXPR_LOCATION (no_expr), no_expr, no_expr, true,
816 &no_rank))
817 return error_mark_node;
818 /* If the condition has a zero rank, then handle array notations in body
819 separately. */
820 if (cond_rank == 0)
821 return orig_stmt;
822 if (cond_rank != yes_rank && yes_rank != 0)
824 error_at (EXPR_LOCATION (yes_expr), "rank mismatch with controlling"
825 " expression of parent if-statement");
826 return error_mark_node;
828 else if (cond_rank != no_rank && no_rank != 0)
830 error_at (EXPR_LOCATION (no_expr), "rank mismatch with controlling "
831 "expression of parent if-statement");
832 return error_mark_node;
835 else if (TREE_CODE (orig_stmt) == IF_STMT)
837 size_t cond_rank = 0, yes_rank = 0, no_rank = 0;
838 tree yes_expr = THEN_CLAUSE (orig_stmt);
839 tree no_expr = ELSE_CLAUSE (orig_stmt);
840 tree cond = IF_COND (orig_stmt);
841 if (!find_rank (EXPR_LOCATION (cond), cond, cond, true, &cond_rank)
842 || (yes_expr
843 && !find_rank (EXPR_LOCATION (yes_expr), yes_expr, yes_expr, true,
844 &yes_rank))
845 || (no_expr
846 && !find_rank (EXPR_LOCATION (no_expr), no_expr, no_expr, true,
847 &no_rank)))
848 return error_mark_node;
850 /* Same reasoning as for COND_EXPR. */
851 if (cond_rank == 0)
852 return orig_stmt;
853 else if (cond_rank != yes_rank && yes_rank != 0)
855 error_at (EXPR_LOCATION (yes_expr), "rank mismatch with controlling"
856 " expression of parent if-statement");
857 return error_mark_node;
859 else if (cond_rank != no_rank && no_rank != 0)
861 error_at (EXPR_LOCATION (no_expr), "rank mismatch with controlling "
862 "expression of parent if-statement");
863 return error_mark_node;
866 else if (truth_value_p (TREE_CODE (orig_stmt)))
868 size_t left_rank = 0, right_rank = 0;
869 tree left_expr = TREE_OPERAND (orig_stmt, 0);
870 tree right_expr = TREE_OPERAND (orig_stmt, 1);
871 if (!find_rank (EXPR_LOCATION (left_expr), left_expr, left_expr, true,
872 &left_rank)
873 || !find_rank (EXPR_LOCATION (right_expr), right_expr, right_expr,
874 true, &right_rank))
875 return error_mark_node;
876 if (right_rank == 0 && left_rank == 0)
877 return orig_stmt;
880 if (!find_rank (EXPR_LOCATION (orig_stmt), orig_stmt, orig_stmt, true,
881 &rank))
882 return error_mark_node;
883 if (rank == 0)
884 return orig_stmt;
886 extract_array_notation_exprs (orig_stmt, false, &array_list);
887 stmt = alloc_stmt_list ();
888 for (ii = 0; ii < vec_safe_length (array_list); ii++)
890 tree array_node = (*array_list)[ii];
891 if (TREE_CODE (array_node) == CALL_EXPR
892 || TREE_CODE (array_node) == AGGR_INIT_EXPR)
894 builtin_loop = expand_sec_reduce_builtin (array_node, &new_var);
895 if (builtin_loop == error_mark_node)
896 finish_expr_stmt (error_mark_node);
897 else if (new_var)
899 vec<tree, va_gc> *sub_list = NULL, *new_var_list = NULL;
900 vec_safe_push (sub_list, array_node);
901 vec_safe_push (new_var_list, new_var);
902 replace_array_notations (&orig_stmt, false, sub_list,
903 new_var_list);
904 append_to_statement_list (builtin_loop, &stmt);
908 append_to_statement_list (orig_stmt, &stmt);
909 rank = 0;
910 array_list = NULL;
911 if (!find_rank (EXPR_LOCATION (stmt), stmt, stmt, true, &rank))
912 return error_mark_node;
913 if (rank == 0)
914 return stmt;
916 extract_array_notation_exprs (stmt, true, &array_list);
917 list_size = vec_safe_length (array_list);
918 if (list_size == 0)
919 return stmt;
921 location = EXPR_LOCATION (orig_stmt);
922 list_size = vec_safe_length (array_list);
923 an_loop_info.safe_grow_cleared (rank);
925 an_init = push_stmt_list ();
927 /* Assign the array notation components to variable so that they can
928 satisfy the exec-once rule. */
929 for (ii = 0; ii < list_size; ii++)
931 tree anode = (*array_list)[ii];
932 make_triplet_val_inv (&ARRAY_NOTATION_START (anode));
933 make_triplet_val_inv (&ARRAY_NOTATION_LENGTH (anode));
934 make_triplet_val_inv (&ARRAY_NOTATION_STRIDE (anode));
936 cilkplus_extract_an_triplets (array_list, list_size, rank, &an_info);
938 for (ii = 0; ii < rank; ii++)
940 tree typ = ptrdiff_type_node;
941 an_loop_info[ii].var = create_temporary_var (typ);
942 add_decl_expr (an_loop_info[ii].var);
943 an_loop_info[ii].ind_init =
944 build_x_modify_expr (location, an_loop_info[ii].var, INIT_EXPR,
945 build_zero_cst (typ), tf_warning_or_error);
947 array_operand = create_array_refs (location, an_info, an_loop_info,
948 list_size, rank);
949 replace_array_notations (&stmt, true, array_list, array_operand);
950 create_cmp_incr (location, &an_loop_info, rank, an_info, tf_warning_or_error);
952 an_init = pop_stmt_list (an_init);
953 append_to_statement_list (an_init, &loop_with_init);
954 body = stmt;
956 for (ii = 0; ii < rank; ii++)
958 tree new_loop = push_stmt_list ();
959 create_an_loop (an_loop_info[ii].ind_init, an_loop_info[ii].cmp,
960 an_loop_info[ii].incr, body);
961 body = pop_stmt_list (new_loop);
963 append_to_statement_list (body, &loop_with_init);
965 an_info.release ();
966 an_loop_info.release ();
968 return loop_with_init;
971 /* Transforms array notations inside unary expression ORIG_STMT with an
972 appropriate loop and ARRAY_REF (and returns all this as a super-tree called
973 LOOP). */
975 static tree
976 expand_unary_array_notation_exprs (tree orig_stmt)
978 vec<tree, va_gc> *array_list = NULL, *array_operand = NULL;
979 size_t list_size = 0, rank = 0, ii = 0;
980 tree body;
981 tree builtin_loop, stmt = NULL_TREE, new_var = NULL_TREE;
982 location_t location = EXPR_LOCATION (orig_stmt);
983 tree an_init, loop_with_init = alloc_stmt_list ();
984 vec<vec<an_parts> > an_info = vNULL;
985 vec<an_loop_parts> an_loop_info = vNULL;
987 if (!find_rank (location, orig_stmt, orig_stmt, true, &rank))
988 return error_mark_node;
989 if (rank == 0)
990 return orig_stmt;
992 extract_array_notation_exprs (orig_stmt, false, &array_list);
993 list_size = vec_safe_length (array_list);
994 location = EXPR_LOCATION (orig_stmt);
995 stmt = NULL_TREE;
996 for (ii = 0; ii < list_size; ii++)
997 if (TREE_CODE ((*array_list)[ii]) == CALL_EXPR
998 || TREE_CODE ((*array_list)[ii]) == AGGR_INIT_EXPR)
1000 tree list_node = (*array_list)[ii];
1001 builtin_loop = expand_sec_reduce_builtin (list_node, &new_var);
1002 if (builtin_loop == error_mark_node)
1003 return error_mark_node;
1004 else if (builtin_loop)
1006 vec<tree, va_gc> *sub_list = NULL, *new_var_list = NULL;
1007 stmt = alloc_stmt_list ();
1008 append_to_statement_list (builtin_loop, &stmt);
1009 vec_safe_push (sub_list, list_node);
1010 vec_safe_push (new_var_list, new_var);
1011 replace_array_notations (&orig_stmt, false, sub_list, new_var_list);
1014 if (stmt != NULL_TREE)
1015 append_to_statement_list (finish_expr_stmt (orig_stmt), &stmt);
1016 else
1017 stmt = orig_stmt;
1018 rank = 0;
1019 list_size = 0;
1020 array_list = NULL;
1021 extract_array_notation_exprs (stmt, true, &array_list);
1022 list_size = vec_safe_length (array_list);
1024 if (!find_rank (EXPR_LOCATION (stmt), stmt, stmt, true, &rank))
1025 return error_mark_node;
1026 if (rank == 0 || list_size == 0)
1027 return stmt;
1028 an_loop_info.safe_grow_cleared (rank);
1029 an_init = push_stmt_list ();
1030 /* Assign the array notation components to variable so that they can satisfy
1031 the exec-once rule. */
1032 for (ii = 0; ii < list_size; ii++)
1034 tree array_node = (*array_list)[ii];
1035 make_triplet_val_inv (&ARRAY_NOTATION_START (array_node));
1036 make_triplet_val_inv (&ARRAY_NOTATION_LENGTH (array_node));
1037 make_triplet_val_inv (&ARRAY_NOTATION_STRIDE (array_node));
1039 cilkplus_extract_an_triplets (array_list, list_size, rank, &an_info);
1041 for (ii = 0; ii < rank; ii++)
1043 tree typ = ptrdiff_type_node;
1044 an_loop_info[ii].var = create_temporary_var (typ);
1045 add_decl_expr (an_loop_info[ii].var);
1046 an_loop_info[ii].ind_init = build_x_modify_expr
1047 (location, an_loop_info[ii].var, INIT_EXPR, build_zero_cst (typ),
1048 tf_warning_or_error);
1050 array_operand = create_array_refs (location, an_info, an_loop_info,
1051 list_size, rank);
1052 replace_array_notations (&stmt, true, array_list, array_operand);
1053 create_cmp_incr (location, &an_loop_info, rank, an_info, tf_warning_or_error);
1055 an_init = pop_stmt_list (an_init);
1056 append_to_statement_list (an_init, &loop_with_init);
1057 body = stmt;
1059 for (ii = 0; ii < rank; ii++)
1061 tree new_loop = push_stmt_list ();
1062 create_an_loop (an_loop_info[ii].ind_init, an_loop_info[ii].cmp,
1063 an_loop_info[ii].incr, body);
1064 body = pop_stmt_list (new_loop);
1066 append_to_statement_list (body, &loop_with_init);
1068 an_info.release ();
1069 an_loop_info.release ();
1071 return loop_with_init;
1074 /* Expands the array notation's builtin reduction function in EXPR
1075 (of type RETURN_EXPR) and returns a STATEMENT_LIST that contains a loop
1076 with the builtin function expansion and a return statement at the end. */
1078 static tree
1079 expand_return_expr (tree expr)
1081 tree new_mod_list, new_var, new_mod, retval_expr;
1082 size_t rank = 0;
1083 location_t loc = EXPR_LOCATION (expr);
1084 if (TREE_CODE (expr) != RETURN_EXPR)
1085 return expr;
1087 if (!find_rank (loc, expr, expr, false, &rank))
1088 return error_mark_node;
1090 /* If the return expression contains array notations, then flag it as
1091 error. */
1092 if (rank >= 1)
1094 error_at (loc, "array notation expression cannot be used as a return "
1095 "value");
1096 return error_mark_node;
1099 new_mod_list = push_stmt_list ();
1100 retval_expr = TREE_OPERAND (expr, 0);
1101 new_var = create_temporary_var (TREE_TYPE (retval_expr));
1102 add_decl_expr (new_var);
1103 new_mod = expand_an_in_modify_expr (loc, new_var, NOP_EXPR,
1104 TREE_OPERAND (retval_expr, 1),
1105 tf_warning_or_error);
1106 TREE_OPERAND (retval_expr, 1) = new_var;
1107 TREE_OPERAND (expr, 0) = retval_expr;
1108 add_stmt (new_mod);
1109 add_stmt (expr);
1110 new_mod_list = pop_stmt_list (new_mod_list);
1111 return new_mod_list;
1114 /* Expands ARRAY_NOTATION_REF and builtin functions in a compound statement,
1115 STMT. Returns the STMT with expanded array notations. */
1117 tree
1118 expand_array_notation_exprs (tree t)
1120 enum tree_code code;
1121 bool is_expr;
1122 location_t loc = UNKNOWN_LOCATION;
1124 if (!t)
1125 return t;
1127 loc = EXPR_LOCATION (t);
1129 code = TREE_CODE (t);
1130 is_expr = IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code));
1131 switch (code)
1133 case ERROR_MARK:
1134 case IDENTIFIER_NODE:
1135 case VOID_CST:
1136 case INTEGER_CST:
1137 case REAL_CST:
1138 case FIXED_CST:
1139 case STRING_CST:
1140 case BLOCK:
1141 case PLACEHOLDER_EXPR:
1142 case FIELD_DECL:
1143 case VOID_TYPE:
1144 case REAL_TYPE:
1145 case SSA_NAME:
1146 case LABEL_DECL:
1147 case RESULT_DECL:
1148 case VAR_DECL:
1149 case PARM_DECL:
1150 case NON_LVALUE_EXPR:
1151 case NOP_EXPR:
1152 case ADDR_EXPR:
1153 case ARRAY_REF:
1154 case BIT_FIELD_REF:
1155 case VECTOR_CST:
1156 case COMPLEX_CST:
1157 return t;
1158 case INIT_EXPR:
1159 case MODIFY_EXPR:
1160 if (contains_array_notation_expr (t))
1161 t = expand_an_in_modify_expr (loc, TREE_OPERAND (t, 0), NOP_EXPR,
1162 TREE_OPERAND (t, 1),
1163 tf_warning_or_error);
1164 return t;
1165 case MODOP_EXPR:
1166 if (contains_array_notation_expr (t) && !processing_template_decl)
1167 t = expand_an_in_modify_expr
1168 (loc, TREE_OPERAND (t, 0), TREE_CODE (TREE_OPERAND (t, 1)),
1169 TREE_OPERAND (t, 2), tf_warning_or_error);
1170 return t;
1171 case CONSTRUCTOR:
1172 return t;
1173 case BIND_EXPR:
1175 BIND_EXPR_BODY (t) =
1176 expand_array_notation_exprs (BIND_EXPR_BODY (t));
1177 return t;
1179 case DECL_EXPR:
1180 if (contains_array_notation_expr (t))
1182 tree x = DECL_EXPR_DECL (t);
1183 if (DECL_INITIAL (x))
1185 location_t loc = DECL_SOURCE_LOCATION (x);
1186 tree lhs = x;
1187 tree rhs = DECL_INITIAL (x);
1188 DECL_INITIAL (x) = NULL;
1189 tree new_modify_expr = build_modify_expr (loc, lhs,
1190 TREE_TYPE (lhs),
1191 NOP_EXPR,
1192 loc, rhs,
1193 TREE_TYPE(rhs));
1194 t = expand_array_notation_exprs (new_modify_expr);
1197 return t;
1198 case STATEMENT_LIST:
1200 tree_stmt_iterator i;
1201 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
1202 *tsi_stmt_ptr (i) =
1203 expand_array_notation_exprs (*tsi_stmt_ptr (i));
1204 return t;
1207 case OMP_PARALLEL:
1208 case OMP_TASK:
1209 case OMP_FOR:
1210 case OMP_SINGLE:
1211 case OMP_SECTION:
1212 case OMP_SECTIONS:
1213 case OMP_MASTER:
1214 case OMP_TASKGROUP:
1215 case OMP_ORDERED:
1216 case OMP_CRITICAL:
1217 case OMP_ATOMIC:
1218 case OMP_CLAUSE:
1219 case TARGET_EXPR:
1220 case INTEGER_TYPE:
1221 case ENUMERAL_TYPE:
1222 case BOOLEAN_TYPE:
1223 case POINTER_TYPE:
1224 case ARRAY_TYPE:
1225 case RECORD_TYPE:
1226 case METHOD_TYPE:
1227 return t;
1228 case RETURN_EXPR:
1229 if (contains_array_notation_expr (t))
1230 t = expand_return_expr (t);
1231 return t;
1232 case PREDECREMENT_EXPR:
1233 case PREINCREMENT_EXPR:
1234 case POSTDECREMENT_EXPR:
1235 case POSTINCREMENT_EXPR:
1236 case AGGR_INIT_EXPR:
1237 case CALL_EXPR:
1238 t = expand_unary_array_notation_exprs (t);
1239 return t;
1240 case CONVERT_EXPR:
1241 case CLEANUP_POINT_EXPR:
1242 case EXPR_STMT:
1243 TREE_OPERAND (t, 0) = expand_array_notation_exprs (TREE_OPERAND (t, 0));
1244 /* It is not necessary to wrap error_mark_node in EXPR_STMT. */
1245 if (TREE_OPERAND (t, 0) == error_mark_node)
1246 return TREE_OPERAND (t, 0);
1247 return t;
1248 case TRUTH_ANDIF_EXPR:
1249 case TRUTH_ORIF_EXPR:
1250 case TRUTH_AND_EXPR:
1251 case TRUTH_OR_EXPR:
1252 case TRUTH_XOR_EXPR:
1253 case TRUTH_NOT_EXPR:
1254 case COND_EXPR:
1255 t = cp_expand_cond_array_notations (t);
1256 if (TREE_CODE (t) == COND_EXPR)
1258 COND_EXPR_THEN (t) =
1259 expand_array_notation_exprs (COND_EXPR_THEN (t));
1260 COND_EXPR_ELSE (t) =
1261 expand_array_notation_exprs (COND_EXPR_ELSE (t));
1263 return t;
1264 case FOR_STMT:
1265 if (contains_array_notation_expr (FOR_COND (t)))
1267 error_at (EXPR_LOCATION (FOR_COND (t)),
1268 "array notation cannot be used in a condition for "
1269 "a for-loop");
1270 return error_mark_node;
1272 /* FIXME: Add a check for CILK_FOR_STMT here when we add Cilk tasking
1273 keywords. */
1274 if (TREE_CODE (t) == FOR_STMT)
1276 FOR_BODY (t) = expand_array_notation_exprs (FOR_BODY (t));
1277 FOR_EXPR (t) = expand_array_notation_exprs (FOR_EXPR (t));
1279 else
1280 t = expand_array_notation_exprs (t);
1281 return t;
1282 case IF_STMT:
1283 t = cp_expand_cond_array_notations (t);
1284 /* If the above function added some extra instructions above the original
1285 if statement, then we can't assume it is still IF_STMT so we have to
1286 check again. */
1287 if (TREE_CODE (t) == IF_STMT)
1289 if (THEN_CLAUSE (t))
1290 THEN_CLAUSE (t) = expand_array_notation_exprs (THEN_CLAUSE (t));
1291 if (ELSE_CLAUSE (t))
1292 ELSE_CLAUSE (t) = expand_array_notation_exprs (ELSE_CLAUSE (t));
1294 else
1295 t = expand_array_notation_exprs (t);
1296 return t;
1297 case SWITCH_STMT:
1298 if (contains_array_notation_expr (SWITCH_STMT_COND (t)))
1300 error_at (EXPR_LOCATION (SWITCH_STMT_COND (t)),
1301 "array notation cannot be used as a condition for "
1302 "switch statement");
1303 return error_mark_node;
1305 if (SWITCH_STMT_BODY (t))
1306 SWITCH_STMT_BODY (t) =
1307 expand_array_notation_exprs (SWITCH_STMT_BODY (t));
1308 return t;
1309 case WHILE_STMT:
1310 if (contains_array_notation_expr (WHILE_COND (t)))
1312 if (EXPR_LOCATION (WHILE_COND (t)) != UNKNOWN_LOCATION)
1313 loc = EXPR_LOCATION (WHILE_COND (t));
1314 error_at (loc, "array notation cannot be used as a condition for "
1315 "while statement");
1316 return error_mark_node;
1318 if (WHILE_BODY (t))
1319 WHILE_BODY (t) = expand_array_notation_exprs (WHILE_BODY (t));
1320 return t;
1321 case DO_STMT:
1322 if (contains_array_notation_expr (DO_COND (t)))
1324 error_at (EXPR_LOCATION (DO_COND (t)),
1325 "array notation cannot be used as a condition for a "
1326 "do-while statement");
1327 return error_mark_node;
1329 if (DO_BODY (t))
1330 DO_BODY (t) = expand_array_notation_exprs (DO_BODY (t));
1331 return t;
1332 default:
1333 if (is_expr)
1335 int i, len;
1337 /* Walk over all the sub-trees of this operand. */
1338 len = TREE_CODE_LENGTH (code);
1340 /* Go through the subtrees. We need to do this in forward order so
1341 that the scope of a FOR_EXPR is handled properly. */
1342 for (i = 0; i < len; ++i)
1343 TREE_OPERAND (t, i) =
1344 expand_array_notation_exprs (TREE_OPERAND (t, i));
1346 return t;
1348 return t;
1351 /* Given the base of an array (ARRAY), the START (start_index), the number of
1352 elements to be accessed (LENGTH) and the STRIDE, construct an
1353 ARRAY_NOTATION_REF tree of type TYPE and return it. Restrictions on START,
1354 LENGTH and STRIDE are the same as that of index field passed into ARRAY_REF.
1355 The only additional restriction is that, unlike index in ARRAY_REF, stride,
1356 length and start_index cannot contain array notations. */
1358 tree
1359 build_array_notation_ref (location_t loc, tree array, tree start, tree length,
1360 tree stride, tree type)
1362 tree array_ntn_expr = NULL_TREE;
1364 /* If we enter the then-case of the if-statement below, we have hit a case
1365 like this: ARRAY [:]. */
1366 if (!start && !length)
1368 if (TREE_CODE (type) != ARRAY_TYPE)
1370 error_at (loc, "start-index and length fields necessary for "
1371 "using array notation in pointers or records");
1372 return error_mark_node;
1374 tree domain = TYPE_DOMAIN (type);
1375 if (!domain)
1377 error_at (loc, "start-index and length fields necessary for "
1378 "using array notation with array of unknown bound");
1379 return error_mark_node;
1381 start = cp_fold_convert (ptrdiff_type_node, TYPE_MINVAL (domain));
1382 length = size_binop (PLUS_EXPR, TYPE_MAXVAL (domain), size_one_node);
1383 length = cp_fold_convert (ptrdiff_type_node, length);
1386 if (!stride)
1387 stride = build_one_cst (ptrdiff_type_node);
1389 /* When dealing with templates, triplet type-checking will be done in pt.c
1390 after type substitution. */
1391 if (processing_template_decl
1392 && (type_dependent_expression_p (array)
1393 || type_dependent_expression_p (length)
1394 || type_dependent_expression_p (start)
1395 || type_dependent_expression_p (stride)))
1396 array_ntn_expr = build_min_nt_loc (loc, ARRAY_NOTATION_REF, array, start,
1397 length, stride, NULL_TREE);
1398 else
1400 if (!cilkplus_an_triplet_types_ok_p (loc, start, length, stride, type))
1401 return error_mark_node;
1402 array_ntn_expr = build4 (ARRAY_NOTATION_REF, NULL_TREE, array, start,
1403 length, stride);
1405 if (TREE_CODE (type) == ARRAY_TYPE || TREE_CODE (type) == POINTER_TYPE)
1406 TREE_TYPE (array_ntn_expr) = TREE_TYPE (type);
1407 else
1409 error_at (loc, "base of array section must be pointer or array type");
1410 return error_mark_node;
1413 SET_EXPR_LOCATION (array_ntn_expr, loc);
1414 return array_ntn_expr;
1417 /* Returns false if any of the Array notation triplet values: START_INDEX,
1418 LENGTH and STRIDE, are not of integral type and have a rank greater than
1419 zero. */
1421 bool
1422 cilkplus_an_triplet_types_ok_p (location_t loc, tree start_index, tree length,
1423 tree stride, tree type)
1425 size_t stride_rank = 0, length_rank = 0, start_rank = 0;
1426 if (!TREE_TYPE (start_index) || !INTEGRAL_TYPE_P (TREE_TYPE (start_index)))
1428 error_at (loc, "start-index of array notation triplet is not an integer");
1429 return false;
1431 if (!TREE_TYPE (length) || !INTEGRAL_TYPE_P (TREE_TYPE (length)))
1433 error_at (loc, "length of array notation triplet is not an integer");
1434 return false;
1436 if (!TREE_TYPE (stride) || !INTEGRAL_TYPE_P (TREE_TYPE (stride)))
1438 error_at (loc, "stride of array notation triplet is not an integer");
1439 return false;
1441 if (TREE_CODE (type) == FUNCTION_TYPE)
1443 error_at (loc, "array notation cannot be used with function type");
1444 return false;
1446 if (!find_rank (loc, start_index, start_index, false, &start_rank)
1447 || !find_rank (loc, length, length, false, &length_rank)
1448 || !find_rank (loc, stride, stride, false, &stride_rank))
1449 return false;
1451 if (start_rank != 0)
1453 error_at (loc, "rank of an array notation triplet%'s start-index is not "
1454 "zero");
1455 return false;
1457 if (length_rank != 0)
1459 error_at (loc, "rank of an array notation triplet%'s length is not zero");
1460 return false;
1462 if (stride_rank != 0)
1464 error_at (loc, "rank of array notation triplet%'s stride is not zero");
1465 return false;
1467 return true;