blob: 129a759cd99f12e8ce6e15197eac035910114b3f [file] [log] [blame]
Owen Anderson20b34ac2009-07-16 18:04:31 +00001//===----------------- LLVMContextImpl.h - Implementation ------*- C++ -*--===//
Owen Anderson8e66e0b2009-06-30 00:48:55 +00002//
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//===----------------------------------------------------------------------===//
Owen Anderson36f62e52009-06-30 17:06:46 +00009//
10// This file declares LLVMContextImpl, the opaque implementation
11// of LLVMContext.
12//
13//===----------------------------------------------------------------------===//
Owen Anderson8e66e0b2009-06-30 00:48:55 +000014
15#ifndef LLVM_LLVMCONTEXT_IMPL_H
16#define LLVM_LLVMCONTEXT_IMPL_H
17
Owen Anderson20b34ac2009-07-16 18:04:31 +000018#include "llvm/System/RWMutex.h"
Owen Andersonc277dc42009-07-16 19:05:41 +000019#include "llvm/ADT/APFloat.h"
Owen Anderson20b34ac2009-07-16 18:04:31 +000020#include "llvm/ADT/APInt.h"
21#include "llvm/ADT/DenseMap.h"
Owen Anderson4118dde2009-07-16 23:44:30 +000022#include "llvm/ADT/FoldingSet.h"
Owen Anderson69ab4162009-07-16 22:11:26 +000023#include "llvm/ADT/StringMap.h"
Owen Anderson8e66e0b2009-06-30 00:48:55 +000024
Owen Anderson20b34ac2009-07-16 18:04:31 +000025namespace llvm {
26
27class ConstantInt;
Owen Andersonc277dc42009-07-16 19:05:41 +000028class ConstantFP;
Owen Anderson69ab4162009-07-16 22:11:26 +000029class MDString;
Owen Anderson4118dde2009-07-16 23:44:30 +000030class MDNode;
Owen Anderson20b34ac2009-07-16 18:04:31 +000031class LLVMContext;
32class Type;
Owen Anderson4118dde2009-07-16 23:44:30 +000033class Value;
Owen Anderson20b34ac2009-07-16 18:04:31 +000034
35struct DenseMapAPIntKeyInfo {
36 struct KeyTy {
37 APInt val;
38 const Type* type;
39 KeyTy(const APInt& V, const Type* Ty) : val(V), type(Ty) {}
40 KeyTy(const KeyTy& that) : val(that.val), type(that.type) {}
41 bool operator==(const KeyTy& that) const {
42 return type == that.type && this->val == that.val;
43 }
44 bool operator!=(const KeyTy& that) const {
45 return !this->operator==(that);
46 }
47 };
48 static inline KeyTy getEmptyKey() { return KeyTy(APInt(1,0), 0); }
49 static inline KeyTy getTombstoneKey() { return KeyTy(APInt(1,1), 0); }
50 static unsigned getHashValue(const KeyTy &Key) {
51 return DenseMapInfo<void*>::getHashValue(Key.type) ^
52 Key.val.getHashValue();
53 }
54 static bool isEqual(const KeyTy &LHS, const KeyTy &RHS) {
55 return LHS == RHS;
56 }
57 static bool isPod() { return false; }
58};
59
Owen Andersonc277dc42009-07-16 19:05:41 +000060struct DenseMapAPFloatKeyInfo {
61 struct KeyTy {
62 APFloat val;
63 KeyTy(const APFloat& V) : val(V){}
64 KeyTy(const KeyTy& that) : val(that.val) {}
65 bool operator==(const KeyTy& that) const {
66 return this->val.bitwiseIsEqual(that.val);
67 }
68 bool operator!=(const KeyTy& that) const {
69 return !this->operator==(that);
70 }
71 };
72 static inline KeyTy getEmptyKey() {
73 return KeyTy(APFloat(APFloat::Bogus,1));
74 }
75 static inline KeyTy getTombstoneKey() {
76 return KeyTy(APFloat(APFloat::Bogus,2));
77 }
78 static unsigned getHashValue(const KeyTy &Key) {
79 return Key.val.getHashValue();
80 }
81 static bool isEqual(const KeyTy &LHS, const KeyTy &RHS) {
82 return LHS == RHS;
83 }
84 static bool isPod() { return false; }
85};
86
Owen Anderson20b34ac2009-07-16 18:04:31 +000087class LLVMContextImpl {
88 sys::SmartRWMutex<true> ConstantsLock;
89
90 typedef DenseMap<DenseMapAPIntKeyInfo::KeyTy, ConstantInt*,
91 DenseMapAPIntKeyInfo> IntMapTy;
92 IntMapTy IntConstants;
93
Owen Andersonc277dc42009-07-16 19:05:41 +000094 typedef DenseMap<DenseMapAPFloatKeyInfo::KeyTy, ConstantFP*,
95 DenseMapAPFloatKeyInfo> FPMapTy;
96 FPMapTy FPConstants;
97
Owen Anderson69ab4162009-07-16 22:11:26 +000098 StringMap<MDString*> MDStringCache;
99
Owen Anderson4118dde2009-07-16 23:44:30 +0000100 FoldingSet<MDNode> MDNodeSet;
101
Owen Anderson20b34ac2009-07-16 18:04:31 +0000102 LLVMContext &Context;
103 LLVMContextImpl();
104 LLVMContextImpl(const LLVMContextImpl&);
105public:
106 LLVMContextImpl(LLVMContext &C) : Context(C) { }
107
108 /// Return a ConstantInt with the specified value and an implied Type. The
109 /// type is the integer type that corresponds to the bit width of the value.
Owen Andersonc277dc42009-07-16 19:05:41 +0000110 ConstantInt *getConstantInt(const APInt &V);
111
112 ConstantFP *getConstantFP(const APFloat &V);
Owen Anderson69ab4162009-07-16 22:11:26 +0000113
114 MDString *getMDString(const char *StrBegin, const char *StrEnd);
115
Owen Anderson4118dde2009-07-16 23:44:30 +0000116 MDNode *getMDNode(Value*const* Vals, unsigned NumVals);
Owen Anderson69ab4162009-07-16 22:11:26 +0000117
118 void erase(MDString *M);
Owen Anderson4118dde2009-07-16 23:44:30 +0000119 void erase(MDNode *M);
Owen Anderson8e66e0b2009-06-30 00:48:55 +0000120};
121
122}
123
Owen Anderson36f62e52009-06-30 17:06:46 +0000124#endif