Skip to content

Commit

Permalink
SILOptimizer: Remove InspectionMode from MemBehehaviorVisitor
Browse files Browse the repository at this point in the history
The InspectionMode was never set to anything else than "IgnoreRetains"
  • Loading branch information
eeckstein committed Oct 9, 2020
1 parent a665ba6 commit aced5c7
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 108 deletions.
47 changes: 9 additions & 38 deletions include/swift/SILOptimizer/Analysis/AliasAnalysis.h
Expand Up @@ -47,7 +47,6 @@ namespace {
struct MemBehaviorKeyTy {
// The SILValue pair:
size_t V1, V2;
RetainObserveKind InspectionMode;
};
}

Expand Down Expand Up @@ -201,24 +200,16 @@ class AliasAnalysis : public SILAnalysis {

/// Use the alias analysis to determine the memory behavior of Inst with
/// respect to V.
///
/// TODO: When ref count behavior is separated from generic memory behavior,
/// the InspectionMode flag will be unnecessary.
MemoryBehavior computeMemoryBehavior(SILInstruction *Inst, SILValue V,
RetainObserveKind);
MemoryBehavior computeMemoryBehavior(SILInstruction *Inst, SILValue V);

/// Use the alias analysis to determine the memory behavior of Inst with
/// respect to V.
///
/// TODO: When ref count behavior is separated from generic memory behavior,
/// the InspectionMode flag will be unnecessary.
MemoryBehavior computeMemoryBehaviorInner(SILInstruction *Inst, SILValue V,
RetainObserveKind);
MemoryBehavior computeMemoryBehaviorInner(SILInstruction *Inst, SILValue V);

/// Returns true if \p Inst may read from memory in a manner that
/// affects V.
bool mayReadFromMemory(SILInstruction *Inst, SILValue V) {
auto B = computeMemoryBehavior(Inst, V, RetainObserveKind::IgnoreRetains);
auto B = computeMemoryBehavior(Inst, V);
return B == MemoryBehavior::MayRead ||
B == MemoryBehavior::MayReadWrite ||
B == MemoryBehavior::MayHaveSideEffects;
Expand All @@ -227,7 +218,7 @@ class AliasAnalysis : public SILAnalysis {
/// Returns true if \p Inst may write to memory in a manner that
/// affects V.
bool mayWriteToMemory(SILInstruction *Inst, SILValue V) {
auto B = computeMemoryBehavior(Inst, V, RetainObserveKind::IgnoreRetains);
auto B = computeMemoryBehavior(Inst, V);
return B == MemoryBehavior::MayWrite ||
B == MemoryBehavior::MayReadWrite ||
B == MemoryBehavior::MayHaveSideEffects;
Expand All @@ -236,26 +227,10 @@ class AliasAnalysis : public SILAnalysis {
/// Returns true if \p Inst may read or write to memory in a manner that
/// affects V.
bool mayReadOrWriteMemory(SILInstruction *Inst, SILValue V) {
auto B = computeMemoryBehavior(Inst, V, RetainObserveKind::IgnoreRetains);
auto B = computeMemoryBehavior(Inst, V);
return MemoryBehavior::None != B;
}

/// Returns true if Inst may have side effects in a manner that affects V.
bool mayHaveSideEffects(SILInstruction *Inst, SILValue V) {
auto B = computeMemoryBehavior(Inst, V, RetainObserveKind::ObserveRetains);
return B == MemoryBehavior::MayWrite ||
B == MemoryBehavior::MayReadWrite ||
B == MemoryBehavior::MayHaveSideEffects;
}

/// Returns true if Inst may have side effects in a manner that affects
/// V. This is independent of whether or not Inst may write to V and is meant
/// to encode notions such as ref count modifications.
bool mayHavePureSideEffects(SILInstruction *Inst, SILValue V) {
auto B = computeMemoryBehavior(Inst, V, RetainObserveKind::ObserveRetains);
return MemoryBehavior::MayHaveSideEffects == B;
}

/// Returns true if \p Ptr may be released in the function call \p FAS.
bool canApplyDecrementRefCount(FullApplySite FAS, SILValue Ptr);

Expand All @@ -268,8 +243,7 @@ class AliasAnalysis : public SILAnalysis {
AliasKeyTy toAliasKey(SILValue V1, SILValue V2, SILType Type1, SILType Type2);

/// Encodes the memory behavior query as a MemBehaviorKeyTy.
MemBehaviorKeyTy toMemoryBehaviorKey(SILInstruction *V1, SILValue V2,
RetainObserveKind K);
MemBehaviorKeyTy toMemoryBehaviorKey(SILInstruction *V1, SILValue V2);

virtual void invalidate() override {
AliasCache.clear();
Expand Down Expand Up @@ -330,24 +304,21 @@ namespace llvm {
template <> struct DenseMapInfo<MemBehaviorKeyTy> {
static inline MemBehaviorKeyTy getEmptyKey() {
auto Allone = std::numeric_limits<size_t>::max();
return {0, Allone, RetainObserveKind::RetainObserveKindEnd};
return {0, Allone};
}
static inline MemBehaviorKeyTy getTombstoneKey() {
auto Allone = std::numeric_limits<size_t>::max();
return {Allone, 0, RetainObserveKind::RetainObserveKindEnd};
return {Allone, 0};
}
static unsigned getHashValue(const MemBehaviorKeyTy V) {
unsigned H = 0;
H ^= DenseMapInfo<size_t>::getHashValue(V.V1);
H ^= DenseMapInfo<size_t>::getHashValue(V.V2);
H ^= DenseMapInfo<int>::getHashValue(static_cast<int>(V.InspectionMode));
return H;
}
static bool isEqual(const MemBehaviorKeyTy LHS,
const MemBehaviorKeyTy RHS) {
return LHS.V1 == RHS.V1 &&
LHS.V2 == RHS.V2 &&
LHS.InspectionMode == RHS.InspectionMode;
return LHS.V1 == RHS.V1 && LHS.V2 == RHS.V2;
}
};
}
Expand Down
40 changes: 13 additions & 27 deletions lib/SILOptimizer/Analysis/MemoryBehavior.cpp
Expand Up @@ -66,15 +66,10 @@ class MemoryBehaviorVisitor
/// The SILType of the value.
Optional<SILType> TypedAccessTy;

/// Should we treat instructions that increment ref counts as None instead of
/// MayHaveSideEffects.
RetainObserveKind InspectionMode;

public:
MemoryBehaviorVisitor(AliasAnalysis *AA, SideEffectAnalysis *SEA,
EscapeAnalysis *EA, SILValue V,
RetainObserveKind IgnoreRefCountIncs)
: AA(AA), SEA(SEA), EA(EA), V(V), InspectionMode(IgnoreRefCountIncs) {}
EscapeAnalysis *EA, SILValue V)
: AA(AA), SEA(SEA), EA(EA), V(V) {}

SILType getValueTBAAType() {
if (!TypedAccessTy)
Expand Down Expand Up @@ -223,9 +218,7 @@ class MemoryBehaviorVisitor
// memory this will be unnecessary.
#define REFCOUNTINC_MEMBEHAVIOR_INST(Name) \
MemBehavior visit##Name(Name *I) { \
if (InspectionMode == RetainObserveKind::IgnoreRetains) \
return MemBehavior::None; \
return I->getMemoryBehavior(); \
return MemBehavior::None; \
}
REFCOUNTINC_MEMBEHAVIOR_INST(StrongRetainInst)
REFCOUNTINC_MEMBEHAVIOR_INST(RetainValueInst)
Expand Down Expand Up @@ -364,19 +357,15 @@ MemBehavior MemoryBehaviorVisitor::visitApplyInst(ApplyInst *AI) {
// one the parameters in the function call is @in_guaranteed of V, ie. the
// callee isn't allowed to modify it.
Behavior = MemBehavior::MayRead;
} else if (ApplyEffects.mayReadRC() ||
(InspectionMode == RetainObserveKind::ObserveRetains &&
ApplyEffects.mayAllocObjects())) {
Behavior = MemBehavior::MayHaveSideEffects;
} else {
auto &GlobalEffects = ApplyEffects.getGlobalEffects();
Behavior = GlobalEffects.getMemBehavior(InspectionMode);
Behavior = GlobalEffects.getMemBehavior(RetainObserveKind::IgnoreRetains);

// Check all parameter effects.
for (unsigned Idx = 0, End = AI->getNumArguments();
Idx < End && Behavior < MemBehavior::MayHaveSideEffects; ++Idx) {
auto &ArgEffect = ApplyEffects.getParameterEffects()[Idx];
auto ArgBehavior = ArgEffect.getMemBehavior(InspectionMode);
auto ArgBehavior = ArgEffect.getMemBehavior(RetainObserveKind::IgnoreRetains);
if (ArgEffect.mayRelease()) {
Behavior = MemBehavior::MayHaveSideEffects;
break;
Expand Down Expand Up @@ -442,9 +431,8 @@ visitBeginCOWMutationInst(BeginCOWMutationInst *BCMI) {
//===----------------------------------------------------------------------===//

MemBehavior
AliasAnalysis::computeMemoryBehavior(SILInstruction *Inst, SILValue V,
RetainObserveKind InspectionMode) {
MemBehaviorKeyTy Key = toMemoryBehaviorKey(Inst, V, InspectionMode);
AliasAnalysis::computeMemoryBehavior(SILInstruction *Inst, SILValue V) {
MemBehaviorKeyTy Key = toMemoryBehaviorKey(Inst, V);
// Check if we've already computed this result.
auto It = MemoryBehaviorCache.find(Key);
if (It != MemoryBehaviorCache.end()) {
Expand All @@ -457,27 +445,25 @@ AliasAnalysis::computeMemoryBehavior(SILInstruction *Inst, SILValue V,
MemoryBehaviorNodeToIndex.clear();

// Key is no longer valid as we cleared the MemoryBehaviorNodeToIndex.
Key = toMemoryBehaviorKey(Inst, V, InspectionMode);
Key = toMemoryBehaviorKey(Inst, V);
}

// Calculate the aliasing result and store it in the cache.
auto Result = computeMemoryBehaviorInner(Inst, V, InspectionMode);
auto Result = computeMemoryBehaviorInner(Inst, V);
MemoryBehaviorCache[Key] = Result;
return Result;
}

MemBehavior
AliasAnalysis::computeMemoryBehaviorInner(SILInstruction *Inst, SILValue V,
RetainObserveKind InspectionMode) {
AliasAnalysis::computeMemoryBehaviorInner(SILInstruction *Inst, SILValue V) {
LLVM_DEBUG(llvm::dbgs() << "GET MEMORY BEHAVIOR FOR:\n " << *Inst << " "
<< *V);
assert(SEA && "SideEffectsAnalysis must be initialized!");
return MemoryBehaviorVisitor(this, SEA, EA, V, InspectionMode).visit(Inst);
return MemoryBehaviorVisitor(this, SEA, EA, V).visit(Inst);
}

MemBehaviorKeyTy AliasAnalysis::toMemoryBehaviorKey(SILInstruction *V1,
SILValue V2,
RetainObserveKind M) {
SILValue V2) {
size_t idx1 =
MemoryBehaviorNodeToIndex.getIndex(V1->getRepresentativeSILNodeInObject());
assert(idx1 != std::numeric_limits<size_t>::max() &&
Expand All @@ -486,5 +472,5 @@ MemBehaviorKeyTy AliasAnalysis::toMemoryBehaviorKey(SILInstruction *V1,
V2->getRepresentativeSILNodeInObject());
assert(idx2 != std::numeric_limits<size_t>::max() &&
"~0 index reserved for empty/tombstone keys");
return {idx1, idx2, M};
return {idx1, idx2};
}
4 changes: 1 addition & 3 deletions lib/SILOptimizer/UtilityPasses/MemBehaviorDumper.cpp
Expand Up @@ -88,11 +88,9 @@ class MemBehaviorDumper : public SILModuleTransform {

bool Read = AA->mayReadFromMemory(&I, V);
bool Write = AA->mayWriteToMemory(&I, V);
bool SideEffects = AA->mayHaveSideEffects(&I, V);
llvm::outs() << "PAIR #" << PairCount++ << ".\n"
<< " " << I << " " << V
<< " r=" << Read << ",w=" << Write
<< ",se=" << SideEffects << "\n";
<< " r=" << Read << ",w=" << Write << "\n";
}
}
}
Expand Down

0 comments on commit aced5c7

Please sign in to comment.