Skip to content

Commit 5d086e7

Browse files
committed
Check that there is only one use of a given global addr function
1 parent 690c1f5 commit 5d086e7

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

lib/SILOptimizer/IPO/GlobalOpt.cpp

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,8 @@ class SILGlobalOpt {
122122

123123
protected:
124124
/// Checks if a given global variable is assigned only once.
125-
bool isAssignedOnlyOnceInInitializer(SILGlobalVariable *SILG);
125+
bool isAssignedOnlyOnceInInitializer(SILGlobalVariable *SILG,
126+
SILFunction *globalAddrF);
126127

127128
/// Reset all the maps of global variables.
128129
void reset();
@@ -640,13 +641,21 @@ static SILFunction *genGetterFromInit(SILOptFunctionBuilder &FunctionBuilder,
640641
return GetterF;
641642
}
642643

643-
bool SILGlobalOpt::isAssignedOnlyOnceInInitializer(SILGlobalVariable *SILG) {
644+
bool SILGlobalOpt::isAssignedOnlyOnceInInitializer(SILGlobalVariable *SILG,
645+
SILFunction *globalAddrF) {
644646
if (SILG->isLet())
645647
return true;
646648

647649
// If we should skip this, it is probably because there are multiple stores.
648650
// 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))
650659
return false;
651660

652661
// Otherwise, return true if this can't be used externally (false, otherwise).
@@ -697,7 +706,7 @@ replaceLoadsByKnownValue(BuiltinInst *CallToOnce, SILFunction *AddrF,
697706
LLVM_DEBUG(llvm::dbgs() << "GlobalOpt: replacing loads with known value for "
698707
<< SILG->getName() << '\n');
699708

700-
assert(isAssignedOnlyOnceInInitializer(SILG) &&
709+
assert(isAssignedOnlyOnceInInitializer(SILG, AddrF) &&
701710
"The value of the initializer should be known at compile-time");
702711
assert(SILG->getDecl() &&
703712
"Decl corresponding to the global variable should be known");
@@ -810,7 +819,7 @@ void SILGlobalOpt::optimizeInitializer(SILFunction *AddrF,
810819
<< SILG->getName() << '\n');
811820

812821
// Remove "once" call from the addressor.
813-
if (!isAssignedOnlyOnceInInitializer(SILG) || !SILG->getDecl()) {
822+
if (!isAssignedOnlyOnceInInitializer(SILG, AddrF) || !SILG->getDecl()) {
814823
LLVM_DEBUG(llvm::dbgs() << "GlobalOpt: building static initializer for "
815824
<< SILG->getName() << '\n');
816825

0 commit comments

Comments
 (0)