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.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2017-06-08 20:06:15 +08:00
|
|
|
// Functions for converting between gmp objects and llvm::APInt.
|
2011-04-29 14:27:02 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "polly/Support/GICHelper.h"
|
2014-07-25 07:48:02 +08:00
|
|
|
#include "llvm/IR/Value.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"
|
2016-02-18 23:24:42 +08:00
|
|
|
#include "isl/space.h"
|
2013-05-07 16:11:54 +08:00
|
|
|
#include "isl/union_map.h"
|
|
|
|
#include "isl/union_set.h"
|
2013-06-21 14:41:31 +08:00
|
|
|
#include "isl/val.h"
|
|
|
|
|
2016-08-26 18:43:28 +08:00
|
|
|
#include <climits>
|
|
|
|
|
2011-04-29 14:27:02 +08:00
|
|
|
using namespace llvm;
|
|
|
|
|
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;
|
|
|
|
|
2016-08-26 20:01:07 +08:00
|
|
|
// As isl is interpreting the input always as unsigned value, we need some
|
|
|
|
// additional pre and post processing to import signed values. The approach
|
|
|
|
// we take is to first obtain the absolute value of Int and then negate the
|
|
|
|
// value after it has been imported to isl.
|
|
|
|
//
|
|
|
|
// It should be noted that the smallest integer value represented in two's
|
|
|
|
// complement with a certain amount of bits does not have a corresponding
|
|
|
|
// positive representation in two's complement representation with the same
|
|
|
|
// number of bits. E.g. 110 (-2) does not have a corresponding value for (2).
|
|
|
|
// To ensure that there is always a corresponding value available we first
|
|
|
|
// sign-extend the input by one bit and only then take the absolute value.
|
2013-06-21 14:41:31 +08:00
|
|
|
if (IsSigned)
|
2016-08-26 20:01:07 +08:00
|
|
|
Abs = Int.sext(Int.getBitWidth() + 1).abs();
|
2013-06-21 14:41:31 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
APInt polly::APIntFromVal(__isl_take isl_val *Val) {
|
|
|
|
uint64_t *Data;
|
|
|
|
int NumChunks;
|
2016-08-26 18:43:28 +08:00
|
|
|
const static int ChunkSize = sizeof(uint64_t);
|
2013-06-21 14:41:31 +08:00
|
|
|
|
2016-08-26 18:43:28 +08:00
|
|
|
assert(isl_val_is_int(Val) && "Only integers can be converted to APInt");
|
2013-06-21 14:41:31 +08:00
|
|
|
|
2016-08-26 18:43:28 +08:00
|
|
|
NumChunks = isl_val_n_abs_num_chunks(Val, ChunkSize);
|
|
|
|
Data = (uint64_t *)malloc(NumChunks * ChunkSize);
|
|
|
|
isl_val_get_abs_num_chunks(Val, ChunkSize, Data);
|
|
|
|
int NumBits = CHAR_BIT * ChunkSize * NumChunks;
|
|
|
|
APInt A(NumBits, NumChunks, Data);
|
2013-06-21 14:41:31 +08:00
|
|
|
|
2016-08-26 18:43:28 +08:00
|
|
|
// As isl provides only an interface to obtain data that describes the
|
|
|
|
// absolute value of an isl_val, A at this point always contains a positive
|
|
|
|
// number. In case Val was originally negative, we expand the size of A by
|
|
|
|
// one and negate the value (in two's complement representation). As a result,
|
|
|
|
// the new value in A corresponds now with Val.
|
2013-06-21 14:41:31 +08:00
|
|
|
if (isl_val_is_neg(Val)) {
|
|
|
|
A = A.zext(A.getBitWidth() + 1);
|
|
|
|
A = -A;
|
|
|
|
}
|
|
|
|
|
2016-08-26 18:43:28 +08:00
|
|
|
// isl may represent small numbers with more than the minimal number of bits.
|
|
|
|
// We truncate the APInt to the minimal number of bits needed to represent the
|
|
|
|
// signed value it contains, to ensure that the bitwidth is always minimal.
|
2013-06-21 14:41:31 +08:00
|
|
|
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) {
|
2015-12-14 03:35:26 +08:00
|
|
|
if (!isl_obj)
|
|
|
|
return "null";
|
2012-07-05 16:42:39 +08:00
|
|
|
isl_ctx *ctx = ctx_getter_fn(isl_obj);
|
|
|
|
isl_printer *p = isl_printer_to_str(ctx);
|
2016-09-08 22:34:54 +08:00
|
|
|
p = printer_fn(p, isl_obj);
|
2011-08-20 19:11:18 +08:00
|
|
|
char *char_str = isl_printer_get_str(p);
|
2015-11-10 23:09:44 +08:00
|
|
|
std::string string;
|
|
|
|
if (char_str)
|
|
|
|
string = char_str;
|
|
|
|
else
|
|
|
|
string = "null";
|
2011-08-20 19:11:18 +08:00
|
|
|
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
|
|
|
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) {
|
2014-11-22 03:39:42 +08:00
|
|
|
return stringFromIslObjInternal(schedule, isl_schedule_get_ctx,
|
2012-07-05 16:42:39 +08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2016-12-17 07:41:26 +08:00
|
|
|
std::string polly::stringFromIslObj(__isl_keep isl_multi_pw_aff *mpa) {
|
|
|
|
return stringFromIslObjInternal(mpa, isl_multi_pw_aff_get_ctx,
|
|
|
|
isl_printer_print_multi_pw_aff);
|
|
|
|
}
|
|
|
|
|
2016-02-20 11:40:19 +08:00
|
|
|
std::string polly::stringFromIslObj(__isl_keep isl_union_pw_multi_aff *upma) {
|
|
|
|
return stringFromIslObjInternal(upma, isl_union_pw_multi_aff_get_ctx,
|
|
|
|
isl_printer_print_union_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);
|
|
|
|
}
|
2014-07-25 07:48:02 +08:00
|
|
|
|
2016-02-18 23:24:42 +08:00
|
|
|
std::string polly::stringFromIslObj(__isl_keep isl_space *space) {
|
|
|
|
return stringFromIslObjInternal(space, isl_space_get_ctx,
|
|
|
|
isl_printer_print_space);
|
|
|
|
}
|
|
|
|
|
2014-07-25 07:48:02 +08:00
|
|
|
static void replace(std::string &str, const std::string &find,
|
|
|
|
const std::string &replace) {
|
|
|
|
size_t pos = 0;
|
|
|
|
while ((pos = str.find(find, pos)) != std::string::npos) {
|
|
|
|
str.replace(pos, find.length(), replace);
|
|
|
|
pos += replace.length();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void makeIslCompatible(std::string &str) {
|
|
|
|
replace(str, ".", "_");
|
|
|
|
replace(str, "\"", "_");
|
2015-07-09 15:31:45 +08:00
|
|
|
replace(str, " ", "__");
|
|
|
|
replace(str, "=>", "TO");
|
2017-05-06 22:03:58 +08:00
|
|
|
replace(str, "+", "_");
|
2014-07-25 07:48:02 +08:00
|
|
|
}
|
|
|
|
|
2015-02-20 02:09:39 +08:00
|
|
|
std::string polly::getIslCompatibleName(const std::string &Prefix,
|
|
|
|
const std::string &Middle,
|
|
|
|
const std::string &Suffix) {
|
|
|
|
std::string S = Prefix + Middle + Suffix;
|
|
|
|
makeIslCompatible(S);
|
|
|
|
return S;
|
|
|
|
}
|
|
|
|
|
2015-02-20 06:16:12 +08:00
|
|
|
std::string polly::getIslCompatibleName(const std::string &Prefix,
|
[ScopInfo] Do not use LLVM names to identify statements, arrays, and parameters
LLVM-IR names are commonly available in debug builds, but often not in release
builds. Hence, using LLVM-IR names to identify statements or memory reference
results makes the behavior of Polly depend on the compile mode. This is
undesirable. Hence, we now just number the statements instead of using LLVM-IR
names to identify them (this issue has previously been brought up by Zino
Benaissa).
However, as LLVM-IR names help in making test cases more readable, we add an
option '-polly-use-llvm-names' to still use LLVM-IR names. This flag is by
default set in the polly tests to make test cases more readable.
This change reduces the time in ScopInfo from 32 seconds to 2 seconds for the
following test case provided by Eli Friedman <efriedma@codeaurora.org> (already
used in one of the previous commits):
struct X { int x; };
void a();
#define SIG (int x, X **y, X **z)
typedef void (*fn)SIG;
#define FN { for (int i = 0; i < x; ++i) { (*y)[i].x += (*z)[i].x; } a(); }
#define FN5 FN FN FN FN FN
#define FN25 FN5 FN5 FN5 FN5
#define FN125 FN25 FN25 FN25 FN25 FN25
#define FN250 FN125 FN125
#define FN1250 FN250 FN250 FN250 FN250 FN250
void x SIG { FN1250 }
For a larger benchmark I have on-hand (10000 loops), this reduces the time for
running -polly-scops from 5 minutes to 4 minutes, a reduction by 20%.
The reason for this large speedup is that our previous use of printAsOperand
had a quadratic cost, as for each printed and unnamed operand the full function
was scanned to find the instruction number that identifies the operand.
We do not need to adjust the way memory reference ids are constructured, as
they do not use LLVM values.
Reviewed by: efriedma
Tags: #polly
Differential Revision: https://reviews.llvm.org/D32789
llvm-svn: 302072
2017-05-04 04:08:52 +08:00
|
|
|
const std::string &Name, long Number,
|
|
|
|
const std::string &Suffix,
|
|
|
|
bool UseInstructionNames) {
|
|
|
|
std::string S = Prefix;
|
|
|
|
|
|
|
|
if (UseInstructionNames)
|
|
|
|
S += std::string("_") + Name;
|
|
|
|
else
|
|
|
|
S += std::to_string(Number);
|
|
|
|
|
|
|
|
S += Suffix;
|
|
|
|
|
|
|
|
makeIslCompatible(S);
|
|
|
|
return S;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string polly::getIslCompatibleName(const std::string &Prefix,
|
|
|
|
const Value *Val, long Number,
|
|
|
|
const std::string &Suffix,
|
|
|
|
bool UseInstructionNames) {
|
2014-07-25 07:48:02 +08:00
|
|
|
std::string ValStr;
|
[ScopInfo] Do not use LLVM names to identify statements, arrays, and parameters
LLVM-IR names are commonly available in debug builds, but often not in release
builds. Hence, using LLVM-IR names to identify statements or memory reference
results makes the behavior of Polly depend on the compile mode. This is
undesirable. Hence, we now just number the statements instead of using LLVM-IR
names to identify them (this issue has previously been brought up by Zino
Benaissa).
However, as LLVM-IR names help in making test cases more readable, we add an
option '-polly-use-llvm-names' to still use LLVM-IR names. This flag is by
default set in the polly tests to make test cases more readable.
This change reduces the time in ScopInfo from 32 seconds to 2 seconds for the
following test case provided by Eli Friedman <efriedma@codeaurora.org> (already
used in one of the previous commits):
struct X { int x; };
void a();
#define SIG (int x, X **y, X **z)
typedef void (*fn)SIG;
#define FN { for (int i = 0; i < x; ++i) { (*y)[i].x += (*z)[i].x; } a(); }
#define FN5 FN FN FN FN FN
#define FN25 FN5 FN5 FN5 FN5
#define FN125 FN25 FN25 FN25 FN25 FN25
#define FN250 FN125 FN125
#define FN1250 FN250 FN250 FN250 FN250 FN250
void x SIG { FN1250 }
For a larger benchmark I have on-hand (10000 loops), this reduces the time for
running -polly-scops from 5 minutes to 4 minutes, a reduction by 20%.
The reason for this large speedup is that our previous use of printAsOperand
had a quadratic cost, as for each printed and unnamed operand the full function
was scanned to find the instruction number that identifies the operand.
We do not need to adjust the way memory reference ids are constructured, as
they do not use LLVM values.
Reviewed by: efriedma
Tags: #polly
Differential Revision: https://reviews.llvm.org/D32789
llvm-svn: 302072
2017-05-04 04:08:52 +08:00
|
|
|
|
|
|
|
if (UseInstructionNames && Val->hasName())
|
|
|
|
ValStr = std::string("_") + std::string(Val->getName());
|
|
|
|
else
|
|
|
|
ValStr = std::to_string(Number);
|
|
|
|
|
2015-02-20 02:09:39 +08:00
|
|
|
return getIslCompatibleName(Prefix, ValStr, Suffix);
|
2014-07-25 07:48:02 +08:00
|
|
|
}
|
2017-09-28 17:51:04 +08:00
|
|
|
|
|
|
|
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
|
|
|
|
/// To call a inline dump() method in a debugger, at it must have been
|
|
|
|
/// instantiated in at least one translation unit. Because isl's dump() method
|
|
|
|
/// are meant to be called from a debugger only, but not from code, no such
|
|
|
|
/// instantiation would exist. We use this method to force an instantiation in
|
|
|
|
/// this translation unit. Because it has non-static linking, the compiler does
|
|
|
|
/// not know that it is never called, and therefore must ensure the existence of
|
|
|
|
/// the dump functions.
|
|
|
|
void neverCalled() {
|
|
|
|
isl::aff().dump();
|
|
|
|
isl::aff_list().dump();
|
|
|
|
isl::ast_expr().dump();
|
|
|
|
isl::ast_expr_list().dump();
|
|
|
|
isl::ast_node().dump();
|
|
|
|
isl::ast_node_list().dump();
|
|
|
|
isl::basic_map().dump();
|
|
|
|
isl::basic_map_list().dump();
|
|
|
|
isl::basic_set().dump();
|
|
|
|
isl::basic_set_list().dump();
|
|
|
|
isl::constraint().dump();
|
|
|
|
isl::constraint_list().dump();
|
|
|
|
isl::id().dump();
|
|
|
|
isl::id_list().dump();
|
|
|
|
isl::id_to_ast_expr().dump();
|
|
|
|
isl::local_space().dump();
|
|
|
|
isl::map().dump();
|
|
|
|
isl::map_list().dump();
|
|
|
|
isl::multi_aff().dump();
|
|
|
|
isl::multi_pw_aff().dump();
|
|
|
|
isl::multi_union_pw_aff().dump();
|
|
|
|
isl::multi_val().dump();
|
|
|
|
isl::point().dump();
|
|
|
|
isl::pw_aff().dump();
|
|
|
|
isl::pw_aff_list().dump();
|
|
|
|
isl::pw_multi_aff().dump();
|
|
|
|
isl::pw_qpolynomial().dump();
|
|
|
|
isl::qpolynomial().dump();
|
|
|
|
isl::schedule().dump();
|
|
|
|
isl::schedule_constraints().dump();
|
|
|
|
isl::schedule_node().dump();
|
|
|
|
isl::set().dump();
|
|
|
|
isl::set_list().dump();
|
|
|
|
isl::space().dump();
|
|
|
|
isl::union_map().dump();
|
|
|
|
isl::union_map_list().dump();
|
|
|
|
isl::union_pw_aff().dump();
|
|
|
|
isl::union_pw_aff_list().dump();
|
|
|
|
isl::union_pw_multi_aff().dump();
|
|
|
|
isl::union_pw_multi_aff_list().dump();
|
|
|
|
isl::union_set().dump();
|
|
|
|
isl::union_set_list().dump();
|
|
|
|
isl::val().dump();
|
|
|
|
isl::val_list().dump();
|
|
|
|
}
|
|
|
|
#endif
|