blob: 392e500f128be73ba8e349179c5f82949028faf0 [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>
30
31template<class ValType, class TypeClass, class ConstantClass,
32 bool HasLargeKey = false /*true for arrays and structs*/ >
Owen Anderson3d344922009-07-21 20:55:28 +000033class ValueMap;
Owen Anderson8e66e0b2009-06-30 00:48:55 +000034
Owen Anderson20b34ac2009-07-16 18:04:31 +000035namespace llvm {
Owen Anderson39ede7b2009-07-21 20:13:12 +000036template<class ValType>
37struct ConstantTraits;
Owen Anderson20b34ac2009-07-16 18:04:31 +000038
39class ConstantInt;
Owen Andersonc277dc42009-07-16 19:05:41 +000040class ConstantFP;
Owen Anderson69ab4162009-07-16 22:11:26 +000041class MDString;
Owen Anderson4118dde2009-07-16 23:44:30 +000042class MDNode;
Owen Anderson20b34ac2009-07-16 18:04:31 +000043class LLVMContext;
44class Type;
Owen Anderson4118dde2009-07-16 23:44:30 +000045class Value;
Owen Anderson20b34ac2009-07-16 18:04:31 +000046
47struct DenseMapAPIntKeyInfo {
48 struct KeyTy {
49 APInt val;
50 const Type* type;
51 KeyTy(const APInt& V, const Type* Ty) : val(V), type(Ty) {}
52 KeyTy(const KeyTy& that) : val(that.val), type(that.type) {}
53 bool operator==(const KeyTy& that) const {
54 return type == that.type && this->val == that.val;
55 }
56 bool operator!=(const KeyTy& that) const {
57 return !this->operator==(that);
58 }
59 };
60 static inline KeyTy getEmptyKey() { return KeyTy(APInt(1,0), 0); }
61 static inline KeyTy getTombstoneKey() { return KeyTy(APInt(1,1), 0); }
62 static unsigned getHashValue(const KeyTy &Key) {
63 return DenseMapInfo<void*>::getHashValue(Key.type) ^
64 Key.val.getHashValue();
65 }
66 static bool isEqual(const KeyTy &LHS, const KeyTy &RHS) {
67 return LHS == RHS;
68 }
69 static bool isPod() { return false; }
70};
71
Owen Andersonc277dc42009-07-16 19:05:41 +000072struct DenseMapAPFloatKeyInfo {
73 struct KeyTy {
74 APFloat val;
75 KeyTy(const APFloat& V) : val(V){}
76 KeyTy(const KeyTy& that) : val(that.val) {}
77 bool operator==(const KeyTy& that) const {
78 return this->val.bitwiseIsEqual(that.val);
79 }
80 bool operator!=(const KeyTy& that) const {
81 return !this->operator==(that);
82 }
83 };
84 static inline KeyTy getEmptyKey() {
85 return KeyTy(APFloat(APFloat::Bogus,1));
86 }
87 static inline KeyTy getTombstoneKey() {
88 return KeyTy(APFloat(APFloat::Bogus,2));
89 }
90 static unsigned getHashValue(const KeyTy &Key) {
91 return Key.val.getHashValue();
92 }
93 static bool isEqual(const KeyTy &LHS, const KeyTy &RHS) {
94 return LHS == RHS;
95 }
96 static bool isPod() { return false; }
97};
98
Owen Anderson20b34ac2009-07-16 18:04:31 +000099class LLVMContextImpl {
100 sys::SmartRWMutex<true> ConstantsLock;
101
102 typedef DenseMap<DenseMapAPIntKeyInfo::KeyTy, ConstantInt*,
103 DenseMapAPIntKeyInfo> IntMapTy;
104 IntMapTy IntConstants;
105
Owen Andersonc277dc42009-07-16 19:05:41 +0000106 typedef DenseMap<DenseMapAPFloatKeyInfo::KeyTy, ConstantFP*,
107 DenseMapAPFloatKeyInfo> FPMapTy;
108 FPMapTy FPConstants;
109
Owen Anderson69ab4162009-07-16 22:11:26 +0000110 StringMap<MDString*> MDStringCache;
111
Owen Anderson4118dde2009-07-16 23:44:30 +0000112 FoldingSet<MDNode> MDNodeSet;
113
Owen Anderson3d344922009-07-21 20:55:28 +0000114 ValueMap<char, Type, ConstantAggregateZero> *AggZeroConstants;
115
116 typedef ValueMap<std::vector<Constant*>, ArrayType,
117 ConstantArray, true /*largekey*/> ArrayConstantsTy;
118 ArrayConstantsTy *ArrayConstants;
Owen Anderson39ede7b2009-07-21 20:13:12 +0000119
Owen Anderson20b34ac2009-07-16 18:04:31 +0000120 LLVMContext &Context;
Owen Anderson2ad52172009-07-21 02:47:59 +0000121 ConstantInt *TheTrueVal;
122 ConstantInt *TheFalseVal;
123
Owen Anderson20b34ac2009-07-16 18:04:31 +0000124 LLVMContextImpl();
125 LLVMContextImpl(const LLVMContextImpl&);
126public:
Owen Anderson39ede7b2009-07-21 20:13:12 +0000127 LLVMContextImpl(LLVMContext &C);
128 ~LLVMContextImpl();
Owen Anderson20b34ac2009-07-16 18:04:31 +0000129
130 /// Return a ConstantInt with the specified value and an implied Type. The
131 /// type is the integer type that corresponds to the bit width of the value.
Owen Andersonc277dc42009-07-16 19:05:41 +0000132 ConstantInt *getConstantInt(const APInt &V);
133
134 ConstantFP *getConstantFP(const APFloat &V);
Owen Anderson69ab4162009-07-16 22:11:26 +0000135
Devang Patel62920032009-07-23 02:00:51 +0000136 MDString *getMDString(const char *StrBegin, unsigned StrLength);
Owen Anderson69ab4162009-07-16 22:11:26 +0000137
Owen Anderson4118dde2009-07-16 23:44:30 +0000138 MDNode *getMDNode(Value*const* Vals, unsigned NumVals);
Owen Anderson69ab4162009-07-16 22:11:26 +0000139
Owen Anderson39ede7b2009-07-21 20:13:12 +0000140 ConstantAggregateZero *getConstantAggregateZero(const Type *Ty);
141
Owen Anderson3d344922009-07-21 20:55:28 +0000142 Constant *getConstantArray(const ArrayType *Ty,
143 const std::vector<Constant*> &V);
144
Owen Andersonc37bc692009-07-21 18:03:38 +0000145 ConstantInt *getTrue() {
Owen Anderson2ad52172009-07-21 02:47:59 +0000146 if (TheTrueVal)
147 return TheTrueVal;
148 else
149 return (TheTrueVal = Context.getConstantInt(IntegerType::get(1), 1));
150 }
151
Owen Andersonc37bc692009-07-21 18:03:38 +0000152 ConstantInt *getFalse() {
Owen Anderson2ad52172009-07-21 02:47:59 +0000153 if (TheFalseVal)
154 return TheFalseVal;
155 else
156 return (TheFalseVal = Context.getConstantInt(IntegerType::get(1), 0));
157 }
158
Owen Anderson69ab4162009-07-16 22:11:26 +0000159 void erase(MDString *M);
Owen Anderson4118dde2009-07-16 23:44:30 +0000160 void erase(MDNode *M);
Owen Anderson39ede7b2009-07-21 20:13:12 +0000161 void erase(ConstantAggregateZero *Z);
Owen Anderson3d344922009-07-21 20:55:28 +0000162 void erase(ConstantArray *C);
163
164 // RAUW helpers
165
166 Constant *replaceUsesOfWithOnConstant(ConstantArray *CA, Value *From,
167 Value *To, Use *U);
Owen Anderson8e66e0b2009-06-30 00:48:55 +0000168};
169
170}
171
Owen Anderson36f62e52009-06-30 17:06:46 +0000172#endif