Skip to content

Commit 4440beb

Browse files
committed
Swift Optimizer: add bridging to the DeadEndBlocksAnalysis
1 parent 40200d6 commit 4440beb

File tree

7 files changed

+63
-1
lines changed

7 files changed

+63
-1
lines changed

SwiftCompilerSources/Sources/Optimizer/Analysis/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@
88

99
swift_compiler_sources(Optimizer
1010
AliasAnalysis.swift
11-
CalleeAnalysis.swift)
11+
CalleeAnalysis.swift
12+
DeadEndBlocksAnalysis.swift)
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//===--- DeadEndBlocksAnalysis.swift - the dead-end blocks analysis -------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2021 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
import OptimizerBridging
14+
import SIL
15+
16+
public struct DeadEndBlocksAnalysis {
17+
let bridged: BridgedDeadEndBlocksAnalysis
18+
19+
public func isDeadEnd(_ block: BasicBlock) -> Bool {
20+
return DeadEndBlocksAnalysis_isDeadEnd(bridged, block.bridged) != 0
21+
}
22+
}

SwiftCompilerSources/Sources/Optimizer/PassManager/PassUtils.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ struct PassContext {
3636
return CalleeAnalysis(bridged: bridgeCA)
3737
}
3838

39+
var deadEndBlocks: DeadEndBlocksAnalysis {
40+
let bridgeDEA = PassContext_getDeadEndBlocksAnalysis(_bridged)
41+
return DeadEndBlocksAnalysis(bridged: bridgeDEA)
42+
}
43+
3944
func notifyInstructionsChanged() {
4045
PassContext_notifyChanges(_bridged, instructionsChanged)
4146
}

include/swift/SILOptimizer/Analysis/DeadEndBlocksAnalysis.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ class DeadEndBlocksAnalysis final : public FunctionAnalysisBase<DeadEndBlocks> {
2626
DeadEndBlocksAnalysis(const DeadEndBlocksAnalysis &) = delete;
2727
DeadEndBlocksAnalysis &operator=(const DeadEndBlocksAnalysis &) = delete;
2828

29+
static SILAnalysisKind getAnalysisKind() {
30+
return SILAnalysisKind::DeadEndBlocks;
31+
}
32+
2933
static bool classof(const SILAnalysis *s) {
3034
return s->getKind() == SILAnalysisKind::DeadEndBlocks;
3135
}

include/swift/SILOptimizer/OptimizerBridging.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ typedef struct {
3939
void * _Nullable bca;
4040
} BridgedCalleeAnalysis;
4141

42+
typedef struct {
43+
void * _Nullable dea;
44+
} BridgedDeadEndBlocksAnalysis;
45+
4246
typedef struct {
4347
void * _Nonnull opaquePtr;
4448
unsigned char kind;
@@ -82,6 +86,12 @@ SwiftInt BridgedFunctionArray_size(BridgedCalleeList callees);
8286
BridgedFunction BridgedFunctionArray_get(BridgedCalleeList callees,
8387
SwiftInt index);
8488

89+
BridgedDeadEndBlocksAnalysis
90+
PassContext_getDeadEndBlocksAnalysis(BridgedPassContext context);
91+
92+
SwiftInt DeadEndBlocksAnalysis_isDeadEnd(BridgedDeadEndBlocksAnalysis debAnalysis,
93+
BridgedBasicBlock);
94+
8595
BridgedSlab PassContext_getNextSlab(BridgedSlab slab);
8696
BridgedSlab PassContext_getPreviousSlab(BridgedSlab slab);
8797
BridgedSlab PassContext_allocSlab(BridgedPassContext passContext,

lib/SILOptimizer/Analysis/DeadEndBlocksAnalysis.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13+
#include "swift/SIL/SILBridgingUtils.h"
1314
#include "swift/SILOptimizer/Analysis/DeadEndBlocksAnalysis.h"
15+
#include "swift/SILOptimizer/OptimizerBridging.h"
1416
#include "swift/AST/Decl.h"
1517
#include "swift/SIL/SILFunction.h"
1618

@@ -55,3 +57,13 @@ void DeadEndBlocksAnalysis::verify(DeadEndBlocks *deBlocks) const {
5557
SILAnalysis *swift::createDeadEndBlocksAnalysis(SILModule *) {
5658
return new DeadEndBlocksAnalysis();
5759
}
60+
61+
//===----------------------------------------------------------------------===//
62+
// Swift Bridging
63+
//===----------------------------------------------------------------------===//
64+
65+
SwiftInt DeadEndBlocksAnalysis_isDeadEnd(BridgedDeadEndBlocksAnalysis debAnalysis,
66+
BridgedBasicBlock block) {
67+
auto *dea = static_cast<DeadEndBlocks *>(debAnalysis.dea);
68+
return dea->isDeadEnd(castToBasicBlock(block));
69+
}

lib/SILOptimizer/PassManager/PassManager.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "swift/SIL/SILModule.h"
2222
#include "swift/SILOptimizer/Analysis/AliasAnalysis.h"
2323
#include "swift/SILOptimizer/Analysis/BasicCalleeAnalysis.h"
24+
#include "swift/SILOptimizer/Analysis/DeadEndBlocksAnalysis.h"
2425
#include "swift/SILOptimizer/Analysis/FunctionOrder.h"
2526
#include "swift/SILOptimizer/OptimizerBridging.h"
2627
#include "swift/SILOptimizer/PassManager/PrettyStackTrace.h"
@@ -1241,6 +1242,13 @@ BridgedCalleeAnalysis PassContext_getCalleeAnalysis(BridgedPassContext context)
12411242
return {pm->getAnalysis<BasicCalleeAnalysis>()};
12421243
}
12431244

1245+
BridgedDeadEndBlocksAnalysis
1246+
PassContext_getDeadEndBlocksAnalysis(BridgedPassContext context) {
1247+
SwiftPassInvocation *invocation = castToPassInvocation(context);
1248+
SILPassManager *pm = invocation->getPassManager();
1249+
return {pm->getAnalysis<DeadEndBlocksAnalysis>(invocation->getFunction())};
1250+
}
1251+
12441252
BridgedBasicBlockSet PassContext_allocBasicBlockSet(BridgedPassContext context) {
12451253
return {castToPassInvocation(context)->allocBlockSet()};
12461254
}

0 commit comments

Comments
 (0)