2011-04-29 14:27:02 +08:00
|
|
|
//===----- ScopDetection.cpp - Detect Scops --------------------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// Detect the maximal Scops of a function.
|
|
|
|
//
|
|
|
|
// A static control part (Scop) is a subgraph of the control flow graph (CFG)
|
|
|
|
// that only has statically known control flow and can therefore be described
|
|
|
|
// within the polyhedral model.
|
|
|
|
//
|
|
|
|
// Every Scop fullfills these restrictions:
|
|
|
|
//
|
|
|
|
// * It is a single entry single exit region
|
|
|
|
//
|
|
|
|
// * Only affine linear bounds in the loops
|
|
|
|
//
|
|
|
|
// Every natural loop in a Scop must have a number of loop iterations that can
|
|
|
|
// be described as an affine linear function in surrounding loop iterators or
|
|
|
|
// parameters. (A parameter is a scalar that does not change its value during
|
|
|
|
// execution of the Scop).
|
|
|
|
//
|
|
|
|
// * Only comparisons of affine linear expressions in conditions
|
|
|
|
//
|
|
|
|
// * All loops and conditions perfectly nested
|
|
|
|
//
|
|
|
|
// The control flow needs to be structured such that it could be written using
|
|
|
|
// just 'for' and 'if' statements, without the need for any 'goto', 'break' or
|
|
|
|
// 'continue'.
|
|
|
|
//
|
|
|
|
// * Side effect free functions call
|
|
|
|
//
|
2016-02-22 03:13:19 +08:00
|
|
|
// Function calls and intrinsics that do not have side effects (readnone)
|
|
|
|
// or memory intrinsics (memset, memcpy, memmove) are allowed.
|
2011-04-29 14:27:02 +08:00
|
|
|
//
|
|
|
|
// The Scop detection finds the largest Scops by checking if the largest
|
|
|
|
// region is a Scop. If this is not the case, its canonical subregions are
|
|
|
|
// checked until a region is a Scop. It is now tried to extend this Scop by
|
|
|
|
// creating a larger non canonical region.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2015-12-21 20:38:56 +08:00
|
|
|
#include "polly/ScopDetection.h"
|
2015-05-09 17:13:42 +08:00
|
|
|
#include "polly/CodeGen/CodeGeneration.h"
|
2011-04-29 14:27:02 +08:00
|
|
|
#include "polly/LinkAllPasses.h"
|
2013-05-07 15:31:10 +08:00
|
|
|
#include "polly/Options.h"
|
2015-05-09 17:13:42 +08:00
|
|
|
#include "polly/ScopDetectionDiagnostic.h"
|
2011-11-07 20:58:54 +08:00
|
|
|
#include "polly/Support/SCEVValidator.h"
|
2015-05-03 13:21:36 +08:00
|
|
|
#include "polly/Support/ScopLocation.h"
|
2011-04-29 14:27:02 +08:00
|
|
|
#include "llvm/ADT/Statistic.h"
|
|
|
|
#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"
|
2011-04-29 14:27:02 +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:50 +08:00
|
|
|
#include "llvm/Analysis/ScalarEvolutionExpressions.h"
|
2014-03-06 08:47:27 +08:00
|
|
|
#include "llvm/IR/DebugInfo.h"
|
2013-12-18 18:49:53 +08:00
|
|
|
#include "llvm/IR/DiagnosticInfo.h"
|
|
|
|
#include "llvm/IR/DiagnosticPrinter.h"
|
2015-05-09 17:13:42 +08:00
|
|
|
#include "llvm/IR/IntrinsicInst.h"
|
2014-03-06 08:47:27 +08:00
|
|
|
#include "llvm/IR/LLVMContext.h"
|
2011-04-29 14:27:02 +08:00
|
|
|
#include "llvm/Support/Debug.h"
|
2011-11-08 23:41:28 +08:00
|
|
|
#include <set>
|
2016-01-22 17:44:37 +08:00
|
|
|
#include <stack>
|
2011-11-08 23:41:28 +08:00
|
|
|
|
2011-04-29 14:27:02 +08:00
|
|
|
using namespace llvm;
|
|
|
|
using namespace polly;
|
|
|
|
|
2014-04-22 11:30:19 +08:00
|
|
|
#define DEBUG_TYPE "polly-detect"
|
|
|
|
|
2015-12-22 05:00:43 +08:00
|
|
|
// This option is set to a very high value, as analyzing such loops increases
|
|
|
|
// compile time on several cases. For experiments that enable this option,
|
|
|
|
// a value of around 40 has been working to avoid run-time regressions with
|
|
|
|
// Polly while still exposing interesting optimization opportunities.
|
|
|
|
static cl::opt<int> ProfitabilityMinPerLoopInstructions(
|
|
|
|
"polly-detect-profitability-min-per-loop-insts",
|
|
|
|
cl::desc("The minimal number of per-loop instructions before a single loop "
|
|
|
|
"region is considered profitable"),
|
|
|
|
cl::Hidden, cl::ValueRequired, cl::init(100000000), cl::cat(PollyCategory));
|
|
|
|
|
2015-10-07 00:10:29 +08:00
|
|
|
bool polly::PollyProcessUnprofitable;
|
|
|
|
static cl::opt<bool, true> XPollyProcessUnprofitable(
|
|
|
|
"polly-process-unprofitable",
|
|
|
|
cl::desc(
|
|
|
|
"Process scops that are unlikely to benefit from Polly optimizations."),
|
|
|
|
cl::location(PollyProcessUnprofitable), cl::init(false), cl::ZeroOrMore,
|
|
|
|
cl::cat(PollyCategory));
|
2015-02-19 13:31:07 +08:00
|
|
|
|
2014-07-09 18:50:10 +08:00
|
|
|
static cl::opt<std::string> OnlyFunction(
|
|
|
|
"polly-only-func",
|
|
|
|
cl::desc("Only run on functions that contain a certain string"),
|
|
|
|
cl::value_desc("string"), cl::ValueRequired, cl::init(""),
|
|
|
|
cl::cat(PollyCategory));
|
|
|
|
|
|
|
|
static cl::opt<std::string> OnlyRegion(
|
|
|
|
"polly-only-region",
|
|
|
|
cl::desc("Only run on certain regions (The provided identifier must "
|
|
|
|
"appear in the name of the region's entry block"),
|
|
|
|
cl::value_desc("identifier"), cl::ValueRequired, cl::init(""),
|
|
|
|
cl::cat(PollyCategory));
|
2014-01-27 22:24:53 +08:00
|
|
|
|
2011-11-10 20:47:26 +08:00
|
|
|
static cl::opt<bool>
|
2014-07-09 18:50:10 +08:00
|
|
|
IgnoreAliasing("polly-ignore-aliasing",
|
|
|
|
cl::desc("Ignore possible aliasing of the array bases"),
|
|
|
|
cl::Hidden, cl::init(false), cl::ZeroOrMore,
|
|
|
|
cl::cat(PollyCategory));
|
2011-10-23 19:17:06 +08:00
|
|
|
|
2014-09-18 19:17:17 +08:00
|
|
|
bool polly::PollyUseRuntimeAliasChecks;
|
|
|
|
static cl::opt<bool, true> XPollyUseRuntimeAliasChecks(
|
|
|
|
"polly-use-runtime-alias-checks",
|
|
|
|
cl::desc("Use runtime alias checks to resolve possible aliasing."),
|
|
|
|
cl::location(PollyUseRuntimeAliasChecks), cl::Hidden, cl::ZeroOrMore,
|
|
|
|
cl::init(true), cl::cat(PollyCategory));
|
|
|
|
|
2013-05-07 15:31:10 +08:00
|
|
|
static cl::opt<bool>
|
2014-07-09 18:50:10 +08:00
|
|
|
ReportLevel("polly-report",
|
|
|
|
cl::desc("Print information about the activities of Polly"),
|
|
|
|
cl::init(false), cl::ZeroOrMore, cl::cat(PollyCategory));
|
2012-11-02 00:45:20 +08:00
|
|
|
|
2016-02-07 16:48:57 +08:00
|
|
|
static cl::opt<bool> AllowDifferentTypes(
|
|
|
|
"polly-allow-differing-element-types",
|
|
|
|
cl::desc("Allow different element types for array accesses"), cl::Hidden,
|
2016-02-16 22:37:24 +08:00
|
|
|
cl::init(true), cl::ZeroOrMore, cl::cat(PollyCategory));
|
2016-02-07 16:48:57 +08:00
|
|
|
|
2011-12-20 18:43:14 +08:00
|
|
|
static cl::opt<bool>
|
2014-07-09 18:50:10 +08:00
|
|
|
AllowNonAffine("polly-allow-nonaffine",
|
|
|
|
cl::desc("Allow non affine access functions in arrays"),
|
|
|
|
cl::Hidden, cl::init(false), cl::ZeroOrMore,
|
|
|
|
cl::cat(PollyCategory));
|
2011-12-20 18:43:14 +08:00
|
|
|
|
2015-02-24 19:45:21 +08:00
|
|
|
static cl::opt<bool> AllowNonAffineSubRegions(
|
|
|
|
"polly-allow-nonaffine-branches",
|
|
|
|
cl::desc("Allow non affine conditions for branches"), cl::Hidden,
|
2015-02-26 19:09:24 +08:00
|
|
|
cl::init(true), cl::ZeroOrMore, cl::cat(PollyCategory));
|
2015-02-24 19:45:21 +08:00
|
|
|
|
2015-04-13 06:52:20 +08:00
|
|
|
static cl::opt<bool>
|
|
|
|
AllowNonAffineSubLoops("polly-allow-nonaffine-loops",
|
|
|
|
cl::desc("Allow non affine conditions for loops"),
|
|
|
|
cl::Hidden, cl::init(false), cl::ZeroOrMore,
|
|
|
|
cl::cat(PollyCategory));
|
|
|
|
|
2015-01-09 08:01:33 +08:00
|
|
|
static cl::opt<bool> AllowUnsigned("polly-allow-unsigned",
|
|
|
|
cl::desc("Allow unsigned expressions"),
|
|
|
|
cl::Hidden, cl::init(false), cl::ZeroOrMore,
|
|
|
|
cl::cat(PollyCategory));
|
|
|
|
|
2013-07-25 11:02:29 +08:00
|
|
|
static cl::opt<bool, true>
|
2014-07-09 18:50:10 +08:00
|
|
|
TrackFailures("polly-detect-track-failures",
|
|
|
|
cl::desc("Track failure strings in detecting scop regions"),
|
|
|
|
cl::location(PollyTrackFailures), cl::Hidden, cl::ZeroOrMore,
|
2014-08-17 18:09:03 +08:00
|
|
|
cl::init(true), cl::cat(PollyCategory));
|
2013-07-25 11:02:29 +08:00
|
|
|
|
2014-05-24 17:25:10 +08:00
|
|
|
static cl::opt<bool> KeepGoing("polly-detect-keep-going",
|
|
|
|
cl::desc("Do not fail on the first error."),
|
|
|
|
cl::Hidden, cl::ZeroOrMore, cl::init(false),
|
|
|
|
cl::cat(PollyCategory));
|
|
|
|
|
2014-04-09 05:20:44 +08:00
|
|
|
static cl::opt<bool, true>
|
2014-07-09 18:50:10 +08:00
|
|
|
PollyDelinearizeX("polly-delinearize",
|
|
|
|
cl::desc("Delinearize array access functions"),
|
|
|
|
cl::location(PollyDelinearize), cl::Hidden,
|
2015-03-08 23:21:18 +08:00
|
|
|
cl::ZeroOrMore, cl::init(true), cl::cat(PollyCategory));
|
2014-04-09 05:20:44 +08:00
|
|
|
|
2014-02-19 02:49:49 +08:00
|
|
|
static cl::opt<bool>
|
2014-07-09 18:50:10 +08:00
|
|
|
VerifyScops("polly-detect-verify",
|
|
|
|
cl::desc("Verify the detected SCoPs after each transformation"),
|
|
|
|
cl::Hidden, cl::init(false), cl::ZeroOrMore,
|
|
|
|
cl::cat(PollyCategory));
|
2014-02-19 02:49:49 +08:00
|
|
|
|
2016-02-27 00:43:35 +08:00
|
|
|
bool polly::PollyInvariantLoadHoisting;
|
|
|
|
static cl::opt<bool, true> XPollyInvariantLoadHoisting(
|
|
|
|
"polly-invariant-load-hoisting", cl::desc("Hoist invariant loads."),
|
|
|
|
cl::location(PollyInvariantLoadHoisting), cl::Hidden, cl::ZeroOrMore,
|
|
|
|
cl::init(true), cl::cat(PollyCategory));
|
|
|
|
|
2015-09-22 03:10:11 +08:00
|
|
|
/// @brief The minimal trip count under which loops are considered unprofitable.
|
|
|
|
static const unsigned MIN_LOOP_TRIP_COUNT = 8;
|
|
|
|
|
2013-07-25 11:02:29 +08:00
|
|
|
bool polly::PollyTrackFailures = false;
|
2014-04-09 05:20:44 +08:00
|
|
|
bool polly::PollyDelinearize = false;
|
2014-07-16 05:06:48 +08:00
|
|
|
StringRef polly::PollySkipFnAttr = "polly.skip.fn";
|
2013-07-25 11:02:29 +08:00
|
|
|
|
2011-04-29 14:27:02 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Statistics.
|
|
|
|
|
|
|
|
STATISTIC(ValidRegion, "Number of regions that a valid part of Scop");
|
|
|
|
|
2013-12-18 18:49:53 +08:00
|
|
|
class DiagnosticScopFound : public DiagnosticInfo {
|
|
|
|
private:
|
|
|
|
static int PluginDiagnosticKind;
|
|
|
|
|
|
|
|
Function &F;
|
|
|
|
std::string FileName;
|
|
|
|
unsigned EntryLine, ExitLine;
|
|
|
|
|
|
|
|
public:
|
2013-12-18 19:14:36 +08:00
|
|
|
DiagnosticScopFound(Function &F, std::string FileName, unsigned EntryLine,
|
|
|
|
unsigned ExitLine)
|
2013-12-18 18:49:53 +08:00
|
|
|
: DiagnosticInfo(PluginDiagnosticKind, DS_Note), F(F), FileName(FileName),
|
2013-12-18 19:14:36 +08:00
|
|
|
EntryLine(EntryLine), ExitLine(ExitLine) {}
|
2013-12-18 18:49:53 +08:00
|
|
|
|
|
|
|
virtual void print(DiagnosticPrinter &DP) const;
|
|
|
|
|
|
|
|
static bool classof(const DiagnosticInfo *DI) {
|
|
|
|
return DI->getKind() == PluginDiagnosticKind;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
int DiagnosticScopFound::PluginDiagnosticKind = 10;
|
|
|
|
|
|
|
|
void DiagnosticScopFound::print(DiagnosticPrinter &DP) const {
|
2013-12-18 19:14:36 +08:00
|
|
|
DP << "Polly detected an optimizable loop region (scop) in function '" << F
|
|
|
|
<< "'\n";
|
2013-12-18 18:49:53 +08:00
|
|
|
|
|
|
|
if (FileName.empty()) {
|
|
|
|
DP << "Scop location is unknown. Compile with debug info "
|
|
|
|
"(-g) to get more precise information. ";
|
2013-12-18 19:14:36 +08:00
|
|
|
return;
|
2013-12-18 18:49:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
DP << FileName << ":" << EntryLine << ": Start of scop\n";
|
|
|
|
DP << FileName << ":" << ExitLine << ": End of scop";
|
|
|
|
}
|
|
|
|
|
2011-04-29 14:27:02 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// ScopDetection.
|
2014-04-02 19:54:01 +08:00
|
|
|
|
2014-09-18 19:17:17 +08:00
|
|
|
ScopDetection::ScopDetection() : FunctionPass(ID) {
|
2014-09-30 01:06:29 +08:00
|
|
|
// Disable runtime alias checks if we ignore aliasing all together.
|
2016-02-25 22:07:49 +08:00
|
|
|
if (IgnoreAliasing)
|
2014-09-30 01:06:29 +08:00
|
|
|
PollyUseRuntimeAliasChecks = false;
|
2014-09-18 19:17:17 +08:00
|
|
|
}
|
|
|
|
|
2014-04-02 19:54:01 +08:00
|
|
|
template <class RR, typename... Args>
|
|
|
|
inline bool ScopDetection::invalid(DetectionContext &Context, bool Assert,
|
|
|
|
Args &&... Arguments) const {
|
|
|
|
|
|
|
|
if (!Context.Verifying) {
|
2014-05-24 17:25:01 +08:00
|
|
|
RejectLog &Log = Context.Log;
|
|
|
|
std::shared_ptr<RR> RejectReason = std::make_shared<RR>(Arguments...);
|
2014-04-02 19:54:01 +08:00
|
|
|
|
2014-05-24 17:25:06 +08:00
|
|
|
if (PollyTrackFailures)
|
2014-05-24 17:25:01 +08:00
|
|
|
Log.report(RejectReason);
|
|
|
|
|
|
|
|
DEBUG(dbgs() << RejectReason->getMessage());
|
2014-04-02 19:54:01 +08:00
|
|
|
DEBUG(dbgs() << "\n");
|
|
|
|
} else {
|
|
|
|
assert(!Assert && "Verification of detected scop failed");
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-02-19 02:49:49 +08:00
|
|
|
bool ScopDetection::isMaxRegionInScop(const Region &R, bool Verify) const {
|
|
|
|
if (!ValidRegions.count(&R))
|
|
|
|
return false;
|
|
|
|
|
2015-02-20 02:11:50 +08:00
|
|
|
if (Verify) {
|
2015-10-25 18:55:35 +08:00
|
|
|
DetectionContextMap.erase(&R);
|
|
|
|
const auto &It = DetectionContextMap.insert(
|
|
|
|
std::make_pair(&R, DetectionContext(const_cast<Region &>(R), *AA,
|
|
|
|
false /*verifying*/)));
|
|
|
|
DetectionContext &Context = It.first->second;
|
2015-02-20 02:11:50 +08:00
|
|
|
return isValidRegion(Context);
|
|
|
|
}
|
2014-02-19 02:49:49 +08:00
|
|
|
|
|
|
|
return true;
|
2011-04-29 14:27:02 +08:00
|
|
|
}
|
|
|
|
|
2011-10-08 08:30:55 +08:00
|
|
|
std::string ScopDetection::regionIsInvalidBecause(const Region *R) const {
|
2014-05-24 17:25:06 +08:00
|
|
|
if (!RejectLogs.count(R))
|
2011-10-08 08:30:55 +08:00
|
|
|
return "";
|
|
|
|
|
2014-05-24 17:25:06 +08:00
|
|
|
// Get the first error we found. Even in keep-going mode, this is the first
|
|
|
|
// reason that caused the candidate to be rejected.
|
|
|
|
RejectLog Errors = RejectLogs.at(R);
|
2014-06-12 15:25:08 +08:00
|
|
|
|
|
|
|
// This can happen when we marked a region invalid, but didn't track
|
|
|
|
// an error for it.
|
|
|
|
if (Errors.size() == 0)
|
|
|
|
return "";
|
|
|
|
|
|
|
|
RejectReasonPtr RR = *Errors.begin();
|
|
|
|
return RR->getMessage();
|
2011-10-08 08:30:55 +08:00
|
|
|
}
|
|
|
|
|
2015-04-13 06:52:20 +08:00
|
|
|
bool ScopDetection::addOverApproximatedRegion(Region *AR,
|
|
|
|
DetectionContext &Context) const {
|
|
|
|
|
|
|
|
// If we already know about Ar we can exit.
|
|
|
|
if (!Context.NonAffineSubRegionSet.insert(AR))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// All loops in the region have to be overapproximated too if there
|
|
|
|
// are accesses that depend on the iteration count.
|
|
|
|
for (BasicBlock *BB : AR->blocks()) {
|
2015-02-24 19:45:21 +08:00
|
|
|
Loop *L = LI->getLoopFor(BB);
|
2015-04-13 06:52:20 +08:00
|
|
|
if (AR->contains(L))
|
|
|
|
Context.BoxedLoopsSet.insert(L);
|
2015-02-24 19:45:21 +08:00
|
|
|
}
|
2015-04-13 06:52:20 +08:00
|
|
|
|
|
|
|
return (AllowNonAffineSubLoops || Context.BoxedLoopsSet.empty());
|
2015-02-24 19:45:21 +08:00
|
|
|
}
|
|
|
|
|
2015-10-08 04:17:36 +08:00
|
|
|
bool ScopDetection::onlyValidRequiredInvariantLoads(
|
|
|
|
InvariantLoadsSetTy &RequiredILS, DetectionContext &Context) const {
|
|
|
|
Region &CurRegion = Context.CurRegion;
|
|
|
|
|
2016-02-27 00:43:35 +08:00
|
|
|
if (!PollyInvariantLoadHoisting && !RequiredILS.empty())
|
|
|
|
return false;
|
|
|
|
|
2015-10-08 04:17:36 +08:00
|
|
|
for (LoadInst *Load : RequiredILS)
|
|
|
|
if (!isHoistableLoad(Load, CurRegion, *LI, *SE))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
Context.RequiredILS.insert(RequiredILS.begin(), RequiredILS.end());
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ScopDetection::isAffine(const SCEV *S, DetectionContext &Context,
|
|
|
|
Value *BaseAddress) const {
|
|
|
|
|
|
|
|
InvariantLoadsSetTy AccessILS;
|
|
|
|
if (!isAffineExpr(&Context.CurRegion, S, *SE, BaseAddress, &AccessILS))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!onlyValidRequiredInvariantLoads(AccessILS, Context))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-09-28 17:33:22 +08:00
|
|
|
bool ScopDetection::isValidSwitch(BasicBlock &BB, SwitchInst *SI,
|
2015-10-04 22:54:27 +08:00
|
|
|
Value *Condition, bool IsLoopBranch,
|
2015-09-28 17:33:22 +08:00
|
|
|
DetectionContext &Context) const {
|
|
|
|
Loop *L = LI->getLoopFor(&BB);
|
|
|
|
const SCEV *ConditionSCEV = SE->getSCEVAtScope(Condition, L);
|
2011-04-29 14:27:02 +08:00
|
|
|
|
2015-10-08 04:17:36 +08:00
|
|
|
if (isAffine(ConditionSCEV, Context))
|
2015-10-04 22:54:27 +08:00
|
|
|
return true;
|
2011-04-29 14:27:02 +08:00
|
|
|
|
2015-10-04 22:54:27 +08:00
|
|
|
if (!IsLoopBranch && AllowNonAffineSubRegions &&
|
|
|
|
addOverApproximatedRegion(RI->getRegionFor(&BB), Context))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (IsLoopBranch)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return invalid<ReportNonAffBranch>(Context, /*Assert=*/true, &BB,
|
|
|
|
ConditionSCEV, ConditionSCEV, SI);
|
2015-09-28 17:33:22 +08:00
|
|
|
}
|
2011-04-29 14:27:02 +08:00
|
|
|
|
2015-09-28 17:33:22 +08:00
|
|
|
bool ScopDetection::isValidBranch(BasicBlock &BB, BranchInst *BI,
|
2015-10-04 22:54:27 +08:00
|
|
|
Value *Condition, bool IsLoopBranch,
|
2015-09-28 17:33:22 +08:00
|
|
|
DetectionContext &Context) const {
|
2015-10-11 21:21:03 +08:00
|
|
|
|
|
|
|
if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(Condition)) {
|
|
|
|
auto Opcode = BinOp->getOpcode();
|
|
|
|
if (Opcode == Instruction::And || Opcode == Instruction::Or) {
|
|
|
|
Value *Op0 = BinOp->getOperand(0);
|
|
|
|
Value *Op1 = BinOp->getOperand(1);
|
|
|
|
return isValidBranch(BB, BI, Op0, IsLoopBranch, Context) &&
|
|
|
|
isValidBranch(BB, BI, Op1, IsLoopBranch, Context);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-28 17:33:22 +08:00
|
|
|
// Non constant conditions of branches need to be ICmpInst.
|
|
|
|
if (!isa<ICmpInst>(Condition)) {
|
2015-10-04 22:54:27 +08:00
|
|
|
if (!IsLoopBranch && AllowNonAffineSubRegions &&
|
|
|
|
addOverApproximatedRegion(RI->getRegionFor(&BB), Context))
|
|
|
|
return true;
|
|
|
|
return invalid<ReportInvalidCond>(Context, /*Assert=*/true, BI, &BB);
|
2015-02-24 19:45:21 +08:00
|
|
|
}
|
2011-04-29 14:27:02 +08:00
|
|
|
|
2015-10-04 22:54:27 +08:00
|
|
|
ICmpInst *ICmp = cast<ICmpInst>(Condition);
|
|
|
|
// Unsigned comparisons are not allowed. They trigger overflow problems
|
|
|
|
// in the code generation.
|
|
|
|
//
|
|
|
|
// TODO: This is not sufficient and just hides bugs. However it does pretty
|
|
|
|
// well.
|
|
|
|
if (ICmp->isUnsigned() && !AllowUnsigned)
|
|
|
|
return invalid<ReportUnsignedCond>(Context, /*Assert=*/true, BI, &BB);
|
|
|
|
|
|
|
|
// Are both operands of the ICmp affine?
|
|
|
|
if (isa<UndefValue>(ICmp->getOperand(0)) ||
|
|
|
|
isa<UndefValue>(ICmp->getOperand(1)))
|
|
|
|
return invalid<ReportUndefOperand>(Context, /*Assert=*/true, &BB, ICmp);
|
|
|
|
|
|
|
|
// TODO: FIXME: IslExprBuilder is not capable of producing valid code
|
|
|
|
// for arbitrary pointer expressions at the moment. Until
|
|
|
|
// this is fixed we disallow pointer expressions completely.
|
|
|
|
if (ICmp->getOperand(0)->getType()->isPointerTy())
|
|
|
|
return false;
|
2015-09-09 22:19:04 +08:00
|
|
|
|
2015-10-04 22:54:27 +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
|
|
|
|
2015-10-08 04:17:36 +08:00
|
|
|
if (isAffine(LHS, Context) && isAffine(RHS, Context))
|
2015-10-04 22:54:27 +08:00
|
|
|
return true;
|
2011-04-29 14:27:02 +08:00
|
|
|
|
2015-10-04 22:54:27 +08:00
|
|
|
if (!IsLoopBranch && AllowNonAffineSubRegions &&
|
|
|
|
addOverApproximatedRegion(RI->getRegionFor(&BB), Context))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (IsLoopBranch)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return invalid<ReportNonAffBranch>(Context, /*Assert=*/true, &BB, LHS, RHS,
|
|
|
|
ICmp);
|
2011-04-29 14:27:02 +08:00
|
|
|
}
|
|
|
|
|
2015-10-04 22:54:27 +08:00
|
|
|
bool ScopDetection::isValidCFG(BasicBlock &BB, bool IsLoopBranch,
|
2015-11-11 16:42:20 +08:00
|
|
|
bool AllowUnreachable,
|
2015-09-28 17:33:22 +08:00
|
|
|
DetectionContext &Context) const {
|
|
|
|
Region &CurRegion = Context.CurRegion;
|
|
|
|
|
|
|
|
TerminatorInst *TI = BB.getTerminator();
|
|
|
|
|
2015-11-11 16:42:20 +08:00
|
|
|
if (AllowUnreachable && isa<UnreachableInst>(TI))
|
|
|
|
return true;
|
|
|
|
|
2015-09-28 17:33:22 +08:00
|
|
|
// Return instructions are only valid if the region is the top level region.
|
|
|
|
if (isa<ReturnInst>(TI) && !CurRegion.getExit() && TI->getNumOperands() == 0)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
Value *Condition = getConditionFromTerminator(TI);
|
|
|
|
|
|
|
|
if (!Condition)
|
|
|
|
return invalid<ReportInvalidTerminator>(Context, /*Assert=*/true, &BB);
|
|
|
|
|
|
|
|
// UndefValue is not allowed as condition.
|
|
|
|
if (isa<UndefValue>(Condition))
|
|
|
|
return invalid<ReportUndefCond>(Context, /*Assert=*/true, TI, &BB);
|
|
|
|
|
2015-10-19 06:56:42 +08:00
|
|
|
// Constant integer conditions are always affine.
|
|
|
|
if (isa<ConstantInt>(Condition))
|
2015-09-28 17:33:22 +08:00
|
|
|
return true;
|
|
|
|
|
|
|
|
if (BranchInst *BI = dyn_cast<BranchInst>(TI))
|
2015-10-04 22:54:27 +08:00
|
|
|
return isValidBranch(BB, BI, Condition, IsLoopBranch, Context);
|
2015-09-28 17:33:22 +08:00
|
|
|
|
|
|
|
SwitchInst *SI = dyn_cast<SwitchInst>(TI);
|
|
|
|
assert(SI && "Terminator was neither branch nor switch");
|
|
|
|
|
2015-10-04 22:54:27 +08:00
|
|
|
return isValidSwitch(BB, SI, Condition, IsLoopBranch, Context);
|
2015-09-28 17:33:22 +08:00
|
|
|
}
|
|
|
|
|
2016-02-22 03:13:19 +08:00
|
|
|
bool ScopDetection::isValidCallInst(CallInst &CI,
|
|
|
|
DetectionContext &Context) const {
|
2015-01-26 02:07:30 +08:00
|
|
|
if (CI.doesNotReturn())
|
2011-04-29 14:27:02 +08:00
|
|
|
return false;
|
|
|
|
|
|
|
|
if (CI.doesNotAccessMemory())
|
|
|
|
return true;
|
|
|
|
|
2016-02-22 03:13:19 +08:00
|
|
|
if (auto *II = dyn_cast<IntrinsicInst>(&CI))
|
2016-02-25 22:08:48 +08:00
|
|
|
if (isValidIntrinsicInst(*II, Context))
|
|
|
|
return true;
|
2016-02-22 03:13:19 +08:00
|
|
|
|
2011-04-29 14:27:02 +08:00
|
|
|
Function *CalledFunction = CI.getCalledFunction();
|
|
|
|
|
|
|
|
// Indirect calls are not supported.
|
|
|
|
if (CalledFunction == 0)
|
|
|
|
return false;
|
|
|
|
|
2016-02-25 22:08:48 +08:00
|
|
|
switch (AA->getModRefBehavior(CalledFunction)) {
|
|
|
|
case llvm::FMRB_UnknownModRefBehavior:
|
|
|
|
return false;
|
|
|
|
case llvm::FMRB_DoesNotAccessMemory:
|
|
|
|
case llvm::FMRB_OnlyReadsMemory:
|
|
|
|
// Implicitly disable delinearization since we have an unknown
|
|
|
|
// accesses with an unknown access function.
|
|
|
|
Context.HasUnknownAccess = true;
|
|
|
|
Context.AST.add(&CI);
|
|
|
|
return true;
|
|
|
|
case llvm::FMRB_OnlyReadsArgumentPointees:
|
|
|
|
case llvm::FMRB_OnlyAccessesArgumentPointees:
|
|
|
|
for (const auto &Arg : CI.arg_operands()) {
|
|
|
|
if (!Arg->getType()->isPointerTy())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Bail if a pointer argument has a base address not known to
|
|
|
|
// ScalarEvolution. Note that a zero pointer is acceptable.
|
|
|
|
auto *ArgSCEV = SE->getSCEVAtScope(Arg, LI->getLoopFor(CI.getParent()));
|
|
|
|
if (ArgSCEV->isZero())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
auto *BP = dyn_cast<SCEVUnknown>(SE->getPointerBase(ArgSCEV));
|
|
|
|
if (!BP)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Implicitly disable delinearization since we have an unknown
|
|
|
|
// accesses with an unknown access function.
|
|
|
|
Context.HasUnknownAccess = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
Context.AST.add(&CI);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-02-22 03:13:19 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ScopDetection::isValidIntrinsicInst(IntrinsicInst &II,
|
|
|
|
DetectionContext &Context) const {
|
|
|
|
if (isIgnoredIntrinsic(&II))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// The closest loop surrounding the call instruction.
|
|
|
|
Loop *L = LI->getLoopFor(II.getParent());
|
|
|
|
|
|
|
|
// The access function and base pointer for memory intrinsics.
|
|
|
|
const SCEV *AF;
|
|
|
|
const SCEVUnknown *BP;
|
|
|
|
|
|
|
|
switch (II.getIntrinsicID()) {
|
|
|
|
// Memory intrinsics that can be represented are supported.
|
|
|
|
case llvm::Intrinsic::memmove:
|
|
|
|
case llvm::Intrinsic::memcpy:
|
|
|
|
AF = SE->getSCEVAtScope(cast<MemTransferInst>(II).getSource(), L);
|
|
|
|
BP = dyn_cast<SCEVUnknown>(SE->getPointerBase(AF));
|
|
|
|
// Bail if the source pointer is not valid.
|
|
|
|
if (!isValidAccess(&II, AF, BP, Context))
|
|
|
|
return false;
|
|
|
|
// Fall through
|
|
|
|
case llvm::Intrinsic::memset:
|
|
|
|
AF = SE->getSCEVAtScope(cast<MemIntrinsic>(II).getDest(), L);
|
|
|
|
BP = dyn_cast<SCEVUnknown>(SE->getPointerBase(AF));
|
|
|
|
// Bail if the destination pointer is not valid.
|
|
|
|
if (!isValidAccess(&II, AF, BP, Context))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Bail if the length is not affine.
|
|
|
|
if (!isAffine(SE->getSCEVAtScope(cast<MemIntrinsic>(II).getLength(), L),
|
|
|
|
Context))
|
|
|
|
return false;
|
|
|
|
|
2015-08-31 00:57:15 +08:00
|
|
|
return true;
|
2016-02-22 03:13:19 +08:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2015-01-26 02:07:30 +08:00
|
|
|
|
2011-04-29 14:27:02 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-01-28 20:58:58 +08:00
|
|
|
bool ScopDetection::isInvariant(const Value &Val, const Region &Reg) const {
|
|
|
|
// A reference to function argument or constant value is invariant.
|
|
|
|
if (isa<Argument>(Val) || isa<Constant>(Val))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
const Instruction *I = dyn_cast<Instruction>(&Val);
|
|
|
|
if (!I)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!Reg.contains(I))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (I->mayHaveSideEffects())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// When Val is a Phi node, it is likely not invariant. We do not check whether
|
|
|
|
// Phi nodes are actually invariant, we assume that Phi nodes are usually not
|
|
|
|
// invariant. Recursively checking the operators of Phi nodes would lead to
|
|
|
|
// infinite recursion.
|
|
|
|
if (isa<PHINode>(*I))
|
|
|
|
return false;
|
|
|
|
|
2014-04-03 04:18:19 +08:00
|
|
|
for (const Use &Operand : I->operands())
|
2014-03-03 21:13:55 +08:00
|
|
|
if (!isInvariant(*Operand, Reg))
|
2014-01-28 20:58:58 +08:00
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-11-25 01:06:38 +08:00
|
|
|
/// @brief Remove smax of smax(0, size) expressions from a SCEV expression and
|
|
|
|
/// register the '...' components.
|
|
|
|
///
|
|
|
|
/// Array access expressions as they are generated by gfortran contain smax(0,
|
|
|
|
/// size) expressions that confuse the 'normal' delinearization algorithm.
|
|
|
|
/// However, if we extract such expressions before the normal delinearization
|
|
|
|
/// takes place they can actually help to identify array size expressions in
|
|
|
|
/// fortran accesses. For the subsequently following delinearization the smax(0,
|
|
|
|
/// size) component can be replaced by just 'size'. This is correct as we will
|
|
|
|
/// always add and verify the assumption that for all subscript expressions
|
|
|
|
/// 'exp' the inequality 0 <= exp < size holds. Hence, we will also verify
|
|
|
|
/// that 0 <= size, which means smax(0, size) == size.
|
|
|
|
struct SCEVRemoveMax : public SCEVVisitor<SCEVRemoveMax, const SCEV *> {
|
|
|
|
public:
|
|
|
|
static const SCEV *remove(ScalarEvolution &SE, const SCEV *Expr,
|
|
|
|
std::vector<const SCEV *> *Terms = nullptr) {
|
|
|
|
|
|
|
|
SCEVRemoveMax D(SE, Terms);
|
|
|
|
return D.visit(Expr);
|
|
|
|
}
|
|
|
|
|
|
|
|
SCEVRemoveMax(ScalarEvolution &SE, std::vector<const SCEV *> *Terms)
|
|
|
|
: SE(SE), Terms(Terms) {}
|
|
|
|
|
|
|
|
const SCEV *visitTruncateExpr(const SCEVTruncateExpr *Expr) { return Expr; }
|
|
|
|
|
|
|
|
const SCEV *visitZeroExtendExpr(const SCEVZeroExtendExpr *Expr) {
|
|
|
|
return Expr;
|
|
|
|
}
|
|
|
|
|
|
|
|
const SCEV *visitSignExtendExpr(const SCEVSignExtendExpr *Expr) {
|
|
|
|
return SE.getSignExtendExpr(visit(Expr->getOperand()), Expr->getType());
|
|
|
|
}
|
|
|
|
|
|
|
|
const SCEV *visitUDivExpr(const SCEVUDivExpr *Expr) { return Expr; }
|
|
|
|
|
|
|
|
const SCEV *visitSMaxExpr(const SCEVSMaxExpr *Expr) {
|
2015-12-20 22:42:32 +08:00
|
|
|
if ((Expr->getNumOperands() == 2) && Expr->getOperand(0)->isZero()) {
|
2015-11-25 01:06:38 +08:00
|
|
|
auto Res = visit(Expr->getOperand(1));
|
|
|
|
if (Terms)
|
|
|
|
(*Terms).push_back(Res);
|
|
|
|
return Res;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Expr;
|
|
|
|
}
|
|
|
|
|
2015-12-18 04:37:17 +08:00
|
|
|
const SCEV *visitUMaxExpr(const SCEVUMaxExpr *Expr) { return Expr; }
|
2015-11-25 01:06:38 +08:00
|
|
|
|
|
|
|
const SCEV *visitUnknown(const SCEVUnknown *Expr) { return Expr; }
|
|
|
|
|
|
|
|
const SCEV *visitCouldNotCompute(const SCEVCouldNotCompute *Expr) {
|
|
|
|
return Expr;
|
|
|
|
}
|
|
|
|
|
|
|
|
const SCEV *visitConstant(const SCEVConstant *Expr) { return Expr; }
|
|
|
|
|
|
|
|
const SCEV *visitAddRecExpr(const SCEVAddRecExpr *Expr) {
|
|
|
|
SmallVector<const SCEV *, 5> NewOps;
|
|
|
|
for (const SCEV *Op : Expr->operands())
|
|
|
|
NewOps.push_back(visit(Op));
|
|
|
|
|
|
|
|
return SE.getAddRecExpr(NewOps, Expr->getLoop(), Expr->getNoWrapFlags());
|
|
|
|
}
|
|
|
|
|
|
|
|
const SCEV *visitAddExpr(const SCEVAddExpr *Expr) {
|
|
|
|
SmallVector<const SCEV *, 5> NewOps;
|
|
|
|
for (const SCEV *Op : Expr->operands())
|
|
|
|
NewOps.push_back(visit(Op));
|
|
|
|
|
|
|
|
return SE.getAddExpr(NewOps);
|
|
|
|
}
|
|
|
|
|
|
|
|
const SCEV *visitMulExpr(const SCEVMulExpr *Expr) {
|
|
|
|
SmallVector<const SCEV *, 5> NewOps;
|
|
|
|
for (const SCEV *Op : Expr->operands())
|
|
|
|
NewOps.push_back(visit(Op));
|
|
|
|
|
|
|
|
return SE.getMulExpr(NewOps);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
ScalarEvolution &SE;
|
|
|
|
std::vector<const SCEV *> *Terms;
|
|
|
|
};
|
|
|
|
|
2015-11-24 13:00:36 +08:00
|
|
|
SmallVector<const SCEV *, 4>
|
|
|
|
ScopDetection::getDelinearizationTerms(DetectionContext &Context,
|
|
|
|
const SCEVUnknown *BasePointer) const {
|
|
|
|
SmallVector<const SCEV *, 4> Terms;
|
|
|
|
for (const auto &Pair : Context.Accesses[BasePointer]) {
|
2015-11-25 01:06:38 +08:00
|
|
|
std::vector<const SCEV *> MaxTerms;
|
|
|
|
SCEVRemoveMax::remove(*SE, Pair.second, &MaxTerms);
|
|
|
|
if (MaxTerms.size() > 0) {
|
|
|
|
Terms.insert(Terms.begin(), MaxTerms.begin(), MaxTerms.end());
|
|
|
|
continue;
|
|
|
|
}
|
2015-11-24 13:00:36 +08:00
|
|
|
// In case the outermost expression is a plain add, we check if any of its
|
|
|
|
// terms has the form 4 * %inst * %param * %param ..., aka a term that
|
|
|
|
// contains a product between a parameter and an instruction that is
|
|
|
|
// inside the scop. Such instructions, if allowed at all, are instructions
|
|
|
|
// SCEV can not represent, but Polly is still looking through. As a
|
|
|
|
// result, these instructions can depend on induction variables and are
|
|
|
|
// most likely no array sizes. However, terms that are multiplied with
|
|
|
|
// them are likely candidates for array sizes.
|
|
|
|
if (auto *AF = dyn_cast<SCEVAddExpr>(Pair.second)) {
|
|
|
|
for (auto Op : AF->operands()) {
|
|
|
|
if (auto *AF2 = dyn_cast<SCEVAddRecExpr>(Op))
|
|
|
|
SE->collectParametricTerms(AF2, Terms);
|
|
|
|
if (auto *AF2 = dyn_cast<SCEVMulExpr>(Op)) {
|
|
|
|
SmallVector<const SCEV *, 0> Operands;
|
|
|
|
|
|
|
|
for (auto *MulOp : AF2->operands()) {
|
|
|
|
if (auto *Const = dyn_cast<SCEVConstant>(MulOp))
|
|
|
|
Operands.push_back(Const);
|
|
|
|
if (auto *Unknown = dyn_cast<SCEVUnknown>(MulOp)) {
|
|
|
|
if (auto *Inst = dyn_cast<Instruction>(Unknown->getValue())) {
|
|
|
|
if (!Context.CurRegion.contains(Inst))
|
2015-06-29 22:44:22 +08:00
|
|
|
Operands.push_back(MulOp);
|
2015-11-24 13:00:36 +08:00
|
|
|
|
|
|
|
} else {
|
|
|
|
Operands.push_back(MulOp);
|
2015-06-29 22:44:22 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-11-24 13:00:36 +08:00
|
|
|
if (Operands.size())
|
|
|
|
Terms.push_back(SE->getMulExpr(Operands));
|
2015-06-29 22:44:22 +08:00
|
|
|
}
|
|
|
|
}
|
2014-09-13 22:47:55 +08:00
|
|
|
}
|
2015-11-24 13:00:36 +08:00
|
|
|
if (Terms.empty())
|
|
|
|
SE->collectParametricTerms(Pair.second, Terms);
|
|
|
|
}
|
|
|
|
return Terms;
|
|
|
|
}
|
2014-05-13 03:02:02 +08:00
|
|
|
|
2015-11-24 13:00:36 +08:00
|
|
|
bool ScopDetection::hasValidArraySizes(DetectionContext &Context,
|
|
|
|
SmallVectorImpl<const SCEV *> &Sizes,
|
|
|
|
const SCEVUnknown *BasePointer) const {
|
|
|
|
Value *BaseValue = BasePointer->getValue();
|
|
|
|
Region &CurRegion = Context.CurRegion;
|
|
|
|
for (const SCEV *DelinearizedSize : Sizes) {
|
|
|
|
if (!isAffine(DelinearizedSize, Context, nullptr)) {
|
|
|
|
Sizes.clear();
|
|
|
|
break;
|
2015-10-25 16:40:38 +08:00
|
|
|
}
|
2015-11-24 13:00:36 +08:00
|
|
|
if (auto *Unknown = dyn_cast<SCEVUnknown>(DelinearizedSize)) {
|
|
|
|
auto *V = dyn_cast<Value>(Unknown->getValue());
|
|
|
|
if (auto *Load = dyn_cast<LoadInst>(V)) {
|
|
|
|
if (Context.CurRegion.contains(Load) &&
|
|
|
|
isHoistableLoad(Load, CurRegion, *LI, *SE))
|
|
|
|
Context.RequiredILS.insert(Load);
|
2014-09-13 22:47:55 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2015-11-24 13:00:36 +08:00
|
|
|
if (hasScalarDepsInsideRegion(DelinearizedSize, &CurRegion))
|
2015-12-21 17:09:39 +08:00
|
|
|
return invalid<ReportNonAffineAccess>(
|
2015-11-24 13:00:36 +08:00
|
|
|
Context, /*Assert=*/true, DelinearizedSize,
|
|
|
|
Context.Accesses[BasePointer].front().first, BaseValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
// No array shape derived.
|
|
|
|
if (Sizes.empty()) {
|
|
|
|
if (AllowNonAffine)
|
|
|
|
return true;
|
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
|
|
|
|
2014-09-13 22:47:55 +08:00
|
|
|
for (const auto &Pair : Context.Accesses[BasePointer]) {
|
|
|
|
const Instruction *Insn = Pair.first;
|
2015-11-24 13:00:36 +08:00
|
|
|
const SCEV *AF = Pair.second;
|
2014-09-13 22:47:55 +08:00
|
|
|
|
2015-11-24 13:00:36 +08:00
|
|
|
if (!isAffine(AF, Context, BaseValue)) {
|
|
|
|
invalid<ReportNonAffineAccess>(Context, /*Assert=*/true, AF, Insn,
|
|
|
|
BaseValue);
|
|
|
|
if (!KeepGoing)
|
2014-09-13 22:47:55 +08:00
|
|
|
return false;
|
|
|
|
}
|
2014-05-10 06:45:15 +08:00
|
|
|
}
|
2015-11-24 13:00:36 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We first store the resulting memory accesses in TempMemoryAccesses. Only
|
|
|
|
// if the access functions for all memory accesses have been successfully
|
|
|
|
// delinearized we continue. Otherwise, we either report a failure or, if
|
|
|
|
// non-affine accesses are allowed, we drop the information. In case the
|
|
|
|
// information is dropped the memory accesses need to be overapproximated
|
|
|
|
// when translated to a polyhedral representation.
|
|
|
|
bool ScopDetection::computeAccessFunctions(
|
|
|
|
DetectionContext &Context, const SCEVUnknown *BasePointer,
|
|
|
|
std::shared_ptr<ArrayShape> Shape) const {
|
|
|
|
Value *BaseValue = BasePointer->getValue();
|
|
|
|
bool BasePtrHasNonAffine = false;
|
|
|
|
MapInsnToMemAcc TempMemoryAccesses;
|
|
|
|
for (const auto &Pair : Context.Accesses[BasePointer]) {
|
|
|
|
const Instruction *Insn = Pair.first;
|
|
|
|
auto *AF = Pair.second;
|
2015-11-25 01:06:38 +08:00
|
|
|
AF = SCEVRemoveMax::remove(*SE, AF);
|
2015-11-24 13:00:36 +08:00
|
|
|
bool IsNonAffine = false;
|
|
|
|
TempMemoryAccesses.insert(std::make_pair(Insn, MemAcc(Insn, Shape)));
|
|
|
|
MemAcc *Acc = &TempMemoryAccesses.find(Insn)->second;
|
|
|
|
|
|
|
|
if (!AF) {
|
|
|
|
if (isAffine(Pair.second, Context, BaseValue))
|
|
|
|
Acc->DelinearizedSubscripts.push_back(Pair.second);
|
|
|
|
else
|
|
|
|
IsNonAffine = true;
|
|
|
|
} else {
|
|
|
|
SE->computeAccessFunctions(AF, Acc->DelinearizedSubscripts,
|
|
|
|
Shape->DelinearizedSizes);
|
|
|
|
if (Acc->DelinearizedSubscripts.size() == 0)
|
|
|
|
IsNonAffine = true;
|
|
|
|
for (const SCEV *S : Acc->DelinearizedSubscripts)
|
|
|
|
if (!isAffine(S, Context, BaseValue))
|
|
|
|
IsNonAffine = true;
|
|
|
|
}
|
2014-09-13 22:47:55 +08:00
|
|
|
|
2015-11-24 13:00:36 +08:00
|
|
|
// (Possibly) report non affine access
|
|
|
|
if (IsNonAffine) {
|
|
|
|
BasePtrHasNonAffine = true;
|
|
|
|
if (!AllowNonAffine)
|
|
|
|
invalid<ReportNonAffineAccess>(Context, /*Assert=*/true, Pair.second,
|
|
|
|
Insn, BaseValue);
|
|
|
|
if (!KeepGoing && !AllowNonAffine)
|
|
|
|
return false;
|
|
|
|
}
|
2014-05-10 06:45:15 +08:00
|
|
|
}
|
2015-11-24 13:00:36 +08:00
|
|
|
|
|
|
|
if (!BasePtrHasNonAffine)
|
2016-02-15 08:20:58 +08:00
|
|
|
Context.InsnToMemAcc.insert(TempMemoryAccesses.begin(),
|
|
|
|
TempMemoryAccesses.end());
|
2015-11-24 13:00:36 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ScopDetection::hasBaseAffineAccesses(
|
|
|
|
DetectionContext &Context, const SCEVUnknown *BasePointer) const {
|
|
|
|
auto Shape = std::shared_ptr<ArrayShape>(new ArrayShape(BasePointer));
|
|
|
|
|
|
|
|
auto Terms = getDelinearizationTerms(Context, BasePointer);
|
|
|
|
|
|
|
|
SE->findArrayDimensions(Terms, Shape->DelinearizedSizes,
|
|
|
|
Context.ElementSize[BasePointer]);
|
|
|
|
|
|
|
|
if (!hasValidArraySizes(Context, Shape->DelinearizedSizes, BasePointer))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return computeAccessFunctions(Context, BasePointer, Shape);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ScopDetection::hasAffineMemoryAccesses(DetectionContext &Context) const {
|
2016-02-25 22:08:48 +08:00
|
|
|
// TODO: If we have an unknown access and other non-affine accesses we do
|
|
|
|
// not try to delinearize them for now.
|
|
|
|
if (Context.HasUnknownAccess && !Context.NonAffineAccesses.empty())
|
|
|
|
return AllowNonAffine;
|
|
|
|
|
2015-11-24 13:00:36 +08:00
|
|
|
for (const SCEVUnknown *BasePointer : Context.NonAffineAccesses)
|
|
|
|
if (!hasBaseAffineAccesses(Context, BasePointer)) {
|
|
|
|
if (KeepGoing)
|
|
|
|
continue;
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
2014-05-13 03:02:02 +08:00
|
|
|
return true;
|
2014-05-10 06:45:15 +08:00
|
|
|
}
|
|
|
|
|
2016-02-22 03:13:19 +08:00
|
|
|
bool ScopDetection::isValidAccess(Instruction *Inst, const SCEV *AF,
|
|
|
|
const SCEVUnknown *BP,
|
|
|
|
DetectionContext &Context) const {
|
2011-11-10 20:44:50 +08:00
|
|
|
|
2016-02-22 03:13:19 +08:00
|
|
|
if (!BP)
|
2016-01-28 01:09:17 +08:00
|
|
|
return invalid<ReportNoBasePtr>(Context, /*Assert=*/true, Inst);
|
2011-11-10 20:44:50 +08:00
|
|
|
|
2016-02-22 03:13:19 +08:00
|
|
|
auto *BV = BP->getValue();
|
|
|
|
if (isa<UndefValue>(BV))
|
2016-01-28 01:09:17 +08:00
|
|
|
return invalid<ReportUndefBasePtr>(Context, /*Assert=*/true, Inst);
|
2011-11-10 20:44:50 +08:00
|
|
|
|
2016-02-22 03:13:19 +08:00
|
|
|
// FIXME: Think about allowing IntToPtrInst
|
|
|
|
if (IntToPtrInst *Inst = dyn_cast<IntToPtrInst>(BV))
|
|
|
|
return invalid<ReportIntToPtr>(Context, /*Assert=*/true, Inst);
|
|
|
|
|
2014-01-28 20:58:58 +08:00
|
|
|
// Check that the base address of the access is invariant in the current
|
|
|
|
// region.
|
2016-02-22 03:13:19 +08:00
|
|
|
if (!isInvariant(*BV, Context.CurRegion))
|
|
|
|
return invalid<ReportVariantBasePtr>(Context, /*Assert=*/true, BV, Inst);
|
2014-01-28 20:58:58 +08:00
|
|
|
|
2016-02-22 03:13:19 +08:00
|
|
|
AF = SE->getMinusSCEV(AF, BP);
|
2014-04-09 05:20:44 +08:00
|
|
|
|
2016-02-22 03:13:19 +08:00
|
|
|
const SCEV *Size;
|
|
|
|
if (!isa<MemIntrinsic>(Inst)) {
|
|
|
|
Size = SE->getElementSize(Inst);
|
|
|
|
} else {
|
|
|
|
auto *SizeTy =
|
|
|
|
SE->getEffectiveSCEVType(PointerType::getInt8PtrTy(SE->getContext()));
|
|
|
|
Size = SE->getConstant(SizeTy, 8);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Context.ElementSize[BP]) {
|
|
|
|
if (!AllowDifferentTypes && Context.ElementSize[BP] != Size)
|
2016-02-07 16:48:57 +08:00
|
|
|
return invalid<ReportDifferentArrayElementSize>(Context, /*Assert=*/true,
|
2016-02-22 03:13:19 +08:00
|
|
|
Inst, BV);
|
2016-02-07 16:48:57 +08:00
|
|
|
|
2016-02-22 03:13:19 +08:00
|
|
|
Context.ElementSize[BP] = SE->getSMinExpr(Size, Context.ElementSize[BP]);
|
2016-02-07 16:48:57 +08:00
|
|
|
} else {
|
2016-02-22 03:13:19 +08:00
|
|
|
Context.ElementSize[BP] = Size;
|
2016-02-07 16:48:57 +08:00
|
|
|
}
|
2014-09-13 22:47:40 +08:00
|
|
|
|
2016-02-22 03:13:19 +08:00
|
|
|
bool IsVariantInNonAffineLoop = false;
|
2015-04-13 06:52:20 +08:00
|
|
|
SetVector<const Loop *> Loops;
|
2016-02-22 03:13:19 +08:00
|
|
|
findLoops(AF, Loops);
|
2015-04-13 06:52:20 +08:00
|
|
|
for (const Loop *L : Loops)
|
|
|
|
if (Context.BoxedLoopsSet.count(L))
|
2016-02-22 03:13:19 +08:00
|
|
|
IsVariantInNonAffineLoop = true;
|
|
|
|
|
|
|
|
bool IsAffine = !IsVariantInNonAffineLoop && isAffine(AF, Context, BV);
|
|
|
|
// Do not try to delinearize memory intrinsics and force them to be affine.
|
|
|
|
if (isa<MemIntrinsic>(Inst) && !IsAffine) {
|
|
|
|
return invalid<ReportNonAffineAccess>(Context, /*Assert=*/true, AF, Inst,
|
|
|
|
BV);
|
|
|
|
} else if (PollyDelinearize && !IsVariantInNonAffineLoop) {
|
|
|
|
Context.Accesses[BP].push_back({Inst, AF});
|
|
|
|
|
|
|
|
if (!IsAffine)
|
|
|
|
Context.NonAffineAccesses.insert(BP);
|
|
|
|
} else if (!AllowNonAffine && !IsAffine) {
|
|
|
|
return invalid<ReportNonAffineAccess>(Context, /*Assert=*/true, AF, Inst,
|
|
|
|
BV);
|
2014-04-09 05:20:44 +08:00
|
|
|
}
|
2011-04-29 14:27:02 +08:00
|
|
|
|
2014-09-25 05:04:29 +08:00
|
|
|
if (IgnoreAliasing)
|
2013-07-04 06:50:36 +08:00
|
|
|
return true;
|
|
|
|
|
|
|
|
// Check if the base pointer of the memory access does alias with
|
|
|
|
// any other pointer. This cannot be handled at the moment.
|
2014-10-05 19:58:57 +08:00
|
|
|
AAMDNodes AATags;
|
2016-02-22 03:13:19 +08:00
|
|
|
Inst->getAAMetadata(AATags);
|
2014-10-05 19:58:57 +08:00
|
|
|
AliasSet &AS = Context.AST.getAliasSetForPointer(
|
2016-02-22 03:13:19 +08:00
|
|
|
BP->getValue(), MemoryLocation::UnknownSize, AATags);
|
2013-07-04 06:50:36 +08:00
|
|
|
|
2014-09-25 05:04:29 +08:00
|
|
|
if (!AS.isMustAlias()) {
|
|
|
|
if (PollyUseRuntimeAliasChecks) {
|
|
|
|
bool CanBuildRunTimeCheck = true;
|
|
|
|
// The run-time alias check places code that involves the base pointer at
|
|
|
|
// the beginning of the SCoP. This breaks if the base pointer is defined
|
|
|
|
// inside the scop. Hence, we can only create a run-time check if we are
|
|
|
|
// sure the base pointer is not an instruction defined inside the scop.
|
2015-10-08 04:17:36 +08:00
|
|
|
// However, we can ignore loads that will be hoisted.
|
2014-09-25 05:04:29 +08:00
|
|
|
for (const auto &Ptr : AS) {
|
|
|
|
Instruction *Inst = dyn_cast<Instruction>(Ptr.getValue());
|
2016-02-22 03:13:19 +08:00
|
|
|
if (Inst && Context.CurRegion.contains(Inst)) {
|
2015-10-08 04:17:36 +08:00
|
|
|
auto *Load = dyn_cast<LoadInst>(Inst);
|
2016-02-22 03:13:19 +08:00
|
|
|
if (Load && isHoistableLoad(Load, Context.CurRegion, *LI, *SE)) {
|
2015-10-08 04:17:36 +08:00
|
|
|
Context.RequiredILS.insert(Load);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2014-09-25 05:04:29 +08:00
|
|
|
CanBuildRunTimeCheck = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (CanBuildRunTimeCheck)
|
|
|
|
return true;
|
|
|
|
}
|
2016-01-28 01:09:17 +08:00
|
|
|
return invalid<ReportAlias>(Context, /*Assert=*/true, Inst, AS);
|
2014-09-25 05:04:29 +08:00
|
|
|
}
|
2011-04-29 14:27:02 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-02-22 03:13:19 +08:00
|
|
|
bool ScopDetection::isValidMemoryAccess(MemAccInst Inst,
|
|
|
|
DetectionContext &Context) const {
|
|
|
|
Value *Ptr = Inst.getPointerOperand();
|
2016-02-26 17:47:11 +08:00
|
|
|
Loop *L = LI->getLoopFor(Inst->getParent());
|
2016-02-22 03:13:19 +08:00
|
|
|
const SCEV *AccessFunction = SE->getSCEVAtScope(Ptr, L);
|
|
|
|
const SCEVUnknown *BasePointer;
|
|
|
|
|
|
|
|
BasePointer = dyn_cast<SCEVUnknown>(SE->getPointerBase(AccessFunction));
|
|
|
|
|
|
|
|
return isValidAccess(Inst, AccessFunction, BasePointer, Context);
|
|
|
|
}
|
|
|
|
|
2011-04-29 14:27:02 +08:00
|
|
|
bool ScopDetection::isValidInstruction(Instruction &Inst,
|
|
|
|
DetectionContext &Context) const {
|
2015-11-11 20:44:18 +08:00
|
|
|
for (auto &Op : Inst.operands()) {
|
|
|
|
auto *OpInst = dyn_cast<Instruction>(&Op);
|
|
|
|
|
|
|
|
if (!OpInst)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (isErrorBlock(*OpInst->getParent(), Context.CurRegion, *LI, *DT))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-04-29 14:27:02 +08:00
|
|
|
// We only check the call instruction but not invoke instruction.
|
|
|
|
if (CallInst *CI = dyn_cast<CallInst>(&Inst)) {
|
2016-02-22 03:13:19 +08:00
|
|
|
if (isValidCallInst(*CI, Context))
|
2011-04-29 14:27:02 +08:00
|
|
|
return true;
|
|
|
|
|
2014-04-02 19:54:01 +08:00
|
|
|
return invalid<ReportFuncCall>(Context, /*Assert=*/true, &Inst);
|
2011-04-29 14:27:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!Inst.mayWriteToMemory() && !Inst.mayReadFromMemory()) {
|
2013-07-22 11:50:33 +08:00
|
|
|
if (!isa<AllocaInst>(Inst))
|
|
|
|
return true;
|
2011-04-29 14:27:02 +08:00
|
|
|
|
2014-04-02 19:54:01 +08:00
|
|
|
return invalid<ReportAlloca>(Context, /*Assert=*/true, &Inst);
|
2011-04-29 14:27:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check the access function.
|
2016-01-28 01:09:17 +08:00
|
|
|
if (auto MemInst = MemAccInst::dyn_cast(Inst)) {
|
2016-02-27 09:49:58 +08:00
|
|
|
Context.hasStores |= isa<StoreInst>(MemInst);
|
|
|
|
Context.hasLoads |= isa<LoadInst>(MemInst);
|
2016-01-28 01:09:17 +08:00
|
|
|
if (!MemInst.isSimple())
|
|
|
|
return invalid<ReportNonSimpleMemoryAccess>(Context, /*Assert=*/true,
|
|
|
|
&Inst);
|
|
|
|
|
|
|
|
return isValidMemoryAccess(MemInst, Context);
|
2015-02-19 13:31:07 +08:00
|
|
|
}
|
2011-04-29 14:27:02 +08:00
|
|
|
|
|
|
|
// We do not know this instruction, therefore we assume it is invalid.
|
2014-04-02 19:54:01 +08:00
|
|
|
return invalid<ReportUnknownInst>(Context, /*Assert=*/true, &Inst);
|
2011-04-29 14:27:02 +08:00
|
|
|
}
|
|
|
|
|
2015-08-27 14:53:52 +08:00
|
|
|
bool ScopDetection::canUseISLTripCount(Loop *L,
|
|
|
|
DetectionContext &Context) const {
|
2015-10-04 22:53:18 +08:00
|
|
|
// Ensure the loop has valid exiting blocks as well as latches, otherwise we
|
|
|
|
// need to overapproximate it as a boxed loop.
|
|
|
|
SmallVector<BasicBlock *, 4> LoopControlBlocks;
|
|
|
|
L->getLoopLatches(LoopControlBlocks);
|
|
|
|
L->getExitingBlocks(LoopControlBlocks);
|
|
|
|
for (BasicBlock *ControlBB : LoopControlBlocks) {
|
2015-11-11 16:42:20 +08:00
|
|
|
if (!isValidCFG(*ControlBB, true, false, Context))
|
2015-08-27 14:53:52 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We can use ISL to compute the trip count of L.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-04-29 14:27:02 +08:00
|
|
|
bool ScopDetection::isValidLoop(Loop *L, DetectionContext &Context) const {
|
2015-10-04 22:56:08 +08:00
|
|
|
if (canUseISLTripCount(L, Context))
|
2015-02-24 19:45:21 +08:00
|
|
|
return true;
|
2015-04-13 06:52:20 +08:00
|
|
|
|
2015-09-10 23:27:46 +08:00
|
|
|
if (AllowNonAffineSubLoops && AllowNonAffineSubRegions) {
|
2015-04-13 06:52:20 +08:00
|
|
|
Region *R = RI->getRegionFor(L->getHeader());
|
2015-10-04 22:54:27 +08:00
|
|
|
while (R != &Context.CurRegion && !R->contains(L))
|
|
|
|
R = R->getParent();
|
|
|
|
|
|
|
|
if (addOverApproximatedRegion(R, Context))
|
|
|
|
return true;
|
2015-04-13 06:52:20 +08:00
|
|
|
}
|
2011-04-29 14:27:02 +08:00
|
|
|
|
2015-09-10 23:27:46 +08:00
|
|
|
const SCEV *LoopCount = SE->getBackedgeTakenCount(L);
|
2015-02-24 19:45:21 +08:00
|
|
|
return invalid<ReportLoopBound>(Context, /*Assert=*/true, L, LoopCount);
|
2011-04-29 14:27:02 +08:00
|
|
|
}
|
|
|
|
|
2015-09-20 22:56:54 +08:00
|
|
|
/// @brief Return the number of loops in @p L (incl. @p L) that have a trip
|
2015-09-22 03:10:11 +08:00
|
|
|
/// count that is not known to be less than MIN_LOOP_TRIP_COUNT.
|
2015-10-04 22:56:08 +08:00
|
|
|
static int countBeneficialSubLoops(Loop *L, ScalarEvolution &SE) {
|
2015-09-20 22:56:54 +08:00
|
|
|
auto *TripCount = SE.getBackedgeTakenCount(L);
|
|
|
|
|
2015-10-04 22:56:08 +08:00
|
|
|
int count = 1;
|
2015-09-20 22:56:54 +08:00
|
|
|
if (auto *TripCountC = dyn_cast<SCEVConstant>(TripCount))
|
2015-10-04 22:56:08 +08:00
|
|
|
if (TripCountC->getType()->getScalarSizeInBits() <= 64)
|
|
|
|
if (TripCountC->getValue()->getZExtValue() < MIN_LOOP_TRIP_COUNT)
|
|
|
|
count -= 1;
|
2015-09-20 22:56:54 +08:00
|
|
|
|
|
|
|
for (auto &SubLoop : *L)
|
2015-10-04 22:56:08 +08:00
|
|
|
count += countBeneficialSubLoops(SubLoop, SE);
|
2015-09-20 22:56:54 +08:00
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2015-10-04 22:56:08 +08:00
|
|
|
int ScopDetection::countBeneficialLoops(Region *R) const {
|
|
|
|
int LoopNum = 0;
|
2015-08-28 00:55:18 +08:00
|
|
|
|
2015-08-31 20:08:11 +08:00
|
|
|
auto L = LI->getLoopFor(R->getEntry());
|
|
|
|
L = L ? R->outermostLoopInRegion(L) : nullptr;
|
|
|
|
L = L ? L->getParentLoop() : nullptr;
|
2015-08-28 00:55:18 +08:00
|
|
|
|
2015-08-31 20:08:11 +08:00
|
|
|
auto SubLoops =
|
|
|
|
L ? L->getSubLoopsVector() : std::vector<Loop *>(LI->begin(), LI->end());
|
2015-08-28 00:55:18 +08:00
|
|
|
|
2015-08-31 20:08:11 +08:00
|
|
|
for (auto &SubLoop : SubLoops)
|
2015-10-04 22:56:08 +08:00
|
|
|
if (R->contains(SubLoop))
|
|
|
|
LoopNum += countBeneficialSubLoops(SubLoop, *SE);
|
2015-08-31 20:08:11 +08:00
|
|
|
|
2015-10-04 22:56:08 +08:00
|
|
|
return LoopNum;
|
2015-08-28 00:55:18 +08:00
|
|
|
}
|
|
|
|
|
2011-04-29 14:27:02 +08:00
|
|
|
Region *ScopDetection::expandRegion(Region &R) {
|
2012-04-07 23:14:28 +08:00
|
|
|
// Initial no valid region was found (greater than R)
|
2015-06-05 01:59:54 +08:00
|
|
|
std::unique_ptr<Region> LastValidRegion;
|
|
|
|
auto ExpandedRegion = std::unique_ptr<Region>(R.getExpandedRegion());
|
2011-04-29 14:27:02 +08:00
|
|
|
|
|
|
|
DEBUG(dbgs() << "\tExpanding " << R.getNameStr() << "\n");
|
|
|
|
|
2012-04-07 23:14:28 +08:00
|
|
|
while (ExpandedRegion) {
|
2015-10-08 04:46:06 +08:00
|
|
|
const auto &It = DetectionContextMap.insert(std::make_pair(
|
|
|
|
ExpandedRegion.get(),
|
|
|
|
DetectionContext(*ExpandedRegion, *AA, false /*verifying*/)));
|
|
|
|
DetectionContext &Context = It.first->second;
|
2012-04-07 23:14:28 +08:00
|
|
|
DEBUG(dbgs() << "\t\tTrying " << ExpandedRegion->getNameStr() << "\n");
|
2014-06-27 14:21:14 +08:00
|
|
|
// Only expand when we did not collect errors.
|
2011-04-29 14:27:02 +08:00
|
|
|
|
2015-09-09 05:44:27 +08:00
|
|
|
if (!Context.Log.hasErrors()) {
|
2012-04-07 23:14:28 +08:00
|
|
|
// If the exit is valid check all blocks
|
|
|
|
// - if true, a valid region was found => store it + keep expanding
|
|
|
|
// - if false, .tbd. => stop (should this really end the loop?)
|
2015-10-01 18:59:14 +08:00
|
|
|
if (!allBlocksValid(Context) || Context.Log.hasErrors()) {
|
|
|
|
removeCachedResults(*ExpandedRegion);
|
2014-06-27 14:21:14 +08:00
|
|
|
break;
|
2015-10-01 18:59:14 +08:00
|
|
|
}
|
2014-06-27 14:21:14 +08:00
|
|
|
|
2013-04-10 14:55:45 +08:00
|
|
|
// Store this region, because it is the greatest valid (encountered so
|
|
|
|
// far).
|
2015-10-01 18:59:14 +08:00
|
|
|
removeCachedResults(*LastValidRegion);
|
2015-06-05 01:59:54 +08:00
|
|
|
LastValidRegion = std::move(ExpandedRegion);
|
2011-04-29 14:27:02 +08:00
|
|
|
|
2012-04-07 23:14:28 +08:00
|
|
|
// Create and test the next greater region (if any)
|
2015-06-05 01:59:54 +08:00
|
|
|
ExpandedRegion =
|
|
|
|
std::unique_ptr<Region>(LastValidRegion->getExpandedRegion());
|
2011-04-29 14:27:02 +08:00
|
|
|
|
2012-04-07 23:14:28 +08:00
|
|
|
} else {
|
|
|
|
// Create and test the next greater region (if any)
|
2015-10-08 04:46:06 +08:00
|
|
|
removeCachedResults(*ExpandedRegion);
|
2015-06-05 01:59:54 +08:00
|
|
|
ExpandedRegion =
|
|
|
|
std::unique_ptr<Region>(ExpandedRegion->getExpandedRegion());
|
2012-04-07 23:14:28 +08:00
|
|
|
}
|
|
|
|
}
|
2011-04-29 14:27:02 +08:00
|
|
|
|
2013-11-17 03:34:11 +08:00
|
|
|
DEBUG({
|
|
|
|
if (LastValidRegion)
|
|
|
|
dbgs() << "\tto " << LastValidRegion->getNameStr() << "\n";
|
|
|
|
else
|
|
|
|
dbgs() << "\tExpanding " << R.getNameStr() << " failed\n";
|
|
|
|
});
|
2011-04-29 14:27:02 +08:00
|
|
|
|
2015-06-05 01:59:54 +08:00
|
|
|
return LastValidRegion.release();
|
2011-04-29 14:27:02 +08:00
|
|
|
}
|
2013-06-04 00:35:37 +08:00
|
|
|
static bool regionWithoutLoops(Region &R, LoopInfo *LI) {
|
2014-04-03 04:18:19 +08:00
|
|
|
for (const BasicBlock *BB : R.blocks())
|
2014-03-03 21:13:55 +08:00
|
|
|
if (R.contains(LI->getLoopFor(BB)))
|
2013-06-04 00:35:37 +08:00
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2011-04-29 14:27:02 +08:00
|
|
|
|
2015-10-01 18:59:14 +08:00
|
|
|
unsigned ScopDetection::removeCachedResultsRecursively(const Region &R) {
|
2014-01-30 03:05:30 +08:00
|
|
|
unsigned Count = 0;
|
2014-04-16 02:45:27 +08:00
|
|
|
for (auto &SubRegion : R) {
|
2015-10-01 18:59:14 +08:00
|
|
|
if (ValidRegions.count(SubRegion.get())) {
|
|
|
|
removeCachedResults(*SubRegion.get());
|
2014-01-30 03:05:30 +08:00
|
|
|
++Count;
|
2015-10-01 18:59:14 +08:00
|
|
|
} else
|
|
|
|
Count += removeCachedResultsRecursively(*SubRegion);
|
2014-01-30 03:05:30 +08:00
|
|
|
}
|
|
|
|
return Count;
|
|
|
|
}
|
|
|
|
|
2015-10-01 18:59:14 +08:00
|
|
|
void ScopDetection::removeCachedResults(const Region &R) {
|
|
|
|
ValidRegions.remove(&R);
|
2015-10-08 04:46:06 +08:00
|
|
|
DetectionContextMap.erase(&R);
|
2015-10-01 18:59:14 +08:00
|
|
|
}
|
|
|
|
|
2011-04-29 14:27:02 +08:00
|
|
|
void ScopDetection::findScops(Region &R) {
|
2015-10-08 04:46:06 +08:00
|
|
|
const auto &It = DetectionContextMap.insert(
|
|
|
|
std::make_pair(&R, DetectionContext(R, *AA, false /*verifying*/)));
|
|
|
|
DetectionContext &Context = It.first->second;
|
2015-03-08 23:11:50 +08:00
|
|
|
|
|
|
|
bool RegionIsValid = false;
|
2015-10-07 00:10:29 +08:00
|
|
|
if (!PollyProcessUnprofitable && regionWithoutLoops(R, LI)) {
|
2015-10-01 18:59:14 +08:00
|
|
|
removeCachedResults(R);
|
2015-03-08 23:11:50 +08:00
|
|
|
invalid<ReportUnprofitable>(Context, /*Assert=*/true, &R);
|
2015-10-01 18:59:14 +08:00
|
|
|
} else
|
2015-03-08 23:11:50 +08:00
|
|
|
RegionIsValid = isValidRegion(Context);
|
|
|
|
|
2015-02-20 02:11:50 +08:00
|
|
|
bool HasErrors = !RegionIsValid || Context.Log.size() > 0;
|
2014-05-24 17:25:10 +08:00
|
|
|
|
2015-02-20 02:11:50 +08:00
|
|
|
if (PollyTrackFailures && HasErrors)
|
|
|
|
RejectLogs.insert(std::make_pair(&R, Context.Log));
|
|
|
|
|
2015-10-01 18:59:14 +08:00
|
|
|
if (HasErrors) {
|
|
|
|
removeCachedResults(R);
|
|
|
|
} else {
|
2011-04-29 14:27:02 +08:00
|
|
|
++ValidRegion;
|
|
|
|
ValidRegions.insert(&R);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-04-16 02:45:27 +08:00
|
|
|
for (auto &SubRegion : R)
|
2014-03-02 20:02:46 +08:00
|
|
|
findScops(*SubRegion);
|
2011-04-29 14:27:02 +08:00
|
|
|
|
|
|
|
// Try to expand regions.
|
|
|
|
//
|
|
|
|
// As the region tree normally only contains canonical regions, non canonical
|
|
|
|
// regions that form a Scop are not found. Therefore, those non canonical
|
|
|
|
// regions are checked by expanding the canonical ones.
|
|
|
|
|
2013-02-05 19:56:05 +08:00
|
|
|
std::vector<Region *> ToExpand;
|
2011-04-29 14:27:02 +08:00
|
|
|
|
2014-04-16 02:45:27 +08:00
|
|
|
for (auto &SubRegion : R)
|
|
|
|
ToExpand.push_back(SubRegion.get());
|
2011-04-29 14:27:02 +08:00
|
|
|
|
2014-04-03 04:18:19 +08:00
|
|
|
for (Region *CurrentRegion : ToExpand) {
|
2014-06-27 14:21:14 +08:00
|
|
|
// Skip regions that had errors.
|
|
|
|
bool HadErrors = RejectLogs.hasErrors(CurrentRegion);
|
|
|
|
if (HadErrors)
|
|
|
|
continue;
|
|
|
|
|
2011-04-29 14:27:02 +08:00
|
|
|
// Skip invalid regions. Regions may become invalid, if they are element of
|
|
|
|
// an already expanded region.
|
2014-10-23 04:39:07 +08:00
|
|
|
if (!ValidRegions.count(CurrentRegion))
|
2011-04-29 14:27:02 +08:00
|
|
|
continue;
|
|
|
|
|
|
|
|
Region *ExpandedR = expandRegion(*CurrentRegion);
|
|
|
|
|
|
|
|
if (!ExpandedR)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
R.addSubRegion(ExpandedR, true);
|
|
|
|
ValidRegions.insert(ExpandedR);
|
2015-10-01 18:59:14 +08:00
|
|
|
removeCachedResults(*CurrentRegion);
|
2011-04-29 14:27:02 +08:00
|
|
|
|
2014-01-30 03:05:30 +08:00
|
|
|
// Erase all (direct and indirect) children of ExpandedR from the valid
|
|
|
|
// regions and update the number of valid regions.
|
2015-10-01 18:59:14 +08:00
|
|
|
ValidRegion -= removeCachedResultsRecursively(*ExpandedR);
|
2011-04-29 14:27:02 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ScopDetection::allBlocksValid(DetectionContext &Context) const {
|
2015-02-23 22:18:28 +08:00
|
|
|
Region &CurRegion = Context.CurRegion;
|
2011-04-29 14:27:02 +08:00
|
|
|
|
2015-02-23 22:18:28 +08:00
|
|
|
for (const BasicBlock *BB : CurRegion.blocks()) {
|
2014-03-03 21:13:55 +08:00
|
|
|
Loop *L = LI->getLoopFor(BB);
|
2014-05-24 17:25:10 +08:00
|
|
|
if (L && L->getHeader() == BB && (!isValidLoop(L, Context) && !KeepGoing))
|
2013-06-12 06:20:32 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-09-11 01:51:27 +08:00
|
|
|
for (BasicBlock *BB : CurRegion.blocks()) {
|
2015-11-11 16:42:20 +08:00
|
|
|
bool IsErrorBlock = isErrorBlock(*BB, CurRegion, *LI, *DT);
|
2015-09-11 01:51:27 +08:00
|
|
|
|
2015-11-11 16:42:20 +08:00
|
|
|
// Also check exception blocks (and possibly register them as non-affine
|
|
|
|
// regions). Even though exception blocks are not modeled, we use them
|
|
|
|
// to forward-propagate domain constraints during ScopInfo construction.
|
|
|
|
if (!isValidCFG(*BB, false, IsErrorBlock, Context) && !KeepGoing)
|
2013-06-12 06:20:27 +08:00
|
|
|
return false;
|
2015-11-11 16:42:20 +08:00
|
|
|
|
|
|
|
if (IsErrorBlock)
|
|
|
|
continue;
|
|
|
|
|
2014-03-03 21:13:55 +08:00
|
|
|
for (BasicBlock::iterator I = BB->begin(), E = --BB->end(); I != E; ++I)
|
2014-05-24 17:25:10 +08:00
|
|
|
if (!isValidInstruction(*I, Context) && !KeepGoing)
|
2013-06-15 04:20:43 +08:00
|
|
|
return false;
|
2015-09-11 01:51:27 +08:00
|
|
|
}
|
2011-04-29 14:27:02 +08:00
|
|
|
|
2014-05-13 03:02:02 +08:00
|
|
|
if (!hasAffineMemoryAccesses(Context))
|
2014-05-10 06:45:15 +08:00
|
|
|
return false;
|
|
|
|
|
2011-04-29 14:27:02 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-12-22 05:00:43 +08:00
|
|
|
bool ScopDetection::hasSufficientCompute(DetectionContext &Context,
|
|
|
|
int NumLoops) const {
|
|
|
|
int InstCount = 0;
|
|
|
|
|
|
|
|
for (auto *BB : Context.CurRegion.blocks())
|
|
|
|
if (Context.CurRegion.contains(LI->getLoopFor(BB)))
|
2015-12-23 01:38:59 +08:00
|
|
|
InstCount += BB->size();
|
2015-12-22 05:00:43 +08:00
|
|
|
|
|
|
|
InstCount = InstCount / NumLoops;
|
|
|
|
|
|
|
|
return InstCount >= ProfitabilityMinPerLoopInstructions;
|
|
|
|
}
|
|
|
|
|
2015-12-21 20:14:48 +08:00
|
|
|
bool ScopDetection::isProfitableRegion(DetectionContext &Context) const {
|
|
|
|
Region &CurRegion = Context.CurRegion;
|
|
|
|
|
|
|
|
if (PollyProcessUnprofitable)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// We can probably not do a lot on scops that only write or only read
|
|
|
|
// data.
|
|
|
|
if (!Context.hasStores || !Context.hasLoads)
|
|
|
|
return invalid<ReportUnprofitable>(Context, /*Assert=*/true, &CurRegion);
|
|
|
|
|
|
|
|
int NumLoops = countBeneficialLoops(&CurRegion);
|
|
|
|
int NumAffineLoops = NumLoops - Context.BoxedLoopsSet.size();
|
|
|
|
|
2015-12-22 05:00:43 +08:00
|
|
|
// Scops with at least two loops may allow either loop fusion or tiling and
|
|
|
|
// are consequently interesting to look at.
|
|
|
|
if (NumAffineLoops >= 2)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// Scops that contain a loop with a non-trivial amount of computation per
|
|
|
|
// loop-iteration are interesting as we may be able to parallelize such
|
|
|
|
// loops. Individual loops that have only a small amount of computation
|
|
|
|
// per-iteration are performance-wise very fragile as any change to the
|
|
|
|
// loop induction variables may affect performance. To not cause spurious
|
|
|
|
// performance regressions, we do not consider such loops.
|
|
|
|
if (NumAffineLoops == 1 && hasSufficientCompute(Context, NumLoops))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return invalid<ReportUnprofitable>(Context, /*Assert=*/true, &CurRegion);
|
2015-12-21 20:14:48 +08:00
|
|
|
}
|
|
|
|
|
2011-04-29 14:27:02 +08:00
|
|
|
bool ScopDetection::isValidRegion(DetectionContext &Context) const {
|
2015-02-23 22:18:28 +08:00
|
|
|
Region &CurRegion = Context.CurRegion;
|
2011-04-29 14:27:02 +08:00
|
|
|
|
2015-02-23 22:18:28 +08:00
|
|
|
DEBUG(dbgs() << "Checking region: " << CurRegion.getNameStr() << "\n\t");
|
2011-04-29 14:27:02 +08:00
|
|
|
|
2015-02-23 22:18:28 +08:00
|
|
|
if (CurRegion.isTopLevelRegion()) {
|
2014-10-23 07:00:03 +08:00
|
|
|
DEBUG(dbgs() << "Top level region is invalid\n");
|
2011-04-29 14:27:02 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-02-23 22:18:28 +08:00
|
|
|
if (!CurRegion.getEntry()->getName().count(OnlyRegion)) {
|
2014-01-27 22:24:53 +08:00
|
|
|
DEBUG({
|
|
|
|
dbgs() << "Region entry does not match -polly-region-only";
|
|
|
|
dbgs() << "\n";
|
|
|
|
});
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-04-11 02:12:19 +08:00
|
|
|
// SCoP cannot contain the entry block of the function, because we need
|
2011-04-29 14:27:02 +08:00
|
|
|
// to insert alloca instruction there when translate scalar to array.
|
2015-02-23 22:18:28 +08:00
|
|
|
if (CurRegion.getEntry() ==
|
|
|
|
&(CurRegion.getEntry()->getParent()->getEntryBlock()))
|
|
|
|
return invalid<ReportEntry>(Context, /*Assert=*/true, CurRegion.getEntry());
|
2011-04-29 14:27:02 +08:00
|
|
|
|
2012-04-07 20:29:17 +08:00
|
|
|
if (!allBlocksValid(Context))
|
2011-04-29 14:27:02 +08:00
|
|
|
return false;
|
|
|
|
|
2016-01-22 17:44:37 +08:00
|
|
|
DebugLoc DbgLoc;
|
|
|
|
if (!isReducibleRegion(CurRegion, DbgLoc))
|
|
|
|
return invalid<ReportIrreducibleRegion>(Context, /*Assert=*/true,
|
|
|
|
&CurRegion, DbgLoc);
|
|
|
|
|
2015-12-21 20:14:48 +08:00
|
|
|
if (!isProfitableRegion(Context))
|
|
|
|
return false;
|
2015-04-13 06:52:20 +08:00
|
|
|
|
2011-04-29 14:27:02 +08:00
|
|
|
DEBUG(dbgs() << "OK\n");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-07-16 05:06:48 +08:00
|
|
|
void ScopDetection::markFunctionAsInvalid(Function *F) const {
|
|
|
|
F->addFnAttr(PollySkipFnAttr);
|
|
|
|
}
|
|
|
|
|
2011-04-29 14:27:02 +08:00
|
|
|
bool ScopDetection::isValidFunction(llvm::Function &F) {
|
2014-07-16 05:06:48 +08:00
|
|
|
return !F.hasFnAttribute(PollySkipFnAttr);
|
2011-04-29 14:27:02 +08:00
|
|
|
}
|
|
|
|
|
2013-03-05 03:49:51 +08:00
|
|
|
void ScopDetection::printLocations(llvm::Function &F) {
|
2014-04-03 04:18:19 +08:00
|
|
|
for (const Region *R : *this) {
|
2012-11-02 00:45:20 +08:00
|
|
|
unsigned LineEntry, LineExit;
|
|
|
|
std::string FileName;
|
|
|
|
|
2014-03-02 20:02:46 +08:00
|
|
|
getDebugLocation(R, LineEntry, LineExit, FileName);
|
2013-12-18 18:49:53 +08:00
|
|
|
DiagnosticScopFound Diagnostic(F, FileName, LineEntry, LineExit);
|
|
|
|
F.getContext().diagnose(Diagnostic);
|
2012-11-02 00:45:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-08 04:46:06 +08:00
|
|
|
void ScopDetection::emitMissedRemarksForValidRegions(const Function &F) {
|
2014-06-26 18:06:40 +08:00
|
|
|
for (const Region *R : ValidRegions) {
|
|
|
|
const Region *Parent = R->getParent();
|
|
|
|
if (Parent && !Parent->isTopLevelRegion() && RejectLogs.count(Parent))
|
|
|
|
emitRejectionRemarks(F, RejectLogs.at(Parent));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScopDetection::emitMissedRemarksForLeaves(const Function &F,
|
|
|
|
const Region *R) {
|
|
|
|
for (const std::unique_ptr<Region> &Child : *R) {
|
2015-10-08 04:46:06 +08:00
|
|
|
bool IsValid = DetectionContextMap.count(Child.get());
|
2014-06-26 18:06:40 +08:00
|
|
|
if (IsValid)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
bool IsLeaf = Child->begin() == Child->end();
|
|
|
|
if (!IsLeaf)
|
|
|
|
emitMissedRemarksForLeaves(F, Child.get());
|
|
|
|
else {
|
|
|
|
if (RejectLogs.count(Child.get())) {
|
|
|
|
emitRejectionRemarks(F, RejectLogs.at(Child.get()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-22 17:44:37 +08:00
|
|
|
bool ScopDetection::isReducibleRegion(Region &R, DebugLoc &DbgLoc) const {
|
|
|
|
BasicBlock *REntry = R.getEntry();
|
|
|
|
BasicBlock *RExit = R.getExit();
|
|
|
|
// Map to match the color of a BasicBlock during the DFS walk.
|
|
|
|
DenseMap<const BasicBlock *, Color> BBColorMap;
|
|
|
|
// Stack keeping track of current BB and index of next child to be processed.
|
|
|
|
std::stack<std::pair<BasicBlock *, unsigned>> DFSStack;
|
|
|
|
|
|
|
|
unsigned AdjacentBlockIndex = 0;
|
|
|
|
BasicBlock *CurrBB, *SuccBB;
|
|
|
|
CurrBB = REntry;
|
|
|
|
|
|
|
|
// Initialize the map for all BB with WHITE color.
|
|
|
|
for (auto *BB : R.blocks())
|
|
|
|
BBColorMap[BB] = ScopDetection::WHITE;
|
|
|
|
|
|
|
|
// Process the entry block of the Region.
|
|
|
|
BBColorMap[CurrBB] = ScopDetection::GREY;
|
|
|
|
DFSStack.push(std::make_pair(CurrBB, 0));
|
|
|
|
|
|
|
|
while (!DFSStack.empty()) {
|
|
|
|
// Get next BB on stack to be processed.
|
|
|
|
CurrBB = DFSStack.top().first;
|
|
|
|
AdjacentBlockIndex = DFSStack.top().second;
|
|
|
|
DFSStack.pop();
|
|
|
|
|
|
|
|
// Loop to iterate over the successors of current BB.
|
|
|
|
const TerminatorInst *TInst = CurrBB->getTerminator();
|
|
|
|
unsigned NSucc = TInst->getNumSuccessors();
|
|
|
|
for (unsigned I = AdjacentBlockIndex; I < NSucc;
|
|
|
|
++I, ++AdjacentBlockIndex) {
|
|
|
|
SuccBB = TInst->getSuccessor(I);
|
|
|
|
|
|
|
|
// Checks for region exit block and self-loops in BB.
|
|
|
|
if (SuccBB == RExit || SuccBB == CurrBB)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// WHITE indicates an unvisited BB in DFS walk.
|
|
|
|
if (BBColorMap[SuccBB] == ScopDetection::WHITE) {
|
|
|
|
// Push the current BB and the index of the next child to be visited.
|
|
|
|
DFSStack.push(std::make_pair(CurrBB, I + 1));
|
|
|
|
// Push the next BB to be processed.
|
|
|
|
DFSStack.push(std::make_pair(SuccBB, 0));
|
|
|
|
// First time the BB is being processed.
|
|
|
|
BBColorMap[SuccBB] = ScopDetection::GREY;
|
|
|
|
break;
|
|
|
|
} else if (BBColorMap[SuccBB] == ScopDetection::GREY) {
|
|
|
|
// GREY indicates a loop in the control flow.
|
|
|
|
// If the destination dominates the source, it is a natural loop
|
|
|
|
// else, an irreducible control flow in the region is detected.
|
|
|
|
if (!DT->dominates(SuccBB, CurrBB)) {
|
|
|
|
// Get debug info of instruction which causes irregular control flow.
|
|
|
|
DbgLoc = TInst->getDebugLoc();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If all children of current BB have been processed,
|
|
|
|
// then mark that BB as fully processed.
|
|
|
|
if (AdjacentBlockIndex == NSucc)
|
|
|
|
BBColorMap[CurrBB] = ScopDetection::BLACK;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-04-29 14:27:02 +08:00
|
|
|
bool ScopDetection::runOnFunction(llvm::Function &F) {
|
2015-01-17 22:16:56 +08:00
|
|
|
LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
|
2014-07-20 02:40:17 +08:00
|
|
|
RI = &getAnalysis<RegionInfoPass>().getRegionInfo();
|
2015-10-07 00:10:29 +08:00
|
|
|
if (!PollyProcessUnprofitable && LI->empty())
|
2013-05-31 01:47:32 +08:00
|
|
|
return false;
|
|
|
|
|
2015-09-10 06:13:56 +08:00
|
|
|
AA = &getAnalysis<AAResultsWrapperPass>().getAAResults();
|
2015-08-17 18:57:08 +08:00
|
|
|
SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE();
|
2015-10-08 04:32:43 +08:00
|
|
|
DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
|
2011-04-29 14:27:02 +08:00
|
|
|
Region *TopRegion = RI->getTopLevelRegion();
|
|
|
|
|
2011-10-23 19:17:06 +08:00
|
|
|
releaseMemory();
|
|
|
|
|
2014-05-07 19:23:32 +08:00
|
|
|
if (OnlyFunction != "" && !F.getName().count(OnlyFunction))
|
2011-10-23 19:17:06 +08:00
|
|
|
return false;
|
|
|
|
|
2012-12-30 07:47:38 +08:00
|
|
|
if (!isValidFunction(F))
|
2011-04-29 14:27:02 +08:00
|
|
|
return false;
|
|
|
|
|
|
|
|
findScops(*TopRegion);
|
2012-11-02 00:45:20 +08:00
|
|
|
|
2014-06-26 18:06:40 +08:00
|
|
|
// Only makes sense when we tracked errors.
|
|
|
|
if (PollyTrackFailures) {
|
2015-10-08 04:46:06 +08:00
|
|
|
emitMissedRemarksForValidRegions(F);
|
2014-06-26 18:06:40 +08:00
|
|
|
emitMissedRemarksForLeaves(F, TopRegion);
|
|
|
|
}
|
|
|
|
|
2014-10-16 07:24:28 +08:00
|
|
|
if (ReportLevel)
|
2013-03-05 03:49:51 +08:00
|
|
|
printLocations(F);
|
2012-11-02 00:45:20 +08:00
|
|
|
|
2015-10-08 04:46:06 +08:00
|
|
|
assert(ValidRegions.size() == DetectionContextMap.size() &&
|
2015-10-01 18:59:14 +08:00
|
|
|
"Cached more results than valid regions");
|
2011-04-29 14:27:02 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-02-24 19:45:21 +08:00
|
|
|
bool ScopDetection::isNonAffineSubRegion(const Region *SubR,
|
|
|
|
const Region *ScopR) const {
|
2015-10-08 04:46:06 +08:00
|
|
|
const DetectionContext *DC = getDetectionContext(ScopR);
|
|
|
|
assert(DC && "ScopR is no valid region!");
|
|
|
|
return DC->NonAffineSubRegionSet.count(SubR);
|
|
|
|
}
|
|
|
|
|
|
|
|
const ScopDetection::DetectionContext *
|
|
|
|
ScopDetection::getDetectionContext(const Region *R) const {
|
|
|
|
auto DCMIt = DetectionContextMap.find(R);
|
|
|
|
if (DCMIt == DetectionContextMap.end())
|
|
|
|
return nullptr;
|
|
|
|
return &DCMIt->second;
|
2015-02-24 19:45:21 +08:00
|
|
|
}
|
|
|
|
|
2015-04-13 06:52:20 +08:00
|
|
|
const ScopDetection::BoxedLoopsSetTy *
|
|
|
|
ScopDetection::getBoxedLoops(const Region *R) const {
|
2015-10-08 04:46:06 +08:00
|
|
|
const DetectionContext *DC = getDetectionContext(R);
|
|
|
|
assert(DC && "ScopR is no valid region!");
|
|
|
|
return &DC->BoxedLoopsSet;
|
2015-04-13 06:52:20 +08:00
|
|
|
}
|
|
|
|
|
2016-02-15 08:20:58 +08:00
|
|
|
const MapInsnToMemAcc *
|
|
|
|
ScopDetection::getInsnToMemAccMap(const Region *R) const {
|
|
|
|
const DetectionContext *DC = getDetectionContext(R);
|
|
|
|
assert(DC && "ScopR is no valid region!");
|
|
|
|
return &DC->InsnToMemAcc;
|
|
|
|
}
|
|
|
|
|
2015-10-08 04:17:36 +08:00
|
|
|
const InvariantLoadsSetTy *
|
|
|
|
ScopDetection::getRequiredInvariantLoads(const Region *R) const {
|
2015-10-08 04:46:06 +08:00
|
|
|
const DetectionContext *DC = getDetectionContext(R);
|
|
|
|
assert(DC && "ScopR is no valid region!");
|
|
|
|
return &DC->RequiredILS;
|
2015-10-08 04:17:36 +08:00
|
|
|
}
|
|
|
|
|
2011-04-29 14:27:02 +08:00
|
|
|
void polly::ScopDetection::verifyRegion(const Region &R) const {
|
|
|
|
assert(isMaxRegionInScop(R) && "Expect R is a valid region.");
|
2015-04-13 06:52:20 +08:00
|
|
|
|
2015-10-08 04:46:06 +08:00
|
|
|
DetectionContext Context(const_cast<Region &>(R), *AA, true /*verifying*/);
|
2011-04-29 14:27:02 +08:00
|
|
|
isValidRegion(Context);
|
|
|
|
}
|
|
|
|
|
|
|
|
void polly::ScopDetection::verifyAnalysis() const {
|
2014-02-19 02:49:49 +08:00
|
|
|
if (!VerifyScops)
|
|
|
|
return;
|
|
|
|
|
2014-04-03 04:18:19 +08:00
|
|
|
for (const Region *R : ValidRegions)
|
2014-03-02 20:02:46 +08:00
|
|
|
verifyRegion(*R);
|
2011-04-29 14:27:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ScopDetection::getAnalysisUsage(AnalysisUsage &AU) const {
|
2015-01-17 22:16:56 +08:00
|
|
|
AU.addRequired<LoopInfoWrapperPass>();
|
2015-08-17 18:57:08 +08:00
|
|
|
AU.addRequired<ScalarEvolutionWrapperPass>();
|
2015-10-08 04:32:43 +08:00
|
|
|
AU.addRequired<DominatorTreeWrapperPass>();
|
2011-04-29 14:27:02 +08:00
|
|
|
// We also need AA and RegionInfo when we are verifying analysis.
|
2015-09-10 06:13:56 +08:00
|
|
|
AU.addRequiredTransitive<AAResultsWrapperPass>();
|
2014-07-20 02:40:17 +08:00
|
|
|
AU.addRequiredTransitive<RegionInfoPass>();
|
2011-04-29 14:27:02 +08:00
|
|
|
AU.setPreservesAll();
|
|
|
|
}
|
|
|
|
|
2013-03-23 09:05:07 +08:00
|
|
|
void ScopDetection::print(raw_ostream &OS, const Module *) const {
|
2014-04-03 04:18:19 +08:00
|
|
|
for (const Region *R : ValidRegions)
|
2014-03-02 20:02:46 +08:00
|
|
|
OS << "Valid Region for Scop: " << R->getNameStr() << '\n';
|
2011-04-29 14:27:02 +08:00
|
|
|
|
|
|
|
OS << "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScopDetection::releaseMemory() {
|
2014-05-24 17:25:01 +08:00
|
|
|
RejectLogs.clear();
|
2015-10-01 00:51:05 +08:00
|
|
|
ValidRegions.clear();
|
2015-10-08 04:46:06 +08:00
|
|
|
DetectionContextMap.clear();
|
2014-05-24 17:25:01 +08:00
|
|
|
|
2011-05-06 10:38:20 +08:00
|
|
|
// Do not clear the invalid function set.
|
2011-04-29 14:27:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
char ScopDetection::ID = 0;
|
|
|
|
|
2013-03-23 09:05:07 +08:00
|
|
|
Pass *polly::createScopDetectionPass() { return new ScopDetection(); }
|
|
|
|
|
2011-10-08 08:30:40 +08:00
|
|
|
INITIALIZE_PASS_BEGIN(ScopDetection, "polly-detect",
|
|
|
|
"Polly - Detect static control parts (SCoPs)", false,
|
2013-03-23 09:05:07 +08:00
|
|
|
false);
|
2015-09-10 06:13:56 +08:00
|
|
|
INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass);
|
2015-01-17 22:16:56 +08:00
|
|
|
INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass);
|
2014-07-20 02:40:17 +08:00
|
|
|
INITIALIZE_PASS_DEPENDENCY(RegionInfoPass);
|
2015-10-08 04:32:43 +08:00
|
|
|
INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass);
|
2015-08-17 18:57:08 +08:00
|
|
|
INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass);
|
2011-10-08 08:30:40 +08:00
|
|
|
INITIALIZE_PASS_END(ScopDetection, "polly-detect",
|
|
|
|
"Polly - Detect static control parts (SCoPs)", false, false)
|