blob: 4f55502ff94825c029bbcc27c7f16624e7340eb2 [file] [log] [blame]
Owen Anderson3fb4aab2009-08-23 04:24:24 +00001//===-- ConstantsContext.h - Constants-related Context Interals -----------===//
Owen Anderson9b676982009-08-04 22:55:26 +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//===----------------------------------------------------------------------===//
9//
10// This file defines various helper methods and classes used by
11// LLVMContextImpl for creating and managing constants.
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_CONSTANTSCONTEXT_H
16#define LLVM_CONSTANTSCONTEXT_H
17
18#include "llvm/Instructions.h"
19#include "llvm/Operator.h"
20#include "llvm/Support/Debug.h"
21#include "llvm/Support/ErrorHandling.h"
Chris Lattner34822f62009-08-23 04:44:11 +000022#include "llvm/Support/raw_ostream.h"
Owen Anderson9b676982009-08-04 22:55:26 +000023#include "llvm/System/Mutex.h"
24#include "llvm/System/RWMutex.h"
25#include <map>
26
27namespace llvm {
28template<class ValType>
29struct ConstantTraits;
30
31/// UnaryConstantExpr - This class is private to Constants.cpp, and is used
32/// behind the scenes to implement unary constant exprs.
33class UnaryConstantExpr : public ConstantExpr {
34 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
35public:
36 // allocate space for exactly one operand
37 void *operator new(size_t s) {
38 return User::operator new(s, 1);
39 }
40 UnaryConstantExpr(unsigned Opcode, Constant *C, const Type *Ty)
41 : ConstantExpr(Ty, Opcode, &Op<0>(), 1) {
42 Op<0>() = C;
43 }
44 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
45};
46
47/// BinaryConstantExpr - This class is private to Constants.cpp, and is used
48/// behind the scenes to implement binary constant exprs.
49class BinaryConstantExpr : public ConstantExpr {
50 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
51public:
52 // allocate space for exactly two operands
53 void *operator new(size_t s) {
54 return User::operator new(s, 2);
55 }
Dan Gohman1b849082009-09-07 23:54:19 +000056 BinaryConstantExpr(unsigned Opcode, Constant *C1, Constant *C2,
57 unsigned Flags)
Owen Anderson9b676982009-08-04 22:55:26 +000058 : ConstantExpr(C1->getType(), Opcode, &Op<0>(), 2) {
59 Op<0>() = C1;
60 Op<1>() = C2;
Dan Gohman1b849082009-09-07 23:54:19 +000061 SubclassOptionalData = Flags;
Owen Anderson9b676982009-08-04 22:55:26 +000062 }
63 /// Transparently provide more efficient getOperand methods.
64 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
65};
66
67/// SelectConstantExpr - This class is private to Constants.cpp, and is used
68/// behind the scenes to implement select constant exprs.
69class SelectConstantExpr : public ConstantExpr {
70 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
71public:
72 // allocate space for exactly three operands
73 void *operator new(size_t s) {
74 return User::operator new(s, 3);
75 }
76 SelectConstantExpr(Constant *C1, Constant *C2, Constant *C3)
77 : ConstantExpr(C2->getType(), Instruction::Select, &Op<0>(), 3) {
78 Op<0>() = C1;
79 Op<1>() = C2;
80 Op<2>() = C3;
81 }
82 /// Transparently provide more efficient getOperand methods.
83 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
84};
85
86/// ExtractElementConstantExpr - This class is private to
87/// Constants.cpp, and is used behind the scenes to implement
88/// extractelement constant exprs.
89class ExtractElementConstantExpr : public ConstantExpr {
90 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
91public:
92 // allocate space for exactly two operands
93 void *operator new(size_t s) {
94 return User::operator new(s, 2);
95 }
96 ExtractElementConstantExpr(Constant *C1, Constant *C2)
97 : ConstantExpr(cast<VectorType>(C1->getType())->getElementType(),
98 Instruction::ExtractElement, &Op<0>(), 2) {
99 Op<0>() = C1;
100 Op<1>() = C2;
101 }
102 /// Transparently provide more efficient getOperand methods.
103 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
104};
105
106/// InsertElementConstantExpr - This class is private to
107/// Constants.cpp, and is used behind the scenes to implement
108/// insertelement constant exprs.
109class InsertElementConstantExpr : public ConstantExpr {
110 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
111public:
112 // allocate space for exactly three operands
113 void *operator new(size_t s) {
114 return User::operator new(s, 3);
115 }
116 InsertElementConstantExpr(Constant *C1, Constant *C2, Constant *C3)
117 : ConstantExpr(C1->getType(), Instruction::InsertElement,
118 &Op<0>(), 3) {
119 Op<0>() = C1;
120 Op<1>() = C2;
121 Op<2>() = C3;
122 }
123 /// Transparently provide more efficient getOperand methods.
124 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
125};
126
127/// ShuffleVectorConstantExpr - This class is private to
128/// Constants.cpp, and is used behind the scenes to implement
129/// shufflevector constant exprs.
130class ShuffleVectorConstantExpr : public ConstantExpr {
131 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
132public:
133 // allocate space for exactly three operands
134 void *operator new(size_t s) {
135 return User::operator new(s, 3);
136 }
137 ShuffleVectorConstantExpr(Constant *C1, Constant *C2, Constant *C3)
138 : ConstantExpr(VectorType::get(
139 cast<VectorType>(C1->getType())->getElementType(),
140 cast<VectorType>(C3->getType())->getNumElements()),
141 Instruction::ShuffleVector,
142 &Op<0>(), 3) {
143 Op<0>() = C1;
144 Op<1>() = C2;
145 Op<2>() = C3;
146 }
147 /// Transparently provide more efficient getOperand methods.
148 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
149};
150
151/// ExtractValueConstantExpr - This class is private to
152/// Constants.cpp, and is used behind the scenes to implement
153/// extractvalue constant exprs.
154class ExtractValueConstantExpr : public ConstantExpr {
155 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
156public:
157 // allocate space for exactly one operand
158 void *operator new(size_t s) {
159 return User::operator new(s, 1);
160 }
161 ExtractValueConstantExpr(Constant *Agg,
162 const SmallVector<unsigned, 4> &IdxList,
163 const Type *DestTy)
164 : ConstantExpr(DestTy, Instruction::ExtractValue, &Op<0>(), 1),
165 Indices(IdxList) {
166 Op<0>() = Agg;
167 }
168
169 /// Indices - These identify which value to extract.
170 const SmallVector<unsigned, 4> Indices;
171
172 /// Transparently provide more efficient getOperand methods.
173 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
174};
175
176/// InsertValueConstantExpr - This class is private to
177/// Constants.cpp, and is used behind the scenes to implement
178/// insertvalue constant exprs.
179class InsertValueConstantExpr : public ConstantExpr {
180 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
181public:
182 // allocate space for exactly one operand
183 void *operator new(size_t s) {
184 return User::operator new(s, 2);
185 }
186 InsertValueConstantExpr(Constant *Agg, Constant *Val,
187 const SmallVector<unsigned, 4> &IdxList,
188 const Type *DestTy)
189 : ConstantExpr(DestTy, Instruction::InsertValue, &Op<0>(), 2),
190 Indices(IdxList) {
191 Op<0>() = Agg;
192 Op<1>() = Val;
193 }
194
195 /// Indices - These identify the position for the insertion.
196 const SmallVector<unsigned, 4> Indices;
197
198 /// Transparently provide more efficient getOperand methods.
199 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
200};
201
202
203/// GetElementPtrConstantExpr - This class is private to Constants.cpp, and is
204/// used behind the scenes to implement getelementpr constant exprs.
205class GetElementPtrConstantExpr : public ConstantExpr {
206 GetElementPtrConstantExpr(Constant *C, const std::vector<Constant*> &IdxList,
207 const Type *DestTy);
208public:
209 static GetElementPtrConstantExpr *Create(Constant *C,
210 const std::vector<Constant*>&IdxList,
Dan Gohman1b849082009-09-07 23:54:19 +0000211 const Type *DestTy,
212 unsigned Flags) {
213 GetElementPtrConstantExpr *Result =
Owen Anderson9b676982009-08-04 22:55:26 +0000214 new(IdxList.size() + 1) GetElementPtrConstantExpr(C, IdxList, DestTy);
Dan Gohman1b849082009-09-07 23:54:19 +0000215 Result->SubclassOptionalData = Flags;
216 return Result;
Owen Anderson9b676982009-08-04 22:55:26 +0000217 }
218 /// Transparently provide more efficient getOperand methods.
219 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
220};
221
222// CompareConstantExpr - This class is private to Constants.cpp, and is used
223// behind the scenes to implement ICmp and FCmp constant expressions. This is
224// needed in order to store the predicate value for these instructions.
225struct CompareConstantExpr : public ConstantExpr {
226 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
227 // allocate space for exactly two operands
228 void *operator new(size_t s) {
229 return User::operator new(s, 2);
230 }
231 unsigned short predicate;
232 CompareConstantExpr(const Type *ty, Instruction::OtherOps opc,
233 unsigned short pred, Constant* LHS, Constant* RHS)
234 : ConstantExpr(ty, opc, &Op<0>(), 2), predicate(pred) {
235 Op<0>() = LHS;
236 Op<1>() = RHS;
237 }
238 /// Transparently provide more efficient getOperand methods.
239 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
240};
241
242template <>
Duncan Sands0f5bbb52009-09-06 08:55:57 +0000243struct OperandTraits<UnaryConstantExpr> : public FixedNumOperandTraits<1> {
Owen Anderson9b676982009-08-04 22:55:26 +0000244};
245DEFINE_TRANSPARENT_OPERAND_ACCESSORS(UnaryConstantExpr, Value)
246
247template <>
Duncan Sands0f5bbb52009-09-06 08:55:57 +0000248struct OperandTraits<BinaryConstantExpr> : public FixedNumOperandTraits<2> {
Owen Anderson9b676982009-08-04 22:55:26 +0000249};
250DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BinaryConstantExpr, Value)
251
252template <>
Duncan Sands0f5bbb52009-09-06 08:55:57 +0000253struct OperandTraits<SelectConstantExpr> : public FixedNumOperandTraits<3> {
Owen Anderson9b676982009-08-04 22:55:26 +0000254};
255DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SelectConstantExpr, Value)
256
257template <>
Duncan Sands0f5bbb52009-09-06 08:55:57 +0000258struct OperandTraits<ExtractElementConstantExpr> : public FixedNumOperandTraits<2> {
Owen Anderson9b676982009-08-04 22:55:26 +0000259};
260DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractElementConstantExpr, Value)
261
262template <>
Duncan Sands0f5bbb52009-09-06 08:55:57 +0000263struct OperandTraits<InsertElementConstantExpr> : public FixedNumOperandTraits<3> {
Owen Anderson9b676982009-08-04 22:55:26 +0000264};
265DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementConstantExpr, Value)
266
267template <>
Duncan Sands0f5bbb52009-09-06 08:55:57 +0000268struct OperandTraits<ShuffleVectorConstantExpr> : public FixedNumOperandTraits<3> {
Owen Anderson9b676982009-08-04 22:55:26 +0000269};
270DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ShuffleVectorConstantExpr, Value)
271
272template <>
Duncan Sands0f5bbb52009-09-06 08:55:57 +0000273struct OperandTraits<ExtractValueConstantExpr> : public FixedNumOperandTraits<1> {
Owen Anderson9b676982009-08-04 22:55:26 +0000274};
275DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractValueConstantExpr, Value)
276
277template <>
Duncan Sands0f5bbb52009-09-06 08:55:57 +0000278struct OperandTraits<InsertValueConstantExpr> : public FixedNumOperandTraits<2> {
Owen Anderson9b676982009-08-04 22:55:26 +0000279};
280DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertValueConstantExpr, Value)
281
282template <>
Duncan Sands0f5bbb52009-09-06 08:55:57 +0000283struct OperandTraits<GetElementPtrConstantExpr> : public VariadicOperandTraits<1> {
Owen Anderson9b676982009-08-04 22:55:26 +0000284};
285
286DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GetElementPtrConstantExpr, Value)
287
288
289template <>
Duncan Sands0f5bbb52009-09-06 08:55:57 +0000290struct OperandTraits<CompareConstantExpr> : public FixedNumOperandTraits<2> {
Owen Anderson9b676982009-08-04 22:55:26 +0000291};
292DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CompareConstantExpr, Value)
293
294struct ExprMapKeyType {
295 typedef SmallVector<unsigned, 4> IndexList;
296
297 ExprMapKeyType(unsigned opc,
298 const std::vector<Constant*> &ops,
Dan Gohman1b849082009-09-07 23:54:19 +0000299 unsigned short flags = 0,
300 unsigned short optionalflags = 0,
Owen Anderson9b676982009-08-04 22:55:26 +0000301 const IndexList &inds = IndexList())
Dan Gohman1b849082009-09-07 23:54:19 +0000302 : opcode(opc), subclassoptionaldata(optionalflags), subclassdata(flags),
303 operands(ops), indices(inds) {}
304 uint8_t opcode;
305 uint8_t subclassoptionaldata;
306 uint16_t subclassdata;
Owen Anderson9b676982009-08-04 22:55:26 +0000307 std::vector<Constant*> operands;
308 IndexList indices;
309 bool operator==(const ExprMapKeyType& that) const {
310 return this->opcode == that.opcode &&
Dan Gohman1b849082009-09-07 23:54:19 +0000311 this->subclassdata == that.subclassdata &&
312 this->subclassoptionaldata == that.subclassoptionaldata &&
Owen Anderson9b676982009-08-04 22:55:26 +0000313 this->operands == that.operands &&
314 this->indices == that.indices;
315 }
316 bool operator<(const ExprMapKeyType & that) const {
Dan Gohman1b849082009-09-07 23:54:19 +0000317 if (this->opcode != that.opcode) return this->opcode < that.opcode;
318 if (this->operands != that.operands) return this->operands < that.operands;
319 if (this->subclassdata != that.subclassdata)
320 return this->subclassdata < that.subclassdata;
321 if (this->subclassoptionaldata != that.subclassoptionaldata)
322 return this->subclassoptionaldata < that.subclassoptionaldata;
323 if (this->indices != that.indices) return this->indices < that.indices;
324 return false;
Owen Anderson9b676982009-08-04 22:55:26 +0000325 }
326
327 bool operator!=(const ExprMapKeyType& that) const {
328 return !(*this == that);
329 }
330};
331
332// The number of operands for each ConstantCreator::create method is
333// determined by the ConstantTraits template.
334// ConstantCreator - A class that is used to create constants by
335// ValueMap*. This class should be partially specialized if there is
336// something strange that needs to be done to interface to the ctor for the
337// constant.
338//
339template<typename T, typename Alloc>
340struct ConstantTraits< std::vector<T, Alloc> > {
341 static unsigned uses(const std::vector<T, Alloc>& v) {
342 return v.size();
343 }
344};
345
346template<class ConstantClass, class TypeClass, class ValType>
347struct ConstantCreator {
348 static ConstantClass *create(const TypeClass *Ty, const ValType &V) {
349 return new(ConstantTraits<ValType>::uses(V)) ConstantClass(Ty, V);
350 }
351};
352
353template<class ConstantClass, class TypeClass>
Devang Patelf7188322009-09-03 01:39:20 +0000354struct ConvertConstantType {
Owen Anderson9b676982009-08-04 22:55:26 +0000355 static void convert(ConstantClass *OldC, const TypeClass *NewTy) {
356 llvm_unreachable("This type cannot be converted!");
357 }
358};
359
360template<>
361struct ConstantCreator<ConstantExpr, Type, ExprMapKeyType> {
362 static ConstantExpr *create(const Type *Ty, const ExprMapKeyType &V,
363 unsigned short pred = 0) {
364 if (Instruction::isCast(V.opcode))
365 return new UnaryConstantExpr(V.opcode, V.operands[0], Ty);
366 if ((V.opcode >= Instruction::BinaryOpsBegin &&
367 V.opcode < Instruction::BinaryOpsEnd))
Dan Gohman1b849082009-09-07 23:54:19 +0000368 return new BinaryConstantExpr(V.opcode, V.operands[0], V.operands[1],
369 V.subclassoptionaldata);
Owen Anderson9b676982009-08-04 22:55:26 +0000370 if (V.opcode == Instruction::Select)
371 return new SelectConstantExpr(V.operands[0], V.operands[1],
372 V.operands[2]);
373 if (V.opcode == Instruction::ExtractElement)
374 return new ExtractElementConstantExpr(V.operands[0], V.operands[1]);
375 if (V.opcode == Instruction::InsertElement)
376 return new InsertElementConstantExpr(V.operands[0], V.operands[1],
377 V.operands[2]);
378 if (V.opcode == Instruction::ShuffleVector)
379 return new ShuffleVectorConstantExpr(V.operands[0], V.operands[1],
380 V.operands[2]);
381 if (V.opcode == Instruction::InsertValue)
382 return new InsertValueConstantExpr(V.operands[0], V.operands[1],
383 V.indices, Ty);
384 if (V.opcode == Instruction::ExtractValue)
385 return new ExtractValueConstantExpr(V.operands[0], V.indices, Ty);
386 if (V.opcode == Instruction::GetElementPtr) {
387 std::vector<Constant*> IdxList(V.operands.begin()+1, V.operands.end());
Dan Gohman1b849082009-09-07 23:54:19 +0000388 return GetElementPtrConstantExpr::Create(V.operands[0], IdxList, Ty,
389 V.subclassoptionaldata);
Owen Anderson9b676982009-08-04 22:55:26 +0000390 }
391
392 // The compare instructions are weird. We have to encode the predicate
393 // value and it is combined with the instruction opcode by multiplying
394 // the opcode by one hundred. We must decode this to get the predicate.
395 if (V.opcode == Instruction::ICmp)
Dan Gohman1b849082009-09-07 23:54:19 +0000396 return new CompareConstantExpr(Ty, Instruction::ICmp, V.subclassdata,
Owen Anderson9b676982009-08-04 22:55:26 +0000397 V.operands[0], V.operands[1]);
398 if (V.opcode == Instruction::FCmp)
Dan Gohman1b849082009-09-07 23:54:19 +0000399 return new CompareConstantExpr(Ty, Instruction::FCmp, V.subclassdata,
Owen Anderson9b676982009-08-04 22:55:26 +0000400 V.operands[0], V.operands[1]);
401 llvm_unreachable("Invalid ConstantExpr!");
402 return 0;
403 }
404};
405
406template<>
Devang Patelf7188322009-09-03 01:39:20 +0000407struct ConvertConstantType<ConstantExpr, Type> {
Owen Anderson9b676982009-08-04 22:55:26 +0000408 static void convert(ConstantExpr *OldC, const Type *NewTy) {
409 Constant *New;
410 switch (OldC->getOpcode()) {
411 case Instruction::Trunc:
412 case Instruction::ZExt:
413 case Instruction::SExt:
414 case Instruction::FPTrunc:
415 case Instruction::FPExt:
416 case Instruction::UIToFP:
417 case Instruction::SIToFP:
418 case Instruction::FPToUI:
419 case Instruction::FPToSI:
420 case Instruction::PtrToInt:
421 case Instruction::IntToPtr:
422 case Instruction::BitCast:
423 New = ConstantExpr::getCast(OldC->getOpcode(), OldC->getOperand(0),
424 NewTy);
425 break;
426 case Instruction::Select:
427 New = ConstantExpr::getSelectTy(NewTy, OldC->getOperand(0),
428 OldC->getOperand(1),
429 OldC->getOperand(2));
430 break;
431 default:
432 assert(OldC->getOpcode() >= Instruction::BinaryOpsBegin &&
433 OldC->getOpcode() < Instruction::BinaryOpsEnd);
434 New = ConstantExpr::getTy(NewTy, OldC->getOpcode(), OldC->getOperand(0),
435 OldC->getOperand(1));
436 break;
437 case Instruction::GetElementPtr:
438 // Make everyone now use a constant of the new type...
439 std::vector<Value*> Idx(OldC->op_begin()+1, OldC->op_end());
Dan Gohmanf436e872009-09-12 22:02:17 +0000440 New = cast<GEPOperator>(OldC)->isInBounds() ?
441 ConstantExpr::getInBoundsGetElementPtrTy(NewTy, OldC->getOperand(0),
442 &Idx[0], Idx.size()) :
443 ConstantExpr::getGetElementPtrTy(NewTy, OldC->getOperand(0),
444 &Idx[0], Idx.size());
Owen Anderson9b676982009-08-04 22:55:26 +0000445 break;
446 }
447
448 assert(New != OldC && "Didn't replace constant??");
449 OldC->uncheckedReplaceAllUsesWith(New);
450 OldC->destroyConstant(); // This constant is now dead, destroy it.
451 }
452};
453
454// ConstantAggregateZero does not take extra "value" argument...
455template<class ValType>
456struct ConstantCreator<ConstantAggregateZero, Type, ValType> {
457 static ConstantAggregateZero *create(const Type *Ty, const ValType &V){
458 return new ConstantAggregateZero(Ty);
459 }
460};
461
462template<>
Devang Patelf7188322009-09-03 01:39:20 +0000463struct ConvertConstantType<ConstantVector, VectorType> {
Owen Anderson9b676982009-08-04 22:55:26 +0000464 static void convert(ConstantVector *OldC, const VectorType *NewTy) {
465 // Make everyone now use a constant of the new type...
466 std::vector<Constant*> C;
467 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
468 C.push_back(cast<Constant>(OldC->getOperand(i)));
469 Constant *New = ConstantVector::get(NewTy, C);
470 assert(New != OldC && "Didn't replace constant??");
471 OldC->uncheckedReplaceAllUsesWith(New);
472 OldC->destroyConstant(); // This constant is now dead, destroy it.
473 }
474};
475
476template<>
Devang Patelf7188322009-09-03 01:39:20 +0000477struct ConvertConstantType<ConstantAggregateZero, Type> {
Owen Anderson9b676982009-08-04 22:55:26 +0000478 static void convert(ConstantAggregateZero *OldC, const Type *NewTy) {
479 // Make everyone now use a constant of the new type...
480 Constant *New = ConstantAggregateZero::get(NewTy);
481 assert(New != OldC && "Didn't replace constant??");
482 OldC->uncheckedReplaceAllUsesWith(New);
483 OldC->destroyConstant(); // This constant is now dead, destroy it.
484 }
485};
486
487template<>
Devang Patelf7188322009-09-03 01:39:20 +0000488struct ConvertConstantType<ConstantArray, ArrayType> {
Owen Anderson9b676982009-08-04 22:55:26 +0000489 static void convert(ConstantArray *OldC, const ArrayType *NewTy) {
490 // Make everyone now use a constant of the new type...
491 std::vector<Constant*> C;
492 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
493 C.push_back(cast<Constant>(OldC->getOperand(i)));
494 Constant *New = ConstantArray::get(NewTy, C);
495 assert(New != OldC && "Didn't replace constant??");
496 OldC->uncheckedReplaceAllUsesWith(New);
497 OldC->destroyConstant(); // This constant is now dead, destroy it.
498 }
499};
500
501template<>
Devang Patelf7188322009-09-03 01:39:20 +0000502struct ConvertConstantType<ConstantStruct, StructType> {
Owen Anderson9b676982009-08-04 22:55:26 +0000503 static void convert(ConstantStruct *OldC, const StructType *NewTy) {
504 // Make everyone now use a constant of the new type...
505 std::vector<Constant*> C;
506 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
507 C.push_back(cast<Constant>(OldC->getOperand(i)));
508 Constant *New = ConstantStruct::get(NewTy, C);
509 assert(New != OldC && "Didn't replace constant??");
510
511 OldC->uncheckedReplaceAllUsesWith(New);
512 OldC->destroyConstant(); // This constant is now dead, destroy it.
513 }
514};
515
516// ConstantPointerNull does not take extra "value" argument...
517template<class ValType>
518struct ConstantCreator<ConstantPointerNull, PointerType, ValType> {
519 static ConstantPointerNull *create(const PointerType *Ty, const ValType &V){
520 return new ConstantPointerNull(Ty);
521 }
522};
523
524template<>
Devang Patelf7188322009-09-03 01:39:20 +0000525struct ConvertConstantType<ConstantPointerNull, PointerType> {
Owen Anderson9b676982009-08-04 22:55:26 +0000526 static void convert(ConstantPointerNull *OldC, const PointerType *NewTy) {
527 // Make everyone now use a constant of the new type...
528 Constant *New = ConstantPointerNull::get(NewTy);
529 assert(New != OldC && "Didn't replace constant??");
530 OldC->uncheckedReplaceAllUsesWith(New);
531 OldC->destroyConstant(); // This constant is now dead, destroy it.
532 }
533};
534
535// UndefValue does not take extra "value" argument...
536template<class ValType>
537struct ConstantCreator<UndefValue, Type, ValType> {
538 static UndefValue *create(const Type *Ty, const ValType &V) {
539 return new UndefValue(Ty);
540 }
541};
542
543template<>
Devang Patelf7188322009-09-03 01:39:20 +0000544struct ConvertConstantType<UndefValue, Type> {
Owen Anderson9b676982009-08-04 22:55:26 +0000545 static void convert(UndefValue *OldC, const Type *NewTy) {
546 // Make everyone now use a constant of the new type.
547 Constant *New = UndefValue::get(NewTy);
548 assert(New != OldC && "Didn't replace constant??");
549 OldC->uncheckedReplaceAllUsesWith(New);
550 OldC->destroyConstant(); // This constant is now dead, destroy it.
551 }
552};
553
554template<class ValType, class TypeClass, class ConstantClass,
555 bool HasLargeKey = false /*true for arrays and structs*/ >
556class ValueMap : public AbstractTypeUser {
557public:
558 typedef std::pair<const Type*, ValType> MapKey;
Devang Patelf7188322009-09-03 01:39:20 +0000559 typedef std::map<MapKey, Constant *> MapTy;
560 typedef std::map<Constant*, typename MapTy::iterator> InverseMapTy;
Owen Anderson9b676982009-08-04 22:55:26 +0000561 typedef std::map<const Type*, typename MapTy::iterator> AbstractTypeMapTy;
562private:
563 /// Map - This is the main map from the element descriptor to the Constants.
564 /// This is the primary way we avoid creating two of the same shape
565 /// constant.
566 MapTy Map;
567
568 /// InverseMap - If "HasLargeKey" is true, this contains an inverse mapping
569 /// from the constants to their element in Map. This is important for
570 /// removal of constants from the array, which would otherwise have to scan
571 /// through the map with very large keys.
572 InverseMapTy InverseMap;
573
574 /// AbstractTypeMap - Map for abstract type constants.
575 ///
576 AbstractTypeMapTy AbstractTypeMap;
577
578 /// ValueMapLock - Mutex for this map.
579 sys::SmartMutex<true> ValueMapLock;
580
581public:
582 // NOTE: This function is not locked. It is the caller's responsibility
583 // to enforce proper synchronization.
Devang Patelc5aa8c62009-08-11 06:31:57 +0000584 typename MapTy::iterator map_begin() { return Map.begin(); }
Owen Anderson9b676982009-08-04 22:55:26 +0000585 typename MapTy::iterator map_end() { return Map.end(); }
Torok Edwind18e6682009-08-31 16:14:59 +0000586
587 void freeConstants() {
588 for (typename MapTy::iterator I=Map.begin(), E=Map.end();
589 I != E; ++I) {
590 if (I->second->use_empty())
591 delete I->second;
592 }
593 }
Owen Anderson9b676982009-08-04 22:55:26 +0000594
595 /// InsertOrGetItem - Return an iterator for the specified element.
596 /// If the element exists in the map, the returned iterator points to the
597 /// entry and Exists=true. If not, the iterator points to the newly
598 /// inserted entry and returns Exists=false. Newly inserted entries have
599 /// I->second == 0, and should be filled in.
600 /// NOTE: This function is not locked. It is the caller's responsibility
601 // to enforce proper synchronization.
602 typename MapTy::iterator InsertOrGetItem(std::pair<MapKey, Constant *>
603 &InsertVal,
604 bool &Exists) {
605 std::pair<typename MapTy::iterator, bool> IP = Map.insert(InsertVal);
606 Exists = !IP.second;
607 return IP.first;
608 }
609
610private:
611 typename MapTy::iterator FindExistingElement(ConstantClass *CP) {
612 if (HasLargeKey) {
613 typename InverseMapTy::iterator IMI = InverseMap.find(CP);
614 assert(IMI != InverseMap.end() && IMI->second != Map.end() &&
615 IMI->second->second == CP &&
616 "InverseMap corrupt!");
617 return IMI->second;
618 }
619
620 typename MapTy::iterator I =
621 Map.find(MapKey(static_cast<const TypeClass*>(CP->getRawType()),
622 getValType(CP)));
623 if (I == Map.end() || I->second != CP) {
624 // FIXME: This should not use a linear scan. If this gets to be a
625 // performance problem, someone should look at this.
626 for (I = Map.begin(); I != Map.end() && I->second != CP; ++I)
627 /* empty */;
628 }
629 return I;
630 }
631
632 ConstantClass* Create(const TypeClass *Ty, const ValType &V,
633 typename MapTy::iterator I) {
634 ConstantClass* Result =
635 ConstantCreator<ConstantClass,TypeClass,ValType>::create(Ty, V);
636
637 assert(Result->getType() == Ty && "Type specified is not correct!");
638 I = Map.insert(I, std::make_pair(MapKey(Ty, V), Result));
639
640 if (HasLargeKey) // Remember the reverse mapping if needed.
641 InverseMap.insert(std::make_pair(Result, I));
642
643 // If the type of the constant is abstract, make sure that an entry
644 // exists for it in the AbstractTypeMap.
645 if (Ty->isAbstract()) {
646 typename AbstractTypeMapTy::iterator TI =
647 AbstractTypeMap.find(Ty);
648
649 if (TI == AbstractTypeMap.end()) {
650 // Add ourselves to the ATU list of the type.
651 cast<DerivedType>(Ty)->addAbstractTypeUser(this);
652
653 AbstractTypeMap.insert(TI, std::make_pair(Ty, I));
654 }
655 }
656
657 return Result;
658 }
659public:
660
661 /// getOrCreate - Return the specified constant from the map, creating it if
662 /// necessary.
663 ConstantClass *getOrCreate(const TypeClass *Ty, const ValType &V) {
664 sys::SmartScopedLock<true> Lock(ValueMapLock);
665 MapKey Lookup(Ty, V);
666 ConstantClass* Result = 0;
667
668 typename MapTy::iterator I = Map.find(Lookup);
669 // Is it in the map?
670 if (I != Map.end())
671 Result = static_cast<ConstantClass *>(I->second);
672
673 if (!Result) {
674 // If no preexisting value, create one now...
675 Result = Create(Ty, V, I);
676 }
677
678 return Result;
679 }
680
681 void remove(ConstantClass *CP) {
682 sys::SmartScopedLock<true> Lock(ValueMapLock);
683 typename MapTy::iterator I = FindExistingElement(CP);
684 assert(I != Map.end() && "Constant not found in constant table!");
685 assert(I->second == CP && "Didn't find correct element?");
686
687 if (HasLargeKey) // Remember the reverse mapping if needed.
688 InverseMap.erase(CP);
689
690 // Now that we found the entry, make sure this isn't the entry that
691 // the AbstractTypeMap points to.
692 const TypeClass *Ty = static_cast<const TypeClass *>(I->first.first);
693 if (Ty->isAbstract()) {
694 assert(AbstractTypeMap.count(Ty) &&
695 "Abstract type not in AbstractTypeMap?");
696 typename MapTy::iterator &ATMEntryIt = AbstractTypeMap[Ty];
697 if (ATMEntryIt == I) {
698 // Yes, we are removing the representative entry for this type.
699 // See if there are any other entries of the same type.
700 typename MapTy::iterator TmpIt = ATMEntryIt;
701
702 // First check the entry before this one...
703 if (TmpIt != Map.begin()) {
704 --TmpIt;
705 if (TmpIt->first.first != Ty) // Not the same type, move back...
706 ++TmpIt;
707 }
708
709 // If we didn't find the same type, try to move forward...
710 if (TmpIt == ATMEntryIt) {
711 ++TmpIt;
712 if (TmpIt == Map.end() || TmpIt->first.first != Ty)
713 --TmpIt; // No entry afterwards with the same type
714 }
715
716 // If there is another entry in the map of the same abstract type,
717 // update the AbstractTypeMap entry now.
718 if (TmpIt != ATMEntryIt) {
719 ATMEntryIt = TmpIt;
720 } else {
721 // Otherwise, we are removing the last instance of this type
722 // from the table. Remove from the ATM, and from user list.
723 cast<DerivedType>(Ty)->removeAbstractTypeUser(this);
724 AbstractTypeMap.erase(Ty);
725 }
726 }
727 }
728
729 Map.erase(I);
730 }
731
732
733 /// MoveConstantToNewSlot - If we are about to change C to be the element
734 /// specified by I, update our internal data structures to reflect this
735 /// fact.
736 /// NOTE: This function is not locked. It is the responsibility of the
737 /// caller to enforce proper synchronization if using this method.
738 void MoveConstantToNewSlot(ConstantClass *C, typename MapTy::iterator I) {
739 // First, remove the old location of the specified constant in the map.
740 typename MapTy::iterator OldI = FindExistingElement(C);
741 assert(OldI != Map.end() && "Constant not found in constant table!");
742 assert(OldI->second == C && "Didn't find correct element?");
743
744 // If this constant is the representative element for its abstract type,
745 // update the AbstractTypeMap so that the representative element is I.
746 if (C->getType()->isAbstract()) {
747 typename AbstractTypeMapTy::iterator ATI =
748 AbstractTypeMap.find(C->getType());
749 assert(ATI != AbstractTypeMap.end() &&
750 "Abstract type not in AbstractTypeMap?");
751 if (ATI->second == OldI)
752 ATI->second = I;
753 }
754
755 // Remove the old entry from the map.
756 Map.erase(OldI);
757
758 // Update the inverse map so that we know that this constant is now
759 // located at descriptor I.
760 if (HasLargeKey) {
761 assert(I->second == C && "Bad inversemap entry!");
762 InverseMap[C] = I;
763 }
764 }
765
766 void refineAbstractType(const DerivedType *OldTy, const Type *NewTy) {
767 sys::SmartScopedLock<true> Lock(ValueMapLock);
768 typename AbstractTypeMapTy::iterator I =
769 AbstractTypeMap.find(cast<Type>(OldTy));
770
771 assert(I != AbstractTypeMap.end() &&
772 "Abstract type not in AbstractTypeMap?");
773
774 // Convert a constant at a time until the last one is gone. The last one
775 // leaving will remove() itself, causing the AbstractTypeMapEntry to be
776 // eliminated eventually.
777 do {
Devang Patelf7188322009-09-03 01:39:20 +0000778 ConvertConstantType<ConstantClass,
779 TypeClass>::convert(
Owen Anderson9b676982009-08-04 22:55:26 +0000780 static_cast<ConstantClass *>(I->second->second),
781 cast<TypeClass>(NewTy));
782
783 I = AbstractTypeMap.find(cast<Type>(OldTy));
784 } while (I != AbstractTypeMap.end());
785 }
786
787 // If the type became concrete without being refined to any other existing
788 // type, we just remove ourselves from the ATU list.
789 void typeBecameConcrete(const DerivedType *AbsTy) {
790 AbsTy->removeAbstractTypeUser(this);
791 }
792
793 void dump() const {
Chris Lattner34822f62009-08-23 04:44:11 +0000794 DEBUG(errs() << "Constant.cpp: ValueMap\n");
Owen Anderson9b676982009-08-04 22:55:26 +0000795 }
796};
797
798}
799
800#endif