2011-04-29 14:27:02 +08:00
|
|
|
//===- ScopHelper.cpp - Some Helper Functions for Scop. ------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// Small functions that help with Scop and LLVM-IR.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "polly/Support/ScopHelper.h"
|
2015-10-02 07:45:51 +08:00
|
|
|
#include "polly/Options.h"
|
2013-04-16 16:04:42 +08:00
|
|
|
#include "polly/ScopInfo.h"
|
2015-10-10 07:40:24 +08:00
|
|
|
#include "polly/Support/SCEVValidator.h"
|
2011-04-29 14:27:02 +08:00
|
|
|
#include "llvm/Analysis/LoopInfo.h"
|
|
|
|
#include "llvm/Analysis/RegionInfo.h"
|
2018-06-29 15:29:45 +08:00
|
|
|
#include "llvm/Analysis/RegionInfoImpl.h"
|
2011-04-29 14:27:02 +08:00
|
|
|
#include "llvm/Analysis/ScalarEvolution.h"
|
2015-08-18 19:56:00 +08:00
|
|
|
#include "llvm/Analysis/ScalarEvolutionExpander.h"
|
2011-04-29 14:27:02 +08:00
|
|
|
#include "llvm/Analysis/ScalarEvolutionExpressions.h"
|
2014-03-04 19:47:37 +08:00
|
|
|
#include "llvm/IR/CFG.h"
|
2015-10-10 07:40:24 +08:00
|
|
|
#include "llvm/IR/IntrinsicInst.h"
|
2011-04-29 14:27:02 +08:00
|
|
|
#include "llvm/Support/Debug.h"
|
2014-04-22 11:30:19 +08:00
|
|
|
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
|
2011-04-29 14:27:02 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
2015-08-18 19:56:00 +08:00
|
|
|
using namespace polly;
|
2011-04-29 14:27:02 +08:00
|
|
|
|
2014-04-22 11:30:19 +08:00
|
|
|
#define DEBUG_TYPE "polly-scop-helper"
|
|
|
|
|
2016-11-18 06:16:35 +08:00
|
|
|
static cl::opt<bool> PollyAllowErrorBlocks(
|
|
|
|
"polly-allow-error-blocks",
|
|
|
|
cl::desc("Allow to speculate on the execution of 'error blocks'."),
|
|
|
|
cl::Hidden, cl::init(true), cl::ZeroOrMore, cl::cat(PollyCategory));
|
|
|
|
|
2018-04-21 02:55:44 +08:00
|
|
|
static cl::list<std::string> DebugFunctions(
|
|
|
|
"polly-debug-func",
|
|
|
|
cl::desc("Allow calls to the specified functions in SCoPs even if their "
|
|
|
|
"side-effects are unknown. This can be used to do debug output in "
|
|
|
|
"Polly-transformed code."),
|
|
|
|
cl::Hidden, cl::ZeroOrMore, cl::CommaSeparated, cl::cat(PollyCategory));
|
|
|
|
|
2015-08-11 22:39:21 +08:00
|
|
|
// Ensures that there is just one predecessor to the entry node from outside the
|
|
|
|
// region.
|
|
|
|
// The identity of the region entry node is preserved.
|
|
|
|
static void simplifyRegionEntry(Region *R, DominatorTree *DT, LoopInfo *LI,
|
|
|
|
RegionInfo *RI) {
|
2014-09-10 22:50:23 +08:00
|
|
|
BasicBlock *EnteringBB = R->getEnteringBlock();
|
2015-08-11 22:39:21 +08:00
|
|
|
BasicBlock *Entry = R->getEntry();
|
2014-09-10 22:50:23 +08:00
|
|
|
|
2015-08-11 22:39:21 +08:00
|
|
|
// Before (one of):
|
|
|
|
//
|
|
|
|
// \ / //
|
|
|
|
// EnteringBB //
|
|
|
|
// | \------> //
|
|
|
|
// \ / | //
|
|
|
|
// Entry <--\ Entry <--\ //
|
|
|
|
// / \ / / \ / //
|
|
|
|
// .... .... //
|
2015-01-19 20:37:33 +08:00
|
|
|
|
2013-04-16 16:04:42 +08:00
|
|
|
// Create single entry edge if the region has multiple entry edges.
|
2014-09-10 22:50:23 +08:00
|
|
|
if (!EnteringBB) {
|
2015-08-11 22:39:21 +08:00
|
|
|
SmallVector<BasicBlock *, 4> Preds;
|
|
|
|
for (BasicBlock *P : predecessors(Entry))
|
|
|
|
if (!R->contains(P))
|
|
|
|
Preds.push_back(P);
|
|
|
|
|
|
|
|
BasicBlock *NewEntering =
|
|
|
|
SplitBlockPredecessors(Entry, Preds, ".region_entering", DT, LI);
|
|
|
|
|
|
|
|
if (RI) {
|
|
|
|
// The exit block of predecessing regions must be changed to NewEntering
|
|
|
|
for (BasicBlock *ExitPred : predecessors(NewEntering)) {
|
|
|
|
Region *RegionOfPred = RI->getRegionFor(ExitPred);
|
|
|
|
if (RegionOfPred->getExit() != Entry)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
while (!RegionOfPred->isTopLevelRegion() &&
|
|
|
|
RegionOfPred->getExit() == Entry) {
|
|
|
|
RegionOfPred->replaceExit(NewEntering);
|
|
|
|
RegionOfPred = RegionOfPred->getParent();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make all ancestors use EnteringBB as entry; there might be edges to it
|
|
|
|
Region *AncestorR = R->getParent();
|
|
|
|
RI->setRegionFor(NewEntering, AncestorR);
|
|
|
|
while (!AncestorR->isTopLevelRegion() && AncestorR->getEntry() == Entry) {
|
|
|
|
AncestorR->replaceEntry(NewEntering);
|
|
|
|
AncestorR = AncestorR->getParent();
|
|
|
|
}
|
2014-09-16 02:34:45 +08:00
|
|
|
}
|
|
|
|
|
2015-08-11 22:39:21 +08:00
|
|
|
EnteringBB = NewEntering;
|
2013-04-16 16:04:42 +08:00
|
|
|
}
|
2015-08-11 22:39:21 +08:00
|
|
|
assert(R->getEnteringBlock() == EnteringBB);
|
2013-04-16 16:04:42 +08:00
|
|
|
|
2015-08-11 22:39:21 +08:00
|
|
|
// After:
|
|
|
|
//
|
|
|
|
// \ / //
|
|
|
|
// EnteringBB //
|
|
|
|
// | //
|
|
|
|
// | //
|
|
|
|
// Entry <--\ //
|
|
|
|
// / \ / //
|
|
|
|
// .... //
|
|
|
|
}
|
2014-09-16 02:34:45 +08:00
|
|
|
|
2015-08-11 22:39:21 +08:00
|
|
|
// Ensure that the region has a single block that branches to the exit node.
|
|
|
|
static void simplifyRegionExit(Region *R, DominatorTree *DT, LoopInfo *LI,
|
|
|
|
RegionInfo *RI) {
|
|
|
|
BasicBlock *ExitBB = R->getExit();
|
|
|
|
BasicBlock *ExitingBB = R->getExitingBlock();
|
|
|
|
|
|
|
|
// Before:
|
|
|
|
//
|
|
|
|
// (Region) ______/ //
|
|
|
|
// \ | / //
|
|
|
|
// ExitBB //
|
|
|
|
// / \ //
|
|
|
|
|
|
|
|
if (!ExitingBB) {
|
|
|
|
SmallVector<BasicBlock *, 4> Preds;
|
|
|
|
for (BasicBlock *P : predecessors(ExitBB))
|
|
|
|
if (R->contains(P))
|
|
|
|
Preds.push_back(P);
|
|
|
|
|
|
|
|
// Preds[0] Preds[1] otherBB //
|
|
|
|
// \ | ________/ //
|
|
|
|
// \ | / //
|
|
|
|
// BB //
|
|
|
|
ExitingBB =
|
|
|
|
SplitBlockPredecessors(ExitBB, Preds, ".region_exiting", DT, LI);
|
|
|
|
// Preds[0] Preds[1] otherBB //
|
|
|
|
// \ / / //
|
|
|
|
// BB.region_exiting / //
|
|
|
|
// \ / //
|
|
|
|
// BB //
|
|
|
|
|
|
|
|
if (RI)
|
|
|
|
RI->setRegionFor(ExitingBB, R);
|
|
|
|
|
|
|
|
// Change the exit of nested regions, but not the region itself,
|
|
|
|
R->replaceExitRecursive(ExitingBB);
|
|
|
|
R->replaceExit(ExitBB);
|
2013-04-16 16:04:42 +08:00
|
|
|
}
|
2015-08-11 22:39:21 +08:00
|
|
|
assert(ExitingBB == R->getExitingBlock());
|
|
|
|
|
|
|
|
// After:
|
|
|
|
//
|
|
|
|
// \ / //
|
|
|
|
// ExitingBB _____/ //
|
|
|
|
// \ / //
|
|
|
|
// ExitBB //
|
|
|
|
// / \ //
|
|
|
|
}
|
|
|
|
|
|
|
|
void polly::simplifyRegion(Region *R, DominatorTree *DT, LoopInfo *LI,
|
|
|
|
RegionInfo *RI) {
|
|
|
|
assert(R && !R->isTopLevelRegion());
|
|
|
|
assert(!RI || RI == R->getRegionInfo());
|
|
|
|
assert((!RI || DT) &&
|
|
|
|
"RegionInfo requires DominatorTree to be updated as well");
|
2014-09-10 22:50:23 +08:00
|
|
|
|
2015-08-11 22:39:21 +08:00
|
|
|
simplifyRegionEntry(R, DT, LI, RI);
|
|
|
|
simplifyRegionExit(R, DT, LI, RI);
|
|
|
|
assert(R->isSimple());
|
2013-04-16 16:04:42 +08:00
|
|
|
}
|
|
|
|
|
2015-08-11 22:04:06 +08:00
|
|
|
// Split the block into two successive blocks.
|
|
|
|
//
|
|
|
|
// Like llvm::SplitBlock, but also preserves RegionInfo
|
|
|
|
static BasicBlock *splitBlock(BasicBlock *Old, Instruction *SplitPt,
|
|
|
|
DominatorTree *DT, llvm::LoopInfo *LI,
|
|
|
|
RegionInfo *RI) {
|
|
|
|
assert(Old && SplitPt);
|
|
|
|
|
|
|
|
// Before:
|
|
|
|
//
|
|
|
|
// \ / //
|
|
|
|
// Old //
|
|
|
|
// / \ //
|
|
|
|
|
|
|
|
BasicBlock *NewBlock = llvm::SplitBlock(Old, SplitPt, DT, LI);
|
|
|
|
|
|
|
|
if (RI) {
|
|
|
|
Region *R = RI->getRegionFor(Old);
|
|
|
|
RI->setRegionFor(NewBlock, R);
|
|
|
|
}
|
|
|
|
|
|
|
|
// After:
|
|
|
|
//
|
|
|
|
// \ / //
|
|
|
|
// Old //
|
|
|
|
// | //
|
|
|
|
// NewBlock //
|
|
|
|
// / \ //
|
|
|
|
|
|
|
|
return NewBlock;
|
|
|
|
}
|
|
|
|
|
[Polly][PM][WIP] Polly pass registration
Summary:
This patch is a first attempt at registering Polly passes with the LLVM tools. Tool plugins are still unsupported, but this registration is usable from the tools if Polly is linked into them (albeit requiring minimal patches to those tools). Registration requires a small amount of machinery (the owning analysis proxies), necessary for injecting ScopAnalysisManager objects into the calling tools.
This patch is marked WIP because the registration is incomplete. Parsing manual pipelines is fully supported, but default pass injection into the O3 pipeline is lacking, mostly because there is opportunity for some redesign here, I believe. The first point of order would be insertion points. I think it makes sense to run before the vectorizers. Running Polly Early, however, is weird. Mostly because it actually is the default (which to me is unexpected), and because Polly runs it's own O1 pipeline. Why not instead insert it at an appropriate place somewhere after simplification happend? Running after the loop optimizers seems intuitive, but it also seems wasteful, since multiple consecutive loops might well be a single scop, and we don't need to run for all of them.
My second request for comments would be regarding all those smallish helper passes we have, like PollyViewer, PollyPrinter, PollyImportJScop. Right now these are controlled by command line options, deciding whether they should be part of the Polly pipeline. What is your opinion on treating them like real passes, and have the user write an appropriate pipeline if they want to use any of them?
Reviewers: grosser, Meinersbur, bollu
Reviewed By: grosser
Subscribers: llvm-commits, pollydev
Tags: #polly
Differential Revision: https://reviews.llvm.org/D35458
llvm-svn: 309826
2017-08-02 23:52:25 +08:00
|
|
|
void polly::splitEntryBlockForAlloca(BasicBlock *EntryBlock, DominatorTree *DT,
|
|
|
|
LoopInfo *LI, RegionInfo *RI) {
|
2017-06-08 20:06:15 +08:00
|
|
|
// Find first non-alloca instruction. Every basic block has a non-alloca
|
2011-04-29 14:27:02 +08:00
|
|
|
// instruction, as every well formed basic block has a terminator.
|
|
|
|
BasicBlock::iterator I = EntryBlock->begin();
|
2013-02-15 00:42:45 +08:00
|
|
|
while (isa<AllocaInst>(I))
|
|
|
|
++I;
|
2011-04-29 14:27:02 +08:00
|
|
|
|
[Polly][PM][WIP] Polly pass registration
Summary:
This patch is a first attempt at registering Polly passes with the LLVM tools. Tool plugins are still unsupported, but this registration is usable from the tools if Polly is linked into them (albeit requiring minimal patches to those tools). Registration requires a small amount of machinery (the owning analysis proxies), necessary for injecting ScopAnalysisManager objects into the calling tools.
This patch is marked WIP because the registration is incomplete. Parsing manual pipelines is fully supported, but default pass injection into the O3 pipeline is lacking, mostly because there is opportunity for some redesign here, I believe. The first point of order would be insertion points. I think it makes sense to run before the vectorizers. Running Polly Early, however, is weird. Mostly because it actually is the default (which to me is unexpected), and because Polly runs it's own O1 pipeline. Why not instead insert it at an appropriate place somewhere after simplification happend? Running after the loop optimizers seems intuitive, but it also seems wasteful, since multiple consecutive loops might well be a single scop, and we don't need to run for all of them.
My second request for comments would be regarding all those smallish helper passes we have, like PollyViewer, PollyPrinter, PollyImportJScop. Right now these are controlled by command line options, deciding whether they should be part of the Polly pipeline. What is your opinion on treating them like real passes, and have the user write an appropriate pipeline if they want to use any of them?
Reviewers: grosser, Meinersbur, bollu
Reviewed By: grosser
Subscribers: llvm-commits, pollydev
Tags: #polly
Differential Revision: https://reviews.llvm.org/D35458
llvm-svn: 309826
2017-08-02 23:52:25 +08:00
|
|
|
// splitBlock updates DT, LI and RI.
|
|
|
|
splitBlock(EntryBlock, &*I, DT, LI, RI);
|
|
|
|
}
|
|
|
|
|
|
|
|
void polly::splitEntryBlockForAlloca(BasicBlock *EntryBlock, Pass *P) {
|
2015-01-18 18:52:23 +08:00
|
|
|
auto *DTWP = P->getAnalysisIfAvailable<DominatorTreeWrapperPass>();
|
|
|
|
auto *DT = DTWP ? &DTWP->getDomTree() : nullptr;
|
|
|
|
auto *LIWP = P->getAnalysisIfAvailable<LoopInfoWrapperPass>();
|
|
|
|
auto *LI = LIWP ? &LIWP->getLoopInfo() : nullptr;
|
2015-08-11 22:04:06 +08:00
|
|
|
RegionInfoPass *RIP = P->getAnalysisIfAvailable<RegionInfoPass>();
|
|
|
|
RegionInfo *RI = RIP ? &RIP->getRegionInfo() : nullptr;
|
2015-01-18 18:52:23 +08:00
|
|
|
|
2015-08-11 22:04:06 +08:00
|
|
|
// splitBlock updates DT, LI and RI.
|
[Polly][PM][WIP] Polly pass registration
Summary:
This patch is a first attempt at registering Polly passes with the LLVM tools. Tool plugins are still unsupported, but this registration is usable from the tools if Polly is linked into them (albeit requiring minimal patches to those tools). Registration requires a small amount of machinery (the owning analysis proxies), necessary for injecting ScopAnalysisManager objects into the calling tools.
This patch is marked WIP because the registration is incomplete. Parsing manual pipelines is fully supported, but default pass injection into the O3 pipeline is lacking, mostly because there is opportunity for some redesign here, I believe. The first point of order would be insertion points. I think it makes sense to run before the vectorizers. Running Polly Early, however, is weird. Mostly because it actually is the default (which to me is unexpected), and because Polly runs it's own O1 pipeline. Why not instead insert it at an appropriate place somewhere after simplification happend? Running after the loop optimizers seems intuitive, but it also seems wasteful, since multiple consecutive loops might well be a single scop, and we don't need to run for all of them.
My second request for comments would be regarding all those smallish helper passes we have, like PollyViewer, PollyPrinter, PollyImportJScop. Right now these are controlled by command line options, deciding whether they should be part of the Polly pipeline. What is your opinion on treating them like real passes, and have the user write an appropriate pipeline if they want to use any of them?
Reviewers: grosser, Meinersbur, bollu
Reviewed By: grosser
Subscribers: llvm-commits, pollydev
Tags: #polly
Differential Revision: https://reviews.llvm.org/D35458
llvm-svn: 309826
2017-08-02 23:52:25 +08:00
|
|
|
polly::splitEntryBlockForAlloca(EntryBlock, DT, LI, RI);
|
2011-04-29 14:27:02 +08:00
|
|
|
}
|
2015-08-18 19:56:00 +08:00
|
|
|
|
|
|
|
/// The SCEVExpander will __not__ generate any code for an existing SDiv/SRem
|
|
|
|
/// instruction but just use it, if it is referenced as a SCEVUnknown. We want
|
|
|
|
/// however to generate new code if the instruction is in the analyzed region
|
|
|
|
/// and we generate code outside/in front of that region. Hence, we generate the
|
|
|
|
/// code for the SDiv/SRem operands in front of the analyzed region and then
|
|
|
|
/// create a new SDiv/SRem operation there too.
|
|
|
|
struct ScopExpander : SCEVVisitor<ScopExpander, const SCEV *> {
|
|
|
|
friend struct SCEVVisitor<ScopExpander, const SCEV *>;
|
|
|
|
|
2016-04-09 22:30:11 +08:00
|
|
|
explicit ScopExpander(const Region &R, ScalarEvolution &SE,
|
2016-11-03 06:32:23 +08:00
|
|
|
const DataLayout &DL, const char *Name, ValueMapT *VMap,
|
|
|
|
BasicBlock *RTCBB)
|
2016-04-09 22:30:11 +08:00
|
|
|
: Expander(SCEVExpander(SE, DL, Name)), SE(SE), Name(Name), R(R),
|
2016-11-03 06:32:23 +08:00
|
|
|
VMap(VMap), RTCBB(RTCBB) {}
|
2015-08-18 19:56:00 +08:00
|
|
|
|
|
|
|
Value *expandCodeFor(const SCEV *E, Type *Ty, Instruction *I) {
|
|
|
|
// If we generate code in the region we will immediately fall back to the
|
|
|
|
// SCEVExpander, otherwise we will stop at all unknowns in the SCEV and if
|
|
|
|
// needed replace them by copies computed in the entering block.
|
|
|
|
if (!R.contains(I))
|
|
|
|
E = visit(E);
|
|
|
|
return Expander.expandCodeFor(E, Ty, I);
|
|
|
|
}
|
|
|
|
|
2018-06-28 04:35:02 +08:00
|
|
|
const SCEV *visit(const SCEV *E) {
|
|
|
|
// Cache the expansion results for intermediate SCEV expressions. A SCEV
|
|
|
|
// expression can refer to an operand multiple times (e.g. "x*x), so
|
|
|
|
// a naive visitor takes exponential time.
|
|
|
|
if (SCEVCache.count(E))
|
|
|
|
return SCEVCache[E];
|
|
|
|
const SCEV *Result = SCEVVisitor::visit(E);
|
|
|
|
SCEVCache[E] = Result;
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2015-08-18 19:56:00 +08:00
|
|
|
private:
|
|
|
|
SCEVExpander Expander;
|
|
|
|
ScalarEvolution &SE;
|
|
|
|
const char *Name;
|
|
|
|
const Region &R;
|
2015-10-01 00:52:03 +08:00
|
|
|
ValueMapT *VMap;
|
2016-11-03 06:32:23 +08:00
|
|
|
BasicBlock *RTCBB;
|
2018-06-28 04:35:02 +08:00
|
|
|
DenseMap<const SCEV *, const SCEV *> SCEVCache;
|
2015-08-18 19:56:00 +08:00
|
|
|
|
2016-06-12 03:26:08 +08:00
|
|
|
const SCEV *visitGenericInst(const SCEVUnknown *E, Instruction *Inst,
|
|
|
|
Instruction *IP) {
|
|
|
|
if (!Inst || !R.contains(Inst))
|
|
|
|
return E;
|
|
|
|
|
|
|
|
assert(!Inst->mayThrow() && !Inst->mayReadOrWriteMemory() &&
|
|
|
|
!isa<PHINode>(Inst));
|
|
|
|
|
|
|
|
auto *InstClone = Inst->clone();
|
|
|
|
for (auto &Op : Inst->operands()) {
|
|
|
|
assert(SE.isSCEVable(Op->getType()));
|
|
|
|
auto *OpSCEV = SE.getSCEV(Op);
|
|
|
|
auto *OpClone = expandCodeFor(OpSCEV, Op->getType(), IP);
|
|
|
|
InstClone->replaceUsesOfWith(Op, OpClone);
|
|
|
|
}
|
|
|
|
|
|
|
|
InstClone->setName(Name + Inst->getName());
|
|
|
|
InstClone->insertBefore(IP);
|
|
|
|
return SE.getSCEV(InstClone);
|
|
|
|
}
|
|
|
|
|
2015-08-18 19:56:00 +08:00
|
|
|
const SCEV *visitUnknown(const SCEVUnknown *E) {
|
2015-10-01 00:52:03 +08:00
|
|
|
|
|
|
|
// If a value mapping was given try if the underlying value is remapped.
|
2016-02-22 00:36:00 +08:00
|
|
|
Value *NewVal = VMap ? VMap->lookup(E->getValue()) : nullptr;
|
|
|
|
if (NewVal) {
|
|
|
|
auto *NewE = SE.getSCEV(NewVal);
|
|
|
|
|
|
|
|
// While the mapped value might be different the SCEV representation might
|
|
|
|
// not be. To this end we will check before we go into recursion here.
|
|
|
|
if (E != NewE)
|
|
|
|
return visit(NewE);
|
|
|
|
}
|
2015-10-01 00:52:03 +08:00
|
|
|
|
2015-08-18 19:56:00 +08:00
|
|
|
Instruction *Inst = dyn_cast<Instruction>(E->getValue());
|
2016-06-12 03:28:15 +08:00
|
|
|
Instruction *IP;
|
|
|
|
if (Inst && !R.contains(Inst))
|
|
|
|
IP = Inst;
|
2016-11-03 06:32:23 +08:00
|
|
|
else if (Inst && RTCBB->getParent() == Inst->getFunction())
|
|
|
|
IP = RTCBB->getTerminator();
|
2016-06-12 03:28:15 +08:00
|
|
|
else
|
2016-11-03 06:32:23 +08:00
|
|
|
IP = RTCBB->getParent()->getEntryBlock().getTerminator();
|
2016-06-12 03:28:15 +08:00
|
|
|
|
2017-02-01 18:12:09 +08:00
|
|
|
if (!Inst || (Inst->getOpcode() != Instruction::SRem &&
|
|
|
|
Inst->getOpcode() != Instruction::SDiv))
|
2016-06-12 03:28:15 +08:00
|
|
|
return visitGenericInst(E, Inst, IP);
|
2015-08-18 19:56:00 +08:00
|
|
|
|
2016-05-23 16:55:43 +08:00
|
|
|
const SCEV *LHSScev = SE.getSCEV(Inst->getOperand(0));
|
|
|
|
const SCEV *RHSScev = SE.getSCEV(Inst->getOperand(1));
|
|
|
|
|
|
|
|
if (!SE.isKnownNonZero(RHSScev))
|
|
|
|
RHSScev = SE.getUMaxExpr(RHSScev, SE.getConstant(E->getType(), 1));
|
2015-08-18 19:56:00 +08:00
|
|
|
|
2016-06-12 03:28:15 +08:00
|
|
|
Value *LHS = expandCodeFor(LHSScev, E->getType(), IP);
|
|
|
|
Value *RHS = expandCodeFor(RHSScev, E->getType(), IP);
|
2015-08-18 19:56:00 +08:00
|
|
|
|
|
|
|
Inst = BinaryOperator::Create((Instruction::BinaryOps)Inst->getOpcode(),
|
2016-06-12 03:28:15 +08:00
|
|
|
LHS, RHS, Inst->getName() + Name, IP);
|
2015-08-18 19:56:00 +08:00
|
|
|
return SE.getSCEV(Inst);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The following functions will just traverse the SCEV and rebuild it with
|
|
|
|
/// the new operands returned by the traversal.
|
|
|
|
///
|
|
|
|
///{
|
|
|
|
const SCEV *visitConstant(const SCEVConstant *E) { return E; }
|
|
|
|
const SCEV *visitTruncateExpr(const SCEVTruncateExpr *E) {
|
|
|
|
return SE.getTruncateExpr(visit(E->getOperand()), E->getType());
|
|
|
|
}
|
|
|
|
const SCEV *visitZeroExtendExpr(const SCEVZeroExtendExpr *E) {
|
|
|
|
return SE.getZeroExtendExpr(visit(E->getOperand()), E->getType());
|
|
|
|
}
|
|
|
|
const SCEV *visitSignExtendExpr(const SCEVSignExtendExpr *E) {
|
|
|
|
return SE.getSignExtendExpr(visit(E->getOperand()), E->getType());
|
|
|
|
}
|
|
|
|
const SCEV *visitUDivExpr(const SCEVUDivExpr *E) {
|
2016-04-29 18:36:58 +08:00
|
|
|
auto *RHSScev = visit(E->getRHS());
|
2016-06-06 20:09:30 +08:00
|
|
|
if (!SE.isKnownNonZero(RHSScev))
|
2016-05-23 16:55:43 +08:00
|
|
|
RHSScev = SE.getUMaxExpr(RHSScev, SE.getConstant(E->getType(), 1));
|
|
|
|
return SE.getUDivExpr(visit(E->getLHS()), RHSScev);
|
2015-08-18 19:56:00 +08:00
|
|
|
}
|
|
|
|
const SCEV *visitAddExpr(const SCEVAddExpr *E) {
|
|
|
|
SmallVector<const SCEV *, 4> NewOps;
|
|
|
|
for (const SCEV *Op : E->operands())
|
|
|
|
NewOps.push_back(visit(Op));
|
|
|
|
return SE.getAddExpr(NewOps);
|
|
|
|
}
|
|
|
|
const SCEV *visitMulExpr(const SCEVMulExpr *E) {
|
|
|
|
SmallVector<const SCEV *, 4> NewOps;
|
|
|
|
for (const SCEV *Op : E->operands())
|
|
|
|
NewOps.push_back(visit(Op));
|
|
|
|
return SE.getMulExpr(NewOps);
|
|
|
|
}
|
|
|
|
const SCEV *visitUMaxExpr(const SCEVUMaxExpr *E) {
|
|
|
|
SmallVector<const SCEV *, 4> NewOps;
|
|
|
|
for (const SCEV *Op : E->operands())
|
|
|
|
NewOps.push_back(visit(Op));
|
|
|
|
return SE.getUMaxExpr(NewOps);
|
|
|
|
}
|
|
|
|
const SCEV *visitSMaxExpr(const SCEVSMaxExpr *E) {
|
|
|
|
SmallVector<const SCEV *, 4> NewOps;
|
|
|
|
for (const SCEV *Op : E->operands())
|
|
|
|
NewOps.push_back(visit(Op));
|
|
|
|
return SE.getSMaxExpr(NewOps);
|
|
|
|
}
|
|
|
|
const SCEV *visitAddRecExpr(const SCEVAddRecExpr *E) {
|
|
|
|
SmallVector<const SCEV *, 4> NewOps;
|
|
|
|
for (const SCEV *Op : E->operands())
|
|
|
|
NewOps.push_back(visit(Op));
|
|
|
|
return SE.getAddRecExpr(NewOps, E->getLoop(), E->getNoWrapFlags());
|
|
|
|
}
|
|
|
|
///}
|
|
|
|
};
|
|
|
|
|
2015-10-08 04:17:36 +08:00
|
|
|
Value *polly::expandCodeFor(Scop &S, ScalarEvolution &SE, const DataLayout &DL,
|
|
|
|
const char *Name, const SCEV *E, Type *Ty,
|
2016-11-03 06:32:23 +08:00
|
|
|
Instruction *IP, ValueMapT *VMap,
|
|
|
|
BasicBlock *RTCBB) {
|
|
|
|
ScopExpander Expander(S.getRegion(), SE, DL, Name, VMap, RTCBB);
|
2015-08-18 19:56:00 +08:00
|
|
|
return Expander.expandCodeFor(E, Ty, IP);
|
|
|
|
}
|
2015-09-11 01:51:27 +08:00
|
|
|
|
2015-10-08 04:32:43 +08:00
|
|
|
bool polly::isErrorBlock(BasicBlock &BB, const Region &R, LoopInfo &LI,
|
|
|
|
const DominatorTree &DT) {
|
2016-11-18 06:16:35 +08:00
|
|
|
if (!PollyAllowErrorBlocks)
|
|
|
|
return false;
|
2015-09-11 01:51:27 +08:00
|
|
|
|
|
|
|
if (isa<UnreachableInst>(BB.getTerminator()))
|
|
|
|
return true;
|
|
|
|
|
2015-10-08 04:32:43 +08:00
|
|
|
if (LI.isLoopHeader(&BB))
|
|
|
|
return false;
|
|
|
|
|
2015-11-11 21:25:13 +08:00
|
|
|
// Basic blocks that are always executed are not considered error blocks,
|
|
|
|
// as their execution can not be a rare event.
|
|
|
|
bool DominatesAllPredecessors = true;
|
2017-05-25 02:39:39 +08:00
|
|
|
if (R.isTopLevelRegion()) {
|
|
|
|
for (BasicBlock &I : *R.getEntry()->getParent())
|
|
|
|
if (isa<ReturnInst>(I.getTerminator()) && !DT.dominates(&BB, &I))
|
|
|
|
DominatesAllPredecessors = false;
|
|
|
|
} else {
|
|
|
|
for (auto Pred : predecessors(R.getExit()))
|
|
|
|
if (R.contains(Pred) && !DT.dominates(&BB, Pred))
|
|
|
|
DominatesAllPredecessors = false;
|
|
|
|
}
|
2015-11-11 21:25:13 +08:00
|
|
|
|
|
|
|
if (DominatesAllPredecessors)
|
2015-10-08 04:32:43 +08:00
|
|
|
return false;
|
|
|
|
|
2015-10-02 07:45:51 +08:00
|
|
|
for (Instruction &Inst : BB)
|
2015-10-08 04:32:43 +08:00
|
|
|
if (CallInst *CI = dyn_cast<CallInst>(&Inst)) {
|
2018-04-21 02:55:44 +08:00
|
|
|
if (isDebugCall(CI))
|
|
|
|
continue;
|
|
|
|
|
2015-11-11 20:44:18 +08:00
|
|
|
if (isIgnoredIntrinsic(CI))
|
2017-08-30 02:27:42 +08:00
|
|
|
continue;
|
2015-11-11 20:44:18 +08:00
|
|
|
|
2017-08-30 02:27:47 +08:00
|
|
|
// memset, memcpy and memmove are modeled intrinsics.
|
|
|
|
if (isa<MemSetInst>(CI) || isa<MemTransferInst>(CI))
|
|
|
|
continue;
|
|
|
|
|
2015-10-08 04:32:43 +08:00
|
|
|
if (!CI->doesNotAccessMemory())
|
|
|
|
return true;
|
|
|
|
if (CI->doesNotReturn())
|
|
|
|
return true;
|
|
|
|
}
|
2015-10-02 07:45:51 +08:00
|
|
|
|
2015-09-11 01:51:27 +08:00
|
|
|
return false;
|
|
|
|
}
|
2015-09-28 17:33:22 +08:00
|
|
|
|
|
|
|
Value *polly::getConditionFromTerminator(TerminatorInst *TI) {
|
|
|
|
if (BranchInst *BR = dyn_cast<BranchInst>(TI)) {
|
|
|
|
if (BR->isUnconditional())
|
|
|
|
return ConstantInt::getTrue(Type::getInt1Ty(TI->getContext()));
|
|
|
|
|
|
|
|
return BR->getCondition();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (SwitchInst *SI = dyn_cast<SwitchInst>(TI))
|
|
|
|
return SI->getCondition();
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
2015-10-08 04:17:36 +08:00
|
|
|
|
2018-06-29 15:29:45 +08:00
|
|
|
static bool hasVariantIndex(GetElementPtrInst *Gep, Loop *L, Region &R,
|
|
|
|
ScalarEvolution &SE) {
|
|
|
|
for (const Use &Val : llvm::drop_begin(Gep->operands(), 1)) {
|
|
|
|
const SCEV *PtrSCEV = SE.getSCEVAtScope(Val, L);
|
|
|
|
Loop *OuterLoop = R.outermostLoopInRegion(L);
|
|
|
|
if (!SE.isLoopInvariant(PtrSCEV, OuterLoop))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-10-08 04:17:36 +08:00
|
|
|
bool polly::isHoistableLoad(LoadInst *LInst, Region &R, LoopInfo &LI,
|
2018-06-29 15:29:45 +08:00
|
|
|
ScalarEvolution &SE, const DominatorTree &DT,
|
|
|
|
const InvariantLoadsSetTy &KnownInvariantLoads) {
|
2015-10-08 04:17:36 +08:00
|
|
|
Loop *L = LI.getLoopFor(LInst->getParent());
|
2016-11-18 06:25:17 +08:00
|
|
|
auto *Ptr = LInst->getPointerOperand();
|
2018-06-29 15:29:45 +08:00
|
|
|
|
|
|
|
// A LoadInst is hoistable if the address it is loading from is also
|
|
|
|
// invariant; in this case: another invariant load (whether that address
|
|
|
|
// is also not written to has to be checked separately)
|
|
|
|
// TODO: This only checks for a LoadInst->GetElementPtrInst->LoadInst
|
|
|
|
// pattern generated by the Chapel frontend, but generally this applies
|
|
|
|
// for any chain of instruction that does not also depend on any
|
|
|
|
// induction variable
|
|
|
|
if (auto *GepInst = dyn_cast<GetElementPtrInst>(Ptr)) {
|
|
|
|
if (!hasVariantIndex(GepInst, L, R, SE)) {
|
|
|
|
if (auto *DecidingLoad =
|
|
|
|
dyn_cast<LoadInst>(GepInst->getPointerOperand())) {
|
|
|
|
if (KnownInvariantLoads.count(DecidingLoad))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-18 06:25:17 +08:00
|
|
|
const SCEV *PtrSCEV = SE.getSCEVAtScope(Ptr, L);
|
2015-10-08 04:17:36 +08:00
|
|
|
while (L && R.contains(L)) {
|
|
|
|
if (!SE.isLoopInvariant(PtrSCEV, L))
|
|
|
|
return false;
|
|
|
|
L = L->getParentLoop();
|
|
|
|
}
|
|
|
|
|
2016-11-18 06:25:17 +08:00
|
|
|
for (auto *User : Ptr->users()) {
|
|
|
|
auto *UserI = dyn_cast<Instruction>(User);
|
|
|
|
if (!UserI || !R.contains(UserI))
|
|
|
|
continue;
|
|
|
|
if (!UserI->mayWriteToMemory())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
auto &BB = *UserI->getParent();
|
2016-12-01 19:10:45 +08:00
|
|
|
if (DT.dominates(&BB, LInst->getParent()))
|
|
|
|
return false;
|
|
|
|
|
2016-11-18 06:25:17 +08:00
|
|
|
bool DominatesAllPredecessors = true;
|
2017-11-30 21:06:10 +08:00
|
|
|
if (R.isTopLevelRegion()) {
|
|
|
|
for (BasicBlock &I : *R.getEntry()->getParent())
|
|
|
|
if (isa<ReturnInst>(I.getTerminator()) && !DT.dominates(&BB, &I))
|
|
|
|
DominatesAllPredecessors = false;
|
|
|
|
} else {
|
|
|
|
for (auto Pred : predecessors(R.getExit()))
|
|
|
|
if (R.contains(Pred) && !DT.dominates(&BB, Pred))
|
|
|
|
DominatesAllPredecessors = false;
|
|
|
|
}
|
2016-11-18 06:25:17 +08:00
|
|
|
|
|
|
|
if (!DominatesAllPredecessors)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-10-08 04:17:36 +08:00
|
|
|
return true;
|
|
|
|
}
|
2015-10-10 07:40:24 +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:
|
2017-06-08 20:06:15 +08:00
|
|
|
// Some debug info intrinsics are supported/ignored.
|
2015-10-10 07:40:24 +08:00
|
|
|
case llvm::Intrinsic::dbg_value:
|
|
|
|
case llvm::Intrinsic::dbg_declare:
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-11-29 23:11:04 +08:00
|
|
|
bool polly::canSynthesize(const Value *V, const Scop &S, ScalarEvolution *SE,
|
2016-05-23 20:47:09 +08:00
|
|
|
Loop *Scope) {
|
2015-10-10 07:40:24 +08:00
|
|
|
if (!V || !SE->isSCEVable(V->getType()))
|
|
|
|
return false;
|
|
|
|
|
2017-07-13 20:18:56 +08:00
|
|
|
const InvariantLoadsSetTy &ILS = S.getRequiredInvariantLoads();
|
2016-03-02 05:44:06 +08:00
|
|
|
if (const SCEV *Scev = SE->getSCEVAtScope(const_cast<Value *>(V), Scope))
|
2015-10-10 07:40:24 +08:00
|
|
|
if (!isa<SCEVCouldNotCompute>(Scev))
|
2017-07-13 20:18:56 +08:00
|
|
|
if (!hasScalarDepsInsideRegion(Scev, &S.getRegion(), Scope, false, ILS))
|
2015-10-10 07:40:24 +08:00
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2016-02-06 17:19:40 +08:00
|
|
|
|
2017-07-20 04:41:56 +08:00
|
|
|
llvm::BasicBlock *polly::getUseBlock(const llvm::Use &U) {
|
2016-02-06 17:19:40 +08:00
|
|
|
Instruction *UI = dyn_cast<Instruction>(U.getUser());
|
|
|
|
if (!UI)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
if (PHINode *PHI = dyn_cast<PHINode>(UI))
|
|
|
|
return PHI->getIncomingBlock(U);
|
|
|
|
|
|
|
|
return UI->getParent();
|
|
|
|
}
|
2016-06-28 09:37:13 +08:00
|
|
|
|
|
|
|
std::tuple<std::vector<const SCEV *>, std::vector<int>>
|
|
|
|
polly::getIndexExpressionsFromGEP(GetElementPtrInst *GEP, ScalarEvolution &SE) {
|
|
|
|
std::vector<const SCEV *> Subscripts;
|
|
|
|
std::vector<int> Sizes;
|
|
|
|
|
|
|
|
Type *Ty = GEP->getPointerOperandType();
|
|
|
|
|
|
|
|
bool DroppedFirstDim = false;
|
|
|
|
|
|
|
|
for (unsigned i = 1; i < GEP->getNumOperands(); i++) {
|
|
|
|
|
|
|
|
const SCEV *Expr = SE.getSCEV(GEP->getOperand(i));
|
|
|
|
|
|
|
|
if (i == 1) {
|
|
|
|
if (auto *PtrTy = dyn_cast<PointerType>(Ty)) {
|
|
|
|
Ty = PtrTy->getElementType();
|
|
|
|
} else if (auto *ArrayTy = dyn_cast<ArrayType>(Ty)) {
|
|
|
|
Ty = ArrayTy->getElementType();
|
|
|
|
} else {
|
|
|
|
Subscripts.clear();
|
|
|
|
Sizes.clear();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (auto *Const = dyn_cast<SCEVConstant>(Expr))
|
|
|
|
if (Const->getValue()->isZero()) {
|
|
|
|
DroppedFirstDim = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
Subscripts.push_back(Expr);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto *ArrayTy = dyn_cast<ArrayType>(Ty);
|
|
|
|
if (!ArrayTy) {
|
|
|
|
Subscripts.clear();
|
|
|
|
Sizes.clear();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
Subscripts.push_back(Expr);
|
|
|
|
if (!(DroppedFirstDim && i == 2))
|
|
|
|
Sizes.push_back(ArrayTy->getNumElements());
|
|
|
|
|
|
|
|
Ty = ArrayTy->getElementType();
|
|
|
|
}
|
|
|
|
|
|
|
|
return std::make_tuple(Subscripts, Sizes);
|
|
|
|
}
|
2017-06-29 20:47:41 +08:00
|
|
|
|
|
|
|
llvm::Loop *polly::getFirstNonBoxedLoopFor(llvm::Loop *L, llvm::LoopInfo &LI,
|
|
|
|
const BoxedLoopsSetTy &BoxedLoops) {
|
|
|
|
while (BoxedLoops.count(L))
|
|
|
|
L = L->getParentLoop();
|
|
|
|
return L;
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::Loop *polly::getFirstNonBoxedLoopFor(llvm::BasicBlock *BB,
|
|
|
|
llvm::LoopInfo &LI,
|
|
|
|
const BoxedLoopsSetTy &BoxedLoops) {
|
|
|
|
Loop *L = LI.getLoopFor(BB);
|
|
|
|
return getFirstNonBoxedLoopFor(L, LI, BoxedLoops);
|
|
|
|
}
|
2018-04-21 02:55:44 +08:00
|
|
|
|
|
|
|
bool polly::isDebugCall(Instruction *Inst) {
|
|
|
|
auto *CI = dyn_cast<CallInst>(Inst);
|
|
|
|
if (!CI)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
Function *CF = CI->getCalledFunction();
|
|
|
|
if (!CF)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return std::find(DebugFunctions.begin(), DebugFunctions.end(),
|
|
|
|
CF->getName()) != DebugFunctions.end();
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool hasDebugCall(BasicBlock *BB) {
|
|
|
|
for (Instruction &Inst : *BB) {
|
|
|
|
if (isDebugCall(&Inst))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool polly::hasDebugCall(ScopStmt *Stmt) {
|
|
|
|
// Quick skip if no debug functions have been defined.
|
|
|
|
if (DebugFunctions.empty())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!Stmt)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
for (Instruction *Inst : Stmt->getInstructions())
|
|
|
|
if (isDebugCall(Inst))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (Stmt->isRegionStmt()) {
|
|
|
|
for (BasicBlock *RBB : Stmt->getRegion()->blocks())
|
|
|
|
if (RBB != Stmt->getEntryBlock() && ::hasDebugCall(RBB))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|