Index: lib/Transforms/IPO/GlobalOpt.cpp =================================================================== --- lib/Transforms/IPO/GlobalOpt.cpp +++ lib/Transforms/IPO/GlobalOpt.cpp @@ -1980,10 +1980,12 @@ static bool isSimpleEnoughValueToCommitHelper(Constant *C, SmallPtrSet &SimpleConstants, const DataLayout *DL) { - // Simple integer, undef, constant aggregate zero, global addresses, etc are - // all supported. - if (C->getNumOperands() == 0 || isa(C) || - isa(C)) + // Simple global addresses are supported, do not allow dllimport globals. + if (auto *GV = dyn_cast(C)) + return !GV->hasDLLImportStorageClass(); + + // Simple integer, undef, constant aggregate zero, etc are all supported. + if (C->getNumOperands() == 0 || isa(C)) return true; // Aggregate values are safe if all their elements are. @@ -2054,8 +2056,7 @@ return false; if (GlobalVariable *GV = dyn_cast(C)) - // Do not allow weak/*_odr/linkonce/dllimport/dllexport linkage or - // external globals. + // Do not allow weak/*_odr/linkonce linkage or external globals. return GV->hasUniqueInitializer(); if (ConstantExpr *CE = dyn_cast(C)) { 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 + +@dllexportptr = global i32* null, align 4 +; CHECK-NOT: @dllexportptr = global i32* @dllexportvar, align 4 +@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 }]