2015-08-24 François Dumont <fdumont@gcc.gnu.org>
[official-gcc.git] / gcc / graphite-blocking.c
blob343429484097d8118805f8c8ff0b9e0c796e17e0
1 /* Heuristics and transform for loop blocking and strip mining on
2 polyhedral representation.
4 Copyright (C) 2009-2015 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 /* Workaround for GMP 5.1.3 bug, see PR56019. */
28 #include <stddef.h>
30 #include <isl/constraint.h>
31 #include <isl/set.h>
32 #include <isl/map.h>
33 #include <isl/union_map.h>
34 #include <isl/constraint.h>
36 #include "system.h"
37 #include "coretypes.h"
38 #include "backend.h"
39 #include "cfghooks.h"
40 #include "tree.h"
41 #include "gimple.h"
42 #include "params.h"
43 #include "fold-const.h"
44 #include "gimple-iterator.h"
45 #include "tree-ssa-loop.h"
46 #include "dumpfile.h"
47 #include "cfgloop.h"
48 #include "tree-data-ref.h"
49 #include "graphite-poly.h"
51 /* Strip mines with a factor STRIDE the scattering (time) dimension
52 around PBB at depth TIME_DEPTH.
54 The following example comes from the wiki page:
55 http://gcc.gnu.org/wiki/Graphite/Strip_mine
57 The strip mine of a loop with a tile of 64 can be obtained with a
58 scattering function as follows:
60 $ cat ./albert_strip_mine.cloog
61 # language: C
64 # parameter {n | n >= 0}
65 1 3
66 # n 1
67 1 1 0
71 1 # Number of statements:
74 # {i | 0 <= i <= n}
75 2 4
76 # i n 1
77 1 1 0 0
78 1 -1 1 0
80 0 0 0
84 1 # Scattering functions
86 3 6
87 # NEW OLD i n 1
88 1 -64 0 1 0 0
89 1 64 0 -1 0 63
90 0 0 1 -1 0 0
93 NEW OLD
95 #the output of CLooG is like this:
96 #$ cloog ./albert_strip_mine.cloog
97 # for (NEW=0;NEW<=floord(n,64);NEW++) {
98 # for (OLD=max(64*NEW,0);OLD<=min(64*NEW+63,n);OLD++) {
99 # S1(i = OLD) ;
104 static void
105 pbb_strip_mine_time_depth (poly_bb_p pbb, int time_depth, int stride)
107 isl_space *d;
108 isl_constraint *c;
109 int iter, strip;
110 /* STRIP is the dimension that iterates with stride STRIDE. */
111 /* ITER is the dimension that enumerates single iterations inside
112 one strip that has at most STRIDE iterations. */
113 strip = time_depth;
114 iter = strip + 2;
116 pbb->transformed = isl_map_insert_dims (pbb->transformed, isl_dim_out,
117 strip, 2);
119 /* Lower bound of the striped loop. */
120 d = isl_map_get_space (pbb->transformed);
121 c = isl_inequality_alloc (isl_local_space_from_space (d));
122 c = isl_constraint_set_coefficient_si (c, isl_dim_out, strip, -stride);
123 c = isl_constraint_set_coefficient_si (c, isl_dim_out, iter, 1);
124 pbb->transformed = isl_map_add_constraint (pbb->transformed, c);
126 /* Upper bound of the striped loop. */
127 d = isl_map_get_space (pbb->transformed);
128 c = isl_inequality_alloc (isl_local_space_from_space (d));
129 c = isl_constraint_set_coefficient_si (c, isl_dim_out, strip, stride);
130 c = isl_constraint_set_coefficient_si (c, isl_dim_out, iter, -1);
131 c = isl_constraint_set_constant_si (c, stride - 1);
132 pbb->transformed = isl_map_add_constraint (pbb->transformed, c);
134 /* Static scheduling for ITER level.
135 This is mandatory to keep the 2d + 1 canonical scheduling format. */
136 d = isl_map_get_space (pbb->transformed);
137 c = isl_equality_alloc (isl_local_space_from_space (d));
138 c = isl_constraint_set_coefficient_si (c, isl_dim_out, strip + 1, 1);
139 pbb->transformed = isl_map_add_constraint (pbb->transformed, c);
142 /* Returns true when strip mining with STRIDE of the loop LST is
143 profitable. */
145 static bool
146 lst_strip_mine_profitable_p (lst_p lst, int stride)
148 mpz_t niter, strip_stride;
149 bool res;
151 gcc_assert (LST_LOOP_P (lst));
152 mpz_init (strip_stride);
153 mpz_init (niter);
155 mpz_set_si (strip_stride, stride);
156 lst_niter_for_loop (lst, niter);
157 res = (mpz_cmp (niter, strip_stride) > 0);
159 mpz_clear (strip_stride);
160 mpz_clear (niter);
161 return res;
164 /* Strip-mines all the loops of LST with STRIDE. Return the number of
165 loops strip-mined. */
167 static int
168 lst_do_strip_mine_loop (lst_p lst, int depth, int stride)
170 int i;
171 lst_p l;
172 poly_bb_p pbb;
174 if (!lst)
175 return 0;
177 if (LST_LOOP_P (lst))
179 int res = 0;
181 FOR_EACH_VEC_ELT (LST_SEQ (lst), i, l)
182 res += lst_do_strip_mine_loop (l, depth, stride);
184 return res;
187 pbb = LST_PBB (lst);
188 pbb_strip_mine_time_depth (pbb, psct_dynamic_dim (pbb, depth), stride);
189 return 1;
192 /* Strip-mines all the loops of LST with STRIDE. When STRIDE is zero,
193 read the stride from the PARAM_LOOP_BLOCK_TILE_SIZE. Return the
194 number of strip-mined loops.
196 Strip mining transforms a loop
198 | for (i = 0; i < N; i++)
199 | S (i);
201 into the following loop nest:
203 | for (k = 0; k < N; k += STRIDE)
204 | for (j = 0; j < STRIDE; j++)
205 | S (i = k + j);
208 static int
209 lst_do_strip_mine (lst_p lst, int stride)
211 int i;
212 lst_p l;
213 int res = 0;
214 int depth;
216 if (!stride)
217 stride = PARAM_VALUE (PARAM_LOOP_BLOCK_TILE_SIZE);
219 if (!lst
220 || !LST_LOOP_P (lst))
221 return false;
223 FOR_EACH_VEC_ELT (LST_SEQ (lst), i, l)
224 res += lst_do_strip_mine (l, stride);
226 depth = lst_depth (lst);
227 if (depth >= 0
228 && lst_strip_mine_profitable_p (lst, stride))
230 res += lst_do_strip_mine_loop (lst, lst_depth (lst), stride);
231 lst_add_loop_under_loop (lst);
234 return res;
237 /* Strip mines all the loops in SCOP. Returns the number of
238 strip-mined loops. */
241 scop_do_strip_mine (scop_p scop, int stride)
243 return lst_do_strip_mine (SCOP_TRANSFORMED_SCHEDULE (scop), stride);
246 /* Loop blocks all the loops in SCOP. Returns true when we manage to
247 block some loops. */
249 bool
250 scop_do_block (scop_p scop)
252 store_scattering (scop);
254 /* If we don't strip mine at least two loops, or not interchange
255 loops, the strip mine alone will not be profitable, and the
256 transform is not a loop blocking: so revert the transform. */
257 if (lst_do_strip_mine (SCOP_TRANSFORMED_SCHEDULE (scop), 0) < 2
258 || scop_do_interchange (scop) == 0)
260 restore_scattering (scop);
261 return false;
264 if (dump_file && (dump_flags & TDF_DETAILS))
265 fprintf (dump_file, "SCoP will be loop blocked.\n");
267 return true;
270 #endif