llvm.org GIT mirror llvm / 48b2f3e
Factor some of the constants+context related code out into a separate header, to make LLVMContextImpl.h not hideous. Also, fix some MSVC compile errors. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@78115 91177308-0d34-0410-b5e6-96231b3b80d8 Owen Anderson 11 years ago
38 changed file(s) with 56 addition(s) and 834 deletion(s). Raw diff Collapse all Expand all
2121 class TargetData;
2222 class Function;
2323 class Type;
24 class LLVMContext;
24 struct LLVMContext;
2525
2626 /// ConstantFoldInstruction - Attempt to constant fold the specified
2727 /// instruction. If successful, the constant result is returned, if not, null
3939 class DebugLoc;
4040 struct DebugLocTracker;
4141 class Instruction;
42 class LLVMContext;
42 struct LLVMContext;
4343
4444 class DIDescriptor {
4545 protected:
3939 class Type;
4040 class ScalarEvolution;
4141 class TargetData;
42 class LLVMContext;
42 struct LLVMContext;
4343 class Loop;
4444 class LoopInfo;
4545 class Operator;
3030 class BasicBlock;
3131 class Function;
3232 class SparseSolver;
33 class LLVMContext;
33 struct LLVMContext;
3434
3535 template class SmallVectorImpl;
3636
2222 class Instruction;
2323 class APInt;
2424 class TargetData;
25 class LLVMContext;
25 struct LLVMContext;
2626
2727 /// ComputeMaskedBits - Determine which of the bits specified in Mask are
2828 /// known to be either zero or one and return them in the KnownZero/KnownOne
2020 class Module;
2121 class SMDiagnostic;
2222 class raw_ostream;
23 class LLVMContext;
23 struct LLVMContext;
2424
2525 /// This function is the main interface to the LLVM Assembly Parser. It parses
2626 /// an ASCII file that (presumably) contains LLVM Assembly code. It returns a
2121 namespace llvm {
2222
2323 class TerminatorInst;
24 class LLVMContext;
24 struct LLVMContext;
2525
2626 template<> struct ilist_traits
2727 : public SymbolTableListTraits {
3131 class Module; // From VMCore
3232 class Archive; // Declared below
3333 class ArchiveMemberHeader; // Internal implementation class
34 class LLVMContext; // Global data
34 struct LLVMContext; // Global data
3535
3636 /// This class is the main class manipulated by users of the Archive class. It
3737 /// holds information about one member of the Archive. It is also the element
2222 class MemoryBuffer;
2323 class ModulePass;
2424 class BitstreamWriter;
25 class LLVMContext;
25 struct LLVMContext;
2626 class raw_ostream;
2727
2828 /// getBitcodeModuleProvider - Read the header of the specified bitcode buffer
2222
2323 namespace llvm {
2424 class Type;
25 class LLVMContext;
25 struct LLVMContext;
2626
2727 struct MVT { // MVT = Machine Value Type
2828 public:
1919 class APInt;
2020
2121 template class SmallVectorImpl;
22 class LLVMContext;
22 struct LLVMContext;
2323
2424 /// This is an important base class in LLVM. It provides the common facilities
2525 /// of all constant values in an LLVM program. A constant is a value that is
230230 APFloat Val;
231231 void *operator new(size_t, unsigned);// DO NOT IMPLEMENT
232232 ConstantFP(const ConstantFP &); // DO NOT IMPLEMENT
233 friend class LLVMContextImpl;
233 friend struct LLVMContextImpl;
234234 protected:
235235 ConstantFP(const Type *Ty, const APFloat& V);
236236 protected:
1919 namespace llvm {
2020 class Module;
2121 class InferiorProcess;
22 class LLVMContext;
22 struct LLVMContext;
2323
2424 /// Debugger class - This class implements the LLVM source-level debugger.
2525 /// This allows clients to handle the user IO processing without having to
2525 namespace llvm {
2626
2727 class FunctionType;
28 class LLVMContext;
28 struct LLVMContext;
2929
3030 // Traits for intrusive list of basic blocks...
3131 template<> struct ilist_traits
2727
2828 class Module;
2929 class Constant;
30 class LLVMContext;
30 struct LLVMContext;
3131 template
3232 class SymbolTableListTraits;
3333
2121
2222 namespace llvm {
2323
24 class LLVMContext;
24 struct LLVMContext;
2525
2626 //===----------------------------------------------------------------------===//
2727 // TerminatorInst Class
1919
2020 namespace llvm {
2121
22 class LLVMContext;
22 struct LLVMContext;
2323
2424 template
2525 class SymbolTableListTraits;
2828 class ConstantInt;
2929 class ConstantRange;
3030 class APInt;
31 class LLVMContext;
31 struct LLVMContext;
3232
3333 //===----------------------------------------------------------------------===//
3434 // AllocationInst Class
2222 class Type;
2323 class FunctionType;
2424 class Function;
25 class LLVMContext;
25 struct LLVMContext;
2626 class Module;
2727 class AttrListPtr;
2828
3333 class ConstantVector;
3434 class FunctionType;
3535 class IntegerType;
36 class LLVMContextImpl;
36 struct LLVMContextImpl;
3737 class MDNode;
3838 class MDString;
3939 class OpaqueType;
2020 namespace llvm {
2121
2222 class Module;
23 class LLVMContext;
23 struct LLVMContext;
2424
2525 /// This class provides the core functionality of linking in LLVM. It retains a
2626 /// Module object which is the composite of the modules and libraries linked
2626
2727 namespace llvm {
2828 class Constant;
29 class LLVMContext;
29 struct LLVMContext;
3030
3131 //===----------------------------------------------------------------------===//
3232 // MetadataBase - A base class for MDNode, MDString and NamedMDNode.
205205
206206 class NamedMDNode : public MetadataBase, public ilist_node {
207207 friend class SymbolTableListTraits;
208 friend class LLVMContextImpl;
208 friend struct LLVMContextImpl;
209209
210210 NamedMDNode(const NamedMDNode &); // DO NOT IMPLEMENT
211211 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
2525
2626 class GlobalValueRefMap; // Used by ConstantVals.cpp
2727 class FunctionType;
28 class LLVMContext;
28 struct LLVMContext;
2929
3030 template<> struct ilist_traits
3131 : public SymbolTableListTraits {
2424 namespace llvm {
2525
2626 class TargetData;
27 class LLVMContext;
27 struct LLVMContext;
2828
2929 /// TargetFolder - Create constants with target dependent folding.
3030 class TargetFolder {
3737 class TargetData;
3838 class Loop;
3939 class LoopInfo;
40 class LLVMContext;
40 struct LLVMContext;
4141
4242 /// CloneModule - Return an exact copy of the specified module
4343 ///
2626 class AllocaInst;
2727 class ConstantExpr;
2828 class TargetData;
29 class LLVMContext;
29 struct LLVMContext;
3030 struct DbgInfoIntrinsic;
3131
3232 template class SmallVectorImpl;
2222 class DominatorTree;
2323 class DominanceFrontier;
2424 class AliasSetTracker;
25 class LLVMContext;
25 struct LLVMContext;
2626
2727 /// isAllocaPromotable - Return true if this alloca is legal for promotion.
2828 /// This is true if there are only loads and stores to the alloca...
1919 namespace llvm {
2020 class Value;
2121 class Instruction;
22 class LLVMContext;
22 struct LLVMContext;
2323 typedef DenseMap ValueMapTy;
2424
2525 Value *MapValue(const Value *V, ValueMapTy &VM, LLVMContext &Context);
4141 class raw_ostream;
4242 class AssemblyAnnotationWriter;
4343 class ValueHandleBase;
44 class LLVMContext;
44 struct LLVMContext;
4545
4646 //===----------------------------------------------------------------------===//
4747 // Value Class
3030
3131 namespace llvm {
3232
33 class LLVMContext;
33 struct LLVMContext;
3434
3535 /// The ArchiveMemberHeader structure is used internally for bitcode
3636 /// archives.
2323 class MemoryBuffer;
2424 class Type;
2525 class SMDiagnostic;
26 class LLVMContext;
26 struct LLVMContext;
2727
2828 class LLLexer {
2929 const char *CurPtr;
2525
2626 namespace llvm {
2727 class MemoryBuffer;
28 class LLVMContext;
28 struct LLVMContext;
2929
3030 //===----------------------------------------------------------------------===//
3131 // BitcodeReaderValueList Class
2222 class Value;
2323 class Constant;
2424 class Type;
25 class LLVMContext;
25 struct LLVMContext;
2626
2727 // Constant fold various types of instruction...
2828 Constant *ConstantFoldCastInstruction(
77 //===----------------------------------------------------------------------===//
88 //
99 // This file implements LLVMContext, as a wrapper around the opaque
10 // class LLVMContextImpl.
10 // struct LLVMContextImpl.
1111 //
1212 //===----------------------------------------------------------------------===//
1313
2828 return *GlobalContext;
2929 }
3030
31 LLVMContext::LLVMContext() : pImpl(new LLVMContextImpl(*this)) { }
31 LLVMContext::LLVMContext() : pImpl(new LLVMContextImpl()) { }
3232 LLVMContext::~LLVMContext() { delete pImpl; }
33
34 GetElementPtrConstantExpr::GetElementPtrConstantExpr
35 (Constant *C,
36 const std::vector &IdxList,
37 const Type *DestTy)
38 : ConstantExpr(DestTy, Instruction::GetElementPtr,
39 OperandTraits::op_end(this)
40 - (IdxList.size()+1),
41 IdxList.size()+1) {
42 OperandList[0] = C;
43 for (unsigned i = 0, E = IdxList.size(); i != E; ++i)
44 OperandList[i+1] = IdxList[i];
45 }
+0
-36
lib/VMCore/LLVMContextImpl.cpp less more
None //===--------------- LLVMContextImpl.cpp - Implementation ------*- C++ -*--===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements LLVMContextImpl, the opaque implementation
10 // of LLVMContext.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "LLVMContextImpl.h"
15 #include "llvm/Constants.h"
16 #include "llvm/DerivedTypes.h"
17 #include "llvm/LLVMContext.h"
18 #include "llvm/Metadata.h"
19 using namespace llvm;
20
21 LLVMContextImpl::LLVMContextImpl(LLVMContext &C) :
22 Context(C), TheTrueVal(0), TheFalseVal(0) { }
23
24 GetElementPtrConstantExpr::GetElementPtrConstantExpr
25 (Constant *C,
26 const std::vector &IdxList,
27 const Type *DestTy)
28 : ConstantExpr(DestTy, Instruction::GetElementPtr,
29 OperandTraits::op_end(this)
30 - (IdxList.size()+1),
31 IdxList.size()+1) {
32 OperandList[0] = C;
33 for (unsigned i = 0, E = IdxList.size(); i != E; ++i)
34 OperandList[i+1] = IdxList[i];
35 }
1414 #ifndef LLVM_LLVMCONTEXT_IMPL_H
1515 #define LLVM_LLVMCONTEXT_IMPL_H
1616
17 #include "ConstantsContext.h"
1718 #include "llvm/LLVMContext.h"
1819 #include "llvm/Constants.h"
1920 #include "llvm/DerivedTypes.h"
20 #include "llvm/Instructions.h"
21 #include "llvm/Operator.h"
22 #include "llvm/Support/Debug.h"
23 #include "llvm/Support/ErrorHandling.h"
24 #include "llvm/System/Mutex.h"
2521 #include "llvm/System/RWMutex.h"
2622 #include "llvm/ADT/APFloat.h"
2723 #include "llvm/ADT/APInt.h"
2824 #include "llvm/ADT/DenseMap.h"
2925 #include "llvm/ADT/FoldingSet.h"
3026 #include "llvm/ADT/StringMap.h"
31 #include
3227 #include
3328
3429 namespace llvm {
35 template
36 struct ConstantTraits;
37
38
39 /// UnaryConstantExpr - This class is private to Constants.cpp, and is used
40 /// behind the scenes to implement unary constant exprs.
41 class UnaryConstantExpr : public ConstantExpr {
42 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
43 public:
44 // allocate space for exactly one operand
45 void *operator new(size_t s) {
46 return User::operator new(s, 1);
47 }
48 UnaryConstantExpr(unsigned Opcode, Constant *C, const Type *Ty)
49 : ConstantExpr(Ty, Opcode, &Op<0>(), 1) {
50 Op<0>() = C;
51 }
52 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
53 };
54
55 /// BinaryConstantExpr - This class is private to Constants.cpp, and is used
56 /// behind the scenes to implement binary constant exprs.
57 class BinaryConstantExpr : public ConstantExpr {
58 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
59 public:
60 // allocate space for exactly two operands
61 void *operator new(size_t s) {
62 return User::operator new(s, 2);
63 }
64 BinaryConstantExpr(unsigned Opcode, Constant *C1, Constant *C2)
65 : ConstantExpr(C1->getType(), Opcode, &Op<0>(), 2) {
66 Op<0>() = C1;
67 Op<1>() = C2;
68 }
69 /// Transparently provide more efficient getOperand methods.
70 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
71 };
72
73 /// SelectConstantExpr - This class is private to Constants.cpp, and is used
74 /// behind the scenes to implement select constant exprs.
75 class SelectConstantExpr : public ConstantExpr {
76 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
77 public:
78 // allocate space for exactly three operands
79 void *operator new(size_t s) {
80 return User::operator new(s, 3);
81 }
82 SelectConstantExpr(Constant *C1, Constant *C2, Constant *C3)
83 : ConstantExpr(C2->getType(), Instruction::Select, &Op<0>(), 3) {
84 Op<0>() = C1;
85 Op<1>() = C2;
86 Op<2>() = C3;
87 }
88 /// Transparently provide more efficient getOperand methods.
89 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
90 };
91
92 /// ExtractElementConstantExpr - This class is private to
93 /// Constants.cpp, and is used behind the scenes to implement
94 /// extractelement constant exprs.
95 class ExtractElementConstantExpr : public ConstantExpr {
96 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
97 public:
98 // allocate space for exactly two operands
99 void *operator new(size_t s) {
100 return User::operator new(s, 2);
101 }
102 ExtractElementConstantExpr(Constant *C1, Constant *C2)
103 : ConstantExpr(cast(C1->getType())->getElementType(),
104 Instruction::ExtractElement, &Op<0>(), 2) {
105 Op<0>() = C1;
106 Op<1>() = C2;
107 }
108 /// Transparently provide more efficient getOperand methods.
109 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
110 };
111
112 /// InsertElementConstantExpr - This class is private to
113 /// Constants.cpp, and is used behind the scenes to implement
114 /// insertelement constant exprs.
115 class InsertElementConstantExpr : public ConstantExpr {
116 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
117 public:
118 // allocate space for exactly three operands
119 void *operator new(size_t s) {
120 return User::operator new(s, 3);
121 }
122 InsertElementConstantExpr(Constant *C1, Constant *C2, Constant *C3)
123 : ConstantExpr(C1->getType(), Instruction::InsertElement,
124 &Op<0>(), 3) {
125 Op<0>() = C1;
126 Op<1>() = C2;
127 Op<2>() = C3;
128 }
129 /// Transparently provide more efficient getOperand methods.
130 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
131 };
132
133 /// ShuffleVectorConstantExpr - This class is private to
134 /// Constants.cpp, and is used behind the scenes to implement
135 /// shufflevector constant exprs.
136 class ShuffleVectorConstantExpr : public ConstantExpr {
137 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
138 public:
139 // allocate space for exactly three operands
140 void *operator new(size_t s) {
141 return User::operator new(s, 3);
142 }
143 ShuffleVectorConstantExpr(Constant *C1, Constant *C2, Constant *C3)
144 : ConstantExpr(VectorType::get(
145 cast(C1->getType())->getElementType(),
146 cast(C3->getType())->getNumElements()),
147 Instruction::ShuffleVector,
148 &Op<0>(), 3) {
149 Op<0>() = C1;
150 Op<1>() = C2;
151 Op<2>() = C3;
152 }
153 /// Transparently provide more efficient getOperand methods.
154 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
155 };
156
157 /// ExtractValueConstantExpr - This class is private to
158 /// Constants.cpp, and is used behind the scenes to implement
159 /// extractvalue constant exprs.
160 class ExtractValueConstantExpr : public ConstantExpr {
161 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
162 public:
163 // allocate space for exactly one operand
164 void *operator new(size_t s) {
165 return User::operator new(s, 1);
166 }
167 ExtractValueConstantExpr(Constant *Agg,
168 const SmallVector &IdxList,
169 const Type *DestTy)
170 : ConstantExpr(DestTy, Instruction::ExtractValue, &Op<0>(), 1),
171 Indices(IdxList) {
172 Op<0>() = Agg;
173 }
174
175 /// Indices - These identify which value to extract.
176 const SmallVector Indices;
177
178 /// Transparently provide more efficient getOperand methods.
179 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
180 };
181
182 /// InsertValueConstantExpr - This class is private to
183 /// Constants.cpp, and is used behind the scenes to implement
184 /// insertvalue constant exprs.
185 class InsertValueConstantExpr : public ConstantExpr {
186 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
187 public:
188 // allocate space for exactly one operand
189 void *operator new(size_t s) {
190 return User::operator new(s, 2);
191 }
192 InsertValueConstantExpr(Constant *Agg, Constant *Val,
193 const SmallVector &IdxList,
194 const Type *DestTy)
195 : ConstantExpr(DestTy, Instruction::InsertValue, &Op<0>(), 2),
196 Indices(IdxList) {
197 Op<0>() = Agg;
198 Op<1>() = Val;
199 }
200
201 /// Indices - These identify the position for the insertion.
202 const SmallVector Indices;
203
204 /// Transparently provide more efficient getOperand methods.
205 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
206 };
207
208
209 /// GetElementPtrConstantExpr - This class is private to Constants.cpp, and is
210 /// used behind the scenes to implement getelementpr constant exprs.
211 class GetElementPtrConstantExpr : public ConstantExpr {
212 GetElementPtrConstantExpr(Constant *C, const std::vector &IdxList,
213 const Type *DestTy);
214 public:
215 static GetElementPtrConstantExpr *Create(Constant *C,
216 const std::vector&IdxList,
217 const Type *DestTy) {
218 return
219 new(IdxList.size() + 1) GetElementPtrConstantExpr(C, IdxList, DestTy);
220 }
221 /// Transparently provide more efficient getOperand methods.
222 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
223 };
224
225 // CompareConstantExpr - This class is private to Constants.cpp, and is used
226 // behind the scenes to implement ICmp and FCmp constant expressions. This is
227 // needed in order to store the predicate value for these instructions.
228 struct CompareConstantExpr : public ConstantExpr {
229 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
230 // allocate space for exactly two operands
231 void *operator new(size_t s) {
232 return User::operator new(s, 2);
233 }
234 unsigned short predicate;
235 CompareConstantExpr(const Type *ty, Instruction::OtherOps opc,
236 unsigned short pred, Constant* LHS, Constant* RHS)
237 : ConstantExpr(ty, opc, &Op<0>(), 2), predicate(pred) {
238 Op<0>() = LHS;
239 Op<1>() = RHS;
240 }
241 /// Transparently provide more efficient getOperand methods.
242 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
243 };
244
245 template <>
246 struct OperandTraits : FixedNumOperandTraits<1> {
247 };
248 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(UnaryConstantExpr, Value)
249
250 template <>
251 struct OperandTraits : FixedNumOperandTraits<2> {
252 };
253 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BinaryConstantExpr, Value)
254
255 template <>
256 struct OperandTraits : FixedNumOperandTraits<3> {
257 };
258 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SelectConstantExpr, Value)
259
260 template <>
261 struct OperandTraits : FixedNumOperandTraits<2> {
262 };
263 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractElementConstantExpr, Value)
264
265 template <>
266 struct OperandTraits : FixedNumOperandTraits<3> {
267 };
268 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementConstantExpr, Value)
269
270 template <>
271 struct OperandTraits : FixedNumOperandTraits<3> {
272 };
273 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ShuffleVectorConstantExpr, Value)
274
275 template <>
276 struct OperandTraits : FixedNumOperandTraits<1> {
277 };
278 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractValueConstantExpr, Value)
279
280 template <>
281 struct OperandTraits : FixedNumOperandTraits<2> {
282 };
283 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertValueConstantExpr, Value)
284
285 template <>
286 struct OperandTraits : VariadicOperandTraits<1> {
287 };
288
289 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GetElementPtrConstantExpr, Value)
290
291
292 template <>
293 struct OperandTraits : FixedNumOperandTraits<2> {
294 };
295 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CompareConstantExpr, Value)
296
297 struct ExprMapKeyType {
298 typedef SmallVector IndexList;
299
300 ExprMapKeyType(unsigned opc,
301 const std::vector &ops,
302 unsigned short pred = 0,
303 const IndexList &inds = IndexList())
304 : opcode(opc), predicate(pred), operands(ops), indices(inds) {}
305 uint16_t opcode;
306 uint16_t predicate;
307 std::vector operands;
308 IndexList indices;
309 bool operator==(const ExprMapKeyType& that) const {
310 return this->opcode == that.opcode &&
311 this->predicate == that.predicate &&
312 this->operands == that.operands &&
313 this->indices == that.indices;
314 }
315 bool operator<(const ExprMapKeyType & that) const {
316 return this->opcode < that.opcode ||
317 (this->opcode == that.opcode && this->predicate < that.predicate) ||
318 (this->opcode == that.opcode && this->predicate == that.predicate &&
319 this->operands < that.operands) ||
320 (this->opcode == that.opcode && this->predicate == that.predicate &&
321 this->operands == that.operands && this->indices < that.indices);
322 }
323
324 bool operator!=(const ExprMapKeyType& that) const {
325 return !(*this == that);
326 }
327 };
328
329 // The number of operands for each ConstantCreator::create method is
330 // determined by the ConstantTraits template.
331 // ConstantCreator - A class that is used to create constants by
332 // ValueMap*. This class should be partially specialized if there is
333 // something strange that needs to be done to interface to the ctor for the
334 // constant.
335 //
336 template
337 struct ConstantTraits< std::vector > {
338 static unsigned uses(const std::vector& v) {
339 return v.size();
340 }
341 };
342
343 template
344 struct ConstantCreator {
345 static ConstantClass *create(const TypeClass *Ty, const ValType &V) {
346 return new(ConstantTraits::uses(V)) ConstantClass(Ty, V);
347 }
348 };
349
350 template
351 struct ConvertConstantType {
352 static void convert(ConstantClass *OldC, const TypeClass *NewTy) {
353 llvm_unreachable("This type cannot be converted!");
354 }
355 };
356
357 template<>
358 struct ConstantCreator {
359 static ConstantExpr *create(const Type *Ty, const ExprMapKeyType &V,
360 unsigned short pred = 0) {
361 if (Instruction::isCast(V.opcode))
362 return new UnaryConstantExpr(V.opcode, V.operands[0], Ty);
363 if ((V.opcode >= Instruction::BinaryOpsBegin &&
364 V.opcode < Instruction::BinaryOpsEnd))
365 return new BinaryConstantExpr(V.opcode, V.operands[0], V.operands[1]);
366 if (V.opcode == Instruction::Select)
367 return new SelectConstantExpr(V.operands[0], V.operands[1],
368 V.operands[2]);
369 if (V.opcode == Instruction::ExtractElement)
370 return new ExtractElementConstantExpr(V.operands[0], V.operands[1]);
371 if (V.opcode == Instruction::InsertElement)
372 return new InsertElementConstantExpr(V.operands[0], V.operands[1],
373 V.operands[2]);
374 if (V.opcode == Instruction::ShuffleVector)
375 return new ShuffleVectorConstantExpr(V.operands[0], V.operands[1],
376 V.operands[2]);
377 if (V.opcode == Instruction::InsertValue)
378 return new InsertValueConstantExpr(V.operands[0], V.operands[1],
379 V.indices, Ty);
380 if (V.opcode == Instruction::ExtractValue)
381 return new ExtractValueConstantExpr(V.operands[0], V.indices, Ty);
382 if (V.opcode == Instruction::GetElementPtr) {
383 std::vector IdxList(V.operands.begin()+1, V.operands.end());
384 return GetElementPtrConstantExpr::Create(V.operands[0], IdxList, Ty);
385 }
386
387 // The compare instructions are weird. We have to encode the predicate
388 // value and it is combined with the instruction opcode by multiplying
389 // the opcode by one hundred. We must decode this to get the predicate.
390 if (V.opcode == Instruction::ICmp)
391 return new CompareConstantExpr(Ty, Instruction::ICmp, V.predicate,
392 V.operands[0], V.operands[1]);
393 if (V.opcode == Instruction::FCmp)
394 return new CompareConstantExpr(Ty, Instruction::FCmp, V.predicate,
395 V.operands[0], V.operands[1]);
396 llvm_unreachable("Invalid ConstantExpr!");
397 return 0;
398 }
399 };
400
401 template<>
402 struct ConvertConstantType {
403 static void convert(ConstantExpr *OldC, const Type *NewTy) {
404 Constant *New;
405 switch (OldC->getOpcode()) {
406 case Instruction::Trunc:
407 case Instruction::ZExt:
408 case Instruction::SExt:
409 case Instruction::FPTrunc:
410 case Instruction::FPExt:
411 case Instruction::UIToFP:
412 case Instruction::SIToFP:
413 case Instruction::FPToUI:
414 case Instruction::FPToSI:
415 case Instruction::PtrToInt:
416 case Instruction::IntToPtr:
417 case Instruction::BitCast:
418 New = ConstantExpr::getCast(OldC->getOpcode(), OldC->getOperand(0),
419 NewTy);
420 break;
421 case Instruction::Select:
422 New = ConstantExpr::getSelectTy(NewTy, OldC->getOperand(0),
423 OldC->getOperand(1),
424 OldC->getOperand(2));
425 break;
426 default:
427 assert(OldC->getOpcode() >= Instruction::BinaryOpsBegin &&
428 OldC->getOpcode() < Instruction::BinaryOpsEnd);
429 New = ConstantExpr::getTy(NewTy, OldC->getOpcode(), OldC->getOperand(0),
430 OldC->getOperand(1));
431 break;
432 case Instruction::GetElementPtr:
433 // Make everyone now use a constant of the new type...
434 std::vector Idx(OldC->op_begin()+1, OldC->op_end());
435 New = ConstantExpr::getGetElementPtrTy(NewTy, OldC->getOperand(0),
436 &Idx[0], Idx.size());
437 break;
438 }
439
440 assert(New != OldC && "Didn't replace constant??");
441 OldC->uncheckedReplaceAllUsesWith(New);
442 OldC->destroyConstant(); // This constant is now dead, destroy it.
443 }
444 };
445
446 // ConstantAggregateZero does not take extra "value" argument...
447 template
448 struct ConstantCreator {
449 static ConstantAggregateZero *create(const Type *Ty, const ValType &V){
450 return new ConstantAggregateZero(Ty);
451 }
452 };
453
454 template<>
455 struct ConvertConstantType {
456 static void convert(ConstantVector *OldC, const VectorType *NewTy) {
457 // Make everyone now use a constant of the new type...
458 std::vector C;
459 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
460 C.push_back(cast(OldC->getOperand(i)));
461 Constant *New = ConstantVector::get(NewTy, C);
462 assert(New != OldC && "Didn't replace constant??");
463 OldC->uncheckedReplaceAllUsesWith(New);
464 OldC->destroyConstant(); // This constant is now dead, destroy it.
465 }
466 };
467
468 template<>
469 struct ConvertConstantType {
470 static void convert(ConstantAggregateZero *OldC, const Type *NewTy) {
471 // Make everyone now use a constant of the new type...
472 Constant *New = ConstantAggregateZero::get(NewTy);
473 assert(New != OldC && "Didn't replace constant??");
474 OldC->uncheckedReplaceAllUsesWith(New);
475 OldC->destroyConstant(); // This constant is now dead, destroy it.
476 }
477 };
478
479 template<>
480 struct ConvertConstantType {
481 static void convert(ConstantArray *OldC, const ArrayType *NewTy) {
482 // Make everyone now use a constant of the new type...
483 std::vector C;
484 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
485 C.push_back(cast(OldC->getOperand(i)));
486 Constant *New = ConstantArray::get(NewTy, C);
487 assert(New != OldC && "Didn't replace constant??");
488 OldC->uncheckedReplaceAllUsesWith(New);
489 OldC->destroyConstant(); // This constant is now dead, destroy it.
490 }
491 };
492
493 template<>
494 struct ConvertConstantType {
495 static void convert(ConstantStruct *OldC, const StructType *NewTy) {
496 // Make everyone now use a constant of the new type...
497 std::vector C;
498 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
499 C.push_back(cast(OldC->getOperand(i)));
500 Constant *New = ConstantStruct::get(NewTy, C);
501 assert(New != OldC && "Didn't replace constant??");
502
503 OldC->uncheckedReplaceAllUsesWith(New);
504 OldC->destroyConstant(); // This constant is now dead, destroy it.
505 }
506 };
507
508 // ConstantPointerNull does not take extra "value" argument...
509 template
510 struct ConstantCreator {
511 static ConstantPointerNull *create(const PointerType *Ty, const ValType &V){
512 return new ConstantPointerNull(Ty);
513 }
514 };
515
516 template<>
517 struct ConvertConstantType {
518 static void convert(ConstantPointerNull *OldC, const PointerType *NewTy) {
519 // Make everyone now use a constant of the new type...
520 Constant *New = ConstantPointerNull::get(NewTy);
521 assert(New != OldC && "Didn't replace constant??");
522 OldC->uncheckedReplaceAllUsesWith(New);
523 OldC->destroyConstant(); // This constant is now dead, destroy it.
524 }
525 };
526
527 // UndefValue does not take extra "value" argument...
528 template
529 struct ConstantCreator {
530 static UndefValue *create(const Type *Ty, const ValType &V) {
531 return new UndefValue(Ty);
532 }
533 };
534
535 template<>
536 struct ConvertConstantType {
537 static void convert(UndefValue *OldC, const Type *NewTy) {
538 // Make everyone now use a constant of the new type.
539 Constant *New = UndefValue::get(NewTy);
540 assert(New != OldC && "Didn't replace constant??");
541 OldC->uncheckedReplaceAllUsesWith(New);
542 OldC->destroyConstant(); // This constant is now dead, destroy it.
543 }
544 };
545
546 template
547 bool HasLargeKey = false /*true for arrays and structs*/ >
548 class ValueMap : public AbstractTypeUser {
549 public:
550 typedef std::pair MapKey;
551 typedef std::map MapTy;
552 typedef std::map InverseMapTy;
553 typedef std::map AbstractTypeMapTy;
554 private:
555 /// Map - This is the main map from the element descriptor to the Constants.
556 /// This is the primary way we avoid creating two of the same shape
557 /// constant.
558 MapTy Map;
559
560 /// InverseMap - If "HasLargeKey" is true, this contains an inverse mapping
561 /// from the constants to their element in Map. This is important for
562 /// removal of constants from the array, which would otherwise have to scan
563 /// through the map with very large keys.
564 InverseMapTy InverseMap;
565
566 /// AbstractTypeMap - Map for abstract type constants.
567 ///
568 AbstractTypeMapTy AbstractTypeMap;
569
570 /// ValueMapLock - Mutex for this map.
571 sys::SmartMutex ValueMapLock;
572
573 public:
574 // NOTE: This function is not locked. It is the caller's responsibility
575 // to enforce proper synchronization.
576 typename MapTy::iterator map_end() { return Map.end(); }
577
578 /// InsertOrGetItem - Return an iterator for the specified element.
579 /// If the element exists in the map, the returned iterator points to the
580 /// entry and Exists=true. If not, the iterator points to the newly
581 /// inserted entry and returns Exists=false. Newly inserted entries have
582 /// I->second == 0, and should be filled in.
583 /// NOTE: This function is not locked. It is the caller's responsibility
584 // to enforce proper synchronization.
585 typename MapTy::iterator InsertOrGetItem(std::pair
586 &InsertVal,
587 bool &Exists) {
588 std::pair IP = Map.insert(InsertVal);
589 Exists = !IP.second;
590 return IP.first;
591 }
592
593 private:
594 typename MapTy::iterator FindExistingElement(ConstantClass *CP) {
595 if (HasLargeKey) {
596 typename InverseMapTy::iterator IMI = InverseMap.find(CP);
597 assert(IMI != InverseMap.end() && IMI->second != Map.end() &&
598 IMI->second->second == CP &&
599 "InverseMap corrupt!");
600 return IMI->second;
601 }
602
603 typename MapTy::iterator I =
604 Map.find(MapKey(static_cast(CP->getRawType()),
605 getValType(CP)));
606 if (I == Map.end() || I->second != CP) {
607 // FIXME: This should not use a linear scan. If this gets to be a
608 // performance problem, someone should look at this.
609 for (I = Map.begin(); I != Map.end() && I->second != CP; ++I)
610 /* empty */;
611 }
612 return I;
613 }
614
615 ConstantClass* Create(const TypeClass *Ty, const ValType &V,
616 typename MapTy::iterator I) {
617 ConstantClass* Result =
618 ConstantCreator::create(Ty, V);
619
620 assert(Result->getType() == Ty && "Type specified is not correct!");
621 I = Map.insert(I, std::make_pair(MapKey(Ty, V), Result));
622
623 if (HasLargeKey) // Remember the reverse mapping if needed.
624 InverseMap.insert(std::make_pair(Result, I));
625
626 // If the type of the constant is abstract, make sure that an entry
627 // exists for it in the AbstractTypeMap.
628 if (Ty->isAbstract()) {
629 typename AbstractTypeMapTy::iterator TI =
630 AbstractTypeMap.find(Ty);
631
632 if (TI == AbstractTypeMap.end()) {
633 // Add ourselves to the ATU list of the type.
634 cast(Ty)->addAbstractTypeUser(this);
635
636 AbstractTypeMap.insert(TI, std::make_pair(Ty, I));
637 }
638 }
639
640 return Result;
641 }
642 public:
643
644 /// getOrCreate - Return the specified constant from the map, creating it if
645 /// necessary.
646 ConstantClass *getOrCreate(const TypeClass *Ty, const ValType &V) {
647 sys::SmartScopedLock Lock(ValueMapLock);
648 MapKey Lookup(Ty, V);
649 ConstantClass* Result = 0;
650
651 typename MapTy::iterator I = Map.find(Lookup);
652 // Is it in the map?
653 if (I != Map.end())
654 Result = static_cast(I->second);
655
656 if (!Result) {
657 // If no preexisting value, create one now...
658 Result = Create(Ty, V, I);
659 }
660
661 return Result;
662 }
663
664 void remove(ConstantClass *CP) {
665 sys::SmartScopedLock Lock(ValueMapLock);
666 typename MapTy::iterator I = FindExistingElement(CP);
667 assert(I != Map.end() && "Constant not found in constant table!");
668 assert(I->second == CP && "Didn't find correct element?");
669
670 if (HasLargeKey) // Remember the reverse mapping if needed.
671 InverseMap.erase(CP);
672
673 // Now that we found the entry, make sure this isn't the entry that
674 // the AbstractTypeMap points to.
675 const TypeClass *Ty = static_cast(I->first.first);
676 if (Ty->isAbstract()) {
677 assert(AbstractTypeMap.count(Ty) &&
678 "Abstract type not in AbstractTypeMap?");
679 typename MapTy::iterator &ATMEntryIt = AbstractTypeMap[Ty];
680 if (ATMEntryIt == I) {
681 // Yes, we are removing the representative entry for this type.
682 // See if there are any other entries of the same type.
683 typename MapTy::iterator TmpIt = ATMEntryIt;
684
685 // First check the entry before this one...
686 if (TmpIt != Map.begin()) {
687 --TmpIt;
688 if (TmpIt->first.first != Ty) // Not the same type, move back...
689 ++TmpIt;
690 }
691
692 // If we didn't find the same type, try to move forward...
693 if (TmpIt == ATMEntryIt) {
694 ++TmpIt;
695 if (TmpIt == Map.end() || TmpIt->first.first != Ty)
696 --TmpIt; // No entry afterwards with the same type
697 }
698
699 // If there is another entry in the map of the same abstract type,
700 // update the AbstractTypeMap entry now.
701 if (TmpIt != ATMEntryIt) {
702 ATMEntryIt = TmpIt;
703 } else {
704 // Otherwise, we are removing the last instance of this type
705 // from the table. Remove from the ATM, and from user list.
706 cast(Ty)->removeAbstractTypeUser(this);
707 AbstractTypeMap.erase(Ty);
708 }
709 }
710 }
711
712 Map.erase(I);
713 }
714
715
716 /// MoveConstantToNewSlot - If we are about to change C to be the element
717 /// specified by I, update our internal data structures to reflect this
718 /// fact.
719 /// NOTE: This function is not locked. It is the responsibility of the
720 /// caller to enforce proper synchronization if using this method.
721 void MoveConstantToNewSlot(ConstantClass *C, typename MapTy::iterator I) {
722 // First, remove the old location of the specified constant in the map.
723 typename MapTy::iterator OldI = FindExistingElement(C);
724 assert(OldI != Map.end() && "Constant not found in constant table!");
725 assert(OldI->second == C && "Didn't find correct element?");
726
727 // If this constant is the representative element for its abstract type,
728 // update the AbstractTypeMap so that the representative element is I.
729 if (C->getType()->isAbstract()) {
730 typename AbstractTypeMapTy::iterator ATI =
731 AbstractTypeMap.find(C->getType());
732 assert(ATI != AbstractTypeMap.end() &&
733 "Abstract type not in AbstractTypeMap?");
734 if (ATI->second == OldI)
735 ATI->second = I;
736 }
737
738 // Remove the old entry from the map.
739 Map.erase(OldI);
740
741 // Update the inverse map so that we know that this constant is now
742 // located at descriptor I.
743 if (HasLargeKey) {
744 assert(I->second == C && "Bad inversemap entry!");
745 InverseMap[C] = I;
746 }
747 }
748
749 void refineAbstractType(const DerivedType *OldTy, const Type *NewTy) {
750 sys::SmartScopedLock Lock(ValueMapLock);
751 typename AbstractTypeMapTy::iterator I =
752 AbstractTypeMap.find(cast(OldTy));
753
754 assert(I != AbstractTypeMap.end() &&
755 "Abstract type not in AbstractTypeMap?");
756
757 // Convert a constant at a time until the last one is gone. The last one
758 // leaving will remove() itself, causing the AbstractTypeMapEntry to be
759 // eliminated eventually.
760 do {
761 ConvertConstantType
762 TypeClass>::convert(
763 static_cast(I->second->second),
764 cast(NewTy));
765
766 I = AbstractTypeMap.find(cast(OldTy));
767 } while (I != AbstractTypeMap.end());
768 }
769
770 // If the type became concrete without being refined to any other existing
771 // type, we just remove ourselves from the ATU list.
772 void typeBecameConcrete(const DerivedType *AbsTy) {
773 AbsTy->removeAbstractTypeUser(this);
774 }
775
776 void dump() const {
777 DOUT << "Constant.cpp: ValueMap\n";
778 }
779 };
780
78130
78231 class ConstantInt;
78332 class ConstantFP;
78433 class MDString;
78534 class MDNode;
786 class LLVMContext;
35 struct LLVMContext;
78736 class Type;
78837 class Value;
78938
84392 sys::SmartRWMutex ConstantsLock;
84493
84594 typedef DenseMap
846 DenseMapAPIntKeyInfo> IntMapTy;
95 DenseMapAPIntKeyInfo> IntMapTy;
84796 IntMapTy IntConstants;
84897
84998 typedef DenseMap
850 DenseMapAPFloatKeyInfo> FPMapTy;
99 DenseMapAPFloatKeyInfo> FPMapTy;
851100 FPMapTy FPConstants;
852101
853102 StringMap MDStringCache;
874123
875124 ValueMap ExprConstants;
876125
877 LLVMContext &Context;
878126 ConstantInt *TheTrueVal;
879127 ConstantInt *TheFalseVal;
880128
881 LLVMContextImpl(LLVMContext &C);
882 private:
883 LLVMContextImpl();
884 LLVMContextImpl(const LLVMContextImpl&);
129 LLVMContextImpl() : TheTrueVal(0), TheFalseVal(0) { }
885130 };
886131
887132 }
2929 class BasicBlock;
3030 class AbstractInterpreter;
3131 class Instruction;
32 class LLVMContext;
32 struct LLVMContext;
3333
3434 class DebugCrashes;
3535
2323 struct SourceLanguage;
2424 class ProgramInfo;
2525 class RuntimeInfo;
26 class LLVMContext;
26 struct LLVMContext;
2727
2828 /// CLIDebugger - This class implements the command line interface for the
2929 /// LLVM debugger.