Merge trunk version 217032 into gupc branch.
[official-gcc.git] / gcc / graphite-blocking.c
blob0b67bea39a86832aa74624d0ee8833011fb7fb98
1 /* Heuristics and transform for loop blocking and strip mining on
2 polyhedral representation.
4 Copyright (C) 2009-2014 Free Software Foundation, Inc.
5 Contributed by Sebastian Pop <sebastian.pop@amd.com> and
6 Pranav Garg <pranav.garg2107@gmail.com>.
8 This file is part of GCC.
10 GCC is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3, or (at your option)
13 any later version.
15 GCC is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU 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 #include "config.h"
26 #ifdef HAVE_isl
27 #include <isl/set.h>
28 #include <isl/map.h>
29 #include <isl/union_map.h>
30 #include <isl/constraint.h>
31 #ifdef HAVE_cloog
32 #include <cloog/cloog.h>
33 #include <cloog/isl/domain.h>
34 #endif
35 #endif
37 #include "system.h"
38 #include "coretypes.h"
39 #include "tree.h"
40 #include "predict.h"
41 #include "vec.h"
42 #include "hashtab.h"
43 #include "hash-set.h"
44 #include "machmode.h"
45 #include "tm.h"
46 #include "hard-reg-set.h"
47 #include "input.h"
48 #include "function.h"
49 #include "dominance.h"
50 #include "basic-block.h"
51 #include "tree-ssa-alias.h"
52 #include "internal-fn.h"
53 #include "gimple-expr.h"
54 #include "is-a.h"
55 #include "gimple.h"
56 #include "gimple-iterator.h"
57 #include "tree-ssa-loop.h"
58 #include "dumpfile.h"
59 #include "cfgloop.h"
60 #include "tree-chrec.h"
61 #include "tree-data-ref.h"
62 #include "sese.h"
64 #ifdef HAVE_isl
65 #include "graphite-poly.h"
68 /* Strip mines with a factor STRIDE the scattering (time) dimension
69 around PBB at depth TIME_DEPTH.
71 The following example comes from the wiki page:
72 http://gcc.gnu.org/wiki/Graphite/Strip_mine
74 The strip mine of a loop with a tile of 64 can be obtained with a
75 scattering function as follows:
77 $ cat ./albert_strip_mine.cloog
78 # language: C
81 # parameter {n | n >= 0}
82 1 3
83 # n 1
84 1 1 0
88 1 # Number of statements:
91 # {i | 0 <= i <= n}
92 2 4
93 # i n 1
94 1 1 0 0
95 1 -1 1 0
97 0 0 0
101 1 # Scattering functions
104 # NEW OLD i n 1
105 1 -64 0 1 0 0
106 1 64 0 -1 0 63
107 0 0 1 -1 0 0
110 NEW OLD
112 #the output of CLooG is like this:
113 #$ cloog ./albert_strip_mine.cloog
114 # for (NEW=0;NEW<=floord(n,64);NEW++) {
115 # for (OLD=max(64*NEW,0);OLD<=min(64*NEW+63,n);OLD++) {
116 # S1(i = OLD) ;
121 static void
122 pbb_strip_mine_time_depth (poly_bb_p pbb, int time_depth, int stride)
124 isl_space *d;
125 isl_constraint *c;
126 int iter, strip;
127 /* STRIP is the dimension that iterates with stride STRIDE. */
128 /* ITER is the dimension that enumerates single iterations inside
129 one strip that has at most STRIDE iterations. */
130 strip = time_depth;
131 iter = strip + 2;
133 pbb->transformed = isl_map_insert_dims (pbb->transformed, isl_dim_out,
134 strip, 2);
136 /* Lower bound of the striped loop. */
137 d = isl_map_get_space (pbb->transformed);
138 c = isl_inequality_alloc (isl_local_space_from_space (d));
139 c = isl_constraint_set_coefficient_si (c, isl_dim_out, strip, -stride);
140 c = isl_constraint_set_coefficient_si (c, isl_dim_out, iter, 1);
141 pbb->transformed = isl_map_add_constraint (pbb->transformed, c);
143 /* Upper bound of the striped loop. */
144 d = isl_map_get_space (pbb->transformed);
145 c = isl_inequality_alloc (isl_local_space_from_space (d));
146 c = isl_constraint_set_coefficient_si (c, isl_dim_out, strip, stride);
147 c = isl_constraint_set_coefficient_si (c, isl_dim_out, iter, -1);
148 c = isl_constraint_set_constant_si (c, stride - 1);
149 pbb->transformed = isl_map_add_constraint (pbb->transformed, c);
151 /* Static scheduling for ITER level.
152 This is mandatory to keep the 2d + 1 canonical scheduling format. */
153 d = isl_map_get_space (pbb->transformed);
154 c = isl_equality_alloc (isl_local_space_from_space (d));
155 c = isl_constraint_set_coefficient_si (c, isl_dim_out, strip + 1, 1);
156 pbb->transformed = isl_map_add_constraint (pbb->transformed, c);
159 /* Returns true when strip mining with STRIDE of the loop LST is
160 profitable. */
162 static bool
163 lst_strip_mine_profitable_p (lst_p lst, int stride)
165 mpz_t niter, strip_stride;
166 bool res;
168 gcc_assert (LST_LOOP_P (lst));
169 mpz_init (strip_stride);
170 mpz_init (niter);
172 mpz_set_si (strip_stride, stride);
173 lst_niter_for_loop (lst, niter);
174 res = (mpz_cmp (niter, strip_stride) > 0);
176 mpz_clear (strip_stride);
177 mpz_clear (niter);
178 return res;
181 /* Strip-mines all the loops of LST with STRIDE. Return the number of
182 loops strip-mined. */
184 static int
185 lst_do_strip_mine_loop (lst_p lst, int depth, int stride)
187 int i;
188 lst_p l;
189 poly_bb_p pbb;
191 if (!lst)
192 return 0;
194 if (LST_LOOP_P (lst))
196 int res = 0;
198 FOR_EACH_VEC_ELT (LST_SEQ (lst), i, l)
199 res += lst_do_strip_mine_loop (l, depth, stride);
201 return res;
204 pbb = LST_PBB (lst);
205 pbb_strip_mine_time_depth (pbb, psct_dynamic_dim (pbb, depth), stride);
206 return 1;
209 /* Strip-mines all the loops of LST with STRIDE. When STRIDE is zero,
210 read the stride from the PARAM_LOOP_BLOCK_TILE_SIZE. Return the
211 number of strip-mined loops.
213 Strip mining transforms a loop
215 | for (i = 0; i < N; i++)
216 | S (i);
218 into the following loop nest:
220 | for (k = 0; k < N; k += STRIDE)
221 | for (j = 0; j < STRIDE; j++)
222 | S (i = k + j);
225 static int
226 lst_do_strip_mine (lst_p lst, int stride)
228 int i;
229 lst_p l;
230 int res = 0;
231 int depth;
233 if (!stride)
234 stride = PARAM_VALUE (PARAM_LOOP_BLOCK_TILE_SIZE);
236 if (!lst
237 || !LST_LOOP_P (lst))
238 return false;
240 FOR_EACH_VEC_ELT (LST_SEQ (lst), i, l)
241 res += lst_do_strip_mine (l, stride);
243 depth = lst_depth (lst);
244 if (depth >= 0
245 && lst_strip_mine_profitable_p (lst, stride))
247 res += lst_do_strip_mine_loop (lst, lst_depth (lst), stride);
248 lst_add_loop_under_loop (lst);
251 return res;
254 /* Strip mines all the loops in SCOP. Returns the number of
255 strip-mined loops. */
258 scop_do_strip_mine (scop_p scop, int stride)
260 return lst_do_strip_mine (SCOP_TRANSFORMED_SCHEDULE (scop), stride);
263 /* Loop blocks all the loops in SCOP. Returns true when we manage to
264 block some loops. */
266 bool
267 scop_do_block (scop_p scop)
269 store_scattering (scop);
271 /* If we don't strip mine at least two loops, or not interchange
272 loops, the strip mine alone will not be profitable, and the
273 transform is not a loop blocking: so revert the transform. */
274 if (lst_do_strip_mine (SCOP_TRANSFORMED_SCHEDULE (scop), 0) < 2
275 || scop_do_interchange (scop) == 0)
277 restore_scattering (scop);
278 return false;
281 if (dump_file && (dump_flags & TDF_DETAILS))
282 fprintf (dump_file, "SCoP will be loop blocked.\n");
284 return true;
287 #endif