Coverage Report

Created: 2017-03-28 09:59

/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
239
                                     __isl_keep isl_union_map *Map) {
186
239
  OS << polly::stringFromIslObj(Map);
187
239
  return OS;
188
239
}
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
/// Enumerate all isl_basic_maps of an isl_map.
260
///
261
/// This basically wraps isl_map_foreach_basic_map() and allows to call back
262
/// C++11 closures.
263
void foreachElt(const isl::map &Map,
264
                const std::function<void(isl::basic_map)> &F);
265
266
/// Enumerate all isl_basic_sets of an isl_set.
267
///
268
/// This basically wraps isl_set_foreach_basic_set() and allows to call back
269
/// C++11 closures.
270
void foreachElt(const isl::set &Set,
271
                const std::function<void(isl::basic_set)> &F);
272
273
/// Enumerate all isl_maps of an isl_union_map.
274
///
275
/// This basically wraps isl_union_map_foreach_map() and allows to call back
276
/// C++11 closures.
277
void foreachElt(const isl::union_map &UMap,
278
                const std::function<void(isl::map Map)> &F);
279
280
/// Enumerate all isl_sets of an isl_union_set.
281
///
282
/// This basically wraps isl_union_set_foreach_set() and allows to call back
283
/// C++11 closures.
284
void foreachElt(const isl::union_set &USet,
285
                const std::function<void(isl::set Set)> &F);
286
287
/// Enumerate all isl_pw_aff of an isl_union_pw_aff.
288
///
289
/// This basically wraps isl_union_pw_aff(), but also allows to call back C++11
290
/// closures.
291
void foreachElt(const isl::union_pw_aff &UPwAff,
292
                const std::function<void(isl::pw_aff)> &F);
293
294
/// Enumerate all polyhedra of an isl_map.
295
///
296
/// This is a wrapper for isl_map_foreach_basic_map() that allows to call back
297
/// C++ closures. The callback has the possibility to interrupt (break) the
298
/// enumeration by returning isl_stat_error. A return value of isl_stat_ok will
299
/// continue enumerations, if any more elements are left.
300
///
301
/// @param UMap Collection to enumerate.
302
/// @param F    The callback function, lambda or closure.
303
///
304
/// @return The isl_stat returned by the last callback invocation; isl_stat_ok
305
///         if the collection was empty.
306
isl_stat foreachEltWithBreak(const isl::map &Map,
307
                             const std::function<isl_stat(isl::basic_map)> &F);
308
309
/// Enumerate all isl_maps of an isl_union_map.
310
///
311
/// This is a wrapper for isl_union_map_foreach_map() that allows to call back
312
/// C++ closures. In contrast to the variant without "_with_break", the callback
313
/// has the possibility to interrupt (break) the enumeration by returning
314
/// isl_stat_error. A return value of isl_stat_ok will continue enumerations, if
315
/// any more elements are left.
316
///
317
/// @param UMap Collection to enumerate.
318
/// @param F    The callback function, lambda or closure.
319
///
320
/// @return The isl_stat returned by the last callback invocation; isl_stat_ok
321
///         if the collection was initially empty.
322
isl_stat foreachEltWithBreak(const isl::union_map &UMap,
323
                             const std::function<isl_stat(isl::map Map)> &F);
324
325
/// Enumerate all pieces of an isl_pw_aff.
326
///
327
/// This is a wrapper around isl_pw_aff_foreach_piece() that allows to call back
328
/// C++11 closures. The callback has the possibility to interrupt (break) the
329
/// enumeration by returning isl_stat_error. A return value of isl_stat_ok will
330
/// continue enumerations, if any more elements are left.
331
///
332
/// @param UMap Collection to enumerate.
333
/// @param F    The callback function, lambda or closure.
334
///
335
/// @return The isl_stat returned by the last callback invocation; isl_stat_ok
336
///         if the collection was initially empty.
337
isl_stat
338
foreachPieceWithBreak(const isl::pw_aff &PwAff,
339
                      const std::function<isl_stat(isl::set, isl::aff)> &F);
340
341
/// Scoped limit of ISL operations.
342
///
343
/// Limits the number of ISL operations during the lifetime of this object. The
344
/// idea is to use this as an RAII guard for the scope where the code is aware
345
/// that ISL can return errors even when all input is valid. After leaving the
346
/// scope, it will return to the error setting as it was before. That also means
347
/// that the error setting should not be changed while in that scope.
348
///
349
/// Such scopes are not allowed to be nested because the previous operations
350
/// counter cannot be reset to the previous state, or one that adds the
351
/// operations while being in the nested scope. Use therefore is only allowed
352
/// while currently a no operations-limit is active.
353
class IslMaxOperationsGuard {
354
private:
355
  /// The ISL context to set the operations limit.
356
  ///
357
  /// If set to nullptr, there is no need for any action at the end of the
358
  /// scope.
359
  isl_ctx *IslCtx;
360
361
  /// Old OnError setting; to reset to when the scope ends.
362
  int OldOnError;
363
364
public:
365
  /// Enter a max operations scope.
366
  ///
367
  /// @param IslCtx      The ISL context to set the operations limit for.
368
  /// @param LocalMaxOps Maximum number of operations allowed in the
369
  ///                    scope. If set to zero, no operations limit is enforced.
370
  IslMaxOperationsGuard(isl_ctx *IslCtx, unsigned long LocalMaxOps)
371
507
      : IslCtx(IslCtx) {
372
507
    assert(IslCtx);
373
507
    assert(isl_ctx_get_max_operations(IslCtx) == 0 &&
374
507
           "Nested max operations not supported");
375
507
376
507
    if (
LocalMaxOps == 0507
)
{0
377
0
      // No limit on operations; also disable restoring on_error/max_operations.
378
0
      this->IslCtx = nullptr;
379
0
      return;
380
0
    }
381
507
382
507
    // Save previous state.
383
507
    OldOnError = isl_options_get_on_error(IslCtx);
384
507
385
507
    // Activate the new setting.
386
507
    isl_ctx_set_max_operations(IslCtx, LocalMaxOps);
387
507
    isl_ctx_reset_operations(IslCtx);
388
507
    isl_options_set_on_error(IslCtx, ISL_ON_ERROR_CONTINUE);
389
507
  }
390
391
  /// Leave the max operations scope.
392
507
  ~IslMaxOperationsGuard() {
393
507
    if (!IslCtx)
394
0
      return;
395
507
396
507
    assert(isl_options_get_on_error(IslCtx) == ISL_ON_ERROR_CONTINUE &&
397
507
           "Unexpected change of the on_error setting");
398
507
399
507
    // Return to the previous error setting.
400
507
    isl_ctx_set_max_operations(IslCtx, 0);
401
507
    isl_options_set_on_error(IslCtx, OldOnError);
402
507
  }
403
};
404
405
} // end namespace polly
406
407
#endif