graphite-interchange.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675
  1. /* Interchange heuristics and transform for loop interchange on
  2. polyhedral representation.
  3. Copyright (C) 2009-2015 Free Software Foundation, Inc.
  4. Contributed by Sebastian Pop <sebastian.pop@amd.com> and
  5. Harsha Jagasia <harsha.jagasia@amd.com>.
  6. This file is part of GCC.
  7. GCC is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 3, or (at your option)
  10. any later version.
  11. GCC is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with GCC; see the file COPYING3. If not see
  17. <http://www.gnu.org/licenses/>. */
  18. #include "config.h"
  19. #ifdef HAVE_isl
  20. #include <isl/aff.h>
  21. #include <isl/set.h>
  22. #include <isl/map.h>
  23. #include <isl/union_map.h>
  24. #include <isl/ilp.h>
  25. #include <isl/val.h>
  26. /* Since ISL-0.13, the extern is in val_gmp.h. */
  27. #if !defined(HAVE_ISL_SCHED_CONSTRAINTS_COMPUTE_SCHEDULE) && defined(__cplusplus)
  28. extern "C" {
  29. #endif
  30. #include <isl/val_gmp.h>
  31. #if !defined(HAVE_ISL_SCHED_CONSTRAINTS_COMPUTE_SCHEDULE) && defined(__cplusplus)
  32. }
  33. #endif
  34. #endif
  35. #include "system.h"
  36. #include "coretypes.h"
  37. #include "hash-set.h"
  38. #include "machmode.h"
  39. #include "vec.h"
  40. #include "double-int.h"
  41. #include "input.h"
  42. #include "alias.h"
  43. #include "symtab.h"
  44. #include "options.h"
  45. #include "wide-int.h"
  46. #include "inchash.h"
  47. #include "tree.h"
  48. #include "fold-const.h"
  49. #include "predict.h"
  50. #include "tm.h"
  51. #include "hard-reg-set.h"
  52. #include "input.h"
  53. #include "function.h"
  54. #include "dominance.h"
  55. #include "cfg.h"
  56. #include "basic-block.h"
  57. #include "tree-ssa-alias.h"
  58. #include "internal-fn.h"
  59. #include "gimple-expr.h"
  60. #include "is-a.h"
  61. #include "gimple.h"
  62. #include "gimple-iterator.h"
  63. #include "tree-ssa-loop.h"
  64. #include "dumpfile.h"
  65. #include "cfgloop.h"
  66. #include "tree-chrec.h"
  67. #include "tree-data-ref.h"
  68. #include "tree-scalar-evolution.h"
  69. #include "sese.h"
  70. #ifdef HAVE_isl
  71. #include "graphite-poly.h"
  72. /* XXX isl rewrite following comment */
  73. /* Builds a linear expression, of dimension DIM, representing PDR's
  74. memory access:
  75. L = r_{n}*r_{n-1}*...*r_{1}*s_{0} + ... + r_{n}*s_{n-1} + s_{n}.
  76. For an array A[10][20] with two subscript locations s0 and s1, the
  77. linear memory access is 20 * s0 + s1: a stride of 1 in subscript s0
  78. corresponds to a memory stride of 20.
  79. OFFSET is a number of dimensions to prepend before the
  80. subscript dimensions: s_0, s_1, ..., s_n.
  81. Thus, the final linear expression has the following format:
  82. 0 .. 0_{offset} | 0 .. 0_{nit} | 0 .. 0_{gd} | 0 | c_0 c_1 ... c_n
  83. where the expression itself is:
  84. c_0 * s_0 + c_1 * s_1 + ... c_n * s_n. */
  85. static isl_constraint *
  86. build_linearized_memory_access (isl_map *map, poly_dr_p pdr)
  87. {
  88. isl_constraint *res;
  89. isl_local_space *ls = isl_local_space_from_space (isl_map_get_space (map));
  90. unsigned offset, nsubs;
  91. int i;
  92. isl_ctx *ctx;
  93. isl_val *size, *subsize, *size1;
  94. res = isl_equality_alloc (ls);
  95. ctx = isl_local_space_get_ctx (ls);
  96. size = isl_val_int_from_ui (ctx, 1);
  97. nsubs = isl_set_dim (pdr->extent, isl_dim_set);
  98. /* -1 for the already included L dimension. */
  99. offset = isl_map_dim (map, isl_dim_out) - 1 - nsubs;
  100. res = isl_constraint_set_coefficient_si (res, isl_dim_out, offset + nsubs, -1);
  101. /* Go through all subscripts from last to first. First dimension
  102. is the alias set, ignore it. */
  103. for (i = nsubs - 1; i >= 1; i--)
  104. {
  105. isl_space *dc;
  106. isl_aff *aff;
  107. size1 = isl_val_copy (size);
  108. res = isl_constraint_set_coefficient_val (res, isl_dim_out, offset + i, size);
  109. dc = isl_set_get_space (pdr->extent);
  110. aff = isl_aff_zero_on_domain (isl_local_space_from_space (dc));
  111. aff = isl_aff_set_coefficient_si (aff, isl_dim_in, i, 1);
  112. subsize = isl_set_max_val (pdr->extent, aff);
  113. isl_aff_free (aff);
  114. size = isl_val_mul (size1, subsize);
  115. }
  116. isl_val_free (size);
  117. return res;
  118. }
  119. /* Set STRIDE to the stride of PDR in memory by advancing by one in
  120. the loop at DEPTH. */
  121. static void
  122. pdr_stride_in_loop (mpz_t stride, graphite_dim_t depth, poly_dr_p pdr)
  123. {
  124. poly_bb_p pbb = PDR_PBB (pdr);
  125. isl_map *map;
  126. isl_set *set;
  127. isl_aff *aff;
  128. isl_space *dc;
  129. isl_constraint *lma, *c;
  130. isl_val *islstride;
  131. graphite_dim_t time_depth;
  132. unsigned offset, nt;
  133. unsigned i;
  134. /* XXX isl rewrite following comments. */
  135. /* Builds a partial difference equations and inserts them
  136. into pointset powerset polyhedron P. Polyhedron is assumed
  137. to have the format: T|I|T'|I'|G|S|S'|l1|l2.
  138. TIME_DEPTH is the time dimension w.r.t. which we are
  139. differentiating.
  140. OFFSET represents the number of dimensions between
  141. columns t_{time_depth} and t'_{time_depth}.
  142. DIM_SCTR is the number of scattering dimensions. It is
  143. essentially the dimensionality of the T vector.
  144. The following equations are inserted into the polyhedron P:
  145. | t_1 = t_1'
  146. | ...
  147. | t_{time_depth-1} = t'_{time_depth-1}
  148. | t_{time_depth} = t'_{time_depth} + 1
  149. | t_{time_depth+1} = t'_{time_depth + 1}
  150. | ...
  151. | t_{dim_sctr} = t'_{dim_sctr}. */
  152. /* Add the equality: t_{time_depth} = t'_{time_depth} + 1.
  153. This is the core part of this alogrithm, since this
  154. constraint asks for the memory access stride (difference)
  155. between two consecutive points in time dimensions. */
  156. /* Add equalities:
  157. | t1 = t1'
  158. | ...
  159. | t_{time_depth-1} = t'_{time_depth-1}
  160. | t_{time_depth+1} = t'_{time_depth+1}
  161. | ...
  162. | t_{dim_sctr} = t'_{dim_sctr}
  163. This means that all the time dimensions are equal except for
  164. time_depth, where the constraint is t_{depth} = t'_{depth} + 1
  165. step. More to this: we should be careful not to add equalities
  166. to the 'coupled' dimensions, which happens when the one dimension
  167. is stripmined dimension, and the other dimension corresponds
  168. to the point loop inside stripmined dimension. */
  169. /* pdr->accesses: [P1..nb_param,I1..nb_domain]->[a,S1..nb_subscript]
  170. ??? [P] not used for PDRs?
  171. pdr->extent: [a,S1..nb_subscript]
  172. pbb->domain: [P1..nb_param,I1..nb_domain]
  173. pbb->transformed: [P1..nb_param,I1..nb_domain]->[T1..Tnb_sctr]
  174. [T] includes local vars (currently unused)
  175. First we create [P,I] -> [T,a,S]. */
  176. map = isl_map_flat_range_product (isl_map_copy (pbb->transformed),
  177. isl_map_copy (pdr->accesses));
  178. /* Add a dimension for L: [P,I] -> [T,a,S,L].*/
  179. map = isl_map_add_dims (map, isl_dim_out, 1);
  180. /* Build a constraint for "lma[S] - L == 0", effectively calculating
  181. L in terms of subscripts. */
  182. lma = build_linearized_memory_access (map, pdr);
  183. /* And add it to the map, so we now have:
  184. [P,I] -> [T,a,S,L] : lma([S]) == L. */
  185. map = isl_map_add_constraint (map, lma);
  186. /* Then we create [P,I,P',I'] -> [T,a,S,L,T',a',S',L']. */
  187. map = isl_map_flat_product (map, isl_map_copy (map));
  188. /* Now add the equality T[time_depth] == T'[time_depth]+1. This will
  189. force L' to be the linear address at T[time_depth] + 1. */
  190. time_depth = psct_dynamic_dim (pbb, depth);
  191. /* Length of [a,S] plus [L] ... */
  192. offset = 1 + isl_map_dim (pdr->accesses, isl_dim_out);
  193. /* ... plus [T]. */
  194. offset += isl_map_dim (pbb->transformed, isl_dim_out);
  195. c = isl_equality_alloc (isl_local_space_from_space (isl_map_get_space (map)));
  196. c = isl_constraint_set_coefficient_si (c, isl_dim_out, time_depth, 1);
  197. c = isl_constraint_set_coefficient_si (c, isl_dim_out,
  198. offset + time_depth, -1);
  199. c = isl_constraint_set_constant_si (c, 1);
  200. map = isl_map_add_constraint (map, c);
  201. /* Now we equate most of the T/T' elements (making PITaSL nearly
  202. the same is (PITaSL)', except for one dimension, namely for 'depth'
  203. (an index into [I]), after translating to index into [T]. Take care
  204. to not produce an empty map, which indicates we wanted to equate
  205. two dimensions that are already coupled via the above time_depth
  206. dimension. Happens with strip mining where several scatter dimension
  207. are interdependend. */
  208. /* Length of [T]. */
  209. nt = pbb_nb_scattering_transform (pbb) + pbb_nb_local_vars (pbb);
  210. for (i = 0; i < nt; i++)
  211. if (i != time_depth)
  212. {
  213. isl_map *temp = isl_map_equate (isl_map_copy (map),
  214. isl_dim_out, i,
  215. isl_dim_out, offset + i);
  216. if (isl_map_is_empty (temp))
  217. isl_map_free (temp);
  218. else
  219. {
  220. isl_map_free (map);
  221. map = temp;
  222. }
  223. }
  224. /* Now maximize the expression L' - L. */
  225. set = isl_map_range (map);
  226. dc = isl_set_get_space (set);
  227. aff = isl_aff_zero_on_domain (isl_local_space_from_space (dc));
  228. aff = isl_aff_set_coefficient_si (aff, isl_dim_in, offset - 1, -1);
  229. aff = isl_aff_set_coefficient_si (aff, isl_dim_in, offset + offset - 1, 1);
  230. islstride = isl_set_max_val (set, aff);
  231. isl_val_get_num_gmp (islstride, stride);
  232. isl_val_free (islstride);
  233. isl_aff_free (aff);
  234. isl_set_free (set);
  235. if (dump_file && (dump_flags & TDF_DETAILS))
  236. {
  237. gmp_fprintf (dump_file, "\nStride in BB_%d, DR_%d, depth %d: %Zd ",
  238. pbb_index (pbb), PDR_ID (pdr), (int) depth, stride);
  239. }
  240. }
  241. /* Sets STRIDES to the sum of all the strides of the data references
  242. accessed in LOOP at DEPTH. */
  243. static void
  244. memory_strides_in_loop_1 (lst_p loop, graphite_dim_t depth, mpz_t strides)
  245. {
  246. int i, j;
  247. lst_p l;
  248. poly_dr_p pdr;
  249. mpz_t s, n;
  250. mpz_init (s);
  251. mpz_init (n);
  252. FOR_EACH_VEC_ELT (LST_SEQ (loop), j, l)
  253. if (LST_LOOP_P (l))
  254. memory_strides_in_loop_1 (l, depth, strides);
  255. else
  256. FOR_EACH_VEC_ELT (PBB_DRS (LST_PBB (l)), i, pdr)
  257. {
  258. pdr_stride_in_loop (s, depth, pdr);
  259. mpz_set_si (n, PDR_NB_REFS (pdr));
  260. mpz_mul (s, s, n);
  261. mpz_add (strides, strides, s);
  262. }
  263. mpz_clear (s);
  264. mpz_clear (n);
  265. }
  266. /* Sets STRIDES to the sum of all the strides of the data references
  267. accessed in LOOP at DEPTH. */
  268. static void
  269. memory_strides_in_loop (lst_p loop, graphite_dim_t depth, mpz_t strides)
  270. {
  271. if (mpz_cmp_si (loop->memory_strides, -1) == 0)
  272. {
  273. mpz_set_si (strides, 0);
  274. memory_strides_in_loop_1 (loop, depth, strides);
  275. }
  276. else
  277. mpz_set (strides, loop->memory_strides);
  278. }
  279. /* Return true when the interchange of loops LOOP1 and LOOP2 is
  280. profitable.
  281. Example:
  282. | int a[100][100];
  283. |
  284. | int
  285. | foo (int N)
  286. | {
  287. | int j;
  288. | int i;
  289. |
  290. | for (i = 0; i < N; i++)
  291. | for (j = 0; j < N; j++)
  292. | a[j][2 * i] += 1;
  293. |
  294. | return a[N][12];
  295. | }
  296. The data access A[j][i] is described like this:
  297. | i j N a s0 s1 1
  298. | 0 0 0 1 0 0 -5 = 0
  299. | 0 -1 0 0 1 0 0 = 0
  300. |-2 0 0 0 0 1 0 = 0
  301. | 0 0 0 0 1 0 0 >= 0
  302. | 0 0 0 0 0 1 0 >= 0
  303. | 0 0 0 0 -1 0 100 >= 0
  304. | 0 0 0 0 0 -1 100 >= 0
  305. The linearized memory access L to A[100][100] is:
  306. | i j N a s0 s1 1
  307. | 0 0 0 0 100 1 0
  308. TODO: the shown format is not valid as it does not show the fact
  309. that the iteration domain "i j" is transformed using the scattering.
  310. Next, to measure the impact of iterating once in loop "i", we build
  311. a maximization problem: first, we add to DR accesses the dimensions
  312. k, s2, s3, L1 = 100 * s0 + s1, L2, and D1: this is the polyhedron P1.
  313. L1 and L2 are the linearized memory access functions.
  314. | i j N a s0 s1 k s2 s3 L1 L2 D1 1
  315. | 0 0 0 1 0 0 0 0 0 0 0 0 -5 = 0 alias = 5
  316. | 0 -1 0 0 1 0 0 0 0 0 0 0 0 = 0 s0 = j
  317. |-2 0 0 0 0 1 0 0 0 0 0 0 0 = 0 s1 = 2 * i
  318. | 0 0 0 0 1 0 0 0 0 0 0 0 0 >= 0
  319. | 0 0 0 0 0 1 0 0 0 0 0 0 0 >= 0
  320. | 0 0 0 0 -1 0 0 0 0 0 0 0 100 >= 0
  321. | 0 0 0 0 0 -1 0 0 0 0 0 0 100 >= 0
  322. | 0 0 0 0 100 1 0 0 0 -1 0 0 0 = 0 L1 = 100 * s0 + s1
  323. Then, we generate the polyhedron P2 by interchanging the dimensions
  324. (s0, s2), (s1, s3), (L1, L2), (k, i)
  325. | i j N a s0 s1 k s2 s3 L1 L2 D1 1
  326. | 0 0 0 1 0 0 0 0 0 0 0 0 -5 = 0 alias = 5
  327. | 0 -1 0 0 0 0 0 1 0 0 0 0 0 = 0 s2 = j
  328. | 0 0 0 0 0 0 -2 0 1 0 0 0 0 = 0 s3 = 2 * k
  329. | 0 0 0 0 0 0 0 1 0 0 0 0 0 >= 0
  330. | 0 0 0 0 0 0 0 0 1 0 0 0 0 >= 0
  331. | 0 0 0 0 0 0 0 -1 0 0 0 0 100 >= 0
  332. | 0 0 0 0 0 0 0 0 -1 0 0 0 100 >= 0
  333. | 0 0 0 0 0 0 0 100 1 0 -1 0 0 = 0 L2 = 100 * s2 + s3
  334. then we add to P2 the equality k = i + 1:
  335. |-1 0 0 0 0 0 1 0 0 0 0 0 -1 = 0 k = i + 1
  336. and finally we maximize the expression "D1 = max (P1 inter P2, L2 - L1)".
  337. Similarly, to determine the impact of one iteration on loop "j", we
  338. interchange (k, j), we add "k = j + 1", and we compute D2 the
  339. maximal value of the difference.
  340. Finally, the profitability test is D1 < D2: if in the outer loop
  341. the strides are smaller than in the inner loop, then it is
  342. profitable to interchange the loops at DEPTH1 and DEPTH2. */
  343. static bool
  344. lst_interchange_profitable_p (lst_p nest, int depth1, int depth2)
  345. {
  346. mpz_t d1, d2;
  347. bool res;
  348. gcc_assert (depth1 < depth2);
  349. mpz_init (d1);
  350. mpz_init (d2);
  351. memory_strides_in_loop (nest, depth1, d1);
  352. memory_strides_in_loop (nest, depth2, d2);
  353. res = mpz_cmp (d1, d2) < 0;
  354. mpz_clear (d1);
  355. mpz_clear (d2);
  356. return res;
  357. }
  358. /* Interchanges the loops at DEPTH1 and DEPTH2 of the original
  359. scattering and assigns the resulting polyhedron to the transformed
  360. scattering. */
  361. static void
  362. pbb_interchange_loop_depths (graphite_dim_t depth1, graphite_dim_t depth2,
  363. poly_bb_p pbb)
  364. {
  365. unsigned i;
  366. unsigned dim1 = psct_dynamic_dim (pbb, depth1);
  367. unsigned dim2 = psct_dynamic_dim (pbb, depth2);
  368. isl_space *d = isl_map_get_space (pbb->transformed);
  369. isl_space *d1 = isl_space_range (d);
  370. unsigned n = isl_space_dim (d1, isl_dim_out);
  371. isl_space *d2 = isl_space_add_dims (d1, isl_dim_in, n);
  372. isl_map *x = isl_map_universe (d2);
  373. x = isl_map_equate (x, isl_dim_in, dim1, isl_dim_out, dim2);
  374. x = isl_map_equate (x, isl_dim_in, dim2, isl_dim_out, dim1);
  375. for (i = 0; i < n; i++)
  376. if (i != dim1 && i != dim2)
  377. x = isl_map_equate (x, isl_dim_in, i, isl_dim_out, i);
  378. pbb->transformed = isl_map_apply_range (pbb->transformed, x);
  379. }
  380. /* Apply the interchange of loops at depths DEPTH1 and DEPTH2 to all
  381. the statements below LST. */
  382. static void
  383. lst_apply_interchange (lst_p lst, int depth1, int depth2)
  384. {
  385. if (!lst)
  386. return;
  387. if (LST_LOOP_P (lst))
  388. {
  389. int i;
  390. lst_p l;
  391. FOR_EACH_VEC_ELT (LST_SEQ (lst), i, l)
  392. lst_apply_interchange (l, depth1, depth2);
  393. }
  394. else
  395. pbb_interchange_loop_depths (depth1, depth2, LST_PBB (lst));
  396. }
  397. /* Return true when the nest starting at LOOP1 and ending on LOOP2 is
  398. perfect: i.e. there are no sequence of statements. */
  399. static bool
  400. lst_perfectly_nested_p (lst_p loop1, lst_p loop2)
  401. {
  402. if (loop1 == loop2)
  403. return true;
  404. if (!LST_LOOP_P (loop1))
  405. return false;
  406. return LST_SEQ (loop1).length () == 1
  407. && lst_perfectly_nested_p (LST_SEQ (loop1)[0], loop2);
  408. }
  409. /* Transform the loop nest between LOOP1 and LOOP2 into a perfect
  410. nest. To continue the naming tradition, this function is called
  411. after perfect_nestify. NEST is set to the perfectly nested loop
  412. that is created. BEFORE/AFTER are set to the loops distributed
  413. before/after the loop NEST. */
  414. static void
  415. lst_perfect_nestify (lst_p loop1, lst_p loop2, lst_p *before,
  416. lst_p *nest, lst_p *after)
  417. {
  418. poly_bb_p first, last;
  419. gcc_assert (loop1 && loop2
  420. && loop1 != loop2
  421. && LST_LOOP_P (loop1) && LST_LOOP_P (loop2));
  422. first = LST_PBB (lst_find_first_pbb (loop2));
  423. last = LST_PBB (lst_find_last_pbb (loop2));
  424. *before = copy_lst (loop1);
  425. *nest = copy_lst (loop1);
  426. *after = copy_lst (loop1);
  427. lst_remove_all_before_including_pbb (*before, first, false);
  428. lst_remove_all_before_including_pbb (*after, last, true);
  429. lst_remove_all_before_excluding_pbb (*nest, first, true);
  430. lst_remove_all_before_excluding_pbb (*nest, last, false);
  431. if (lst_empty_p (*before))
  432. {
  433. free_lst (*before);
  434. *before = NULL;
  435. }
  436. if (lst_empty_p (*after))
  437. {
  438. free_lst (*after);
  439. *after = NULL;
  440. }
  441. if (lst_empty_p (*nest))
  442. {
  443. free_lst (*nest);
  444. *nest = NULL;
  445. }
  446. }
  447. /* Try to interchange LOOP1 with LOOP2 for all the statements of the
  448. body of LOOP2. LOOP1 contains LOOP2. Return true if it did the
  449. interchange. */
  450. static bool
  451. lst_try_interchange_loops (scop_p scop, lst_p loop1, lst_p loop2)
  452. {
  453. int depth1 = lst_depth (loop1);
  454. int depth2 = lst_depth (loop2);
  455. lst_p transformed;
  456. lst_p before = NULL, nest = NULL, after = NULL;
  457. if (!lst_perfectly_nested_p (loop1, loop2))
  458. lst_perfect_nestify (loop1, loop2, &before, &nest, &after);
  459. if (!lst_interchange_profitable_p (loop2, depth1, depth2))
  460. return false;
  461. lst_apply_interchange (loop2, depth1, depth2);
  462. /* Sync the transformed LST information and the PBB scatterings
  463. before using the scatterings in the data dependence analysis. */
  464. if (before || nest || after)
  465. {
  466. transformed = lst_substitute_3 (SCOP_TRANSFORMED_SCHEDULE (scop), loop1,
  467. before, nest, after);
  468. lst_update_scattering (transformed);
  469. free_lst (transformed);
  470. }
  471. if (graphite_legal_transform (scop))
  472. {
  473. if (dump_file && (dump_flags & TDF_DETAILS))
  474. fprintf (dump_file,
  475. "Loops at depths %d and %d will be interchanged.\n",
  476. depth1, depth2);
  477. /* Transform the SCOP_TRANSFORMED_SCHEDULE of the SCOP. */
  478. lst_insert_in_sequence (before, loop1, true);
  479. lst_insert_in_sequence (after, loop1, false);
  480. if (nest)
  481. {
  482. lst_replace (loop1, nest);
  483. free_lst (loop1);
  484. }
  485. return true;
  486. }
  487. /* Undo the transform. */
  488. free_lst (before);
  489. free_lst (nest);
  490. free_lst (after);
  491. lst_apply_interchange (loop2, depth2, depth1);
  492. return false;
  493. }
  494. /* Selects the inner loop in LST_SEQ (INNER_FATHER) to be interchanged
  495. with the loop OUTER in LST_SEQ (OUTER_FATHER). */
  496. static bool
  497. lst_interchange_select_inner (scop_p scop, lst_p outer_father, int outer,
  498. lst_p inner_father)
  499. {
  500. int inner;
  501. lst_p loop1, loop2;
  502. gcc_assert (outer_father
  503. && LST_LOOP_P (outer_father)
  504. && LST_LOOP_P (LST_SEQ (outer_father)[outer])
  505. && inner_father
  506. && LST_LOOP_P (inner_father));
  507. loop1 = LST_SEQ (outer_father)[outer];
  508. FOR_EACH_VEC_ELT (LST_SEQ (inner_father), inner, loop2)
  509. if (LST_LOOP_P (loop2)
  510. && (lst_try_interchange_loops (scop, loop1, loop2)
  511. || lst_interchange_select_inner (scop, outer_father, outer, loop2)))
  512. return true;
  513. return false;
  514. }
  515. /* Interchanges all the loops of LOOP and the loops of its body that
  516. are considered profitable to interchange. Return the number of
  517. interchanged loops. OUTER is the index in LST_SEQ (LOOP) that
  518. points to the next outer loop to be considered for interchange. */
  519. static int
  520. lst_interchange_select_outer (scop_p scop, lst_p loop, int outer)
  521. {
  522. lst_p l;
  523. int res = 0;
  524. int i = 0;
  525. lst_p father;
  526. if (!loop || !LST_LOOP_P (loop))
  527. return 0;
  528. father = LST_LOOP_FATHER (loop);
  529. if (father)
  530. {
  531. while (lst_interchange_select_inner (scop, father, outer, loop))
  532. {
  533. res++;
  534. loop = LST_SEQ (father)[outer];
  535. }
  536. }
  537. if (LST_LOOP_P (loop))
  538. FOR_EACH_VEC_ELT (LST_SEQ (loop), i, l)
  539. if (LST_LOOP_P (l))
  540. res += lst_interchange_select_outer (scop, l, i);
  541. return res;
  542. }
  543. /* Interchanges all the loop depths that are considered profitable for
  544. SCOP. Return the number of interchanged loops. */
  545. int
  546. scop_do_interchange (scop_p scop)
  547. {
  548. int res = lst_interchange_select_outer
  549. (scop, SCOP_TRANSFORMED_SCHEDULE (scop), 0);
  550. lst_update_scattering (SCOP_TRANSFORMED_SCHEDULE (scop));
  551. return res;
  552. }
  553. #endif