Skip to content

Commit 6493872

Browse files
committed
BooleanLiteralFolding: remove dead blocks
Remove all dead blocks which are a result of constant folding condition branches
1 parent c89f63f commit 6493872

File tree

3 files changed

+19
-7
lines changed

3 files changed

+19
-7
lines changed

SwiftCompilerSources/Sources/Optimizer/FunctionPasses/BooleanLiteralFolding.swift

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,25 +46,30 @@ import SIL
4646
let booleanLiteralFolding = FunctionPass(name: "boolean-literal-folding") {
4747
(function: Function, context: FunctionPassContext) in
4848

49+
var changed = false
4950
for block in function.blocks {
5051
if let condBr = block.terminator as? CondBranchInst {
51-
fold(condBranch: condBr, context)
52+
changed = fold(condBranch: condBr, context) || changed
5253
}
5354
}
55+
if changed {
56+
_ = context.removeDeadBlocks(in: function)
57+
}
5458
}
5559

56-
private func fold(condBranch: CondBranchInst, _ context: FunctionPassContext) {
60+
private func fold(condBranch: CondBranchInst, _ context: FunctionPassContext) -> Bool {
5761
guard let structExtract = condBranch.condition as? StructExtractInst,
5862
let initApply = structExtract.struct as? ApplyInst,
5963
initApply.hasSemanticsAttribute("bool.literal_init"),
6064
initApply.arguments.count == 2,
6165
let literal = initApply.arguments[0] as? IntegerLiteralInst,
6266
let literalValue = literal.value else
6367
{
64-
return
68+
return false
6569
}
6670

6771
let builder = Builder(before: condBranch, context)
6872
builder.createBranch(to: literalValue == 0 ? condBranch.falseBlock : condBranch.trueBlock)
6973
context.erase(instruction: condBranch)
74+
return true
7075
}

test/SILOptimizer/boolean-literal-folding.sil

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ sil public_external [_semantics "bool.literal_init"] @wrong_bool_literal_init :
1515
// CHECK: struct_extract
1616
// CHECK-NEXT: br bb1
1717
// CHECK: bb1:
18+
// CHECK-NEXT: debug_step
19+
// CHECK-NEXT: br bb2
20+
// CHECK-NOT: bb3
1821
// CHECK: } // end sil function 'replace_true'
1922
sil [ossa] @replace_true : $@convention(thin) () -> () {
2023
bb0:
@@ -25,6 +28,7 @@ bb0:
2528
%4 = struct_extract %3 : $Bool, #Bool._value
2629
cond_br %4, bb1, bb2
2730
bb1:
31+
debug_step
2832
br bb3
2933
bb2:
3034
br bb3
@@ -35,8 +39,10 @@ bb3:
3539

3640
// CHECK-LABEL: sil [ossa] @replace_false :
3741
// CHECK: struct_extract
38-
// CHECK-NEXT: br bb2
42+
// CHECK-NEXT: br bb1
3943
// CHECK: bb1:
44+
// CHECK-NEXT: br bb2
45+
// CHECK-NOT: bb3
4046
// CHECK: } // end sil function 'replace_false'
4147
sil [ossa] @replace_false : $@convention(thin) () -> () {
4248
bb0:
@@ -47,6 +53,7 @@ bb0:
4753
%4 = struct_extract %3 : $Bool, #Bool._value
4854
cond_br %4, bb1, bb2
4955
bb1:
56+
debug_step
5057
br bb3
5158
bb2:
5259
br bb3

test/SILOptimizer/discard_checking.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,15 @@ struct Basics: ~Copyable {
3333
repeat {
3434
switch c {
3535
case .red:
36-
fatalError("bah!") // expected-error {{must consume 'self' before exiting method that discards self}}
36+
fatalError("bah!")
3737
case .blue:
38-
throw E.someError // expected-error {{must consume 'self' before exiting method that discards self}}
38+
throw E.someError
3939
case .green:
4040
self = Basics()
4141
default: print("hi")
4242
}
4343
} while true
44-
discard self // expected-note 2{{discarded self here}}
44+
discard self
4545
}
4646

4747
consuming func test2_fixed(_ c: Color) throws {

0 commit comments

Comments
 (0)