2013-10-30 Richard Biener <rguenther@suse.de>
[official-gcc.git] / gcc / graphite-blocking.c
blob1fff36c53bc73e7239db4db138dc25db0fef0515
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 "gimple.h"
39 #include "tree-ssa-loop.h"
40 #include "dumpfile.h"
41 #include "cfgloop.h"
42 #include "tree-chrec.h"
43 #include "tree-data-ref.h"
44 #include "sese.h"
46 #ifdef HAVE_cloog
47 #include "graphite-poly.h"
50 /* Strip mines with a factor STRIDE the scattering (time) dimension
51 around PBB at depth TIME_DEPTH.
53 The following example comes from the wiki page:
54 http://gcc.gnu.org/wiki/Graphite/Strip_mine
56 The strip mine of a loop with a tile of 64 can be obtained with a
57 scattering function as follows:
59 $ cat ./albert_strip_mine.cloog
60 # language: C
63 # parameter {n | n >= 0}
64 1 3
65 # n 1
66 1 1 0
70 1 # Number of statements:
73 # {i | 0 <= i <= n}
74 2 4
75 # i n 1
76 1 1 0 0
77 1 -1 1 0
79 0 0 0
83 1 # Scattering functions
85 3 6
86 # NEW OLD i n 1
87 1 -64 0 1 0 0
88 1 64 0 -1 0 63
89 0 0 1 -1 0 0
92 NEW OLD
94 #the output of CLooG is like this:
95 #$ cloog ./albert_strip_mine.cloog
96 # for (NEW=0;NEW<=floord(n,64);NEW++) {
97 # for (OLD=max(64*NEW,0);OLD<=min(64*NEW+63,n);OLD++) {
98 # S1(i = OLD) ;
99 # }
103 static void
104 pbb_strip_mine_time_depth (poly_bb_p pbb, int time_depth, int stride)
106 isl_space *d;
107 isl_constraint *c;
108 int iter, strip;
109 /* STRIP is the dimension that iterates with stride STRIDE. */
110 /* ITER is the dimension that enumerates single iterations inside
111 one strip that has at most STRIDE iterations. */
112 strip = time_depth;
113 iter = strip + 2;
115 pbb->transformed = isl_map_insert_dims (pbb->transformed, isl_dim_out,
116 strip, 2);
118 /* Lower bound of the striped loop. */
119 d = isl_map_get_space (pbb->transformed);
120 c = isl_inequality_alloc (isl_local_space_from_space (d));
121 c = isl_constraint_set_coefficient_si (c, isl_dim_out, strip, -stride);
122 c = isl_constraint_set_coefficient_si (c, isl_dim_out, iter, 1);
123 pbb->transformed = isl_map_add_constraint (pbb->transformed, c);
125 /* Upper bound of the striped loop. */
126 d = isl_map_get_space (pbb->transformed);
127 c = isl_inequality_alloc (isl_local_space_from_space (d));
128 c = isl_constraint_set_coefficient_si (c, isl_dim_out, strip, stride);
129 c = isl_constraint_set_coefficient_si (c, isl_dim_out, iter, -1);
130 c = isl_constraint_set_constant_si (c, stride - 1);
131 pbb->transformed = isl_map_add_constraint (pbb->transformed, c);
133 /* Static scheduling for ITER level.
134 This is mandatory to keep the 2d + 1 canonical scheduling format. */
135 d = isl_map_get_space (pbb->transformed);
136 c = isl_equality_alloc (isl_local_space_from_space (d));
137 c = isl_constraint_set_coefficient_si (c, isl_dim_out, strip + 1, 1);
138 pbb->transformed = isl_map_add_constraint (pbb->transformed, c);
141 /* Returns true when strip mining with STRIDE of the loop LST is
142 profitable. */
144 static bool
145 lst_strip_mine_profitable_p (lst_p lst, int stride)
147 mpz_t niter, strip_stride;
148 bool res;
150 gcc_assert (LST_LOOP_P (lst));
151 mpz_init (strip_stride);
152 mpz_init (niter);
154 mpz_set_si (strip_stride, stride);
155 lst_niter_for_loop (lst, niter);
156 res = (mpz_cmp (niter, strip_stride) > 0);
158 mpz_clear (strip_stride);
159 mpz_clear (niter);
160 return res;
163 /* Strip-mines all the loops of LST with STRIDE. Return the number of
164 loops strip-mined. */
166 static int
167 lst_do_strip_mine_loop (lst_p lst, int depth, int stride)
169 int i;
170 lst_p l;
171 poly_bb_p pbb;
173 if (!lst)
174 return 0;
176 if (LST_LOOP_P (lst))
178 int res = 0;
180 FOR_EACH_VEC_ELT (LST_SEQ (lst), i, l)
181 res += lst_do_strip_mine_loop (l, depth, stride);
183 return res;
186 pbb = LST_PBB (lst);
187 pbb_strip_mine_time_depth (pbb, psct_dynamic_dim (pbb, depth), stride);
188 return 1;
191 /* Strip-mines all the loops of LST with STRIDE. When STRIDE is zero,
192 read the stride from the PARAM_LOOP_BLOCK_TILE_SIZE. Return the
193 number of strip-mined loops.
195 Strip mining transforms a loop
197 | for (i = 0; i < N; i++)
198 | S (i);
200 into the following loop nest:
202 | for (k = 0; k < N; k += STRIDE)
203 | for (j = 0; j < STRIDE; j++)
204 | S (i = k + j);
207 static int
208 lst_do_strip_mine (lst_p lst, int stride)
210 int i;
211 lst_p l;
212 int res = 0;
213 int depth;
215 if (!stride)
216 stride = PARAM_VALUE (PARAM_LOOP_BLOCK_TILE_SIZE);
218 if (!lst
219 || !LST_LOOP_P (lst))
220 return false;
222 FOR_EACH_VEC_ELT (LST_SEQ (lst), i, l)
223 res += lst_do_strip_mine (l, stride);
225 depth = lst_depth (lst);
226 if (depth >= 0
227 && lst_strip_mine_profitable_p (lst, stride))
229 res += lst_do_strip_mine_loop (lst, lst_depth (lst), stride);
230 lst_add_loop_under_loop (lst);
233 return res;
236 /* Strip mines all the loops in SCOP. Returns the number of
237 strip-mined loops. */
240 scop_do_strip_mine (scop_p scop, int stride)
242 return lst_do_strip_mine (SCOP_TRANSFORMED_SCHEDULE (scop), stride);
245 /* Loop blocks all the loops in SCOP. Returns true when we manage to
246 block some loops. */
248 bool
249 scop_do_block (scop_p scop)
251 store_scattering (scop);
253 /* If we don't strip mine at least two loops, or not interchange
254 loops, the strip mine alone will not be profitable, and the
255 transform is not a loop blocking: so revert the transform. */
256 if (lst_do_strip_mine (SCOP_TRANSFORMED_SCHEDULE (scop), 0) < 2
257 || scop_do_interchange (scop) == 0)
259 restore_scattering (scop);
260 return false;
263 if (dump_file && (dump_flags & TDF_DETAILS))
264 fprintf (dump_file, "SCoP will be loop blocked.\n");
266 return true;
269 #endif