1 //===- GmpConv.cpp - Recreate LLVM IR from the Scop.  ---------------------===//
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 // Functions for converting between gmp objects and apint.
11 //
12 //===----------------------------------------------------------------------===//
13 #include "polly/Support/GICHelper.h"
14 #include "llvm/IR/Value.h"
15 #include "isl/aff.h"
16 #include "isl/map.h"
17 #include "isl/schedule.h"
18 #include "isl/set.h"
19 #include "isl/union_map.h"
20 #include "isl/union_set.h"
21 #include "isl/val.h"
22 
23 using namespace llvm;
24 
25 __isl_give isl_val *polly::isl_valFromAPInt(isl_ctx *Ctx, const APInt Int,
26                                             bool IsSigned) {
27   APInt Abs;
28   isl_val *v;
29 
30   if (IsSigned)
31     Abs = Int.abs();
32   else
33     Abs = Int;
34 
35   const uint64_t *Data = Abs.getRawData();
36   unsigned Words = Abs.getNumWords();
37 
38   v = isl_val_int_from_chunks(Ctx, Words, sizeof(uint64_t), Data);
39 
40   if (IsSigned && Int.isNegative())
41     v = isl_val_neg(v);
42 
43   return v;
44 }
45 
46 APInt polly::APIntFromVal(__isl_take isl_val *Val) {
47   uint64_t *Data;
48   int NumChunks;
49 
50   NumChunks = isl_val_n_abs_num_chunks(Val, sizeof(uint64_t));
51 
52   Data = (uint64_t *)malloc(NumChunks * sizeof(uint64_t));
53   isl_val_get_abs_num_chunks(Val, sizeof(uint64_t), Data);
54   APInt A(8 * sizeof(uint64_t) * NumChunks, NumChunks, Data);
55 
56   if (isl_val_is_neg(Val)) {
57     A = A.zext(A.getBitWidth() + 1);
58     A = -A;
59   }
60 
61   if (A.getMinSignedBits() < A.getBitWidth())
62     A = A.trunc(A.getMinSignedBits());
63 
64   free(Data);
65   isl_val_free(Val);
66   return A;
67 }
68 
69 template <typename ISLTy, typename ISL_CTX_GETTER, typename ISL_PRINTER>
70 static inline std::string stringFromIslObjInternal(__isl_keep ISLTy *isl_obj,
71                                                    ISL_CTX_GETTER ctx_getter_fn,
72                                                    ISL_PRINTER printer_fn) {
73   isl_ctx *ctx = ctx_getter_fn(isl_obj);
74   isl_printer *p = isl_printer_to_str(ctx);
75   printer_fn(p, isl_obj);
76   char *char_str = isl_printer_get_str(p);
77   std::string string(char_str);
78   free(char_str);
79   isl_printer_free(p);
80   return string;
81 }
82 
83 std::string polly::stringFromIslObj(__isl_keep isl_map *map) {
84   return stringFromIslObjInternal(map, isl_map_get_ctx, isl_printer_print_map);
85 }
86 
87 std::string polly::stringFromIslObj(__isl_keep isl_set *set) {
88   return stringFromIslObjInternal(set, isl_set_get_ctx, isl_printer_print_set);
89 }
90 
91 std::string polly::stringFromIslObj(__isl_keep isl_union_map *umap) {
92   return stringFromIslObjInternal(umap, isl_union_map_get_ctx,
93                                   isl_printer_print_union_map);
94 }
95 
96 std::string polly::stringFromIslObj(__isl_keep isl_union_set *uset) {
97   return stringFromIslObjInternal(uset, isl_union_set_get_ctx,
98                                   isl_printer_print_union_set);
99 }
100 
101 std::string polly::stringFromIslObj(__isl_keep isl_schedule *schedule) {
102   return stringFromIslObjInternal(schedule, isl_schedule_get_ctx,
103                                   isl_printer_print_schedule);
104 }
105 
106 std::string polly::stringFromIslObj(__isl_keep isl_multi_aff *maff) {
107   return stringFromIslObjInternal(maff, isl_multi_aff_get_ctx,
108                                   isl_printer_print_multi_aff);
109 }
110 
111 std::string polly::stringFromIslObj(__isl_keep isl_pw_multi_aff *pma) {
112   return stringFromIslObjInternal(pma, isl_pw_multi_aff_get_ctx,
113                                   isl_printer_print_pw_multi_aff);
114 }
115 
116 std::string polly::stringFromIslObj(__isl_keep isl_aff *aff) {
117   return stringFromIslObjInternal(aff, isl_aff_get_ctx, isl_printer_print_aff);
118 }
119 
120 std::string polly::stringFromIslObj(__isl_keep isl_pw_aff *pwaff) {
121   return stringFromIslObjInternal(pwaff, isl_pw_aff_get_ctx,
122                                   isl_printer_print_pw_aff);
123 }
124 
125 static void replace(std::string &str, const std::string &find,
126                     const std::string &replace) {
127   size_t pos = 0;
128   while ((pos = str.find(find, pos)) != std::string::npos) {
129     str.replace(pos, find.length(), replace);
130     pos += replace.length();
131   }
132 }
133 
134 static void makeIslCompatible(std::string &str) {
135   replace(str, ".", "_");
136   replace(str, "\"", "_");
137   replace(str, " ", "__");
138   replace(str, "=>", "TO");
139 }
140 
141 std::string polly::getIslCompatibleName(const std::string &Prefix,
142                                         const std::string &Middle,
143                                         const std::string &Suffix) {
144   std::string S = Prefix + Middle + Suffix;
145   makeIslCompatible(S);
146   return S;
147 }
148 
149 std::string polly::getIslCompatibleName(const std::string &Prefix,
150                                         const Value *Val,
151                                         const std::string &Suffix) {
152   std::string ValStr;
153   raw_string_ostream OS(ValStr);
154   Val->printAsOperand(OS, false);
155   ValStr = OS.str();
156   // Remove the leading %
157   ValStr.erase(0, 1);
158   return getIslCompatibleName(Prefix, ValStr, Suffix);
159 }
160