Skip to content

Commit 4cf6269

Browse files
committed
Swift Optimizer: add Onone simplification of cond_br instructions
1 parent d56ed65 commit 4cf6269

File tree

3 files changed

+99
-0
lines changed

3 files changed

+99
-0
lines changed

SwiftCompilerSources/Sources/Optimizer/InstructionSimplification/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@ swift_compiler_sources(Optimizer
1111
SimplifyBeginCOWMutation.swift
1212
SimplifyBranch.swift
1313
SimplifyBuiltin.swift
14+
SimplifyCondBranch.swift
1415
SimplifyGlobalValue.swift
1516
SimplifyStrongRetainRelease.swift)
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//===--- SimplifyCondBranch.swift -----------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2023 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 SIL
14+
15+
extension CondBranchInst : OnoneSimplifyable {
16+
func simplify(_ context: SimplifyContext) {
17+
tryConstantFold(context)
18+
}
19+
}
20+
21+
private extension CondBranchInst {
22+
func tryConstantFold(_ context: SimplifyContext) {
23+
guard let literal = condition as? IntegerLiteralInst else {
24+
return
25+
}
26+
let builder = Builder(before: self, context)
27+
if literal.value.isZero() {
28+
builder.createBranch(to: falseBlock, arguments: Array(falseOperands.map { $0.value }))
29+
} else {
30+
builder.createBranch(to: trueBlock, arguments: Array(trueOperands.map { $0.value }))
31+
}
32+
context.erase(instruction: self)
33+
}
34+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// RUN: %target-sil-opt -enable-sil-verify-all %s -onone-simplification -simplify-instruction=cond_br | %FileCheck %s
2+
3+
4+
// REQUIRES: swift_in_compiler
5+
6+
import Swift
7+
import Builtin
8+
9+
// CHECK-LABEL: sil @constant_cond_br_true
10+
// CHECK: bb0(%0 : $Int, %1 : $Int):
11+
// CHECK-NEXT: br bb1(%0 : $Int)
12+
// CHECK: } // end sil function 'constant_cond_br_true'
13+
sil @constant_cond_br_true : $@convention(thin) (Int, Int) -> Int {
14+
bb0(%0 : $Int, %1 : $Int):
15+
%2 = integer_literal $Builtin.Int1, 1
16+
cond_br %2, bb1(%0 : $Int), bb2(%1 : $Int)
17+
18+
bb1(%4 : $Int):
19+
br bb3(%4 : $Int)
20+
21+
bb2(%6 : $Int):
22+
br bb3(%6 : $Int)
23+
24+
bb3(%8 : $Int):
25+
return %8 : $Int
26+
}
27+
28+
// CHECK-LABEL: sil @constant_cond_br_false
29+
// CHECK: bb0(%0 : $Int, %1 : $Int):
30+
// CHECK-NEXT: br bb1(%1 : $Int)
31+
// CHECK: } // end sil function 'constant_cond_br_false'
32+
sil @constant_cond_br_false : $@convention(thin) (Int, Int) -> Int {
33+
bb0(%0 : $Int, %1 : $Int):
34+
%2 = integer_literal $Builtin.Int1, 0
35+
cond_br %2, bb1(%0 : $Int), bb2(%1 : $Int)
36+
37+
bb1(%4 : $Int):
38+
br bb3(%4 : $Int)
39+
40+
bb2(%6 : $Int):
41+
br bb3(%6 : $Int)
42+
43+
bb3(%8 : $Int):
44+
return %8 : $Int
45+
}
46+
47+
// CHECK-LABEL: sil @non_constant_cond_br
48+
// CHECK: bb0(%0 : $Int, %1 : $Int, %2 : $Builtin.Int1):
49+
// CHECK-NEXT: cond_br %2
50+
// CHECK: } // end sil function 'non_constant_cond_br'
51+
sil @non_constant_cond_br : $@convention(thin) (Int, Int, Builtin.Int1) -> Int {
52+
bb0(%0 : $Int, %1 : $Int, %2 : $Builtin.Int1):
53+
cond_br %2, bb1(%0 : $Int), bb2(%1 : $Int)
54+
55+
bb1(%4 : $Int):
56+
br bb3(%4 : $Int)
57+
58+
bb2(%6 : $Int):
59+
br bb3(%6 : $Int)
60+
61+
bb3(%8 : $Int):
62+
return %8 : $Int
63+
}
64+

0 commit comments

Comments
 (0)