Coverage Report

Created: 2017-03-28 09:59

/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 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
15.6k
                                            bool IsSigned) {
30
15.6k
  APInt Abs;
31
15.6k
  isl_val *v;
32
15.6k
33
15.6k
  // As isl is interpreting the input always as unsigned value, we need some
34
15.6k
  // additional pre and post processing to import signed values. The approach
35
15.6k
  // we take is to first obtain the absolute value of Int and then negate the
36
15.6k
  // value after it has been imported to isl.
37
15.6k
  //
38
15.6k
  // It should be noted that the smallest integer value represented in two's
39
15.6k
  // complement with a certain amount of bits does not have a corresponding
40
15.6k
  // positive representation in two's complement representation with the same
41
15.6k
  // number of bits. E.g. 110 (-2) does not have a corresponding value for (2).
42
15.6k
  // To ensure that there is always a corresponding value available we first
43
15.6k
  // sign-extend the input by one bit and only then take the absolute value.
44
15.6k
  if (IsSigned)
45
15.6k
    Abs = Int.sext(Int.getBitWidth() + 1).abs();
46
15.6k
  else
47
24
    Abs = Int;
48
15.6k
49
15.6k
  const uint64_t *Data = Abs.getRawData();
50
15.6k
  unsigned Words = Abs.getNumWords();
51
15.6k
52
15.6k
  v = isl_val_int_from_chunks(Ctx, Words, sizeof(uint64_t), Data);
53
15.6k
54
15.6k
  if (
IsSigned && 15.6k
Int.isNegative()15.6k
)
55
1.10k
    v = isl_val_neg(v);
56
15.6k
57
15.6k
  return v;
58
15.6k
}
59
60
1.96k
APInt polly::APIntFromVal(__isl_take isl_val *Val) {
61
1.96k
  uint64_t *Data;
62
1.96k
  int NumChunks;
63
1.96k
  const static int ChunkSize = sizeof(uint64_t);
64
1.96k
65
1.96k
  assert(isl_val_is_int(Val) && "Only integers can be converted to APInt");
66
1.96k
67
1.96k
  NumChunks = isl_val_n_abs_num_chunks(Val, ChunkSize);
68
1.96k
  Data = (uint64_t *)malloc(NumChunks * ChunkSize);
69
1.96k
  isl_val_get_abs_num_chunks(Val, ChunkSize, Data);
70
1.96k
  int NumBits = CHAR_BIT * ChunkSize * NumChunks;
71
1.96k
  APInt A(NumBits, NumChunks, Data);
72
1.96k
73
1.96k
  // As isl provides only an interface to obtain data that describes the
74
1.96k
  // absolute value of an isl_val, A at this point always contains a positive
75
1.96k
  // number. In case Val was originally negative, we expand the size of A by
76
1.96k
  // one and negate the value (in two's complement representation). As a result,
77
1.96k
  // the new value in A corresponds now with Val.
78
1.96k
  if (
isl_val_is_neg(Val)1.96k
)
{81
79
81
    A = A.zext(A.getBitWidth() + 1);
80
81
    A = -A;
81
81
  }
82
1.96k
83
1.96k
  // isl may represent small numbers with more than the minimal number of bits.
84
1.96k
  // We truncate the APInt to the minimal number of bits needed to represent the
85
1.96k
  // signed value it contains, to ensure that the bitwidth is always minimal.
86
1.96k
  if (A.getMinSignedBits() < A.getBitWidth())
87
1.94k
    A = A.trunc(A.getMinSignedBits());
88
1.96k
89
1.96k
  free(Data);
90
1.96k
  isl_val_free(Val);
91
1.96k
  return A;
92
1.96k
}
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
5.47k
                                                   ISL_PRINTER printer_fn) {
98
5.47k
  if (!isl_obj)
99
4
    return "null";
100
5.46k
  isl_ctx *ctx = ctx_getter_fn(isl_obj);
101
5.46k
  isl_printer *p = isl_printer_to_str(ctx);
102
5.46k
  p = printer_fn(p, isl_obj);
103
5.46k
  char *char_str = isl_printer_get_str(p);
104
5.46k
  std::string string;
105
5.46k
  if (char_str)
106
5.46k
    string = char_str;
107
5.46k
  else
108
0
    string = "null";
109
5.46k
  free(char_str);
110
5.46k
  isl_printer_free(p);
111
5.46k
  return string;
112
5.47k
}
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
2.19k
                                                   ISL_PRINTER printer_fn) {
98
2.19k
  if (!isl_obj)
99
4
    return "null";
100
2.19k
  isl_ctx *ctx = ctx_getter_fn(isl_obj);
101
2.19k
  isl_printer *p = isl_printer_to_str(ctx);
102
2.19k
  p = printer_fn(p, isl_obj);
103
2.19k
  char *char_str = isl_printer_get_str(p);
104
2.19k
  std::string string;
105
2.19k
  if (char_str)
106
2.19k
    string = char_str;
107
2.19k
  else
108
0
    string = "null";
109
2.19k
  free(char_str);
110
2.19k
  isl_printer_free(p);
111
2.19k
  return string;
112
2.19k
}
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.48k
                                                   ISL_PRINTER printer_fn) {
98
2.48k
  if (!isl_obj)
99
0
    return "null";
100
2.48k
  isl_ctx *ctx = ctx_getter_fn(isl_obj);
101
2.48k
  isl_printer *p = isl_printer_to_str(ctx);
102
2.48k
  p = printer_fn(p, isl_obj);
103
2.48k
  char *char_str = isl_printer_get_str(p);
104
2.48k
  std::string string;
105
2.48k
  if (char_str)
106
2.48k
    string = char_str;
107
2.48k
  else
108
0
    string = "null";
109
2.48k
  free(char_str);
110
2.48k
  isl_printer_free(p);
111
2.48k
  return string;
112
2.48k
}
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
239
                                                   ISL_PRINTER printer_fn) {
98
239
  if (!isl_obj)
99
0
    return "null";
100
239
  isl_ctx *ctx = ctx_getter_fn(isl_obj);
101
239
  isl_printer *p = isl_printer_to_str(ctx);
102
239
  p = printer_fn(p, isl_obj);
103
239
  char *char_str = isl_printer_get_str(p);
104
239
  std::string string;
105
239
  if (char_str)
106
239
    string = char_str;
107
239
  else
108
0
    string = "null";
109
239
  free(char_str);
110
239
  isl_printer_free(p);
111
239
  return string;
112
239
}
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
338
                                                   ISL_PRINTER printer_fn) {
98
338
  if (!isl_obj)
99
0
    return "null";
100
338
  isl_ctx *ctx = ctx_getter_fn(isl_obj);
101
338
  isl_printer *p = isl_printer_to_str(ctx);
102
338
  p = printer_fn(p, isl_obj);
103
338
  char *char_str = isl_printer_get_str(p);
104
338
  std::string string;
105
338
  if (char_str)
106
338
    string = char_str;
107
338
  else
108
0
    string = "null";
109
338
  free(char_str);
110
338
  isl_printer_free(p);
111
338
  return string;
112
338
}
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*))
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*))
Line
Count
Source
97
209
                                                   ISL_PRINTER printer_fn) {
98
209
  if (!isl_obj)
99
0
    return "null";
100
209
  isl_ctx *ctx = ctx_getter_fn(isl_obj);
101
209
  isl_printer *p = isl_printer_to_str(ctx);
102
209
  p = printer_fn(p, isl_obj);
103
209
  char *char_str = isl_printer_get_str(p);
104
209
  std::string string;
105
209
  if (char_str)
106
209
    string = char_str;
107
209
  else
108
0
    string = "null";
109
209
  free(char_str);
110
209
  isl_printer_free(p);
111
209
  return string;
112
209
}
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
2.19k
std::string polly::stringFromIslObj(__isl_keep isl_map *map) {
115
2.19k
  return stringFromIslObjInternal(map, isl_map_get_ctx, isl_printer_print_map);
116
2.19k
}
117
118
2.48k
std::string polly::stringFromIslObj(__isl_keep isl_set *set) {
119
2.48k
  return stringFromIslObjInternal(set, isl_set_get_ctx, isl_printer_print_set);
120
2.48k
}
121
122
239
std::string polly::stringFromIslObj(__isl_keep isl_union_map *umap) {
123
239
  return stringFromIslObjInternal(umap, isl_union_map_get_ctx,
124
239
                                  isl_printer_print_union_map);
125
239
}
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
338
std::string polly::stringFromIslObj(__isl_keep isl_pw_multi_aff *pma) {
143
338
  return stringFromIslObjInternal(pma, isl_pw_multi_aff_get_ctx,
144
338
                                  isl_printer_print_pw_multi_aff);
145
338
}
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
209
std::string polly::stringFromIslObj(__isl_keep isl_pw_aff *pwaff) {
162
209
  return stringFromIslObjInternal(pwaff, isl_pw_aff_get_ctx,
163
209
                                  isl_printer_print_pw_aff);
164
209
}
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
59.6k
                    const std::string &replace) {
173
59.6k
  size_t pos = 0;
174
64.7k
  while (
(pos = str.find(find, pos)) != std::string::npos64.7k
)
{5.05k
175
5.05k
    str.replace(pos, find.length(), replace);
176
5.05k
    pos += replace.length();
177
5.05k
  }
178
59.6k
}
179
180
14.9k
static void makeIslCompatible(std::string &str) {
181
14.9k
  replace(str, ".", "_");
182
14.9k
  replace(str, "\"", "_");
183
14.9k
  replace(str, " ", "__");
184
14.9k
  replace(str, "=>", "TO");
185
14.9k
}
186
187
std::string polly::getIslCompatibleName(const std::string &Prefix,
188
                                        const std::string &Middle,
189
14.9k
                                        const std::string &Suffix) {
190
14.9k
  std::string S = Prefix + Middle + Suffix;
191
14.9k
  makeIslCompatible(S);
192
14.9k
  return S;
193
14.9k
}
194
195
std::string polly::getIslCompatibleName(const std::string &Prefix,
196
                                        const Value *Val,
197
9.96k
                                        const std::string &Suffix) {
198
9.96k
  std::string ValStr;
199
9.96k
  raw_string_ostream OS(ValStr);
200
9.96k
  Val->printAsOperand(OS, false);
201
9.96k
  ValStr = OS.str();
202
9.96k
  // Remove the leading %
203
9.96k
  ValStr.erase(0, 1);
204
9.96k
  return getIslCompatibleName(Prefix, ValStr, Suffix);
205
9.96k
}
206
207
void polly::foreachElt(const isl::map &Map,
208
1
                       const std::function<void(isl::basic_map)> &F) {
209
1
  isl_map_foreach_basic_map(
210
1
      Map.keep(),
211
1
      [](__isl_take isl_basic_map *BMap, void *User) -> isl_stat {
212
1
        auto &F =
213
1
            *static_cast<const std::function<void(isl::basic_map)> *>(User);
214
1
        F(give(BMap));
215
1
        return isl_stat_ok;
216
1
      },
217
1
      const_cast<void *>(static_cast<const void *>(&F)));
218
1
}
219
220
void polly::foreachElt(const isl::set &Set,
221
1
                       const std::function<void(isl::basic_set)> &F) {
222
1
  isl_set_foreach_basic_set(
223
1
      Set.keep(),
224
1
      [](__isl_take isl_basic_set *BSet, void *User) -> isl_stat {
225
1
        auto &F =
226
1
            *static_cast<const std::function<void(isl::basic_set)> *>(User);
227
1
        F(give(BSet));
228
1
        return isl_stat_ok;
229
1
      },
230
1
      const_cast<void *>(static_cast<const void *>(&F)));
231
1
}
232
233
void polly::foreachElt(const isl::union_map &UMap,
234
542
                       const std::function<void(isl::map Map)> &F) {
235
542
  isl_union_map_foreach_map(
236
542
      UMap.keep(),
237
1.02k
      [](__isl_take isl_map *Map, void *User) -> isl_stat {
238
1.02k
        auto &F = *static_cast<const std::function<void(isl::map)> *>(User);
239
1.02k
        F(give(Map));
240
1.02k
        return isl_stat_ok;
241
1.02k
      },
242
542
      const_cast<void *>(static_cast<const void *>(&F)));
243
542
}
244
245
void polly::foreachElt(const isl::union_set &USet,
246
244
                       const std::function<void(isl::set Set)> &F) {
247
244
  isl_union_set_foreach_set(
248
244
      USet.keep(),
249
265
      [](__isl_take isl_set *Set, void *User) -> isl_stat {
250
265
        auto &F = *static_cast<const std::function<void(isl::set)> *>(User);
251
265
        F(give(Set));
252
265
        return isl_stat_ok;
253
265
      },
254
244
      const_cast<void *>(static_cast<const void *>(&F)));
255
244
}
256
257
void polly::foreachElt(const isl::union_pw_aff &UPwAff,
258
11
                       const std::function<void(isl::pw_aff)> &F) {
259
11
  isl_union_pw_aff_foreach_pw_aff(
260
11
      UPwAff.keep(),
261
35
      [](__isl_take isl_pw_aff *PwAff, void *User) -> isl_stat {
262
35
        auto &F = *static_cast<const std::function<void(isl::pw_aff)> *>(User);
263
35
        F(give(PwAff));
264
35
        return isl_stat_ok;
265
35
      },
266
11
      const_cast<void *>(static_cast<const void *>(&F)));
267
11
}
268
269
isl_stat
270
polly::foreachEltWithBreak(const isl::map &Map,
271
44
                           const std::function<isl_stat(isl::basic_map)> &F) {
272
44
  return isl_map_foreach_basic_map(
273
44
      Map.keep(),
274
44
      [](__isl_take isl_basic_map *BMap, void *User) -> isl_stat {
275
44
        auto &F =
276
44
            *static_cast<const std::function<isl_stat(isl::basic_map)> *>(User);
277
44
        return F(give(BMap));
278
44
      },
279
44
      const_cast<void *>(static_cast<const void *>(&F)));
280
44
}
281
282
isl_stat
283
polly::foreachEltWithBreak(const isl::union_map &UMap,
284
27
                           const std::function<isl_stat(isl::map Map)> &F) {
285
27
  return isl_union_map_foreach_map(
286
27
      UMap.keep(),
287
44
      [](__isl_take isl_map *Map, void *User) -> isl_stat {
288
44
        auto &F =
289
44
            *static_cast<const std::function<isl_stat(isl::map Map)> *>(User);
290
44
        return F(give(Map));
291
44
      },
292
27
      const_cast<void *>(static_cast<const void *>(&F)));
293
27
}
294
295
isl_stat polly::foreachPieceWithBreak(
296
    const isl::pw_aff &PwAff,
297
23
    const std::function<isl_stat(isl::set, isl::aff)> &F) {
298
23
  return isl_pw_aff_foreach_piece(
299
23
      PwAff.keep(),
300
23
      [](__isl_take isl_set *Domain, __isl_take isl_aff *Aff,
301
23
         void *User) -> isl_stat {
302
23
        auto &F =
303
23
            *static_cast<const std::function<isl_stat(isl::set, isl::aff)> *>(
304
23
                User);
305
23
        return F(give(Domain), give(Aff));
306
23
      },
307
23
      const_cast<void *>(static_cast<const void *>(&F)));
308
23
}