ValueEnumerator.cpp 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041
  1. //===- ValueEnumerator.cpp - Number values and types for bitcode writer ---===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file implements the ValueEnumerator class.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "ValueEnumerator.h"
  14. #include "llvm/ADT/DenseMap.h"
  15. #include "llvm/ADT/SmallVector.h"
  16. #include "llvm/Config/llvm-config.h"
  17. #include "llvm/IR/Argument.h"
  18. #include "llvm/IR/Attributes.h"
  19. #include "llvm/IR/BasicBlock.h"
  20. #include "llvm/IR/Constant.h"
  21. #include "llvm/IR/DebugInfoMetadata.h"
  22. #include "llvm/IR/DerivedTypes.h"
  23. #include "llvm/IR/Function.h"
  24. #include "llvm/IR/GlobalAlias.h"
  25. #include "llvm/IR/GlobalIFunc.h"
  26. #include "llvm/IR/GlobalObject.h"
  27. #include "llvm/IR/GlobalValue.h"
  28. #include "llvm/IR/GlobalVariable.h"
  29. #include "llvm/IR/Instruction.h"
  30. #include "llvm/IR/Instructions.h"
  31. #include "llvm/IR/Metadata.h"
  32. #include "llvm/IR/Module.h"
  33. #include "llvm/IR/Type.h"
  34. #include "llvm/IR/Use.h"
  35. #include "llvm/IR/UseListOrder.h"
  36. #include "llvm/IR/User.h"
  37. #include "llvm/IR/Value.h"
  38. #include "llvm/IR/ValueSymbolTable.h"
  39. #include "llvm/Support/Casting.h"
  40. #include "llvm/Support/Compiler.h"
  41. #include "llvm/Support/Debug.h"
  42. #include "llvm/Support/MathExtras.h"
  43. #include "llvm/Support/raw_ostream.h"
  44. #include <algorithm>
  45. #include <cassert>
  46. #include <cstddef>
  47. #include <iterator>
  48. #include <tuple>
  49. #include <utility>
  50. #include <vector>
  51. using namespace llvm;
  52. namespace {
  53. struct OrderMap {
  54. DenseMap<const Value *, std::pair<unsigned, bool>> IDs;
  55. unsigned LastGlobalConstantID = 0;
  56. unsigned LastGlobalValueID = 0;
  57. OrderMap() = default;
  58. bool isGlobalConstant(unsigned ID) const {
  59. return ID <= LastGlobalConstantID;
  60. }
  61. bool isGlobalValue(unsigned ID) const {
  62. return ID <= LastGlobalValueID && !isGlobalConstant(ID);
  63. }
  64. unsigned size() const { return IDs.size(); }
  65. std::pair<unsigned, bool> &operator[](const Value *V) { return IDs[V]; }
  66. std::pair<unsigned, bool> lookup(const Value *V) const {
  67. return IDs.lookup(V);
  68. }
  69. void index(const Value *V) {
  70. // Explicitly sequence get-size and insert-value operations to avoid UB.
  71. unsigned ID = IDs.size() + 1;
  72. IDs[V].first = ID;
  73. }
  74. };
  75. } // end anonymous namespace
  76. static void orderValue(const Value *V, OrderMap &OM) {
  77. if (OM.lookup(V).first)
  78. return;
  79. if (const Constant *C = dyn_cast<Constant>(V))
  80. if (C->getNumOperands() && !isa<GlobalValue>(C))
  81. for (const Value *Op : C->operands())
  82. if (!isa<BasicBlock>(Op) && !isa<GlobalValue>(Op))
  83. orderValue(Op, OM);
  84. // Note: we cannot cache this lookup above, since inserting into the map
  85. // changes the map's size, and thus affects the other IDs.
  86. OM.index(V);
  87. }
  88. static OrderMap orderModule(const Module &M) {
  89. // This needs to match the order used by ValueEnumerator::ValueEnumerator()
  90. // and ValueEnumerator::incorporateFunction().
  91. OrderMap OM;
  92. // In the reader, initializers of GlobalValues are set *after* all the
  93. // globals have been read. Rather than awkwardly modeling this behaviour
  94. // directly in predictValueUseListOrderImpl(), just assign IDs to
  95. // initializers of GlobalValues before GlobalValues themselves to model this
  96. // implicitly.
  97. for (const GlobalVariable &G : M.globals())
  98. if (G.hasInitializer())
  99. if (!isa<GlobalValue>(G.getInitializer()))
  100. orderValue(G.getInitializer(), OM);
  101. for (const GlobalAlias &A : M.aliases())
  102. if (!isa<GlobalValue>(A.getAliasee()))
  103. orderValue(A.getAliasee(), OM);
  104. for (const GlobalIFunc &I : M.ifuncs())
  105. if (!isa<GlobalValue>(I.getResolver()))
  106. orderValue(I.getResolver(), OM);
  107. for (const Function &F : M) {
  108. for (const Use &U : F.operands())
  109. if (!isa<GlobalValue>(U.get()))
  110. orderValue(U.get(), OM);
  111. }
  112. OM.LastGlobalConstantID = OM.size();
  113. // Initializers of GlobalValues are processed in
  114. // BitcodeReader::ResolveGlobalAndAliasInits(). Match the order there rather
  115. // than ValueEnumerator, and match the code in predictValueUseListOrderImpl()
  116. // by giving IDs in reverse order.
  117. //
  118. // Since GlobalValues never reference each other directly (just through
  119. // initializers), their relative IDs only matter for determining order of
  120. // uses in their initializers.
  121. for (const Function &F : M)
  122. orderValue(&F, OM);
  123. for (const GlobalAlias &A : M.aliases())
  124. orderValue(&A, OM);
  125. for (const GlobalIFunc &I : M.ifuncs())
  126. orderValue(&I, OM);
  127. for (const GlobalVariable &G : M.globals())
  128. orderValue(&G, OM);
  129. OM.LastGlobalValueID = OM.size();
  130. for (const Function &F : M) {
  131. if (F.isDeclaration())
  132. continue;
  133. // Here we need to match the union of ValueEnumerator::incorporateFunction()
  134. // and WriteFunction(). Basic blocks are implicitly declared before
  135. // anything else (by declaring their size).
  136. for (const BasicBlock &BB : F)
  137. orderValue(&BB, OM);
  138. for (const Argument &A : F.args())
  139. orderValue(&A, OM);
  140. for (const BasicBlock &BB : F)
  141. for (const Instruction &I : BB)
  142. for (const Value *Op : I.operands())
  143. if ((isa<Constant>(*Op) && !isa<GlobalValue>(*Op)) ||
  144. isa<InlineAsm>(*Op))
  145. orderValue(Op, OM);
  146. for (const BasicBlock &BB : F)
  147. for (const Instruction &I : BB)
  148. orderValue(&I, OM);
  149. }
  150. return OM;
  151. }
  152. static void predictValueUseListOrderImpl(const Value *V, const Function *F,
  153. unsigned ID, const OrderMap &OM,
  154. UseListOrderStack &Stack) {
  155. // Predict use-list order for this one.
  156. using Entry = std::pair<const Use *, unsigned>;
  157. SmallVector<Entry, 64> List;
  158. for (const Use &U : V->uses())
  159. // Check if this user will be serialized.
  160. if (OM.lookup(U.getUser()).first)
  161. List.push_back(std::make_pair(&U, List.size()));
  162. if (List.size() < 2)
  163. // We may have lost some users.
  164. return;
  165. bool IsGlobalValue = OM.isGlobalValue(ID);
  166. llvm::sort(List.begin(), List.end(), [&](const Entry &L, const Entry &R) {
  167. const Use *LU = L.first;
  168. const Use *RU = R.first;
  169. if (LU == RU)
  170. return false;
  171. auto LID = OM.lookup(LU->getUser()).first;
  172. auto RID = OM.lookup(RU->getUser()).first;
  173. // Global values are processed in reverse order.
  174. //
  175. // Moreover, initializers of GlobalValues are set *after* all the globals
  176. // have been read (despite having earlier IDs). Rather than awkwardly
  177. // modeling this behaviour here, orderModule() has assigned IDs to
  178. // initializers of GlobalValues before GlobalValues themselves.
  179. if (OM.isGlobalValue(LID) && OM.isGlobalValue(RID))
  180. return LID < RID;
  181. // If ID is 4, then expect: 7 6 5 1 2 3.
  182. if (LID < RID) {
  183. if (RID <= ID)
  184. if (!IsGlobalValue) // GlobalValue uses don't get reversed.
  185. return true;
  186. return false;
  187. }
  188. if (RID < LID) {
  189. if (LID <= ID)
  190. if (!IsGlobalValue) // GlobalValue uses don't get reversed.
  191. return false;
  192. return true;
  193. }
  194. // LID and RID are equal, so we have different operands of the same user.
  195. // Assume operands are added in order for all instructions.
  196. if (LID <= ID)
  197. if (!IsGlobalValue) // GlobalValue uses don't get reversed.
  198. return LU->getOperandNo() < RU->getOperandNo();
  199. return LU->getOperandNo() > RU->getOperandNo();
  200. });
  201. if (std::is_sorted(
  202. List.begin(), List.end(),
  203. [](const Entry &L, const Entry &R) { return L.second < R.second; }))
  204. // Order is already correct.
  205. return;
  206. // Store the shuffle.
  207. Stack.emplace_back(V, F, List.size());
  208. assert(List.size() == Stack.back().Shuffle.size() && "Wrong size");
  209. for (size_t I = 0, E = List.size(); I != E; ++I)
  210. Stack.back().Shuffle[I] = List[I].second;
  211. }
  212. static void predictValueUseListOrder(const Value *V, const Function *F,
  213. OrderMap &OM, UseListOrderStack &Stack) {
  214. auto &IDPair = OM[V];
  215. assert(IDPair.first && "Unmapped value");
  216. if (IDPair.second)
  217. // Already predicted.
  218. return;
  219. // Do the actual prediction.
  220. IDPair.second = true;
  221. if (!V->use_empty() && std::next(V->use_begin()) != V->use_end())
  222. predictValueUseListOrderImpl(V, F, IDPair.first, OM, Stack);
  223. // Recursive descent into constants.
  224. if (const Constant *C = dyn_cast<Constant>(V))
  225. if (C->getNumOperands()) // Visit GlobalValues.
  226. for (const Value *Op : C->operands())
  227. if (isa<Constant>(Op)) // Visit GlobalValues.
  228. predictValueUseListOrder(Op, F, OM, Stack);
  229. }
  230. static UseListOrderStack predictUseListOrder(const Module &M) {
  231. OrderMap OM = orderModule(M);
  232. // Use-list orders need to be serialized after all the users have been added
  233. // to a value, or else the shuffles will be incomplete. Store them per
  234. // function in a stack.
  235. //
  236. // Aside from function order, the order of values doesn't matter much here.
  237. UseListOrderStack Stack;
  238. // We want to visit the functions backward now so we can list function-local
  239. // constants in the last Function they're used in. Module-level constants
  240. // have already been visited above.
  241. for (auto I = M.rbegin(), E = M.rend(); I != E; ++I) {
  242. const Function &F = *I;
  243. if (F.isDeclaration())
  244. continue;
  245. for (const BasicBlock &BB : F)
  246. predictValueUseListOrder(&BB, &F, OM, Stack);
  247. for (const Argument &A : F.args())
  248. predictValueUseListOrder(&A, &F, OM, Stack);
  249. for (const BasicBlock &BB : F)
  250. for (const Instruction &I : BB)
  251. for (const Value *Op : I.operands())
  252. if (isa<Constant>(*Op) || isa<InlineAsm>(*Op)) // Visit GlobalValues.
  253. predictValueUseListOrder(Op, &F, OM, Stack);
  254. for (const BasicBlock &BB : F)
  255. for (const Instruction &I : BB)
  256. predictValueUseListOrder(&I, &F, OM, Stack);
  257. }
  258. // Visit globals last, since the module-level use-list block will be seen
  259. // before the function bodies are processed.
  260. for (const GlobalVariable &G : M.globals())
  261. predictValueUseListOrder(&G, nullptr, OM, Stack);
  262. for (const Function &F : M)
  263. predictValueUseListOrder(&F, nullptr, OM, Stack);
  264. for (const GlobalAlias &A : M.aliases())
  265. predictValueUseListOrder(&A, nullptr, OM, Stack);
  266. for (const GlobalIFunc &I : M.ifuncs())
  267. predictValueUseListOrder(&I, nullptr, OM, Stack);
  268. for (const GlobalVariable &G : M.globals())
  269. if (G.hasInitializer())
  270. predictValueUseListOrder(G.getInitializer(), nullptr, OM, Stack);
  271. for (const GlobalAlias &A : M.aliases())
  272. predictValueUseListOrder(A.getAliasee(), nullptr, OM, Stack);
  273. for (const GlobalIFunc &I : M.ifuncs())
  274. predictValueUseListOrder(I.getResolver(), nullptr, OM, Stack);
  275. for (const Function &F : M) {
  276. for (const Use &U : F.operands())
  277. predictValueUseListOrder(U.get(), nullptr, OM, Stack);
  278. }
  279. return Stack;
  280. }
  281. static bool isIntOrIntVectorValue(const std::pair<const Value*, unsigned> &V) {
  282. return V.first->getType()->isIntOrIntVectorTy();
  283. }
  284. ValueEnumerator::ValueEnumerator(const Module &M,
  285. bool ShouldPreserveUseListOrder)
  286. : ShouldPreserveUseListOrder(ShouldPreserveUseListOrder) {
  287. if (ShouldPreserveUseListOrder)
  288. UseListOrders = predictUseListOrder(M);
  289. // Enumerate the global variables.
  290. for (const GlobalVariable &GV : M.globals())
  291. EnumerateValue(&GV);
  292. // Enumerate the functions.
  293. for (const Function & F : M) {
  294. EnumerateValue(&F);
  295. EnumerateAttributes(F.getAttributes());
  296. }
  297. // Enumerate the aliases.
  298. for (const GlobalAlias &GA : M.aliases())
  299. EnumerateValue(&GA);
  300. // Enumerate the ifuncs.
  301. for (const GlobalIFunc &GIF : M.ifuncs())
  302. EnumerateValue(&GIF);
  303. // Remember what is the cutoff between globalvalue's and other constants.
  304. unsigned FirstConstant = Values.size();
  305. // Enumerate the global variable initializers and attributes.
  306. for (const GlobalVariable &GV : M.globals()) {
  307. if (GV.hasInitializer())
  308. EnumerateValue(GV.getInitializer());
  309. if (GV.hasAttributes())
  310. EnumerateAttributes(GV.getAttributesAsList(AttributeList::FunctionIndex));
  311. }
  312. // Enumerate the aliasees.
  313. for (const GlobalAlias &GA : M.aliases())
  314. EnumerateValue(GA.getAliasee());
  315. // Enumerate the ifunc resolvers.
  316. for (const GlobalIFunc &GIF : M.ifuncs())
  317. EnumerateValue(GIF.getResolver());
  318. // Enumerate any optional Function data.
  319. for (const Function &F : M)
  320. for (const Use &U : F.operands())
  321. EnumerateValue(U.get());
  322. // Enumerate the metadata type.
  323. //
  324. // TODO: Move this to ValueEnumerator::EnumerateOperandType() once bitcode
  325. // only encodes the metadata type when it's used as a value.
  326. EnumerateType(Type::getMetadataTy(M.getContext()));
  327. // Insert constants and metadata that are named at module level into the slot
  328. // pool so that the module symbol table can refer to them...
  329. EnumerateValueSymbolTable(M.getValueSymbolTable());
  330. EnumerateNamedMetadata(M);
  331. SmallVector<std::pair<unsigned, MDNode *>, 8> MDs;
  332. for (const GlobalVariable &GV : M.globals()) {
  333. MDs.clear();
  334. GV.getAllMetadata(MDs);
  335. for (const auto &I : MDs)
  336. // FIXME: Pass GV to EnumerateMetadata and arrange for the bitcode writer
  337. // to write metadata to the global variable's own metadata block
  338. // (PR28134).
  339. EnumerateMetadata(nullptr, I.second);
  340. }
  341. // Enumerate types used by function bodies and argument lists.
  342. for (const Function &F : M) {
  343. for (const Argument &A : F.args())
  344. EnumerateType(A.getType());
  345. // Enumerate metadata attached to this function.
  346. MDs.clear();
  347. F.getAllMetadata(MDs);
  348. for (const auto &I : MDs)
  349. EnumerateMetadata(F.isDeclaration() ? nullptr : &F, I.second);
  350. for (const BasicBlock &BB : F)
  351. for (const Instruction &I : BB) {
  352. for (const Use &Op : I.operands()) {
  353. auto *MD = dyn_cast<MetadataAsValue>(&Op);
  354. if (!MD) {
  355. EnumerateOperandType(Op);
  356. continue;
  357. }
  358. // Local metadata is enumerated during function-incorporation.
  359. if (isa<LocalAsMetadata>(MD->getMetadata()))
  360. continue;
  361. EnumerateMetadata(&F, MD->getMetadata());
  362. }
  363. EnumerateType(I.getType());
  364. if (const CallInst *CI = dyn_cast<CallInst>(&I))
  365. EnumerateAttributes(CI->getAttributes());
  366. else if (const InvokeInst *II = dyn_cast<InvokeInst>(&I))
  367. EnumerateAttributes(II->getAttributes());
  368. // Enumerate metadata attached with this instruction.
  369. MDs.clear();
  370. I.getAllMetadataOtherThanDebugLoc(MDs);
  371. for (unsigned i = 0, e = MDs.size(); i != e; ++i)
  372. EnumerateMetadata(&F, MDs[i].second);
  373. // Don't enumerate the location directly -- it has a special record
  374. // type -- but enumerate its operands.
  375. if (DILocation *L = I.getDebugLoc())
  376. for (const Metadata *Op : L->operands())
  377. EnumerateMetadata(&F, Op);
  378. }
  379. }
  380. // Optimize constant ordering.
  381. OptimizeConstants(FirstConstant, Values.size());
  382. // Organize metadata ordering.
  383. organizeMetadata();
  384. }
  385. unsigned ValueEnumerator::getInstructionID(const Instruction *Inst) const {
  386. InstructionMapType::const_iterator I = InstructionMap.find(Inst);
  387. assert(I != InstructionMap.end() && "Instruction is not mapped!");
  388. return I->second;
  389. }
  390. unsigned ValueEnumerator::getComdatID(const Comdat *C) const {
  391. unsigned ComdatID = Comdats.idFor(C);
  392. assert(ComdatID && "Comdat not found!");
  393. return ComdatID;
  394. }
  395. void ValueEnumerator::setInstructionID(const Instruction *I) {
  396. InstructionMap[I] = InstructionCount++;
  397. }
  398. unsigned ValueEnumerator::getValueID(const Value *V) const {
  399. if (auto *MD = dyn_cast<MetadataAsValue>(V))
  400. return getMetadataID(MD->getMetadata());
  401. ValueMapType::const_iterator I = ValueMap.find(V);
  402. assert(I != ValueMap.end() && "Value not in slotcalculator!");
  403. return I->second-1;
  404. }
  405. #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  406. LLVM_DUMP_METHOD void ValueEnumerator::dump() const {
  407. print(dbgs(), ValueMap, "Default");
  408. dbgs() << '\n';
  409. print(dbgs(), MetadataMap, "MetaData");
  410. dbgs() << '\n';
  411. }
  412. #endif
  413. void ValueEnumerator::print(raw_ostream &OS, const ValueMapType &Map,
  414. const char *Name) const {
  415. OS << "Map Name: " << Name << "\n";
  416. OS << "Size: " << Map.size() << "\n";
  417. for (ValueMapType::const_iterator I = Map.begin(),
  418. E = Map.end(); I != E; ++I) {
  419. const Value *V = I->first;
  420. if (V->hasName())
  421. OS << "Value: " << V->getName();
  422. else
  423. OS << "Value: [null]\n";
  424. V->print(errs());
  425. errs() << '\n';
  426. OS << " Uses(" << std::distance(V->use_begin(),V->use_end()) << "):";
  427. for (const Use &U : V->uses()) {
  428. if (&U != &*V->use_begin())
  429. OS << ",";
  430. if(U->hasName())
  431. OS << " " << U->getName();
  432. else
  433. OS << " [null]";
  434. }
  435. OS << "\n\n";
  436. }
  437. }
  438. void ValueEnumerator::print(raw_ostream &OS, const MetadataMapType &Map,
  439. const char *Name) const {
  440. OS << "Map Name: " << Name << "\n";
  441. OS << "Size: " << Map.size() << "\n";
  442. for (auto I = Map.begin(), E = Map.end(); I != E; ++I) {
  443. const Metadata *MD = I->first;
  444. OS << "Metadata: slot = " << I->second.ID << "\n";
  445. OS << "Metadata: function = " << I->second.F << "\n";
  446. MD->print(OS);
  447. OS << "\n";
  448. }
  449. }
  450. /// OptimizeConstants - Reorder constant pool for denser encoding.
  451. void ValueEnumerator::OptimizeConstants(unsigned CstStart, unsigned CstEnd) {
  452. if (CstStart == CstEnd || CstStart+1 == CstEnd) return;
  453. if (ShouldPreserveUseListOrder)
  454. // Optimizing constants makes the use-list order difficult to predict.
  455. // Disable it for now when trying to preserve the order.
  456. return;
  457. std::stable_sort(Values.begin() + CstStart, Values.begin() + CstEnd,
  458. [this](const std::pair<const Value *, unsigned> &LHS,
  459. const std::pair<const Value *, unsigned> &RHS) {
  460. // Sort by plane.
  461. if (LHS.first->getType() != RHS.first->getType())
  462. return getTypeID(LHS.first->getType()) < getTypeID(RHS.first->getType());
  463. // Then by frequency.
  464. return LHS.second > RHS.second;
  465. });
  466. // Ensure that integer and vector of integer constants are at the start of the
  467. // constant pool. This is important so that GEP structure indices come before
  468. // gep constant exprs.
  469. std::stable_partition(Values.begin() + CstStart, Values.begin() + CstEnd,
  470. isIntOrIntVectorValue);
  471. // Rebuild the modified portion of ValueMap.
  472. for (; CstStart != CstEnd; ++CstStart)
  473. ValueMap[Values[CstStart].first] = CstStart+1;
  474. }
  475. /// EnumerateValueSymbolTable - Insert all of the values in the specified symbol
  476. /// table into the values table.
  477. void ValueEnumerator::EnumerateValueSymbolTable(const ValueSymbolTable &VST) {
  478. for (ValueSymbolTable::const_iterator VI = VST.begin(), VE = VST.end();
  479. VI != VE; ++VI)
  480. EnumerateValue(VI->getValue());
  481. }
  482. /// Insert all of the values referenced by named metadata in the specified
  483. /// module.
  484. void ValueEnumerator::EnumerateNamedMetadata(const Module &M) {
  485. for (const auto &I : M.named_metadata())
  486. EnumerateNamedMDNode(&I);
  487. }
  488. void ValueEnumerator::EnumerateNamedMDNode(const NamedMDNode *MD) {
  489. for (unsigned i = 0, e = MD->getNumOperands(); i != e; ++i)
  490. EnumerateMetadata(nullptr, MD->getOperand(i));
  491. }
  492. unsigned ValueEnumerator::getMetadataFunctionID(const Function *F) const {
  493. return F ? getValueID(F) + 1 : 0;
  494. }
  495. void ValueEnumerator::EnumerateMetadata(const Function *F, const Metadata *MD) {
  496. EnumerateMetadata(getMetadataFunctionID(F), MD);
  497. }
  498. void ValueEnumerator::EnumerateFunctionLocalMetadata(
  499. const Function &F, const LocalAsMetadata *Local) {
  500. EnumerateFunctionLocalMetadata(getMetadataFunctionID(&F), Local);
  501. }
  502. void ValueEnumerator::dropFunctionFromMetadata(
  503. MetadataMapType::value_type &FirstMD) {
  504. SmallVector<const MDNode *, 64> Worklist;
  505. auto push = [&Worklist](MetadataMapType::value_type &MD) {
  506. auto &Entry = MD.second;
  507. // Nothing to do if this metadata isn't tagged.
  508. if (!Entry.F)
  509. return;
  510. // Drop the function tag.
  511. Entry.F = 0;
  512. // If this is has an ID and is an MDNode, then its operands have entries as
  513. // well. We need to drop the function from them too.
  514. if (Entry.ID)
  515. if (auto *N = dyn_cast<MDNode>(MD.first))
  516. Worklist.push_back(N);
  517. };
  518. push(FirstMD);
  519. while (!Worklist.empty())
  520. for (const Metadata *Op : Worklist.pop_back_val()->operands()) {
  521. if (!Op)
  522. continue;
  523. auto MD = MetadataMap.find(Op);
  524. if (MD != MetadataMap.end())
  525. push(*MD);
  526. }
  527. }
  528. void ValueEnumerator::EnumerateMetadata(unsigned F, const Metadata *MD) {
  529. // It's vital for reader efficiency that uniqued subgraphs are done in
  530. // post-order; it's expensive when their operands have forward references.
  531. // If a distinct node is referenced from a uniqued node, it'll be delayed
  532. // until the uniqued subgraph has been completely traversed.
  533. SmallVector<const MDNode *, 32> DelayedDistinctNodes;
  534. // Start by enumerating MD, and then work through its transitive operands in
  535. // post-order. This requires a depth-first search.
  536. SmallVector<std::pair<const MDNode *, MDNode::op_iterator>, 32> Worklist;
  537. if (const MDNode *N = enumerateMetadataImpl(F, MD))
  538. Worklist.push_back(std::make_pair(N, N->op_begin()));
  539. while (!Worklist.empty()) {
  540. const MDNode *N = Worklist.back().first;
  541. // Enumerate operands until we hit a new node. We need to traverse these
  542. // nodes' operands before visiting the rest of N's operands.
  543. MDNode::op_iterator I = std::find_if(
  544. Worklist.back().second, N->op_end(),
  545. [&](const Metadata *MD) { return enumerateMetadataImpl(F, MD); });
  546. if (I != N->op_end()) {
  547. auto *Op = cast<MDNode>(*I);
  548. Worklist.back().second = ++I;
  549. // Delay traversing Op if it's a distinct node and N is uniqued.
  550. if (Op->isDistinct() && !N->isDistinct())
  551. DelayedDistinctNodes.push_back(Op);
  552. else
  553. Worklist.push_back(std::make_pair(Op, Op->op_begin()));
  554. continue;
  555. }
  556. // All the operands have been visited. Now assign an ID.
  557. Worklist.pop_back();
  558. MDs.push_back(N);
  559. MetadataMap[N].ID = MDs.size();
  560. // Flush out any delayed distinct nodes; these are all the distinct nodes
  561. // that are leaves in last uniqued subgraph.
  562. if (Worklist.empty() || Worklist.back().first->isDistinct()) {
  563. for (const MDNode *N : DelayedDistinctNodes)
  564. Worklist.push_back(std::make_pair(N, N->op_begin()));
  565. DelayedDistinctNodes.clear();
  566. }
  567. }
  568. }
  569. const MDNode *ValueEnumerator::enumerateMetadataImpl(unsigned F, const Metadata *MD) {
  570. if (!MD)
  571. return nullptr;
  572. assert(
  573. (isa<MDNode>(MD) || isa<MDString>(MD) || isa<ConstantAsMetadata>(MD)) &&
  574. "Invalid metadata kind");
  575. auto Insertion = MetadataMap.insert(std::make_pair(MD, MDIndex(F)));
  576. MDIndex &Entry = Insertion.first->second;
  577. if (!Insertion.second) {
  578. // Already mapped. If F doesn't match the function tag, drop it.
  579. if (Entry.hasDifferentFunction(F))
  580. dropFunctionFromMetadata(*Insertion.first);
  581. return nullptr;
  582. }
  583. // Don't assign IDs to metadata nodes.
  584. if (auto *N = dyn_cast<MDNode>(MD))
  585. return N;
  586. // Save the metadata.
  587. MDs.push_back(MD);
  588. Entry.ID = MDs.size();
  589. // Enumerate the constant, if any.
  590. if (auto *C = dyn_cast<ConstantAsMetadata>(MD))
  591. EnumerateValue(C->getValue());
  592. return nullptr;
  593. }
  594. /// EnumerateFunctionLocalMetadataa - Incorporate function-local metadata
  595. /// information reachable from the metadata.
  596. void ValueEnumerator::EnumerateFunctionLocalMetadata(
  597. unsigned F, const LocalAsMetadata *Local) {
  598. assert(F && "Expected a function");
  599. // Check to see if it's already in!
  600. MDIndex &Index = MetadataMap[Local];
  601. if (Index.ID) {
  602. assert(Index.F == F && "Expected the same function");
  603. return;
  604. }
  605. MDs.push_back(Local);
  606. Index.F = F;
  607. Index.ID = MDs.size();
  608. EnumerateValue(Local->getValue());
  609. }
  610. static unsigned getMetadataTypeOrder(const Metadata *MD) {
  611. // Strings are emitted in bulk and must come first.
  612. if (isa<MDString>(MD))
  613. return 0;
  614. // ConstantAsMetadata doesn't reference anything. We may as well shuffle it
  615. // to the front since we can detect it.
  616. auto *N = dyn_cast<MDNode>(MD);
  617. if (!N)
  618. return 1;
  619. // The reader is fast forward references for distinct node operands, but slow
  620. // when uniqued operands are unresolved.
  621. return N->isDistinct() ? 2 : 3;
  622. }
  623. void ValueEnumerator::organizeMetadata() {
  624. assert(MetadataMap.size() == MDs.size() &&
  625. "Metadata map and vector out of sync");
  626. if (MDs.empty())
  627. return;
  628. // Copy out the index information from MetadataMap in order to choose a new
  629. // order.
  630. SmallVector<MDIndex, 64> Order;
  631. Order.reserve(MetadataMap.size());
  632. for (const Metadata *MD : MDs)
  633. Order.push_back(MetadataMap.lookup(MD));
  634. // Partition:
  635. // - by function, then
  636. // - by isa<MDString>
  637. // and then sort by the original/current ID. Since the IDs are guaranteed to
  638. // be unique, the result of std::sort will be deterministic. There's no need
  639. // for std::stable_sort.
  640. llvm::sort(Order.begin(), Order.end(), [this](MDIndex LHS, MDIndex RHS) {
  641. return std::make_tuple(LHS.F, getMetadataTypeOrder(LHS.get(MDs)), LHS.ID) <
  642. std::make_tuple(RHS.F, getMetadataTypeOrder(RHS.get(MDs)), RHS.ID);
  643. });
  644. // Rebuild MDs, index the metadata ranges for each function in FunctionMDs,
  645. // and fix up MetadataMap.
  646. std::vector<const Metadata *> OldMDs = std::move(MDs);
  647. MDs.reserve(OldMDs.size());
  648. for (unsigned I = 0, E = Order.size(); I != E && !Order[I].F; ++I) {
  649. auto *MD = Order[I].get(OldMDs);
  650. MDs.push_back(MD);
  651. MetadataMap[MD].ID = I + 1;
  652. if (isa<MDString>(MD))
  653. ++NumMDStrings;
  654. }
  655. // Return early if there's nothing for the functions.
  656. if (MDs.size() == Order.size())
  657. return;
  658. // Build the function metadata ranges.
  659. MDRange R;
  660. FunctionMDs.reserve(OldMDs.size());
  661. unsigned PrevF = 0;
  662. for (unsigned I = MDs.size(), E = Order.size(), ID = MDs.size(); I != E;
  663. ++I) {
  664. unsigned F = Order[I].F;
  665. if (!PrevF) {
  666. PrevF = F;
  667. } else if (PrevF != F) {
  668. R.Last = FunctionMDs.size();
  669. std::swap(R, FunctionMDInfo[PrevF]);
  670. R.First = FunctionMDs.size();
  671. ID = MDs.size();
  672. PrevF = F;
  673. }
  674. auto *MD = Order[I].get(OldMDs);
  675. FunctionMDs.push_back(MD);
  676. MetadataMap[MD].ID = ++ID;
  677. if (isa<MDString>(MD))
  678. ++R.NumStrings;
  679. }
  680. R.Last = FunctionMDs.size();
  681. FunctionMDInfo[PrevF] = R;
  682. }
  683. void ValueEnumerator::incorporateFunctionMetadata(const Function &F) {
  684. NumModuleMDs = MDs.size();
  685. auto R = FunctionMDInfo.lookup(getValueID(&F) + 1);
  686. NumMDStrings = R.NumStrings;
  687. MDs.insert(MDs.end(), FunctionMDs.begin() + R.First,
  688. FunctionMDs.begin() + R.Last);
  689. }
  690. void ValueEnumerator::EnumerateValue(const Value *V) {
  691. assert(!V->getType()->isVoidTy() && "Can't insert void values!");
  692. assert(!isa<MetadataAsValue>(V) && "EnumerateValue doesn't handle Metadata!");
  693. // Check to see if it's already in!
  694. unsigned &ValueID = ValueMap[V];
  695. if (ValueID) {
  696. // Increment use count.
  697. Values[ValueID-1].second++;
  698. return;
  699. }
  700. if (auto *GO = dyn_cast<GlobalObject>(V))
  701. if (const Comdat *C = GO->getComdat())
  702. Comdats.insert(C);
  703. // Enumerate the type of this value.
  704. EnumerateType(V->getType());
  705. if (const Constant *C = dyn_cast<Constant>(V)) {
  706. if (isa<GlobalValue>(C)) {
  707. // Initializers for globals are handled explicitly elsewhere.
  708. } else if (C->getNumOperands()) {
  709. // If a constant has operands, enumerate them. This makes sure that if a
  710. // constant has uses (for example an array of const ints), that they are
  711. // inserted also.
  712. // We prefer to enumerate them with values before we enumerate the user
  713. // itself. This makes it more likely that we can avoid forward references
  714. // in the reader. We know that there can be no cycles in the constants
  715. // graph that don't go through a global variable.
  716. for (User::const_op_iterator I = C->op_begin(), E = C->op_end();
  717. I != E; ++I)
  718. if (!isa<BasicBlock>(*I)) // Don't enumerate BB operand to BlockAddress.
  719. EnumerateValue(*I);
  720. // Finally, add the value. Doing this could make the ValueID reference be
  721. // dangling, don't reuse it.
  722. Values.push_back(std::make_pair(V, 1U));
  723. ValueMap[V] = Values.size();
  724. return;
  725. }
  726. }
  727. // Add the value.
  728. Values.push_back(std::make_pair(V, 1U));
  729. ValueID = Values.size();
  730. }
  731. void ValueEnumerator::EnumerateType(Type *Ty) {
  732. unsigned *TypeID = &TypeMap[Ty];
  733. // We've already seen this type.
  734. if (*TypeID)
  735. return;
  736. // If it is a non-anonymous struct, mark the type as being visited so that we
  737. // don't recursively visit it. This is safe because we allow forward
  738. // references of these in the bitcode reader.
  739. if (StructType *STy = dyn_cast<StructType>(Ty))
  740. if (!STy->isLiteral())
  741. *TypeID = ~0U;
  742. // Enumerate all of the subtypes before we enumerate this type. This ensures
  743. // that the type will be enumerated in an order that can be directly built.
  744. for (Type *SubTy : Ty->subtypes())
  745. EnumerateType(SubTy);
  746. // Refresh the TypeID pointer in case the table rehashed.
  747. TypeID = &TypeMap[Ty];
  748. // Check to see if we got the pointer another way. This can happen when
  749. // enumerating recursive types that hit the base case deeper than they start.
  750. //
  751. // If this is actually a struct that we are treating as forward ref'able,
  752. // then emit the definition now that all of its contents are available.
  753. if (*TypeID && *TypeID != ~0U)
  754. return;
  755. // Add this type now that its contents are all happily enumerated.
  756. Types.push_back(Ty);
  757. *TypeID = Types.size();
  758. }
  759. // Enumerate the types for the specified value. If the value is a constant,
  760. // walk through it, enumerating the types of the constant.
  761. void ValueEnumerator::EnumerateOperandType(const Value *V) {
  762. EnumerateType(V->getType());
  763. assert(!isa<MetadataAsValue>(V) && "Unexpected metadata operand");
  764. const Constant *C = dyn_cast<Constant>(V);
  765. if (!C)
  766. return;
  767. // If this constant is already enumerated, ignore it, we know its type must
  768. // be enumerated.
  769. if (ValueMap.count(C))
  770. return;
  771. // This constant may have operands, make sure to enumerate the types in
  772. // them.
  773. for (const Value *Op : C->operands()) {
  774. // Don't enumerate basic blocks here, this happens as operands to
  775. // blockaddress.
  776. if (isa<BasicBlock>(Op))
  777. continue;
  778. EnumerateOperandType(Op);
  779. }
  780. }
  781. void ValueEnumerator::EnumerateAttributes(AttributeList PAL) {
  782. if (PAL.isEmpty()) return; // null is always 0.
  783. // Do a lookup.
  784. unsigned &Entry = AttributeListMap[PAL];
  785. if (Entry == 0) {
  786. // Never saw this before, add it.
  787. AttributeLists.push_back(PAL);
  788. Entry = AttributeLists.size();
  789. }
  790. // Do lookups for all attribute groups.
  791. for (unsigned i = PAL.index_begin(), e = PAL.index_end(); i != e; ++i) {
  792. AttributeSet AS = PAL.getAttributes(i);
  793. if (!AS.hasAttributes())
  794. continue;
  795. IndexAndAttrSet Pair = {i, AS};
  796. unsigned &Entry = AttributeGroupMap[Pair];
  797. if (Entry == 0) {
  798. AttributeGroups.push_back(Pair);
  799. Entry = AttributeGroups.size();
  800. }
  801. }
  802. }
  803. void ValueEnumerator::incorporateFunction(const Function &F) {
  804. InstructionCount = 0;
  805. NumModuleValues = Values.size();
  806. // Add global metadata to the function block. This doesn't include
  807. // LocalAsMetadata.
  808. incorporateFunctionMetadata(F);
  809. // Adding function arguments to the value table.
  810. for (const auto &I : F.args())
  811. EnumerateValue(&I);
  812. FirstFuncConstantID = Values.size();
  813. // Add all function-level constants to the value table.
  814. for (const BasicBlock &BB : F) {
  815. for (const Instruction &I : BB)
  816. for (const Use &OI : I.operands()) {
  817. if ((isa<Constant>(OI) && !isa<GlobalValue>(OI)) || isa<InlineAsm>(OI))
  818. EnumerateValue(OI);
  819. }
  820. BasicBlocks.push_back(&BB);
  821. ValueMap[&BB] = BasicBlocks.size();
  822. }
  823. // Optimize the constant layout.
  824. OptimizeConstants(FirstFuncConstantID, Values.size());
  825. // Add the function's parameter attributes so they are available for use in
  826. // the function's instruction.
  827. EnumerateAttributes(F.getAttributes());
  828. FirstInstID = Values.size();
  829. SmallVector<LocalAsMetadata *, 8> FnLocalMDVector;
  830. // Add all of the instructions.
  831. for (const BasicBlock &BB : F) {
  832. for (const Instruction &I : BB) {
  833. for (const Use &OI : I.operands()) {
  834. if (auto *MD = dyn_cast<MetadataAsValue>(&OI))
  835. if (auto *Local = dyn_cast<LocalAsMetadata>(MD->getMetadata()))
  836. // Enumerate metadata after the instructions they might refer to.
  837. FnLocalMDVector.push_back(Local);
  838. }
  839. if (!I.getType()->isVoidTy())
  840. EnumerateValue(&I);
  841. }
  842. }
  843. // Add all of the function-local metadata.
  844. for (unsigned i = 0, e = FnLocalMDVector.size(); i != e; ++i) {
  845. // At this point, every local values have been incorporated, we shouldn't
  846. // have a metadata operand that references a value that hasn't been seen.
  847. assert(ValueMap.count(FnLocalMDVector[i]->getValue()) &&
  848. "Missing value for metadata operand");
  849. EnumerateFunctionLocalMetadata(F, FnLocalMDVector[i]);
  850. }
  851. }
  852. void ValueEnumerator::purgeFunction() {
  853. /// Remove purged values from the ValueMap.
  854. for (unsigned i = NumModuleValues, e = Values.size(); i != e; ++i)
  855. ValueMap.erase(Values[i].first);
  856. for (unsigned i = NumModuleMDs, e = MDs.size(); i != e; ++i)
  857. MetadataMap.erase(MDs[i]);
  858. for (unsigned i = 0, e = BasicBlocks.size(); i != e; ++i)
  859. ValueMap.erase(BasicBlocks[i]);
  860. Values.resize(NumModuleValues);
  861. MDs.resize(NumModuleMDs);
  862. BasicBlocks.clear();
  863. NumMDStrings = 0;
  864. }
  865. static void IncorporateFunctionInfoGlobalBBIDs(const Function *F,
  866. DenseMap<const BasicBlock*, unsigned> &IDMap) {
  867. unsigned Counter = 0;
  868. for (const BasicBlock &BB : *F)
  869. IDMap[&BB] = ++Counter;
  870. }
  871. /// getGlobalBasicBlockID - This returns the function-specific ID for the
  872. /// specified basic block. This is relatively expensive information, so it
  873. /// should only be used by rare constructs such as address-of-label.
  874. unsigned ValueEnumerator::getGlobalBasicBlockID(const BasicBlock *BB) const {
  875. unsigned &Idx = GlobalBasicBlockIDs[BB];
  876. if (Idx != 0)
  877. return Idx-1;
  878. IncorporateFunctionInfoGlobalBBIDs(BB->getParent(), GlobalBasicBlockIDs);
  879. return getGlobalBasicBlockID(BB);
  880. }
  881. uint64_t ValueEnumerator::computeBitsRequiredForTypeIndicies() const {
  882. return Log2_32_Ceil(getTypes().size() + 1);
  883. }