summaryrefslogtreecommitdiff
path: root/cloog-0.17.0/source/union_domain.c
blob: 0d24658b9fdbbb0346361f97517c4fcab9b46261 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include "../include/cloog/cloog.h"

#ifdef OSL_SUPPORT
#include <osl/strings.h>
#include <osl/extensions/scatnames.h>
#include <osl/statement.h>
#include <osl/scop.h>
#endif

#define ALLOC(type) (type*)malloc(sizeof(type))
#define ALLOCN(type,n) (type*)malloc((n)*sizeof(type))

void cloog_named_domain_list_free(CloogNamedDomainList *list)
{
	while (list != NULL) {
		CloogNamedDomainList *temp = list->next;
		cloog_domain_free(list->domain);
		cloog_scattering_free(list->scattering);
		free(list->name);
		free(list);
		list = temp;
	}
}

CloogUnionDomain *cloog_union_domain_alloc(int nb_par)
{
	CloogUnionDomain *ud;

	ud = ALLOC(CloogUnionDomain);
	if (!ud)
		cloog_die("memory overflow.\n");

	ud->domain = NULL;
	ud->next_domain = &ud->domain;
	
	ud->n_name[CLOOG_PARAM] = nb_par;
	ud->n_name[CLOOG_ITER] = 0;
	ud->n_name[CLOOG_SCAT] = 0;

	ud->name[CLOOG_PARAM] = NULL;
	ud->name[CLOOG_ITER] = NULL;
	ud->name[CLOOG_SCAT] = NULL;

	return ud;
}

void cloog_union_domain_free(CloogUnionDomain *ud)
{
	int i;
	int j;

	if (!ud)
		return;

	for (i = 0; i < 3; ++i) {
		if (!ud->name[i])
			continue;
		for (j = 0; j < ud->n_name[i]; ++i)
			free(ud->name[i][j]);
		free(ud->name[i]);
	}

	cloog_named_domain_list_free(ud->domain);

	free(ud);
}

/**
 * Add a domain with scattering function to the union of domains.
 * name may be NULL and is duplicated if it is not.
 * domain and scattering are taken over by the CloogUnionDomain.
 * scattering may be NULL.
 */
CloogUnionDomain *cloog_union_domain_add_domain(CloogUnionDomain *ud,
	const char *name, CloogDomain *domain, CloogScattering *scattering,
	void *usr)
{
	CloogNamedDomainList *named;
	int n;

	if (!ud)
		return NULL;

	named = ALLOC(CloogNamedDomainList);
	if (!named)
		cloog_die("memory overflow.\n");

	if (ud->name[CLOOG_ITER])
		cloog_die("iterator names must be set after adding domains.\n");
	if (ud->name[CLOOG_SCAT])
		cloog_die("scattering names must be set after adding domains.\n");

	n = cloog_domain_dimension(domain);
	if (n > ud->n_name[CLOOG_ITER])
		ud->n_name[CLOOG_ITER] = n;

	if (scattering) {
		n = cloog_scattering_dimension(scattering, domain);
		if (n > ud->n_name[CLOOG_SCAT])
			ud->n_name[CLOOG_SCAT] = n;
	}

	named->domain = domain;
	named->scattering = scattering;
	named->name = name ? strdup(name) : NULL;
	named->usr = usr;
	named->next = NULL;

	*ud->next_domain = named;
	ud->next_domain = &named->next;

	return ud;
}

/**
 * Set the name of parameter, iterator or scattering dimension
 * at the specified position.  The name is duplicated.
 */
CloogUnionDomain *cloog_union_domain_set_name(CloogUnionDomain *ud,
	enum cloog_dim_type type, int index, const char *name)
{
	int i;

	if (!ud)
		return ud;

	if (type != CLOOG_PARAM &&
	    type != CLOOG_ITER &&
	    type != CLOOG_SCAT)
		cloog_die("invalid dim type\n");

	if (index < 0 || index >= ud->n_name[type])
		cloog_die("index out of range\n");

	if (!ud->name[type]) {
		ud->name[type] = ALLOCN(char *, ud->n_name[type]);
		if (!ud->name[type])
			cloog_die("memory overflow.\n");
		for (i = 0; i < ud->n_name[type]; ++i)
			ud->name[type][i] = NULL;
	}

	free(ud->name[type][index]);
	ud->name[type][index] = strdup(name);
	if (!ud->name[type][index])
		cloog_die("memory overflow.\n");

	return ud;
}

static char *next_line(FILE *input, char *line, unsigned len)
{
	char *p;

	do {
		if (!(p = fgets(line, len, input)))
			return NULL;
		while (isspace(*p) && *p != '\n')
			++p;
	} while (*p == '#' || *p == '\n');

	return p;
}

/**
 * cloog_scattering_list_read
 * Read in a list of scattering functions for the nb_statements
 * domains in loop.
 */
static CloogScatteringList *cloog_scattering_list_read(FILE * foo,
	CloogDomain **domain, int nb_statements, int nb_parameters)
{
    int nb_scat = 0;
    char s[MAX_STRING];
    CloogScatteringList *list = NULL, **next = &list;

    /* We read first the number of scattering functions in the list. */
    do {
	if (!fgets(s, MAX_STRING, foo))
	    break;
    } while ((*s=='#' || *s=='\n') || (sscanf(s, " %d", &nb_scat) < 1));

    if (nb_scat == 0)
	return NULL;

    if (nb_scat != nb_statements)
	cloog_die("wrong number of scattering functions.\n");

    while (nb_scat--) {
	*next = (CloogScatteringList *)malloc(sizeof(CloogScatteringList));
	(*next)->scatt = cloog_domain_read_scattering(*domain, foo);
	(*next)->next = NULL;

	next = &(*next)->next;
	domain++;
    }
    return list;
}

static CloogUnionDomain *set_names_from_list(CloogUnionDomain *ud,
	enum cloog_dim_type type, int n, char **names)
{
	int i;

	if (!names)
		return ud;

	for (i = 0; i < n; ++i) {
		ud = cloog_union_domain_set_name(ud, type, i, names[i]);
		free(names[i]);
	}
	free(names);

	return ud;
}

/**
 * Fill up a CloogUnionDomain from information in a CLooG input file.
 * The language and the context are assumed to have been read from
 * the input file already.
 */
CloogUnionDomain *cloog_union_domain_read(FILE *file, int nb_par,
	CloogOptions *options)
{
	int op1, op2, op3;
	char line[MAX_STRING];
	CloogDomain **domain;
	CloogUnionDomain *ud;
	CloogScatteringList *scatteringl;
	int i;
	int n_iter = -1;
	int n_dom;
	char **names;

	ud = cloog_union_domain_alloc(nb_par);

	names = cloog_names_read_strings(file, nb_par);
	ud = set_names_from_list(ud, CLOOG_PARAM, nb_par, names);

	/* We read the number of statements. */
	if (!next_line(file, line, sizeof(line)))
		cloog_die("Input error.\n");
	if (sscanf(line, "%d", &n_dom) != 1)
		cloog_die("Input error.\n");

	domain = ALLOCN(CloogDomain *, n_dom);
	if (!domain)
		cloog_die("memory overflow.\n");

	for (i = 0; i < n_dom; ++i) {
		int dim;

		domain[i] = cloog_domain_union_read(options->state, file,
						    nb_par);
		dim = cloog_domain_dimension(domain[i]);
		if (dim > n_iter)
			n_iter = dim;

		/* To read that stupid "0 0 0" line. */
		if (!next_line(file, line, sizeof(line)))
			cloog_die("Input error.\n");
		if (sscanf(line, " %d %d %d", &op1, &op2, &op3) != 3)
			cloog_die("Input error.\n");
	}
        
	/* Reading of the iterator names. */
	names = cloog_names_read_strings(file, n_iter);

	/* Reading and putting the scattering data in program structure. */
	scatteringl = cloog_scattering_list_read(file, domain, n_dom, nb_par);

	if (scatteringl) {
		CloogScatteringList *is, *next;

		if (cloog_scattering_list_lazy_same(scatteringl))
			cloog_msg(options, CLOOG_WARNING,
				  "some scattering functions are similar.\n");
		
		for (i = 0, is = scatteringl; i < n_dom; ++i, is = next) {
			next = is->next;
			ud = cloog_union_domain_add_domain(ud, NULL, domain[i],
							      is->scatt, NULL);
			free(is);
		}
	} else {
		for (i = 0; i < n_dom; ++i)
			ud = cloog_union_domain_add_domain(ud, NULL, domain[i],
								NULL, NULL);
	}

	ud = set_names_from_list(ud, CLOOG_ITER, n_iter, names);

	if (scatteringl) {
		int n_scat = ud->n_name[CLOOG_SCAT];
		names = cloog_names_read_strings(file, n_scat);
		ud = set_names_from_list(ud, CLOOG_SCAT, n_scat, names);
	}

	free(domain);

	return ud;
}


#ifdef OSL_SUPPORT
/**
 * Extracts a CloogUnionDomain from an openscop scop (the CloogUnionDomain
 * corresponds more or less to the openscop statement).
 * \param[in,out] state CLooG state.
 * \param[in]     scop  OpenScop scop to convert.
 * \return A new CloogUnionDomain corresponding the input OpenScop scop.
 */
CloogUnionDomain *cloog_union_domain_from_osl_scop(CloogState *state,
                                                   osl_scop_p scop) {
  int i, nb_parameters;
  CloogDomain *domain = NULL;
  CloogScattering *scattering = NULL;
  CloogUnionDomain *ud = NULL;
  osl_scop_p normalized;
  osl_statement_p statement;
  osl_scatnames_p scatnames;

  /* Set the union of domains. */
  nb_parameters = (scop->context == NULL) ? 0 : scop->context->nb_parameters;
  ud = cloog_union_domain_alloc(nb_parameters);

  /* - Set the parameter names. */
  if (osl_generic_has_URI(scop->parameters, OSL_URI_STRINGS)) {
    for (i = 0; i < osl_strings_size(scop->parameters->data); i++) {
      ud = cloog_union_domain_set_name(ud, CLOOG_PARAM, i,
        ((osl_strings_p)(scop->parameters->data))->string[i]);
    }
  }

  /* - Set each statement (domain/scattering).
   *   Since CLooG requires all number of scattering dimensions to be
   *   equal, we normalize them first.
   */
  normalized = osl_scop_clone(scop);
  osl_scop_normalize_scattering(normalized);
  statement = normalized->statement;
  while(statement != NULL) {
    domain = cloog_domain_from_osl_relation(state, statement->domain);
    scattering = cloog_scattering_from_osl_relation(state,
                                                    statement->scattering);
    ud = cloog_union_domain_add_domain(ud, NULL, domain, scattering, NULL);
    statement = statement->next;
  }
  osl_scop_free(normalized);

  /* - Set the scattering dimension names. */
  scatnames = osl_generic_lookup(scop->extension, OSL_URI_SCATNAMES);
  if ((scatnames != NULL) && (scatnames->names != NULL)) {
    for (i = 0; (i < osl_strings_size(scatnames->names)) &&
                (i < ud->n_name[CLOOG_SCAT]); i++) {
      ud = cloog_union_domain_set_name(ud, CLOOG_SCAT, i,
                                       scatnames->names->string[i]);
    }
  }

  return ud;
}
#endif