1 /* Linear Loop transforms
2 Copyright (C) 2003, 2004, 2005, 2007 Free Software Foundation, Inc.
3 Contributed by Daniel Berlin <dberlin@dberlin.org>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
24 #include "coretypes.h"
31 #include "basic-block.h"
32 #include "diagnostic.h"
34 #include "tree-flow.h"
35 #include "tree-dump.h"
40 #include "tree-chrec.h"
41 #include "tree-data-ref.h"
42 #include "tree-scalar-evolution.h"
43 #include "tree-pass.h"
46 /* Linear loop transforms include any composition of interchange,
47 scaling, skewing, and reversal. They are used to change the
48 iteration order of loop nests in order to optimize data locality of
49 traversals, or remove dependences that prevent
50 parallelization/vectorization/etc.
52 TODO: Determine reuse vectors/matrix and use it to determine optimal
53 transform matrix for locality purposes.
54 TODO: Completion of partial transforms. */
56 /* Gather statistics for loop interchange. LOOP is the loop being
57 considered. The first loop in the considered loop nest is
58 FIRST_LOOP, and consequently, the index of the considered loop is
59 obtained by LOOP->DEPTH - FIRST_LOOP->DEPTH
62 - DEPENDENCE_STEPS the sum of all the data dependence distances
65 - NB_DEPS_NOT_CARRIED_BY_LOOP the number of dependence relations
66 for which the loop LOOP is not carrying any dependence,
68 - ACCESS_STRIDES the sum of all the strides in LOOP.
70 Example: for the following loop,
72 | loop_1 runs 1335 times
73 | loop_2 runs 1335 times
74 | A[{{0, +, 1}_1, +, 1335}_2]
75 | B[{{0, +, 1}_1, +, 1335}_2]
80 gather_interchange_stats (in loop_1) will return
81 DEPENDENCE_STEPS = 3002
82 NB_DEPS_NOT_CARRIED_BY_LOOP = 5
83 ACCESS_STRIDES = 10694
85 gather_interchange_stats (in loop_2) will return
86 DEPENDENCE_STEPS = 3000
87 NB_DEPS_NOT_CARRIED_BY_LOOP = 7
92 gather_interchange_stats (VEC (ddr_p
, heap
) *dependence_relations ATTRIBUTE_UNUSED
,
93 VEC (data_reference_p
, heap
) *datarefs ATTRIBUTE_UNUSED
,
94 struct loop
*loop ATTRIBUTE_UNUSED
,
95 struct loop
*first_loop ATTRIBUTE_UNUSED
,
96 unsigned int *dependence_steps ATTRIBUTE_UNUSED
,
97 unsigned int *nb_deps_not_carried_by_loop ATTRIBUTE_UNUSED
,
98 double_int
*access_strides ATTRIBUTE_UNUSED
)
101 struct data_dependence_relation
*ddr
;
102 struct data_reference
*dr
;
104 *dependence_steps
= 0;
105 *nb_deps_not_carried_by_loop
= 0;
106 *access_strides
= double_int_zero
;
108 for (i
= 0; VEC_iterate (ddr_p
, dependence_relations
, i
, ddr
); i
++)
110 /* If we don't know anything about this dependence, or the distance
111 vector is NULL, or there is no dependence, then there is no reuse of
113 if (DDR_ARE_DEPENDENT (ddr
) == chrec_dont_know
114 || DDR_ARE_DEPENDENT (ddr
) == chrec_known
115 || DDR_NUM_DIST_VECTS (ddr
) == 0)
118 for (j
= 0; j
< DDR_NUM_DIST_VECTS (ddr
); j
++)
120 int dist
= DDR_DIST_VECT (ddr
, j
)[loop_depth (loop
) - loop_depth (first_loop
)];
123 (*nb_deps_not_carried_by_loop
) += 1;
126 (*dependence_steps
) += -dist
;
129 (*dependence_steps
) += dist
;
133 /* Compute the access strides. */
134 for (i
= 0; VEC_iterate (data_reference_p
, datarefs
, i
, dr
); i
++)
137 tree ref
= DR_REF (dr
);
138 gimple stmt
= DR_STMT (dr
);
139 struct loop
*stmt_loop
= loop_containing_stmt (stmt
);
140 struct loop
*inner_loop
= first_loop
->inner
;
142 if (inner_loop
!= stmt_loop
143 && !flow_loop_nested_p (inner_loop
, stmt_loop
))
146 for (it
= 0; it
< DR_NUM_DIMENSIONS (dr
);
147 it
++, ref
= TREE_OPERAND (ref
, 0))
149 int num
= am_vector_index_for_loop (DR_ACCESS_MATRIX (dr
), loop
->num
);
150 int istride
= AM_GET_ACCESS_MATRIX_ELEMENT (DR_ACCESS_MATRIX (dr
), it
, num
);
151 tree array_size
= TYPE_SIZE (TREE_TYPE (ref
));
154 if (array_size
== NULL_TREE
155 || TREE_CODE (array_size
) != INTEGER_CST
)
158 dstride
= double_int_mul (tree_to_double_int (array_size
),
159 shwi_to_double_int (istride
));
160 (*access_strides
) = double_int_add (*access_strides
, dstride
);
165 /* Attempt to apply interchange transformations to TRANS to maximize the
166 spatial and temporal locality of the loop.
167 Returns the new transform matrix. The smaller the reuse vector
168 distances in the inner loops, the fewer the cache misses.
169 FIRST_LOOP is the loop->num of the first loop in the analyzed loop
173 static lambda_trans_matrix
174 try_interchange_loops (lambda_trans_matrix trans
,
176 VEC (ddr_p
, heap
) *dependence_relations
,
177 VEC (data_reference_p
, heap
) *datarefs
,
178 struct loop
*first_loop
)
183 unsigned int dependence_steps_i
, dependence_steps_j
;
184 double_int access_strides_i
, access_strides_j
;
185 double_int small
, large
, nb_iter
;
186 double_int l1_cache_size
, l2_cache_size
;
188 unsigned int nb_deps_not_carried_by_i
, nb_deps_not_carried_by_j
;
189 struct data_dependence_relation
*ddr
;
191 if (VEC_length (ddr_p
, dependence_relations
) == 0)
194 /* When there is an unknown relation in the dependence_relations, we
195 know that it is no worth looking at this loop nest: give up. */
196 ddr
= VEC_index (ddr_p
, dependence_relations
, 0);
197 if (ddr
== NULL
|| DDR_ARE_DEPENDENT (ddr
) == chrec_dont_know
)
200 l1_cache_size
= uhwi_to_double_int (L1_CACHE_SIZE
* 1024);
201 l2_cache_size
= uhwi_to_double_int (L2_CACHE_SIZE
* 1024);
203 /* LOOP_I is always the outer loop. */
204 for (loop_j
= first_loop
->inner
;
206 loop_j
= loop_j
->inner
)
207 for (loop_i
= first_loop
;
208 loop_depth (loop_i
) < loop_depth (loop_j
);
209 loop_i
= loop_i
->inner
)
211 gather_interchange_stats (dependence_relations
, datarefs
,
214 &nb_deps_not_carried_by_i
,
216 gather_interchange_stats (dependence_relations
, datarefs
,
219 &nb_deps_not_carried_by_j
,
222 /* Heuristics for loop interchange profitability:
224 0. Don't transform if the smallest stride is larger than
225 the L2 cache, or if the largest stride multiplied by the
226 number of iterations is smaller than the L1 cache.
228 1. (spatial locality) Inner loops should have smallest
231 2. (spatial locality) Inner loops should contain more
232 dependence relations not carried by the loop.
234 3. (temporal locality) Inner loops should have smallest
235 array access strides.
238 cmp
= double_int_ucmp (access_strides_i
, access_strides_j
);
239 small
= cmp
< 0 ? access_strides_i
: access_strides_j
;
240 large
= cmp
< 0 ? access_strides_j
: access_strides_i
;
242 if (double_int_ucmp (small
, l2_cache_size
) > 0)
246 estimated_loop_iterations (loop_j
, false, &nb_iter
):
247 estimated_loop_iterations (loop_i
, false, &nb_iter
);
248 large
= double_int_mul (large
, nb_iter
);
250 if (res
&& double_int_ucmp (large
, l1_cache_size
) < 0)
253 if (dependence_steps_i
< dependence_steps_j
254 || nb_deps_not_carried_by_i
> nb_deps_not_carried_by_j
257 lambda_matrix_row_exchange (LTM_MATRIX (trans
),
258 loop_depth (loop_i
) - loop_depth (first_loop
),
259 loop_depth (loop_j
) - loop_depth (first_loop
));
260 /* Validate the resulting matrix. When the transformation
261 is not valid, reverse to the previous transformation. */
262 if (!lambda_transform_legal_p (trans
, depth
, dependence_relations
))
263 lambda_matrix_row_exchange (LTM_MATRIX (trans
),
264 loop_depth (loop_i
) - loop_depth (first_loop
),
265 loop_depth (loop_j
) - loop_depth (first_loop
));
272 /* Return the number of nested loops in LOOP_NEST, or 0 if the loops
273 are not perfectly nested. */
276 perfect_loop_nest_depth (struct loop
*loop_nest
)
279 unsigned int depth
= 1;
281 /* If it's not a loop nest, we don't want it. We also don't handle
282 sibling loops properly, which are loops of the following form:
284 | for (i = 0; i < 50; i++)
286 | for (j = 0; j < 50; j++)
290 | for (j = 0; j < 50; j++)
297 if (!loop_nest
->inner
|| !single_exit (loop_nest
))
300 for (temp
= loop_nest
->inner
; temp
; temp
= temp
->inner
)
302 /* If we have a sibling loop or multiple exit edges, jump ship. */
303 if (temp
->next
|| !single_exit (temp
))
312 /* Perform a set of linear transforms on loops. */
315 linear_transform_loops (void)
317 bool modified
= false;
319 VEC(tree
,heap
) *oldivs
= NULL
;
320 VEC(tree
,heap
) *invariants
= NULL
;
321 VEC(tree
,heap
) *lambda_parameters
= NULL
;
322 VEC(gimple
,heap
) *remove_ivs
= VEC_alloc (gimple
, heap
, 3);
323 struct loop
*loop_nest
;
327 FOR_EACH_LOOP (li
, loop_nest
, 0)
329 unsigned int depth
= 0;
330 VEC (ddr_p
, heap
) *dependence_relations
;
331 VEC (data_reference_p
, heap
) *datarefs
;
333 lambda_loopnest before
, after
;
334 lambda_trans_matrix trans
;
335 struct obstack lambda_obstack
;
337 VEC(loop_p
,heap
) *nest
= VEC_alloc (loop_p
, heap
, 3);
339 depth
= perfect_loop_nest_depth (loop_nest
);
343 for (loop
= loop_nest
; loop
; loop
= loop
->inner
)
344 VEC_safe_push (loop_p
, heap
, nest
, loop
);
346 gcc_obstack_init (&lambda_obstack
);
347 VEC_truncate (tree
, oldivs
, 0);
348 VEC_truncate (tree
, invariants
, 0);
349 VEC_truncate (tree
, lambda_parameters
, 0);
351 datarefs
= VEC_alloc (data_reference_p
, heap
, 10);
352 dependence_relations
= VEC_alloc (ddr_p
, heap
, 10 * 10);
353 if (!compute_data_dependences_for_loop (loop_nest
, true, &datarefs
,
354 &dependence_relations
))
355 goto free_and_continue
;
357 lambda_collect_parameters (datarefs
, &lambda_parameters
);
358 if (!lambda_compute_access_matrices (datarefs
, lambda_parameters
, nest
))
359 goto free_and_continue
;
361 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
362 dump_ddrs (dump_file
, dependence_relations
);
364 /* Build the transformation matrix. */
365 trans
= lambda_trans_matrix_new (depth
, depth
);
366 lambda_matrix_id (LTM_MATRIX (trans
), depth
);
367 trans
= try_interchange_loops (trans
, depth
, dependence_relations
,
368 datarefs
, loop_nest
);
370 if (lambda_trans_matrix_id_p (trans
))
373 fprintf (dump_file
, "Won't transform loop. Optimal transform is the identity transform\n");
374 goto free_and_continue
;
377 /* Check whether the transformation is legal. */
378 if (!lambda_transform_legal_p (trans
, depth
, dependence_relations
))
381 fprintf (dump_file
, "Can't transform loop, transform is illegal:\n");
382 goto free_and_continue
;
385 before
= gcc_loopnest_to_lambda_loopnest (loop_nest
, &oldivs
,
386 &invariants
, &lambda_obstack
);
389 goto free_and_continue
;
393 fprintf (dump_file
, "Before:\n");
394 print_lambda_loopnest (dump_file
, before
, 'i');
397 after
= lambda_loopnest_transform (before
, trans
, &lambda_obstack
);
401 fprintf (dump_file
, "After:\n");
402 print_lambda_loopnest (dump_file
, after
, 'u');
405 lambda_loopnest_to_gcc_loopnest (loop_nest
, oldivs
, invariants
,
407 after
, trans
, &lambda_obstack
);
411 fprintf (dump_file
, "Successfully transformed loop.\n");
414 obstack_free (&lambda_obstack
, NULL
);
415 free_dependence_relations (dependence_relations
);
416 free_data_refs (datarefs
);
417 VEC_free (loop_p
, heap
, nest
);
420 for (i
= 0; VEC_iterate (gimple
, remove_ivs
, i
, oldiv_stmt
); i
++)
421 remove_iv (oldiv_stmt
);
423 VEC_free (tree
, heap
, oldivs
);
424 VEC_free (tree
, heap
, invariants
);
425 VEC_free (gimple
, heap
, remove_ivs
);
429 rewrite_into_loop_closed_ssa (NULL
, TODO_update_ssa_full_phi
);