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