ValueEnumerator.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  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/Constants.h"
  17. #include "llvm/DerivedTypes.h"
  18. #include "llvm/Instructions.h"
  19. #include "llvm/Module.h"
  20. #include "llvm/Support/Debug.h"
  21. #include "llvm/Support/raw_ostream.h"
  22. #include "llvm/ValueSymbolTable.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. // Insert constants and metadata that are named at module level into the slot
  55. // pool so that the module symbol table can refer to them...
  56. EnumerateValueSymbolTable(M->getValueSymbolTable());
  57. EnumerateNamedMetadata(M);
  58. SmallVector<std::pair<unsigned, MDNode*>, 8> MDs;
  59. // Enumerate types used by function bodies and argument lists.
  60. for (Module::const_iterator F = M->begin(), E = M->end(); F != E; ++F) {
  61. for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
  62. I != E; ++I)
  63. EnumerateType(I->getType());
  64. for (Function::const_iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
  65. for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E;++I){
  66. for (User::const_op_iterator OI = I->op_begin(), E = I->op_end();
  67. OI != E; ++OI) {
  68. if (MDNode *MD = dyn_cast<MDNode>(*OI))
  69. if (MD->isFunctionLocal() && MD->getFunction())
  70. // These will get enumerated during function-incorporation.
  71. continue;
  72. EnumerateOperandType(*OI);
  73. }
  74. EnumerateType(I->getType());
  75. if (const CallInst *CI = dyn_cast<CallInst>(I))
  76. EnumerateAttributes(CI->getAttributes());
  77. else if (const InvokeInst *II = dyn_cast<InvokeInst>(I))
  78. EnumerateAttributes(II->getAttributes());
  79. // Enumerate metadata attached with this instruction.
  80. MDs.clear();
  81. I->getAllMetadataOtherThanDebugLoc(MDs);
  82. for (unsigned i = 0, e = MDs.size(); i != e; ++i)
  83. EnumerateMetadata(MDs[i].second);
  84. if (!I->getDebugLoc().isUnknown()) {
  85. MDNode *Scope, *IA;
  86. I->getDebugLoc().getScopeAndInlinedAt(Scope, IA, I->getContext());
  87. if (Scope) EnumerateMetadata(Scope);
  88. if (IA) EnumerateMetadata(IA);
  89. }
  90. }
  91. }
  92. // Optimize constant ordering.
  93. OptimizeConstants(FirstConstant, Values.size());
  94. }
  95. unsigned ValueEnumerator::getInstructionID(const Instruction *Inst) const {
  96. InstructionMapType::const_iterator I = InstructionMap.find(Inst);
  97. assert(I != InstructionMap.end() && "Instruction is not mapped!");
  98. return I->second;
  99. }
  100. void ValueEnumerator::setInstructionID(const Instruction *I) {
  101. InstructionMap[I] = InstructionCount++;
  102. }
  103. unsigned ValueEnumerator::getValueID(const Value *V) const {
  104. if (isa<MDNode>(V) || isa<MDString>(V)) {
  105. ValueMapType::const_iterator I = MDValueMap.find(V);
  106. assert(I != MDValueMap.end() && "Value not in slotcalculator!");
  107. return I->second-1;
  108. }
  109. ValueMapType::const_iterator I = ValueMap.find(V);
  110. assert(I != ValueMap.end() && "Value not in slotcalculator!");
  111. return I->second-1;
  112. }
  113. void ValueEnumerator::dump() const {
  114. print(dbgs(), ValueMap, "Default");
  115. dbgs() << '\n';
  116. print(dbgs(), MDValueMap, "MetaData");
  117. dbgs() << '\n';
  118. }
  119. void ValueEnumerator::print(raw_ostream &OS, const ValueMapType &Map,
  120. const char *Name) const {
  121. OS << "Map Name: " << Name << "\n";
  122. OS << "Size: " << Map.size() << "\n";
  123. for (ValueMapType::const_iterator I = Map.begin(),
  124. E = Map.end(); I != E; ++I) {
  125. const Value *V = I->first;
  126. if (V->hasName())
  127. OS << "Value: " << V->getName();
  128. else
  129. OS << "Value: [null]\n";
  130. V->dump();
  131. OS << " Uses(" << std::distance(V->use_begin(),V->use_end()) << "):";
  132. for (Value::const_use_iterator UI = V->use_begin(), UE = V->use_end();
  133. UI != UE; ++UI) {
  134. if (UI != V->use_begin())
  135. OS << ",";
  136. if((*UI)->hasName())
  137. OS << " " << (*UI)->getName();
  138. else
  139. OS << " [null]";
  140. }
  141. OS << "\n\n";
  142. }
  143. }
  144. // Optimize constant ordering.
  145. namespace {
  146. struct CstSortPredicate {
  147. ValueEnumerator &VE;
  148. explicit CstSortPredicate(ValueEnumerator &ve) : VE(ve) {}
  149. bool operator()(const std::pair<const Value*, unsigned> &LHS,
  150. const std::pair<const Value*, unsigned> &RHS) {
  151. // Sort by plane.
  152. if (LHS.first->getType() != RHS.first->getType())
  153. return VE.getTypeID(LHS.first->getType()) <
  154. VE.getTypeID(RHS.first->getType());
  155. // Then by frequency.
  156. return LHS.second > RHS.second;
  157. }
  158. };
  159. }
  160. /// OptimizeConstants - Reorder constant pool for denser encoding.
  161. void ValueEnumerator::OptimizeConstants(unsigned CstStart, unsigned CstEnd) {
  162. if (CstStart == CstEnd || CstStart+1 == CstEnd) return;
  163. CstSortPredicate P(*this);
  164. std::stable_sort(Values.begin()+CstStart, Values.begin()+CstEnd, P);
  165. // Ensure that integer and vector of integer constants are at the start of the
  166. // constant pool. This is important so that GEP structure indices come before
  167. // gep constant exprs.
  168. std::partition(Values.begin()+CstStart, Values.begin()+CstEnd,
  169. isIntOrIntVectorValue);
  170. // Rebuild the modified portion of ValueMap.
  171. for (; CstStart != CstEnd; ++CstStart)
  172. ValueMap[Values[CstStart].first] = CstStart+1;
  173. }
  174. /// EnumerateValueSymbolTable - Insert all of the values in the specified symbol
  175. /// table into the values table.
  176. void ValueEnumerator::EnumerateValueSymbolTable(const ValueSymbolTable &VST) {
  177. for (ValueSymbolTable::const_iterator VI = VST.begin(), VE = VST.end();
  178. VI != VE; ++VI)
  179. EnumerateValue(VI->getValue());
  180. }
  181. /// EnumerateNamedMetadata - Insert all of the values referenced by
  182. /// named metadata in the specified module.
  183. void ValueEnumerator::EnumerateNamedMetadata(const Module *M) {
  184. for (Module::const_named_metadata_iterator I = M->named_metadata_begin(),
  185. E = M->named_metadata_end(); I != E; ++I)
  186. EnumerateNamedMDNode(I);
  187. }
  188. void ValueEnumerator::EnumerateNamedMDNode(const NamedMDNode *MD) {
  189. for (unsigned i = 0, e = MD->getNumOperands(); i != e; ++i)
  190. EnumerateMetadata(MD->getOperand(i));
  191. }
  192. /// EnumerateMDNodeOperands - Enumerate all non-function-local values
  193. /// and types referenced by the given MDNode.
  194. void ValueEnumerator::EnumerateMDNodeOperands(const MDNode *N) {
  195. for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
  196. if (Value *V = N->getOperand(i)) {
  197. if (isa<MDNode>(V) || isa<MDString>(V))
  198. EnumerateMetadata(V);
  199. else if (!isa<Instruction>(V) && !isa<Argument>(V))
  200. EnumerateValue(V);
  201. } else
  202. EnumerateType(Type::getVoidTy(N->getContext()));
  203. }
  204. }
  205. void ValueEnumerator::EnumerateMetadata(const Value *MD) {
  206. assert((isa<MDNode>(MD) || isa<MDString>(MD)) && "Invalid metadata kind");
  207. // Enumerate the type of this value.
  208. EnumerateType(MD->getType());
  209. const MDNode *N = dyn_cast<MDNode>(MD);
  210. // In the module-level pass, skip function-local nodes themselves, but
  211. // do walk their operands.
  212. if (N && N->isFunctionLocal() && N->getFunction()) {
  213. EnumerateMDNodeOperands(N);
  214. return;
  215. }
  216. // Check to see if it's already in!
  217. unsigned &MDValueID = MDValueMap[MD];
  218. if (MDValueID) {
  219. // Increment use count.
  220. MDValues[MDValueID-1].second++;
  221. return;
  222. }
  223. MDValues.push_back(std::make_pair(MD, 1U));
  224. MDValueID = MDValues.size();
  225. // Enumerate all non-function-local operands.
  226. if (N)
  227. EnumerateMDNodeOperands(N);
  228. }
  229. /// EnumerateFunctionLocalMetadataa - Incorporate function-local metadata
  230. /// information reachable from the given MDNode.
  231. void ValueEnumerator::EnumerateFunctionLocalMetadata(const MDNode *N) {
  232. assert(N->isFunctionLocal() && N->getFunction() &&
  233. "EnumerateFunctionLocalMetadata called on non-function-local mdnode!");
  234. // Enumerate the type of this value.
  235. EnumerateType(N->getType());
  236. // Check to see if it's already in!
  237. unsigned &MDValueID = MDValueMap[N];
  238. if (MDValueID) {
  239. // Increment use count.
  240. MDValues[MDValueID-1].second++;
  241. return;
  242. }
  243. MDValues.push_back(std::make_pair(N, 1U));
  244. MDValueID = MDValues.size();
  245. // To incoroporate function-local information visit all function-local
  246. // MDNodes and all function-local values they reference.
  247. for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
  248. if (Value *V = N->getOperand(i)) {
  249. if (MDNode *O = dyn_cast<MDNode>(V)) {
  250. if (O->isFunctionLocal() && O->getFunction())
  251. EnumerateFunctionLocalMetadata(O);
  252. } else if (isa<Instruction>(V) || isa<Argument>(V))
  253. EnumerateValue(V);
  254. }
  255. // Also, collect all function-local MDNodes for easy access.
  256. FunctionLocalMDs.push_back(N);
  257. }
  258. void ValueEnumerator::EnumerateValue(const Value *V) {
  259. assert(!V->getType()->isVoidTy() && "Can't insert void values!");
  260. assert(!isa<MDNode>(V) && !isa<MDString>(V) &&
  261. "EnumerateValue doesn't handle Metadata!");
  262. // Check to see if it's already in!
  263. unsigned &ValueID = ValueMap[V];
  264. if (ValueID) {
  265. // Increment use count.
  266. Values[ValueID-1].second++;
  267. return;
  268. }
  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(const AttrListPtr &PAL) {
  352. if (PAL.isEmpty()) return; // null is always 0.
  353. // Do a lookup.
  354. unsigned &Entry = AttributeMap[PAL.getRawPointer()];
  355. if (Entry == 0) {
  356. // Never saw this before, add it.
  357. Attributes.push_back(PAL);
  358. Entry = Attributes.size();
  359. }
  360. }
  361. void ValueEnumerator::incorporateFunction(const Function &F) {
  362. InstructionCount = 0;
  363. NumModuleValues = Values.size();
  364. NumModuleMDValues = MDValues.size();
  365. // Adding function arguments to the value table.
  366. for (Function::const_arg_iterator I = F.arg_begin(), E = F.arg_end();
  367. I != E; ++I)
  368. EnumerateValue(I);
  369. FirstFuncConstantID = Values.size();
  370. // Add all function-level constants to the value table.
  371. for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
  372. for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E; ++I)
  373. for (User::const_op_iterator OI = I->op_begin(), E = I->op_end();
  374. OI != E; ++OI) {
  375. if ((isa<Constant>(*OI) && !isa<GlobalValue>(*OI)) ||
  376. isa<InlineAsm>(*OI))
  377. EnumerateValue(*OI);
  378. }
  379. BasicBlocks.push_back(BB);
  380. ValueMap[BB] = BasicBlocks.size();
  381. }
  382. // Optimize the constant layout.
  383. OptimizeConstants(FirstFuncConstantID, Values.size());
  384. // Add the function's parameter attributes so they are available for use in
  385. // the function's instruction.
  386. EnumerateAttributes(F.getAttributes());
  387. FirstInstID = Values.size();
  388. SmallVector<MDNode *, 8> FnLocalMDVector;
  389. // Add all of the instructions.
  390. for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
  391. for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E; ++I) {
  392. for (User::const_op_iterator OI = I->op_begin(), E = I->op_end();
  393. OI != E; ++OI) {
  394. if (MDNode *MD = dyn_cast<MDNode>(*OI))
  395. if (MD->isFunctionLocal() && MD->getFunction())
  396. // Enumerate metadata after the instructions they might refer to.
  397. FnLocalMDVector.push_back(MD);
  398. }
  399. SmallVector<std::pair<unsigned, MDNode*>, 8> MDs;
  400. I->getAllMetadataOtherThanDebugLoc(MDs);
  401. for (unsigned i = 0, e = MDs.size(); i != e; ++i) {
  402. MDNode *N = MDs[i].second;
  403. if (N->isFunctionLocal() && N->getFunction())
  404. FnLocalMDVector.push_back(N);
  405. }
  406. if (!I->getType()->isVoidTy())
  407. EnumerateValue(I);
  408. }
  409. }
  410. // Add all of the function-local metadata.
  411. for (unsigned i = 0, e = FnLocalMDVector.size(); i != e; ++i)
  412. EnumerateFunctionLocalMetadata(FnLocalMDVector[i]);
  413. }
  414. void ValueEnumerator::purgeFunction() {
  415. /// Remove purged values from the ValueMap.
  416. for (unsigned i = NumModuleValues, e = Values.size(); i != e; ++i)
  417. ValueMap.erase(Values[i].first);
  418. for (unsigned i = NumModuleMDValues, e = MDValues.size(); i != e; ++i)
  419. MDValueMap.erase(MDValues[i].first);
  420. for (unsigned i = 0, e = BasicBlocks.size(); i != e; ++i)
  421. ValueMap.erase(BasicBlocks[i]);
  422. Values.resize(NumModuleValues);
  423. MDValues.resize(NumModuleMDValues);
  424. BasicBlocks.clear();
  425. FunctionLocalMDs.clear();
  426. }
  427. static void IncorporateFunctionInfoGlobalBBIDs(const Function *F,
  428. DenseMap<const BasicBlock*, unsigned> &IDMap) {
  429. unsigned Counter = 0;
  430. for (Function::const_iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
  431. IDMap[BB] = ++Counter;
  432. }
  433. /// getGlobalBasicBlockID - This returns the function-specific ID for the
  434. /// specified basic block. This is relatively expensive information, so it
  435. /// should only be used by rare constructs such as address-of-label.
  436. unsigned ValueEnumerator::getGlobalBasicBlockID(const BasicBlock *BB) const {
  437. unsigned &Idx = GlobalBasicBlockIDs[BB];
  438. if (Idx != 0)
  439. return Idx-1;
  440. IncorporateFunctionInfoGlobalBBIDs(BB->getParent(), GlobalBasicBlockIDs);
  441. return getGlobalBasicBlockID(BB);
  442. }