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