/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/polly/lib/External/isl/isl_blk.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_blk.h> |
11 | | #include <isl_ctx_private.h> |
12 | | |
13 | | /* The maximal number of cache misses before first element is evicted */ |
14 | 630k | #define ISL_BLK_MAX_MISS 100 |
15 | | |
16 | | struct isl_blk isl_blk_empty() |
17 | 34.8M | { |
18 | 34.8M | struct isl_blk block; |
19 | 34.8M | block.size = 0; |
20 | 34.8M | block.data = NULL; |
21 | 34.8M | return block; |
22 | 34.8M | } |
23 | | |
24 | | static int isl_blk_is_empty(struct isl_blk block) |
25 | 29.4M | { |
26 | 29.4M | return block.size == 0 && block.data == NULL11.0M ; |
27 | 29.4M | } |
28 | | |
29 | | static struct isl_blk isl_blk_error() |
30 | 0 | { |
31 | 0 | struct isl_blk block; |
32 | 0 | block.size = -1; |
33 | 0 | block.data = NULL; |
34 | 0 | return block; |
35 | 0 | } |
36 | | |
37 | | int isl_blk_is_error(struct isl_blk block) |
38 | 40.6M | { |
39 | 40.6M | return block.size == -1 && block.data == NULL0 ; |
40 | 40.6M | } |
41 | | |
42 | | static void isl_blk_free_force(struct isl_ctx *ctx, struct isl_blk block) |
43 | 875k | { |
44 | 875k | int i; |
45 | 875k | |
46 | 119M | for (i = 0; i < block.size; ++i118M ) |
47 | 118M | isl_int_clear(block.data[i]); |
48 | 875k | free(block.data); |
49 | 875k | } |
50 | | |
51 | | static struct isl_blk extend(struct isl_ctx *ctx, struct isl_blk block, |
52 | | size_t new_n) |
53 | 22.7M | { |
54 | 22.7M | int i; |
55 | 22.7M | isl_int *p; |
56 | 22.7M | |
57 | 22.7M | if (block.size >= new_n) |
58 | 21.1M | return block; |
59 | 1.59M | |
60 | 1.59M | p = isl_realloc_array(ctx, block.data, isl_int, new_n); |
61 | 1.59M | if (!p) { |
62 | 0 | isl_blk_free_force(ctx, block); |
63 | 0 | return isl_blk_error(); |
64 | 0 | } |
65 | 1.59M | block.data = p; |
66 | 1.59M | |
67 | 119M | for (i = block.size; i < new_n; ++i118M ) |
68 | 118M | isl_int_init(block.data[i]); |
69 | 1.59M | block.size = new_n; |
70 | 1.59M | |
71 | 1.59M | return block; |
72 | 1.59M | } |
73 | | |
74 | | struct isl_blk isl_blk_alloc(struct isl_ctx *ctx, size_t n) |
75 | 22.5M | { |
76 | 22.5M | int i; |
77 | 22.5M | struct isl_blk block; |
78 | 22.5M | |
79 | 22.5M | block = isl_blk_empty(); |
80 | 22.5M | if (n && ctx->n_cached18.1M ) { |
81 | 17.8M | int best = 0; |
82 | 183M | for (i = 1; ctx->cache[best].size != n && i < ctx->n_cached179M ; ++i165M ) { |
83 | 165M | if (ctx->cache[best].size < n) { |
84 | 20.2M | if (ctx->cache[i].size > ctx->cache[best].size) |
85 | 10.2M | best = i; |
86 | 145M | } else if (ctx->cache[i].size >= n && |
87 | 145M | ctx->cache[i].size < ctx->cache[best].size85.5M ) |
88 | 27.5M | best = i; |
89 | 165M | } |
90 | 17.8M | if (ctx->cache[best].size < 2 * n + 100) { |
91 | 17.2M | block = ctx->cache[best]; |
92 | 17.2M | if (--ctx->n_cached != best) |
93 | 12.9M | ctx->cache[best] = ctx->cache[ctx->n_cached]; |
94 | 17.2M | if (best == 0) |
95 | 1.97M | ctx->n_miss = 0; |
96 | 17.2M | } else if (630k ctx->n_miss++ >= 630k ISL_BLK_MAX_MISS630k ) { |
97 | 250 | isl_blk_free_force(ctx, ctx->cache[0]); |
98 | 250 | if (--ctx->n_cached != 0) |
99 | 250 | ctx->cache[0] = ctx->cache[ctx->n_cached]; |
100 | 250 | ctx->n_miss = 0; |
101 | 250 | } |
102 | 17.8M | } |
103 | 22.5M | |
104 | 22.5M | return extend(ctx, block, n); |
105 | 22.5M | } |
106 | | |
107 | | struct isl_blk isl_blk_extend(struct isl_ctx *ctx, struct isl_blk block, |
108 | | size_t new_n) |
109 | 417k | { |
110 | 417k | if (isl_blk_is_empty(block)) |
111 | 177k | return isl_blk_alloc(ctx, new_n); |
112 | 239k | |
113 | 239k | return extend(ctx, block, new_n); |
114 | 239k | } |
115 | | |
116 | | void isl_blk_free(struct isl_ctx *ctx, struct isl_blk block) |
117 | 28.9M | { |
118 | 28.9M | if (isl_blk_is_empty(block) || isl_blk_is_error(block)18.1M ) |
119 | 10.8M | return; |
120 | 18.1M | |
121 | 18.1M | if (ctx->n_cached < ISL_BLK_CACHE_SIZE) |
122 | 18.1M | ctx->cache[ctx->n_cached++] = block17.2M ; |
123 | 851k | else |
124 | 851k | isl_blk_free_force(ctx, block); |
125 | 18.1M | } |
126 | | |
127 | | void isl_blk_clear_cache(struct isl_ctx *ctx) |
128 | 1.26k | { |
129 | 1.26k | int i; |
130 | 1.26k | |
131 | 25.2k | for (i = 0; i < ctx->n_cached; ++i24.0k ) |
132 | 24.0k | isl_blk_free_force(ctx, ctx->cache[i]); |
133 | 1.26k | ctx->n_cached = 0; |
134 | 1.26k | } |