CloogBlock: remove unused scattering field
[cloog/uuh.git] / source / block.c
blobfb4fb26502b87406a1c9bf6d527bf5b5031cbab4
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 static 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 static 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 fprintf(file,"+-- Null scattering function\n") ;
120 /* A blank line. */
121 for (i=0; i<level+2; i++)
122 fprintf(file,"|\t") ;
123 fprintf(file,"\n") ;
125 /* Print scalar dimensions. */
126 for (i=0; i<level+1; i++)
127 fprintf(file,"|\t") ;
129 if (block->nb_scaldims == 0)
130 fprintf(file,"No scalar dimensions\n") ;
131 else
132 { fprintf(file,"Scalar dimensions (%d):",block->nb_scaldims) ;
133 for (i=0; i<block->nb_scaldims; i++)
134 value_print(file," "VALUE_FMT,block->scaldims[i]) ;
135 fprintf(file,"\n") ;
138 /* A blank line. */
139 for (i=0; i<level+2; i++)
140 fprintf(file,"|\t") ;
141 fprintf(file,"\n") ;
143 /* Print depth. */
144 for (i=0; i<level+1; i++)
145 fprintf(file,"|\t") ;
146 fprintf(file,"Depth: %d\n",block->depth) ;
148 /* A blank line. */
149 for (i=0; i<level+1; i++)
150 fprintf(file,"|\t") ;
151 fprintf(file,"\n") ;
153 else
154 fprintf(file,"+-- Null CloogBlock\n") ;
159 * cloog_block_print function:
160 * This function prints the content of a CloogBlock structure (block) into a
161 * file (file, possibly stdout).
162 * - June 11th 2005: first version.
163 * - June 16th 2005: now just a call to cloog_block_print_structure.
165 void cloog_block_print(FILE * file, CloogBlock * block)
166 { cloog_block_print_structure(file,block,0) ;
171 * cloog_block_list_print function:
172 * This function prints the content of a CloogBlock structure (block) into a
173 * file (file, possibly stdout).
174 * - June 16th 2005: first version.
176 void cloog_block_list_print(FILE * file, CloogBlockList * blocklist)
177 { int i=0 ;
179 while (blocklist != NULL)
180 { fprintf(file,"+-- CloogBlockList node %d\n",i) ;
181 cloog_block_print_structure(file,blocklist->block,1) ;
182 blocklist = blocklist->next ;
183 i++ ;
188 /******************************************************************************
189 * Memory deallocation function *
190 ******************************************************************************/
194 * cloog_block_free function:
195 * This function frees the allocated memory for a CloogStatement structure.
196 * - June 11th 2005: first version.
197 * - June 30th 2005: scaldims field management.
199 void cloog_block_free(CloogBlock * block)
200 { int i ;
202 if (block != NULL)
203 { block->references -- ;
205 if (block->references == 0)
206 { cloog_block_leak_down() ;
207 if (block->scaldims != NULL)
208 { for (i=0;i<block->nb_scaldims;i++)
209 value_clear_c(block->scaldims[i]) ;
211 free(block->scaldims) ;
213 cloog_statement_free(block->statement) ;
214 free(block) ;
221 * cloog_block_list_free function:
222 * This function frees the allocated memory for a CloogBlockList structure.
223 * - June 11th 2005: first version.
225 void cloog_block_list_free(CloogBlockList * blocklist)
226 { CloogBlockList * temp ;
228 while (blocklist != NULL)
229 { temp = blocklist->next ;
230 cloog_block_free(blocklist->block) ;
231 free(blocklist) ;
232 blocklist = temp ;
237 /******************************************************************************
238 * Processing functions *
239 ******************************************************************************/
242 * cloog_block_malloc function:
243 * This function allocates the memory space for a CloogBlock structure and
244 * sets its fields with default values. Then it returns a pointer to the
245 * allocated space.
246 * - November 21th 2005: first version.
248 CloogBlock * cloog_block_malloc()
249 { CloogBlock * block ;
251 /* Memory allocation for the CloogBlock structure. */
252 block = (CloogBlock *)malloc(sizeof(CloogBlock)) ;
253 if (block == NULL)
254 { fprintf(stderr, "[CLooG]ERROR: memory overflow.\n") ;
255 exit(1) ;
257 cloog_block_leak_up() ;
259 /* We set the various fields with default values. */
260 block->statement = NULL ;
261 block->nb_scaldims = 0 ;
262 block->scaldims = NULL ;
263 block->depth = 0 ;
264 block->references = 1 ;
265 block->usr = NULL;
267 return block ;
272 * cloog_block_alloc function:
273 * This function allocates the memory space for a CloogBlock structure and
274 * sets its fields with those given as input. Then it returns a pointer to the
275 * allocated space. The two parameters nb_scaldims and scaldims are for internal
276 * service, put to respectively 0 and NULL if you don't know what they are
277 * useful for !
278 * - statement is the statement list of the block,
279 * - scattering is the scattering function for the block (NULL if unsure !),
280 * - nb_scaldims is the number of scalar dimensions (0 if unsure !),
281 * - scaldims is the array with the scalar dimensions values (NULL if unsure !),
282 * - depth is the original block depth (the number of outer loops).
284 * - June 11th 2005: first version.
285 * - June 30th 2005: addition of the nb_scaldims and scaldims parameters.
286 * - November 21th 2005: use of cloog_block_malloc.
288 CloogBlock *cloog_block_alloc(CloogStatement *statement, int nb_scaldims,
289 Value *scaldims, int depth)
290 { CloogBlock * block ;
292 /* Block allocation. */
293 block = cloog_block_malloc() ;
295 block->statement = statement ;
296 block->nb_scaldims = nb_scaldims ;
297 block->scaldims = scaldims ;
298 block->depth = depth ;
299 block->references = 1 ;
301 return block ;
306 * cloog_block_list_malloc function:
307 * This function allocates the memory space for a CloogBlockList structure and
308 * sets its fields with default values. Then it returns a pointer to the
309 * allocated space.
310 * - November 21th 2005: first version.
312 CloogBlockList * cloog_block_list_malloc()
313 { CloogBlockList * blocklist ;
315 /* Memory allocation for the CloogBlock structure. */
316 blocklist = (CloogBlockList *)malloc(sizeof(CloogBlockList)) ;
317 if (blocklist == NULL)
318 { fprintf(stderr, "[CLooG]ERROR: memory overflow.\n") ;
319 exit(1) ;
322 /* We set the various fields with default values. */
323 blocklist->block = NULL ;
324 blocklist->next = NULL ;
326 return blocklist ;
331 * cloog_block_list_alloc function:
332 * This function allocates the memory space for a CloogBlockList structure and
333 * sets its fields with those given as input. Then it returns a pointer to the
334 * allocated space.
335 * - block is the block element of the list node,
337 * - June 11th 2005: first version.
338 * - November 21th 2005: use of cloog_block_list_malloc.
340 CloogBlockList * cloog_block_list_alloc(CloogBlock * block)
341 { CloogBlockList * blocklist ;
343 /* Block list node allocation. */
344 blocklist = cloog_block_list_malloc() ;
346 blocklist->block = block ;
347 blocklist->block->references ++ ; /* The block has a new reference to it. */
348 blocklist->next = NULL ;
350 return blocklist ;
355 * cloog_block_copy function:
356 * This function returns a copy of a CloogBlock structure 'block'. To save
357 * memory this is not a memory copy but we increment a counter of active
358 * references inside the structure, then return a pointer to that structure.
360 CloogBlock * cloog_block_copy(CloogBlock * block)
361 { if (block == NULL)
362 return NULL ;
364 block->references ++ ;
365 return block ;
370 * cloog_block_merge function:
371 * this function adds at the end of the statement list of the block 'block',
372 * the statement list of the block 'merged'. Then the CloogBlock structure
373 * of 'merged' is freed (obviously not its statement list that is now
374 * included in 'block').
375 * - June 11th 2005: first version.
377 void cloog_block_merge(CloogBlock * block, CloogBlock * merged)
378 { CloogStatement * statement ;
380 if ((block == NULL) || (merged == NULL))
381 return ;
383 if (block->statement != NULL)
384 { statement = block->statement ;
386 while (statement->next != NULL)
387 statement = statement->next ;
389 statement->next = merged->statement ;
391 else
392 block->statement = merged->statement ;
394 cloog_block_leak_down() ;
395 free(merged) ;