10
10
//
11
11
//===----------------------------------------------------------------------===//
12
12
13
- import OptimizerBridging
14
13
import SILBridging
15
14
16
15
/// Clones the initializer value of a GlobalVariable.
@@ -19,7 +18,7 @@ import SILBridging
19
18
/// from or to the static initializer value of a GlobalVariable.
20
19
///
21
20
public struct Cloner < Context: MutatingContext > {
22
- public var bridged : BridgedCloner
21
+ private var bridged : BridgedCloner
23
22
public let context : Context
24
23
25
24
public enum GetClonedResult {
@@ -45,7 +44,7 @@ public struct Cloner<Context: MutatingContext> {
45
44
self . context = context
46
45
self . target = . function( inst. parentFunction)
47
46
}
48
-
47
+
49
48
public init ( cloneToEmptyFunction: Function , _ context: Context ) {
50
49
self . bridged = BridgedCloner ( cloneToEmptyFunction. bridged, context. _bridged)
51
50
self . context = context
@@ -63,6 +62,24 @@ public struct Cloner<Context: MutatingContext> {
63
62
return function
64
63
}
65
64
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
+
66
83
public mutating func clone( instruction: Instruction ) -> Instruction {
67
84
let cloned = bridged. clone ( instruction. bridged) . instruction
68
85
if case . function = target {
@@ -71,7 +88,7 @@ public struct Cloner<Context: MutatingContext> {
71
88
}
72
89
return cloned
73
90
}
74
-
91
+
75
92
public mutating func cloneRecursivelyToGlobal( value: Value ) -> Value {
76
93
guard let cloned = cloneRecursively ( value: value, customGetCloned: { value, cloner in
77
94
guard let beginAccess = value as? BeginAccessInst else {
@@ -89,6 +106,11 @@ public struct Cloner<Context: MutatingContext> {
89
106
return cloned
90
107
}
91
108
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
+
92
114
/// Transitively clones `value` including its defining instruction's operands.
93
115
public mutating func cloneRecursively( value: Value , customGetCloned: ( Value , inout Cloner ) -> GetClonedResult ) -> Value ? {
94
116
if isCloned ( value: value) {
@@ -134,7 +156,7 @@ public struct Cloner<Context: MutatingContext> {
134
156
public func getClonedBlock( for originalBlock: BasicBlock ) -> BasicBlock {
135
157
bridged. getClonedBasicBlock ( originalBlock. bridged) . block
136
158
}
137
-
159
+
138
160
public func recordFoldedValue( _ origValue: Value , mappedTo mappedValue: Value ) {
139
161
bridged. recordFoldedValue ( origValue. bridged, mappedValue. bridged)
140
162
}
0 commit comments