Skip to content

Commit 68cf62a

Browse files
committed
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
1 parent 1c8b0a1 commit 68cf62a

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

llvm/lib/Transforms/IPO/ConstantMerge.cpp

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,12 @@
1919

2020
#define DEBUG_TYPE "constmerge"
2121
#include "llvm/Transforms/IPO.h"
22+
#include "llvm/Constants.h"
2223
#include "llvm/DerivedTypes.h"
2324
#include "llvm/Module.h"
2425
#include "llvm/Pass.h"
2526
#include "llvm/ADT/DenseMap.h"
27+
#include "llvm/ADT/SmallPtrSet.h"
2628
#include "llvm/ADT/Statistic.h"
2729
using namespace llvm;
2830

@@ -46,7 +48,27 @@ INITIALIZE_PASS(ConstantMerge, "constmerge",
4648

4749
ModulePass *llvm::createConstantMergePass() { return new ConstantMerge(); }
4850

51+
52+
53+
/// Find values that are marked as llvm.used.
54+
static void FindUsedValues(GlobalVariable *LLVMUsed,
55+
SmallPtrSet<const GlobalValue*, 8> &UsedValues) {
56+
if (LLVMUsed == 0) return;
57+
ConstantArray *Inits = dyn_cast<ConstantArray>(LLVMUsed->getInitializer());
58+
if (Inits == 0) return;
59+
60+
for (unsigned i = 0, e = Inits->getNumOperands(); i != e; ++i)
61+
if (GlobalValue *GV =
62+
dyn_cast<GlobalValue>(Inits->getOperand(i)->stripPointerCasts()))
63+
UsedValues.insert(GV);
64+
}
65+
4966
bool ConstantMerge::runOnModule(Module &M) {
67+
// Find all the globals that are marked "used". These cannot be merged.
68+
SmallPtrSet<const GlobalValue*, 8> UsedGlobals;
69+
FindUsedValues(M.getGlobalVariable("llvm.used"), UsedGlobals);
70+
FindUsedValues(M.getGlobalVariable("llvm.compiler.used"), UsedGlobals);
71+
5072
// Map unique constant/section pairs to globals. We don't want to merge
5173
// globals in different sections.
5274
DenseMap<Constant*, GlobalVariable*> CMap;
@@ -79,9 +101,13 @@ bool ConstantMerge::runOnModule(Module &M) {
79101

80102
// Only process constants with initializers in the default addres space.
81103
if (!GV->isConstant() ||!GV->hasDefinitiveInitializer() ||
82-
GV->getType()->getAddressSpace() != 0 || !GV->getSection().empty())
104+
GV->getType()->getAddressSpace() != 0 || !GV->getSection().empty() ||
105+
// Don't touch values marked with attribute(used).
106+
UsedGlobals.count(GV))
83107
continue;
84108

109+
110+
85111
Constant *Init = GV->getInitializer();
86112

87113
// Check to see if the initializer is already known.

llvm/test/Transforms/ConstantMerge/dont-merge.ll

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,17 @@ define void @test2(i32** %P1, i32 addrspace(30)** %P2) {
2828
store i32 addrspace(30)* @T2b, i32 addrspace(30)** %P2
2929
ret void
3030
}
31+
32+
; PR8144 - Don't merge globals marked attribute(used)
33+
; CHECK: @T3A =
34+
; CHECK: @T3B =
35+
36+
@T3A = internal constant i32 0
37+
@T3B = internal constant i32 0
38+
@llvm.used = appending global [2 x i32*] [i32* @T3A, i32* @T3B], section
39+
"llvm.metadata"
40+
41+
define void @test3() {
42+
call void asm sideeffect "T3A, T3B",""() ; invisible use of T3A and T3B
43+
ret void
44+
}

0 commit comments

Comments
 (0)