Move more code back to 2.5 APIs.
[oota-llvm.git] / lib / VMCore / LLVMContextImpl.cpp
1 //===--------------- LLVMContextImpl.cpp - Implementation ------*- C++ -*--===//
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 LLVMContextImpl, the opaque implementation 
11 //  of LLVMContext.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "LLVMContextImpl.h"
16 #include "llvm/Constants.h"
17 #include "llvm/DerivedTypes.h"
18 #include "llvm/LLVMContext.h"
19 #include "llvm/Metadata.h"
20 using namespace llvm;
21
22 LLVMContextImpl::LLVMContextImpl(LLVMContext &C) :
23     Context(C), TheTrueVal(0), TheFalseVal(0) { }
24
25 MDString *LLVMContextImpl::getMDString(const char *StrBegin,
26                                        unsigned StrLength) {
27   sys::SmartScopedWriter<true> Writer(ConstantsLock);
28   StringMapEntry<MDString *> &Entry = 
29     MDStringCache.GetOrCreateValue(StringRef(StrBegin, StrLength));
30   MDString *&S = Entry.getValue();
31   if (!S) S = new MDString(Entry.getKeyData(),
32                            Entry.getKeyLength());
33
34   return S;
35 }
36
37 MDNode *LLVMContextImpl::getMDNode(Value*const* Vals, unsigned NumVals) {
38   FoldingSetNodeID ID;
39   for (unsigned i = 0; i != NumVals; ++i)
40     ID.AddPointer(Vals[i]);
41
42   ConstantsLock.reader_acquire();
43   void *InsertPoint;
44   MDNode *N = MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint);
45   ConstantsLock.reader_release();
46   
47   if (!N) {
48     sys::SmartScopedWriter<true> Writer(ConstantsLock);
49     N = MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint);
50     if (!N) {
51       // InsertPoint will have been set by the FindNodeOrInsertPos call.
52       N = new MDNode(Vals, NumVals);
53       MDNodeSet.InsertNode(N, InsertPoint);
54     }
55   }
56
57   return N;
58 }
59
60 // *** erase methods ***
61
62 void LLVMContextImpl::erase(MDString *M) {
63   sys::SmartScopedWriter<true> Writer(ConstantsLock);
64   MDStringCache.erase(MDStringCache.find(M->getString()));
65 }
66
67 void LLVMContextImpl::erase(MDNode *M) {
68   sys::SmartScopedWriter<true> Writer(ConstantsLock);
69   MDNodeSet.RemoveNode(M);
70 }