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 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 ******************************************************************************/
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 ;
279 * cloog_block_alloc function:
280 * This function allocates the memory space for a CloogBlock structure and
281 * sets its fields with those given as input. Then it returns a pointer to the
282 * allocated space. The two parameters nb_scaldims and scaldims are for internal
283 * service, put to respectively 0 and NULL if you don't know what they are
285 * - statement is the statement list of the block,
286 * - scattering is the scattering function for the block (NULL if unsure !),
287 * - nb_scaldims is the number of scalar dimensions (0 if unsure !),
288 * - scaldims is the array with the scalar dimensions values (NULL if unsure !),
289 * - depth is the original block depth (the number of outer loops).
291 * - June 11th 2005: first version.
292 * - June 30th 2005: addition of the nb_scaldims and scaldims parameters.
293 * - November 21th 2005: use of cloog_block_malloc.
295 CloogBlock
* cloog_block_alloc(statement
,scattering
,nb_scaldims
,scaldims
,depth
)
296 CloogStatement
* statement
;
297 CloogMatrix
* scattering
;
298 int nb_scaldims
, depth
;
300 { CloogBlock
* block
;
302 /* Block allocation. */
303 block
= cloog_block_malloc() ;
305 block
->statement
= statement
;
306 block
->scattering
= scattering
;
307 block
->nb_scaldims
= nb_scaldims
;
308 block
->scaldims
= scaldims
;
309 block
->depth
= depth
;
310 block
->references
= 1 ;
317 * cloog_block_list_malloc function:
318 * This function allocates the memory space for a CloogBlockList structure and
319 * sets its fields with default values. Then it returns a pointer to the
321 * - November 21th 2005: first version.
323 CloogBlockList
* cloog_block_list_malloc()
324 { CloogBlockList
* blocklist
;
326 /* Memory allocation for the CloogBlock structure. */
327 blocklist
= (CloogBlockList
*)malloc(sizeof(CloogBlockList
)) ;
328 if (blocklist
== NULL
)
329 { fprintf(stderr
, "[CLooG]ERROR: memory overflow.\n") ;
333 /* We set the various fields with default values. */
334 blocklist
->block
= NULL
;
335 blocklist
->next
= NULL
;
342 * cloog_block_list_alloc function:
343 * This function allocates the memory space for a CloogBlockList structure and
344 * sets its fields with those given as input. Then it returns a pointer to the
346 * - block is the block element of the list node,
348 * - June 11th 2005: first version.
349 * - November 21th 2005: use of cloog_block_list_malloc.
351 CloogBlockList
* cloog_block_list_alloc(CloogBlock
* block
)
352 { CloogBlockList
* blocklist
;
354 /* Block list node allocation. */
355 blocklist
= cloog_block_list_malloc() ;
357 blocklist
->block
= block
;
358 blocklist
->block
->references
++ ; /* The block has a new reference to it. */
359 blocklist
->next
= NULL
;
366 * cloog_block_copy function:
367 * This function returns a copy of a CloogBlock structure 'block'. To save
368 * memory this is not a memory copy but we increment a counter of active
369 * references inside the structure, then return a pointer to that structure.
371 CloogBlock
* cloog_block_copy(CloogBlock
* block
)
375 block
->references
++ ;
381 * cloog_block_merge function:
382 * this function adds at the end of the statement list of the block 'block',
383 * the statement list of the block 'merged'. Then the CloogBlock structure
384 * of 'merged' is freed (obviously not its statement list that is now
385 * included in 'block').
386 * - June 11th 2005: first version.
388 void cloog_block_merge(CloogBlock
* block
, CloogBlock
* merged
)
389 { CloogStatement
* statement
;
391 if ((block
== NULL
) || (merged
== NULL
))
394 if (block
->statement
!= NULL
)
395 { statement
= block
->statement
;
397 while (statement
->next
!= NULL
)
398 statement
= statement
->next
;
400 statement
->next
= merged
->statement
;
403 block
->statement
= merged
->statement
;
405 cloog_block_leak_down() ;