Coverage Report

Created: 2017-11-21 03:47

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/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.8k
                                            bool IsSigned) {
30
19.8k
  APInt Abs;
31
19.8k
  isl_val *v;
32
19.8k
33
19.8k
  // As isl is interpreting the input always as unsigned value, we need some
34
19.8k
  // additional pre and post processing to import signed values. The approach
35
19.8k
  // we take is to first obtain the absolute value of Int and then negate the
36
19.8k
  // value after it has been imported to isl.
37
19.8k
  //
38
19.8k
  // It should be noted that the smallest integer value represented in two's
39
19.8k
  // complement with a certain amount of bits does not have a corresponding
40
19.8k
  // positive representation in two's complement representation with the same
41
19.8k
  // number of bits. E.g. 110 (-2) does not have a corresponding value for (2).
42
19.8k
  // To ensure that there is always a corresponding value available we first
43
19.8k
  // sign-extend the input by one bit and only then take the absolute value.
44
19.8k
  if (IsSigned)
45
19.8k
    Abs = Int.sext(Int.getBitWidth() + 1).abs();
46
24
  else
47
24
    Abs = Int;
48
19.8k
49
19.8k
  const uint64_t *Data = Abs.getRawData();
50
19.8k
  unsigned Words = Abs.getNumWords();
51
19.8k
52
19.8k
  v = isl_val_int_from_chunks(Ctx, Words, sizeof(uint64_t), Data);
53
19.8k
54
19.8k
  if (IsSigned && 
Int.isNegative()19.8k
)
55
1.42k
    v = isl_val_neg(v);
56
19.8k
57
19.8k
  return v;
58
19.8k
}
59
60
3.49k
APInt polly::APIntFromVal(__isl_take isl_val *Val) {
61
3.49k
  uint64_t *Data;
62
3.49k
  int NumChunks;
63
3.49k
  const static int ChunkSize = sizeof(uint64_t);
64
3.49k
65
3.49k
  assert(isl_val_is_int(Val) && "Only integers can be converted to APInt");
66
3.49k
67
3.49k
  NumChunks = isl_val_n_abs_num_chunks(Val, ChunkSize);
68
3.49k
  Data = (uint64_t *)malloc(NumChunks * ChunkSize);
69
3.49k
  isl_val_get_abs_num_chunks(Val, ChunkSize, Data);
70
3.49k
  int NumBits = CHAR_BIT * ChunkSize * NumChunks;
71
3.49k
  APInt A(NumBits, NumChunks, Data);
72
3.49k
73
3.49k
  // As isl provides only an interface to obtain data that describes the
74
3.49k
  // absolute value of an isl_val, A at this point always contains a positive
75
3.49k
  // number. In case Val was originally negative, we expand the size of A by
76
3.49k
  // one and negate the value (in two's complement representation). As a result,
77
3.49k
  // the new value in A corresponds now with Val.
78
3.49k
  if (isl_val_is_neg(Val)) {
79
118
    A = A.zext(A.getBitWidth() + 1);
80
118
    A = -A;
81
118
  }
82
3.49k
83
3.49k
  // isl may represent small numbers with more than the minimal number of bits.
84
3.49k
  // We truncate the APInt to the minimal number of bits needed to represent the
85
3.49k
  // signed value it contains, to ensure that the bitwidth is always minimal.
86
3.49k
  if (A.getMinSignedBits() < A.getBitWidth())
87
3.48k
    A = A.trunc(A.getMinSignedBits());
88
3.49k
89
3.49k
  free(Data);
90
3.49k
  isl_val_free(Val);
91
3.49k
  return A;
92
3.49k
}
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
1.10k
                                                   ISL_PRINTER printer_fn) {
98
1.10k
  if (!isl_obj)
99
0
    return "null";
100
1.10k
  isl_ctx *ctx = ctx_getter_fn(isl_obj);
101
1.10k
  isl_printer *p = isl_printer_to_str(ctx);
102
1.10k
  p = printer_fn(p, isl_obj);
103
1.10k
  char *char_str = isl_printer_get_str(p);
104
1.10k
  std::string string;
105
1.10k
  if (char_str)
106
1.10k
    string = char_str;
107
0
  else
108
0
    string = "null";
109
1.10k
  free(char_str);
110
1.10k
  isl_printer_free(p);
111
1.10k
  return string;
112
1.10k
}
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
844
                                                   ISL_PRINTER printer_fn) {
98
844
  if (!isl_obj)
99
0
    return "null";
100
844
  isl_ctx *ctx = ctx_getter_fn(isl_obj);
101
844
  isl_printer *p = isl_printer_to_str(ctx);
102
844
  p = printer_fn(p, isl_obj);
103
844
  char *char_str = isl_printer_get_str(p);
104
844
  std::string string;
105
844
  if (char_str)
106
844
    string = char_str;
107
0
  else
108
0
    string = "null";
109
844
  free(char_str);
110
844
  isl_printer_free(p);
111
844
  return string;
112
844
}
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
20
                                                   ISL_PRINTER printer_fn) {
98
20
  if (!isl_obj)
99
0
    return "null";
100
20
  isl_ctx *ctx = ctx_getter_fn(isl_obj);
101
20
  isl_printer *p = isl_printer_to_str(ctx);
102
20
  p = printer_fn(p, isl_obj);
103
20
  char *char_str = isl_printer_get_str(p);
104
20
  std::string string;
105
20
  if (char_str)
106
20
    string = char_str;
107
0
  else
108
0
    string = "null";
109
20
  free(char_str);
110
20
  isl_printer_free(p);
111
20
  return string;
112
20
}
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
0
  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*))
Unexecuted instantiation: 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*))
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
844
std::string polly::stringFromIslObj(__isl_keep isl_map *map) {
115
844
  return stringFromIslObjInternal(map, isl_map_get_ctx, isl_printer_print_map);
116
844
}
117
118
20
std::string polly::stringFromIslObj(__isl_keep isl_set *set) {
119
20
  return stringFromIslObjInternal(set, isl_set_get_ctx, isl_printer_print_set);
120
20
}
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
0
std::string polly::stringFromIslObj(__isl_keep isl_pw_multi_aff *pma) {
143
0
  return stringFromIslObjInternal(pma, isl_pw_multi_aff_get_ctx,
144
0
                                  isl_printer_print_pw_multi_aff);
145
0
}
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
48.4k
                    const std::string &replace) {
173
48.4k
  size_t pos = 0;
174
53.6k
  while ((pos = str.find(find, pos)) != std::string::npos) {
175
5.12k
    str.replace(pos, find.length(), replace);
176
5.12k
    pos += replace.length();
177
5.12k
  }
178
48.4k
}
179
180
9.69k
static void makeIslCompatible(std::string &str) {
181
9.69k
  replace(str, ".", "_");
182
9.69k
  replace(str, "\"", "_");
183
9.69k
  replace(str, " ", "__");
184
9.69k
  replace(str, "=>", "TO");
185
9.69k
  replace(str, "+", "_");
186
9.69k
}
187
188
std::string polly::getIslCompatibleName(const std::string &Prefix,
189
                                        const std::string &Middle,
190
9.57k
                                        const std::string &Suffix) {
191
9.57k
  std::string S = Prefix + Middle + Suffix;
192
9.57k
  makeIslCompatible(S);
193
9.57k
  return S;
194
9.57k
}
195
196
std::string polly::getIslCompatibleName(const std::string &Prefix,
197
                                        const std::string &Name, long Number,
198
                                        const std::string &Suffix,
199
121
                                        bool UseInstructionNames) {
200
121
  std::string S = Prefix;
201
121
202
121
  if (UseInstructionNames)
203
120
    S += std::string("_") + Name;
204
1
  else
205
1
    S += std::to_string(Number);
206
121
207
121
  S += Suffix;
208
121
209
121
  makeIslCompatible(S);
210
121
  return S;
211
121
}
212
213
std::string polly::getIslCompatibleName(const std::string &Prefix,
214
                                        const Value *Val, long Number,
215
                                        const std::string &Suffix,
216
8.37k
                                        bool UseInstructionNames) {
217
8.37k
  std::string ValStr;
218
8.37k
219
8.37k
  if (UseInstructionNames && 
Val->hasName()8.36k
)
220
8.20k
    ValStr = std::string("_") + std::string(Val->getName());
221
169
  else
222
169
    ValStr = std::to_string(Number);
223
8.37k
224
8.37k
  return getIslCompatibleName(Prefix, ValStr, Suffix);
225
8.37k
}
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