cloog_domain_dimension: return actual dimension (i.e., without parameters)
[cloog.git] / source / block.c
blob092d465b5dc673382b26b39b70709b19816d23c4
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 library is free software; you can redistribute it and/or *
18 * modify it under the terms of the GNU Lesser General Public *
19 * License as published by the Free Software Foundation; either *
20 * version 2.1 of the License, or (at your option) any later version. *
21 * *
22 * This library is distributed in the hope that it will be useful, *
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
25 * Lesser General Public License for more details. *
26 * *
27 * You should have received a copy of the GNU Lesser General Public *
28 * License along with this library; if not, write to the Free Software *
29 * Foundation, Inc., 51 Franklin Street, Fifth Floor, *
30 * Boston, MA 02110-1301 USA *
31 * *
32 * CLooG, the Chunky Loop Generator *
33 * Written by Cedric Bastoul, Cedric.Bastoul@inria.fr *
34 * *
35 ******************************************************************************/
36 /* CAUTION: the english used for comments is probably the worst you ever read,
37 * please feel free to correct and improve it !
40 # include <stdlib.h>
41 # include <stdio.h>
42 # include "../include/cloog/cloog.h"
45 /******************************************************************************
46 * Memory leaks hunting *
47 ******************************************************************************/
50 /**
51 * These functions and global variables are devoted to memory leaks hunting: we
52 * want to know at each moment how many CloogBlock structures had been allocated
53 * (cloog_block_allocated) and how many had been freed (cloog_block_freed).
54 * Each time a CloogBlock structure is allocated, a call to the function
55 * cloog_block_leak_up() must be carried out, and respectively
56 * cloog_block_leak_down() when a CloogBlock structure is freed. The special
57 * variable cloog_block_max gives the maximal number of CloogBlock structures
58 * simultaneously alive (i.e. allocated and non-freed) in memory.
59 * - June 11th 2005: first version.
63 int cloog_block_allocated = 0 ;
64 int cloog_block_freed = 0 ;
65 int cloog_block_max = 0 ;
68 static void cloog_block_leak_up()
69 { cloog_block_allocated ++ ;
70 if ((cloog_block_allocated - cloog_block_freed) > cloog_block_max)
71 cloog_block_max = cloog_block_allocated - cloog_block_freed ;
75 static void cloog_block_leak_down()
76 { cloog_block_freed ++ ;
80 /******************************************************************************
81 * Structure display function *
82 ******************************************************************************/
85 /**
86 * cloog_domain_print_structure :
87 * this function is a human-friendly way to display the CloogDomain data
88 * structure, it includes an indentation level (level) in order to work with
89 * others print_structure functions.
90 * - June 16th 2005: first version.
92 void cloog_block_print_structure(FILE * file, CloogBlock * block, int level)
93 { int i ;
95 /* Go to the right level. */
96 for (i=0; i<level; i++)
97 fprintf(file,"|\t") ;
99 if (block != NULL)
100 { fprintf(file,"+-- CloogBlock\n") ;
102 /* A blank line. */
103 for (i=0; i<level+2; i++)
104 fprintf(file,"|\t") ;
105 fprintf(file,"\n") ;
107 /* Print statement list. */
108 cloog_statement_print_structure(file,block->statement,level+1) ;
110 /* A blank line. */
111 for (i=0; i<level+2; i++)
112 fprintf(file,"|\t") ;
113 fprintf(file,"\n") ;
115 /* Print scattering function. */
116 for(i=0; i<level+1; i++)
117 fprintf(file,"|\t") ;
119 fprintf(file,"+-- Null scattering function\n") ;
121 /* A blank line. */
122 for (i=0; i<level+2; i++)
123 fprintf(file,"|\t") ;
124 fprintf(file,"\n") ;
126 /* Print scalar dimensions. */
127 for (i=0; i<level+1; i++)
128 fprintf(file,"|\t") ;
130 if (block->nb_scaldims == 0)
131 fprintf(file,"No scalar dimensions\n") ;
132 else
133 { fprintf(file,"Scalar dimensions (%d):",block->nb_scaldims) ;
134 for (i = 0; i < block->nb_scaldims; i++) {
135 fprintf(file, " ");
136 cloog_int_print(file, block->scaldims[i]);
138 fprintf(file,"\n") ;
141 /* A blank line. */
142 for (i=0; i<level+2; i++)
143 fprintf(file,"|\t") ;
144 fprintf(file,"\n") ;
146 /* Print depth. */
147 for (i=0; i<level+1; i++)
148 fprintf(file,"|\t") ;
149 fprintf(file,"Depth: %d\n",block->depth) ;
151 /* A blank line. */
152 for (i=0; i<level+1; i++)
153 fprintf(file,"|\t") ;
154 fprintf(file,"\n") ;
156 else
157 fprintf(file,"+-- Null CloogBlock\n") ;
162 * cloog_block_print function:
163 * This function prints the content of a CloogBlock structure (block) into a
164 * file (file, possibly stdout).
165 * - June 11th 2005: first version.
166 * - June 16th 2005: now just a call to cloog_block_print_structure.
168 void cloog_block_print(FILE * file, CloogBlock * block)
169 { cloog_block_print_structure(file,block,0) ;
174 * cloog_block_list_print function:
175 * This function prints the content of a CloogBlock structure (block) into a
176 * file (file, possibly stdout).
177 * - June 16th 2005: first version.
179 void cloog_block_list_print(FILE * file, CloogBlockList * blocklist)
180 { int i=0 ;
182 while (blocklist != NULL)
183 { fprintf(file,"+-- CloogBlockList node %d\n",i) ;
184 cloog_block_print_structure(file,blocklist->block,1) ;
185 blocklist = blocklist->next ;
186 i++ ;
191 /******************************************************************************
192 * Memory deallocation function *
193 ******************************************************************************/
197 * cloog_block_free function:
198 * This function frees the allocated memory for a CloogStatement structure.
199 * - June 11th 2005: first version.
200 * - June 30th 2005: scaldims field management.
202 void cloog_block_free(CloogBlock * block)
203 { int i ;
205 if (block != NULL)
206 { block->references -- ;
208 if (block->references == 0)
209 { cloog_block_leak_down() ;
210 if (block->scaldims != NULL)
211 { for (i=0;i<block->nb_scaldims;i++)
212 cloog_int_clear(block->scaldims[i]);
214 free(block->scaldims) ;
216 if (block->statement)
217 cloog_statement_free(block->statement);
218 free(block) ;
225 * cloog_block_list_free function:
226 * This function frees the allocated memory for a CloogBlockList structure.
227 * - June 11th 2005: first version.
229 void cloog_block_list_free(CloogBlockList * blocklist)
230 { CloogBlockList * temp ;
232 while (blocklist != NULL)
233 { temp = blocklist->next ;
234 cloog_block_free(blocklist->block) ;
235 free(blocklist) ;
236 blocklist = temp ;
241 /******************************************************************************
242 * Processing functions *
243 ******************************************************************************/
246 * cloog_block_malloc function:
247 * This function allocates the memory space for a CloogBlock structure and
248 * sets its fields with default values. Then it returns a pointer to the
249 * allocated space.
250 * - November 21th 2005: first version.
252 CloogBlock * cloog_block_malloc()
253 { CloogBlock * block ;
255 /* Memory allocation for the CloogBlock structure. */
256 block = (CloogBlock *)malloc(sizeof(CloogBlock)) ;
257 if (block == NULL)
258 cloog_die("memory overflow.\n");
259 cloog_block_leak_up() ;
261 /* We set the various fields with default values. */
262 block->statement = NULL ;
263 block->nb_scaldims = 0 ;
264 block->scaldims = NULL ;
265 block->depth = 0 ;
266 block->references = 1 ;
267 block->usr = NULL;
269 return block ;
274 * cloog_block_alloc function:
275 * This function allocates the memory space for a CloogBlock structure and
276 * sets its fields with those given as input. Then it returns a pointer to the
277 * allocated space. The two parameters nb_scaldims and scaldims are for internal
278 * service, put to respectively 0 and NULL if you don't know what they are
279 * useful for !
280 * - statement is the statement list of the block,
281 * - scattering is the scattering function for the block (NULL if unsure !),
282 * - nb_scaldims is the number of scalar dimensions (0 if unsure !),
283 * - scaldims is the array with the scalar dimensions values (NULL if unsure !),
284 * - depth is the original block depth (the number of outer loops).
286 * - June 11th 2005: first version.
287 * - June 30th 2005: addition of the nb_scaldims and scaldims parameters.
288 * - November 21th 2005: use of cloog_block_malloc.
290 CloogBlock *cloog_block_alloc(CloogStatement *statement, int nb_scaldims,
291 cloog_int_t *scaldims, int depth)
292 { CloogBlock * block ;
294 /* Block allocation. */
295 block = cloog_block_malloc() ;
297 block->statement = statement ;
298 block->nb_scaldims = nb_scaldims ;
299 block->scaldims = scaldims ;
300 block->depth = depth ;
301 block->references = 1 ;
303 return block ;
308 * cloog_block_list_malloc function:
309 * This function allocates the memory space for a CloogBlockList structure and
310 * sets its fields with default values. Then it returns a pointer to the
311 * allocated space.
312 * - November 21th 2005: first version.
314 CloogBlockList * cloog_block_list_malloc()
315 { CloogBlockList * blocklist ;
317 /* Memory allocation for the CloogBlock structure. */
318 blocklist = (CloogBlockList *)malloc(sizeof(CloogBlockList)) ;
319 if (blocklist == NULL)
320 cloog_die("memory overflow.\n");
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 merged->statement = NULL;
395 cloog_block_free(merged);