resetting manifest requested domain to floor
[platform/upstream/cloog.git] / source / block.c
1
2    /**-------------------------------------------------------------------**
3     **                              CLooG                                **
4     **-------------------------------------------------------------------**
5     **                             block.c                               **
6     **-------------------------------------------------------------------**
7     **                   First version: june 11th 2005                   **
8     **-------------------------------------------------------------------**/
9
10
11 /******************************************************************************
12  *               CLooG : the Chunky Loop Generator (experimental)             *
13  ******************************************************************************
14  *                                                                            *
15  * Copyright (C) 2001-2005 Cedric Bastoul                                     *
16  *                                                                            *
17  * This is free software; you can redistribute it and/or modify it under the  *
18  * terms of the GNU General Public License as published by the Free Software  *
19  * Foundation; either version 2 of the License, or (at your option) any later *
20  * version.                                                                   *
21  *                                                                            *
22  * This software is distributed in the hope that it will be useful, but       *
23  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY *
24  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License   *
25  * for more details.                                                          *
26  *                                                                            *
27  * You should have received a copy of the GNU General Public License along    *
28  * with software; if not, write to the Free Software Foundation, Inc.,        *
29  * 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA                     *
30  *                                                                            *
31  * CLooG, the Chunky Loop Generator                                           *
32  * Written by Cedric Bastoul, Cedric.Bastoul@inria.fr                         *
33  *                                                                            *
34  ******************************************************************************/
35 /* CAUTION: the english used for comments is probably the worst you ever read,
36  *          please feel free to correct and improve it !
37  */
38
39 # include <stdlib.h>
40 # include <stdio.h>
41 # include "../include/cloog/cloog.h"
42
43
44 /******************************************************************************
45  *                             Memory leaks hunting                           *
46  ******************************************************************************/
47
48
49 /**
50  * These functions and global variables are devoted to memory leaks hunting: we
51  * want to know at each moment how many CloogBlock structures had been allocated
52  * (cloog_block_allocated) and how many had been freed (cloog_block_freed).
53  * Each time a CloogBlock structure is allocated, a call to the function
54  * cloog_block_leak_up() must be carried out, and respectively
55  * cloog_block_leak_down() when a CloogBlock structure is freed. The special
56  * variable cloog_block_max gives the maximal number of CloogBlock structures
57  * simultaneously alive (i.e. allocated and non-freed) in memory.
58  * - June 11th 2005: first version.
59  */
60
61
62 int cloog_block_allocated = 0 ;
63 int cloog_block_freed = 0 ;
64 int cloog_block_max = 0 ;
65
66
67 static void cloog_block_leak_up (void)
68 { cloog_block_allocated ++ ;
69   if ((cloog_block_allocated - cloog_block_freed) > cloog_block_max)
70   cloog_block_max = cloog_block_allocated - cloog_block_freed ;
71 }
72
73
74 static void cloog_block_leak_down (void)
75 { cloog_block_freed ++ ;
76 }
77
78 static inline int cloog_block_references (CloogBlock *b)
79 {
80   return b->_references;
81 }
82
83 static inline void cloog_block_init_references (CloogBlock *b)
84 {
85   b->_references = 1;
86 }
87
88 static inline void cloog_block_inc_references (CloogBlock *b)
89 {
90   b->_references++;
91 }
92
93 static inline void cloog_block_dec_references (CloogBlock *b)
94 {
95   b->_references--;
96 }
97
98
99
100
101 /******************************************************************************
102  *                          Structure display function                        *
103  ******************************************************************************/
104
105
106 /**
107  * cloog_domain_print_structure :
108  * this function is a human-friendly way to display the CloogDomain data
109  * structure, it includes an indentation level (level) in order to work with
110  * others print_structure functions.
111  * - June  16th 2005: first version.
112  */
113 void cloog_block_print_structure(FILE * file, CloogBlock * block, int level)
114 { int i ;
115   
116   /* Go to the right level. */
117   for (i=0; i<level; i++)
118   fprintf(file,"|\t") ;
119     
120   if (block != NULL)
121   { fprintf(file,"+-- CloogBlock\n") ;
122    
123     /* A blank line. */
124     for (i=0; i<level+2; i++)
125     fprintf(file,"|\t") ;
126     fprintf(file,"\n") ;    
127
128     /* Print statement list. */
129     cloog_statement_print_structure(file,cloog_block_stmt (block),level+1) ;
130     
131     /* A blank line. */
132     for (i=0; i<level+2; i++)
133     fprintf(file,"|\t") ;    
134     fprintf(file,"\n") ;    
135   
136     /* A blank line. */
137     for (i=0; i<level+2; i++)
138     fprintf(file,"|\t") ;    
139     fprintf(file,"\n") ;    
140
141     /* Print scalar dimensions. */
142     for (i=0; i<level+1; i++)
143     fprintf(file,"|\t") ;
144     
145     if (cloog_block_nb_scaldims (block) == 0)
146     fprintf(file,"No scalar dimensions\n") ;
147     else
148       {
149         fprintf (file, "Scalar dimensions (%d):", cloog_block_nb_scaldims (block));
150         for (i = 0; i < cloog_block_nb_scaldims (block); i++)
151           value_print (file, " "VALUE_FMT, block->scaldims[i]);
152         fprintf (file, "\n");
153       }
154     
155     /* A blank line. */
156     for (i=0; i<level+2; i++)
157     fprintf(file,"|\t") ;    
158     fprintf(file,"\n") ;    
159
160     /* Print depth. */
161     for (i=0; i<level+1; i++)
162     fprintf(file,"|\t") ;
163     fprintf (file, "Depth: %d\n", cloog_block_depth (block));
164     
165     /* A blank line. */
166     for (i=0; i<level+1; i++)
167     fprintf(file,"|\t") ;   
168     fprintf(file,"\n") ;    
169   }
170   else
171   fprintf(file,"+-- Null CloogBlock\n") ;
172 }
173
174
175 /**
176  * cloog_block_print function:
177  * This function prints the content of a CloogBlock structure (block) into a
178  * file (file, possibly stdout).
179  * - June 11th 2005: first version.
180  * - June  16th 2005: now just a call to cloog_block_print_structure.
181  */
182 void cloog_block_print(FILE * file, CloogBlock * block)
183 { cloog_block_print_structure(file,block,0) ;  
184 }
185
186
187 /**
188  * cloog_block_list_print function:
189  * This function prints the content of a CloogBlock structure (block) into a
190  * file (file, possibly stdout).
191  * - June 16th 2005: first version.
192  */
193 void cloog_block_list_print(FILE * file, CloogBlockList * blocklist)
194 { int i=0 ;
195   
196   while (blocklist != NULL)
197     {
198       fprintf(file,"+-- CloogBlockList node %d\n",i) ;
199       cloog_block_print_structure (file, cloog_block_list_block (blocklist), 1);
200       blocklist = cloog_block_list_next (blocklist);
201       i++ ;
202     }
203 }
204
205
206 /******************************************************************************
207  *                         Memory deallocation function                       *
208  ******************************************************************************/
209
210
211 /**
212  * cloog_block_free function:
213  * This function frees the allocated memory for a CloogStatement structure.
214  * - June 11th 2005: first version.
215  * - June 30th 2005: scaldims field management.
216  */
217 void cloog_block_free(CloogBlock * block)
218 { int i ;
219
220   if (block != NULL)
221     {
222       cloog_block_dec_references (block);
223     
224       if (cloog_block_references (block) == 0)
225         { cloog_block_leak_down() ;
226           if (cloog_block_scaldims (block))
227             {
228               for (i = 0; i < cloog_block_nb_scaldims (block); i++)
229                 value_clear_c (block->scaldims[i]);
230       
231               free (cloog_block_scaldims (block)) ;
232             }
233           cloog_statement_free(cloog_block_stmt (block)) ;
234           free(block) ;
235         }
236     }
237 }
238
239
240 /**
241  * cloog_block_list_free function:
242  * This function frees the allocated memory for a CloogBlockList structure.
243  * - June 11th 2005: first version.
244  */
245 void cloog_block_list_free(CloogBlockList * blocklist)
246 { CloogBlockList * temp ;
247   
248   while (blocklist != NULL)
249     {
250       temp = cloog_block_list_next (blocklist);
251       cloog_block_free (cloog_block_list_block (blocklist));
252       free(blocklist) ;
253       blocklist = temp ;
254     }
255 }
256
257
258 /******************************************************************************
259  *                            Processing functions                            *
260  ******************************************************************************/
261
262 /**
263  * cloog_block_malloc function:
264  * This function allocates the memory space for a CloogBlock structure and
265  * sets its fields with default values. Then it returns a pointer to the
266  * allocated space.
267  * - November 21th 2005: first version.
268  */
269 CloogBlock * cloog_block_malloc (void)
270 { CloogBlock * block ;
271   
272   /* Memory allocation for the CloogBlock structure. */
273   block = (CloogBlock *)malloc(sizeof(CloogBlock)) ;
274   if (block == NULL) 
275   { fprintf(stderr, "[CLooG]ERROR: memory overflow.\n") ;
276     exit(1) ;
277   }
278   cloog_block_leak_up() ;
279   
280   /* We set the various fields with default values. */
281   cloog_block_set_stmt (block, NULL);
282   cloog_block_set_nb_scaldims (block, 0);
283   cloog_block_set_scaldims (block, NULL);
284   cloog_block_set_depth (block, 0);
285   cloog_block_init_references (block);
286   cloog_block_set_usr (block, NULL);
287   
288   return block ;
289 }  
290
291
292 /**
293  * cloog_block_alloc function:
294  * This function allocates the memory space for a CloogBlock structure and
295  * sets its fields with those given as input. Then it returns a pointer to the
296  * allocated space. The two parameters nb_scaldims and scaldims are for internal
297  * service, put to respectively 0 and NULL if you don't know what they are
298  * useful for !
299  * - statement is the statement list of the block,
300  * - nb_scaldims is the number of scalar dimensions (0 if unsure !),
301  * - scaldims is the array with the scalar dimensions values (NULL if unsure !),
302  * - depth is the original block depth (the number of outer loops).
303  **
304  * - June     11th 2005: first version.
305  * - June     30th 2005: addition of the nb_scaldims and scaldims parameters.
306  * - November 21th 2005: use of cloog_block_malloc.
307  */
308 CloogBlock * cloog_block_alloc(CloogStatement * statement, int nb_scaldims,
309                                Value * scaldims, int depth)
310 {
311   CloogBlock * block ;
312     
313   /* Block allocation. */
314   block = cloog_block_malloc() ;
315
316   cloog_block_set_stmt (block, statement);
317   cloog_block_set_nb_scaldims (block, nb_scaldims);
318   cloog_block_set_scaldims (block, scaldims);
319   cloog_block_set_depth (block, depth);
320   cloog_block_init_references (block);
321   
322   return block ;
323 }
324
325
326 /**
327  * cloog_block_list_malloc function:
328  * This function allocates the memory space for a CloogBlockList structure and
329  * sets its fields with default values. Then it returns a pointer to the
330  * allocated space.
331  * - November 21th 2005: first version.
332  */
333 CloogBlockList * cloog_block_list_malloc (void)
334 {
335   CloogBlockList * blocklist ;
336   
337   /* Memory allocation for the CloogBlock structure. */
338   blocklist = (CloogBlockList *)malloc(sizeof(CloogBlockList)) ;
339   if (blocklist == NULL) 
340   { fprintf(stderr, "[CLooG]ERROR: memory overflow.\n") ;
341     exit(1) ;
342   }
343   
344   /* We set the various fields with default values. */
345   cloog_block_list_set_block (blocklist, NULL);
346   cloog_block_list_set_next (blocklist, NULL);
347   
348   return blocklist ;
349 }  
350
351
352 /**
353  * cloog_block_list_alloc function:
354  * This function allocates the memory space for a CloogBlockList structure and
355  * sets its fields with those given as input. Then it returns a pointer to the
356  * allocated space.
357  * - block is the block element of the list node,
358  **
359  * - June     11th 2005: first version.
360  * - November 21th 2005: use of cloog_block_list_malloc.
361  */
362 CloogBlockList * cloog_block_list_alloc(CloogBlock * block)
363 { CloogBlockList * blocklist ;
364   
365   /* Block list node allocation. */
366   blocklist = cloog_block_list_malloc() ;
367
368   cloog_block_list_set_block (blocklist, block);
369   cloog_block_inc_references (cloog_block_list_block (blocklist)); /* The block has a new reference to it. */
370   cloog_block_list_set_next (blocklist, NULL);
371   
372   return blocklist ;
373 }
374
375
376 /**
377  * cloog_block_copy function:
378  * This function returns a copy of a CloogBlock structure 'block'. To save
379  * memory this is not a memory copy but we increment a counter of active
380  * references inside the structure, then return a pointer to that structure.
381  */ 
382 CloogBlock * cloog_block_copy(CloogBlock * block)
383 { if (block == NULL)
384   return NULL ;
385
386   cloog_block_inc_references (block);
387   return block ;
388 }
389
390
391 /**
392  * cloog_block_merge function:
393  * this function adds at the end of the statement list of the block 'block',
394  * the statement list of the block 'merged'. Then the  CloogBlock structure
395  * of 'merged' is freed (obviously not its statement list that is now
396  * included in 'block').
397  * - June 11th 2005: first version.
398  */
399 void cloog_block_merge(CloogBlock * block, CloogBlock * merged)
400 { CloogStatement * statement ;
401
402   if ((block == NULL) || (merged == NULL))
403   return ;
404   
405   if (cloog_block_stmt (block))
406     {
407       statement = cloog_block_stmt (block) ;
408     
409       while (cloog_statement_next (statement))
410         statement = cloog_statement_next (statement) ;
411     
412       cloog_statement_set_next (statement, cloog_block_stmt (merged));
413     }
414   else
415     cloog_block_set_stmt (block, cloog_block_stmt (merged));
416
417   cloog_block_leak_down() ;
418   free(merged) ;
419 }
420
421
422
423
424
425
426
427
428
429