Skip to content

Commit a8c9aae

Browse files
committed
Swift Optimizer: add simplification for cond_fail
1 parent 8a8a895 commit a8c9aae

File tree

3 files changed

+69
-0
lines changed

3 files changed

+69
-0
lines changed

SwiftCompilerSources/Sources/Optimizer/InstructionSimplification/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ swift_compiler_sources(Optimizer
1212
SimplifyBranch.swift
1313
SimplifyBuiltin.swift
1414
SimplifyCondBranch.swift
15+
SimplifyCondFail.swift
1516
SimplifyGlobalValue.swift
1617
SimplifyStrongRetainRelease.swift
1718
SimplifyStructExtract.swift
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//===--- SimplifyCondFail.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 CondFailInst : OnoneSimplifyable {
16+
func simplify(_ context: SimplifyContext) {
17+
18+
/// Eliminates
19+
/// ```
20+
/// %0 = integer_literal 0
21+
/// cond_fail %0, "message"
22+
/// ```
23+
if let literal = condition as? IntegerLiteralInst,
24+
literal.value.isZero() {
25+
26+
context.erase(instruction: self)
27+
}
28+
}
29+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// RUN: %target-sil-opt -enable-sil-verify-all %s -onone-simplification -simplify-instruction=cond_fail | %FileCheck %s
2+
3+
// REQUIRES: swift_in_compiler
4+
5+
import Swift
6+
import Builtin
7+
8+
// CHECK-LABEL: sil @not_failing
9+
// CHECK-NOT: cond_fail
10+
// CHECK: } // end sil function 'not_failing'
11+
sil @not_failing : $@convention(thin) () -> () {
12+
bb0:
13+
%0 = integer_literal $Builtin.Int1, 0
14+
cond_fail %0 : $Builtin.Int1, "not failing"
15+
%r = tuple ()
16+
return %r : $()
17+
}
18+
19+
// CHECK-LABEL: sil @failing
20+
// CHECK: cond_fail
21+
// CHECK: } // end sil function 'failing'
22+
sil @failing : $@convention(thin) () -> () {
23+
bb0:
24+
%0 = integer_literal $Builtin.Int1, -1
25+
cond_fail %0 : $Builtin.Int1, "failing"
26+
%r = tuple ()
27+
return %r : $()
28+
}
29+
30+
// CHECK-LABEL: sil @unknown
31+
// CHECK: cond_fail
32+
// CHECK: } // end sil function 'unknown'
33+
sil @unknown : $@convention(thin) (Builtin.Int1) -> () {
34+
bb0(%0 : $Builtin.Int1):
35+
cond_fail %0 : $Builtin.Int1, "unknown"
36+
%r = tuple ()
37+
return %r : $()
38+
}
39+

0 commit comments

Comments
 (0)