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" |
Hongbin Zheng | 454e8f9 | 2012-07-05 08:55:31 +0000 | [diff] [blame] | 14 | #include "isl/aff.h" |
Tobias Grosser | 8362818 | 2013-05-07 08:11:54 +0000 | [diff] [blame] | 15 | #include "isl/map.h" |
| 16 | #include "isl/schedule.h" |
| 17 | #include "isl/set.h" |
| 18 | #include "isl/union_map.h" |
| 19 | #include "isl/union_set.h" |
Tobias Grosser | edab135 | 2013-06-21 06:41:31 +0000 | [diff] [blame] | 20 | #include "isl/val.h" |
| 21 | |
| 22 | #include "llvm/Support/raw_ostream.h" |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 23 | |
| 24 | using namespace llvm; |
| 25 | |
Tobias Grosser | de49b8f | 2013-02-22 08:21:52 +0000 | [diff] [blame] | 26 | void polly::MPZ_from_APInt(mpz_t v, const APInt apint, bool is_signed) { |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 27 | // There is no sign taken from the data, rop will simply be a positive |
| 28 | // integer. An application can handle any sign itself, and apply it for |
| 29 | // instance with mpz_neg. |
| 30 | APInt abs; |
| 31 | if (is_signed) |
Tobias Grosser | de49b8f | 2013-02-22 08:21:52 +0000 | [diff] [blame] | 32 | abs = apint.abs(); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 33 | else |
Tobias Grosser | de49b8f | 2013-02-22 08:21:52 +0000 | [diff] [blame] | 34 | abs = apint; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 35 | |
| 36 | const uint64_t *rawdata = abs.getRawData(); |
| 37 | unsigned numWords = abs.getNumWords(); |
| 38 | |
Tobias Grosser | 3e030e1 | 2013-06-14 16:23:38 +0000 | [diff] [blame] | 39 | mpz_import(v, numWords, -1, sizeof(uint64_t), 0, 0, rawdata); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 40 | |
Tobias Grosser | de49b8f | 2013-02-22 08:21:52 +0000 | [diff] [blame] | 41 | if (is_signed && apint.isNegative()) |
| 42 | mpz_neg(v, v); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 43 | } |
| 44 | |
Tobias Grosser | edab135 | 2013-06-21 06:41:31 +0000 | [diff] [blame] | 45 | __isl_give isl_val *polly::isl_valFromAPInt(isl_ctx *Ctx, const APInt Int, |
| 46 | bool IsSigned) { |
| 47 | APInt Abs; |
| 48 | isl_val *v; |
| 49 | |
| 50 | if (IsSigned) |
| 51 | Abs = Int.abs(); |
| 52 | else |
| 53 | Abs = Int; |
| 54 | |
| 55 | const uint64_t *Data = Abs.getRawData(); |
| 56 | unsigned Words = Abs.getNumWords(); |
| 57 | |
| 58 | v = isl_val_int_from_chunks(Ctx, Words, sizeof(uint64_t), Data); |
| 59 | |
| 60 | if (IsSigned && Int.isNegative()) |
| 61 | v = isl_val_neg(v); |
| 62 | |
| 63 | return v; |
| 64 | } |
| 65 | |
Tobias Grosser | de49b8f | 2013-02-22 08:21:52 +0000 | [diff] [blame] | 66 | APInt polly::APInt_from_MPZ(const mpz_t mpz) { |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 67 | uint64_t *p = NULL; |
| 68 | size_t sz; |
| 69 | |
Tobias Grosser | 3e030e1 | 2013-06-14 16:23:38 +0000 | [diff] [blame] | 70 | p = (uint64_t *)mpz_export(p, &sz, -1, sizeof(uint64_t), 0, 0, mpz); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 71 | |
| 72 | if (p) { |
Tobias Grosser | de49b8f | 2013-02-22 08:21:52 +0000 | [diff] [blame] | 73 | APInt A((unsigned) mpz_sizeinbase(mpz, 2), (unsigned) sz, p); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 74 | A = A.zext(A.getBitWidth() + 1); |
Tobias Grosser | b76f3853 | 2011-08-20 11:11:25 +0000 | [diff] [blame] | 75 | free(p); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 76 | |
| 77 | if (mpz_sgn(mpz) == -1) |
| 78 | return -A; |
| 79 | else |
| 80 | return A; |
| 81 | } else { |
| 82 | uint64_t val = 0; |
| 83 | return APInt(1, 1, &val); |
| 84 | } |
| 85 | } |
| 86 | |
Tobias Grosser | edab135 | 2013-06-21 06:41:31 +0000 | [diff] [blame] | 87 | APInt polly::APIntFromVal(__isl_take isl_val *Val) { |
| 88 | uint64_t *Data; |
| 89 | int NumChunks; |
| 90 | |
| 91 | NumChunks = isl_val_n_abs_num_chunks(Val, sizeof(uint64_t)); |
| 92 | |
| 93 | Data = (uint64_t*) malloc(NumChunks * sizeof(uint64_t)); |
| 94 | isl_val_get_abs_num_chunks(Val, sizeof(uint64_t), Data); |
| 95 | APInt A(8 * sizeof(uint64_t) * NumChunks, NumChunks, Data); |
| 96 | |
| 97 | if (isl_val_is_neg(Val)) { |
| 98 | A = A.zext(A.getBitWidth() + 1); |
| 99 | A = -A; |
| 100 | } |
| 101 | |
| 102 | if (A.getMinSignedBits() < A.getBitWidth()) |
| 103 | A = A.trunc(A.getMinSignedBits()); |
| 104 | |
| 105 | free(Data); |
| 106 | isl_val_free(Val); |
| 107 | return A; |
| 108 | } |
| 109 | |
Tobias Grosser | de49b8f | 2013-02-22 08:21:52 +0000 | [diff] [blame] | 110 | template <typename ISLTy, typename ISL_CTX_GETTER, typename ISL_PRINTER> |
Tobias Grosser | e602a07 | 2013-05-07 07:30:56 +0000 | [diff] [blame] | 111 | static inline std::string stringFromIslObjInternal(__isl_keep ISLTy *isl_obj, |
| 112 | ISL_CTX_GETTER ctx_getter_fn, |
| 113 | ISL_PRINTER printer_fn) { |
Hongbin Zheng | 5205e0c | 2012-07-05 08:42:39 +0000 | [diff] [blame] | 114 | isl_ctx *ctx = ctx_getter_fn(isl_obj); |
| 115 | isl_printer *p = isl_printer_to_str(ctx); |
| 116 | printer_fn(p, isl_obj); |
Tobias Grosser | 15f5eff | 2011-08-20 11:11:18 +0000 | [diff] [blame] | 117 | char *char_str = isl_printer_get_str(p); |
| 118 | std::string string(char_str); |
| 119 | free(char_str); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 120 | isl_printer_free(p); |
| 121 | return string; |
| 122 | } |
| 123 | |
Tobias Grosser | de49b8f | 2013-02-22 08:21:52 +0000 | [diff] [blame] | 124 | static inline isl_ctx *schedule_get_ctx(__isl_keep isl_schedule *schedule) { |
Hongbin Zheng | 5205e0c | 2012-07-05 08:42:39 +0000 | [diff] [blame] | 125 | return isl_union_map_get_ctx(isl_schedule_get_map(schedule)); |
| 126 | } |
| 127 | |
Tobias Grosser | de49b8f | 2013-02-22 08:21:52 +0000 | [diff] [blame] | 128 | std::string polly::stringFromIslObj(__isl_keep isl_map *map) { |
| 129 | return stringFromIslObjInternal(map, isl_map_get_ctx, isl_printer_print_map); |
Hongbin Zheng | 5205e0c | 2012-07-05 08:42:39 +0000 | [diff] [blame] | 130 | } |
| 131 | |
Tobias Grosser | de49b8f | 2013-02-22 08:21:52 +0000 | [diff] [blame] | 132 | std::string polly::stringFromIslObj(__isl_keep isl_set *set) { |
| 133 | return stringFromIslObjInternal(set, isl_set_get_ctx, isl_printer_print_set); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 134 | } |
| 135 | |
Tobias Grosser | de49b8f | 2013-02-22 08:21:52 +0000 | [diff] [blame] | 136 | std::string polly::stringFromIslObj(__isl_keep isl_union_map *umap) { |
Hongbin Zheng | 5205e0c | 2012-07-05 08:42:39 +0000 | [diff] [blame] | 137 | return stringFromIslObjInternal(umap, isl_union_map_get_ctx, |
| 138 | isl_printer_print_union_map); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 139 | } |
| 140 | |
Tobias Grosser | de49b8f | 2013-02-22 08:21:52 +0000 | [diff] [blame] | 141 | std::string polly::stringFromIslObj(__isl_keep isl_union_set *uset) { |
Hongbin Zheng | 5205e0c | 2012-07-05 08:42:39 +0000 | [diff] [blame] | 142 | return stringFromIslObjInternal(uset, isl_union_set_get_ctx, |
| 143 | isl_printer_print_union_set); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 144 | } |
Tobias Grosser | de68cc9 | 2011-06-30 20:01:02 +0000 | [diff] [blame] | 145 | |
Tobias Grosser | de49b8f | 2013-02-22 08:21:52 +0000 | [diff] [blame] | 146 | std::string polly::stringFromIslObj(__isl_keep isl_schedule *schedule) { |
Hongbin Zheng | 5205e0c | 2012-07-05 08:42:39 +0000 | [diff] [blame] | 147 | return stringFromIslObjInternal(schedule, schedule_get_ctx, |
| 148 | isl_printer_print_schedule); |
Tobias Grosser | de68cc9 | 2011-06-30 20:01:02 +0000 | [diff] [blame] | 149 | } |
Hongbin Zheng | 454e8f9 | 2012-07-05 08:55:31 +0000 | [diff] [blame] | 150 | |
Tobias Grosser | de49b8f | 2013-02-22 08:21:52 +0000 | [diff] [blame] | 151 | std::string polly::stringFromIslObj(__isl_keep isl_multi_aff *maff) { |
Hongbin Zheng | 454e8f9 | 2012-07-05 08:55:31 +0000 | [diff] [blame] | 152 | return stringFromIslObjInternal(maff, isl_multi_aff_get_ctx, |
| 153 | isl_printer_print_multi_aff); |
| 154 | } |
| 155 | |
Tobias Grosser | de49b8f | 2013-02-22 08:21:52 +0000 | [diff] [blame] | 156 | std::string polly::stringFromIslObj(__isl_keep isl_pw_multi_aff *pma) { |
Hongbin Zheng | 454e8f9 | 2012-07-05 08:55:31 +0000 | [diff] [blame] | 157 | return stringFromIslObjInternal(pma, isl_pw_multi_aff_get_ctx, |
| 158 | isl_printer_print_pw_multi_aff); |
| 159 | } |
| 160 | |
Tobias Grosser | de49b8f | 2013-02-22 08:21:52 +0000 | [diff] [blame] | 161 | std::string polly::stringFromIslObj(__isl_keep isl_aff *aff) { |
Hongbin Zheng | 454e8f9 | 2012-07-05 08:55:31 +0000 | [diff] [blame] | 162 | return stringFromIslObjInternal(aff, isl_aff_get_ctx, isl_printer_print_aff); |
| 163 | } |
| 164 | |
Tobias Grosser | de49b8f | 2013-02-22 08:21:52 +0000 | [diff] [blame] | 165 | std::string polly::stringFromIslObj(__isl_keep isl_pw_aff *pwaff) { |
Hongbin Zheng | 454e8f9 | 2012-07-05 08:55:31 +0000 | [diff] [blame] | 166 | return stringFromIslObjInternal(pwaff, isl_pw_aff_get_ctx, |
| 167 | isl_printer_print_pw_aff); |
| 168 | } |