2015-06-25 Zhouyi Zhou <yizhouzhou@ict.ac.cn>
[official-gcc.git] / gcc / graphite-blocking.c
blobaba4ee6973c7d3bf09ecf773c46184d6890fdd4f
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 "alias.h"
36 #include "symtab.h"
37 #include "options.h"
38 #include "tree.h"
39 #include "fold-const.h"
40 #include "predict.h"
41 #include "tm.h"
42 #include "hard-reg-set.h"
43 #include "function.h"
44 #include "dominance.h"
45 #include "basic-block.h"
46 #include "tree-ssa-alias.h"
47 #include "internal-fn.h"
48 #include "gimple-expr.h"
49 #include "gimple.h"
50 #include "gimple-iterator.h"
51 #include "tree-ssa-loop.h"
52 #include "dumpfile.h"
53 #include "cfgloop.h"
54 #include "tree-chrec.h"
55 #include "tree-data-ref.h"
56 #include "sese.h"
58 #ifdef HAVE_isl
59 #include "graphite-poly.h"
62 /* Strip mines with a factor STRIDE the scattering (time) dimension
63 around PBB at depth TIME_DEPTH.
65 The following example comes from the wiki page:
66 http://gcc.gnu.org/wiki/Graphite/Strip_mine
68 The strip mine of a loop with a tile of 64 can be obtained with a
69 scattering function as follows:
71 $ cat ./albert_strip_mine.cloog
72 # language: C
75 # parameter {n | n >= 0}
76 1 3
77 # n 1
78 1 1 0
82 1 # Number of statements:
85 # {i | 0 <= i <= n}
86 2 4
87 # i n 1
88 1 1 0 0
89 1 -1 1 0
91 0 0 0
95 1 # Scattering functions
97 3 6
98 # NEW OLD i n 1
99 1 -64 0 1 0 0
100 1 64 0 -1 0 63
101 0 0 1 -1 0 0
104 NEW OLD
106 #the output of CLooG is like this:
107 #$ cloog ./albert_strip_mine.cloog
108 # for (NEW=0;NEW<=floord(n,64);NEW++) {
109 # for (OLD=max(64*NEW,0);OLD<=min(64*NEW+63,n);OLD++) {
110 # S1(i = OLD) ;
115 static void
116 pbb_strip_mine_time_depth (poly_bb_p pbb, int time_depth, int stride)
118 isl_space *d;
119 isl_constraint *c;
120 int iter, strip;
121 /* STRIP is the dimension that iterates with stride STRIDE. */
122 /* ITER is the dimension that enumerates single iterations inside
123 one strip that has at most STRIDE iterations. */
124 strip = time_depth;
125 iter = strip + 2;
127 pbb->transformed = isl_map_insert_dims (pbb->transformed, isl_dim_out,
128 strip, 2);
130 /* Lower bound of the striped loop. */
131 d = isl_map_get_space (pbb->transformed);
132 c = isl_inequality_alloc (isl_local_space_from_space (d));
133 c = isl_constraint_set_coefficient_si (c, isl_dim_out, strip, -stride);
134 c = isl_constraint_set_coefficient_si (c, isl_dim_out, iter, 1);
135 pbb->transformed = isl_map_add_constraint (pbb->transformed, c);
137 /* Upper bound of the striped loop. */
138 d = isl_map_get_space (pbb->transformed);
139 c = isl_inequality_alloc (isl_local_space_from_space (d));
140 c = isl_constraint_set_coefficient_si (c, isl_dim_out, strip, stride);
141 c = isl_constraint_set_coefficient_si (c, isl_dim_out, iter, -1);
142 c = isl_constraint_set_constant_si (c, stride - 1);
143 pbb->transformed = isl_map_add_constraint (pbb->transformed, c);
145 /* Static scheduling for ITER level.
146 This is mandatory to keep the 2d + 1 canonical scheduling format. */
147 d = isl_map_get_space (pbb->transformed);
148 c = isl_equality_alloc (isl_local_space_from_space (d));
149 c = isl_constraint_set_coefficient_si (c, isl_dim_out, strip + 1, 1);
150 pbb->transformed = isl_map_add_constraint (pbb->transformed, c);
153 /* Returns true when strip mining with STRIDE of the loop LST is
154 profitable. */
156 static bool
157 lst_strip_mine_profitable_p (lst_p lst, int stride)
159 mpz_t niter, strip_stride;
160 bool res;
162 gcc_assert (LST_LOOP_P (lst));
163 mpz_init (strip_stride);
164 mpz_init (niter);
166 mpz_set_si (strip_stride, stride);
167 lst_niter_for_loop (lst, niter);
168 res = (mpz_cmp (niter, strip_stride) > 0);
170 mpz_clear (strip_stride);
171 mpz_clear (niter);
172 return res;
175 /* Strip-mines all the loops of LST with STRIDE. Return the number of
176 loops strip-mined. */
178 static int
179 lst_do_strip_mine_loop (lst_p lst, int depth, int stride)
181 int i;
182 lst_p l;
183 poly_bb_p pbb;
185 if (!lst)
186 return 0;
188 if (LST_LOOP_P (lst))
190 int res = 0;
192 FOR_EACH_VEC_ELT (LST_SEQ (lst), i, l)
193 res += lst_do_strip_mine_loop (l, depth, stride);
195 return res;
198 pbb = LST_PBB (lst);
199 pbb_strip_mine_time_depth (pbb, psct_dynamic_dim (pbb, depth), stride);
200 return 1;
203 /* Strip-mines all the loops of LST with STRIDE. When STRIDE is zero,
204 read the stride from the PARAM_LOOP_BLOCK_TILE_SIZE. Return the
205 number of strip-mined loops.
207 Strip mining transforms a loop
209 | for (i = 0; i < N; i++)
210 | S (i);
212 into the following loop nest:
214 | for (k = 0; k < N; k += STRIDE)
215 | for (j = 0; j < STRIDE; j++)
216 | S (i = k + j);
219 static int
220 lst_do_strip_mine (lst_p lst, int stride)
222 int i;
223 lst_p l;
224 int res = 0;
225 int depth;
227 if (!stride)
228 stride = PARAM_VALUE (PARAM_LOOP_BLOCK_TILE_SIZE);
230 if (!lst
231 || !LST_LOOP_P (lst))
232 return false;
234 FOR_EACH_VEC_ELT (LST_SEQ (lst), i, l)
235 res += lst_do_strip_mine (l, stride);
237 depth = lst_depth (lst);
238 if (depth >= 0
239 && lst_strip_mine_profitable_p (lst, stride))
241 res += lst_do_strip_mine_loop (lst, lst_depth (lst), stride);
242 lst_add_loop_under_loop (lst);
245 return res;
248 /* Strip mines all the loops in SCOP. Returns the number of
249 strip-mined loops. */
252 scop_do_strip_mine (scop_p scop, int stride)
254 return lst_do_strip_mine (SCOP_TRANSFORMED_SCHEDULE (scop), stride);
257 /* Loop blocks all the loops in SCOP. Returns true when we manage to
258 block some loops. */
260 bool
261 scop_do_block (scop_p scop)
263 store_scattering (scop);
265 /* If we don't strip mine at least two loops, or not interchange
266 loops, the strip mine alone will not be profitable, and the
267 transform is not a loop blocking: so revert the transform. */
268 if (lst_do_strip_mine (SCOP_TRANSFORMED_SCHEDULE (scop), 0) < 2
269 || scop_do_interchange (scop) == 0)
271 restore_scattering (scop);
272 return false;
275 if (dump_file && (dump_flags & TDF_DETAILS))
276 fprintf (dump_file, "SCoP will be loop blocked.\n");
278 return true;
281 #endif