Coverage Report

Created: 2017-10-03 07:32

/Users/buildslave/jenkins/sharedspace/clang-stage2-coverage-R@2/llvm/tools/polly/lib/Support/GICHelper.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- GmpConv.cpp - Recreate LLVM IR from the Scop.  ---------------------===//
2
//
3
//                     The LLVM Compiler Infrastructure
4
//
5
// This file is distributed under the University of Illinois Open Source
6
// License. See LICENSE.TXT for details.
7
//
8
//===----------------------------------------------------------------------===//
9
//
10
// Functions for converting between gmp objects and llvm::APInt.
11
//
12
//===----------------------------------------------------------------------===//
13
#include "polly/Support/GICHelper.h"
14
#include "llvm/IR/Value.h"
15
#include "isl/aff.h"
16
#include "isl/map.h"
17
#include "isl/schedule.h"
18
#include "isl/set.h"
19
#include "isl/space.h"
20
#include "isl/union_map.h"
21
#include "isl/union_set.h"
22
#include "isl/val.h"
23
24
#include <climits>
25
26
using namespace llvm;
27
28
__isl_give isl_val *polly::isl_valFromAPInt(isl_ctx *Ctx, const APInt Int,
29
19.4k
                                            bool IsSigned) {
30
19.4k
  APInt Abs;
31
19.4k
  isl_val *v;
32
19.4k
33
19.4k
  // As isl is interpreting the input always as unsigned value, we need some
34
19.4k
  // additional pre and post processing to import signed values. The approach
35
19.4k
  // we take is to first obtain the absolute value of Int and then negate the
36
19.4k
  // value after it has been imported to isl.
37
19.4k
  //
38
19.4k
  // It should be noted that the smallest integer value represented in two's
39
19.4k
  // complement with a certain amount of bits does not have a corresponding
40
19.4k
  // positive representation in two's complement representation with the same
41
19.4k
  // number of bits. E.g. 110 (-2) does not have a corresponding value for (2).
42
19.4k
  // To ensure that there is always a corresponding value available we first
43
19.4k
  // sign-extend the input by one bit and only then take the absolute value.
44
19.4k
  if (IsSigned)
45
19.4k
    Abs = Int.sext(Int.getBitWidth() + 1).abs();
46
19.4k
  else
47
24
    Abs = Int;
48
19.4k
49
19.4k
  const uint64_t *Data = Abs.getRawData();
50
19.4k
  unsigned Words = Abs.getNumWords();
51
19.4k
52
19.4k
  v = isl_val_int_from_chunks(Ctx, Words, sizeof(uint64_t), Data);
53
19.4k
54
19.4k
  if (
IsSigned && 19.4k
Int.isNegative()19.4k
)
55
1.40k
    v = isl_val_neg(v);
56
19.4k
57
19.4k
  return v;
58
19.4k
}
59
60
3.47k
APInt polly::APIntFromVal(__isl_take isl_val *Val) {
61
3.47k
  uint64_t *Data;
62
3.47k
  int NumChunks;
63
3.47k
  const static int ChunkSize = sizeof(uint64_t);
64
3.47k
65
3.47k
  assert(isl_val_is_int(Val) && "Only integers can be converted to APInt");
66
3.47k
67
3.47k
  NumChunks = isl_val_n_abs_num_chunks(Val, ChunkSize);
68
3.47k
  Data = (uint64_t *)malloc(NumChunks * ChunkSize);
69
3.47k
  isl_val_get_abs_num_chunks(Val, ChunkSize, Data);
70
3.47k
  int NumBits = CHAR_BIT * ChunkSize * NumChunks;
71
3.47k
  APInt A(NumBits, NumChunks, Data);
72
3.47k
73
3.47k
  // As isl provides only an interface to obtain data that describes the
74
3.47k
  // absolute value of an isl_val, A at this point always contains a positive
75
3.47k
  // number. In case Val was originally negative, we expand the size of A by
76
3.47k
  // one and negate the value (in two's complement representation). As a result,
77
3.47k
  // the new value in A corresponds now with Val.
78
3.47k
  if (
isl_val_is_neg(Val)3.47k
) {
79
117
    A = A.zext(A.getBitWidth() + 1);
80
117
    A = -A;
81
117
  }
82
3.47k
83
3.47k
  // isl may represent small numbers with more than the minimal number of bits.
84
3.47k
  // We truncate the APInt to the minimal number of bits needed to represent the
85
3.47k
  // signed value it contains, to ensure that the bitwidth is always minimal.
86
3.47k
  if (A.getMinSignedBits() < A.getBitWidth())
87
3.46k
    A = A.trunc(A.getMinSignedBits());
88
3.47k
89
3.47k
  free(Data);
90
3.47k
  isl_val_free(Val);
91
3.47k
  return A;
92
3.47k
}
93
94
template <typename ISLTy, typename ISL_CTX_GETTER, typename ISL_PRINTER>
95
static inline std::string stringFromIslObjInternal(__isl_keep ISLTy *isl_obj,
96
                                                   ISL_CTX_GETTER ctx_getter_fn,
97
6.38k
                                                   ISL_PRINTER printer_fn) {
98
6.38k
  if (!isl_obj)
99
16
    return "null";
100
6.36k
  isl_ctx *ctx = ctx_getter_fn(isl_obj);
101
6.36k
  isl_printer *p = isl_printer_to_str(ctx);
102
6.36k
  p = printer_fn(p, isl_obj);
103
6.36k
  char *char_str = isl_printer_get_str(p);
104
6.36k
  std::string string;
105
6.36k
  if (char_str)
106
6.36k
    string = char_str;
107
6.36k
  else
108
0
    string = "null";
109
6.38k
  free(char_str);
110
6.38k
  isl_printer_free(p);
111
6.38k
  return string;
112
6.38k
}
GICHelper.cpp:std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > stringFromIslObjInternal<isl_map, isl_ctx* (*)(isl_map*), isl_printer* (*)(isl_printer*, isl_map*)>(isl_map*, isl_ctx* (*)(isl_map*), isl_printer* (*)(isl_printer*, isl_map*))
Line
Count
Source
97
3.34k
                                                   ISL_PRINTER printer_fn) {
98
3.34k
  if (!isl_obj)
99
16
    return "null";
100
3.32k
  isl_ctx *ctx = ctx_getter_fn(isl_obj);
101
3.32k
  isl_printer *p = isl_printer_to_str(ctx);
102
3.32k
  p = printer_fn(p, isl_obj);
103
3.32k
  char *char_str = isl_printer_get_str(p);
104
3.32k
  std::string string;
105
3.32k
  if (char_str)
106
3.32k
    string = char_str;
107
3.32k
  else
108
0
    string = "null";
109
3.34k
  free(char_str);
110
3.34k
  isl_printer_free(p);
111
3.34k
  return string;
112
3.34k
}
GICHelper.cpp:std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > stringFromIslObjInternal<isl_set, isl_ctx* (*)(isl_set*), isl_printer* (*)(isl_printer*, isl_set*)>(isl_set*, isl_ctx* (*)(isl_set*), isl_printer* (*)(isl_printer*, isl_set*))
Line
Count
Source
97
2.26k
                                                   ISL_PRINTER printer_fn) {
98
2.26k
  if (!isl_obj)
99
0
    return "null";
100
2.26k
  isl_ctx *ctx = ctx_getter_fn(isl_obj);
101
2.26k
  isl_printer *p = isl_printer_to_str(ctx);
102
2.26k
  p = printer_fn(p, isl_obj);
103
2.26k
  char *char_str = isl_printer_get_str(p);
104
2.26k
  std::string string;
105
2.26k
  if (char_str)
106
2.26k
    string = char_str;
107
2.26k
  else
108
0
    string = "null";
109
2.26k
  free(char_str);
110
2.26k
  isl_printer_free(p);
111
2.26k
  return string;
112
2.26k
}
GICHelper.cpp:std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > stringFromIslObjInternal<isl_union_map, isl_ctx* (*)(isl_union_map*), isl_printer* (*)(isl_printer*, isl_union_map*)>(isl_union_map*, isl_ctx* (*)(isl_union_map*), isl_printer* (*)(isl_printer*, isl_union_map*))
Line
Count
Source
97
244
                                                   ISL_PRINTER printer_fn) {
98
244
  if (!isl_obj)
99
0
    return "null";
100
244
  isl_ctx *ctx = ctx_getter_fn(isl_obj);
101
244
  isl_printer *p = isl_printer_to_str(ctx);
102
244
  p = printer_fn(p, isl_obj);
103
244
  char *char_str = isl_printer_get_str(p);
104
244
  std::string string;
105
244
  if (char_str)
106
244
    string = char_str;
107
244
  else
108
0
    string = "null";
109
244
  free(char_str);
110
244
  isl_printer_free(p);
111
244
  return string;
112
244
}
Unexecuted instantiation: GICHelper.cpp:std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > stringFromIslObjInternal<isl_union_set, isl_ctx* (*)(isl_union_set*), isl_printer* (*)(isl_printer*, isl_union_set*)>(isl_union_set*, isl_ctx* (*)(isl_union_set*), isl_printer* (*)(isl_printer*, isl_union_set*))
Unexecuted instantiation: GICHelper.cpp:std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > stringFromIslObjInternal<isl_schedule, isl_ctx* (*)(isl_schedule*), isl_printer* (*)(isl_printer*, isl_schedule*)>(isl_schedule*, isl_ctx* (*)(isl_schedule*), isl_printer* (*)(isl_printer*, isl_schedule*))
Unexecuted instantiation: GICHelper.cpp:std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > stringFromIslObjInternal<isl_multi_aff, isl_ctx* (*)(isl_multi_aff*), isl_printer* (*)(isl_printer*, isl_multi_aff*)>(isl_multi_aff*, isl_ctx* (*)(isl_multi_aff*), isl_printer* (*)(isl_printer*, isl_multi_aff*))
GICHelper.cpp:std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > stringFromIslObjInternal<isl_pw_multi_aff, isl_ctx* (*)(isl_pw_multi_aff*), isl_printer* (*)(isl_printer*, isl_pw_multi_aff*)>(isl_pw_multi_aff*, isl_ctx* (*)(isl_pw_multi_aff*), isl_printer* (*)(isl_printer*, isl_pw_multi_aff*))
Line
Count
Source
97
532
                                                   ISL_PRINTER printer_fn) {
98
532
  if (!isl_obj)
99
0
    return "null";
100
532
  isl_ctx *ctx = ctx_getter_fn(isl_obj);
101
532
  isl_printer *p = isl_printer_to_str(ctx);
102
532
  p = printer_fn(p, isl_obj);
103
532
  char *char_str = isl_printer_get_str(p);
104
532
  std::string string;
105
532
  if (char_str)
106
532
    string = char_str;
107
532
  else
108
0
    string = "null";
109
532
  free(char_str);
110
532
  isl_printer_free(p);
111
532
  return string;
112
532
}
Unexecuted instantiation: GICHelper.cpp:std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > stringFromIslObjInternal<isl_multi_pw_aff, isl_ctx* (*)(isl_multi_pw_aff*), isl_printer* (*)(isl_printer*, isl_multi_pw_aff*)>(isl_multi_pw_aff*, isl_ctx* (*)(isl_multi_pw_aff*), isl_printer* (*)(isl_printer*, isl_multi_pw_aff*))
Unexecuted instantiation: GICHelper.cpp:std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > stringFromIslObjInternal<isl_union_pw_multi_aff, isl_ctx* (*)(isl_union_pw_multi_aff*), isl_printer* (*)(isl_printer*, isl_union_pw_multi_aff*)>(isl_union_pw_multi_aff*, isl_ctx* (*)(isl_union_pw_multi_aff*), isl_printer* (*)(isl_printer*, isl_union_pw_multi_aff*))
Unexecuted instantiation: GICHelper.cpp:std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > stringFromIslObjInternal<isl_aff, isl_ctx* (*)(isl_aff*), isl_printer* (*)(isl_printer*, isl_aff*)>(isl_aff*, isl_ctx* (*)(isl_aff*), isl_printer* (*)(isl_printer*, isl_aff*))
Unexecuted instantiation: GICHelper.cpp:std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > stringFromIslObjInternal<isl_pw_aff, isl_ctx* (*)(isl_pw_aff*), isl_printer* (*)(isl_printer*, isl_pw_aff*)>(isl_pw_aff*, isl_ctx* (*)(isl_pw_aff*), isl_printer* (*)(isl_printer*, isl_pw_aff*))
Unexecuted instantiation: GICHelper.cpp:std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > stringFromIslObjInternal<isl_space, isl_ctx* (*)(isl_space*), isl_printer* (*)(isl_printer*, isl_space*)>(isl_space*, isl_ctx* (*)(isl_space*), isl_printer* (*)(isl_printer*, isl_space*))
113
114
3.34k
std::string polly::stringFromIslObj(__isl_keep isl_map *map) {
115
3.34k
  return stringFromIslObjInternal(map, isl_map_get_ctx, isl_printer_print_map);
116
3.34k
}
117
118
2.26k
std::string polly::stringFromIslObj(__isl_keep isl_set *set) {
119
2.26k
  return stringFromIslObjInternal(set, isl_set_get_ctx, isl_printer_print_set);
120
2.26k
}
121
122
244
std::string polly::stringFromIslObj(__isl_keep isl_union_map *umap) {
123
244
  return stringFromIslObjInternal(umap, isl_union_map_get_ctx,
124
244
                                  isl_printer_print_union_map);
125
244
}
126
127
0
std::string polly::stringFromIslObj(__isl_keep isl_union_set *uset) {
128
0
  return stringFromIslObjInternal(uset, isl_union_set_get_ctx,
129
0
                                  isl_printer_print_union_set);
130
0
}
131
132
0
std::string polly::stringFromIslObj(__isl_keep isl_schedule *schedule) {
133
0
  return stringFromIslObjInternal(schedule, isl_schedule_get_ctx,
134
0
                                  isl_printer_print_schedule);
135
0
}
136
137
0
std::string polly::stringFromIslObj(__isl_keep isl_multi_aff *maff) {
138
0
  return stringFromIslObjInternal(maff, isl_multi_aff_get_ctx,
139
0
                                  isl_printer_print_multi_aff);
140
0
}
141
142
532
std::string polly::stringFromIslObj(__isl_keep isl_pw_multi_aff *pma) {
143
532
  return stringFromIslObjInternal(pma, isl_pw_multi_aff_get_ctx,
144
532
                                  isl_printer_print_pw_multi_aff);
145
532
}
146
147
0
std::string polly::stringFromIslObj(__isl_keep isl_multi_pw_aff *mpa) {
148
0
  return stringFromIslObjInternal(mpa, isl_multi_pw_aff_get_ctx,
149
0
                                  isl_printer_print_multi_pw_aff);
150
0
}
151
152
0
std::string polly::stringFromIslObj(__isl_keep isl_union_pw_multi_aff *upma) {
153
0
  return stringFromIslObjInternal(upma, isl_union_pw_multi_aff_get_ctx,
154
0
                                  isl_printer_print_union_pw_multi_aff);
155
0
}
156
157
0
std::string polly::stringFromIslObj(__isl_keep isl_aff *aff) {
158
0
  return stringFromIslObjInternal(aff, isl_aff_get_ctx, isl_printer_print_aff);
159
0
}
160
161
0
std::string polly::stringFromIslObj(__isl_keep isl_pw_aff *pwaff) {
162
0
  return stringFromIslObjInternal(pwaff, isl_pw_aff_get_ctx,
163
0
                                  isl_printer_print_pw_aff);
164
0
}
165
166
0
std::string polly::stringFromIslObj(__isl_keep isl_space *space) {
167
0
  return stringFromIslObjInternal(space, isl_space_get_ctx,
168
0
                                  isl_printer_print_space);
169
0
}
170
171
static void replace(std::string &str, const std::string &find,
172
46.8k
                    const std::string &replace) {
173
46.8k
  size_t pos = 0;
174
51.8k
  while (
(pos = str.find(find, pos)) != std::string::npos51.8k
) {
175
5.02k
    str.replace(pos, find.length(), replace);
176
5.02k
    pos += replace.length();
177
5.02k
  }
178
46.8k
}
179
180
9.37k
static void makeIslCompatible(std::string &str) {
181
9.37k
  replace(str, ".", "_");
182
9.37k
  replace(str, "\"", "_");
183
9.37k
  replace(str, " ", "__");
184
9.37k
  replace(str, "=>", "TO");
185
9.37k
  replace(str, "+", "_");
186
9.37k
}
187
188
std::string polly::getIslCompatibleName(const std::string &Prefix,
189
                                        const std::string &Middle,
190
9.25k
                                        const std::string &Suffix) {
191
9.25k
  std::string S = Prefix + Middle + Suffix;
192
9.25k
  makeIslCompatible(S);
193
9.25k
  return S;
194
9.25k
}
195
196
std::string polly::getIslCompatibleName(const std::string &Prefix,
197
                                        const std::string &Name, long Number,
198
                                        const std::string &Suffix,
199
120
                                        bool UseInstructionNames) {
200
120
  std::string S = Prefix;
201
120
202
120
  if (UseInstructionNames)
203
119
    S += std::string("_") + Name;
204
120
  else
205
1
    S += std::to_string(Number);
206
120
207
120
  S += Suffix;
208
120
209
120
  makeIslCompatible(S);
210
120
  return S;
211
120
}
212
213
std::string polly::getIslCompatibleName(const std::string &Prefix,
214
                                        const Value *Val, long Number,
215
                                        const std::string &Suffix,
216
8.06k
                                        bool UseInstructionNames) {
217
8.06k
  std::string ValStr;
218
8.06k
219
8.06k
  if (
UseInstructionNames && 8.06k
Val->hasName()8.05k
)
220
7.91k
    ValStr = std::string("_") + std::string(Val->getName());
221
8.06k
  else
222
150
    ValStr = std::to_string(Number);
223
8.06k
224
8.06k
  return getIslCompatibleName(Prefix, ValStr, Suffix);
225
8.06k
}
226
227
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
228
/// To call a inline dump() method in a debugger, at it must have been
229
/// instantiated in at least one translation unit. Because isl's dump() method
230
/// are meant to be called from a debugger only, but not from code, no such
231
/// instantiation would exist. We use this method to force an instantiation in
232
/// this translation unit. Because it has non-static linking, the compiler does
233
/// not know that it is never called, and therefore must ensure the existence of
234
/// the dump functions.
235
void neverCalled() {
236
  isl::aff().dump();
237
  isl::aff_list().dump();
238
  isl::ast_expr().dump();
239
  isl::ast_expr_list().dump();
240
  isl::ast_node().dump();
241
  isl::ast_node_list().dump();
242
  isl::band_list().dump();
243
  isl::basic_map().dump();
244
  isl::basic_map_list().dump();
245
  isl::basic_set().dump();
246
  isl::basic_set_list().dump();
247
  isl::constraint().dump();
248
  isl::constraint_list().dump();
249
  isl::id().dump();
250
  isl::id_list().dump();
251
  isl::id_to_ast_expr().dump();
252
  isl::local_space().dump();
253
  isl::map().dump();
254
  isl::map_list().dump();
255
  isl::multi_aff().dump();
256
  isl::multi_pw_aff().dump();
257
  isl::multi_union_pw_aff().dump();
258
  isl::multi_val().dump();
259
  isl::point().dump();
260
  isl::pw_aff().dump();
261
  isl::pw_aff_list().dump();
262
  isl::pw_multi_aff().dump();
263
  isl::pw_qpolynomial().dump();
264
  isl::qpolynomial().dump();
265
  isl::schedule().dump();
266
  isl::schedule_constraints().dump();
267
  isl::schedule_node().dump();
268
  isl::set().dump();
269
  isl::set_list().dump();
270
  isl::space().dump();
271
  isl::union_map().dump();
272
  isl::union_map_list().dump();
273
  isl::union_pw_aff().dump();
274
  isl::union_pw_aff_list().dump();
275
  isl::union_pw_multi_aff().dump();
276
  isl::union_pw_multi_aff_list().dump();
277
  isl::union_set().dump();
278
  isl::union_set_list().dump();
279
  isl::val().dump();
280
  isl::val_list().dump();
281
}
282
#endif