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