Skip to content

Commit 47db3fc

Browse files
committed
Optimizer: move the getGlobalInitialization utility from OptUtils.swift to SimplifyLoad.swift
Because it's not used anywhere else anymore
1 parent 51fda63 commit 47db3fc

File tree

2 files changed

+72
-71
lines changed

2 files changed

+72
-71
lines changed

SwiftCompilerSources/Sources/Optimizer/InstructionSimplification/SimplifyLoad.swift

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,3 +376,75 @@ private extension Instruction {
376376
return shiftValue > 0
377377
}
378378
}
379+
380+
/// Analyses the global initializer function and returns the `alloc_global` and `store`
381+
/// instructions which initialize the global.
382+
/// Returns nil if `function` has any side-effects beside initializing the global.
383+
///
384+
/// The function's single basic block must contain following code pattern:
385+
/// ```
386+
/// alloc_global @the_global
387+
/// %a = global_addr @the_global
388+
/// %i = some_const_initializer_insts
389+
/// store %i to %a
390+
/// ```
391+
///
392+
/// For all other instructions `handleUnknownInstruction` is called and such an instruction
393+
/// is accepted if `handleUnknownInstruction` returns true.
394+
private func getGlobalInitialization(
395+
of function: Function,
396+
_ context: some Context,
397+
handleUnknownInstruction: (Instruction) -> Bool
398+
) -> (allocInst: AllocGlobalInst, storeToGlobal: StoreInst)? {
399+
guard let block = function.blocks.singleElement else {
400+
return nil
401+
}
402+
403+
var allocInst: AllocGlobalInst? = nil
404+
var globalAddr: GlobalAddrInst? = nil
405+
var store: StoreInst? = nil
406+
407+
for inst in block.instructions {
408+
switch inst {
409+
case is ReturnInst,
410+
is DebugValueInst,
411+
is DebugStepInst,
412+
is BeginAccessInst,
413+
is EndAccessInst:
414+
continue
415+
case let agi as AllocGlobalInst:
416+
if allocInst == nil {
417+
allocInst = agi
418+
continue
419+
}
420+
case let ga as GlobalAddrInst:
421+
if let agi = allocInst, agi.global == ga.global {
422+
globalAddr = ga
423+
}
424+
continue
425+
case let si as StoreInst:
426+
if store == nil,
427+
let ga = globalAddr,
428+
si.destination == ga
429+
{
430+
store = si
431+
continue
432+
}
433+
// Note that the initializer must not contain a `global_value` because `global_value` needs to
434+
// initialize the class metadata at runtime.
435+
default:
436+
if inst.isValidInStaticInitializerOfGlobal(context) {
437+
continue
438+
}
439+
}
440+
if handleUnknownInstruction(inst) {
441+
continue
442+
}
443+
return nil
444+
}
445+
if let store = store {
446+
return (allocInst: allocInst!, storeToGlobal: store)
447+
}
448+
return nil
449+
}
450+

SwiftCompilerSources/Sources/Optimizer/Utilities/OptUtils.swift

Lines changed: 0 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -853,77 +853,6 @@ extension InstructionRange {
853853
}
854854
}
855855

856-
/// Analyses the global initializer function and returns the `alloc_global` and `store`
857-
/// instructions which initialize the global.
858-
/// Returns nil if `function` has any side-effects beside initializing the global.
859-
///
860-
/// The function's single basic block must contain following code pattern:
861-
/// ```
862-
/// alloc_global @the_global
863-
/// %a = global_addr @the_global
864-
/// %i = some_const_initializer_insts
865-
/// store %i to %a
866-
/// ```
867-
///
868-
/// For all other instructions `handleUnknownInstruction` is called and such an instruction
869-
/// is accepted if `handleUnknownInstruction` returns true.
870-
func getGlobalInitialization(
871-
of function: Function,
872-
_ context: some Context,
873-
handleUnknownInstruction: (Instruction) -> Bool
874-
) -> (allocInst: AllocGlobalInst, storeToGlobal: StoreInst)? {
875-
guard let block = function.blocks.singleElement else {
876-
return nil
877-
}
878-
879-
var allocInst: AllocGlobalInst? = nil
880-
var globalAddr: GlobalAddrInst? = nil
881-
var store: StoreInst? = nil
882-
883-
for inst in block.instructions {
884-
switch inst {
885-
case is ReturnInst,
886-
is DebugValueInst,
887-
is DebugStepInst,
888-
is BeginAccessInst,
889-
is EndAccessInst:
890-
continue
891-
case let agi as AllocGlobalInst:
892-
if allocInst == nil {
893-
allocInst = agi
894-
continue
895-
}
896-
case let ga as GlobalAddrInst:
897-
if let agi = allocInst, agi.global == ga.global {
898-
globalAddr = ga
899-
}
900-
continue
901-
case let si as StoreInst:
902-
if store == nil,
903-
let ga = globalAddr,
904-
si.destination == ga
905-
{
906-
store = si
907-
continue
908-
}
909-
// Note that the initializer must not contain a `global_value` because `global_value` needs to
910-
// initialize the class metadata at runtime.
911-
default:
912-
if inst.isValidInStaticInitializerOfGlobal(context) {
913-
continue
914-
}
915-
}
916-
if handleUnknownInstruction(inst) {
917-
continue
918-
}
919-
return nil
920-
}
921-
if let store = store {
922-
return (allocInst: allocInst!, storeToGlobal: store)
923-
}
924-
return nil
925-
}
926-
927856
func canDynamicallyCast(from sourceType: CanonicalType, to destType: CanonicalType,
928857
in function: Function, sourceTypeIsExact: Bool
929858
) -> Bool? {

0 commit comments

Comments
 (0)