5efc1ebe5e7648f3b80f30c198d122d7265dd547
[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/MDNode.h"
20 using namespace llvm;
21
22 static char getValType(ConstantAggregateZero *CPZ) { return 0; }
23
24 LLVMContextImpl::LLVMContextImpl(LLVMContext &C) :
25     Context(C), TheTrueVal(0), TheFalseVal(0) { }
26
27 MDString *LLVMContextImpl::getMDString(const char *StrBegin,
28                                        unsigned StrLength) {
29   sys::SmartScopedWriter<true> Writer(ConstantsLock);
30   StringMapEntry<MDString *> &Entry = 
31     MDStringCache.GetOrCreateValue(StringRef(StrBegin, StrLength));
32   MDString *&S = Entry.getValue();
33   if (!S) S = new MDString(Entry.getKeyData(),
34                            Entry.getKeyLength());
35
36   return S;
37 }
38
39 MDNode *LLVMContextImpl::getMDNode(Value*const* Vals, unsigned NumVals) {
40   FoldingSetNodeID ID;
41   for (unsigned i = 0; i != NumVals; ++i)
42     ID.AddPointer(Vals[i]);
43
44   ConstantsLock.reader_acquire();
45   void *InsertPoint;
46   MDNode *N = MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint);
47   ConstantsLock.reader_release();
48   
49   if (!N) {
50     sys::SmartScopedWriter<true> Writer(ConstantsLock);
51     N = MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint);
52     if (!N) {
53       // InsertPoint will have been set by the FindNodeOrInsertPos call.
54       N = new MDNode(Vals, NumVals);
55       MDNodeSet.InsertNode(N, InsertPoint);
56     }
57   }
58
59   return N;
60 }
61
62 ConstantAggregateZero*
63 LLVMContextImpl::getConstantAggregateZero(const Type *Ty) {
64   assert((isa<StructType>(Ty) || isa<ArrayType>(Ty) || isa<VectorType>(Ty)) &&
65          "Cannot create an aggregate zero of non-aggregate type!");
66
67   // Implicitly locked.
68   return AggZeroConstants.getOrCreate(Ty, 0);
69 }
70
71 // *** erase methods ***
72
73 void LLVMContextImpl::erase(MDString *M) {
74   sys::SmartScopedWriter<true> Writer(ConstantsLock);
75   MDStringCache.erase(MDStringCache.find(M->getString()));
76 }
77
78 void LLVMContextImpl::erase(MDNode *M) {
79   sys::SmartScopedWriter<true> Writer(ConstantsLock);
80   MDNodeSet.RemoveNode(M);
81 }
82
83 void LLVMContextImpl::erase(ConstantAggregateZero *Z) {
84   AggZeroConstants.remove(Z);
85 }