[MergeFunctions] Don't blindly RAUW a GlobalValue with a ConstantExpr.

MergeFunctions uses (through FunctionComparator) a map of GlobalValues
to identifiers because it needs to compare functions and globals
do not have an inherent total order. Thus, FunctionComparator
(through GlobalNumberState) has a ValueMap<GlobalValue *>.

r315852 added a RAUW on globals that may have been previously
encountered by the FunctionComparator, which would replace
a GlobalValue * key with a ConstantExpr *, which is illegal.

This commit adjusts that code path to remove the function being
replaced from the ValueMap as well.

llvm-svn: 316145
This commit is contained in:
whitequark 2017-10-19 04:47:48 +00:00
parent deaba3862f
commit a99ecf1bbb
2 changed files with 7 additions and 0 deletions

View File

@ -78,6 +78,10 @@ public:
return MapIter->second; return MapIter->second;
} }
void erase(GlobalValue *Global) {
GlobalNumbers.erase(Global);
}
void clear() { void clear() {
GlobalNumbers.clear(); GlobalNumbers.clear();
} }

View File

@ -629,6 +629,9 @@ void MergeFunctions::filterInstsUnrelatedToPDI(
void MergeFunctions::writeThunk(Function *F, Function *G) { void MergeFunctions::writeThunk(Function *F, Function *G) {
if (!G->isInterposable() && !MergeFunctionsPDI) { if (!G->isInterposable() && !MergeFunctionsPDI) {
if (G->hasGlobalUnnamedAddr()) { if (G->hasGlobalUnnamedAddr()) {
// G might have been a key in our GlobalNumberState, and it's illegal
// to replace a key in ValueMap<GlobalValue *> with a non-global.
GlobalNumbers.erase(G);
// If G's address is not significant, replace it entirely. // If G's address is not significant, replace it entirely.
Constant *BitcastF = ConstantExpr::getBitCast(F, G->getType()); Constant *BitcastF = ConstantExpr::getBitCast(F, G->getType());
G->replaceAllUsesWith(BitcastF); G->replaceAllUsesWith(BitcastF);