Skip to content

Commit 1c70060

Browse files
committed
Swift Optimizer: add Onone simplification of struct_extract instructions
1 parent 20edeb6 commit 1c70060

File tree

3 files changed

+68
-0
lines changed

3 files changed

+68
-0
lines changed

SwiftCompilerSources/Sources/Optimizer/InstructionSimplification/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@ swift_compiler_sources(Optimizer
1414
SimplifyCondBranch.swift
1515
SimplifyGlobalValue.swift
1616
SimplifyStrongRetainRelease.swift
17+
SimplifyStructExtract.swift
1718
SimplifyUncheckedEnumData.swift)
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//===--- SimplifyStructExtract.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 StructExtractInst : OnoneSimplifyable {
16+
func simplify(_ context: SimplifyContext) {
17+
guard let structInst = operand as? StructInst else {
18+
return
19+
}
20+
context.tryReplaceRedundantInstructionPair(first: structInst, second: self,
21+
with: structInst.operands[fieldIndex].value)
22+
}
23+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// RUN: %target-sil-opt -enable-sil-verify-all %s -onone-simplification -simplify-instruction=struct_extract | %FileCheck %s
2+
3+
// REQUIRES: swift_in_compiler
4+
5+
import Swift
6+
import Builtin
7+
8+
struct S {
9+
var a: String
10+
var b: String
11+
}
12+
13+
// CHECK-LABEL: sil @struct_extract_first_field
14+
// CHECK: return %0
15+
// CHECK: } // end sil function 'struct_extract_first_field'
16+
sil @struct_extract_first_field : $@convention(thin) (@owned String, @guaranteed String) -> @owned String {
17+
bb0(%0 : $String, %1 : $String):
18+
%2 = struct $S (%0 : $String, %1 : $String)
19+
%3 = struct_extract %2 : $S, #S.a
20+
return %3 : $String
21+
}
22+
23+
// CHECK-LABEL: sil @struct_extract_second_field
24+
// CHECK: return %1
25+
// CHECK: } // end sil function 'struct_extract_second_field'
26+
sil @struct_extract_second_field : $@convention(thin) (@guaranteed String, @owned String) -> @owned String {
27+
bb0(%0 : $String, %1 : $String):
28+
%2 = struct $S (%0 : $String, %1 : $String)
29+
%3 = struct_extract %2 : $S, #S.b
30+
return %3 : $String
31+
}
32+
33+
// CHECK-LABEL: sil [ossa] @struct_extract_ossa
34+
// CHECK: %2 = copy_value %0
35+
// CHECK: return %2
36+
// CHECK: } // end sil function 'struct_extract_ossa'
37+
sil [ossa] @struct_extract_ossa : $@convention(thin) (@guaranteed String, @guaranteed String) -> @owned String {
38+
bb0(%0 : @guaranteed $String, %1 : @guaranteed $String):
39+
%2 = struct $S (%0 : $String, %1 : $String)
40+
%3 = struct_extract %2 : $S, #S.a
41+
%4 = copy_value %3 : $String
42+
return %4 : $String
43+
}
44+

0 commit comments

Comments
 (0)