Skip to content

Commit f082584

Browse files
committed
Use the new side effects in CSE
1 parent 42ede18 commit f082584

File tree

3 files changed

+27
-17
lines changed

3 files changed

+27
-17
lines changed

lib/SILOptimizer/Transforms/CSE.cpp

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@
2727
#include "swift/SIL/SILValue.h"
2828
#include "swift/SIL/SILVisitor.h"
2929
#include "swift/SILOptimizer/Analysis/ArraySemantic.h"
30+
#include "swift/SILOptimizer/Analysis/BasicCalleeAnalysis.h"
3031
#include "swift/SILOptimizer/Analysis/DominanceAnalysis.h"
31-
#include "swift/SILOptimizer/Analysis/SideEffectAnalysis.h"
3232
#include "swift/SILOptimizer/Analysis/SimplifyInstruction.h"
3333
#include "swift/SILOptimizer/PassManager/Passes.h"
3434
#include "swift/SILOptimizer/PassManager/Transforms.h"
@@ -583,7 +583,7 @@ class CSE {
583583
/// their lookup.
584584
ScopedHTType *AvailableValues;
585585

586-
SideEffectAnalysis *SEA;
586+
BasicCalleeAnalysis *BCA;
587587

588588
SILOptFunctionBuilder &FuncBuilder;
589589

@@ -595,10 +595,10 @@ class CSE {
595595
/// load of the property value.
596596
llvm::SmallVector<ApplyInst *, 8> lazyPropertyGetters;
597597

598-
CSE(bool RunsOnHighLevelSil, SideEffectAnalysis *SEA,
598+
CSE(bool RunsOnHighLevelSil, BasicCalleeAnalysis *BCA,
599599
SILOptFunctionBuilder &FuncBuilder, DeadEndBlocks &DeadEndBBs,
600600
OwnershipFixupContext &RAUWFixupContext)
601-
: SEA(SEA), FuncBuilder(FuncBuilder), DeadEndBBs(DeadEndBBs),
601+
: BCA(BCA), FuncBuilder(FuncBuilder), DeadEndBBs(DeadEndBBs),
602602
RAUWFixupContext(RAUWFixupContext),
603603
RunsOnHighLevelSil(RunsOnHighLevelSil) {}
604604

@@ -1064,16 +1064,22 @@ bool CSE::canHandle(SILInstruction *Inst) {
10641064
return false;
10651065
}
10661066
}
1067-
1067+
1068+
if (!AI->getFunction()->hasOwnership()) {
1069+
// In non-OSSA we don't balance CSE'd apply results which return an
1070+
// owned value.
1071+
if (auto ri = AI->getSingleResult()) {
1072+
if (ri.getValue().getConvention() != ResultConvention::Unowned)
1073+
return false;
1074+
}
1075+
}
1076+
10681077
// We can CSE function calls which do not read or write memory and don't
10691078
// have any other side effects.
1070-
FunctionSideEffects Effects;
1071-
SEA->getCalleeEffects(Effects, AI);
1072-
10731079
// Note that the function also may not contain any retains. And there are
10741080
// functions which are read-none and have a retain, e.g. functions which
10751081
// _convert_ a global_addr to a reference and retain it.
1076-
auto MB = Effects.getMemBehavior(RetainObserveKind::ObserveRetains);
1082+
auto MB = BCA->getMemoryBehavior(ApplySite(AI), /*observeRetains*/false);
10771083
if (MB == SILInstruction::MemoryBehavior::None)
10781084
return true;
10791085

@@ -1392,14 +1398,14 @@ class SILCSE : public SILFunctionTransform {
13921398

13931399
DominanceAnalysis* DA = getAnalysis<DominanceAnalysis>();
13941400

1395-
auto *SEA = PM->getAnalysis<SideEffectAnalysis>();
1401+
auto *BCA = PM->getAnalysis<BasicCalleeAnalysis>();
13961402
SILOptFunctionBuilder FuncBuilder(*this);
13971403

13981404
auto *Fn = getFunction();
13991405
DeadEndBlocks DeadEndBBs(Fn);
14001406
InstModCallbacks callbacks;
14011407
OwnershipFixupContext FixupCtx{callbacks, DeadEndBBs};
1402-
CSE C(RunsOnHighLevelSil, SEA, FuncBuilder, DeadEndBBs, FixupCtx);
1408+
CSE C(RunsOnHighLevelSil, BCA, FuncBuilder, DeadEndBBs, FixupCtx);
14031409
bool Changed = false;
14041410

14051411
// Perform the traditional CSE.

test/SILOptimizer/cse_apply.sil

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
// RUN: %target-sil-opt -enable-sil-verify-all %s -cse | %FileCheck %s
1+
// RUN: %target-sil-opt -enable-sil-verify-all %s -compute-side-effects -cse | %FileCheck %s
2+
3+
// REQUIRES: swift_in_compiler
24

35
sil_stage canonical
46

@@ -72,7 +74,7 @@ class XX {
7274

7375
sil_global @ggs : $Storage
7476

75-
sil @retain_only : $@convention(thin) () -> XX {
77+
sil @retain_only : $@convention(thin) () -> @owned XX {
7678
bb0:
7779
%g = global_addr @ggs : $*Storage
7880
%a = address_to_pointer %g : $*Storage to $Builtin.RawPointer
@@ -130,9 +132,9 @@ bb0(%0 : $Int64):
130132
//CHECK: return
131133
sil @dont_cse_retain_only_apply : $@convention(thin) () -> () {
132134
bb0:
133-
%f = function_ref @retain_only : $@convention(thin) () -> XX
134-
%a1 = apply %f() : $@convention(thin) () -> XX
135-
%a2 = apply %f() : $@convention(thin) () -> XX
135+
%f = function_ref @retain_only : $@convention(thin) () -> @owned XX
136+
%a1 = apply %f() : $@convention(thin) () -> @owned XX
137+
%a2 = apply %f() : $@convention(thin) () -> @owned XX
136138
strong_release %a1 : $XX
137139
strong_release %a2 : $XX
138140
%r = tuple()

test/SILOptimizer/cse_apply_ossa.sil

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
// RUN: %target-sil-opt -enable-sil-verify-all %s -cse | %FileCheck %s
1+
// RUN: %target-sil-opt -enable-sil-verify-all %s -compute-side-effects -cse | %FileCheck %s
2+
3+
// REQUIRES: swift_in_compiler
24

35
sil_stage canonical
46

0 commit comments

Comments
 (0)