2011-04-29 14:27:02 +08:00
|
|
|
//===---------- TempScopInfo.cpp - Extract TempScops ---------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// Collect information about the control flow regions detected by the Scop
|
|
|
|
// detection, such that this information can be translated info its polyhedral
|
|
|
|
// representation.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "polly/TempScopInfo.h"
|
2013-06-10 21:55:34 +08:00
|
|
|
#include "polly/CodeGen/BlockGenerators.h"
|
2015-05-09 17:13:42 +08:00
|
|
|
#include "polly/LinkAllPasses.h"
|
|
|
|
#include "polly/ScopDetection.h"
|
2011-04-29 14:27:02 +08:00
|
|
|
#include "polly/Support/GICHelper.h"
|
2011-11-08 23:41:28 +08:00
|
|
|
#include "polly/Support/SCEVValidator.h"
|
2013-05-07 16:11:54 +08:00
|
|
|
#include "polly/Support/ScopHelper.h"
|
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2011-04-29 14:27:02 +08:00
|
|
|
#include "llvm/Analysis/AliasAnalysis.h"
|
2011-11-10 06:35:00 +08:00
|
|
|
#include "llvm/Analysis/LoopInfo.h"
|
2014-07-20 02:40:17 +08:00
|
|
|
#include "llvm/Analysis/PostDominators.h"
|
2013-05-07 16:11:54 +08:00
|
|
|
#include "llvm/Analysis/RegionIterator.h"
|
2011-11-10 06:35:00 +08:00
|
|
|
#include "llvm/Analysis/ScalarEvolution.h"
|
2011-11-10 20:44:55 +08:00
|
|
|
#include "llvm/Analysis/ScalarEvolutionExpressions.h"
|
2013-05-07 16:11:54 +08:00
|
|
|
#include "llvm/IR/DataLayout.h"
|
2015-03-05 17:48:20 +08:00
|
|
|
#include "llvm/IR/Module.h"
|
2011-04-29 14:27:02 +08:00
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
using namespace polly;
|
|
|
|
|
2014-04-22 11:30:19 +08:00
|
|
|
#define DEBUG_TYPE "polly-analyze-ir"
|
|
|
|
|
2011-04-29 14:27:02 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2013-06-29 15:00:14 +08:00
|
|
|
/// Helper Classes
|
|
|
|
|
|
|
|
void IRAccess::print(raw_ostream &OS) const {
|
|
|
|
if (isRead())
|
|
|
|
OS << "Read ";
|
2014-09-12 19:00:49 +08:00
|
|
|
else {
|
|
|
|
if (isMayWrite())
|
|
|
|
OS << "May";
|
2013-06-29 15:00:14 +08:00
|
|
|
OS << "Write ";
|
2014-09-12 19:00:49 +08:00
|
|
|
}
|
2013-06-29 15:00:14 +08:00
|
|
|
OS << BaseAddress->getName() << '[' << *Offset << "]\n";
|
|
|
|
}
|
2011-04-29 14:27:02 +08:00
|
|
|
|
|
|
|
void Comparison::print(raw_ostream &OS) const {
|
|
|
|
// Not yet implemented.
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Helper function to print the condition
|
|
|
|
static void printBBCond(raw_ostream &OS, const BBCond &Cond) {
|
|
|
|
assert(!Cond.empty() && "Unexpected empty condition!");
|
|
|
|
Cond[0].print(OS);
|
|
|
|
for (unsigned i = 1, e = Cond.size(); i != e; ++i) {
|
|
|
|
OS << " && ";
|
|
|
|
Cond[i].print(OS);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
inline raw_ostream &operator<<(raw_ostream &OS, const BBCond &Cond) {
|
|
|
|
printBBCond(OS, Cond);
|
|
|
|
return OS;
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// TempScop implementation
|
2014-02-12 09:55:28 +08:00
|
|
|
TempScop::~TempScop() {}
|
2011-04-29 14:27:02 +08:00
|
|
|
|
|
|
|
void TempScop::print(raw_ostream &OS, ScalarEvolution *SE, LoopInfo *LI) const {
|
2014-11-01 08:12:13 +08:00
|
|
|
OS << "Scop: " << R.getNameStr() << "\n";
|
2011-04-29 14:27:02 +08:00
|
|
|
|
|
|
|
printDetail(OS, SE, LI, &R, 0);
|
|
|
|
}
|
|
|
|
|
2013-07-03 00:13:07 +08:00
|
|
|
void TempScop::printDetail(raw_ostream &OS, ScalarEvolution *SE, LoopInfo *LI,
|
|
|
|
const Region *CurR, unsigned ind) const {
|
2013-06-29 15:00:14 +08:00
|
|
|
// FIXME: Print other details rather than memory accesses.
|
2014-03-03 21:13:55 +08:00
|
|
|
for (const auto &CurBlock : CurR->blocks()) {
|
2013-06-29 15:00:14 +08:00
|
|
|
AccFuncMapType::const_iterator AccSetIt = AccFuncMap.find(CurBlock);
|
|
|
|
|
|
|
|
// Ignore trivial blocks that do not contain any memory access.
|
2013-07-03 00:13:07 +08:00
|
|
|
if (AccSetIt == AccFuncMap.end())
|
|
|
|
continue;
|
2013-06-29 15:00:14 +08:00
|
|
|
|
|
|
|
OS.indent(ind) << "BB: " << CurBlock->getName() << '\n';
|
|
|
|
typedef AccFuncSetType::const_iterator access_iterator;
|
|
|
|
const AccFuncSetType &AccFuncs = AccSetIt->second;
|
|
|
|
|
2013-07-03 00:13:07 +08:00
|
|
|
for (access_iterator AI = AccFuncs.begin(), AE = AccFuncs.end(); AI != AE;
|
|
|
|
++AI)
|
2013-06-29 15:00:14 +08:00
|
|
|
AI->first.print(OS.indent(ind + 2));
|
|
|
|
}
|
|
|
|
}
|
2011-04-29 14:27:02 +08:00
|
|
|
|
2015-02-07 04:13:15 +08:00
|
|
|
void TempScopInfo::buildPHIAccesses(PHINode *PHI, Region &R,
|
2015-03-02 22:06:01 +08:00
|
|
|
AccFuncSetType &Functions,
|
|
|
|
Region *NonAffineSubRegion) {
|
2015-02-07 04:13:15 +08:00
|
|
|
if (canSynthesize(PHI, LI, SE, &R))
|
|
|
|
return;
|
|
|
|
|
|
|
|
// PHI nodes are modeled as if they had been demoted prior to the SCoP
|
|
|
|
// detection. Hence, the PHI is a load of a new memory location in which the
|
|
|
|
// incoming value was written at the end of the incoming basic block.
|
2015-03-02 22:06:01 +08:00
|
|
|
bool Written = false;
|
2015-02-07 04:13:15 +08:00
|
|
|
for (unsigned u = 0; u < PHI->getNumIncomingValues(); u++) {
|
|
|
|
Value *Op = PHI->getIncomingValue(u);
|
|
|
|
BasicBlock *OpBB = PHI->getIncomingBlock(u);
|
|
|
|
|
|
|
|
if (!R.contains(OpBB))
|
|
|
|
continue;
|
|
|
|
|
2015-03-02 22:06:01 +08:00
|
|
|
// Do not build scalar dependences inside a non-affine subregion.
|
|
|
|
if (NonAffineSubRegion && NonAffineSubRegion->contains(OpBB))
|
|
|
|
continue;
|
|
|
|
|
2015-02-07 04:13:15 +08:00
|
|
|
Instruction *OpI = dyn_cast<Instruction>(Op);
|
|
|
|
if (OpI) {
|
|
|
|
BasicBlock *OpIBB = OpI->getParent();
|
|
|
|
// As we pretend there is a use (or more precise a write) of OpI in OpBB
|
|
|
|
// we have to insert a scalar dependence from the definition of OpI to
|
|
|
|
// OpBB if the definition is not in OpBB.
|
|
|
|
if (OpIBB != OpBB) {
|
|
|
|
IRAccess ScalarRead(IRAccess::READ, OpI, ZeroOffset, 1, true);
|
|
|
|
AccFuncMap[OpBB].push_back(std::make_pair(ScalarRead, PHI));
|
|
|
|
IRAccess ScalarWrite(IRAccess::MUST_WRITE, OpI, ZeroOffset, 1, true);
|
|
|
|
AccFuncMap[OpIBB].push_back(std::make_pair(ScalarWrite, OpI));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the operand is a constant, global or argument we need an access
|
|
|
|
// instruction and just choose the PHI.
|
|
|
|
if (!OpI)
|
|
|
|
OpI = PHI;
|
|
|
|
|
2015-03-02 22:06:01 +08:00
|
|
|
Written = true;
|
|
|
|
|
2015-02-07 04:13:15 +08:00
|
|
|
IRAccess ScalarAccess(IRAccess::MUST_WRITE, PHI, ZeroOffset, 1, true);
|
|
|
|
AccFuncMap[OpBB].push_back(std::make_pair(ScalarAccess, OpI));
|
|
|
|
}
|
|
|
|
|
2015-03-02 22:06:01 +08:00
|
|
|
if (Written) {
|
|
|
|
IRAccess ScalarAccess(IRAccess::READ, PHI, ZeroOffset, 1, true);
|
|
|
|
Functions.push_back(std::make_pair(ScalarAccess, PHI));
|
|
|
|
}
|
2015-02-07 04:13:15 +08:00
|
|
|
}
|
|
|
|
|
2015-03-02 22:06:01 +08:00
|
|
|
bool TempScopInfo::buildScalarDependences(Instruction *Inst, Region *R,
|
|
|
|
Region *NonAffineSubRegion) {
|
2015-02-12 01:02:52 +08:00
|
|
|
bool canSynthesizeInst = canSynthesize(Inst, LI, SE, R);
|
2015-01-26 23:55:54 +08:00
|
|
|
if (isIgnoredIntrinsic(Inst))
|
|
|
|
return false;
|
2013-06-10 21:55:34 +08:00
|
|
|
|
|
|
|
bool AnyCrossStmtUse = false;
|
|
|
|
BasicBlock *ParentBB = Inst->getParent();
|
|
|
|
|
2014-03-09 16:29:29 +08:00
|
|
|
for (User *U : Inst->users()) {
|
|
|
|
Instruction *UI = dyn_cast<Instruction>(U);
|
2013-06-10 21:55:34 +08:00
|
|
|
|
|
|
|
// Ignore the strange user
|
2014-03-09 16:29:29 +08:00
|
|
|
if (UI == 0)
|
2013-06-10 21:55:34 +08:00
|
|
|
continue;
|
|
|
|
|
2014-03-09 16:29:29 +08:00
|
|
|
BasicBlock *UseParent = UI->getParent();
|
2013-06-10 21:55:34 +08:00
|
|
|
|
|
|
|
// Ignore the users in the same BB (statement)
|
|
|
|
if (UseParent == ParentBB)
|
|
|
|
continue;
|
|
|
|
|
2015-03-02 22:06:01 +08:00
|
|
|
// Do not build scalar dependences inside a non-affine subregion.
|
|
|
|
if (NonAffineSubRegion && NonAffineSubRegion->contains(UseParent))
|
|
|
|
continue;
|
|
|
|
|
2015-02-12 01:02:52 +08:00
|
|
|
// Check whether or not the use is in the SCoP.
|
|
|
|
if (!R->contains(UseParent)) {
|
|
|
|
AnyCrossStmtUse = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the instruction can be synthesized and the user is in the region
|
|
|
|
// we do not need to add scalar dependences.
|
|
|
|
if (canSynthesizeInst)
|
|
|
|
continue;
|
|
|
|
|
2013-06-10 21:55:34 +08:00
|
|
|
// No need to translate these scalar dependences into polyhedral form,
|
|
|
|
// because synthesizable scalars can be generated by the code generator.
|
2014-03-09 16:29:29 +08:00
|
|
|
if (canSynthesize(UI, LI, SE, R))
|
2013-06-10 21:55:34 +08:00
|
|
|
continue;
|
|
|
|
|
2015-02-12 01:02:52 +08:00
|
|
|
// Skip PHI nodes in the region as they handle their operands on their own.
|
2015-02-07 04:13:15 +08:00
|
|
|
if (isa<PHINode>(UI))
|
|
|
|
continue;
|
|
|
|
|
2013-06-10 21:55:34 +08:00
|
|
|
// Now U is used in another statement.
|
|
|
|
AnyCrossStmtUse = true;
|
|
|
|
|
|
|
|
// Do not build a read access that is not in the current SCoP
|
|
|
|
// Use the def instruction as base address of the IRAccess, so that it will
|
2014-02-21 05:29:02 +08:00
|
|
|
// become the name of the scalar access in the polyhedral form.
|
2015-02-03 03:41:30 +08:00
|
|
|
IRAccess ScalarAccess(IRAccess::READ, Inst, ZeroOffset, 1, true);
|
2014-03-09 16:29:29 +08:00
|
|
|
AccFuncMap[UseParent].push_back(std::make_pair(ScalarAccess, UI));
|
2013-06-10 21:55:34 +08:00
|
|
|
}
|
|
|
|
|
2013-07-16 23:20:29 +08:00
|
|
|
return AnyCrossStmtUse;
|
2013-06-10 21:55:34 +08:00
|
|
|
}
|
|
|
|
|
record delinearization result and reuse it in polyhedral translation
Without this patch, the testcase would fail on the delinearization of the second
array:
; void foo(long n, long m, long o, double A[n][m][o]) {
; for (long i = 0; i < n; i++)
; for (long j = 0; j < m; j++)
; for (long k = 0; k < o; k++) {
; A[i+3][j-4][k+7] = 1.0;
; A[i][0][k] = 2.0;
; }
; }
; CHECK: [n, m, o] -> { Stmt_for_body6[i0, i1, i2] -> MemRef_A[3 + i0, -4 + i1, 7 + i2] };
; CHECK: [n, m, o] -> { Stmt_for_body6[i0, i1, i2] -> MemRef_A[i0, 0, i2] };
Here is the output of FileCheck on the testcase without this patch:
; CHECK: [n, m, o] -> { Stmt_for_body6[i0, i1, i2] -> MemRef_A[i0, 0, i2] };
^
<stdin>:26:2: note: possible intended match here
[n, m, o] -> { Stmt_for_body6[i0, i1, i2] -> MemRef_A[o0] };
^
It is possible to find a good delinearization for A[i][0][k] only in the context
of the delinearization of both array accesses.
There are two ways to delinearize together all array subscripts touching the
same base address: either duplicate the code from scop detection to first gather
all array references and then run the delinearization; or as implemented in this
patch, use the same delinearization info that we computed during scop detection.
llvm-svn: 210117
2014-06-04 02:16:31 +08:00
|
|
|
extern MapInsnToMemAcc InsnToMemAcc;
|
|
|
|
|
2015-04-13 06:58:40 +08:00
|
|
|
IRAccess
|
|
|
|
TempScopInfo::buildIRAccess(Instruction *Inst, Loop *L, Region *R,
|
|
|
|
const ScopDetection::BoxedLoopsSetTy *BoxedLoops) {
|
2013-06-10 10:52:30 +08:00
|
|
|
unsigned Size;
|
2014-04-09 05:20:44 +08:00
|
|
|
Type *SizeType;
|
2013-06-10 10:52:30 +08:00
|
|
|
enum IRAccess::TypeKind Type;
|
|
|
|
|
|
|
|
if (LoadInst *Load = dyn_cast<LoadInst>(Inst)) {
|
2014-04-09 05:20:44 +08:00
|
|
|
SizeType = Load->getType();
|
|
|
|
Size = TD->getTypeStoreSize(SizeType);
|
2013-06-10 10:52:30 +08:00
|
|
|
Type = IRAccess::READ;
|
|
|
|
} else {
|
|
|
|
StoreInst *Store = cast<StoreInst>(Inst);
|
2014-04-09 05:20:44 +08:00
|
|
|
SizeType = Store->getValueOperand()->getType();
|
|
|
|
Size = TD->getTypeStoreSize(SizeType);
|
2014-09-12 19:00:49 +08:00
|
|
|
Type = IRAccess::MUST_WRITE;
|
2013-06-10 10:52:30 +08:00
|
|
|
}
|
|
|
|
|
2013-06-23 09:29:29 +08:00
|
|
|
const SCEV *AccessFunction = SE->getSCEVAtScope(getPointerOperand(*Inst), L);
|
2013-06-10 10:52:30 +08:00
|
|
|
const SCEVUnknown *BasePointer =
|
|
|
|
dyn_cast<SCEVUnknown>(SE->getPointerBase(AccessFunction));
|
|
|
|
|
|
|
|
assert(BasePointer && "Could not find base pointer");
|
|
|
|
AccessFunction = SE->getMinusSCEV(AccessFunction, BasePointer);
|
2014-04-09 05:20:44 +08:00
|
|
|
|
record delinearization result and reuse it in polyhedral translation
Without this patch, the testcase would fail on the delinearization of the second
array:
; void foo(long n, long m, long o, double A[n][m][o]) {
; for (long i = 0; i < n; i++)
; for (long j = 0; j < m; j++)
; for (long k = 0; k < o; k++) {
; A[i+3][j-4][k+7] = 1.0;
; A[i][0][k] = 2.0;
; }
; }
; CHECK: [n, m, o] -> { Stmt_for_body6[i0, i1, i2] -> MemRef_A[3 + i0, -4 + i1, 7 + i2] };
; CHECK: [n, m, o] -> { Stmt_for_body6[i0, i1, i2] -> MemRef_A[i0, 0, i2] };
Here is the output of FileCheck on the testcase without this patch:
; CHECK: [n, m, o] -> { Stmt_for_body6[i0, i1, i2] -> MemRef_A[i0, 0, i2] };
^
<stdin>:26:2: note: possible intended match here
[n, m, o] -> { Stmt_for_body6[i0, i1, i2] -> MemRef_A[o0] };
^
It is possible to find a good delinearization for A[i][0][k] only in the context
of the delinearization of both array accesses.
There are two ways to delinearize together all array subscripts touching the
same base address: either duplicate the code from scop detection to first gather
all array references and then run the delinearization; or as implemented in this
patch, use the same delinearization info that we computed during scop detection.
llvm-svn: 210117
2014-06-04 02:16:31 +08:00
|
|
|
MemAcc *Acc = InsnToMemAcc[Inst];
|
|
|
|
if (PollyDelinearize && Acc)
|
|
|
|
return IRAccess(Type, BasePointer->getValue(), AccessFunction, Size, true,
|
|
|
|
Acc->DelinearizedSubscripts, Acc->Shape->DelinearizedSizes);
|
2013-06-10 10:52:30 +08:00
|
|
|
|
2015-04-13 06:58:40 +08:00
|
|
|
// Check if the access depends on a loop contained in a non-affine subregion.
|
|
|
|
bool isVariantInNonAffineLoop = false;
|
|
|
|
if (BoxedLoops) {
|
|
|
|
SetVector<const Loop *> Loops;
|
|
|
|
findLoops(AccessFunction, Loops);
|
|
|
|
for (const Loop *L : Loops)
|
|
|
|
if (BoxedLoops->count(L))
|
|
|
|
isVariantInNonAffineLoop = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsAffine = !isVariantInNonAffineLoop &&
|
|
|
|
isAffineExpr(R, AccessFunction, *SE, BasePointer->getValue());
|
2015-02-03 03:41:30 +08:00
|
|
|
|
|
|
|
SmallVector<const SCEV *, 4> Subscripts, Sizes;
|
record delinearization result and reuse it in polyhedral translation
Without this patch, the testcase would fail on the delinearization of the second
array:
; void foo(long n, long m, long o, double A[n][m][o]) {
; for (long i = 0; i < n; i++)
; for (long j = 0; j < m; j++)
; for (long k = 0; k < o; k++) {
; A[i+3][j-4][k+7] = 1.0;
; A[i][0][k] = 2.0;
; }
; }
; CHECK: [n, m, o] -> { Stmt_for_body6[i0, i1, i2] -> MemRef_A[3 + i0, -4 + i1, 7 + i2] };
; CHECK: [n, m, o] -> { Stmt_for_body6[i0, i1, i2] -> MemRef_A[i0, 0, i2] };
Here is the output of FileCheck on the testcase without this patch:
; CHECK: [n, m, o] -> { Stmt_for_body6[i0, i1, i2] -> MemRef_A[i0, 0, i2] };
^
<stdin>:26:2: note: possible intended match here
[n, m, o] -> { Stmt_for_body6[i0, i1, i2] -> MemRef_A[o0] };
^
It is possible to find a good delinearization for A[i][0][k] only in the context
of the delinearization of both array accesses.
There are two ways to delinearize together all array subscripts touching the
same base address: either duplicate the code from scop detection to first gather
all array references and then run the delinearization; or as implemented in this
patch, use the same delinearization info that we computed during scop detection.
llvm-svn: 210117
2014-06-04 02:16:31 +08:00
|
|
|
Subscripts.push_back(AccessFunction);
|
2015-02-03 03:41:30 +08:00
|
|
|
Sizes.push_back(SE->getConstant(ZeroOffset->getType(), Size));
|
|
|
|
|
2014-09-12 19:00:49 +08:00
|
|
|
if (!IsAffine && Type == IRAccess::MUST_WRITE)
|
|
|
|
Type = IRAccess::MAY_WRITE;
|
|
|
|
|
2014-04-09 05:37:58 +08:00
|
|
|
return IRAccess(Type, BasePointer->getValue(), AccessFunction, Size, IsAffine,
|
|
|
|
Subscripts, Sizes);
|
2013-06-10 10:52:30 +08:00
|
|
|
}
|
|
|
|
|
2015-03-02 22:06:01 +08:00
|
|
|
void TempScopInfo::buildAccessFunctions(Region &R, Region &SR) {
|
|
|
|
|
|
|
|
if (SD->isNonAffineSubRegion(&SR, &R)) {
|
|
|
|
for (BasicBlock *BB : SR.blocks())
|
|
|
|
buildAccessFunctions(R, *BB, &SR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto I = SR.element_begin(), E = SR.element_end(); I != E; ++I)
|
|
|
|
if (I->isSubRegion())
|
|
|
|
buildAccessFunctions(R, *I->getNodeAs<Region>());
|
|
|
|
else
|
|
|
|
buildAccessFunctions(R, *I->getNodeAs<BasicBlock>());
|
|
|
|
}
|
|
|
|
|
|
|
|
void TempScopInfo::buildAccessFunctions(Region &R, BasicBlock &BB,
|
|
|
|
Region *NonAffineSubRegion) {
|
2011-04-29 14:27:02 +08:00
|
|
|
AccFuncSetType Functions;
|
scop detection: properly instantiate SCEVs to the place where they are used
Fix inspired from c2d4a0627e95c34a819b9d4ffb4db62daa78dade.
Given the following code
for (i = 0; i < 10; i++) {
;
}
S: A[i] = 0
When translate the data reference A[i] in statement S using scev, we need to
retrieve the scev of 'i' at the location of 'S'. If we do not do this the
scev that we obtain will be expressed as {0,+,1}_for and will reference loop
iterators that do not surround 'S'. What we really want is the scev to be
instantiated to the value of 'i' after the loop. This value is {10}.
This used to crash in:
int loopDimension = getLoopDepth(Expr->getLoop());
isl_aff *LAff = isl_aff_set_coefficient_si(
isl_aff_zero_on_domain(LocalSpace), isl_dim_in, loopDimension, 1);
(gdb) p Expr->dump()
{8,+,8}<nw><%do.body>
(gdb) p getLoopDepth(Expr->getLoop())
$5 = 0
isl_space *Space = isl_space_set_alloc(Ctx, 0, NbLoopSpaces);
isl_local_space *LocalSpace = isl_local_space_from_space(Space);
As we are trying to create a memory access in a stmt that is outside all loops,
LocalSpace has 0 dimensions:
(gdb) p NbLoopSpaces
$12 = 0
(gdb) p Statement.BB->dump()
if.then: ; preds = %do.end
%0 = load float* %add.ptr, align 4
store float %0, float* %q.1.reg2mem, align 4
br label %if.end.single_exit
and so the scev for %add.ptr should be taken at the place where it is used,
i.e., it should be the value on the last iteration of the do.body loop, and not
"{8,+,8}<nw><%do.body>".
llvm-svn: 179148
2013-04-10 12:05:18 +08:00
|
|
|
Loop *L = LI->getLoopFor(&BB);
|
2011-08-18 14:29:25 +08:00
|
|
|
|
2015-04-13 06:58:40 +08:00
|
|
|
// The set of loops contained in non-affine subregions that are part of R.
|
|
|
|
const ScopDetection::BoxedLoopsSetTy *BoxedLoops = SD->getBoxedLoops(&R);
|
|
|
|
|
2011-04-29 14:27:02 +08:00
|
|
|
for (BasicBlock::iterator I = BB.begin(), E = --BB.end(); I != E; ++I) {
|
2013-06-10 10:52:30 +08:00
|
|
|
Instruction *Inst = I;
|
|
|
|
if (isa<LoadInst>(Inst) || isa<StoreInst>(Inst))
|
2015-04-13 06:58:40 +08:00
|
|
|
Functions.push_back(
|
|
|
|
std::make_pair(buildIRAccess(Inst, L, &R, BoxedLoops), Inst));
|
2013-06-10 21:55:34 +08:00
|
|
|
|
2015-02-07 04:13:15 +08:00
|
|
|
if (PHINode *PHI = dyn_cast<PHINode>(Inst))
|
2015-03-02 22:06:01 +08:00
|
|
|
buildPHIAccesses(PHI, R, Functions, NonAffineSubRegion);
|
2015-02-07 04:13:15 +08:00
|
|
|
|
2015-03-02 22:06:01 +08:00
|
|
|
if (!isa<StoreInst>(Inst) &&
|
|
|
|
buildScalarDependences(Inst, &R, NonAffineSubRegion)) {
|
2013-07-16 23:20:29 +08:00
|
|
|
// If the Instruction is used outside the statement, we need to build the
|
|
|
|
// write access.
|
2015-02-03 03:41:30 +08:00
|
|
|
IRAccess ScalarAccess(IRAccess::MUST_WRITE, Inst, ZeroOffset, 1, true);
|
2013-07-16 23:20:29 +08:00
|
|
|
Functions.push_back(std::make_pair(ScalarAccess, Inst));
|
|
|
|
}
|
2011-04-29 14:27:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (Functions.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
AccFuncSetType &Accs = AccFuncMap[&BB];
|
|
|
|
Accs.insert(Accs.end(), Functions.begin(), Functions.end());
|
|
|
|
}
|
|
|
|
|
|
|
|
void TempScopInfo::buildAffineCondition(Value &V, bool inverted,
|
2012-12-30 07:47:38 +08:00
|
|
|
Comparison **Comp) const {
|
2011-04-29 14:27:02 +08:00
|
|
|
if (ConstantInt *C = dyn_cast<ConstantInt>(&V)) {
|
2013-09-10 12:47:19 +08:00
|
|
|
// If this is always true condition, we will create 0 <= 1,
|
|
|
|
// otherwise we will create 0 >= 1.
|
2011-11-10 06:34:44 +08:00
|
|
|
const SCEV *LHS = SE->getConstant(C->getType(), 0);
|
2013-09-10 12:47:19 +08:00
|
|
|
const SCEV *RHS = SE->getConstant(C->getType(), 1);
|
2011-11-10 06:34:44 +08:00
|
|
|
|
2011-04-29 14:27:02 +08:00
|
|
|
if (C->isOne() == inverted)
|
2013-09-10 12:47:19 +08:00
|
|
|
*Comp = new Comparison(LHS, RHS, ICmpInst::ICMP_SLE);
|
2011-04-29 14:27:02 +08:00
|
|
|
else
|
2013-09-10 12:47:19 +08:00
|
|
|
*Comp = new Comparison(LHS, RHS, ICmpInst::ICMP_SGE);
|
2011-04-29 14:27:02 +08:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ICmpInst *ICmp = dyn_cast<ICmpInst>(&V);
|
|
|
|
assert(ICmp && "Only ICmpInst of constant as condition supported!");
|
|
|
|
|
scop detection: properly instantiate SCEVs to the place where they are used
Fix inspired from c2d4a0627e95c34a819b9d4ffb4db62daa78dade.
Given the following code
for (i = 0; i < 10; i++) {
;
}
S: A[i] = 0
When translate the data reference A[i] in statement S using scev, we need to
retrieve the scev of 'i' at the location of 'S'. If we do not do this the
scev that we obtain will be expressed as {0,+,1}_for and will reference loop
iterators that do not surround 'S'. What we really want is the scev to be
instantiated to the value of 'i' after the loop. This value is {10}.
This used to crash in:
int loopDimension = getLoopDepth(Expr->getLoop());
isl_aff *LAff = isl_aff_set_coefficient_si(
isl_aff_zero_on_domain(LocalSpace), isl_dim_in, loopDimension, 1);
(gdb) p Expr->dump()
{8,+,8}<nw><%do.body>
(gdb) p getLoopDepth(Expr->getLoop())
$5 = 0
isl_space *Space = isl_space_set_alloc(Ctx, 0, NbLoopSpaces);
isl_local_space *LocalSpace = isl_local_space_from_space(Space);
As we are trying to create a memory access in a stmt that is outside all loops,
LocalSpace has 0 dimensions:
(gdb) p NbLoopSpaces
$12 = 0
(gdb) p Statement.BB->dump()
if.then: ; preds = %do.end
%0 = load float* %add.ptr, align 4
store float %0, float* %q.1.reg2mem, align 4
br label %if.end.single_exit
and so the scev for %add.ptr should be taken at the place where it is used,
i.e., it should be the value on the last iteration of the do.body loop, and not
"{8,+,8}<nw><%do.body>".
llvm-svn: 179148
2013-04-10 12:05:18 +08:00
|
|
|
Loop *L = LI->getLoopFor(ICmp->getParent());
|
|
|
|
const SCEV *LHS = SE->getSCEVAtScope(ICmp->getOperand(0), L);
|
|
|
|
const SCEV *RHS = SE->getSCEVAtScope(ICmp->getOperand(1), L);
|
2011-04-29 14:27:02 +08:00
|
|
|
|
|
|
|
ICmpInst::Predicate Pred = ICmp->getPredicate();
|
|
|
|
|
|
|
|
// Invert the predicate if needed.
|
|
|
|
if (inverted)
|
|
|
|
Pred = ICmpInst::getInversePredicate(Pred);
|
|
|
|
|
|
|
|
switch (Pred) {
|
|
|
|
case ICmpInst::ICMP_UGT:
|
|
|
|
case ICmpInst::ICMP_UGE:
|
|
|
|
case ICmpInst::ICMP_ULT:
|
|
|
|
case ICmpInst::ICMP_ULE:
|
|
|
|
// TODO: At the moment we need to see everything as signed. This is an
|
|
|
|
// correctness issue that needs to be solved.
|
2013-06-23 09:29:29 +08:00
|
|
|
// AffLHS->setUnsigned();
|
|
|
|
// AffRHS->setUnsigned();
|
2011-04-29 14:27:02 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2011-11-10 06:34:44 +08:00
|
|
|
*Comp = new Comparison(LHS, RHS, Pred);
|
2011-04-29 14:27:02 +08:00
|
|
|
}
|
|
|
|
|
2015-02-24 20:00:50 +08:00
|
|
|
void TempScopInfo::buildCondition(BasicBlock *BB, Region &R) {
|
|
|
|
BasicBlock *RegionEntry = R.getEntry();
|
2011-04-29 14:27:02 +08:00
|
|
|
BBCond Cond;
|
|
|
|
|
|
|
|
DomTreeNode *BBNode = DT->getNode(BB), *EntryNode = DT->getNode(RegionEntry);
|
|
|
|
assert(BBNode && EntryNode && "Get null node while building condition!");
|
|
|
|
|
2015-02-24 20:00:50 +08:00
|
|
|
// Walk up the dominance tree until reaching the entry node. Collect all
|
|
|
|
// branching blocks on the path to BB except if BB postdominates the block
|
2011-04-29 14:27:02 +08:00
|
|
|
// containing the condition.
|
2015-02-24 20:00:50 +08:00
|
|
|
SmallVector<BasicBlock *, 4> DominatorBrBlocks;
|
2011-04-29 14:27:02 +08:00
|
|
|
while (BBNode != EntryNode) {
|
|
|
|
BasicBlock *CurBB = BBNode->getBlock();
|
|
|
|
BBNode = BBNode->getIDom();
|
|
|
|
assert(BBNode && "BBNode should not reach the root node!");
|
|
|
|
|
|
|
|
if (PDT->dominates(CurBB, BBNode->getBlock()))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
BranchInst *Br = dyn_cast<BranchInst>(BBNode->getBlock()->getTerminator());
|
|
|
|
assert(Br && "A Valid Scop should only contain branch instruction");
|
|
|
|
|
|
|
|
if (Br->isUnconditional())
|
|
|
|
continue;
|
|
|
|
|
2015-02-24 20:00:50 +08:00
|
|
|
DominatorBrBlocks.push_back(BBNode->getBlock());
|
|
|
|
}
|
|
|
|
|
|
|
|
RegionInfo *RI = R.getRegionInfo();
|
|
|
|
// Iterate in reverse order over the dominating blocks. Until a non-affine
|
|
|
|
// branch was encountered add all conditions collected. If a non-affine branch
|
|
|
|
// was encountered, stop as we overapproximate from here on anyway.
|
|
|
|
for (auto BIt = DominatorBrBlocks.rbegin(), BEnd = DominatorBrBlocks.rend();
|
|
|
|
BIt != BEnd; BIt++) {
|
|
|
|
|
|
|
|
BasicBlock *BBNode = *BIt;
|
|
|
|
BranchInst *Br = dyn_cast<BranchInst>(BBNode->getTerminator());
|
|
|
|
assert(Br && "A Valid Scop should only contain branch instruction");
|
|
|
|
assert(Br->isConditional() && "Assumed a conditional branch");
|
|
|
|
|
|
|
|
if (SD->isNonAffineSubRegion(RI->getRegionFor(BBNode), &R))
|
|
|
|
break;
|
|
|
|
|
2014-11-28 11:26:06 +08:00
|
|
|
BasicBlock *TrueBB = Br->getSuccessor(0), *FalseBB = Br->getSuccessor(1);
|
|
|
|
|
2011-04-29 14:27:02 +08:00
|
|
|
// Is BB on the ELSE side of the branch?
|
2014-11-28 11:26:06 +08:00
|
|
|
bool inverted = DT->dominates(FalseBB, BB);
|
|
|
|
|
|
|
|
// If both TrueBB and FalseBB dominate BB, one of them must be the target of
|
|
|
|
// a back-edge, i.e. a loop header.
|
|
|
|
if (inverted && DT->dominates(TrueBB, BB)) {
|
|
|
|
assert(
|
|
|
|
(DT->dominates(TrueBB, FalseBB) || DT->dominates(FalseBB, TrueBB)) &&
|
|
|
|
"One of the successors should be the loop header and dominate the"
|
|
|
|
"other!");
|
|
|
|
|
|
|
|
// It is not an invert if the FalseBB is the header.
|
|
|
|
if (DT->dominates(FalseBB, TrueBB))
|
|
|
|
inverted = false;
|
|
|
|
}
|
2011-04-29 14:27:02 +08:00
|
|
|
|
|
|
|
Comparison *Cmp;
|
2012-11-19 20:26:25 +08:00
|
|
|
buildAffineCondition(*(Br->getCondition()), inverted, &Cmp);
|
2011-04-29 14:27:02 +08:00
|
|
|
Cond.push_back(*Cmp);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!Cond.empty())
|
|
|
|
BBConds[BB] = Cond;
|
|
|
|
}
|
|
|
|
|
|
|
|
TempScop *TempScopInfo::buildTempScop(Region &R) {
|
2014-11-01 09:14:56 +08:00
|
|
|
TempScop *TScop = new TempScop(R, BBConds, AccFuncMap);
|
2011-04-29 14:27:02 +08:00
|
|
|
|
2015-03-02 22:06:01 +08:00
|
|
|
buildAccessFunctions(R, R);
|
|
|
|
|
|
|
|
for (const auto &BB : R.blocks())
|
2015-02-24 20:00:50 +08:00
|
|
|
buildCondition(BB, R);
|
2011-04-29 14:27:02 +08:00
|
|
|
|
|
|
|
return TScop;
|
|
|
|
}
|
|
|
|
|
|
|
|
TempScop *TempScopInfo::getTempScop(const Region *R) const {
|
|
|
|
TempScopMapType::const_iterator at = TempScops.find(R);
|
|
|
|
return at == TempScops.end() ? 0 : at->second;
|
|
|
|
}
|
|
|
|
|
2013-03-23 09:05:07 +08:00
|
|
|
void TempScopInfo::print(raw_ostream &OS, const Module *) const {
|
2011-04-29 14:27:02 +08:00
|
|
|
for (TempScopMapType::const_iterator I = TempScops.begin(),
|
2013-02-05 20:27:23 +08:00
|
|
|
E = TempScops.end();
|
|
|
|
I != E; ++I)
|
2011-04-29 14:27:02 +08:00
|
|
|
I->second->print(OS, SE, LI);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TempScopInfo::runOnFunction(Function &F) {
|
2014-01-14 06:29:56 +08:00
|
|
|
DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
|
2011-04-29 14:27:02 +08:00
|
|
|
PDT = &getAnalysis<PostDominatorTree>();
|
|
|
|
SE = &getAnalysis<ScalarEvolution>();
|
2015-01-17 22:16:56 +08:00
|
|
|
LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
|
2011-04-29 14:27:02 +08:00
|
|
|
SD = &getAnalysis<ScopDetection>();
|
|
|
|
AA = &getAnalysis<AliasAnalysis>();
|
2015-03-05 17:48:20 +08:00
|
|
|
TD = &F.getParent()->getDataLayout();
|
2013-06-10 21:55:34 +08:00
|
|
|
ZeroOffset = SE->getConstant(TD->getIntPtrType(F.getContext()), 0);
|
2011-04-29 14:27:02 +08:00
|
|
|
|
|
|
|
for (ScopDetection::iterator I = SD->begin(), E = SD->end(); I != E; ++I) {
|
2014-02-19 02:49:49 +08:00
|
|
|
if (!SD->isMaxRegionInScop(**I))
|
|
|
|
continue;
|
2013-02-05 20:27:23 +08:00
|
|
|
Region *R = const_cast<Region *>(*I);
|
2011-04-29 14:27:02 +08:00
|
|
|
TempScops.insert(std::make_pair(R, buildTempScop(*R)));
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TempScopInfo::getAnalysisUsage(AnalysisUsage &AU) const {
|
2014-01-14 06:29:56 +08:00
|
|
|
AU.addRequiredTransitive<DominatorTreeWrapperPass>();
|
2011-04-29 14:27:02 +08:00
|
|
|
AU.addRequiredTransitive<PostDominatorTree>();
|
2015-01-17 22:16:56 +08:00
|
|
|
AU.addRequiredTransitive<LoopInfoWrapperPass>();
|
2011-04-29 14:27:02 +08:00
|
|
|
AU.addRequiredTransitive<ScalarEvolution>();
|
|
|
|
AU.addRequiredTransitive<ScopDetection>();
|
|
|
|
AU.addRequiredID(IndependentBlocksID);
|
|
|
|
AU.addRequired<AliasAnalysis>();
|
|
|
|
AU.setPreservesAll();
|
|
|
|
}
|
|
|
|
|
2013-02-05 20:27:23 +08:00
|
|
|
TempScopInfo::~TempScopInfo() { clear(); }
|
2011-04-29 14:27:02 +08:00
|
|
|
|
|
|
|
void TempScopInfo::clear() {
|
|
|
|
BBConds.clear();
|
|
|
|
AccFuncMap.clear();
|
|
|
|
DeleteContainerSeconds(TempScops);
|
|
|
|
TempScops.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// TempScop information extraction pass implement
|
|
|
|
char TempScopInfo::ID = 0;
|
|
|
|
|
2013-03-23 09:05:07 +08:00
|
|
|
Pass *polly::createTempScopInfoPass() { return new TempScopInfo(); }
|
|
|
|
|
2011-10-08 08:30:40 +08:00
|
|
|
INITIALIZE_PASS_BEGIN(TempScopInfo, "polly-analyze-ir",
|
|
|
|
"Polly - Analyse the LLVM-IR in the detected regions",
|
2013-03-23 09:05:07 +08:00
|
|
|
false, false);
|
|
|
|
INITIALIZE_AG_DEPENDENCY(AliasAnalysis);
|
2014-01-14 06:29:56 +08:00
|
|
|
INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass);
|
2015-01-17 22:16:56 +08:00
|
|
|
INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass);
|
2013-03-23 09:05:07 +08:00
|
|
|
INITIALIZE_PASS_DEPENDENCY(PostDominatorTree);
|
2014-07-20 02:40:17 +08:00
|
|
|
INITIALIZE_PASS_DEPENDENCY(RegionInfoPass);
|
2013-03-23 09:05:07 +08:00
|
|
|
INITIALIZE_PASS_DEPENDENCY(ScalarEvolution);
|
2011-10-08 08:30:40 +08:00
|
|
|
INITIALIZE_PASS_END(TempScopInfo, "polly-analyze-ir",
|
|
|
|
"Polly - Analyse the LLVM-IR in the detected regions",
|
|
|
|
false, false)
|