Coverage Report

Created: 2017-04-29 12:21

/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.7k
                                            bool IsSigned) {
30
15.7k
  APInt Abs;
31
15.7k
  isl_val *v;
32
15.7k
33
15.7k
  // As isl is interpreting the input always as unsigned value, we need some
34
15.7k
  // additional pre and post processing to import signed values. The approach
35
15.7k
  // we take is to first obtain the absolute value of Int and then negate the
36
15.7k
  // value after it has been imported to isl.
37
15.7k
  //
38
15.7k
  // It should be noted that the smallest integer value represented in two's
39
15.7k
  // complement with a certain amount of bits does not have a corresponding
40
15.7k
  // positive representation in two's complement representation with the same
41
15.7k
  // number of bits. E.g. 110 (-2) does not have a corresponding value for (2).
42
15.7k
  // To ensure that there is always a corresponding value available we first
43
15.7k
  // sign-extend the input by one bit and only then take the absolute value.
44
15.7k
  if (IsSigned)
45
15.7k
    Abs = Int.sext(Int.getBitWidth() + 1).abs();
46
15.7k
  else
47
24
    Abs = Int;
48
15.7k
49
15.7k
  const uint64_t *Data = Abs.getRawData();
50
15.7k
  unsigned Words = Abs.getNumWords();
51
15.7k
52
15.7k
  v = isl_val_int_from_chunks(Ctx, Words, sizeof(uint64_t), Data);
53
15.7k
54
15.7k
  if (
IsSigned && 15.7k
Int.isNegative()15.7k
)
55
1.10k
    v = isl_val_neg(v);
56
15.7k
57
15.7k
  return v;
58
15.7k
}
59
60
1.98k
APInt polly::APIntFromVal(__isl_take isl_val *Val) {
61
1.98k
  uint64_t *Data;
62
1.98k
  int NumChunks;
63
1.98k
  const static int ChunkSize = sizeof(uint64_t);
64
1.98k
65
1.98k
  assert(isl_val_is_int(Val) && "Only integers can be converted to APInt");
66
1.98k
67
1.98k
  NumChunks = isl_val_n_abs_num_chunks(Val, ChunkSize);
68
1.98k
  Data = (uint64_t *)malloc(NumChunks * ChunkSize);
69
1.98k
  isl_val_get_abs_num_chunks(Val, ChunkSize, Data);
70
1.98k
  int NumBits = CHAR_BIT * ChunkSize * NumChunks;
71
1.98k
  APInt A(NumBits, NumChunks, Data);
72
1.98k
73
1.98k
  // As isl provides only an interface to obtain data that describes the
74
1.98k
  // absolute value of an isl_val, A at this point always contains a positive
75
1.98k
  // number. In case Val was originally negative, we expand the size of A by
76
1.98k
  // one and negate the value (in two's complement representation). As a result,
77
1.98k
  // the new value in A corresponds now with Val.
78
1.98k
  if (
isl_val_is_neg(Val)1.98k
)
{81
79
81
    A = A.zext(A.getBitWidth() + 1);
80
81
    A = -A;
81
81
  }
82
1.98k
83
1.98k
  // isl may represent small numbers with more than the minimal number of bits.
84
1.98k
  // We truncate the APInt to the minimal number of bits needed to represent the
85
1.98k
  // signed value it contains, to ensure that the bitwidth is always minimal.
86
1.98k
  if (A.getMinSignedBits() < A.getBitWidth())
87
1.96k
    A = A.trunc(A.getMinSignedBits());
88
1.98k
89
1.98k
  free(Data);
90
1.98k
  isl_val_free(Val);
91
1.98k
  return A;
92
1.98k
}
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
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
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
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
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
60.3k
                    const std::string &replace) {
173
60.3k
  size_t pos = 0;
174
65.4k
  while (
(pos = str.find(find, pos)) != std::string::npos65.4k
)
{5.07k
175
5.07k
    str.replace(pos, find.length(), replace);
176
5.07k
    pos += replace.length();
177
5.07k
  }
178
60.3k
}
179
180
15.0k
static void makeIslCompatible(std::string &str) {
181
15.0k
  replace(str, ".", "_");
182
15.0k
  replace(str, "\"", "_");
183
15.0k
  replace(str, " ", "__");
184
15.0k
  replace(str, "=>", "TO");
185
15.0k
}
186
187
std::string polly::getIslCompatibleName(const std::string &Prefix,
188
                                        const std::string &Middle,
189
15.0k
                                        const std::string &Suffix) {
190
15.0k
  std::string S = Prefix + Middle + Suffix;
191
15.0k
  makeIslCompatible(S);
192
15.0k
  return S;
193
15.0k
}
194
195
std::string polly::getIslCompatibleName(const std::string &Prefix,
196
                                        const Value *Val,
197
10.0k
                                        const std::string &Suffix) {
198
10.0k
  std::string ValStr;
199
10.0k
  raw_string_ostream OS(ValStr);
200
10.0k
  Val->printAsOperand(OS, false);
201
10.0k
  ValStr = OS.str();
202
10.0k
  // Remove the leading %
203
10.0k
  ValStr.erase(0, 1);
204
10.0k
  return getIslCompatibleName(Prefix, ValStr, Suffix);
205
10.0k
}