Add a newline to the end of this file.
[oota-llvm.git] / lib / Analysis / IPA / FindUsedTypes.cpp
index 5c961d2bebe7b72d8e3fc04a4ca68514152460ae..a954414b43952e9a177f43daa2459edaf9ff7b1d 100644 (file)
@@ -1,32 +1,38 @@
-//===- FindUsedTypes.cpp - Find all Types used by a module ------------------=//
+//===- FindUsedTypes.cpp - Find all Types used by a module ----------------===//
 //
-// This pass is used to seek out all of the types in use by the program.
+//                     The LLVM Compiler Infrastructure
+//
+// This file was developed by the LLVM research group and is distributed under
+// the University of Illinois Open Source License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// This pass is used to seek out all of the types in use by the program.  Note
+// that this analysis explicitly does not include types only used by the symbol
+// table.
 //
 //===----------------------------------------------------------------------===//
 
 #include "llvm/Analysis/FindUsedTypes.h"
-#include "llvm/Assembly/CachedWriter.h"
-#include "llvm/SymbolTable.h"
-#include "llvm/GlobalVariable.h"
+#include "llvm/Constants.h"
 #include "llvm/DerivedTypes.h"
 #include "llvm/Module.h"
-#include "llvm/Function.h"
-#include "llvm/Instruction.h"
+#include "llvm/Assembly/Writer.h"
 #include "llvm/Support/InstIterator.h"
+using namespace llvm;
 
-AnalysisID FindUsedTypes::ID(AnalysisID::create<FindUsedTypes>());
-AnalysisID FindUsedTypes::IncludeSymbolTableID(AnalysisID::create<FindUsedTypes>());
+char FindUsedTypes::ID = 0;
+static RegisterPass<FindUsedTypes>
+X("printusedtypes", "Find Used Types");
 
 // IncorporateType - Incorporate one type and all of its subtypes into the
 // collection of used types.
 //
 void FindUsedTypes::IncorporateType(const Type *Ty) {
-  if (UsedTypes.count(Ty)) return;  // Already contain Ty.
-                             
-  // If ty doesn't already exist in the used types map, add it now.
-  //
-  UsedTypes.insert(Ty);
-  
+  // If ty doesn't already exist in the used types map, add it now, otherwise
+  // return.
+  if (!UsedTypes.insert(Ty).second) return;  // Already contain Ty.
+
   // Make sure to add any types this type references now.
   //
   for (Type::subtype_iterator I = Ty->subtype_begin(), E = Ty->subtype_end();
@@ -34,46 +40,49 @@ void FindUsedTypes::IncorporateType(const Type *Ty) {
     IncorporateType(*I);
 }
 
-// IncorporateSymbolTable - Add all types referenced by the specified symtab
-// into the collection of used types.
-//
-void FindUsedTypes::IncorporateSymbolTable(const SymbolTable *ST) {
-  assert(0 && "Unimp");
+void FindUsedTypes::IncorporateValue(const Value *V) {
+  IncorporateType(V->getType());
+
+  // If this is a constant, it could be using other types...
+  if (const Constant *C = dyn_cast<Constant>(V)) {
+    if (!isa<GlobalValue>(C))
+      for (User::const_op_iterator OI = C->op_begin(), OE = C->op_end();
+           OI != OE; ++OI)
+        IncorporateValue(*OI);
+  }
 }
 
+
 // run - This incorporates all types used by the specified module
 //
-bool FindUsedTypes::run(Module *m) {
+bool FindUsedTypes::runOnModule(Module &m) {
   UsedTypes.clear();  // reset if run multiple times...
 
-  if (IncludeSymbolTables && m->hasSymbolTable())
-    IncorporateSymbolTable(m->getSymbolTable()); // Add symtab first...
-
   // Loop over global variables, incorporating their types
-  for (Module::const_giterator I = m->gbegin(), E = m->gend(); I != E; ++I)
-    IncorporateType((*I)->getType());
+  for (Module::const_global_iterator I = m.global_begin(), E = m.global_end(); I != E; ++I) {
+    IncorporateType(I->getType());
+    if (I->hasInitializer())
+      IncorporateValue(I->getInitializer());
+  }
+
+  for (Module::iterator MI = m.begin(), ME = m.end(); MI != ME; ++MI) {
+    IncorporateType(MI->getType());
+    const Function &F = *MI;
 
-  for (Module::iterator MI = m->begin(), ME = m->end(); MI != ME; ++MI) {
-    const Function *M = *MI;
-    if (IncludeSymbolTables && M->hasSymbolTable())
-      IncorporateSymbolTable(M->getSymbolTable()); // Add symtab first...
-  
     // Loop over all of the instructions in the function, adding their return
     // type as well as the types of their operands.
     //
-    for (const_inst_iterator II = inst_begin(M), IE = inst_end(M);
+    for (const_inst_iterator II = inst_begin(F), IE = inst_end(F);
          II != IE; ++II) {
-      const Instruction *I = *II;
-      const Type *Ty = I->getType();
-    
-      IncorporateType(Ty);  // Incorporate the type of the instruction
-      for (User::const_op_iterator OI = I->op_begin(), OE = I->op_end();
+      const Instruction &I = *II;
+
+      IncorporateType(I.getType());  // Incorporate the type of the instruction
+      for (User::const_op_iterator OI = I.op_begin(), OE = I.op_end();
            OI != OE; ++OI)
-        if ((*OI)->getType() != Ty)         // Avoid set lookup in common case
-          IncorporateType((*OI)->getType());// Insert inst operand types as well
+        IncorporateValue(*OI);  // Insert inst operand types as well
     }
   }
+
   return false;
 }
 
@@ -81,23 +90,12 @@ bool FindUsedTypes::run(Module *m) {
 // passed in, then the types are printed symbolically if possible, using the
 // symbol table from the module.
 //
-void FindUsedTypes::printTypes(std::ostream &o, const Module *M = 0) const {
+void FindUsedTypes::print(std::ostream &o, const Module *M) const {
   o << "Types in use by this module:\n";
-  if (M) {
-    CachedWriter CW(M, o);
-    for (std::set<const Type *>::const_iterator I = UsedTypes.begin(),
-           E = UsedTypes.end(); I != E; ++I)
-      CW << "  " << *I << "\n";
-  } else
-    for (std::set<const Type *>::const_iterator I = UsedTypes.begin(),
-           E = UsedTypes.end(); I != E; ++I)
-      o << "  " << *I << "\n";
+  for (std::set<const Type *>::const_iterator I = UsedTypes.begin(),
+       E = UsedTypes.end(); I != E; ++I)
+    WriteTypeSymbolic(o << "  ", *I, M) << "\n";
 }
 
-// getAnalysisUsageInfo - Of course, we provide ourself...
-//
-void FindUsedTypes::getAnalysisUsageInfo(Pass::AnalysisSet &Required,
-                                         Pass::AnalysisSet &Destroyed,
-                                         Pass::AnalysisSet &Provided) {
-  Provided.push_back(FindUsedTypes::ID);
-}
+// Ensure that this file gets linked in when FindUsedTypes.h is used.
+DEFINING_FILE_FOR(FindUsedTypes)