blob: 11fc197967f87ffabe6f26ca881e4697823c0138 [file] [log] [blame]
Tobias Grosser75805372011-04-29 06:27:02 +00001//===- 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 Zheng454e8f92012-07-05 08:55:31 +000014#include "isl/aff.h"
Tobias Grosser83628182013-05-07 08:11:54 +000015#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 Grosseredab1352013-06-21 06:41:31 +000020#include "isl/val.h"
21
22#include "llvm/Support/raw_ostream.h"
Tobias Grosser75805372011-04-29 06:27:02 +000023
24using namespace llvm;
25
Tobias Grosserde49b8f2013-02-22 08:21:52 +000026void polly::MPZ_from_APInt(mpz_t v, const APInt apint, bool is_signed) {
Tobias Grosser75805372011-04-29 06:27:02 +000027 // 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 Grosserde49b8f2013-02-22 08:21:52 +000032 abs = apint.abs();
Tobias Grosser75805372011-04-29 06:27:02 +000033 else
Tobias Grosserde49b8f2013-02-22 08:21:52 +000034 abs = apint;
Tobias Grosser75805372011-04-29 06:27:02 +000035
36 const uint64_t *rawdata = abs.getRawData();
37 unsigned numWords = abs.getNumWords();
38
Tobias Grosser3e030e12013-06-14 16:23:38 +000039 mpz_import(v, numWords, -1, sizeof(uint64_t), 0, 0, rawdata);
Tobias Grosser75805372011-04-29 06:27:02 +000040
Tobias Grosserde49b8f2013-02-22 08:21:52 +000041 if (is_signed && apint.isNegative())
42 mpz_neg(v, v);
Tobias Grosser75805372011-04-29 06:27:02 +000043}
44
Tobias Grosseredab1352013-06-21 06:41:31 +000045__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 Grosserde49b8f2013-02-22 08:21:52 +000066APInt polly::APInt_from_MPZ(const mpz_t mpz) {
Tobias Grosser75805372011-04-29 06:27:02 +000067 uint64_t *p = NULL;
68 size_t sz;
69
Tobias Grosser3e030e12013-06-14 16:23:38 +000070 p = (uint64_t *)mpz_export(p, &sz, -1, sizeof(uint64_t), 0, 0, mpz);
Tobias Grosser75805372011-04-29 06:27:02 +000071
72 if (p) {
Tobias Grosserde49b8f2013-02-22 08:21:52 +000073 APInt A((unsigned) mpz_sizeinbase(mpz, 2), (unsigned) sz, p);
Tobias Grosser75805372011-04-29 06:27:02 +000074 A = A.zext(A.getBitWidth() + 1);
Tobias Grosserb76f38532011-08-20 11:11:25 +000075 free(p);
Tobias Grosser75805372011-04-29 06:27:02 +000076
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 Grosseredab1352013-06-21 06:41:31 +000087APInt 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 Grosserde49b8f2013-02-22 08:21:52 +0000110template <typename ISLTy, typename ISL_CTX_GETTER, typename ISL_PRINTER>
Tobias Grossere602a072013-05-07 07:30:56 +0000111static inline std::string stringFromIslObjInternal(__isl_keep ISLTy *isl_obj,
112 ISL_CTX_GETTER ctx_getter_fn,
113 ISL_PRINTER printer_fn) {
Hongbin Zheng5205e0c2012-07-05 08:42:39 +0000114 isl_ctx *ctx = ctx_getter_fn(isl_obj);
115 isl_printer *p = isl_printer_to_str(ctx);
116 printer_fn(p, isl_obj);
Tobias Grosser15f5eff2011-08-20 11:11:18 +0000117 char *char_str = isl_printer_get_str(p);
118 std::string string(char_str);
119 free(char_str);
Tobias Grosser75805372011-04-29 06:27:02 +0000120 isl_printer_free(p);
121 return string;
122}
123
Tobias Grosserde49b8f2013-02-22 08:21:52 +0000124static inline isl_ctx *schedule_get_ctx(__isl_keep isl_schedule *schedule) {
Hongbin Zheng5205e0c2012-07-05 08:42:39 +0000125 return isl_union_map_get_ctx(isl_schedule_get_map(schedule));
126}
127
Tobias Grosserde49b8f2013-02-22 08:21:52 +0000128std::string polly::stringFromIslObj(__isl_keep isl_map *map) {
129 return stringFromIslObjInternal(map, isl_map_get_ctx, isl_printer_print_map);
Hongbin Zheng5205e0c2012-07-05 08:42:39 +0000130}
131
Tobias Grosserde49b8f2013-02-22 08:21:52 +0000132std::string polly::stringFromIslObj(__isl_keep isl_set *set) {
133 return stringFromIslObjInternal(set, isl_set_get_ctx, isl_printer_print_set);
Tobias Grosser75805372011-04-29 06:27:02 +0000134}
135
Tobias Grosserde49b8f2013-02-22 08:21:52 +0000136std::string polly::stringFromIslObj(__isl_keep isl_union_map *umap) {
Hongbin Zheng5205e0c2012-07-05 08:42:39 +0000137 return stringFromIslObjInternal(umap, isl_union_map_get_ctx,
138 isl_printer_print_union_map);
Tobias Grosser75805372011-04-29 06:27:02 +0000139}
140
Tobias Grosserde49b8f2013-02-22 08:21:52 +0000141std::string polly::stringFromIslObj(__isl_keep isl_union_set *uset) {
Hongbin Zheng5205e0c2012-07-05 08:42:39 +0000142 return stringFromIslObjInternal(uset, isl_union_set_get_ctx,
143 isl_printer_print_union_set);
Tobias Grosser75805372011-04-29 06:27:02 +0000144}
Tobias Grosserde68cc92011-06-30 20:01:02 +0000145
Tobias Grosserde49b8f2013-02-22 08:21:52 +0000146std::string polly::stringFromIslObj(__isl_keep isl_schedule *schedule) {
Hongbin Zheng5205e0c2012-07-05 08:42:39 +0000147 return stringFromIslObjInternal(schedule, schedule_get_ctx,
148 isl_printer_print_schedule);
Tobias Grosserde68cc92011-06-30 20:01:02 +0000149}
Hongbin Zheng454e8f92012-07-05 08:55:31 +0000150
Tobias Grosserde49b8f2013-02-22 08:21:52 +0000151std::string polly::stringFromIslObj(__isl_keep isl_multi_aff *maff) {
Hongbin Zheng454e8f92012-07-05 08:55:31 +0000152 return stringFromIslObjInternal(maff, isl_multi_aff_get_ctx,
153 isl_printer_print_multi_aff);
154}
155
Tobias Grosserde49b8f2013-02-22 08:21:52 +0000156std::string polly::stringFromIslObj(__isl_keep isl_pw_multi_aff *pma) {
Hongbin Zheng454e8f92012-07-05 08:55:31 +0000157 return stringFromIslObjInternal(pma, isl_pw_multi_aff_get_ctx,
158 isl_printer_print_pw_multi_aff);
159}
160
Tobias Grosserde49b8f2013-02-22 08:21:52 +0000161std::string polly::stringFromIslObj(__isl_keep isl_aff *aff) {
Hongbin Zheng454e8f92012-07-05 08:55:31 +0000162 return stringFromIslObjInternal(aff, isl_aff_get_ctx, isl_printer_print_aff);
163}
164
Tobias Grosserde49b8f2013-02-22 08:21:52 +0000165std::string polly::stringFromIslObj(__isl_keep isl_pw_aff *pwaff) {
Hongbin Zheng454e8f92012-07-05 08:55:31 +0000166 return stringFromIslObjInternal(pwaff, isl_pw_aff_get_ctx,
167 isl_printer_print_pw_aff);
168}