Coverage Report

Created: 2018-06-24 14:39

/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.27k
#define __isl_calloc(type,size)   ((type *)calloc(1, size))
15
1.27k
#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
56.7k
{
22
56.7k
  return b < 0 ? 
isl_bool_error0
: !b;
23
56.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
55.5M
{
31
55.5M
  if (p || 
size == 00
)
32
55.5M
    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
58.8M
{
45
58.8M
  if (!ctx)
46
0
    return -1;
47
58.8M
  if (ctx->abort) {
48
0
    isl_ctx_set_error(ctx, isl_error_abort);
49
0
    return -1;
50
0
  }
51
58.8M
  if (ctx->max_operations && 
ctx->operations >= ctx->max_operations9.86M
)
52
58.8M
    
isl_die107
(ctx, isl_error_quota,
53
58.8M
      "maximal number of operations exceeded", return -1);
54
58.8M
  ctx->operations++;
55
58.8M
  return 0;
56
58.8M
}
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
40.6M
{
63
40.6M
  if (isl_ctx_next_operation(ctx) < 0)
64
26
    return NULL;
65
40.6M
  return ctx ? check_non_null(ctx, malloc(size), size) : NULL;
66
40.6M
}
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
13.6M
{
73
13.6M
  if (isl_ctx_next_operation(ctx) < 0)
74
81
    return NULL;
75
13.6M
  return ctx ? check_non_null(ctx, calloc(nmemb, size), nmemb) : NULL;
76
13.6M
}
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
1.26M
{
83
1.26M
  if (isl_ctx_next_operation(ctx) < 0)
84
0
    return NULL;
85
1.26M
  return ctx ? check_non_null(ctx, realloc(ptr, size), size) : NULL;
86
1.26M
}
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
114
{
94
114
  if (!ctx)
95
0
    return;
96
114
  ctx->error = error;
97
114
  ctx->error_msg = msg;
98
114
  ctx->error_file = file;
99
114
  ctx->error_line = line;
100
114
}
101
102
void isl_handle_error(isl_ctx *ctx, enum isl_error error, const char *msg,
103
  const char *file, int line)
104
114
{
105
114
  if (!ctx)
106
0
    return;
107
114
108
114
  isl_ctx_set_full_error(ctx, error, msg, file, line);
109
114
110
114
  switch (ctx->opt->on_error) {
111
114
  
case 0
ISL_ON_ERROR_WARN0
:
112
0
    fprintf(stderr, "%s:%d: %s\n", file, line, msg);
113
0
    return;
114
114
  case ISL_ON_ERROR_CONTINUE:
115
114
    return;
116
114
  
case 0
ISL_ON_ERROR_ABORT0
:
117
0
    fprintf(stderr, "%s:%d: %s\n", file, line, msg);
118
0
    abort();
119
114
    
return0
;
120
114
  }
121
114
}
122
123
static struct isl_options *find_nested_options(struct isl_args *args,
124
  void *opt, struct isl_args *wanted)
125
1.27k
{
126
1.27k
  int i;
127
1.27k
  struct isl_options *options;
128
1.27k
129
1.27k
  if (args == wanted)
130
1.27k
    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.27k
{
156
1.27k
  return find_nested_options(args, opt, &isl_options_args);
157
1.27k
}
158
159
void *isl_ctx_peek_options(isl_ctx *ctx, struct isl_args *args)
160
36.4k
{
161
36.4k
  if (!ctx)
162
0
    return NULL;
163
36.4k
  if (args == &isl_options_args)
164
36.4k
    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.27k
{
170
1.27k
  struct isl_ctx *ctx = NULL;
171
1.27k
  struct isl_options *opt = NULL;
172
1.27k
  int opt_allocated = 0;
173
1.27k
174
1.27k
  if (!user_opt)
175
0
    return NULL;
176
1.27k
177
1.27k
  opt = find_nested_isl_options(args, user_opt);
178
1.27k
  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.27k
185
1.27k
  ctx = __isl_calloc_type(struct isl_ctx);
186
1.27k
  if (!ctx)
187
0
    goto error;
188
1.27k
189
1.27k
  if (isl_hash_table_init(ctx, &ctx->id_table, 0))
190
0
    goto error;
191
1.27k
192
1.27k
  ctx->stats = isl_calloc_type(ctx, struct isl_stats);
193
1.27k
  if (!ctx->stats)
194
0
    goto error;
195
1.27k
196
1.27k
  ctx->user_args = args;
197
1.27k
  ctx->user_opt = user_opt;
198
1.27k
  ctx->opt_allocated = opt_allocated;
199
1.27k
  ctx->opt = opt;
200
1.27k
  ctx->ref = 0;
201
1.27k
202
1.27k
  isl_int_init(ctx->zero);
203
1.27k
  isl_int_set_si(ctx->zero, 0);
204
1.27k
205
1.27k
  isl_int_init(ctx->one);
206
1.27k
  isl_int_set_si(ctx->one, 1);
207
1.27k
208
1.27k
  isl_int_init(ctx->two);
209
1.27k
  isl_int_set_si(ctx->two, 2);
210
1.27k
211
1.27k
  isl_int_init(ctx->negone);
212
1.27k
  isl_int_set_si(ctx->negone, -1);
213
1.27k
214
1.27k
  isl_int_init(ctx->normalize_gcd);
215
1.27k
216
1.27k
  ctx->n_cached = 0;
217
1.27k
  ctx->n_miss = 0;
218
1.27k
219
1.27k
  isl_ctx_reset_error(ctx);
220
1.27k
221
1.27k
  ctx->operations = 0;
222
1.27k
  isl_ctx_set_max_operations(ctx, ctx->opt->max_operations);
223
1.27k
224
1.27k
  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.27k
}
232
233
struct isl_ctx *isl_ctx_alloc()
234
1.27k
{
235
1.27k
  struct isl_options *opt;
236
1.27k
237
1.27k
  opt = isl_options_new_with_defaults();
238
1.27k
239
1.27k
  return isl_ctx_alloc_with_options(&isl_options_args, opt);
240
1.27k
}
241
242
void isl_ctx_ref(struct isl_ctx *ctx)
243
23.1M
{
244
23.1M
  ctx->ref++;
245
23.1M
}
246
247
void isl_ctx_deref(struct isl_ctx *ctx)
248
23.1M
{
249
23.1M
  isl_assert(ctx, ctx->ref > 0, return);
250
23.1M
  ctx->ref--;
251
23.1M
}
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.25k
{
262
1.25k
  if (!ctx)
263
0
    return;
264
1.25k
  if (ctx->ref != 0)
265
1.25k
    
isl_die0
(ctx, isl_error_invalid,
266
1.25k
      "isl_ctx freed, but some objects still reference it",
267
1.25k
      return);
268
1.25k
269
1.25k
  if (ctx->opt->print_stats)
270
0
    print_stats(ctx);
271
1.25k
272
1.25k
  isl_hash_table_clear(&ctx->id_table);
273
1.25k
  isl_blk_clear_cache(ctx);
274
1.25k
  isl_int_clear(ctx->zero);
275
1.25k
  isl_int_clear(ctx->one);
276
1.25k
  isl_int_clear(ctx->two);
277
1.25k
  isl_int_clear(ctx->negone);
278
1.25k
  isl_int_clear(ctx->normalize_gcd);
279
1.25k
  isl_args_free(ctx->user_args, ctx->user_opt);
280
1.25k
  if (ctx->opt_allocated)
281
0
    isl_options_free(ctx->opt);
282
1.25k
  free(ctx->stats);
283
1.25k
  free(ctx);
284
1.25k
}
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.09k
{
295
1.09k
  return ctx ? ctx->error : 
isl_error_invalid0
;
296
1.09k
}
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.43k
{
321
3.43k
  if (!ctx)
322
0
    return;
323
3.43k
  ctx->error = isl_error_none;
324
3.43k
  ctx->error_msg = NULL;
325
3.43k
  ctx->error_file = NULL;
326
3.43k
  ctx->error_line = -1;
327
3.43k
}
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.46k
{
362
3.46k
  if (!ctx)
363
0
    return;
364
3.46k
  ctx->max_operations = max_operations;
365
3.46k
}
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.05k
{
378
1.05k
  if (!ctx)
379
0
    return;
380
1.05k
  ctx->operations = 0;
381
1.05k
}