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 | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 20 | |
| 21 | using namespace llvm; |
| 22 | |
Tobias Grosser | de49b8f | 2013-02-22 08:21:52 +0000 | [diff] [blame] | 23 | 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] | 24 | // There is no sign taken from the data, rop will simply be a positive |
| 25 | // integer. An application can handle any sign itself, and apply it for |
| 26 | // instance with mpz_neg. |
| 27 | APInt abs; |
| 28 | if (is_signed) |
Tobias Grosser | de49b8f | 2013-02-22 08:21:52 +0000 | [diff] [blame] | 29 | abs = apint.abs(); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 30 | else |
Tobias Grosser | de49b8f | 2013-02-22 08:21:52 +0000 | [diff] [blame] | 31 | abs = apint; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 32 | |
| 33 | const uint64_t *rawdata = abs.getRawData(); |
| 34 | unsigned numWords = abs.getNumWords(); |
| 35 | |
| 36 | // TODO: Check if this is true for all platforms. |
Tobias Grosser | de49b8f | 2013-02-22 08:21:52 +0000 | [diff] [blame] | 37 | mpz_import(v, numWords, 1, sizeof(uint64_t), 0, 0, rawdata); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 38 | |
Tobias Grosser | de49b8f | 2013-02-22 08:21:52 +0000 | [diff] [blame] | 39 | if (is_signed && apint.isNegative()) |
| 40 | mpz_neg(v, v); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 41 | } |
| 42 | |
Tobias Grosser | de49b8f | 2013-02-22 08:21:52 +0000 | [diff] [blame] | 43 | APInt polly::APInt_from_MPZ(const mpz_t mpz) { |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 44 | uint64_t *p = NULL; |
| 45 | size_t sz; |
| 46 | |
Tobias Grosser | de49b8f | 2013-02-22 08:21:52 +0000 | [diff] [blame] | 47 | 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] | 48 | |
| 49 | if (p) { |
Tobias Grosser | de49b8f | 2013-02-22 08:21:52 +0000 | [diff] [blame] | 50 | APInt A((unsigned) mpz_sizeinbase(mpz, 2), (unsigned) sz, p); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 51 | A = A.zext(A.getBitWidth() + 1); |
Tobias Grosser | b76f3853 | 2011-08-20 11:11:25 +0000 | [diff] [blame] | 52 | free(p); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 53 | |
| 54 | if (mpz_sgn(mpz) == -1) |
| 55 | return -A; |
| 56 | else |
| 57 | return A; |
| 58 | } else { |
| 59 | uint64_t val = 0; |
| 60 | return APInt(1, 1, &val); |
| 61 | } |
| 62 | } |
| 63 | |
Tobias Grosser | de49b8f | 2013-02-22 08:21:52 +0000 | [diff] [blame] | 64 | template <typename ISLTy, typename ISL_CTX_GETTER, typename ISL_PRINTER> |
Tobias Grosser | e602a07 | 2013-05-07 07:30:56 +0000 | [diff] [blame] | 65 | static inline std::string stringFromIslObjInternal(__isl_keep ISLTy *isl_obj, |
| 66 | ISL_CTX_GETTER ctx_getter_fn, |
| 67 | ISL_PRINTER printer_fn) { |
Hongbin Zheng | 5205e0c | 2012-07-05 08:42:39 +0000 | [diff] [blame] | 68 | isl_ctx *ctx = ctx_getter_fn(isl_obj); |
| 69 | isl_printer *p = isl_printer_to_str(ctx); |
| 70 | printer_fn(p, isl_obj); |
Tobias Grosser | 15f5eff | 2011-08-20 11:11:18 +0000 | [diff] [blame] | 71 | char *char_str = isl_printer_get_str(p); |
| 72 | std::string string(char_str); |
| 73 | free(char_str); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 74 | isl_printer_free(p); |
| 75 | return string; |
| 76 | } |
| 77 | |
Tobias Grosser | de49b8f | 2013-02-22 08:21:52 +0000 | [diff] [blame] | 78 | static inline isl_ctx *schedule_get_ctx(__isl_keep isl_schedule *schedule) { |
Hongbin Zheng | 5205e0c | 2012-07-05 08:42:39 +0000 | [diff] [blame] | 79 | return isl_union_map_get_ctx(isl_schedule_get_map(schedule)); |
| 80 | } |
| 81 | |
Tobias Grosser | de49b8f | 2013-02-22 08:21:52 +0000 | [diff] [blame] | 82 | std::string polly::stringFromIslObj(__isl_keep isl_map *map) { |
| 83 | return stringFromIslObjInternal(map, isl_map_get_ctx, isl_printer_print_map); |
Hongbin Zheng | 5205e0c | 2012-07-05 08:42:39 +0000 | [diff] [blame] | 84 | } |
| 85 | |
Tobias Grosser | de49b8f | 2013-02-22 08:21:52 +0000 | [diff] [blame] | 86 | std::string polly::stringFromIslObj(__isl_keep isl_set *set) { |
| 87 | return stringFromIslObjInternal(set, isl_set_get_ctx, isl_printer_print_set); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 88 | } |
| 89 | |
Tobias Grosser | de49b8f | 2013-02-22 08:21:52 +0000 | [diff] [blame] | 90 | std::string polly::stringFromIslObj(__isl_keep isl_union_map *umap) { |
Hongbin Zheng | 5205e0c | 2012-07-05 08:42:39 +0000 | [diff] [blame] | 91 | return stringFromIslObjInternal(umap, isl_union_map_get_ctx, |
| 92 | isl_printer_print_union_map); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 93 | } |
| 94 | |
Tobias Grosser | de49b8f | 2013-02-22 08:21:52 +0000 | [diff] [blame] | 95 | std::string polly::stringFromIslObj(__isl_keep isl_union_set *uset) { |
Hongbin Zheng | 5205e0c | 2012-07-05 08:42:39 +0000 | [diff] [blame] | 96 | return stringFromIslObjInternal(uset, isl_union_set_get_ctx, |
| 97 | isl_printer_print_union_set); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 98 | } |
Tobias Grosser | de68cc9 | 2011-06-30 20:01:02 +0000 | [diff] [blame] | 99 | |
Tobias Grosser | de49b8f | 2013-02-22 08:21:52 +0000 | [diff] [blame] | 100 | std::string polly::stringFromIslObj(__isl_keep isl_schedule *schedule) { |
Hongbin Zheng | 5205e0c | 2012-07-05 08:42:39 +0000 | [diff] [blame] | 101 | return stringFromIslObjInternal(schedule, schedule_get_ctx, |
| 102 | isl_printer_print_schedule); |
Tobias Grosser | de68cc9 | 2011-06-30 20:01:02 +0000 | [diff] [blame] | 103 | } |
Hongbin Zheng | 454e8f9 | 2012-07-05 08:55:31 +0000 | [diff] [blame] | 104 | |
Tobias Grosser | de49b8f | 2013-02-22 08:21:52 +0000 | [diff] [blame] | 105 | std::string polly::stringFromIslObj(__isl_keep isl_multi_aff *maff) { |
Hongbin Zheng | 454e8f9 | 2012-07-05 08:55:31 +0000 | [diff] [blame] | 106 | return stringFromIslObjInternal(maff, isl_multi_aff_get_ctx, |
| 107 | isl_printer_print_multi_aff); |
| 108 | } |
| 109 | |
Tobias Grosser | de49b8f | 2013-02-22 08:21:52 +0000 | [diff] [blame] | 110 | std::string polly::stringFromIslObj(__isl_keep isl_pw_multi_aff *pma) { |
Hongbin Zheng | 454e8f9 | 2012-07-05 08:55:31 +0000 | [diff] [blame] | 111 | return stringFromIslObjInternal(pma, isl_pw_multi_aff_get_ctx, |
| 112 | isl_printer_print_pw_multi_aff); |
| 113 | } |
| 114 | |
Tobias Grosser | de49b8f | 2013-02-22 08:21:52 +0000 | [diff] [blame] | 115 | std::string polly::stringFromIslObj(__isl_keep isl_aff *aff) { |
Hongbin Zheng | 454e8f9 | 2012-07-05 08:55:31 +0000 | [diff] [blame] | 116 | return stringFromIslObjInternal(aff, isl_aff_get_ctx, isl_printer_print_aff); |
| 117 | } |
| 118 | |
Tobias Grosser | de49b8f | 2013-02-22 08:21:52 +0000 | [diff] [blame] | 119 | std::string polly::stringFromIslObj(__isl_keep isl_pw_aff *pwaff) { |
Hongbin Zheng | 454e8f9 | 2012-07-05 08:55:31 +0000 | [diff] [blame] | 120 | return stringFromIslObjInternal(pwaff, isl_pw_aff_get_ctx, |
| 121 | isl_printer_print_pw_aff); |
| 122 | } |