2015-06-11 Paul Thomas <pault@gcc.gnu.org>
[official-gcc.git] / gcc / graphite-blocking.c
bloba14a474c9b6473769f122da0f668a14aef7a8df3
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 #include <isl/set.h>
28 #include <isl/map.h>
29 #include <isl/union_map.h>
30 #include <isl/constraint.h>
31 #endif
33 #include "system.h"
34 #include "coretypes.h"
35 #include "input.h"
36 #include "alias.h"
37 #include "symtab.h"
38 #include "options.h"
39 #include "tree.h"
40 #include "fold-const.h"
41 #include "predict.h"
42 #include "tm.h"
43 #include "hard-reg-set.h"
44 #include "input.h"
45 #include "function.h"
46 #include "dominance.h"
47 #include "basic-block.h"
48 #include "tree-ssa-alias.h"
49 #include "internal-fn.h"
50 #include "gimple-expr.h"
51 #include "is-a.h"
52 #include "gimple.h"
53 #include "gimple-iterator.h"
54 #include "tree-ssa-loop.h"
55 #include "dumpfile.h"
56 #include "cfgloop.h"
57 #include "tree-chrec.h"
58 #include "tree-data-ref.h"
59 #include "sese.h"
61 #ifdef HAVE_isl
62 #include "graphite-poly.h"
65 /* Strip mines with a factor STRIDE the scattering (time) dimension
66 around PBB at depth TIME_DEPTH.
68 The following example comes from the wiki page:
69 http://gcc.gnu.org/wiki/Graphite/Strip_mine
71 The strip mine of a loop with a tile of 64 can be obtained with a
72 scattering function as follows:
74 $ cat ./albert_strip_mine.cloog
75 # language: C
78 # parameter {n | n >= 0}
79 1 3
80 # n 1
81 1 1 0
85 1 # Number of statements:
88 # {i | 0 <= i <= n}
89 2 4
90 # i n 1
91 1 1 0 0
92 1 -1 1 0
94 0 0 0
98 1 # Scattering functions
101 # NEW OLD i n 1
102 1 -64 0 1 0 0
103 1 64 0 -1 0 63
104 0 0 1 -1 0 0
107 NEW OLD
109 #the output of CLooG is like this:
110 #$ cloog ./albert_strip_mine.cloog
111 # for (NEW=0;NEW<=floord(n,64);NEW++) {
112 # for (OLD=max(64*NEW,0);OLD<=min(64*NEW+63,n);OLD++) {
113 # S1(i = OLD) ;
118 static void
119 pbb_strip_mine_time_depth (poly_bb_p pbb, int time_depth, int stride)
121 isl_space *d;
122 isl_constraint *c;
123 int iter, strip;
124 /* STRIP is the dimension that iterates with stride STRIDE. */
125 /* ITER is the dimension that enumerates single iterations inside
126 one strip that has at most STRIDE iterations. */
127 strip = time_depth;
128 iter = strip + 2;
130 pbb->transformed = isl_map_insert_dims (pbb->transformed, isl_dim_out,
131 strip, 2);
133 /* Lower bound of the striped loop. */
134 d = isl_map_get_space (pbb->transformed);
135 c = isl_inequality_alloc (isl_local_space_from_space (d));
136 c = isl_constraint_set_coefficient_si (c, isl_dim_out, strip, -stride);
137 c = isl_constraint_set_coefficient_si (c, isl_dim_out, iter, 1);
138 pbb->transformed = isl_map_add_constraint (pbb->transformed, c);
140 /* Upper bound of the striped loop. */
141 d = isl_map_get_space (pbb->transformed);
142 c = isl_inequality_alloc (isl_local_space_from_space (d));
143 c = isl_constraint_set_coefficient_si (c, isl_dim_out, strip, stride);
144 c = isl_constraint_set_coefficient_si (c, isl_dim_out, iter, -1);
145 c = isl_constraint_set_constant_si (c, stride - 1);
146 pbb->transformed = isl_map_add_constraint (pbb->transformed, c);
148 /* Static scheduling for ITER level.
149 This is mandatory to keep the 2d + 1 canonical scheduling format. */
150 d = isl_map_get_space (pbb->transformed);
151 c = isl_equality_alloc (isl_local_space_from_space (d));
152 c = isl_constraint_set_coefficient_si (c, isl_dim_out, strip + 1, 1);
153 pbb->transformed = isl_map_add_constraint (pbb->transformed, c);
156 /* Returns true when strip mining with STRIDE of the loop LST is
157 profitable. */
159 static bool
160 lst_strip_mine_profitable_p (lst_p lst, int stride)
162 mpz_t niter, strip_stride;
163 bool res;
165 gcc_assert (LST_LOOP_P (lst));
166 mpz_init (strip_stride);
167 mpz_init (niter);
169 mpz_set_si (strip_stride, stride);
170 lst_niter_for_loop (lst, niter);
171 res = (mpz_cmp (niter, strip_stride) > 0);
173 mpz_clear (strip_stride);
174 mpz_clear (niter);
175 return res;
178 /* Strip-mines all the loops of LST with STRIDE. Return the number of
179 loops strip-mined. */
181 static int
182 lst_do_strip_mine_loop (lst_p lst, int depth, int stride)
184 int i;
185 lst_p l;
186 poly_bb_p pbb;
188 if (!lst)
189 return 0;
191 if (LST_LOOP_P (lst))
193 int res = 0;
195 FOR_EACH_VEC_ELT (LST_SEQ (lst), i, l)
196 res += lst_do_strip_mine_loop (l, depth, stride);
198 return res;
201 pbb = LST_PBB (lst);
202 pbb_strip_mine_time_depth (pbb, psct_dynamic_dim (pbb, depth), stride);
203 return 1;
206 /* Strip-mines all the loops of LST with STRIDE. When STRIDE is zero,
207 read the stride from the PARAM_LOOP_BLOCK_TILE_SIZE. Return the
208 number of strip-mined loops.
210 Strip mining transforms a loop
212 | for (i = 0; i < N; i++)
213 | S (i);
215 into the following loop nest:
217 | for (k = 0; k < N; k += STRIDE)
218 | for (j = 0; j < STRIDE; j++)
219 | S (i = k + j);
222 static int
223 lst_do_strip_mine (lst_p lst, int stride)
225 int i;
226 lst_p l;
227 int res = 0;
228 int depth;
230 if (!stride)
231 stride = PARAM_VALUE (PARAM_LOOP_BLOCK_TILE_SIZE);
233 if (!lst
234 || !LST_LOOP_P (lst))
235 return false;
237 FOR_EACH_VEC_ELT (LST_SEQ (lst), i, l)
238 res += lst_do_strip_mine (l, stride);
240 depth = lst_depth (lst);
241 if (depth >= 0
242 && lst_strip_mine_profitable_p (lst, stride))
244 res += lst_do_strip_mine_loop (lst, lst_depth (lst), stride);
245 lst_add_loop_under_loop (lst);
248 return res;
251 /* Strip mines all the loops in SCOP. Returns the number of
252 strip-mined loops. */
255 scop_do_strip_mine (scop_p scop, int stride)
257 return lst_do_strip_mine (SCOP_TRANSFORMED_SCHEDULE (scop), stride);
260 /* Loop blocks all the loops in SCOP. Returns true when we manage to
261 block some loops. */
263 bool
264 scop_do_block (scop_p scop)
266 store_scattering (scop);
268 /* If we don't strip mine at least two loops, or not interchange
269 loops, the strip mine alone will not be profitable, and the
270 transform is not a loop blocking: so revert the transform. */
271 if (lst_do_strip_mine (SCOP_TRANSFORMED_SCHEDULE (scop), 0) < 2
272 || scop_do_interchange (scop) == 0)
274 restore_scattering (scop);
275 return false;
278 if (dump_file && (dump_flags & TDF_DETAILS))
279 fprintf (dump_file, "SCoP will be loop blocked.\n");
281 return true;
284 #endif