ConstantsContext.h 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694
  1. //===-- ConstantsContext.h - Constants-related Context Interals -*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file defines various helper methods and classes used by
  10. // LLVMContextImpl for creating and managing constants.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_LIB_IR_CONSTANTSCONTEXT_H
  14. #define LLVM_LIB_IR_CONSTANTSCONTEXT_H
  15. #include "llvm/ADT/ArrayRef.h"
  16. #include "llvm/ADT/DenseMapInfo.h"
  17. #include "llvm/ADT/DenseSet.h"
  18. #include "llvm/ADT/Hashing.h"
  19. #include "llvm/ADT/SmallVector.h"
  20. #include "llvm/ADT/StringRef.h"
  21. #include "llvm/IR/Constant.h"
  22. #include "llvm/IR/Constants.h"
  23. #include "llvm/IR/DerivedTypes.h"
  24. #include "llvm/IR/InlineAsm.h"
  25. #include "llvm/IR/Instruction.h"
  26. #include "llvm/IR/Instructions.h"
  27. #include "llvm/IR/OperandTraits.h"
  28. #include "llvm/Support/Casting.h"
  29. #include "llvm/Support/Debug.h"
  30. #include "llvm/Support/ErrorHandling.h"
  31. #include "llvm/Support/raw_ostream.h"
  32. #include <cassert>
  33. #include <cstddef>
  34. #include <cstdint>
  35. #include <utility>
  36. #define DEBUG_TYPE "ir"
  37. namespace llvm {
  38. /// CastConstantExpr - This class is private to Constants.cpp, and is used
  39. /// behind the scenes to implement cast constant exprs.
  40. class CastConstantExpr final : public ConstantExpr {
  41. public:
  42. CastConstantExpr(unsigned Opcode, Constant *C, Type *Ty)
  43. : ConstantExpr(Ty, Opcode, &Op<0>(), 1) {
  44. Op<0>() = C;
  45. }
  46. // allocate space for exactly one operand
  47. void *operator new(size_t S) { return User::operator new(S, 1); }
  48. void operator delete(void *Ptr) { User::operator delete(Ptr); }
  49. DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
  50. static bool classof(const ConstantExpr *CE) {
  51. return Instruction::isCast(CE->getOpcode());
  52. }
  53. static bool classof(const Value *V) {
  54. return isa<ConstantExpr>(V) && classof(cast<ConstantExpr>(V));
  55. }
  56. };
  57. /// BinaryConstantExpr - This class is private to Constants.cpp, and is used
  58. /// behind the scenes to implement binary constant exprs.
  59. class BinaryConstantExpr final : public ConstantExpr {
  60. public:
  61. BinaryConstantExpr(unsigned Opcode, Constant *C1, Constant *C2,
  62. unsigned Flags)
  63. : ConstantExpr(C1->getType(), Opcode, &Op<0>(), 2) {
  64. Op<0>() = C1;
  65. Op<1>() = C2;
  66. SubclassOptionalData = Flags;
  67. }
  68. // allocate space for exactly two operands
  69. void *operator new(size_t S) { return User::operator new(S, 2); }
  70. void operator delete(void *Ptr) { User::operator delete(Ptr); }
  71. /// Transparently provide more efficient getOperand methods.
  72. DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
  73. static bool classof(const ConstantExpr *CE) {
  74. return Instruction::isBinaryOp(CE->getOpcode());
  75. }
  76. static bool classof(const Value *V) {
  77. return isa<ConstantExpr>(V) && classof(cast<ConstantExpr>(V));
  78. }
  79. };
  80. /// SelectConstantExpr - This class is private to Constants.cpp, and is used
  81. /// behind the scenes to implement select constant exprs.
  82. class SelectConstantExpr final : public ConstantExpr {
  83. public:
  84. SelectConstantExpr(Constant *C1, Constant *C2, Constant *C3)
  85. : ConstantExpr(C2->getType(), Instruction::Select, &Op<0>(), 3) {
  86. Op<0>() = C1;
  87. Op<1>() = C2;
  88. Op<2>() = C3;
  89. }
  90. // allocate space for exactly three operands
  91. void *operator new(size_t S) { return User::operator new(S, 3); }
  92. void operator delete(void *Ptr) { User::operator delete(Ptr); }
  93. /// Transparently provide more efficient getOperand methods.
  94. DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
  95. static bool classof(const ConstantExpr *CE) {
  96. return CE->getOpcode() == Instruction::Select;
  97. }
  98. static bool classof(const Value *V) {
  99. return isa<ConstantExpr>(V) && classof(cast<ConstantExpr>(V));
  100. }
  101. };
  102. /// ExtractElementConstantExpr - This class is private to
  103. /// Constants.cpp, and is used behind the scenes to implement
  104. /// extractelement constant exprs.
  105. class ExtractElementConstantExpr final : public ConstantExpr {
  106. public:
  107. ExtractElementConstantExpr(Constant *C1, Constant *C2)
  108. : ConstantExpr(cast<VectorType>(C1->getType())->getElementType(),
  109. Instruction::ExtractElement, &Op<0>(), 2) {
  110. Op<0>() = C1;
  111. Op<1>() = C2;
  112. }
  113. // allocate space for exactly two operands
  114. void *operator new(size_t S) { return User::operator new(S, 2); }
  115. void operator delete(void *Ptr) { User::operator delete(Ptr); }
  116. /// Transparently provide more efficient getOperand methods.
  117. DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
  118. static bool classof(const ConstantExpr *CE) {
  119. return CE->getOpcode() == Instruction::ExtractElement;
  120. }
  121. static bool classof(const Value *V) {
  122. return isa<ConstantExpr>(V) && classof(cast<ConstantExpr>(V));
  123. }
  124. };
  125. /// InsertElementConstantExpr - This class is private to
  126. /// Constants.cpp, and is used behind the scenes to implement
  127. /// insertelement constant exprs.
  128. class InsertElementConstantExpr final : public ConstantExpr {
  129. public:
  130. InsertElementConstantExpr(Constant *C1, Constant *C2, Constant *C3)
  131. : ConstantExpr(C1->getType(), Instruction::InsertElement,
  132. &Op<0>(), 3) {
  133. Op<0>() = C1;
  134. Op<1>() = C2;
  135. Op<2>() = C3;
  136. }
  137. // allocate space for exactly three operands
  138. void *operator new(size_t S) { return User::operator new(S, 3); }
  139. void operator delete(void *Ptr) { User::operator delete(Ptr); }
  140. /// Transparently provide more efficient getOperand methods.
  141. DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
  142. static bool classof(const ConstantExpr *CE) {
  143. return CE->getOpcode() == Instruction::InsertElement;
  144. }
  145. static bool classof(const Value *V) {
  146. return isa<ConstantExpr>(V) && classof(cast<ConstantExpr>(V));
  147. }
  148. };
  149. /// ShuffleVectorConstantExpr - This class is private to
  150. /// Constants.cpp, and is used behind the scenes to implement
  151. /// shufflevector constant exprs.
  152. class ShuffleVectorConstantExpr final : public ConstantExpr {
  153. public:
  154. ShuffleVectorConstantExpr(Constant *C1, Constant *C2, ArrayRef<int> Mask)
  155. : ConstantExpr(VectorType::get(
  156. cast<VectorType>(C1->getType())->getElementType(),
  157. Mask.size(), isa<ScalableVectorType>(C1->getType())),
  158. Instruction::ShuffleVector, &Op<0>(), 2) {
  159. assert(ShuffleVectorInst::isValidOperands(C1, C2, Mask) &&
  160. "Invalid shuffle vector instruction operands!");
  161. Op<0>() = C1;
  162. Op<1>() = C2;
  163. ShuffleMask.assign(Mask.begin(), Mask.end());
  164. ShuffleMaskForBitcode =
  165. ShuffleVectorInst::convertShuffleMaskForBitcode(Mask, getType());
  166. }
  167. SmallVector<int, 4> ShuffleMask;
  168. Constant *ShuffleMaskForBitcode;
  169. void *operator new(size_t S) { return User::operator new(S, 2); }
  170. void operator delete(void *Ptr) { return User::operator delete(Ptr); }
  171. /// Transparently provide more efficient getOperand methods.
  172. DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
  173. static bool classof(const ConstantExpr *CE) {
  174. return CE->getOpcode() == Instruction::ShuffleVector;
  175. }
  176. static bool classof(const Value *V) {
  177. return isa<ConstantExpr>(V) && classof(cast<ConstantExpr>(V));
  178. }
  179. };
  180. /// GetElementPtrConstantExpr - This class is private to Constants.cpp, and is
  181. /// used behind the scenes to implement getelementpr constant exprs.
  182. class GetElementPtrConstantExpr final : public ConstantExpr {
  183. Type *SrcElementTy;
  184. Type *ResElementTy;
  185. GetElementPtrConstantExpr(Type *SrcElementTy, Constant *C,
  186. ArrayRef<Constant *> IdxList, Type *DestTy);
  187. public:
  188. static GetElementPtrConstantExpr *Create(Type *SrcElementTy, Constant *C,
  189. ArrayRef<Constant *> IdxList,
  190. Type *DestTy, unsigned Flags) {
  191. GetElementPtrConstantExpr *Result = new (IdxList.size() + 1)
  192. GetElementPtrConstantExpr(SrcElementTy, C, IdxList, DestTy);
  193. Result->SubclassOptionalData = Flags;
  194. return Result;
  195. }
  196. Type *getSourceElementType() const;
  197. Type *getResultElementType() const;
  198. /// Transparently provide more efficient getOperand methods.
  199. DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
  200. static bool classof(const ConstantExpr *CE) {
  201. return CE->getOpcode() == Instruction::GetElementPtr;
  202. }
  203. static bool classof(const Value *V) {
  204. return isa<ConstantExpr>(V) && classof(cast<ConstantExpr>(V));
  205. }
  206. };
  207. // CompareConstantExpr - This class is private to Constants.cpp, and is used
  208. // behind the scenes to implement ICmp and FCmp constant expressions. This is
  209. // needed in order to store the predicate value for these instructions.
  210. class CompareConstantExpr final : public ConstantExpr {
  211. public:
  212. unsigned short predicate;
  213. CompareConstantExpr(Type *ty, Instruction::OtherOps opc,
  214. unsigned short pred, Constant* LHS, Constant* RHS)
  215. : ConstantExpr(ty, opc, &Op<0>(), 2), predicate(pred) {
  216. Op<0>() = LHS;
  217. Op<1>() = RHS;
  218. }
  219. // allocate space for exactly two operands
  220. void *operator new(size_t S) { return User::operator new(S, 2); }
  221. void operator delete(void *Ptr) { return User::operator delete(Ptr); }
  222. /// Transparently provide more efficient getOperand methods.
  223. DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
  224. static bool classof(const ConstantExpr *CE) {
  225. return CE->getOpcode() == Instruction::ICmp ||
  226. CE->getOpcode() == Instruction::FCmp;
  227. }
  228. static bool classof(const Value *V) {
  229. return isa<ConstantExpr>(V) && classof(cast<ConstantExpr>(V));
  230. }
  231. };
  232. template <>
  233. struct OperandTraits<CastConstantExpr>
  234. : public FixedNumOperandTraits<CastConstantExpr, 1> {};
  235. DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CastConstantExpr, Value)
  236. template <>
  237. struct OperandTraits<BinaryConstantExpr>
  238. : public FixedNumOperandTraits<BinaryConstantExpr, 2> {};
  239. DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BinaryConstantExpr, Value)
  240. template <>
  241. struct OperandTraits<SelectConstantExpr>
  242. : public FixedNumOperandTraits<SelectConstantExpr, 3> {};
  243. DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SelectConstantExpr, Value)
  244. template <>
  245. struct OperandTraits<ExtractElementConstantExpr>
  246. : public FixedNumOperandTraits<ExtractElementConstantExpr, 2> {};
  247. DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractElementConstantExpr, Value)
  248. template <>
  249. struct OperandTraits<InsertElementConstantExpr>
  250. : public FixedNumOperandTraits<InsertElementConstantExpr, 3> {};
  251. DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementConstantExpr, Value)
  252. template <>
  253. struct OperandTraits<ShuffleVectorConstantExpr>
  254. : public FixedNumOperandTraits<ShuffleVectorConstantExpr, 2> {};
  255. DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ShuffleVectorConstantExpr, Value)
  256. template <>
  257. struct OperandTraits<GetElementPtrConstantExpr>
  258. : public VariadicOperandTraits<GetElementPtrConstantExpr, 1> {};
  259. DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GetElementPtrConstantExpr, Value)
  260. template <>
  261. struct OperandTraits<CompareConstantExpr>
  262. : public FixedNumOperandTraits<CompareConstantExpr, 2> {};
  263. DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CompareConstantExpr, Value)
  264. template <class ConstantClass> struct ConstantAggrKeyType;
  265. struct InlineAsmKeyType;
  266. struct ConstantExprKeyType;
  267. template <class ConstantClass> struct ConstantInfo;
  268. template <> struct ConstantInfo<ConstantExpr> {
  269. using ValType = ConstantExprKeyType;
  270. using TypeClass = Type;
  271. };
  272. template <> struct ConstantInfo<InlineAsm> {
  273. using ValType = InlineAsmKeyType;
  274. using TypeClass = PointerType;
  275. };
  276. template <> struct ConstantInfo<ConstantArray> {
  277. using ValType = ConstantAggrKeyType<ConstantArray>;
  278. using TypeClass = ArrayType;
  279. };
  280. template <> struct ConstantInfo<ConstantStruct> {
  281. using ValType = ConstantAggrKeyType<ConstantStruct>;
  282. using TypeClass = StructType;
  283. };
  284. template <> struct ConstantInfo<ConstantVector> {
  285. using ValType = ConstantAggrKeyType<ConstantVector>;
  286. using TypeClass = VectorType;
  287. };
  288. template <class ConstantClass> struct ConstantAggrKeyType {
  289. ArrayRef<Constant *> Operands;
  290. ConstantAggrKeyType(ArrayRef<Constant *> Operands) : Operands(Operands) {}
  291. ConstantAggrKeyType(ArrayRef<Constant *> Operands, const ConstantClass *)
  292. : Operands(Operands) {}
  293. ConstantAggrKeyType(const ConstantClass *C,
  294. SmallVectorImpl<Constant *> &Storage) {
  295. assert(Storage.empty() && "Expected empty storage");
  296. for (unsigned I = 0, E = C->getNumOperands(); I != E; ++I)
  297. Storage.push_back(C->getOperand(I));
  298. Operands = Storage;
  299. }
  300. bool operator==(const ConstantAggrKeyType &X) const {
  301. return Operands == X.Operands;
  302. }
  303. bool operator==(const ConstantClass *C) const {
  304. if (Operands.size() != C->getNumOperands())
  305. return false;
  306. for (unsigned I = 0, E = Operands.size(); I != E; ++I)
  307. if (Operands[I] != C->getOperand(I))
  308. return false;
  309. return true;
  310. }
  311. unsigned getHash() const {
  312. return hash_combine_range(Operands.begin(), Operands.end());
  313. }
  314. using TypeClass = typename ConstantInfo<ConstantClass>::TypeClass;
  315. ConstantClass *create(TypeClass *Ty) const {
  316. return new (Operands.size()) ConstantClass(Ty, Operands);
  317. }
  318. };
  319. struct InlineAsmKeyType {
  320. StringRef AsmString;
  321. StringRef Constraints;
  322. FunctionType *FTy;
  323. bool HasSideEffects;
  324. bool IsAlignStack;
  325. InlineAsm::AsmDialect AsmDialect;
  326. bool CanThrow;
  327. InlineAsmKeyType(StringRef AsmString, StringRef Constraints,
  328. FunctionType *FTy, bool HasSideEffects, bool IsAlignStack,
  329. InlineAsm::AsmDialect AsmDialect, bool canThrow)
  330. : AsmString(AsmString), Constraints(Constraints), FTy(FTy),
  331. HasSideEffects(HasSideEffects), IsAlignStack(IsAlignStack),
  332. AsmDialect(AsmDialect), CanThrow(canThrow) {}
  333. InlineAsmKeyType(const InlineAsm *Asm, SmallVectorImpl<Constant *> &)
  334. : AsmString(Asm->getAsmString()), Constraints(Asm->getConstraintString()),
  335. FTy(Asm->getFunctionType()), HasSideEffects(Asm->hasSideEffects()),
  336. IsAlignStack(Asm->isAlignStack()), AsmDialect(Asm->getDialect()),
  337. CanThrow(Asm->canThrow()) {}
  338. bool operator==(const InlineAsmKeyType &X) const {
  339. return HasSideEffects == X.HasSideEffects &&
  340. IsAlignStack == X.IsAlignStack && AsmDialect == X.AsmDialect &&
  341. AsmString == X.AsmString && Constraints == X.Constraints &&
  342. FTy == X.FTy && CanThrow == X.CanThrow;
  343. }
  344. bool operator==(const InlineAsm *Asm) const {
  345. return HasSideEffects == Asm->hasSideEffects() &&
  346. IsAlignStack == Asm->isAlignStack() &&
  347. AsmDialect == Asm->getDialect() &&
  348. AsmString == Asm->getAsmString() &&
  349. Constraints == Asm->getConstraintString() &&
  350. FTy == Asm->getFunctionType() && CanThrow == Asm->canThrow();
  351. }
  352. unsigned getHash() const {
  353. return hash_combine(AsmString, Constraints, HasSideEffects, IsAlignStack,
  354. AsmDialect, FTy, CanThrow);
  355. }
  356. using TypeClass = ConstantInfo<InlineAsm>::TypeClass;
  357. InlineAsm *create(TypeClass *Ty) const {
  358. assert(PointerType::getUnqual(FTy) == Ty);
  359. return new InlineAsm(FTy, std::string(AsmString), std::string(Constraints),
  360. HasSideEffects, IsAlignStack, AsmDialect, CanThrow);
  361. }
  362. };
  363. struct ConstantExprKeyType {
  364. private:
  365. uint8_t Opcode;
  366. uint8_t SubclassOptionalData;
  367. uint16_t SubclassData;
  368. ArrayRef<Constant *> Ops;
  369. ArrayRef<int> ShuffleMask;
  370. Type *ExplicitTy;
  371. static ArrayRef<int> getShuffleMaskIfValid(const ConstantExpr *CE) {
  372. if (CE->getOpcode() == Instruction::ShuffleVector)
  373. return CE->getShuffleMask();
  374. return std::nullopt;
  375. }
  376. static Type *getSourceElementTypeIfValid(const ConstantExpr *CE) {
  377. if (auto *GEPCE = dyn_cast<GetElementPtrConstantExpr>(CE))
  378. return GEPCE->getSourceElementType();
  379. return nullptr;
  380. }
  381. public:
  382. ConstantExprKeyType(unsigned Opcode, ArrayRef<Constant *> Ops,
  383. unsigned short SubclassData = 0,
  384. unsigned short SubclassOptionalData = 0,
  385. ArrayRef<int> ShuffleMask = std::nullopt,
  386. Type *ExplicitTy = nullptr)
  387. : Opcode(Opcode), SubclassOptionalData(SubclassOptionalData),
  388. SubclassData(SubclassData), Ops(Ops), ShuffleMask(ShuffleMask),
  389. ExplicitTy(ExplicitTy) {}
  390. ConstantExprKeyType(ArrayRef<Constant *> Operands, const ConstantExpr *CE)
  391. : Opcode(CE->getOpcode()),
  392. SubclassOptionalData(CE->getRawSubclassOptionalData()),
  393. SubclassData(CE->isCompare() ? CE->getPredicate() : 0), Ops(Operands),
  394. ShuffleMask(getShuffleMaskIfValid(CE)),
  395. ExplicitTy(getSourceElementTypeIfValid(CE)) {}
  396. ConstantExprKeyType(const ConstantExpr *CE,
  397. SmallVectorImpl<Constant *> &Storage)
  398. : Opcode(CE->getOpcode()),
  399. SubclassOptionalData(CE->getRawSubclassOptionalData()),
  400. SubclassData(CE->isCompare() ? CE->getPredicate() : 0),
  401. ShuffleMask(getShuffleMaskIfValid(CE)),
  402. ExplicitTy(getSourceElementTypeIfValid(CE)) {
  403. assert(Storage.empty() && "Expected empty storage");
  404. for (unsigned I = 0, E = CE->getNumOperands(); I != E; ++I)
  405. Storage.push_back(CE->getOperand(I));
  406. Ops = Storage;
  407. }
  408. bool operator==(const ConstantExprKeyType &X) const {
  409. return Opcode == X.Opcode && SubclassData == X.SubclassData &&
  410. SubclassOptionalData == X.SubclassOptionalData && Ops == X.Ops &&
  411. ShuffleMask == X.ShuffleMask && ExplicitTy == X.ExplicitTy;
  412. }
  413. bool operator==(const ConstantExpr *CE) const {
  414. if (Opcode != CE->getOpcode())
  415. return false;
  416. if (SubclassOptionalData != CE->getRawSubclassOptionalData())
  417. return false;
  418. if (Ops.size() != CE->getNumOperands())
  419. return false;
  420. if (SubclassData != (CE->isCompare() ? CE->getPredicate() : 0))
  421. return false;
  422. for (unsigned I = 0, E = Ops.size(); I != E; ++I)
  423. if (Ops[I] != CE->getOperand(I))
  424. return false;
  425. if (ShuffleMask != getShuffleMaskIfValid(CE))
  426. return false;
  427. if (ExplicitTy != getSourceElementTypeIfValid(CE))
  428. return false;
  429. return true;
  430. }
  431. unsigned getHash() const {
  432. return hash_combine(
  433. Opcode, SubclassOptionalData, SubclassData,
  434. hash_combine_range(Ops.begin(), Ops.end()),
  435. hash_combine_range(ShuffleMask.begin(), ShuffleMask.end()), ExplicitTy);
  436. }
  437. using TypeClass = ConstantInfo<ConstantExpr>::TypeClass;
  438. ConstantExpr *create(TypeClass *Ty) const {
  439. switch (Opcode) {
  440. default:
  441. if (Instruction::isCast(Opcode))
  442. return new CastConstantExpr(Opcode, Ops[0], Ty);
  443. if ((Opcode >= Instruction::BinaryOpsBegin &&
  444. Opcode < Instruction::BinaryOpsEnd))
  445. return new BinaryConstantExpr(Opcode, Ops[0], Ops[1],
  446. SubclassOptionalData);
  447. llvm_unreachable("Invalid ConstantExpr!");
  448. case Instruction::Select:
  449. return new SelectConstantExpr(Ops[0], Ops[1], Ops[2]);
  450. case Instruction::ExtractElement:
  451. return new ExtractElementConstantExpr(Ops[0], Ops[1]);
  452. case Instruction::InsertElement:
  453. return new InsertElementConstantExpr(Ops[0], Ops[1], Ops[2]);
  454. case Instruction::ShuffleVector:
  455. return new ShuffleVectorConstantExpr(Ops[0], Ops[1], ShuffleMask);
  456. case Instruction::GetElementPtr:
  457. return GetElementPtrConstantExpr::Create(ExplicitTy, Ops[0], Ops.slice(1),
  458. Ty, SubclassOptionalData);
  459. case Instruction::ICmp:
  460. return new CompareConstantExpr(Ty, Instruction::ICmp, SubclassData,
  461. Ops[0], Ops[1]);
  462. case Instruction::FCmp:
  463. return new CompareConstantExpr(Ty, Instruction::FCmp, SubclassData,
  464. Ops[0], Ops[1]);
  465. }
  466. }
  467. };
  468. // Free memory for a given constant. Assumes the constant has already been
  469. // removed from all relevant maps.
  470. void deleteConstant(Constant *C);
  471. template <class ConstantClass> class ConstantUniqueMap {
  472. public:
  473. using ValType = typename ConstantInfo<ConstantClass>::ValType;
  474. using TypeClass = typename ConstantInfo<ConstantClass>::TypeClass;
  475. using LookupKey = std::pair<TypeClass *, ValType>;
  476. /// Key and hash together, so that we compute the hash only once and reuse it.
  477. using LookupKeyHashed = std::pair<unsigned, LookupKey>;
  478. private:
  479. struct MapInfo {
  480. using ConstantClassInfo = DenseMapInfo<ConstantClass *>;
  481. static inline ConstantClass *getEmptyKey() {
  482. return ConstantClassInfo::getEmptyKey();
  483. }
  484. static inline ConstantClass *getTombstoneKey() {
  485. return ConstantClassInfo::getTombstoneKey();
  486. }
  487. static unsigned getHashValue(const ConstantClass *CP) {
  488. SmallVector<Constant *, 32> Storage;
  489. return getHashValue(LookupKey(CP->getType(), ValType(CP, Storage)));
  490. }
  491. static bool isEqual(const ConstantClass *LHS, const ConstantClass *RHS) {
  492. return LHS == RHS;
  493. }
  494. static unsigned getHashValue(const LookupKey &Val) {
  495. return hash_combine(Val.first, Val.second.getHash());
  496. }
  497. static unsigned getHashValue(const LookupKeyHashed &Val) {
  498. return Val.first;
  499. }
  500. static bool isEqual(const LookupKey &LHS, const ConstantClass *RHS) {
  501. if (RHS == getEmptyKey() || RHS == getTombstoneKey())
  502. return false;
  503. if (LHS.first != RHS->getType())
  504. return false;
  505. return LHS.second == RHS;
  506. }
  507. static bool isEqual(const LookupKeyHashed &LHS, const ConstantClass *RHS) {
  508. return isEqual(LHS.second, RHS);
  509. }
  510. };
  511. public:
  512. using MapTy = DenseSet<ConstantClass *, MapInfo>;
  513. private:
  514. MapTy Map;
  515. public:
  516. typename MapTy::iterator begin() { return Map.begin(); }
  517. typename MapTy::iterator end() { return Map.end(); }
  518. void freeConstants() {
  519. for (auto &I : Map)
  520. deleteConstant(I);
  521. }
  522. private:
  523. ConstantClass *create(TypeClass *Ty, ValType V, LookupKeyHashed &HashKey) {
  524. ConstantClass *Result = V.create(Ty);
  525. assert(Result->getType() == Ty && "Type specified is not correct!");
  526. Map.insert_as(Result, HashKey);
  527. return Result;
  528. }
  529. public:
  530. /// Return the specified constant from the map, creating it if necessary.
  531. ConstantClass *getOrCreate(TypeClass *Ty, ValType V) {
  532. LookupKey Key(Ty, V);
  533. /// Hash once, and reuse it for the lookup and the insertion if needed.
  534. LookupKeyHashed Lookup(MapInfo::getHashValue(Key), Key);
  535. ConstantClass *Result = nullptr;
  536. auto I = Map.find_as(Lookup);
  537. if (I == Map.end())
  538. Result = create(Ty, V, Lookup);
  539. else
  540. Result = *I;
  541. assert(Result && "Unexpected nullptr");
  542. return Result;
  543. }
  544. /// Remove this constant from the map
  545. void remove(ConstantClass *CP) {
  546. typename MapTy::iterator I = Map.find(CP);
  547. assert(I != Map.end() && "Constant not found in constant table!");
  548. assert(*I == CP && "Didn't find correct element?");
  549. Map.erase(I);
  550. }
  551. ConstantClass *replaceOperandsInPlace(ArrayRef<Constant *> Operands,
  552. ConstantClass *CP, Value *From,
  553. Constant *To, unsigned NumUpdated = 0,
  554. unsigned OperandNo = ~0u) {
  555. LookupKey Key(CP->getType(), ValType(Operands, CP));
  556. /// Hash once, and reuse it for the lookup and the insertion if needed.
  557. LookupKeyHashed Lookup(MapInfo::getHashValue(Key), Key);
  558. auto ItMap = Map.find_as(Lookup);
  559. if (ItMap != Map.end())
  560. return *ItMap;
  561. // Update to the new value. Optimize for the case when we have a single
  562. // operand that we're changing, but handle bulk updates efficiently.
  563. remove(CP);
  564. if (NumUpdated == 1) {
  565. assert(OperandNo < CP->getNumOperands() && "Invalid index");
  566. assert(CP->getOperand(OperandNo) != To && "I didn't contain From!");
  567. CP->setOperand(OperandNo, To);
  568. } else {
  569. for (unsigned I = 0, E = CP->getNumOperands(); I != E; ++I)
  570. if (CP->getOperand(I) == From)
  571. CP->setOperand(I, To);
  572. }
  573. Map.insert_as(CP, Lookup);
  574. return nullptr;
  575. }
  576. void dump() const {
  577. LLVM_DEBUG(dbgs() << "Constant.cpp: ConstantUniqueMap\n");
  578. }
  579. };
  580. template <> inline void ConstantUniqueMap<InlineAsm>::freeConstants() {
  581. for (auto &I : Map)
  582. delete I;
  583. }
  584. } // end namespace llvm
  585. #endif // LLVM_LIB_IR_CONSTANTSCONTEXT_H