Coverage Report

Created: 2017-06-28 17:40

/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
1.11k
#define __isl_calloc(type,size)   ((type *)calloc(1, size))
15
1.11k
#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
27.9k
{
22
27.9k
  return b < 0 ? 
isl_bool_error0
:
!b27.9k
;
23
27.9k
}
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
39.8M
{
31
39.8M
  if (
p || 39.8M
size == 00
)
32
39.8M
    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
41.9M
{
45
41.9M
  if (!ctx)
46
0
    return -1;
47
41.9M
  
if (41.9M
ctx->abort41.9M
)
{0
48
0
    isl_ctx_set_error(ctx, isl_error_abort);
49
0
    return -1;
50
0
  }
51
41.9M
  
if (41.9M
ctx->max_operations && 41.9M
ctx->operations >= ctx->max_operations6.86M
)
52
46
    isl_die(ctx, isl_error_quota,
53
41.9M
      "maximal number of operations exceeded", return -1);
54
41.9M
  ctx->operations++;
55
41.9M
  return 0;
56
41.9M
}
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
28.2M
{
63
28.2M
  if (isl_ctx_next_operation(ctx) < 0)
64
13
    return NULL;
65
28.2M
  
return ctx ? 28.2M
check_non_null(ctx, malloc(size), size)28.2M
: NULL;
66
28.2M
}
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
10.6M
{
73
10.6M
  if (isl_ctx_next_operation(ctx) < 0)
74
33
    return NULL;
75
10.6M
  
return ctx ? 10.6M
check_non_null(ctx, calloc(nmemb, size), nmemb)10.6M
: NULL;
76
10.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
867k
{
83
867k
  if (isl_ctx_next_operation(ctx) < 0)
84
0
    return NULL;
85
867k
  
return ctx ? 867k
check_non_null(ctx, realloc(ptr, size), size)867k
: NULL;
86
867k
}
87
88
void isl_handle_error(isl_ctx *ctx, enum isl_error error, const char *msg,
89
  const char *file, int line)
90
47
{
91
47
  if (!ctx)
92
0
    return;
93
47
94
47
  isl_ctx_set_error(ctx, error);
95
47
96
47
  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
47
  
case 47
ISL_ON_ERROR_CONTINUE47
:
101
47
    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
47
  }
107
47
}
108
109
static struct isl_options *find_nested_options(struct isl_args *args,
110
  void *opt, struct isl_args *wanted)
111
1.11k
{
112
1.11k
  int i;
113
1.11k
  struct isl_options *options;
114
1.11k
115
1.11k
  if (args == wanted)
116
1.11k
    return opt;
117
1.11k
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
1.11k
{
142
1.11k
  return find_nested_options(args, opt, &isl_options_args);
143
1.11k
}
144
145
void *isl_ctx_peek_options(isl_ctx *ctx, struct isl_args *args)
146
24.1k
{
147
24.1k
  if (!ctx)
148
0
    return NULL;
149
24.1k
  
if (24.1k
args == &isl_options_args24.1k
)
150
24.1k
    return ctx->opt;
151
0
  return find_nested_options(ctx->user_args, ctx->user_opt, args);
152
24.1k
}
153
154
isl_ctx *isl_ctx_alloc_with_options(struct isl_args *args, void *user_opt)
155
1.11k
{
156
1.11k
  struct isl_ctx *ctx = NULL;
157
1.11k
  struct isl_options *opt = NULL;
158
1.11k
  int opt_allocated = 0;
159
1.11k
160
1.11k
  if (!user_opt)
161
0
    return NULL;
162
1.11k
163
1.11k
  opt = find_nested_isl_options(args, user_opt);
164
1.11k
  if (
!opt1.11k
)
{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
1.11k
171
1.11k
  
ctx = 1.11k
__isl_calloc_type1.11k
(struct isl_ctx);
172
1.11k
  if (!ctx)
173
0
    goto error;
174
1.11k
175
1.11k
  
if (1.11k
isl_hash_table_init(ctx, &ctx->id_table, 0)1.11k
)
176
0
    goto error;
177
1.11k
178
1.11k
  
ctx->stats = 1.11k
isl_calloc_type1.11k
(ctx, struct isl_stats);
179
1.11k
  if (!ctx->stats)
180
0
    goto error;
181
1.11k
182
1.11k
  ctx->user_args = args;
183
1.11k
  ctx->user_opt = user_opt;
184
1.11k
  ctx->opt_allocated = opt_allocated;
185
1.11k
  ctx->opt = opt;
186
1.11k
  ctx->ref = 0;
187
1.11k
188
1.11k
  isl_int_init(ctx->zero);
189
1.11k
  isl_int_set_si(ctx->zero, 0);
190
1.11k
191
1.11k
  isl_int_init(ctx->one);
192
1.11k
  isl_int_set_si(ctx->one, 1);
193
1.11k
194
1.11k
  isl_int_init(ctx->two);
195
1.11k
  isl_int_set_si(ctx->two, 2);
196
1.11k
197
1.11k
  isl_int_init(ctx->negone);
198
1.11k
  isl_int_set_si(ctx->negone, -1);
199
1.11k
200
1.11k
  isl_int_init(ctx->normalize_gcd);
201
1.11k
202
1.11k
  ctx->n_cached = 0;
203
1.11k
  ctx->n_miss = 0;
204
1.11k
205
1.11k
  ctx->error = isl_error_none;
206
1.11k
207
1.11k
  ctx->operations = 0;
208
1.11k
  isl_ctx_set_max_operations(ctx, ctx->opt->max_operations);
209
1.11k
210
1.11k
  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
1.11k
}
218
219
struct isl_ctx *isl_ctx_alloc()
220
1.11k
{
221
1.11k
  struct isl_options *opt;
222
1.11k
223
1.11k
  opt = isl_options_new_with_defaults();
224
1.11k
225
1.11k
  return isl_ctx_alloc_with_options(&isl_options_args, opt);
226
1.11k
}
227
228
void isl_ctx_ref(struct isl_ctx *ctx)
229
16.0M
{
230
16.0M
  ctx->ref++;
231
16.0M
}
232
233
void isl_ctx_deref(struct isl_ctx *ctx)
234
16.0M
{
235
16.0M
  isl_assert(ctx, ctx->ref > 0, return);
236
16.0M
  ctx->ref--;
237
16.0M
}
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
1.11k
{
248
1.11k
  if (!ctx)
249
0
    return;
250
1.11k
  
if (1.11k
ctx->ref != 01.11k
)
251
0
    isl_die(ctx, isl_error_invalid,
252
1.11k
      "isl_ctx freed, but some objects still reference it",
253
1.11k
      return);
254
1.11k
255
1.11k
  
if (1.11k
ctx->opt->print_stats1.11k
)
256
0
    print_stats(ctx);
257
1.11k
258
1.11k
  isl_hash_table_clear(&ctx->id_table);
259
1.11k
  isl_blk_clear_cache(ctx);
260
1.11k
  isl_int_clear(ctx->zero);
261
1.11k
  isl_int_clear(ctx->one);
262
1.11k
  isl_int_clear(ctx->two);
263
1.11k
  isl_int_clear(ctx->negone);
264
1.11k
  isl_int_clear(ctx->normalize_gcd);
265
1.11k
  isl_args_free(ctx->user_args, ctx->user_opt);
266
1.11k
  if (ctx->opt_allocated)
267
0
    isl_options_free(ctx->opt);
268
1.11k
  free(ctx->stats);
269
1.11k
  free(ctx);
270
1.11k
}
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
1.33k
{
281
1.33k
  return ctx->error;
282
1.33k
}
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
47
{
291
47
  if (ctx)
292
47
    ctx->error = error;
293
47
}
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.93k
{
323
2.93k
  if (!ctx)
324
0
    return;
325
2.93k
  ctx->max_operations = max_operations;
326
2.93k
}
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
912
{
339
912
  if (!ctx)
340
0
    return;
341
912
  ctx->operations = 0;
342
912
}