2017-08-25 05:22:41 +08:00
|
|
|
//===- ForwardOpTree.h ------------------------------------------*- C++ -*-===//
|
2017-07-22 22:02:47 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// Move instructions between statements.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "polly/ForwardOpTree.h"
|
2017-08-08 02:40:29 +08:00
|
|
|
#include "polly/Options.h"
|
2017-07-24 20:43:27 +08:00
|
|
|
#include "polly/ScopBuilder.h"
|
2017-07-22 22:02:47 +08:00
|
|
|
#include "polly/ScopInfo.h"
|
|
|
|
#include "polly/ScopPass.h"
|
|
|
|
#include "polly/Support/GICHelper.h"
|
2017-08-08 02:40:29 +08:00
|
|
|
#include "polly/Support/ISLOStream.h"
|
|
|
|
#include "polly/Support/ISLTools.h"
|
2017-07-22 22:02:47 +08:00
|
|
|
#include "polly/Support/VirtualInstruction.h"
|
2017-08-08 02:40:29 +08:00
|
|
|
#include "polly/ZoneAlgo.h"
|
2017-08-25 05:22:41 +08:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
|
|
|
#include "llvm/ADT/SmallVector.h"
|
|
|
|
#include "llvm/ADT/Statistic.h"
|
|
|
|
#include "llvm/Analysis/LoopInfo.h"
|
2017-07-22 22:02:47 +08:00
|
|
|
#include "llvm/Analysis/ValueTracking.h"
|
2017-08-25 05:22:41 +08:00
|
|
|
#include "llvm/IR/Instruction.h"
|
|
|
|
#include "llvm/IR/Instructions.h"
|
|
|
|
#include "llvm/IR/Value.h"
|
|
|
|
#include "llvm/Pass.h"
|
|
|
|
#include "llvm/Support/Casting.h"
|
|
|
|
#include "llvm/Support/CommandLine.h"
|
|
|
|
#include "llvm/Support/Compiler.h"
|
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
#include "isl/ctx.h"
|
|
|
|
#include "isl/isl-noexceptions.h"
|
|
|
|
#include <cassert>
|
|
|
|
#include <memory>
|
2017-07-22 22:02:47 +08:00
|
|
|
|
2017-08-09 20:27:35 +08:00
|
|
|
#define DEBUG_TYPE "polly-optree"
|
2017-07-22 22:02:47 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
2017-08-25 05:22:41 +08:00
|
|
|
using namespace polly;
|
2017-07-22 22:02:47 +08:00
|
|
|
|
2017-08-08 02:40:29 +08:00
|
|
|
static cl::opt<bool>
|
|
|
|
AnalyzeKnown("polly-optree-analyze-known",
|
|
|
|
cl::desc("Analyze array contents for load forwarding"),
|
|
|
|
cl::cat(PollyCategory), cl::init(true), cl::Hidden);
|
|
|
|
|
2017-09-19 01:43:50 +08:00
|
|
|
static cl::opt<unsigned>
|
2017-08-08 02:40:29 +08:00
|
|
|
MaxOps("polly-optree-max-ops",
|
|
|
|
cl::desc("Maximum number of ISL operations to invest for known "
|
|
|
|
"analysis; 0=no limit"),
|
|
|
|
cl::init(1000000), cl::cat(PollyCategory), cl::Hidden);
|
|
|
|
|
|
|
|
STATISTIC(KnownAnalyzed, "Number of successfully analyzed SCoPs");
|
|
|
|
STATISTIC(KnownOutOfQuota,
|
|
|
|
"Analyses aborted because max_operations was reached");
|
|
|
|
|
2017-07-22 22:02:47 +08:00
|
|
|
STATISTIC(TotalInstructionsCopied, "Number of copied instructions");
|
2017-08-08 02:40:29 +08:00
|
|
|
STATISTIC(TotalKnownLoadsForwarded,
|
|
|
|
"Number of forwarded loads because their value was known");
|
2017-07-24 20:43:27 +08:00
|
|
|
STATISTIC(TotalReadOnlyCopied, "Number of copied read-only accesses");
|
2017-07-22 22:02:47 +08:00
|
|
|
STATISTIC(TotalForwardedTrees, "Number of forwarded operand trees");
|
|
|
|
STATISTIC(TotalModifiedStmts,
|
|
|
|
"Number of statements with at least one forwarded tree");
|
|
|
|
|
|
|
|
STATISTIC(ScopsModified, "Number of SCoPs with at least one forwarded tree");
|
|
|
|
|
2017-08-23 21:50:30 +08:00
|
|
|
STATISTIC(NumValueWrites, "Number of scalar value writes after OpTree");
|
|
|
|
STATISTIC(NumValueWritesInLoops,
|
|
|
|
"Number of scalar value writes nested in affine loops after OpTree");
|
|
|
|
STATISTIC(NumPHIWrites, "Number of scalar phi writes after OpTree");
|
|
|
|
STATISTIC(NumPHIWritesInLoops,
|
|
|
|
"Number of scalar phi writes nested in affine loops after OpTree");
|
|
|
|
STATISTIC(NumSingletonWrites, "Number of singleton writes after OpTree");
|
|
|
|
STATISTIC(NumSingletonWritesInLoops,
|
|
|
|
"Number of singleton writes nested in affine loops after OpTree");
|
|
|
|
|
2017-07-22 22:02:47 +08:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
/// The state of whether an operand tree was/can be forwarded.
|
2017-07-24 23:33:53 +08:00
|
|
|
///
|
|
|
|
/// The items apply to an instructions and its operand tree with the instruction
|
|
|
|
/// as the root element. If the value in question is not an instruction in the
|
|
|
|
/// SCoP, it can be a leaf of an instruction's operand tree.
|
2017-07-22 22:02:47 +08:00
|
|
|
enum ForwardingDecision {
|
2017-07-24 23:33:53 +08:00
|
|
|
/// The root instruction or value cannot be forwarded at all.
|
2017-07-22 22:02:47 +08:00
|
|
|
FD_CannotForward,
|
2017-07-24 23:33:53 +08:00
|
|
|
|
|
|
|
/// The root instruction or value can be forwarded as a leaf of a larger
|
|
|
|
/// operand tree.
|
|
|
|
/// It does not make sense to move the value itself, it would just replace it
|
|
|
|
/// by a use of itself. For instance, a constant "5" used in a statement can
|
|
|
|
/// be forwarded, but it would just replace it by the same constant "5".
|
|
|
|
/// However, it makes sense to move as an operand of
|
|
|
|
///
|
|
|
|
/// %add = add 5, 5
|
|
|
|
///
|
|
|
|
/// where "5" is moved as part of a larger operand tree. "5" would be placed
|
|
|
|
/// (disregarding for a moment that literal constants don't have a location
|
|
|
|
/// and can be used anywhere) into the same statement as %add would.
|
2017-07-24 23:33:58 +08:00
|
|
|
FD_CanForwardLeaf,
|
2017-07-24 23:33:53 +08:00
|
|
|
|
|
|
|
/// The root instruction can be forwarded in a non-trivial way. This requires
|
|
|
|
/// the operand tree root to be an instruction in some statement.
|
2017-07-24 20:43:27 +08:00
|
|
|
FD_CanForwardTree,
|
2017-07-24 23:33:53 +08:00
|
|
|
|
|
|
|
/// Used to indicate that a forwarding has be carried out successfully.
|
2017-07-22 22:02:47 +08:00
|
|
|
FD_DidForward,
|
2017-08-04 20:28:42 +08:00
|
|
|
|
|
|
|
/// A forwarding method cannot be applied to the operand tree.
|
|
|
|
/// The difference to FD_CannotForward is that there might be other methods
|
|
|
|
/// that can handle it.
|
|
|
|
/// The conditions that make an operand tree applicable must be checked even
|
|
|
|
/// with DoIt==true because a method following the one that returned
|
|
|
|
/// FD_NotApplicable might have returned FD_CanForwardTree.
|
|
|
|
FD_NotApplicable
|
2017-07-22 22:02:47 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/// Implementation of operand tree forwarding for a specific SCoP.
|
|
|
|
///
|
|
|
|
/// For a statement that requires a scalar value (through a value read
|
|
|
|
/// MemoryAccess), see if its operand can be moved into the statement. If so,
|
|
|
|
/// the MemoryAccess is removed and the all the operand tree instructions are
|
|
|
|
/// moved into the statement. All original instructions are left in the source
|
|
|
|
/// statements. The simplification pass can clean these up.
|
2017-08-08 02:40:29 +08:00
|
|
|
class ForwardOpTreeImpl : ZoneAlgorithm {
|
2017-07-22 22:02:47 +08:00
|
|
|
private:
|
2017-09-20 06:53:20 +08:00
|
|
|
/// Scope guard to limit the number of isl operations for this pass.
|
|
|
|
IslMaxOperationsGuard &MaxOpGuard;
|
|
|
|
|
2017-07-22 22:02:47 +08:00
|
|
|
/// How many instructions have been copied to other statements.
|
|
|
|
int NumInstructionsCopied = 0;
|
|
|
|
|
2017-08-08 02:40:29 +08:00
|
|
|
/// Number of loads forwarded because their value was known.
|
|
|
|
int NumKnownLoadsForwarded = 0;
|
|
|
|
|
2017-07-24 20:43:27 +08:00
|
|
|
/// How many read-only accesses have been copied.
|
|
|
|
int NumReadOnlyCopied = 0;
|
|
|
|
|
2017-07-22 22:02:47 +08:00
|
|
|
/// How many operand trees have been forwarded.
|
|
|
|
int NumForwardedTrees = 0;
|
|
|
|
|
|
|
|
/// Number of statements with at least one forwarded operand tree.
|
|
|
|
int NumModifiedStmts = 0;
|
|
|
|
|
|
|
|
/// Whether we carried out at least one change to the SCoP.
|
|
|
|
bool Modified = false;
|
|
|
|
|
2017-08-08 02:40:29 +08:00
|
|
|
/// Contains the zones where array elements are known to contain a specific
|
|
|
|
/// value.
|
|
|
|
/// { [Element[] -> Zone[]] -> ValInst[] }
|
|
|
|
/// @see computeKnown()
|
|
|
|
isl::union_map Known;
|
|
|
|
|
|
|
|
/// Translator for newly introduced ValInsts to already existing ValInsts such
|
|
|
|
/// that new introduced load instructions can reuse the Known analysis of its
|
|
|
|
/// original load. { ValInst[] -> ValInst[] }
|
|
|
|
isl::union_map Translator;
|
|
|
|
|
|
|
|
/// Get list of array elements that do contain the same ValInst[] at Domain[].
|
|
|
|
///
|
|
|
|
/// @param ValInst { Domain[] -> ValInst[] }
|
|
|
|
/// The values for which we search for alternative locations,
|
|
|
|
/// per statement instance.
|
|
|
|
///
|
|
|
|
/// @return { Domain[] -> Element[] }
|
|
|
|
/// For each statement instance, the array elements that contain the
|
|
|
|
/// same ValInst.
|
|
|
|
isl::union_map findSameContentElements(isl::union_map ValInst) {
|
|
|
|
assert(ValInst.is_single_valued().is_true());
|
|
|
|
|
|
|
|
// { Domain[] }
|
|
|
|
isl::union_set Domain = ValInst.domain();
|
|
|
|
|
|
|
|
// { Domain[] -> Scatter[] }
|
|
|
|
isl::union_map Schedule = getScatterFor(Domain);
|
|
|
|
|
|
|
|
// { Element[] -> [Scatter[] -> ValInst[]] }
|
|
|
|
isl::union_map MustKnownCurried =
|
|
|
|
convertZoneToTimepoints(Known, isl::dim::in, false, true).curry();
|
|
|
|
|
|
|
|
// { [Domain[] -> ValInst[]] -> Scatter[] }
|
|
|
|
isl::union_map DomValSched = ValInst.domain_map().apply_range(Schedule);
|
|
|
|
|
|
|
|
// { [Scatter[] -> ValInst[]] -> [Domain[] -> ValInst[]] }
|
|
|
|
isl::union_map SchedValDomVal =
|
|
|
|
DomValSched.range_product(ValInst.range_map()).reverse();
|
|
|
|
|
|
|
|
// { Element[] -> [Domain[] -> ValInst[]] }
|
|
|
|
isl::union_map MustKnownInst = MustKnownCurried.apply_range(SchedValDomVal);
|
|
|
|
|
|
|
|
// { Domain[] -> Element[] }
|
|
|
|
isl::union_map MustKnownMap =
|
|
|
|
MustKnownInst.uncurry().domain().unwrap().reverse();
|
|
|
|
simplify(MustKnownMap);
|
|
|
|
|
|
|
|
return MustKnownMap;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Find a single array element for each statement instance, within a single
|
|
|
|
/// array.
|
|
|
|
///
|
|
|
|
/// @param MustKnown { Domain[] -> Element[] }
|
|
|
|
/// Set of candidate array elements.
|
|
|
|
/// @param Domain { Domain[] }
|
|
|
|
/// The statement instance for which we need elements for.
|
|
|
|
///
|
|
|
|
/// @return { Domain[] -> Element[] }
|
|
|
|
/// For each statement instance, an array element out of @p MustKnown.
|
|
|
|
/// All array elements must be in the same array (Polly does not yet
|
|
|
|
/// support reading from different accesses using the same
|
|
|
|
/// MemoryAccess). If no mapping for all of @p Domain exists, returns
|
|
|
|
/// null.
|
|
|
|
isl::map singleLocation(isl::union_map MustKnown, isl::set Domain) {
|
|
|
|
// { Domain[] -> Element[] }
|
|
|
|
isl::map Result;
|
|
|
|
|
|
|
|
// MemoryAccesses can read only elements from a single array
|
|
|
|
// (i.e. not: { Dom[0] -> A[0]; Dom[1] -> B[1] }).
|
|
|
|
// Look through all spaces until we find one that contains at least the
|
|
|
|
// wanted statement instance.s
|
2017-08-11 05:46:22 +08:00
|
|
|
MustKnown.foreach_map([&](isl::map Map) -> isl::stat {
|
2017-08-08 02:40:29 +08:00
|
|
|
// Get the array this is accessing.
|
|
|
|
isl::id ArrayId = Map.get_tuple_id(isl::dim::out);
|
|
|
|
ScopArrayInfo *SAI = static_cast<ScopArrayInfo *>(ArrayId.get_user());
|
|
|
|
|
|
|
|
// No support for generation of indirect array accesses.
|
|
|
|
if (SAI->getBasePtrOriginSAI())
|
|
|
|
return isl::stat::ok; // continue
|
|
|
|
|
|
|
|
// Determine whether this map contains all wanted values.
|
|
|
|
isl::set MapDom = Map.domain();
|
|
|
|
if (!Domain.is_subset(MapDom).is_true())
|
|
|
|
return isl::stat::ok; // continue
|
|
|
|
|
|
|
|
// There might be multiple array elements that contain the same value, but
|
|
|
|
// choose only one of them. lexmin is used because it returns a one-value
|
|
|
|
// mapping, we do not care about which one.
|
|
|
|
// TODO: Get the simplest access function.
|
|
|
|
Result = Map.lexmin();
|
|
|
|
return isl::stat::error; // break
|
|
|
|
});
|
|
|
|
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
2017-09-20 06:53:20 +08:00
|
|
|
ForwardOpTreeImpl(Scop *S, LoopInfo *LI, IslMaxOperationsGuard &MaxOpGuard)
|
|
|
|
: ZoneAlgorithm("polly-optree", S, LI), MaxOpGuard(MaxOpGuard) {}
|
2017-08-25 05:22:41 +08:00
|
|
|
|
2017-08-08 02:40:29 +08:00
|
|
|
/// Compute the zones of known array element contents.
|
|
|
|
///
|
|
|
|
/// @return True if the computed #Known is usable.
|
|
|
|
bool computeKnownValues() {
|
|
|
|
isl::union_map MustKnown, KnownFromLoad, KnownFromInit;
|
|
|
|
|
|
|
|
// Check that nothing strange occurs.
|
2017-08-29 04:39:07 +08:00
|
|
|
collectCompatibleElts();
|
2017-08-08 02:40:29 +08:00
|
|
|
|
|
|
|
{
|
2017-09-20 06:53:20 +08:00
|
|
|
IslQuotaScope QuotaScope = MaxOpGuard.enter();
|
2017-08-08 02:40:29 +08:00
|
|
|
|
|
|
|
computeCommon();
|
|
|
|
Known = computeKnown(true, true);
|
|
|
|
|
|
|
|
// Preexisting ValInsts use the known content analysis of themselves.
|
|
|
|
Translator = makeIdentityMap(Known.range(), false);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!Known || !Translator) {
|
|
|
|
assert(isl_ctx_last_error(IslCtx.get()) == isl_error_quota);
|
|
|
|
Known = nullptr;
|
|
|
|
Translator = nullptr;
|
|
|
|
DEBUG(dbgs() << "Known analysis exceeded max_operations\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
KnownAnalyzed++;
|
|
|
|
DEBUG(dbgs() << "All known: " << Known << "\n");
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-07-22 22:02:47 +08:00
|
|
|
void printStatistics(raw_ostream &OS, int Indent = 0) {
|
|
|
|
OS.indent(Indent) << "Statistics {\n";
|
|
|
|
OS.indent(Indent + 4) << "Instructions copied: " << NumInstructionsCopied
|
|
|
|
<< '\n';
|
2017-08-08 02:40:29 +08:00
|
|
|
OS.indent(Indent + 4) << "Known loads forwarded: " << NumKnownLoadsForwarded
|
|
|
|
<< '\n';
|
2017-07-24 20:43:27 +08:00
|
|
|
OS.indent(Indent + 4) << "Read-only accesses copied: " << NumReadOnlyCopied
|
|
|
|
<< '\n';
|
2017-07-22 22:02:47 +08:00
|
|
|
OS.indent(Indent + 4) << "Operand trees forwarded: " << NumForwardedTrees
|
|
|
|
<< '\n';
|
|
|
|
OS.indent(Indent + 4) << "Statements with forwarded operand trees: "
|
|
|
|
<< NumModifiedStmts << '\n';
|
|
|
|
OS.indent(Indent) << "}\n";
|
|
|
|
}
|
|
|
|
|
2017-08-25 05:22:41 +08:00
|
|
|
void printStatements(raw_ostream &OS, int Indent = 0) const {
|
2017-07-22 22:02:47 +08:00
|
|
|
OS.indent(Indent) << "After statements {\n";
|
|
|
|
for (auto &Stmt : *S) {
|
|
|
|
OS.indent(Indent + 4) << Stmt.getBaseName() << "\n";
|
|
|
|
for (auto *MA : Stmt)
|
|
|
|
MA->print(OS);
|
|
|
|
|
|
|
|
OS.indent(Indent + 12);
|
|
|
|
Stmt.printInstructions(OS);
|
|
|
|
}
|
|
|
|
OS.indent(Indent) << "}\n";
|
|
|
|
}
|
|
|
|
|
2017-08-08 02:40:29 +08:00
|
|
|
/// Create a new MemoryAccess of type read and MemoryKind::Array.
|
|
|
|
///
|
|
|
|
/// @param Stmt The statement in which the access occurs.
|
|
|
|
/// @param LI The instruction that does the access.
|
|
|
|
/// @param AccessRelation The array element that each statement instance
|
|
|
|
/// accesses.
|
|
|
|
///
|
|
|
|
/// @param The newly created access.
|
|
|
|
MemoryAccess *makeReadArrayAccess(ScopStmt *Stmt, LoadInst *LI,
|
|
|
|
isl::map AccessRelation) {
|
|
|
|
isl::id ArrayId = AccessRelation.get_tuple_id(isl::dim::out);
|
|
|
|
ScopArrayInfo *SAI = reinterpret_cast<ScopArrayInfo *>(ArrayId.get_user());
|
|
|
|
|
|
|
|
// Create a dummy SCEV access, to be replaced anyway.
|
|
|
|
SmallVector<const SCEV *, 4> Sizes;
|
|
|
|
Sizes.reserve(SAI->getNumberOfDimensions());
|
|
|
|
SmallVector<const SCEV *, 4> Subscripts;
|
|
|
|
Subscripts.reserve(SAI->getNumberOfDimensions());
|
|
|
|
for (unsigned i = 0; i < SAI->getNumberOfDimensions(); i += 1) {
|
|
|
|
Sizes.push_back(SAI->getDimensionSize(i));
|
|
|
|
Subscripts.push_back(nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
MemoryAccess *Access =
|
|
|
|
new MemoryAccess(Stmt, LI, MemoryAccess::READ, SAI->getBasePtr(),
|
|
|
|
LI->getType(), true, {}, Sizes, LI, MemoryKind::Array);
|
|
|
|
S->addAccessFunction(Access);
|
|
|
|
Stmt->addAccess(Access, true);
|
|
|
|
|
|
|
|
Access->setNewAccessRelation(AccessRelation);
|
|
|
|
|
|
|
|
return Access;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// For an llvm::Value defined in @p DefStmt, compute the RAW dependency for a
|
|
|
|
/// use in every instance of @p UseStmt.
|
|
|
|
///
|
|
|
|
/// @param UseStmt Statement a scalar is used in.
|
|
|
|
/// @param DefStmt Statement a scalar is defined in.
|
|
|
|
///
|
|
|
|
/// @return { DomainUse[] -> DomainDef[] }
|
|
|
|
isl::map computeUseToDefFlowDependency(ScopStmt *UseStmt, ScopStmt *DefStmt) {
|
|
|
|
// { DomainUse[] -> Scatter[] }
|
|
|
|
isl::map UseScatter = getScatterFor(UseStmt);
|
|
|
|
|
|
|
|
// { Zone[] -> DomainDef[] }
|
|
|
|
isl::map ReachDefZone = getScalarReachingDefinition(DefStmt);
|
|
|
|
|
|
|
|
// { Scatter[] -> DomainDef[] }
|
|
|
|
isl::map ReachDefTimepoints =
|
|
|
|
convertZoneToTimepoints(ReachDefZone, isl::dim::in, false, true);
|
|
|
|
|
|
|
|
// { DomainUse[] -> DomainDef[] }
|
|
|
|
return UseScatter.apply_range(ReachDefTimepoints);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Forward a load by reading from an array element that contains the same
|
|
|
|
/// value. Typically the location it was loaded from.
|
|
|
|
///
|
|
|
|
/// @param TargetStmt The statement the operand tree will be copied to.
|
|
|
|
/// @param Inst The (possibly speculatable) instruction to forward.
|
|
|
|
/// @param UseStmt The statement that uses @p Inst.
|
|
|
|
/// @param UseLoop The loop @p Inst is used in.
|
|
|
|
/// @param UseToTarget { DomainUse[] -> DomainTarget[] }
|
|
|
|
/// A mapping from the statement instance @p Inst is used
|
|
|
|
/// to the statement instance it is forwarded to.
|
|
|
|
/// @param DefStmt The statement @p Inst is defined in.
|
|
|
|
/// @param DefLoop The loop which contains @p Inst.
|
|
|
|
/// @param DefToTarget { DomainDef[] -> DomainTarget[] }
|
|
|
|
/// A mapping from the statement instance @p Inst is
|
|
|
|
/// defined to the statement instance it is forwarded to.
|
|
|
|
/// @param DoIt If false, only determine whether an operand tree can be
|
|
|
|
/// forwarded. If true, carry out the forwarding. Do not
|
|
|
|
/// use DoIt==true if an operand tree is not known to be
|
|
|
|
/// forwardable.
|
|
|
|
///
|
|
|
|
/// @return FD_NotApplicable if @p Inst is not a LoadInst.
|
|
|
|
/// FD_CannotForward if no array element to load from was found.
|
|
|
|
/// FD_CanForwardLeaf if the load is already in the target statement
|
|
|
|
/// instance.
|
|
|
|
/// FD_CanForwardTree if @p Inst is forwardable.
|
|
|
|
/// FD_DidForward if @p DoIt was true.
|
|
|
|
ForwardingDecision forwardKnownLoad(ScopStmt *TargetStmt, Instruction *Inst,
|
|
|
|
ScopStmt *UseStmt, Loop *UseLoop,
|
|
|
|
isl::map UseToTarget, ScopStmt *DefStmt,
|
|
|
|
Loop *DefLoop, isl::map DefToTarget,
|
|
|
|
bool DoIt) {
|
|
|
|
// Cannot do anything without successful known analysis.
|
2017-09-20 06:53:20 +08:00
|
|
|
if (Known.is_null() || Translator.is_null() || UseToTarget.is_null() ||
|
|
|
|
DefToTarget.is_null() || MaxOpGuard.hasQuotaExceeded())
|
2017-08-08 02:40:29 +08:00
|
|
|
return FD_NotApplicable;
|
|
|
|
|
|
|
|
LoadInst *LI = dyn_cast<LoadInst>(Inst);
|
|
|
|
if (!LI)
|
|
|
|
return FD_NotApplicable;
|
|
|
|
|
2017-09-04 00:09:38 +08:00
|
|
|
// If the load is already in the statement, no forwarding is necessary.
|
2017-08-08 02:40:29 +08:00
|
|
|
// However, it might happen that the LoadInst is already present in the
|
|
|
|
// statement's instruction list. In that case we do as follows:
|
|
|
|
// - For the evaluation (DoIt==false), we can trivially forward it as it is
|
|
|
|
// benefit of forwarding an already present instruction.
|
2017-09-04 00:09:38 +08:00
|
|
|
// - For the execution (DoIt==true), prepend the instruction (to make it
|
2017-08-08 02:40:29 +08:00
|
|
|
// available to all instructions following in the instruction list), but
|
|
|
|
// do not add another MemoryAccess.
|
|
|
|
MemoryAccess *Access = TargetStmt->getArrayAccessOrNULLFor(LI);
|
|
|
|
if (Access && !DoIt)
|
[ForwardOp] Remove read accesses for all instructions that have been moved
Before this patch, OpTree did not consider forwarding an operand tree consisting
of only single LoadInst as useful. The motivation was that, like an access to a
read-only variable, it would just replace one MemoryAccess by another. However,
in contrast to read-only accesses, this would replace a scalar access by an
array access, which is something worth doing.
In addition, leaving scalar MemoryAccess is problematic in that VirtualUse
prioritizes inter-Stmt use over intra-Stmt. It was possible that the same LLVM
value has a MemoryAccess for accessing the remote Stmt's LoadInst as well as
having the same LoadInst in its own instruction list (due to being forwarded
from another operand tree).
With this patch we ensure that if a LoadInst is forwarded is any operand tree,
also the operand tree containing just the LoadInst is forwarded as well, which
effectively removes the scalar MemoryAccess such that only the array access
remains, not both.
Thanks Michael for the detailed explanation.
Reviewers: Meinersbur, bellu, singam-sanjay, gareevroman
Subscribers: hfinkel, pollydev, llvm-commits
Tags: #polly
Differential Revision: https://reviews.llvm.org/D37424
llvm-svn: 312456
2017-09-04 03:52:15 +08:00
|
|
|
return FD_CanForwardTree;
|
2017-08-08 02:40:29 +08:00
|
|
|
|
|
|
|
if (DoIt)
|
|
|
|
TargetStmt->prependInstruction(LI);
|
|
|
|
|
|
|
|
ForwardingDecision OpDecision =
|
|
|
|
forwardTree(TargetStmt, LI->getPointerOperand(), DefStmt, DefLoop,
|
|
|
|
DefToTarget, DoIt);
|
|
|
|
switch (OpDecision) {
|
|
|
|
case FD_CannotForward:
|
|
|
|
assert(!DoIt);
|
|
|
|
return OpDecision;
|
|
|
|
|
|
|
|
case FD_CanForwardLeaf:
|
|
|
|
case FD_CanForwardTree:
|
|
|
|
assert(!DoIt);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FD_DidForward:
|
|
|
|
assert(DoIt);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
llvm_unreachable("Shouldn't return this");
|
|
|
|
}
|
|
|
|
|
2017-09-20 06:53:20 +08:00
|
|
|
IslQuotaScope QuotaScope = MaxOpGuard.enter(!DoIt);
|
|
|
|
|
2017-08-08 02:40:29 +08:00
|
|
|
// { DomainDef[] -> ValInst[] }
|
|
|
|
isl::map ExpectedVal = makeValInst(Inst, UseStmt, UseLoop);
|
|
|
|
|
|
|
|
// { DomainTarget[] -> ValInst[] }
|
|
|
|
isl::map TargetExpectedVal = ExpectedVal.apply_domain(UseToTarget);
|
|
|
|
isl::union_map TranslatedExpectedVal =
|
|
|
|
isl::union_map(TargetExpectedVal).apply_range(Translator);
|
|
|
|
|
|
|
|
// { DomainTarget[] -> Element[] }
|
|
|
|
isl::union_map Candidates = findSameContentElements(TranslatedExpectedVal);
|
|
|
|
|
|
|
|
isl::map SameVal = singleLocation(Candidates, getDomainFor(TargetStmt));
|
|
|
|
if (!SameVal)
|
|
|
|
return FD_CannotForward;
|
|
|
|
|
|
|
|
if (!DoIt)
|
|
|
|
return FD_CanForwardTree;
|
|
|
|
|
|
|
|
if (Access) {
|
|
|
|
DEBUG(dbgs() << " forwarded known load with preexisting MemoryAccess"
|
|
|
|
<< Access << "\n");
|
|
|
|
} else {
|
|
|
|
Access = makeReadArrayAccess(TargetStmt, LI, SameVal);
|
|
|
|
DEBUG(dbgs() << " forwarded known load with new MemoryAccess" << Access
|
|
|
|
<< "\n");
|
|
|
|
|
|
|
|
// { ValInst[] }
|
|
|
|
isl::space ValInstSpace = ExpectedVal.get_space().range();
|
|
|
|
|
|
|
|
// After adding a new load to the SCoP, also update the Known content
|
|
|
|
// about it. The new load will have a known ValInst of
|
|
|
|
// { [DomainTarget[] -> Value[]] }
|
|
|
|
// but which -- because it is a copy of it -- has same value as the
|
|
|
|
// { [DomainDef[] -> Value[]] }
|
|
|
|
// that it replicates. Instead of cloning the known content of
|
|
|
|
// [DomainDef[] -> Value[]]
|
|
|
|
// for DomainTarget[], we add a 'translator' that maps
|
|
|
|
// [DomainTarget[] -> Value[]] to [DomainDef[] -> Value[]]
|
|
|
|
// before comparing to the known content.
|
|
|
|
// TODO: 'Translator' could also be used to map PHINodes to their incoming
|
|
|
|
// ValInsts.
|
|
|
|
if (ValInstSpace.is_wrapping()) {
|
|
|
|
// { DefDomain[] -> Value[] }
|
|
|
|
isl::map ValInsts = ExpectedVal.range().unwrap();
|
|
|
|
|
|
|
|
// { DefDomain[] }
|
|
|
|
isl::set DefDomain = ValInsts.domain();
|
|
|
|
|
|
|
|
// { Value[] }
|
|
|
|
isl::space ValSpace = ValInstSpace.unwrap().range();
|
|
|
|
|
|
|
|
// { Value[] -> Value[] }
|
|
|
|
isl::map ValToVal =
|
|
|
|
isl::map::identity(ValSpace.map_from_domain_and_range(ValSpace));
|
|
|
|
|
|
|
|
// { [TargetDomain[] -> Value[]] -> [DefDomain[] -> Value] }
|
|
|
|
isl::map LocalTranslator = DefToTarget.reverse().product(ValToVal);
|
|
|
|
|
|
|
|
Translator = Translator.add_map(LocalTranslator);
|
|
|
|
DEBUG(dbgs() << " local translator is " << LocalTranslator
|
|
|
|
<< "\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
DEBUG(dbgs() << " expected values where " << TargetExpectedVal
|
|
|
|
<< "\n");
|
|
|
|
DEBUG(dbgs() << " candidate elements where " << Candidates << "\n");
|
|
|
|
assert(Access);
|
|
|
|
|
|
|
|
NumKnownLoadsForwarded++;
|
|
|
|
TotalKnownLoadsForwarded++;
|
|
|
|
return FD_DidForward;
|
|
|
|
}
|
|
|
|
|
2017-08-04 20:28:42 +08:00
|
|
|
/// Forwards a speculatively executable instruction.
|
|
|
|
///
|
2017-08-08 02:40:29 +08:00
|
|
|
/// @param TargetStmt The statement the operand tree will be copied to.
|
|
|
|
/// @param UseInst The (possibly speculatable) instruction to forward.
|
|
|
|
/// @param DefStmt The statement @p UseInst is defined in.
|
|
|
|
/// @param DefLoop The loop which contains @p UseInst.
|
|
|
|
/// @param DefToTarget { DomainDef[] -> DomainTarget[] }
|
|
|
|
/// A mapping from the statement instance @p UseInst is
|
|
|
|
/// defined to the statement instance it is forwarded to.
|
|
|
|
/// @param DoIt If false, only determine whether an operand tree can be
|
|
|
|
/// forwarded. If true, carry out the forwarding. Do not
|
|
|
|
/// use DoIt==true if an operand tree is not known to be
|
|
|
|
/// forwardable.
|
2017-08-04 20:28:42 +08:00
|
|
|
///
|
2017-08-08 02:40:29 +08:00
|
|
|
/// @return FD_NotApplicable if @p UseInst is not speculatable.
|
|
|
|
/// FD_CannotForward if one of @p UseInst's operands is not
|
|
|
|
/// forwardable.
|
|
|
|
/// FD_CanForwardTree if @p UseInst is forwardable.
|
|
|
|
/// FD_DidForward if @p DoIt was true.
|
2017-08-04 20:28:42 +08:00
|
|
|
ForwardingDecision forwardSpeculatable(ScopStmt *TargetStmt,
|
|
|
|
Instruction *UseInst,
|
2017-08-08 02:40:29 +08:00
|
|
|
ScopStmt *DefStmt, Loop *DefLoop,
|
|
|
|
isl::map DefToTarget, bool DoIt) {
|
2017-08-04 20:28:42 +08:00
|
|
|
// PHIs, unless synthesizable, are not yet supported.
|
|
|
|
if (isa<PHINode>(UseInst))
|
|
|
|
return FD_NotApplicable;
|
|
|
|
|
|
|
|
// Compatible instructions must satisfy the following conditions:
|
|
|
|
// 1. Idempotent (instruction will be copied, not moved; although its
|
|
|
|
// original instance might be removed by simplification)
|
|
|
|
// 2. Not access memory (There might be memory writes between)
|
|
|
|
// 3. Not cause undefined behaviour (we might copy to a location when the
|
|
|
|
// original instruction was no executed; this is currently not possible
|
|
|
|
// because we do not forward PHINodes)
|
|
|
|
// 4. Not leak memory if executed multiple times (i.e. malloc)
|
|
|
|
//
|
|
|
|
// Instruction::mayHaveSideEffects is not sufficient because it considers
|
|
|
|
// malloc to not have side-effects. llvm::isSafeToSpeculativelyExecute is
|
|
|
|
// not sufficient because it allows memory accesses.
|
|
|
|
if (mayBeMemoryDependent(*UseInst))
|
|
|
|
return FD_NotApplicable;
|
|
|
|
|
|
|
|
if (DoIt) {
|
|
|
|
// To ensure the right order, prepend this instruction before its
|
|
|
|
// operands. This ensures that its operands are inserted before the
|
|
|
|
// instruction using them.
|
|
|
|
// TODO: The operand tree is not really a tree, but a DAG. We should be
|
|
|
|
// able to handle DAGs without duplication.
|
|
|
|
TargetStmt->prependInstruction(UseInst);
|
|
|
|
NumInstructionsCopied++;
|
|
|
|
TotalInstructionsCopied++;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (Value *OpVal : UseInst->operand_values()) {
|
|
|
|
ForwardingDecision OpDecision =
|
2017-08-08 02:40:29 +08:00
|
|
|
forwardTree(TargetStmt, OpVal, DefStmt, DefLoop, DefToTarget, DoIt);
|
2017-08-04 20:28:42 +08:00
|
|
|
switch (OpDecision) {
|
|
|
|
case FD_CannotForward:
|
|
|
|
assert(!DoIt);
|
|
|
|
return FD_CannotForward;
|
|
|
|
|
|
|
|
case FD_CanForwardLeaf:
|
|
|
|
case FD_CanForwardTree:
|
|
|
|
assert(!DoIt);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FD_DidForward:
|
|
|
|
assert(DoIt);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FD_NotApplicable:
|
|
|
|
llvm_unreachable("forwardTree should never return FD_NotApplicable");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (DoIt)
|
|
|
|
return FD_DidForward;
|
|
|
|
return FD_CanForwardTree;
|
|
|
|
}
|
|
|
|
|
2017-07-22 22:02:47 +08:00
|
|
|
/// Determines whether an operand tree can be forwarded or carries out a
|
|
|
|
/// forwarding, depending on the @p DoIt flag.
|
|
|
|
///
|
2017-08-08 02:40:29 +08:00
|
|
|
/// @param TargetStmt The statement the operand tree will be copied to.
|
|
|
|
/// @param UseVal The value (usually an instruction) which is root of an
|
|
|
|
/// operand tree.
|
|
|
|
/// @param UseStmt The statement that uses @p UseVal.
|
|
|
|
/// @param UseLoop The loop @p UseVal is used in.
|
|
|
|
/// @param UseToTarget { DomainUse[] -> DomainTarget[] }
|
|
|
|
/// A mapping from the statement instance @p UseVal is used
|
|
|
|
/// to the statement instance it is forwarded to.
|
|
|
|
/// @param DoIt If false, only determine whether an operand tree can be
|
|
|
|
/// forwarded. If true, carry out the forwarding. Do not
|
|
|
|
/// use DoIt==true if an operand tree is not known to be
|
|
|
|
/// forwardable.
|
2017-07-22 22:02:47 +08:00
|
|
|
///
|
2017-07-24 20:39:46 +08:00
|
|
|
/// @return If DoIt==false, return whether the operand tree can be forwarded.
|
|
|
|
/// If DoIt==true, return FD_DidForward.
|
2017-08-25 05:22:41 +08:00
|
|
|
ForwardingDecision forwardTree(ScopStmt *TargetStmt, Value *UseVal,
|
|
|
|
ScopStmt *UseStmt, Loop *UseLoop,
|
2017-08-08 02:40:29 +08:00
|
|
|
isl::map UseToTarget, bool DoIt) {
|
|
|
|
ScopStmt *DefStmt = nullptr;
|
|
|
|
Loop *DefLoop = nullptr;
|
|
|
|
|
|
|
|
// { DefDomain[] -> TargetDomain[] }
|
|
|
|
isl::map DefToTarget;
|
|
|
|
|
2017-07-22 22:02:47 +08:00
|
|
|
VirtualUse VUse = VirtualUse::create(UseStmt, UseLoop, UseVal, true);
|
|
|
|
switch (VUse.getKind()) {
|
|
|
|
case VirtualUse::Constant:
|
|
|
|
case VirtualUse::Block:
|
2017-07-22 22:30:02 +08:00
|
|
|
case VirtualUse::Hoisted:
|
2017-07-22 22:02:47 +08:00
|
|
|
// These can be used anywhere without special considerations.
|
|
|
|
if (DoIt)
|
|
|
|
return FD_DidForward;
|
2017-07-24 23:33:58 +08:00
|
|
|
return FD_CanForwardLeaf;
|
2017-07-22 22:02:47 +08:00
|
|
|
|
2017-08-01 03:46:21 +08:00
|
|
|
case VirtualUse::Synthesizable: {
|
|
|
|
// ScopExpander will take care for of generating the code at the new
|
|
|
|
// location.
|
|
|
|
if (DoIt)
|
|
|
|
return FD_DidForward;
|
|
|
|
|
|
|
|
// Check if the value is synthesizable at the new location as well. This
|
|
|
|
// might be possible when leaving a loop for which ScalarEvolution is
|
|
|
|
// unable to derive the exit value for.
|
|
|
|
// TODO: If there is a LCSSA PHI at the loop exit, use that one.
|
|
|
|
// If the SCEV contains a SCEVAddRecExpr, we currently depend on that we
|
|
|
|
// do not forward past its loop header. This would require us to use a
|
|
|
|
// previous loop induction variable instead the current one. We currently
|
|
|
|
// do not allow forwarding PHI nodes, thus this should never occur (the
|
|
|
|
// only exception where no phi is necessary being an unreachable loop
|
|
|
|
// without edge from the outside).
|
|
|
|
VirtualUse TargetUse = VirtualUse::create(
|
|
|
|
S, TargetStmt, TargetStmt->getSurroundingLoop(), UseVal, true);
|
|
|
|
if (TargetUse.getKind() == VirtualUse::Synthesizable)
|
|
|
|
return FD_CanForwardLeaf;
|
|
|
|
|
|
|
|
DEBUG(dbgs() << " Synthesizable would not be synthesizable anymore: "
|
|
|
|
<< *UseVal << "\n");
|
2017-07-22 22:02:47 +08:00
|
|
|
return FD_CannotForward;
|
2017-08-01 03:46:21 +08:00
|
|
|
}
|
2017-07-22 22:02:47 +08:00
|
|
|
|
|
|
|
case VirtualUse::ReadOnly:
|
2017-07-24 23:33:53 +08:00
|
|
|
// Note that we cannot return FD_CanForwardTree here. With a operand tree
|
|
|
|
// depth of 0, UseVal is the use in TargetStmt that we try to replace.
|
|
|
|
// With -polly-analyze-read-only-scalars=true we would ensure the
|
|
|
|
// existence of a MemoryAccess (which already exists for a leaf) and be
|
|
|
|
// removed again by tryForwardTree because it's goal is to remove this
|
|
|
|
// scalar MemoryAccess. It interprets FD_CanForwardTree as the permission
|
|
|
|
// to do so.
|
2017-07-24 20:43:27 +08:00
|
|
|
if (!DoIt)
|
2017-07-24 23:33:58 +08:00
|
|
|
return FD_CanForwardLeaf;
|
2017-07-24 20:43:27 +08:00
|
|
|
|
|
|
|
// If we model read-only scalars, we need to create a MemoryAccess for it.
|
|
|
|
if (ModelReadOnlyScalars)
|
|
|
|
TargetStmt->ensureValueRead(UseVal);
|
|
|
|
|
|
|
|
NumReadOnlyCopied++;
|
|
|
|
TotalReadOnlyCopied++;
|
|
|
|
return FD_DidForward;
|
2017-07-22 22:02:47 +08:00
|
|
|
|
|
|
|
case VirtualUse::Intra:
|
2017-08-08 02:40:29 +08:00
|
|
|
// Knowing that UseStmt and DefStmt are the same statement instance, just
|
|
|
|
// reuse the information about UseStmt for DefStmt
|
|
|
|
DefStmt = UseStmt;
|
|
|
|
DefToTarget = UseToTarget;
|
|
|
|
|
|
|
|
LLVM_FALLTHROUGH;
|
2017-07-22 22:02:47 +08:00
|
|
|
case VirtualUse::Inter:
|
2017-08-08 02:40:29 +08:00
|
|
|
Instruction *Inst = cast<Instruction>(UseVal);
|
|
|
|
|
2017-08-10 00:45:37 +08:00
|
|
|
if (!DefStmt) {
|
2017-08-08 02:40:29 +08:00
|
|
|
DefStmt = S->getStmtFor(Inst);
|
2017-08-10 00:45:37 +08:00
|
|
|
if (!DefStmt)
|
|
|
|
return FD_CannotForward;
|
|
|
|
}
|
|
|
|
|
2017-08-08 02:40:29 +08:00
|
|
|
DefLoop = LI->getLoopFor(Inst->getParent());
|
|
|
|
|
|
|
|
if (DefToTarget.is_null() && !Known.is_null()) {
|
2017-09-20 06:53:20 +08:00
|
|
|
IslQuotaScope QuotaScope = MaxOpGuard.enter(!DoIt);
|
|
|
|
|
2017-08-08 02:40:29 +08:00
|
|
|
// { UseDomain[] -> DefDomain[] }
|
|
|
|
isl::map UseToDef = computeUseToDefFlowDependency(UseStmt, DefStmt);
|
|
|
|
|
|
|
|
// { DefDomain[] -> UseDomain[] -> TargetDomain[] } shortened to
|
|
|
|
// { DefDomain[] -> TargetDomain[] }
|
|
|
|
DefToTarget = UseToTarget.apply_domain(UseToDef);
|
|
|
|
simplify(DefToTarget);
|
|
|
|
}
|
2017-07-22 22:02:47 +08:00
|
|
|
|
2017-08-08 02:40:29 +08:00
|
|
|
ForwardingDecision SpeculativeResult = forwardSpeculatable(
|
|
|
|
TargetStmt, Inst, DefStmt, DefLoop, DefToTarget, DoIt);
|
2017-08-04 20:28:42 +08:00
|
|
|
if (SpeculativeResult != FD_NotApplicable)
|
|
|
|
return SpeculativeResult;
|
2017-08-01 03:46:21 +08:00
|
|
|
|
2017-08-08 02:40:29 +08:00
|
|
|
ForwardingDecision KnownResult =
|
|
|
|
forwardKnownLoad(TargetStmt, Inst, UseStmt, UseLoop, UseToTarget,
|
|
|
|
DefStmt, DefLoop, DefToTarget, DoIt);
|
|
|
|
if (KnownResult != FD_NotApplicable)
|
|
|
|
return KnownResult;
|
|
|
|
|
2017-08-04 20:28:42 +08:00
|
|
|
// When no method is found to forward the operand tree, we effectively
|
|
|
|
// cannot handle it.
|
|
|
|
DEBUG(dbgs() << " Cannot forward instruction: " << *Inst << "\n");
|
|
|
|
return FD_CannotForward;
|
2017-07-22 22:02:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
llvm_unreachable("Case unhandled");
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Try to forward an operand tree rooted in @p RA.
|
|
|
|
bool tryForwardTree(MemoryAccess *RA) {
|
|
|
|
assert(RA->isLatestScalarKind());
|
|
|
|
DEBUG(dbgs() << "Trying to forward operand tree " << RA << "...\n");
|
|
|
|
|
|
|
|
ScopStmt *Stmt = RA->getStatement();
|
|
|
|
Loop *InLoop = Stmt->getSurroundingLoop();
|
|
|
|
|
2017-08-08 02:40:29 +08:00
|
|
|
isl::map TargetToUse;
|
|
|
|
if (!Known.is_null()) {
|
|
|
|
isl::space DomSpace = Stmt->getDomainSpace();
|
|
|
|
TargetToUse =
|
|
|
|
isl::map::identity(DomSpace.map_from_domain_and_range(DomSpace));
|
|
|
|
}
|
|
|
|
|
|
|
|
ForwardingDecision Assessment = forwardTree(
|
|
|
|
Stmt, RA->getAccessValue(), Stmt, InLoop, TargetToUse, false);
|
2017-07-22 22:02:47 +08:00
|
|
|
assert(Assessment != FD_DidForward);
|
2017-07-24 20:43:27 +08:00
|
|
|
if (Assessment != FD_CanForwardTree)
|
2017-07-22 22:02:47 +08:00
|
|
|
return false;
|
|
|
|
|
2017-08-08 02:40:29 +08:00
|
|
|
ForwardingDecision Execution = forwardTree(Stmt, RA->getAccessValue(), Stmt,
|
|
|
|
InLoop, TargetToUse, true);
|
2017-08-02 06:15:04 +08:00
|
|
|
assert(Execution == FD_DidForward &&
|
|
|
|
"A previous positive assessment must also be executable");
|
|
|
|
(void)Execution;
|
2017-07-22 22:02:47 +08:00
|
|
|
|
|
|
|
Stmt->removeSingleMemoryAccess(RA);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Return which SCoP this instance is processing.
|
|
|
|
Scop *getScop() const { return S; }
|
|
|
|
|
|
|
|
/// Run the algorithm: Use value read accesses as operand tree roots and try
|
|
|
|
/// to forward them into the statement.
|
|
|
|
bool forwardOperandTrees() {
|
|
|
|
for (ScopStmt &Stmt : *S) {
|
|
|
|
bool StmtModified = false;
|
|
|
|
|
|
|
|
// Because we are modifying the MemoryAccess list, collect them first to
|
|
|
|
// avoid iterator invalidation.
|
|
|
|
SmallVector<MemoryAccess *, 16> Accs;
|
|
|
|
for (MemoryAccess *RA : Stmt) {
|
|
|
|
if (!RA->isRead())
|
|
|
|
continue;
|
|
|
|
if (!RA->isLatestScalarKind())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
Accs.push_back(RA);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (MemoryAccess *RA : Accs) {
|
|
|
|
if (tryForwardTree(RA)) {
|
|
|
|
Modified = true;
|
|
|
|
StmtModified = true;
|
|
|
|
NumForwardedTrees++;
|
|
|
|
TotalForwardedTrees++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (StmtModified) {
|
|
|
|
NumModifiedStmts++;
|
|
|
|
TotalModifiedStmts++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Modified)
|
|
|
|
ScopsModified++;
|
|
|
|
return Modified;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Print the pass result, performed transformations and the SCoP after the
|
|
|
|
/// transformation.
|
2017-08-25 05:22:41 +08:00
|
|
|
void print(raw_ostream &OS, int Indent = 0) {
|
2017-07-22 22:02:47 +08:00
|
|
|
printStatistics(OS, Indent);
|
|
|
|
|
|
|
|
if (!Modified) {
|
|
|
|
// This line can easily be checked in regression tests.
|
|
|
|
OS << "ForwardOpTree executed, but did not modify anything\n";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
printStatements(OS, Indent);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Pass that redirects scalar reads to array elements that are known to contain
|
|
|
|
/// the same value.
|
|
|
|
///
|
|
|
|
/// This reduces the number of scalar accesses and therefore potentially
|
|
|
|
/// increases the freedom of the scheduler. In the ideal case, all reads of a
|
|
|
|
/// scalar definition are redirected (We currently do not care about removing
|
|
|
|
/// the write in this case). This is also useful for the main DeLICM pass as
|
|
|
|
/// there are less scalars to be mapped.
|
|
|
|
class ForwardOpTree : public ScopPass {
|
|
|
|
private:
|
|
|
|
/// The pass implementation, also holding per-scop data.
|
|
|
|
std::unique_ptr<ForwardOpTreeImpl> Impl;
|
|
|
|
|
|
|
|
public:
|
|
|
|
static char ID;
|
|
|
|
|
|
|
|
explicit ForwardOpTree() : ScopPass(ID) {}
|
2017-08-25 05:22:41 +08:00
|
|
|
ForwardOpTree(const ForwardOpTree &) = delete;
|
|
|
|
ForwardOpTree &operator=(const ForwardOpTree &) = delete;
|
2017-07-22 22:02:47 +08:00
|
|
|
|
2017-08-25 05:22:41 +08:00
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override {
|
2017-07-22 22:02:47 +08:00
|
|
|
AU.addRequiredTransitive<ScopInfoRegionPass>();
|
|
|
|
AU.addRequired<LoopInfoWrapperPass>();
|
|
|
|
AU.setPreservesAll();
|
|
|
|
}
|
|
|
|
|
2017-08-25 05:22:41 +08:00
|
|
|
bool runOnScop(Scop &S) override {
|
2017-07-22 22:02:47 +08:00
|
|
|
// Free resources for previous SCoP's computation, if not yet done.
|
|
|
|
releaseMemory();
|
|
|
|
|
|
|
|
LoopInfo &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
|
|
|
|
|
2017-09-20 06:53:20 +08:00
|
|
|
{
|
|
|
|
IslMaxOperationsGuard MaxOpGuard(S.getIslCtx(), MaxOps, false);
|
|
|
|
Impl = llvm::make_unique<ForwardOpTreeImpl>(&S, &LI, MaxOpGuard);
|
2017-08-08 02:40:29 +08:00
|
|
|
|
2017-09-20 06:53:20 +08:00
|
|
|
if (AnalyzeKnown) {
|
|
|
|
DEBUG(dbgs() << "Prepare forwarders...\n");
|
|
|
|
Impl->computeKnownValues();
|
|
|
|
}
|
|
|
|
|
|
|
|
DEBUG(dbgs() << "Forwarding operand trees...\n");
|
|
|
|
Impl->forwardOperandTrees();
|
|
|
|
|
|
|
|
if (MaxOpGuard.hasQuotaExceeded()) {
|
|
|
|
DEBUG(dbgs() << "Not all operations completed because of "
|
|
|
|
"max_operations exceeded\n");
|
|
|
|
KnownOutOfQuota++;
|
|
|
|
}
|
|
|
|
}
|
2017-07-22 22:02:47 +08:00
|
|
|
|
|
|
|
DEBUG(dbgs() << "\nFinal Scop:\n");
|
|
|
|
DEBUG(dbgs() << S);
|
|
|
|
|
2017-08-23 21:50:30 +08:00
|
|
|
// Update statistics
|
|
|
|
auto ScopStats = S.getStatistics();
|
|
|
|
NumValueWrites += ScopStats.NumValueWrites;
|
|
|
|
NumValueWritesInLoops += ScopStats.NumValueWritesInLoops;
|
|
|
|
NumPHIWrites += ScopStats.NumPHIWrites;
|
|
|
|
NumPHIWritesInLoops += ScopStats.NumPHIWritesInLoops;
|
|
|
|
NumSingletonWrites += ScopStats.NumSingletonWrites;
|
|
|
|
NumSingletonWritesInLoops += ScopStats.NumSingletonWritesInLoops;
|
|
|
|
|
2017-07-22 22:02:47 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-08-25 05:22:41 +08:00
|
|
|
void printScop(raw_ostream &OS, Scop &S) const override {
|
2017-07-22 22:02:47 +08:00
|
|
|
if (!Impl)
|
|
|
|
return;
|
|
|
|
|
|
|
|
assert(Impl->getScop() == &S);
|
|
|
|
Impl->print(OS);
|
|
|
|
}
|
|
|
|
|
2017-08-25 05:22:41 +08:00
|
|
|
void releaseMemory() override { Impl.reset(); }
|
2017-07-22 22:02:47 +08:00
|
|
|
}; // class ForwardOpTree
|
|
|
|
|
|
|
|
char ForwardOpTree::ID;
|
2017-08-25 05:22:41 +08:00
|
|
|
|
|
|
|
} // namespace
|
2017-07-22 22:02:47 +08:00
|
|
|
|
|
|
|
ScopPass *polly::createForwardOpTreePass() { return new ForwardOpTree(); }
|
|
|
|
|
|
|
|
INITIALIZE_PASS_BEGIN(ForwardOpTree, "polly-optree",
|
|
|
|
"Polly - Forward operand tree", false, false)
|
|
|
|
INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
|
|
|
|
INITIALIZE_PASS_END(ForwardOpTree, "polly-optree",
|
|
|
|
"Polly - Forward operand tree", false, false)
|