ValueEnumerator.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  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/ValueSymbolTable.h"
  21. #include "llvm/Support/Debug.h"
  22. #include "llvm/Support/raw_ostream.h"
  23. #include <algorithm>
  24. using namespace llvm;
  25. static bool isIntOrIntVectorValue(const std::pair<const Value*, unsigned> &V) {
  26. return V.first->getType()->isIntOrIntVectorTy();
  27. }
  28. /// ValueEnumerator - Enumerate module-level information.
  29. ValueEnumerator::ValueEnumerator(const Module *M) {
  30. // Enumerate the global variables.
  31. for (Module::const_global_iterator I = M->global_begin(),
  32. E = M->global_end(); I != E; ++I)
  33. EnumerateValue(I);
  34. // Enumerate the functions.
  35. for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I) {
  36. EnumerateValue(I);
  37. EnumerateAttributes(cast<Function>(I)->getAttributes());
  38. }
  39. // Enumerate the aliases.
  40. for (Module::const_alias_iterator I = M->alias_begin(), E = M->alias_end();
  41. I != E; ++I)
  42. EnumerateValue(I);
  43. // Remember what is the cutoff between globalvalue's and other constants.
  44. unsigned FirstConstant = Values.size();
  45. // Enumerate the global variable initializers.
  46. for (Module::const_global_iterator I = M->global_begin(),
  47. E = M->global_end(); I != E; ++I)
  48. if (I->hasInitializer())
  49. EnumerateValue(I->getInitializer());
  50. // Enumerate the aliasees.
  51. for (Module::const_alias_iterator I = M->alias_begin(), E = M->alias_end();
  52. I != E; ++I)
  53. EnumerateValue(I->getAliasee());
  54. // Enumerate the prefix data constants.
  55. for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
  56. if (I->hasPrefixData())
  57. EnumerateValue(I->getPrefixData());
  58. // Insert constants and metadata that are named at module level into the slot
  59. // pool so that the module symbol table can refer to them...
  60. EnumerateValueSymbolTable(M->getValueSymbolTable());
  61. EnumerateNamedMetadata(M);
  62. SmallVector<std::pair<unsigned, MDNode*>, 8> MDs;
  63. // Enumerate types used by function bodies and argument lists.
  64. for (const Function &F : *M) {
  65. for (const Argument &A : F.args())
  66. EnumerateType(A.getType());
  67. for (const BasicBlock &BB : F)
  68. for (const Instruction &I : BB) {
  69. for (const Use &Op : I.operands()) {
  70. if (MDNode *MD = dyn_cast<MDNode>(&Op))
  71. if (MD->isFunctionLocal() && MD->getFunction())
  72. // These will get enumerated during function-incorporation.
  73. continue;
  74. EnumerateOperandType(Op);
  75. }
  76. EnumerateType(I.getType());
  77. if (const CallInst *CI = dyn_cast<CallInst>(&I))
  78. EnumerateAttributes(CI->getAttributes());
  79. else if (const InvokeInst *II = dyn_cast<InvokeInst>(&I))
  80. EnumerateAttributes(II->getAttributes());
  81. // Enumerate metadata attached with this instruction.
  82. MDs.clear();
  83. I.getAllMetadataOtherThanDebugLoc(MDs);
  84. for (unsigned i = 0, e = MDs.size(); i != e; ++i)
  85. EnumerateMetadata(MDs[i].second);
  86. if (!I.getDebugLoc().isUnknown()) {
  87. MDNode *Scope, *IA;
  88. I.getDebugLoc().getScopeAndInlinedAt(Scope, IA, I.getContext());
  89. if (Scope) EnumerateMetadata(Scope);
  90. if (IA) EnumerateMetadata(IA);
  91. }
  92. }
  93. }
  94. // Optimize constant ordering.
  95. OptimizeConstants(FirstConstant, Values.size());
  96. }
  97. unsigned ValueEnumerator::getInstructionID(const Instruction *Inst) const {
  98. InstructionMapType::const_iterator I = InstructionMap.find(Inst);
  99. assert(I != InstructionMap.end() && "Instruction is not mapped!");
  100. return I->second;
  101. }
  102. unsigned ValueEnumerator::getComdatID(const Comdat *C) const {
  103. unsigned ComdatID = Comdats.idFor(C);
  104. assert(ComdatID && "Comdat not found!");
  105. return ComdatID;
  106. }
  107. void ValueEnumerator::setInstructionID(const Instruction *I) {
  108. InstructionMap[I] = InstructionCount++;
  109. }
  110. unsigned ValueEnumerator::getValueID(const Value *V) const {
  111. if (isa<MDNode>(V) || isa<MDString>(V)) {
  112. ValueMapType::const_iterator I = MDValueMap.find(V);
  113. assert(I != MDValueMap.end() && "Value not in slotcalculator!");
  114. return I->second-1;
  115. }
  116. ValueMapType::const_iterator I = ValueMap.find(V);
  117. assert(I != ValueMap.end() && "Value not in slotcalculator!");
  118. return I->second-1;
  119. }
  120. void ValueEnumerator::dump() const {
  121. print(dbgs(), ValueMap, "Default");
  122. dbgs() << '\n';
  123. print(dbgs(), MDValueMap, "MetaData");
  124. dbgs() << '\n';
  125. }
  126. void ValueEnumerator::print(raw_ostream &OS, const ValueMapType &Map,
  127. const char *Name) const {
  128. OS << "Map Name: " << Name << "\n";
  129. OS << "Size: " << Map.size() << "\n";
  130. for (ValueMapType::const_iterator I = Map.begin(),
  131. E = Map.end(); I != E; ++I) {
  132. const Value *V = I->first;
  133. if (V->hasName())
  134. OS << "Value: " << V->getName();
  135. else
  136. OS << "Value: [null]\n";
  137. V->dump();
  138. OS << " Uses(" << std::distance(V->use_begin(),V->use_end()) << "):";
  139. for (const Use &U : V->uses()) {
  140. if (&U != &*V->use_begin())
  141. OS << ",";
  142. if(U->hasName())
  143. OS << " " << U->getName();
  144. else
  145. OS << " [null]";
  146. }
  147. OS << "\n\n";
  148. }
  149. }
  150. /// OptimizeConstants - Reorder constant pool for denser encoding.
  151. void ValueEnumerator::OptimizeConstants(unsigned CstStart, unsigned CstEnd) {
  152. if (CstStart == CstEnd || CstStart+1 == CstEnd) return;
  153. std::stable_sort(Values.begin() + CstStart, Values.begin() + CstEnd,
  154. [this](const std::pair<const Value *, unsigned> &LHS,
  155. const std::pair<const Value *, unsigned> &RHS) {
  156. // Sort by plane.
  157. if (LHS.first->getType() != RHS.first->getType())
  158. return getTypeID(LHS.first->getType()) < getTypeID(RHS.first->getType());
  159. // Then by frequency.
  160. return LHS.second > RHS.second;
  161. });
  162. // Ensure that integer and vector of integer constants are at the start of the
  163. // constant pool. This is important so that GEP structure indices come before
  164. // gep constant exprs.
  165. std::partition(Values.begin()+CstStart, Values.begin()+CstEnd,
  166. isIntOrIntVectorValue);
  167. // Rebuild the modified portion of ValueMap.
  168. for (; CstStart != CstEnd; ++CstStart)
  169. ValueMap[Values[CstStart].first] = CstStart+1;
  170. }
  171. /// EnumerateValueSymbolTable - Insert all of the values in the specified symbol
  172. /// table into the values table.
  173. void ValueEnumerator::EnumerateValueSymbolTable(const ValueSymbolTable &VST) {
  174. for (ValueSymbolTable::const_iterator VI = VST.begin(), VE = VST.end();
  175. VI != VE; ++VI)
  176. EnumerateValue(VI->getValue());
  177. }
  178. /// EnumerateNamedMetadata - Insert all of the values referenced by
  179. /// named metadata in the specified module.
  180. void ValueEnumerator::EnumerateNamedMetadata(const Module *M) {
  181. for (Module::const_named_metadata_iterator I = M->named_metadata_begin(),
  182. E = M->named_metadata_end(); I != E; ++I)
  183. EnumerateNamedMDNode(I);
  184. }
  185. void ValueEnumerator::EnumerateNamedMDNode(const NamedMDNode *MD) {
  186. for (unsigned i = 0, e = MD->getNumOperands(); i != e; ++i)
  187. EnumerateMetadata(MD->getOperand(i));
  188. }
  189. /// EnumerateMDNodeOperands - Enumerate all non-function-local values
  190. /// and types referenced by the given MDNode.
  191. void ValueEnumerator::EnumerateMDNodeOperands(const MDNode *N) {
  192. for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
  193. if (Value *V = N->getOperand(i)) {
  194. if (isa<MDNode>(V) || isa<MDString>(V))
  195. EnumerateMetadata(V);
  196. else if (!isa<Instruction>(V) && !isa<Argument>(V))
  197. EnumerateValue(V);
  198. } else
  199. EnumerateType(Type::getVoidTy(N->getContext()));
  200. }
  201. }
  202. void ValueEnumerator::EnumerateMetadata(const Value *MD) {
  203. assert((isa<MDNode>(MD) || isa<MDString>(MD)) && "Invalid metadata kind");
  204. // Enumerate the type of this value.
  205. EnumerateType(MD->getType());
  206. const MDNode *N = dyn_cast<MDNode>(MD);
  207. // In the module-level pass, skip function-local nodes themselves, but
  208. // do walk their operands.
  209. if (N && N->isFunctionLocal() && N->getFunction()) {
  210. EnumerateMDNodeOperands(N);
  211. return;
  212. }
  213. // Check to see if it's already in!
  214. unsigned &MDValueID = MDValueMap[MD];
  215. if (MDValueID) {
  216. // Increment use count.
  217. MDValues[MDValueID-1].second++;
  218. return;
  219. }
  220. MDValues.push_back(std::make_pair(MD, 1U));
  221. MDValueID = MDValues.size();
  222. // Enumerate all non-function-local operands.
  223. if (N)
  224. EnumerateMDNodeOperands(N);
  225. }
  226. /// EnumerateFunctionLocalMetadataa - Incorporate function-local metadata
  227. /// information reachable from the given MDNode.
  228. void ValueEnumerator::EnumerateFunctionLocalMetadata(const MDNode *N) {
  229. assert(N->isFunctionLocal() && N->getFunction() &&
  230. "EnumerateFunctionLocalMetadata called on non-function-local mdnode!");
  231. // Enumerate the type of this value.
  232. EnumerateType(N->getType());
  233. // Check to see if it's already in!
  234. unsigned &MDValueID = MDValueMap[N];
  235. if (MDValueID) {
  236. // Increment use count.
  237. MDValues[MDValueID-1].second++;
  238. return;
  239. }
  240. MDValues.push_back(std::make_pair(N, 1U));
  241. MDValueID = MDValues.size();
  242. // To incoroporate function-local information visit all function-local
  243. // MDNodes and all function-local values they reference.
  244. for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
  245. if (Value *V = N->getOperand(i)) {
  246. if (MDNode *O = dyn_cast<MDNode>(V)) {
  247. if (O->isFunctionLocal() && O->getFunction())
  248. EnumerateFunctionLocalMetadata(O);
  249. } else if (isa<Instruction>(V) || isa<Argument>(V))
  250. EnumerateValue(V);
  251. }
  252. // Also, collect all function-local MDNodes for easy access.
  253. FunctionLocalMDs.push_back(N);
  254. }
  255. void ValueEnumerator::EnumerateValue(const Value *V) {
  256. assert(!V->getType()->isVoidTy() && "Can't insert void values!");
  257. assert(!isa<MDNode>(V) && !isa<MDString>(V) &&
  258. "EnumerateValue doesn't handle Metadata!");
  259. // Check to see if it's already in!
  260. unsigned &ValueID = ValueMap[V];
  261. if (ValueID) {
  262. // Increment use count.
  263. Values[ValueID-1].second++;
  264. return;
  265. }
  266. if (auto *GO = dyn_cast<GlobalObject>(V))
  267. if (const Comdat *C = GO->getComdat())
  268. Comdats.insert(C);
  269. // Enumerate the type of this value.
  270. EnumerateType(V->getType());
  271. if (const Constant *C = dyn_cast<Constant>(V)) {
  272. if (isa<GlobalValue>(C)) {
  273. // Initializers for globals are handled explicitly elsewhere.
  274. } else if (C->getNumOperands()) {
  275. // If a constant has operands, enumerate them. This makes sure that if a
  276. // constant has uses (for example an array of const ints), that they are
  277. // inserted also.
  278. // We prefer to enumerate them with values before we enumerate the user
  279. // itself. This makes it more likely that we can avoid forward references
  280. // in the reader. We know that there can be no cycles in the constants
  281. // graph that don't go through a global variable.
  282. for (User::const_op_iterator I = C->op_begin(), E = C->op_end();
  283. I != E; ++I)
  284. if (!isa<BasicBlock>(*I)) // Don't enumerate BB operand to BlockAddress.
  285. EnumerateValue(*I);
  286. // Finally, add the value. Doing this could make the ValueID reference be
  287. // dangling, don't reuse it.
  288. Values.push_back(std::make_pair(V, 1U));
  289. ValueMap[V] = Values.size();
  290. return;
  291. }
  292. }
  293. // Add the value.
  294. Values.push_back(std::make_pair(V, 1U));
  295. ValueID = Values.size();
  296. }
  297. void ValueEnumerator::EnumerateType(Type *Ty) {
  298. unsigned *TypeID = &TypeMap[Ty];
  299. // We've already seen this type.
  300. if (*TypeID)
  301. return;
  302. // If it is a non-anonymous struct, mark the type as being visited so that we
  303. // don't recursively visit it. This is safe because we allow forward
  304. // references of these in the bitcode reader.
  305. if (StructType *STy = dyn_cast<StructType>(Ty))
  306. if (!STy->isLiteral())
  307. *TypeID = ~0U;
  308. // Enumerate all of the subtypes before we enumerate this type. This ensures
  309. // that the type will be enumerated in an order that can be directly built.
  310. for (Type::subtype_iterator I = Ty->subtype_begin(), E = Ty->subtype_end();
  311. I != E; ++I)
  312. EnumerateType(*I);
  313. // Refresh the TypeID pointer in case the table rehashed.
  314. TypeID = &TypeMap[Ty];
  315. // Check to see if we got the pointer another way. This can happen when
  316. // enumerating recursive types that hit the base case deeper than they start.
  317. //
  318. // If this is actually a struct that we are treating as forward ref'able,
  319. // then emit the definition now that all of its contents are available.
  320. if (*TypeID && *TypeID != ~0U)
  321. return;
  322. // Add this type now that its contents are all happily enumerated.
  323. Types.push_back(Ty);
  324. *TypeID = Types.size();
  325. }
  326. // Enumerate the types for the specified value. If the value is a constant,
  327. // walk through it, enumerating the types of the constant.
  328. void ValueEnumerator::EnumerateOperandType(const Value *V) {
  329. EnumerateType(V->getType());
  330. if (const Constant *C = dyn_cast<Constant>(V)) {
  331. // If this constant is already enumerated, ignore it, we know its type must
  332. // be enumerated.
  333. if (ValueMap.count(V)) return;
  334. // This constant may have operands, make sure to enumerate the types in
  335. // them.
  336. for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i) {
  337. const Value *Op = C->getOperand(i);
  338. // Don't enumerate basic blocks here, this happens as operands to
  339. // blockaddress.
  340. if (isa<BasicBlock>(Op)) continue;
  341. EnumerateOperandType(Op);
  342. }
  343. if (const MDNode *N = dyn_cast<MDNode>(V)) {
  344. for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
  345. if (Value *Elem = N->getOperand(i))
  346. EnumerateOperandType(Elem);
  347. }
  348. } else if (isa<MDString>(V) || isa<MDNode>(V))
  349. EnumerateMetadata(V);
  350. }
  351. void ValueEnumerator::EnumerateAttributes(AttributeSet PAL) {
  352. if (PAL.isEmpty()) return; // null is always 0.
  353. // Do a lookup.
  354. unsigned &Entry = AttributeMap[PAL];
  355. if (Entry == 0) {
  356. // Never saw this before, add it.
  357. Attribute.push_back(PAL);
  358. Entry = Attribute.size();
  359. }
  360. // Do lookups for all attribute groups.
  361. for (unsigned i = 0, e = PAL.getNumSlots(); i != e; ++i) {
  362. AttributeSet AS = PAL.getSlotAttributes(i);
  363. unsigned &Entry = AttributeGroupMap[AS];
  364. if (Entry == 0) {
  365. AttributeGroups.push_back(AS);
  366. Entry = AttributeGroups.size();
  367. }
  368. }
  369. }
  370. void ValueEnumerator::incorporateFunction(const Function &F) {
  371. InstructionCount = 0;
  372. NumModuleValues = Values.size();
  373. NumModuleMDValues = MDValues.size();
  374. // Adding function arguments to the value table.
  375. for (Function::const_arg_iterator I = F.arg_begin(), E = F.arg_end();
  376. I != E; ++I)
  377. EnumerateValue(I);
  378. FirstFuncConstantID = Values.size();
  379. // Add all function-level constants to the value table.
  380. for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
  381. for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E; ++I)
  382. for (User::const_op_iterator OI = I->op_begin(), E = I->op_end();
  383. OI != E; ++OI) {
  384. if ((isa<Constant>(*OI) && !isa<GlobalValue>(*OI)) ||
  385. isa<InlineAsm>(*OI))
  386. EnumerateValue(*OI);
  387. }
  388. BasicBlocks.push_back(BB);
  389. ValueMap[BB] = BasicBlocks.size();
  390. }
  391. // Optimize the constant layout.
  392. OptimizeConstants(FirstFuncConstantID, Values.size());
  393. // Add the function's parameter attributes so they are available for use in
  394. // the function's instruction.
  395. EnumerateAttributes(F.getAttributes());
  396. FirstInstID = Values.size();
  397. SmallVector<MDNode *, 8> FnLocalMDVector;
  398. // Add all of the instructions.
  399. for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
  400. for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E; ++I) {
  401. for (User::const_op_iterator OI = I->op_begin(), E = I->op_end();
  402. OI != E; ++OI) {
  403. if (MDNode *MD = dyn_cast<MDNode>(*OI))
  404. if (MD->isFunctionLocal() && MD->getFunction())
  405. // Enumerate metadata after the instructions they might refer to.
  406. FnLocalMDVector.push_back(MD);
  407. }
  408. SmallVector<std::pair<unsigned, MDNode*>, 8> MDs;
  409. I->getAllMetadataOtherThanDebugLoc(MDs);
  410. for (unsigned i = 0, e = MDs.size(); i != e; ++i) {
  411. MDNode *N = MDs[i].second;
  412. if (N->isFunctionLocal() && N->getFunction())
  413. FnLocalMDVector.push_back(N);
  414. }
  415. if (!I->getType()->isVoidTy())
  416. EnumerateValue(I);
  417. }
  418. }
  419. // Add all of the function-local metadata.
  420. for (unsigned i = 0, e = FnLocalMDVector.size(); i != e; ++i)
  421. EnumerateFunctionLocalMetadata(FnLocalMDVector[i]);
  422. }
  423. void ValueEnumerator::purgeFunction() {
  424. /// Remove purged values from the ValueMap.
  425. for (unsigned i = NumModuleValues, e = Values.size(); i != e; ++i)
  426. ValueMap.erase(Values[i].first);
  427. for (unsigned i = NumModuleMDValues, e = MDValues.size(); i != e; ++i)
  428. MDValueMap.erase(MDValues[i].first);
  429. for (unsigned i = 0, e = BasicBlocks.size(); i != e; ++i)
  430. ValueMap.erase(BasicBlocks[i]);
  431. Values.resize(NumModuleValues);
  432. MDValues.resize(NumModuleMDValues);
  433. BasicBlocks.clear();
  434. FunctionLocalMDs.clear();
  435. }
  436. static void IncorporateFunctionInfoGlobalBBIDs(const Function *F,
  437. DenseMap<const BasicBlock*, unsigned> &IDMap) {
  438. unsigned Counter = 0;
  439. for (Function::const_iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
  440. IDMap[BB] = ++Counter;
  441. }
  442. /// getGlobalBasicBlockID - This returns the function-specific ID for the
  443. /// specified basic block. This is relatively expensive information, so it
  444. /// should only be used by rare constructs such as address-of-label.
  445. unsigned ValueEnumerator::getGlobalBasicBlockID(const BasicBlock *BB) const {
  446. unsigned &Idx = GlobalBasicBlockIDs[BB];
  447. if (Idx != 0)
  448. return Idx-1;
  449. IncorporateFunctionInfoGlobalBBIDs(BB->getParent(), GlobalBasicBlockIDs);
  450. return getGlobalBasicBlockID(BB);
  451. }