2 /*+-----------------------------------------------------------------**
4 **-----------------------------------------------------------------**
6 **-----------------------------------------------------------------**
7 ** First version: 30/04/2008 **
8 **-----------------------------------------------------------------**
11 *****************************************************************************
12 * OpenScop: Structures and formats for polyhedral tools to talk together *
13 *****************************************************************************
14 * ,___,,_,__,,__,,__,,__,,_,__,,_,__,,__,,___,_,__,,_,__, *
15 * / / / // // // // / / / // // / / // / /|,_, *
16 * / / / // // // // / / / // // / / // / / / /\ *
17 * |~~~|~|~~~|~~~|~~~|~~~|~|~~~|~|~~~|~~~|~~~|~|~~~|~|~~~|/_/ \ *
18 * | G |C| P | = | L | P |=| = |C| = | = | = |=| = |=| C |\ \ /\ *
19 * | R |l| o | = | e | l |=| = |a| = | = | = |=| = |=| L | \# \ /\ *
20 * | A |a| l | = | t | u |=| = |n| = | = | = |=| = |=| o | |\# \ \ *
21 * | P |n| l | = | s | t |=| = |d| = | = | = | | |=| o | | \# \ \ *
22 * | H | | y | | e | o | | = |l| | | = | | | | G | | \ \ \ *
23 * | I | | | | e | | | | | | | | | | | | | \ \ \ *
24 * | T | | | | | | | | | | | | | | | | | \ \ \ *
25 * | E | | | | | | | | | | | | | | | | | \ \ \ *
26 * | * |*| * | * | * | * |*| * |*| * | * | * |*| * |*| * | / \* \ \ *
27 * | O |p| e | n | S | c |o| p |-| L | i | b |r| a |r| y |/ \ \ / *
28 * '---'-'---'---'---'---'-'---'-'---'---'---'-'---'-'---' '--' *
30 * Copyright (C) 2008 University Paris-Sud 11 and INRIA *
32 * (3-clause BSD license) *
33 * Redistribution and use in source and binary forms, with or without *
34 * modification, are permitted provided that the following conditions *
37 * 1. Redistributions of source code must retain the above copyright notice, *
38 * this list of conditions and the following disclaimer. *
39 * 2. Redistributions in binary form must reproduce the above copyright *
40 * notice, this list of conditions and the following disclaimer in the *
41 * documentation and/or other materials provided with the distribution. *
42 * 3. The name of the author may not be used to endorse or promote products *
43 * derived from this software without specific prior written permission. *
45 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR *
46 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES *
47 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. *
48 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, *
49 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT *
50 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, *
51 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY *
52 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT *
53 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF *
54 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
56 * OpenScop Library, a library to manipulate OpenScop formats and data *
57 * structures. Written by: *
58 * Cedric Bastoul <Cedric.Bastoul@u-psud.fr> and *
59 * Louis-Noel Pouchet <Louis-Noel.pouchet@inria.fr> *
61 *****************************************************************************/
68 # include <openscop/statement.h>
71 /*+***************************************************************************
72 * Structure display functions *
73 *****************************************************************************/
77 * openscop_statement_idump function:
78 * Displays a openscop_statement_t structure (*statement) into a file (file,
79 * possibly stdout) in a way that trends to be understandable without falling
80 * in a deep depression or, for the lucky ones, getting a headache... It
81 * includes an indentation level (level) in order to work with others
82 * print_structure functions.
83 * \param file File where informations are printed.
84 * \param statement The statement whose information have to be printed.
85 * \param level Number of spaces before printing, for each line.
87 void openscop_statement_idump(FILE * file
,
88 openscop_statement_p statement
,
90 int i
, j
, first
= 1, number
= 1;
92 // Go to the right level.
93 for (j
= 0; j
< level
; j
++)
96 if (statement
!= NULL
)
97 fprintf(file
, "+-- openscop_statement_t (S%d)\n", number
);
99 fprintf(file
, "+-- NULL statement\n");
101 while (statement
!= NULL
) {
103 // Go to the right level.
104 for (j
= 0; j
< level
; j
++)
105 fprintf(file
, "|\t");
106 fprintf(file
, "| openscop_statement_t (S%d)\n", number
);
112 for (j
= 0; j
<= level
+1; j
++)
113 fprintf(file
, "|\t");
116 // Print the domain of the statement.
117 openscop_relation_idump(file
, statement
->domain
, level
+1);
119 // Print the scattering of the statement.
120 openscop_relation_idump(file
, statement
->scattering
, level
+1);
122 // Print the array access information of the statement.
123 openscop_relation_list_idump(file
, statement
->access
, level
+1);
125 // Print the original iterator names.
126 for (i
= 0; i
<= level
; i
++)
127 fprintf(file
, "|\t");
128 if (statement
->nb_iterators
> 0) {
129 fprintf(file
, "+-- Original iterator strings:");
130 for (i
= 0; i
< statement
->nb_iterators
; i
++)
131 fprintf(file
, " %s", statement
->iterators
[i
]);
135 fprintf(file
, "+-- No original iterator string\n");
138 for (i
= 0; i
<= level
+1; i
++)
139 fprintf(file
, "|\t");
142 // Print the original statement body.
143 for (i
= 0; i
<= level
; i
++)
144 fprintf(file
, "|\t");
145 if (statement
->body
!= NULL
)
146 fprintf(file
, "+-- Original body: %s\n", statement
->body
);
148 fprintf(file
, "+-- No original body\n");
151 for (i
= 0; i
<= level
+1; i
++)
152 fprintf(file
, "|\t");
155 statement
= statement
->next
;
159 if (statement
!= NULL
) {
160 for (j
= 0; j
<= level
; j
++)
161 fprintf(file
, "|\t");
162 fprintf(file
, "V\n");
167 for (j
= 0; j
<= level
; j
++)
168 fprintf(file
, "|\t");
174 * openscop_statement_dump function:
175 * This function prints the content of a openscop_statement_t structure
176 * (*statement) into a file (file, possibly stdout).
177 * \param file File where informations are printed.
178 * \param statement The statement whose information have to be printed.
180 void openscop_statement_dump(FILE * file
, openscop_statement_p statement
) {
181 openscop_statement_idump(file
, statement
, 0);
186 * openscop_statement_print function:
187 * This function prints the content of a openscop_statement_t structure
188 * (*statement) into a file (file, possibly stdout) in the OpenScop format.
189 * \param file File where informations are printed.
190 * \param statement The statement whose information have to be printed.
191 * \param names The textual names of the various elements. Is is important
192 * that names->nb_parameters is exact if the matrix
193 * representation is used. Set to NULL if printing comments
196 void openscop_statement_print(FILE * file
,
197 openscop_statement_p statement
,
198 openscop_names_p names
) {
199 int i
, switched
, number
= 1;
201 int tmp_nb_iterators
= 0;
202 char ** tmp_iterators
= NULL
;
204 while (statement
!= NULL
) {
207 // Switch iterator names to the current statement names if possible.
209 if ((statement
->nb_iterators
> 0) && (names
!= NULL
)) {
210 tmp_nb_iterators
= names
->nb_iterators
;
211 tmp_iterators
= names
->iterators
;
212 names
->nb_iterators
= statement
->nb_iterators
;
213 names
->iterators
= statement
->iterators
;
217 fprintf(file
, "# =============================================== ");
218 fprintf(file
, "Statement %d\n", number
);
220 fprintf(file
, "# Number of relations describing the statement:\n");
222 if (statement
->domain
!= NULL
)
224 if (statement
->scattering
!= NULL
)
226 nb_relations
+= openscop_relation_list_count(statement
->access
);
228 fprintf(file
, "%d\n\n", nb_relations
);
230 fprintf(file
, "# ---------------------------------------------- ");
231 fprintf(file
, "%2d.1 Domain\n", number
);
232 openscop_relation_print(file
, statement
->domain
, names
);
235 fprintf(file
, "# ---------------------------------------------- ");
236 fprintf(file
, "%2d.2 Scattering\n", number
);
237 openscop_relation_print(file
, statement
->scattering
, names
);
240 fprintf(file
, "# ---------------------------------------------- ");
241 fprintf(file
, "%2d.3 Access\n", number
);
242 openscop_relation_list_print_elts(file
, statement
->access
, names
);
245 fprintf(file
, "# ---------------------------------------------- ");
246 fprintf(file
, "%2d.4 Body\n", number
);
247 if (statement
->body
!= NULL
) {
248 fprintf(file
, "# Statement body is provided\n");
249 fprintf(file
, "1\n");
250 if (statement
->nb_iterators
> 0) {
251 fprintf(file
, "# Original iterator names\n");
252 for (i
= 0; i
< statement
->nb_iterators
; i
++)
253 fprintf(file
, "%s ", statement
->iterators
[i
]);
257 fprintf(file
, "# No original iterator names\n");
259 fprintf(file
, "# Statement body\n");
260 fprintf(file
, "%s\n", statement
->body
);
263 fprintf(file
, "# Statement body is not provided\n");
264 fprintf(file
, "0\n");
266 fprintf(file
, "\n\n");
269 names
->nb_iterators
= tmp_nb_iterators
;
270 names
->iterators
= tmp_iterators
;
272 statement
= statement
->next
;
278 /*****************************************************************************
280 *****************************************************************************/
284 * openscop_statement_dispatch function:
285 * this function dispatches the relations from a relation list to the
286 * convenient fields of a statement structure: it extracts the domain,
287 * the scattering and the access list and store them accordingly in the
288 * statement structure provided as a parameter.
289 * \param stmt The statement where to dispatch the relations.
290 * \param list The "brute" relation list to sort and dispatch (freed).
293 void openscop_statement_dispatch(openscop_statement_p stmt
,
294 openscop_relation_list_p list
) {
295 openscop_relation_list_p domain_list
;
296 openscop_relation_list_p scattering_list
;
297 int nb_domains
, nb_scattering
, nb_accesses
;
300 domain_list
= openscop_relation_list_filter(list
, OPENSCOP_TYPE_DOMAIN
);
301 nb_domains
= openscop_relation_list_count(domain_list
);
302 if (nb_domains
> 1) {
303 fprintf(stderr
, "[OpenScop] Error: more than one domain for a "
307 if (domain_list
!= NULL
) {
308 stmt
->domain
= domain_list
->elt
;
309 domain_list
->elt
= NULL
;
310 openscop_relation_list_free(domain_list
);
317 scattering_list
=openscop_relation_list_filter(list
,OPENSCOP_TYPE_SCATTERING
);
318 nb_scattering
= openscop_relation_list_count(scattering_list
);
319 if (nb_scattering
> 1) {
320 fprintf(stderr
, "[OpenScop] Error: more than one scattering relation "
321 "for a statement.\n");
324 if (scattering_list
!= NULL
) {
325 stmt
->scattering
= scattering_list
->elt
;
326 scattering_list
->elt
= NULL
;
327 openscop_relation_list_free(scattering_list
);
330 stmt
->scattering
= NULL
;
334 stmt
->access
= openscop_relation_list_filter(list
, OPENSCOP_TYPE_ACCESS
);
335 nb_accesses
= openscop_relation_list_count(stmt
->access
);
337 if ((nb_domains
+ nb_scattering
+ nb_accesses
) !=
338 (openscop_relation_list_count(list
))) {
339 fprintf(stderr
, "[OpenScop] Error: unexpected relation type to define "
344 openscop_relation_list_free(list
);
349 * openscop_statement_read function:
350 * This function reads a openscop_statement_t structure from an input stream
352 * \param file The input stream.
353 * \param nb_parameters The number of global parameters.
354 * \return A pointer to the statement structure that has been read.
356 openscop_statement_p
openscop_statement_read(FILE * file
,
358 openscop_statement_p stmt
= openscop_statement_malloc();
359 openscop_relation_list_p list
;
360 char buff
[OPENSCOP_MAX_STRING
], * start
, * end
;
361 int expected_nb_iterators
, nb_iterators
;
364 // Read all statement relations.
365 list
= openscop_relation_list_read(file
);
367 // Store relations at the right place according to their type.
368 openscop_statement_dispatch(stmt
, list
);
370 // Read the body information, if any.
371 if (openscop_util_read_int(file
, NULL
) > 0) {
372 // Is there any iterator to read ?
373 if (stmt
->domain
!= NULL
) {
374 if (openscop_relation_is_matrix(stmt
->domain
)) {
375 if (nb_parameters
== OPENSCOP_UNDEFINED
) {
376 fprintf(stderr
, "[OpenScop] Warning: undefined number of "
377 "parameters (no context ?), assuming 0.\n");
381 expected_nb_iterators
= stmt
->domain
->nb_columns
-
385 expected_nb_iterators
= stmt
->domain
->nb_output_dims
;
389 fprintf(stderr
, "[OpenScop] Warning: no domain, assuming 0 "
390 "original iterator.\n");
391 expected_nb_iterators
= 0;
394 // Read the original iterator names.
395 if (expected_nb_iterators
> 0) {
396 stmt
->iterators
= openscop_util_strings_read(file
, &nb_iterators
);
397 stmt
->nb_iterators
= nb_iterators
;
398 if (expected_nb_iterators
!= nb_iterators
) {
399 fprintf(stderr
, "[OpenScop] Warning: not the right number of "
400 "original iterators.\n");
405 // - Skip blank/commented lines and spaces before the body.
406 start
= openscop_util_skip_blank_and_comments(file
, buff
);
408 // - Remove the comments after the body.
410 while ((*end
!= '#') && (*end
!= '\n'))
415 stmt
->body
= strdup(start
);
418 stmt
->nb_iterators
= 0;
419 stmt
->iterators
= NULL
;
428 /*+***************************************************************************
429 * Memory allocation/deallocation functions *
430 *****************************************************************************/
434 * openscop_statement_malloc function:
435 * This function allocates the memory space for a openscop_statement_t
436 * structure and sets its fields with default values. Then it returns a pointer
437 * to the allocated space.
438 * \return A pointer to an empty statement with fields set to default values.
440 openscop_statement_p
openscop_statement_malloc() {
441 openscop_statement_p statement
;
443 statement
= (openscop_statement_p
)malloc(sizeof(openscop_statement_t
));
444 if (statement
== NULL
) {
445 fprintf(stderr
, "[OpenScop] Error: memory overflow.\n");
449 statement
->domain
= NULL
;
450 statement
->scattering
= NULL
;
451 statement
->access
= NULL
;
452 statement
->nb_iterators
= 0;
453 statement
->iterators
= NULL
;
454 statement
->body
= NULL
;
455 statement
->next
= NULL
;
462 * openscop_statement_free function:
463 * This function frees the allocated memory for a openscop_statement_t
465 * \param statement The pointer to the statement we want to free.
467 void openscop_statement_free(openscop_statement_p statement
) {
469 openscop_statement_p next
;
471 while (statement
!= NULL
) {
472 next
= statement
->next
;
473 openscop_relation_free(statement
->domain
);
474 openscop_relation_free(statement
->scattering
);
475 openscop_relation_list_free(statement
->access
);
476 if (statement
->iterators
!= NULL
) {
477 for (i
= 0; i
< statement
->nb_iterators
; i
++)
478 free(statement
->iterators
[i
]);
479 free(statement
->iterators
);
481 if (statement
->body
!= NULL
)
482 free(statement
->body
);
490 /*+***************************************************************************
491 * Processing functions *
492 *****************************************************************************/
496 * openscop_statement_add function:
497 * This function adds a statement "statement" at the end of the statement
498 * list pointed by "location".
499 * \param location Address of the first element of the statement list.
500 * \param statement The statement to add to the list.
502 void openscop_statement_add(openscop_statement_p
* location
,
503 openscop_statement_p statement
) {
504 while (*location
!= NULL
)
505 location
= &((*location
)->next
);
507 *location
= statement
;
512 * openscop_statement_number function:
513 * This function returns the number of statements in the statement list
514 * provided as parameter.
515 * \param statement The first element of the statement list.
516 * \return The number of statements in the statement list.
518 int openscop_statement_number(openscop_statement_p statement
) {
521 while (statement
!= NULL
) {
523 statement
= statement
->next
;
530 * openscop_statement_copy function:
531 * This functions builds and returns a "hard copy" (not a pointer copy) of a
532 * openscop_statement_t data structure provided as parameter.
533 * \param statement The pointer to the statement we want to copy.
534 * \return A pointer to the full copy of the statement provided as parameter.
536 openscop_statement_p
openscop_statement_copy(openscop_statement_p statement
) {
538 openscop_statement_p copy
= NULL
, node
, previous
= NULL
;
540 while (statement
!= NULL
) {
541 node
= openscop_statement_malloc();
542 node
->domain
= openscop_relation_copy(statement
->domain
);
543 node
->scattering
= openscop_relation_copy(statement
->scattering
);
544 node
->access
= openscop_relation_list_copy(statement
->access
);
545 node
->nb_iterators
= statement
->nb_iterators
;
546 node
->iterators
= openscop_util_strings_copy(statement
->iterators
,
547 statement
->nb_iterators
);
548 node
->body
= strdup(statement
->body
);
557 previous
->next
= node
;
558 previous
= previous
->next
;
561 statement
= statement
->next
;
569 * openscop_statement_equal function:
570 * This function returns true if the two statements are the same, false
571 * otherwise (the usr field is not tested).
572 * \param s1 The first statement.
573 * \param s2 The second statement.
574 * \return 1 if s1 and s2 are the same (content-wise), 0 otherwise.
576 int openscop_statement_equal(openscop_statement_p s1
,
577 openscop_statement_p s2
) {
583 if (((s1
->next
!= NULL
) && (s2
->next
== NULL
)) ||
584 ((s1
->next
== NULL
) && (s2
->next
!= NULL
)))
587 if ((s1
->next
!= NULL
) && (s2
->next
!= NULL
))
588 if (!openscop_statement_equal(s1
->next
, s2
->next
))
591 if ((s1
->nb_iterators
!= s2
->nb_iterators
) ||
592 (!openscop_relation_equal(s1
->domain
, s2
->domain
)) ||
593 (!openscop_relation_equal(s1
->scattering
, s2
->scattering
)) ||
594 (!openscop_relation_list_equal(s1
->access
, s2
->access
)))
597 if (((s1
->body
== NULL
) && (s2
->body
!= NULL
)) ||
598 ((s1
->body
!= NULL
) && (s2
->body
== NULL
)) ||
599 ((s1
->body
!= NULL
) && (s2
->body
!= NULL
) &&
600 (strcmp(s1
->body
, s2
->body
) != 0))) {
601 fprintf(stderr
, "[OpenScop] info: bodies are not the same.\n");
605 for (i
= 0; i
< s1
->nb_iterators
; i
++) {
606 if (((s1
->iterators
[i
] == NULL
) && (s2
->iterators
[i
] != NULL
)) ||
607 ((s1
->iterators
[i
] != NULL
) && (s2
->iterators
[i
] == NULL
)) ||
608 ((s1
->iterators
[i
] != NULL
) && (s2
->iterators
[i
] != NULL
) &&
609 (strcmp(s1
->iterators
[i
], s2
->iterators
[i
]) != 0))) {
610 fprintf(stderr
, "[OpenScop] info: original iterators "
611 "are not the same.\n");
621 * openscop_statement_integrity_check function:
622 * This function checks that a statement is "well formed" according to some
623 * expected properties (setting an expected value to OPENSCOP_UNDEFINED means
624 * that we do not expect a specific value). It returns 0 if the check failed
625 * or 1 if no problem has been detected.
626 * \param statement The statement we want to check.
627 * \param expected_nb_parameters Expected number of parameters.
628 * \return 0 if the integrity check fails, 1 otherwise.
630 int openscop_statement_integrity_check(openscop_statement_p statement
,
631 int expected_nb_parameters
) {
632 int expected_nb_iterators
= OPENSCOP_UNDEFINED
;
634 while (statement
!= NULL
) {
636 if ((openscop_relation_is_matrix(statement
->domain
) &&
637 !openscop_relation_integrity_check(statement
->domain
,
638 OPENSCOP_TYPE_DOMAIN
,
641 OPENSCOP_UNDEFINED
)) ||
642 (!openscop_relation_is_matrix(statement
->domain
) &&
643 !openscop_relation_integrity_check(statement
->domain
,
644 OPENSCOP_TYPE_DOMAIN
,
647 expected_nb_parameters
))) {
651 // Get the number of iterators.
652 if (statement
->domain
!= NULL
) {
653 if (openscop_relation_is_matrix(statement
->domain
)) {
654 if (expected_nb_parameters
!= OPENSCOP_UNDEFINED
)
655 expected_nb_iterators
= statement
->domain
->nb_columns
-
656 expected_nb_parameters
- 2;
658 expected_nb_iterators
= OPENSCOP_UNDEFINED
;
661 expected_nb_iterators
= statement
->domain
->nb_output_dims
;
664 // Check the scattering relation.
665 if ((openscop_relation_is_matrix(statement
->scattering
) &&
666 !openscop_relation_integrity_check(statement
->scattering
,
667 OPENSCOP_TYPE_SCATTERING
,
670 OPENSCOP_UNDEFINED
)) ||
671 (!openscop_relation_is_matrix(statement
->scattering
) &&
672 !openscop_relation_integrity_check(statement
->scattering
,
673 OPENSCOP_TYPE_SCATTERING
,
675 expected_nb_iterators
,
676 expected_nb_parameters
))) {
680 // Check the access relations.
681 if (!openscop_relation_list_integrity_check(statement
->access
,
682 OPENSCOP_TYPE_ACCESS
,
684 expected_nb_iterators
,
685 expected_nb_parameters
)) {
689 if ((statement
->nb_iterators
> 0) &&
690 (statement
->nb_iterators
< statement
->domain
->nb_output_dims
)) {
691 fprintf(stderr
, "[OpenScop] Warning: not enough original iterator "
696 statement
= statement
->next
;