Fix a major source of compile-time slowness at -O0 -g by optimizing
[oota-llvm.git] / lib / VMCore / LLVMContext.cpp
1 //===-- LLVMContext.cpp - Implement LLVMContext -----------------------===//
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 LLVMContext, as a wrapper around the opaque
11 //  class LLVMContextImpl.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "llvm/LLVMContext.h"
16 #include "llvm/Metadata.h"
17 #include "llvm/Constants.h"
18 #include "llvm/Instruction.h"
19 #include "llvm/Support/ManagedStatic.h"
20 #include "LLVMContextImpl.h"
21 using namespace llvm;
22
23 static ManagedStatic<LLVMContext> GlobalContext;
24
25 LLVMContext& llvm::getGlobalContext() {
26   return *GlobalContext;
27 }
28
29 LLVMContext::LLVMContext() : pImpl(new LLVMContextImpl(*this)) {
30   // Create the first metadata kind, which is always 'dbg'.
31   unsigned DbgID = getMDKindID("dbg");
32   assert(DbgID == MD_dbg && "dbg kind id drifted"); (void)DbgID;
33 }
34 LLVMContext::~LLVMContext() { delete pImpl; }
35
36
37 #ifndef NDEBUG
38 /// isValidName - Return true if Name is a valid custom metadata handler name.
39 static bool isValidName(StringRef MDName) {
40   if (MDName.empty())
41     return false;
42   
43   if (!isalpha(MDName[0]))
44     return false;
45   
46   for (StringRef::iterator I = MDName.begin() + 1, E = MDName.end(); I != E;
47        ++I) {
48     if (!isalnum(*I) && *I != '_' && *I != '-' && *I != '.')
49       return false;
50   }
51   return true;
52 }
53 #endif
54
55 /// getMDKindID - Return a unique non-zero ID for the specified metadata kind.
56 unsigned LLVMContext::getMDKindID(StringRef Name) const {
57   assert(isValidName(Name) && "Invalid MDNode name");
58   
59   unsigned &Entry = pImpl->CustomMDKindNames[Name];
60   
61   // If this is new, assign it its ID.
62   if (Entry == 0) Entry = pImpl->CustomMDKindNames.size();
63   return Entry;
64 }
65
66 /// getHandlerNames - Populate client supplied smallvector using custome
67 /// metadata name and ID.
68 void LLVMContext::getMDKindNames(SmallVectorImpl<StringRef> &Names) const {
69   Names.resize(pImpl->CustomMDKindNames.size()+1);
70   Names[0] = "";
71   for (StringMap<unsigned>::const_iterator I = pImpl->CustomMDKindNames.begin(),
72        E = pImpl->CustomMDKindNames.end(); I != E; ++I)
73     // MD Handlers are numbered from 1.
74     Names[I->second] = I->first();
75 }
76
77