2015-04-27 20:32:24 +08:00
|
|
|
//===------ IslNodeBuilder.cpp - Translate an isl AST into a LLVM-IR AST---===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file contains the IslNodeBuilder, a class to translate an isl AST into
|
|
|
|
// a LLVM-IR AST.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "polly/CodeGen/IslNodeBuilder.h"
|
|
|
|
#include "polly/CodeGen/BlockGenerators.h"
|
|
|
|
#include "polly/CodeGen/CodeGeneration.h"
|
|
|
|
#include "polly/CodeGen/IslAst.h"
|
2015-05-09 17:13:42 +08:00
|
|
|
#include "polly/CodeGen/IslExprBuilder.h"
|
2015-04-27 20:32:24 +08:00
|
|
|
#include "polly/CodeGen/LoopGenerators.h"
|
|
|
|
#include "polly/CodeGen/Utils.h"
|
2015-05-09 17:13:42 +08:00
|
|
|
#include "polly/Config/config.h"
|
2015-04-27 20:32:24 +08:00
|
|
|
#include "polly/DependenceInfo.h"
|
|
|
|
#include "polly/LinkAllPasses.h"
|
|
|
|
#include "polly/ScopInfo.h"
|
|
|
|
#include "polly/Support/GICHelper.h"
|
|
|
|
#include "polly/Support/SCEVValidator.h"
|
2015-05-09 17:13:42 +08:00
|
|
|
#include "polly/Support/ScopHelper.h"
|
2015-04-27 20:32:24 +08:00
|
|
|
#include "polly/TempScopInfo.h"
|
|
|
|
#include "llvm/ADT/PostOrderIterator.h"
|
|
|
|
#include "llvm/ADT/SmallPtrSet.h"
|
|
|
|
#include "llvm/Analysis/LoopInfo.h"
|
|
|
|
#include "llvm/Analysis/PostDominators.h"
|
2015-05-09 17:13:42 +08:00
|
|
|
#include "llvm/IR/DataLayout.h"
|
2015-04-27 20:32:24 +08:00
|
|
|
#include "llvm/IR/Module.h"
|
2015-05-09 17:13:42 +08:00
|
|
|
#include "llvm/IR/Verifier.h"
|
2015-04-27 20:32:24 +08:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
|
2015-05-09 17:13:42 +08:00
|
|
|
#include "isl/aff.h"
|
2015-04-27 20:32:24 +08:00
|
|
|
#include "isl/ast.h"
|
|
|
|
#include "isl/ast_build.h"
|
2015-05-09 17:13:42 +08:00
|
|
|
#include "isl/list.h"
|
2015-04-27 20:32:24 +08:00
|
|
|
#include "isl/map.h"
|
2015-05-09 17:13:42 +08:00
|
|
|
#include "isl/set.h"
|
|
|
|
#include "isl/union_map.h"
|
2015-05-09 17:36:38 +08:00
|
|
|
#include "isl/union_set.h"
|
2015-04-27 20:32:24 +08:00
|
|
|
|
|
|
|
using namespace polly;
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
__isl_give isl_ast_expr *
|
|
|
|
IslNodeBuilder::getUpperBound(__isl_keep isl_ast_node *For,
|
|
|
|
ICmpInst::Predicate &Predicate) {
|
|
|
|
isl_id *UBID, *IteratorID;
|
|
|
|
isl_ast_expr *Cond, *Iterator, *UB, *Arg0;
|
|
|
|
isl_ast_op_type Type;
|
|
|
|
|
|
|
|
Cond = isl_ast_node_for_get_cond(For);
|
|
|
|
Iterator = isl_ast_node_for_get_iterator(For);
|
|
|
|
isl_ast_expr_get_type(Cond);
|
|
|
|
assert(isl_ast_expr_get_type(Cond) == isl_ast_expr_op &&
|
|
|
|
"conditional expression is not an atomic upper bound");
|
|
|
|
|
|
|
|
Type = isl_ast_expr_get_op_type(Cond);
|
|
|
|
|
|
|
|
switch (Type) {
|
|
|
|
case isl_ast_op_le:
|
|
|
|
Predicate = ICmpInst::ICMP_SLE;
|
|
|
|
break;
|
|
|
|
case isl_ast_op_lt:
|
|
|
|
Predicate = ICmpInst::ICMP_SLT;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
llvm_unreachable("Unexpected comparision type in loop conditon");
|
|
|
|
}
|
|
|
|
|
|
|
|
Arg0 = isl_ast_expr_get_op_arg(Cond, 0);
|
|
|
|
|
|
|
|
assert(isl_ast_expr_get_type(Arg0) == isl_ast_expr_id &&
|
|
|
|
"conditional expression is not an atomic upper bound");
|
|
|
|
|
|
|
|
UBID = isl_ast_expr_get_id(Arg0);
|
|
|
|
|
|
|
|
assert(isl_ast_expr_get_type(Iterator) == isl_ast_expr_id &&
|
|
|
|
"Could not get the iterator");
|
|
|
|
|
|
|
|
IteratorID = isl_ast_expr_get_id(Iterator);
|
|
|
|
|
|
|
|
assert(UBID == IteratorID &&
|
|
|
|
"conditional expression is not an atomic upper bound");
|
|
|
|
|
|
|
|
UB = isl_ast_expr_get_op_arg(Cond, 1);
|
|
|
|
|
|
|
|
isl_ast_expr_free(Cond);
|
|
|
|
isl_ast_expr_free(Iterator);
|
|
|
|
isl_ast_expr_free(Arg0);
|
|
|
|
isl_id_free(IteratorID);
|
|
|
|
isl_id_free(UBID);
|
|
|
|
|
|
|
|
return UB;
|
|
|
|
}
|
|
|
|
|
2015-08-21 17:08:14 +08:00
|
|
|
/// @brief Return true if a return value of Predicate is true for the value
|
|
|
|
/// represented by passed isl_ast_expr_int.
|
|
|
|
static bool checkIslAstExprInt(__isl_take isl_ast_expr *Expr,
|
|
|
|
isl_bool (*Predicate)(__isl_keep isl_val *)) {
|
|
|
|
if (isl_ast_expr_get_type(Expr) != isl_ast_expr_int) {
|
|
|
|
isl_ast_expr_free(Expr);
|
|
|
|
return false;
|
2015-04-27 20:32:24 +08:00
|
|
|
}
|
2015-08-21 17:08:14 +08:00
|
|
|
auto ExprVal = isl_ast_expr_get_val(Expr);
|
|
|
|
isl_ast_expr_free(Expr);
|
|
|
|
if (Predicate(ExprVal) != true) {
|
|
|
|
isl_val_free(ExprVal);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
isl_val_free(ExprVal);
|
|
|
|
return true;
|
|
|
|
}
|
2015-04-27 20:32:24 +08:00
|
|
|
|
2015-08-21 17:08:14 +08:00
|
|
|
int IslNodeBuilder::getNumberOfIterations(__isl_keep isl_ast_node *For) {
|
|
|
|
assert(isl_ast_node_get_type(For) == isl_ast_node_for);
|
2015-08-25 04:11:34 +08:00
|
|
|
auto Body = isl_ast_node_for_get_body(For);
|
|
|
|
|
|
|
|
// First, check if we can actually handle this code
|
|
|
|
switch (isl_ast_node_get_type(Body)) {
|
|
|
|
case isl_ast_node_user:
|
|
|
|
break;
|
|
|
|
case isl_ast_node_block: {
|
|
|
|
isl_ast_node_list *List = isl_ast_node_block_get_children(Body);
|
|
|
|
for (int i = 0; i < isl_ast_node_list_n_ast_node(List); ++i) {
|
|
|
|
isl_ast_node *Node = isl_ast_node_list_get_ast_node(List, i);
|
|
|
|
int Type = isl_ast_node_get_type(Node);
|
|
|
|
isl_ast_node_free(Node);
|
|
|
|
if (Type != isl_ast_node_user) {
|
|
|
|
isl_ast_node_list_free(List);
|
|
|
|
isl_ast_node_free(Body);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
isl_ast_node_list_free(List);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
isl_ast_node_free(Body);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
isl_ast_node_free(Body);
|
|
|
|
|
2015-08-21 17:08:14 +08:00
|
|
|
auto Init = isl_ast_node_for_get_init(For);
|
|
|
|
if (!checkIslAstExprInt(Init, isl_val_is_zero))
|
|
|
|
return -1;
|
|
|
|
auto Inc = isl_ast_node_for_get_inc(For);
|
|
|
|
if (!checkIslAstExprInt(Inc, isl_val_is_one))
|
|
|
|
return -1;
|
|
|
|
CmpInst::Predicate Predicate;
|
|
|
|
auto UB = getUpperBound(For, Predicate);
|
|
|
|
if (isl_ast_expr_get_type(UB) != isl_ast_expr_int) {
|
|
|
|
isl_ast_expr_free(UB);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
auto UpVal = isl_ast_expr_get_val(UB);
|
|
|
|
isl_ast_expr_free(UB);
|
|
|
|
int NumberIterations = isl_val_get_num_si(UpVal);
|
|
|
|
isl_val_free(UpVal);
|
|
|
|
if (NumberIterations < 0)
|
2015-04-27 20:32:24 +08:00
|
|
|
return -1;
|
2015-08-21 17:08:14 +08:00
|
|
|
if (Predicate == CmpInst::ICMP_SLT)
|
|
|
|
return NumberIterations;
|
|
|
|
else
|
|
|
|
return NumberIterations + 1;
|
2015-04-27 20:32:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
struct FindValuesUser {
|
|
|
|
LoopInfo &LI;
|
|
|
|
ScalarEvolution &SE;
|
|
|
|
Region &R;
|
|
|
|
SetVector<Value *> &Values;
|
|
|
|
SetVector<const SCEV *> &SCEVs;
|
|
|
|
};
|
|
|
|
|
|
|
|
/// @brief Extract the values and SCEVs needed to generate code for a block.
|
|
|
|
static int findValuesInBlock(struct FindValuesUser &User, const ScopStmt *Stmt,
|
|
|
|
const BasicBlock *BB) {
|
|
|
|
// Check all the operands of instructions in the basic block.
|
|
|
|
for (const Instruction &Inst : *BB) {
|
|
|
|
for (Value *SrcVal : Inst.operands()) {
|
|
|
|
if (Instruction *OpInst = dyn_cast<Instruction>(SrcVal))
|
|
|
|
if (canSynthesize(OpInst, &User.LI, &User.SE, &User.R)) {
|
|
|
|
User.SCEVs.insert(
|
|
|
|
User.SE.getSCEVAtScope(OpInst, User.LI.getLoopFor(BB)));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (Instruction *OpInst = dyn_cast<Instruction>(SrcVal))
|
|
|
|
if (Stmt->getParent()->getRegion().contains(OpInst))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (isa<Instruction>(SrcVal) || isa<Argument>(SrcVal))
|
|
|
|
User.Values.insert(SrcVal);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Extract the values and SCEVs needed to generate code for a ScopStmt.
|
|
|
|
///
|
|
|
|
/// This function extracts a ScopStmt from a given isl_set and computes the
|
|
|
|
/// Values this statement depends on as well as a set of SCEV expressions that
|
|
|
|
/// need to be synthesized when generating code for this statment.
|
2015-05-28 21:32:11 +08:00
|
|
|
static isl_stat findValuesInStmt(isl_set *Set, void *UserPtr) {
|
2015-04-27 20:32:24 +08:00
|
|
|
isl_id *Id = isl_set_get_tuple_id(Set);
|
|
|
|
struct FindValuesUser &User = *static_cast<struct FindValuesUser *>(UserPtr);
|
|
|
|
const ScopStmt *Stmt = static_cast<const ScopStmt *>(isl_id_get_user(Id));
|
|
|
|
|
|
|
|
if (Stmt->isBlockStmt())
|
|
|
|
findValuesInBlock(User, Stmt, Stmt->getBasicBlock());
|
|
|
|
else {
|
|
|
|
assert(Stmt->isRegionStmt() &&
|
|
|
|
"Stmt was neither block nor region statement");
|
|
|
|
for (const BasicBlock *BB : Stmt->getRegion()->blocks())
|
|
|
|
findValuesInBlock(User, Stmt, BB);
|
|
|
|
}
|
|
|
|
|
|
|
|
isl_id_free(Id);
|
|
|
|
isl_set_free(Set);
|
2015-05-28 21:32:11 +08:00
|
|
|
return isl_stat_ok;
|
2015-04-27 20:32:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void IslNodeBuilder::getReferencesInSubtree(__isl_keep isl_ast_node *For,
|
|
|
|
SetVector<Value *> &Values,
|
|
|
|
SetVector<const Loop *> &Loops) {
|
|
|
|
|
|
|
|
SetVector<const SCEV *> SCEVs;
|
|
|
|
struct FindValuesUser FindValues = {LI, SE, S.getRegion(), Values, SCEVs};
|
|
|
|
|
|
|
|
for (const auto &I : IDToValue)
|
|
|
|
Values.insert(I.second);
|
|
|
|
|
|
|
|
for (const auto &I : OutsideLoopIterations)
|
|
|
|
Values.insert(cast<SCEVUnknown>(I.second)->getValue());
|
|
|
|
|
|
|
|
isl_union_set *Schedule = isl_union_map_domain(IslAstInfo::getSchedule(For));
|
|
|
|
|
|
|
|
isl_union_set_foreach_set(Schedule, findValuesInStmt, &FindValues);
|
|
|
|
isl_union_set_free(Schedule);
|
|
|
|
|
|
|
|
for (const SCEV *Expr : SCEVs) {
|
|
|
|
findValues(Expr, Values);
|
|
|
|
findLoops(Expr, Loops);
|
|
|
|
}
|
|
|
|
|
|
|
|
Values.remove_if([](const Value *V) { return isa<GlobalValue>(V); });
|
|
|
|
|
|
|
|
/// Remove loops that contain the scop or that are part of the scop, as they
|
|
|
|
/// are considered local. This leaves only loops that are before the scop, but
|
|
|
|
/// do not contain the scop itself.
|
|
|
|
Loops.remove_if([this](const Loop *L) {
|
2015-05-27 05:37:31 +08:00
|
|
|
return S.getRegion().contains(L) || L->contains(S.getRegion().getEntry());
|
2015-04-27 20:32:24 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void IslNodeBuilder::updateValues(
|
|
|
|
ParallelLoopGenerator::ValueToValueMapTy &NewValues) {
|
|
|
|
SmallPtrSet<Value *, 5> Inserted;
|
|
|
|
|
|
|
|
for (const auto &I : IDToValue) {
|
|
|
|
IDToValue[I.first] = NewValues[I.second];
|
|
|
|
Inserted.insert(I.second);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const auto &I : NewValues) {
|
|
|
|
if (Inserted.count(I.first))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
ValueMap[I.first] = I.second;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void IslNodeBuilder::createUserVector(__isl_take isl_ast_node *User,
|
|
|
|
std::vector<Value *> &IVS,
|
|
|
|
__isl_take isl_id *IteratorID,
|
|
|
|
__isl_take isl_union_map *Schedule) {
|
|
|
|
isl_ast_expr *Expr = isl_ast_node_user_get_expr(User);
|
|
|
|
isl_ast_expr *StmtExpr = isl_ast_expr_get_op_arg(Expr, 0);
|
|
|
|
isl_id *Id = isl_ast_expr_get_id(StmtExpr);
|
|
|
|
isl_ast_expr_free(StmtExpr);
|
|
|
|
ScopStmt *Stmt = (ScopStmt *)isl_id_get_user(Id);
|
|
|
|
Stmt->setAstBuild(IslAstInfo::getBuild(User));
|
|
|
|
VectorValueMapT VectorMap(IVS.size());
|
|
|
|
std::vector<LoopToScevMapT> VLTS(IVS.size());
|
|
|
|
|
|
|
|
isl_union_set *Domain = isl_union_set_from_set(Stmt->getDomain());
|
|
|
|
Schedule = isl_union_map_intersect_domain(Schedule, Domain);
|
|
|
|
isl_map *S = isl_map_from_union_map(Schedule);
|
|
|
|
|
|
|
|
createSubstitutionsVector(Expr, Stmt, VectorMap, VLTS, IVS, IteratorID);
|
|
|
|
VectorBlockGenerator::generate(BlockGen, *Stmt, VectorMap, VLTS, S);
|
|
|
|
|
|
|
|
isl_map_free(S);
|
|
|
|
isl_id_free(Id);
|
|
|
|
isl_ast_node_free(User);
|
|
|
|
}
|
|
|
|
|
2015-08-23 17:11:00 +08:00
|
|
|
void IslNodeBuilder::createMark(__isl_take isl_ast_node *Node) {
|
|
|
|
auto Child = isl_ast_node_mark_get_node(Node);
|
|
|
|
create(Child);
|
|
|
|
isl_ast_node_free(Node);
|
|
|
|
}
|
|
|
|
|
2015-04-27 20:32:24 +08:00
|
|
|
void IslNodeBuilder::createForVector(__isl_take isl_ast_node *For,
|
|
|
|
int VectorWidth) {
|
|
|
|
isl_ast_node *Body = isl_ast_node_for_get_body(For);
|
|
|
|
isl_ast_expr *Init = isl_ast_node_for_get_init(For);
|
|
|
|
isl_ast_expr *Inc = isl_ast_node_for_get_inc(For);
|
|
|
|
isl_ast_expr *Iterator = isl_ast_node_for_get_iterator(For);
|
|
|
|
isl_id *IteratorID = isl_ast_expr_get_id(Iterator);
|
|
|
|
|
|
|
|
Value *ValueLB = ExprBuilder.create(Init);
|
|
|
|
Value *ValueInc = ExprBuilder.create(Inc);
|
|
|
|
|
|
|
|
Type *MaxType = ExprBuilder.getType(Iterator);
|
|
|
|
MaxType = ExprBuilder.getWidestType(MaxType, ValueLB->getType());
|
|
|
|
MaxType = ExprBuilder.getWidestType(MaxType, ValueInc->getType());
|
|
|
|
|
|
|
|
if (MaxType != ValueLB->getType())
|
|
|
|
ValueLB = Builder.CreateSExt(ValueLB, MaxType);
|
|
|
|
if (MaxType != ValueInc->getType())
|
|
|
|
ValueInc = Builder.CreateSExt(ValueInc, MaxType);
|
|
|
|
|
|
|
|
std::vector<Value *> IVS(VectorWidth);
|
|
|
|
IVS[0] = ValueLB;
|
|
|
|
|
|
|
|
for (int i = 1; i < VectorWidth; i++)
|
|
|
|
IVS[i] = Builder.CreateAdd(IVS[i - 1], ValueInc, "p_vector_iv");
|
|
|
|
|
|
|
|
isl_union_map *Schedule = IslAstInfo::getSchedule(For);
|
|
|
|
assert(Schedule && "For statement annotation does not contain its schedule");
|
|
|
|
|
|
|
|
IDToValue[IteratorID] = ValueLB;
|
|
|
|
|
|
|
|
switch (isl_ast_node_get_type(Body)) {
|
|
|
|
case isl_ast_node_user:
|
|
|
|
createUserVector(Body, IVS, isl_id_copy(IteratorID),
|
|
|
|
isl_union_map_copy(Schedule));
|
|
|
|
break;
|
|
|
|
case isl_ast_node_block: {
|
|
|
|
isl_ast_node_list *List = isl_ast_node_block_get_children(Body);
|
|
|
|
|
|
|
|
for (int i = 0; i < isl_ast_node_list_n_ast_node(List); ++i)
|
|
|
|
createUserVector(isl_ast_node_list_get_ast_node(List, i), IVS,
|
|
|
|
isl_id_copy(IteratorID), isl_union_map_copy(Schedule));
|
|
|
|
|
|
|
|
isl_ast_node_free(Body);
|
|
|
|
isl_ast_node_list_free(List);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
isl_ast_node_dump(Body);
|
|
|
|
llvm_unreachable("Unhandled isl_ast_node in vectorizer");
|
|
|
|
}
|
|
|
|
|
|
|
|
IDToValue.erase(IDToValue.find(IteratorID));
|
|
|
|
isl_id_free(IteratorID);
|
|
|
|
isl_union_map_free(Schedule);
|
|
|
|
|
|
|
|
isl_ast_node_free(For);
|
|
|
|
isl_ast_expr_free(Iterator);
|
|
|
|
}
|
|
|
|
|
|
|
|
void IslNodeBuilder::createForSequential(__isl_take isl_ast_node *For) {
|
|
|
|
isl_ast_node *Body;
|
|
|
|
isl_ast_expr *Init, *Inc, *Iterator, *UB;
|
|
|
|
isl_id *IteratorID;
|
|
|
|
Value *ValueLB, *ValueUB, *ValueInc;
|
|
|
|
Type *MaxType;
|
|
|
|
BasicBlock *ExitBlock;
|
|
|
|
Value *IV;
|
|
|
|
CmpInst::Predicate Predicate;
|
|
|
|
bool Parallel;
|
|
|
|
|
|
|
|
Parallel =
|
|
|
|
IslAstInfo::isParallel(For) && !IslAstInfo::isReductionParallel(For);
|
|
|
|
|
|
|
|
Body = isl_ast_node_for_get_body(For);
|
|
|
|
|
|
|
|
// isl_ast_node_for_is_degenerate(For)
|
|
|
|
//
|
|
|
|
// TODO: For degenerated loops we could generate a plain assignment.
|
|
|
|
// However, for now we just reuse the logic for normal loops, which will
|
|
|
|
// create a loop with a single iteration.
|
|
|
|
|
|
|
|
Init = isl_ast_node_for_get_init(For);
|
|
|
|
Inc = isl_ast_node_for_get_inc(For);
|
|
|
|
Iterator = isl_ast_node_for_get_iterator(For);
|
|
|
|
IteratorID = isl_ast_expr_get_id(Iterator);
|
|
|
|
UB = getUpperBound(For, Predicate);
|
|
|
|
|
|
|
|
ValueLB = ExprBuilder.create(Init);
|
|
|
|
ValueUB = ExprBuilder.create(UB);
|
|
|
|
ValueInc = ExprBuilder.create(Inc);
|
|
|
|
|
|
|
|
MaxType = ExprBuilder.getType(Iterator);
|
|
|
|
MaxType = ExprBuilder.getWidestType(MaxType, ValueLB->getType());
|
|
|
|
MaxType = ExprBuilder.getWidestType(MaxType, ValueUB->getType());
|
|
|
|
MaxType = ExprBuilder.getWidestType(MaxType, ValueInc->getType());
|
|
|
|
|
|
|
|
if (MaxType != ValueLB->getType())
|
|
|
|
ValueLB = Builder.CreateSExt(ValueLB, MaxType);
|
|
|
|
if (MaxType != ValueUB->getType())
|
|
|
|
ValueUB = Builder.CreateSExt(ValueUB, MaxType);
|
|
|
|
if (MaxType != ValueInc->getType())
|
|
|
|
ValueInc = Builder.CreateSExt(ValueInc, MaxType);
|
|
|
|
|
|
|
|
// If we can show that LB <Predicate> UB holds at least once, we can
|
|
|
|
// omit the GuardBB in front of the loop.
|
|
|
|
bool UseGuardBB =
|
|
|
|
!SE.isKnownPredicate(Predicate, SE.getSCEV(ValueLB), SE.getSCEV(ValueUB));
|
|
|
|
IV = createLoop(ValueLB, ValueUB, ValueInc, Builder, P, LI, DT, ExitBlock,
|
|
|
|
Predicate, &Annotator, Parallel, UseGuardBB);
|
|
|
|
IDToValue[IteratorID] = IV;
|
|
|
|
|
|
|
|
create(Body);
|
|
|
|
|
|
|
|
Annotator.popLoop(Parallel);
|
|
|
|
|
|
|
|
IDToValue.erase(IDToValue.find(IteratorID));
|
|
|
|
|
|
|
|
Builder.SetInsertPoint(ExitBlock->begin());
|
|
|
|
|
|
|
|
isl_ast_node_free(For);
|
|
|
|
isl_ast_expr_free(Iterator);
|
|
|
|
isl_id_free(IteratorID);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// @brief Remove the BBs contained in a (sub)function from the dominator tree.
|
|
|
|
///
|
|
|
|
/// This function removes the basic blocks that are part of a subfunction from
|
|
|
|
/// the dominator tree. Specifically, when generating code it may happen that at
|
|
|
|
/// some point the code generation continues in a new sub-function (e.g., when
|
|
|
|
/// generating OpenMP code). The basic blocks that are created in this
|
|
|
|
/// sub-function are then still part of the dominator tree of the original
|
|
|
|
/// function, such that the dominator tree reaches over function boundaries.
|
|
|
|
/// This is not only incorrect, but also causes crashes. This function now
|
|
|
|
/// removes from the dominator tree all basic blocks that are dominated (and
|
|
|
|
/// consequently reachable) from the entry block of this (sub)function.
|
|
|
|
///
|
|
|
|
/// FIXME: A LLVM (function or region) pass should not touch anything outside of
|
|
|
|
/// the function/region it runs on. Hence, the pure need for this function shows
|
|
|
|
/// that we do not comply to this rule. At the moment, this does not cause any
|
|
|
|
/// issues, but we should be aware that such issues may appear. Unfortunately
|
|
|
|
/// the current LLVM pass infrastructure does not allow to make Polly a module
|
|
|
|
/// or call-graph pass to solve this issue, as such a pass would not have access
|
|
|
|
/// to the per-function analyses passes needed by Polly. A future pass manager
|
|
|
|
/// infrastructure is supposed to enable such kind of access possibly allowing
|
|
|
|
/// us to create a cleaner solution here.
|
|
|
|
///
|
|
|
|
/// FIXME: Instead of adding the dominance information and then dropping it
|
|
|
|
/// later on, we should try to just not add it in the first place. This requires
|
|
|
|
/// some careful testing to make sure this does not break in interaction with
|
|
|
|
/// the SCEVBuilder and SplitBlock which may rely on the dominator tree or
|
|
|
|
/// which may try to update it.
|
|
|
|
///
|
|
|
|
/// @param F The function which contains the BBs to removed.
|
|
|
|
/// @param DT The dominator tree from which to remove the BBs.
|
|
|
|
static void removeSubFuncFromDomTree(Function *F, DominatorTree &DT) {
|
|
|
|
DomTreeNode *N = DT.getNode(&F->getEntryBlock());
|
|
|
|
std::vector<BasicBlock *> Nodes;
|
|
|
|
|
|
|
|
// We can only remove an element from the dominator tree, if all its children
|
|
|
|
// have been removed. To ensure this we obtain the list of nodes to remove
|
|
|
|
// using a post-order tree traversal.
|
|
|
|
for (po_iterator<DomTreeNode *> I = po_begin(N), E = po_end(N); I != E; ++I)
|
|
|
|
Nodes.push_back(I->getBlock());
|
|
|
|
|
|
|
|
for (BasicBlock *BB : Nodes)
|
|
|
|
DT.eraseNode(BB);
|
|
|
|
}
|
|
|
|
|
|
|
|
void IslNodeBuilder::createForParallel(__isl_take isl_ast_node *For) {
|
|
|
|
isl_ast_node *Body;
|
|
|
|
isl_ast_expr *Init, *Inc, *Iterator, *UB;
|
|
|
|
isl_id *IteratorID;
|
|
|
|
Value *ValueLB, *ValueUB, *ValueInc;
|
|
|
|
Type *MaxType;
|
|
|
|
Value *IV;
|
|
|
|
CmpInst::Predicate Predicate;
|
|
|
|
|
|
|
|
Body = isl_ast_node_for_get_body(For);
|
|
|
|
Init = isl_ast_node_for_get_init(For);
|
|
|
|
Inc = isl_ast_node_for_get_inc(For);
|
|
|
|
Iterator = isl_ast_node_for_get_iterator(For);
|
|
|
|
IteratorID = isl_ast_expr_get_id(Iterator);
|
|
|
|
UB = getUpperBound(For, Predicate);
|
|
|
|
|
|
|
|
ValueLB = ExprBuilder.create(Init);
|
|
|
|
ValueUB = ExprBuilder.create(UB);
|
|
|
|
ValueInc = ExprBuilder.create(Inc);
|
|
|
|
|
|
|
|
// OpenMP always uses SLE. In case the isl generated AST uses a SLT
|
|
|
|
// expression, we need to adjust the loop blound by one.
|
|
|
|
if (Predicate == CmpInst::ICMP_SLT)
|
|
|
|
ValueUB = Builder.CreateAdd(
|
|
|
|
ValueUB, Builder.CreateSExt(Builder.getTrue(), ValueUB->getType()));
|
|
|
|
|
|
|
|
MaxType = ExprBuilder.getType(Iterator);
|
|
|
|
MaxType = ExprBuilder.getWidestType(MaxType, ValueLB->getType());
|
|
|
|
MaxType = ExprBuilder.getWidestType(MaxType, ValueUB->getType());
|
|
|
|
MaxType = ExprBuilder.getWidestType(MaxType, ValueInc->getType());
|
|
|
|
|
|
|
|
if (MaxType != ValueLB->getType())
|
|
|
|
ValueLB = Builder.CreateSExt(ValueLB, MaxType);
|
|
|
|
if (MaxType != ValueUB->getType())
|
|
|
|
ValueUB = Builder.CreateSExt(ValueUB, MaxType);
|
|
|
|
if (MaxType != ValueInc->getType())
|
|
|
|
ValueInc = Builder.CreateSExt(ValueInc, MaxType);
|
|
|
|
|
|
|
|
BasicBlock::iterator LoopBody;
|
|
|
|
|
|
|
|
SetVector<Value *> SubtreeValues;
|
|
|
|
SetVector<const Loop *> Loops;
|
|
|
|
|
|
|
|
getReferencesInSubtree(For, SubtreeValues, Loops);
|
|
|
|
|
|
|
|
// Create for all loops we depend on values that contain the current loop
|
|
|
|
// iteration. These values are necessary to generate code for SCEVs that
|
|
|
|
// depend on such loops. As a result we need to pass them to the subfunction.
|
|
|
|
for (const Loop *L : Loops) {
|
|
|
|
const SCEV *OuterLIV = SE.getAddRecExpr(SE.getUnknown(Builder.getInt64(0)),
|
|
|
|
SE.getUnknown(Builder.getInt64(1)),
|
|
|
|
L, SCEV::FlagAnyWrap);
|
|
|
|
Value *V = generateSCEV(OuterLIV);
|
|
|
|
OutsideLoopIterations[L] = SE.getUnknown(V);
|
|
|
|
SubtreeValues.insert(V);
|
|
|
|
}
|
|
|
|
|
|
|
|
ParallelLoopGenerator::ValueToValueMapTy NewValues;
|
|
|
|
ParallelLoopGenerator ParallelLoopGen(Builder, P, LI, DT, DL);
|
|
|
|
|
|
|
|
IV = ParallelLoopGen.createParallelLoop(ValueLB, ValueUB, ValueInc,
|
|
|
|
SubtreeValues, NewValues, &LoopBody);
|
|
|
|
BasicBlock::iterator AfterLoop = Builder.GetInsertPoint();
|
|
|
|
Builder.SetInsertPoint(LoopBody);
|
|
|
|
|
|
|
|
// Save the current values.
|
|
|
|
ValueMapT ValueMapCopy = ValueMap;
|
|
|
|
IslExprBuilder::IDToValueTy IDToValueCopy = IDToValue;
|
|
|
|
|
|
|
|
updateValues(NewValues);
|
|
|
|
IDToValue[IteratorID] = IV;
|
|
|
|
|
2015-08-20 00:04:35 +08:00
|
|
|
ParallelLoopGenerator::ValueToValueMapTy NewValuesReverse;
|
|
|
|
|
|
|
|
for (auto P : NewValues)
|
|
|
|
NewValuesReverse[P.second] = P.first;
|
|
|
|
|
|
|
|
Annotator.addAlternativeAliasBases(NewValuesReverse);
|
|
|
|
|
2015-04-27 20:32:24 +08:00
|
|
|
create(Body);
|
|
|
|
|
2015-08-20 00:04:35 +08:00
|
|
|
Annotator.resetAlternativeAliasBases();
|
2015-04-27 20:32:24 +08:00
|
|
|
// Restore the original values.
|
|
|
|
ValueMap = ValueMapCopy;
|
|
|
|
IDToValue = IDToValueCopy;
|
|
|
|
|
|
|
|
Builder.SetInsertPoint(AfterLoop);
|
|
|
|
removeSubFuncFromDomTree((*LoopBody).getParent()->getParent(), DT);
|
|
|
|
|
|
|
|
for (const Loop *L : Loops)
|
|
|
|
OutsideLoopIterations.erase(L);
|
|
|
|
|
|
|
|
isl_ast_node_free(For);
|
|
|
|
isl_ast_expr_free(Iterator);
|
|
|
|
isl_id_free(IteratorID);
|
|
|
|
}
|
|
|
|
|
|
|
|
void IslNodeBuilder::createFor(__isl_take isl_ast_node *For) {
|
|
|
|
bool Vector = PollyVectorizerChoice == VECTORIZER_POLLY;
|
|
|
|
|
|
|
|
if (Vector && IslAstInfo::isInnermostParallel(For) &&
|
|
|
|
!IslAstInfo::isReductionParallel(For)) {
|
|
|
|
int VectorWidth = getNumberOfIterations(For);
|
|
|
|
if (1 < VectorWidth && VectorWidth <= 16) {
|
|
|
|
createForVector(For, VectorWidth);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (IslAstInfo::isExecutedInParallel(For)) {
|
|
|
|
createForParallel(For);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
createForSequential(For);
|
|
|
|
}
|
|
|
|
|
|
|
|
void IslNodeBuilder::createIf(__isl_take isl_ast_node *If) {
|
|
|
|
isl_ast_expr *Cond = isl_ast_node_if_get_cond(If);
|
|
|
|
|
|
|
|
Function *F = Builder.GetInsertBlock()->getParent();
|
|
|
|
LLVMContext &Context = F->getContext();
|
|
|
|
|
|
|
|
BasicBlock *CondBB =
|
|
|
|
SplitBlock(Builder.GetInsertBlock(), Builder.GetInsertPoint(), &DT, &LI);
|
|
|
|
CondBB->setName("polly.cond");
|
|
|
|
BasicBlock *MergeBB = SplitBlock(CondBB, CondBB->begin(), &DT, &LI);
|
|
|
|
MergeBB->setName("polly.merge");
|
|
|
|
BasicBlock *ThenBB = BasicBlock::Create(Context, "polly.then", F);
|
|
|
|
BasicBlock *ElseBB = BasicBlock::Create(Context, "polly.else", F);
|
|
|
|
|
|
|
|
DT.addNewBlock(ThenBB, CondBB);
|
|
|
|
DT.addNewBlock(ElseBB, CondBB);
|
|
|
|
DT.changeImmediateDominator(MergeBB, CondBB);
|
|
|
|
|
|
|
|
Loop *L = LI.getLoopFor(CondBB);
|
|
|
|
if (L) {
|
|
|
|
L->addBasicBlockToLoop(ThenBB, LI);
|
|
|
|
L->addBasicBlockToLoop(ElseBB, LI);
|
|
|
|
}
|
|
|
|
|
|
|
|
CondBB->getTerminator()->eraseFromParent();
|
|
|
|
|
|
|
|
Builder.SetInsertPoint(CondBB);
|
|
|
|
Value *Predicate = ExprBuilder.create(Cond);
|
|
|
|
Builder.CreateCondBr(Predicate, ThenBB, ElseBB);
|
|
|
|
Builder.SetInsertPoint(ThenBB);
|
|
|
|
Builder.CreateBr(MergeBB);
|
|
|
|
Builder.SetInsertPoint(ElseBB);
|
|
|
|
Builder.CreateBr(MergeBB);
|
|
|
|
Builder.SetInsertPoint(ThenBB->begin());
|
|
|
|
|
|
|
|
create(isl_ast_node_if_get_then(If));
|
|
|
|
|
|
|
|
Builder.SetInsertPoint(ElseBB->begin());
|
|
|
|
|
|
|
|
if (isl_ast_node_if_has_else(If))
|
|
|
|
create(isl_ast_node_if_get_else(If));
|
|
|
|
|
|
|
|
Builder.SetInsertPoint(MergeBB->begin());
|
|
|
|
|
|
|
|
isl_ast_node_free(If);
|
|
|
|
}
|
|
|
|
|
|
|
|
void IslNodeBuilder::createSubstitutions(isl_ast_expr *Expr, ScopStmt *Stmt,
|
|
|
|
ValueMapT &VMap, LoopToScevMapT <S) {
|
|
|
|
assert(isl_ast_expr_get_type(Expr) == isl_ast_expr_op &&
|
|
|
|
"Expression of type 'op' expected");
|
|
|
|
assert(isl_ast_expr_get_op_type(Expr) == isl_ast_op_call &&
|
|
|
|
"Opertation of type 'call' expected");
|
|
|
|
for (int i = 0; i < isl_ast_expr_get_op_n_arg(Expr) - 1; ++i) {
|
|
|
|
isl_ast_expr *SubExpr;
|
|
|
|
Value *V;
|
|
|
|
|
|
|
|
SubExpr = isl_ast_expr_get_op_arg(Expr, i + 1);
|
|
|
|
V = ExprBuilder.create(SubExpr);
|
|
|
|
ScalarEvolution *SE = Stmt->getParent()->getSE();
|
|
|
|
LTS[Stmt->getLoopForDimension(i)] = SE->getUnknown(V);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the current ValueMap to our per-statement value map.
|
|
|
|
//
|
|
|
|
// This is needed e.g. to rewrite array base addresses when moving code
|
|
|
|
// into a parallely executed subfunction.
|
|
|
|
VMap.insert(ValueMap.begin(), ValueMap.end());
|
|
|
|
|
|
|
|
isl_ast_expr_free(Expr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void IslNodeBuilder::createSubstitutionsVector(
|
|
|
|
__isl_take isl_ast_expr *Expr, ScopStmt *Stmt, VectorValueMapT &VMap,
|
|
|
|
std::vector<LoopToScevMapT> &VLTS, std::vector<Value *> &IVS,
|
|
|
|
__isl_take isl_id *IteratorID) {
|
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
Value *OldValue = IDToValue[IteratorID];
|
|
|
|
for (Value *IV : IVS) {
|
|
|
|
IDToValue[IteratorID] = IV;
|
|
|
|
createSubstitutions(isl_ast_expr_copy(Expr), Stmt, VMap[i], VLTS[i]);
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
IDToValue[IteratorID] = OldValue;
|
|
|
|
isl_id_free(IteratorID);
|
|
|
|
isl_ast_expr_free(Expr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void IslNodeBuilder::createUser(__isl_take isl_ast_node *User) {
|
|
|
|
ValueMapT VMap;
|
|
|
|
LoopToScevMapT LTS;
|
|
|
|
isl_id *Id;
|
|
|
|
ScopStmt *Stmt;
|
|
|
|
|
|
|
|
isl_ast_expr *Expr = isl_ast_node_user_get_expr(User);
|
|
|
|
isl_ast_expr *StmtExpr = isl_ast_expr_get_op_arg(Expr, 0);
|
|
|
|
Id = isl_ast_expr_get_id(StmtExpr);
|
|
|
|
isl_ast_expr_free(StmtExpr);
|
|
|
|
|
|
|
|
LTS.insert(OutsideLoopIterations.begin(), OutsideLoopIterations.end());
|
|
|
|
|
|
|
|
Stmt = (ScopStmt *)isl_id_get_user(Id);
|
|
|
|
Stmt->setAstBuild(IslAstInfo::getBuild(User));
|
|
|
|
|
|
|
|
createSubstitutions(Expr, Stmt, VMap, LTS);
|
|
|
|
if (Stmt->isBlockStmt())
|
|
|
|
BlockGen.copyStmt(*Stmt, VMap, LTS);
|
|
|
|
else
|
|
|
|
RegionGen.copyStmt(*Stmt, VMap, LTS);
|
|
|
|
|
|
|
|
isl_ast_node_free(User);
|
|
|
|
isl_id_free(Id);
|
|
|
|
}
|
|
|
|
|
|
|
|
void IslNodeBuilder::createBlock(__isl_take isl_ast_node *Block) {
|
|
|
|
isl_ast_node_list *List = isl_ast_node_block_get_children(Block);
|
|
|
|
|
|
|
|
for (int i = 0; i < isl_ast_node_list_n_ast_node(List); ++i)
|
|
|
|
create(isl_ast_node_list_get_ast_node(List, i));
|
|
|
|
|
|
|
|
isl_ast_node_free(Block);
|
|
|
|
isl_ast_node_list_free(List);
|
|
|
|
}
|
|
|
|
|
|
|
|
void IslNodeBuilder::create(__isl_take isl_ast_node *Node) {
|
|
|
|
switch (isl_ast_node_get_type(Node)) {
|
|
|
|
case isl_ast_node_error:
|
|
|
|
llvm_unreachable("code generation error");
|
|
|
|
case isl_ast_node_mark:
|
2015-08-23 17:11:00 +08:00
|
|
|
createMark(Node);
|
|
|
|
return;
|
2015-04-27 20:32:24 +08:00
|
|
|
case isl_ast_node_for:
|
|
|
|
createFor(Node);
|
|
|
|
return;
|
|
|
|
case isl_ast_node_if:
|
|
|
|
createIf(Node);
|
|
|
|
return;
|
|
|
|
case isl_ast_node_user:
|
|
|
|
createUser(Node);
|
|
|
|
return;
|
|
|
|
case isl_ast_node_block:
|
|
|
|
createBlock(Node);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm_unreachable("Unknown isl_ast_node type");
|
|
|
|
}
|
|
|
|
|
|
|
|
void IslNodeBuilder::addParameters(__isl_take isl_set *Context) {
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < isl_set_dim(Context, isl_dim_param); ++i) {
|
|
|
|
isl_id *Id;
|
|
|
|
|
|
|
|
Id = isl_set_get_dim_id(Context, isl_dim_param, i);
|
|
|
|
IDToValue[Id] = generateSCEV((const SCEV *)isl_id_get_user(Id));
|
|
|
|
|
|
|
|
isl_id_free(Id);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generate values for the current loop iteration for all surrounding loops.
|
|
|
|
//
|
|
|
|
// We may also reference loops outside of the scop which do not contain the
|
|
|
|
// scop itself, but as the number of such scops may be arbitrarily large we do
|
|
|
|
// not generate code for them here, but only at the point of code generation
|
|
|
|
// where these values are needed.
|
|
|
|
Region &R = S.getRegion();
|
|
|
|
Loop *L = LI.getLoopFor(R.getEntry());
|
|
|
|
|
|
|
|
while (L != nullptr && R.contains(L))
|
|
|
|
L = L->getParentLoop();
|
|
|
|
|
|
|
|
while (L != nullptr) {
|
|
|
|
const SCEV *OuterLIV = SE.getAddRecExpr(SE.getUnknown(Builder.getInt64(0)),
|
|
|
|
SE.getUnknown(Builder.getInt64(1)),
|
|
|
|
L, SCEV::FlagAnyWrap);
|
|
|
|
Value *V = generateSCEV(OuterLIV);
|
|
|
|
OutsideLoopIterations[L] = SE.getUnknown(V);
|
|
|
|
L = L->getParentLoop();
|
|
|
|
}
|
|
|
|
|
|
|
|
isl_set_free(Context);
|
|
|
|
}
|
|
|
|
|
|
|
|
Value *IslNodeBuilder::generateSCEV(const SCEV *Expr) {
|
|
|
|
Instruction *InsertLocation = --(Builder.GetInsertBlock()->end());
|
2015-08-18 19:56:00 +08:00
|
|
|
return expandCodeFor(S, SE, DL, "polly", Expr, Expr->getType(),
|
|
|
|
InsertLocation);
|
2015-04-27 20:32:24 +08:00
|
|
|
}
|