Coverage Report

Created: 2017-08-21 19:50

/Users/buildslave/jenkins/sharedspace/clang-stage2-coverage-R@2/llvm/tools/polly/include/polly/Support/GICHelper.h
Line
Count
Source (jump to first uncovered line)
1
//===- Support/GICHelper.h -- Helper functions for ISL --------------------===//
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
// Helper functions for isl objects.
11
//
12
//===----------------------------------------------------------------------===//
13
//
14
#ifndef POLLY_SUPPORT_GIC_HELPER_H
15
#define POLLY_SUPPORT_GIC_HELPER_H
16
17
#include "llvm/ADT/APInt.h"
18
#include "llvm/IR/DiagnosticInfo.h"
19
#include "llvm/Support/raw_ostream.h"
20
#include "isl/aff.h"
21
#include "isl/ctx.h"
22
#include "isl/map.h"
23
#include "isl/options.h"
24
#include "isl/set.h"
25
#include "isl/union_map.h"
26
#include "isl/union_set.h"
27
28
#include "isl-noexceptions.h"
29
30
#include <functional>
31
#include <string>
32
33
struct isl_schedule;
34
struct isl_multi_aff;
35
36
namespace llvm {
37
class Value;
38
} // namespace llvm
39
40
namespace polly {
41
42
/// Translate an llvm::APInt to an isl_val.
43
///
44
/// Translate the bitsequence without sign information as provided by APInt into
45
/// a signed isl_val type. Depending on the value of @p IsSigned @p Int is
46
/// interpreted as unsigned value or as signed value in two's complement
47
/// representation.
48
///
49
/// Input IsSigned                 Output
50
///
51
///     0        0           ->    0
52
///     1        0           ->    1
53
///    00        0           ->    0
54
///    01        0           ->    1
55
///    10        0           ->    2
56
///    11        0           ->    3
57
///
58
///     0        1           ->    0
59
///     1        1           ->   -1
60
///    00        1           ->    0
61
///    01        1           ->    1
62
///    10        1           ->   -2
63
///    11        1           ->   -1
64
///
65
/// @param Ctx      The isl_ctx to create the isl_val in.
66
/// @param Int      The integer value to translate.
67
/// @param IsSigned If the APInt should be interpreted as signed or unsigned
68
///                 value.
69
///
70
/// @return The isl_val corresponding to @p Int.
71
__isl_give isl_val *isl_valFromAPInt(isl_ctx *Ctx, const llvm::APInt Int,
72
                                     bool IsSigned);
73
74
/// Translate an llvm::APInt to an isl::val.
75
///
76
/// Translate the bitsequence without sign information as provided by APInt into
77
/// a signed isl::val type. Depending on the value of @p IsSigned @p Int is
78
/// interpreted as unsigned value or as signed value in two's complement
79
/// representation.
80
///
81
/// Input IsSigned                 Output
82
///
83
///     0        0           ->    0
84
///     1        0           ->    1
85
///    00        0           ->    0
86
///    01        0           ->    1
87
///    10        0           ->    2
88
///    11        0           ->    3
89
///
90
///     0        1           ->    0
91
///     1        1           ->   -1
92
///    00        1           ->    0
93
///    01        1           ->    1
94
///    10        1           ->   -2
95
///    11        1           ->   -1
96
///
97
/// @param Ctx      The isl_ctx to create the isl::val in.
98
/// @param Int      The integer value to translate.
99
/// @param IsSigned If the APInt should be interpreted as signed or unsigned
100
///                 value.
101
///
102
/// @return The isl::val corresponding to @p Int.
103
inline isl::val valFromAPInt(isl_ctx *Ctx, const llvm::APInt Int,
104
2.36k
                             bool IsSigned) {
105
2.36k
  return isl::manage(isl_valFromAPInt(Ctx, Int, IsSigned));
106
2.36k
}
107
108
/// Translate isl_val to llvm::APInt.
109
///
110
/// This function can only be called on isl_val values which are integers.
111
/// Calling this function with a non-integral rational, NaN or infinity value
112
/// is not allowed.
113
///
114
/// As the input isl_val may be negative, the APInt that this function returns
115
/// must always be interpreted as signed two's complement value. The bitwidth of
116
/// the generated APInt is always the minimal bitwidth necessary to model the
117
/// provided integer when interpreting the bit pattern as signed value.
118
///
119
/// Some example conversions are:
120
///
121
///   Input      Bits    Signed  Bitwidth
122
///       0 ->      0         0         1
123
///      -1 ->      1        -1         1
124
///       1 ->     01         1         2
125
///      -2 ->     10        -2         2
126
///       2 ->    010         2         3
127
///      -3 ->    101        -3         3
128
///       3 ->    011         3         3
129
///      -4 ->    100        -4         3
130
///       4 ->   0100         4         4
131
///
132
/// @param Val The isl val to translate.
133
///
134
/// @return The APInt value corresponding to @p Val.
135
llvm::APInt APIntFromVal(__isl_take isl_val *Val);
136
137
/// Translate isl::val to llvm::APInt.
138
///
139
/// This function can only be called on isl::val values which are integers.
140
/// Calling this function with a non-integral rational, NaN or infinity value
141
/// is not allowed.
142
///
143
/// As the input isl::val may be negative, the APInt that this function returns
144
/// must always be interpreted as signed two's complement value. The bitwidth of
145
/// the generated APInt is always the minimal bitwidth necessary to model the
146
/// provided integer when interpreting the bit pattern as signed value.
147
///
148
/// Some example conversions are:
149
///
150
///   Input      Bits    Signed  Bitwidth
151
///       0 ->      0         0         1
152
///      -1 ->      1        -1         1
153
///       1 ->     01         1         2
154
///      -2 ->     10        -2         2
155
///       2 ->    010         2         3
156
///      -3 ->    101        -3         3
157
///       3 ->    011         3         3
158
///      -4 ->    100        -4         3
159
///       4 ->   0100         4         4
160
///
161
/// @param Val The isl val to translate.
162
///
163
/// @return The APInt value corresponding to @p Val.
164
0
inline llvm::APInt APIntFromVal(isl::val V) {
165
0
  return APIntFromVal(V.release());
166
0
}
167
168
/// Get c++ string from Isl objects.
169
//@{
170
std::string stringFromIslObj(__isl_keep isl_map *map);
171
std::string stringFromIslObj(__isl_keep isl_union_map *umap);
172
std::string stringFromIslObj(__isl_keep isl_set *set);
173
std::string stringFromIslObj(__isl_keep isl_union_set *uset);
174
std::string stringFromIslObj(__isl_keep isl_schedule *schedule);
175
std::string stringFromIslObj(__isl_keep isl_multi_aff *maff);
176
std::string stringFromIslObj(__isl_keep isl_pw_multi_aff *pma);
177
std::string stringFromIslObj(__isl_keep isl_multi_pw_aff *mpa);
178
std::string stringFromIslObj(__isl_keep isl_union_pw_multi_aff *upma);
179
std::string stringFromIslObj(__isl_keep isl_aff *aff);
180
std::string stringFromIslObj(__isl_keep isl_pw_aff *pwaff);
181
std::string stringFromIslObj(__isl_keep isl_space *space);
182
//@}
183
184
inline llvm::raw_ostream &operator<<(llvm::raw_ostream &OS,
185
244
                                     __isl_keep isl_union_map *Map) {
186
244
  OS << polly::stringFromIslObj(Map);
187
244
  return OS;
188
244
}
189
190
inline llvm::raw_ostream &operator<<(llvm::raw_ostream &OS,
191
0
                                     __isl_keep isl_map *Map) {
192
0
  OS << polly::stringFromIslObj(Map);
193
0
  return OS;
194
0
}
195
196
inline llvm::raw_ostream &operator<<(llvm::raw_ostream &OS,
197
1.53k
                                     __isl_keep isl_set *Set) {
198
1.53k
  OS << polly::stringFromIslObj(Set);
199
1.53k
  return OS;
200
1.53k
}
201
202
inline llvm::raw_ostream &operator<<(llvm::raw_ostream &OS,
203
0
                                     __isl_keep isl_pw_aff *Map) {
204
0
  OS << polly::stringFromIslObj(Map);
205
0
  return OS;
206
0
}
207
208
inline llvm::raw_ostream &operator<<(llvm::raw_ostream &OS,
209
484
                                     __isl_keep isl_pw_multi_aff *PMA) {
210
484
  OS << polly::stringFromIslObj(PMA);
211
484
  return OS;
212
484
}
213
214
inline llvm::raw_ostream &operator<<(llvm::raw_ostream &OS,
215
0
                                     __isl_keep isl_multi_aff *MA) {
216
0
  OS << polly::stringFromIslObj(MA);
217
0
  return OS;
218
0
}
219
220
inline llvm::raw_ostream &operator<<(llvm::raw_ostream &OS,
221
0
                                     __isl_keep isl_union_pw_multi_aff *UPMA) {
222
0
  OS << polly::stringFromIslObj(UPMA);
223
0
  return OS;
224
0
}
225
226
inline llvm::raw_ostream &operator<<(llvm::raw_ostream &OS,
227
0
                                     __isl_keep isl_schedule *Schedule) {
228
0
  OS << polly::stringFromIslObj(Schedule);
229
0
  return OS;
230
0
}
231
232
inline llvm::raw_ostream &operator<<(llvm::raw_ostream &OS,
233
0
                                     __isl_keep isl_space *Space) {
234
0
  OS << polly::stringFromIslObj(Space);
235
0
  return OS;
236
0
}
237
238
/// Combine Prefix, Val (or Number) and Suffix to an isl-compatible name.
239
///
240
/// In case @p UseInstructionNames is set, this function returns:
241
///
242
/// @p Prefix + "_" + @p Val->getName() + @p Suffix
243
///
244
/// otherwise
245
///
246
/// @p Prefix + to_string(Number) + @p Suffix
247
///
248
/// We ignore the value names by default, as they may change between release
249
/// and debug mode and can consequently not be used when aiming for reproducible
250
/// builds. However, for debugging named statements are often helpful, hence
251
/// we allow their optional use.
252
std::string getIslCompatibleName(const std::string &Prefix,
253
                                 const llvm::Value *Val, long Number,
254
                                 const std::string &Suffix,
255
                                 bool UseInstructionNames);
256
257
/// Combine Prefix, Name (or Number) and Suffix to an isl-compatible name.
258
///
259
/// In case @p UseInstructionNames is set, this function returns:
260
///
261
/// @p Prefix + "_" + Name + @p Suffix
262
///
263
/// otherwise
264
///
265
/// @p Prefix + to_string(Number) + @p Suffix
266
///
267
/// We ignore @p Name by default, as they may change between release
268
/// and debug mode and can consequently not be used when aiming for reproducible
269
/// builds. However, for debugging named statements are often helpful, hence
270
/// we allow their optional use.
271
std::string getIslCompatibleName(const std::string &Prefix,
272
                                 const std::string &Middle, long Number,
273
                                 const std::string &Suffix,
274
                                 bool UseInstructionNames);
275
276
std::string getIslCompatibleName(const std::string &Prefix,
277
                                 const std::string &Middle,
278
                                 const std::string &Suffix);
279
280
// Make isl::give available in polly namespace. We do this as there was
281
// previously a function polly::give() which did the very same thing and we
282
// did not want yet to introduce the isl:: prefix to each call of give.
283
using isl::give;
284
285
inline llvm::DiagnosticInfoOptimizationBase &
286
operator<<(llvm::DiagnosticInfoOptimizationBase &OS,
287
4
           const isl::union_map &Obj) {
288
4
  OS << Obj.to_str();
289
4
  return OS;
290
4
}
291
292
/// Scoped limit of ISL operations.
293
///
294
/// Limits the number of ISL operations during the lifetime of this object. The
295
/// idea is to use this as an RAII guard for the scope where the code is aware
296
/// that ISL can return errors even when all input is valid. After leaving the
297
/// scope, it will return to the error setting as it was before. That also means
298
/// that the error setting should not be changed while in that scope.
299
///
300
/// Such scopes are not allowed to be nested because the previous operations
301
/// counter cannot be reset to the previous state, or one that adds the
302
/// operations while being in the nested scope. Use therefore is only allowed
303
/// while currently a no operations-limit is active.
304
class IslMaxOperationsGuard {
305
private:
306
  /// The ISL context to set the operations limit.
307
  ///
308
  /// If set to nullptr, there is no need for any action at the end of the
309
  /// scope.
310
  isl_ctx *IslCtx;
311
312
  /// Old OnError setting; to reset to when the scope ends.
313
  int OldOnError;
314
315
public:
316
  /// Enter a max operations scope.
317
  ///
318
  /// @param IslCtx      The ISL context to set the operations limit for.
319
  /// @param LocalMaxOps Maximum number of operations allowed in the
320
  ///                    scope. If set to zero, no operations limit is enforced.
321
  IslMaxOperationsGuard(isl_ctx *IslCtx, unsigned long LocalMaxOps)
322
987
      : IslCtx(IslCtx) {
323
987
    assert(IslCtx);
324
987
    assert(isl_ctx_get_max_operations(IslCtx) == 0 &&
325
987
           "Nested max operations not supported");
326
987
327
987
    if (
LocalMaxOps == 0987
)
{0
328
0
      // No limit on operations; also disable restoring on_error/max_operations.
329
0
      this->IslCtx = nullptr;
330
0
      return;
331
987
    }
332
987
333
987
    // Save previous state.
334
987
    OldOnError = isl_options_get_on_error(IslCtx);
335
987
336
987
    // Activate the new setting.
337
987
    isl_ctx_set_max_operations(IslCtx, LocalMaxOps);
338
987
    isl_ctx_reset_operations(IslCtx);
339
987
    isl_options_set_on_error(IslCtx, ISL_ON_ERROR_CONTINUE);
340
987
  }
341
342
  /// Leave the max operations scope.
343
987
  ~IslMaxOperationsGuard() {
344
987
    if (!IslCtx)
345
0
      return;
346
987
347
987
    assert(isl_options_get_on_error(IslCtx) == ISL_ON_ERROR_CONTINUE &&
348
987
           "Unexpected change of the on_error setting");
349
987
350
987
    // Return to the previous error setting.
351
987
    isl_ctx_set_max_operations(IslCtx, 0);
352
987
    isl_options_set_on_error(IslCtx, OldOnError);
353
987
  }
354
};
355
356
} // end namespace polly
357
358
#endif