Skip to content

Commit b99533a

Browse files
authored
Merge pull request #34895 from meg-gupta/cseossa
Enable CSE on OSSA
2 parents 2497273 + 42c0319 commit b99533a

File tree

14 files changed

+2887
-133
lines changed

14 files changed

+2887
-133
lines changed

include/swift/SIL/InstructionUtils.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,13 @@ SILValue stripCasts(SILValue V);
3737
/// mark_dependence) from the current SILValue.
3838
SILValue stripCastsWithoutMarkDependence(SILValue V);
3939

40-
/// Return the underlying SILValue after stripping off all copy_value and
40+
/// Return the underlying SILValue after looking through all copy_value and
4141
/// begin_borrow instructions.
42-
SILValue stripOwnershipInsts(SILValue v);
42+
SILValue lookThroughOwnershipInsts(SILValue v);
43+
44+
/// Return the underlying SILValue after looking through all copy_value
45+
/// instructions.
46+
SILValue lookThroughCopyValueInsts(SILValue v);
4347

4448
/// Return the underlying SILValue after stripping off all upcasts from the
4549
/// current SILValue.

include/swift/SIL/SILInstruction.h

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -523,12 +523,20 @@ class SILInstruction
523523
private:
524524
/// Predicate used to filter OperandValueRange.
525525
struct OperandToValue;
526+
/// Predicate used to filter TransformedOperandValueRange.
527+
struct OperandToTransformedValue;
526528

527529
public:
528530
using OperandValueRange =
529531
OptionalTransformRange<ArrayRef<Operand>, OperandToValue>;
532+
using TransformedOperandValueRange =
533+
OptionalTransformRange<ArrayRef<Operand>, OperandToTransformedValue>;
534+
530535
OperandValueRange
531536
getOperandValues(bool skipTypeDependentOperands = false) const;
537+
TransformedOperandValueRange
538+
getOperandValues(std::function<SILValue(SILValue)> transformFn,
539+
bool skipTypeDependentOperands) const;
532540

533541
SILValue getOperand(unsigned Num) const {
534542
return getAllOperands()[Num].get();
@@ -727,13 +735,40 @@ struct SILInstruction::OperandToValue {
727735
}
728736
};
729737

738+
struct SILInstruction::OperandToTransformedValue {
739+
const SILInstruction &i;
740+
std::function<SILValue(SILValue)> transformFn;
741+
bool skipTypeDependentOps;
742+
743+
OperandToTransformedValue(const SILInstruction &i,
744+
std::function<SILValue(SILValue)> transformFn,
745+
bool skipTypeDependentOps)
746+
: i(i), transformFn(transformFn),
747+
skipTypeDependentOps(skipTypeDependentOps) {}
748+
749+
Optional<SILValue> operator()(const Operand &use) const {
750+
if (skipTypeDependentOps && i.isTypeDependentOperand(use))
751+
return None;
752+
return transformFn(use.get());
753+
}
754+
};
755+
730756
inline auto
731757
SILInstruction::getOperandValues(bool skipTypeDependentOperands) const
732758
-> OperandValueRange {
733759
return OperandValueRange(getAllOperands(),
734760
OperandToValue(*this, skipTypeDependentOperands));
735761
}
736762

763+
inline auto
764+
SILInstruction::getOperandValues(std::function<SILValue(SILValue)> transformFn,
765+
bool skipTypeDependentOperands) const
766+
-> TransformedOperandValueRange {
767+
return TransformedOperandValueRange(
768+
getAllOperands(),
769+
OperandToTransformedValue(*this, transformFn, skipTypeDependentOperands));
770+
}
771+
737772
struct SILInstruction::OperandToType {
738773
const SILInstruction &i;
739774

include/swift/SILOptimizer/Utils/OwnershipOptUtils.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ struct OwnershipFixupContext {
4242
/// Namely, we do not support RAUWing values with ValueOwnershipKind::None
4343
/// that have uses that do not require ValueOwnershipKind::None or
4444
/// ValueOwnershipKind::Any.
45-
static bool canFixUpOwnershipForRAUW(SingleValueInstruction *oldValue,
45+
static bool canFixUpOwnershipForRAUW(const SingleValueInstruction *oldValue,
4646
SILValue newValue);
4747
};
4848

lib/SIL/IR/SILInstruction.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,14 @@ namespace {
372372
return true;
373373
}
374374

375+
bool visitDestructureStructInst(const DestructureStructInst *RHS) {
376+
return true;
377+
}
378+
379+
bool visitDestructureTupleInst(const DestructureTupleInst *RHS) {
380+
return true;
381+
}
382+
375383
bool visitAllocRefInst(const AllocRefInst *RHS) {
376384
auto *LHSInst = cast<AllocRefInst>(LHS);
377385
auto LHSTypes = LHSInst->getTailAllocatedTypes();

lib/SIL/Utils/InstructionUtils.cpp

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
using namespace swift;
2727

28-
SILValue swift::stripOwnershipInsts(SILValue v) {
28+
SILValue swift::lookThroughOwnershipInsts(SILValue v) {
2929
while (true) {
3030
switch (v->getKind()) {
3131
default:
@@ -37,6 +37,14 @@ SILValue swift::stripOwnershipInsts(SILValue v) {
3737
}
3838
}
3939

40+
SILValue swift::lookThroughCopyValueInsts(SILValue val) {
41+
while (auto *cvi =
42+
dyn_cast_or_null<CopyValueInst>(val->getDefiningInstruction())) {
43+
val = cvi->getOperand();
44+
}
45+
return val;
46+
}
47+
4048
/// Strip off casts/indexing insts/address projections from V until there is
4149
/// nothing left to strip.
4250
///
@@ -46,7 +54,7 @@ SILValue swift::getUnderlyingObject(SILValue v) {
4654
SILValue v2 = stripCasts(v);
4755
v2 = stripAddressProjections(v2);
4856
v2 = stripIndexingInsts(v2);
49-
v2 = stripOwnershipInsts(v2);
57+
v2 = lookThroughOwnershipInsts(v2);
5058
if (v2 == v)
5159
return v2;
5260
v = v2;
@@ -58,7 +66,7 @@ SILValue swift::getUnderlyingObjectStopAtMarkDependence(SILValue v) {
5866
SILValue v2 = stripCastsWithoutMarkDependence(v);
5967
v2 = stripAddressProjections(v2);
6068
v2 = stripIndexingInsts(v2);
61-
v2 = stripOwnershipInsts(v2);
69+
v2 = lookThroughOwnershipInsts(v2);
6270
if (v2 == v)
6371
return v2;
6472
v = v2;
@@ -137,7 +145,7 @@ SILValue swift::stripCasts(SILValue v) {
137145
continue;
138146
}
139147
}
140-
SILValue v2 = stripOwnershipInsts(v);
148+
SILValue v2 = lookThroughOwnershipInsts(v);
141149
if (v2 != v) {
142150
v = v2;
143151
continue;
@@ -159,7 +167,7 @@ SILValue swift::stripUpCasts(SILValue v) {
159167
}
160168

161169
SILValue v2 = stripSinglePredecessorArgs(v);
162-
v2 = stripOwnershipInsts(v2);
170+
v2 = lookThroughOwnershipInsts(v2);
163171
if (v2 == v) {
164172
return v2;
165173
}
@@ -179,7 +187,7 @@ SILValue swift::stripClassCasts(SILValue v) {
179187
continue;
180188
}
181189

182-
SILValue v2 = stripOwnershipInsts(v);
190+
SILValue v2 = lookThroughOwnershipInsts(v);
183191
if (v2 != v) {
184192
v = v2;
185193
continue;

0 commit comments

Comments
 (0)