| 
 | 1 | +//===--- SimplifyClassifyBridgeObject.swift -------------------------------===//  | 
 | 2 | +//  | 
 | 3 | +// This source file is part of the Swift.org open source project  | 
 | 4 | +//  | 
 | 5 | +// Copyright (c) 2014 - 2024 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 AST  | 
 | 14 | +import SIL  | 
 | 15 | + | 
 | 16 | +extension ClassifyBridgeObjectInst : OnoneSimplifyable, SILCombineSimplifyable {  | 
 | 17 | +  func simplify(_ context: SimplifyContext) {  | 
 | 18 | +    // Constant fold `classify_bridge_object` to `(false, false)` if the operand is known  | 
 | 19 | +    // to be a swift class.  | 
 | 20 | +    var walker = CheckForSwiftClasses();  | 
 | 21 | +    if walker.walkUp(value: operand.value, path: UnusedWalkingPath()) == .abortWalk {  | 
 | 22 | +      return  | 
 | 23 | +    }  | 
 | 24 | + | 
 | 25 | +    let builder = Builder(before: self, context)  | 
 | 26 | +    let falseLiteral = builder.createIntegerLiteral(0, type: context.getBuiltinIntegerType(bitWidth: 1))  | 
 | 27 | +    let tp = builder.createTuple(type: self.type, elements: [falseLiteral, falseLiteral])  | 
 | 28 | +    uses.replaceAll(with: tp, context)  | 
 | 29 | +    context.erase(instruction: self)  | 
 | 30 | +  }  | 
 | 31 | +}  | 
 | 32 | + | 
 | 33 | +private struct CheckForSwiftClasses: ValueUseDefWalker {  | 
 | 34 | +  mutating func walkUp(value: Value, path: UnusedWalkingPath) -> WalkResult {  | 
 | 35 | +    if let nominal = value.type.nominal,  | 
 | 36 | +       let classDecl = nominal as? ClassDecl,  | 
 | 37 | +       !classDecl.isObjC  | 
 | 38 | +    {  | 
 | 39 | +      // Stop this use-def walk if the value is known to be a swift class.  | 
 | 40 | +      return .continueWalk  | 
 | 41 | +    }  | 
 | 42 | +    return walkUpDefault(value: value, path: path)  | 
 | 43 | +  }  | 
 | 44 | + | 
 | 45 | +  mutating func rootDef(value: Value, path: UnusedWalkingPath) -> WalkResult {  | 
 | 46 | +    return .abortWalk  | 
 | 47 | +  }  | 
 | 48 | + | 
 | 49 | +  var walkUpCache = WalkerCache<UnusedWalkingPath>()  | 
 | 50 | +}  | 
0 commit comments