Coverage Report

Created: 2017-06-23 12:40

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