blob: 5571995c7e5fd4b61d3b878a4856d55db9733ceb [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 Anderson2ad52172009-07-21 02:47:59 +000018#include "llvm/LLVMContext.h"
19#include "llvm/DerivedTypes.h"
Owen Anderson20b34ac2009-07-16 18:04:31 +000020#include "llvm/System/RWMutex.h"
Owen Andersonc277dc42009-07-16 19:05:41 +000021#include "llvm/ADT/APFloat.h"
Owen Anderson20b34ac2009-07-16 18:04:31 +000022#include "llvm/ADT/APInt.h"
23#include "llvm/ADT/DenseMap.h"
Owen Anderson4118dde2009-07-16 23:44:30 +000024#include "llvm/ADT/FoldingSet.h"
Owen Anderson69ab4162009-07-16 22:11:26 +000025#include "llvm/ADT/StringMap.h"
Owen Anderson8e66e0b2009-06-30 00:48:55 +000026
Owen Anderson20b34ac2009-07-16 18:04:31 +000027namespace llvm {
28
29class ConstantInt;
Owen Andersonc277dc42009-07-16 19:05:41 +000030class ConstantFP;
Owen Anderson69ab4162009-07-16 22:11:26 +000031class MDString;
Owen Anderson4118dde2009-07-16 23:44:30 +000032class MDNode;
Owen Anderson20b34ac2009-07-16 18:04:31 +000033class LLVMContext;
34class Type;
Owen Anderson4118dde2009-07-16 23:44:30 +000035class Value;
Owen Anderson20b34ac2009-07-16 18:04:31 +000036
37struct DenseMapAPIntKeyInfo {
38 struct KeyTy {
39 APInt val;
40 const Type* type;
41 KeyTy(const APInt& V, const Type* Ty) : val(V), type(Ty) {}
42 KeyTy(const KeyTy& that) : val(that.val), type(that.type) {}
43 bool operator==(const KeyTy& that) const {
44 return type == that.type && this->val == that.val;
45 }
46 bool operator!=(const KeyTy& that) const {
47 return !this->operator==(that);
48 }
49 };
50 static inline KeyTy getEmptyKey() { return KeyTy(APInt(1,0), 0); }
51 static inline KeyTy getTombstoneKey() { return KeyTy(APInt(1,1), 0); }
52 static unsigned getHashValue(const KeyTy &Key) {
53 return DenseMapInfo<void*>::getHashValue(Key.type) ^
54 Key.val.getHashValue();
55 }
56 static bool isEqual(const KeyTy &LHS, const KeyTy &RHS) {
57 return LHS == RHS;
58 }
59 static bool isPod() { return false; }
60};
61
Owen Andersonc277dc42009-07-16 19:05:41 +000062struct DenseMapAPFloatKeyInfo {
63 struct KeyTy {
64 APFloat val;
65 KeyTy(const APFloat& V) : val(V){}
66 KeyTy(const KeyTy& that) : val(that.val) {}
67 bool operator==(const KeyTy& that) const {
68 return this->val.bitwiseIsEqual(that.val);
69 }
70 bool operator!=(const KeyTy& that) const {
71 return !this->operator==(that);
72 }
73 };
74 static inline KeyTy getEmptyKey() {
75 return KeyTy(APFloat(APFloat::Bogus,1));
76 }
77 static inline KeyTy getTombstoneKey() {
78 return KeyTy(APFloat(APFloat::Bogus,2));
79 }
80 static unsigned getHashValue(const KeyTy &Key) {
81 return Key.val.getHashValue();
82 }
83 static bool isEqual(const KeyTy &LHS, const KeyTy &RHS) {
84 return LHS == RHS;
85 }
86 static bool isPod() { return false; }
87};
88
Owen Anderson20b34ac2009-07-16 18:04:31 +000089class LLVMContextImpl {
90 sys::SmartRWMutex<true> ConstantsLock;
91
92 typedef DenseMap<DenseMapAPIntKeyInfo::KeyTy, ConstantInt*,
93 DenseMapAPIntKeyInfo> IntMapTy;
94 IntMapTy IntConstants;
95
Owen Andersonc277dc42009-07-16 19:05:41 +000096 typedef DenseMap<DenseMapAPFloatKeyInfo::KeyTy, ConstantFP*,
97 DenseMapAPFloatKeyInfo> FPMapTy;
98 FPMapTy FPConstants;
99
Owen Anderson69ab4162009-07-16 22:11:26 +0000100 StringMap<MDString*> MDStringCache;
101
Owen Anderson4118dde2009-07-16 23:44:30 +0000102 FoldingSet<MDNode> MDNodeSet;
103
Owen Anderson20b34ac2009-07-16 18:04:31 +0000104 LLVMContext &Context;
Owen Anderson2ad52172009-07-21 02:47:59 +0000105 ConstantInt *TheTrueVal;
106 ConstantInt *TheFalseVal;
107
Owen Anderson20b34ac2009-07-16 18:04:31 +0000108 LLVMContextImpl();
109 LLVMContextImpl(const LLVMContextImpl&);
110public:
Owen Anderson2ad52172009-07-21 02:47:59 +0000111 LLVMContextImpl(LLVMContext &C) : Context(C), TheTrueVal(0), TheFalseVal(0) {}
Owen Anderson20b34ac2009-07-16 18:04:31 +0000112
113 /// Return a ConstantInt with the specified value and an implied Type. The
114 /// type is the integer type that corresponds to the bit width of the value.
Owen Andersonc277dc42009-07-16 19:05:41 +0000115 ConstantInt *getConstantInt(const APInt &V);
116
117 ConstantFP *getConstantFP(const APFloat &V);
Owen Anderson69ab4162009-07-16 22:11:26 +0000118
119 MDString *getMDString(const char *StrBegin, const char *StrEnd);
120
Owen Anderson4118dde2009-07-16 23:44:30 +0000121 MDNode *getMDNode(Value*const* Vals, unsigned NumVals);
Owen Anderson69ab4162009-07-16 22:11:26 +0000122
Owen Anderson2ad52172009-07-21 02:47:59 +0000123 ConstantInt *getConstantIntTrue() {
124 if (TheTrueVal)
125 return TheTrueVal;
126 else
127 return (TheTrueVal = Context.getConstantInt(IntegerType::get(1), 1));
128 }
129
130 ConstantInt *getConstantIntFalse() {
131 if (TheFalseVal)
132 return TheFalseVal;
133 else
134 return (TheFalseVal = Context.getConstantInt(IntegerType::get(1), 0));
135 }
136
Owen Anderson69ab4162009-07-16 22:11:26 +0000137 void erase(MDString *M);
Owen Anderson4118dde2009-07-16 23:44:30 +0000138 void erase(MDNode *M);
Owen Anderson8e66e0b2009-06-30 00:48:55 +0000139};
140
141}
142
Owen Anderson36f62e52009-06-30 17:06:46 +0000143#endif