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>,
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)
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:
29 3. Number of elements we need to access (we call it length)
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>;
55 #include "coretypes.h"
65 #include "c-family/c-common.h"
66 #include "diagnostic.h"
67 #include "tree-iterator.h"
70 /* Creates a FOR_STMT with INIT, COND, INCR and BODY as the initializer,
71 condition, increment expression and the loop-body, respectively. */
74 create_an_loop (tree init
, tree cond
, tree incr
, tree body
)
78 finish_expr_stmt (init
);
79 for_stmt
= begin_for_stmt (NULL_TREE
, NULL_TREE
);
80 finish_for_init_stmt (for_stmt
);
81 finish_for_cond (cond
, for_stmt
, false);
82 finish_for_expr (incr
, for_stmt
);
83 finish_expr_stmt (body
);
84 finish_for_stmt (for_stmt
);
87 /* If *VALUE is not a constant integer, then this function replaces it with
88 a variable to make it loop invariant for array notations. */
91 make_triplet_val_inv (tree
*value
)
93 if (TREE_CODE (*value
) != INTEGER_CST
94 && TREE_CODE (*value
) != PARM_DECL
95 && TREE_CODE (*value
) != VAR_DECL
)
96 *value
= get_temp_regvar (ptrdiff_type_node
, *value
);
99 /* Returns a vector of size RANK that contains an ARRAY_REF. This vector is
100 created using array notation-triplet information stored in AN_INFO. The
101 induction var is taken from AN_LOOP_INFO.
103 For example: For an array notation A[5:10:2], the vector start will be
104 of size 1 holding '5', stride of same size as start but holding the value of
105 as 2, and is_vector as true. Let's assume VAR is 'x'
106 This function returns a vector of size 1 with the following data:
110 static vec
<tree
, va_gc
> *
111 create_array_refs (location_t loc
, vec
<vec
<an_parts
> > an_info
,
112 vec
<an_loop_parts
> an_loop_info
, size_t size
, size_t rank
)
114 tree ind_mult
, ind_incr
;
115 vec
<tree
, va_gc
> *array_operand
= NULL
;
117 for (size_t ii
= 0; ii
< size
; ii
++)
118 if (an_info
[ii
][0].is_vector
)
120 tree array_opr
= an_info
[ii
][rank
- 1].value
;
121 for (int s_jj
= rank
-1; s_jj
>= 0; s_jj
--)
123 tree start
= cp_fold_convert (ptrdiff_type_node
,
124 an_info
[ii
][s_jj
].start
);
125 tree stride
= cp_fold_convert (ptrdiff_type_node
,
126 an_info
[ii
][s_jj
].stride
);
127 tree var
= cp_fold_convert (ptrdiff_type_node
,
128 an_loop_info
[s_jj
].var
);
130 ind_mult
= build2 (MULT_EXPR
, TREE_TYPE (var
), var
, stride
);
131 ind_incr
= build2 (PLUS_EXPR
, TREE_TYPE (var
), start
, ind_mult
);
132 /* Array [ start_index + (induction_var * stride)] */
133 array_opr
= grok_array_decl (loc
, array_opr
, ind_incr
, false);
135 vec_safe_push (array_operand
, array_opr
);
138 vec_safe_push (array_operand
, integer_one_node
);
139 return array_operand
;
142 /* Populates the INCR and CMP fields in *NODE with the increment
143 (of type POSTINCREMENT) and comparison (of TYPE LT_EXPR) expressions, using
144 data from AN_INFO. */
147 create_cmp_incr (location_t loc
, vec
<an_loop_parts
> *node
, size_t rank
,
148 vec
<vec
<an_parts
> > an_info
, tsubst_flags_t complain
)
150 for (size_t ii
= 0; ii
< rank
; ii
++)
152 (*node
)[ii
].incr
= build_x_unary_op (loc
, POSTINCREMENT_EXPR
,
153 (*node
)[ii
].var
, complain
);
154 (*node
)[ii
].cmp
= build_x_binary_op (loc
, LT_EXPR
, (*node
)[ii
].var
,
155 TREE_CODE ((*node
)[ii
].var
),
156 an_info
[0][ii
].length
,
157 TREE_CODE (an_info
[0][ii
].length
),
162 /* Replaces all the scalar expressions in *NODE. Returns a STATEMENT LIST that
163 holds the NODE along with the variables that hold the results of the
164 invariant expressions. */
167 replace_invariant_exprs (tree
*node
)
170 tree node_list
= NULL_TREE
;
171 tree t
= NULL_TREE
, new_var
= NULL_TREE
;
172 struct inv_list data
;
174 data
.list_values
= NULL
;
175 data
.replacement
= NULL
;
176 data
.additional_tcodes
= NULL
;
177 cp_walk_tree (node
, find_inv_trees
, (void *) &data
, NULL
);
179 if (vec_safe_length (data
.list_values
))
181 node_list
= push_stmt_list ();
182 for (ix
= 0; vec_safe_iterate (data
.list_values
, ix
, &t
); ix
++)
184 /* Sometimes, when comma_expr has a function call in it, it will
185 typecast it to void. Find_inv_trees finds those nodes and so
186 if it void type, then don't bother creating a new var to hold
188 if (VOID_TYPE_P (TREE_TYPE (t
)))
190 finish_expr_stmt (t
);
194 new_var
= get_temp_regvar (TREE_TYPE (t
), t
);
195 vec_safe_push (data
.replacement
, new_var
);
197 cp_walk_tree (node
, replace_inv_trees
, (void *) &data
, NULL
);
198 node_list
= pop_stmt_list (node_list
);
203 /* Replace array notation's built-in function passed in AN_BUILTIN_FN with
204 the appropriate loop and computation (all stored in variable LOOP of type
205 tree node). The output of the function function is always a scalar and that
206 result is returned in *NEW_VAR. *NEW_VAR is NULL_TREE if the function is
207 __sec_reduce_mutating. */
210 expand_sec_reduce_builtin (tree an_builtin_fn
, tree
*new_var
)
212 tree new_var_type
= NULL_TREE
, func_parm
, new_yes_expr
, new_no_expr
;
213 tree array_ind_value
= NULL_TREE
, new_no_ind
, new_yes_ind
, new_no_list
;
214 tree new_yes_list
, new_cond_expr
, new_expr
= NULL_TREE
;
215 vec
<tree
, va_gc
> *array_list
= NULL
, *array_operand
= NULL
;
216 size_t list_size
= 0, rank
= 0, ii
= 0;
217 tree body
, an_init
, loop_with_init
= alloc_stmt_list ();
218 tree array_op0
, comp_node
= NULL_TREE
;
219 tree call_fn
= NULL_TREE
, identity_value
= NULL_TREE
;
220 tree init
= NULL_TREE
, cond_init
= NULL_TREE
;
221 enum tree_code code
= NOP_EXPR
;
222 location_t location
= UNKNOWN_LOCATION
;
223 vec
<vec
<an_parts
> > an_info
= vNULL
;
224 vec
<an_loop_parts
> an_loop_info
= vNULL
;
225 enum built_in_function an_type
=
226 is_cilkplus_reduce_builtin (CALL_EXPR_FN (an_builtin_fn
));
227 vec
<tree
, va_gc
> *func_args
;
229 if (an_type
== BUILT_IN_NONE
)
232 if (an_type
!= BUILT_IN_CILKPLUS_SEC_REDUCE
233 && an_type
!= BUILT_IN_CILKPLUS_SEC_REDUCE_MUTATING
)
234 func_parm
= CALL_EXPR_ARG (an_builtin_fn
, 0);
237 call_fn
= CALL_EXPR_ARG (an_builtin_fn
, 2);
239 /* We need to do this because we are "faking" the builtin function types,
240 so the compiler does a bunch of typecasts and this will get rid of
242 STRIP_NOPS (call_fn
);
243 if (TREE_CODE (call_fn
) != OVERLOAD
244 && TREE_CODE (call_fn
) != FUNCTION_DECL
)
245 call_fn
= TREE_OPERAND (call_fn
, 0);
246 identity_value
= CALL_EXPR_ARG (an_builtin_fn
, 0);
247 func_parm
= CALL_EXPR_ARG (an_builtin_fn
, 1);
248 STRIP_NOPS (identity_value
);
250 STRIP_NOPS (func_parm
);
252 location
= EXPR_LOCATION (an_builtin_fn
);
254 /* Note about using find_rank (): If find_rank returns false, then it must
255 have already reported an error, thus we just return an error_mark_node
256 without any doing any error emission. */
257 if (!find_rank (location
, an_builtin_fn
, an_builtin_fn
, true, &rank
))
258 return error_mark_node
;
261 error_at (location
, "Invalid builtin arguments");
262 return error_mark_node
;
265 && (an_type
== BUILT_IN_CILKPLUS_SEC_REDUCE_MAX_IND
266 || an_type
== BUILT_IN_CILKPLUS_SEC_REDUCE_MIN_IND
))
268 error_at (location
, "__sec_reduce_min_ind or __sec_reduce_max_ind cannot "
269 "have arrays with dimension greater than 1");
270 return error_mark_node
;
273 extract_array_notation_exprs (func_parm
, true, &array_list
);
274 list_size
= vec_safe_length (array_list
);
277 case BUILT_IN_CILKPLUS_SEC_REDUCE_ADD
:
278 case BUILT_IN_CILKPLUS_SEC_REDUCE_MUL
:
279 case BUILT_IN_CILKPLUS_SEC_REDUCE_MAX
:
280 case BUILT_IN_CILKPLUS_SEC_REDUCE_MIN
:
281 new_var_type
= TREE_TYPE ((*array_list
)[0]);
283 case BUILT_IN_CILKPLUS_SEC_REDUCE_ALL_ZERO
:
284 case BUILT_IN_CILKPLUS_SEC_REDUCE_ANY_ZERO
:
285 case BUILT_IN_CILKPLUS_SEC_REDUCE_ANY_NONZERO
:
286 case BUILT_IN_CILKPLUS_SEC_REDUCE_ALL_NONZERO
:
287 new_var_type
= boolean_type_node
;
289 case BUILT_IN_CILKPLUS_SEC_REDUCE_MAX_IND
:
290 case BUILT_IN_CILKPLUS_SEC_REDUCE_MIN_IND
:
291 new_var_type
= size_type_node
;
293 case BUILT_IN_CILKPLUS_SEC_REDUCE
:
294 if (call_fn
&& identity_value
)
295 new_var_type
= TREE_TYPE ((*array_list
)[0]);
297 case BUILT_IN_CILKPLUS_SEC_REDUCE_MUTATING
:
298 new_var_type
= NULL_TREE
;
304 if (new_var_type
&& TREE_CODE (new_var_type
) == ARRAY_TYPE
)
305 new_var_type
= TREE_TYPE (new_var_type
);
306 an_loop_info
.safe_grow_cleared (rank
);
308 an_init
= push_stmt_list ();
310 /* Assign the array notation components to variable so that they can satisfy
311 the exec-once rule. */
312 for (ii
= 0; ii
< list_size
; ii
++)
313 if (TREE_CODE ((*array_list
)[ii
]) == ARRAY_NOTATION_REF
)
315 tree anode
= (*array_list
)[ii
];
316 make_triplet_val_inv (&ARRAY_NOTATION_START (anode
));
317 make_triplet_val_inv (&ARRAY_NOTATION_LENGTH (anode
));
318 make_triplet_val_inv (&ARRAY_NOTATION_STRIDE (anode
));
320 cilkplus_extract_an_triplets (array_list
, list_size
, rank
, &an_info
);
321 for (ii
= 0; ii
< rank
; ii
++)
323 tree typ
= ptrdiff_type_node
;
325 /* In this place, we are using get_temp_regvar instead of
326 create_temporary_var if an_type is SEC_REDUCE_MAX/MIN_IND because
327 the array_ind_value depends on this value being initalized to 0. */
328 if (an_type
== BUILT_IN_CILKPLUS_SEC_REDUCE_MAX_IND
329 || an_type
== BUILT_IN_CILKPLUS_SEC_REDUCE_MIN_IND
)
330 an_loop_info
[ii
].var
= get_temp_regvar (typ
, build_zero_cst (typ
));
333 an_loop_info
[ii
].var
= create_temporary_var (typ
);
334 add_decl_expr (an_loop_info
[ii
].var
);
336 an_loop_info
[ii
].ind_init
=
337 build_x_modify_expr (location
, an_loop_info
[ii
].var
, INIT_EXPR
,
338 build_zero_cst (typ
), tf_warning_or_error
);
340 array_operand
= create_array_refs (location
, an_info
, an_loop_info
,
342 replace_array_notations (&func_parm
, true, array_list
, array_operand
);
344 if (!TREE_TYPE (func_parm
))
345 TREE_TYPE (func_parm
) = TREE_TYPE ((*array_list
)[0]);
347 create_cmp_incr (location
, &an_loop_info
, rank
, an_info
, tf_warning_or_error
);
348 if (an_type
== BUILT_IN_CILKPLUS_SEC_REDUCE_MAX_IND
349 || an_type
== BUILT_IN_CILKPLUS_SEC_REDUCE_MIN_IND
)
350 array_ind_value
= get_temp_regvar (TREE_TYPE (func_parm
), func_parm
);
352 array_op0
= (*array_operand
)[0];
353 if (TREE_CODE (array_op0
) == INDIRECT_REF
)
354 array_op0
= TREE_OPERAND (array_op0
, 0);
357 case BUILT_IN_CILKPLUS_SEC_REDUCE_ADD
:
359 init
= build_zero_cst (new_var_type
);
361 case BUILT_IN_CILKPLUS_SEC_REDUCE_MUL
:
363 init
= build_one_cst (new_var_type
);
365 case BUILT_IN_CILKPLUS_SEC_REDUCE_ANY_ZERO
:
366 case BUILT_IN_CILKPLUS_SEC_REDUCE_ANY_NONZERO
:
367 code
= ((an_type
== BUILT_IN_CILKPLUS_SEC_REDUCE_ANY_ZERO
) ? EQ_EXPR
369 init
= build_zero_cst (new_var_type
);
370 cond_init
= build_one_cst (new_var_type
);
371 comp_node
= build_zero_cst (TREE_TYPE (func_parm
));
373 case BUILT_IN_CILKPLUS_SEC_REDUCE_ALL_ZERO
:
374 case BUILT_IN_CILKPLUS_SEC_REDUCE_ALL_NONZERO
:
375 code
= ((an_type
== BUILT_IN_CILKPLUS_SEC_REDUCE_ALL_ZERO
) ? NE_EXPR
377 init
= build_one_cst (new_var_type
);
378 cond_init
= build_zero_cst (new_var_type
);
379 comp_node
= build_zero_cst (TREE_TYPE (func_parm
));
381 case BUILT_IN_CILKPLUS_SEC_REDUCE_MAX
:
383 init
= (TYPE_MIN_VALUE (new_var_type
) ? TYPE_MIN_VALUE (new_var_type
)
386 case BUILT_IN_CILKPLUS_SEC_REDUCE_MIN
:
388 init
= (TYPE_MAX_VALUE (new_var_type
) ? TYPE_MAX_VALUE (new_var_type
)
391 case BUILT_IN_CILKPLUS_SEC_REDUCE_MIN_IND
:
392 case BUILT_IN_CILKPLUS_SEC_REDUCE_MAX_IND
:
393 code
= (an_type
== BUILT_IN_CILKPLUS_SEC_REDUCE_MAX_IND
? LE_EXPR
395 init
= an_loop_info
[0].var
;
397 case BUILT_IN_CILKPLUS_SEC_REDUCE
:
398 init
= identity_value
;
400 case BUILT_IN_CILKPLUS_SEC_REDUCE_MUTATING
:
407 if (an_type
!= BUILT_IN_CILKPLUS_SEC_REDUCE_MUTATING
)
408 *new_var
= get_temp_regvar (new_var_type
, init
);
410 *new_var
= NULL_TREE
;
414 case BUILT_IN_CILKPLUS_SEC_REDUCE_ADD
:
415 case BUILT_IN_CILKPLUS_SEC_REDUCE_MUL
:
416 new_expr
= build_x_modify_expr (location
, *new_var
, code
, func_parm
,
417 tf_warning_or_error
);
419 case BUILT_IN_CILKPLUS_SEC_REDUCE_ALL_ZERO
:
420 case BUILT_IN_CILKPLUS_SEC_REDUCE_ALL_NONZERO
:
421 case BUILT_IN_CILKPLUS_SEC_REDUCE_ANY_ZERO
:
422 case BUILT_IN_CILKPLUS_SEC_REDUCE_ANY_NONZERO
:
423 /* In all these cases, assume the false case is true and as soon as
424 we find a true case, set the true flag on and latch it in. */
425 new_yes_expr
= build_x_modify_expr (location
, *new_var
, NOP_EXPR
,
426 cond_init
, tf_warning_or_error
);
427 new_no_expr
= build_x_modify_expr (location
, *new_var
, NOP_EXPR
,
428 *new_var
, tf_warning_or_error
);
429 new_cond_expr
= build_x_binary_op
430 (location
, code
, func_parm
, TREE_CODE (func_parm
), comp_node
,
431 TREE_CODE (comp_node
), NULL
, tf_warning_or_error
);
432 new_expr
= build_x_conditional_expr (location
, new_cond_expr
,
433 new_yes_expr
, new_no_expr
,
434 tf_warning_or_error
);
436 case BUILT_IN_CILKPLUS_SEC_REDUCE_MAX
:
437 case BUILT_IN_CILKPLUS_SEC_REDUCE_MIN
:
438 new_cond_expr
= build_x_binary_op
439 (location
, code
, *new_var
, TREE_CODE (*new_var
), func_parm
,
440 TREE_CODE (func_parm
), NULL
, tf_warning_or_error
);
441 new_expr
= build_x_modify_expr (location
, *new_var
, NOP_EXPR
, func_parm
,
442 tf_warning_or_error
);
444 case BUILT_IN_CILKPLUS_SEC_REDUCE_MAX_IND
:
445 case BUILT_IN_CILKPLUS_SEC_REDUCE_MIN_IND
:
446 new_yes_expr
= build_x_modify_expr (location
, array_ind_value
, NOP_EXPR
,
447 func_parm
, tf_warning_or_error
);
448 new_no_expr
= build_x_modify_expr (location
, array_ind_value
, NOP_EXPR
,
449 array_ind_value
, tf_warning_or_error
);
451 new_yes_ind
= build_x_modify_expr (location
, *new_var
, NOP_EXPR
,
453 tf_warning_or_error
);
455 new_yes_ind
= build_x_modify_expr (location
, *new_var
, NOP_EXPR
,
456 TREE_OPERAND (array_op0
, 1),
457 tf_warning_or_error
);
458 new_no_ind
= build_x_modify_expr (location
, *new_var
, NOP_EXPR
, *new_var
,
459 tf_warning_or_error
);
460 new_yes_list
= alloc_stmt_list ();
461 append_to_statement_list (new_yes_ind
, &new_yes_list
);
462 append_to_statement_list (new_yes_expr
, &new_yes_list
);
464 new_no_list
= alloc_stmt_list ();
465 append_to_statement_list (new_no_ind
, &new_no_list
);
466 append_to_statement_list (new_no_expr
, &new_no_list
);
468 new_cond_expr
= build_x_binary_op (location
, code
, array_ind_value
,
469 TREE_CODE (array_ind_value
), func_parm
,
470 TREE_CODE (func_parm
), NULL
,
471 tf_warning_or_error
);
472 new_expr
= build_x_conditional_expr (location
, new_cond_expr
,
473 new_yes_list
, new_no_list
,
474 tf_warning_or_error
);
476 case BUILT_IN_CILKPLUS_SEC_REDUCE
:
477 case BUILT_IN_CILKPLUS_SEC_REDUCE_MUTATING
:
478 func_args
= make_tree_vector ();
479 if (an_type
== BUILT_IN_CILKPLUS_SEC_REDUCE
)
480 vec_safe_push (func_args
, *new_var
);
482 vec_safe_push (func_args
, identity_value
);
483 vec_safe_push (func_args
, func_parm
);
485 new_expr
= finish_call_expr (call_fn
, &func_args
, false, true,
486 tf_warning_or_error
);
487 if (an_type
== BUILT_IN_CILKPLUS_SEC_REDUCE
)
488 new_expr
= build_x_modify_expr (location
, *new_var
, NOP_EXPR
, new_expr
,
489 tf_warning_or_error
);
490 release_tree_vector (func_args
);
495 an_init
= pop_stmt_list (an_init
);
496 append_to_statement_list (an_init
, &loop_with_init
);
499 for (ii
= 0; ii
< rank
; ii
++)
501 tree new_loop
= push_stmt_list ();
502 create_an_loop (an_loop_info
[ii
].ind_init
, an_loop_info
[ii
].cmp
,
503 an_loop_info
[ii
].incr
, body
);
504 body
= pop_stmt_list (new_loop
);
506 append_to_statement_list (body
, &loop_with_init
);
509 an_loop_info
.release ();
511 return loop_with_init
;
514 /* Returns a loop with ARRAY_REF inside it with an appropriate modify expr.
515 The LHS and/or RHS will be array notation expressions that have a
516 MODIFYCODE. The location of the variable is specified by LOCATION. */
519 expand_an_in_modify_expr (location_t location
, tree lhs
,
520 enum tree_code modifycode
, tree rhs
,
521 tsubst_flags_t complain
)
523 tree array_expr_lhs
= NULL_TREE
, array_expr_rhs
= NULL_TREE
;
524 tree array_expr
= NULL_TREE
;
525 tree body
= NULL_TREE
;
526 vec
<tree
> cond_expr
= vNULL
;
527 vec
<tree
, va_gc
> *lhs_array_operand
= NULL
, *rhs_array_operand
= NULL
;
528 size_t lhs_rank
= 0, rhs_rank
= 0, ii
= 0;
529 vec
<tree
, va_gc
> *rhs_list
= NULL
, *lhs_list
= NULL
;
530 size_t rhs_list_size
= 0, lhs_list_size
= 0;
531 tree new_modify_expr
, new_var
= NULL_TREE
, builtin_loop
, scalar_mods
;
532 bool found_builtin_fn
= false;
533 tree an_init
, loop_with_init
= alloc_stmt_list ();
534 vec
<vec
<an_parts
> > lhs_an_info
= vNULL
, rhs_an_info
= vNULL
;
535 vec
<an_loop_parts
> lhs_an_loop_info
= vNULL
, rhs_an_loop_info
= vNULL
;
537 if (!find_rank (location
, rhs
, rhs
, false, &rhs_rank
))
538 return error_mark_node
;
539 extract_array_notation_exprs (rhs
, false, &rhs_list
);
540 rhs_list_size
= vec_safe_length (rhs_list
);
541 an_init
= push_stmt_list ();
544 scalar_mods
= replace_invariant_exprs (&rhs
);
546 finish_expr_stmt (scalar_mods
);
548 for (ii
= 0; ii
< rhs_list_size
; ii
++)
550 tree rhs_node
= (*rhs_list
)[ii
];
551 if (TREE_CODE (rhs_node
) == CALL_EXPR
)
553 builtin_loop
= expand_sec_reduce_builtin (rhs_node
, &new_var
);
554 if (builtin_loop
== error_mark_node
)
555 return error_mark_node
;
556 else if (builtin_loop
)
558 finish_expr_stmt (builtin_loop
);
559 found_builtin_fn
= true;
562 vec
<tree
, va_gc
> *rhs_sub_list
= NULL
, *new_var_list
= NULL
;
563 vec_safe_push (rhs_sub_list
, rhs_node
);
564 vec_safe_push (new_var_list
, new_var
);
565 replace_array_notations (&rhs
, false, rhs_sub_list
,
573 if (!find_rank (location
, lhs
, lhs
, true, &lhs_rank
)
574 || !find_rank (location
, rhs
, rhs
, true, &rhs_rank
))
576 pop_stmt_list (an_init
);
577 return error_mark_node
;
580 /* If both are scalar, then the only reason why we will get this far is if
581 there is some array notations inside it and was using a builtin array
582 notation functions. If so, we have already broken those guys up and now
583 a simple build_x_modify_expr would do. */
584 if (lhs_rank
== 0 && rhs_rank
== 0)
586 if (found_builtin_fn
)
588 new_modify_expr
= build_x_modify_expr (location
, lhs
,
589 modifycode
, rhs
, complain
);
590 finish_expr_stmt (new_modify_expr
);
591 pop_stmt_list (an_init
);
598 /* If for some reason location is not set, then find if LHS or RHS has
599 location info. If so, then use that so we atleast have an idea. */
600 if (location
== UNKNOWN_LOCATION
)
602 if (EXPR_LOCATION (lhs
) != UNKNOWN_LOCATION
)
603 location
= EXPR_LOCATION (lhs
);
604 else if (EXPR_LOCATION (rhs
) != UNKNOWN_LOCATION
)
605 location
= EXPR_LOCATION (rhs
);
608 /* We need this when we have a scatter issue. */
609 extract_array_notation_exprs (lhs
, true, &lhs_list
);
611 extract_array_notation_exprs (rhs
, true, &rhs_list
);
612 rhs_list_size
= vec_safe_length (rhs_list
);
613 lhs_list_size
= vec_safe_length (lhs_list
);
615 if (lhs_rank
== 0 && rhs_rank
!= 0)
617 error_at (location
, "%qE cannot be scalar when %qE is not", lhs
, rhs
);
618 return error_mark_node
;
620 if (lhs_rank
!= 0 && rhs_rank
!= 0 && lhs_rank
!= rhs_rank
)
622 error_at (location
, "rank mismatch between %qE and %qE", lhs
, rhs
);
623 return error_mark_node
;
626 /* Assign the array notation components to variable so that they can satisfy
627 the execute-once rule. */
628 for (ii
= 0; ii
< lhs_list_size
; ii
++)
630 tree anode
= (*lhs_list
)[ii
];
631 make_triplet_val_inv (&ARRAY_NOTATION_START (anode
));
632 make_triplet_val_inv (&ARRAY_NOTATION_LENGTH (anode
));
633 make_triplet_val_inv (&ARRAY_NOTATION_STRIDE (anode
));
635 for (ii
= 0; ii
< rhs_list_size
; ii
++)
636 if ((*rhs_list
)[ii
] && TREE_CODE ((*rhs_list
)[ii
]) == ARRAY_NOTATION_REF
)
638 tree aa
= (*rhs_list
)[ii
];
639 make_triplet_val_inv (&ARRAY_NOTATION_START (aa
));
640 make_triplet_val_inv (&ARRAY_NOTATION_LENGTH (aa
));
641 make_triplet_val_inv (&ARRAY_NOTATION_STRIDE (aa
));
643 lhs_an_loop_info
.safe_grow_cleared (lhs_rank
);
646 rhs_an_loop_info
.safe_grow_cleared (rhs_rank
);
648 cond_expr
.safe_grow_cleared (MAX (lhs_rank
, rhs_rank
));
649 cilkplus_extract_an_triplets (lhs_list
, lhs_list_size
, lhs_rank
,
652 cilkplus_extract_an_triplets (rhs_list
, rhs_list_size
, rhs_rank
,
654 if (length_mismatch_in_expr_p (EXPR_LOCATION (lhs
), lhs_an_info
)
655 || (rhs_list
&& length_mismatch_in_expr_p (EXPR_LOCATION (rhs
),
658 pop_stmt_list (an_init
);
659 return error_mark_node
;
661 tree rhs_len
= ((rhs_list_size
> 0 && rhs_rank
> 0) ?
662 rhs_an_info
[0][0].length
: NULL_TREE
);
663 tree lhs_len
= ((lhs_list_size
> 0 && lhs_rank
> 0) ?
664 lhs_an_info
[0][0].length
: NULL_TREE
);
665 if (lhs_list_size
> 0 && rhs_list_size
> 0 && lhs_rank
> 0 && rhs_rank
> 0
666 && TREE_CODE (lhs_len
) == INTEGER_CST
&& rhs_len
667 && TREE_CODE (rhs_len
) == INTEGER_CST
668 && !tree_int_cst_equal (rhs_len
, lhs_len
))
670 error_at (location
, "length mismatch between LHS and RHS");
671 pop_stmt_list (an_init
);
672 return error_mark_node
;
674 for (ii
= 0; ii
< lhs_rank
; ii
++)
676 tree typ
= ptrdiff_type_node
;
677 lhs_an_loop_info
[ii
].var
= create_temporary_var (typ
);
678 add_decl_expr (lhs_an_loop_info
[ii
].var
);
679 lhs_an_loop_info
[ii
].ind_init
= build_x_modify_expr
680 (location
, lhs_an_loop_info
[ii
].var
, INIT_EXPR
, build_zero_cst (typ
),
684 if (rhs_list_size
> 0)
686 rhs_array_operand
= fix_sec_implicit_args (location
, rhs_list
,
687 lhs_an_loop_info
, lhs_rank
,
689 if (!rhs_array_operand
)
690 return error_mark_node
;
692 replace_array_notations (&rhs
, true, rhs_list
, rhs_array_operand
);
695 extract_array_notation_exprs (rhs
, true, &rhs_list
);
696 rhs_list_size
= vec_safe_length (rhs_list
);
698 for (ii
= 0; ii
< rhs_rank
; ii
++)
700 tree typ
= ptrdiff_type_node
;
701 rhs_an_loop_info
[ii
].var
= create_temporary_var (typ
);
702 add_decl_expr (rhs_an_loop_info
[ii
].var
);
703 rhs_an_loop_info
[ii
].ind_init
= build_x_modify_expr
704 (location
, rhs_an_loop_info
[ii
].var
, INIT_EXPR
, build_zero_cst (typ
),
711 create_array_refs (location
, lhs_an_info
, lhs_an_loop_info
,
712 lhs_list_size
, lhs_rank
);
713 replace_array_notations (&lhs
, true, lhs_list
, lhs_array_operand
);
716 if (rhs_array_operand
)
717 vec_safe_truncate (rhs_array_operand
, 0);
720 rhs_array_operand
= create_array_refs (location
, rhs_an_info
,
721 rhs_an_loop_info
, rhs_list_size
,
723 /* Replace all the array refs created by the above function because this
724 variable is blown away by the fix_sec_implicit_args function below. */
725 replace_array_notations (&rhs
, true, rhs_list
, rhs_array_operand
);
726 vec_safe_truncate (rhs_array_operand
, 0);
727 rhs_array_operand
= fix_sec_implicit_args (location
, rhs_list
,
728 rhs_an_loop_info
, rhs_rank
,
730 if (!rhs_array_operand
)
731 return error_mark_node
;
732 replace_array_notations (&rhs
, true, rhs_list
, rhs_array_operand
);
735 array_expr_rhs
= rhs
;
736 array_expr_lhs
= lhs
;
738 array_expr
= build_x_modify_expr (location
, array_expr_lhs
, modifycode
,
739 array_expr_rhs
, complain
);
740 create_cmp_incr (location
, &lhs_an_loop_info
, lhs_rank
, lhs_an_info
,
743 create_cmp_incr (location
, &rhs_an_loop_info
, rhs_rank
, rhs_an_info
,
745 for (ii
= 0; ii
< MAX (rhs_rank
, lhs_rank
); ii
++)
746 if (ii
< lhs_rank
&& ii
< rhs_rank
)
747 cond_expr
[ii
] = build_x_binary_op
748 (location
, TRUTH_ANDIF_EXPR
, lhs_an_loop_info
[ii
].cmp
,
749 TREE_CODE (lhs_an_loop_info
[ii
].cmp
), rhs_an_loop_info
[ii
].cmp
,
750 TREE_CODE (rhs_an_loop_info
[ii
].cmp
), NULL
, complain
);
751 else if (ii
< lhs_rank
&& ii
>= rhs_rank
)
752 cond_expr
[ii
] = lhs_an_loop_info
[ii
].cmp
;
754 /* No need to compare ii < rhs_rank && ii >= lhs_rank because in a valid
755 Array notation expression, rank of RHS cannot be greater than LHS. */
758 an_init
= pop_stmt_list (an_init
);
759 append_to_statement_list (an_init
, &loop_with_init
);
761 for (ii
= 0; ii
< MAX (lhs_rank
, rhs_rank
); ii
++)
763 tree incr_list
= alloc_stmt_list ();
764 tree init_list
= alloc_stmt_list ();
765 tree new_loop
= push_stmt_list ();
769 append_to_statement_list (lhs_an_loop_info
[ii
].ind_init
, &init_list
);
770 append_to_statement_list (lhs_an_loop_info
[ii
].incr
, &incr_list
);
774 append_to_statement_list (rhs_an_loop_info
[ii
].ind_init
, &init_list
);
775 append_to_statement_list (rhs_an_loop_info
[ii
].incr
, &incr_list
);
777 create_an_loop (init_list
, cond_expr
[ii
], incr_list
, body
);
778 body
= pop_stmt_list (new_loop
);
780 append_to_statement_list (body
, &loop_with_init
);
782 lhs_an_info
.release ();
783 lhs_an_loop_info
.release ();
786 rhs_an_info
.release ();
787 rhs_an_loop_info
.release ();
789 cond_expr
.release ();
791 return loop_with_init
;
794 /* Helper function for expand_conditonal_array_notations. Encloses the
795 conditional statement passed in ORIG_STMT with a loop around it and
796 replaces the condition in STMT with a ARRAY_REF tree-node to the array.
797 The condition must have a ARRAY_NOTATION_REF tree. */
800 cp_expand_cond_array_notations (tree orig_stmt
)
802 vec
<tree
, va_gc
> *array_list
= NULL
, *array_operand
= NULL
;
803 size_t list_size
= 0;
804 size_t rank
= 0, ii
= 0;
805 tree an_init
, body
, stmt
= NULL_TREE
;
806 tree builtin_loop
, new_var
= NULL_TREE
;
807 tree loop_with_init
= alloc_stmt_list ();
808 location_t location
= UNKNOWN_LOCATION
;
809 vec
<vec
<an_parts
> > an_info
= vNULL
;
810 vec
<an_loop_parts
> an_loop_info
= vNULL
;
812 if (TREE_CODE (orig_stmt
) == COND_EXPR
)
814 size_t cond_rank
= 0, yes_rank
= 0, no_rank
= 0;
815 tree yes_expr
= COND_EXPR_THEN (orig_stmt
);
816 tree no_expr
= COND_EXPR_ELSE (orig_stmt
);
817 tree cond
= COND_EXPR_COND (orig_stmt
);
818 if (!find_rank (EXPR_LOCATION (cond
), cond
, cond
, true, &cond_rank
)
819 || !find_rank (EXPR_LOCATION (yes_expr
), yes_expr
, yes_expr
, true,
821 || find_rank (EXPR_LOCATION (no_expr
), no_expr
, no_expr
, true,
823 return error_mark_node
;
824 /* If the condition has a zero rank, then handle array notations in body
828 if (cond_rank
!= yes_rank
&& yes_rank
!= 0)
830 error_at (EXPR_LOCATION (yes_expr
), "rank mismatch with controlling"
831 " expression of parent if-statement");
832 return error_mark_node
;
834 else if (cond_rank
!= no_rank
&& no_rank
!= 0)
836 error_at (EXPR_LOCATION (no_expr
), "rank mismatch with controlling "
837 "expression of parent if-statement");
838 return error_mark_node
;
841 else if (TREE_CODE (orig_stmt
) == IF_STMT
)
843 size_t cond_rank
= 0, yes_rank
= 0, no_rank
= 0;
844 tree yes_expr
= THEN_CLAUSE (orig_stmt
);
845 tree no_expr
= ELSE_CLAUSE (orig_stmt
);
846 tree cond
= IF_COND (orig_stmt
);
847 if (!find_rank (EXPR_LOCATION (cond
), cond
, cond
, true, &cond_rank
)
849 && !find_rank (EXPR_LOCATION (yes_expr
), yes_expr
, yes_expr
, true,
852 && !find_rank (EXPR_LOCATION (no_expr
), no_expr
, no_expr
, true,
854 return error_mark_node
;
856 /* Same reasoning as for COND_EXPR. */
859 else if (cond_rank
!= yes_rank
&& yes_rank
!= 0)
861 error_at (EXPR_LOCATION (yes_expr
), "rank mismatch with controlling"
862 " expression of parent if-statement");
863 return error_mark_node
;
865 else if (cond_rank
!= no_rank
&& no_rank
!= 0)
867 error_at (EXPR_LOCATION (no_expr
), "rank mismatch with controlling "
868 "expression of parent if-statement");
869 return error_mark_node
;
872 else if (truth_value_p (TREE_CODE (orig_stmt
)))
874 size_t left_rank
= 0, right_rank
= 0;
875 tree left_expr
= TREE_OPERAND (orig_stmt
, 0);
876 tree right_expr
= TREE_OPERAND (orig_stmt
, 1);
877 if (!find_rank (EXPR_LOCATION (left_expr
), left_expr
, left_expr
, true,
879 || !find_rank (EXPR_LOCATION (right_expr
), right_expr
, right_expr
,
881 return error_mark_node
;
882 if (right_rank
== 0 && left_rank
== 0)
886 if (!find_rank (EXPR_LOCATION (orig_stmt
), orig_stmt
, orig_stmt
, true,
888 return error_mark_node
;
892 extract_array_notation_exprs (orig_stmt
, false, &array_list
);
893 stmt
= alloc_stmt_list ();
894 for (ii
= 0; ii
< vec_safe_length (array_list
); ii
++)
896 tree array_node
= (*array_list
)[ii
];
897 if (TREE_CODE (array_node
) == CALL_EXPR
898 || TREE_CODE (array_node
) == AGGR_INIT_EXPR
)
900 builtin_loop
= expand_sec_reduce_builtin (array_node
, &new_var
);
901 if (builtin_loop
== error_mark_node
)
902 finish_expr_stmt (error_mark_node
);
905 vec
<tree
, va_gc
> *sub_list
= NULL
, *new_var_list
= NULL
;
906 vec_safe_push (sub_list
, array_node
);
907 vec_safe_push (new_var_list
, new_var
);
908 replace_array_notations (&orig_stmt
, false, sub_list
,
910 append_to_statement_list (builtin_loop
, &stmt
);
914 append_to_statement_list (orig_stmt
, &stmt
);
917 if (!find_rank (EXPR_LOCATION (stmt
), stmt
, stmt
, true, &rank
))
918 return error_mark_node
;
922 extract_array_notation_exprs (stmt
, true, &array_list
);
923 list_size
= vec_safe_length (array_list
);
927 location
= EXPR_LOCATION (orig_stmt
);
928 list_size
= vec_safe_length (array_list
);
929 an_loop_info
.safe_grow_cleared (rank
);
931 an_init
= push_stmt_list ();
933 /* Assign the array notation components to variable so that they can
934 satisfy the exec-once rule. */
935 for (ii
= 0; ii
< list_size
; ii
++)
937 tree anode
= (*array_list
)[ii
];
938 make_triplet_val_inv (&ARRAY_NOTATION_START (anode
));
939 make_triplet_val_inv (&ARRAY_NOTATION_LENGTH (anode
));
940 make_triplet_val_inv (&ARRAY_NOTATION_STRIDE (anode
));
942 cilkplus_extract_an_triplets (array_list
, list_size
, rank
, &an_info
);
944 for (ii
= 0; ii
< rank
; ii
++)
946 tree typ
= ptrdiff_type_node
;
947 an_loop_info
[ii
].var
= create_temporary_var (typ
);
948 add_decl_expr (an_loop_info
[ii
].var
);
949 an_loop_info
[ii
].ind_init
=
950 build_x_modify_expr (location
, an_loop_info
[ii
].var
, INIT_EXPR
,
951 build_zero_cst (typ
), tf_warning_or_error
);
953 array_operand
= create_array_refs (location
, an_info
, an_loop_info
,
955 replace_array_notations (&stmt
, true, array_list
, array_operand
);
956 create_cmp_incr (location
, &an_loop_info
, rank
, an_info
, tf_warning_or_error
);
958 an_init
= pop_stmt_list (an_init
);
959 append_to_statement_list (an_init
, &loop_with_init
);
962 for (ii
= 0; ii
< rank
; ii
++)
964 tree new_loop
= push_stmt_list ();
965 create_an_loop (an_loop_info
[ii
].ind_init
, an_loop_info
[ii
].cmp
,
966 an_loop_info
[ii
].incr
, body
);
967 body
= pop_stmt_list (new_loop
);
969 append_to_statement_list (body
, &loop_with_init
);
972 an_loop_info
.release ();
974 return loop_with_init
;
977 /* Transforms array notations inside unary expression ORIG_STMT with an
978 appropriate loop and ARRAY_REF (and returns all this as a super-tree called
982 expand_unary_array_notation_exprs (tree orig_stmt
)
984 vec
<tree
, va_gc
> *array_list
= NULL
, *array_operand
= NULL
;
985 size_t list_size
= 0, rank
= 0, ii
= 0;
987 tree builtin_loop
, stmt
= NULL_TREE
, new_var
= NULL_TREE
;
988 location_t location
= EXPR_LOCATION (orig_stmt
);
989 tree an_init
, loop_with_init
= alloc_stmt_list ();
990 vec
<vec
<an_parts
> > an_info
= vNULL
;
991 vec
<an_loop_parts
> an_loop_info
= vNULL
;
993 if (!find_rank (location
, orig_stmt
, orig_stmt
, true, &rank
))
994 return error_mark_node
;
998 extract_array_notation_exprs (orig_stmt
, false, &array_list
);
999 list_size
= vec_safe_length (array_list
);
1000 location
= EXPR_LOCATION (orig_stmt
);
1002 for (ii
= 0; ii
< list_size
; ii
++)
1003 if (TREE_CODE ((*array_list
)[ii
]) == CALL_EXPR
1004 || TREE_CODE ((*array_list
)[ii
]) == AGGR_INIT_EXPR
)
1006 tree list_node
= (*array_list
)[ii
];
1007 builtin_loop
= expand_sec_reduce_builtin (list_node
, &new_var
);
1008 if (builtin_loop
== error_mark_node
)
1009 return error_mark_node
;
1010 else if (builtin_loop
)
1012 vec
<tree
, va_gc
> *sub_list
= NULL
, *new_var_list
= NULL
;
1013 stmt
= alloc_stmt_list ();
1014 append_to_statement_list (builtin_loop
, &stmt
);
1015 vec_safe_push (sub_list
, list_node
);
1016 vec_safe_push (new_var_list
, new_var
);
1017 replace_array_notations (&orig_stmt
, false, sub_list
, new_var_list
);
1020 if (stmt
!= NULL_TREE
)
1021 append_to_statement_list (finish_expr_stmt (orig_stmt
), &stmt
);
1027 extract_array_notation_exprs (stmt
, true, &array_list
);
1028 list_size
= vec_safe_length (array_list
);
1030 if (!find_rank (EXPR_LOCATION (stmt
), stmt
, stmt
, true, &rank
))
1031 return error_mark_node
;
1032 if (rank
== 0 || list_size
== 0)
1034 an_loop_info
.safe_grow_cleared (rank
);
1035 an_init
= push_stmt_list ();
1036 /* Assign the array notation components to variable so that they can satisfy
1037 the exec-once rule. */
1038 for (ii
= 0; ii
< list_size
; ii
++)
1040 tree array_node
= (*array_list
)[ii
];
1041 make_triplet_val_inv (&ARRAY_NOTATION_START (array_node
));
1042 make_triplet_val_inv (&ARRAY_NOTATION_LENGTH (array_node
));
1043 make_triplet_val_inv (&ARRAY_NOTATION_STRIDE (array_node
));
1045 cilkplus_extract_an_triplets (array_list
, list_size
, rank
, &an_info
);
1047 for (ii
= 0; ii
< rank
; ii
++)
1049 tree typ
= ptrdiff_type_node
;
1050 an_loop_info
[ii
].var
= create_temporary_var (typ
);
1051 add_decl_expr (an_loop_info
[ii
].var
);
1052 an_loop_info
[ii
].ind_init
= build_x_modify_expr
1053 (location
, an_loop_info
[ii
].var
, INIT_EXPR
, build_zero_cst (typ
),
1054 tf_warning_or_error
);
1056 array_operand
= create_array_refs (location
, an_info
, an_loop_info
,
1058 replace_array_notations (&stmt
, true, array_list
, array_operand
);
1059 create_cmp_incr (location
, &an_loop_info
, rank
, an_info
, tf_warning_or_error
);
1061 an_init
= pop_stmt_list (an_init
);
1062 append_to_statement_list (an_init
, &loop_with_init
);
1065 for (ii
= 0; ii
< rank
; ii
++)
1067 tree new_loop
= push_stmt_list ();
1068 create_an_loop (an_loop_info
[ii
].ind_init
, an_loop_info
[ii
].cmp
,
1069 an_loop_info
[ii
].incr
, body
);
1070 body
= pop_stmt_list (new_loop
);
1072 append_to_statement_list (body
, &loop_with_init
);
1075 an_loop_info
.release ();
1077 return loop_with_init
;
1080 /* Expands the array notation's builtin reduction function in EXPR
1081 (of type RETURN_EXPR) and returns a STATEMENT_LIST that contains a loop
1082 with the builtin function expansion and a return statement at the end. */
1085 expand_return_expr (tree expr
)
1087 tree new_mod_list
, new_var
, new_mod
, retval_expr
;
1089 location_t loc
= EXPR_LOCATION (expr
);
1090 if (TREE_CODE (expr
) != RETURN_EXPR
)
1093 if (!find_rank (loc
, expr
, expr
, false, &rank
))
1094 return error_mark_node
;
1096 /* If the return expression contains array notations, then flag it as
1100 error_at (loc
, "array notation expression cannot be used as a return "
1102 return error_mark_node
;
1105 new_mod_list
= push_stmt_list ();
1106 retval_expr
= TREE_OPERAND (expr
, 0);
1107 new_var
= create_temporary_var (TREE_TYPE (retval_expr
));
1108 add_decl_expr (new_var
);
1109 new_mod
= expand_an_in_modify_expr (loc
, new_var
, NOP_EXPR
,
1110 TREE_OPERAND (retval_expr
, 1),
1111 tf_warning_or_error
);
1112 TREE_OPERAND (retval_expr
, 1) = new_var
;
1113 TREE_OPERAND (expr
, 0) = retval_expr
;
1116 new_mod_list
= pop_stmt_list (new_mod_list
);
1117 return new_mod_list
;
1120 /* Expands ARRAY_NOTATION_REF and builtin functions in a compound statement,
1121 STMT. Returns the STMT with expanded array notations. */
1124 expand_array_notation_exprs (tree t
)
1126 enum tree_code code
;
1128 location_t loc
= UNKNOWN_LOCATION
;
1133 loc
= EXPR_LOCATION (t
);
1135 code
= TREE_CODE (t
);
1136 is_expr
= IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code
));
1140 case IDENTIFIER_NODE
:
1147 case PLACEHOLDER_EXPR
:
1156 case NON_LVALUE_EXPR
:
1166 if (contains_array_notation_expr (t
))
1167 t
= expand_an_in_modify_expr (loc
, TREE_OPERAND (t
, 0), NOP_EXPR
,
1168 TREE_OPERAND (t
, 1),
1169 tf_warning_or_error
);
1172 if (contains_array_notation_expr (t
) && !processing_template_decl
)
1173 t
= expand_an_in_modify_expr
1174 (loc
, TREE_OPERAND (t
, 0), TREE_CODE (TREE_OPERAND (t
, 1)),
1175 TREE_OPERAND (t
, 2), tf_warning_or_error
);
1181 BIND_EXPR_BODY (t
) =
1182 expand_array_notation_exprs (BIND_EXPR_BODY (t
));
1186 if (contains_array_notation_expr (t
))
1188 tree x
= DECL_EXPR_DECL (t
);
1189 if (DECL_INITIAL (x
))
1191 location_t loc
= DECL_SOURCE_LOCATION (x
);
1193 tree rhs
= DECL_INITIAL (x
);
1194 DECL_INITIAL (x
) = NULL
;
1195 tree new_modify_expr
= build_modify_expr (loc
, lhs
,
1200 t
= expand_array_notation_exprs (new_modify_expr
);
1204 case STATEMENT_LIST
:
1206 tree_stmt_iterator i
;
1207 for (i
= tsi_start (t
); !tsi_end_p (i
); tsi_next (&i
))
1209 expand_array_notation_exprs (*tsi_stmt_ptr (i
));
1235 if (contains_array_notation_expr (t
))
1236 t
= expand_return_expr (t
);
1238 case PREDECREMENT_EXPR
:
1239 case PREINCREMENT_EXPR
:
1240 case POSTDECREMENT_EXPR
:
1241 case POSTINCREMENT_EXPR
:
1242 case AGGR_INIT_EXPR
:
1244 t
= expand_unary_array_notation_exprs (t
);
1247 case CLEANUP_POINT_EXPR
:
1249 TREE_OPERAND (t
, 0) = expand_array_notation_exprs (TREE_OPERAND (t
, 0));
1250 /* It is not necessary to wrap error_mark_node in EXPR_STMT. */
1251 if (TREE_OPERAND (t
, 0) == error_mark_node
)
1252 return TREE_OPERAND (t
, 0);
1254 case TRUTH_ANDIF_EXPR
:
1255 case TRUTH_ORIF_EXPR
:
1256 case TRUTH_AND_EXPR
:
1258 case TRUTH_XOR_EXPR
:
1259 case TRUTH_NOT_EXPR
:
1261 t
= cp_expand_cond_array_notations (t
);
1262 if (TREE_CODE (t
) == COND_EXPR
)
1264 COND_EXPR_THEN (t
) =
1265 expand_array_notation_exprs (COND_EXPR_THEN (t
));
1266 COND_EXPR_ELSE (t
) =
1267 expand_array_notation_exprs (COND_EXPR_ELSE (t
));
1271 if (contains_array_notation_expr (FOR_COND (t
)))
1273 error_at (EXPR_LOCATION (FOR_COND (t
)),
1274 "array notation cannot be used in a condition for "
1276 return error_mark_node
;
1278 /* FIXME: Add a check for CILK_FOR_STMT here when we add Cilk tasking
1280 if (TREE_CODE (t
) == FOR_STMT
)
1282 FOR_BODY (t
) = expand_array_notation_exprs (FOR_BODY (t
));
1283 FOR_EXPR (t
) = expand_array_notation_exprs (FOR_EXPR (t
));
1286 t
= expand_array_notation_exprs (t
);
1289 t
= cp_expand_cond_array_notations (t
);
1290 /* If the above function added some extra instructions above the original
1291 if statement, then we can't assume it is still IF_STMT so we have to
1293 if (TREE_CODE (t
) == IF_STMT
)
1295 if (THEN_CLAUSE (t
))
1296 THEN_CLAUSE (t
) = expand_array_notation_exprs (THEN_CLAUSE (t
));
1297 if (ELSE_CLAUSE (t
))
1298 ELSE_CLAUSE (t
) = expand_array_notation_exprs (ELSE_CLAUSE (t
));
1301 t
= expand_array_notation_exprs (t
);
1304 if (contains_array_notation_expr (SWITCH_STMT_COND (t
)))
1306 error_at (EXPR_LOCATION (SWITCH_STMT_COND (t
)),
1307 "array notation cannot be used as a condition for "
1308 "switch statement");
1309 return error_mark_node
;
1311 if (SWITCH_STMT_BODY (t
))
1312 SWITCH_STMT_BODY (t
) =
1313 expand_array_notation_exprs (SWITCH_STMT_BODY (t
));
1316 if (contains_array_notation_expr (WHILE_COND (t
)))
1318 if (EXPR_LOCATION (WHILE_COND (t
)) != UNKNOWN_LOCATION
)
1319 loc
= EXPR_LOCATION (WHILE_COND (t
));
1320 error_at (loc
, "array notation cannot be used as a condition for "
1322 return error_mark_node
;
1325 WHILE_BODY (t
) = expand_array_notation_exprs (WHILE_BODY (t
));
1328 if (contains_array_notation_expr (DO_COND (t
)))
1330 error_at (EXPR_LOCATION (DO_COND (t
)),
1331 "array notation cannot be used as a condition for a "
1332 "do-while statement");
1333 return error_mark_node
;
1336 DO_BODY (t
) = expand_array_notation_exprs (DO_BODY (t
));
1343 /* Walk over all the sub-trees of this operand. */
1344 len
= TREE_CODE_LENGTH (code
);
1346 /* Go through the subtrees. We need to do this in forward order so
1347 that the scope of a FOR_EXPR is handled properly. */
1348 for (i
= 0; i
< len
; ++i
)
1349 TREE_OPERAND (t
, i
) =
1350 expand_array_notation_exprs (TREE_OPERAND (t
, i
));
1357 /* Given the base of an array (ARRAY), the START (start_index), the number of
1358 elements to be accessed (LENGTH) and the STRIDE, construct an
1359 ARRAY_NOTATION_REF tree of type TYPE and return it. Restrictions on START,
1360 LENGTH and STRIDE are the same as that of index field passed into ARRAY_REF.
1361 The only additional restriction is that, unlike index in ARRAY_REF, stride,
1362 length and start_index cannot contain array notations. */
1365 build_array_notation_ref (location_t loc
, tree array
, tree start
, tree length
,
1366 tree stride
, tree type
)
1368 tree array_ntn_expr
= NULL_TREE
;
1370 /* If we enter the then-case of the if-statement below, we have hit a case
1371 like this: ARRAY [:]. */
1372 if (!start
&& !length
)
1374 if (TREE_CODE (type
) != ARRAY_TYPE
)
1376 error_at (loc
, "start-index and length fields necessary for "
1377 "using array notation in pointers or records");
1378 return error_mark_node
;
1380 tree domain
= TYPE_DOMAIN (type
);
1383 error_at (loc
, "start-index and length fields necessary for "
1384 "using array notation with array of unknown bound");
1385 return error_mark_node
;
1387 start
= cp_fold_convert (ptrdiff_type_node
, TYPE_MINVAL (domain
));
1388 length
= size_binop (PLUS_EXPR
, TYPE_MAXVAL (domain
), size_one_node
);
1389 length
= cp_fold_convert (ptrdiff_type_node
, length
);
1393 stride
= build_one_cst (ptrdiff_type_node
);
1395 /* When dealing with templates, triplet type-checking will be done in pt.c
1396 after type substitution. */
1397 if (processing_template_decl
1398 && (type_dependent_expression_p (array
)
1399 || type_dependent_expression_p (length
)
1400 || type_dependent_expression_p (start
)
1401 || type_dependent_expression_p (stride
)))
1402 array_ntn_expr
= build_min_nt_loc (loc
, ARRAY_NOTATION_REF
, array
, start
,
1403 length
, stride
, NULL_TREE
);
1406 if (!cilkplus_an_triplet_types_ok_p (loc
, start
, length
, stride
, type
))
1407 return error_mark_node
;
1408 array_ntn_expr
= build4 (ARRAY_NOTATION_REF
, NULL_TREE
, array
, start
,
1411 if (TREE_CODE (type
) == ARRAY_TYPE
|| TREE_CODE (type
) == POINTER_TYPE
)
1412 TREE_TYPE (array_ntn_expr
) = TREE_TYPE (type
);
1415 error_at (loc
, "base of array section must be pointer or array type");
1416 return error_mark_node
;
1419 SET_EXPR_LOCATION (array_ntn_expr
, loc
);
1420 return array_ntn_expr
;
1423 /* Returns false if any of the Array notation triplet values: START_INDEX,
1424 LENGTH and STRIDE, are not of integral type and have a rank greater than
1428 cilkplus_an_triplet_types_ok_p (location_t loc
, tree start_index
, tree length
,
1429 tree stride
, tree type
)
1431 size_t stride_rank
= 0, length_rank
= 0, start_rank
= 0;
1432 if (!TREE_TYPE (start_index
) || !INTEGRAL_TYPE_P (TREE_TYPE (start_index
)))
1434 error_at (loc
, "start-index of array notation triplet is not an integer");
1437 if (!TREE_TYPE (length
) || !INTEGRAL_TYPE_P (TREE_TYPE (length
)))
1439 error_at (loc
, "length of array notation triplet is not an integer");
1442 if (!TREE_TYPE (stride
) || !INTEGRAL_TYPE_P (TREE_TYPE (stride
)))
1444 error_at (loc
, "stride of array notation triplet is not an integer");
1447 if (TREE_CODE (type
) == FUNCTION_TYPE
)
1449 error_at (loc
, "array notation cannot be used with function type");
1452 if (!find_rank (loc
, start_index
, start_index
, false, &start_rank
)
1453 || !find_rank (loc
, length
, length
, false, &length_rank
)
1454 || !find_rank (loc
, stride
, stride
, false, &stride_rank
))
1457 if (start_rank
!= 0)
1459 error_at (loc
, "rank of an array notation triplet%'s start-index is not "
1463 if (length_rank
!= 0)
1465 error_at (loc
, "rank of an array notation triplet%'s length is not zero");
1468 if (stride_rank
!= 0)
1470 error_at (loc
, "rank of array notation triplet%'s stride is not zero");