xref: /llvm-project/polly/lib/Support/GICHelper.cpp (revision 758053788bde4747953f5f276ded345cd01323b1)
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 
20 using namespace llvm;
21 
22 void polly::MPZ_from_APInt (mpz_t v, const APInt apint, bool is_signed) {
23   // There is no sign taken from the data, rop will simply be a positive
24   // integer. An application can handle any sign itself, and apply it for
25   // instance with mpz_neg.
26   APInt abs;
27   if (is_signed)
28    abs  = apint.abs();
29   else
30    abs = apint;
31 
32   const uint64_t *rawdata = abs.getRawData();
33   unsigned numWords = abs.getNumWords();
34 
35   // TODO: Check if this is true for all platforms.
36   mpz_import(v, numWords, 1, sizeof (uint64_t), 0, 0, rawdata);
37 
38   if (is_signed && apint.isNegative()) mpz_neg(v, v);
39 }
40 
41 APInt polly::APInt_from_MPZ (const mpz_t mpz) {
42   uint64_t *p = NULL;
43   size_t sz;
44 
45   p = (uint64_t*) mpz_export(p, &sz, 1, sizeof(uint64_t), 0, 0, mpz);
46 
47   if (p) {
48     APInt A((unsigned)mpz_sizeinbase(mpz, 2), (unsigned)sz , p);
49     A = A.zext(A.getBitWidth() + 1);
50 
51     if (mpz_sgn(mpz) == -1)
52       return -A;
53     else
54       return A;
55   } else {
56     uint64_t val = 0;
57     return APInt(1, 1, &val);
58   }
59 }
60 
61 std::string polly::stringFromIslObj(/*__isl_keep*/ isl_map *map) {
62   isl_printer *p = isl_printer_to_str(isl_map_get_ctx(map));
63   isl_printer_print_map(p, map);
64   std::string string(isl_printer_get_str(p));
65   isl_printer_free(p);
66   return string;
67 }
68 
69 std::string polly::stringFromIslObj(/*__isl_keep*/ isl_set *set) {
70   isl_printer *p = isl_printer_to_str(isl_set_get_ctx(set));
71   isl_printer_print_set(p, set);
72   std::string string(isl_printer_get_str(p));
73   isl_printer_free(p);
74   return string;
75 }
76 
77 std::string polly::stringFromIslObj(/*__isl_keep*/ isl_union_map *umap) {
78   isl_printer *p = isl_printer_to_str(isl_union_map_get_ctx(umap));
79   isl_printer_print_union_map(p, umap);
80   std::string string(isl_printer_get_str(p));
81   isl_printer_free(p);
82   return string;
83 }
84 
85 std::string polly::stringFromIslObj(/*__isl_keep*/ isl_union_set *uset) {
86   isl_printer *p = isl_printer_to_str(isl_union_set_get_ctx(uset));
87   isl_printer_print_union_set(p, uset);
88   std::string string(isl_printer_get_str(p));
89   isl_printer_free(p);
90   return string;
91 }
92