TypeFinder.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. //===-- TypeFinder.cpp - Implement the TypeFinder class -------------------===//
  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 TypeFinder class for the IR library.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "llvm/IR/TypeFinder.h"
  14. #include "llvm/ADT/SmallVector.h"
  15. #include "llvm/IR/BasicBlock.h"
  16. #include "llvm/IR/DerivedTypes.h"
  17. #include "llvm/IR/Function.h"
  18. #include "llvm/IR/Metadata.h"
  19. #include "llvm/IR/Module.h"
  20. using namespace llvm;
  21. void TypeFinder::run(const Module &M, bool onlyNamed) {
  22. OnlyNamed = onlyNamed;
  23. // Get types from global variables.
  24. for (Module::const_global_iterator I = M.global_begin(),
  25. E = M.global_end(); I != E; ++I) {
  26. incorporateType(I->getType());
  27. if (I->hasInitializer())
  28. incorporateValue(I->getInitializer());
  29. }
  30. // Get types from aliases.
  31. for (Module::const_alias_iterator I = M.alias_begin(),
  32. E = M.alias_end(); I != E; ++I) {
  33. incorporateType(I->getType());
  34. if (const Value *Aliasee = I->getAliasee())
  35. incorporateValue(Aliasee);
  36. }
  37. // Get types from functions.
  38. SmallVector<std::pair<unsigned, MDNode *>, 4> MDForInst;
  39. for (Module::const_iterator FI = M.begin(), E = M.end(); FI != E; ++FI) {
  40. incorporateType(FI->getType());
  41. if (FI->hasPrefixData())
  42. incorporateValue(FI->getPrefixData());
  43. if (FI->hasPrologueData())
  44. incorporateValue(FI->getPrologueData());
  45. if (FI->hasPersonalityFn())
  46. incorporateValue(FI->getPersonalityFn());
  47. // First incorporate the arguments.
  48. for (Function::const_arg_iterator AI = FI->arg_begin(),
  49. AE = FI->arg_end(); AI != AE; ++AI)
  50. incorporateValue(AI);
  51. for (Function::const_iterator BB = FI->begin(), E = FI->end();
  52. BB != E;++BB)
  53. for (BasicBlock::const_iterator II = BB->begin(),
  54. E = BB->end(); II != E; ++II) {
  55. const Instruction &I = *II;
  56. // Incorporate the type of the instruction.
  57. incorporateType(I.getType());
  58. // Incorporate non-instruction operand types. (We are incorporating all
  59. // instructions with this loop.)
  60. for (User::const_op_iterator OI = I.op_begin(), OE = I.op_end();
  61. OI != OE; ++OI)
  62. if (*OI && !isa<Instruction>(OI))
  63. incorporateValue(*OI);
  64. // Incorporate types hiding in metadata.
  65. I.getAllMetadataOtherThanDebugLoc(MDForInst);
  66. for (unsigned i = 0, e = MDForInst.size(); i != e; ++i)
  67. incorporateMDNode(MDForInst[i].second);
  68. MDForInst.clear();
  69. }
  70. }
  71. for (Module::const_named_metadata_iterator I = M.named_metadata_begin(),
  72. E = M.named_metadata_end(); I != E; ++I) {
  73. const NamedMDNode *NMD = I;
  74. for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i)
  75. incorporateMDNode(NMD->getOperand(i));
  76. }
  77. }
  78. void TypeFinder::clear() {
  79. VisitedConstants.clear();
  80. VisitedTypes.clear();
  81. StructTypes.clear();
  82. }
  83. /// incorporateType - This method adds the type to the list of used structures
  84. /// if it's not in there already.
  85. void TypeFinder::incorporateType(Type *Ty) {
  86. // Check to see if we've already visited this type.
  87. if (!VisitedTypes.insert(Ty).second)
  88. return;
  89. SmallVector<Type *, 4> TypeWorklist;
  90. TypeWorklist.push_back(Ty);
  91. do {
  92. Ty = TypeWorklist.pop_back_val();
  93. // If this is a structure or opaque type, add a name for the type.
  94. if (StructType *STy = dyn_cast<StructType>(Ty))
  95. if (!OnlyNamed || STy->hasName())
  96. StructTypes.push_back(STy);
  97. // Add all unvisited subtypes to worklist for processing
  98. for (Type::subtype_reverse_iterator I = Ty->subtype_rbegin(),
  99. E = Ty->subtype_rend();
  100. I != E; ++I)
  101. if (VisitedTypes.insert(*I).second)
  102. TypeWorklist.push_back(*I);
  103. } while (!TypeWorklist.empty());
  104. }
  105. /// incorporateValue - This method is used to walk operand lists finding types
  106. /// hiding in constant expressions and other operands that won't be walked in
  107. /// other ways. GlobalValues, basic blocks, instructions, and inst operands are
  108. /// all explicitly enumerated.
  109. void TypeFinder::incorporateValue(const Value *V) {
  110. if (const auto *M = dyn_cast<MetadataAsValue>(V)) {
  111. if (const auto *N = dyn_cast<MDNode>(M->getMetadata()))
  112. return incorporateMDNode(N);
  113. if (const auto *MDV = dyn_cast<ValueAsMetadata>(M->getMetadata()))
  114. return incorporateValue(MDV->getValue());
  115. return;
  116. }
  117. if (!isa<Constant>(V) || isa<GlobalValue>(V)) return;
  118. // Already visited?
  119. if (!VisitedConstants.insert(V).second)
  120. return;
  121. // Check this type.
  122. incorporateType(V->getType());
  123. // If this is an instruction, we incorporate it separately.
  124. if (isa<Instruction>(V))
  125. return;
  126. // Look in operands for types.
  127. const User *U = cast<User>(V);
  128. for (Constant::const_op_iterator I = U->op_begin(),
  129. E = U->op_end(); I != E;++I)
  130. incorporateValue(*I);
  131. }
  132. /// incorporateMDNode - This method is used to walk the operands of an MDNode to
  133. /// find types hiding within.
  134. void TypeFinder::incorporateMDNode(const MDNode *V) {
  135. // Already visited?
  136. if (!VisitedMetadata.insert(V).second)
  137. return;
  138. // Look in operands for types.
  139. for (unsigned i = 0, e = V->getNumOperands(); i != e; ++i) {
  140. Metadata *Op = V->getOperand(i);
  141. if (!Op)
  142. continue;
  143. if (auto *N = dyn_cast<MDNode>(Op)) {
  144. incorporateMDNode(N);
  145. continue;
  146. }
  147. if (auto *C = dyn_cast<ConstantAsMetadata>(Op)) {
  148. incorporateValue(C->getValue());
  149. continue;
  150. }
  151. }
  152. }