diff options
author | Bill Wendling <isanbard@gmail.com> | 2010-09-15 00:32:05 +0000 |
---|---|---|
committer | Bill Wendling <isanbard@gmail.com> | 2010-09-15 00:32:05 +0000 |
commit | 68cf62a515043c1795e0ebf4c45f906a297ad2e2 (patch) | |
tree | 96d084aad4e57936f0915af71a8871a7966c125a | |
parent | 1c8b0a1846a6a4548bd4b1acc5b8d4ef2f2f3424 (diff) | |
download | llvm-68cf62a515043c1795e0ebf4c45f906a297ad2e2.zip llvm-68cf62a515043c1795e0ebf4c45f906a297ad2e2.tar.gz llvm-68cf62a515043c1795e0ebf4c45f906a297ad2e2.tar.bz2 |
Approved by Chris:
$ svn merge -c 113911 https://llvm.org/svn/llvm-project/llvm/trunk
--- Merging r113911 into '.':
U test/Transforms/ConstantMerge/dont-merge.ll
U lib/Transforms/IPO/ConstantMerge.cpp
llvm-svn: 113912
-rw-r--r-- | llvm/lib/Transforms/IPO/ConstantMerge.cpp | 28 | ||||
-rw-r--r-- | llvm/test/Transforms/ConstantMerge/dont-merge.ll | 14 |
2 files changed, 41 insertions, 1 deletions
diff --git a/llvm/lib/Transforms/IPO/ConstantMerge.cpp b/llvm/lib/Transforms/IPO/ConstantMerge.cpp index 75282fa..64e8d79 100644 --- a/llvm/lib/Transforms/IPO/ConstantMerge.cpp +++ b/llvm/lib/Transforms/IPO/ConstantMerge.cpp @@ -19,10 +19,12 @@ #define DEBUG_TYPE "constmerge" #include "llvm/Transforms/IPO.h" +#include "llvm/Constants.h" #include "llvm/DerivedTypes.h" #include "llvm/Module.h" #include "llvm/Pass.h" #include "llvm/ADT/DenseMap.h" +#include "llvm/ADT/SmallPtrSet.h" #include "llvm/ADT/Statistic.h" using namespace llvm; @@ -46,7 +48,27 @@ INITIALIZE_PASS(ConstantMerge, "constmerge", ModulePass *llvm::createConstantMergePass() { return new ConstantMerge(); } + + +/// Find values that are marked as llvm.used. +static void FindUsedValues(GlobalVariable *LLVMUsed, + SmallPtrSet<const GlobalValue*, 8> &UsedValues) { + if (LLVMUsed == 0) return; + ConstantArray *Inits = dyn_cast<ConstantArray>(LLVMUsed->getInitializer()); + if (Inits == 0) return; + + for (unsigned i = 0, e = Inits->getNumOperands(); i != e; ++i) + if (GlobalValue *GV = + dyn_cast<GlobalValue>(Inits->getOperand(i)->stripPointerCasts())) + UsedValues.insert(GV); +} + bool ConstantMerge::runOnModule(Module &M) { + // Find all the globals that are marked "used". These cannot be merged. + SmallPtrSet<const GlobalValue*, 8> UsedGlobals; + FindUsedValues(M.getGlobalVariable("llvm.used"), UsedGlobals); + FindUsedValues(M.getGlobalVariable("llvm.compiler.used"), UsedGlobals); + // Map unique constant/section pairs to globals. We don't want to merge // globals in different sections. DenseMap<Constant*, GlobalVariable*> CMap; @@ -79,9 +101,13 @@ bool ConstantMerge::runOnModule(Module &M) { // Only process constants with initializers in the default addres space. if (!GV->isConstant() ||!GV->hasDefinitiveInitializer() || - GV->getType()->getAddressSpace() != 0 || !GV->getSection().empty()) + GV->getType()->getAddressSpace() != 0 || !GV->getSection().empty() || + // Don't touch values marked with attribute(used). + UsedGlobals.count(GV)) continue; + + Constant *Init = GV->getInitializer(); // Check to see if the initializer is already known. diff --git a/llvm/test/Transforms/ConstantMerge/dont-merge.ll b/llvm/test/Transforms/ConstantMerge/dont-merge.ll index 877cf8d..e5337df 100644 --- a/llvm/test/Transforms/ConstantMerge/dont-merge.ll +++ b/llvm/test/Transforms/ConstantMerge/dont-merge.ll @@ -28,3 +28,17 @@ define void @test2(i32** %P1, i32 addrspace(30)** %P2) { store i32 addrspace(30)* @T2b, i32 addrspace(30)** %P2 ret void } + +; PR8144 - Don't merge globals marked attribute(used) +; CHECK: @T3A = +; CHECK: @T3B = + +@T3A = internal constant i32 0 +@T3B = internal constant i32 0 +@llvm.used = appending global [2 x i32*] [i32* @T3A, i32* @T3B], section +"llvm.metadata" + +define void @test3() { + call void asm sideeffect "T3A, T3B",""() ; invisible use of T3A and T3B + ret void +} |