ConstantsContext.h 27 KB

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