2 /**-------------------------------------------------------------------**
4 **-------------------------------------------------------------------**
6 **-------------------------------------------------------------------**
7 ** First version: june 11th 2005 **
8 **-------------------------------------------------------------------**/
11 /******************************************************************************
12 * CLooG : the Chunky Loop Generator (experimental) *
13 ******************************************************************************
15 * Copyright (C) 2001-2005 Cedric Bastoul *
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 *
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 *
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 *
31 * CLooG, the Chunky Loop Generator *
32 * Written by Cedric Bastoul, Cedric.Bastoul@inria.fr *
34 ******************************************************************************/
35 /* CAUTION: the english used for comments is probably the worst you ever read,
36 * please feel free to correct and improve it !
41 # include "../include/cloog/cloog.h"
44 /******************************************************************************
45 * Memory leaks hunting *
46 ******************************************************************************/
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 ******************************************************************************/
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
)
94 /* Go to the right level. */
95 for (i
=0; i
<level
; i
++)
99 { fprintf(file
,"+-- CloogBlock\n") ;
102 for (i
=0; i
<level
+2; i
++)
103 fprintf(file
,"|\t") ;
106 /* Print statement list. */
107 cloog_statement_print_structure(file
,block
->statement
,level
+1) ;
110 for (i
=0; i
<level
+2; i
++)
111 fprintf(file
,"|\t") ;
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) ;
124 fprintf(file
,"+-- Null scattering function\n") ;
127 for (i
=0; i
<level
+2; i
++)
128 fprintf(file
,"|\t") ;
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") ;
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
]) ;
145 for (i
=0; i
<level
+2; i
++)
146 fprintf(file
,"|\t") ;
150 for (i
=0; i
<level
+1; i
++)
151 fprintf(file
,"|\t") ;
152 fprintf(file
,"Depth: %d\n",block
->depth
) ;
155 for (i
=0; i
<level
+1; i
++)
156 fprintf(file
,"|\t") ;
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
)
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
;
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
)
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
) ;
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
) ;
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
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
)) ;
260 { fprintf(stderr
, "[CLooG]ERROR: memory overflow.\n") ;
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
;
271 block
->references
= 1 ;
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
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
;
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 ;
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
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") ;
332 /* We set the various fields with default values. */
333 blocklist
->block
= NULL
;
334 blocklist
->next
= NULL
;
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
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
;
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
)
374 block
->references
++ ;
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
))
393 if (block
->statement
!= NULL
)
394 { statement
= block
->statement
;
396 while (statement
->next
!= NULL
)
397 statement
= statement
->next
;
399 statement
->next
= merged
->statement
;
402 block
->statement
= merged
->statement
;
404 cloog_block_leak_down() ;