OSDN Git Service

* gcc.doxy: Rename from tree-ssa.doxy.
[pf3gnuchains/gcc-fork.git] / gcc / lambda.h
1 /* Lambda matrix and vector interface.
2    Copyright (C) 2003, 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
3    Contributed by Daniel Berlin <dberlin@dberlin.org>
4
5 This file is part of GCC.
6
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.
11
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.
16
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/>.  */
20
21 #ifndef LAMBDA_H
22 #define LAMBDA_H
23
24 #include "vec.h"
25
26 /* An integer vector.  A vector formally consists of an element of a vector
27    space. A vector space is a set that is closed under vector addition
28    and scalar multiplication.  In this vector space, an element is a list of
29    integers.  */
30 typedef int *lambda_vector;
31
32 DEF_VEC_P(lambda_vector);
33 DEF_VEC_ALLOC_P(lambda_vector,heap);
34
35 /* An integer matrix.  A matrix consists of m vectors of length n (IE
36    all vectors are the same length).  */
37 typedef lambda_vector *lambda_matrix;
38
39 /* A transformation matrix, which is a self-contained ROWSIZE x COLSIZE
40    matrix.  Rather than use floats, we simply keep a single DENOMINATOR that
41    represents the denominator for every element in the matrix.  */
42 typedef struct lambda_trans_matrix_s
43 {
44   lambda_matrix matrix;
45   int rowsize;
46   int colsize;
47   int denominator;
48 } *lambda_trans_matrix;
49 #define LTM_MATRIX(T) ((T)->matrix)
50 #define LTM_ROWSIZE(T) ((T)->rowsize)
51 #define LTM_COLSIZE(T) ((T)->colsize)
52 #define LTM_DENOMINATOR(T) ((T)->denominator)
53
54 /* A vector representing a statement in the body of a loop.
55    The COEFFICIENTS vector contains a coefficient for each induction variable
56    in the loop nest containing the statement.
57    The DENOMINATOR represents the denominator for each coefficient in the
58    COEFFICIENT vector.
59
60    This structure is used during code generation in order to rewrite the old
61    induction variable uses in a statement in terms of the newly created
62    induction variables.  */
63 typedef struct lambda_body_vector_s
64 {
65   lambda_vector coefficients;
66   int size;
67   int denominator;
68 } *lambda_body_vector;
69 #define LBV_COEFFICIENTS(T) ((T)->coefficients)
70 #define LBV_SIZE(T) ((T)->size)
71 #define LBV_DENOMINATOR(T) ((T)->denominator)
72
73 /* Piecewise linear expression.  
74    This structure represents a linear expression with terms for the invariants
75    and induction variables of a loop. 
76    COEFFICIENTS is a vector of coefficients for the induction variables, one
77    per loop in the loop nest.
78    CONSTANT is the constant portion of the linear expression
79    INVARIANT_COEFFICIENTS is a vector of coefficients for the loop invariants,
80    one per invariant.
81    DENOMINATOR is the denominator for all of the coefficients and constants in
82    the expression.  
83    The linear expressions can be linked together using the NEXT field, in
84    order to represent MAX or MIN of a group of linear expressions.  */
85 typedef struct lambda_linear_expression_s
86 {
87   lambda_vector coefficients;
88   int constant;
89   lambda_vector invariant_coefficients;
90   int denominator;
91   struct lambda_linear_expression_s *next;
92 } *lambda_linear_expression;
93
94 #define LLE_COEFFICIENTS(T) ((T)->coefficients)
95 #define LLE_CONSTANT(T) ((T)->constant)
96 #define LLE_INVARIANT_COEFFICIENTS(T) ((T)->invariant_coefficients)
97 #define LLE_DENOMINATOR(T) ((T)->denominator)
98 #define LLE_NEXT(T) ((T)->next)
99
100 lambda_linear_expression lambda_linear_expression_new (int, int);
101 void print_lambda_linear_expression (FILE *, lambda_linear_expression, int,
102                                      int, char);
103
104 /* Loop structure.  Our loop structure consists of a constant representing the
105    STEP of the loop, a set of linear expressions representing the LOWER_BOUND
106    of the loop, a set of linear expressions representing the UPPER_BOUND of
107    the loop, and a set of linear expressions representing the LINEAR_OFFSET of
108    the loop.  The linear offset is a set of linear expressions that are
109    applied to *both* the lower bound, and the upper bound.  */
110 typedef struct lambda_loop_s
111 {
112   lambda_linear_expression lower_bound;
113   lambda_linear_expression upper_bound;
114   lambda_linear_expression linear_offset;
115   int step;
116 } *lambda_loop;
117
118 #define LL_LOWER_BOUND(T) ((T)->lower_bound)
119 #define LL_UPPER_BOUND(T) ((T)->upper_bound)
120 #define LL_LINEAR_OFFSET(T) ((T)->linear_offset)
121 #define LL_STEP(T)   ((T)->step)
122
123 /* Loop nest structure.  
124    The loop nest structure consists of a set of loop structures (defined
125    above) in LOOPS, along with an integer representing the DEPTH of the loop,
126    and an integer representing the number of INVARIANTS in the loop.  Both of
127    these integers are used to size the associated coefficient vectors in the
128    linear expression structures.  */
129 typedef struct lambda_loopnest_s
130 {
131   lambda_loop *loops;
132   int depth;
133   int invariants;
134 } *lambda_loopnest;
135
136 #define LN_LOOPS(T) ((T)->loops)
137 #define LN_DEPTH(T) ((T)->depth)
138 #define LN_INVARIANTS(T) ((T)->invariants)
139
140 lambda_loopnest lambda_loopnest_new (int, int);
141 lambda_loopnest lambda_loopnest_transform (lambda_loopnest, lambda_trans_matrix);
142 struct loop;
143 bool perfect_nest_p (struct loop *);
144 void print_lambda_loopnest (FILE *, lambda_loopnest, char);
145
146 #define lambda_loop_new() (lambda_loop) ggc_alloc_cleared (sizeof (struct lambda_loop_s))
147
148 void print_lambda_loop (FILE *, lambda_loop, int, int, char);
149
150 lambda_matrix lambda_matrix_new (int, int);
151
152 void lambda_matrix_id (lambda_matrix, int);
153 bool lambda_matrix_id_p (lambda_matrix, int);
154 void lambda_matrix_copy (lambda_matrix, lambda_matrix, int, int);
155 void lambda_matrix_negate (lambda_matrix, lambda_matrix, int, int);
156 void lambda_matrix_transpose (lambda_matrix, lambda_matrix, int, int);
157 void lambda_matrix_add (lambda_matrix, lambda_matrix, lambda_matrix, int,
158                         int);
159 void lambda_matrix_add_mc (lambda_matrix, int, lambda_matrix, int,
160                            lambda_matrix, int, int);
161 void lambda_matrix_mult (lambda_matrix, lambda_matrix, lambda_matrix,
162                          int, int, int);
163 void lambda_matrix_delete_rows (lambda_matrix, int, int, int);
164 void lambda_matrix_row_exchange (lambda_matrix, int, int);
165 void lambda_matrix_row_add (lambda_matrix, int, int, int, int);
166 void lambda_matrix_row_negate (lambda_matrix mat, int, int);
167 void lambda_matrix_row_mc (lambda_matrix, int, int, int);
168 void lambda_matrix_col_exchange (lambda_matrix, int, int, int);
169 void lambda_matrix_col_add (lambda_matrix, int, int, int, int);
170 void lambda_matrix_col_negate (lambda_matrix, int, int);
171 void lambda_matrix_col_mc (lambda_matrix, int, int, int);
172 int lambda_matrix_inverse (lambda_matrix, lambda_matrix, int);
173 void lambda_matrix_hermite (lambda_matrix, int, lambda_matrix, lambda_matrix);
174 void lambda_matrix_left_hermite (lambda_matrix, int, int, lambda_matrix, lambda_matrix);
175 void lambda_matrix_right_hermite (lambda_matrix, int, int, lambda_matrix, lambda_matrix);
176 int lambda_matrix_first_nz_vec (lambda_matrix, int, int, int);
177 void lambda_matrix_project_to_null (lambda_matrix, int, int, int, 
178                                     lambda_vector);
179 void print_lambda_matrix (FILE *, lambda_matrix, int, int);
180
181 lambda_trans_matrix lambda_trans_matrix_new (int, int);
182 bool lambda_trans_matrix_nonsingular_p (lambda_trans_matrix);
183 bool lambda_trans_matrix_fullrank_p (lambda_trans_matrix);
184 int lambda_trans_matrix_rank (lambda_trans_matrix);
185 lambda_trans_matrix lambda_trans_matrix_basis (lambda_trans_matrix);
186 lambda_trans_matrix lambda_trans_matrix_padding (lambda_trans_matrix);
187 lambda_trans_matrix lambda_trans_matrix_inverse (lambda_trans_matrix);
188 void print_lambda_trans_matrix (FILE *, lambda_trans_matrix);
189 void lambda_matrix_vector_mult (lambda_matrix, int, int, lambda_vector, 
190                                 lambda_vector);
191 bool lambda_trans_matrix_id_p (lambda_trans_matrix);
192
193 lambda_body_vector lambda_body_vector_new (int);
194 lambda_body_vector lambda_body_vector_compute_new (lambda_trans_matrix, 
195                                                    lambda_body_vector);
196 void print_lambda_body_vector (FILE *, lambda_body_vector);
197 lambda_loopnest gcc_loopnest_to_lambda_loopnest (struct loop *,
198                                                  VEC(tree,heap) **,
199                                                  VEC(tree,heap) **);
200 void lambda_loopnest_to_gcc_loopnest (struct loop *,
201                                       VEC(tree,heap) *, VEC(tree,heap) *,
202                                       lambda_loopnest, lambda_trans_matrix);
203
204
205 static inline void lambda_vector_negate (lambda_vector, lambda_vector, int);
206 static inline void lambda_vector_mult_const (lambda_vector, lambda_vector, int, int);
207 static inline void lambda_vector_add (lambda_vector, lambda_vector,
208                                       lambda_vector, int);
209 static inline void lambda_vector_add_mc (lambda_vector, int, lambda_vector, int,
210                                          lambda_vector, int);
211 static inline void lambda_vector_copy (lambda_vector, lambda_vector, int);
212 static inline bool lambda_vector_zerop (lambda_vector, int);
213 static inline void lambda_vector_clear (lambda_vector, int);
214 static inline bool lambda_vector_equal (lambda_vector, lambda_vector, int);
215 static inline int lambda_vector_min_nz (lambda_vector, int, int);
216 static inline int lambda_vector_first_nz (lambda_vector, int, int);
217 static inline void print_lambda_vector (FILE *, lambda_vector, int);
218
219 /* Allocate a new vector of given SIZE.  */
220
221 static inline lambda_vector
222 lambda_vector_new (int size)
223 {
224   return GGC_CNEWVEC (int, size);
225 }
226
227
228
229 /* Multiply vector VEC1 of length SIZE by a constant CONST1,
230    and store the result in VEC2.  */
231
232 static inline void
233 lambda_vector_mult_const (lambda_vector vec1, lambda_vector vec2,
234                           int size, int const1)
235 {
236   int i;
237
238   if (const1 == 0)
239     lambda_vector_clear (vec2, size);
240   else
241     for (i = 0; i < size; i++)
242       vec2[i] = const1 * vec1[i];
243 }
244
245 /* Negate vector VEC1 with length SIZE and store it in VEC2.  */
246
247 static inline void 
248 lambda_vector_negate (lambda_vector vec1, lambda_vector vec2,
249                       int size)
250 {
251   lambda_vector_mult_const (vec1, vec2, size, -1);
252 }
253
254 /* VEC3 = VEC1+VEC2, where all three the vectors are of length SIZE.  */
255
256 static inline void
257 lambda_vector_add (lambda_vector vec1, lambda_vector vec2,
258                    lambda_vector vec3, int size)
259 {
260   int i;
261   for (i = 0; i < size; i++)
262     vec3[i] = vec1[i] + vec2[i];
263 }
264
265 /* VEC3 = CONSTANT1*VEC1 + CONSTANT2*VEC2.  All vectors have length SIZE.  */
266
267 static inline void
268 lambda_vector_add_mc (lambda_vector vec1, int const1,
269                       lambda_vector vec2, int const2,
270                       lambda_vector vec3, int size)
271 {
272   int i;
273   for (i = 0; i < size; i++)
274     vec3[i] = const1 * vec1[i] + const2 * vec2[i];
275 }
276
277 /* Copy the elements of vector VEC1 with length SIZE to VEC2.  */
278
279 static inline void
280 lambda_vector_copy (lambda_vector vec1, lambda_vector vec2,
281                     int size)
282 {
283   memcpy (vec2, vec1, size * sizeof (*vec1));
284 }
285
286 /* Return true if vector VEC1 of length SIZE is the zero vector.  */
287
288 static inline bool 
289 lambda_vector_zerop (lambda_vector vec1, int size)
290 {
291   int i;
292   for (i = 0; i < size; i++)
293     if (vec1[i] != 0)
294       return false;
295   return true;
296 }
297
298 /* Clear out vector VEC1 of length SIZE.  */
299
300 static inline void
301 lambda_vector_clear (lambda_vector vec1, int size)
302 {
303   memset (vec1, 0, size * sizeof (*vec1));
304 }
305
306 /* Return true if two vectors are equal.  */
307  
308 static inline bool
309 lambda_vector_equal (lambda_vector vec1, lambda_vector vec2, int size)
310 {
311   int i;
312   for (i = 0; i < size; i++)
313     if (vec1[i] != vec2[i])
314       return false;
315   return true;
316 }
317
318 /* Return the minimum nonzero element in vector VEC1 between START and N.
319    We must have START <= N.  */
320
321 static inline int
322 lambda_vector_min_nz (lambda_vector vec1, int n, int start)
323 {
324   int j;
325   int min = -1;
326
327   gcc_assert (start <= n);
328   for (j = start; j < n; j++)
329     {
330       if (vec1[j])
331         if (min < 0 || vec1[j] < vec1[min])
332           min = j;
333     }
334   gcc_assert (min >= 0);
335
336   return min;
337 }
338
339 /* Return the first nonzero element of vector VEC1 between START and N.
340    We must have START <= N.   Returns N if VEC1 is the zero vector.  */
341
342 static inline int
343 lambda_vector_first_nz (lambda_vector vec1, int n, int start)
344 {
345   int j = start;
346   while (j < n && vec1[j] == 0)
347     j++;
348   return j;
349 }
350
351
352 /* Multiply a vector by a matrix.  */
353
354 static inline void
355 lambda_vector_matrix_mult (lambda_vector vect, int m, lambda_matrix mat, 
356                            int n, lambda_vector dest)
357 {
358   int i, j;
359   lambda_vector_clear (dest, n);
360   for (i = 0; i < n; i++)
361     for (j = 0; j < m; j++)
362       dest[i] += mat[j][i] * vect[j];
363 }
364
365
366 /* Print out a vector VEC of length N to OUTFILE.  */
367
368 static inline void
369 print_lambda_vector (FILE * outfile, lambda_vector vector, int n)
370 {
371   int i;
372
373   for (i = 0; i < n; i++)
374     fprintf (outfile, "%3d ", vector[i]);
375   fprintf (outfile, "\n");
376 }
377
378 /* Compute the greatest common divisor of two numbers using
379    Euclid's algorithm.  */
380
381 static inline int 
382 gcd (int a, int b)
383 {
384   int x, y, z;
385
386   x = abs (a);
387   y = abs (b);
388
389   while (x > 0)
390     {
391       z = y % x;
392       y = x;
393       x = z;
394     }
395
396   return y;
397 }
398
399 /* Compute the greatest common divisor of a VECTOR of SIZE numbers.  */
400
401 static inline int
402 lambda_vector_gcd (lambda_vector vector, int size)
403 {
404   int i;
405   int gcd1 = 0;
406
407   if (size > 0)
408     {
409       gcd1 = vector[0];
410       for (i = 1; i < size; i++)
411         gcd1 = gcd (gcd1, vector[i]);
412     }
413   return gcd1;
414 }
415
416 /* Returns true when the vector V is lexicographically positive, in
417    other words, when the first nonzero element is positive.  */
418
419 static inline bool
420 lambda_vector_lexico_pos (lambda_vector v, 
421                           unsigned n)
422 {
423   unsigned i;
424   for (i = 0; i < n; i++)
425     {
426       if (v[i] == 0)
427         continue;
428       if (v[i] < 0)
429         return false;
430       if (v[i] > 0)
431         return true;
432     }
433   return true;
434 }
435
436 /* Given a vector of induction variables IVS, and a vector of
437    coefficients COEFS, build a tree that is a linear combination of
438    the induction variables.  */
439
440 static inline tree
441 build_linear_expr (tree type, lambda_vector coefs, VEC (tree, heap) *ivs)
442 {
443   unsigned i;
444   tree iv;
445   tree expr = fold_convert (type, integer_zero_node);
446
447   for (i = 0; VEC_iterate (tree, ivs, i, iv); i++)
448     {
449       int k = coefs[i];
450
451       if (k == 1)
452         expr = fold_build2 (PLUS_EXPR, type, expr, iv);
453
454       else if (k != 0)
455         expr = fold_build2 (PLUS_EXPR, type, expr,
456                             fold_build2 (MULT_EXPR, type, iv,
457                                          build_int_cst (type, k)));
458     }
459
460   return expr;
461 }
462
463 #endif /* LAMBDA_H  */
464