From 6ab910b946b6af54384662fb404d6e9b22fa5727 Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Tue, 19 Aug 2008 04:36:02 +0000 Subject: rearrange code and make more legible. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@54976 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/VMCore/AsmWriter.cpp | 531 +++++++++++++++++++++++++---------------------- 1 file changed, 280 insertions(+), 251 deletions(-) (limited to 'lib/VMCore') diff --git a/lib/VMCore/AsmWriter.cpp b/lib/VMCore/AsmWriter.cpp index 26383d8170..5d98505d44 100644 --- a/lib/VMCore/AsmWriter.cpp +++ b/lib/VMCore/AsmWriter.cpp @@ -40,78 +40,6 @@ using namespace llvm; // Make virtual table appear in this compilation unit. AssemblyAnnotationWriter::~AssemblyAnnotationWriter() {} -namespace { - -/// This class provides computation of slot numbers for LLVM Assembly writing. -/// -class SlotTracker { -public: - /// ValueMap - A mapping of Values to slot numbers - typedef DenseMap ValueMap; - -private: - /// TheModule - The module for which we are holding slot numbers - const Module* TheModule; - - /// TheFunction - The function for which we are holding slot numbers - const Function* TheFunction; - bool FunctionProcessed; - - /// mMap - The TypePlanes map for the module level data - ValueMap mMap; - unsigned mNext; - - /// fMap - The TypePlanes map for the function level data - ValueMap fMap; - unsigned fNext; - -public: - /// Construct from a module - explicit SlotTracker(const Module *M); - /// Construct from a function, starting out in incorp state. - explicit SlotTracker(const Function *F); - - /// Return the slot number of the specified value in it's type - /// plane. If something is not in the SlotTracker, return -1. - int getLocalSlot(const Value *V); - int getGlobalSlot(const GlobalValue *V); - - /// If you'd like to deal with a function instead of just a module, use - /// this method to get its data into the SlotTracker. - void incorporateFunction(const Function *F) { - TheFunction = F; - FunctionProcessed = false; - } - - /// After calling incorporateFunction, use this method to remove the - /// most recently incorporated function from the SlotTracker. This - /// will reset the state of the machine back to just the module contents. - void purgeFunction(); - - // Implementation Details -private: - /// This function does the actual initialization. - inline void initialize(); - - /// CreateModuleSlot - Insert the specified GlobalValue* into the slot table. - void CreateModuleSlot(const GlobalValue *V); - - /// CreateFunctionSlot - Insert the specified Value* into the slot table. - void CreateFunctionSlot(const Value *V); - - /// Add all of the module level global variables (and their initializers) - /// and function declarations, but not the contents of those functions. - void processModule(); - - /// Add all of the functions arguments, basic blocks, and instructions - void processFunction(); - - SlotTracker(const SlotTracker &); // DO NOT IMPLEMENT - void operator=(const SlotTracker &); // DO NOT IMPLEMENT -}; - -} // end anonymous namespace - char PrintModulePass::ID = 0; static RegisterPass X("printm", "Print module to stderr"); @@ -119,39 +47,28 @@ char PrintFunctionPass::ID = 0; static RegisterPass Y("print","Print function to stderr"); -static void WriteAsOperandInternal(std::ostream &Out, const Value *V, - std::map &TypeTable, - SlotTracker *Machine); + +//===----------------------------------------------------------------------===// +// Helper Functions +//===----------------------------------------------------------------------===// static const Module *getModuleFromVal(const Value *V) { if (const Argument *MA = dyn_cast(V)) return MA->getParent() ? MA->getParent()->getParent() : 0; - else if (const BasicBlock *BB = dyn_cast(V)) + + if (const BasicBlock *BB = dyn_cast(V)) return BB->getParent() ? BB->getParent()->getParent() : 0; - else if (const Instruction *I = dyn_cast(V)) { + + if (const Instruction *I = dyn_cast(V)) { const Function *M = I->getParent() ? I->getParent()->getParent() : 0; return M ? M->getParent() : 0; - } else if (const GlobalValue *GV = dyn_cast(V)) + } + + if (const GlobalValue *GV = dyn_cast(V)) return GV->getParent(); return 0; } -static SlotTracker *createSlotTracker(const Value *V) { - if (const Argument *FA = dyn_cast(V)) { - return new SlotTracker(FA->getParent()); - } else if (const Instruction *I = dyn_cast(V)) { - return new SlotTracker(I->getParent()->getParent()); - } else if (const BasicBlock *BB = dyn_cast(V)) { - return new SlotTracker(BB->getParent()); - } else if (const GlobalVariable *GV = dyn_cast(V)){ - return new SlotTracker(GV->getParent()); - } else if (const GlobalAlias *GA = dyn_cast(V)){ - return new SlotTracker(GA->getParent()); - } else if (const Function *Func = dyn_cast(V)) { - return new SlotTracker(Func); - } - return 0; -} /// NameNeedsQuotes - Return true if the specified llvm name should be wrapped /// with ""'s. @@ -214,10 +131,10 @@ static void PrintLLVMName(std::ostream &OS, const ValueName *Name, PrefixType Prefix) { assert(Name && "Cannot get empty name!"); switch (Prefix) { - default: assert(0 && "Bad prefix!"); - case GlobalPrefix: OS << '@'; break; - case LabelPrefix: break; - case LocalPrefix: OS << '%'; break; + default: assert(0 && "Bad prefix!"); + case GlobalPrefix: OS << '@'; break; + case LabelPrefix: break; + case LocalPrefix: OS << '%'; break; } // Scan the name to see if it needs quotes first. @@ -234,7 +151,7 @@ static void PrintLLVMName(std::ostream &OS, const ValueName *Name, } } } - + // If we didn't need any quotes, just write out the name in one blast. if (!NeedsQuotes) { OS.write(NameStr, NameLen); @@ -268,12 +185,276 @@ static void PrintLLVMName(std::ostream &OS, const ValueName *Name, OS << '"'; } +/// PrintLLVMName - Turn the specified name into an 'LLVM name', which is either +/// prefixed with % (if the string only contains simple characters) or is +/// surrounded with ""'s (if it has special chars in it). Print it out. static void PrintLLVMName(std::ostream &OS, const Value *V) { PrintLLVMName(OS, V->getValueName(), isa(V) ? GlobalPrefix : LocalPrefix); } + +//===----------------------------------------------------------------------===// +// SlotTracker Class: Enumerate slot numbers for unnamed values +//===----------------------------------------------------------------------===// + +namespace { + +/// This class provides computation of slot numbers for LLVM Assembly writing. +/// +class SlotTracker { +public: + /// ValueMap - A mapping of Values to slot numbers + typedef DenseMap ValueMap; + +private: + /// TheModule - The module for which we are holding slot numbers + const Module* TheModule; + + /// TheFunction - The function for which we are holding slot numbers + const Function* TheFunction; + bool FunctionProcessed; + + /// mMap - The TypePlanes map for the module level data + ValueMap mMap; + unsigned mNext; + + /// fMap - The TypePlanes map for the function level data + ValueMap fMap; + unsigned fNext; + +public: + /// Construct from a module + explicit SlotTracker(const Module *M); + /// Construct from a function, starting out in incorp state. + explicit SlotTracker(const Function *F); + + /// Return the slot number of the specified value in it's type + /// plane. If something is not in the SlotTracker, return -1. + int getLocalSlot(const Value *V); + int getGlobalSlot(const GlobalValue *V); + + /// If you'd like to deal with a function instead of just a module, use + /// this method to get its data into the SlotTracker. + void incorporateFunction(const Function *F) { + TheFunction = F; + FunctionProcessed = false; + } + + /// After calling incorporateFunction, use this method to remove the + /// most recently incorporated function from the SlotTracker. This + /// will reset the state of the machine back to just the module contents. + void purgeFunction(); + + // Implementation Details +private: + /// This function does the actual initialization. + inline void initialize(); + + /// CreateModuleSlot - Insert the specified GlobalValue* into the slot table. + void CreateModuleSlot(const GlobalValue *V); + + /// CreateFunctionSlot - Insert the specified Value* into the slot table. + void CreateFunctionSlot(const Value *V); + + /// Add all of the module level global variables (and their initializers) + /// and function declarations, but not the contents of those functions. + void processModule(); + + /// Add all of the functions arguments, basic blocks, and instructions + void processFunction(); + + SlotTracker(const SlotTracker &); // DO NOT IMPLEMENT + void operator=(const SlotTracker &); // DO NOT IMPLEMENT +}; + +} // end anonymous namespace + + +static SlotTracker *createSlotTracker(const Value *V) { + if (const Argument *FA = dyn_cast(V)) + return new SlotTracker(FA->getParent()); + + if (const Instruction *I = dyn_cast(V)) + return new SlotTracker(I->getParent()->getParent()); + + if (const BasicBlock *BB = dyn_cast(V)) + return new SlotTracker(BB->getParent()); + + if (const GlobalVariable *GV = dyn_cast(V)) + return new SlotTracker(GV->getParent()); + + if (const GlobalAlias *GA = dyn_cast(V)) + return new SlotTracker(GA->getParent()); + + if (const Function *Func = dyn_cast(V)) + return new SlotTracker(Func); + + return 0; +} + +#if 0 +#define SC_DEBUG(X) cerr << X +#else +#define SC_DEBUG(X) +#endif + +// Module level constructor. Causes the contents of the Module (sans functions) +// to be added to the slot table. +SlotTracker::SlotTracker(const Module *M) +: TheModule(M) ///< Saved for lazy initialization. +, TheFunction(0) +, FunctionProcessed(false) +, mNext(0), fNext(0) +{ +} + +// Function level constructor. Causes the contents of the Module and the one +// function provided to be added to the slot table. +SlotTracker::SlotTracker(const Function *F) +: TheModule(F ? F->getParent() : 0) ///< Saved for lazy initialization +, TheFunction(F) ///< Saved for lazy initialization +, FunctionProcessed(false) +, mNext(0), fNext(0) +{ +} + +inline void SlotTracker::initialize() { + if (TheModule) { + processModule(); + TheModule = 0; ///< Prevent re-processing next time we're called. + } + if (TheFunction && !FunctionProcessed) + processFunction(); +} + +// Iterate through all the global variables, functions, and global +// variable initializers and create slots for them. +void SlotTracker::processModule() { + SC_DEBUG("begin processModule!\n"); + + // Add all of the unnamed global variables to the value table. + for (Module::const_global_iterator I = TheModule->global_begin(), + E = TheModule->global_end(); I != E; ++I) + if (!I->hasName()) + CreateModuleSlot(I); + + // Add all the unnamed functions to the table. + for (Module::const_iterator I = TheModule->begin(), E = TheModule->end(); + I != E; ++I) + if (!I->hasName()) + CreateModuleSlot(I); + + SC_DEBUG("end processModule!\n"); +} + + +// Process the arguments, basic blocks, and instructions of a function. +void SlotTracker::processFunction() { + SC_DEBUG("begin processFunction!\n"); + fNext = 0; + + // Add all the function arguments with no names. + for(Function::const_arg_iterator AI = TheFunction->arg_begin(), + AE = TheFunction->arg_end(); AI != AE; ++AI) + if (!AI->hasName()) + CreateFunctionSlot(AI); + + SC_DEBUG("Inserting Instructions:\n"); + + // Add all of the basic blocks and instructions with no names. + for (Function::const_iterator BB = TheFunction->begin(), + E = TheFunction->end(); BB != E; ++BB) { + if (!BB->hasName()) + CreateFunctionSlot(BB); + for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I) + if (I->getType() != Type::VoidTy && !I->hasName()) + CreateFunctionSlot(I); + } + + FunctionProcessed = true; + + SC_DEBUG("end processFunction!\n"); +} + +/// Clean up after incorporating a function. This is the only way to get out of +/// the function incorporation state that affects get*Slot/Create*Slot. Function +/// incorporation state is indicated by TheFunction != 0. +void SlotTracker::purgeFunction() { + SC_DEBUG("begin purgeFunction!\n"); + fMap.clear(); // Simply discard the function level map + TheFunction = 0; + FunctionProcessed = false; + SC_DEBUG("end purgeFunction!\n"); +} + +/// getGlobalSlot - Get the slot number of a global value. +int SlotTracker::getGlobalSlot(const GlobalValue *V) { + // Check for uninitialized state and do lazy initialization. + initialize(); + + // Find the type plane in the module map + ValueMap::iterator MI = mMap.find(V); + return MI == mMap.end() ? -1 : MI->second; +} + + +/// getLocalSlot - Get the slot number for a value that is local to a function. +int SlotTracker::getLocalSlot(const Value *V) { + assert(!isa(V) && "Can't get a constant or global slot with this!"); + + // Check for uninitialized state and do lazy initialization. + initialize(); + + ValueMap::iterator FI = fMap.find(V); + return FI == fMap.end() ? -1 : FI->second; +} + + +/// CreateModuleSlot - Insert the specified GlobalValue* into the slot table. +void SlotTracker::CreateModuleSlot(const GlobalValue *V) { + assert(V && "Can't insert a null Value into SlotTracker!"); + assert(V->getType() != Type::VoidTy && "Doesn't need a slot!"); + assert(!V->hasName() && "Doesn't need a slot!"); + + unsigned DestSlot = mNext++; + mMap[V] = DestSlot; + + SC_DEBUG(" Inserting value [" << V->getType() << "] = " << V << " slot=" << + DestSlot << " ["); + // G = Global, F = Function, A = Alias, o = other + SC_DEBUG((isa(V) ? 'G' : + (isa(V) ? 'F' : + (isa(V) ? 'A' : 'o'))) << "]\n"); +} + + +/// CreateSlot - Create a new slot for the specified value if it has no name. +void SlotTracker::CreateFunctionSlot(const Value *V) { + assert(V->getType() != Type::VoidTy && !V->hasName() && + "Doesn't need a slot!"); + + unsigned DestSlot = fNext++; + fMap[V] = DestSlot; + + // G = Global, F = Function, o = other + SC_DEBUG(" Inserting value [" << V->getType() << "] = " << V << " slot=" << + DestSlot << " [o]\n"); +} + + + +//===----------------------------------------------------------------------===// +// AsmWriter Implementation +//===----------------------------------------------------------------------===// + +static void WriteAsOperandInternal(std::ostream &Out, const Value *V, + std::map &TypeTable, + SlotTracker *Machine); + + + /// fillTypeNameTable - If the module has a symbol table, take all global types /// and stuff their names into the TypeNames map. /// @@ -1585,155 +1766,3 @@ void Value::dump() const { print(*cerr.stream()); cerr << '\n'; } // Located here because so much of the needed functionality is here. void Type::dump() const { print(*cerr.stream()); cerr << '\n'; } -//===----------------------------------------------------------------------===// -// SlotTracker Implementation -//===----------------------------------------------------------------------===// - -#if 0 -#define SC_DEBUG(X) cerr << X -#else -#define SC_DEBUG(X) -#endif - -// Module level constructor. Causes the contents of the Module (sans functions) -// to be added to the slot table. -SlotTracker::SlotTracker(const Module *M) - : TheModule(M) ///< Saved for lazy initialization. - , TheFunction(0) - , FunctionProcessed(false) - , mNext(0), fNext(0) -{ -} - -// Function level constructor. Causes the contents of the Module and the one -// function provided to be added to the slot table. -SlotTracker::SlotTracker(const Function *F) - : TheModule(F ? F->getParent() : 0) ///< Saved for lazy initialization - , TheFunction(F) ///< Saved for lazy initialization - , FunctionProcessed(false) - , mNext(0), fNext(0) -{ -} - -inline void SlotTracker::initialize() { - if (TheModule) { - processModule(); - TheModule = 0; ///< Prevent re-processing next time we're called. - } - if (TheFunction && !FunctionProcessed) - processFunction(); -} - -// Iterate through all the global variables, functions, and global -// variable initializers and create slots for them. -void SlotTracker::processModule() { - SC_DEBUG("begin processModule!\n"); - - // Add all of the unnamed global variables to the value table. - for (Module::const_global_iterator I = TheModule->global_begin(), - E = TheModule->global_end(); I != E; ++I) - if (!I->hasName()) - CreateModuleSlot(I); - - // Add all the unnamed functions to the table. - for (Module::const_iterator I = TheModule->begin(), E = TheModule->end(); - I != E; ++I) - if (!I->hasName()) - CreateModuleSlot(I); - - SC_DEBUG("end processModule!\n"); -} - - -// Process the arguments, basic blocks, and instructions of a function. -void SlotTracker::processFunction() { - SC_DEBUG("begin processFunction!\n"); - fNext = 0; - - // Add all the function arguments with no names. - for(Function::const_arg_iterator AI = TheFunction->arg_begin(), - AE = TheFunction->arg_end(); AI != AE; ++AI) - if (!AI->hasName()) - CreateFunctionSlot(AI); - - SC_DEBUG("Inserting Instructions:\n"); - - // Add all of the basic blocks and instructions with no names. - for (Function::const_iterator BB = TheFunction->begin(), - E = TheFunction->end(); BB != E; ++BB) { - if (!BB->hasName()) - CreateFunctionSlot(BB); - for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I) - if (I->getType() != Type::VoidTy && !I->hasName()) - CreateFunctionSlot(I); - } - - FunctionProcessed = true; - - SC_DEBUG("end processFunction!\n"); -} - -/// Clean up after incorporating a function. This is the only way to get out of -/// the function incorporation state that affects get*Slot/Create*Slot. Function -/// incorporation state is indicated by TheFunction != 0. -void SlotTracker::purgeFunction() { - SC_DEBUG("begin purgeFunction!\n"); - fMap.clear(); // Simply discard the function level map - TheFunction = 0; - FunctionProcessed = false; - SC_DEBUG("end purgeFunction!\n"); -} - -/// getGlobalSlot - Get the slot number of a global value. -int SlotTracker::getGlobalSlot(const GlobalValue *V) { - // Check for uninitialized state and do lazy initialization. - initialize(); - - // Find the type plane in the module map - ValueMap::iterator MI = mMap.find(V); - return MI == mMap.end() ? -1 : MI->second; -} - - -/// getLocalSlot - Get the slot number for a value that is local to a function. -int SlotTracker::getLocalSlot(const Value *V) { - assert(!isa(V) && "Can't get a constant or global slot with this!"); - - // Check for uninitialized state and do lazy initialization. - initialize(); - - ValueMap::iterator FI = fMap.find(V); - return FI == fMap.end() ? -1 : FI->second; -} - - -/// CreateModuleSlot - Insert the specified GlobalValue* into the slot table. -void SlotTracker::CreateModuleSlot(const GlobalValue *V) { - assert(V && "Can't insert a null Value into SlotTracker!"); - assert(V->getType() != Type::VoidTy && "Doesn't need a slot!"); - assert(!V->hasName() && "Doesn't need a slot!"); - - unsigned DestSlot = mNext++; - mMap[V] = DestSlot; - - SC_DEBUG(" Inserting value [" << V->getType() << "] = " << V << " slot=" << - DestSlot << " ["); - // G = Global, F = Function, A = Alias, o = other - SC_DEBUG((isa(V) ? 'G' : - (isa(V) ? 'F' : - (isa(V) ? 'A' : 'o'))) << "]\n"); -} - - -/// CreateSlot - Create a new slot for the specified value if it has no name. -void SlotTracker::CreateFunctionSlot(const Value *V) { - assert(V->getType() != Type::VoidTy && !V->hasName() && - "Doesn't need a slot!"); - - unsigned DestSlot = fNext++; - fMap[V] = DestSlot; - - // G = Global, F = Function, o = other - SC_DEBUG(" Inserting value [" << V->getType() << "] = " << V << " slot=" << - DestSlot << " [o]\n"); -} -- cgit v1.2.3