1010//
1111//===----------------------------------------------------------------------===//
1212
13- import OptimizerBridging
1413import SILBridging
1514
1615/// Clones the initializer value of a GlobalVariable.
@@ -19,7 +18,7 @@ import SILBridging
1918/// from or to the static initializer value of a GlobalVariable.
2019///
2120public struct Cloner < Context: MutatingContext > {
22- public var bridged : BridgedCloner
21+ private var bridged : BridgedCloner
2322 public let context : Context
2423
2524 public enum GetClonedResult {
@@ -45,7 +44,7 @@ public struct Cloner<Context: MutatingContext> {
4544 self . context = context
4645 self . target = . function( inst. parentFunction)
4746 }
48-
47+
4948 public init ( cloneToEmptyFunction: Function , _ context: Context ) {
5049 self . bridged = BridgedCloner ( cloneToEmptyFunction. bridged, context. _bridged)
5150 self . context = context
@@ -63,6 +62,24 @@ public struct Cloner<Context: MutatingContext> {
6362 return function
6463 }
6564
65+ public func getOrCreateEntryBlock( ) -> BasicBlock {
66+ if let entryBlock = targetFunction. blocks. first {
67+ return entryBlock
68+ }
69+ return targetFunction. appendNewBlock ( context)
70+ }
71+
72+ public func cloneFunctionBody( from originalFunction: Function , entryBlockArguments: [ Value ] ) {
73+ entryBlockArguments. withBridgedValues { bridgedEntryBlockArgs in
74+ let entryBlock = getOrCreateEntryBlock ( )
75+ bridged. cloneFunctionBody ( originalFunction. bridged, entryBlock. bridged, bridgedEntryBlockArgs)
76+ }
77+ }
78+
79+ public func cloneFunctionBody( from originalFunction: Function ) {
80+ bridged. cloneFunctionBody ( originalFunction. bridged)
81+ }
82+
6683 public mutating func clone( instruction: Instruction ) -> Instruction {
6784 let cloned = bridged. clone ( instruction. bridged) . instruction
6885 if case . function = target {
@@ -71,7 +88,7 @@ public struct Cloner<Context: MutatingContext> {
7188 }
7289 return cloned
7390 }
74-
91+
7592 public mutating func cloneRecursivelyToGlobal( value: Value ) -> Value {
7693 guard let cloned = cloneRecursively ( value: value, customGetCloned: { value, cloner in
7794 guard let beginAccess = value as? BeginAccessInst else {
@@ -89,6 +106,11 @@ public struct Cloner<Context: MutatingContext> {
89106 return cloned
90107 }
91108
109+ /// Transitively clones `value` including its defining instruction's operands.
110+ public mutating func cloneRecursively( value: Value ) -> Value {
111+ return cloneRecursively ( value: value, customGetCloned: { _, _ in . defaultValue } ) !
112+ }
113+
92114 /// Transitively clones `value` including its defining instruction's operands.
93115 public mutating func cloneRecursively( value: Value , customGetCloned: ( Value , inout Cloner ) -> GetClonedResult ) -> Value ? {
94116 if isCloned ( value: value) {
@@ -134,7 +156,7 @@ public struct Cloner<Context: MutatingContext> {
134156 public func getClonedBlock( for originalBlock: BasicBlock ) -> BasicBlock {
135157 bridged. getClonedBasicBlock ( originalBlock. bridged) . block
136158 }
137-
159+
138160 public func recordFoldedValue( _ origValue: Value , mappedTo mappedValue: Value ) {
139161 bridged. recordFoldedValue ( origValue. bridged, mappedValue. bridged)
140162 }
0 commit comments