Skip to content

Add new cast pattern in SILCombine #31595

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions lib/SILOptimizer/SILCombiner/SILCombinerCastVisitors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,10 @@ SILCombiner::visitThickToObjCMetatypeInst(ThickToObjCMetatypeInst *TTOCMI) {
if (CastOpt.optimizeMetatypeConversion(TTOCMI, MetatypeRepresentation::Thick))
MadeChange = true;

if (auto *OCTTMI = dyn_cast<ObjCToThickMetatypeInst>(TTOCMI->getOperand())) {
TTOCMI->replaceAllUsesWith(OCTTMI->getOperand());
return eraseInstFromFunction(*TTOCMI);
}
return nullptr;
}

Expand All @@ -472,6 +476,10 @@ SILCombiner::visitObjCToThickMetatypeInst(ObjCToThickMetatypeInst *OCTTMI) {
if (CastOpt.optimizeMetatypeConversion(OCTTMI, MetatypeRepresentation::ObjC))
MadeChange = true;

if (auto *TTOCMI = dyn_cast<ThickToObjCMetatypeInst>(OCTTMI->getOperand())) {
OCTTMI->replaceAllUsesWith(TTOCMI->getOperand());
return eraseInstFromFunction(*OCTTMI);
}
return nullptr;
}

Expand Down
47 changes: 47 additions & 0 deletions test/SILOptimizer/peephole_thick_to_objc_metatype.sil
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import Builtin
import Swift
import SwiftShims

import Foundation

@objc(XX) protocol X {
}

Expand Down Expand Up @@ -115,3 +117,48 @@ bb0(%0 : $T):
strong_release %0 : $T
return %3 : $@thick T.Type
}

// CHECK-LABEL: sil @$test_peephole_objc_to_thick_to_objc :
// CHECK: [[T:%.*]] = apply
// CHECK-NOT: objc_to_thick_metatype
// CHECK-NOT: thick_to_objc_metatype
// CHECK: enum $Optional<@objc_metatype AnyObject.Type>, #Optional.some!enumelt, [[T]] : $@objc_metatype AnyObject.Type
// CHECK: } // end sil function '$test_peephole_objc_to_thick_to_objc'

sil @$test_peephole_objc_to_thick_to_objc : $@convention(thin) (@guaranteed NSObject) -> Optional<UnsafeMutablePointer<OpaquePointer>> {
// %0 "obj" // users: %3, %2, %1
bb0(%0 : $NSObject):
debug_value %0 : $NSObject, let, name "obj", argno 1 // id: %1
%2 = objc_method %0 : $NSObject, #NSObject.classForCoder!getter.foreign : (NSObject) -> () -> AnyObject.Type, $@convention(objc_method) (NSObject) -> @objc_metatype AnyObject.Type // user: %3
%3 = apply %2(%0) : $@convention(objc_method) (NSObject) -> @objc_metatype AnyObject.Type // user: %4
%4 = objc_to_thick_metatype %3 : $@objc_metatype AnyObject.Type to $@thick AnyObject.Type // users: %6, %5
debug_value %4 : $@thick AnyObject.Type, let, name "c" // id: %5
%6 = thick_to_objc_metatype %4 : $@thick AnyObject.Type to $@objc_metatype AnyObject.Type // user: %7
%7 = enum $Optional<@objc_metatype AnyObject.Type>, #Optional.some!enumelt, %6 : $@objc_metatype AnyObject.Type // user: %10
%8 = enum $Optional<UnsafeMutablePointer<UInt32>>, #Optional.none!enumelt // user: %10
// function_ref class_copyMethodList
%9 = function_ref @class_copyMethodList : $@convention(c) (Optional<@objc_metatype AnyObject.Type>, Optional<UnsafeMutablePointer<UInt32>>) -> Optional<UnsafeMutablePointer<OpaquePointer>> // user: %10
%10 = apply %9(%7, %8) : $@convention(c) (Optional<@objc_metatype AnyObject.Type>, Optional<UnsafeMutablePointer<UInt32>>) -> Optional<UnsafeMutablePointer<OpaquePointer>> // users: %12, %11
debug_value %10 : $Optional<UnsafeMutablePointer<OpaquePointer>>, let, name "l" // id: %11
return %10 : $Optional<UnsafeMutablePointer<OpaquePointer>> // id: %12
}

// CHECK-LABEL: sil @$test_peephole_thick_to_objc_to_thick :
// CHECK: [[T:%.*]] = apply
// CHECK-NOT: thick_to_objc_metatype
// CHECK-NOT: objc_to_thick_metatype
// CHECK: return [[T]]
// CHECK: } // end sil function '$test_peephole_thick_to_objc_to_thick'

sil @$test_peephole_thick_to_objc_to_thick : $@convention(thin) (@guaranteed AnyObject) -> @thick AnyObject.Type {
bb0(%0 : $AnyObject):
%func = function_ref @foo : $@convention(thin) (@guaranteed AnyObject) -> @thick AnyObject.Type
%res = apply %func(%0) : $@convention(thin) (@guaranteed AnyObject) -> @thick AnyObject.Type
%objctype = thick_to_objc_metatype %res : $@thick AnyObject.Type to $@objc_metatype AnyObject.Type
%thicktype = objc_to_thick_metatype %objctype : $@objc_metatype AnyObject.Type to $@thick AnyObject.Type
return %thicktype : $@thick AnyObject.Type
}

// class_copyMethodList
sil [serializable] [clang class_copyMethodList] @class_copyMethodList : $@convention(c) (Optional<@objc_metatype AnyObject.Type>, Optional<UnsafeMutablePointer<UInt32>>) -> Optional<UnsafeMutablePointer<OpaquePointer>>
sil [serializable] @foo : $@convention(thin) (@guaranteed AnyObject) -> @thick AnyObject.Type