blob: 28883187269030590beb8cf5ecc3e7a5e7f77f9b [file] [log] [blame]
Tobias Grosser75805372011-04-29 06:27:02 +00001//===- 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"
Johannes Doerfert79fc23f2014-07-24 23:48:02 +000014#include "llvm/IR/Value.h"
Hongbin Zheng454e8f92012-07-05 08:55:31 +000015#include "isl/aff.h"
Tobias Grosser83628182013-05-07 08:11:54 +000016#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"
Tobias Grosseredab1352013-06-21 06:41:31 +000021#include "isl/val.h"
22
Tobias Grosser75805372011-04-29 06:27:02 +000023using namespace llvm;
24
Tobias Grosseredab1352013-06-21 06:41:31 +000025__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
Tobias Grosseredab1352013-06-21 06:41:31 +000046APInt 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
Tobias Grosser58032cb2013-06-23 01:29:29 +000052 Data = (uint64_t *)malloc(NumChunks * sizeof(uint64_t));
Tobias Grosseredab1352013-06-21 06:41:31 +000053 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
Tobias Grosserde49b8f2013-02-22 08:21:52 +000069template <typename ISLTy, typename ISL_CTX_GETTER, typename ISL_PRINTER>
Tobias Grossere602a072013-05-07 07:30:56 +000070static inline std::string stringFromIslObjInternal(__isl_keep ISLTy *isl_obj,
71 ISL_CTX_GETTER ctx_getter_fn,
72 ISL_PRINTER printer_fn) {
Hongbin Zheng5205e0c2012-07-05 08:42:39 +000073 isl_ctx *ctx = ctx_getter_fn(isl_obj);
74 isl_printer *p = isl_printer_to_str(ctx);
75 printer_fn(p, isl_obj);
Tobias Grosser15f5eff2011-08-20 11:11:18 +000076 char *char_str = isl_printer_get_str(p);
Tobias Grosserb43cc622015-11-10 15:09:44 +000077 std::string string;
78 if (char_str)
79 string = char_str;
80 else
81 string = "null";
Tobias Grosser15f5eff2011-08-20 11:11:18 +000082 free(char_str);
Tobias Grosser75805372011-04-29 06:27:02 +000083 isl_printer_free(p);
84 return string;
85}
86
Tobias Grosserde49b8f2013-02-22 08:21:52 +000087std::string polly::stringFromIslObj(__isl_keep isl_map *map) {
88 return stringFromIslObjInternal(map, isl_map_get_ctx, isl_printer_print_map);
Hongbin Zheng5205e0c2012-07-05 08:42:39 +000089}
90
Tobias Grosserde49b8f2013-02-22 08:21:52 +000091std::string polly::stringFromIslObj(__isl_keep isl_set *set) {
92 return stringFromIslObjInternal(set, isl_set_get_ctx, isl_printer_print_set);
Tobias Grosser75805372011-04-29 06:27:02 +000093}
94
Tobias Grosserde49b8f2013-02-22 08:21:52 +000095std::string polly::stringFromIslObj(__isl_keep isl_union_map *umap) {
Hongbin Zheng5205e0c2012-07-05 08:42:39 +000096 return stringFromIslObjInternal(umap, isl_union_map_get_ctx,
97 isl_printer_print_union_map);
Tobias Grosser75805372011-04-29 06:27:02 +000098}
99
Tobias Grosserde49b8f2013-02-22 08:21:52 +0000100std::string polly::stringFromIslObj(__isl_keep isl_union_set *uset) {
Hongbin Zheng5205e0c2012-07-05 08:42:39 +0000101 return stringFromIslObjInternal(uset, isl_union_set_get_ctx,
102 isl_printer_print_union_set);
Tobias Grosser75805372011-04-29 06:27:02 +0000103}
Tobias Grosserde68cc92011-06-30 20:01:02 +0000104
Tobias Grosserde49b8f2013-02-22 08:21:52 +0000105std::string polly::stringFromIslObj(__isl_keep isl_schedule *schedule) {
Tobias Grosser6a629c52014-11-21 19:39:42 +0000106 return stringFromIslObjInternal(schedule, isl_schedule_get_ctx,
Hongbin Zheng5205e0c2012-07-05 08:42:39 +0000107 isl_printer_print_schedule);
Tobias Grosserde68cc92011-06-30 20:01:02 +0000108}
Hongbin Zheng454e8f92012-07-05 08:55:31 +0000109
Tobias Grosserde49b8f2013-02-22 08:21:52 +0000110std::string polly::stringFromIslObj(__isl_keep isl_multi_aff *maff) {
Hongbin Zheng454e8f92012-07-05 08:55:31 +0000111 return stringFromIslObjInternal(maff, isl_multi_aff_get_ctx,
112 isl_printer_print_multi_aff);
113}
114
Tobias Grosserde49b8f2013-02-22 08:21:52 +0000115std::string polly::stringFromIslObj(__isl_keep isl_pw_multi_aff *pma) {
Hongbin Zheng454e8f92012-07-05 08:55:31 +0000116 return stringFromIslObjInternal(pma, isl_pw_multi_aff_get_ctx,
117 isl_printer_print_pw_multi_aff);
118}
119
Tobias Grosserde49b8f2013-02-22 08:21:52 +0000120std::string polly::stringFromIslObj(__isl_keep isl_aff *aff) {
Hongbin Zheng454e8f92012-07-05 08:55:31 +0000121 return stringFromIslObjInternal(aff, isl_aff_get_ctx, isl_printer_print_aff);
122}
123
Tobias Grosserde49b8f2013-02-22 08:21:52 +0000124std::string polly::stringFromIslObj(__isl_keep isl_pw_aff *pwaff) {
Hongbin Zheng454e8f92012-07-05 08:55:31 +0000125 return stringFromIslObjInternal(pwaff, isl_pw_aff_get_ctx,
126 isl_printer_print_pw_aff);
127}
Johannes Doerfert79fc23f2014-07-24 23:48:02 +0000128
129static void replace(std::string &str, const std::string &find,
130 const std::string &replace) {
131 size_t pos = 0;
132 while ((pos = str.find(find, pos)) != std::string::npos) {
133 str.replace(pos, find.length(), replace);
134 pos += replace.length();
135 }
136}
137
138static void makeIslCompatible(std::string &str) {
139 replace(str, ".", "_");
140 replace(str, "\"", "_");
Tobias Grosser16c44032015-07-09 07:31:45 +0000141 replace(str, " ", "__");
142 replace(str, "=>", "TO");
Johannes Doerfert79fc23f2014-07-24 23:48:02 +0000143}
144
Johannes Doerfert3a7e8122015-02-19 18:09:39 +0000145std::string polly::getIslCompatibleName(const std::string &Prefix,
146 const std::string &Middle,
147 const std::string &Suffix) {
148 std::string S = Prefix + Middle + Suffix;
149 makeIslCompatible(S);
150 return S;
151}
152
Tobias Grosserf567e1a2015-02-19 22:16:12 +0000153std::string polly::getIslCompatibleName(const std::string &Prefix,
154 const Value *Val,
Johannes Doerfert3a7e8122015-02-19 18:09:39 +0000155 const std::string &Suffix) {
Johannes Doerfert79fc23f2014-07-24 23:48:02 +0000156 std::string ValStr;
157 raw_string_ostream OS(ValStr);
158 Val->printAsOperand(OS, false);
159 ValStr = OS.str();
160 // Remove the leading %
161 ValStr.erase(0, 1);
Johannes Doerfert3a7e8122015-02-19 18:09:39 +0000162 return getIslCompatibleName(Prefix, ValStr, Suffix);
Johannes Doerfert79fc23f2014-07-24 23:48:02 +0000163}