First commit : 0.14.0 version (with roadmap in doc instead of
[cloog.git] / source / block.c
blobb2a26d23a3bef369a2a19078bde3b59f671822dc
2 /**-------------------------------------------------------------------**
3 ** CLooG **
4 **-------------------------------------------------------------------**
5 ** block.c **
6 **-------------------------------------------------------------------**
7 ** First version: june 11th 2005 **
8 **-------------------------------------------------------------------**/
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 !
39 # include <stdlib.h>
40 # include <stdio.h>
41 # include "../include/cloog/cloog.h"
44 /******************************************************************************
45 * Memory leaks hunting *
46 ******************************************************************************/
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.
62 int cloog_block_allocated = 0 ;
63 int cloog_block_freed = 0 ;
64 int cloog_block_max = 0 ;
67 void cloog_block_leak_up()
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 ;
74 void cloog_block_leak_down()
75 { cloog_block_freed ++ ;
79 /******************************************************************************
80 * Structure display function *
81 ******************************************************************************/
84 /**
85 * cloog_domain_print_structure :
86 * this function is a human-friendly way to display the CloogDomain data
87 * structure, it includes an indentation level (level) in order to work with
88 * others print_structure functions.
89 * - June 16th 2005: first version.
91 void cloog_block_print_structure(FILE * file, CloogBlock * block, int level)
92 { int i ;
94 /* Go to the right level. */
95 for (i=0; i<level; i++)
96 fprintf(file,"|\t") ;
98 if (block != NULL)
99 { fprintf(file,"+-- CloogBlock\n") ;
101 /* A blank line. */
102 for (i=0; i<level+2; i++)
103 fprintf(file,"|\t") ;
104 fprintf(file,"\n") ;
106 /* Print statement list. */
107 cloog_statement_print_structure(file,block->statement,level+1) ;
109 /* A blank line. */
110 for (i=0; i<level+2; i++)
111 fprintf(file,"|\t") ;
112 fprintf(file,"\n") ;
114 /* Print scattering function. */
115 for(i=0; i<level+1; i++)
116 fprintf(file,"|\t") ;
118 if (block->scattering != NULL)
119 { fprintf(file,"+-- Scattering function\n") ;
120 /* Print the matrix. */
121 cloog_matrix_print_structure(file,block->scattering,level+1) ;
123 else
124 fprintf(file,"+-- Null scattering function\n") ;
126 /* A blank line. */
127 for (i=0; i<level+2; i++)
128 fprintf(file,"|\t") ;
129 fprintf(file,"\n") ;
131 /* Print scalar dimensions. */
132 for (i=0; i<level+1; i++)
133 fprintf(file,"|\t") ;
135 if (block->nb_scaldims == 0)
136 fprintf(file,"No scalar dimensions\n") ;
137 else
138 { fprintf(file,"Scalar dimensions (%d):",block->nb_scaldims) ;
139 for (i=0; i<block->nb_scaldims; i++)
140 value_print(file," "VALUE_FMT,block->scaldims[i]) ;
141 fprintf(file,"\n") ;
144 /* A blank line. */
145 for (i=0; i<level+2; i++)
146 fprintf(file,"|\t") ;
147 fprintf(file,"\n") ;
149 /* Print depth. */
150 for (i=0; i<level+1; i++)
151 fprintf(file,"|\t") ;
152 fprintf(file,"Depth: %d\n",block->depth) ;
154 /* A blank line. */
155 for (i=0; i<level+1; i++)
156 fprintf(file,"|\t") ;
157 fprintf(file,"\n") ;
159 else
160 fprintf(file,"+-- Null CloogBlock\n") ;
165 * cloog_block_print function:
166 * This function prints the content of a CloogBlock structure (block) into a
167 * file (file, possibly stdout).
168 * - June 11th 2005: first version.
169 * - June 16th 2005: now just a call to cloog_block_print_structure.
171 void cloog_block_print(FILE * file, CloogBlock * block)
172 { cloog_block_print_structure(file,block,0) ;
177 * cloog_block_list_print function:
178 * This function prints the content of a CloogBlock structure (block) into a
179 * file (file, possibly stdout).
180 * - June 16th 2005: first version.
182 void cloog_block_list_print(FILE * file, CloogBlockList * blocklist)
183 { int i=0 ;
185 while (blocklist != NULL)
186 { fprintf(file,"+-- CloogBlockList node %d\n",i) ;
187 cloog_block_print_structure(file,blocklist->block,1) ;
188 blocklist = blocklist->next ;
189 i++ ;
194 /******************************************************************************
195 * Memory deallocation function *
196 ******************************************************************************/
200 * cloog_block_free function:
201 * This function frees the allocated memory for a CloogStatement structure.
202 * - June 11th 2005: first version.
203 * - June 30th 2005: scaldims field management.
205 void cloog_block_free(CloogBlock * block)
206 { int i ;
208 if (block != NULL)
209 { block->references -- ;
211 if (block->references == 0)
212 { cloog_block_leak_down() ;
213 if (block->scaldims != NULL)
214 { for (i=0;i<block->nb_scaldims;i++)
215 value_clear_c(block->scaldims[i]) ;
217 free(block->scaldims) ;
219 cloog_statement_free(block->statement) ;
220 free(block) ;
227 * cloog_block_list_free function:
228 * This function frees the allocated memory for a CloogBlockList structure.
229 * - June 11th 2005: first version.
231 void cloog_block_list_free(CloogBlockList * blocklist)
232 { CloogBlockList * temp ;
234 while (blocklist != NULL)
235 { temp = blocklist->next ;
236 cloog_block_free(blocklist->block) ;
237 free(blocklist) ;
238 blocklist = temp ;
243 /******************************************************************************
244 * Processing functions *
245 ******************************************************************************/
248 * cloog_block_malloc function:
249 * This function allocates the memory space for a CloogBlock structure and
250 * sets its fields with default values. Then it returns a pointer to the
251 * allocated space.
252 * - November 21th 2005: first version.
254 CloogBlock * cloog_block_malloc()
255 { CloogBlock * block ;
257 /* Memory allocation for the CloogBlock structure. */
258 block = (CloogBlock *)malloc(sizeof(CloogBlock)) ;
259 if (block == NULL)
260 { fprintf(stderr, "[CLooG]ERROR: memory overflow.\n") ;
261 exit(1) ;
263 cloog_block_leak_up() ;
265 /* We set the various fields with default values. */
266 block->statement = NULL ;
267 block->scattering = NULL ;
268 block->nb_scaldims = 0 ;
269 block->scaldims = NULL ;
270 block->depth = 0 ;
271 block->references = 1 ;
273 return block ;
278 * cloog_block_alloc function:
279 * This function allocates the memory space for a CloogBlock structure and
280 * sets its fields with those given as input. Then it returns a pointer to the
281 * allocated space. The two parameters nb_scaldims and scaldims are for internal
282 * service, put to respectively 0 and NULL if you don't know what they are
283 * useful for !
284 * - statement is the statement list of the block,
285 * - scattering is the scattering function for the block (NULL if unsure !),
286 * - nb_scaldims is the number of scalar dimensions (0 if unsure !),
287 * - scaldims is the array with the scalar dimensions values (NULL if unsure !),
288 * - depth is the original block depth (the number of outer loops).
290 * - June 11th 2005: first version.
291 * - June 30th 2005: addition of the nb_scaldims and scaldims parameters.
292 * - November 21th 2005: use of cloog_block_malloc.
294 CloogBlock * cloog_block_alloc(statement,scattering,nb_scaldims,scaldims,depth)
295 CloogStatement * statement ;
296 CloogMatrix * scattering ;
297 int nb_scaldims, depth ;
298 Value * scaldims ;
299 { CloogBlock * block ;
301 /* Block allocation. */
302 block = cloog_block_malloc() ;
304 block->statement = statement ;
305 block->scattering = scattering ;
306 block->nb_scaldims = nb_scaldims ;
307 block->scaldims = scaldims ;
308 block->depth = depth ;
309 block->references = 1 ;
311 return block ;
316 * cloog_block_list_malloc function:
317 * This function allocates the memory space for a CloogBlockList structure and
318 * sets its fields with default values. Then it returns a pointer to the
319 * allocated space.
320 * - November 21th 2005: first version.
322 CloogBlockList * cloog_block_list_malloc()
323 { CloogBlockList * blocklist ;
325 /* Memory allocation for the CloogBlock structure. */
326 blocklist = (CloogBlockList *)malloc(sizeof(CloogBlockList)) ;
327 if (blocklist == NULL)
328 { fprintf(stderr, "[CLooG]ERROR: memory overflow.\n") ;
329 exit(1) ;
332 /* We set the various fields with default values. */
333 blocklist->block = NULL ;
334 blocklist->next = NULL ;
336 return blocklist ;
341 * cloog_block_list_alloc function:
342 * This function allocates the memory space for a CloogBlockList structure and
343 * sets its fields with those given as input. Then it returns a pointer to the
344 * allocated space.
345 * - block is the block element of the list node,
347 * - June 11th 2005: first version.
348 * - November 21th 2005: use of cloog_block_list_malloc.
350 CloogBlockList * cloog_block_list_alloc(CloogBlock * block)
351 { CloogBlockList * blocklist ;
353 /* Block list node allocation. */
354 blocklist = cloog_block_list_malloc() ;
356 blocklist->block = block ;
357 blocklist->block->references ++ ; /* The block has a new reference to it. */
358 blocklist->next = NULL ;
360 return blocklist ;
365 * cloog_block_copy function:
366 * This function returns a copy of a CloogBlock structure 'block'. To save
367 * memory this is not a memory copy but we increment a counter of active
368 * references inside the structure, then return a pointer to that structure.
370 CloogBlock * cloog_block_copy(CloogBlock * block)
371 { if (block == NULL)
372 return NULL ;
374 block->references ++ ;
375 return block ;
380 * cloog_block_merge function:
381 * this function adds at the end of the statement list of the block 'block',
382 * the statement list of the block 'merged'. Then the CloogBlock structure
383 * of 'merged' is freed (obviously not its statement list that is now
384 * included in 'block').
385 * - June 11th 2005: first version.
387 void cloog_block_merge(CloogBlock * block, CloogBlock * merged)
388 { CloogStatement * statement ;
390 if ((block == NULL) || (merged == NULL))
391 return ;
393 if (block->statement != NULL)
394 { statement = block->statement ;
396 while (statement->next != NULL)
397 statement = statement->next ;
399 statement->next = merged->statement ;
401 else
402 block->statement = merged->statement ;
404 cloog_block_leak_down() ;
405 free(merged) ;