cp-array-notation.c 49 KB

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