xref: /llvm-project/polly/lib/Support/GICHelper.cpp (revision cfe117def7b0714e5d724c80839e68f40fba4f47)
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 
13 #include "polly/Support/GICHelper.h"
14 #include "llvm/ADT/APInt.h"
15 #include "isl/val.h"
16 
17 using namespace llvm;
18 
19 __isl_give isl_val *polly::isl_valFromAPInt(isl_ctx *Ctx, const APInt Int,
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 
51 APInt polly::APIntFromVal(__isl_take isl_val *Val) {
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.getMinSignedBits() < A.getBitWidth())
78     A = A.trunc(A.getMinSignedBits());
79 
80   free(Data);
81   isl_val_free(Val);
82   return A;
83 }
84 
85 template <typename ISLTy, typename ISL_CTX_GETTER, typename ISL_PRINTER>
86 static inline std::string stringFromIslObjInternal(__isl_keep ISLTy *isl_obj,
87                                                    ISL_CTX_GETTER ctx_getter_fn,
88                                                    ISL_PRINTER printer_fn,
89                                                    std::string DefaultValue) {
90   if (!isl_obj)
91     return 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);
96   std::string string;
97   if (char_str)
98     string = char_str;
99   else
100     string = DefaultValue;
101   free(char_str);
102   isl_printer_free(p);
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 
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)
136 
137 static void replace(std::string &str, const std::string &find,
138                     const std::string &replace) {
139   size_t pos = 0;
140   while ((pos = str.find(find, pos)) != std::string::npos) {
141     str.replace(pos, find.length(), replace);
142     pos += replace.length();
143   }
144 }
145 
146 static void makeIslCompatible(std::string &str) {
147   replace(str, ".", "_");
148   replace(str, "\"", "_");
149   replace(str, " ", "__");
150   replace(str, "=>", "TO");
151   replace(str, "+", "_");
152 }
153 
154 std::string polly::getIslCompatibleName(const std::string &Prefix,
155                                         const std::string &Middle,
156                                         const std::string &Suffix) {
157   std::string S = Prefix + Middle + Suffix;
158   makeIslCompatible(S);
159   return S;
160 }
161 
162 std::string polly::getIslCompatibleName(const std::string &Prefix,
163                                         const std::string &Name, long Number,
164                                         const std::string &Suffix,
165                                         bool UseInstructionNames) {
166   std::string S = Prefix;
167 
168   if (UseInstructionNames)
169     S += std::string("_") + Name;
170   else
171     S += std::to_string(Number);
172 
173   S += Suffix;
174 
175   makeIslCompatible(S);
176   return S;
177 }
178 
179 std::string polly::getIslCompatibleName(const std::string &Prefix,
180                                         const Value *Val, long Number,
181                                         const std::string &Suffix,
182                                         bool UseInstructionNames) {
183   std::string ValStr;
184 
185   if (UseInstructionNames && Val->hasName())
186     ValStr = std::string("_") + std::string(Val->getName());
187   else
188     ValStr = std::to_string(Number);
189 
190   return getIslCompatibleName(Prefix, ValStr, Suffix);
191 }
192 
193 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
194 /// To call a inline dump() method in a debugger, at it must have been
195 /// instantiated in at least one translation unit. Because isl's dump() method
196 /// are meant to be called from a debugger only, but not from code, no such
197 /// instantiation would exist. We use this method to force an instantiation in
198 /// this translation unit. Because it has non-static linking, the compiler does
199 /// not know that it is never called, and therefore must ensure the existence of
200 /// the dump functions.
201 void neverCalled() {
202   isl::aff().dump();
203   isl::aff_list().dump();
204   isl::ast_expr().dump();
205   isl::ast_expr_list().dump();
206   isl::ast_node().dump();
207   isl::ast_node_list().dump();
208   isl::basic_map().dump();
209   isl::basic_map_list().dump();
210   isl::basic_set().dump();
211   isl::basic_set_list().dump();
212   isl::constraint().dump();
213   isl::constraint_list().dump();
214   isl::id().dump();
215   isl::id_list().dump();
216   isl::id_to_ast_expr().dump();
217   isl::local_space().dump();
218   isl::map().dump();
219   isl::map_list().dump();
220   isl::multi_aff().dump();
221   isl::multi_pw_aff().dump();
222   isl::multi_union_pw_aff().dump();
223   isl::multi_val().dump();
224   isl::point().dump();
225   isl::pw_aff().dump();
226   isl::pw_aff_list().dump();
227   isl::pw_multi_aff().dump();
228   isl::pw_qpolynomial().dump();
229   isl::qpolynomial().dump();
230   isl::schedule().dump();
231   isl::schedule_constraints().dump();
232   isl::schedule_node().dump();
233   isl::set().dump();
234   isl::set_list().dump();
235   isl::space().dump();
236   isl::union_map().dump();
237   isl::union_map_list().dump();
238   isl::union_pw_aff().dump();
239   isl::union_pw_aff_list().dump();
240   isl::union_pw_multi_aff().dump();
241   isl::union_pw_multi_aff_list().dump();
242   isl::union_set().dump();
243   isl::union_set_list().dump();
244   isl::val().dump();
245   isl::val_list().dump();
246 }
247 #endif
248