1 //===-- ConstantsContext.h - Constants-related Context Interals -----------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines various helper methods and classes used by
11 // LLVMContextImpl for creating and managing constants.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_LIB_IR_CONSTANTSCONTEXT_H
16 #define LLVM_LIB_IR_CONSTANTSCONTEXT_H
18 #include "llvm/ADT/DenseMap.h"
19 #include "llvm/ADT/Hashing.h"
20 #include "llvm/IR/InlineAsm.h"
21 #include "llvm/IR/Instructions.h"
22 #include "llvm/IR/Operator.h"
23 #include "llvm/Support/Debug.h"
24 #include "llvm/Support/ErrorHandling.h"
25 #include "llvm/Support/raw_ostream.h"
29 #define DEBUG_TYPE "ir"
33 /// UnaryConstantExpr - This class is private to Constants.cpp, and is used
34 /// behind the scenes to implement unary constant exprs.
35 class UnaryConstantExpr : public ConstantExpr {
36 void anchor() override;
37 void *operator new(size_t, unsigned) = delete;
39 // allocate space for exactly one operand
40 void *operator new(size_t s) {
41 return User::operator new(s, 1);
43 UnaryConstantExpr(unsigned Opcode, Constant *C, Type *Ty)
44 : ConstantExpr(Ty, Opcode, &Op<0>(), 1) {
47 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
50 /// BinaryConstantExpr - This class is private to Constants.cpp, and is used
51 /// behind the scenes to implement binary constant exprs.
52 class BinaryConstantExpr : public ConstantExpr {
53 void anchor() override;
54 void *operator new(size_t, unsigned) = delete;
56 // allocate space for exactly two operands
57 void *operator new(size_t s) {
58 return User::operator new(s, 2);
60 BinaryConstantExpr(unsigned Opcode, Constant *C1, Constant *C2,
62 : ConstantExpr(C1->getType(), Opcode, &Op<0>(), 2) {
65 SubclassOptionalData = Flags;
67 /// Transparently provide more efficient getOperand methods.
68 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
71 /// SelectConstantExpr - This class is private to Constants.cpp, and is used
72 /// behind the scenes to implement select constant exprs.
73 class SelectConstantExpr : public ConstantExpr {
74 void anchor() override;
75 void *operator new(size_t, unsigned) = delete;
77 // allocate space for exactly three operands
78 void *operator new(size_t s) {
79 return User::operator new(s, 3);
81 SelectConstantExpr(Constant *C1, Constant *C2, Constant *C3)
82 : ConstantExpr(C2->getType(), Instruction::Select, &Op<0>(), 3) {
87 /// Transparently provide more efficient getOperand methods.
88 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
91 /// ExtractElementConstantExpr - This class is private to
92 /// Constants.cpp, and is used behind the scenes to implement
93 /// extractelement constant exprs.
94 class ExtractElementConstantExpr : public ConstantExpr {
95 void anchor() override;
96 void *operator new(size_t, unsigned) = delete;
98 // allocate space for exactly two operands
99 void *operator new(size_t s) {
100 return User::operator new(s, 2);
102 ExtractElementConstantExpr(Constant *C1, Constant *C2)
103 : ConstantExpr(cast<VectorType>(C1->getType())->getElementType(),
104 Instruction::ExtractElement, &Op<0>(), 2) {
108 /// Transparently provide more efficient getOperand methods.
109 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
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 anchor() override;
117 void *operator new(size_t, unsigned) = delete;
119 // allocate space for exactly three operands
120 void *operator new(size_t s) {
121 return User::operator new(s, 3);
123 InsertElementConstantExpr(Constant *C1, Constant *C2, Constant *C3)
124 : ConstantExpr(C1->getType(), Instruction::InsertElement,
130 /// Transparently provide more efficient getOperand methods.
131 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
134 /// ShuffleVectorConstantExpr - This class is private to
135 /// Constants.cpp, and is used behind the scenes to implement
136 /// shufflevector constant exprs.
137 class ShuffleVectorConstantExpr : public ConstantExpr {
138 void anchor() override;
139 void *operator new(size_t, unsigned) = delete;
141 // allocate space for exactly three operands
142 void *operator new(size_t s) {
143 return User::operator new(s, 3);
145 ShuffleVectorConstantExpr(Constant *C1, Constant *C2, Constant *C3)
146 : ConstantExpr(VectorType::get(
147 cast<VectorType>(C1->getType())->getElementType(),
148 cast<VectorType>(C3->getType())->getNumElements()),
149 Instruction::ShuffleVector,
155 /// Transparently provide more efficient getOperand methods.
156 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
159 /// ExtractValueConstantExpr - This class is private to
160 /// Constants.cpp, and is used behind the scenes to implement
161 /// extractvalue constant exprs.
162 class ExtractValueConstantExpr : public ConstantExpr {
163 void anchor() override;
164 void *operator new(size_t, unsigned) = delete;
166 // allocate space for exactly one operand
167 void *operator new(size_t s) {
168 return User::operator new(s, 1);
170 ExtractValueConstantExpr(Constant *Agg, ArrayRef<unsigned> IdxList,
172 : ConstantExpr(DestTy, Instruction::ExtractValue, &Op<0>(), 1),
173 Indices(IdxList.begin(), IdxList.end()) {
177 /// Indices - These identify which value to extract.
178 const SmallVector<unsigned, 4> Indices;
180 /// Transparently provide more efficient getOperand methods.
181 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
184 /// InsertValueConstantExpr - This class is private to
185 /// Constants.cpp, and is used behind the scenes to implement
186 /// insertvalue constant exprs.
187 class InsertValueConstantExpr : public ConstantExpr {
188 void anchor() override;
189 void *operator new(size_t, unsigned) = delete;
191 // allocate space for exactly one operand
192 void *operator new(size_t s) {
193 return User::operator new(s, 2);
195 InsertValueConstantExpr(Constant *Agg, Constant *Val,
196 ArrayRef<unsigned> IdxList, Type *DestTy)
197 : ConstantExpr(DestTy, Instruction::InsertValue, &Op<0>(), 2),
198 Indices(IdxList.begin(), IdxList.end()) {
203 /// Indices - These identify the position for the insertion.
204 const SmallVector<unsigned, 4> Indices;
206 /// Transparently provide more efficient getOperand methods.
207 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
210 /// GetElementPtrConstantExpr - This class is private to Constants.cpp, and is
211 /// used behind the scenes to implement getelementpr constant exprs.
212 class GetElementPtrConstantExpr : public ConstantExpr {
214 void anchor() override;
215 GetElementPtrConstantExpr(Type *SrcElementTy, Constant *C,
216 ArrayRef<Constant *> IdxList, Type *DestTy);
219 static GetElementPtrConstantExpr *Create(Constant *C,
220 ArrayRef<Constant*> IdxList,
224 cast<PointerType>(C->getType()->getScalarType())->getElementType(), C,
225 IdxList, DestTy, Flags);
227 static GetElementPtrConstantExpr *Create(Type *SrcElementTy, Constant *C,
228 ArrayRef<Constant *> IdxList,
229 Type *DestTy, unsigned Flags) {
230 GetElementPtrConstantExpr *Result = new (IdxList.size() + 1)
231 GetElementPtrConstantExpr(SrcElementTy, C, IdxList, DestTy);
232 Result->SubclassOptionalData = Flags;
235 Type *getSourceElementType() const;
236 /// Transparently provide more efficient getOperand methods.
237 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
240 // CompareConstantExpr - This class is private to Constants.cpp, and is used
241 // behind the scenes to implement ICmp and FCmp constant expressions. This is
242 // needed in order to store the predicate value for these instructions.
243 class CompareConstantExpr : public ConstantExpr {
244 void anchor() override;
245 void *operator new(size_t, unsigned) = delete;
247 // allocate space for exactly two operands
248 void *operator new(size_t s) {
249 return User::operator new(s, 2);
251 unsigned short predicate;
252 CompareConstantExpr(Type *ty, Instruction::OtherOps opc,
253 unsigned short pred, Constant* LHS, Constant* RHS)
254 : ConstantExpr(ty, opc, &Op<0>(), 2), predicate(pred) {
258 /// Transparently provide more efficient getOperand methods.
259 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
263 struct OperandTraits<UnaryConstantExpr>
264 : public FixedNumOperandTraits<UnaryConstantExpr, 1> {};
265 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(UnaryConstantExpr, Value)
268 struct OperandTraits<BinaryConstantExpr>
269 : public FixedNumOperandTraits<BinaryConstantExpr, 2> {};
270 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BinaryConstantExpr, Value)
273 struct OperandTraits<SelectConstantExpr>
274 : public FixedNumOperandTraits<SelectConstantExpr, 3> {};
275 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SelectConstantExpr, Value)
278 struct OperandTraits<ExtractElementConstantExpr>
279 : public FixedNumOperandTraits<ExtractElementConstantExpr, 2> {};
280 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractElementConstantExpr, Value)
283 struct OperandTraits<InsertElementConstantExpr>
284 : public FixedNumOperandTraits<InsertElementConstantExpr, 3> {};
285 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementConstantExpr, Value)
288 struct OperandTraits<ShuffleVectorConstantExpr>
289 : public FixedNumOperandTraits<ShuffleVectorConstantExpr, 3> {};
290 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ShuffleVectorConstantExpr, Value)
293 struct OperandTraits<ExtractValueConstantExpr>
294 : public FixedNumOperandTraits<ExtractValueConstantExpr, 1> {};
295 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractValueConstantExpr, Value)
298 struct OperandTraits<InsertValueConstantExpr>
299 : public FixedNumOperandTraits<InsertValueConstantExpr, 2> {};
300 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertValueConstantExpr, Value)
303 struct OperandTraits<GetElementPtrConstantExpr>
304 : public VariadicOperandTraits<GetElementPtrConstantExpr, 1> {};
306 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GetElementPtrConstantExpr, Value)
309 struct OperandTraits<CompareConstantExpr>
310 : public FixedNumOperandTraits<CompareConstantExpr, 2> {};
311 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CompareConstantExpr, Value)
313 template <class ConstantClass> struct ConstantAggrKeyType;
314 struct InlineAsmKeyType;
315 struct ConstantExprKeyType;
317 template <class ConstantClass> struct ConstantInfo;
318 template <> struct ConstantInfo<ConstantExpr> {
319 typedef ConstantExprKeyType ValType;
320 typedef Type TypeClass;
322 template <> struct ConstantInfo<InlineAsm> {
323 typedef InlineAsmKeyType ValType;
324 typedef PointerType TypeClass;
326 template <> struct ConstantInfo<ConstantArray> {
327 typedef ConstantAggrKeyType<ConstantArray> ValType;
328 typedef ArrayType TypeClass;
330 template <> struct ConstantInfo<ConstantStruct> {
331 typedef ConstantAggrKeyType<ConstantStruct> ValType;
332 typedef StructType TypeClass;
334 template <> struct ConstantInfo<ConstantVector> {
335 typedef ConstantAggrKeyType<ConstantVector> ValType;
336 typedef VectorType TypeClass;
339 template <class ConstantClass> struct ConstantAggrKeyType {
340 ArrayRef<Constant *> Operands;
341 ConstantAggrKeyType(ArrayRef<Constant *> Operands) : Operands(Operands) {}
342 ConstantAggrKeyType(ArrayRef<Constant *> Operands, const ConstantClass *)
343 : Operands(Operands) {}
344 ConstantAggrKeyType(const ConstantClass *C,
345 SmallVectorImpl<Constant *> &Storage) {
346 assert(Storage.empty() && "Expected empty storage");
347 for (unsigned I = 0, E = C->getNumOperands(); I != E; ++I)
348 Storage.push_back(C->getOperand(I));
352 bool operator==(const ConstantAggrKeyType &X) const {
353 return Operands == X.Operands;
355 bool operator==(const ConstantClass *C) const {
356 if (Operands.size() != C->getNumOperands())
358 for (unsigned I = 0, E = Operands.size(); I != E; ++I)
359 if (Operands[I] != C->getOperand(I))
363 unsigned getHash() const {
364 return hash_combine_range(Operands.begin(), Operands.end());
367 typedef typename ConstantInfo<ConstantClass>::TypeClass TypeClass;
368 ConstantClass *create(TypeClass *Ty) const {
369 return new (Operands.size()) ConstantClass(Ty, Operands);
373 struct InlineAsmKeyType {
375 StringRef Constraints;
379 InlineAsm::AsmDialect AsmDialect;
381 InlineAsmKeyType(StringRef AsmString, StringRef Constraints,
382 FunctionType *FTy, bool HasSideEffects, bool IsAlignStack,
383 InlineAsm::AsmDialect AsmDialect)
384 : AsmString(AsmString), Constraints(Constraints), FTy(FTy),
385 HasSideEffects(HasSideEffects), IsAlignStack(IsAlignStack),
386 AsmDialect(AsmDialect) {}
387 InlineAsmKeyType(const InlineAsm *Asm, SmallVectorImpl<Constant *> &)
388 : AsmString(Asm->getAsmString()), Constraints(Asm->getConstraintString()),
389 FTy(Asm->getFunctionType()), HasSideEffects(Asm->hasSideEffects()),
390 IsAlignStack(Asm->isAlignStack()), AsmDialect(Asm->getDialect()) {}
392 bool operator==(const InlineAsmKeyType &X) const {
393 return HasSideEffects == X.HasSideEffects &&
394 IsAlignStack == X.IsAlignStack && AsmDialect == X.AsmDialect &&
395 AsmString == X.AsmString && Constraints == X.Constraints &&
398 bool operator==(const InlineAsm *Asm) const {
399 return HasSideEffects == Asm->hasSideEffects() &&
400 IsAlignStack == Asm->isAlignStack() &&
401 AsmDialect == Asm->getDialect() &&
402 AsmString == Asm->getAsmString() &&
403 Constraints == Asm->getConstraintString() &&
404 FTy == Asm->getFunctionType();
406 unsigned getHash() const {
407 return hash_combine(AsmString, Constraints, HasSideEffects, IsAlignStack,
411 typedef ConstantInfo<InlineAsm>::TypeClass TypeClass;
412 InlineAsm *create(TypeClass *Ty) const {
413 assert(PointerType::getUnqual(FTy) == Ty);
414 return new InlineAsm(FTy, AsmString, Constraints, HasSideEffects,
415 IsAlignStack, AsmDialect);
419 struct ConstantExprKeyType {
421 uint8_t SubclassOptionalData;
422 uint16_t SubclassData;
423 ArrayRef<Constant *> Ops;
424 ArrayRef<unsigned> Indexes;
427 ConstantExprKeyType(unsigned Opcode, ArrayRef<Constant *> Ops,
428 unsigned short SubclassData = 0,
429 unsigned short SubclassOptionalData = 0,
430 ArrayRef<unsigned> Indexes = None,
431 Type *ExplicitTy = nullptr)
432 : Opcode(Opcode), SubclassOptionalData(SubclassOptionalData),
433 SubclassData(SubclassData), Ops(Ops), Indexes(Indexes),
434 ExplicitTy(ExplicitTy) {}
435 ConstantExprKeyType(ArrayRef<Constant *> Operands, const ConstantExpr *CE)
436 : Opcode(CE->getOpcode()),
437 SubclassOptionalData(CE->getRawSubclassOptionalData()),
438 SubclassData(CE->isCompare() ? CE->getPredicate() : 0), Ops(Operands),
439 Indexes(CE->hasIndices() ? CE->getIndices() : ArrayRef<unsigned>()) {}
440 ConstantExprKeyType(const ConstantExpr *CE,
441 SmallVectorImpl<Constant *> &Storage)
442 : Opcode(CE->getOpcode()),
443 SubclassOptionalData(CE->getRawSubclassOptionalData()),
444 SubclassData(CE->isCompare() ? CE->getPredicate() : 0),
445 Indexes(CE->hasIndices() ? CE->getIndices() : ArrayRef<unsigned>()) {
446 assert(Storage.empty() && "Expected empty storage");
447 for (unsigned I = 0, E = CE->getNumOperands(); I != E; ++I)
448 Storage.push_back(CE->getOperand(I));
452 bool operator==(const ConstantExprKeyType &X) const {
453 return Opcode == X.Opcode && SubclassData == X.SubclassData &&
454 SubclassOptionalData == X.SubclassOptionalData && Ops == X.Ops &&
455 Indexes == X.Indexes;
458 bool operator==(const ConstantExpr *CE) const {
459 if (Opcode != CE->getOpcode())
461 if (SubclassOptionalData != CE->getRawSubclassOptionalData())
463 if (Ops.size() != CE->getNumOperands())
465 if (SubclassData != (CE->isCompare() ? CE->getPredicate() : 0))
467 for (unsigned I = 0, E = Ops.size(); I != E; ++I)
468 if (Ops[I] != CE->getOperand(I))
470 if (Indexes != (CE->hasIndices() ? CE->getIndices() : ArrayRef<unsigned>()))
475 unsigned getHash() const {
476 return hash_combine(Opcode, SubclassOptionalData, SubclassData,
477 hash_combine_range(Ops.begin(), Ops.end()),
478 hash_combine_range(Indexes.begin(), Indexes.end()));
481 typedef ConstantInfo<ConstantExpr>::TypeClass TypeClass;
482 ConstantExpr *create(TypeClass *Ty) const {
485 if (Instruction::isCast(Opcode))
486 return new UnaryConstantExpr(Opcode, Ops[0], Ty);
487 if ((Opcode >= Instruction::BinaryOpsBegin &&
488 Opcode < Instruction::BinaryOpsEnd))
489 return new BinaryConstantExpr(Opcode, Ops[0], Ops[1],
490 SubclassOptionalData);
491 llvm_unreachable("Invalid ConstantExpr!");
492 case Instruction::Select:
493 return new SelectConstantExpr(Ops[0], Ops[1], Ops[2]);
494 case Instruction::ExtractElement:
495 return new ExtractElementConstantExpr(Ops[0], Ops[1]);
496 case Instruction::InsertElement:
497 return new InsertElementConstantExpr(Ops[0], Ops[1], Ops[2]);
498 case Instruction::ShuffleVector:
499 return new ShuffleVectorConstantExpr(Ops[0], Ops[1], Ops[2]);
500 case Instruction::InsertValue:
501 return new InsertValueConstantExpr(Ops[0], Ops[1], Indexes, Ty);
502 case Instruction::ExtractValue:
503 return new ExtractValueConstantExpr(Ops[0], Indexes, Ty);
504 case Instruction::GetElementPtr:
505 return GetElementPtrConstantExpr::Create(
506 ExplicitTy ? ExplicitTy
507 : cast<PointerType>(Ops[0]->getType()->getScalarType())
509 Ops[0], Ops.slice(1), Ty, SubclassOptionalData);
510 case Instruction::ICmp:
511 return new CompareConstantExpr(Ty, Instruction::ICmp, SubclassData,
513 case Instruction::FCmp:
514 return new CompareConstantExpr(Ty, Instruction::FCmp, SubclassData,
520 template <class ConstantClass> class ConstantUniqueMap {
522 typedef typename ConstantInfo<ConstantClass>::ValType ValType;
523 typedef typename ConstantInfo<ConstantClass>::TypeClass TypeClass;
524 typedef std::pair<TypeClass *, ValType> LookupKey;
528 typedef DenseMapInfo<ConstantClass *> ConstantClassInfo;
529 static inline ConstantClass *getEmptyKey() {
530 return ConstantClassInfo::getEmptyKey();
532 static inline ConstantClass *getTombstoneKey() {
533 return ConstantClassInfo::getTombstoneKey();
535 static unsigned getHashValue(const ConstantClass *CP) {
536 SmallVector<Constant *, 8> Storage;
537 return getHashValue(LookupKey(CP->getType(), ValType(CP, Storage)));
539 static bool isEqual(const ConstantClass *LHS, const ConstantClass *RHS) {
542 static unsigned getHashValue(const LookupKey &Val) {
543 return hash_combine(Val.first, Val.second.getHash());
545 static bool isEqual(const LookupKey &LHS, const ConstantClass *RHS) {
546 if (RHS == getEmptyKey() || RHS == getTombstoneKey())
548 if (LHS.first != RHS->getType())
550 return LHS.second == RHS;
555 typedef DenseMap<ConstantClass *, char, MapInfo> MapTy;
561 typename MapTy::iterator map_begin() { return Map.begin(); }
562 typename MapTy::iterator map_end() { return Map.end(); }
564 void freeConstants() {
566 // Asserts that use_empty().
571 ConstantClass *create(TypeClass *Ty, ValType V) {
572 ConstantClass *Result = V.create(Ty);
574 assert(Result->getType() == Ty && "Type specified is not correct!");
581 /// Return the specified constant from the map, creating it if necessary.
582 ConstantClass *getOrCreate(TypeClass *Ty, ValType V) {
583 LookupKey Lookup(Ty, V);
584 ConstantClass *Result = nullptr;
586 auto I = find(Lookup);
588 Result = create(Ty, V);
591 assert(Result && "Unexpected nullptr");
596 /// Find the constant by lookup key.
597 typename MapTy::iterator find(LookupKey Lookup) {
598 return Map.find_as(Lookup);
601 /// Insert the constant into its proper slot.
602 void insert(ConstantClass *CP) { Map[CP] = '\0'; }
604 /// Remove this constant from the map
605 void remove(ConstantClass *CP) {
606 typename MapTy::iterator I = Map.find(CP);
607 assert(I != Map.end() && "Constant not found in constant table!");
608 assert(I->first == CP && "Didn't find correct element?");
612 ConstantClass *replaceOperandsInPlace(ArrayRef<Constant *> Operands,
613 ConstantClass *CP, Value *From,
614 Constant *To, unsigned NumUpdated = 0,
615 unsigned OperandNo = ~0u) {
616 LookupKey Lookup(CP->getType(), ValType(Operands, CP));
617 auto I = find(Lookup);
621 // Update to the new value. Optimize for the case when we have a single
622 // operand that we're changing, but handle bulk updates efficiently.
624 if (NumUpdated == 1) {
625 assert(OperandNo < CP->getNumOperands() && "Invalid index");
626 assert(CP->getOperand(OperandNo) != To && "I didn't contain From!");
627 CP->setOperand(OperandNo, To);
629 for (unsigned I = 0, E = CP->getNumOperands(); I != E; ++I)
630 if (CP->getOperand(I) == From)
631 CP->setOperand(I, To);
637 void dump() const { DEBUG(dbgs() << "Constant.cpp: ConstantUniqueMap\n"); }
640 } // end namespace llvm