annotate lib/Transforms/IPO/ConstantMerge.cpp @ 125:56c5119fbcd2

fix
author mir3636
date Sun, 03 Dec 2017 20:09:16 +0900
parents 803732b1fca8
children c2174574ed3a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1 //===- ConstantMerge.cpp - Merge duplicate global constants ---------------===//
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
2 //
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
3 // The LLVM Compiler Infrastructure
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
4 //
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
5 // This file is distributed under the University of Illinois Open Source
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
6 // License. See LICENSE.TXT for details.
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
7 //
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
8 //===----------------------------------------------------------------------===//
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
9 //
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
10 // This file defines the interface to a pass that merges duplicate global
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
11 // constants together into a single constant that is shared. This is useful
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
12 // because some passes (ie TraceValues) insert a lot of string constants into
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
13 // the program, regardless of whether or not an existing string is available.
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
14 //
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
15 // Algorithm: ConstantMerge is designed to build up a map of available constants
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
16 // and eliminate duplicates when it is initialized.
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
17 //
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
18 //===----------------------------------------------------------------------===//
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
19
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
20 #include "llvm/Transforms/IPO/ConstantMerge.h"
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
21 #include "llvm/ADT/DenseMap.h"
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
22 #include "llvm/ADT/SmallPtrSet.h"
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
23 #include "llvm/ADT/SmallVector.h"
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
24 #include "llvm/ADT/Statistic.h"
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
25 #include "llvm/IR/Constants.h"
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
26 #include "llvm/IR/DataLayout.h"
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
27 #include "llvm/IR/DerivedTypes.h"
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
28 #include "llvm/IR/GlobalValue.h"
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
29 #include "llvm/IR/GlobalVariable.h"
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
30 #include "llvm/IR/LLVMContext.h"
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
31 #include "llvm/IR/Module.h"
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
32 #include "llvm/Pass.h"
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
33 #include "llvm/Support/Casting.h"
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
34 #include "llvm/Transforms/IPO.h"
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
35 #include <algorithm>
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
36 #include <cassert>
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
37 #include <utility>
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
38
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
39 using namespace llvm;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
40
77
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
41 #define DEBUG_TYPE "constmerge"
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
42
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
43 STATISTIC(NumMerged, "Number of global constants merged");
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
44
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
45 /// Find values that are marked as llvm.used.
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
46 static void FindUsedValues(GlobalVariable *LLVMUsed,
77
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
47 SmallPtrSetImpl<const GlobalValue*> &UsedValues) {
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
48 if (!LLVMUsed) return;
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
49 ConstantArray *Inits = cast<ConstantArray>(LLVMUsed->getInitializer());
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
50
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
51 for (unsigned i = 0, e = Inits->getNumOperands(); i != e; ++i) {
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
52 Value *Operand = Inits->getOperand(i)->stripPointerCastsNoFollowAliases();
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
53 GlobalValue *GV = cast<GlobalValue>(Operand);
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
54 UsedValues.insert(GV);
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
55 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
56 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
57
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
58 // True if A is better than B.
77
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
59 static bool IsBetterCanonical(const GlobalVariable &A,
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
60 const GlobalVariable &B) {
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
61 if (!A.hasLocalLinkage() && B.hasLocalLinkage())
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
62 return true;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
63
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
64 if (A.hasLocalLinkage() && !B.hasLocalLinkage())
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
65 return false;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
66
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
67 return A.hasGlobalUnnamedAddr();
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
68 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
69
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
70 static bool hasMetadataOtherThanDebugLoc(const GlobalVariable *GV) {
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
71 SmallVector<std::pair<unsigned, MDNode *>, 4> MDs;
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
72 GV->getAllMetadata(MDs);
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
73 for (const auto &V : MDs)
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
74 if (V.first != LLVMContext::MD_dbg)
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
75 return true;
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
76 return false;
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
77 }
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
78
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
79 static void copyDebugLocMetadata(const GlobalVariable *From,
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
80 GlobalVariable *To) {
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
81 SmallVector<DIGlobalVariableExpression *, 1> MDs;
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
82 From->getDebugInfo(MDs);
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
83 for (auto MD : MDs)
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
84 To->addDebugInfo(MD);
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
85 }
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
86
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
87 static unsigned getAlignment(GlobalVariable *GV) {
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
88 unsigned Align = GV->getAlignment();
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
89 if (Align)
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
90 return Align;
95
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
91 return GV->getParent()->getDataLayout().getPreferredAlignment(GV);
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
92 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
93
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
94 static bool mergeConstants(Module &M) {
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
95 // Find all the globals that are marked "used". These cannot be merged.
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
96 SmallPtrSet<const GlobalValue*, 8> UsedGlobals;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
97 FindUsedValues(M.getGlobalVariable("llvm.used"), UsedGlobals);
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
98 FindUsedValues(M.getGlobalVariable("llvm.compiler.used"), UsedGlobals);
95
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
99
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
100 // Map unique constants to globals.
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
101 DenseMap<Constant *, GlobalVariable *> CMap;
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
102
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
103 // Replacements - This vector contains a list of replacements to perform.
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
104 SmallVector<std::pair<GlobalVariable*, GlobalVariable*>, 32> Replacements;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
105
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
106 bool MadeChange = false;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
107
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
108 // Iterate constant merging while we are still making progress. Merging two
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
109 // constants together may allow us to merge other constants together if the
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
110 // second level constants have initializers which point to the globals that
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
111 // were just merged.
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
112 while (true) {
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
113 // First: Find the canonical constants others will be merged with.
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
114 for (Module::global_iterator GVI = M.global_begin(), E = M.global_end();
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
115 GVI != E; ) {
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
116 GlobalVariable *GV = &*GVI++;
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
117
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
118 // If this GV is dead, remove it.
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
119 GV->removeDeadConstantUsers();
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
120 if (GV->use_empty() && GV->hasLocalLinkage()) {
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
121 GV->eraseFromParent();
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
122 continue;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
123 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
124
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
125 // Only process constants with initializers in the default address space.
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
126 if (!GV->isConstant() || !GV->hasDefinitiveInitializer() ||
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
127 GV->getType()->getAddressSpace() != 0 || GV->hasSection() ||
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
128 // Don't touch values marked with attribute(used).
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
129 UsedGlobals.count(GV))
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
130 continue;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
131
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
132 // This transformation is legal for weak ODR globals in the sense it
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
133 // doesn't change semantics, but we really don't want to perform it
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
134 // anyway; it's likely to pessimize code generation, and some tools
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
135 // (like the Darwin linker in cases involving CFString) don't expect it.
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
136 if (GV->isWeakForLinker())
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
137 continue;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
138
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
139 // Don't touch globals with metadata other then !dbg.
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
140 if (hasMetadataOtherThanDebugLoc(GV))
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
141 continue;
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
142
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
143 Constant *Init = GV->getInitializer();
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
144
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
145 // Check to see if the initializer is already known.
95
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
146 GlobalVariable *&Slot = CMap[Init];
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
147
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
148 // If this is the first constant we find or if the old one is local,
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
149 // replace with the current one. If the current is externally visible
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
150 // it cannot be replace, but can be the canonical constant we merge with.
77
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
151 if (!Slot || IsBetterCanonical(*GV, *Slot))
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
152 Slot = GV;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
153 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
154
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
155 // Second: identify all globals that can be merged together, filling in
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
156 // the Replacements vector. We cannot do the replacement in this pass
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
157 // because doing so may cause initializers of other globals to be rewritten,
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
158 // invalidating the Constant* pointers in CMap.
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
159 for (Module::global_iterator GVI = M.global_begin(), E = M.global_end();
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
160 GVI != E; ) {
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
161 GlobalVariable *GV = &*GVI++;
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
162
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
163 // Only process constants with initializers in the default address space.
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
164 if (!GV->isConstant() || !GV->hasDefinitiveInitializer() ||
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
165 GV->getType()->getAddressSpace() != 0 || GV->hasSection() ||
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
166 // Don't touch values marked with attribute(used).
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
167 UsedGlobals.count(GV))
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
168 continue;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
169
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
170 // We can only replace constant with local linkage.
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
171 if (!GV->hasLocalLinkage())
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
172 continue;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
173
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
174 Constant *Init = GV->getInitializer();
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
175
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
176 // Check to see if the initializer is already known.
95
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
177 GlobalVariable *Slot = CMap[Init];
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
178
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
179 if (!Slot || Slot == GV)
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
180 continue;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
181
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
182 if (!Slot->hasGlobalUnnamedAddr() && !GV->hasGlobalUnnamedAddr())
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
183 continue;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
184
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
185 if (hasMetadataOtherThanDebugLoc(GV))
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
186 continue;
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
187
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
188 if (!GV->hasGlobalUnnamedAddr())
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
189 Slot->setUnnamedAddr(GlobalValue::UnnamedAddr::None);
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
190
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
191 // Make all uses of the duplicate constant use the canonical version.
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
192 Replacements.push_back(std::make_pair(GV, Slot));
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
193 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
194
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
195 if (Replacements.empty())
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
196 return MadeChange;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
197 CMap.clear();
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
198
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
199 // Now that we have figured out which replacements must be made, do them all
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
200 // now. This avoid invalidating the pointers in CMap, which are unneeded
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
201 // now.
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
202 for (unsigned i = 0, e = Replacements.size(); i != e; ++i) {
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
203 // Bump the alignment if necessary.
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
204 if (Replacements[i].first->getAlignment() ||
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
205 Replacements[i].second->getAlignment()) {
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
206 Replacements[i].second->setAlignment(
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
207 std::max(getAlignment(Replacements[i].first),
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
208 getAlignment(Replacements[i].second)));
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
209 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
210
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
211 copyDebugLocMetadata(Replacements[i].first, Replacements[i].second);
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
212
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
213 // Eliminate any uses of the dead global.
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
214 Replacements[i].first->replaceAllUsesWith(Replacements[i].second);
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
215
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
216 // Delete the global value from the module.
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
217 assert(Replacements[i].first->hasLocalLinkage() &&
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
218 "Refusing to delete an externally visible global variable.");
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
219 Replacements[i].first->eraseFromParent();
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
220 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
221
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
222 NumMerged += Replacements.size();
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
223 Replacements.clear();
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
224 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
225 }
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
226
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
227 PreservedAnalyses ConstantMergePass::run(Module &M, ModuleAnalysisManager &) {
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
228 if (!mergeConstants(M))
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
229 return PreservedAnalyses::all();
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
230 return PreservedAnalyses::none();
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
231 }
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
232
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
233 namespace {
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
234
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
235 struct ConstantMergeLegacyPass : public ModulePass {
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
236 static char ID; // Pass identification, replacement for typeid
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
237
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
238 ConstantMergeLegacyPass() : ModulePass(ID) {
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
239 initializeConstantMergeLegacyPassPass(*PassRegistry::getPassRegistry());
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
240 }
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
241
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
242 // For this pass, process all of the globals in the module, eliminating
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
243 // duplicate constants.
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
244 bool runOnModule(Module &M) override {
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
245 if (skipModule(M))
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
246 return false;
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
247 return mergeConstants(M);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
248 }
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
249 };
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
250
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
251 } // end anonymous namespace
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
252
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
253 char ConstantMergeLegacyPass::ID = 0;
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
254
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
255 INITIALIZE_PASS(ConstantMergeLegacyPass, "constmerge",
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
256 "Merge Duplicate Global Constants", false, false)
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
257
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
258 ModulePass *llvm::createConstantMergePass() {
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
259 return new ConstantMergeLegacyPass();
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
260 }