ValueEnumerator.cpp 28 KB

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