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

whitequark via llvm-commits llvm-commits at lists.llvm.org
Wed Oct 18 21:47:49 PDT 2017


Author: whitequark
Date: Wed Oct 18 21:47:48 2017
New Revision: 316145

URL: http://llvm.org/viewvc/llvm-project?rev=316145&view=rev
Log:
[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.

Modified:
    llvm/trunk/include/llvm/Transforms/Utils/FunctionComparator.h
    llvm/trunk/lib/Transforms/IPO/MergeFunctions.cpp

Modified: llvm/trunk/include/llvm/Transforms/Utils/FunctionComparator.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Transforms/Utils/FunctionComparator.h?rev=316145&r1=316144&r2=316145&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Transforms/Utils/FunctionComparator.h (original)
+++ llvm/trunk/include/llvm/Transforms/Utils/FunctionComparator.h Wed Oct 18 21:47:48 2017
@@ -78,6 +78,10 @@ public:
     return MapIter->second;
   }
 
+  void erase(GlobalValue *Global) {
+    GlobalNumbers.erase(Global);
+  }
+
   void clear() {
     GlobalNumbers.clear();
   }

Modified: llvm/trunk/lib/Transforms/IPO/MergeFunctions.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/IPO/MergeFunctions.cpp?rev=316145&r1=316144&r2=316145&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/IPO/MergeFunctions.cpp (original)
+++ llvm/trunk/lib/Transforms/IPO/MergeFunctions.cpp Wed Oct 18 21:47:48 2017
@@ -629,6 +629,9 @@ void MergeFunctions::filterInstsUnrelate
 void MergeFunctions::writeThunk(Function *F, Function *G) {
   if (!G->isInterposable() && !MergeFunctionsPDI) {
     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.
       Constant *BitcastF = ConstantExpr::getBitCast(F, G->getType());
       G->replaceAllUsesWith(BitcastF);




More information about the llvm-commits mailing list