blob: a7277f6736456dd716dcfb768902e3e048c3d20e [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
Talin46e9b442012-02-05 20:54:10 +000018#include "llvm/ADT/DenseMap.h"
Jeffrey Yasskinade270e2010-03-21 20:37:19 +000019#include "llvm/InlineAsm.h"
Owen Anderson9b676982009-08-04 22:55:26 +000020#include "llvm/Instructions.h"
21#include "llvm/Operator.h"
David Greene338a9032010-01-05 01:34:26 +000022#include "llvm/Support/Debug.h"
Owen Anderson9b676982009-08-04 22:55:26 +000023#include "llvm/Support/ErrorHandling.h"
Chris Lattner34822f62009-08-23 04:44:11 +000024#include "llvm/Support/raw_ostream.h"
Owen Anderson9b676982009-08-04 22:55:26 +000025#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 {
David Blaikiea379b1812011-12-20 02:50:00 +000034 virtual void anchor();
Owen Anderson9b676982009-08-04 22:55:26 +000035 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
36public:
37 // allocate space for exactly one operand
38 void *operator new(size_t s) {
39 return User::operator new(s, 1);
40 }
Chris Lattner229907c2011-07-18 04:54:35 +000041 UnaryConstantExpr(unsigned Opcode, Constant *C, Type *Ty)
Owen Anderson9b676982009-08-04 22:55:26 +000042 : ConstantExpr(Ty, Opcode, &Op<0>(), 1) {
43 Op<0>() = C;
44 }
45 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
46};
47
48/// BinaryConstantExpr - This class is private to Constants.cpp, and is used
49/// behind the scenes to implement binary constant exprs.
50class BinaryConstantExpr : public ConstantExpr {
David Blaikiea379b1812011-12-20 02:50:00 +000051 virtual void anchor();
Owen Anderson9b676982009-08-04 22:55:26 +000052 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
53public:
54 // allocate space for exactly two operands
55 void *operator new(size_t s) {
56 return User::operator new(s, 2);
57 }
Dan Gohman1b849082009-09-07 23:54:19 +000058 BinaryConstantExpr(unsigned Opcode, Constant *C1, Constant *C2,
59 unsigned Flags)
Owen Anderson9b676982009-08-04 22:55:26 +000060 : ConstantExpr(C1->getType(), Opcode, &Op<0>(), 2) {
61 Op<0>() = C1;
62 Op<1>() = C2;
Dan Gohman1b849082009-09-07 23:54:19 +000063 SubclassOptionalData = Flags;
Owen Anderson9b676982009-08-04 22:55:26 +000064 }
65 /// Transparently provide more efficient getOperand methods.
66 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
67};
68
69/// SelectConstantExpr - This class is private to Constants.cpp, and is used
70/// behind the scenes to implement select constant exprs.
71class SelectConstantExpr : public ConstantExpr {
David Blaikiea379b1812011-12-20 02:50:00 +000072 virtual void anchor();
Owen Anderson9b676982009-08-04 22:55:26 +000073 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
74public:
75 // allocate space for exactly three operands
76 void *operator new(size_t s) {
77 return User::operator new(s, 3);
78 }
79 SelectConstantExpr(Constant *C1, Constant *C2, Constant *C3)
80 : ConstantExpr(C2->getType(), Instruction::Select, &Op<0>(), 3) {
81 Op<0>() = C1;
82 Op<1>() = C2;
83 Op<2>() = C3;
84 }
85 /// Transparently provide more efficient getOperand methods.
86 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
87};
88
89/// ExtractElementConstantExpr - This class is private to
90/// Constants.cpp, and is used behind the scenes to implement
91/// extractelement constant exprs.
92class ExtractElementConstantExpr : public ConstantExpr {
David Blaikiea379b1812011-12-20 02:50:00 +000093 virtual void anchor();
Owen Anderson9b676982009-08-04 22:55:26 +000094 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
95public:
96 // allocate space for exactly two operands
97 void *operator new(size_t s) {
98 return User::operator new(s, 2);
99 }
100 ExtractElementConstantExpr(Constant *C1, Constant *C2)
101 : ConstantExpr(cast<VectorType>(C1->getType())->getElementType(),
102 Instruction::ExtractElement, &Op<0>(), 2) {
103 Op<0>() = C1;
104 Op<1>() = C2;
105 }
106 /// Transparently provide more efficient getOperand methods.
107 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
108};
109
110/// InsertElementConstantExpr - This class is private to
111/// Constants.cpp, and is used behind the scenes to implement
112/// insertelement constant exprs.
113class InsertElementConstantExpr : public ConstantExpr {
David Blaikiea379b1812011-12-20 02:50:00 +0000114 virtual void anchor();
Owen Anderson9b676982009-08-04 22:55:26 +0000115 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
116public:
117 // allocate space for exactly three operands
118 void *operator new(size_t s) {
119 return User::operator new(s, 3);
120 }
121 InsertElementConstantExpr(Constant *C1, Constant *C2, Constant *C3)
122 : ConstantExpr(C1->getType(), Instruction::InsertElement,
123 &Op<0>(), 3) {
124 Op<0>() = C1;
125 Op<1>() = C2;
126 Op<2>() = C3;
127 }
128 /// Transparently provide more efficient getOperand methods.
129 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
130};
131
132/// ShuffleVectorConstantExpr - This class is private to
133/// Constants.cpp, and is used behind the scenes to implement
134/// shufflevector constant exprs.
135class ShuffleVectorConstantExpr : public ConstantExpr {
David Blaikiea379b1812011-12-20 02:50:00 +0000136 virtual void anchor();
Owen Anderson9b676982009-08-04 22:55:26 +0000137 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
138public:
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<VectorType>(C1->getType())->getElementType(),
146 cast<VectorType>(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.
160class ExtractValueConstantExpr : public ConstantExpr {
David Blaikiea379b1812011-12-20 02:50:00 +0000161 virtual void anchor();
Owen Anderson9b676982009-08-04 22:55:26 +0000162 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
163public:
164 // allocate space for exactly one operand
165 void *operator new(size_t s) {
166 return User::operator new(s, 1);
167 }
168 ExtractValueConstantExpr(Constant *Agg,
169 const SmallVector<unsigned, 4> &IdxList,
Chris Lattner229907c2011-07-18 04:54:35 +0000170 Type *DestTy)
Owen Anderson9b676982009-08-04 22:55:26 +0000171 : ConstantExpr(DestTy, Instruction::ExtractValue, &Op<0>(), 1),
172 Indices(IdxList) {
173 Op<0>() = Agg;
174 }
175
176 /// Indices - These identify which value to extract.
177 const SmallVector<unsigned, 4> Indices;
178
179 /// Transparently provide more efficient getOperand methods.
180 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
181};
182
183/// InsertValueConstantExpr - This class is private to
184/// Constants.cpp, and is used behind the scenes to implement
185/// insertvalue constant exprs.
186class InsertValueConstantExpr : public ConstantExpr {
David Blaikiea379b1812011-12-20 02:50:00 +0000187 virtual void anchor();
Owen Anderson9b676982009-08-04 22:55:26 +0000188 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
189public:
190 // allocate space for exactly one operand
191 void *operator new(size_t s) {
192 return User::operator new(s, 2);
193 }
194 InsertValueConstantExpr(Constant *Agg, Constant *Val,
195 const SmallVector<unsigned, 4> &IdxList,
Chris Lattner229907c2011-07-18 04:54:35 +0000196 Type *DestTy)
Owen Anderson9b676982009-08-04 22:55:26 +0000197 : ConstantExpr(DestTy, Instruction::InsertValue, &Op<0>(), 2),
198 Indices(IdxList) {
199 Op<0>() = Agg;
200 Op<1>() = Val;
201 }
202
203 /// Indices - These identify the position for the insertion.
204 const SmallVector<unsigned, 4> Indices;
205
206 /// Transparently provide more efficient getOperand methods.
207 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
208};
209
210
211/// GetElementPtrConstantExpr - This class is private to Constants.cpp, and is
212/// used behind the scenes to implement getelementpr constant exprs.
213class GetElementPtrConstantExpr : public ConstantExpr {
David Blaikiea379b1812011-12-20 02:50:00 +0000214 virtual void anchor();
Chris Lattnera474bb22012-01-26 20:40:56 +0000215 GetElementPtrConstantExpr(Constant *C, ArrayRef<Constant*> IdxList,
Chris Lattner229907c2011-07-18 04:54:35 +0000216 Type *DestTy);
Owen Anderson9b676982009-08-04 22:55:26 +0000217public:
218 static GetElementPtrConstantExpr *Create(Constant *C,
Chris Lattnera474bb22012-01-26 20:40:56 +0000219 ArrayRef<Constant*> IdxList,
Chris Lattner229907c2011-07-18 04:54:35 +0000220 Type *DestTy,
Dan Gohman1b849082009-09-07 23:54:19 +0000221 unsigned Flags) {
222 GetElementPtrConstantExpr *Result =
Owen Anderson9b676982009-08-04 22:55:26 +0000223 new(IdxList.size() + 1) GetElementPtrConstantExpr(C, IdxList, DestTy);
Dan Gohman1b849082009-09-07 23:54:19 +0000224 Result->SubclassOptionalData = Flags;
225 return Result;
Owen Anderson9b676982009-08-04 22:55:26 +0000226 }
227 /// Transparently provide more efficient getOperand methods.
228 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
229};
230
231// CompareConstantExpr - This class is private to Constants.cpp, and is used
232// behind the scenes to implement ICmp and FCmp constant expressions. This is
233// needed in order to store the predicate value for these instructions.
David Blaikiea379b1812011-12-20 02:50:00 +0000234class CompareConstantExpr : public ConstantExpr {
235 virtual void anchor();
Owen Anderson9b676982009-08-04 22:55:26 +0000236 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
David Blaikiea379b1812011-12-20 02:50:00 +0000237public:
Owen Anderson9b676982009-08-04 22:55:26 +0000238 // allocate space for exactly two operands
239 void *operator new(size_t s) {
240 return User::operator new(s, 2);
241 }
242 unsigned short predicate;
Chris Lattner229907c2011-07-18 04:54:35 +0000243 CompareConstantExpr(Type *ty, Instruction::OtherOps opc,
Owen Anderson9b676982009-08-04 22:55:26 +0000244 unsigned short pred, Constant* LHS, Constant* RHS)
245 : ConstantExpr(ty, opc, &Op<0>(), 2), predicate(pred) {
246 Op<0>() = LHS;
247 Op<1>() = RHS;
248 }
249 /// Transparently provide more efficient getOperand methods.
250 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
251};
252
253template <>
Jay Foadc8adf5f2011-01-11 15:07:38 +0000254struct OperandTraits<UnaryConstantExpr> :
255 public FixedNumOperandTraits<UnaryConstantExpr, 1> {
Owen Anderson9b676982009-08-04 22:55:26 +0000256};
257DEFINE_TRANSPARENT_OPERAND_ACCESSORS(UnaryConstantExpr, Value)
258
259template <>
Jay Foadc8adf5f2011-01-11 15:07:38 +0000260struct OperandTraits<BinaryConstantExpr> :
261 public FixedNumOperandTraits<BinaryConstantExpr, 2> {
Owen Anderson9b676982009-08-04 22:55:26 +0000262};
263DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BinaryConstantExpr, Value)
264
265template <>
Jay Foadc8adf5f2011-01-11 15:07:38 +0000266struct OperandTraits<SelectConstantExpr> :
267 public FixedNumOperandTraits<SelectConstantExpr, 3> {
Owen Anderson9b676982009-08-04 22:55:26 +0000268};
269DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SelectConstantExpr, Value)
270
271template <>
Jay Foadc8adf5f2011-01-11 15:07:38 +0000272struct OperandTraits<ExtractElementConstantExpr> :
273 public FixedNumOperandTraits<ExtractElementConstantExpr, 2> {
Owen Anderson9b676982009-08-04 22:55:26 +0000274};
275DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractElementConstantExpr, Value)
276
277template <>
Jay Foadc8adf5f2011-01-11 15:07:38 +0000278struct OperandTraits<InsertElementConstantExpr> :
279 public FixedNumOperandTraits<InsertElementConstantExpr, 3> {
Owen Anderson9b676982009-08-04 22:55:26 +0000280};
281DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementConstantExpr, Value)
282
283template <>
Jay Foadc8adf5f2011-01-11 15:07:38 +0000284struct OperandTraits<ShuffleVectorConstantExpr> :
285 public FixedNumOperandTraits<ShuffleVectorConstantExpr, 3> {
Owen Anderson9b676982009-08-04 22:55:26 +0000286};
287DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ShuffleVectorConstantExpr, Value)
288
289template <>
Jay Foadc8adf5f2011-01-11 15:07:38 +0000290struct OperandTraits<ExtractValueConstantExpr> :
291 public FixedNumOperandTraits<ExtractValueConstantExpr, 1> {
Owen Anderson9b676982009-08-04 22:55:26 +0000292};
293DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractValueConstantExpr, Value)
294
295template <>
Jay Foadc8adf5f2011-01-11 15:07:38 +0000296struct OperandTraits<InsertValueConstantExpr> :
297 public FixedNumOperandTraits<InsertValueConstantExpr, 2> {
Owen Anderson9b676982009-08-04 22:55:26 +0000298};
299DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertValueConstantExpr, Value)
300
301template <>
Jay Foadc8adf5f2011-01-11 15:07:38 +0000302struct OperandTraits<GetElementPtrConstantExpr> :
303 public VariadicOperandTraits<GetElementPtrConstantExpr, 1> {
Owen Anderson9b676982009-08-04 22:55:26 +0000304};
305
306DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GetElementPtrConstantExpr, Value)
307
308
309template <>
Jay Foadc8adf5f2011-01-11 15:07:38 +0000310struct OperandTraits<CompareConstantExpr> :
311 public FixedNumOperandTraits<CompareConstantExpr, 2> {
Owen Anderson9b676982009-08-04 22:55:26 +0000312};
313DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CompareConstantExpr, Value)
314
315struct ExprMapKeyType {
Owen Anderson9b676982009-08-04 22:55:26 +0000316 ExprMapKeyType(unsigned opc,
Jay Foad0091fe82011-04-13 15:22:40 +0000317 ArrayRef<Constant*> ops,
Dan Gohman1b849082009-09-07 23:54:19 +0000318 unsigned short flags = 0,
319 unsigned short optionalflags = 0,
Jay Foad0091fe82011-04-13 15:22:40 +0000320 ArrayRef<unsigned> inds = ArrayRef<unsigned>())
Dan Gohman1b849082009-09-07 23:54:19 +0000321 : opcode(opc), subclassoptionaldata(optionalflags), subclassdata(flags),
Jay Foad0091fe82011-04-13 15:22:40 +0000322 operands(ops.begin(), ops.end()), indices(inds.begin(), inds.end()) {}
Dan Gohman1b849082009-09-07 23:54:19 +0000323 uint8_t opcode;
324 uint8_t subclassoptionaldata;
325 uint16_t subclassdata;
Owen Anderson9b676982009-08-04 22:55:26 +0000326 std::vector<Constant*> operands;
Jay Foad0091fe82011-04-13 15:22:40 +0000327 SmallVector<unsigned, 4> indices;
Owen Anderson9b676982009-08-04 22:55:26 +0000328 bool operator==(const ExprMapKeyType& that) const {
329 return this->opcode == that.opcode &&
Dan Gohman1b849082009-09-07 23:54:19 +0000330 this->subclassdata == that.subclassdata &&
331 this->subclassoptionaldata == that.subclassoptionaldata &&
Owen Anderson9b676982009-08-04 22:55:26 +0000332 this->operands == that.operands &&
333 this->indices == that.indices;
334 }
335 bool operator<(const ExprMapKeyType & that) const {
Dan Gohman1b849082009-09-07 23:54:19 +0000336 if (this->opcode != that.opcode) return this->opcode < that.opcode;
337 if (this->operands != that.operands) return this->operands < that.operands;
338 if (this->subclassdata != that.subclassdata)
339 return this->subclassdata < that.subclassdata;
340 if (this->subclassoptionaldata != that.subclassoptionaldata)
341 return this->subclassoptionaldata < that.subclassoptionaldata;
342 if (this->indices != that.indices) return this->indices < that.indices;
343 return false;
Owen Anderson9b676982009-08-04 22:55:26 +0000344 }
345
346 bool operator!=(const ExprMapKeyType& that) const {
347 return !(*this == that);
348 }
349};
350
Jeffrey Yasskinade270e2010-03-21 20:37:19 +0000351struct InlineAsmKeyType {
352 InlineAsmKeyType(StringRef AsmString,
353 StringRef Constraints, bool hasSideEffects,
354 bool isAlignStack)
355 : asm_string(AsmString), constraints(Constraints),
356 has_side_effects(hasSideEffects), is_align_stack(isAlignStack) {}
357 std::string asm_string;
358 std::string constraints;
359 bool has_side_effects;
360 bool is_align_stack;
361 bool operator==(const InlineAsmKeyType& that) const {
362 return this->asm_string == that.asm_string &&
363 this->constraints == that.constraints &&
364 this->has_side_effects == that.has_side_effects &&
365 this->is_align_stack == that.is_align_stack;
366 }
367 bool operator<(const InlineAsmKeyType& that) const {
368 if (this->asm_string != that.asm_string)
369 return this->asm_string < that.asm_string;
370 if (this->constraints != that.constraints)
371 return this->constraints < that.constraints;
372 if (this->has_side_effects != that.has_side_effects)
373 return this->has_side_effects < that.has_side_effects;
374 if (this->is_align_stack != that.is_align_stack)
375 return this->is_align_stack < that.is_align_stack;
376 return false;
377 }
378
379 bool operator!=(const InlineAsmKeyType& that) const {
380 return !(*this == that);
381 }
382};
383
Owen Anderson9b676982009-08-04 22:55:26 +0000384// The number of operands for each ConstantCreator::create method is
385// determined by the ConstantTraits template.
386// ConstantCreator - A class that is used to create constants by
Jeffrey Yasskinf6ee7be2009-10-27 23:45:55 +0000387// ConstantUniqueMap*. This class should be partially specialized if there is
Owen Anderson9b676982009-08-04 22:55:26 +0000388// something strange that needs to be done to interface to the ctor for the
389// constant.
390//
391template<typename T, typename Alloc>
392struct ConstantTraits< std::vector<T, Alloc> > {
393 static unsigned uses(const std::vector<T, Alloc>& v) {
394 return v.size();
395 }
396};
397
Chris Lattner392be582010-02-12 20:49:41 +0000398template<>
399struct ConstantTraits<Constant *> {
400 static unsigned uses(Constant * const & v) {
401 return 1;
402 }
403};
404
Owen Anderson9b676982009-08-04 22:55:26 +0000405template<class ConstantClass, class TypeClass, class ValType>
406struct ConstantCreator {
Chris Lattner229907c2011-07-18 04:54:35 +0000407 static ConstantClass *create(TypeClass *Ty, const ValType &V) {
Owen Anderson9b676982009-08-04 22:55:26 +0000408 return new(ConstantTraits<ValType>::uses(V)) ConstantClass(Ty, V);
409 }
410};
411
Talin46e9b442012-02-05 20:54:10 +0000412template<class ConstantClass, class TypeClass>
413struct ConstantArrayCreator {
414 static ConstantClass *create(TypeClass *Ty, ArrayRef<Constant*> V) {
415 return new(V.size()) ConstantClass(Ty, V);
416 }
417};
418
Dan Gohmane4532f32009-09-15 15:58:07 +0000419template<class ConstantClass>
420struct ConstantKeyData {
421 typedef void ValType;
422 static ValType getValType(ConstantClass *C) {
423 llvm_unreachable("Unknown Constant type!");
Owen Anderson9b676982009-08-04 22:55:26 +0000424 }
425};
426
427template<>
428struct ConstantCreator<ConstantExpr, Type, ExprMapKeyType> {
Chris Lattner229907c2011-07-18 04:54:35 +0000429 static ConstantExpr *create(Type *Ty, const ExprMapKeyType &V,
Owen Anderson9b676982009-08-04 22:55:26 +0000430 unsigned short pred = 0) {
431 if (Instruction::isCast(V.opcode))
432 return new UnaryConstantExpr(V.opcode, V.operands[0], Ty);
433 if ((V.opcode >= Instruction::BinaryOpsBegin &&
434 V.opcode < Instruction::BinaryOpsEnd))
Dan Gohman1b849082009-09-07 23:54:19 +0000435 return new BinaryConstantExpr(V.opcode, V.operands[0], V.operands[1],
436 V.subclassoptionaldata);
Owen Anderson9b676982009-08-04 22:55:26 +0000437 if (V.opcode == Instruction::Select)
438 return new SelectConstantExpr(V.operands[0], V.operands[1],
439 V.operands[2]);
440 if (V.opcode == Instruction::ExtractElement)
441 return new ExtractElementConstantExpr(V.operands[0], V.operands[1]);
442 if (V.opcode == Instruction::InsertElement)
443 return new InsertElementConstantExpr(V.operands[0], V.operands[1],
444 V.operands[2]);
445 if (V.opcode == Instruction::ShuffleVector)
446 return new ShuffleVectorConstantExpr(V.operands[0], V.operands[1],
447 V.operands[2]);
448 if (V.opcode == Instruction::InsertValue)
449 return new InsertValueConstantExpr(V.operands[0], V.operands[1],
450 V.indices, Ty);
451 if (V.opcode == Instruction::ExtractValue)
452 return new ExtractValueConstantExpr(V.operands[0], V.indices, Ty);
453 if (V.opcode == Instruction::GetElementPtr) {
454 std::vector<Constant*> IdxList(V.operands.begin()+1, V.operands.end());
Dan Gohman1b849082009-09-07 23:54:19 +0000455 return GetElementPtrConstantExpr::Create(V.operands[0], IdxList, Ty,
456 V.subclassoptionaldata);
Owen Anderson9b676982009-08-04 22:55:26 +0000457 }
458
459 // The compare instructions are weird. We have to encode the predicate
460 // value and it is combined with the instruction opcode by multiplying
461 // the opcode by one hundred. We must decode this to get the predicate.
462 if (V.opcode == Instruction::ICmp)
Dan Gohman1b849082009-09-07 23:54:19 +0000463 return new CompareConstantExpr(Ty, Instruction::ICmp, V.subclassdata,
Owen Anderson9b676982009-08-04 22:55:26 +0000464 V.operands[0], V.operands[1]);
465 if (V.opcode == Instruction::FCmp)
Dan Gohman1b849082009-09-07 23:54:19 +0000466 return new CompareConstantExpr(Ty, Instruction::FCmp, V.subclassdata,
Owen Anderson9b676982009-08-04 22:55:26 +0000467 V.operands[0], V.operands[1]);
468 llvm_unreachable("Invalid ConstantExpr!");
Owen Anderson9b676982009-08-04 22:55:26 +0000469 }
470};
471
472template<>
Dan Gohmane4532f32009-09-15 15:58:07 +0000473struct ConstantKeyData<ConstantExpr> {
474 typedef ExprMapKeyType ValType;
475 static ValType getValType(ConstantExpr *CE) {
476 std::vector<Constant*> Operands;
477 Operands.reserve(CE->getNumOperands());
478 for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i)
479 Operands.push_back(cast<Constant>(CE->getOperand(i)));
480 return ExprMapKeyType(CE->getOpcode(), Operands,
481 CE->isCompare() ? CE->getPredicate() : 0,
482 CE->getRawSubclassOptionalData(),
483 CE->hasIndices() ?
Jay Foad0091fe82011-04-13 15:22:40 +0000484 CE->getIndices() : ArrayRef<unsigned>());
Owen Anderson9b676982009-08-04 22:55:26 +0000485 }
486};
487
Jeffrey Yasskinade270e2010-03-21 20:37:19 +0000488template<>
489struct ConstantCreator<InlineAsm, PointerType, InlineAsmKeyType> {
Chris Lattner229907c2011-07-18 04:54:35 +0000490 static InlineAsm *create(PointerType *Ty, const InlineAsmKeyType &Key) {
Jeffrey Yasskinade270e2010-03-21 20:37:19 +0000491 return new InlineAsm(Ty, Key.asm_string, Key.constraints,
492 Key.has_side_effects, Key.is_align_stack);
493 }
494};
495
496template<>
497struct ConstantKeyData<InlineAsm> {
498 typedef InlineAsmKeyType ValType;
499 static ValType getValType(InlineAsm *Asm) {
500 return InlineAsmKeyType(Asm->getAsmString(), Asm->getConstraintString(),
501 Asm->hasSideEffects(), Asm->isAlignStack());
502 }
503};
504
Jay Foadc365eea2011-06-22 08:50:06 +0000505template<class ValType, class ValRefType, class TypeClass, class ConstantClass,
Owen Anderson9b676982009-08-04 22:55:26 +0000506 bool HasLargeKey = false /*true for arrays and structs*/ >
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000507class ConstantUniqueMap {
Owen Anderson9b676982009-08-04 22:55:26 +0000508public:
Chris Lattner229907c2011-07-18 04:54:35 +0000509 typedef std::pair<TypeClass*, ValType> MapKey;
Dan Gohmane4532f32009-09-15 15:58:07 +0000510 typedef std::map<MapKey, ConstantClass *> MapTy;
511 typedef std::map<ConstantClass *, typename MapTy::iterator> InverseMapTy;
Owen Anderson9b676982009-08-04 22:55:26 +0000512private:
513 /// Map - This is the main map from the element descriptor to the Constants.
514 /// This is the primary way we avoid creating two of the same shape
515 /// constant.
516 MapTy Map;
517
518 /// InverseMap - If "HasLargeKey" is true, this contains an inverse mapping
519 /// from the constants to their element in Map. This is important for
520 /// removal of constants from the array, which would otherwise have to scan
521 /// through the map with very large keys.
522 InverseMapTy InverseMap;
523
Owen Anderson9b676982009-08-04 22:55:26 +0000524public:
Devang Patelc5aa8c62009-08-11 06:31:57 +0000525 typename MapTy::iterator map_begin() { return Map.begin(); }
Owen Anderson9b676982009-08-04 22:55:26 +0000526 typename MapTy::iterator map_end() { return Map.end(); }
Torok Edwind18e6682009-08-31 16:14:59 +0000527
528 void freeConstants() {
529 for (typename MapTy::iterator I=Map.begin(), E=Map.end();
530 I != E; ++I) {
Jeffrey Yasskina6eedc32010-03-22 05:23:37 +0000531 // Asserts that use_empty().
532 delete I->second;
Torok Edwind18e6682009-08-31 16:14:59 +0000533 }
534 }
Owen Anderson9b676982009-08-04 22:55:26 +0000535
536 /// InsertOrGetItem - Return an iterator for the specified element.
537 /// If the element exists in the map, the returned iterator points to the
538 /// entry and Exists=true. If not, the iterator points to the newly
539 /// inserted entry and returns Exists=false. Newly inserted entries have
540 /// I->second == 0, and should be filled in.
Dan Gohmane4532f32009-09-15 15:58:07 +0000541 typename MapTy::iterator InsertOrGetItem(std::pair<MapKey, ConstantClass *>
Owen Anderson9b676982009-08-04 22:55:26 +0000542 &InsertVal,
543 bool &Exists) {
544 std::pair<typename MapTy::iterator, bool> IP = Map.insert(InsertVal);
545 Exists = !IP.second;
546 return IP.first;
547 }
548
549private:
550 typename MapTy::iterator FindExistingElement(ConstantClass *CP) {
551 if (HasLargeKey) {
552 typename InverseMapTy::iterator IMI = InverseMap.find(CP);
553 assert(IMI != InverseMap.end() && IMI->second != Map.end() &&
554 IMI->second->second == CP &&
555 "InverseMap corrupt!");
556 return IMI->second;
557 }
558
559 typename MapTy::iterator I =
Chris Lattner229907c2011-07-18 04:54:35 +0000560 Map.find(MapKey(static_cast<TypeClass*>(CP->getType()),
Dan Gohmane4532f32009-09-15 15:58:07 +0000561 ConstantKeyData<ConstantClass>::getValType(CP)));
Owen Anderson9b676982009-08-04 22:55:26 +0000562 if (I == Map.end() || I->second != CP) {
563 // FIXME: This should not use a linear scan. If this gets to be a
564 // performance problem, someone should look at this.
565 for (I = Map.begin(); I != Map.end() && I->second != CP; ++I)
566 /* empty */;
567 }
568 return I;
569 }
Dan Gohmane4532f32009-09-15 15:58:07 +0000570
Chris Lattner229907c2011-07-18 04:54:35 +0000571 ConstantClass *Create(TypeClass *Ty, ValRefType V,
Owen Anderson9b676982009-08-04 22:55:26 +0000572 typename MapTy::iterator I) {
573 ConstantClass* Result =
574 ConstantCreator<ConstantClass,TypeClass,ValType>::create(Ty, V);
575
576 assert(Result->getType() == Ty && "Type specified is not correct!");
577 I = Map.insert(I, std::make_pair(MapKey(Ty, V), Result));
578
579 if (HasLargeKey) // Remember the reverse mapping if needed.
580 InverseMap.insert(std::make_pair(Result, I));
581
Owen Anderson9b676982009-08-04 22:55:26 +0000582 return Result;
583 }
584public:
585
586 /// getOrCreate - Return the specified constant from the map, creating it if
587 /// necessary.
Chris Lattner229907c2011-07-18 04:54:35 +0000588 ConstantClass *getOrCreate(TypeClass *Ty, ValRefType V) {
Owen Anderson9b676982009-08-04 22:55:26 +0000589 MapKey Lookup(Ty, V);
590 ConstantClass* Result = 0;
591
592 typename MapTy::iterator I = Map.find(Lookup);
593 // Is it in the map?
594 if (I != Map.end())
Dan Gohmane4532f32009-09-15 15:58:07 +0000595 Result = I->second;
Owen Anderson9b676982009-08-04 22:55:26 +0000596
597 if (!Result) {
598 // If no preexisting value, create one now...
599 Result = Create(Ty, V, I);
600 }
601
602 return Result;
603 }
604
605 void remove(ConstantClass *CP) {
Owen Anderson9b676982009-08-04 22:55:26 +0000606 typename MapTy::iterator I = FindExistingElement(CP);
607 assert(I != Map.end() && "Constant not found in constant table!");
608 assert(I->second == CP && "Didn't find correct element?");
609
610 if (HasLargeKey) // Remember the reverse mapping if needed.
611 InverseMap.erase(CP);
Owen Anderson9b676982009-08-04 22:55:26 +0000612
613 Map.erase(I);
614 }
615
Owen Anderson9b676982009-08-04 22:55:26 +0000616 /// MoveConstantToNewSlot - If we are about to change C to be the element
617 /// specified by I, update our internal data structures to reflect this
618 /// fact.
Owen Anderson9b676982009-08-04 22:55:26 +0000619 void MoveConstantToNewSlot(ConstantClass *C, typename MapTy::iterator I) {
620 // First, remove the old location of the specified constant in the map.
621 typename MapTy::iterator OldI = FindExistingElement(C);
622 assert(OldI != Map.end() && "Constant not found in constant table!");
623 assert(OldI->second == C && "Didn't find correct element?");
624
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000625 // Remove the old entry from the map.
Owen Anderson9b676982009-08-04 22:55:26 +0000626 Map.erase(OldI);
627
628 // Update the inverse map so that we know that this constant is now
629 // located at descriptor I.
630 if (HasLargeKey) {
631 assert(I->second == C && "Bad inversemap entry!");
632 InverseMap[C] = I;
633 }
634 }
Owen Anderson9b676982009-08-04 22:55:26 +0000635
636 void dump() const {
David Greene338a9032010-01-05 01:34:26 +0000637 DEBUG(dbgs() << "Constant.cpp: ConstantUniqueMap\n");
Owen Anderson9b676982009-08-04 22:55:26 +0000638 }
639};
640
Talin46e9b442012-02-05 20:54:10 +0000641// Unique map for aggregate constants
642template<class TypeClass, class ConstantClass>
643class ConstantAggrUniqueMap {
644public:
645 typedef ArrayRef<Constant*> Operands;
646 typedef std::pair<TypeClass*, Operands> LookupKey;
647private:
648 struct MapInfo {
649 typedef DenseMapInfo<ConstantClass*> ConstantClassInfo;
650 typedef DenseMapInfo<Constant*> ConstantInfo;
651 typedef DenseMapInfo<TypeClass*> TypeClassInfo;
652 static inline ConstantClass* getEmptyKey() {
653 return ConstantClassInfo::getEmptyKey();
654 }
655 static inline ConstantClass* getTombstoneKey() {
656 return ConstantClassInfo::getTombstoneKey();
657 }
658 static unsigned getHashValue(const ConstantClass *CP) {
659 // This is adapted from SuperFastHash by Paul Hsieh.
660 unsigned Hash = TypeClassInfo::getHashValue(CP->getType());
661 for (unsigned I = 0, E = CP->getNumOperands(); I < E; ++I) {
662 unsigned Data = ConstantInfo::getHashValue(CP->getOperand(I));
663 Hash += Data & 0xFFFF;
664 unsigned Tmp = ((Data >> 16) << 11) ^ Hash;
665 Hash = (Hash << 16) ^ Tmp;
666 Hash += Hash >> 11;
667 }
668
669 // Force "avalanching" of final 127 bits.
670 Hash ^= Hash << 3;
671 Hash += Hash >> 5;
672 Hash ^= Hash << 4;
673 Hash += Hash >> 17;
674 Hash ^= Hash << 25;
675 Hash += Hash >> 6;
676 return Hash;
677 }
678 static bool isEqual(const ConstantClass *LHS, const ConstantClass *RHS) {
679 return LHS == RHS;
680 }
681 static unsigned getHashValue(const LookupKey &Val) {
682 // This is adapted from SuperFastHash by Paul Hsieh.
683 unsigned Hash = TypeClassInfo::getHashValue(Val.first);
684 for (Operands::const_iterator
685 I = Val.second.begin(), E = Val.second.end(); I != E; ++I) {
686 unsigned Data = ConstantInfo::getHashValue(*I);
687 Hash += Data & 0xFFFF;
688 unsigned Tmp = ((Data >> 16) << 11) ^ Hash;
689 Hash = (Hash << 16) ^ Tmp;
690 Hash += Hash >> 11;
691 }
692
693 // Force "avalanching" of final 127 bits.
694 Hash ^= Hash << 3;
695 Hash += Hash >> 5;
696 Hash ^= Hash << 4;
697 Hash += Hash >> 17;
698 Hash ^= Hash << 25;
699 Hash += Hash >> 6;
700 return Hash;
701 }
702 static bool isEqual(const LookupKey &LHS, const ConstantClass *RHS) {
703 if (RHS == getEmptyKey() || RHS == getTombstoneKey())
704 return false;
705 if (LHS.first != RHS->getType()
706 || LHS.second.size() != RHS->getNumOperands())
707 return false;
708 for (unsigned I = 0, E = RHS->getNumOperands(); I < E; ++I) {
709 if (LHS.second[I] != RHS->getOperand(I))
710 return false;
711 }
712 return true;
713 }
714 };
715public:
716 typedef DenseMap<ConstantClass *, char, MapInfo> MapTy;
717
718private:
719 /// Map - This is the main map from the element descriptor to the Constants.
720 /// This is the primary way we avoid creating two of the same shape
721 /// constant.
722 MapTy Map;
723
724public:
725 typename MapTy::iterator map_begin() { return Map.begin(); }
726 typename MapTy::iterator map_end() { return Map.end(); }
727
728 void freeConstants() {
729 for (typename MapTy::iterator I=Map.begin(), E=Map.end();
730 I != E; ++I) {
731 // Asserts that use_empty().
732 delete I->first;
733 }
734 }
735
736private:
737 typename MapTy::iterator findExistingElement(ConstantClass *CP) {
738 return Map.find(CP);
739 }
740
741 ConstantClass *Create(TypeClass *Ty, Operands V, typename MapTy::iterator I) {
742 ConstantClass* Result =
743 ConstantArrayCreator<ConstantClass,TypeClass>::create(Ty, V);
744
745 assert(Result->getType() == Ty && "Type specified is not correct!");
746 Map[Result] = '\0';
747
748 return Result;
749 }
750public:
751
752 /// getOrCreate - Return the specified constant from the map, creating it if
753 /// necessary.
754 ConstantClass *getOrCreate(TypeClass *Ty, Operands V) {
755 LookupKey Lookup(Ty, V);
756 ConstantClass* Result = 0;
757
758 typename MapTy::iterator I = Map.find_as(Lookup);
759 // Is it in the map?
760 if (I != Map.end())
761 Result = I->first;
762
763 if (!Result) {
764 // If no preexisting value, create one now...
765 Result = Create(Ty, V, I);
766 }
767
768 return Result;
769 }
770
771 /// Find the constant by lookup key.
772 typename MapTy::iterator find(LookupKey Lookup) {
773 return Map.find_as(Lookup);
774 }
775
776 /// Insert the constant into its proper slot.
777 void insert(ConstantClass *CP) {
778 Map[CP] = '\0';
779 }
780
781 /// Remove this constant from the map
782 void remove(ConstantClass *CP) {
783 typename MapTy::iterator I = findExistingElement(CP);
784 assert(I != Map.end() && "Constant not found in constant table!");
785 assert(I->first == CP && "Didn't find correct element?");
786 Map.erase(I);
787 }
788
789 void dump() const {
790 DEBUG(dbgs() << "Constant.cpp: ConstantUniqueMap\n");
791 }
792};
793
Owen Anderson9b676982009-08-04 22:55:26 +0000794}
795
796#endif