isl_schedule_band.c: use isl_ast_loop_type enum instead of plain integers
[isl.git] / isl_ctx.c
blob1a57da7d22608341164b64564f56d5d18a1f9fff
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
4 * Use of this software is governed by the MIT license
6 * Written by Sven Verdoolaege, K.U.Leuven, Departement
7 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
8 */
10 #include <isl_ctx_private.h>
11 #include <isl/vec.h>
12 #include <isl_options_private.h>
14 #define __isl_calloc(type,size) ((type *)calloc(1, size))
15 #define __isl_calloc_type(type) __isl_calloc(type,sizeof(type))
17 /* Return the negation of "b", where the negation of isl_bool_error
18 * is isl_bool_error again.
20 isl_bool isl_bool_not(isl_bool b)
22 return b < 0 ? isl_bool_error : !b;
25 /* Check that the result of an allocation ("p") is not NULL and
26 * complain if it is.
27 * The only exception is when allocation size ("size") is equal to zero.
29 static void *check_non_null(isl_ctx *ctx, void *p, size_t size)
31 if (p || size == 0)
32 return p;
33 isl_die(ctx, isl_error_alloc, "allocation failure", return NULL);
36 /* Prepare for performing the next "operation" in the context.
37 * Return 0 if we are allowed to perform this operation and
38 * return -1 if we should abort the computation.
40 * In particular, we should stop if the user has explicitly aborted
41 * the computation or if the maximal number of operations has been exceeded.
43 int isl_ctx_next_operation(isl_ctx *ctx)
45 if (!ctx)
46 return -1;
47 if (ctx->abort) {
48 isl_ctx_set_error(ctx, isl_error_abort);
49 return -1;
51 if (ctx->max_operations && ctx->operations >= ctx->max_operations)
52 isl_die(ctx, isl_error_quota,
53 "maximal number of operations exceeded", return -1);
54 ctx->operations++;
55 return 0;
58 /* Call malloc and complain if it fails.
59 * If ctx is NULL, then return NULL.
61 void *isl_malloc_or_die(isl_ctx *ctx, size_t size)
63 if (isl_ctx_next_operation(ctx) < 0)
64 return NULL;
65 return ctx ? check_non_null(ctx, malloc(size), size) : NULL;
68 /* Call calloc and complain if it fails.
69 * If ctx is NULL, then return NULL.
71 void *isl_calloc_or_die(isl_ctx *ctx, size_t nmemb, size_t size)
73 if (isl_ctx_next_operation(ctx) < 0)
74 return NULL;
75 return ctx ? check_non_null(ctx, calloc(nmemb, size), nmemb) : NULL;
78 /* Call realloc and complain if it fails.
79 * If ctx is NULL, then return NULL.
81 void *isl_realloc_or_die(isl_ctx *ctx, void *ptr, size_t size)
83 if (isl_ctx_next_operation(ctx) < 0)
84 return NULL;
85 return ctx ? check_non_null(ctx, realloc(ptr, size), size) : NULL;
88 /* Keep track of all information about the current error ("error", "msg",
89 * "file", "line") in "ctx".
91 void isl_ctx_set_full_error(isl_ctx *ctx, enum isl_error error, const char *msg,
92 const char *file, int line)
94 if (!ctx)
95 return;
96 ctx->error = error;
97 ctx->error_msg = msg;
98 ctx->error_file = file;
99 ctx->error_line = line;
102 void isl_handle_error(isl_ctx *ctx, enum isl_error error, const char *msg,
103 const char *file, int line)
105 if (!ctx)
106 return;
108 isl_ctx_set_full_error(ctx, error, msg, file, line);
110 switch (ctx->opt->on_error) {
111 case ISL_ON_ERROR_WARN:
112 fprintf(stderr, "%s:%d: %s\n", file, line, msg);
113 return;
114 case ISL_ON_ERROR_CONTINUE:
115 return;
116 case ISL_ON_ERROR_ABORT:
117 fprintf(stderr, "%s:%d: %s\n", file, line, msg);
118 abort();
119 return;
123 static struct isl_options *find_nested_options(struct isl_args *args,
124 void *opt, struct isl_args *wanted)
126 int i;
127 struct isl_options *options;
129 if (args == wanted)
130 return opt;
132 for (i = 0; args->args[i].type != isl_arg_end; ++i) {
133 struct isl_arg *arg = &args->args[i];
134 void *child;
136 if (arg->type != isl_arg_child)
137 continue;
139 if (arg->offset == (size_t) -1)
140 child = opt;
141 else
142 child = *(void **)(((char *)opt) + arg->offset);
144 options = find_nested_options(arg->u.child.child,
145 child, wanted);
146 if (options)
147 return options;
150 return NULL;
153 static struct isl_options *find_nested_isl_options(struct isl_args *args,
154 void *opt)
156 return find_nested_options(args, opt, &isl_options_args);
159 void *isl_ctx_peek_options(isl_ctx *ctx, struct isl_args *args)
161 if (!ctx)
162 return NULL;
163 if (args == &isl_options_args)
164 return ctx->opt;
165 return find_nested_options(ctx->user_args, ctx->user_opt, args);
168 isl_ctx *isl_ctx_alloc_with_options(struct isl_args *args, void *user_opt)
170 struct isl_ctx *ctx = NULL;
171 struct isl_options *opt = NULL;
172 int opt_allocated = 0;
174 if (!user_opt)
175 return NULL;
177 opt = find_nested_isl_options(args, user_opt);
178 if (!opt) {
179 opt = isl_options_new_with_defaults();
180 if (!opt)
181 goto error;
182 opt_allocated = 1;
185 ctx = __isl_calloc_type(struct isl_ctx);
186 if (!ctx)
187 goto error;
189 if (isl_hash_table_init(ctx, &ctx->id_table, 0))
190 goto error;
192 ctx->stats = isl_calloc_type(ctx, struct isl_stats);
193 if (!ctx->stats)
194 goto error;
196 ctx->user_args = args;
197 ctx->user_opt = user_opt;
198 ctx->opt_allocated = opt_allocated;
199 ctx->opt = opt;
200 ctx->ref = 0;
202 isl_int_init(ctx->zero);
203 isl_int_set_si(ctx->zero, 0);
205 isl_int_init(ctx->one);
206 isl_int_set_si(ctx->one, 1);
208 isl_int_init(ctx->two);
209 isl_int_set_si(ctx->two, 2);
211 isl_int_init(ctx->negone);
212 isl_int_set_si(ctx->negone, -1);
214 isl_int_init(ctx->normalize_gcd);
216 ctx->n_cached = 0;
217 ctx->n_miss = 0;
219 isl_ctx_reset_error(ctx);
221 ctx->operations = 0;
222 isl_ctx_set_max_operations(ctx, ctx->opt->max_operations);
224 return ctx;
225 error:
226 isl_args_free(args, user_opt);
227 if (opt_allocated)
228 isl_options_free(opt);
229 free(ctx);
230 return NULL;
233 struct isl_ctx *isl_ctx_alloc()
235 struct isl_options *opt;
237 opt = isl_options_new_with_defaults();
239 return isl_ctx_alloc_with_options(&isl_options_args, opt);
242 void isl_ctx_ref(struct isl_ctx *ctx)
244 ctx->ref++;
247 void isl_ctx_deref(struct isl_ctx *ctx)
249 isl_assert(ctx, ctx->ref > 0, return);
250 ctx->ref--;
253 /* Print statistics on usage.
255 static void print_stats(isl_ctx *ctx)
257 fprintf(stderr, "operations: %lu\n", ctx->operations);
260 void isl_ctx_free(struct isl_ctx *ctx)
262 if (!ctx)
263 return;
264 if (ctx->ref != 0)
265 isl_die(ctx, isl_error_invalid,
266 "isl_ctx freed, but some objects still reference it",
267 return);
269 if (ctx->opt->print_stats)
270 print_stats(ctx);
272 isl_hash_table_clear(&ctx->id_table);
273 isl_blk_clear_cache(ctx);
274 isl_int_clear(ctx->zero);
275 isl_int_clear(ctx->one);
276 isl_int_clear(ctx->two);
277 isl_int_clear(ctx->negone);
278 isl_int_clear(ctx->normalize_gcd);
279 isl_args_free(ctx->user_args, ctx->user_opt);
280 if (ctx->opt_allocated)
281 isl_options_free(ctx->opt);
282 free(ctx->stats);
283 free(ctx);
286 struct isl_options *isl_ctx_options(isl_ctx *ctx)
288 if (!ctx)
289 return NULL;
290 return ctx->opt;
293 enum isl_error isl_ctx_last_error(isl_ctx *ctx)
295 return ctx ? ctx->error : isl_error_invalid;
298 /* Return the error message of the last error in "ctx".
300 const char *isl_ctx_last_error_msg(isl_ctx *ctx)
302 return ctx ? ctx->error_msg : NULL;
305 /* Return the file name where the last error in "ctx" occurred.
307 const char *isl_ctx_last_error_file(isl_ctx *ctx)
309 return ctx ? ctx->error_file : NULL;
312 /* Return the line number where the last error in "ctx" occurred.
314 int isl_ctx_last_error_line(isl_ctx *ctx)
316 return ctx ? ctx->error_line : -1;
319 void isl_ctx_reset_error(isl_ctx *ctx)
321 if (!ctx)
322 return;
323 ctx->error = isl_error_none;
324 ctx->error_msg = NULL;
325 ctx->error_file = NULL;
326 ctx->error_line = -1;
329 void isl_ctx_set_error(isl_ctx *ctx, enum isl_error error)
331 isl_ctx_set_full_error(ctx, error, NULL, NULL, -1);
334 void isl_ctx_abort(isl_ctx *ctx)
336 if (ctx)
337 ctx->abort = 1;
340 void isl_ctx_resume(isl_ctx *ctx)
342 if (ctx)
343 ctx->abort = 0;
346 int isl_ctx_aborted(isl_ctx *ctx)
348 return ctx ? ctx->abort : -1;
351 int isl_ctx_parse_options(isl_ctx *ctx, int argc, char **argv, unsigned flags)
353 if (!ctx)
354 return -1;
355 return isl_args_parse(ctx->user_args, argc, argv, ctx->user_opt, flags);
358 /* Set the maximal number of iterations of "ctx" to "max_operations".
360 void isl_ctx_set_max_operations(isl_ctx *ctx, unsigned long max_operations)
362 if (!ctx)
363 return;
364 ctx->max_operations = max_operations;
367 /* Return the maximal number of iterations of "ctx".
369 unsigned long isl_ctx_get_max_operations(isl_ctx *ctx)
371 return ctx ? ctx->max_operations : 0;
374 /* Reset the number of operations performed by "ctx".
376 void isl_ctx_reset_operations(isl_ctx *ctx)
378 if (!ctx)
379 return;
380 ctx->operations = 0;