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" |
| 14 | |
| 15 | #include "isl/set.h" |
| 16 | #include "isl/union_set.h" |
| 17 | #include "isl/map.h" |
| 18 | #include "isl/union_map.h" |
Tobias Grosser | de68cc9 | 2011-06-30 20:01:02 +0000 | [diff] [blame] | 19 | #include "isl/schedule.h" |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 20 | |
| 21 | using namespace llvm; |
| 22 | |
| 23 | void polly::MPZ_from_APInt (mpz_t v, const APInt apint, bool is_signed) { |
| 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) |
| 29 | abs = apint.abs(); |
| 30 | else |
| 31 | abs = apint; |
| 32 | |
| 33 | const uint64_t *rawdata = abs.getRawData(); |
| 34 | unsigned numWords = abs.getNumWords(); |
| 35 | |
| 36 | // TODO: Check if this is true for all platforms. |
| 37 | mpz_import(v, numWords, 1, sizeof (uint64_t), 0, 0, rawdata); |
| 38 | |
| 39 | if (is_signed && apint.isNegative()) mpz_neg(v, v); |
| 40 | } |
| 41 | |
| 42 | APInt polly::APInt_from_MPZ (const mpz_t mpz) { |
| 43 | uint64_t *p = NULL; |
| 44 | size_t sz; |
| 45 | |
| 46 | p = (uint64_t*) mpz_export(p, &sz, 1, sizeof(uint64_t), 0, 0, mpz); |
| 47 | |
| 48 | if (p) { |
| 49 | APInt A((unsigned)mpz_sizeinbase(mpz, 2), (unsigned)sz , p); |
| 50 | A = A.zext(A.getBitWidth() + 1); |
| 51 | |
| 52 | if (mpz_sgn(mpz) == -1) |
| 53 | return -A; |
| 54 | else |
| 55 | return A; |
| 56 | } else { |
| 57 | uint64_t val = 0; |
| 58 | return APInt(1, 1, &val); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | std::string polly::stringFromIslObj(/*__isl_keep*/ isl_map *map) { |
| 63 | isl_printer *p = isl_printer_to_str(isl_map_get_ctx(map)); |
| 64 | isl_printer_print_map(p, map); |
Tobias Grosser | 15f5eff | 2011-08-20 11:11:18 +0000 | [diff] [blame^] | 65 | char *char_str = isl_printer_get_str(p); |
| 66 | std::string string(char_str); |
| 67 | free(char_str); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 68 | isl_printer_free(p); |
| 69 | return string; |
| 70 | } |
| 71 | |
| 72 | std::string polly::stringFromIslObj(/*__isl_keep*/ isl_set *set) { |
| 73 | isl_printer *p = isl_printer_to_str(isl_set_get_ctx(set)); |
| 74 | isl_printer_print_set(p, set); |
Tobias Grosser | 15f5eff | 2011-08-20 11:11:18 +0000 | [diff] [blame^] | 75 | char *char_str = isl_printer_get_str(p); |
| 76 | std::string string(char_str); |
| 77 | free(char_str); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 78 | isl_printer_free(p); |
| 79 | return string; |
| 80 | } |
| 81 | |
| 82 | std::string polly::stringFromIslObj(/*__isl_keep*/ isl_union_map *umap) { |
| 83 | isl_printer *p = isl_printer_to_str(isl_union_map_get_ctx(umap)); |
| 84 | isl_printer_print_union_map(p, umap); |
Tobias Grosser | 15f5eff | 2011-08-20 11:11:18 +0000 | [diff] [blame^] | 85 | char *char_str = isl_printer_get_str(p); |
| 86 | std::string string(char_str); |
| 87 | free(char_str); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 88 | isl_printer_free(p); |
| 89 | return string; |
| 90 | } |
| 91 | |
| 92 | std::string polly::stringFromIslObj(/*__isl_keep*/ isl_union_set *uset) { |
| 93 | isl_printer *p = isl_printer_to_str(isl_union_set_get_ctx(uset)); |
| 94 | isl_printer_print_union_set(p, uset); |
Tobias Grosser | 15f5eff | 2011-08-20 11:11:18 +0000 | [diff] [blame^] | 95 | char *char_str = isl_printer_get_str(p); |
| 96 | std::string string(char_str); |
| 97 | free(char_str); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 98 | isl_printer_free(p); |
| 99 | return string; |
| 100 | } |
Tobias Grosser | de68cc9 | 2011-06-30 20:01:02 +0000 | [diff] [blame] | 101 | |
| 102 | std::string polly::stringFromIslObj(/*__isl_keep*/ isl_schedule *schedule) { |
| 103 | isl_ctx *ctx = isl_union_map_get_ctx(isl_schedule_get_map(schedule)); |
| 104 | isl_printer *p = isl_printer_to_str(ctx); |
| 105 | isl_printer_print_schedule(p, schedule); |
Tobias Grosser | 15f5eff | 2011-08-20 11:11:18 +0000 | [diff] [blame^] | 106 | char *char_str = isl_printer_get_str(p); |
| 107 | std::string string(char_str); |
| 108 | free(char_str); |
Tobias Grosser | de68cc9 | 2011-06-30 20:01:02 +0000 | [diff] [blame] | 109 | isl_printer_free(p); |
| 110 | return string; |
| 111 | } |