Skip to content

Enable CSE on OSSA #34895

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Dec 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions include/swift/SIL/InstructionUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,13 @@ SILValue stripCasts(SILValue V);
/// mark_dependence) from the current SILValue.
SILValue stripCastsWithoutMarkDependence(SILValue V);

/// Return the underlying SILValue after stripping off all copy_value and
/// Return the underlying SILValue after looking through all copy_value and
/// begin_borrow instructions.
SILValue stripOwnershipInsts(SILValue v);
SILValue lookThroughOwnershipInsts(SILValue v);

/// Return the underlying SILValue after looking through all copy_value
/// instructions.
SILValue lookThroughCopyValueInsts(SILValue v);

/// Return the underlying SILValue after stripping off all upcasts from the
/// current SILValue.
Expand Down
35 changes: 35 additions & 0 deletions include/swift/SIL/SILInstruction.h
Original file line number Diff line number Diff line change
Expand Up @@ -523,12 +523,20 @@ class SILInstruction
private:
/// Predicate used to filter OperandValueRange.
struct OperandToValue;
/// Predicate used to filter TransformedOperandValueRange.
struct OperandToTransformedValue;

public:
using OperandValueRange =
OptionalTransformRange<ArrayRef<Operand>, OperandToValue>;
using TransformedOperandValueRange =
OptionalTransformRange<ArrayRef<Operand>, OperandToTransformedValue>;

OperandValueRange
getOperandValues(bool skipTypeDependentOperands = false) const;
TransformedOperandValueRange
getOperandValues(std::function<SILValue(SILValue)> transformFn,
bool skipTypeDependentOperands) const;

SILValue getOperand(unsigned Num) const {
return getAllOperands()[Num].get();
Expand Down Expand Up @@ -727,13 +735,40 @@ struct SILInstruction::OperandToValue {
}
};

struct SILInstruction::OperandToTransformedValue {
const SILInstruction &i;
std::function<SILValue(SILValue)> transformFn;
bool skipTypeDependentOps;

OperandToTransformedValue(const SILInstruction &i,
std::function<SILValue(SILValue)> transformFn,
bool skipTypeDependentOps)
: i(i), transformFn(transformFn),
skipTypeDependentOps(skipTypeDependentOps) {}

Optional<SILValue> operator()(const Operand &use) const {
if (skipTypeDependentOps && i.isTypeDependentOperand(use))
return None;
return transformFn(use.get());
}
};

inline auto
SILInstruction::getOperandValues(bool skipTypeDependentOperands) const
-> OperandValueRange {
return OperandValueRange(getAllOperands(),
OperandToValue(*this, skipTypeDependentOperands));
}

inline auto
SILInstruction::getOperandValues(std::function<SILValue(SILValue)> transformFn,
bool skipTypeDependentOperands) const
-> TransformedOperandValueRange {
return TransformedOperandValueRange(
getAllOperands(),
OperandToTransformedValue(*this, transformFn, skipTypeDependentOperands));
}

struct SILInstruction::OperandToType {
const SILInstruction &i;

Expand Down
2 changes: 1 addition & 1 deletion include/swift/SILOptimizer/Utils/OwnershipOptUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ struct OwnershipFixupContext {
/// Namely, we do not support RAUWing values with ValueOwnershipKind::None
/// that have uses that do not require ValueOwnershipKind::None or
/// ValueOwnershipKind::Any.
static bool canFixUpOwnershipForRAUW(SingleValueInstruction *oldValue,
static bool canFixUpOwnershipForRAUW(const SingleValueInstruction *oldValue,
SILValue newValue);
};

Expand Down
8 changes: 8 additions & 0 deletions lib/SIL/IR/SILInstruction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,14 @@ namespace {
return true;
}

bool visitDestructureStructInst(const DestructureStructInst *RHS) {
return true;
}

bool visitDestructureTupleInst(const DestructureTupleInst *RHS) {
return true;
}

bool visitAllocRefInst(const AllocRefInst *RHS) {
auto *LHSInst = cast<AllocRefInst>(LHS);
auto LHSTypes = LHSInst->getTailAllocatedTypes();
Expand Down
20 changes: 14 additions & 6 deletions lib/SIL/Utils/InstructionUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

using namespace swift;

SILValue swift::stripOwnershipInsts(SILValue v) {
SILValue swift::lookThroughOwnershipInsts(SILValue v) {
while (true) {
switch (v->getKind()) {
default:
Expand All @@ -37,6 +37,14 @@ SILValue swift::stripOwnershipInsts(SILValue v) {
}
}

SILValue swift::lookThroughCopyValueInsts(SILValue val) {
while (auto *cvi =
dyn_cast_or_null<CopyValueInst>(val->getDefiningInstruction())) {
val = cvi->getOperand();
}
return val;
}

/// Strip off casts/indexing insts/address projections from V until there is
/// nothing left to strip.
///
Expand All @@ -46,7 +54,7 @@ SILValue swift::getUnderlyingObject(SILValue v) {
SILValue v2 = stripCasts(v);
v2 = stripAddressProjections(v2);
v2 = stripIndexingInsts(v2);
v2 = stripOwnershipInsts(v2);
v2 = lookThroughOwnershipInsts(v2);
if (v2 == v)
return v2;
v = v2;
Expand All @@ -58,7 +66,7 @@ SILValue swift::getUnderlyingObjectStopAtMarkDependence(SILValue v) {
SILValue v2 = stripCastsWithoutMarkDependence(v);
v2 = stripAddressProjections(v2);
v2 = stripIndexingInsts(v2);
v2 = stripOwnershipInsts(v2);
v2 = lookThroughOwnershipInsts(v2);
if (v2 == v)
return v2;
v = v2;
Expand Down Expand Up @@ -137,7 +145,7 @@ SILValue swift::stripCasts(SILValue v) {
continue;
}
}
SILValue v2 = stripOwnershipInsts(v);
SILValue v2 = lookThroughOwnershipInsts(v);
if (v2 != v) {
v = v2;
continue;
Expand All @@ -159,7 +167,7 @@ SILValue swift::stripUpCasts(SILValue v) {
}

SILValue v2 = stripSinglePredecessorArgs(v);
v2 = stripOwnershipInsts(v2);
v2 = lookThroughOwnershipInsts(v2);
if (v2 == v) {
return v2;
}
Expand All @@ -179,7 +187,7 @@ SILValue swift::stripClassCasts(SILValue v) {
continue;
}

SILValue v2 = stripOwnershipInsts(v);
SILValue v2 = lookThroughOwnershipInsts(v);
if (v2 != v) {
v = v2;
continue;
Expand Down
Loading