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