control_flow_insn_p takes an insn
[official-gcc.git] / gcc / graphite-blocking.c
blob2e83fea4c7b7fffa4940411d3512b55993021bbc
1 /* Heuristics and transform for loop blocking and strip mining on
2 polyhedral representation.
4 Copyright (C) 2009-2014 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 #ifdef HAVE_cloog
32 #include <cloog/cloog.h>
33 #include <cloog/isl/domain.h>
34 #endif
35 #endif
37 #include "system.h"
38 #include "coretypes.h"
39 #include "tree.h"
40 #include "basic-block.h"
41 #include "tree-ssa-alias.h"
42 #include "internal-fn.h"
43 #include "gimple-expr.h"
44 #include "is-a.h"
45 #include "gimple.h"
46 #include "gimple-iterator.h"
47 #include "tree-ssa-loop.h"
48 #include "dumpfile.h"
49 #include "cfgloop.h"
50 #include "tree-chrec.h"
51 #include "tree-data-ref.h"
52 #include "sese.h"
54 #ifdef HAVE_isl
55 #include "graphite-poly.h"
58 /* Strip mines with a factor STRIDE the scattering (time) dimension
59 around PBB at depth TIME_DEPTH.
61 The following example comes from the wiki page:
62 http://gcc.gnu.org/wiki/Graphite/Strip_mine
64 The strip mine of a loop with a tile of 64 can be obtained with a
65 scattering function as follows:
67 $ cat ./albert_strip_mine.cloog
68 # language: C
71 # parameter {n | n >= 0}
72 1 3
73 # n 1
74 1 1 0
78 1 # Number of statements:
81 # {i | 0 <= i <= n}
82 2 4
83 # i n 1
84 1 1 0 0
85 1 -1 1 0
87 0 0 0
91 1 # Scattering functions
93 3 6
94 # NEW OLD i n 1
95 1 -64 0 1 0 0
96 1 64 0 -1 0 63
97 0 0 1 -1 0 0
100 NEW OLD
102 #the output of CLooG is like this:
103 #$ cloog ./albert_strip_mine.cloog
104 # for (NEW=0;NEW<=floord(n,64);NEW++) {
105 # for (OLD=max(64*NEW,0);OLD<=min(64*NEW+63,n);OLD++) {
106 # S1(i = OLD) ;
111 static void
112 pbb_strip_mine_time_depth (poly_bb_p pbb, int time_depth, int stride)
114 isl_space *d;
115 isl_constraint *c;
116 int iter, strip;
117 /* STRIP is the dimension that iterates with stride STRIDE. */
118 /* ITER is the dimension that enumerates single iterations inside
119 one strip that has at most STRIDE iterations. */
120 strip = time_depth;
121 iter = strip + 2;
123 pbb->transformed = isl_map_insert_dims (pbb->transformed, isl_dim_out,
124 strip, 2);
126 /* Lower 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 pbb->transformed = isl_map_add_constraint (pbb->transformed, c);
133 /* Upper 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 c = isl_constraint_set_constant_si (c, stride - 1);
139 pbb->transformed = isl_map_add_constraint (pbb->transformed, c);
141 /* Static scheduling for ITER level.
142 This is mandatory to keep the 2d + 1 canonical scheduling format. */
143 d = isl_map_get_space (pbb->transformed);
144 c = isl_equality_alloc (isl_local_space_from_space (d));
145 c = isl_constraint_set_coefficient_si (c, isl_dim_out, strip + 1, 1);
146 pbb->transformed = isl_map_add_constraint (pbb->transformed, c);
149 /* Returns true when strip mining with STRIDE of the loop LST is
150 profitable. */
152 static bool
153 lst_strip_mine_profitable_p (lst_p lst, int stride)
155 mpz_t niter, strip_stride;
156 bool res;
158 gcc_assert (LST_LOOP_P (lst));
159 mpz_init (strip_stride);
160 mpz_init (niter);
162 mpz_set_si (strip_stride, stride);
163 lst_niter_for_loop (lst, niter);
164 res = (mpz_cmp (niter, strip_stride) > 0);
166 mpz_clear (strip_stride);
167 mpz_clear (niter);
168 return res;
171 /* Strip-mines all the loops of LST with STRIDE. Return the number of
172 loops strip-mined. */
174 static int
175 lst_do_strip_mine_loop (lst_p lst, int depth, int stride)
177 int i;
178 lst_p l;
179 poly_bb_p pbb;
181 if (!lst)
182 return 0;
184 if (LST_LOOP_P (lst))
186 int res = 0;
188 FOR_EACH_VEC_ELT (LST_SEQ (lst), i, l)
189 res += lst_do_strip_mine_loop (l, depth, stride);
191 return res;
194 pbb = LST_PBB (lst);
195 pbb_strip_mine_time_depth (pbb, psct_dynamic_dim (pbb, depth), stride);
196 return 1;
199 /* Strip-mines all the loops of LST with STRIDE. When STRIDE is zero,
200 read the stride from the PARAM_LOOP_BLOCK_TILE_SIZE. Return the
201 number of strip-mined loops.
203 Strip mining transforms a loop
205 | for (i = 0; i < N; i++)
206 | S (i);
208 into the following loop nest:
210 | for (k = 0; k < N; k += STRIDE)
211 | for (j = 0; j < STRIDE; j++)
212 | S (i = k + j);
215 static int
216 lst_do_strip_mine (lst_p lst, int stride)
218 int i;
219 lst_p l;
220 int res = 0;
221 int depth;
223 if (!stride)
224 stride = PARAM_VALUE (PARAM_LOOP_BLOCK_TILE_SIZE);
226 if (!lst
227 || !LST_LOOP_P (lst))
228 return false;
230 FOR_EACH_VEC_ELT (LST_SEQ (lst), i, l)
231 res += lst_do_strip_mine (l, stride);
233 depth = lst_depth (lst);
234 if (depth >= 0
235 && lst_strip_mine_profitable_p (lst, stride))
237 res += lst_do_strip_mine_loop (lst, lst_depth (lst), stride);
238 lst_add_loop_under_loop (lst);
241 return res;
244 /* Strip mines all the loops in SCOP. Returns the number of
245 strip-mined loops. */
248 scop_do_strip_mine (scop_p scop, int stride)
250 return lst_do_strip_mine (SCOP_TRANSFORMED_SCHEDULE (scop), stride);
253 /* Loop blocks all the loops in SCOP. Returns true when we manage to
254 block some loops. */
256 bool
257 scop_do_block (scop_p scop)
259 store_scattering (scop);
261 /* If we don't strip mine at least two loops, or not interchange
262 loops, the strip mine alone will not be profitable, and the
263 transform is not a loop blocking: so revert the transform. */
264 if (lst_do_strip_mine (SCOP_TRANSFORMED_SCHEDULE (scop), 0) < 2
265 || scop_do_interchange (scop) == 0)
267 restore_scattering (scop);
268 return false;
271 if (dump_file && (dump_flags & TDF_DETAILS))
272 fprintf (dump_file, "SCoP will be loop blocked.\n");
274 return true;
277 #endif