19
19
20
20
#define DEBUG_TYPE " constmerge"
21
21
#include " llvm/Transforms/IPO.h"
22
+ #include " llvm/Constants.h"
22
23
#include " llvm/DerivedTypes.h"
23
24
#include " llvm/Module.h"
24
25
#include " llvm/Pass.h"
25
26
#include " llvm/ADT/DenseMap.h"
27
+ #include " llvm/ADT/SmallPtrSet.h"
26
28
#include " llvm/ADT/Statistic.h"
27
29
using namespace llvm ;
28
30
@@ -46,7 +48,27 @@ INITIALIZE_PASS(ConstantMerge, "constmerge",
46
48
47
49
ModulePass *llvm::createConstantMergePass () { return new ConstantMerge (); }
48
50
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
+
49
66
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
+
50
72
// Map unique constant/section pairs to globals. We don't want to merge
51
73
// globals in different sections.
52
74
DenseMap<Constant*, GlobalVariable*> CMap;
@@ -79,9 +101,13 @@ bool ConstantMerge::runOnModule(Module &M) {
79
101
80
102
// Only process constants with initializers in the default addres space.
81
103
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))
83
107
continue ;
84
108
109
+
110
+
85
111
Constant *Init = GV->getInitializer ();
86
112
87
113
// Check to see if the initializer is already known.
0 commit comments