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"
19 #include "isl/schedule.h"
20 #include "isl/aff.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   // TODO: Check if this is true for all platforms.
38   mpz_import(v, numWords, 1, sizeof (uint64_t), 0, 0, rawdata);
39 
40   if (is_signed && apint.isNegative()) mpz_neg(v, v);
41 }
42 
43 APInt polly::APInt_from_MPZ (const mpz_t mpz) {
44   uint64_t *p = NULL;
45   size_t sz;
46 
47   p = (uint64_t*) mpz_export(p, &sz, 1, sizeof(uint64_t), 0, 0, mpz);
48 
49   if (p) {
50     APInt A((unsigned)mpz_sizeinbase(mpz, 2), (unsigned)sz , p);
51     A = A.zext(A.getBitWidth() + 1);
52     free(p);
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 
64 template<typename ISLTy, typename ISL_CTX_GETTER, typename ISL_PRINTER>
65 static inline std::string stringFromIslObjInternal(/*__isl_keep*/
66   ISLTy *isl_obj, ISL_CTX_GETTER ctx_getter_fn, ISL_PRINTER printer_fn) {
67   isl_ctx *ctx = ctx_getter_fn(isl_obj);
68   isl_printer *p = isl_printer_to_str(ctx);
69   printer_fn(p, isl_obj);
70   char *char_str = isl_printer_get_str(p);
71   std::string string(char_str);
72   free(char_str);
73   isl_printer_free(p);
74   return string;
75 }
76 
77 static inline isl_ctx *schedule_get_ctx(/*__isl_keep*/ isl_schedule *schedule) {
78   return isl_union_map_get_ctx(isl_schedule_get_map(schedule));
79 }
80 
81 std::string polly::stringFromIslObj(/*__isl_keep*/ isl_map *map) {
82   return stringFromIslObjInternal(map, isl_map_get_ctx,
83                                   isl_printer_print_map);
84 }
85 
86 std::string polly::stringFromIslObj(/*__isl_keep*/ isl_set *set) {
87   return stringFromIslObjInternal(set, isl_set_get_ctx,
88                                   isl_printer_print_set);
89 }
90 
91 std::string polly::stringFromIslObj(/*__isl_keep*/ isl_union_map *umap) {
92   return stringFromIslObjInternal(umap, isl_union_map_get_ctx,
93                                   isl_printer_print_union_map);
94 }
95 
96 std::string polly::stringFromIslObj(/*__isl_keep*/ isl_union_set *uset) {
97   return stringFromIslObjInternal(uset, isl_union_set_get_ctx,
98                                   isl_printer_print_union_set);
99 }
100 
101 std::string polly::stringFromIslObj(/*__isl_keep*/ isl_schedule *schedule) {
102   return stringFromIslObjInternal(schedule, schedule_get_ctx,
103                                   isl_printer_print_schedule);
104 }
105 
106 std::string polly::stringFromIslObj(/*__isl_keep*/ isl_multi_aff *maff) {
107   return stringFromIslObjInternal(maff, isl_multi_aff_get_ctx,
108                                   isl_printer_print_multi_aff);
109 }
110 
111 std::string polly::stringFromIslObj(/*__isl_keep*/ isl_pw_multi_aff *pma) {
112   return stringFromIslObjInternal(pma, isl_pw_multi_aff_get_ctx,
113                                   isl_printer_print_pw_multi_aff);
114 }
115 
116 std::string polly::stringFromIslObj(/*__isl_keep*/ isl_aff *aff) {
117   return stringFromIslObjInternal(aff, isl_aff_get_ctx, isl_printer_print_aff);
118 }
119 
120 std::string polly::stringFromIslObj(/*__isl_keep*/ isl_pw_aff *pwaff) {
121   return stringFromIslObjInternal(pwaff, isl_pw_aff_get_ctx,
122                                   isl_printer_print_pw_aff);
123 }
124 
125