Skip to content

Commit 3d555a4

Browse files
committed
refactor two swift passes
NFC
1 parent 2407256 commit 3d555a4

File tree

2 files changed

+61
-63
lines changed

2 files changed

+61
-63
lines changed

SwiftCompilerSources/Sources/Optimizer/FunctionPasses/InitializeStaticGlobals.swift

Lines changed: 57 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ let initializeStaticGlobalsPass = FunctionPass(name: "initialize-static-globals"
4848
return
4949
}
5050

51-
guard let (allocInst, storeToGlobal) = function.getGlobalInitialization() else {
51+
guard let (allocInst, storeToGlobal) = getGlobalInitialization(of: function) else {
5252
return
5353
}
5454

@@ -62,69 +62,69 @@ let initializeStaticGlobalsPass = FunctionPass(name: "initialize-static-globals"
6262
context.erase(instruction: storeToGlobal)
6363
}
6464

65-
private extension Function {
66-
/// Analyses the global initializer function and returns the `alloc_global` and `store`
67-
/// instructions which initialize the global.
68-
///
69-
/// The function's single basic block must contain following code pattern:
70-
/// ```
71-
/// alloc_global @the_global
72-
/// %a = global_addr @the_global
73-
/// %i = some_const_initializer_insts
74-
/// store %i to %a
75-
/// ```
76-
func getGlobalInitialization() -> (allocInst: AllocGlobalInst, storeToGlobal: StoreInst)? {
65+
/// Analyses the global initializer function and returns the `alloc_global` and `store`
66+
/// instructions which initialize the global.
67+
///
68+
/// The function's single basic block must contain following code pattern:
69+
/// ```
70+
/// alloc_global @the_global
71+
/// %a = global_addr @the_global
72+
/// %i = some_const_initializer_insts
73+
/// store %i to %a
74+
/// ```
75+
private func getGlobalInitialization(of function: Function) -> (allocInst: AllocGlobalInst, storeToGlobal: StoreInst)? {
7776

78-
guard let block = singleBlock else {
79-
return nil
80-
}
77+
guard let block = function.singleBlock else {
78+
return nil
79+
}
8180

82-
var allocInst: AllocGlobalInst? = nil
83-
var globalAddr: GlobalAddrInst? = nil
84-
var store: StoreInst? = nil
81+
var allocInst: AllocGlobalInst? = nil
82+
var globalAddr: GlobalAddrInst? = nil
83+
var store: StoreInst? = nil
8584

86-
for inst in block.instructions {
87-
switch inst {
88-
case is ReturnInst,
89-
is DebugValueInst,
90-
is DebugStepInst:
91-
break
92-
case let agi as AllocGlobalInst:
93-
if allocInst != nil {
94-
return nil
95-
}
96-
allocInst = agi
97-
case let ga as GlobalAddrInst:
98-
if globalAddr != nil {
99-
return nil
100-
}
101-
guard let agi = allocInst, agi.global == ga.global else {
102-
return nil
103-
}
104-
globalAddr = ga
105-
case let si as StoreInst:
106-
if store != nil {
107-
return nil
108-
}
109-
guard let ga = globalAddr else {
110-
return nil
111-
}
112-
if si.destination != ga {
113-
return nil
114-
}
115-
store = si
116-
default:
117-
if !inst.isValidInStaticInitializerOfGlobal {
118-
return nil
119-
}
85+
for inst in block.instructions {
86+
switch inst {
87+
case is ReturnInst,
88+
is DebugValueInst,
89+
is DebugStepInst:
90+
break
91+
case let agi as AllocGlobalInst:
92+
if allocInst != nil {
93+
return nil
94+
}
95+
allocInst = agi
96+
case let ga as GlobalAddrInst:
97+
if globalAddr != nil {
98+
return nil
99+
}
100+
guard let agi = allocInst, agi.global == ga.global else {
101+
return nil
102+
}
103+
globalAddr = ga
104+
case let si as StoreInst:
105+
if store != nil {
106+
return nil
107+
}
108+
guard let ga = globalAddr else {
109+
return nil
110+
}
111+
if si.destination != ga {
112+
return nil
113+
}
114+
store = si
115+
default:
116+
if !inst.isValidInStaticInitializerOfGlobal {
117+
return nil
120118
}
121119
}
122-
if let store = store {
123-
return (allocInst: allocInst!, storeToGlobal: store)
124-
}
125-
return nil
126120
}
121+
if let store = store {
122+
return (allocInst: allocInst!, storeToGlobal: store)
123+
}
124+
return nil
125+
}
127126

127+
private extension Function {
128128
var singleBlock: BasicBlock? {
129129
let block = entryBlock
130130
if block.next != nil {

SwiftCompilerSources/Sources/Optimizer/ModulePasses/ReadOnlyGlobalVariables.swift

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ let readOnlyGlobalVariablesPass = ModulePass(name: "read-only-global-variables")
2727
for f in moduleContext.functions {
2828
for inst in f.instructions {
2929
if let gAddr = inst as? GlobalAddrInst {
30-
if gAddr.addressHasWrites {
30+
if findWrites(toAddress: gAddr) {
3131
writtenGlobals.insert(gAddr.global)
3232
}
3333
}
@@ -43,11 +43,9 @@ let readOnlyGlobalVariablesPass = ModulePass(name: "read-only-global-variables")
4343
}
4444
}
4545

46-
private extension Value {
47-
var addressHasWrites: Bool {
48-
var walker = FindWrites()
49-
return walker.walkDownUses(ofAddress: self, path: UnusedWalkingPath()) == .abortWalk
50-
}
46+
private func findWrites(toAddress: Value) -> Bool {
47+
var walker = FindWrites()
48+
return walker.walkDownUses(ofAddress: toAddress, path: UnusedWalkingPath()) == .abortWalk
5149
}
5250

5351
private struct FindWrites : AddressDefUseWalker {

0 commit comments

Comments
 (0)