OSDN Git Service

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