2011-04-29 14:27:02 +08:00
|
|
|
//===- GmpConv.cpp - Recreate LLVM IR from the Scop. ---------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// Functions for converting between gmp objects and apint.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "polly/Support/GICHelper.h"
|
2013-05-07 16:11:54 +08:00
|
|
|
#include "isl/aff.h"
|
2011-04-29 14:27:02 +08:00
|
|
|
#include "isl/map.h"
|
2011-07-01 04:01:02 +08:00
|
|
|
#include "isl/schedule.h"
|
2013-05-07 16:11:54 +08:00
|
|
|
#include "isl/set.h"
|
|
|
|
#include "isl/union_map.h"
|
|
|
|
#include "isl/union_set.h"
|
2013-06-21 14:41:31 +08:00
|
|
|
#include "isl/val.h"
|
|
|
|
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2011-04-29 14:27:02 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2013-02-22 16:21:52 +08:00
|
|
|
void polly::MPZ_from_APInt(mpz_t v, const APInt apint, bool is_signed) {
|
2011-04-29 14:27:02 +08:00
|
|
|
// There is no sign taken from the data, rop will simply be a positive
|
|
|
|
// integer. An application can handle any sign itself, and apply it for
|
|
|
|
// instance with mpz_neg.
|
|
|
|
APInt abs;
|
|
|
|
if (is_signed)
|
2013-02-22 16:21:52 +08:00
|
|
|
abs = apint.abs();
|
2011-04-29 14:27:02 +08:00
|
|
|
else
|
2013-02-22 16:21:52 +08:00
|
|
|
abs = apint;
|
2011-04-29 14:27:02 +08:00
|
|
|
|
|
|
|
const uint64_t *rawdata = abs.getRawData();
|
|
|
|
unsigned numWords = abs.getNumWords();
|
|
|
|
|
2013-06-15 00:23:38 +08:00
|
|
|
mpz_import(v, numWords, -1, sizeof(uint64_t), 0, 0, rawdata);
|
2011-04-29 14:27:02 +08:00
|
|
|
|
2013-02-22 16:21:52 +08:00
|
|
|
if (is_signed && apint.isNegative())
|
|
|
|
mpz_neg(v, v);
|
2011-04-29 14:27:02 +08:00
|
|
|
}
|
|
|
|
|
2013-06-21 14:41:31 +08:00
|
|
|
__isl_give isl_val *polly::isl_valFromAPInt(isl_ctx *Ctx, const APInt Int,
|
|
|
|
bool IsSigned) {
|
|
|
|
APInt Abs;
|
|
|
|
isl_val *v;
|
|
|
|
|
|
|
|
if (IsSigned)
|
|
|
|
Abs = Int.abs();
|
|
|
|
else
|
|
|
|
Abs = Int;
|
|
|
|
|
|
|
|
const uint64_t *Data = Abs.getRawData();
|
|
|
|
unsigned Words = Abs.getNumWords();
|
|
|
|
|
|
|
|
v = isl_val_int_from_chunks(Ctx, Words, sizeof(uint64_t), Data);
|
|
|
|
|
|
|
|
if (IsSigned && Int.isNegative())
|
|
|
|
v = isl_val_neg(v);
|
|
|
|
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
2013-02-22 16:21:52 +08:00
|
|
|
APInt polly::APInt_from_MPZ(const mpz_t mpz) {
|
2011-04-29 14:27:02 +08:00
|
|
|
uint64_t *p = NULL;
|
|
|
|
size_t sz;
|
|
|
|
|
2013-06-15 00:23:38 +08:00
|
|
|
p = (uint64_t *)mpz_export(p, &sz, -1, sizeof(uint64_t), 0, 0, mpz);
|
2011-04-29 14:27:02 +08:00
|
|
|
|
|
|
|
if (p) {
|
2013-02-22 16:21:52 +08:00
|
|
|
APInt A((unsigned) mpz_sizeinbase(mpz, 2), (unsigned) sz, p);
|
2011-04-29 14:27:02 +08:00
|
|
|
A = A.zext(A.getBitWidth() + 1);
|
2011-08-20 19:11:25 +08:00
|
|
|
free(p);
|
2011-04-29 14:27:02 +08:00
|
|
|
|
|
|
|
if (mpz_sgn(mpz) == -1)
|
|
|
|
return -A;
|
|
|
|
else
|
|
|
|
return A;
|
|
|
|
} else {
|
|
|
|
uint64_t val = 0;
|
|
|
|
return APInt(1, 1, &val);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-21 14:41:31 +08:00
|
|
|
APInt polly::APIntFromVal(__isl_take isl_val *Val) {
|
|
|
|
uint64_t *Data;
|
|
|
|
int NumChunks;
|
|
|
|
|
|
|
|
NumChunks = isl_val_n_abs_num_chunks(Val, sizeof(uint64_t));
|
|
|
|
|
|
|
|
Data = (uint64_t*) malloc(NumChunks * sizeof(uint64_t));
|
|
|
|
isl_val_get_abs_num_chunks(Val, sizeof(uint64_t), Data);
|
|
|
|
APInt A(8 * sizeof(uint64_t) * NumChunks, NumChunks, Data);
|
|
|
|
|
|
|
|
if (isl_val_is_neg(Val)) {
|
|
|
|
A = A.zext(A.getBitWidth() + 1);
|
|
|
|
A = -A;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (A.getMinSignedBits() < A.getBitWidth())
|
|
|
|
A = A.trunc(A.getMinSignedBits());
|
|
|
|
|
|
|
|
free(Data);
|
|
|
|
isl_val_free(Val);
|
|
|
|
return A;
|
|
|
|
}
|
|
|
|
|
2013-02-22 16:21:52 +08:00
|
|
|
template <typename ISLTy, typename ISL_CTX_GETTER, typename ISL_PRINTER>
|
2013-05-07 15:30:56 +08:00
|
|
|
static inline std::string stringFromIslObjInternal(__isl_keep ISLTy *isl_obj,
|
|
|
|
ISL_CTX_GETTER ctx_getter_fn,
|
|
|
|
ISL_PRINTER printer_fn) {
|
2012-07-05 16:42:39 +08:00
|
|
|
isl_ctx *ctx = ctx_getter_fn(isl_obj);
|
|
|
|
isl_printer *p = isl_printer_to_str(ctx);
|
|
|
|
printer_fn(p, isl_obj);
|
2011-08-20 19:11:18 +08:00
|
|
|
char *char_str = isl_printer_get_str(p);
|
|
|
|
std::string string(char_str);
|
|
|
|
free(char_str);
|
2011-04-29 14:27:02 +08:00
|
|
|
isl_printer_free(p);
|
|
|
|
return string;
|
|
|
|
}
|
|
|
|
|
2013-02-22 16:21:52 +08:00
|
|
|
static inline isl_ctx *schedule_get_ctx(__isl_keep isl_schedule *schedule) {
|
2012-07-05 16:42:39 +08:00
|
|
|
return isl_union_map_get_ctx(isl_schedule_get_map(schedule));
|
|
|
|
}
|
|
|
|
|
2013-02-22 16:21:52 +08:00
|
|
|
std::string polly::stringFromIslObj(__isl_keep isl_map *map) {
|
|
|
|
return stringFromIslObjInternal(map, isl_map_get_ctx, isl_printer_print_map);
|
2012-07-05 16:42:39 +08:00
|
|
|
}
|
|
|
|
|
2013-02-22 16:21:52 +08:00
|
|
|
std::string polly::stringFromIslObj(__isl_keep isl_set *set) {
|
|
|
|
return stringFromIslObjInternal(set, isl_set_get_ctx, isl_printer_print_set);
|
2011-04-29 14:27:02 +08:00
|
|
|
}
|
|
|
|
|
2013-02-22 16:21:52 +08:00
|
|
|
std::string polly::stringFromIslObj(__isl_keep isl_union_map *umap) {
|
2012-07-05 16:42:39 +08:00
|
|
|
return stringFromIslObjInternal(umap, isl_union_map_get_ctx,
|
|
|
|
isl_printer_print_union_map);
|
2011-04-29 14:27:02 +08:00
|
|
|
}
|
|
|
|
|
2013-02-22 16:21:52 +08:00
|
|
|
std::string polly::stringFromIslObj(__isl_keep isl_union_set *uset) {
|
2012-07-05 16:42:39 +08:00
|
|
|
return stringFromIslObjInternal(uset, isl_union_set_get_ctx,
|
|
|
|
isl_printer_print_union_set);
|
2011-04-29 14:27:02 +08:00
|
|
|
}
|
2011-07-01 04:01:02 +08:00
|
|
|
|
2013-02-22 16:21:52 +08:00
|
|
|
std::string polly::stringFromIslObj(__isl_keep isl_schedule *schedule) {
|
2012-07-05 16:42:39 +08:00
|
|
|
return stringFromIslObjInternal(schedule, schedule_get_ctx,
|
|
|
|
isl_printer_print_schedule);
|
2011-07-01 04:01:02 +08:00
|
|
|
}
|
2012-07-05 16:55:31 +08:00
|
|
|
|
2013-02-22 16:21:52 +08:00
|
|
|
std::string polly::stringFromIslObj(__isl_keep isl_multi_aff *maff) {
|
2012-07-05 16:55:31 +08:00
|
|
|
return stringFromIslObjInternal(maff, isl_multi_aff_get_ctx,
|
|
|
|
isl_printer_print_multi_aff);
|
|
|
|
}
|
|
|
|
|
2013-02-22 16:21:52 +08:00
|
|
|
std::string polly::stringFromIslObj(__isl_keep isl_pw_multi_aff *pma) {
|
2012-07-05 16:55:31 +08:00
|
|
|
return stringFromIslObjInternal(pma, isl_pw_multi_aff_get_ctx,
|
|
|
|
isl_printer_print_pw_multi_aff);
|
|
|
|
}
|
|
|
|
|
2013-02-22 16:21:52 +08:00
|
|
|
std::string polly::stringFromIslObj(__isl_keep isl_aff *aff) {
|
2012-07-05 16:55:31 +08:00
|
|
|
return stringFromIslObjInternal(aff, isl_aff_get_ctx, isl_printer_print_aff);
|
|
|
|
}
|
|
|
|
|
2013-02-22 16:21:52 +08:00
|
|
|
std::string polly::stringFromIslObj(__isl_keep isl_pw_aff *pwaff) {
|
2012-07-05 16:55:31 +08:00
|
|
|
return stringFromIslObjInternal(pwaff, isl_pw_aff_get_ctx,
|
|
|
|
isl_printer_print_pw_aff);
|
|
|
|
}
|