ValueEnumerator.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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/Constants.h"
  15. #include "llvm/DerivedTypes.h"
  16. #include "llvm/Metadata.h"
  17. #include "llvm/Module.h"
  18. #include "llvm/TypeSymbolTable.h"
  19. #include "llvm/ValueSymbolTable.h"
  20. #include "llvm/Instructions.h"
  21. #include <algorithm>
  22. using namespace llvm;
  23. static bool isSingleValueType(const std::pair<const llvm::Type*,
  24. unsigned int> &P) {
  25. return P.first->isSingleValueType();
  26. }
  27. static bool isIntegerValue(const std::pair<const Value*, unsigned> &V) {
  28. return isa<IntegerType>(V.first->getType());
  29. }
  30. static bool CompareByFrequency(const std::pair<const llvm::Type*,
  31. unsigned int> &P1,
  32. const std::pair<const llvm::Type*,
  33. unsigned int> &P2) {
  34. return P1.second > P2.second;
  35. }
  36. /// ValueEnumerator - Enumerate module-level information.
  37. ValueEnumerator::ValueEnumerator(const Module *M) {
  38. // Enumerate the global variables.
  39. for (Module::const_global_iterator I = M->global_begin(),
  40. E = M->global_end(); I != E; ++I)
  41. EnumerateValue(I);
  42. // Enumerate the functions.
  43. for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I) {
  44. EnumerateValue(I);
  45. EnumerateAttributes(cast<Function>(I)->getAttributes());
  46. }
  47. // Enumerate the aliases.
  48. for (Module::const_alias_iterator I = M->alias_begin(), E = M->alias_end();
  49. I != E; ++I)
  50. EnumerateValue(I);
  51. // Remember what is the cutoff between globalvalue's and other constants.
  52. unsigned FirstConstant = Values.size();
  53. // Enumerate the global variable initializers.
  54. for (Module::const_global_iterator I = M->global_begin(),
  55. E = M->global_end(); I != E; ++I)
  56. if (I->hasInitializer())
  57. EnumerateValue(I->getInitializer());
  58. // Enumerate the aliasees.
  59. for (Module::const_alias_iterator I = M->alias_begin(), E = M->alias_end();
  60. I != E; ++I)
  61. EnumerateValue(I->getAliasee());
  62. // Enumerate types used by the type symbol table.
  63. EnumerateTypeSymbolTable(M->getTypeSymbolTable());
  64. // Insert constants that are named at module level into the slot pool so that
  65. // the module symbol table can refer to them...
  66. EnumerateValueSymbolTable(M->getValueSymbolTable());
  67. // Enumerate types used by function bodies and argument lists.
  68. for (Module::const_iterator F = M->begin(), E = M->end(); F != E; ++F) {
  69. for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
  70. I != E; ++I)
  71. EnumerateType(I->getType());
  72. for (Function::const_iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
  73. for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E;++I){
  74. for (User::const_op_iterator OI = I->op_begin(), E = I->op_end();
  75. OI != E; ++OI)
  76. EnumerateOperandType(*OI);
  77. EnumerateType(I->getType());
  78. if (const CallInst *CI = dyn_cast<CallInst>(I))
  79. EnumerateAttributes(CI->getAttributes());
  80. else if (const InvokeInst *II = dyn_cast<InvokeInst>(I))
  81. EnumerateAttributes(II->getAttributes());
  82. }
  83. }
  84. // Optimize constant ordering.
  85. OptimizeConstants(FirstConstant, Values.size());
  86. // Sort the type table by frequency so that most commonly used types are early
  87. // in the table (have low bit-width).
  88. std::stable_sort(Types.begin(), Types.end(), CompareByFrequency);
  89. // Partition the Type ID's so that the single-value types occur before the
  90. // aggregate types. This allows the aggregate types to be dropped from the
  91. // type table after parsing the global variable initializers.
  92. std::partition(Types.begin(), Types.end(), isSingleValueType);
  93. // Now that we rearranged the type table, rebuild TypeMap.
  94. for (unsigned i = 0, e = Types.size(); i != e; ++i)
  95. TypeMap[Types[i].first] = i+1;
  96. }
  97. unsigned ValueEnumerator::getValueID(const Value *V) const {
  98. if (isa<MetadataBase>(V)) {
  99. ValueMapType::const_iterator I = MDValueMap.find(V);
  100. assert(I != MDValueMap.end() && "Value not in slotcalculator!");
  101. return I->second-1;
  102. }
  103. ValueMapType::const_iterator I = ValueMap.find(V);
  104. assert(I != ValueMap.end() && "Value not in slotcalculator!");
  105. return I->second-1;
  106. }
  107. // Optimize constant ordering.
  108. namespace {
  109. struct CstSortPredicate {
  110. ValueEnumerator &VE;
  111. explicit CstSortPredicate(ValueEnumerator &ve) : VE(ve) {}
  112. bool operator()(const std::pair<const Value*, unsigned> &LHS,
  113. const std::pair<const Value*, unsigned> &RHS) {
  114. // Sort by plane.
  115. if (LHS.first->getType() != RHS.first->getType())
  116. return VE.getTypeID(LHS.first->getType()) <
  117. VE.getTypeID(RHS.first->getType());
  118. // Then by frequency.
  119. return LHS.second > RHS.second;
  120. }
  121. };
  122. }
  123. /// OptimizeConstants - Reorder constant pool for denser encoding.
  124. void ValueEnumerator::OptimizeConstants(unsigned CstStart, unsigned CstEnd) {
  125. if (CstStart == CstEnd || CstStart+1 == CstEnd) return;
  126. CstSortPredicate P(*this);
  127. std::stable_sort(Values.begin()+CstStart, Values.begin()+CstEnd, P);
  128. // Ensure that integer constants are at the start of the constant pool. This
  129. // is important so that GEP structure indices come before gep constant exprs.
  130. std::partition(Values.begin()+CstStart, Values.begin()+CstEnd,
  131. isIntegerValue);
  132. // Rebuild the modified portion of ValueMap.
  133. for (; CstStart != CstEnd; ++CstStart)
  134. ValueMap[Values[CstStart].first] = CstStart+1;
  135. }
  136. /// EnumerateTypeSymbolTable - Insert all of the types in the specified symbol
  137. /// table.
  138. void ValueEnumerator::EnumerateTypeSymbolTable(const TypeSymbolTable &TST) {
  139. for (TypeSymbolTable::const_iterator TI = TST.begin(), TE = TST.end();
  140. TI != TE; ++TI)
  141. EnumerateType(TI->second);
  142. }
  143. /// EnumerateValueSymbolTable - Insert all of the values in the specified symbol
  144. /// table into the values table.
  145. void ValueEnumerator::EnumerateValueSymbolTable(const ValueSymbolTable &VST) {
  146. for (ValueSymbolTable::const_iterator VI = VST.begin(), VE = VST.end();
  147. VI != VE; ++VI)
  148. EnumerateValue(VI->getValue());
  149. }
  150. void ValueEnumerator::EnumerateMetadata(const MetadataBase *MD) {
  151. // Check to see if it's already in!
  152. unsigned &MDValueID = MDValueMap[MD];
  153. if (MDValueID) {
  154. // Increment use count.
  155. MDValues[MDValueID-1].second++;
  156. return;
  157. }
  158. // Enumerate the type of this value.
  159. EnumerateType(MD->getType());
  160. if (const MDNode *N = dyn_cast<MDNode>(MD)) {
  161. MDValues.push_back(std::make_pair(MD, 1U));
  162. MDValueMap[MD] = MDValues.size();
  163. MDValueID = MDValues.size();
  164. for (MDNode::const_elem_iterator I = N->elem_begin(), E = N->elem_end();
  165. I != E; ++I) {
  166. if (*I)
  167. EnumerateValue(*I);
  168. else
  169. EnumerateType(Type::getVoidTy(MD->getContext()));
  170. }
  171. return;
  172. } else if (const NamedMDNode *N = dyn_cast<NamedMDNode>(MD)) {
  173. for(NamedMDNode::const_elem_iterator I = N->elem_begin(),
  174. E = N->elem_end(); I != E; ++I) {
  175. MetadataBase *M = *I;
  176. EnumerateValue(M);
  177. }
  178. MDValues.push_back(std::make_pair(MD, 1U));
  179. MDValueMap[MD] = Values.size();
  180. return;
  181. }
  182. // Add the value.
  183. MDValues.push_back(std::make_pair(MD, 1U));
  184. MDValueID = MDValues.size();
  185. }
  186. void ValueEnumerator::EnumerateValue(const Value *V) {
  187. assert(V->getType() != Type::getVoidTy(V->getContext()) &&
  188. "Can't insert void values!");
  189. if (const MetadataBase *MB = dyn_cast<MetadataBase>(V))
  190. return EnumerateMetadata(MB);
  191. // Check to see if it's already in!
  192. unsigned &ValueID = ValueMap[V];
  193. if (ValueID) {
  194. // Increment use count.
  195. Values[ValueID-1].second++;
  196. return;
  197. }
  198. // Enumerate the type of this value.
  199. EnumerateType(V->getType());
  200. if (const Constant *C = dyn_cast<Constant>(V)) {
  201. if (isa<GlobalValue>(C)) {
  202. // Initializers for globals are handled explicitly elsewhere.
  203. } else if (isa<ConstantArray>(C) && cast<ConstantArray>(C)->isString()) {
  204. // Do not enumerate the initializers for an array of simple characters.
  205. // The initializers just polute the value table, and we emit the strings
  206. // specially.
  207. } else if (C->getNumOperands()) {
  208. // If a constant has operands, enumerate them. This makes sure that if a
  209. // constant has uses (for example an array of const ints), that they are
  210. // inserted also.
  211. // We prefer to enumerate them with values before we enumerate the user
  212. // itself. This makes it more likely that we can avoid forward references
  213. // in the reader. We know that there can be no cycles in the constants
  214. // graph that don't go through a global variable.
  215. for (User::const_op_iterator I = C->op_begin(), E = C->op_end();
  216. I != E; ++I)
  217. EnumerateValue(*I);
  218. // Finally, add the value. Doing this could make the ValueID reference be
  219. // dangling, don't reuse it.
  220. Values.push_back(std::make_pair(V, 1U));
  221. ValueMap[V] = Values.size();
  222. return;
  223. }
  224. }
  225. // Add the value.
  226. Values.push_back(std::make_pair(V, 1U));
  227. ValueID = Values.size();
  228. }
  229. void ValueEnumerator::EnumerateType(const Type *Ty) {
  230. unsigned &TypeID = TypeMap[Ty];
  231. if (TypeID) {
  232. // If we've already seen this type, just increase its occurrence count.
  233. Types[TypeID-1].second++;
  234. return;
  235. }
  236. // First time we saw this type, add it.
  237. Types.push_back(std::make_pair(Ty, 1U));
  238. TypeID = Types.size();
  239. // Enumerate subtypes.
  240. for (Type::subtype_iterator I = Ty->subtype_begin(), E = Ty->subtype_end();
  241. I != E; ++I)
  242. EnumerateType(*I);
  243. }
  244. // Enumerate the types for the specified value. If the value is a constant,
  245. // walk through it, enumerating the types of the constant.
  246. void ValueEnumerator::EnumerateOperandType(const Value *V) {
  247. EnumerateType(V->getType());
  248. if (const Constant *C = dyn_cast<Constant>(V)) {
  249. // If this constant is already enumerated, ignore it, we know its type must
  250. // be enumerated.
  251. if (ValueMap.count(V)) return;
  252. // This constant may have operands, make sure to enumerate the types in
  253. // them.
  254. for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i)
  255. EnumerateOperandType(C->getOperand(i));
  256. if (const MDNode *N = dyn_cast<MDNode>(V)) {
  257. for (unsigned i = 0, e = N->getNumElements(); i != e; ++i) {
  258. Value *Elem = N->getElement(i);
  259. if (Elem)
  260. EnumerateOperandType(Elem);
  261. }
  262. }
  263. } else if (isa<MDString>(V) || isa<MDNode>(V))
  264. EnumerateValue(V);
  265. }
  266. void ValueEnumerator::EnumerateAttributes(const AttrListPtr &PAL) {
  267. if (PAL.isEmpty()) return; // null is always 0.
  268. // Do a lookup.
  269. unsigned &Entry = AttributeMap[PAL.getRawPointer()];
  270. if (Entry == 0) {
  271. // Never saw this before, add it.
  272. Attributes.push_back(PAL);
  273. Entry = Attributes.size();
  274. }
  275. }
  276. void ValueEnumerator::incorporateFunction(const Function &F) {
  277. NumModuleValues = Values.size();
  278. // Adding function arguments to the value table.
  279. for(Function::const_arg_iterator I = F.arg_begin(), E = F.arg_end();
  280. I != E; ++I)
  281. EnumerateValue(I);
  282. FirstFuncConstantID = Values.size();
  283. // Add all function-level constants to the value table.
  284. for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
  285. for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E; ++I)
  286. for (User::const_op_iterator OI = I->op_begin(), E = I->op_end();
  287. OI != E; ++OI) {
  288. if ((isa<Constant>(*OI) && !isa<GlobalValue>(*OI)) ||
  289. isa<InlineAsm>(*OI))
  290. EnumerateValue(*OI);
  291. }
  292. BasicBlocks.push_back(BB);
  293. ValueMap[BB] = BasicBlocks.size();
  294. }
  295. // Optimize the constant layout.
  296. OptimizeConstants(FirstFuncConstantID, Values.size());
  297. // Add the function's parameter attributes so they are available for use in
  298. // the function's instruction.
  299. EnumerateAttributes(F.getAttributes());
  300. FirstInstID = Values.size();
  301. // Add all of the instructions.
  302. for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
  303. for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E; ++I) {
  304. if (I->getType() != Type::getVoidTy(F.getContext()))
  305. EnumerateValue(I);
  306. }
  307. }
  308. }
  309. void ValueEnumerator::purgeFunction() {
  310. /// Remove purged values from the ValueMap.
  311. for (unsigned i = NumModuleValues, e = Values.size(); i != e; ++i)
  312. ValueMap.erase(Values[i].first);
  313. for (unsigned i = 0, e = BasicBlocks.size(); i != e; ++i)
  314. ValueMap.erase(BasicBlocks[i]);
  315. Values.resize(NumModuleValues);
  316. BasicBlocks.clear();
  317. }