From e9fa33eb6629aea72cd59560216bb6a7b8ebc689 Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Sat, 28 Feb 2009 23:20:19 +0000 Subject: [PATCH] move type name population out of TypePrinting class into a static AsmWriter.cpp method. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@65736 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/Assembly/Writer.h | 13 ++++- lib/VMCore/AsmWriter.cpp | 87 ++++++++++++++++++++-------------- 2 files changed, 62 insertions(+), 38 deletions(-) diff --git a/include/llvm/Assembly/Writer.h b/include/llvm/Assembly/Writer.h index c9f73eed910..1138919bb4a 100644 --- a/include/llvm/Assembly/Writer.h +++ b/include/llvm/Assembly/Writer.h @@ -29,11 +29,11 @@ template class SmallVectorImpl; /// TypePrinting - Type printing machinery. class TypePrinting { - void *TypeNames; + void *TypeNames; // A map to remember type names. TypePrinting(const TypePrinting &); // DO NOT IMPLEMENT void operator=(const TypePrinting&); // DO NOT IMPLEMENT public: - TypePrinting(const Module *M = 0); + TypePrinting(); ~TypePrinting(); void clear(); @@ -41,6 +41,15 @@ public: void print(const Type *Ty, raw_ostream &OS); void printAtLeastOneLevel(const Type *Ty, raw_ostream &OS); + /// hasTypeName - Return true if the type has a name in TypeNames, false + /// otherwise. + bool hasTypeName(const Type *Ty) const; + + /// addTypeName - Add a name for the specified type if it doesn't already have + /// one. This name will be printed instead of the structural version of the + /// type in order to make the output more concise. + void addTypeName(const Type *Ty, const std::string &N); + private: void CalcTypeName(const Type *Ty, SmallVectorImpl &TypeStack, raw_ostream &OS); diff --git a/lib/VMCore/AsmWriter.cpp b/lib/VMCore/AsmWriter.cpp index b6f8313e556..f9aa2c9b703 100644 --- a/lib/VMCore/AsmWriter.cpp +++ b/lib/VMCore/AsmWriter.cpp @@ -145,36 +145,17 @@ void TypePrinting::clear() { getTypeNamesMap(TypeNames).clear(); } -TypePrinting::TypePrinting(const Module *M) { +bool TypePrinting::hasTypeName(const Type *Ty) const { + return getTypeNamesMap(TypeNames).count(Ty); +} + +void TypePrinting::addTypeName(const Type *Ty, const std::string &N) { + getTypeNamesMap(TypeNames).insert(std::make_pair(Ty, N)); +} + + +TypePrinting::TypePrinting() { TypeNames = new DenseMap(); - if (M == 0) return; - - // If the module has a symbol table, take all global types and stuff their - // names into the TypeNames map. - const TypeSymbolTable &ST = M->getTypeSymbolTable(); - for (TypeSymbolTable::const_iterator TI = ST.begin(), E = ST.end(); - TI != E; ++TI) { - const Type *Ty = cast(TI->second); - - // As a heuristic, don't insert pointer to primitive types, because - // they are used too often to have a single useful name. - if (const PointerType *PTy = dyn_cast(Ty)) { - const Type *PETy = PTy->getElementType(); - if ((PETy->isPrimitiveType() || PETy->isInteger()) && - !isa(PETy)) - continue; - } - - // Likewise don't insert primitives either. - if (Ty->isInteger() || Ty->isPrimitiveType()) - continue; - - // Get the name as a string and insert it into TypeNames. - std::string NameStr; - raw_string_ostream NameOS(NameStr); - PrintLLVMName(NameOS, TI->first.c_str(), TI->first.length(), LocalPrefix); - getTypeNamesMap(TypeNames).insert(std::make_pair(Ty, NameOS.str())); - } } TypePrinting::~TypePrinting() { @@ -337,13 +318,46 @@ void TypePrinting::printAtLeastOneLevel(const Type *Ty, raw_ostream &OS) { std::swap(OldName, I->second); } +static void AddModuleTypesToPrinter(TypePrinting &TP, const Module *M) { + if (M == 0) return; + + // If the module has a symbol table, take all global types and stuff their + // names into the TypeNames map. + const TypeSymbolTable &ST = M->getTypeSymbolTable(); + for (TypeSymbolTable::const_iterator TI = ST.begin(), E = ST.end(); + TI != E; ++TI) { + const Type *Ty = cast(TI->second); + + // As a heuristic, don't insert pointer to primitive types, because + // they are used too often to have a single useful name. + if (const PointerType *PTy = dyn_cast(Ty)) { + const Type *PETy = PTy->getElementType(); + if ((PETy->isPrimitiveType() || PETy->isInteger()) && + !isa(PETy)) + continue; + } + + // Likewise don't insert primitives either. + if (Ty->isInteger() || Ty->isPrimitiveType()) + continue; + + // Get the name as a string and insert it into TypeNames. + std::string NameStr; + raw_string_ostream NameOS(NameStr); + PrintLLVMName(NameOS, TI->first.c_str(), TI->first.length(), LocalPrefix); + TP.addTypeName(Ty, NameOS.str()); + } +} + /// WriteTypeSymbolic - This attempts to write the specified type as a symbolic /// type, iff there is an entry in the modules symbol table for the specified /// type or one of it's component types. /// -void llvm::WriteTypeSymbolic(raw_ostream &OS, const Type *Ty, const Module *M){ - TypePrinting(M).print(Ty, OS); +void llvm::WriteTypeSymbolic(raw_ostream &OS, const Type *Ty, const Module *M) { + TypePrinting Printer; + AddModuleTypesToPrinter(Printer, M); + Printer.print(Ty, OS); } //===----------------------------------------------------------------------===// @@ -918,7 +932,8 @@ void llvm::WriteAsOperand(raw_ostream &Out, const Value *V, bool PrintType, const Module *Context) { if (Context == 0) Context = getModuleFromVal(V); - TypePrinting TypePrinter(Context); + TypePrinting TypePrinter; + AddModuleTypesToPrinter(TypePrinter, Context); if (PrintType) { TypePrinter.print(V->getType(), Out); Out << ' '; @@ -939,8 +954,8 @@ class AssemblyWriter { public: inline AssemblyWriter(raw_ostream &o, SlotTracker &Mac, const Module *M, AssemblyAnnotationWriter *AAW) - : Out(o), Machine(Mac), TheModule(M), TypePrinter(M), - AnnotationWriter(AAW) { + : Out(o), Machine(Mac), TheModule(M), AnnotationWriter(AAW) { + AddModuleTypesToPrinter(TypePrinter, M); } void write(const Module *M) { printModule(M); } @@ -1649,7 +1664,7 @@ void Type::print(raw_ostream &OS) const { OS << ""; return; } - TypePrinting(0).print(this, OS); + TypePrinting().print(this, OS); } void Value::print(raw_ostream &OS, AssemblyAnnotationWriter *AAW) const { @@ -1673,7 +1688,7 @@ void Value::print(raw_ostream &OS, AssemblyAnnotationWriter *AAW) const { AssemblyWriter W(OS, SlotTable, GV->getParent(), 0); W.write(GV); } else if (const Constant *C = dyn_cast(this)) { - TypePrinting TypePrinter(0); + TypePrinting TypePrinter; TypePrinter.print(C->getType(), OS); OS << ' '; WriteConstantInt(OS, C, TypePrinter, 0); -- 2.34.1