ipa-cp.c (ipcp_cloning_candidate_p): Use opt_for_fn.
[gcc.git] / gcc / graphite-blocking.c
1 /* Heuristics and transform for loop blocking and strip mining on
2 polyhedral representation.
3
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>.
7
8 This file is part of GCC.
9
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.
14
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.
19
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/>. */
23
24 #include "config.h"
25
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
32
33 #include "system.h"
34 #include "coretypes.h"
35 #include "tree.h"
36 #include "predict.h"
37 #include "vec.h"
38 #include "hashtab.h"
39 #include "hash-set.h"
40 #include "machmode.h"
41 #include "tm.h"
42 #include "hard-reg-set.h"
43 #include "input.h"
44 #include "function.h"
45 #include "dominance.h"
46 #include "basic-block.h"
47 #include "tree-ssa-alias.h"
48 #include "internal-fn.h"
49 #include "gimple-expr.h"
50 #include "is-a.h"
51 #include "gimple.h"
52 #include "gimple-iterator.h"
53 #include "tree-ssa-loop.h"
54 #include "dumpfile.h"
55 #include "cfgloop.h"
56 #include "tree-chrec.h"
57 #include "tree-data-ref.h"
58 #include "sese.h"
59
60 #ifdef HAVE_isl
61 #include "graphite-poly.h"
62
63
64 /* Strip mines with a factor STRIDE the scattering (time) dimension
65 around PBB at depth TIME_DEPTH.
66
67 The following example comes from the wiki page:
68 http://gcc.gnu.org/wiki/Graphite/Strip_mine
69
70 The strip mine of a loop with a tile of 64 can be obtained with a
71 scattering function as follows:
72
73 $ cat ./albert_strip_mine.cloog
74 # language: C
75 c
76
77 # parameter {n | n >= 0}
78 1 3
79 # n 1
80 1 1 0
81 1
82 n
83
84 1 # Number of statements:
85
86 1
87 # {i | 0 <= i <= n}
88 2 4
89 # i n 1
90 1 1 0 0
91 1 -1 1 0
92
93 0 0 0
94 1
95 i
96
97 1 # Scattering functions
98
99 3 6
100 # NEW OLD i n 1
101 1 -64 0 1 0 0
102 1 64 0 -1 0 63
103 0 0 1 -1 0 0
104
105 1
106 NEW OLD
107
108 #the output of CLooG is like this:
109 #$ cloog ./albert_strip_mine.cloog
110 # for (NEW=0;NEW<=floord(n,64);NEW++) {
111 # for (OLD=max(64*NEW,0);OLD<=min(64*NEW+63,n);OLD++) {
112 # S1(i = OLD) ;
113 # }
114 # }
115 */
116
117 static void
118 pbb_strip_mine_time_depth (poly_bb_p pbb, int time_depth, int stride)
119 {
120 isl_space *d;
121 isl_constraint *c;
122 int iter, strip;
123 /* STRIP is the dimension that iterates with stride STRIDE. */
124 /* ITER is the dimension that enumerates single iterations inside
125 one strip that has at most STRIDE iterations. */
126 strip = time_depth;
127 iter = strip + 2;
128
129 pbb->transformed = isl_map_insert_dims (pbb->transformed, isl_dim_out,
130 strip, 2);
131
132 /* Lower bound of the striped loop. */
133 d = isl_map_get_space (pbb->transformed);
134 c = isl_inequality_alloc (isl_local_space_from_space (d));
135 c = isl_constraint_set_coefficient_si (c, isl_dim_out, strip, -stride);
136 c = isl_constraint_set_coefficient_si (c, isl_dim_out, iter, 1);
137 pbb->transformed = isl_map_add_constraint (pbb->transformed, c);
138
139 /* Upper bound of the striped loop. */
140 d = isl_map_get_space (pbb->transformed);
141 c = isl_inequality_alloc (isl_local_space_from_space (d));
142 c = isl_constraint_set_coefficient_si (c, isl_dim_out, strip, stride);
143 c = isl_constraint_set_coefficient_si (c, isl_dim_out, iter, -1);
144 c = isl_constraint_set_constant_si (c, stride - 1);
145 pbb->transformed = isl_map_add_constraint (pbb->transformed, c);
146
147 /* Static scheduling for ITER level.
148 This is mandatory to keep the 2d + 1 canonical scheduling format. */
149 d = isl_map_get_space (pbb->transformed);
150 c = isl_equality_alloc (isl_local_space_from_space (d));
151 c = isl_constraint_set_coefficient_si (c, isl_dim_out, strip + 1, 1);
152 pbb->transformed = isl_map_add_constraint (pbb->transformed, c);
153 }
154
155 /* Returns true when strip mining with STRIDE of the loop LST is
156 profitable. */
157
158 static bool
159 lst_strip_mine_profitable_p (lst_p lst, int stride)
160 {
161 mpz_t niter, strip_stride;
162 bool res;
163
164 gcc_assert (LST_LOOP_P (lst));
165 mpz_init (strip_stride);
166 mpz_init (niter);
167
168 mpz_set_si (strip_stride, stride);
169 lst_niter_for_loop (lst, niter);
170 res = (mpz_cmp (niter, strip_stride) > 0);
171
172 mpz_clear (strip_stride);
173 mpz_clear (niter);
174 return res;
175 }
176
177 /* Strip-mines all the loops of LST with STRIDE. Return the number of
178 loops strip-mined. */
179
180 static int
181 lst_do_strip_mine_loop (lst_p lst, int depth, int stride)
182 {
183 int i;
184 lst_p l;
185 poly_bb_p pbb;
186
187 if (!lst)
188 return 0;
189
190 if (LST_LOOP_P (lst))
191 {
192 int res = 0;
193
194 FOR_EACH_VEC_ELT (LST_SEQ (lst), i, l)
195 res += lst_do_strip_mine_loop (l, depth, stride);
196
197 return res;
198 }
199
200 pbb = LST_PBB (lst);
201 pbb_strip_mine_time_depth (pbb, psct_dynamic_dim (pbb, depth), stride);
202 return 1;
203 }
204
205 /* Strip-mines all the loops of LST with STRIDE. When STRIDE is zero,
206 read the stride from the PARAM_LOOP_BLOCK_TILE_SIZE. Return the
207 number of strip-mined loops.
208
209 Strip mining transforms a loop
210
211 | for (i = 0; i < N; i++)
212 | S (i);
213
214 into the following loop nest:
215
216 | for (k = 0; k < N; k += STRIDE)
217 | for (j = 0; j < STRIDE; j++)
218 | S (i = k + j);
219 */
220
221 static int
222 lst_do_strip_mine (lst_p lst, int stride)
223 {
224 int i;
225 lst_p l;
226 int res = 0;
227 int depth;
228
229 if (!stride)
230 stride = PARAM_VALUE (PARAM_LOOP_BLOCK_TILE_SIZE);
231
232 if (!lst
233 || !LST_LOOP_P (lst))
234 return false;
235
236 FOR_EACH_VEC_ELT (LST_SEQ (lst), i, l)
237 res += lst_do_strip_mine (l, stride);
238
239 depth = lst_depth (lst);
240 if (depth >= 0
241 && lst_strip_mine_profitable_p (lst, stride))
242 {
243 res += lst_do_strip_mine_loop (lst, lst_depth (lst), stride);
244 lst_add_loop_under_loop (lst);
245 }
246
247 return res;
248 }
249
250 /* Strip mines all the loops in SCOP. Returns the number of
251 strip-mined loops. */
252
253 int
254 scop_do_strip_mine (scop_p scop, int stride)
255 {
256 return lst_do_strip_mine (SCOP_TRANSFORMED_SCHEDULE (scop), stride);
257 }
258
259 /* Loop blocks all the loops in SCOP. Returns true when we manage to
260 block some loops. */
261
262 bool
263 scop_do_block (scop_p scop)
264 {
265 store_scattering (scop);
266
267 /* If we don't strip mine at least two loops, or not interchange
268 loops, the strip mine alone will not be profitable, and the
269 transform is not a loop blocking: so revert the transform. */
270 if (lst_do_strip_mine (SCOP_TRANSFORMED_SCHEDULE (scop), 0) < 2
271 || scop_do_interchange (scop) == 0)
272 {
273 restore_scattering (scop);
274 return false;
275 }
276
277 if (dump_file && (dump_flags & TDF_DETAILS))
278 fprintf (dump_file, "SCoP will be loop blocked.\n");
279
280 return true;
281 }
282
283 #endif