/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/polly/lib/External/isl/isl_ctx.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 2008-2009 Katholieke Universiteit Leuven |
3 | | * |
4 | | * Use of this software is governed by the MIT license |
5 | | * |
6 | | * Written by Sven Verdoolaege, K.U.Leuven, Departement |
7 | | * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium |
8 | | */ |
9 | | |
10 | | #include <isl_ctx_private.h> |
11 | | #include <isl/vec.h> |
12 | | #include <isl_options_private.h> |
13 | | |
14 | 1.29k | #define __isl_calloc(type,size) ((type *)calloc(1, size)) |
15 | 1.29k | #define __isl_calloc_type(type) __isl_calloc(type,sizeof(type)) |
16 | | |
17 | | /* Return the negation of "b", where the negation of isl_bool_error |
18 | | * is isl_bool_error again. |
19 | | */ |
20 | | isl_bool isl_bool_not(isl_bool b) |
21 | 83.7k | { |
22 | 83.7k | return b < 0 ? isl_bool_error0 : !b; |
23 | 83.7k | } |
24 | | |
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. |
28 | | */ |
29 | | static void *check_non_null(isl_ctx *ctx, void *p, size_t size) |
30 | 87.9M | { |
31 | 87.9M | if (p || size == 00 ) |
32 | 87.9M | return p; |
33 | 0 | isl_die(ctx, isl_error_alloc, "allocation failure", return NULL); |
34 | 0 | } |
35 | | |
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. |
39 | | * |
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. |
42 | | */ |
43 | | int isl_ctx_next_operation(isl_ctx *ctx) |
44 | 94.7M | { |
45 | 94.7M | if (!ctx) |
46 | 0 | return -1; |
47 | 94.7M | if (ctx->abort) { |
48 | 0 | isl_ctx_set_error(ctx, isl_error_abort); |
49 | 0 | return -1; |
50 | 0 | } |
51 | 94.7M | if (ctx->max_operations && ctx->operations >= ctx->max_operations9.85M ) |
52 | 94.7M | isl_die103 (ctx, isl_error_quota, |
53 | 94.7M | "maximal number of operations exceeded", return -1); |
54 | 94.7M | ctx->operations++; |
55 | 94.7M | return 0; |
56 | 94.7M | } |
57 | | |
58 | | /* Call malloc and complain if it fails. |
59 | | * If ctx is NULL, then return NULL. |
60 | | */ |
61 | | void *isl_malloc_or_die(isl_ctx *ctx, size_t size) |
62 | 67.5M | { |
63 | 67.5M | if (isl_ctx_next_operation(ctx) < 0) |
64 | 21 | return NULL; |
65 | 67.5M | return ctx ? check_non_null(ctx, malloc(size), size) : NULL; |
66 | 67.5M | } |
67 | | |
68 | | /* Call calloc and complain if it fails. |
69 | | * If ctx is NULL, then return NULL. |
70 | | */ |
71 | | void *isl_calloc_or_die(isl_ctx *ctx, size_t nmemb, size_t size) |
72 | 18.1M | { |
73 | 18.1M | if (isl_ctx_next_operation(ctx) < 0) |
74 | 82 | return NULL; |
75 | 18.1M | return ctx ? check_non_null(ctx, calloc(nmemb, size), nmemb) : NULL; |
76 | 18.1M | } |
77 | | |
78 | | /* Call realloc and complain if it fails. |
79 | | * If ctx is NULL, then return NULL. |
80 | | */ |
81 | | void *isl_realloc_or_die(isl_ctx *ctx, void *ptr, size_t size) |
82 | 2.24M | { |
83 | 2.24M | if (isl_ctx_next_operation(ctx) < 0) |
84 | 0 | return NULL; |
85 | 2.24M | return ctx ? check_non_null(ctx, realloc(ptr, size), size) : NULL; |
86 | 2.24M | } |
87 | | |
88 | | /* Keep track of all information about the current error ("error", "msg", |
89 | | * "file", "line") in "ctx". |
90 | | */ |
91 | | void isl_ctx_set_full_error(isl_ctx *ctx, enum isl_error error, const char *msg, |
92 | | const char *file, int line) |
93 | 111 | { |
94 | 111 | if (!ctx) |
95 | 0 | return; |
96 | 111 | ctx->error = error; |
97 | 111 | ctx->error_msg = msg; |
98 | 111 | ctx->error_file = file; |
99 | 111 | ctx->error_line = line; |
100 | 111 | } |
101 | | |
102 | | void isl_handle_error(isl_ctx *ctx, enum isl_error error, const char *msg, |
103 | | const char *file, int line) |
104 | 111 | { |
105 | 111 | if (!ctx) |
106 | 0 | return; |
107 | 111 | |
108 | 111 | isl_ctx_set_full_error(ctx, error, msg, file, line); |
109 | 111 | |
110 | 111 | switch (ctx->opt->on_error) { |
111 | 111 | case 0 ISL_ON_ERROR_WARN0 : |
112 | 0 | fprintf(stderr, "%s:%d: %s\n", file, line, msg); |
113 | 0 | return; |
114 | 111 | case ISL_ON_ERROR_CONTINUE: |
115 | 111 | return; |
116 | 111 | case 0 ISL_ON_ERROR_ABORT0 : |
117 | 0 | fprintf(stderr, "%s:%d: %s\n", file, line, msg); |
118 | 0 | abort(); |
119 | 111 | return0 ; |
120 | 111 | } |
121 | 111 | } |
122 | | |
123 | | static struct isl_options *find_nested_options(struct isl_args *args, |
124 | | void *opt, struct isl_args *wanted) |
125 | 1.29k | { |
126 | 1.29k | int i; |
127 | 1.29k | struct isl_options *options; |
128 | 1.29k | |
129 | 1.29k | if (args == wanted) |
130 | 1.29k | return opt; |
131 | 0 | |
132 | 0 | for (i = 0; args->args[i].type != isl_arg_end; ++i) { |
133 | 0 | struct isl_arg *arg = &args->args[i]; |
134 | 0 | void *child; |
135 | 0 |
|
136 | 0 | if (arg->type != isl_arg_child) |
137 | 0 | continue; |
138 | 0 | |
139 | 0 | if (arg->offset == (size_t) -1) |
140 | 0 | child = opt; |
141 | 0 | else |
142 | 0 | child = *(void **)(((char *)opt) + arg->offset); |
143 | 0 |
|
144 | 0 | options = find_nested_options(arg->u.child.child, |
145 | 0 | child, wanted); |
146 | 0 | if (options) |
147 | 0 | return options; |
148 | 0 | } |
149 | 0 |
|
150 | 0 | return NULL; |
151 | 0 | } |
152 | | |
153 | | static struct isl_options *find_nested_isl_options(struct isl_args *args, |
154 | | void *opt) |
155 | 1.29k | { |
156 | 1.29k | return find_nested_options(args, opt, &isl_options_args); |
157 | 1.29k | } |
158 | | |
159 | | void *isl_ctx_peek_options(isl_ctx *ctx, struct isl_args *args) |
160 | 58.1k | { |
161 | 58.1k | if (!ctx) |
162 | 0 | return NULL; |
163 | 58.1k | if (args == &isl_options_args) |
164 | 58.1k | return ctx->opt; |
165 | 0 | return find_nested_options(ctx->user_args, ctx->user_opt, args); |
166 | 0 | } |
167 | | |
168 | | isl_ctx *isl_ctx_alloc_with_options(struct isl_args *args, void *user_opt) |
169 | 1.29k | { |
170 | 1.29k | struct isl_ctx *ctx = NULL; |
171 | 1.29k | struct isl_options *opt = NULL; |
172 | 1.29k | int opt_allocated = 0; |
173 | 1.29k | |
174 | 1.29k | if (!user_opt) |
175 | 0 | return NULL; |
176 | 1.29k | |
177 | 1.29k | opt = find_nested_isl_options(args, user_opt); |
178 | 1.29k | if (!opt) { |
179 | 0 | opt = isl_options_new_with_defaults(); |
180 | 0 | if (!opt) |
181 | 0 | goto error; |
182 | 0 | opt_allocated = 1; |
183 | 0 | } |
184 | 1.29k | |
185 | 1.29k | ctx = __isl_calloc_type(struct isl_ctx); |
186 | 1.29k | if (!ctx) |
187 | 0 | goto error; |
188 | 1.29k | |
189 | 1.29k | if (isl_hash_table_init(ctx, &ctx->id_table, 0)) |
190 | 0 | goto error; |
191 | 1.29k | |
192 | 1.29k | ctx->stats = isl_calloc_type(ctx, struct isl_stats); |
193 | 1.29k | if (!ctx->stats) |
194 | 0 | goto error; |
195 | 1.29k | |
196 | 1.29k | ctx->user_args = args; |
197 | 1.29k | ctx->user_opt = user_opt; |
198 | 1.29k | ctx->opt_allocated = opt_allocated; |
199 | 1.29k | ctx->opt = opt; |
200 | 1.29k | ctx->ref = 0; |
201 | 1.29k | |
202 | 1.29k | isl_int_init(ctx->zero); |
203 | 1.29k | isl_int_set_si(ctx->zero, 0); |
204 | 1.29k | |
205 | 1.29k | isl_int_init(ctx->one); |
206 | 1.29k | isl_int_set_si(ctx->one, 1); |
207 | 1.29k | |
208 | 1.29k | isl_int_init(ctx->two); |
209 | 1.29k | isl_int_set_si(ctx->two, 2); |
210 | 1.29k | |
211 | 1.29k | isl_int_init(ctx->negone); |
212 | 1.29k | isl_int_set_si(ctx->negone, -1); |
213 | 1.29k | |
214 | 1.29k | isl_int_init(ctx->normalize_gcd); |
215 | 1.29k | |
216 | 1.29k | ctx->n_cached = 0; |
217 | 1.29k | ctx->n_miss = 0; |
218 | 1.29k | |
219 | 1.29k | isl_ctx_reset_error(ctx); |
220 | 1.29k | |
221 | 1.29k | ctx->operations = 0; |
222 | 1.29k | isl_ctx_set_max_operations(ctx, ctx->opt->max_operations); |
223 | 1.29k | |
224 | 1.29k | return ctx; |
225 | 0 | error: |
226 | 0 | isl_args_free(args, user_opt); |
227 | 0 | if (opt_allocated) |
228 | 0 | isl_options_free(opt); |
229 | 0 | free(ctx); |
230 | 0 | return NULL; |
231 | 1.29k | } |
232 | | |
233 | | struct isl_ctx *isl_ctx_alloc() |
234 | 1.29k | { |
235 | 1.29k | struct isl_options *opt; |
236 | 1.29k | |
237 | 1.29k | opt = isl_options_new_with_defaults(); |
238 | 1.29k | |
239 | 1.29k | return isl_ctx_alloc_with_options(&isl_options_args, opt); |
240 | 1.29k | } |
241 | | |
242 | | void isl_ctx_ref(struct isl_ctx *ctx) |
243 | 36.7M | { |
244 | 36.7M | ctx->ref++; |
245 | 36.7M | } |
246 | | |
247 | | void isl_ctx_deref(struct isl_ctx *ctx) |
248 | 36.7M | { |
249 | 36.7M | isl_assert(ctx, ctx->ref > 0, return); |
250 | 36.7M | ctx->ref--; |
251 | 36.7M | } |
252 | | |
253 | | /* Print statistics on usage. |
254 | | */ |
255 | | static void print_stats(isl_ctx *ctx) |
256 | 0 | { |
257 | 0 | fprintf(stderr, "operations: %lu\n", ctx->operations); |
258 | 0 | } |
259 | | |
260 | | void isl_ctx_free(struct isl_ctx *ctx) |
261 | 1.26k | { |
262 | 1.26k | if (!ctx) |
263 | 0 | return; |
264 | 1.26k | if (ctx->ref != 0) |
265 | 1.26k | isl_die0 (ctx, isl_error_invalid, |
266 | 1.26k | "isl_ctx freed, but some objects still reference it", |
267 | 1.26k | return); |
268 | 1.26k | |
269 | 1.26k | if (ctx->opt->print_stats) |
270 | 0 | print_stats(ctx); |
271 | 1.26k | |
272 | 1.26k | isl_hash_table_clear(&ctx->id_table); |
273 | 1.26k | isl_blk_clear_cache(ctx); |
274 | 1.26k | isl_int_clear(ctx->zero); |
275 | 1.26k | isl_int_clear(ctx->one); |
276 | 1.26k | isl_int_clear(ctx->two); |
277 | 1.26k | isl_int_clear(ctx->negone); |
278 | 1.26k | isl_int_clear(ctx->normalize_gcd); |
279 | 1.26k | isl_args_free(ctx->user_args, ctx->user_opt); |
280 | 1.26k | if (ctx->opt_allocated) |
281 | 0 | isl_options_free(ctx->opt); |
282 | 1.26k | free(ctx->stats); |
283 | 1.26k | free(ctx); |
284 | 1.26k | } |
285 | | |
286 | | struct isl_options *isl_ctx_options(isl_ctx *ctx) |
287 | 0 | { |
288 | 0 | if (!ctx) |
289 | 0 | return NULL; |
290 | 0 | return ctx->opt; |
291 | 0 | } |
292 | | |
293 | | enum isl_error isl_ctx_last_error(isl_ctx *ctx) |
294 | 1.10k | { |
295 | 1.10k | return ctx ? ctx->error : isl_error_invalid0 ; |
296 | 1.10k | } |
297 | | |
298 | | /* Return the error message of the last error in "ctx". |
299 | | */ |
300 | | const char *isl_ctx_last_error_msg(isl_ctx *ctx) |
301 | 0 | { |
302 | 0 | return ctx ? ctx->error_msg : NULL; |
303 | 0 | } |
304 | | |
305 | | /* Return the file name where the last error in "ctx" occurred. |
306 | | */ |
307 | | const char *isl_ctx_last_error_file(isl_ctx *ctx) |
308 | 0 | { |
309 | 0 | return ctx ? ctx->error_file : NULL; |
310 | 0 | } |
311 | | |
312 | | /* Return the line number where the last error in "ctx" occurred. |
313 | | */ |
314 | | int isl_ctx_last_error_line(isl_ctx *ctx) |
315 | 0 | { |
316 | 0 | return ctx ? ctx->error_line : -1; |
317 | 0 | } |
318 | | |
319 | | void isl_ctx_reset_error(isl_ctx *ctx) |
320 | 3.48k | { |
321 | 3.48k | if (!ctx) |
322 | 0 | return; |
323 | 3.48k | ctx->error = isl_error_none; |
324 | 3.48k | ctx->error_msg = NULL; |
325 | 3.48k | ctx->error_file = NULL; |
326 | 3.48k | ctx->error_line = -1; |
327 | 3.48k | } |
328 | | |
329 | | void isl_ctx_set_error(isl_ctx *ctx, enum isl_error error) |
330 | 0 | { |
331 | 0 | isl_ctx_set_full_error(ctx, error, NULL, NULL, -1); |
332 | 0 | } |
333 | | |
334 | | void isl_ctx_abort(isl_ctx *ctx) |
335 | 0 | { |
336 | 0 | if (ctx) |
337 | 0 | ctx->abort = 1; |
338 | 0 | } |
339 | | |
340 | | void isl_ctx_resume(isl_ctx *ctx) |
341 | 0 | { |
342 | 0 | if (ctx) |
343 | 0 | ctx->abort = 0; |
344 | 0 | } |
345 | | |
346 | | int isl_ctx_aborted(isl_ctx *ctx) |
347 | 0 | { |
348 | 0 | return ctx ? ctx->abort : -1; |
349 | 0 | } |
350 | | |
351 | | int isl_ctx_parse_options(isl_ctx *ctx, int argc, char **argv, unsigned flags) |
352 | 0 | { |
353 | 0 | if (!ctx) |
354 | 0 | return -1; |
355 | 0 | return isl_args_parse(ctx->user_args, argc, argv, ctx->user_opt, flags); |
356 | 0 | } |
357 | | |
358 | | /* Set the maximal number of iterations of "ctx" to "max_operations". |
359 | | */ |
360 | | void isl_ctx_set_max_operations(isl_ctx *ctx, unsigned long max_operations) |
361 | 3.51k | { |
362 | 3.51k | if (!ctx) |
363 | 0 | return; |
364 | 3.51k | ctx->max_operations = max_operations; |
365 | 3.51k | } |
366 | | |
367 | | /* Return the maximal number of iterations of "ctx". |
368 | | */ |
369 | | unsigned long isl_ctx_get_max_operations(isl_ctx *ctx) |
370 | 0 | { |
371 | 0 | return ctx ? ctx->max_operations : 0; |
372 | 0 | } |
373 | | |
374 | | /* Reset the number of operations performed by "ctx". |
375 | | */ |
376 | | void isl_ctx_reset_operations(isl_ctx *ctx) |
377 | 1.07k | { |
378 | 1.07k | if (!ctx) |
379 | 0 | return; |
380 | 1.07k | ctx->operations = 0; |
381 | 1.07k | } |