Polly 20.0.0git
GICHelper.cpp
Go to the documentation of this file.
1//===- GmpConv.cpp - Recreate LLVM IR from the Scop. ---------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// Functions for converting between gmp objects and llvm::APInt.
10//
11//===----------------------------------------------------------------------===//
12
14#include "llvm/ADT/APInt.h"
15#include "isl/val.h"
16
17using namespace llvm;
18
20 bool IsSigned) {
21 APInt Abs;
22 isl_val *v;
23
24 // As isl is interpreting the input always as unsigned value, we need some
25 // additional pre and post processing to import signed values. The approach
26 // we take is to first obtain the absolute value of Int and then negate the
27 // value after it has been imported to isl.
28 //
29 // It should be noted that the smallest integer value represented in two's
30 // complement with a certain amount of bits does not have a corresponding
31 // positive representation in two's complement representation with the same
32 // number of bits. E.g. 110 (-2) does not have a corresponding value for (2).
33 // To ensure that there is always a corresponding value available we first
34 // sign-extend the input by one bit and only then take the absolute value.
35 if (IsSigned)
36 Abs = Int.sext(Int.getBitWidth() + 1).abs();
37 else
38 Abs = Int;
39
40 const uint64_t *Data = Abs.getRawData();
41 unsigned Words = Abs.getNumWords();
42
43 v = isl_val_int_from_chunks(Ctx, Words, sizeof(uint64_t), Data);
44
45 if (IsSigned && Int.isNegative())
46 v = isl_val_neg(v);
47
48 return v;
49}
50
52 uint64_t *Data;
53 int NumChunks;
54 const static int ChunkSize = sizeof(uint64_t);
55
56 assert(isl_val_is_int(Val) && "Only integers can be converted to APInt");
57
58 NumChunks = isl_val_n_abs_num_chunks(Val, ChunkSize);
59 Data = (uint64_t *)malloc(NumChunks * ChunkSize);
60 isl_val_get_abs_num_chunks(Val, ChunkSize, Data);
61 int NumBits = CHAR_BIT * ChunkSize * NumChunks;
62 APInt A(NumBits, NumChunks, Data);
63
64 // As isl provides only an interface to obtain data that describes the
65 // absolute value of an isl_val, A at this point always contains a positive
66 // number. In case Val was originally negative, we expand the size of A by
67 // one and negate the value (in two's complement representation). As a result,
68 // the new value in A corresponds now with Val.
69 if (isl_val_is_neg(Val)) {
70 A = A.zext(A.getBitWidth() + 1);
71 A = -A;
72 }
73
74 // isl may represent small numbers with more than the minimal number of bits.
75 // We truncate the APInt to the minimal number of bits needed to represent the
76 // signed value it contains, to ensure that the bitwidth is always minimal.
77 if (A.getSignificantBits() < A.getBitWidth())
78 A = A.trunc(A.getSignificantBits());
79
80 free(Data);
81 isl_val_free(Val);
82 return A;
83}
84
85template <typename ISLTy, typename ISL_CTX_GETTER, typename ISL_PRINTER>
86static inline std::string
88 ISL_CTX_GETTER ctx_getter_fn, ISL_PRINTER printer_fn,
89 const std::string &DefaultValue) {
90 if (!isl_obj)
91 return DefaultValue;
92 isl_ctx *ctx = ctx_getter_fn(isl_obj);
94 p = printer_fn(p, isl_obj);
95 char *char_str = isl_printer_get_str(p);
96 std::string string;
97 if (char_str)
98 string = char_str;
99 else
100 string = DefaultValue;
101 free(char_str);
103 return string;
104}
105
106#define ISL_C_OBJECT_TO_STRING(name) \
107 std::string polly::stringFromIslObj(__isl_keep isl_##name *Obj, \
108 std::string DefaultValue) { \
109 return stringFromIslObjInternal(Obj, isl_##name##_get_ctx, \
110 isl_printer_print_##name, DefaultValue); \
111 }
112
116ISL_C_OBJECT_TO_STRING(basic_map)
117ISL_C_OBJECT_TO_STRING(basic_set)
121ISL_C_OBJECT_TO_STRING(multi_aff)
122ISL_C_OBJECT_TO_STRING(multi_pw_aff)
123ISL_C_OBJECT_TO_STRING(multi_union_pw_aff)
126ISL_C_OBJECT_TO_STRING(pw_multi_aff)
128ISL_C_OBJECT_TO_STRING(schedule_node)
130ISL_C_OBJECT_TO_STRING(union_access_info)
131ISL_C_OBJECT_TO_STRING(union_flow)
132ISL_C_OBJECT_TO_STRING(union_set)
133ISL_C_OBJECT_TO_STRING(union_map)
134ISL_C_OBJECT_TO_STRING(union_pw_aff)
135ISL_C_OBJECT_TO_STRING(union_pw_multi_aff)
136
137static void replace(std::string &str, StringRef find, StringRef replace) {
138 size_t pos = 0;
139 while ((pos = str.find(find, pos)) != std::string::npos) {
140 str.replace(pos, find.size(), replace);
141 pos += replace.size();
142 }
143}
144
145static void makeIslCompatible(std::string &str) {
146 llvm::replace(str, '.', '_');
147 llvm::replace(str, '\"', '_');
148 replace(str, StringRef(" "), StringRef("__"));
149 replace(str, StringRef("=>"), StringRef("TO"));
150 llvm::replace(str, '+', '_');
151}
152
153std::string polly::getIslCompatibleName(const std::string &Prefix,
154 const std::string &Middle,
155 const std::string &Suffix) {
156 std::string S = Prefix + Middle + Suffix;
158 return S;
159}
160
161std::string polly::getIslCompatibleName(const std::string &Prefix,
162 const std::string &Name, long Number,
163 const std::string &Suffix,
164 bool UseInstructionNames) {
165 std::string S = Prefix;
166
168 S += std::string("_") + Name;
169 else
170 S += std::to_string(Number);
171
172 S += Suffix;
173
175 return S;
176}
177
178std::string polly::getIslCompatibleName(const std::string &Prefix,
179 const Value *Val, long Number,
180 const std::string &Suffix,
181 bool UseInstructionNames) {
182 std::string ValStr;
183
184 if (UseInstructionNames && Val->hasName())
185 ValStr = std::string("_") + std::string(Val->getName());
186 else
187 ValStr = std::to_string(Number);
188
189 return getIslCompatibleName(Prefix, ValStr, Suffix);
190}
191
192#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
193#define ISL_DUMP_OBJECT_IMPL(NAME) \
194 void polly::dumpIslObj(const isl::NAME &Obj) { \
195 isl_##NAME##_dump(Obj.get()); \
196 } \
197 void polly::dumpIslObj(isl_##NAME *Obj) { isl_##NAME##_dump(Obj); }
198
200ISL_DUMP_OBJECT_IMPL(aff_list)
201ISL_DUMP_OBJECT_IMPL(ast_expr)
202ISL_DUMP_OBJECT_IMPL(ast_node)
203ISL_DUMP_OBJECT_IMPL(ast_node_list)
204ISL_DUMP_OBJECT_IMPL(basic_map)
205ISL_DUMP_OBJECT_IMPL(basic_map_list)
206ISL_DUMP_OBJECT_IMPL(basic_set)
207ISL_DUMP_OBJECT_IMPL(basic_set_list)
208ISL_DUMP_OBJECT_IMPL(constraint)
211ISL_DUMP_OBJECT_IMPL(id_to_ast_expr)
212ISL_DUMP_OBJECT_IMPL(local_space)
214ISL_DUMP_OBJECT_IMPL(map_list)
215ISL_DUMP_OBJECT_IMPL(multi_aff)
216ISL_DUMP_OBJECT_IMPL(multi_pw_aff)
217ISL_DUMP_OBJECT_IMPL(multi_union_pw_aff)
218ISL_DUMP_OBJECT_IMPL(multi_val)
221ISL_DUMP_OBJECT_IMPL(pw_aff_list)
222ISL_DUMP_OBJECT_IMPL(pw_multi_aff)
224ISL_DUMP_OBJECT_IMPL(schedule_constraints)
225ISL_DUMP_OBJECT_IMPL(schedule_node)
227ISL_DUMP_OBJECT_IMPL(set_list)
229ISL_DUMP_OBJECT_IMPL(union_map)
230ISL_DUMP_OBJECT_IMPL(union_pw_aff)
231ISL_DUMP_OBJECT_IMPL(union_pw_aff_list)
232ISL_DUMP_OBJECT_IMPL(union_pw_multi_aff)
233ISL_DUMP_OBJECT_IMPL(union_set)
234ISL_DUMP_OBJECT_IMPL(union_set_list)
236ISL_DUMP_OBJECT_IMPL(val_list)
237
238void polly::dumpIslObj(__isl_keep isl_schedule_node *node, raw_ostream &OS) {
239 if (!node)
240 return;
241
246
247 char *char_str = isl_printer_get_str(p);
248 OS << char_str;
249
250 free(char_str);
252}
253
254void polly::dumpIslObj(const isl::schedule_node &Node, raw_ostream &OS) {
255 dumpIslObj(Node.get(), OS);
256}
257
258#endif
static std::string stringFromIslObjInternal(__isl_keep ISLTy *isl_obj, ISL_CTX_GETTER ctx_getter_fn, ISL_PRINTER printer_fn, const std::string &DefaultValue)
Definition: GICHelper.cpp:87
static void replace(std::string &str, StringRef find, StringRef replace)
Definition: GICHelper.cpp:137
#define ISL_C_OBJECT_TO_STRING(name)
Definition: GICHelper.cpp:106
#define ISL_DUMP_OBJECT_IMPL(NAME)
Definition: GICHelper.cpp:193
static void makeIslCompatible(std::string &str)
Definition: GICHelper.cpp:145
__isl_keep isl_schedule_node * get() const
A()
#define __isl_take
Definition: ctx.h:22
#define __isl_give
Definition: ctx.h:19
#define __isl_keep
Definition: ctx.h:25
__isl_export __isl_give ISL_HMAP __isl_take ISL_KEY __isl_take ISL_VAL * val
Definition: hmap.h:32
static int find(int *con, unsigned len, int status)
Definition: isl_coalesce.c:142
static unsigned pos(__isl_keep isl_space *space, enum isl_dim_type type)
Definition: isl_map.c:70
const char * schedule
Definition: isl_test.c:10697
const char * set
Definition: isl_test.c:1356
const char * map
Definition: isl_test.c:1783
const char * p
Definition: isl_test.c:8643
const char * point
Definition: isl_test.c:10700
const char * aff
Definition: isl_test.c:7278
const char * str
Definition: isl_test.c:2095
#define assert(exp)
This file contains the declaration of the PolyhedralInfo class, which will provide an interface to ex...
std::string getIslCompatibleName(const std::string &Prefix, const llvm::Value *Val, long Number, const std::string &Suffix, bool UseInstructionNames)
Combine Prefix, Val (or Number) and Suffix to an isl-compatible name.
__isl_give isl_val * isl_valFromAPInt(isl_ctx *Ctx, const llvm::APInt Int, bool IsSigned)
Translate an llvm::APInt to an isl_val.
bool UseInstructionNames
Definition: ScopInfo.cpp:156
void dumpIslObj(const isl::schedule_node &Node, llvm::raw_ostream &OS)
Emit the equivaltent of the isl_*_dump output into a raw_ostream.
llvm::APInt APIntFromVal(__isl_take isl_val *Val)
Translate isl_val to llvm::APInt.
Definition: GICHelper.cpp:51
__isl_null isl_printer * isl_printer_free(__isl_take isl_printer *printer)
Definition: isl_printer.c:269
__isl_give char * isl_printer_get_str(__isl_keep isl_printer *printer)
Definition: isl_printer.c:677
#define ISL_YAML_STYLE_BLOCK
Definition: printer.h:38
__isl_give isl_printer * isl_printer_set_yaml_style(__isl_take isl_printer *p, int yaml_style)
Definition: isl_printer.c:464
__isl_give isl_printer * isl_printer_to_str(isl_ctx *ctx)
Definition: isl_printer.c:240
__isl_give isl_printer * isl_printer_print_schedule_node(__isl_take isl_printer *p, __isl_keep isl_schedule_node *node)
isl_ctx * isl_schedule_node_get_ctx(__isl_keep isl_schedule_node *node)
Definition: obj.h:48
static TupleKindPtr Ctx
__isl_give isl_val * isl_val_int_from_chunks(isl_ctx *ctx, size_t n, size_t size, const void *chunks)
Definition: isl_val_gmp.c:71
isl_size isl_val_n_abs_num_chunks(__isl_keep isl_val *v, size_t size)
Definition: isl_val_gmp.c:89
__isl_export isl_bool isl_val_is_neg(__isl_keep isl_val *v)
Definition: isl_val.c:1234
__isl_null isl_val * isl_val_free(__isl_take isl_val *v)
Definition: isl_val.c:263
isl_stat isl_val_get_abs_num_chunks(__isl_keep isl_val *v, size_t size, void *chunks)
Definition: isl_val_gmp.c:113
__isl_export __isl_give isl_val * isl_val_neg(__isl_take isl_val *v)
Definition: isl_val.c:410
__isl_export isl_bool isl_val_is_int(__isl_keep isl_val *v)
Definition: isl_val.c:1141