[PATCH] GlobalOpt: Don't optimize dllimport for initializers

David Majnemer david.majnemer at gmail.com
Sun Jun 22 21:23:39 PDT 2014


Hi rnk, nicholas,

Referencing a dllimport variable requires actually instructions, not
just a relocation.  This partially fixes PR19955.

http://reviews.llvm.org/D4249

Files:
  lib/Transforms/IPO/GlobalOpt.cpp
  test/Transforms/GlobalOpt/constantfold-initializers.ll

Index: lib/Transforms/IPO/GlobalOpt.cpp
===================================================================
--- lib/Transforms/IPO/GlobalOpt.cpp
+++ lib/Transforms/IPO/GlobalOpt.cpp
@@ -2054,9 +2054,8 @@
     return false;
 
   if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C))
-    // Do not allow weak/*_odr/linkonce/dllimport/dllexport linkage or
-    // external globals.
-    return GV->hasUniqueInitializer();
+    // Do not allow weak/*_odr/linkonce linkage, dllimport, or external globals.
+    return GV->hasUniqueInitializer() || GV->hasDLLImportStorageClass();
 
   if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
     // Handle a constantexpr gep.
Index: test/Transforms/GlobalOpt/constantfold-initializers.ll
===================================================================
--- test/Transforms/GlobalOpt/constantfold-initializers.ll
+++ test/Transforms/GlobalOpt/constantfold-initializers.ll
@@ -50,7 +50,19 @@
   ret void
 }
 
+; PR19955
+
+ at dllexportptr = global i32* null, align 4
+; CHECK-NOT: @dllexportptr = global i32* @dllexportvar, align 4
+ at dllexportvar = external dllimport global i32
+define internal void @test3() {
+entry:
+  store i32* @dllexportvar, i32** @dllexportptr, align 4
+  ret void
+}
+
 @llvm.global_ctors = appending constant
-  [2 x { i32, void ()* }]
+  [3 x { i32, void ()* }]
   [{ i32, void ()* } { i32 65535, void ()* @test1 },
-   { i32, void ()* } { i32 65535, void ()* @test2 }]
+   { i32, void ()* } { i32 65535, void ()* @test2 },
+   { i32, void ()* } { i32 65535, void ()* @test3 }]
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D4249.10734.patch
Type: text/x-patch
Size: 1547 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20140623/c0824dc5/attachment.bin>


More information about the llvm-commits mailing list