Coverage Report

Created: 2018-04-24 22:41

/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
20.1k
                                            bool IsSigned) {
30
20.1k
  APInt Abs;
31
20.1k
  isl_val *v;
32
20.1k
33
20.1k
  // As isl is interpreting the input always as unsigned value, we need some
34
20.1k
  // additional pre and post processing to import signed values. The approach
35
20.1k
  // we take is to first obtain the absolute value of Int and then negate the
36
20.1k
  // value after it has been imported to isl.
37
20.1k
  //
38
20.1k
  // It should be noted that the smallest integer value represented in two's
39
20.1k
  // complement with a certain amount of bits does not have a corresponding
40
20.1k
  // positive representation in two's complement representation with the same
41
20.1k
  // number of bits. E.g. 110 (-2) does not have a corresponding value for (2).
42
20.1k
  // To ensure that there is always a corresponding value available we first
43
20.1k
  // sign-extend the input by one bit and only then take the absolute value.
44
20.1k
  if (IsSigned)
45
20.0k
    Abs = Int.sext(Int.getBitWidth() + 1).abs();
46
24
  else
47
24
    Abs = Int;
48
20.1k
49
20.1k
  const uint64_t *Data = Abs.getRawData();
50
20.1k
  unsigned Words = Abs.getNumWords();
51
20.1k
52
20.1k
  v = isl_val_int_from_chunks(Ctx, Words, sizeof(uint64_t), Data);
53
20.1k
54
20.1k
  if (IsSigned && 
Int.isNegative()20.0k
)
55
1.43k
    v = isl_val_neg(v);
56
20.1k
57
20.1k
  return v;
58
20.1k
}
59
60
3.51k
APInt polly::APIntFromVal(__isl_take isl_val *Val) {
61
3.51k
  uint64_t *Data;
62
3.51k
  int NumChunks;
63
3.51k
  const static int ChunkSize = sizeof(uint64_t);
64
3.51k
65
3.51k
  assert(isl_val_is_int(Val) && "Only integers can be converted to APInt");
66
3.51k
67
3.51k
  NumChunks = isl_val_n_abs_num_chunks(Val, ChunkSize);
68
3.51k
  Data = (uint64_t *)malloc(NumChunks * ChunkSize);
69
3.51k
  isl_val_get_abs_num_chunks(Val, ChunkSize, Data);
70
3.51k
  int NumBits = CHAR_BIT * ChunkSize * NumChunks;
71
3.51k
  APInt A(NumBits, NumChunks, Data);
72
3.51k
73
3.51k
  // As isl provides only an interface to obtain data that describes the
74
3.51k
  // absolute value of an isl_val, A at this point always contains a positive
75
3.51k
  // number. In case Val was originally negative, we expand the size of A by
76
3.51k
  // one and negate the value (in two's complement representation). As a result,
77
3.51k
  // the new value in A corresponds now with Val.
78
3.51k
  if (isl_val_is_neg(Val)) {
79
119
    A = A.zext(A.getBitWidth() + 1);
80
119
    A = -A;
81
119
  }
82
3.51k
83
3.51k
  // isl may represent small numbers with more than the minimal number of bits.
84
3.51k
  // We truncate the APInt to the minimal number of bits needed to represent the
85
3.51k
  // signed value it contains, to ensure that the bitwidth is always minimal.
86
3.51k
  if (A.getMinSignedBits() < A.getBitWidth())
87
3.50k
    A = A.trunc(A.getMinSignedBits());
88
3.51k
89
3.51k
  free(Data);
90
3.51k
  isl_val_free(Val);
91
3.51k
  return A;
92
3.51k
}
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.14k
                                                   ISL_PRINTER printer_fn) {
98
1.14k
  if (!isl_obj)
99
0
    return "null";
100
1.14k
  isl_ctx *ctx = ctx_getter_fn(isl_obj);
101
1.14k
  isl_printer *p = isl_printer_to_str(ctx);
102
1.14k
  p = printer_fn(p, isl_obj);
103
1.14k
  char *char_str = isl_printer_get_str(p);
104
1.14k
  std::string string;
105
1.14k
  if (char_str)
106
1.14k
    string = char_str;
107
0
  else
108
0
    string = "null";
109
1.14k
  free(char_str);
110
1.14k
  isl_printer_free(p);
111
1.14k
  return string;
112
1.14k
}
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
864
                                                   ISL_PRINTER printer_fn) {
98
864
  if (!isl_obj)
99
0
    return "null";
100
864
  isl_ctx *ctx = ctx_getter_fn(isl_obj);
101
864
  isl_printer *p = isl_printer_to_str(ctx);
102
864
  p = printer_fn(p, isl_obj);
103
864
  char *char_str = isl_printer_get_str(p);
104
864
  std::string string;
105
864
  if (char_str)
106
864
    string = char_str;
107
0
  else
108
0
    string = "null";
109
864
  free(char_str);
110
864
  isl_printer_free(p);
111
864
  return string;
112
864
}
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
261
                                                   ISL_PRINTER printer_fn) {
98
261
  if (!isl_obj)
99
0
    return "null";
100
261
  isl_ctx *ctx = ctx_getter_fn(isl_obj);
101
261
  isl_printer *p = isl_printer_to_str(ctx);
102
261
  p = printer_fn(p, isl_obj);
103
261
  char *char_str = isl_printer_get_str(p);
104
261
  std::string string;
105
261
  if (char_str)
106
261
    string = char_str;
107
0
  else
108
0
    string = "null";
109
261
  free(char_str);
110
261
  isl_printer_free(p);
111
261
  return string;
112
261
}
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
864
std::string polly::stringFromIslObj(__isl_keep isl_map *map) {
115
864
  return stringFromIslObjInternal(map, isl_map_get_ctx, isl_printer_print_map);
116
864
}
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
261
std::string polly::stringFromIslObj(__isl_keep isl_union_map *umap) {
123
261
  return stringFromIslObjInternal(umap, isl_union_map_get_ctx,
124
261
                                  isl_printer_print_union_map);
125
261
}
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
66.4k
                    const std::string &replace) {
173
66.4k
  size_t pos = 0;
174
74.1k
  while ((pos = str.find(find, pos)) != std::string::npos) {
175
7.73k
    str.replace(pos, find.length(), replace);
176
7.73k
    pos += replace.length();
177
7.73k
  }
178
66.4k
}
179
180
13.2k
static void makeIslCompatible(std::string &str) {
181
13.2k
  replace(str, ".", "_");
182
13.2k
  replace(str, "\"", "_");
183
13.2k
  replace(str, " ", "__");
184
13.2k
  replace(str, "=>", "TO");
185
13.2k
  replace(str, "+", "_");
186
13.2k
}
187
188
std::string polly::getIslCompatibleName(const std::string &Prefix,
189
                                        const std::string &Middle,
190
13.1k
                                        const std::string &Suffix) {
191
13.1k
  std::string S = Prefix + Middle + Suffix;
192
13.1k
  makeIslCompatible(S);
193
13.1k
  return S;
194
13.1k
}
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
11.9k
                                        bool UseInstructionNames) {
217
11.9k
  std::string ValStr;
218
11.9k
219
11.9k
  if (UseInstructionNames && 
Val->hasName()11.9k
)
220
11.7k
    ValStr = std::string("_") + std::string(Val->getName());
221
206
  else
222
206
    ValStr = std::to_string(Number);
223
11.9k
224
11.9k
  return getIslCompatibleName(Prefix, ValStr, Suffix);
225
11.9k
}
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::basic_map().dump();
243
  isl::basic_map_list().dump();
244
  isl::basic_set().dump();
245
  isl::basic_set_list().dump();
246
  isl::constraint().dump();
247
  isl::constraint_list().dump();
248
  isl::id().dump();
249
  isl::id_list().dump();
250
  isl::id_to_ast_expr().dump();
251
  isl::local_space().dump();
252
  isl::map().dump();
253
  isl::map_list().dump();
254
  isl::multi_aff().dump();
255
  isl::multi_pw_aff().dump();
256
  isl::multi_union_pw_aff().dump();
257
  isl::multi_val().dump();
258
  isl::point().dump();
259
  isl::pw_aff().dump();
260
  isl::pw_aff_list().dump();
261
  isl::pw_multi_aff().dump();
262
  isl::pw_qpolynomial().dump();
263
  isl::qpolynomial().dump();
264
  isl::schedule().dump();
265
  isl::schedule_constraints().dump();
266
  isl::schedule_node().dump();
267
  isl::set().dump();
268
  isl::set_list().dump();
269
  isl::space().dump();
270
  isl::union_map().dump();
271
  isl::union_map_list().dump();
272
  isl::union_pw_aff().dump();
273
  isl::union_pw_aff_list().dump();
274
  isl::union_pw_multi_aff().dump();
275
  isl::union_pw_multi_aff_list().dump();
276
  isl::union_set().dump();
277
  isl::union_set_list().dump();
278
  isl::val().dump();
279
  isl::val_list().dump();
280
}
281
#endif