@@ -122,7 +122,8 @@ class SILGlobalOpt {
122
122
123
123
protected:
124
124
// / Checks if a given global variable is assigned only once.
125
- bool isAssignedOnlyOnceInInitializer (SILGlobalVariable *SILG);
125
+ bool isAssignedOnlyOnceInInitializer (SILGlobalVariable *SILG,
126
+ SILFunction *globalAddrF);
126
127
127
128
// / Reset all the maps of global variables.
128
129
void reset ();
@@ -640,13 +641,21 @@ static SILFunction *genGetterFromInit(SILOptFunctionBuilder &FunctionBuilder,
640
641
return GetterF;
641
642
}
642
643
643
- bool SILGlobalOpt::isAssignedOnlyOnceInInitializer (SILGlobalVariable *SILG) {
644
+ bool SILGlobalOpt::isAssignedOnlyOnceInInitializer (SILGlobalVariable *SILG,
645
+ SILFunction *globalAddrF) {
644
646
if (SILG->isLet ())
645
647
return true ;
646
648
647
649
// If we should skip this, it is probably because there are multiple stores.
648
650
// Return false if there are multiple stores or no stores.
649
- if (GlobalVarSkipProcessing.count (SILG) || !GlobalVarStore.count (SILG))
651
+ if (GlobalVarSkipProcessing.count (SILG) || !GlobalVarStore.count (SILG) ||
652
+ // Check if there is more than one use the global addr function. If there
653
+ // is only one use, it must be the use that we are trying to optimize, so
654
+ // that is OK. If there is more than one use, one of the other uses may
655
+ // have a store attached to it which means there may be more than one
656
+ // assignment, so return false.
657
+ (GlobalInitCallMap.count (globalAddrF) &&
658
+ GlobalInitCallMap[globalAddrF].size () != 1 ))
650
659
return false ;
651
660
652
661
// Otherwise, return true if this can't be used externally (false, otherwise).
@@ -697,7 +706,7 @@ replaceLoadsByKnownValue(BuiltinInst *CallToOnce, SILFunction *AddrF,
697
706
LLVM_DEBUG (llvm::dbgs () << " GlobalOpt: replacing loads with known value for "
698
707
<< SILG->getName () << ' \n ' );
699
708
700
- assert (isAssignedOnlyOnceInInitializer (SILG) &&
709
+ assert (isAssignedOnlyOnceInInitializer (SILG, AddrF ) &&
701
710
" The value of the initializer should be known at compile-time" );
702
711
assert (SILG->getDecl () &&
703
712
" Decl corresponding to the global variable should be known" );
@@ -810,7 +819,7 @@ void SILGlobalOpt::optimizeInitializer(SILFunction *AddrF,
810
819
<< SILG->getName () << ' \n ' );
811
820
812
821
// Remove "once" call from the addressor.
813
- if (!isAssignedOnlyOnceInInitializer (SILG) || !SILG->getDecl ()) {
822
+ if (!isAssignedOnlyOnceInInitializer (SILG, AddrF ) || !SILG->getDecl ()) {
814
823
LLVM_DEBUG (llvm::dbgs () << " GlobalOpt: building static initializer for "
815
824
<< SILG->getName () << ' \n ' );
816
825
0 commit comments