Skip to content

Commit 9b9c195

Browse files
committed
UseListOrder: Order GlobalValue uses after initializers
To avoid unnecessary forward references, the reader doesn't process initializers of `GlobalValue`s until after the constant pool has been processed, and then in reverse order. Model this when predicting use-list order. This gets two more Bitcode tests passing with `llvm-uselistorder`. Part of PR5680. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@214242 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent abf3c77 commit 9b9c195

File tree

3 files changed

+57
-14
lines changed

3 files changed

+57
-14
lines changed

lib/Bitcode/Writer/ValueEnumerator.cpp

+55-14
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,17 @@ using namespace llvm;
2828
namespace {
2929
struct OrderMap {
3030
DenseMap<const Value *, std::pair<unsigned, bool>> IDs;
31+
unsigned LastGlobalConstantID;
32+
unsigned LastGlobalValueID;
33+
34+
OrderMap() : LastGlobalConstantID(0), LastGlobalValueID(0) {}
35+
36+
bool isGlobalConstant(unsigned ID) const {
37+
return ID <= LastGlobalConstantID;
38+
}
39+
bool isGlobalValue(unsigned ID) const {
40+
return ID <= LastGlobalValueID && !isGlobalConstant(ID);
41+
}
3142

3243
unsigned size() const { return IDs.size(); }
3344
std::pair<unsigned, bool> &operator[](const Value *V) { return IDs[V]; }
@@ -44,7 +55,7 @@ static void orderValue(const Value *V, OrderMap &OM) {
4455
if (const Constant *C = dyn_cast<Constant>(V))
4556
if (C->getNumOperands() && !isa<GlobalValue>(C))
4657
for (const Value *Op : C->operands())
47-
if (!isa<BasicBlock>(Op))
58+
if (!isa<BasicBlock>(Op) && !isa<GlobalValue>(Op))
4859
orderValue(Op, OM);
4960

5061
// Note: we cannot cache this lookup above, since inserting into the map
@@ -57,12 +68,11 @@ static OrderMap orderModule(const Module *M) {
5768
// and ValueEnumerator::incorporateFunction().
5869
OrderMap OM;
5970

60-
for (const GlobalVariable &G : M->globals())
61-
orderValue(&G, OM);
62-
for (const Function &F : *M)
63-
orderValue(&F, OM);
64-
for (const GlobalAlias &A : M->aliases())
65-
orderValue(&A, OM);
71+
// In the reader, initializers of GlobalValues are set *after* all the
72+
// globals have been read. Rather than awkwardly modeling this behaviour
73+
// directly in predictValueUseListOrderImpl(), just assign IDs to
74+
// initializers of GlobalValues before GlobalValues themselves to model this
75+
// implicitly.
6676
for (const GlobalVariable &G : M->globals())
6777
if (G.hasInitializer())
6878
orderValue(G.getInitializer(), OM);
@@ -71,6 +81,23 @@ static OrderMap orderModule(const Module *M) {
7181
for (const Function &F : *M)
7282
if (F.hasPrefixData())
7383
orderValue(F.getPrefixData(), OM);
84+
OM.LastGlobalConstantID = OM.size();
85+
86+
// Initializers of GlobalValues are processed in
87+
// BitcodeReader::ResolveGlobalAndAliasInits(). Match the order there rather
88+
// than ValueEnumerator, and match the code in predictValueUseListOrderImpl()
89+
// by giving IDs in reverse order.
90+
//
91+
// Since GlobalValues never reference each other directly (just through
92+
// initializers), their relative IDs only matter for determining order of
93+
// uses in their initializers.
94+
for (const Function &F : *M)
95+
orderValue(&F, OM);
96+
for (const GlobalAlias &A : M->aliases())
97+
orderValue(&A, OM);
98+
for (const GlobalVariable &G : M->globals())
99+
orderValue(&G, OM);
100+
OM.LastGlobalValueID = OM.size();
74101

75102
for (const Function &F : *M) {
76103
if (F.isDeclaration())
@@ -110,31 +137,45 @@ static void predictValueUseListOrderImpl(const Value *V, const Function *F,
110137
// We may have lost some users.
111138
return;
112139

113-
std::sort(List.begin(), List.end(),
114-
[&OM, ID](const Entry &L, const Entry &R) {
140+
bool IsGlobalValue = OM.isGlobalValue(ID);
141+
std::sort(List.begin(), List.end(), [&](const Entry &L, const Entry &R) {
115142
const Use *LU = L.first;
116143
const Use *RU = R.first;
117144
if (LU == RU)
118145
return false;
119146

120147
auto LID = OM.lookup(LU->getUser()).first;
121148
auto RID = OM.lookup(RU->getUser()).first;
149+
150+
// Global values are processed in reverse order.
151+
//
152+
// Moreover, initializers of GlobalValues are set *after* all the globals
153+
// have been read (despite having earlier IDs). Rather than awkwardly
154+
// modeling this behaviour here, orderModule() has assigned IDs to
155+
// initializers of GlobalValues before GlobalValues themselves.
156+
if (OM.isGlobalValue(LID) && OM.isGlobalValue(RID))
157+
return LID < RID;
158+
122159
// If ID is 4, then expect: 7 6 5 1 2 3.
123160
if (LID < RID) {
124161
if (RID < ID)
125-
return true;
162+
if (!IsGlobalValue) // GlobalValue uses don't get reversed.
163+
return true;
126164
return false;
127165
}
128166
if (RID < LID) {
129167
if (LID < ID)
130-
return false;
168+
if (!IsGlobalValue) // GlobalValue uses don't get reversed.
169+
return false;
131170
return true;
132171
}
172+
133173
// LID and RID are equal, so we have different operands of the same user.
134174
// Assume operands are added in order for all instructions.
135-
if (LU->getOperandNo() < RU->getOperandNo())
136-
return LID < ID;
137-
return ID < LID;
175+
if (LID < ID)
176+
if (!IsGlobalValue) // GlobalValue uses don't get reversed.
177+
return LU->getOperandNo() < RU->getOperandNo();
178+
return LU->getOperandNo() > RU->getOperandNo();
138179
});
139180

140181
if (std::is_sorted(

test/Bitcode/local-linkage-default-visibility.3.4.ll

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
; RUN: llvm-dis < %s.bc | FileCheck %s
2+
; RUN: llvm-uselistorder < %s.bc -preserve-bc-use-list-order -num-shuffles=5
23

34
; local-linkage-default-visibility.3.4.ll.bc was generated by passing this file
45
; to llvm-as-3.4. The test checks that LLVM upgrades visibility of symbols

test/Bitcode/old-aliases.ll

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
; RUN: llvm-dis < %s.bc | FileCheck %s
2+
; RUN: llvm-uselistorder < %s.bc -preserve-bc-use-list-order -num-shuffles=5
23

34
; old-aliases.bc consist of this file assembled with an old llvm-as (3.5 trunk)
45
; from when aliases contained a ConstantExpr.

0 commit comments

Comments
 (0)