Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 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" |
Johannes Doerfert | 79fc23f | 2014-07-24 23:48:02 +0000 | [diff] [blame] | 14 | #include "llvm/IR/Value.h" |
Hongbin Zheng | 454e8f9 | 2012-07-05 08:55:31 +0000 | [diff] [blame] | 15 | #include "isl/aff.h" |
Tobias Grosser | 8362818 | 2013-05-07 08:11:54 +0000 | [diff] [blame] | 16 | #include "isl/map.h" |
| 17 | #include "isl/schedule.h" |
| 18 | #include "isl/set.h" |
Hongbin Zheng | a7bdd29 | 2016-02-18 15:24:42 +0000 | [diff] [blame] | 19 | #include "isl/space.h" |
Tobias Grosser | 8362818 | 2013-05-07 08:11:54 +0000 | [diff] [blame] | 20 | #include "isl/union_map.h" |
| 21 | #include "isl/union_set.h" |
Tobias Grosser | edab135 | 2013-06-21 06:41:31 +0000 | [diff] [blame] | 22 | #include "isl/val.h" |
| 23 | |
Tobias Grosser | 76f8279 | 2016-08-26 10:43:28 +0000 | [diff] [blame^] | 24 | #include <climits> |
| 25 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 26 | using namespace llvm; |
| 27 | |
Tobias Grosser | edab135 | 2013-06-21 06:41:31 +0000 | [diff] [blame] | 28 | __isl_give isl_val *polly::isl_valFromAPInt(isl_ctx *Ctx, const APInt Int, |
| 29 | bool IsSigned) { |
| 30 | APInt Abs; |
| 31 | isl_val *v; |
| 32 | |
| 33 | if (IsSigned) |
| 34 | Abs = Int.abs(); |
| 35 | else |
| 36 | Abs = Int; |
| 37 | |
| 38 | const uint64_t *Data = Abs.getRawData(); |
| 39 | unsigned Words = Abs.getNumWords(); |
| 40 | |
| 41 | v = isl_val_int_from_chunks(Ctx, Words, sizeof(uint64_t), Data); |
| 42 | |
| 43 | if (IsSigned && Int.isNegative()) |
| 44 | v = isl_val_neg(v); |
| 45 | |
| 46 | return v; |
| 47 | } |
| 48 | |
Tobias Grosser | edab135 | 2013-06-21 06:41:31 +0000 | [diff] [blame] | 49 | APInt polly::APIntFromVal(__isl_take isl_val *Val) { |
| 50 | uint64_t *Data; |
| 51 | int NumChunks; |
Tobias Grosser | 76f8279 | 2016-08-26 10:43:28 +0000 | [diff] [blame^] | 52 | const static int ChunkSize = sizeof(uint64_t); |
Tobias Grosser | edab135 | 2013-06-21 06:41:31 +0000 | [diff] [blame] | 53 | |
Tobias Grosser | 76f8279 | 2016-08-26 10:43:28 +0000 | [diff] [blame^] | 54 | assert(isl_val_is_int(Val) && "Only integers can be converted to APInt"); |
Tobias Grosser | edab135 | 2013-06-21 06:41:31 +0000 | [diff] [blame] | 55 | |
Tobias Grosser | 76f8279 | 2016-08-26 10:43:28 +0000 | [diff] [blame^] | 56 | NumChunks = isl_val_n_abs_num_chunks(Val, ChunkSize); |
| 57 | Data = (uint64_t *)malloc(NumChunks * ChunkSize); |
| 58 | isl_val_get_abs_num_chunks(Val, ChunkSize, Data); |
| 59 | int NumBits = CHAR_BIT * ChunkSize * NumChunks; |
| 60 | APInt A(NumBits, NumChunks, Data); |
Tobias Grosser | edab135 | 2013-06-21 06:41:31 +0000 | [diff] [blame] | 61 | |
Tobias Grosser | 76f8279 | 2016-08-26 10:43:28 +0000 | [diff] [blame^] | 62 | // As isl provides only an interface to obtain data that describes the |
| 63 | // absolute value of an isl_val, A at this point always contains a positive |
| 64 | // number. In case Val was originally negative, we expand the size of A by |
| 65 | // one and negate the value (in two's complement representation). As a result, |
| 66 | // the new value in A corresponds now with Val. |
Tobias Grosser | edab135 | 2013-06-21 06:41:31 +0000 | [diff] [blame] | 67 | if (isl_val_is_neg(Val)) { |
| 68 | A = A.zext(A.getBitWidth() + 1); |
| 69 | A = -A; |
| 70 | } |
| 71 | |
Tobias Grosser | 76f8279 | 2016-08-26 10:43:28 +0000 | [diff] [blame^] | 72 | // isl may represent small numbers with more than the minimal number of bits. |
| 73 | // We truncate the APInt to the minimal number of bits needed to represent the |
| 74 | // signed value it contains, to ensure that the bitwidth is always minimal. |
Tobias Grosser | edab135 | 2013-06-21 06:41:31 +0000 | [diff] [blame] | 75 | if (A.getMinSignedBits() < A.getBitWidth()) |
| 76 | A = A.trunc(A.getMinSignedBits()); |
| 77 | |
| 78 | free(Data); |
| 79 | isl_val_free(Val); |
| 80 | return A; |
| 81 | } |
| 82 | |
Tobias Grosser | de49b8f | 2013-02-22 08:21:52 +0000 | [diff] [blame] | 83 | template <typename ISLTy, typename ISL_CTX_GETTER, typename ISL_PRINTER> |
Tobias Grosser | e602a07 | 2013-05-07 07:30:56 +0000 | [diff] [blame] | 84 | static inline std::string stringFromIslObjInternal(__isl_keep ISLTy *isl_obj, |
| 85 | ISL_CTX_GETTER ctx_getter_fn, |
| 86 | ISL_PRINTER printer_fn) { |
Michael Kruse | b8d2644 | 2015-12-13 19:35:26 +0000 | [diff] [blame] | 87 | if (!isl_obj) |
| 88 | return "null"; |
Hongbin Zheng | 5205e0c | 2012-07-05 08:42:39 +0000 | [diff] [blame] | 89 | isl_ctx *ctx = ctx_getter_fn(isl_obj); |
| 90 | isl_printer *p = isl_printer_to_str(ctx); |
| 91 | printer_fn(p, isl_obj); |
Tobias Grosser | 15f5eff | 2011-08-20 11:11:18 +0000 | [diff] [blame] | 92 | char *char_str = isl_printer_get_str(p); |
Tobias Grosser | b43cc62 | 2015-11-10 15:09:44 +0000 | [diff] [blame] | 93 | std::string string; |
| 94 | if (char_str) |
| 95 | string = char_str; |
| 96 | else |
| 97 | string = "null"; |
Tobias Grosser | 15f5eff | 2011-08-20 11:11:18 +0000 | [diff] [blame] | 98 | free(char_str); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 99 | isl_printer_free(p); |
| 100 | return string; |
| 101 | } |
| 102 | |
Tobias Grosser | de49b8f | 2013-02-22 08:21:52 +0000 | [diff] [blame] | 103 | std::string polly::stringFromIslObj(__isl_keep isl_map *map) { |
| 104 | return stringFromIslObjInternal(map, isl_map_get_ctx, isl_printer_print_map); |
Hongbin Zheng | 5205e0c | 2012-07-05 08:42:39 +0000 | [diff] [blame] | 105 | } |
| 106 | |
Tobias Grosser | de49b8f | 2013-02-22 08:21:52 +0000 | [diff] [blame] | 107 | std::string polly::stringFromIslObj(__isl_keep isl_set *set) { |
| 108 | return stringFromIslObjInternal(set, isl_set_get_ctx, isl_printer_print_set); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 109 | } |
| 110 | |
Tobias Grosser | de49b8f | 2013-02-22 08:21:52 +0000 | [diff] [blame] | 111 | std::string polly::stringFromIslObj(__isl_keep isl_union_map *umap) { |
Hongbin Zheng | 5205e0c | 2012-07-05 08:42:39 +0000 | [diff] [blame] | 112 | return stringFromIslObjInternal(umap, isl_union_map_get_ctx, |
| 113 | isl_printer_print_union_map); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 114 | } |
| 115 | |
Tobias Grosser | de49b8f | 2013-02-22 08:21:52 +0000 | [diff] [blame] | 116 | std::string polly::stringFromIslObj(__isl_keep isl_union_set *uset) { |
Hongbin Zheng | 5205e0c | 2012-07-05 08:42:39 +0000 | [diff] [blame] | 117 | return stringFromIslObjInternal(uset, isl_union_set_get_ctx, |
| 118 | isl_printer_print_union_set); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 119 | } |
Tobias Grosser | de68cc9 | 2011-06-30 20:01:02 +0000 | [diff] [blame] | 120 | |
Tobias Grosser | de49b8f | 2013-02-22 08:21:52 +0000 | [diff] [blame] | 121 | std::string polly::stringFromIslObj(__isl_keep isl_schedule *schedule) { |
Tobias Grosser | 6a629c5 | 2014-11-21 19:39:42 +0000 | [diff] [blame] | 122 | return stringFromIslObjInternal(schedule, isl_schedule_get_ctx, |
Hongbin Zheng | 5205e0c | 2012-07-05 08:42:39 +0000 | [diff] [blame] | 123 | isl_printer_print_schedule); |
Tobias Grosser | de68cc9 | 2011-06-30 20:01:02 +0000 | [diff] [blame] | 124 | } |
Hongbin Zheng | 454e8f9 | 2012-07-05 08:55:31 +0000 | [diff] [blame] | 125 | |
Tobias Grosser | de49b8f | 2013-02-22 08:21:52 +0000 | [diff] [blame] | 126 | std::string polly::stringFromIslObj(__isl_keep isl_multi_aff *maff) { |
Hongbin Zheng | 454e8f9 | 2012-07-05 08:55:31 +0000 | [diff] [blame] | 127 | return stringFromIslObjInternal(maff, isl_multi_aff_get_ctx, |
| 128 | isl_printer_print_multi_aff); |
| 129 | } |
| 130 | |
Tobias Grosser | de49b8f | 2013-02-22 08:21:52 +0000 | [diff] [blame] | 131 | std::string polly::stringFromIslObj(__isl_keep isl_pw_multi_aff *pma) { |
Hongbin Zheng | 454e8f9 | 2012-07-05 08:55:31 +0000 | [diff] [blame] | 132 | return stringFromIslObjInternal(pma, isl_pw_multi_aff_get_ctx, |
| 133 | isl_printer_print_pw_multi_aff); |
| 134 | } |
| 135 | |
Hongbin Zheng | 2ac7ee7 | 2016-02-20 03:40:19 +0000 | [diff] [blame] | 136 | std::string polly::stringFromIslObj(__isl_keep isl_union_pw_multi_aff *upma) { |
| 137 | return stringFromIslObjInternal(upma, isl_union_pw_multi_aff_get_ctx, |
| 138 | isl_printer_print_union_pw_multi_aff); |
| 139 | } |
| 140 | |
Tobias Grosser | de49b8f | 2013-02-22 08:21:52 +0000 | [diff] [blame] | 141 | std::string polly::stringFromIslObj(__isl_keep isl_aff *aff) { |
Hongbin Zheng | 454e8f9 | 2012-07-05 08:55:31 +0000 | [diff] [blame] | 142 | return stringFromIslObjInternal(aff, isl_aff_get_ctx, isl_printer_print_aff); |
| 143 | } |
| 144 | |
Tobias Grosser | de49b8f | 2013-02-22 08:21:52 +0000 | [diff] [blame] | 145 | std::string polly::stringFromIslObj(__isl_keep isl_pw_aff *pwaff) { |
Hongbin Zheng | 454e8f9 | 2012-07-05 08:55:31 +0000 | [diff] [blame] | 146 | return stringFromIslObjInternal(pwaff, isl_pw_aff_get_ctx, |
| 147 | isl_printer_print_pw_aff); |
| 148 | } |
Johannes Doerfert | 79fc23f | 2014-07-24 23:48:02 +0000 | [diff] [blame] | 149 | |
Hongbin Zheng | a7bdd29 | 2016-02-18 15:24:42 +0000 | [diff] [blame] | 150 | std::string polly::stringFromIslObj(__isl_keep isl_space *space) { |
| 151 | return stringFromIslObjInternal(space, isl_space_get_ctx, |
| 152 | isl_printer_print_space); |
| 153 | } |
| 154 | |
Johannes Doerfert | 79fc23f | 2014-07-24 23:48:02 +0000 | [diff] [blame] | 155 | static void replace(std::string &str, const std::string &find, |
| 156 | const std::string &replace) { |
| 157 | size_t pos = 0; |
| 158 | while ((pos = str.find(find, pos)) != std::string::npos) { |
| 159 | str.replace(pos, find.length(), replace); |
| 160 | pos += replace.length(); |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | static void makeIslCompatible(std::string &str) { |
| 165 | replace(str, ".", "_"); |
| 166 | replace(str, "\"", "_"); |
Tobias Grosser | 16c4403 | 2015-07-09 07:31:45 +0000 | [diff] [blame] | 167 | replace(str, " ", "__"); |
| 168 | replace(str, "=>", "TO"); |
Johannes Doerfert | 79fc23f | 2014-07-24 23:48:02 +0000 | [diff] [blame] | 169 | } |
| 170 | |
Johannes Doerfert | 3a7e812 | 2015-02-19 18:09:39 +0000 | [diff] [blame] | 171 | std::string polly::getIslCompatibleName(const std::string &Prefix, |
| 172 | const std::string &Middle, |
| 173 | const std::string &Suffix) { |
| 174 | std::string S = Prefix + Middle + Suffix; |
| 175 | makeIslCompatible(S); |
| 176 | return S; |
| 177 | } |
| 178 | |
Tobias Grosser | f567e1a | 2015-02-19 22:16:12 +0000 | [diff] [blame] | 179 | std::string polly::getIslCompatibleName(const std::string &Prefix, |
| 180 | const Value *Val, |
Johannes Doerfert | 3a7e812 | 2015-02-19 18:09:39 +0000 | [diff] [blame] | 181 | const std::string &Suffix) { |
Johannes Doerfert | 79fc23f | 2014-07-24 23:48:02 +0000 | [diff] [blame] | 182 | std::string ValStr; |
| 183 | raw_string_ostream OS(ValStr); |
| 184 | Val->printAsOperand(OS, false); |
| 185 | ValStr = OS.str(); |
| 186 | // Remove the leading % |
| 187 | ValStr.erase(0, 1); |
Johannes Doerfert | 3a7e812 | 2015-02-19 18:09:39 +0000 | [diff] [blame] | 188 | return getIslCompatibleName(Prefix, ValStr, Suffix); |
Johannes Doerfert | 79fc23f | 2014-07-24 23:48:02 +0000 | [diff] [blame] | 189 | } |