Coverage Report

Created: 2017-03-28 09:59

/Users/buildslave/jenkins/sharedspace/clang-stage2-coverage-R@2/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
986
#define __isl_calloc(type,size)   ((type *)calloc(1, size))
15
986
#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
23.8k
{
22
23.8k
  return b < 0 ? 
isl_bool_error0
:
!b23.8k
;
23
23.8k
}
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
32.1M
{
31
32.1M
  if (
p || 32.1M
size == 00
)
32
32.1M
    return p;
33
0
  
isl_die0
(ctx, isl_error_alloc, "allocation failure", return NULL);0
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
34.0M
{
45
34.0M
  if (!ctx)
46
0
    return -1;
47
34.0M
  
if (34.0M
ctx->abort34.0M
)
{0
48
0
    isl_ctx_set_error(ctx, isl_error_abort);
49
0
    return -1;
50
0
  }
51
34.0M
  
if (34.0M
ctx->max_operations && 34.0M
ctx->operations >= ctx->max_operations2.40M
)
52
44
    isl_die(ctx, isl_error_quota,
53
34.0M
      "maximal number of operations exceeded", return -1);
54
34.0M
  ctx->operations++;
55
34.0M
  return 0;
56
34.0M
}
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
23.0M
{
63
23.0M
  if (isl_ctx_next_operation(ctx) < 0)
64
6
    return NULL;
65
23.0M
  
return ctx ? 23.0M
check_non_null(ctx, malloc(size), size)23.0M
: NULL;
66
23.0M
}
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
8.41M
{
73
8.41M
  if (isl_ctx_next_operation(ctx) < 0)
74
38
    return NULL;
75
8.41M
  
return ctx ? 8.41M
check_non_null(ctx, calloc(nmemb, size), nmemb)8.41M
: NULL;
76
8.41M
}
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
685k
{
83
685k
  if (isl_ctx_next_operation(ctx) < 0)
84
0
    return NULL;
85
685k
  
return ctx ? 685k
check_non_null(ctx, realloc(ptr, size), size)685k
: NULL;
86
685k
}
87
88
void isl_handle_error(isl_ctx *ctx, enum isl_error error, const char *msg,
89
  const char *file, int line)
90
45
{
91
45
  if (!ctx)
92
0
    return;
93
45
94
45
  isl_ctx_set_error(ctx, error);
95
45
96
45
  switch (ctx->opt->on_error) {
97
0
  
case 0
ISL_ON_ERROR_WARN0
:
98
0
    fprintf(stderr, "%s:%d: %s\n", file, line, msg);
99
0
    return;
100
45
  
case 45
ISL_ON_ERROR_CONTINUE45
:
101
45
    return;
102
0
  
case 0
ISL_ON_ERROR_ABORT0
:
103
0
    fprintf(stderr, "%s:%d: %s\n", file, line, msg);
104
0
    abort();
105
0
    return;
106
45
  }
107
45
}
108
109
static struct isl_options *find_nested_options(struct isl_args *args,
110
  void *opt, struct isl_args *wanted)
111
986
{
112
986
  int i;
113
986
  struct isl_options *options;
114
986
115
986
  if (args == wanted)
116
986
    return opt;
117
986
118
0
  
for (i = 0; 0
args->args[i].type != isl_arg_end0
;
++i0
)
{0
119
0
    struct isl_arg *arg = &args->args[i];
120
0
    void *child;
121
0
122
0
    if (arg->type != isl_arg_child)
123
0
      continue;
124
0
125
0
    
if (0
arg->offset == (size_t) -10
)
126
0
      child = opt;
127
0
    else
128
0
      child = *(void **)(((char *)opt) + arg->offset);
129
0
130
0
    options = find_nested_options(arg->u.child.child,
131
0
            child, wanted);
132
0
    if (options)
133
0
      return options;
134
0
  }
135
0
136
0
  return NULL;
137
0
}
138
139
static struct isl_options *find_nested_isl_options(struct isl_args *args,
140
  void *opt)
141
986
{
142
986
  return find_nested_options(args, opt, &isl_options_args);
143
986
}
144
145
void *isl_ctx_peek_options(isl_ctx *ctx, struct isl_args *args)
146
20.2k
{
147
20.2k
  if (!ctx)
148
0
    return NULL;
149
20.2k
  
if (20.2k
args == &isl_options_args20.2k
)
150
20.2k
    return ctx->opt;
151
0
  return find_nested_options(ctx->user_args, ctx->user_opt, args);
152
20.2k
}
153
154
isl_ctx *isl_ctx_alloc_with_options(struct isl_args *args, void *user_opt)
155
986
{
156
986
  struct isl_ctx *ctx = NULL;
157
986
  struct isl_options *opt = NULL;
158
986
  int opt_allocated = 0;
159
986
160
986
  if (!user_opt)
161
0
    return NULL;
162
986
163
986
  opt = find_nested_isl_options(args, user_opt);
164
986
  if (
!opt986
)
{0
165
0
    opt = isl_options_new_with_defaults();
166
0
    if (!opt)
167
0
      goto error;
168
0
    opt_allocated = 1;
169
0
  }
170
986
171
986
  
ctx = 986
__isl_calloc_type986
(struct isl_ctx);
172
986
  if (!ctx)
173
0
    goto error;
174
986
175
986
  
if (986
isl_hash_table_init(ctx, &ctx->id_table, 0)986
)
176
0
    goto error;
177
986
178
986
  
ctx->stats = 986
isl_calloc_type986
(ctx, struct isl_stats);
179
986
  if (!ctx->stats)
180
0
    goto error;
181
986
182
986
  ctx->user_args = args;
183
986
  ctx->user_opt = user_opt;
184
986
  ctx->opt_allocated = opt_allocated;
185
986
  ctx->opt = opt;
186
986
  ctx->ref = 0;
187
986
188
986
  isl_int_init(ctx->zero);
189
986
  isl_int_set_si(ctx->zero, 0);
190
986
191
986
  isl_int_init(ctx->one);
192
986
  isl_int_set_si(ctx->one, 1);
193
986
194
986
  isl_int_init(ctx->two);
195
986
  isl_int_set_si(ctx->two, 2);
196
986
197
986
  isl_int_init(ctx->negone);
198
986
  isl_int_set_si(ctx->negone, -1);
199
986
200
986
  isl_int_init(ctx->normalize_gcd);
201
986
202
986
  ctx->n_cached = 0;
203
986
  ctx->n_miss = 0;
204
986
205
986
  ctx->error = isl_error_none;
206
986
207
986
  ctx->operations = 0;
208
986
  isl_ctx_set_max_operations(ctx, ctx->opt->max_operations);
209
986
210
986
  return ctx;
211
0
error:
212
0
  isl_args_free(args, user_opt);
213
0
  if (opt_allocated)
214
0
    isl_options_free(opt);
215
0
  free(ctx);
216
0
  return NULL;
217
986
}
218
219
struct isl_ctx *isl_ctx_alloc()
220
985
{
221
985
  struct isl_options *opt;
222
985
223
985
  opt = isl_options_new_with_defaults();
224
985
225
985
  return isl_ctx_alloc_with_options(&isl_options_args, opt);
226
985
}
227
228
void isl_ctx_ref(struct isl_ctx *ctx)
229
13.1M
{
230
13.1M
  ctx->ref++;
231
13.1M
}
232
233
void isl_ctx_deref(struct isl_ctx *ctx)
234
13.1M
{
235
13.1M
  isl_assert(ctx, ctx->ref > 0, return);
236
13.1M
  ctx->ref--;
237
13.1M
}
238
239
/* Print statistics on usage.
240
 */
241
static void print_stats(isl_ctx *ctx)
242
0
{
243
0
  fprintf(stderr, "operations: %lu\n", ctx->operations);
244
0
}
245
246
void isl_ctx_free(struct isl_ctx *ctx)
247
986
{
248
986
  if (!ctx)
249
0
    return;
250
986
  
if (986
ctx->ref != 0986
)
251
0
    isl_die(ctx, isl_error_invalid,
252
986
      "isl_ctx freed, but some objects still reference it",
253
986
      return);
254
986
255
986
  
if (986
ctx->opt->print_stats986
)
256
0
    print_stats(ctx);
257
986
258
986
  isl_hash_table_clear(&ctx->id_table);
259
986
  isl_blk_clear_cache(ctx);
260
986
  isl_int_clear(ctx->zero);
261
986
  isl_int_clear(ctx->one);
262
986
  isl_int_clear(ctx->two);
263
986
  isl_int_clear(ctx->negone);
264
986
  isl_int_clear(ctx->normalize_gcd);
265
986
  isl_args_free(ctx->user_args, ctx->user_opt);
266
986
  if (ctx->opt_allocated)
267
0
    isl_options_free(ctx->opt);
268
986
  free(ctx->stats);
269
986
  free(ctx);
270
986
}
271
272
struct isl_options *isl_ctx_options(isl_ctx *ctx)
273
0
{
274
0
  if (!ctx)
275
0
    return NULL;
276
0
  return ctx->opt;
277
0
}
278
279
enum isl_error isl_ctx_last_error(isl_ctx *ctx)
280
492
{
281
492
  return ctx->error;
282
492
}
283
284
void isl_ctx_reset_error(isl_ctx *ctx)
285
5
{
286
5
  ctx->error = isl_error_none;
287
5
}
288
289
void isl_ctx_set_error(isl_ctx *ctx, enum isl_error error)
290
45
{
291
45
  if (ctx)
292
45
    ctx->error = error;
293
45
}
294
295
void isl_ctx_abort(isl_ctx *ctx)
296
0
{
297
0
  if (ctx)
298
0
    ctx->abort = 1;
299
0
}
300
301
void isl_ctx_resume(isl_ctx *ctx)
302
0
{
303
0
  if (ctx)
304
0
    ctx->abort = 0;
305
0
}
306
307
int isl_ctx_aborted(isl_ctx *ctx)
308
0
{
309
0
  return ctx ? 
ctx->abort0
:
-10
;
310
0
}
311
312
int isl_ctx_parse_options(isl_ctx *ctx, int argc, char **argv, unsigned flags)
313
0
{
314
0
  if (!ctx)
315
0
    return -1;
316
0
  return isl_args_parse(ctx->user_args, argc, argv, ctx->user_opt, flags);
317
0
}
318
319
/* Set the maximal number of iterations of "ctx" to "max_operations".
320
 */
321
void isl_ctx_set_max_operations(isl_ctx *ctx, unsigned long max_operations)
322
2.00k
{
323
2.00k
  if (!ctx)
324
0
    return;
325
2.00k
  ctx->max_operations = max_operations;
326
2.00k
}
327
328
/* Return the maximal number of iterations of "ctx".
329
 */
330
unsigned long isl_ctx_get_max_operations(isl_ctx *ctx)
331
0
{
332
0
  return ctx ? 
ctx->max_operations0
:
00
;
333
0
}
334
335
/* Reset the number of operations performed by "ctx".
336
 */
337
void isl_ctx_reset_operations(isl_ctx *ctx)
338
507
{
339
507
  if (!ctx)
340
0
    return;
341
507
  ctx->operations = 0;
342
507
}