Daily bump.
[official-gcc.git] / gcc / tree-loop-linear.c
blob88a77dd228ce31fb822e7759efa48609f3b5030b
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
10 version.
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
15 for more details.
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/>. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "ggc.h"
27 #include "tree.h"
28 #include "target.h"
30 #include "rtl.h"
31 #include "basic-block.h"
32 #include "diagnostic.h"
33 #include "obstack.h"
34 #include "tree-flow.h"
35 #include "tree-dump.h"
36 #include "timevar.h"
37 #include "cfgloop.h"
38 #include "expr.h"
39 #include "optabs.h"
40 #include "tree-chrec.h"
41 #include "tree-data-ref.h"
42 #include "tree-scalar-evolution.h"
43 #include "tree-pass.h"
44 #include "lambda.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
61 Initializes:
62 - DEPENDENCE_STEPS the sum of all the data dependence distances
63 carried by loop LOOP,
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]
76 | endloop_2
77 | A[{0, +, 1336}_1]
78 | endloop_1
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
88 ACCESS_STRIDES = 8010
91 static void
92 gather_interchange_stats (VEC (ddr_p, heap) *dependence_relations,
93 VEC (data_reference_p, heap) *datarefs,
94 struct loop *loop,
95 struct loop *first_loop,
96 unsigned int *dependence_steps,
97 unsigned int *nb_deps_not_carried_by_loop,
98 double_int *access_strides)
100 unsigned int i, j;
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
112 data. */
113 if (DDR_ARE_DEPENDENT (ddr) == chrec_dont_know
114 || DDR_ARE_DEPENDENT (ddr) == chrec_known
115 || DDR_NUM_DIST_VECTS (ddr) == 0)
116 continue;
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)];
122 if (dist == 0)
123 (*nb_deps_not_carried_by_loop) += 1;
125 else if (dist < 0)
126 (*dependence_steps) += -dist;
128 else
129 (*dependence_steps) += dist;
133 /* Compute the access strides. */
134 for (i = 0; VEC_iterate (data_reference_p, datarefs, i, dr); i++)
136 unsigned int it;
137 tree ref = DR_REF (dr);
138 tree 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))
144 continue;
146 for (it = 0; it < DR_NUM_DIMENSIONS (dr);
147 it++, ref = TREE_OPERAND (ref, 0))
149 tree chrec = DR_ACCESS_FN (dr, it);
150 tree tstride = evolution_part_in_loop_num (chrec, loop->num);
151 tree array_size = TYPE_SIZE (TREE_TYPE (ref));
152 double_int dstride;
154 if (tstride == NULL_TREE
155 || array_size == NULL_TREE
156 || TREE_CODE (tstride) != INTEGER_CST
157 || TREE_CODE (array_size) != INTEGER_CST)
158 continue;
160 dstride = double_int_mul (tree_to_double_int (array_size),
161 tree_to_double_int (tstride));
162 (*access_strides) = double_int_add (*access_strides, dstride);
167 /* Attempt to apply interchange transformations to TRANS to maximize the
168 spatial and temporal locality of the loop.
169 Returns the new transform matrix. The smaller the reuse vector
170 distances in the inner loops, the fewer the cache misses.
171 FIRST_LOOP is the loop->num of the first loop in the analyzed loop
172 nest. */
175 static lambda_trans_matrix
176 try_interchange_loops (lambda_trans_matrix trans,
177 unsigned int depth,
178 VEC (ddr_p, heap) *dependence_relations,
179 VEC (data_reference_p, heap) *datarefs,
180 struct loop *first_loop)
182 struct loop *loop_i;
183 struct loop *loop_j;
184 unsigned int dependence_steps_i, dependence_steps_j;
185 double_int access_strides_i, access_strides_j;
186 unsigned int nb_deps_not_carried_by_i, nb_deps_not_carried_by_j;
187 struct data_dependence_relation *ddr;
189 if (VEC_length (ddr_p, dependence_relations) == 0)
190 return trans;
192 /* When there is an unknown relation in the dependence_relations, we
193 know that it is no worth looking at this loop nest: give up. */
194 ddr = VEC_index (ddr_p, dependence_relations, 0);
195 if (ddr == NULL || DDR_ARE_DEPENDENT (ddr) == chrec_dont_know)
196 return trans;
198 /* LOOP_I is always the outer loop. */
199 for (loop_j = first_loop->inner;
200 loop_j;
201 loop_j = loop_j->inner)
202 for (loop_i = first_loop;
203 loop_depth (loop_i) < loop_depth (loop_j);
204 loop_i = loop_i->inner)
206 gather_interchange_stats (dependence_relations, datarefs,
207 loop_i, first_loop,
208 &dependence_steps_i,
209 &nb_deps_not_carried_by_i,
210 &access_strides_i);
211 gather_interchange_stats (dependence_relations, datarefs,
212 loop_j, first_loop,
213 &dependence_steps_j,
214 &nb_deps_not_carried_by_j,
215 &access_strides_j);
217 /* Heuristics for loop interchange profitability:
219 1. (spatial locality) Inner loops should have smallest
220 dependence steps.
222 2. (spatial locality) Inner loops should contain more
223 dependence relations not carried by the loop.
225 3. (temporal locality) Inner loops should have smallest
226 array access strides.
228 if (dependence_steps_i < dependence_steps_j
229 || nb_deps_not_carried_by_i > nb_deps_not_carried_by_j
230 || double_int_ucmp (access_strides_i, access_strides_j) < 0)
232 lambda_matrix_row_exchange (LTM_MATRIX (trans),
233 loop_depth (loop_i) - loop_depth (first_loop),
234 loop_depth (loop_j) - loop_depth (first_loop));
235 /* Validate the resulting matrix. When the transformation
236 is not valid, reverse to the previous transformation. */
237 if (!lambda_transform_legal_p (trans, depth, dependence_relations))
238 lambda_matrix_row_exchange (LTM_MATRIX (trans),
239 loop_depth (loop_i) - loop_depth (first_loop),
240 loop_depth (loop_j) - loop_depth (first_loop));
244 return trans;
247 /* Return the number of nested loops in LOOP_NEST, or 0 if the loops
248 are not perfectly nested. */
250 static unsigned int
251 perfect_loop_nest_depth (struct loop *loop_nest)
253 struct loop *temp;
254 unsigned int depth = 1;
256 /* If it's not a loop nest, we don't want it. We also don't handle
257 sibling loops properly, which are loops of the following form:
259 | for (i = 0; i < 50; i++)
261 | for (j = 0; j < 50; j++)
263 | ...
265 | for (j = 0; j < 50; j++)
267 | ...
272 if (!loop_nest->inner || !single_exit (loop_nest))
273 return 0;
275 for (temp = loop_nest->inner; temp; temp = temp->inner)
277 /* If we have a sibling loop or multiple exit edges, jump ship. */
278 if (temp->next || !single_exit (temp))
279 return 0;
281 depth++;
284 return depth;
287 /* Perform a set of linear transforms on loops. */
289 void
290 linear_transform_loops (void)
292 bool modified = false;
293 loop_iterator li;
294 VEC(tree,heap) *oldivs = NULL;
295 VEC(tree,heap) *invariants = NULL;
296 VEC(tree,heap) *remove_ivs = VEC_alloc (tree, heap, 3);
297 struct loop *loop_nest;
298 tree oldiv_stmt;
299 unsigned i;
301 FOR_EACH_LOOP (li, loop_nest, 0)
303 unsigned int depth = 0;
304 VEC (ddr_p, heap) *dependence_relations;
305 VEC (data_reference_p, heap) *datarefs;
306 lambda_loopnest before, after;
307 lambda_trans_matrix trans;
308 struct obstack lambda_obstack;
309 gcc_obstack_init (&lambda_obstack);
311 depth = perfect_loop_nest_depth (loop_nest);
312 if (depth == 0)
313 continue;
315 VEC_truncate (tree, oldivs, 0);
316 VEC_truncate (tree, invariants, 0);
318 datarefs = VEC_alloc (data_reference_p, heap, 10);
319 dependence_relations = VEC_alloc (ddr_p, heap, 10 * 10);
320 compute_data_dependences_for_loop (loop_nest, true, &datarefs,
321 &dependence_relations);
323 if (dump_file && (dump_flags & TDF_DETAILS))
324 dump_ddrs (dump_file, dependence_relations);
326 /* Build the transformation matrix. */
327 trans = lambda_trans_matrix_new (depth, depth);
328 lambda_matrix_id (LTM_MATRIX (trans), depth);
329 trans = try_interchange_loops (trans, depth, dependence_relations,
330 datarefs, loop_nest);
332 if (lambda_trans_matrix_id_p (trans))
334 if (dump_file)
335 fprintf (dump_file, "Won't transform loop. Optimal transform is the identity transform\n");
336 goto free_and_continue;
339 /* Check whether the transformation is legal. */
340 if (!lambda_transform_legal_p (trans, depth, dependence_relations))
342 if (dump_file)
343 fprintf (dump_file, "Can't transform loop, transform is illegal:\n");
344 goto free_and_continue;
347 before = gcc_loopnest_to_lambda_loopnest (loop_nest, &oldivs,
348 &invariants, &lambda_obstack);
350 if (!before)
351 goto free_and_continue;
353 if (dump_file)
355 fprintf (dump_file, "Before:\n");
356 print_lambda_loopnest (dump_file, before, 'i');
359 after = lambda_loopnest_transform (before, trans, &lambda_obstack);
361 if (dump_file)
363 fprintf (dump_file, "After:\n");
364 print_lambda_loopnest (dump_file, after, 'u');
367 lambda_loopnest_to_gcc_loopnest (loop_nest, oldivs, invariants,
368 &remove_ivs,
369 after, trans, &lambda_obstack);
370 modified = true;
372 if (dump_file)
373 fprintf (dump_file, "Successfully transformed loop.\n");
375 free_and_continue:
376 obstack_free (&lambda_obstack, NULL);
377 free_dependence_relations (dependence_relations);
378 free_data_refs (datarefs);
381 for (i = 0; VEC_iterate (tree, remove_ivs, i, oldiv_stmt); i++)
382 remove_iv (oldiv_stmt);
384 VEC_free (tree, heap, oldivs);
385 VEC_free (tree, heap, invariants);
386 VEC_free (tree, heap, remove_ivs);
387 scev_reset ();
389 if (modified)
390 rewrite_into_loop_closed_ssa (NULL, TODO_update_ssa_full_phi);