Coverage Report

Created: 2017-04-29 12:21

/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
0
                             bool IsSigned) {
105
0
  return isl::manage(isl_valFromAPInt(Ctx, Int, IsSigned));
106
0
}
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 bitpattern 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 bitpattern 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.28k
                                     __isl_keep isl_set *Set) {
198
1.28k
  OS << polly::stringFromIslObj(Set);
199
1.28k
  return OS;
200
1.28k
}
201
202
inline llvm::raw_ostream &operator<<(llvm::raw_ostream &OS,
203
209
                                     __isl_keep isl_pw_aff *Map) {
204
209
  OS << polly::stringFromIslObj(Map);
205
209
  return OS;
206
209
}
207
208
inline llvm::raw_ostream &operator<<(llvm::raw_ostream &OS,
209
338
                                     __isl_keep isl_pw_multi_aff *PMA) {
210
338
  OS << polly::stringFromIslObj(PMA);
211
338
  return OS;
212
338
}
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
/// Return @p Prefix + @p Val->getName() + @p Suffix but Isl compatible.
239
std::string getIslCompatibleName(const std::string &Prefix,
240
                                 const llvm::Value *Val,
241
                                 const std::string &Suffix);
242
243
std::string getIslCompatibleName(const std::string &Prefix,
244
                                 const std::string &Middle,
245
                                 const std::string &Suffix);
246
247
// Make isl::give available in polly namespace. We do this as there was
248
// previously a function polly::give() which did the very same thing and we
249
// did not want yet to introduce the isl:: prefix to each call of give.
250
using isl::give;
251
252
inline llvm::DiagnosticInfoOptimizationBase &
253
operator<<(llvm::DiagnosticInfoOptimizationBase &OS,
254
4
           const isl::union_map &Obj) {
255
4
  OS << Obj.to_str();
256
4
  return OS;
257
4
}
258
259
/// Scoped limit of ISL operations.
260
///
261
/// Limits the number of ISL operations during the lifetime of this object. The
262
/// idea is to use this as an RAII guard for the scope where the code is aware
263
/// that ISL can return errors even when all input is valid. After leaving the
264
/// scope, it will return to the error setting as it was before. That also means
265
/// that the error setting should not be changed while in that scope.
266
///
267
/// Such scopes are not allowed to be nested because the previous operations
268
/// counter cannot be reset to the previous state, or one that adds the
269
/// operations while being in the nested scope. Use therefore is only allowed
270
/// while currently a no operations-limit is active.
271
class IslMaxOperationsGuard {
272
private:
273
  /// The ISL context to set the operations limit.
274
  ///
275
  /// If set to nullptr, there is no need for any action at the end of the
276
  /// scope.
277
  isl_ctx *IslCtx;
278
279
  /// Old OnError setting; to reset to when the scope ends.
280
  int OldOnError;
281
282
public:
283
  /// Enter a max operations scope.
284
  ///
285
  /// @param IslCtx      The ISL context to set the operations limit for.
286
  /// @param LocalMaxOps Maximum number of operations allowed in the
287
  ///                    scope. If set to zero, no operations limit is enforced.
288
  IslMaxOperationsGuard(isl_ctx *IslCtx, unsigned long LocalMaxOps)
289
512
      : IslCtx(IslCtx) {
290
512
    assert(IslCtx);
291
512
    assert(isl_ctx_get_max_operations(IslCtx) == 0 &&
292
512
           "Nested max operations not supported");
293
512
294
512
    if (
LocalMaxOps == 0512
)
{0
295
0
      // No limit on operations; also disable restoring on_error/max_operations.
296
0
      this->IslCtx = nullptr;
297
0
      return;
298
0
    }
299
512
300
512
    // Save previous state.
301
512
    OldOnError = isl_options_get_on_error(IslCtx);
302
512
303
512
    // Activate the new setting.
304
512
    isl_ctx_set_max_operations(IslCtx, LocalMaxOps);
305
512
    isl_ctx_reset_operations(IslCtx);
306
512
    isl_options_set_on_error(IslCtx, ISL_ON_ERROR_CONTINUE);
307
512
  }
308
309
  /// Leave the max operations scope.
310
512
  ~IslMaxOperationsGuard() {
311
512
    if (!IslCtx)
312
0
      return;
313
512
314
512
    assert(isl_options_get_on_error(IslCtx) == ISL_ON_ERROR_CONTINUE &&
315
512
           "Unexpected change of the on_error setting");
316
512
317
512
    // Return to the previous error setting.
318
512
    isl_ctx_set_max_operations(IslCtx, 0);
319
512
    isl_options_set_on_error(IslCtx, OldOnError);
320
512
  }
321
};
322
323
} // end namespace polly
324
325
#endif