blob: f313ed62078455506cf65635c75a7ab30c69f94d [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 Anderson39ede7b2009-07-21 20:13:12 +000020#include "llvm/Support/Debug.h"
21#include "llvm/Support/ErrorHandling.h"
22#include "llvm/System/Mutex.h"
Owen Anderson20b34ac2009-07-16 18:04:31 +000023#include "llvm/System/RWMutex.h"
Owen Andersonc277dc42009-07-16 19:05:41 +000024#include "llvm/ADT/APFloat.h"
Owen Anderson20b34ac2009-07-16 18:04:31 +000025#include "llvm/ADT/APInt.h"
26#include "llvm/ADT/DenseMap.h"
Owen Anderson4118dde2009-07-16 23:44:30 +000027#include "llvm/ADT/FoldingSet.h"
Owen Anderson69ab4162009-07-16 22:11:26 +000028#include "llvm/ADT/StringMap.h"
Owen Anderson39ede7b2009-07-21 20:13:12 +000029#include <map>
Owen Anderson909f6002009-07-23 23:25:33 +000030#include <vector>
Owen Anderson39ede7b2009-07-21 20:13:12 +000031
32template<class ValType, class TypeClass, class ConstantClass,
33 bool HasLargeKey = false /*true for arrays and structs*/ >
Owen Anderson3d344922009-07-21 20:55:28 +000034class ValueMap;
Owen Anderson8e66e0b2009-06-30 00:48:55 +000035
Owen Anderson20b34ac2009-07-16 18:04:31 +000036namespace llvm {
Owen Anderson39ede7b2009-07-21 20:13:12 +000037template<class ValType>
38struct ConstantTraits;
Owen Anderson20b34ac2009-07-16 18:04:31 +000039
40class ConstantInt;
Owen Andersonc277dc42009-07-16 19:05:41 +000041class ConstantFP;
Owen Anderson69ab4162009-07-16 22:11:26 +000042class MDString;
Owen Anderson4118dde2009-07-16 23:44:30 +000043class MDNode;
Owen Anderson20b34ac2009-07-16 18:04:31 +000044class LLVMContext;
45class Type;
Owen Anderson4118dde2009-07-16 23:44:30 +000046class Value;
Owen Anderson20b34ac2009-07-16 18:04:31 +000047
48struct DenseMapAPIntKeyInfo {
49 struct KeyTy {
50 APInt val;
51 const Type* type;
52 KeyTy(const APInt& V, const Type* Ty) : val(V), type(Ty) {}
53 KeyTy(const KeyTy& that) : val(that.val), type(that.type) {}
54 bool operator==(const KeyTy& that) const {
55 return type == that.type && this->val == that.val;
56 }
57 bool operator!=(const KeyTy& that) const {
58 return !this->operator==(that);
59 }
60 };
61 static inline KeyTy getEmptyKey() { return KeyTy(APInt(1,0), 0); }
62 static inline KeyTy getTombstoneKey() { return KeyTy(APInt(1,1), 0); }
63 static unsigned getHashValue(const KeyTy &Key) {
64 return DenseMapInfo<void*>::getHashValue(Key.type) ^
65 Key.val.getHashValue();
66 }
67 static bool isEqual(const KeyTy &LHS, const KeyTy &RHS) {
68 return LHS == RHS;
69 }
70 static bool isPod() { return false; }
71};
72
Owen Andersonc277dc42009-07-16 19:05:41 +000073struct DenseMapAPFloatKeyInfo {
74 struct KeyTy {
75 APFloat val;
76 KeyTy(const APFloat& V) : val(V){}
77 KeyTy(const KeyTy& that) : val(that.val) {}
78 bool operator==(const KeyTy& that) const {
79 return this->val.bitwiseIsEqual(that.val);
80 }
81 bool operator!=(const KeyTy& that) const {
82 return !this->operator==(that);
83 }
84 };
85 static inline KeyTy getEmptyKey() {
86 return KeyTy(APFloat(APFloat::Bogus,1));
87 }
88 static inline KeyTy getTombstoneKey() {
89 return KeyTy(APFloat(APFloat::Bogus,2));
90 }
91 static unsigned getHashValue(const KeyTy &Key) {
92 return Key.val.getHashValue();
93 }
94 static bool isEqual(const KeyTy &LHS, const KeyTy &RHS) {
95 return LHS == RHS;
96 }
97 static bool isPod() { return false; }
98};
99
Owen Anderson20b34ac2009-07-16 18:04:31 +0000100class LLVMContextImpl {
101 sys::SmartRWMutex<true> ConstantsLock;
102
103 typedef DenseMap<DenseMapAPIntKeyInfo::KeyTy, ConstantInt*,
104 DenseMapAPIntKeyInfo> IntMapTy;
105 IntMapTy IntConstants;
106
Owen Andersonc277dc42009-07-16 19:05:41 +0000107 typedef DenseMap<DenseMapAPFloatKeyInfo::KeyTy, ConstantFP*,
108 DenseMapAPFloatKeyInfo> FPMapTy;
109 FPMapTy FPConstants;
110
Owen Anderson69ab4162009-07-16 22:11:26 +0000111 StringMap<MDString*> MDStringCache;
112
Owen Anderson4118dde2009-07-16 23:44:30 +0000113 FoldingSet<MDNode> MDNodeSet;
114
Owen Anderson3d344922009-07-21 20:55:28 +0000115 ValueMap<char, Type, ConstantAggregateZero> *AggZeroConstants;
116
117 typedef ValueMap<std::vector<Constant*>, ArrayType,
118 ConstantArray, true /*largekey*/> ArrayConstantsTy;
119 ArrayConstantsTy *ArrayConstants;
Owen Anderson39ede7b2009-07-21 20:13:12 +0000120
Owen Anderson909f6002009-07-23 23:25:33 +0000121 typedef ValueMap<std::vector<Constant*>, StructType,
122 ConstantStruct, true /*largekey*/> StructConstantsTy;
123 StructConstantsTy *StructConstants;
124
Owen Anderson20b34ac2009-07-16 18:04:31 +0000125 LLVMContext &Context;
Owen Anderson2ad52172009-07-21 02:47:59 +0000126 ConstantInt *TheTrueVal;
127 ConstantInt *TheFalseVal;
128
Owen Anderson20b34ac2009-07-16 18:04:31 +0000129 LLVMContextImpl();
130 LLVMContextImpl(const LLVMContextImpl&);
131public:
Owen Anderson39ede7b2009-07-21 20:13:12 +0000132 LLVMContextImpl(LLVMContext &C);
133 ~LLVMContextImpl();
Owen Anderson20b34ac2009-07-16 18:04:31 +0000134
135 /// Return a ConstantInt with the specified value and an implied Type. The
136 /// type is the integer type that corresponds to the bit width of the value.
Owen Andersonc277dc42009-07-16 19:05:41 +0000137 ConstantInt *getConstantInt(const APInt &V);
138
139 ConstantFP *getConstantFP(const APFloat &V);
Owen Anderson69ab4162009-07-16 22:11:26 +0000140
Devang Patel62920032009-07-23 02:00:51 +0000141 MDString *getMDString(const char *StrBegin, unsigned StrLength);
Owen Anderson69ab4162009-07-16 22:11:26 +0000142
Owen Anderson4118dde2009-07-16 23:44:30 +0000143 MDNode *getMDNode(Value*const* Vals, unsigned NumVals);
Owen Anderson69ab4162009-07-16 22:11:26 +0000144
Owen Anderson39ede7b2009-07-21 20:13:12 +0000145 ConstantAggregateZero *getConstantAggregateZero(const Type *Ty);
146
Owen Anderson3d344922009-07-21 20:55:28 +0000147 Constant *getConstantArray(const ArrayType *Ty,
148 const std::vector<Constant*> &V);
149
Owen Anderson909f6002009-07-23 23:25:33 +0000150 Constant *getConstantStruct(const StructType *Ty,
151 const std::vector<Constant*> &V);
152
Owen Andersonc37bc692009-07-21 18:03:38 +0000153 ConstantInt *getTrue() {
Owen Anderson2ad52172009-07-21 02:47:59 +0000154 if (TheTrueVal)
155 return TheTrueVal;
156 else
157 return (TheTrueVal = Context.getConstantInt(IntegerType::get(1), 1));
158 }
159
Owen Andersonc37bc692009-07-21 18:03:38 +0000160 ConstantInt *getFalse() {
Owen Anderson2ad52172009-07-21 02:47:59 +0000161 if (TheFalseVal)
162 return TheFalseVal;
163 else
164 return (TheFalseVal = Context.getConstantInt(IntegerType::get(1), 0));
165 }
166
Owen Anderson69ab4162009-07-16 22:11:26 +0000167 void erase(MDString *M);
Owen Anderson4118dde2009-07-16 23:44:30 +0000168 void erase(MDNode *M);
Owen Anderson39ede7b2009-07-21 20:13:12 +0000169 void erase(ConstantAggregateZero *Z);
Owen Anderson3d344922009-07-21 20:55:28 +0000170 void erase(ConstantArray *C);
Owen Anderson909f6002009-07-23 23:25:33 +0000171 void erase(ConstantStruct *S);
Owen Anderson3d344922009-07-21 20:55:28 +0000172
173 // RAUW helpers
174
175 Constant *replaceUsesOfWithOnConstant(ConstantArray *CA, Value *From,
176 Value *To, Use *U);
Owen Anderson909f6002009-07-23 23:25:33 +0000177 Constant *replaceUsesOfWithOnConstant(ConstantStruct *CS, Value *From,
178 Value *To, Use *U);
Owen Anderson8e66e0b2009-06-30 00:48:55 +0000179};
180
181}
182
Owen Anderson36f62e52009-06-30 17:06:46 +0000183#endif