ValueEnumerator.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812
  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/STLExtras.h"
  15. #include "llvm/ADT/SmallPtrSet.h"
  16. #include "llvm/IR/Constants.h"
  17. #include "llvm/IR/DerivedTypes.h"
  18. #include "llvm/IR/Instructions.h"
  19. #include "llvm/IR/Module.h"
  20. #include "llvm/IR/UseListOrder.h"
  21. #include "llvm/IR/ValueSymbolTable.h"
  22. #include "llvm/Support/Debug.h"
  23. #include "llvm/Support/raw_ostream.h"
  24. #include <algorithm>
  25. using namespace llvm;
  26. namespace {
  27. struct OrderMap {
  28. DenseMap<const Value *, std::pair<unsigned, bool>> IDs;
  29. unsigned LastGlobalConstantID;
  30. unsigned LastGlobalValueID;
  31. OrderMap() : LastGlobalConstantID(0), LastGlobalValueID(0) {}
  32. bool isGlobalConstant(unsigned ID) const {
  33. return ID <= LastGlobalConstantID;
  34. }
  35. bool isGlobalValue(unsigned ID) const {
  36. return ID <= LastGlobalValueID && !isGlobalConstant(ID);
  37. }
  38. unsigned size() const { return IDs.size(); }
  39. std::pair<unsigned, bool> &operator[](const Value *V) { return IDs[V]; }
  40. std::pair<unsigned, bool> lookup(const Value *V) const {
  41. return IDs.lookup(V);
  42. }
  43. void index(const Value *V) {
  44. // Explicitly sequence get-size and insert-value operations to avoid UB.
  45. unsigned ID = IDs.size() + 1;
  46. IDs[V].first = ID;
  47. }
  48. };
  49. }
  50. static void orderValue(const Value *V, OrderMap &OM) {
  51. if (OM.lookup(V).first)
  52. return;
  53. if (const Constant *C = dyn_cast<Constant>(V))
  54. if (C->getNumOperands() && !isa<GlobalValue>(C))
  55. for (const Value *Op : C->operands())
  56. if (!isa<BasicBlock>(Op) && !isa<GlobalValue>(Op))
  57. orderValue(Op, OM);
  58. // Note: we cannot cache this lookup above, since inserting into the map
  59. // changes the map's size, and thus affects the other IDs.
  60. OM.index(V);
  61. }
  62. static OrderMap orderModule(const Module &M) {
  63. // This needs to match the order used by ValueEnumerator::ValueEnumerator()
  64. // and ValueEnumerator::incorporateFunction().
  65. OrderMap OM;
  66. // In the reader, initializers of GlobalValues are set *after* all the
  67. // globals have been read. Rather than awkwardly modeling this behaviour
  68. // directly in predictValueUseListOrderImpl(), just assign IDs to
  69. // initializers of GlobalValues before GlobalValues themselves to model this
  70. // implicitly.
  71. for (const GlobalVariable &G : M.globals())
  72. if (G.hasInitializer())
  73. if (!isa<GlobalValue>(G.getInitializer()))
  74. orderValue(G.getInitializer(), OM);
  75. for (const GlobalAlias &A : M.aliases())
  76. if (!isa<GlobalValue>(A.getAliasee()))
  77. orderValue(A.getAliasee(), OM);
  78. for (const Function &F : M) {
  79. if (F.hasPrefixData())
  80. if (!isa<GlobalValue>(F.getPrefixData()))
  81. orderValue(F.getPrefixData(), OM);
  82. if (F.hasPrologueData())
  83. if (!isa<GlobalValue>(F.getPrologueData()))
  84. orderValue(F.getPrologueData(), OM);
  85. }
  86. OM.LastGlobalConstantID = OM.size();
  87. // Initializers of GlobalValues are processed in
  88. // BitcodeReader::ResolveGlobalAndAliasInits(). Match the order there rather
  89. // than ValueEnumerator, and match the code in predictValueUseListOrderImpl()
  90. // by giving IDs in reverse order.
  91. //
  92. // Since GlobalValues never reference each other directly (just through
  93. // initializers), their relative IDs only matter for determining order of
  94. // uses in their initializers.
  95. for (const Function &F : M)
  96. orderValue(&F, OM);
  97. for (const GlobalAlias &A : M.aliases())
  98. orderValue(&A, OM);
  99. for (const GlobalVariable &G : M.globals())
  100. orderValue(&G, OM);
  101. OM.LastGlobalValueID = OM.size();
  102. for (const Function &F : M) {
  103. if (F.isDeclaration())
  104. continue;
  105. // Here we need to match the union of ValueEnumerator::incorporateFunction()
  106. // and WriteFunction(). Basic blocks are implicitly declared before
  107. // anything else (by declaring their size).
  108. for (const BasicBlock &BB : F)
  109. orderValue(&BB, OM);
  110. for (const Argument &A : F.args())
  111. orderValue(&A, OM);
  112. for (const BasicBlock &BB : F)
  113. for (const Instruction &I : BB)
  114. for (const Value *Op : I.operands())
  115. if ((isa<Constant>(*Op) && !isa<GlobalValue>(*Op)) ||
  116. isa<InlineAsm>(*Op))
  117. orderValue(Op, OM);
  118. for (const BasicBlock &BB : F)
  119. for (const Instruction &I : BB)
  120. orderValue(&I, OM);
  121. }
  122. return OM;
  123. }
  124. static void predictValueUseListOrderImpl(const Value *V, const Function *F,
  125. unsigned ID, const OrderMap &OM,
  126. UseListOrderStack &Stack) {
  127. // Predict use-list order for this one.
  128. typedef std::pair<const Use *, unsigned> Entry;
  129. SmallVector<Entry, 64> List;
  130. for (const Use &U : V->uses())
  131. // Check if this user will be serialized.
  132. if (OM.lookup(U.getUser()).first)
  133. List.push_back(std::make_pair(&U, List.size()));
  134. if (List.size() < 2)
  135. // We may have lost some users.
  136. return;
  137. bool IsGlobalValue = OM.isGlobalValue(ID);
  138. std::sort(List.begin(), List.end(), [&](const Entry &L, const Entry &R) {
  139. const Use *LU = L.first;
  140. const Use *RU = R.first;
  141. if (LU == RU)
  142. return false;
  143. auto LID = OM.lookup(LU->getUser()).first;
  144. auto RID = OM.lookup(RU->getUser()).first;
  145. // Global values are processed in reverse order.
  146. //
  147. // Moreover, initializers of GlobalValues are set *after* all the globals
  148. // have been read (despite having earlier IDs). Rather than awkwardly
  149. // modeling this behaviour here, orderModule() has assigned IDs to
  150. // initializers of GlobalValues before GlobalValues themselves.
  151. if (OM.isGlobalValue(LID) && OM.isGlobalValue(RID))
  152. return LID < RID;
  153. // If ID is 4, then expect: 7 6 5 1 2 3.
  154. if (LID < RID) {
  155. if (RID <= ID)
  156. if (!IsGlobalValue) // GlobalValue uses don't get reversed.
  157. return true;
  158. return false;
  159. }
  160. if (RID < LID) {
  161. if (LID <= ID)
  162. if (!IsGlobalValue) // GlobalValue uses don't get reversed.
  163. return false;
  164. return true;
  165. }
  166. // LID and RID are equal, so we have different operands of the same user.
  167. // Assume operands are added in order for all instructions.
  168. if (LID <= ID)
  169. if (!IsGlobalValue) // GlobalValue uses don't get reversed.
  170. return LU->getOperandNo() < RU->getOperandNo();
  171. return LU->getOperandNo() > RU->getOperandNo();
  172. });
  173. if (std::is_sorted(
  174. List.begin(), List.end(),
  175. [](const Entry &L, const Entry &R) { return L.second < R.second; }))
  176. // Order is already correct.
  177. return;
  178. // Store the shuffle.
  179. Stack.emplace_back(V, F, List.size());
  180. assert(List.size() == Stack.back().Shuffle.size() && "Wrong size");
  181. for (size_t I = 0, E = List.size(); I != E; ++I)
  182. Stack.back().Shuffle[I] = List[I].second;
  183. }
  184. static void predictValueUseListOrder(const Value *V, const Function *F,
  185. OrderMap &OM, UseListOrderStack &Stack) {
  186. auto &IDPair = OM[V];
  187. assert(IDPair.first && "Unmapped value");
  188. if (IDPair.second)
  189. // Already predicted.
  190. return;
  191. // Do the actual prediction.
  192. IDPair.second = true;
  193. if (!V->use_empty() && std::next(V->use_begin()) != V->use_end())
  194. predictValueUseListOrderImpl(V, F, IDPair.first, OM, Stack);
  195. // Recursive descent into constants.
  196. if (const Constant *C = dyn_cast<Constant>(V))
  197. if (C->getNumOperands()) // Visit GlobalValues.
  198. for (const Value *Op : C->operands())
  199. if (isa<Constant>(Op)) // Visit GlobalValues.
  200. predictValueUseListOrder(Op, F, OM, Stack);
  201. }
  202. static UseListOrderStack predictUseListOrder(const Module &M) {
  203. OrderMap OM = orderModule(M);
  204. // Use-list orders need to be serialized after all the users have been added
  205. // to a value, or else the shuffles will be incomplete. Store them per
  206. // function in a stack.
  207. //
  208. // Aside from function order, the order of values doesn't matter much here.
  209. UseListOrderStack Stack;
  210. // We want to visit the functions backward now so we can list function-local
  211. // constants in the last Function they're used in. Module-level constants
  212. // have already been visited above.
  213. for (auto I = M.rbegin(), E = M.rend(); I != E; ++I) {
  214. const Function &F = *I;
  215. if (F.isDeclaration())
  216. continue;
  217. for (const BasicBlock &BB : F)
  218. predictValueUseListOrder(&BB, &F, OM, Stack);
  219. for (const Argument &A : F.args())
  220. predictValueUseListOrder(&A, &F, OM, Stack);
  221. for (const BasicBlock &BB : F)
  222. for (const Instruction &I : BB)
  223. for (const Value *Op : I.operands())
  224. if (isa<Constant>(*Op) || isa<InlineAsm>(*Op)) // Visit GlobalValues.
  225. predictValueUseListOrder(Op, &F, OM, Stack);
  226. for (const BasicBlock &BB : F)
  227. for (const Instruction &I : BB)
  228. predictValueUseListOrder(&I, &F, OM, Stack);
  229. }
  230. // Visit globals last, since the module-level use-list block will be seen
  231. // before the function bodies are processed.
  232. for (const GlobalVariable &G : M.globals())
  233. predictValueUseListOrder(&G, nullptr, OM, Stack);
  234. for (const Function &F : M)
  235. predictValueUseListOrder(&F, nullptr, OM, Stack);
  236. for (const GlobalAlias &A : M.aliases())
  237. predictValueUseListOrder(&A, nullptr, OM, Stack);
  238. for (const GlobalVariable &G : M.globals())
  239. if (G.hasInitializer())
  240. predictValueUseListOrder(G.getInitializer(), nullptr, OM, Stack);
  241. for (const GlobalAlias &A : M.aliases())
  242. predictValueUseListOrder(A.getAliasee(), nullptr, OM, Stack);
  243. for (const Function &F : M) {
  244. if (F.hasPrefixData())
  245. predictValueUseListOrder(F.getPrefixData(), nullptr, OM, Stack);
  246. if (F.hasPrologueData())
  247. predictValueUseListOrder(F.getPrologueData(), nullptr, OM, Stack);
  248. }
  249. return Stack;
  250. }
  251. static bool isIntOrIntVectorValue(const std::pair<const Value*, unsigned> &V) {
  252. return V.first->getType()->isIntOrIntVectorTy();
  253. }
  254. ValueEnumerator::ValueEnumerator(const Module &M) {
  255. if (shouldPreserveBitcodeUseListOrder())
  256. UseListOrders = predictUseListOrder(M);
  257. // Enumerate the global variables.
  258. for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
  259. I != E; ++I)
  260. EnumerateValue(I);
  261. // Enumerate the functions.
  262. for (Module::const_iterator I = M.begin(), E = M.end(); I != E; ++I) {
  263. EnumerateValue(I);
  264. EnumerateAttributes(cast<Function>(I)->getAttributes());
  265. }
  266. // Enumerate the aliases.
  267. for (Module::const_alias_iterator I = M.alias_begin(), E = M.alias_end();
  268. I != E; ++I)
  269. EnumerateValue(I);
  270. // Remember what is the cutoff between globalvalue's and other constants.
  271. unsigned FirstConstant = Values.size();
  272. // Enumerate the global variable initializers.
  273. for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
  274. I != E; ++I)
  275. if (I->hasInitializer())
  276. EnumerateValue(I->getInitializer());
  277. // Enumerate the aliasees.
  278. for (Module::const_alias_iterator I = M.alias_begin(), E = M.alias_end();
  279. I != E; ++I)
  280. EnumerateValue(I->getAliasee());
  281. // Enumerate the prefix data constants.
  282. for (Module::const_iterator I = M.begin(), E = M.end(); I != E; ++I)
  283. if (I->hasPrefixData())
  284. EnumerateValue(I->getPrefixData());
  285. // Enumerate the prologue data constants.
  286. for (Module::const_iterator I = M.begin(), E = M.end(); I != E; ++I)
  287. if (I->hasPrologueData())
  288. EnumerateValue(I->getPrologueData());
  289. // Enumerate the metadata type.
  290. //
  291. // TODO: Move this to ValueEnumerator::EnumerateOperandType() once bitcode
  292. // only encodes the metadata type when it's used as a value.
  293. EnumerateType(Type::getMetadataTy(M.getContext()));
  294. // Insert constants and metadata that are named at module level into the slot
  295. // pool so that the module symbol table can refer to them...
  296. EnumerateValueSymbolTable(M.getValueSymbolTable());
  297. EnumerateNamedMetadata(M);
  298. SmallVector<std::pair<unsigned, MDNode *>, 8> MDs;
  299. // Enumerate types used by function bodies and argument lists.
  300. for (const Function &F : M) {
  301. for (const Argument &A : F.args())
  302. EnumerateType(A.getType());
  303. for (const BasicBlock &BB : F)
  304. for (const Instruction &I : BB) {
  305. for (const Use &Op : I.operands()) {
  306. auto *MD = dyn_cast<MetadataAsValue>(&Op);
  307. if (!MD) {
  308. EnumerateOperandType(Op);
  309. continue;
  310. }
  311. // Local metadata is enumerated during function-incorporation.
  312. if (isa<LocalAsMetadata>(MD->getMetadata()))
  313. continue;
  314. EnumerateMetadata(MD->getMetadata());
  315. }
  316. EnumerateType(I.getType());
  317. if (const CallInst *CI = dyn_cast<CallInst>(&I))
  318. EnumerateAttributes(CI->getAttributes());
  319. else if (const InvokeInst *II = dyn_cast<InvokeInst>(&I))
  320. EnumerateAttributes(II->getAttributes());
  321. // Enumerate metadata attached with this instruction.
  322. MDs.clear();
  323. I.getAllMetadataOtherThanDebugLoc(MDs);
  324. for (unsigned i = 0, e = MDs.size(); i != e; ++i)
  325. EnumerateMetadata(MDs[i].second);
  326. if (!I.getDebugLoc().isUnknown()) {
  327. MDNode *Scope, *IA;
  328. I.getDebugLoc().getScopeAndInlinedAt(Scope, IA, I.getContext());
  329. if (Scope) EnumerateMetadata(Scope);
  330. if (IA) EnumerateMetadata(IA);
  331. }
  332. }
  333. }
  334. // Optimize constant ordering.
  335. OptimizeConstants(FirstConstant, Values.size());
  336. }
  337. unsigned ValueEnumerator::getInstructionID(const Instruction *Inst) const {
  338. InstructionMapType::const_iterator I = InstructionMap.find(Inst);
  339. assert(I != InstructionMap.end() && "Instruction is not mapped!");
  340. return I->second;
  341. }
  342. unsigned ValueEnumerator::getComdatID(const Comdat *C) const {
  343. unsigned ComdatID = Comdats.idFor(C);
  344. assert(ComdatID && "Comdat not found!");
  345. return ComdatID;
  346. }
  347. void ValueEnumerator::setInstructionID(const Instruction *I) {
  348. InstructionMap[I] = InstructionCount++;
  349. }
  350. unsigned ValueEnumerator::getValueID(const Value *V) const {
  351. if (auto *MD = dyn_cast<MetadataAsValue>(V))
  352. return getMetadataID(MD->getMetadata());
  353. ValueMapType::const_iterator I = ValueMap.find(V);
  354. assert(I != ValueMap.end() && "Value not in slotcalculator!");
  355. return I->second-1;
  356. }
  357. unsigned ValueEnumerator::getMetadataID(const Metadata *MD) const {
  358. auto I = MDValueMap.find(MD);
  359. assert(I != MDValueMap.end() && "Metadata not in slotcalculator!");
  360. return I->second - 1;
  361. }
  362. void ValueEnumerator::dump() const {
  363. print(dbgs(), ValueMap, "Default");
  364. dbgs() << '\n';
  365. print(dbgs(), MDValueMap, "MetaData");
  366. dbgs() << '\n';
  367. }
  368. void ValueEnumerator::print(raw_ostream &OS, const ValueMapType &Map,
  369. const char *Name) const {
  370. OS << "Map Name: " << Name << "\n";
  371. OS << "Size: " << Map.size() << "\n";
  372. for (ValueMapType::const_iterator I = Map.begin(),
  373. E = Map.end(); I != E; ++I) {
  374. const Value *V = I->first;
  375. if (V->hasName())
  376. OS << "Value: " << V->getName();
  377. else
  378. OS << "Value: [null]\n";
  379. V->dump();
  380. OS << " Uses(" << std::distance(V->use_begin(),V->use_end()) << "):";
  381. for (const Use &U : V->uses()) {
  382. if (&U != &*V->use_begin())
  383. OS << ",";
  384. if(U->hasName())
  385. OS << " " << U->getName();
  386. else
  387. OS << " [null]";
  388. }
  389. OS << "\n\n";
  390. }
  391. }
  392. void ValueEnumerator::print(raw_ostream &OS, const MetadataMapType &Map,
  393. const char *Name) const {
  394. OS << "Map Name: " << Name << "\n";
  395. OS << "Size: " << Map.size() << "\n";
  396. for (auto I = Map.begin(), E = Map.end(); I != E; ++I) {
  397. const Metadata *MD = I->first;
  398. OS << "Metadata: slot = " << I->second << "\n";
  399. MD->dump();
  400. }
  401. }
  402. /// OptimizeConstants - Reorder constant pool for denser encoding.
  403. void ValueEnumerator::OptimizeConstants(unsigned CstStart, unsigned CstEnd) {
  404. if (CstStart == CstEnd || CstStart+1 == CstEnd) return;
  405. if (shouldPreserveBitcodeUseListOrder())
  406. // Optimizing constants makes the use-list order difficult to predict.
  407. // Disable it for now when trying to preserve the order.
  408. return;
  409. std::stable_sort(Values.begin() + CstStart, Values.begin() + CstEnd,
  410. [this](const std::pair<const Value *, unsigned> &LHS,
  411. const std::pair<const Value *, unsigned> &RHS) {
  412. // Sort by plane.
  413. if (LHS.first->getType() != RHS.first->getType())
  414. return getTypeID(LHS.first->getType()) < getTypeID(RHS.first->getType());
  415. // Then by frequency.
  416. return LHS.second > RHS.second;
  417. });
  418. // Ensure that integer and vector of integer constants are at the start of the
  419. // constant pool. This is important so that GEP structure indices come before
  420. // gep constant exprs.
  421. std::partition(Values.begin()+CstStart, Values.begin()+CstEnd,
  422. isIntOrIntVectorValue);
  423. // Rebuild the modified portion of ValueMap.
  424. for (; CstStart != CstEnd; ++CstStart)
  425. ValueMap[Values[CstStart].first] = CstStart+1;
  426. }
  427. /// EnumerateValueSymbolTable - Insert all of the values in the specified symbol
  428. /// table into the values table.
  429. void ValueEnumerator::EnumerateValueSymbolTable(const ValueSymbolTable &VST) {
  430. for (ValueSymbolTable::const_iterator VI = VST.begin(), VE = VST.end();
  431. VI != VE; ++VI)
  432. EnumerateValue(VI->getValue());
  433. }
  434. /// Insert all of the values referenced by named metadata in the specified
  435. /// module.
  436. void ValueEnumerator::EnumerateNamedMetadata(const Module &M) {
  437. for (Module::const_named_metadata_iterator I = M.named_metadata_begin(),
  438. E = M.named_metadata_end();
  439. I != E; ++I)
  440. EnumerateNamedMDNode(I);
  441. }
  442. void ValueEnumerator::EnumerateNamedMDNode(const NamedMDNode *MD) {
  443. for (unsigned i = 0, e = MD->getNumOperands(); i != e; ++i)
  444. EnumerateMetadata(MD->getOperand(i));
  445. }
  446. /// EnumerateMDNodeOperands - Enumerate all non-function-local values
  447. /// and types referenced by the given MDNode.
  448. void ValueEnumerator::EnumerateMDNodeOperands(const MDNode *N) {
  449. for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
  450. Metadata *MD = N->getOperand(i);
  451. if (!MD) {
  452. EnumerateType(Type::getVoidTy(N->getContext()));
  453. continue;
  454. }
  455. assert(!isa<LocalAsMetadata>(MD) && "MDNodes cannot be function-local");
  456. if (auto *C = dyn_cast<ConstantAsMetadata>(MD)) {
  457. EnumerateValue(C->getValue());
  458. continue;
  459. }
  460. EnumerateMetadata(MD);
  461. }
  462. }
  463. void ValueEnumerator::EnumerateMetadata(const Metadata *MD) {
  464. assert(
  465. (isa<MDNode>(MD) || isa<MDString>(MD) || isa<ConstantAsMetadata>(MD)) &&
  466. "Invalid metadata kind");
  467. // Insert a dummy ID to block the co-recursive call to
  468. // EnumerateMDNodeOperands() from re-visiting MD in a cyclic graph.
  469. //
  470. // Return early if there's already an ID.
  471. if (!MDValueMap.insert(std::make_pair(MD, 0)).second)
  472. return;
  473. // Visit operands first to minimize RAUW.
  474. if (auto *N = dyn_cast<MDNode>(MD))
  475. EnumerateMDNodeOperands(N);
  476. else if (auto *C = dyn_cast<ConstantAsMetadata>(MD))
  477. EnumerateValue(C->getValue());
  478. // Replace the dummy ID inserted above with the correct one. MDValueMap may
  479. // have changed by inserting operands, so we need a fresh lookup here.
  480. MDs.push_back(MD);
  481. MDValueMap[MD] = MDs.size();
  482. }
  483. /// EnumerateFunctionLocalMetadataa - Incorporate function-local metadata
  484. /// information reachable from the metadata.
  485. void ValueEnumerator::EnumerateFunctionLocalMetadata(
  486. const LocalAsMetadata *Local) {
  487. // Check to see if it's already in!
  488. unsigned &MDValueID = MDValueMap[Local];
  489. if (MDValueID)
  490. return;
  491. MDs.push_back(Local);
  492. MDValueID = MDs.size();
  493. EnumerateValue(Local->getValue());
  494. // Also, collect all function-local metadata for easy access.
  495. FunctionLocalMDs.push_back(Local);
  496. }
  497. void ValueEnumerator::EnumerateValue(const Value *V) {
  498. assert(!V->getType()->isVoidTy() && "Can't insert void values!");
  499. assert(!isa<MetadataAsValue>(V) && "EnumerateValue doesn't handle Metadata!");
  500. // Check to see if it's already in!
  501. unsigned &ValueID = ValueMap[V];
  502. if (ValueID) {
  503. // Increment use count.
  504. Values[ValueID-1].second++;
  505. return;
  506. }
  507. if (auto *GO = dyn_cast<GlobalObject>(V))
  508. if (const Comdat *C = GO->getComdat())
  509. Comdats.insert(C);
  510. // Enumerate the type of this value.
  511. EnumerateType(V->getType());
  512. if (const Constant *C = dyn_cast<Constant>(V)) {
  513. if (isa<GlobalValue>(C)) {
  514. // Initializers for globals are handled explicitly elsewhere.
  515. } else if (C->getNumOperands()) {
  516. // If a constant has operands, enumerate them. This makes sure that if a
  517. // constant has uses (for example an array of const ints), that they are
  518. // inserted also.
  519. // We prefer to enumerate them with values before we enumerate the user
  520. // itself. This makes it more likely that we can avoid forward references
  521. // in the reader. We know that there can be no cycles in the constants
  522. // graph that don't go through a global variable.
  523. for (User::const_op_iterator I = C->op_begin(), E = C->op_end();
  524. I != E; ++I)
  525. if (!isa<BasicBlock>(*I)) // Don't enumerate BB operand to BlockAddress.
  526. EnumerateValue(*I);
  527. // Finally, add the value. Doing this could make the ValueID reference be
  528. // dangling, don't reuse it.
  529. Values.push_back(std::make_pair(V, 1U));
  530. ValueMap[V] = Values.size();
  531. return;
  532. }
  533. }
  534. // Add the value.
  535. Values.push_back(std::make_pair(V, 1U));
  536. ValueID = Values.size();
  537. }
  538. void ValueEnumerator::EnumerateType(Type *Ty) {
  539. unsigned *TypeID = &TypeMap[Ty];
  540. // We've already seen this type.
  541. if (*TypeID)
  542. return;
  543. // If it is a non-anonymous struct, mark the type as being visited so that we
  544. // don't recursively visit it. This is safe because we allow forward
  545. // references of these in the bitcode reader.
  546. if (StructType *STy = dyn_cast<StructType>(Ty))
  547. if (!STy->isLiteral())
  548. *TypeID = ~0U;
  549. // Enumerate all of the subtypes before we enumerate this type. This ensures
  550. // that the type will be enumerated in an order that can be directly built.
  551. for (Type *SubTy : Ty->subtypes())
  552. EnumerateType(SubTy);
  553. // Refresh the TypeID pointer in case the table rehashed.
  554. TypeID = &TypeMap[Ty];
  555. // Check to see if we got the pointer another way. This can happen when
  556. // enumerating recursive types that hit the base case deeper than they start.
  557. //
  558. // If this is actually a struct that we are treating as forward ref'able,
  559. // then emit the definition now that all of its contents are available.
  560. if (*TypeID && *TypeID != ~0U)
  561. return;
  562. // Add this type now that its contents are all happily enumerated.
  563. Types.push_back(Ty);
  564. *TypeID = Types.size();
  565. }
  566. // Enumerate the types for the specified value. If the value is a constant,
  567. // walk through it, enumerating the types of the constant.
  568. void ValueEnumerator::EnumerateOperandType(const Value *V) {
  569. EnumerateType(V->getType());
  570. if (auto *MD = dyn_cast<MetadataAsValue>(V)) {
  571. assert(!isa<LocalAsMetadata>(MD->getMetadata()) &&
  572. "Function-local metadata should be left for later");
  573. EnumerateMetadata(MD->getMetadata());
  574. return;
  575. }
  576. const Constant *C = dyn_cast<Constant>(V);
  577. if (!C)
  578. return;
  579. // If this constant is already enumerated, ignore it, we know its type must
  580. // be enumerated.
  581. if (ValueMap.count(C))
  582. return;
  583. // This constant may have operands, make sure to enumerate the types in
  584. // them.
  585. for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i) {
  586. const Value *Op = C->getOperand(i);
  587. // Don't enumerate basic blocks here, this happens as operands to
  588. // blockaddress.
  589. if (isa<BasicBlock>(Op))
  590. continue;
  591. EnumerateOperandType(Op);
  592. }
  593. }
  594. void ValueEnumerator::EnumerateAttributes(AttributeSet PAL) {
  595. if (PAL.isEmpty()) return; // null is always 0.
  596. // Do a lookup.
  597. unsigned &Entry = AttributeMap[PAL];
  598. if (Entry == 0) {
  599. // Never saw this before, add it.
  600. Attribute.push_back(PAL);
  601. Entry = Attribute.size();
  602. }
  603. // Do lookups for all attribute groups.
  604. for (unsigned i = 0, e = PAL.getNumSlots(); i != e; ++i) {
  605. AttributeSet AS = PAL.getSlotAttributes(i);
  606. unsigned &Entry = AttributeGroupMap[AS];
  607. if (Entry == 0) {
  608. AttributeGroups.push_back(AS);
  609. Entry = AttributeGroups.size();
  610. }
  611. }
  612. }
  613. void ValueEnumerator::incorporateFunction(const Function &F) {
  614. InstructionCount = 0;
  615. NumModuleValues = Values.size();
  616. NumModuleMDs = MDs.size();
  617. // Adding function arguments to the value table.
  618. for (Function::const_arg_iterator I = F.arg_begin(), E = F.arg_end();
  619. I != E; ++I)
  620. EnumerateValue(I);
  621. FirstFuncConstantID = Values.size();
  622. // Add all function-level constants to the value table.
  623. for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
  624. for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E; ++I)
  625. for (User::const_op_iterator OI = I->op_begin(), E = I->op_end();
  626. OI != E; ++OI) {
  627. if ((isa<Constant>(*OI) && !isa<GlobalValue>(*OI)) ||
  628. isa<InlineAsm>(*OI))
  629. EnumerateValue(*OI);
  630. }
  631. BasicBlocks.push_back(BB);
  632. ValueMap[BB] = BasicBlocks.size();
  633. }
  634. // Optimize the constant layout.
  635. OptimizeConstants(FirstFuncConstantID, Values.size());
  636. // Add the function's parameter attributes so they are available for use in
  637. // the function's instruction.
  638. EnumerateAttributes(F.getAttributes());
  639. FirstInstID = Values.size();
  640. SmallVector<LocalAsMetadata *, 8> FnLocalMDVector;
  641. // Add all of the instructions.
  642. for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
  643. for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E; ++I) {
  644. for (User::const_op_iterator OI = I->op_begin(), E = I->op_end();
  645. OI != E; ++OI) {
  646. if (auto *MD = dyn_cast<MetadataAsValue>(&*OI))
  647. if (auto *Local = dyn_cast<LocalAsMetadata>(MD->getMetadata()))
  648. // Enumerate metadata after the instructions they might refer to.
  649. FnLocalMDVector.push_back(Local);
  650. }
  651. if (!I->getType()->isVoidTy())
  652. EnumerateValue(I);
  653. }
  654. }
  655. // Add all of the function-local metadata.
  656. for (unsigned i = 0, e = FnLocalMDVector.size(); i != e; ++i)
  657. EnumerateFunctionLocalMetadata(FnLocalMDVector[i]);
  658. }
  659. void ValueEnumerator::purgeFunction() {
  660. /// Remove purged values from the ValueMap.
  661. for (unsigned i = NumModuleValues, e = Values.size(); i != e; ++i)
  662. ValueMap.erase(Values[i].first);
  663. for (unsigned i = NumModuleMDs, e = MDs.size(); i != e; ++i)
  664. MDValueMap.erase(MDs[i]);
  665. for (unsigned i = 0, e = BasicBlocks.size(); i != e; ++i)
  666. ValueMap.erase(BasicBlocks[i]);
  667. Values.resize(NumModuleValues);
  668. MDs.resize(NumModuleMDs);
  669. BasicBlocks.clear();
  670. FunctionLocalMDs.clear();
  671. }
  672. static void IncorporateFunctionInfoGlobalBBIDs(const Function *F,
  673. DenseMap<const BasicBlock*, unsigned> &IDMap) {
  674. unsigned Counter = 0;
  675. for (Function::const_iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
  676. IDMap[BB] = ++Counter;
  677. }
  678. /// getGlobalBasicBlockID - This returns the function-specific ID for the
  679. /// specified basic block. This is relatively expensive information, so it
  680. /// should only be used by rare constructs such as address-of-label.
  681. unsigned ValueEnumerator::getGlobalBasicBlockID(const BasicBlock *BB) const {
  682. unsigned &Idx = GlobalBasicBlockIDs[BB];
  683. if (Idx != 0)
  684. return Idx-1;
  685. IncorporateFunctionInfoGlobalBBIDs(BB->getParent(), GlobalBasicBlockIDs);
  686. return getGlobalBasicBlockID(BB);
  687. }