2013-10-22 Jan-Benedict Glaw <jbglaw@lug-owl.de>
[official-gcc.git] / gcc / graphite-blocking.c
blob491c815c9e598fee2546b9b2ad2124bc46691337
1 /* Heuristics and transform for loop blocking and strip mining on
2 polyhedral representation.
4 Copyright (C) 2009-2013 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_cloog
27 #include <isl/set.h>
28 #include <isl/map.h>
29 #include <isl/union_map.h>
30 #include <isl/constraint.h>
31 #include <cloog/cloog.h>
32 #include <cloog/isl/domain.h>
33 #endif
35 #include "system.h"
36 #include "coretypes.h"
37 #include "tree.h"
38 #include "tree-ssa.h"
39 #include "dumpfile.h"
40 #include "cfgloop.h"
41 #include "tree-chrec.h"
42 #include "tree-data-ref.h"
43 #include "sese.h"
45 #ifdef HAVE_cloog
46 #include "graphite-poly.h"
49 /* Strip mines with a factor STRIDE the scattering (time) dimension
50 around PBB at depth TIME_DEPTH.
52 The following example comes from the wiki page:
53 http://gcc.gnu.org/wiki/Graphite/Strip_mine
55 The strip mine of a loop with a tile of 64 can be obtained with a
56 scattering function as follows:
58 $ cat ./albert_strip_mine.cloog
59 # language: C
62 # parameter {n | n >= 0}
63 1 3
64 # n 1
65 1 1 0
69 1 # Number of statements:
72 # {i | 0 <= i <= n}
73 2 4
74 # i n 1
75 1 1 0 0
76 1 -1 1 0
78 0 0 0
82 1 # Scattering functions
84 3 6
85 # NEW OLD i n 1
86 1 -64 0 1 0 0
87 1 64 0 -1 0 63
88 0 0 1 -1 0 0
91 NEW OLD
93 #the output of CLooG is like this:
94 #$ cloog ./albert_strip_mine.cloog
95 # for (NEW=0;NEW<=floord(n,64);NEW++) {
96 # for (OLD=max(64*NEW,0);OLD<=min(64*NEW+63,n);OLD++) {
97 # S1(i = OLD) ;
98 # }
99 # }
102 static void
103 pbb_strip_mine_time_depth (poly_bb_p pbb, int time_depth, int stride)
105 isl_space *d;
106 isl_constraint *c;
107 int iter, strip;
108 /* STRIP is the dimension that iterates with stride STRIDE. */
109 /* ITER is the dimension that enumerates single iterations inside
110 one strip that has at most STRIDE iterations. */
111 strip = time_depth;
112 iter = strip + 2;
114 pbb->transformed = isl_map_insert_dims (pbb->transformed, isl_dim_out,
115 strip, 2);
117 /* Lower bound of the striped loop. */
118 d = isl_map_get_space (pbb->transformed);
119 c = isl_inequality_alloc (isl_local_space_from_space (d));
120 c = isl_constraint_set_coefficient_si (c, isl_dim_out, strip, -stride);
121 c = isl_constraint_set_coefficient_si (c, isl_dim_out, iter, 1);
122 pbb->transformed = isl_map_add_constraint (pbb->transformed, c);
124 /* Upper bound of the striped loop. */
125 d = isl_map_get_space (pbb->transformed);
126 c = isl_inequality_alloc (isl_local_space_from_space (d));
127 c = isl_constraint_set_coefficient_si (c, isl_dim_out, strip, stride);
128 c = isl_constraint_set_coefficient_si (c, isl_dim_out, iter, -1);
129 c = isl_constraint_set_constant_si (c, stride - 1);
130 pbb->transformed = isl_map_add_constraint (pbb->transformed, c);
132 /* Static scheduling for ITER level.
133 This is mandatory to keep the 2d + 1 canonical scheduling format. */
134 d = isl_map_get_space (pbb->transformed);
135 c = isl_equality_alloc (isl_local_space_from_space (d));
136 c = isl_constraint_set_coefficient_si (c, isl_dim_out, strip + 1, 1);
137 pbb->transformed = isl_map_add_constraint (pbb->transformed, c);
140 /* Returns true when strip mining with STRIDE of the loop LST is
141 profitable. */
143 static bool
144 lst_strip_mine_profitable_p (lst_p lst, int stride)
146 mpz_t niter, strip_stride;
147 bool res;
149 gcc_assert (LST_LOOP_P (lst));
150 mpz_init (strip_stride);
151 mpz_init (niter);
153 mpz_set_si (strip_stride, stride);
154 lst_niter_for_loop (lst, niter);
155 res = (mpz_cmp (niter, strip_stride) > 0);
157 mpz_clear (strip_stride);
158 mpz_clear (niter);
159 return res;
162 /* Strip-mines all the loops of LST with STRIDE. Return the number of
163 loops strip-mined. */
165 static int
166 lst_do_strip_mine_loop (lst_p lst, int depth, int stride)
168 int i;
169 lst_p l;
170 poly_bb_p pbb;
172 if (!lst)
173 return 0;
175 if (LST_LOOP_P (lst))
177 int res = 0;
179 FOR_EACH_VEC_ELT (LST_SEQ (lst), i, l)
180 res += lst_do_strip_mine_loop (l, depth, stride);
182 return res;
185 pbb = LST_PBB (lst);
186 pbb_strip_mine_time_depth (pbb, psct_dynamic_dim (pbb, depth), stride);
187 return 1;
190 /* Strip-mines all the loops of LST with STRIDE. When STRIDE is zero,
191 read the stride from the PARAM_LOOP_BLOCK_TILE_SIZE. Return the
192 number of strip-mined loops.
194 Strip mining transforms a loop
196 | for (i = 0; i < N; i++)
197 | S (i);
199 into the following loop nest:
201 | for (k = 0; k < N; k += STRIDE)
202 | for (j = 0; j < STRIDE; j++)
203 | S (i = k + j);
206 static int
207 lst_do_strip_mine (lst_p lst, int stride)
209 int i;
210 lst_p l;
211 int res = 0;
212 int depth;
214 if (!stride)
215 stride = PARAM_VALUE (PARAM_LOOP_BLOCK_TILE_SIZE);
217 if (!lst
218 || !LST_LOOP_P (lst))
219 return false;
221 FOR_EACH_VEC_ELT (LST_SEQ (lst), i, l)
222 res += lst_do_strip_mine (l, stride);
224 depth = lst_depth (lst);
225 if (depth >= 0
226 && lst_strip_mine_profitable_p (lst, stride))
228 res += lst_do_strip_mine_loop (lst, lst_depth (lst), stride);
229 lst_add_loop_under_loop (lst);
232 return res;
235 /* Strip mines all the loops in SCOP. Returns the number of
236 strip-mined loops. */
239 scop_do_strip_mine (scop_p scop, int stride)
241 return lst_do_strip_mine (SCOP_TRANSFORMED_SCHEDULE (scop), stride);
244 /* Loop blocks all the loops in SCOP. Returns true when we manage to
245 block some loops. */
247 bool
248 scop_do_block (scop_p scop)
250 store_scattering (scop);
252 /* If we don't strip mine at least two loops, or not interchange
253 loops, the strip mine alone will not be profitable, and the
254 transform is not a loop blocking: so revert the transform. */
255 if (lst_do_strip_mine (SCOP_TRANSFORMED_SCHEDULE (scop), 0) < 2
256 || scop_do_interchange (scop) == 0)
258 restore_scattering (scop);
259 return false;
262 if (dump_file && (dump_flags & TDF_DETAILS))
263 fprintf (dump_file, "SCoP will be loop blocked.\n");
265 return true;
268 #endif