Skip to content

Commit 1355e94

Browse files
committed
Simplification: simplify builtin "canBeClass" and builtin "assert_configuration"
1 parent b916906 commit 1355e94

File tree

2 files changed

+103
-3
lines changed

2 files changed

+103
-3
lines changed

SwiftCompilerSources/Sources/Optimizer/InstructionSimplification/SimplifyBuiltin.swift

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ extension BuiltinInst : OnoneSimplifyable {
2424
optimizeIsSameMetatype(context)
2525
case .Once:
2626
optimizeBuiltinOnce(context)
27+
case .CanBeObjCClass:
28+
optimizeCanBeClass(context)
29+
case .AssertConf:
30+
optimizeAssertConfig(context)
2731
default:
2832
if let literal = constantFold(context) {
2933
uses.replaceAll(with: literal, context)
@@ -90,6 +94,43 @@ private extension BuiltinInst {
9094
}
9195
return nil
9296
}
97+
98+
func optimizeCanBeClass(_ context: SimplifyContext) {
99+
guard let ty = substitutionMap.replacementTypes[0] else {
100+
return
101+
}
102+
let literal: IntegerLiteralInst
103+
switch ty.canBeClass {
104+
case .IsNot:
105+
let builder = Builder(before: self, context)
106+
literal = builder.createIntegerLiteral(0, type: type)
107+
case .Is:
108+
let builder = Builder(before: self, context)
109+
literal = builder.createIntegerLiteral(1, type: type)
110+
case .CanBe:
111+
return
112+
default:
113+
fatalError()
114+
}
115+
uses.replaceAll(with: literal, context)
116+
context.erase(instruction: self)
117+
}
118+
119+
func optimizeAssertConfig(_ context: SimplifyContext) {
120+
let literal: IntegerLiteralInst
121+
switch context.options.assertConfiguration {
122+
case .enabled:
123+
let builder = Builder(before: self, context)
124+
literal = builder.createIntegerLiteral(1, type: type)
125+
case .disabled:
126+
let builder = Builder(before: self, context)
127+
literal = builder.createIntegerLiteral(0, type: type)
128+
default:
129+
return
130+
}
131+
uses.replaceAll(with: literal, context)
132+
context.erase(instruction: self)
133+
}
93134
}
94135

95136
private func typesOfValuesAreEqual(_ lhs: Value, _ rhs: Value, in function: Function) -> Bool? {

test/SILOptimizer/simplify_builtin.sil

Lines changed: 62 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
// RUN: %target-sil-opt -enable-sil-verify-all %s -onone-simplification -simplify-instruction=builtin | %FileCheck %s --check-prefix=CHECK --check-prefix=EARLY
2-
// RUN: %target-sil-opt -enable-sil-verify-all %s -late-onone-simplification -simplify-instruction=builtin | %FileCheck %s --check-prefix=CHECK --check-prefix=LATE
1+
// RUN: %target-sil-opt -enable-sil-verify-all %s -onone-simplification -simplify-instruction=builtin | %FileCheck %s --check-prefix=CHECK --check-prefix=CHECK-EARLY
2+
// RUN: %target-sil-opt -enable-sil-verify-all %s -late-onone-simplification -simplify-instruction=builtin | %FileCheck %s --check-prefix=CHECK --check-prefix=CHECK-LATE
3+
// RUN: %target-sil-opt -enable-sil-verify-all %s -assert-conf-id=1 -onone-simplification -simplify-instruction=builtin | %FileCheck %s --check-prefix=CHECK --check-prefix=CHECK-NOASSERTS
4+
// RUN: %target-sil-opt -enable-sil-verify-all %s -assert-conf-id=2 -onone-simplification -simplify-instruction=builtin | %FileCheck %s --check-prefix=CHECK --check-prefix=CHECK-NOASSERTS
35

46
// REQUIRES: swift_in_compiler
57

@@ -55,7 +57,7 @@ bb0(%0 : $@thin Int.Type):
5557
// CHECK-LABEL: sil @isConcrete_false
5658
// CHECK: bb0(%0 : $@thin T.Type):
5759
// CHECK-EARLY: [[R:%.*]] = builtin "isConcrete"<T>(%0 : $@thin T.Type) : $Builtin.Int1
58-
// CHECK-LATE: [[R:%.*]] = integer_literal $Builtin.Int1, -1
60+
// CHECK-LATE: [[R:%.*]] = integer_literal $Builtin.Int1, 0
5961
// CHECK: return [[R]]
6062
// CHECK: } // end sil function 'isConcrete_false'
6163
sil @isConcrete_false : $@convention(thin) <T> (@thin T.Type) -> Builtin.Int1 {
@@ -322,3 +324,60 @@ bb0(%0 : $Builtin.RawPointer):
322324
return %3 : $()
323325
}
324326

327+
// CHECK-LABEL: sil @generic_canBeClass
328+
// CHECK: [[TYPE:%.*]] = metatype $@thick T.Type
329+
// CHECK: [[B:%.*]] = builtin "canBeClass"<T>([[TYPE]] : $@thick T.Type)
330+
// CHECK: [[R:%.*]] = struct $Int8 ([[B]] : $Builtin.Int8)
331+
// CHECK: return [[R]]
332+
// CHECK: } // end sil function 'generic_canBeClass'
333+
sil @generic_canBeClass : $@convention(thin) <T> (@in T) -> Int8 {
334+
bb0(%0 : $*T):
335+
%1 = metatype $@thick T.Type
336+
%3 = builtin "canBeClass"<T>(%1 : $@thick T.Type) : $Builtin.Int8
337+
%4 = struct $Int8 (%3 : $Builtin.Int8)
338+
destroy_addr %0 : $*T
339+
return %4 : $Int8
340+
}
341+
342+
// CHECK-LABEL: sil @int_canBeClass
343+
// CHECK-NOT: builtin "canBeClass"
344+
// CHECK: [[L:%.*]] = integer_literal $Builtin.Int8, 0
345+
// CHECK: [[R:%.*]] = struct $Int8 ([[L]] : $Builtin.Int8)
346+
// CHECK: return [[R]]
347+
// CHECK: } // end sil function 'int_canBeClass'
348+
sil @int_canBeClass : $@convention(thin) () -> Int8 {
349+
bb0:
350+
%1 = metatype $@thick Int.Type
351+
%3 = builtin "canBeClass"<Int>(%1 : $@thick Int.Type) : $Builtin.Int8
352+
%4 = struct $Int8 (%3 : $Builtin.Int8)
353+
return %4 : $Int8
354+
}
355+
356+
// CHECK-LABEL: sil @class_canBeClass
357+
// CHECK-NOT: builtin "canBeClass"
358+
// CHECK: [[L:%.*]] = integer_literal $Builtin.Int8, 1
359+
// CHECK: [[R:%.*]] = struct $Int8 ([[L]] : $Builtin.Int8)
360+
// CHECK: return [[R]]
361+
// CHECK: } // end sil function 'class_canBeClass'
362+
sil @class_canBeClass : $@convention(thin) () -> Int8 {
363+
bb0:
364+
%1 = metatype $@thick C1<Int>.Type
365+
%3 = builtin "canBeClass"<C1<Int>>(%1 : $@thick C1<Int>.Type) : $Builtin.Int8
366+
%4 = struct $Int8 (%3 : $Builtin.Int8)
367+
return %4 : $Int8
368+
}
369+
370+
// CHECK-LABEL: sil @remove_assert_configuration
371+
// CHECK-NOT: builtin "assert_configuration"
372+
// CHECK-EARLY: [[L:%.*]] = integer_literal $Builtin.Int8, 1
373+
// CHECK-NOASSERTS: [[L:%.*]] = integer_literal $Builtin.Int8, 0
374+
// CHECK: [[R:%.*]] = struct $Int8 ([[L]] : $Builtin.Int8)
375+
// CHECK: return [[R]]
376+
// CHECK: } // end sil function 'remove_assert_configuration'
377+
sil @remove_assert_configuration : $@convention(thin) () -> Int8 {
378+
bb0:
379+
%3 = builtin "assert_configuration"() : $Builtin.Int8
380+
%4 = struct $Int8 (%3 : $Builtin.Int8)
381+
return %4 : $Int8
382+
}
383+

0 commit comments

Comments
 (0)