@@ -1602,6 +1602,39 @@ static void WriteValueSymbolTable(const ValueSymbolTable &VST,
1602
1602
Stream.ExitBlock ();
1603
1603
}
1604
1604
1605
+ static void WriteUseList (ValueEnumerator &VE, UseListOrder &&Order,
1606
+ BitstreamWriter &Stream) {
1607
+ assert (Order.Shuffle .size () >= 2 && " Shuffle too small" );
1608
+ unsigned Code;
1609
+ if (isa<BasicBlock>(Order.V ))
1610
+ Code = bitc::USELIST_CODE_BB;
1611
+ else
1612
+ Code = bitc::USELIST_CODE_DEFAULT;
1613
+
1614
+ SmallVector<uint64_t , 64 > Record;
1615
+ for (unsigned I : Order.Shuffle )
1616
+ Record.push_back (I);
1617
+ Record.push_back (VE.getValueID (Order.V ));
1618
+ Stream.EmitRecord (Code, Record);
1619
+ }
1620
+
1621
+ static void WriteUseListBlock (const Function *F, ValueEnumerator &VE,
1622
+ BitstreamWriter &Stream) {
1623
+ auto hasMore = [&]() {
1624
+ return !VE.UseListOrders .empty () && VE.UseListOrders .back ().F == F;
1625
+ };
1626
+ if (!hasMore ())
1627
+ // Nothing to do.
1628
+ return ;
1629
+
1630
+ Stream.EnterSubblock (bitc::USELIST_BLOCK_ID, 3 );
1631
+ while (hasMore ()) {
1632
+ WriteUseList (VE, std::move (VE.UseListOrders .back ()), Stream);
1633
+ VE.UseListOrders .pop_back ();
1634
+ }
1635
+ Stream.ExitBlock ();
1636
+ }
1637
+
1605
1638
// / WriteFunction - Emit a function body to the module stream.
1606
1639
static void WriteFunction (const Function &F, ValueEnumerator &VE,
1607
1640
BitstreamWriter &Stream) {
@@ -1670,6 +1703,8 @@ static void WriteFunction(const Function &F, ValueEnumerator &VE,
1670
1703
1671
1704
if (NeedsMetadataAttachment)
1672
1705
WriteMetadataAttachment (F, VE, Stream);
1706
+ if (shouldPreserveBitcodeUseListOrder ())
1707
+ WriteUseListBlock (&F, VE, Stream);
1673
1708
VE.purgeFunction ();
1674
1709
Stream.ExitBlock ();
1675
1710
}
@@ -1835,98 +1870,6 @@ static void WriteBlockInfo(const ValueEnumerator &VE, BitstreamWriter &Stream) {
1835
1870
Stream.ExitBlock ();
1836
1871
}
1837
1872
1838
- // Sort the Users based on the order in which the reader parses the bitcode
1839
- // file.
1840
- static bool bitcodereader_order (const User *lhs, const User *rhs) {
1841
- // TODO: Implement.
1842
- return true ;
1843
- }
1844
-
1845
- static void WriteUseList (const Value *V, const ValueEnumerator &VE,
1846
- BitstreamWriter &Stream) {
1847
-
1848
- // One or zero uses can't get out of order.
1849
- if (V->use_empty () || V->hasNUses (1 ))
1850
- return ;
1851
-
1852
- // Make a copy of the in-memory use-list for sorting.
1853
- SmallVector<const User*, 8 > UserList (V->user_begin (), V->user_end ());
1854
-
1855
- // Sort the copy based on the order read by the BitcodeReader.
1856
- std::sort (UserList.begin (), UserList.end (), bitcodereader_order);
1857
-
1858
- // TODO: Generate a diff between the BitcodeWriter in-memory use-list and the
1859
- // sorted list (i.e., the expected BitcodeReader in-memory use-list).
1860
-
1861
- // TODO: Emit the USELIST_CODE_ENTRYs.
1862
- }
1863
-
1864
- static void WriteFunctionUseList (const Function *F, ValueEnumerator &VE,
1865
- BitstreamWriter &Stream) {
1866
- VE.incorporateFunction (*F);
1867
-
1868
- for (Function::const_arg_iterator AI = F->arg_begin (), AE = F->arg_end ();
1869
- AI != AE; ++AI)
1870
- WriteUseList (AI, VE, Stream);
1871
- for (Function::const_iterator BB = F->begin (), FE = F->end (); BB != FE;
1872
- ++BB) {
1873
- WriteUseList (BB, VE, Stream);
1874
- for (BasicBlock::const_iterator II = BB->begin (), IE = BB->end (); II != IE;
1875
- ++II) {
1876
- WriteUseList (II, VE, Stream);
1877
- for (User::const_op_iterator OI = II->op_begin (), E = II->op_end ();
1878
- OI != E; ++OI) {
1879
- if ((isa<Constant>(*OI) && !isa<GlobalValue>(*OI)) ||
1880
- isa<InlineAsm>(*OI))
1881
- WriteUseList (*OI, VE, Stream);
1882
- }
1883
- }
1884
- }
1885
- VE.purgeFunction ();
1886
- }
1887
-
1888
- // Emit use-lists.
1889
- static void WriteModuleUseLists (const Module *M, ValueEnumerator &VE,
1890
- BitstreamWriter &Stream) {
1891
- Stream.EnterSubblock (bitc::USELIST_BLOCK_ID, 3 );
1892
-
1893
- // XXX: this modifies the module, but in a way that should never change the
1894
- // behavior of any pass or codegen in LLVM. The problem is that GVs may
1895
- // contain entries in the use_list that do not exist in the Module and are
1896
- // not stored in the .bc file.
1897
- for (Module::const_global_iterator I = M->global_begin (), E = M->global_end ();
1898
- I != E; ++I)
1899
- I->removeDeadConstantUsers ();
1900
-
1901
- // Write the global variables.
1902
- for (Module::const_global_iterator GI = M->global_begin (),
1903
- GE = M->global_end (); GI != GE; ++GI) {
1904
- WriteUseList (GI, VE, Stream);
1905
-
1906
- // Write the global variable initializers.
1907
- if (GI->hasInitializer ())
1908
- WriteUseList (GI->getInitializer (), VE, Stream);
1909
- }
1910
-
1911
- // Write the functions.
1912
- for (Module::const_iterator FI = M->begin (), FE = M->end (); FI != FE; ++FI) {
1913
- WriteUseList (FI, VE, Stream);
1914
- if (!FI->isDeclaration ())
1915
- WriteFunctionUseList (FI, VE, Stream);
1916
- if (FI->hasPrefixData ())
1917
- WriteUseList (FI->getPrefixData (), VE, Stream);
1918
- }
1919
-
1920
- // Write the aliases.
1921
- for (Module::const_alias_iterator AI = M->alias_begin (), AE = M->alias_end ();
1922
- AI != AE; ++AI) {
1923
- WriteUseList (AI, VE, Stream);
1924
- WriteUseList (AI->getAliasee (), VE, Stream);
1925
- }
1926
-
1927
- Stream.ExitBlock ();
1928
- }
1929
-
1930
1873
// / WriteModule - Emit the specified module to the bitstream.
1931
1874
static void WriteModule (const Module *M, BitstreamWriter &Stream) {
1932
1875
Stream.EnterSubblock (bitc::MODULE_BLOCK_ID, 3 );
@@ -1969,9 +1912,9 @@ static void WriteModule(const Module *M, BitstreamWriter &Stream) {
1969
1912
// Emit names for globals/functions etc.
1970
1913
WriteValueSymbolTable (M->getValueSymbolTable (), VE, Stream);
1971
1914
1972
- // Emit use-lists.
1915
+ // Emit module-level use-lists.
1973
1916
if (shouldPreserveBitcodeUseListOrder ())
1974
- WriteModuleUseLists (M , VE, Stream);
1917
+ WriteUseListBlock ( nullptr , VE, Stream);
1975
1918
1976
1919
// Emit function bodies.
1977
1920
for (Module::const_iterator F = M->begin (), E = M->end (); F != E; ++F)
0 commit comments