2012-04-25 21:16:49 +08:00
|
|
|
//===--- BlockGenerators.cpp - Generate code for statements -----*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements the BlockGenerator and VectorBlockGenerator classes,
|
|
|
|
// which generate sequential code and vectorized code for a polyhedral
|
|
|
|
// statement, respectively.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "polly/ScopInfo.h"
|
2012-04-25 21:18:28 +08:00
|
|
|
#include "polly/CodeGen/BlockGenerators.h"
|
2013-05-07 16:11:54 +08:00
|
|
|
#include "polly/CodeGen/CodeGeneration.h"
|
2014-08-03 09:51:59 +08:00
|
|
|
#include "polly/CodeGen/IslExprBuilder.h"
|
2015-09-06 16:47:57 +08:00
|
|
|
#include "polly/CodeGen/RuntimeDebugBuilder.h"
|
2013-05-07 15:31:10 +08:00
|
|
|
#include "polly/Options.h"
|
2012-04-25 21:16:49 +08:00
|
|
|
#include "polly/Support/GICHelper.h"
|
2013-03-19 04:21:13 +08:00
|
|
|
#include "polly/Support/SCEVValidator.h"
|
2013-03-21 02:03:18 +08:00
|
|
|
#include "polly/Support/ScopHelper.h"
|
2012-04-28 00:36:14 +08:00
|
|
|
#include "llvm/Analysis/LoopInfo.h"
|
2015-03-02 02:45:58 +08:00
|
|
|
#include "llvm/Analysis/RegionInfo.h"
|
2012-04-28 00:36:14 +08:00
|
|
|
#include "llvm/Analysis/ScalarEvolution.h"
|
2014-02-21 23:06:05 +08:00
|
|
|
#include "llvm/IR/IntrinsicInst.h"
|
2015-03-10 23:24:33 +08:00
|
|
|
#include "llvm/IR/Module.h"
|
2012-04-25 21:16:49 +08:00
|
|
|
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
|
2015-09-27 19:17:22 +08:00
|
|
|
#include "llvm/Transforms/Utils/Local.h"
|
2015-03-02 02:45:58 +08:00
|
|
|
#include "isl/aff.h"
|
|
|
|
#include "isl/ast.h"
|
|
|
|
#include "isl/ast_build.h"
|
2015-05-09 17:13:42 +08:00
|
|
|
#include "isl/set.h"
|
2015-03-02 02:45:58 +08:00
|
|
|
#include <deque>
|
|
|
|
|
2012-04-25 21:16:49 +08:00
|
|
|
using namespace llvm;
|
|
|
|
using namespace polly;
|
|
|
|
|
2014-10-23 07:22:41 +08:00
|
|
|
static cl::opt<bool> Aligned("enable-polly-aligned",
|
|
|
|
cl::desc("Assumed aligned memory accesses."),
|
|
|
|
cl::Hidden, cl::init(false), cl::ZeroOrMore,
|
|
|
|
cl::cat(PollyCategory));
|
2012-04-25 21:16:49 +08:00
|
|
|
|
2015-09-06 16:47:57 +08:00
|
|
|
static cl::opt<bool> DebugPrinting(
|
|
|
|
"polly-codegen-add-debug-printing",
|
|
|
|
cl::desc("Add printf calls that show the values loaded/stored."),
|
|
|
|
cl::Hidden, cl::init(false), cl::ZeroOrMore, cl::cat(PollyCategory));
|
|
|
|
|
2015-08-04 21:54:20 +08:00
|
|
|
bool polly::canSynthesize(const Value *V, const llvm::LoopInfo *LI,
|
2013-03-21 02:03:18 +08:00
|
|
|
ScalarEvolution *SE, const Region *R) {
|
2015-08-04 21:54:20 +08:00
|
|
|
if (!V || !SE->isSCEVable(V->getType()))
|
2013-03-21 02:03:18 +08:00
|
|
|
return false;
|
|
|
|
|
2015-08-04 21:54:20 +08:00
|
|
|
if (const SCEV *Scev = SE->getSCEV(const_cast<Value *>(V)))
|
2014-11-30 22:33:31 +08:00
|
|
|
if (!isa<SCEVCouldNotCompute>(Scev))
|
|
|
|
if (!hasScalarDepsInsideRegion(Scev, R))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
2013-03-21 02:03:18 +08:00
|
|
|
}
|
|
|
|
|
2015-01-26 23:55:54 +08:00
|
|
|
bool polly::isIgnoredIntrinsic(const Value *V) {
|
|
|
|
if (auto *IT = dyn_cast<IntrinsicInst>(V)) {
|
|
|
|
switch (IT->getIntrinsicID()) {
|
|
|
|
// Lifetime markers are supported/ignored.
|
|
|
|
case llvm::Intrinsic::lifetime_start:
|
|
|
|
case llvm::Intrinsic::lifetime_end:
|
|
|
|
// Invariant markers are supported/ignored.
|
|
|
|
case llvm::Intrinsic::invariant_start:
|
|
|
|
case llvm::Intrinsic::invariant_end:
|
|
|
|
// Some misc annotations are supported/ignored.
|
|
|
|
case llvm::Intrinsic::var_annotation:
|
|
|
|
case llvm::Intrinsic::ptr_annotation:
|
|
|
|
case llvm::Intrinsic::annotation:
|
|
|
|
case llvm::Intrinsic::donothing:
|
|
|
|
case llvm::Intrinsic::assume:
|
|
|
|
case llvm::Intrinsic::expect:
|
2015-08-31 00:57:20 +08:00
|
|
|
// Some debug info intrisics are supported/ignored.
|
|
|
|
case llvm::Intrinsic::dbg_value:
|
|
|
|
case llvm::Intrinsic::dbg_declare:
|
2015-01-26 23:55:54 +08:00
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-02-23 21:51:35 +08:00
|
|
|
BlockGenerator::BlockGenerator(PollyIRBuilder &B, LoopInfo &LI,
|
|
|
|
ScalarEvolution &SE, DominatorTree &DT,
|
2015-05-23 07:43:58 +08:00
|
|
|
ScalarAllocaMapTy &ScalarMap,
|
|
|
|
ScalarAllocaMapTy &PHIOpMap,
|
|
|
|
EscapeUsersAllocaMapTy &EscapeMap,
|
2015-09-05 17:56:54 +08:00
|
|
|
ValueToValueMap &GlobalMap,
|
2015-02-23 21:51:35 +08:00
|
|
|
IslExprBuilder *ExprBuilder)
|
2015-05-23 07:43:58 +08:00
|
|
|
: Builder(B), LI(LI), SE(SE), ExprBuilder(ExprBuilder), DT(DT),
|
|
|
|
EntryBB(nullptr), PHIOpMap(PHIOpMap), ScalarMap(ScalarMap),
|
2015-09-05 17:56:54 +08:00
|
|
|
EscapeMap(EscapeMap), GlobalMap(GlobalMap) {}
|
2015-02-07 05:39:31 +08:00
|
|
|
|
|
|
|
Value *BlockGenerator::getNewValue(ScopStmt &Stmt, const Value *Old,
|
2015-09-05 17:56:54 +08:00
|
|
|
ValueMapT &BBMap, LoopToScevMapT <S,
|
|
|
|
Loop *L) const {
|
2012-04-25 21:16:49 +08:00
|
|
|
// We assume constants never change.
|
|
|
|
// This avoids map lookups for many calls to this function.
|
|
|
|
if (isa<Constant>(Old))
|
2013-02-06 02:01:29 +08:00
|
|
|
return const_cast<Value *>(Old);
|
2012-04-25 21:16:49 +08:00
|
|
|
|
2013-06-29 21:22:15 +08:00
|
|
|
if (Value *New = GlobalMap.lookup(Old)) {
|
2015-09-30 07:47:21 +08:00
|
|
|
if (Value *NewRemapped = GlobalMap.lookup(New))
|
|
|
|
New = NewRemapped;
|
2013-02-06 02:01:29 +08:00
|
|
|
if (Old->getType()->getScalarSizeInBits() <
|
2013-04-10 14:55:45 +08:00
|
|
|
New->getType()->getScalarSizeInBits())
|
2012-04-25 21:16:49 +08:00
|
|
|
New = Builder.CreateTruncOrBitCast(New, Old->getType());
|
|
|
|
|
|
|
|
return New;
|
|
|
|
}
|
|
|
|
|
2013-06-29 21:22:15 +08:00
|
|
|
if (Value *New = BBMap.lookup(Old))
|
|
|
|
return New;
|
2012-04-25 21:16:49 +08:00
|
|
|
|
2014-11-30 22:33:31 +08:00
|
|
|
if (SE.isSCEVable(Old->getType()))
|
2013-03-23 07:42:53 +08:00
|
|
|
if (const SCEV *Scev = SE.getSCEVAtScope(const_cast<Value *>(Old), L)) {
|
2012-04-28 00:36:14 +08:00
|
|
|
if (!isa<SCEVCouldNotCompute>(Scev)) {
|
2013-02-16 04:56:01 +08:00
|
|
|
const SCEV *NewScev = apply(Scev, LTS, SE);
|
|
|
|
ValueToValueMap VTV;
|
|
|
|
VTV.insert(BBMap.begin(), BBMap.end());
|
|
|
|
VTV.insert(GlobalMap.begin(), GlobalMap.end());
|
2013-02-16 05:26:53 +08:00
|
|
|
NewScev = SCEVParameterRewriter::rewrite(NewScev, SE, VTV);
|
2015-08-18 19:56:00 +08:00
|
|
|
|
|
|
|
Scop &S = *Stmt.getParent();
|
|
|
|
const DataLayout &DL =
|
|
|
|
S.getRegion().getEntry()->getParent()->getParent()->getDataLayout();
|
|
|
|
auto IP = Builder.GetInsertPoint();
|
|
|
|
|
|
|
|
assert(IP != Builder.GetInsertBlock()->end() &&
|
2015-08-01 17:07:57 +08:00
|
|
|
"Only instructions can be insert points for SCEVExpander");
|
2015-08-18 19:56:00 +08:00
|
|
|
Value *Expanded =
|
|
|
|
expandCodeFor(S, SE, DL, "polly", NewScev, Old->getType(), IP);
|
2012-04-28 00:36:14 +08:00
|
|
|
|
|
|
|
BBMap[Old] = Expanded;
|
|
|
|
return Expanded;
|
|
|
|
}
|
2013-03-23 07:42:53 +08:00
|
|
|
}
|
2012-04-28 00:36:14 +08:00
|
|
|
|
2014-11-06 04:48:56 +08:00
|
|
|
// A scop-constant value defined by a global or a function parameter.
|
|
|
|
if (isa<GlobalValue>(Old) || isa<Argument>(Old))
|
|
|
|
return const_cast<Value *>(Old);
|
|
|
|
|
|
|
|
// A scop-constant value defined by an instruction executed outside the scop.
|
|
|
|
if (const Instruction *Inst = dyn_cast<Instruction>(Old))
|
2015-02-07 05:39:31 +08:00
|
|
|
if (!Stmt.getParent()->getRegion().contains(Inst->getParent()))
|
2014-11-06 04:48:56 +08:00
|
|
|
return const_cast<Value *>(Old);
|
|
|
|
|
|
|
|
// The scalar dependence is neither available nor SCEVCodegenable.
|
2013-07-25 17:12:07 +08:00
|
|
|
llvm_unreachable("Unexpected scalar dependence in region!");
|
2014-04-16 15:33:47 +08:00
|
|
|
return nullptr;
|
2012-04-25 21:16:49 +08:00
|
|
|
}
|
|
|
|
|
2015-02-07 05:39:31 +08:00
|
|
|
void BlockGenerator::copyInstScalar(ScopStmt &Stmt, const Instruction *Inst,
|
2015-09-05 17:56:54 +08:00
|
|
|
ValueMapT &BBMap, LoopToScevMapT <S) {
|
2014-02-21 23:06:05 +08:00
|
|
|
// We do not generate debug intrinsics as we did not investigate how to
|
|
|
|
// copy them correctly. At the current state, they just crash the code
|
|
|
|
// generation as the meta-data operands are not correctly copied.
|
|
|
|
if (isa<DbgInfoIntrinsic>(Inst))
|
|
|
|
return;
|
|
|
|
|
2012-04-25 21:16:49 +08:00
|
|
|
Instruction *NewInst = Inst->clone();
|
|
|
|
|
|
|
|
// Replace old operands with the new ones.
|
2014-06-04 16:06:40 +08:00
|
|
|
for (Value *OldOperand : Inst->operands()) {
|
2015-09-05 17:56:54 +08:00
|
|
|
Value *NewOperand =
|
|
|
|
getNewValue(Stmt, OldOperand, BBMap, LTS, getLoopForInst(Inst));
|
2012-04-25 21:16:49 +08:00
|
|
|
|
|
|
|
if (!NewOperand) {
|
2013-02-06 02:01:29 +08:00
|
|
|
assert(!isa<StoreInst>(NewInst) &&
|
|
|
|
"Store instructions are always needed!");
|
2012-04-25 21:16:49 +08:00
|
|
|
delete NewInst;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
NewInst->replaceUsesOfWith(OldOperand, NewOperand);
|
|
|
|
}
|
|
|
|
|
|
|
|
Builder.Insert(NewInst);
|
|
|
|
BBMap[Inst] = NewInst;
|
|
|
|
|
|
|
|
if (!NewInst->getType()->isVoidTy())
|
|
|
|
NewInst->setName("p_" + Inst->getName());
|
|
|
|
}
|
|
|
|
|
2015-02-07 05:39:31 +08:00
|
|
|
Value *BlockGenerator::generateLocationAccessed(
|
|
|
|
ScopStmt &Stmt, const Instruction *Inst, const Value *Pointer,
|
2015-09-05 17:56:54 +08:00
|
|
|
ValueMapT &BBMap, LoopToScevMapT <S, isl_id_to_ast_expr *NewAccesses) {
|
2015-02-07 05:39:31 +08:00
|
|
|
const MemoryAccess &MA = Stmt.getAccessFor(Inst);
|
2012-04-25 21:16:49 +08:00
|
|
|
|
2015-08-27 15:28:16 +08:00
|
|
|
isl_ast_expr *AccessExpr = isl_id_to_ast_expr_get(NewAccesses, MA.getId());
|
2012-04-25 21:16:49 +08:00
|
|
|
|
2015-08-27 15:28:16 +08:00
|
|
|
if (AccessExpr) {
|
|
|
|
AccessExpr = isl_ast_expr_address_of(AccessExpr);
|
2015-09-29 14:44:38 +08:00
|
|
|
auto Address = ExprBuilder->create(AccessExpr);
|
|
|
|
|
|
|
|
// Cast the address of this memory access to a pointer type that has the
|
|
|
|
// same element type as the original access, but uses the address space of
|
|
|
|
// the newly generated pointer.
|
|
|
|
auto OldPtrTy = MA.getAccessValue()->getType()->getPointerTo();
|
|
|
|
auto NewPtrTy = Address->getType();
|
|
|
|
OldPtrTy = PointerType::get(OldPtrTy->getElementType(),
|
|
|
|
NewPtrTy->getPointerAddressSpace());
|
|
|
|
|
|
|
|
if (OldPtrTy != NewPtrTy) {
|
|
|
|
assert(OldPtrTy->getPointerElementType()->getPrimitiveSizeInBits() ==
|
|
|
|
NewPtrTy->getPointerElementType()->getPrimitiveSizeInBits() &&
|
|
|
|
"Pointer types to elements with different size found");
|
|
|
|
Address = Builder.CreateBitOrPointerCast(Address, OldPtrTy);
|
|
|
|
}
|
|
|
|
return Address;
|
2015-08-27 15:28:16 +08:00
|
|
|
}
|
|
|
|
|
2015-09-05 17:56:54 +08:00
|
|
|
return getNewValue(Stmt, Pointer, BBMap, LTS, getLoopForInst(Inst));
|
2012-04-25 21:16:49 +08:00
|
|
|
}
|
|
|
|
|
2013-03-23 09:05:07 +08:00
|
|
|
Loop *BlockGenerator::getLoopForInst(const llvm::Instruction *Inst) {
|
2014-08-08 01:14:54 +08:00
|
|
|
return LI.getLoopFor(Inst->getParent());
|
2013-03-23 07:42:53 +08:00
|
|
|
}
|
|
|
|
|
2015-02-07 05:39:31 +08:00
|
|
|
Value *BlockGenerator::generateScalarLoad(ScopStmt &Stmt, const LoadInst *Load,
|
2015-09-05 17:56:54 +08:00
|
|
|
ValueMapT &BBMap, LoopToScevMapT <S,
|
2015-08-27 15:28:16 +08:00
|
|
|
isl_id_to_ast_expr *NewAccesses) {
|
2015-09-30 07:47:21 +08:00
|
|
|
if (Value *PreloadLoad = GlobalMap.lookup(Load))
|
|
|
|
return PreloadLoad;
|
|
|
|
|
2012-04-25 21:16:49 +08:00
|
|
|
const Value *Pointer = Load->getPointerOperand();
|
2015-09-05 17:56:54 +08:00
|
|
|
Value *NewPointer =
|
|
|
|
generateLocationAccessed(Stmt, Load, Pointer, BBMap, LTS, NewAccesses);
|
2014-10-03 00:22:19 +08:00
|
|
|
Value *ScalarLoad = Builder.CreateAlignedLoad(
|
|
|
|
NewPointer, Load->getAlignment(), Load->getName() + "_p_scalar_");
|
2015-09-06 16:47:57 +08:00
|
|
|
|
|
|
|
if (DebugPrinting)
|
|
|
|
RuntimeDebugBuilder::createCPUPrinter(Builder, "Load from ", NewPointer,
|
|
|
|
": ", ScalarLoad, "\n");
|
|
|
|
|
2012-04-25 21:16:49 +08:00
|
|
|
return ScalarLoad;
|
|
|
|
}
|
|
|
|
|
2015-08-11 16:13:15 +08:00
|
|
|
void BlockGenerator::generateScalarStore(ScopStmt &Stmt, const StoreInst *Store,
|
2015-09-05 17:56:54 +08:00
|
|
|
ValueMapT &BBMap, LoopToScevMapT <S,
|
2015-08-27 15:28:16 +08:00
|
|
|
isl_id_to_ast_expr *NewAccesses) {
|
2012-04-25 21:16:49 +08:00
|
|
|
const Value *Pointer = Store->getPointerOperand();
|
2015-09-05 17:56:54 +08:00
|
|
|
Value *NewPointer =
|
|
|
|
generateLocationAccessed(Stmt, Store, Pointer, BBMap, LTS, NewAccesses);
|
|
|
|
Value *ValueOperand = getNewValue(Stmt, Store->getValueOperand(), BBMap, LTS,
|
|
|
|
getLoopForInst(Store));
|
2012-04-25 21:16:49 +08:00
|
|
|
|
2015-09-06 16:47:57 +08:00
|
|
|
if (DebugPrinting)
|
|
|
|
RuntimeDebugBuilder::createCPUPrinter(Builder, "Store to ", NewPointer,
|
|
|
|
": ", ValueOperand, "\n");
|
|
|
|
|
2015-08-11 16:13:15 +08:00
|
|
|
Builder.CreateAlignedStore(ValueOperand, NewPointer, Store->getAlignment());
|
2012-04-25 21:16:49 +08:00
|
|
|
}
|
|
|
|
|
2015-02-07 05:39:31 +08:00
|
|
|
void BlockGenerator::copyInstruction(ScopStmt &Stmt, const Instruction *Inst,
|
2015-09-05 17:56:54 +08:00
|
|
|
ValueMapT &BBMap, LoopToScevMapT <S,
|
2015-08-27 15:28:16 +08:00
|
|
|
isl_id_to_ast_expr *NewAccesses) {
|
2015-05-23 07:43:58 +08:00
|
|
|
|
|
|
|
// First check for possible scalar dependences for this instruction.
|
2015-09-05 17:56:54 +08:00
|
|
|
generateScalarLoads(Stmt, Inst, BBMap);
|
2015-05-23 07:43:58 +08:00
|
|
|
|
2012-04-25 21:16:49 +08:00
|
|
|
// Terminator instructions control the control flow. They are explicitly
|
|
|
|
// expressed in the clast and do not need to be copied.
|
|
|
|
if (Inst->isTerminator())
|
|
|
|
return;
|
|
|
|
|
2015-05-23 07:43:58 +08:00
|
|
|
Loop *L = getLoopForInst(Inst);
|
|
|
|
if ((Stmt.isBlockStmt() || !Stmt.getRegion()->contains(L)) &&
|
|
|
|
canSynthesize(Inst, &LI, &SE, &Stmt.getParent()->getRegion())) {
|
2015-09-30 07:43:40 +08:00
|
|
|
Value *NewValue = getNewValue(Stmt, Inst, BBMap, LTS, L);
|
|
|
|
BBMap[Inst] = NewValue;
|
2012-04-28 00:36:14 +08:00
|
|
|
return;
|
2015-05-23 07:43:58 +08:00
|
|
|
}
|
2012-04-28 00:36:14 +08:00
|
|
|
|
2012-04-25 21:16:49 +08:00
|
|
|
if (const LoadInst *Load = dyn_cast<LoadInst>(Inst)) {
|
2015-09-05 17:56:54 +08:00
|
|
|
Value *NewLoad = generateScalarLoad(Stmt, Load, BBMap, LTS, NewAccesses);
|
2013-05-25 02:46:02 +08:00
|
|
|
// Compute NewLoad before its insertion in BBMap to make the insertion
|
|
|
|
// deterministic.
|
2013-05-25 01:16:02 +08:00
|
|
|
BBMap[Load] = NewLoad;
|
2012-04-25 21:16:49 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (const StoreInst *Store = dyn_cast<StoreInst>(Inst)) {
|
2015-09-05 17:56:54 +08:00
|
|
|
generateScalarStore(Stmt, Store, BBMap, LTS, NewAccesses);
|
2012-04-25 21:16:49 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-05-23 07:43:58 +08:00
|
|
|
if (const PHINode *PHI = dyn_cast<PHINode>(Inst)) {
|
2015-09-05 17:56:54 +08:00
|
|
|
copyPHIInstruction(Stmt, PHI, BBMap, LTS);
|
2015-05-23 07:43:58 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-01-26 02:07:30 +08:00
|
|
|
// Skip some special intrinsics for which we do not adjust the semantics to
|
|
|
|
// the new schedule. All others are handled like every other instruction.
|
2015-08-31 00:57:15 +08:00
|
|
|
if (isIgnoredIntrinsic(Inst))
|
|
|
|
return;
|
2015-01-26 02:07:30 +08:00
|
|
|
|
2015-09-05 17:56:54 +08:00
|
|
|
copyInstScalar(Stmt, Inst, BBMap, LTS);
|
2012-04-25 21:16:49 +08:00
|
|
|
}
|
|
|
|
|
2015-09-30 07:43:40 +08:00
|
|
|
/// @brief Remove trivially dead instructions from BB
|
|
|
|
///
|
|
|
|
/// This function drops trivially dead instructions from a basic block. It
|
|
|
|
/// on purpose does _not_ recurse into other BBs even if the deletion of
|
|
|
|
/// instructions in this basic block can make instructions in other basic blocks
|
|
|
|
/// triviall dead.
|
|
|
|
static void simplifyInstsInBlockOnly(BasicBlock *BB) {
|
|
|
|
auto BI = --BB->end(), BE = BB->begin();
|
|
|
|
bool Exit = false;
|
|
|
|
while (!Exit) {
|
|
|
|
auto ToRemove = BI;
|
|
|
|
if (BI != BE)
|
|
|
|
BI--;
|
|
|
|
else
|
|
|
|
Exit = true;
|
|
|
|
|
|
|
|
if (!isInstructionTriviallyDead(ToRemove))
|
|
|
|
continue;
|
|
|
|
ToRemove->eraseFromParent();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-05 17:56:54 +08:00
|
|
|
void BlockGenerator::copyStmt(ScopStmt &Stmt, LoopToScevMapT <S,
|
2015-08-27 15:28:16 +08:00
|
|
|
isl_id_to_ast_expr *NewAccesses) {
|
2015-02-25 00:16:32 +08:00
|
|
|
assert(Stmt.isBlockStmt() &&
|
|
|
|
"Only block statements can be copied by the block generator");
|
|
|
|
|
|
|
|
ValueMapT BBMap;
|
|
|
|
|
2015-02-07 05:39:31 +08:00
|
|
|
BasicBlock *BB = Stmt.getBasicBlock();
|
2015-09-05 17:56:54 +08:00
|
|
|
copyBB(Stmt, BB, BBMap, LTS, NewAccesses);
|
2015-09-30 07:43:40 +08:00
|
|
|
|
|
|
|
auto CopyBB = Builder.GetInsertBlock();
|
|
|
|
// Delete trivially dead instructions in CopyBB, but not in any other BB.
|
|
|
|
// Only for copyBB we know that there will _never_ be any future uses of
|
|
|
|
// instructions that have no use after copyBB has finished. Other instructions
|
|
|
|
// in the AST that have been generated by IslNodeBuilder may look dead at
|
|
|
|
// the moment, but may possibly still be referenced by GlobalMaps. If we
|
|
|
|
// delete them now, later uses would break surprisingly.
|
|
|
|
simplifyInstsInBlockOnly(CopyBB);
|
|
|
|
Builder.SetInsertPoint(CopyBB->getTerminator());
|
2015-02-25 00:16:32 +08:00
|
|
|
}
|
|
|
|
|
2015-02-28 02:29:04 +08:00
|
|
|
BasicBlock *BlockGenerator::splitBB(BasicBlock *BB) {
|
2013-02-06 02:01:29 +08:00
|
|
|
BasicBlock *CopyBB =
|
2015-02-23 21:51:35 +08:00
|
|
|
SplitBlock(Builder.GetInsertBlock(), Builder.GetInsertPoint(), &DT, &LI);
|
2012-04-25 21:16:49 +08:00
|
|
|
CopyBB->setName("polly.stmt." + BB->getName());
|
2015-02-28 02:29:04 +08:00
|
|
|
return CopyBB;
|
|
|
|
}
|
2012-04-25 21:16:49 +08:00
|
|
|
|
2015-02-28 02:29:04 +08:00
|
|
|
BasicBlock *BlockGenerator::copyBB(ScopStmt &Stmt, BasicBlock *BB,
|
2015-09-05 17:56:54 +08:00
|
|
|
ValueMapT &BBMap, LoopToScevMapT <S,
|
2015-08-27 15:28:16 +08:00
|
|
|
isl_id_to_ast_expr *NewAccesses) {
|
2015-02-28 02:29:04 +08:00
|
|
|
BasicBlock *CopyBB = splitBB(BB);
|
2015-09-05 17:56:54 +08:00
|
|
|
copyBB(Stmt, BB, CopyBB, BBMap, LTS, NewAccesses);
|
2015-02-28 02:29:04 +08:00
|
|
|
return CopyBB;
|
|
|
|
}
|
|
|
|
|
|
|
|
void BlockGenerator::copyBB(ScopStmt &Stmt, BasicBlock *BB, BasicBlock *CopyBB,
|
2015-09-05 17:56:54 +08:00
|
|
|
ValueMapT &BBMap, LoopToScevMapT <S,
|
2015-08-27 15:28:16 +08:00
|
|
|
isl_id_to_ast_expr *NewAccesses) {
|
2015-02-28 02:29:04 +08:00
|
|
|
Builder.SetInsertPoint(CopyBB->begin());
|
2015-05-23 07:43:58 +08:00
|
|
|
EntryBB = &CopyBB->getParent()->getEntryBlock();
|
|
|
|
|
2014-06-04 16:06:40 +08:00
|
|
|
for (Instruction &Inst : *BB)
|
2015-09-05 17:56:54 +08:00
|
|
|
copyInstruction(Stmt, &Inst, BBMap, LTS, NewAccesses);
|
2015-05-23 07:43:58 +08:00
|
|
|
|
|
|
|
// After a basic block was copied store all scalars that escape this block
|
|
|
|
// in their alloca. First the scalars that have dependences inside the SCoP,
|
|
|
|
// then the ones that might escape the SCoP.
|
2015-09-05 17:56:54 +08:00
|
|
|
generateScalarStores(Stmt, BB, BBMap);
|
2015-05-23 07:43:58 +08:00
|
|
|
|
|
|
|
const Region &R = Stmt.getParent()->getRegion();
|
|
|
|
for (Instruction &Inst : *BB)
|
2015-09-05 17:56:54 +08:00
|
|
|
handleOutsideUsers(R, &Inst, BBMap[&Inst]);
|
2015-05-23 07:43:58 +08:00
|
|
|
}
|
|
|
|
|
2015-08-31 13:52:24 +08:00
|
|
|
Value *BlockGenerator::getOrCreateAlloca(Value *ScalarBase,
|
|
|
|
ScalarAllocaMapTy &Map,
|
|
|
|
const char *NameExt) {
|
2015-05-23 07:43:58 +08:00
|
|
|
// Check if an alloca was cached for the base instruction.
|
2015-09-18 14:01:11 +08:00
|
|
|
Value *&Addr = Map[ScalarBase];
|
2015-05-23 07:43:58 +08:00
|
|
|
|
|
|
|
// If no alloca was found create one and insert it in the entry block.
|
|
|
|
if (!Addr) {
|
|
|
|
auto *Ty = ScalarBase->getType();
|
2015-09-18 14:01:11 +08:00
|
|
|
auto NewAddr = new AllocaInst(Ty, ScalarBase->getName() + NameExt);
|
2015-08-31 13:52:24 +08:00
|
|
|
EntryBB = &Builder.GetInsertBlock()->getParent()->getEntryBlock();
|
2015-09-18 14:01:11 +08:00
|
|
|
NewAddr->insertBefore(EntryBB->getFirstInsertionPt());
|
|
|
|
Addr = NewAddr;
|
2015-05-23 07:43:58 +08:00
|
|
|
}
|
|
|
|
|
2015-09-05 17:56:54 +08:00
|
|
|
if (GlobalMap.count(Addr))
|
|
|
|
return GlobalMap[Addr];
|
2015-08-31 13:52:24 +08:00
|
|
|
|
2015-05-23 07:43:58 +08:00
|
|
|
return Addr;
|
|
|
|
}
|
|
|
|
|
2015-09-05 17:56:54 +08:00
|
|
|
Value *BlockGenerator::getOrCreateAlloca(MemoryAccess &Access) {
|
2015-08-30 02:12:03 +08:00
|
|
|
if (Access.getScopArrayInfo()->isPHI())
|
2015-09-05 17:56:54 +08:00
|
|
|
return getOrCreatePHIAlloca(Access.getBaseAddr());
|
2015-08-30 02:12:03 +08:00
|
|
|
else
|
2015-09-05 17:56:54 +08:00
|
|
|
return getOrCreateScalarAlloca(Access.getBaseAddr());
|
2015-08-30 02:12:03 +08:00
|
|
|
}
|
|
|
|
|
2015-09-05 17:56:54 +08:00
|
|
|
Value *BlockGenerator::getOrCreateScalarAlloca(Value *ScalarBase) {
|
|
|
|
return getOrCreateAlloca(ScalarBase, ScalarMap, ".s2a");
|
2015-08-28 16:23:35 +08:00
|
|
|
}
|
|
|
|
|
2015-09-05 17:56:54 +08:00
|
|
|
Value *BlockGenerator::getOrCreatePHIAlloca(Value *ScalarBase) {
|
|
|
|
return getOrCreateAlloca(ScalarBase, PHIOpMap, ".phiops");
|
2015-08-28 16:23:35 +08:00
|
|
|
}
|
|
|
|
|
2015-09-05 17:56:54 +08:00
|
|
|
void BlockGenerator::handleOutsideUsers(const Region &R, Instruction *Inst,
|
2015-09-18 14:01:11 +08:00
|
|
|
Value *InstCopy, Value *Address) {
|
Remove isNew from getOrCreateAlloca
This commit drops some dead code. Specifically, there is no need to initialize
the virtual memory locations of scalars in BlockGenerator::handleOutsideUsers,
the function that initalizes the escape map that keeps track of out-of-scope
uses of scalar values. We already model instructions inside the scop that
are used outside the scope (escaping instructions) as scalar memory writes at
the position of the instruction. As a result, the virtual memory location of
this instructions is already initialized when code-generating the corresponding
virtual scalar write and consequently does not need to be initialized later on
when generating the set of escaping values.
Code references:
In TempScopInfo::buildScalarDependences we detect scalar cross-statement
dependences for all instructions (including PHIs) that have uses outside of the
scop's region:
// Check whether or not the use is in the SCoP.
if (!R->contains(UseParent)) {
AnyCrossStmtUse = true;
continue;
}
We use this information in TempScopInfo::buildAccessFunctions were we build
scalar write memory accesses for all these instructions:
if (!isa<StoreInst>(Inst) &&
buildScalarDependences(Inst, &R, NonAffineSubRegion)) {
// If the Instruction is used outside the statement, we need to build the
// write access.
IRAccess ScalarAccess(IRAccess::MUST_WRITE, Inst, ZeroOffset, 1, true,
Inst);
Functions.push_back(std::make_pair(ScalarAccess, Inst));
}
Reviewers: jdoerfert
Subscribers: pollydev, llvm-commits
Differential Revision: http://reviews.llvm.org/D12472
llvm-svn: 246383
2015-08-30 23:03:59 +08:00
|
|
|
// If there are escape users we get the alloca for this instruction and put it
|
|
|
|
// in the EscapeMap for later finalization. Lastly, if the instruction was
|
|
|
|
// copied multiple times we already did this and can exit.
|
2015-08-19 01:25:48 +08:00
|
|
|
if (EscapeMap.count(Inst))
|
|
|
|
return;
|
2015-08-18 19:56:00 +08:00
|
|
|
|
2015-05-23 07:43:58 +08:00
|
|
|
EscapeUserVectorTy EscapeUsers;
|
|
|
|
for (User *U : Inst->users()) {
|
|
|
|
|
|
|
|
// Non-instruction user will never escape.
|
|
|
|
Instruction *UI = dyn_cast<Instruction>(U);
|
|
|
|
if (!UI)
|
|
|
|
continue;
|
|
|
|
|
2015-08-16 16:35:40 +08:00
|
|
|
if (R.contains(UI))
|
2015-05-23 07:43:58 +08:00
|
|
|
continue;
|
|
|
|
|
|
|
|
EscapeUsers.push_back(UI);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Exit if no escape uses were found.
|
|
|
|
if (EscapeUsers.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Get or create an escape alloca for this instruction.
|
2015-09-18 14:01:11 +08:00
|
|
|
auto *ScalarAddr = Address ? Address : getOrCreateScalarAlloca(Inst);
|
2015-05-23 07:43:58 +08:00
|
|
|
|
|
|
|
// Remember that this instruction has escape uses and the escape alloca.
|
|
|
|
EscapeMap[Inst] = std::make_pair(ScalarAddr, std::move(EscapeUsers));
|
|
|
|
}
|
|
|
|
|
|
|
|
void BlockGenerator::generateScalarLoads(ScopStmt &Stmt,
|
|
|
|
const Instruction *Inst,
|
2015-09-05 17:56:54 +08:00
|
|
|
ValueMapT &BBMap) {
|
2015-07-28 01:57:58 +08:00
|
|
|
auto *MAL = Stmt.lookupAccessesFor(Inst);
|
2015-05-23 07:43:58 +08:00
|
|
|
|
2015-07-28 01:57:58 +08:00
|
|
|
if (!MAL)
|
|
|
|
return;
|
|
|
|
|
2015-09-19 03:59:43 +08:00
|
|
|
for (MemoryAccess *MA : *MAL) {
|
2015-09-26 05:21:00 +08:00
|
|
|
if (MA->isExplicit() || !MA->isRead())
|
2015-07-28 01:57:58 +08:00
|
|
|
continue;
|
|
|
|
|
2015-09-19 03:59:43 +08:00
|
|
|
auto *Address = getOrCreateAlloca(*MA);
|
|
|
|
BBMap[MA->getBaseAddr()] =
|
2015-08-31 03:51:01 +08:00
|
|
|
Builder.CreateLoad(Address, Address->getName() + ".reload");
|
2015-05-23 07:43:58 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Value *BlockGenerator::getNewScalarValue(Value *ScalarValue, const Region &R,
|
2015-09-05 17:56:54 +08:00
|
|
|
ValueMapT &BBMap) {
|
2015-05-23 07:43:58 +08:00
|
|
|
// If the value we want to store is an instruction we might have demoted it
|
|
|
|
// in order to make it accessible here. In such a case a reload is
|
|
|
|
// necessary. If it is no instruction it will always be a value that
|
|
|
|
// dominates the current point and we can just use it. In total there are 4
|
|
|
|
// options:
|
|
|
|
// (1) The value is no instruction ==> use the value.
|
|
|
|
// (2) The value is an instruction that was split out of the region prior to
|
|
|
|
// code generation ==> use the instruction as it dominates the region.
|
|
|
|
// (3) The value is an instruction:
|
|
|
|
// (a) The value was defined in the current block, thus a copy is in
|
|
|
|
// the BBMap ==> use the mapped value.
|
|
|
|
// (b) The value was defined in a previous block, thus we demoted it
|
|
|
|
// earlier ==> use the reloaded value.
|
|
|
|
Instruction *ScalarValueInst = dyn_cast<Instruction>(ScalarValue);
|
|
|
|
if (!ScalarValueInst)
|
|
|
|
return ScalarValue;
|
|
|
|
|
|
|
|
if (!R.contains(ScalarValueInst)) {
|
|
|
|
if (Value *ScalarValueCopy = GlobalMap.lookup(ScalarValueInst))
|
|
|
|
return /* Case (3a) */ ScalarValueCopy;
|
|
|
|
else
|
|
|
|
return /* Case 2 */ ScalarValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Value *ScalarValueCopy = BBMap.lookup(ScalarValueInst))
|
|
|
|
return /* Case (3a) */ ScalarValueCopy;
|
|
|
|
|
|
|
|
// Case (3b)
|
2015-09-05 17:56:54 +08:00
|
|
|
Value *Address = getOrCreateScalarAlloca(ScalarValueInst);
|
2015-08-31 01:37:55 +08:00
|
|
|
ScalarValue = Builder.CreateLoad(Address, Address->getName() + ".reload");
|
2015-05-23 07:43:58 +08:00
|
|
|
|
|
|
|
return ScalarValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
void BlockGenerator::generateScalarStores(ScopStmt &Stmt, BasicBlock *BB,
|
2015-09-05 17:56:54 +08:00
|
|
|
ValueMapT &BBMap) {
|
2015-05-23 07:43:58 +08:00
|
|
|
const Region &R = Stmt.getParent()->getRegion();
|
|
|
|
|
|
|
|
assert(Stmt.isBlockStmt() && BB == Stmt.getBasicBlock() &&
|
|
|
|
"Region statements need to use the generateScalarStores() "
|
|
|
|
"function in the RegionGenerator");
|
|
|
|
|
|
|
|
for (MemoryAccess *MA : Stmt) {
|
2015-09-26 05:21:00 +08:00
|
|
|
if (MA->isExplicit() || MA->isRead())
|
2015-05-23 07:43:58 +08:00
|
|
|
continue;
|
|
|
|
|
2015-08-17 18:58:17 +08:00
|
|
|
Value *Val = MA->getAccessValue();
|
2015-09-05 17:56:54 +08:00
|
|
|
auto *Address = getOrCreateAlloca(*MA);
|
2015-08-17 18:58:17 +08:00
|
|
|
|
2015-09-05 17:56:54 +08:00
|
|
|
Val = getNewScalarValue(Val, R, BBMap);
|
2015-07-28 22:53:44 +08:00
|
|
|
Builder.CreateStore(Val, Address);
|
2015-05-23 07:43:58 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-31 03:19:34 +08:00
|
|
|
void BlockGenerator::createScalarInitialization(Scop &S) {
|
|
|
|
Region &R = S.getRegion();
|
2015-05-23 07:43:58 +08:00
|
|
|
// The split block __just before__ the region and optimized region.
|
|
|
|
BasicBlock *SplitBB = R.getEnteringBlock();
|
|
|
|
BranchInst *SplitBBTerm = cast<BranchInst>(SplitBB->getTerminator());
|
|
|
|
assert(SplitBBTerm->getNumSuccessors() == 2 && "Bad region entering block!");
|
|
|
|
|
|
|
|
// Get the start block of the __optimized__ region.
|
|
|
|
BasicBlock *StartBB = SplitBBTerm->getSuccessor(0);
|
|
|
|
if (StartBB == R.getEntry())
|
|
|
|
StartBB = SplitBBTerm->getSuccessor(1);
|
|
|
|
|
2015-09-27 04:57:59 +08:00
|
|
|
Builder.SetInsertPoint(StartBB->getTerminator());
|
2015-05-23 07:43:58 +08:00
|
|
|
|
2015-08-31 03:19:34 +08:00
|
|
|
for (auto &Pair : S.arrays()) {
|
|
|
|
auto &Array = Pair.second;
|
|
|
|
if (Array->getNumberOfDimensions() != 0)
|
|
|
|
continue;
|
|
|
|
if (Array->isPHI()) {
|
|
|
|
// For PHI nodes, the only values we need to store are the ones that
|
|
|
|
// reach the PHI node from outside the region. In general there should
|
|
|
|
// only be one such incoming edge and this edge should enter through
|
|
|
|
// 'SplitBB'.
|
|
|
|
auto PHI = cast<PHINode>(Array->getBasePtr());
|
|
|
|
|
|
|
|
for (auto BI = PHI->block_begin(), BE = PHI->block_end(); BI != BE; BI++)
|
|
|
|
if (!R.contains(*BI) && *BI != SplitBB)
|
|
|
|
llvm_unreachable("Incoming edges from outside the scop should always "
|
|
|
|
"come from SplitBB");
|
|
|
|
|
|
|
|
int Idx = PHI->getBasicBlockIndex(SplitBB);
|
|
|
|
if (Idx < 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
Value *ScalarValue = PHI->getIncomingValue(Idx);
|
2015-05-23 07:43:58 +08:00
|
|
|
|
2015-09-05 17:56:54 +08:00
|
|
|
Builder.CreateStore(ScalarValue, getOrCreatePHIAlloca(PHI));
|
2015-05-23 07:43:58 +08:00
|
|
|
continue;
|
2015-08-31 03:19:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
auto *Inst = dyn_cast<Instruction>(Array->getBasePtr());
|
2015-05-23 07:43:58 +08:00
|
|
|
|
2015-08-31 03:19:34 +08:00
|
|
|
if (Inst && R.contains(Inst))
|
|
|
|
continue;
|
2015-05-23 07:43:58 +08:00
|
|
|
|
2015-09-09 05:44:27 +08:00
|
|
|
// PHI nodes that are not marked as such in their SAI object are exit PHI
|
|
|
|
// nodes we model as common scalars but do not need to initialize.
|
|
|
|
if (Inst && isa<PHINode>(Inst))
|
|
|
|
continue;
|
|
|
|
|
2015-08-31 13:52:24 +08:00
|
|
|
ValueMapT EmptyMap;
|
2015-08-31 03:19:34 +08:00
|
|
|
Builder.CreateStore(Array->getBasePtr(),
|
2015-09-05 17:56:54 +08:00
|
|
|
getOrCreateScalarAlloca(Array->getBasePtr()));
|
2015-05-23 07:43:58 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void BlockGenerator::createScalarFinalization(Region &R) {
|
|
|
|
// The exit block of the __unoptimized__ region.
|
|
|
|
BasicBlock *ExitBB = R.getExitingBlock();
|
|
|
|
// The merge block __just after__ the region and the optimized region.
|
|
|
|
BasicBlock *MergeBB = R.getExit();
|
|
|
|
|
|
|
|
// The exit block of the __optimized__ region.
|
|
|
|
BasicBlock *OptExitBB = *(pred_begin(MergeBB));
|
|
|
|
if (OptExitBB == ExitBB)
|
|
|
|
OptExitBB = *(++pred_begin(MergeBB));
|
|
|
|
|
|
|
|
Builder.SetInsertPoint(OptExitBB->getTerminator());
|
|
|
|
for (const auto &EscapeMapping : EscapeMap) {
|
|
|
|
// Extract the escaping instruction and the escaping users as well as the
|
|
|
|
// alloca the instruction was demoted to.
|
|
|
|
Instruction *EscapeInst = EscapeMapping.getFirst();
|
|
|
|
const auto &EscapeMappingValue = EscapeMapping.getSecond();
|
|
|
|
const EscapeUserVectorTy &EscapeUsers = EscapeMappingValue.second;
|
2015-08-31 13:52:24 +08:00
|
|
|
Value *ScalarAddr = EscapeMappingValue.first;
|
2015-05-23 07:43:58 +08:00
|
|
|
|
|
|
|
// Reload the demoted instruction in the optimized version of the SCoP.
|
|
|
|
Instruction *EscapeInstReload =
|
|
|
|
Builder.CreateLoad(ScalarAddr, EscapeInst->getName() + ".final_reload");
|
|
|
|
|
|
|
|
// Create the merge PHI that merges the optimized and unoptimized version.
|
|
|
|
PHINode *MergePHI = PHINode::Create(EscapeInst->getType(), 2,
|
|
|
|
EscapeInst->getName() + ".merge");
|
|
|
|
MergePHI->insertBefore(MergeBB->getFirstInsertionPt());
|
|
|
|
|
|
|
|
// Add the respective values to the merge PHI.
|
|
|
|
MergePHI->addIncoming(EscapeInstReload, OptExitBB);
|
|
|
|
MergePHI->addIncoming(EscapeInst, ExitBB);
|
|
|
|
|
|
|
|
// The information of scalar evolution about the escaping instruction needs
|
|
|
|
// to be revoked so the new merged instruction will be used.
|
|
|
|
if (SE.isSCEVable(EscapeInst->getType()))
|
|
|
|
SE.forgetValue(EscapeInst);
|
|
|
|
|
|
|
|
// Replace all uses of the demoted instruction with the merge PHI.
|
|
|
|
for (Instruction *EUser : EscapeUsers)
|
|
|
|
EUser->replaceUsesOfWith(EscapeInst, MergePHI);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-31 01:32:39 +08:00
|
|
|
void BlockGenerator::finalizeSCoP(Scop &S) {
|
2015-09-09 05:44:27 +08:00
|
|
|
|
|
|
|
// Handle PHI nodes that were in the original exit and are now
|
|
|
|
// moved into the region exiting block.
|
|
|
|
if (!S.hasSingleExitEdge()) {
|
|
|
|
for (Instruction &I : *S.getRegion().getExitingBlock()) {
|
|
|
|
PHINode *PHI = dyn_cast<PHINode>(&I);
|
|
|
|
if (!PHI)
|
|
|
|
break;
|
|
|
|
|
|
|
|
assert(PHI->getNumUses() == 1);
|
|
|
|
assert(ScalarMap.count(PHI->user_back()));
|
|
|
|
|
|
|
|
handleOutsideUsers(S.getRegion(), PHI, nullptr,
|
|
|
|
ScalarMap[PHI->user_back()]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-31 03:19:34 +08:00
|
|
|
createScalarInitialization(S);
|
2015-05-23 07:43:58 +08:00
|
|
|
createScalarFinalization(S.getRegion());
|
2012-04-25 21:16:49 +08:00
|
|
|
}
|
|
|
|
|
2015-02-07 05:39:31 +08:00
|
|
|
VectorBlockGenerator::VectorBlockGenerator(BlockGenerator &BlockGen,
|
|
|
|
std::vector<LoopToScevMapT> &VLTS,
|
|
|
|
isl_map *Schedule)
|
2015-09-05 17:56:54 +08:00
|
|
|
: BlockGenerator(BlockGen), VLTS(VLTS), Schedule(Schedule) {
|
2012-12-18 15:46:06 +08:00
|
|
|
assert(Schedule && "No statement domain provided");
|
|
|
|
}
|
2012-04-25 21:16:49 +08:00
|
|
|
|
2015-02-07 05:39:31 +08:00
|
|
|
Value *VectorBlockGenerator::getVectorValue(ScopStmt &Stmt, const Value *Old,
|
2013-05-07 15:30:56 +08:00
|
|
|
ValueMapT &VectorMap,
|
|
|
|
VectorValueMapT &ScalarMaps,
|
|
|
|
Loop *L) {
|
2013-06-29 21:22:15 +08:00
|
|
|
if (Value *NewValue = VectorMap.lookup(Old))
|
|
|
|
return NewValue;
|
2012-04-25 21:16:49 +08:00
|
|
|
|
|
|
|
int Width = getVectorWidth();
|
|
|
|
|
|
|
|
Value *Vector = UndefValue::get(VectorType::get(Old->getType(), Width));
|
|
|
|
|
|
|
|
for (int Lane = 0; Lane < Width; Lane++)
|
2013-02-06 02:01:29 +08:00
|
|
|
Vector = Builder.CreateInsertElement(
|
2015-09-05 17:56:54 +08:00
|
|
|
Vector, getNewValue(Stmt, Old, ScalarMaps[Lane], VLTS[Lane], L),
|
2013-02-22 16:07:06 +08:00
|
|
|
Builder.getInt32(Lane));
|
2012-04-25 21:16:49 +08:00
|
|
|
|
|
|
|
VectorMap[Old] = Vector;
|
|
|
|
|
|
|
|
return Vector;
|
|
|
|
}
|
|
|
|
|
|
|
|
Type *VectorBlockGenerator::getVectorPtrTy(const Value *Val, int Width) {
|
|
|
|
PointerType *PointerTy = dyn_cast<PointerType>(Val->getType());
|
|
|
|
assert(PointerTy && "PointerType expected");
|
|
|
|
|
|
|
|
Type *ScalarType = PointerTy->getElementType();
|
|
|
|
VectorType *VectorType = VectorType::get(ScalarType, Width);
|
|
|
|
|
|
|
|
return PointerType::getUnqual(VectorType);
|
|
|
|
}
|
|
|
|
|
2015-02-07 05:39:31 +08:00
|
|
|
Value *VectorBlockGenerator::generateStrideOneLoad(
|
|
|
|
ScopStmt &Stmt, const LoadInst *Load, VectorValueMapT &ScalarMaps,
|
2015-08-27 15:28:16 +08:00
|
|
|
__isl_keep isl_id_to_ast_expr *NewAccesses, bool NegativeStride = false) {
|
2014-03-20 03:27:24 +08:00
|
|
|
unsigned VectorWidth = getVectorWidth();
|
2012-04-25 21:16:49 +08:00
|
|
|
const Value *Pointer = Load->getPointerOperand();
|
2014-03-20 03:27:24 +08:00
|
|
|
Type *VectorPtrType = getVectorPtrTy(Pointer, VectorWidth);
|
|
|
|
unsigned Offset = NegativeStride ? VectorWidth - 1 : 0;
|
|
|
|
|
2014-04-16 15:33:47 +08:00
|
|
|
Value *NewPointer = nullptr;
|
2015-09-05 17:56:54 +08:00
|
|
|
NewPointer = generateLocationAccessed(Stmt, Load, Pointer, ScalarMaps[Offset],
|
|
|
|
VLTS[Offset], NewAccesses);
|
2013-02-06 02:01:29 +08:00
|
|
|
Value *VectorPtr =
|
|
|
|
Builder.CreateBitCast(NewPointer, VectorPtrType, "vector_ptr");
|
|
|
|
LoadInst *VecLoad =
|
|
|
|
Builder.CreateLoad(VectorPtr, Load->getName() + "_p_vec_full");
|
2012-04-25 21:16:49 +08:00
|
|
|
if (!Aligned)
|
|
|
|
VecLoad->setAlignment(8);
|
|
|
|
|
2014-03-20 03:27:24 +08:00
|
|
|
if (NegativeStride) {
|
|
|
|
SmallVector<Constant *, 16> Indices;
|
|
|
|
for (int i = VectorWidth - 1; i >= 0; i--)
|
|
|
|
Indices.push_back(ConstantInt::get(Builder.getInt32Ty(), i));
|
|
|
|
Constant *SV = llvm::ConstantVector::get(Indices);
|
|
|
|
Value *RevVecLoad = Builder.CreateShuffleVector(
|
|
|
|
VecLoad, VecLoad, SV, Load->getName() + "_reverse");
|
|
|
|
return RevVecLoad;
|
|
|
|
}
|
|
|
|
|
2012-04-25 21:16:49 +08:00
|
|
|
return VecLoad;
|
|
|
|
}
|
|
|
|
|
2015-08-27 15:28:16 +08:00
|
|
|
Value *VectorBlockGenerator::generateStrideZeroLoad(
|
|
|
|
ScopStmt &Stmt, const LoadInst *Load, ValueMapT &BBMap,
|
|
|
|
__isl_keep isl_id_to_ast_expr *NewAccesses) {
|
2012-04-25 21:16:49 +08:00
|
|
|
const Value *Pointer = Load->getPointerOperand();
|
|
|
|
Type *VectorPtrType = getVectorPtrTy(Pointer, 1);
|
2015-09-05 17:56:54 +08:00
|
|
|
Value *NewPointer = generateLocationAccessed(Stmt, Load, Pointer, BBMap,
|
|
|
|
VLTS[0], NewAccesses);
|
2012-04-25 21:16:49 +08:00
|
|
|
Value *VectorPtr = Builder.CreateBitCast(NewPointer, VectorPtrType,
|
|
|
|
Load->getName() + "_p_vec_p");
|
2013-02-06 02:01:29 +08:00
|
|
|
LoadInst *ScalarLoad =
|
|
|
|
Builder.CreateLoad(VectorPtr, Load->getName() + "_p_splat_one");
|
2012-04-25 21:16:49 +08:00
|
|
|
|
|
|
|
if (!Aligned)
|
|
|
|
ScalarLoad->setAlignment(8);
|
|
|
|
|
2013-02-06 02:01:29 +08:00
|
|
|
Constant *SplatVector = Constant::getNullValue(
|
|
|
|
VectorType::get(Builder.getInt32Ty(), getVectorWidth()));
|
2012-04-25 21:16:49 +08:00
|
|
|
|
2013-02-06 02:01:29 +08:00
|
|
|
Value *VectorLoad = Builder.CreateShuffleVector(
|
|
|
|
ScalarLoad, ScalarLoad, SplatVector, Load->getName() + "_p_splat");
|
2012-04-25 21:16:49 +08:00
|
|
|
return VectorLoad;
|
|
|
|
}
|
|
|
|
|
2015-02-07 05:39:31 +08:00
|
|
|
Value *VectorBlockGenerator::generateUnknownStrideLoad(
|
2015-08-27 15:28:16 +08:00
|
|
|
ScopStmt &Stmt, const LoadInst *Load, VectorValueMapT &ScalarMaps,
|
|
|
|
__isl_keep isl_id_to_ast_expr *NewAccesses
|
|
|
|
|
|
|
|
) {
|
2012-04-25 21:16:49 +08:00
|
|
|
int VectorWidth = getVectorWidth();
|
|
|
|
const Value *Pointer = Load->getPointerOperand();
|
|
|
|
VectorType *VectorType = VectorType::get(
|
2013-02-06 02:01:29 +08:00
|
|
|
dyn_cast<PointerType>(Pointer->getType())->getElementType(), VectorWidth);
|
2012-04-25 21:16:49 +08:00
|
|
|
|
|
|
|
Value *Vector = UndefValue::get(VectorType);
|
|
|
|
|
|
|
|
for (int i = 0; i < VectorWidth; i++) {
|
2015-09-05 17:56:54 +08:00
|
|
|
Value *NewPointer = generateLocationAccessed(
|
|
|
|
Stmt, Load, Pointer, ScalarMaps[i], VLTS[i], NewAccesses);
|
2013-02-06 02:01:29 +08:00
|
|
|
Value *ScalarLoad =
|
|
|
|
Builder.CreateLoad(NewPointer, Load->getName() + "_p_scalar_");
|
|
|
|
Vector = Builder.CreateInsertElement(
|
|
|
|
Vector, ScalarLoad, Builder.getInt32(i), Load->getName() + "_p_vec_");
|
2012-04-25 21:16:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return Vector;
|
|
|
|
}
|
|
|
|
|
2015-08-27 15:28:16 +08:00
|
|
|
void VectorBlockGenerator::generateLoad(
|
|
|
|
ScopStmt &Stmt, const LoadInst *Load, ValueMapT &VectorMap,
|
|
|
|
VectorValueMapT &ScalarMaps, __isl_keep isl_id_to_ast_expr *NewAccesses) {
|
2015-09-30 07:47:21 +08:00
|
|
|
if (Value *PreloadLoad = GlobalMap.lookup(Load)) {
|
|
|
|
VectorMap[Load] = Builder.CreateVectorSplat(getVectorWidth(), PreloadLoad,
|
|
|
|
Load->getName() + "_p");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-03-23 15:00:36 +08:00
|
|
|
if (!VectorType::isValidElementType(Load->getType())) {
|
2012-04-25 21:16:49 +08:00
|
|
|
for (int i = 0; i < getVectorWidth(); i++)
|
2015-09-05 17:56:54 +08:00
|
|
|
ScalarMaps[i][Load] =
|
|
|
|
generateScalarLoad(Stmt, Load, ScalarMaps[i], VLTS[i], NewAccesses);
|
2012-04-25 21:16:49 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-02-07 05:39:31 +08:00
|
|
|
const MemoryAccess &Access = Stmt.getAccessFor(Load);
|
2012-04-25 21:16:49 +08:00
|
|
|
|
2014-04-18 17:46:35 +08:00
|
|
|
// Make sure we have scalar values available to access the pointer to
|
|
|
|
// the data location.
|
|
|
|
extractScalarValues(Load, VectorMap, ScalarMaps);
|
|
|
|
|
2012-04-25 21:16:49 +08:00
|
|
|
Value *NewLoad;
|
2012-12-18 15:46:06 +08:00
|
|
|
if (Access.isStrideZero(isl_map_copy(Schedule)))
|
2015-08-27 15:28:16 +08:00
|
|
|
NewLoad = generateStrideZeroLoad(Stmt, Load, ScalarMaps[0], NewAccesses);
|
2012-12-18 15:46:06 +08:00
|
|
|
else if (Access.isStrideOne(isl_map_copy(Schedule)))
|
2015-08-27 15:28:16 +08:00
|
|
|
NewLoad = generateStrideOneLoad(Stmt, Load, ScalarMaps, NewAccesses);
|
2014-03-20 03:27:24 +08:00
|
|
|
else if (Access.isStrideX(isl_map_copy(Schedule), -1))
|
2015-08-27 15:28:16 +08:00
|
|
|
NewLoad = generateStrideOneLoad(Stmt, Load, ScalarMaps, NewAccesses, true);
|
2012-04-25 21:16:49 +08:00
|
|
|
else
|
2015-08-27 15:28:16 +08:00
|
|
|
NewLoad = generateUnknownStrideLoad(Stmt, Load, ScalarMaps, NewAccesses);
|
2012-04-25 21:16:49 +08:00
|
|
|
|
|
|
|
VectorMap[Load] = NewLoad;
|
|
|
|
}
|
|
|
|
|
2015-02-07 05:39:31 +08:00
|
|
|
void VectorBlockGenerator::copyUnaryInst(ScopStmt &Stmt,
|
|
|
|
const UnaryInstruction *Inst,
|
2012-04-25 21:16:49 +08:00
|
|
|
ValueMapT &VectorMap,
|
|
|
|
VectorValueMapT &ScalarMaps) {
|
|
|
|
int VectorWidth = getVectorWidth();
|
2015-02-07 05:39:31 +08:00
|
|
|
Value *NewOperand = getVectorValue(Stmt, Inst->getOperand(0), VectorMap,
|
|
|
|
ScalarMaps, getLoopForInst(Inst));
|
2012-04-25 21:16:49 +08:00
|
|
|
|
|
|
|
assert(isa<CastInst>(Inst) && "Can not generate vector code for instruction");
|
|
|
|
|
|
|
|
const CastInst *Cast = dyn_cast<CastInst>(Inst);
|
|
|
|
VectorType *DestType = VectorType::get(Inst->getType(), VectorWidth);
|
|
|
|
VectorMap[Inst] = Builder.CreateCast(Cast->getOpcode(), NewOperand, DestType);
|
|
|
|
}
|
|
|
|
|
2015-02-07 05:39:31 +08:00
|
|
|
void VectorBlockGenerator::copyBinaryInst(ScopStmt &Stmt,
|
|
|
|
const BinaryOperator *Inst,
|
2012-04-25 21:16:49 +08:00
|
|
|
ValueMapT &VectorMap,
|
|
|
|
VectorValueMapT &ScalarMaps) {
|
2013-03-23 07:42:53 +08:00
|
|
|
Loop *L = getLoopForInst(Inst);
|
2012-04-25 21:16:49 +08:00
|
|
|
Value *OpZero = Inst->getOperand(0);
|
|
|
|
Value *OpOne = Inst->getOperand(1);
|
|
|
|
|
|
|
|
Value *NewOpZero, *NewOpOne;
|
2015-02-07 05:39:31 +08:00
|
|
|
NewOpZero = getVectorValue(Stmt, OpZero, VectorMap, ScalarMaps, L);
|
|
|
|
NewOpOne = getVectorValue(Stmt, OpOne, VectorMap, ScalarMaps, L);
|
2012-04-25 21:16:49 +08:00
|
|
|
|
2012-12-30 07:47:38 +08:00
|
|
|
Value *NewInst = Builder.CreateBinOp(Inst->getOpcode(), NewOpZero, NewOpOne,
|
2012-04-25 21:16:49 +08:00
|
|
|
Inst->getName() + "p_vec");
|
|
|
|
VectorMap[Inst] = NewInst;
|
|
|
|
}
|
|
|
|
|
2015-08-27 15:28:16 +08:00
|
|
|
void VectorBlockGenerator::copyStore(
|
|
|
|
ScopStmt &Stmt, const StoreInst *Store, ValueMapT &VectorMap,
|
|
|
|
VectorValueMapT &ScalarMaps, __isl_keep isl_id_to_ast_expr *NewAccesses) {
|
2015-02-07 05:39:31 +08:00
|
|
|
const MemoryAccess &Access = Stmt.getAccessFor(Store);
|
2012-04-25 21:16:49 +08:00
|
|
|
|
|
|
|
const Value *Pointer = Store->getPointerOperand();
|
2015-02-07 05:39:31 +08:00
|
|
|
Value *Vector = getVectorValue(Stmt, Store->getValueOperand(), VectorMap,
|
2013-03-23 07:42:53 +08:00
|
|
|
ScalarMaps, getLoopForInst(Store));
|
2012-04-25 21:16:49 +08:00
|
|
|
|
2014-04-18 07:13:49 +08:00
|
|
|
// Make sure we have scalar values available to access the pointer to
|
|
|
|
// the data location.
|
|
|
|
extractScalarValues(Store, VectorMap, ScalarMaps);
|
|
|
|
|
2012-12-18 15:46:06 +08:00
|
|
|
if (Access.isStrideOne(isl_map_copy(Schedule))) {
|
2014-10-09 04:18:32 +08:00
|
|
|
Type *VectorPtrType = getVectorPtrTy(Pointer, getVectorWidth());
|
2015-09-05 17:56:54 +08:00
|
|
|
Value *NewPointer = generateLocationAccessed(
|
|
|
|
Stmt, Store, Pointer, ScalarMaps[0], VLTS[0], NewAccesses);
|
2012-04-25 21:16:49 +08:00
|
|
|
|
2013-02-06 02:01:29 +08:00
|
|
|
Value *VectorPtr =
|
|
|
|
Builder.CreateBitCast(NewPointer, VectorPtrType, "vector_ptr");
|
2012-04-25 21:16:49 +08:00
|
|
|
StoreInst *Store = Builder.CreateStore(Vector, VectorPtr);
|
|
|
|
|
|
|
|
if (!Aligned)
|
|
|
|
Store->setAlignment(8);
|
|
|
|
} else {
|
|
|
|
for (unsigned i = 0; i < ScalarMaps.size(); i++) {
|
2012-12-30 07:47:38 +08:00
|
|
|
Value *Scalar = Builder.CreateExtractElement(Vector, Builder.getInt32(i));
|
2015-09-05 17:56:54 +08:00
|
|
|
Value *NewPointer = generateLocationAccessed(
|
|
|
|
Stmt, Store, Pointer, ScalarMaps[i], VLTS[i], NewAccesses);
|
2012-04-25 21:16:49 +08:00
|
|
|
Builder.CreateStore(Scalar, NewPointer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool VectorBlockGenerator::hasVectorOperands(const Instruction *Inst,
|
|
|
|
ValueMapT &VectorMap) {
|
2014-06-04 16:06:40 +08:00
|
|
|
for (Value *Operand : Inst->operands())
|
|
|
|
if (VectorMap.count(Operand))
|
2012-04-25 21:16:49 +08:00
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool VectorBlockGenerator::extractScalarValues(const Instruction *Inst,
|
|
|
|
ValueMapT &VectorMap,
|
|
|
|
VectorValueMapT &ScalarMaps) {
|
|
|
|
bool HasVectorOperand = false;
|
|
|
|
int VectorWidth = getVectorWidth();
|
|
|
|
|
2014-06-04 16:06:40 +08:00
|
|
|
for (Value *Operand : Inst->operands()) {
|
|
|
|
ValueMapT::iterator VecOp = VectorMap.find(Operand);
|
2012-04-25 21:16:49 +08:00
|
|
|
|
|
|
|
if (VecOp == VectorMap.end())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
HasVectorOperand = true;
|
|
|
|
Value *NewVector = VecOp->second;
|
|
|
|
|
|
|
|
for (int i = 0; i < VectorWidth; ++i) {
|
|
|
|
ValueMapT &SM = ScalarMaps[i];
|
|
|
|
|
|
|
|
// If there is one scalar extracted, all scalar elements should have
|
|
|
|
// already been extracted by the code here. So no need to check for the
|
|
|
|
// existance of all of them.
|
2014-06-04 16:06:40 +08:00
|
|
|
if (SM.count(Operand))
|
2012-04-25 21:16:49 +08:00
|
|
|
break;
|
|
|
|
|
2014-06-04 16:06:40 +08:00
|
|
|
SM[Operand] =
|
|
|
|
Builder.CreateExtractElement(NewVector, Builder.getInt32(i));
|
2012-04-25 21:16:49 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return HasVectorOperand;
|
|
|
|
}
|
|
|
|
|
2015-08-27 15:28:16 +08:00
|
|
|
void VectorBlockGenerator::copyInstScalarized(
|
|
|
|
ScopStmt &Stmt, const Instruction *Inst, ValueMapT &VectorMap,
|
|
|
|
VectorValueMapT &ScalarMaps, __isl_keep isl_id_to_ast_expr *NewAccesses) {
|
2012-04-25 21:16:49 +08:00
|
|
|
bool HasVectorOperand;
|
|
|
|
int VectorWidth = getVectorWidth();
|
|
|
|
|
|
|
|
HasVectorOperand = extractScalarValues(Inst, VectorMap, ScalarMaps);
|
|
|
|
|
|
|
|
for (int VectorLane = 0; VectorLane < getVectorWidth(); VectorLane++)
|
2015-02-07 05:39:31 +08:00
|
|
|
BlockGenerator::copyInstruction(Stmt, Inst, ScalarMaps[VectorLane],
|
2015-09-05 17:56:54 +08:00
|
|
|
VLTS[VectorLane], NewAccesses);
|
2012-04-25 21:16:49 +08:00
|
|
|
|
|
|
|
if (!VectorType::isValidElementType(Inst->getType()) || !HasVectorOperand)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Make the result available as vector value.
|
|
|
|
VectorType *VectorType = VectorType::get(Inst->getType(), VectorWidth);
|
|
|
|
Value *Vector = UndefValue::get(VectorType);
|
|
|
|
|
|
|
|
for (int i = 0; i < VectorWidth; i++)
|
|
|
|
Vector = Builder.CreateInsertElement(Vector, ScalarMaps[i][Inst],
|
|
|
|
Builder.getInt32(i));
|
|
|
|
|
|
|
|
VectorMap[Inst] = Vector;
|
|
|
|
}
|
|
|
|
|
2015-09-05 17:56:54 +08:00
|
|
|
int VectorBlockGenerator::getVectorWidth() { return VLTS.size(); }
|
2012-04-25 21:16:49 +08:00
|
|
|
|
2015-08-27 15:28:16 +08:00
|
|
|
void VectorBlockGenerator::copyInstruction(
|
|
|
|
ScopStmt &Stmt, const Instruction *Inst, ValueMapT &VectorMap,
|
|
|
|
VectorValueMapT &ScalarMaps, __isl_keep isl_id_to_ast_expr *NewAccesses) {
|
2012-04-25 21:16:49 +08:00
|
|
|
// Terminator instructions control the control flow. They are explicitly
|
|
|
|
// expressed in the clast and do not need to be copied.
|
|
|
|
if (Inst->isTerminator())
|
|
|
|
return;
|
|
|
|
|
2015-02-09 04:50:42 +08:00
|
|
|
if (canSynthesize(Inst, &LI, &SE, &Stmt.getParent()->getRegion()))
|
2012-04-28 00:36:14 +08:00
|
|
|
return;
|
|
|
|
|
2012-04-25 21:16:49 +08:00
|
|
|
if (const LoadInst *Load = dyn_cast<LoadInst>(Inst)) {
|
2015-08-27 15:28:16 +08:00
|
|
|
generateLoad(Stmt, Load, VectorMap, ScalarMaps, NewAccesses);
|
2012-04-25 21:16:49 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hasVectorOperands(Inst, VectorMap)) {
|
|
|
|
if (const StoreInst *Store = dyn_cast<StoreInst>(Inst)) {
|
2015-08-27 15:28:16 +08:00
|
|
|
copyStore(Stmt, Store, VectorMap, ScalarMaps, NewAccesses);
|
2012-04-25 21:16:49 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (const UnaryInstruction *Unary = dyn_cast<UnaryInstruction>(Inst)) {
|
2015-02-07 05:39:31 +08:00
|
|
|
copyUnaryInst(Stmt, Unary, VectorMap, ScalarMaps);
|
2012-04-25 21:16:49 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (const BinaryOperator *Binary = dyn_cast<BinaryOperator>(Inst)) {
|
2015-02-07 05:39:31 +08:00
|
|
|
copyBinaryInst(Stmt, Binary, VectorMap, ScalarMaps);
|
2012-04-25 21:16:49 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Falltrough: We generate scalar instructions, if we don't know how to
|
|
|
|
// generate vector code.
|
|
|
|
}
|
|
|
|
|
2015-08-27 15:28:16 +08:00
|
|
|
copyInstScalarized(Stmt, Inst, VectorMap, ScalarMaps, NewAccesses);
|
2012-04-25 21:16:49 +08:00
|
|
|
}
|
|
|
|
|
2015-08-27 15:28:16 +08:00
|
|
|
void VectorBlockGenerator::copyStmt(
|
|
|
|
ScopStmt &Stmt, __isl_keep isl_id_to_ast_expr *NewAccesses) {
|
2015-02-25 00:16:32 +08:00
|
|
|
assert(Stmt.isBlockStmt() && "TODO: Only block statements can be copied by "
|
|
|
|
"the vector block generator");
|
|
|
|
|
2015-02-07 05:39:31 +08:00
|
|
|
BasicBlock *BB = Stmt.getBasicBlock();
|
2013-02-06 02:01:29 +08:00
|
|
|
BasicBlock *CopyBB =
|
2015-02-23 21:51:35 +08:00
|
|
|
SplitBlock(Builder.GetInsertBlock(), Builder.GetInsertPoint(), &DT, &LI);
|
2012-04-25 21:16:49 +08:00
|
|
|
CopyBB->setName("polly.stmt." + BB->getName());
|
|
|
|
Builder.SetInsertPoint(CopyBB->begin());
|
|
|
|
|
|
|
|
// Create two maps that store the mapping from the original instructions of
|
|
|
|
// the old basic block to their copies in the new basic block. Those maps
|
|
|
|
// are basic block local.
|
|
|
|
//
|
|
|
|
// As vector code generation is supported there is one map for scalar values
|
|
|
|
// and one for vector values.
|
|
|
|
//
|
|
|
|
// In case we just do scalar code generation, the vectorMap is not used and
|
|
|
|
// the scalarMap has just one dimension, which contains the mapping.
|
|
|
|
//
|
|
|
|
// In case vector code generation is done, an instruction may either appear
|
|
|
|
// in the vector map once (as it is calculating >vectorwidth< values at a
|
|
|
|
// time. Or (if the values are calculated using scalar operations), it
|
|
|
|
// appears once in every dimension of the scalarMap.
|
|
|
|
VectorValueMapT ScalarBlockMap(getVectorWidth());
|
|
|
|
ValueMapT VectorBlockMap;
|
|
|
|
|
2014-06-04 16:06:40 +08:00
|
|
|
for (Instruction &Inst : *BB)
|
2015-08-27 15:28:16 +08:00
|
|
|
copyInstruction(Stmt, &Inst, VectorBlockMap, ScalarBlockMap, NewAccesses);
|
2012-04-25 21:16:49 +08:00
|
|
|
}
|
2015-02-25 00:16:32 +08:00
|
|
|
|
2015-05-23 07:43:58 +08:00
|
|
|
BasicBlock *RegionGenerator::repairDominance(BasicBlock *BB,
|
|
|
|
BasicBlock *BBCopy) {
|
2015-02-28 02:29:04 +08:00
|
|
|
|
|
|
|
BasicBlock *BBIDom = DT.getNode(BB)->getIDom()->getBlock();
|
|
|
|
BasicBlock *BBCopyIDom = BlockMap.lookup(BBIDom);
|
|
|
|
|
|
|
|
if (BBCopyIDom)
|
|
|
|
DT.changeImmediateDominator(BBCopy, BBCopyIDom);
|
|
|
|
|
|
|
|
return BBCopyIDom;
|
|
|
|
}
|
|
|
|
|
2015-09-05 17:56:54 +08:00
|
|
|
void RegionGenerator::copyStmt(ScopStmt &Stmt, LoopToScevMapT <S,
|
2015-08-27 15:28:16 +08:00
|
|
|
isl_id_to_ast_expr *IdToAstExp) {
|
2015-02-25 00:16:32 +08:00
|
|
|
assert(Stmt.isRegionStmt() &&
|
2015-08-01 14:26:51 +08:00
|
|
|
"Only region statements can be copied by the region generator");
|
2015-02-25 00:16:32 +08:00
|
|
|
|
2015-05-23 07:43:58 +08:00
|
|
|
// Forget all old mappings.
|
|
|
|
BlockMap.clear();
|
|
|
|
RegionMaps.clear();
|
|
|
|
IncompletePHINodeMap.clear();
|
|
|
|
|
2015-02-25 00:16:32 +08:00
|
|
|
// The region represented by the statement.
|
|
|
|
Region *R = Stmt.getRegion();
|
|
|
|
|
2015-05-23 07:43:58 +08:00
|
|
|
// Create a dedicated entry for the region where we can reload all demoted
|
|
|
|
// inputs.
|
|
|
|
BasicBlock *EntryBB = R->getEntry();
|
|
|
|
BasicBlock *EntryBBCopy =
|
|
|
|
SplitBlock(Builder.GetInsertBlock(), Builder.GetInsertPoint(), &DT, &LI);
|
|
|
|
EntryBBCopy->setName("polly.stmt." + EntryBB->getName() + ".entry");
|
|
|
|
Builder.SetInsertPoint(EntryBBCopy->begin());
|
2015-02-28 02:29:04 +08:00
|
|
|
|
2015-05-23 07:43:58 +08:00
|
|
|
for (auto PI = pred_begin(EntryBB), PE = pred_end(EntryBB); PI != PE; ++PI)
|
|
|
|
if (!R->contains(*PI))
|
|
|
|
BlockMap[*PI] = EntryBBCopy;
|
2015-02-25 00:16:32 +08:00
|
|
|
|
|
|
|
// Iterate over all blocks in the region in a breadth-first search.
|
|
|
|
std::deque<BasicBlock *> Blocks;
|
|
|
|
SmallPtrSet<BasicBlock *, 8> SeenBlocks;
|
2015-05-23 07:43:58 +08:00
|
|
|
Blocks.push_back(EntryBB);
|
|
|
|
SeenBlocks.insert(EntryBB);
|
2015-02-25 00:16:32 +08:00
|
|
|
|
|
|
|
while (!Blocks.empty()) {
|
|
|
|
BasicBlock *BB = Blocks.front();
|
|
|
|
Blocks.pop_front();
|
|
|
|
|
2015-02-28 02:29:04 +08:00
|
|
|
// First split the block and update dominance information.
|
|
|
|
BasicBlock *BBCopy = splitBB(BB);
|
2015-05-23 07:43:58 +08:00
|
|
|
BasicBlock *BBCopyIDom = repairDominance(BB, BBCopy);
|
|
|
|
|
|
|
|
// In order to remap PHI nodes we store also basic block mappings.
|
|
|
|
BlockMap[BB] = BBCopy;
|
2015-02-28 02:29:04 +08:00
|
|
|
|
|
|
|
// Get the mapping for this block and initialize it with the mapping
|
|
|
|
// available at its immediate dominator (in the new region).
|
|
|
|
ValueMapT &RegionMap = RegionMaps[BBCopy];
|
|
|
|
RegionMap = RegionMaps[BBCopyIDom];
|
|
|
|
|
2015-02-25 00:16:32 +08:00
|
|
|
// Copy the block with the BlockGenerator.
|
2015-09-05 17:56:54 +08:00
|
|
|
copyBB(Stmt, BB, BBCopy, RegionMap, LTS, IdToAstExp);
|
2015-02-25 00:16:32 +08:00
|
|
|
|
2015-05-23 07:43:58 +08:00
|
|
|
// In order to remap PHI nodes we store also basic block mappings.
|
|
|
|
BlockMap[BB] = BBCopy;
|
|
|
|
|
|
|
|
// Add values to incomplete PHI nodes waiting for this block to be copied.
|
|
|
|
for (const PHINodePairTy &PHINodePair : IncompletePHINodeMap[BB])
|
2015-09-05 17:56:54 +08:00
|
|
|
addOperandToPHI(Stmt, PHINodePair.first, PHINodePair.second, BB, LTS);
|
2015-05-23 07:43:58 +08:00
|
|
|
IncompletePHINodeMap[BB].clear();
|
|
|
|
|
2015-02-25 00:16:32 +08:00
|
|
|
// And continue with new successors inside the region.
|
|
|
|
for (auto SI = succ_begin(BB), SE = succ_end(BB); SI != SE; SI++)
|
|
|
|
if (R->contains(*SI) && SeenBlocks.insert(*SI).second)
|
|
|
|
Blocks.push_back(*SI);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now create a new dedicated region exit block and add it to the region map.
|
2015-02-28 02:29:04 +08:00
|
|
|
BasicBlock *ExitBBCopy =
|
2015-02-25 00:16:32 +08:00
|
|
|
SplitBlock(Builder.GetInsertBlock(), Builder.GetInsertPoint(), &DT, &LI);
|
2015-05-23 07:43:58 +08:00
|
|
|
ExitBBCopy->setName("polly.stmt." + R->getExit()->getName() + ".exit");
|
2015-02-28 02:29:04 +08:00
|
|
|
BlockMap[R->getExit()] = ExitBBCopy;
|
|
|
|
|
2015-05-23 07:43:58 +08:00
|
|
|
repairDominance(R->getExit(), ExitBBCopy);
|
2015-02-25 00:16:32 +08:00
|
|
|
|
|
|
|
// As the block generator doesn't handle control flow we need to add the
|
|
|
|
// region control flow by hand after all blocks have been copied.
|
|
|
|
for (BasicBlock *BB : SeenBlocks) {
|
|
|
|
|
2015-02-28 02:29:04 +08:00
|
|
|
BasicBlock *BBCopy = BlockMap[BB];
|
2015-09-14 19:15:58 +08:00
|
|
|
TerminatorInst *TI = BB->getTerminator();
|
|
|
|
if (isa<UnreachableInst>(TI)) {
|
|
|
|
while (!BBCopy->empty())
|
|
|
|
BBCopy->begin()->eraseFromParent();
|
|
|
|
new UnreachableInst(BBCopy->getContext(), BBCopy);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-02-25 00:16:32 +08:00
|
|
|
Instruction *BICopy = BBCopy->getTerminator();
|
|
|
|
|
2015-02-28 02:29:04 +08:00
|
|
|
ValueMapT &RegionMap = RegionMaps[BBCopy];
|
|
|
|
RegionMap.insert(BlockMap.begin(), BlockMap.end());
|
|
|
|
|
2015-08-01 17:07:57 +08:00
|
|
|
Builder.SetInsertPoint(BICopy);
|
2015-09-28 17:33:22 +08:00
|
|
|
copyInstScalar(Stmt, TI, RegionMap, LTS);
|
2015-02-25 00:16:32 +08:00
|
|
|
BICopy->eraseFromParent();
|
|
|
|
}
|
|
|
|
|
2015-05-23 07:43:58 +08:00
|
|
|
// Add counting PHI nodes to all loops in the region that can be used as
|
|
|
|
// replacement for SCEVs refering to the old loop.
|
|
|
|
for (BasicBlock *BB : SeenBlocks) {
|
|
|
|
Loop *L = LI.getLoopFor(BB);
|
|
|
|
if (L == nullptr || L->getHeader() != BB)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
BasicBlock *BBCopy = BlockMap[BB];
|
|
|
|
Value *NullVal = Builder.getInt32(0);
|
|
|
|
PHINode *LoopPHI =
|
|
|
|
PHINode::Create(Builder.getInt32Ty(), 2, "polly.subregion.iv");
|
|
|
|
Instruction *LoopPHIInc = BinaryOperator::CreateAdd(
|
|
|
|
LoopPHI, Builder.getInt32(1), "polly.subregion.iv.inc");
|
|
|
|
LoopPHI->insertBefore(BBCopy->begin());
|
|
|
|
LoopPHIInc->insertBefore(BBCopy->getTerminator());
|
|
|
|
|
|
|
|
for (auto *PredBB : make_range(pred_begin(BB), pred_end(BB))) {
|
|
|
|
if (!R->contains(PredBB))
|
|
|
|
continue;
|
|
|
|
if (L->contains(PredBB))
|
|
|
|
LoopPHI->addIncoming(LoopPHIInc, BlockMap[PredBB]);
|
|
|
|
else
|
|
|
|
LoopPHI->addIncoming(NullVal, BlockMap[PredBB]);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto *PredBBCopy : make_range(pred_begin(BBCopy), pred_end(BBCopy)))
|
|
|
|
if (LoopPHI->getBasicBlockIndex(PredBBCopy) < 0)
|
|
|
|
LoopPHI->addIncoming(NullVal, PredBBCopy);
|
|
|
|
|
|
|
|
LTS[L] = SE.getUnknown(LoopPHI);
|
|
|
|
}
|
|
|
|
|
2015-09-30 07:43:40 +08:00
|
|
|
// Delete trivially dead instructions in CopyBB, but not in any other BB.
|
|
|
|
// Only for copyBB we know that there will _never_ be any future uses of
|
|
|
|
// instructions that have no use after copyBB has finished. Other instructions
|
|
|
|
// in the AST that have been generated by IslNodeBuilder may look dead at
|
|
|
|
// the moment, but may possibly still be referenced by GlobalMaps. If we
|
|
|
|
// delete them now, later uses would break surprisingly.
|
|
|
|
for (auto *BB : SeenBlocks)
|
|
|
|
simplifyInstsInBlockOnly(BlockMap[BB]);
|
|
|
|
|
2015-02-25 00:16:32 +08:00
|
|
|
// Reset the old insert point for the build.
|
2015-02-28 02:29:04 +08:00
|
|
|
Builder.SetInsertPoint(ExitBBCopy->begin());
|
2015-02-25 00:16:32 +08:00
|
|
|
}
|
2015-05-23 07:43:58 +08:00
|
|
|
|
|
|
|
void RegionGenerator::generateScalarLoads(ScopStmt &Stmt,
|
|
|
|
const Instruction *Inst,
|
2015-09-05 17:56:54 +08:00
|
|
|
ValueMapT &BBMap) {
|
2015-05-23 07:43:58 +08:00
|
|
|
|
|
|
|
// Inside a non-affine region PHI nodes are copied not demoted. Once the
|
|
|
|
// phi is copied it will reload all inputs from outside the region, hence
|
|
|
|
// we do not need to generate code for the read access of the operands of a
|
|
|
|
// PHI.
|
|
|
|
if (isa<PHINode>(Inst))
|
|
|
|
return;
|
|
|
|
|
2015-09-05 17:56:54 +08:00
|
|
|
return BlockGenerator::generateScalarLoads(Stmt, Inst, BBMap);
|
2015-05-23 07:43:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void RegionGenerator::generateScalarStores(ScopStmt &Stmt, BasicBlock *BB,
|
2015-09-05 17:56:54 +08:00
|
|
|
ValueMapT &BBMap) {
|
2015-05-23 07:43:58 +08:00
|
|
|
const Region &R = Stmt.getParent()->getRegion();
|
|
|
|
|
2015-08-22 03:23:21 +08:00
|
|
|
assert(Stmt.getRegion() &&
|
|
|
|
"Block statements need to use the generateScalarStores() "
|
|
|
|
"function in the BlockGenerator");
|
2015-05-23 07:43:58 +08:00
|
|
|
|
|
|
|
for (MemoryAccess *MA : Stmt) {
|
|
|
|
|
2015-09-26 05:21:00 +08:00
|
|
|
if (MA->isExplicit() || MA->isRead())
|
2015-05-23 07:43:58 +08:00
|
|
|
continue;
|
|
|
|
|
|
|
|
Instruction *ScalarInst = MA->getAccessInstruction();
|
|
|
|
|
2015-08-03 00:17:41 +08:00
|
|
|
// Only generate accesses that belong to this basic block.
|
|
|
|
if (ScalarInst->getParent() != BB)
|
|
|
|
continue;
|
|
|
|
|
2015-08-17 18:58:17 +08:00
|
|
|
Value *Val = MA->getAccessValue();
|
2015-05-23 07:43:58 +08:00
|
|
|
|
2015-09-05 17:56:54 +08:00
|
|
|
auto Address = getOrCreateAlloca(*MA);
|
2015-05-23 07:43:58 +08:00
|
|
|
|
2015-09-05 17:56:54 +08:00
|
|
|
Val = getNewScalarValue(Val, R, BBMap);
|
2015-08-30 02:12:03 +08:00
|
|
|
Builder.CreateStore(Val, Address);
|
2015-05-23 07:43:58 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void RegionGenerator::addOperandToPHI(ScopStmt &Stmt, const PHINode *PHI,
|
|
|
|
PHINode *PHICopy, BasicBlock *IncomingBB,
|
|
|
|
LoopToScevMapT <S) {
|
|
|
|
Region *StmtR = Stmt.getRegion();
|
|
|
|
|
|
|
|
// If the incoming block was not yet copied mark this PHI as incomplete.
|
|
|
|
// Once the block will be copied the incoming value will be added.
|
|
|
|
BasicBlock *BBCopy = BlockMap[IncomingBB];
|
|
|
|
if (!BBCopy) {
|
|
|
|
assert(StmtR->contains(IncomingBB) &&
|
|
|
|
"Bad incoming block for PHI in non-affine region");
|
|
|
|
IncompletePHINodeMap[IncomingBB].push_back(std::make_pair(PHI, PHICopy));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Value *OpCopy = nullptr;
|
|
|
|
if (StmtR->contains(IncomingBB)) {
|
|
|
|
assert(RegionMaps.count(BBCopy) &&
|
|
|
|
"Incoming PHI block did not have a BBMap");
|
|
|
|
ValueMapT &BBCopyMap = RegionMaps[BBCopy];
|
|
|
|
|
|
|
|
Value *Op = PHI->getIncomingValueForBlock(IncomingBB);
|
2015-09-05 17:56:54 +08:00
|
|
|
OpCopy = getNewValue(Stmt, Op, BBCopyMap, LTS, getLoopForInst(PHI));
|
2015-05-23 07:43:58 +08:00
|
|
|
} else {
|
|
|
|
|
|
|
|
if (PHICopy->getBasicBlockIndex(BBCopy) >= 0)
|
|
|
|
return;
|
|
|
|
|
2015-09-05 17:56:54 +08:00
|
|
|
Value *PHIOpAddr = getOrCreatePHIAlloca(const_cast<PHINode *>(PHI));
|
2015-05-23 07:43:58 +08:00
|
|
|
OpCopy = new LoadInst(PHIOpAddr, PHIOpAddr->getName() + ".reload",
|
|
|
|
BlockMap[IncomingBB]->getTerminator());
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(OpCopy && "Incoming PHI value was not copied properly");
|
|
|
|
assert(BBCopy && "Incoming PHI block was not copied properly");
|
|
|
|
PHICopy->addIncoming(OpCopy, BBCopy);
|
|
|
|
}
|
|
|
|
|
|
|
|
Value *RegionGenerator::copyPHIInstruction(ScopStmt &Stmt, const PHINode *PHI,
|
|
|
|
ValueMapT &BBMap,
|
|
|
|
LoopToScevMapT <S) {
|
|
|
|
unsigned NumIncoming = PHI->getNumIncomingValues();
|
|
|
|
PHINode *PHICopy =
|
|
|
|
Builder.CreatePHI(PHI->getType(), NumIncoming, "polly." + PHI->getName());
|
|
|
|
PHICopy->moveBefore(PHICopy->getParent()->getFirstNonPHI());
|
|
|
|
BBMap[PHI] = PHICopy;
|
|
|
|
|
|
|
|
for (unsigned u = 0; u < NumIncoming; u++)
|
2015-09-05 17:56:54 +08:00
|
|
|
addOperandToPHI(Stmt, PHI, PHICopy, PHI->getIncomingBlock(u), LTS);
|
2015-05-23 07:43:58 +08:00
|
|
|
return PHICopy;
|
|
|
|
}
|