Coverage Report

Created: 2019-02-23 12:57

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