1 /* Heuristics and transform for loop blocking and strip mining on
2 polyhedral representation.
4 Copyright (C) 2009 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)
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/>. */
25 #include "coretypes.h"
31 #include "basic-block.h"
32 #include "diagnostic.h"
33 #include "tree-flow.h"
35 #include "tree-dump.h"
38 #include "tree-chrec.h"
39 #include "tree-data-ref.h"
40 #include "tree-scalar-evolution.h"
41 #include "tree-pass.h"
43 #include "value-prof.h"
44 #include "pointer-set.h"
49 #include "cloog/cloog.h"
52 #include "graphite-ppl.h"
54 #include "graphite-poly.h"
57 /* Strip mines with a factor STRIDE the scattering (time) dimension
58 around PBB at depth TIME_DEPTH.
60 The following example comes from the wiki page:
61 http://gcc.gnu.org/wiki/Graphite/Strip_mine
63 The strip mine of a loop with a tile of 64 can be obtained with a
64 scattering function as follows:
66 $ cat ./albert_strip_mine.cloog
70 # parameter {n | n >= 0}
77 1 # Number of statements:
90 1 # Scattering functions
101 #the output of CLooG is like this:
102 #$ cloog ./albert_strip_mine.cloog
103 # for (NEW=0;NEW<=floord(n,64);NEW++) {
104 # for (OLD=max(64*NEW,0);OLD<=min(64*NEW+63,n);OLD++) {
111 pbb_strip_mine_time_depth (poly_bb_p pbb
, int time_depth
, int stride
)
113 ppl_dimension_type iter
, dim
, strip
;
114 ppl_Polyhedron_t res
= PBB_TRANSFORMED_SCATTERING (pbb
);
115 /* STRIP is the dimension that iterates with stride STRIDE. */
116 /* ITER is the dimension that enumerates single iterations inside
117 one strip that has at most STRIDE iterations. */
121 psct_add_scattering_dimension (pbb
, strip
);
122 psct_add_scattering_dimension (pbb
, strip
+ 1);
124 ppl_Polyhedron_space_dimension (res
, &dim
);
126 /* Lower bound of the striped loop. */
128 ppl_Constraint_t new_cstr
;
129 ppl_Linear_Expression_t expr
;
131 ppl_new_Linear_Expression_with_dimension (&expr
, dim
);
132 ppl_set_coef (expr
, strip
, -1 * stride
);
133 ppl_set_coef (expr
, iter
, 1);
135 ppl_new_Constraint (&new_cstr
, expr
, PPL_CONSTRAINT_TYPE_GREATER_OR_EQUAL
);
136 ppl_delete_Linear_Expression (expr
);
137 ppl_Polyhedron_add_constraint (res
, new_cstr
);
138 ppl_delete_Constraint (new_cstr
);
141 /* Upper bound of the striped loop. */
143 ppl_Constraint_t new_cstr
;
144 ppl_Linear_Expression_t expr
;
146 ppl_new_Linear_Expression_with_dimension (&expr
, dim
);
147 ppl_set_coef (expr
, strip
, stride
);
148 ppl_set_coef (expr
, iter
, -1);
149 ppl_set_inhomogeneous (expr
, stride
- 1);
151 ppl_new_Constraint (&new_cstr
, expr
, PPL_CONSTRAINT_TYPE_GREATER_OR_EQUAL
);
152 ppl_delete_Linear_Expression (expr
);
153 ppl_Polyhedron_add_constraint (res
, new_cstr
);
154 ppl_delete_Constraint (new_cstr
);
157 /* Static scheduling for ITER level.
158 This is mandatory to keep the 2d + 1 canonical scheduling format. */
160 ppl_Constraint_t new_cstr
;
161 ppl_Linear_Expression_t expr
;
163 ppl_new_Linear_Expression_with_dimension (&expr
, dim
);
164 ppl_set_coef (expr
, strip
+ 1, 1);
165 ppl_set_inhomogeneous (expr
, 0);
167 ppl_new_Constraint (&new_cstr
, expr
, PPL_CONSTRAINT_TYPE_EQUAL
);
168 ppl_delete_Linear_Expression (expr
);
169 ppl_Polyhedron_add_constraint (res
, new_cstr
);
170 ppl_delete_Constraint (new_cstr
);
176 /* Returns true when strip mining with STRIDE of the loop around PBB
177 at scattering time TIME_DEPTH is profitable. */
180 pbb_strip_mine_profitable_p (poly_bb_p pbb
,
181 graphite_dim_t time_depth
,
184 Value niter
, strip_stride
;
187 value_init (strip_stride
);
189 value_set_si (strip_stride
, stride
);
190 pbb_number_of_iterations_at_time (pbb
, time_depth
, niter
);
191 res
= value_gt (niter
, strip_stride
);
192 value_clear (strip_stride
);
198 /* Strip mines all the loops around PBB. Nothing profitable in all this:
199 this is just a driver function. */
202 pbb_do_strip_mine (poly_bb_p pbb
)
204 graphite_dim_t s_dim
;
206 bool transform_done
= false;
208 for (s_dim
= 0; s_dim
< pbb_nb_dynamic_scattering_transform (pbb
); s_dim
++)
209 if (pbb_strip_mine_profitable_p (pbb
, psct_dynamic_dim (pbb
, s_dim
),
212 ppl_dimension_type d
= psct_dynamic_dim (pbb
, s_dim
);
213 transform_done
|= pbb_strip_mine_time_depth (pbb
, d
, stride
);
217 return transform_done
;
220 /* Strip mines all the loops in SCOP. Nothing profitable in all this:
221 this is just a driver function. */
224 scop_do_strip_mine (scop_p scop
)
228 bool transform_done
= false;
230 store_scattering (scop
);
232 for (i
= 0; VEC_iterate (poly_bb_p
, SCOP_BBS (scop
), i
, pbb
); i
++)
233 transform_done
|= pbb_do_strip_mine (pbb
);
238 if (!graphite_legal_transform (scop
))
240 restore_scattering (scop
);