[llvm] r214271 - Reapply "UseListOrder: Order GlobalValue uses after initializers"

Duncan P. N. Exon Smith dexonsmith at apple.com
Tue Jul 29 18:22:16 PDT 2014


Author: dexonsmith
Date: Tue Jul 29 20:22:16 2014
New Revision: 214271

URL: http://llvm.org/viewvc/llvm-project?rev=214271&view=rev
Log:
Reapply "UseListOrder: Order GlobalValue uses after initializers"

This reverts commit r214249, reapplying r214242 and r214243, now that
r214270 has fixed the UB.

Modified:
    llvm/trunk/lib/Bitcode/Writer/ValueEnumerator.cpp
    llvm/trunk/test/Bitcode/local-linkage-default-visibility.3.4.ll
    llvm/trunk/test/Bitcode/old-aliases.ll
    llvm/trunk/test/Bitcode/use-list-order.ll

Modified: llvm/trunk/lib/Bitcode/Writer/ValueEnumerator.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Bitcode/Writer/ValueEnumerator.cpp?rev=214271&r1=214270&r2=214271&view=diff
==============================================================================
--- llvm/trunk/lib/Bitcode/Writer/ValueEnumerator.cpp (original)
+++ llvm/trunk/lib/Bitcode/Writer/ValueEnumerator.cpp Tue Jul 29 20:22:16 2014
@@ -28,6 +28,17 @@ using namespace llvm;
 namespace {
 struct OrderMap {
   DenseMap<const Value *, std::pair<unsigned, bool>> IDs;
+  unsigned LastGlobalConstantID;
+  unsigned LastGlobalValueID;
+
+  OrderMap() : LastGlobalConstantID(0), LastGlobalValueID(0) {}
+
+  bool isGlobalConstant(unsigned ID) const {
+    return ID <= LastGlobalConstantID;
+  }
+  bool isGlobalValue(unsigned ID) const {
+    return ID <= LastGlobalValueID && !isGlobalConstant(ID);
+  }
 
   unsigned size() const { return IDs.size(); }
   std::pair<unsigned, bool> &operator[](const Value *V) { return IDs[V]; }
@@ -49,7 +60,7 @@ static void orderValue(const Value *V, O
   if (const Constant *C = dyn_cast<Constant>(V))
     if (C->getNumOperands() && !isa<GlobalValue>(C))
       for (const Value *Op : C->operands())
-        if (!isa<BasicBlock>(Op))
+        if (!isa<BasicBlock>(Op) && !isa<GlobalValue>(Op))
           orderValue(Op, OM);
 
   // Note: we cannot cache this lookup above, since inserting into the map
@@ -62,12 +73,11 @@ static OrderMap orderModule(const Module
   // and ValueEnumerator::incorporateFunction().
   OrderMap OM;
 
-  for (const GlobalVariable &G : M->globals())
-    orderValue(&G, OM);
-  for (const Function &F : *M)
-    orderValue(&F, OM);
-  for (const GlobalAlias &A : M->aliases())
-    orderValue(&A, OM);
+  // In the reader, initializers of GlobalValues are set *after* all the
+  // globals have been read.  Rather than awkwardly modeling this behaviour
+  // directly in predictValueUseListOrderImpl(), just assign IDs to
+  // initializers of GlobalValues before GlobalValues themselves to model this
+  // implicitly.
   for (const GlobalVariable &G : M->globals())
     if (G.hasInitializer())
       orderValue(G.getInitializer(), OM);
@@ -76,6 +86,23 @@ static OrderMap orderModule(const Module
   for (const Function &F : *M)
     if (F.hasPrefixData())
       orderValue(F.getPrefixData(), OM);
+  OM.LastGlobalConstantID = OM.size();
+
+  // Initializers of GlobalValues are processed in
+  // BitcodeReader::ResolveGlobalAndAliasInits().  Match the order there rather
+  // than ValueEnumerator, and match the code in predictValueUseListOrderImpl()
+  // by giving IDs in reverse order.
+  //
+  // Since GlobalValues never reference each other directly (just through
+  // initializers), their relative IDs only matter for determining order of
+  // uses in their initializers.
+  for (const Function &F : *M)
+    orderValue(&F, OM);
+  for (const GlobalAlias &A : M->aliases())
+    orderValue(&A, OM);
+  for (const GlobalVariable &G : M->globals())
+    orderValue(&G, OM);
+  OM.LastGlobalValueID = OM.size();
 
   for (const Function &F : *M) {
     if (F.isDeclaration())
@@ -115,8 +142,8 @@ static void predictValueUseListOrderImpl
     // We may have lost some users.
     return;
 
-  std::sort(List.begin(), List.end(),
-            [&OM, ID](const Entry &L, const Entry &R) {
+  bool IsGlobalValue = OM.isGlobalValue(ID);
+  std::sort(List.begin(), List.end(), [&](const Entry &L, const Entry &R) {
     const Use *LU = L.first;
     const Use *RU = R.first;
     if (LU == RU)
@@ -124,22 +151,36 @@ static void predictValueUseListOrderImpl
 
     auto LID = OM.lookup(LU->getUser()).first;
     auto RID = OM.lookup(RU->getUser()).first;
+
+    // Global values are processed in reverse order.
+    //
+    // Moreover, initializers of GlobalValues are set *after* all the globals
+    // have been read (despite having earlier IDs).  Rather than awkwardly
+    // modeling this behaviour here, orderModule() has assigned IDs to
+    // initializers of GlobalValues before GlobalValues themselves.
+    if (OM.isGlobalValue(LID) && OM.isGlobalValue(RID))
+      return LID < RID;
+
     // If ID is 4, then expect: 7 6 5 1 2 3.
     if (LID < RID) {
       if (RID < ID)
-        return true;
+        if (!IsGlobalValue) // GlobalValue uses don't get reversed.
+          return true;
       return false;
     }
     if (RID < LID) {
       if (LID < ID)
-        return false;
+        if (!IsGlobalValue) // GlobalValue uses don't get reversed.
+          return false;
       return true;
     }
+
     // LID and RID are equal, so we have different operands of the same user.
     // Assume operands are added in order for all instructions.
-    if (LU->getOperandNo() < RU->getOperandNo())
-      return LID < ID;
-    return ID < LID;
+    if (LID < ID)
+      if (!IsGlobalValue) // GlobalValue uses don't get reversed.
+        return LU->getOperandNo() < RU->getOperandNo();
+    return LU->getOperandNo() > RU->getOperandNo();
   });
 
   if (std::is_sorted(

Modified: llvm/trunk/test/Bitcode/local-linkage-default-visibility.3.4.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Bitcode/local-linkage-default-visibility.3.4.ll?rev=214271&r1=214270&r2=214271&view=diff
==============================================================================
--- llvm/trunk/test/Bitcode/local-linkage-default-visibility.3.4.ll (original)
+++ llvm/trunk/test/Bitcode/local-linkage-default-visibility.3.4.ll Tue Jul 29 20:22:16 2014
@@ -1,4 +1,5 @@
 ; RUN: llvm-dis < %s.bc | FileCheck %s
+; RUN: llvm-uselistorder < %s.bc -preserve-bc-use-list-order -num-shuffles=5
 
 ; local-linkage-default-visibility.3.4.ll.bc was generated by passing this file
 ; to llvm-as-3.4.  The test checks that LLVM upgrades visibility of symbols

Modified: llvm/trunk/test/Bitcode/old-aliases.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Bitcode/old-aliases.ll?rev=214271&r1=214270&r2=214271&view=diff
==============================================================================
--- llvm/trunk/test/Bitcode/old-aliases.ll (original)
+++ llvm/trunk/test/Bitcode/old-aliases.ll Tue Jul 29 20:22:16 2014
@@ -1,4 +1,5 @@
 ; RUN: llvm-dis < %s.bc | FileCheck %s
+; RUN: llvm-uselistorder < %s.bc -preserve-bc-use-list-order -num-shuffles=5
 
 ; old-aliases.bc consist of this file assembled with an old llvm-as (3.5 trunk)
 ; from when aliases contained a ConstantExpr.

Modified: llvm/trunk/test/Bitcode/use-list-order.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Bitcode/use-list-order.ll?rev=214271&r1=214270&r2=214271&view=diff
==============================================================================
--- llvm/trunk/test/Bitcode/use-list-order.ll (original)
+++ llvm/trunk/test/Bitcode/use-list-order.ll Tue Jul 29 20:22:16 2014
@@ -3,6 +3,20 @@
 @a = global [4 x i1] [i1 0, i1 1, i1 0, i1 1]
 @b = alias i1* getelementptr ([4 x i1]* @a, i64 0, i64 2)
 
+; Check use-list order of constants used by globals.
+ at glob1 = global i5 7
+ at glob2 = global i5 7
+ at glob3 = global i5 7
+
+; Check use-list order between variables and aliases.
+ at target = global i3 zeroinitializer
+ at alias1 = alias i3* @target
+ at alias2 = alias i3* @target
+ at alias3 = alias i3* @target
+ at var1 = global i3* @target
+ at var2 = global i3* @target
+ at var3 = global i3* @target
+
 define i64 @f(i64 %f) {
 entry:
   %sum = add i64 %f, 0





More information about the llvm-commits mailing list