blob: 3959cd014a52912a6b90ca3d586bdbc8fecd630e [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"
14
15#include "isl/set.h"
16#include "isl/union_set.h"
17#include "isl/map.h"
18#include "isl/union_map.h"
Tobias Grosserde68cc92011-06-30 20:01:02 +000019#include "isl/schedule.h"
Hongbin Zheng454e8f92012-07-05 08:55:31 +000020#include "isl/aff.h"
Tobias Grosser75805372011-04-29 06:27:02 +000021
22using namespace llvm;
23
Tobias Grosserde49b8f2013-02-22 08:21:52 +000024void polly::MPZ_from_APInt(mpz_t v, const APInt apint, bool is_signed) {
Tobias Grosser75805372011-04-29 06:27:02 +000025 // 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)
Tobias Grosserde49b8f2013-02-22 08:21:52 +000030 abs = apint.abs();
Tobias Grosser75805372011-04-29 06:27:02 +000031 else
Tobias Grosserde49b8f2013-02-22 08:21:52 +000032 abs = apint;
Tobias Grosser75805372011-04-29 06:27:02 +000033
34 const uint64_t *rawdata = abs.getRawData();
35 unsigned numWords = abs.getNumWords();
36
37 // TODO: Check if this is true for all platforms.
Tobias Grosserde49b8f2013-02-22 08:21:52 +000038 mpz_import(v, numWords, 1, sizeof(uint64_t), 0, 0, rawdata);
Tobias Grosser75805372011-04-29 06:27:02 +000039
Tobias Grosserde49b8f2013-02-22 08:21:52 +000040 if (is_signed && apint.isNegative())
41 mpz_neg(v, v);
Tobias Grosser75805372011-04-29 06:27:02 +000042}
43
Tobias Grosserde49b8f2013-02-22 08:21:52 +000044APInt polly::APInt_from_MPZ(const mpz_t mpz) {
Tobias Grosser75805372011-04-29 06:27:02 +000045 uint64_t *p = NULL;
46 size_t sz;
47
Tobias Grosserde49b8f2013-02-22 08:21:52 +000048 p = (uint64_t *)mpz_export(p, &sz, 1, sizeof(uint64_t), 0, 0, mpz);
Tobias Grosser75805372011-04-29 06:27:02 +000049
50 if (p) {
Tobias Grosserde49b8f2013-02-22 08:21:52 +000051 APInt A((unsigned) mpz_sizeinbase(mpz, 2), (unsigned) sz, p);
Tobias Grosser75805372011-04-29 06:27:02 +000052 A = A.zext(A.getBitWidth() + 1);
Tobias Grosserb76f38532011-08-20 11:11:25 +000053 free(p);
Tobias Grosser75805372011-04-29 06:27:02 +000054
55 if (mpz_sgn(mpz) == -1)
56 return -A;
57 else
58 return A;
59 } else {
60 uint64_t val = 0;
61 return APInt(1, 1, &val);
62 }
63}
64
Tobias Grosserde49b8f2013-02-22 08:21:52 +000065template <typename ISLTy, typename ISL_CTX_GETTER, typename ISL_PRINTER>
66static inline std::string
67stringFromIslObjInternal(__isl_keep ISLTy *isl_obj,
68 ISL_CTX_GETTER ctx_getter_fn, ISL_PRINTER printer_fn) {
Hongbin Zheng5205e0c2012-07-05 08:42:39 +000069 isl_ctx *ctx = ctx_getter_fn(isl_obj);
70 isl_printer *p = isl_printer_to_str(ctx);
71 printer_fn(p, isl_obj);
Tobias Grosser15f5eff2011-08-20 11:11:18 +000072 char *char_str = isl_printer_get_str(p);
73 std::string string(char_str);
74 free(char_str);
Tobias Grosser75805372011-04-29 06:27:02 +000075 isl_printer_free(p);
76 return string;
77}
78
Tobias Grosserde49b8f2013-02-22 08:21:52 +000079static inline isl_ctx *schedule_get_ctx(__isl_keep isl_schedule *schedule) {
Hongbin Zheng5205e0c2012-07-05 08:42:39 +000080 return isl_union_map_get_ctx(isl_schedule_get_map(schedule));
81}
82
Tobias Grosserde49b8f2013-02-22 08:21:52 +000083std::string polly::stringFromIslObj(__isl_keep isl_map *map) {
84 return stringFromIslObjInternal(map, isl_map_get_ctx, isl_printer_print_map);
Hongbin Zheng5205e0c2012-07-05 08:42:39 +000085}
86
Tobias Grosserde49b8f2013-02-22 08:21:52 +000087std::string polly::stringFromIslObj(__isl_keep isl_set *set) {
88 return stringFromIslObjInternal(set, isl_set_get_ctx, isl_printer_print_set);
Tobias Grosser75805372011-04-29 06:27:02 +000089}
90
Tobias Grosserde49b8f2013-02-22 08:21:52 +000091std::string polly::stringFromIslObj(__isl_keep isl_union_map *umap) {
Hongbin Zheng5205e0c2012-07-05 08:42:39 +000092 return stringFromIslObjInternal(umap, isl_union_map_get_ctx,
93 isl_printer_print_union_map);
Tobias Grosser75805372011-04-29 06:27:02 +000094}
95
Tobias Grosserde49b8f2013-02-22 08:21:52 +000096std::string polly::stringFromIslObj(__isl_keep isl_union_set *uset) {
Hongbin Zheng5205e0c2012-07-05 08:42:39 +000097 return stringFromIslObjInternal(uset, isl_union_set_get_ctx,
98 isl_printer_print_union_set);
Tobias Grosser75805372011-04-29 06:27:02 +000099}
Tobias Grosserde68cc92011-06-30 20:01:02 +0000100
Tobias Grosserde49b8f2013-02-22 08:21:52 +0000101std::string polly::stringFromIslObj(__isl_keep isl_schedule *schedule) {
Hongbin Zheng5205e0c2012-07-05 08:42:39 +0000102 return stringFromIslObjInternal(schedule, schedule_get_ctx,
103 isl_printer_print_schedule);
Tobias Grosserde68cc92011-06-30 20:01:02 +0000104}
Hongbin Zheng454e8f92012-07-05 08:55:31 +0000105
Tobias Grosserde49b8f2013-02-22 08:21:52 +0000106std::string polly::stringFromIslObj(__isl_keep isl_multi_aff *maff) {
Hongbin Zheng454e8f92012-07-05 08:55:31 +0000107 return stringFromIslObjInternal(maff, isl_multi_aff_get_ctx,
108 isl_printer_print_multi_aff);
109}
110
Tobias Grosserde49b8f2013-02-22 08:21:52 +0000111std::string polly::stringFromIslObj(__isl_keep isl_pw_multi_aff *pma) {
Hongbin Zheng454e8f92012-07-05 08:55:31 +0000112 return stringFromIslObjInternal(pma, isl_pw_multi_aff_get_ctx,
113 isl_printer_print_pw_multi_aff);
114}
115
Tobias Grosserde49b8f2013-02-22 08:21:52 +0000116std::string polly::stringFromIslObj(__isl_keep isl_aff *aff) {
Hongbin Zheng454e8f92012-07-05 08:55:31 +0000117 return stringFromIslObjInternal(aff, isl_aff_get_ctx, isl_printer_print_aff);
118}
119
Tobias Grosserde49b8f2013-02-22 08:21:52 +0000120std::string polly::stringFromIslObj(__isl_keep isl_pw_aff *pwaff) {
Hongbin Zheng454e8f92012-07-05 08:55:31 +0000121 return stringFromIslObjInternal(pwaff, isl_pw_aff_get_ctx,
122 isl_printer_print_pw_aff);
123}
124