1 //===- GmpConv.cpp - Recreate LLVM IR from the Scop. ---------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // Functions for converting between gmp objects and llvm::APInt.
11 //===----------------------------------------------------------------------===//
13 #include "polly/Support/GICHelper.h"
14 #include "llvm/ADT/APInt.h"
19 __isl_give isl_val
*polly::isl_valFromAPInt(isl_ctx
*Ctx
, const APInt Int
,
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.
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.
36 Abs
= Int
.sext(Int
.getBitWidth() + 1).abs();
40 const uint64_t *Data
= Abs
.getRawData();
41 unsigned Words
= Abs
.getNumWords();
43 v
= isl_val_int_from_chunks(Ctx
, Words
, sizeof(uint64_t), Data
);
45 if (IsSigned
&& Int
.isNegative())
51 APInt
polly::APIntFromVal(__isl_take isl_val
*Val
) {
54 const static int ChunkSize
= sizeof(uint64_t);
56 assert(isl_val_is_int(Val
) && "Only integers can be converted to APInt");
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
);
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);
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());
85 template <typename ISLTy
, typename ISL_CTX_GETTER
, typename ISL_PRINTER
>
86 static inline std::string
87 stringFromIslObjInternal(__isl_keep ISLTy
*isl_obj
,
88 ISL_CTX_GETTER ctx_getter_fn
, ISL_PRINTER printer_fn
,
89 const std::string
&DefaultValue
) {
92 isl_ctx
*ctx
= ctx_getter_fn(isl_obj
);
93 isl_printer
*p
= isl_printer_to_str(ctx
);
94 p
= printer_fn(p
, isl_obj
);
95 char *char_str
= isl_printer_get_str(p
);
100 string
= DefaultValue
;
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); \
113 ISL_C_OBJECT_TO_STRING(aff
)
114 ISL_C_OBJECT_TO_STRING(ast_expr
)
115 ISL_C_OBJECT_TO_STRING(ast_node
)
116 ISL_C_OBJECT_TO_STRING(basic_map
)
117 ISL_C_OBJECT_TO_STRING(basic_set
)
118 ISL_C_OBJECT_TO_STRING(map
)
119 ISL_C_OBJECT_TO_STRING(set
)
120 ISL_C_OBJECT_TO_STRING(id
)
121 ISL_C_OBJECT_TO_STRING(multi_aff
)
122 ISL_C_OBJECT_TO_STRING(multi_pw_aff
)
123 ISL_C_OBJECT_TO_STRING(multi_union_pw_aff
)
124 ISL_C_OBJECT_TO_STRING(point
)
125 ISL_C_OBJECT_TO_STRING(pw_aff
)
126 ISL_C_OBJECT_TO_STRING(pw_multi_aff
)
127 ISL_C_OBJECT_TO_STRING(schedule
)
128 ISL_C_OBJECT_TO_STRING(schedule_node
)
129 ISL_C_OBJECT_TO_STRING(space
)
130 ISL_C_OBJECT_TO_STRING(union_access_info
)
131 ISL_C_OBJECT_TO_STRING(union_flow
)
132 ISL_C_OBJECT_TO_STRING(union_set
)
133 ISL_C_OBJECT_TO_STRING(union_map
)
134 ISL_C_OBJECT_TO_STRING(union_pw_aff
)
135 ISL_C_OBJECT_TO_STRING(union_pw_multi_aff
)
137 static void replace(std::string
&str
, StringRef find
, StringRef replace
) {
139 while ((pos
= str
.find(find
, pos
)) != std::string::npos
) {
140 str
.replace(pos
, find
.size(), replace
);
141 pos
+= replace
.size();
145 static 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
, '+', '_');
153 std::string
polly::getIslCompatibleName(const std::string
&Prefix
,
154 const std::string
&Middle
,
155 const std::string
&Suffix
) {
156 std::string S
= Prefix
+ Middle
+ Suffix
;
157 makeIslCompatible(S
);
161 std::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
;
167 if (UseInstructionNames
)
168 S
+= std::string("_") + Name
;
170 S
+= std::to_string(Number
);
174 makeIslCompatible(S
);
178 std::string
polly::getIslCompatibleName(const std::string
&Prefix
,
179 const Value
*Val
, long Number
,
180 const std::string
&Suffix
,
181 bool UseInstructionNames
) {
184 if (UseInstructionNames
&& Val
->hasName())
185 ValStr
= std::string("_") + std::string(Val
->getName());
187 ValStr
= std::to_string(Number
);
189 return getIslCompatibleName(Prefix
, ValStr
, Suffix
);
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()); \
197 void polly::dumpIslObj(isl_##NAME *Obj) { isl_##NAME##_dump(Obj); }
199 ISL_DUMP_OBJECT_IMPL(aff
)
200 ISL_DUMP_OBJECT_IMPL(aff_list
)
201 ISL_DUMP_OBJECT_IMPL(ast_expr
)
202 ISL_DUMP_OBJECT_IMPL(ast_node
)
203 ISL_DUMP_OBJECT_IMPL(ast_node_list
)
204 ISL_DUMP_OBJECT_IMPL(basic_map
)
205 ISL_DUMP_OBJECT_IMPL(basic_map_list
)
206 ISL_DUMP_OBJECT_IMPL(basic_set
)
207 ISL_DUMP_OBJECT_IMPL(basic_set_list
)
208 ISL_DUMP_OBJECT_IMPL(constraint
)
209 ISL_DUMP_OBJECT_IMPL(id
)
210 ISL_DUMP_OBJECT_IMPL(id_list
)
211 ISL_DUMP_OBJECT_IMPL(id_to_ast_expr
)
212 ISL_DUMP_OBJECT_IMPL(local_space
)
213 ISL_DUMP_OBJECT_IMPL(map
)
214 ISL_DUMP_OBJECT_IMPL(map_list
)
215 ISL_DUMP_OBJECT_IMPL(multi_aff
)
216 ISL_DUMP_OBJECT_IMPL(multi_pw_aff
)
217 ISL_DUMP_OBJECT_IMPL(multi_union_pw_aff
)
218 ISL_DUMP_OBJECT_IMPL(multi_val
)
219 ISL_DUMP_OBJECT_IMPL(point
)
220 ISL_DUMP_OBJECT_IMPL(pw_aff
)
221 ISL_DUMP_OBJECT_IMPL(pw_aff_list
)
222 ISL_DUMP_OBJECT_IMPL(pw_multi_aff
)
223 ISL_DUMP_OBJECT_IMPL(schedule
)
224 ISL_DUMP_OBJECT_IMPL(schedule_constraints
)
225 ISL_DUMP_OBJECT_IMPL(schedule_node
)
226 ISL_DUMP_OBJECT_IMPL(set
)
227 ISL_DUMP_OBJECT_IMPL(set_list
)
228 ISL_DUMP_OBJECT_IMPL(space
)
229 ISL_DUMP_OBJECT_IMPL(union_map
)
230 ISL_DUMP_OBJECT_IMPL(union_pw_aff
)
231 ISL_DUMP_OBJECT_IMPL(union_pw_aff_list
)
232 ISL_DUMP_OBJECT_IMPL(union_pw_multi_aff
)
233 ISL_DUMP_OBJECT_IMPL(union_set
)
234 ISL_DUMP_OBJECT_IMPL(union_set_list
)
235 ISL_DUMP_OBJECT_IMPL(val
)
236 ISL_DUMP_OBJECT_IMPL(val_list
)
238 void polly::dumpIslObj(__isl_keep isl_schedule_node
*node
, raw_ostream
&OS
) {
242 isl_ctx
*ctx
= isl_schedule_node_get_ctx(node
);
243 isl_printer
*p
= isl_printer_to_str(ctx
);
244 p
= isl_printer_set_yaml_style(p
, ISL_YAML_STYLE_BLOCK
);
245 p
= isl_printer_print_schedule_node(p
, node
);
247 char *char_str
= isl_printer_get_str(p
);
254 void polly::dumpIslObj(const isl::schedule_node
&Node
, raw_ostream
&OS
) {
255 dumpIslObj(Node
.get(), OS
);