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 | |
| 15 | #ifndef LLVM_CONSTANTSCONTEXT_H |
| 16 | #define LLVM_CONSTANTSCONTEXT_H |
| 17 | |
Jeffrey Yasskin | ade270e | 2010-03-21 20:37:19 +0000 | [diff] [blame] | 18 | #include "llvm/InlineAsm.h" |
Owen Anderson | 9b67698 | 2009-08-04 22:55:26 +0000 | [diff] [blame] | 19 | #include "llvm/Instructions.h" |
| 20 | #include "llvm/Operator.h" |
David Greene | 338a903 | 2010-01-05 01:34:26 +0000 | [diff] [blame] | 21 | #include "llvm/Support/Debug.h" |
Owen Anderson | 9b67698 | 2009-08-04 22:55:26 +0000 | [diff] [blame] | 22 | #include "llvm/Support/ErrorHandling.h" |
Chris Lattner | 34822f6 | 2009-08-23 04:44:11 +0000 | [diff] [blame] | 23 | #include "llvm/Support/raw_ostream.h" |
Owen Anderson | 9b67698 | 2009-08-04 22:55:26 +0000 | [diff] [blame] | 24 | #include <map> |
| 25 | |
| 26 | namespace llvm { |
| 27 | template<class ValType> |
| 28 | struct ConstantTraits; |
| 29 | |
| 30 | /// UnaryConstantExpr - This class is private to Constants.cpp, and is used |
| 31 | /// behind the scenes to implement unary constant exprs. |
| 32 | class UnaryConstantExpr : public ConstantExpr { |
| 33 | void *operator new(size_t, unsigned); // DO NOT IMPLEMENT |
| 34 | public: |
| 35 | // allocate space for exactly one operand |
| 36 | void *operator new(size_t s) { |
| 37 | return User::operator new(s, 1); |
| 38 | } |
| 39 | UnaryConstantExpr(unsigned Opcode, Constant *C, const Type *Ty) |
| 40 | : ConstantExpr(Ty, Opcode, &Op<0>(), 1) { |
| 41 | Op<0>() = C; |
| 42 | } |
| 43 | DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value); |
| 44 | }; |
| 45 | |
| 46 | /// BinaryConstantExpr - This class is private to Constants.cpp, and is used |
| 47 | /// behind the scenes to implement binary constant exprs. |
| 48 | class BinaryConstantExpr : public ConstantExpr { |
| 49 | void *operator new(size_t, unsigned); // DO NOT IMPLEMENT |
| 50 | public: |
| 51 | // allocate space for exactly two operands |
| 52 | void *operator new(size_t s) { |
| 53 | return User::operator new(s, 2); |
| 54 | } |
Dan Gohman | 1b84908 | 2009-09-07 23:54:19 +0000 | [diff] [blame] | 55 | BinaryConstantExpr(unsigned Opcode, Constant *C1, Constant *C2, |
| 56 | unsigned Flags) |
Owen Anderson | 9b67698 | 2009-08-04 22:55:26 +0000 | [diff] [blame] | 57 | : ConstantExpr(C1->getType(), Opcode, &Op<0>(), 2) { |
| 58 | Op<0>() = C1; |
| 59 | Op<1>() = C2; |
Dan Gohman | 1b84908 | 2009-09-07 23:54:19 +0000 | [diff] [blame] | 60 | SubclassOptionalData = Flags; |
Owen Anderson | 9b67698 | 2009-08-04 22:55:26 +0000 | [diff] [blame] | 61 | } |
| 62 | /// Transparently provide more efficient getOperand methods. |
| 63 | DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value); |
| 64 | }; |
| 65 | |
| 66 | /// SelectConstantExpr - This class is private to Constants.cpp, and is used |
| 67 | /// behind the scenes to implement select constant exprs. |
| 68 | class SelectConstantExpr : public ConstantExpr { |
| 69 | void *operator new(size_t, unsigned); // DO NOT IMPLEMENT |
| 70 | public: |
| 71 | // allocate space for exactly three operands |
| 72 | void *operator new(size_t s) { |
| 73 | return User::operator new(s, 3); |
| 74 | } |
| 75 | SelectConstantExpr(Constant *C1, Constant *C2, Constant *C3) |
| 76 | : ConstantExpr(C2->getType(), Instruction::Select, &Op<0>(), 3) { |
| 77 | Op<0>() = C1; |
| 78 | Op<1>() = C2; |
| 79 | Op<2>() = C3; |
| 80 | } |
| 81 | /// Transparently provide more efficient getOperand methods. |
| 82 | DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value); |
| 83 | }; |
| 84 | |
| 85 | /// ExtractElementConstantExpr - This class is private to |
| 86 | /// Constants.cpp, and is used behind the scenes to implement |
| 87 | /// extractelement constant exprs. |
| 88 | class ExtractElementConstantExpr : public ConstantExpr { |
| 89 | void *operator new(size_t, unsigned); // DO NOT IMPLEMENT |
| 90 | public: |
| 91 | // allocate space for exactly two operands |
| 92 | void *operator new(size_t s) { |
| 93 | return User::operator new(s, 2); |
| 94 | } |
| 95 | ExtractElementConstantExpr(Constant *C1, Constant *C2) |
| 96 | : ConstantExpr(cast<VectorType>(C1->getType())->getElementType(), |
| 97 | Instruction::ExtractElement, &Op<0>(), 2) { |
| 98 | Op<0>() = C1; |
| 99 | Op<1>() = C2; |
| 100 | } |
| 101 | /// Transparently provide more efficient getOperand methods. |
| 102 | DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value); |
| 103 | }; |
| 104 | |
| 105 | /// InsertElementConstantExpr - This class is private to |
| 106 | /// Constants.cpp, and is used behind the scenes to implement |
| 107 | /// insertelement constant exprs. |
| 108 | class InsertElementConstantExpr : public ConstantExpr { |
| 109 | void *operator new(size_t, unsigned); // DO NOT IMPLEMENT |
| 110 | public: |
| 111 | // allocate space for exactly three operands |
| 112 | void *operator new(size_t s) { |
| 113 | return User::operator new(s, 3); |
| 114 | } |
| 115 | InsertElementConstantExpr(Constant *C1, Constant *C2, Constant *C3) |
| 116 | : ConstantExpr(C1->getType(), Instruction::InsertElement, |
| 117 | &Op<0>(), 3) { |
| 118 | Op<0>() = C1; |
| 119 | Op<1>() = C2; |
| 120 | Op<2>() = C3; |
| 121 | } |
| 122 | /// Transparently provide more efficient getOperand methods. |
| 123 | DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value); |
| 124 | }; |
| 125 | |
| 126 | /// ShuffleVectorConstantExpr - This class is private to |
| 127 | /// Constants.cpp, and is used behind the scenes to implement |
| 128 | /// shufflevector constant exprs. |
| 129 | class ShuffleVectorConstantExpr : public ConstantExpr { |
| 130 | void *operator new(size_t, unsigned); // DO NOT IMPLEMENT |
| 131 | public: |
| 132 | // allocate space for exactly three operands |
| 133 | void *operator new(size_t s) { |
| 134 | return User::operator new(s, 3); |
| 135 | } |
| 136 | ShuffleVectorConstantExpr(Constant *C1, Constant *C2, Constant *C3) |
| 137 | : ConstantExpr(VectorType::get( |
| 138 | cast<VectorType>(C1->getType())->getElementType(), |
| 139 | cast<VectorType>(C3->getType())->getNumElements()), |
| 140 | Instruction::ShuffleVector, |
| 141 | &Op<0>(), 3) { |
| 142 | Op<0>() = C1; |
| 143 | Op<1>() = C2; |
| 144 | Op<2>() = C3; |
| 145 | } |
| 146 | /// Transparently provide more efficient getOperand methods. |
| 147 | DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value); |
| 148 | }; |
| 149 | |
| 150 | /// ExtractValueConstantExpr - This class is private to |
| 151 | /// Constants.cpp, and is used behind the scenes to implement |
| 152 | /// extractvalue constant exprs. |
| 153 | class ExtractValueConstantExpr : public ConstantExpr { |
| 154 | void *operator new(size_t, unsigned); // DO NOT IMPLEMENT |
| 155 | public: |
| 156 | // allocate space for exactly one operand |
| 157 | void *operator new(size_t s) { |
| 158 | return User::operator new(s, 1); |
| 159 | } |
| 160 | ExtractValueConstantExpr(Constant *Agg, |
| 161 | const SmallVector<unsigned, 4> &IdxList, |
| 162 | const Type *DestTy) |
| 163 | : ConstantExpr(DestTy, Instruction::ExtractValue, &Op<0>(), 1), |
| 164 | Indices(IdxList) { |
| 165 | Op<0>() = Agg; |
| 166 | } |
| 167 | |
| 168 | /// Indices - These identify which value to extract. |
| 169 | const SmallVector<unsigned, 4> Indices; |
| 170 | |
| 171 | /// Transparently provide more efficient getOperand methods. |
| 172 | DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value); |
| 173 | }; |
| 174 | |
| 175 | /// InsertValueConstantExpr - This class is private to |
| 176 | /// Constants.cpp, and is used behind the scenes to implement |
| 177 | /// insertvalue constant exprs. |
| 178 | class InsertValueConstantExpr : public ConstantExpr { |
| 179 | void *operator new(size_t, unsigned); // DO NOT IMPLEMENT |
| 180 | public: |
| 181 | // allocate space for exactly one operand |
| 182 | void *operator new(size_t s) { |
| 183 | return User::operator new(s, 2); |
| 184 | } |
| 185 | InsertValueConstantExpr(Constant *Agg, Constant *Val, |
| 186 | const SmallVector<unsigned, 4> &IdxList, |
| 187 | const Type *DestTy) |
| 188 | : ConstantExpr(DestTy, Instruction::InsertValue, &Op<0>(), 2), |
| 189 | Indices(IdxList) { |
| 190 | Op<0>() = Agg; |
| 191 | Op<1>() = Val; |
| 192 | } |
| 193 | |
| 194 | /// Indices - These identify the position for the insertion. |
| 195 | const SmallVector<unsigned, 4> Indices; |
| 196 | |
| 197 | /// Transparently provide more efficient getOperand methods. |
| 198 | DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value); |
| 199 | }; |
| 200 | |
| 201 | |
| 202 | /// GetElementPtrConstantExpr - This class is private to Constants.cpp, and is |
| 203 | /// used behind the scenes to implement getelementpr constant exprs. |
| 204 | class GetElementPtrConstantExpr : public ConstantExpr { |
| 205 | GetElementPtrConstantExpr(Constant *C, const std::vector<Constant*> &IdxList, |
| 206 | const Type *DestTy); |
| 207 | public: |
| 208 | static GetElementPtrConstantExpr *Create(Constant *C, |
| 209 | const std::vector<Constant*>&IdxList, |
Dan Gohman | 1b84908 | 2009-09-07 23:54:19 +0000 | [diff] [blame] | 210 | const Type *DestTy, |
| 211 | unsigned Flags) { |
| 212 | GetElementPtrConstantExpr *Result = |
Owen Anderson | 9b67698 | 2009-08-04 22:55:26 +0000 | [diff] [blame] | 213 | new(IdxList.size() + 1) GetElementPtrConstantExpr(C, IdxList, DestTy); |
Dan Gohman | 1b84908 | 2009-09-07 23:54:19 +0000 | [diff] [blame] | 214 | Result->SubclassOptionalData = Flags; |
| 215 | return Result; |
Owen Anderson | 9b67698 | 2009-08-04 22:55:26 +0000 | [diff] [blame] | 216 | } |
| 217 | /// Transparently provide more efficient getOperand methods. |
| 218 | DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value); |
| 219 | }; |
| 220 | |
| 221 | // CompareConstantExpr - This class is private to Constants.cpp, and is used |
| 222 | // behind the scenes to implement ICmp and FCmp constant expressions. This is |
| 223 | // needed in order to store the predicate value for these instructions. |
| 224 | struct CompareConstantExpr : public ConstantExpr { |
| 225 | void *operator new(size_t, unsigned); // DO NOT IMPLEMENT |
| 226 | // allocate space for exactly two operands |
| 227 | void *operator new(size_t s) { |
| 228 | return User::operator new(s, 2); |
| 229 | } |
| 230 | unsigned short predicate; |
| 231 | CompareConstantExpr(const Type *ty, Instruction::OtherOps opc, |
| 232 | unsigned short pred, Constant* LHS, Constant* RHS) |
| 233 | : ConstantExpr(ty, opc, &Op<0>(), 2), predicate(pred) { |
| 234 | Op<0>() = LHS; |
| 235 | Op<1>() = RHS; |
| 236 | } |
| 237 | /// Transparently provide more efficient getOperand methods. |
| 238 | DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value); |
| 239 | }; |
| 240 | |
| 241 | template <> |
Jay Foad | c8adf5f | 2011-01-11 15:07:38 +0000 | [diff] [blame] | 242 | struct OperandTraits<UnaryConstantExpr> : |
| 243 | public FixedNumOperandTraits<UnaryConstantExpr, 1> { |
Owen Anderson | 9b67698 | 2009-08-04 22:55:26 +0000 | [diff] [blame] | 244 | }; |
| 245 | DEFINE_TRANSPARENT_OPERAND_ACCESSORS(UnaryConstantExpr, Value) |
| 246 | |
| 247 | template <> |
Jay Foad | c8adf5f | 2011-01-11 15:07:38 +0000 | [diff] [blame] | 248 | struct OperandTraits<BinaryConstantExpr> : |
| 249 | public FixedNumOperandTraits<BinaryConstantExpr, 2> { |
Owen Anderson | 9b67698 | 2009-08-04 22:55:26 +0000 | [diff] [blame] | 250 | }; |
| 251 | DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BinaryConstantExpr, Value) |
| 252 | |
| 253 | template <> |
Jay Foad | c8adf5f | 2011-01-11 15:07:38 +0000 | [diff] [blame] | 254 | struct OperandTraits<SelectConstantExpr> : |
| 255 | public FixedNumOperandTraits<SelectConstantExpr, 3> { |
Owen Anderson | 9b67698 | 2009-08-04 22:55:26 +0000 | [diff] [blame] | 256 | }; |
| 257 | DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SelectConstantExpr, Value) |
| 258 | |
| 259 | template <> |
Jay Foad | c8adf5f | 2011-01-11 15:07:38 +0000 | [diff] [blame] | 260 | struct OperandTraits<ExtractElementConstantExpr> : |
| 261 | public FixedNumOperandTraits<ExtractElementConstantExpr, 2> { |
Owen Anderson | 9b67698 | 2009-08-04 22:55:26 +0000 | [diff] [blame] | 262 | }; |
| 263 | DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractElementConstantExpr, Value) |
| 264 | |
| 265 | template <> |
Jay Foad | c8adf5f | 2011-01-11 15:07:38 +0000 | [diff] [blame] | 266 | struct OperandTraits<InsertElementConstantExpr> : |
| 267 | public FixedNumOperandTraits<InsertElementConstantExpr, 3> { |
Owen Anderson | 9b67698 | 2009-08-04 22:55:26 +0000 | [diff] [blame] | 268 | }; |
| 269 | DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementConstantExpr, Value) |
| 270 | |
| 271 | template <> |
Jay Foad | c8adf5f | 2011-01-11 15:07:38 +0000 | [diff] [blame] | 272 | struct OperandTraits<ShuffleVectorConstantExpr> : |
| 273 | public FixedNumOperandTraits<ShuffleVectorConstantExpr, 3> { |
Owen Anderson | 9b67698 | 2009-08-04 22:55:26 +0000 | [diff] [blame] | 274 | }; |
| 275 | DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ShuffleVectorConstantExpr, Value) |
| 276 | |
| 277 | template <> |
Jay Foad | c8adf5f | 2011-01-11 15:07:38 +0000 | [diff] [blame] | 278 | struct OperandTraits<ExtractValueConstantExpr> : |
| 279 | public FixedNumOperandTraits<ExtractValueConstantExpr, 1> { |
Owen Anderson | 9b67698 | 2009-08-04 22:55:26 +0000 | [diff] [blame] | 280 | }; |
| 281 | DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractValueConstantExpr, Value) |
| 282 | |
| 283 | template <> |
Jay Foad | c8adf5f | 2011-01-11 15:07:38 +0000 | [diff] [blame] | 284 | struct OperandTraits<InsertValueConstantExpr> : |
| 285 | public FixedNumOperandTraits<InsertValueConstantExpr, 2> { |
Owen Anderson | 9b67698 | 2009-08-04 22:55:26 +0000 | [diff] [blame] | 286 | }; |
| 287 | DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertValueConstantExpr, Value) |
| 288 | |
| 289 | template <> |
Jay Foad | c8adf5f | 2011-01-11 15:07:38 +0000 | [diff] [blame] | 290 | struct OperandTraits<GetElementPtrConstantExpr> : |
| 291 | public VariadicOperandTraits<GetElementPtrConstantExpr, 1> { |
Owen Anderson | 9b67698 | 2009-08-04 22:55:26 +0000 | [diff] [blame] | 292 | }; |
| 293 | |
| 294 | DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GetElementPtrConstantExpr, Value) |
| 295 | |
| 296 | |
| 297 | template <> |
Jay Foad | c8adf5f | 2011-01-11 15:07:38 +0000 | [diff] [blame] | 298 | struct OperandTraits<CompareConstantExpr> : |
| 299 | public FixedNumOperandTraits<CompareConstantExpr, 2> { |
Owen Anderson | 9b67698 | 2009-08-04 22:55:26 +0000 | [diff] [blame] | 300 | }; |
| 301 | DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CompareConstantExpr, Value) |
| 302 | |
| 303 | struct ExprMapKeyType { |
Owen Anderson | 9b67698 | 2009-08-04 22:55:26 +0000 | [diff] [blame] | 304 | ExprMapKeyType(unsigned opc, |
Jay Foad | 0091fe8 | 2011-04-13 15:22:40 +0000 | [diff] [blame^] | 305 | ArrayRef<Constant*> ops, |
Dan Gohman | 1b84908 | 2009-09-07 23:54:19 +0000 | [diff] [blame] | 306 | unsigned short flags = 0, |
| 307 | unsigned short optionalflags = 0, |
Jay Foad | 0091fe8 | 2011-04-13 15:22:40 +0000 | [diff] [blame^] | 308 | ArrayRef<unsigned> inds = ArrayRef<unsigned>()) |
Dan Gohman | 1b84908 | 2009-09-07 23:54:19 +0000 | [diff] [blame] | 309 | : opcode(opc), subclassoptionaldata(optionalflags), subclassdata(flags), |
Jay Foad | 0091fe8 | 2011-04-13 15:22:40 +0000 | [diff] [blame^] | 310 | operands(ops.begin(), ops.end()), indices(inds.begin(), inds.end()) {} |
Dan Gohman | 1b84908 | 2009-09-07 23:54:19 +0000 | [diff] [blame] | 311 | uint8_t opcode; |
| 312 | uint8_t subclassoptionaldata; |
| 313 | uint16_t subclassdata; |
Owen Anderson | 9b67698 | 2009-08-04 22:55:26 +0000 | [diff] [blame] | 314 | std::vector<Constant*> operands; |
Jay Foad | 0091fe8 | 2011-04-13 15:22:40 +0000 | [diff] [blame^] | 315 | SmallVector<unsigned, 4> indices; |
Owen Anderson | 9b67698 | 2009-08-04 22:55:26 +0000 | [diff] [blame] | 316 | bool operator==(const ExprMapKeyType& that) const { |
| 317 | return this->opcode == that.opcode && |
Dan Gohman | 1b84908 | 2009-09-07 23:54:19 +0000 | [diff] [blame] | 318 | this->subclassdata == that.subclassdata && |
| 319 | this->subclassoptionaldata == that.subclassoptionaldata && |
Owen Anderson | 9b67698 | 2009-08-04 22:55:26 +0000 | [diff] [blame] | 320 | this->operands == that.operands && |
| 321 | this->indices == that.indices; |
| 322 | } |
| 323 | bool operator<(const ExprMapKeyType & that) const { |
Dan Gohman | 1b84908 | 2009-09-07 23:54:19 +0000 | [diff] [blame] | 324 | if (this->opcode != that.opcode) return this->opcode < that.opcode; |
| 325 | if (this->operands != that.operands) return this->operands < that.operands; |
| 326 | if (this->subclassdata != that.subclassdata) |
| 327 | return this->subclassdata < that.subclassdata; |
| 328 | if (this->subclassoptionaldata != that.subclassoptionaldata) |
| 329 | return this->subclassoptionaldata < that.subclassoptionaldata; |
| 330 | if (this->indices != that.indices) return this->indices < that.indices; |
| 331 | return false; |
Owen Anderson | 9b67698 | 2009-08-04 22:55:26 +0000 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | bool operator!=(const ExprMapKeyType& that) const { |
| 335 | return !(*this == that); |
| 336 | } |
| 337 | }; |
| 338 | |
Jeffrey Yasskin | ade270e | 2010-03-21 20:37:19 +0000 | [diff] [blame] | 339 | struct InlineAsmKeyType { |
| 340 | InlineAsmKeyType(StringRef AsmString, |
| 341 | StringRef Constraints, bool hasSideEffects, |
| 342 | bool isAlignStack) |
| 343 | : asm_string(AsmString), constraints(Constraints), |
| 344 | has_side_effects(hasSideEffects), is_align_stack(isAlignStack) {} |
| 345 | std::string asm_string; |
| 346 | std::string constraints; |
| 347 | bool has_side_effects; |
| 348 | bool is_align_stack; |
| 349 | bool operator==(const InlineAsmKeyType& that) const { |
| 350 | return this->asm_string == that.asm_string && |
| 351 | this->constraints == that.constraints && |
| 352 | this->has_side_effects == that.has_side_effects && |
| 353 | this->is_align_stack == that.is_align_stack; |
| 354 | } |
| 355 | bool operator<(const InlineAsmKeyType& that) const { |
| 356 | if (this->asm_string != that.asm_string) |
| 357 | return this->asm_string < that.asm_string; |
| 358 | if (this->constraints != that.constraints) |
| 359 | return this->constraints < that.constraints; |
| 360 | if (this->has_side_effects != that.has_side_effects) |
| 361 | return this->has_side_effects < that.has_side_effects; |
| 362 | if (this->is_align_stack != that.is_align_stack) |
| 363 | return this->is_align_stack < that.is_align_stack; |
| 364 | return false; |
| 365 | } |
| 366 | |
| 367 | bool operator!=(const InlineAsmKeyType& that) const { |
| 368 | return !(*this == that); |
| 369 | } |
| 370 | }; |
| 371 | |
Owen Anderson | 9b67698 | 2009-08-04 22:55:26 +0000 | [diff] [blame] | 372 | // The number of operands for each ConstantCreator::create method is |
| 373 | // determined by the ConstantTraits template. |
| 374 | // ConstantCreator - A class that is used to create constants by |
Jeffrey Yasskin | f6ee7be | 2009-10-27 23:45:55 +0000 | [diff] [blame] | 375 | // ConstantUniqueMap*. This class should be partially specialized if there is |
Owen Anderson | 9b67698 | 2009-08-04 22:55:26 +0000 | [diff] [blame] | 376 | // something strange that needs to be done to interface to the ctor for the |
| 377 | // constant. |
| 378 | // |
| 379 | template<typename T, typename Alloc> |
| 380 | struct ConstantTraits< std::vector<T, Alloc> > { |
| 381 | static unsigned uses(const std::vector<T, Alloc>& v) { |
| 382 | return v.size(); |
| 383 | } |
| 384 | }; |
| 385 | |
Chris Lattner | 392be58 | 2010-02-12 20:49:41 +0000 | [diff] [blame] | 386 | template<> |
| 387 | struct ConstantTraits<Constant *> { |
| 388 | static unsigned uses(Constant * const & v) { |
| 389 | return 1; |
| 390 | } |
| 391 | }; |
| 392 | |
Owen Anderson | 9b67698 | 2009-08-04 22:55:26 +0000 | [diff] [blame] | 393 | template<class ConstantClass, class TypeClass, class ValType> |
| 394 | struct ConstantCreator { |
| 395 | static ConstantClass *create(const TypeClass *Ty, const ValType &V) { |
| 396 | return new(ConstantTraits<ValType>::uses(V)) ConstantClass(Ty, V); |
| 397 | } |
| 398 | }; |
| 399 | |
Dan Gohman | e4532f3 | 2009-09-15 15:58:07 +0000 | [diff] [blame] | 400 | template<class ConstantClass> |
| 401 | struct ConstantKeyData { |
| 402 | typedef void ValType; |
| 403 | static ValType getValType(ConstantClass *C) { |
| 404 | llvm_unreachable("Unknown Constant type!"); |
Owen Anderson | 9b67698 | 2009-08-04 22:55:26 +0000 | [diff] [blame] | 405 | } |
| 406 | }; |
| 407 | |
| 408 | template<> |
| 409 | struct ConstantCreator<ConstantExpr, Type, ExprMapKeyType> { |
| 410 | static ConstantExpr *create(const Type *Ty, const ExprMapKeyType &V, |
| 411 | unsigned short pred = 0) { |
| 412 | if (Instruction::isCast(V.opcode)) |
| 413 | return new UnaryConstantExpr(V.opcode, V.operands[0], Ty); |
| 414 | if ((V.opcode >= Instruction::BinaryOpsBegin && |
| 415 | V.opcode < Instruction::BinaryOpsEnd)) |
Dan Gohman | 1b84908 | 2009-09-07 23:54:19 +0000 | [diff] [blame] | 416 | return new BinaryConstantExpr(V.opcode, V.operands[0], V.operands[1], |
| 417 | V.subclassoptionaldata); |
Owen Anderson | 9b67698 | 2009-08-04 22:55:26 +0000 | [diff] [blame] | 418 | if (V.opcode == Instruction::Select) |
| 419 | return new SelectConstantExpr(V.operands[0], V.operands[1], |
| 420 | V.operands[2]); |
| 421 | if (V.opcode == Instruction::ExtractElement) |
| 422 | return new ExtractElementConstantExpr(V.operands[0], V.operands[1]); |
| 423 | if (V.opcode == Instruction::InsertElement) |
| 424 | return new InsertElementConstantExpr(V.operands[0], V.operands[1], |
| 425 | V.operands[2]); |
| 426 | if (V.opcode == Instruction::ShuffleVector) |
| 427 | return new ShuffleVectorConstantExpr(V.operands[0], V.operands[1], |
| 428 | V.operands[2]); |
| 429 | if (V.opcode == Instruction::InsertValue) |
| 430 | return new InsertValueConstantExpr(V.operands[0], V.operands[1], |
| 431 | V.indices, Ty); |
| 432 | if (V.opcode == Instruction::ExtractValue) |
| 433 | return new ExtractValueConstantExpr(V.operands[0], V.indices, Ty); |
| 434 | if (V.opcode == Instruction::GetElementPtr) { |
| 435 | std::vector<Constant*> IdxList(V.operands.begin()+1, V.operands.end()); |
Dan Gohman | 1b84908 | 2009-09-07 23:54:19 +0000 | [diff] [blame] | 436 | return GetElementPtrConstantExpr::Create(V.operands[0], IdxList, Ty, |
| 437 | V.subclassoptionaldata); |
Owen Anderson | 9b67698 | 2009-08-04 22:55:26 +0000 | [diff] [blame] | 438 | } |
| 439 | |
| 440 | // The compare instructions are weird. We have to encode the predicate |
| 441 | // value and it is combined with the instruction opcode by multiplying |
| 442 | // the opcode by one hundred. We must decode this to get the predicate. |
| 443 | if (V.opcode == Instruction::ICmp) |
Dan Gohman | 1b84908 | 2009-09-07 23:54:19 +0000 | [diff] [blame] | 444 | return new CompareConstantExpr(Ty, Instruction::ICmp, V.subclassdata, |
Owen Anderson | 9b67698 | 2009-08-04 22:55:26 +0000 | [diff] [blame] | 445 | V.operands[0], V.operands[1]); |
| 446 | if (V.opcode == Instruction::FCmp) |
Dan Gohman | 1b84908 | 2009-09-07 23:54:19 +0000 | [diff] [blame] | 447 | return new CompareConstantExpr(Ty, Instruction::FCmp, V.subclassdata, |
Owen Anderson | 9b67698 | 2009-08-04 22:55:26 +0000 | [diff] [blame] | 448 | V.operands[0], V.operands[1]); |
| 449 | llvm_unreachable("Invalid ConstantExpr!"); |
| 450 | return 0; |
| 451 | } |
| 452 | }; |
| 453 | |
| 454 | template<> |
Dan Gohman | e4532f3 | 2009-09-15 15:58:07 +0000 | [diff] [blame] | 455 | struct ConstantKeyData<ConstantExpr> { |
| 456 | typedef ExprMapKeyType ValType; |
| 457 | static ValType getValType(ConstantExpr *CE) { |
| 458 | std::vector<Constant*> Operands; |
| 459 | Operands.reserve(CE->getNumOperands()); |
| 460 | for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i) |
| 461 | Operands.push_back(cast<Constant>(CE->getOperand(i))); |
| 462 | return ExprMapKeyType(CE->getOpcode(), Operands, |
| 463 | CE->isCompare() ? CE->getPredicate() : 0, |
| 464 | CE->getRawSubclassOptionalData(), |
| 465 | CE->hasIndices() ? |
Jay Foad | 0091fe8 | 2011-04-13 15:22:40 +0000 | [diff] [blame^] | 466 | CE->getIndices() : ArrayRef<unsigned>()); |
Owen Anderson | 9b67698 | 2009-08-04 22:55:26 +0000 | [diff] [blame] | 467 | } |
| 468 | }; |
| 469 | |
| 470 | // ConstantAggregateZero does not take extra "value" argument... |
| 471 | template<class ValType> |
| 472 | struct ConstantCreator<ConstantAggregateZero, Type, ValType> { |
| 473 | static ConstantAggregateZero *create(const Type *Ty, const ValType &V){ |
| 474 | return new ConstantAggregateZero(Ty); |
| 475 | } |
| 476 | }; |
| 477 | |
| 478 | template<> |
Dan Gohman | e4532f3 | 2009-09-15 15:58:07 +0000 | [diff] [blame] | 479 | struct ConstantKeyData<ConstantVector> { |
| 480 | typedef std::vector<Constant*> ValType; |
| 481 | static ValType getValType(ConstantVector *CP) { |
| 482 | std::vector<Constant*> Elements; |
| 483 | Elements.reserve(CP->getNumOperands()); |
| 484 | for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i) |
| 485 | Elements.push_back(CP->getOperand(i)); |
| 486 | return Elements; |
Owen Anderson | 9b67698 | 2009-08-04 22:55:26 +0000 | [diff] [blame] | 487 | } |
| 488 | }; |
| 489 | |
| 490 | template<> |
Dan Gohman | e4532f3 | 2009-09-15 15:58:07 +0000 | [diff] [blame] | 491 | struct ConstantKeyData<ConstantAggregateZero> { |
| 492 | typedef char ValType; |
| 493 | static ValType getValType(ConstantAggregateZero *C) { |
| 494 | return 0; |
Owen Anderson | 9b67698 | 2009-08-04 22:55:26 +0000 | [diff] [blame] | 495 | } |
| 496 | }; |
| 497 | |
| 498 | template<> |
Dan Gohman | e4532f3 | 2009-09-15 15:58:07 +0000 | [diff] [blame] | 499 | struct ConstantKeyData<ConstantArray> { |
| 500 | typedef std::vector<Constant*> ValType; |
| 501 | static ValType getValType(ConstantArray *CA) { |
| 502 | std::vector<Constant*> Elements; |
| 503 | Elements.reserve(CA->getNumOperands()); |
| 504 | for (unsigned i = 0, e = CA->getNumOperands(); i != e; ++i) |
| 505 | Elements.push_back(cast<Constant>(CA->getOperand(i))); |
| 506 | return Elements; |
Owen Anderson | 9b67698 | 2009-08-04 22:55:26 +0000 | [diff] [blame] | 507 | } |
| 508 | }; |
| 509 | |
| 510 | template<> |
Dan Gohman | e4532f3 | 2009-09-15 15:58:07 +0000 | [diff] [blame] | 511 | struct ConstantKeyData<ConstantStruct> { |
| 512 | typedef std::vector<Constant*> ValType; |
| 513 | static ValType getValType(ConstantStruct *CS) { |
| 514 | std::vector<Constant*> Elements; |
| 515 | Elements.reserve(CS->getNumOperands()); |
| 516 | for (unsigned i = 0, e = CS->getNumOperands(); i != e; ++i) |
| 517 | Elements.push_back(cast<Constant>(CS->getOperand(i))); |
| 518 | return Elements; |
Owen Anderson | 9b67698 | 2009-08-04 22:55:26 +0000 | [diff] [blame] | 519 | } |
| 520 | }; |
| 521 | |
| 522 | // ConstantPointerNull does not take extra "value" argument... |
| 523 | template<class ValType> |
| 524 | struct ConstantCreator<ConstantPointerNull, PointerType, ValType> { |
| 525 | static ConstantPointerNull *create(const PointerType *Ty, const ValType &V){ |
| 526 | return new ConstantPointerNull(Ty); |
| 527 | } |
| 528 | }; |
| 529 | |
| 530 | template<> |
Dan Gohman | e4532f3 | 2009-09-15 15:58:07 +0000 | [diff] [blame] | 531 | struct ConstantKeyData<ConstantPointerNull> { |
| 532 | typedef char ValType; |
| 533 | static ValType getValType(ConstantPointerNull *C) { |
| 534 | return 0; |
Owen Anderson | 9b67698 | 2009-08-04 22:55:26 +0000 | [diff] [blame] | 535 | } |
| 536 | }; |
| 537 | |
| 538 | // UndefValue does not take extra "value" argument... |
| 539 | template<class ValType> |
| 540 | struct ConstantCreator<UndefValue, Type, ValType> { |
| 541 | static UndefValue *create(const Type *Ty, const ValType &V) { |
| 542 | return new UndefValue(Ty); |
| 543 | } |
| 544 | }; |
| 545 | |
| 546 | template<> |
Dan Gohman | e4532f3 | 2009-09-15 15:58:07 +0000 | [diff] [blame] | 547 | struct ConstantKeyData<UndefValue> { |
| 548 | typedef char ValType; |
| 549 | static ValType getValType(UndefValue *C) { |
| 550 | return 0; |
Owen Anderson | 9b67698 | 2009-08-04 22:55:26 +0000 | [diff] [blame] | 551 | } |
| 552 | }; |
| 553 | |
Jeffrey Yasskin | ade270e | 2010-03-21 20:37:19 +0000 | [diff] [blame] | 554 | template<> |
| 555 | struct ConstantCreator<InlineAsm, PointerType, InlineAsmKeyType> { |
| 556 | static InlineAsm *create(const PointerType *Ty, const InlineAsmKeyType &Key) { |
| 557 | return new InlineAsm(Ty, Key.asm_string, Key.constraints, |
| 558 | Key.has_side_effects, Key.is_align_stack); |
| 559 | } |
| 560 | }; |
| 561 | |
| 562 | template<> |
| 563 | struct ConstantKeyData<InlineAsm> { |
| 564 | typedef InlineAsmKeyType ValType; |
| 565 | static ValType getValType(InlineAsm *Asm) { |
| 566 | return InlineAsmKeyType(Asm->getAsmString(), Asm->getConstraintString(), |
| 567 | Asm->hasSideEffects(), Asm->isAlignStack()); |
| 568 | } |
| 569 | }; |
| 570 | |
Owen Anderson | 9b67698 | 2009-08-04 22:55:26 +0000 | [diff] [blame] | 571 | template<class ValType, class TypeClass, class ConstantClass, |
| 572 | bool HasLargeKey = false /*true for arrays and structs*/ > |
Jeffrey Yasskin | f6ee7be | 2009-10-27 23:45:55 +0000 | [diff] [blame] | 573 | class ConstantUniqueMap : public AbstractTypeUser { |
Owen Anderson | 9b67698 | 2009-08-04 22:55:26 +0000 | [diff] [blame] | 574 | public: |
Dan Gohman | e4532f3 | 2009-09-15 15:58:07 +0000 | [diff] [blame] | 575 | typedef std::pair<const TypeClass*, ValType> MapKey; |
| 576 | typedef std::map<MapKey, ConstantClass *> MapTy; |
| 577 | typedef std::map<ConstantClass *, typename MapTy::iterator> InverseMapTy; |
| 578 | typedef std::map<const DerivedType*, typename MapTy::iterator> |
| 579 | AbstractTypeMapTy; |
Owen Anderson | 9b67698 | 2009-08-04 22:55:26 +0000 | [diff] [blame] | 580 | private: |
| 581 | /// Map - This is the main map from the element descriptor to the Constants. |
| 582 | /// This is the primary way we avoid creating two of the same shape |
| 583 | /// constant. |
| 584 | MapTy Map; |
| 585 | |
| 586 | /// InverseMap - If "HasLargeKey" is true, this contains an inverse mapping |
| 587 | /// from the constants to their element in Map. This is important for |
| 588 | /// removal of constants from the array, which would otherwise have to scan |
| 589 | /// through the map with very large keys. |
| 590 | InverseMapTy InverseMap; |
| 591 | |
| 592 | /// AbstractTypeMap - Map for abstract type constants. |
| 593 | /// |
| 594 | AbstractTypeMapTy AbstractTypeMap; |
| 595 | |
Owen Anderson | 9b67698 | 2009-08-04 22:55:26 +0000 | [diff] [blame] | 596 | public: |
Devang Patel | c5aa8c6 | 2009-08-11 06:31:57 +0000 | [diff] [blame] | 597 | typename MapTy::iterator map_begin() { return Map.begin(); } |
Owen Anderson | 9b67698 | 2009-08-04 22:55:26 +0000 | [diff] [blame] | 598 | typename MapTy::iterator map_end() { return Map.end(); } |
Torok Edwin | d18e668 | 2009-08-31 16:14:59 +0000 | [diff] [blame] | 599 | |
| 600 | void freeConstants() { |
| 601 | for (typename MapTy::iterator I=Map.begin(), E=Map.end(); |
| 602 | I != E; ++I) { |
Jeffrey Yasskin | a6eedc3 | 2010-03-22 05:23:37 +0000 | [diff] [blame] | 603 | // Asserts that use_empty(). |
| 604 | delete I->second; |
Torok Edwin | d18e668 | 2009-08-31 16:14:59 +0000 | [diff] [blame] | 605 | } |
| 606 | } |
Owen Anderson | 9b67698 | 2009-08-04 22:55:26 +0000 | [diff] [blame] | 607 | |
| 608 | /// InsertOrGetItem - Return an iterator for the specified element. |
| 609 | /// If the element exists in the map, the returned iterator points to the |
| 610 | /// entry and Exists=true. If not, the iterator points to the newly |
| 611 | /// inserted entry and returns Exists=false. Newly inserted entries have |
| 612 | /// I->second == 0, and should be filled in. |
Dan Gohman | e4532f3 | 2009-09-15 15:58:07 +0000 | [diff] [blame] | 613 | typename MapTy::iterator InsertOrGetItem(std::pair<MapKey, ConstantClass *> |
Owen Anderson | 9b67698 | 2009-08-04 22:55:26 +0000 | [diff] [blame] | 614 | &InsertVal, |
| 615 | bool &Exists) { |
| 616 | std::pair<typename MapTy::iterator, bool> IP = Map.insert(InsertVal); |
| 617 | Exists = !IP.second; |
| 618 | return IP.first; |
| 619 | } |
| 620 | |
| 621 | private: |
| 622 | typename MapTy::iterator FindExistingElement(ConstantClass *CP) { |
| 623 | if (HasLargeKey) { |
| 624 | typename InverseMapTy::iterator IMI = InverseMap.find(CP); |
| 625 | assert(IMI != InverseMap.end() && IMI->second != Map.end() && |
| 626 | IMI->second->second == CP && |
| 627 | "InverseMap corrupt!"); |
| 628 | return IMI->second; |
| 629 | } |
| 630 | |
| 631 | typename MapTy::iterator I = |
| 632 | Map.find(MapKey(static_cast<const TypeClass*>(CP->getRawType()), |
Dan Gohman | e4532f3 | 2009-09-15 15:58:07 +0000 | [diff] [blame] | 633 | ConstantKeyData<ConstantClass>::getValType(CP))); |
Owen Anderson | 9b67698 | 2009-08-04 22:55:26 +0000 | [diff] [blame] | 634 | if (I == Map.end() || I->second != CP) { |
| 635 | // FIXME: This should not use a linear scan. If this gets to be a |
| 636 | // performance problem, someone should look at this. |
| 637 | for (I = Map.begin(); I != Map.end() && I->second != CP; ++I) |
| 638 | /* empty */; |
| 639 | } |
| 640 | return I; |
| 641 | } |
| 642 | |
Dan Gohman | e4532f3 | 2009-09-15 15:58:07 +0000 | [diff] [blame] | 643 | void AddAbstractTypeUser(const Type *Ty, typename MapTy::iterator I) { |
| 644 | // If the type of the constant is abstract, make sure that an entry |
| 645 | // exists for it in the AbstractTypeMap. |
| 646 | if (Ty->isAbstract()) { |
| 647 | const DerivedType *DTy = static_cast<const DerivedType *>(Ty); |
| 648 | typename AbstractTypeMapTy::iterator TI = AbstractTypeMap.find(DTy); |
| 649 | |
| 650 | if (TI == AbstractTypeMap.end()) { |
| 651 | // Add ourselves to the ATU list of the type. |
| 652 | cast<DerivedType>(DTy)->addAbstractTypeUser(this); |
| 653 | |
| 654 | AbstractTypeMap.insert(TI, std::make_pair(DTy, I)); |
| 655 | } |
| 656 | } |
| 657 | } |
| 658 | |
Owen Anderson | 9b67698 | 2009-08-04 22:55:26 +0000 | [diff] [blame] | 659 | ConstantClass* Create(const TypeClass *Ty, const ValType &V, |
| 660 | typename MapTy::iterator I) { |
| 661 | ConstantClass* Result = |
| 662 | ConstantCreator<ConstantClass,TypeClass,ValType>::create(Ty, V); |
| 663 | |
| 664 | assert(Result->getType() == Ty && "Type specified is not correct!"); |
| 665 | I = Map.insert(I, std::make_pair(MapKey(Ty, V), Result)); |
| 666 | |
| 667 | if (HasLargeKey) // Remember the reverse mapping if needed. |
| 668 | InverseMap.insert(std::make_pair(Result, I)); |
| 669 | |
Dan Gohman | e4532f3 | 2009-09-15 15:58:07 +0000 | [diff] [blame] | 670 | AddAbstractTypeUser(Ty, I); |
Owen Anderson | 9b67698 | 2009-08-04 22:55:26 +0000 | [diff] [blame] | 671 | |
| 672 | return Result; |
| 673 | } |
| 674 | public: |
| 675 | |
| 676 | /// getOrCreate - Return the specified constant from the map, creating it if |
| 677 | /// necessary. |
| 678 | ConstantClass *getOrCreate(const TypeClass *Ty, const ValType &V) { |
Owen Anderson | 9b67698 | 2009-08-04 22:55:26 +0000 | [diff] [blame] | 679 | MapKey Lookup(Ty, V); |
| 680 | ConstantClass* Result = 0; |
| 681 | |
| 682 | typename MapTy::iterator I = Map.find(Lookup); |
| 683 | // Is it in the map? |
| 684 | if (I != Map.end()) |
Dan Gohman | e4532f3 | 2009-09-15 15:58:07 +0000 | [diff] [blame] | 685 | Result = I->second; |
Owen Anderson | 9b67698 | 2009-08-04 22:55:26 +0000 | [diff] [blame] | 686 | |
| 687 | if (!Result) { |
| 688 | // If no preexisting value, create one now... |
| 689 | Result = Create(Ty, V, I); |
| 690 | } |
| 691 | |
| 692 | return Result; |
| 693 | } |
| 694 | |
Dan Gohman | e4532f3 | 2009-09-15 15:58:07 +0000 | [diff] [blame] | 695 | void UpdateAbstractTypeMap(const DerivedType *Ty, |
| 696 | typename MapTy::iterator I) { |
| 697 | assert(AbstractTypeMap.count(Ty) && |
| 698 | "Abstract type not in AbstractTypeMap?"); |
| 699 | typename MapTy::iterator &ATMEntryIt = AbstractTypeMap[Ty]; |
| 700 | if (ATMEntryIt == I) { |
| 701 | // Yes, we are removing the representative entry for this type. |
| 702 | // See if there are any other entries of the same type. |
| 703 | typename MapTy::iterator TmpIt = ATMEntryIt; |
| 704 | |
| 705 | // First check the entry before this one... |
| 706 | if (TmpIt != Map.begin()) { |
| 707 | --TmpIt; |
| 708 | if (TmpIt->first.first != Ty) // Not the same type, move back... |
| 709 | ++TmpIt; |
| 710 | } |
| 711 | |
| 712 | // If we didn't find the same type, try to move forward... |
| 713 | if (TmpIt == ATMEntryIt) { |
| 714 | ++TmpIt; |
| 715 | if (TmpIt == Map.end() || TmpIt->first.first != Ty) |
| 716 | --TmpIt; // No entry afterwards with the same type |
| 717 | } |
| 718 | |
| 719 | // If there is another entry in the map of the same abstract type, |
| 720 | // update the AbstractTypeMap entry now. |
| 721 | if (TmpIt != ATMEntryIt) { |
| 722 | ATMEntryIt = TmpIt; |
| 723 | } else { |
| 724 | // Otherwise, we are removing the last instance of this type |
| 725 | // from the table. Remove from the ATM, and from user list. |
| 726 | cast<DerivedType>(Ty)->removeAbstractTypeUser(this); |
| 727 | AbstractTypeMap.erase(Ty); |
| 728 | } |
| 729 | } |
| 730 | } |
| 731 | |
Owen Anderson | 9b67698 | 2009-08-04 22:55:26 +0000 | [diff] [blame] | 732 | void remove(ConstantClass *CP) { |
Owen Anderson | 9b67698 | 2009-08-04 22:55:26 +0000 | [diff] [blame] | 733 | typename MapTy::iterator I = FindExistingElement(CP); |
| 734 | assert(I != Map.end() && "Constant not found in constant table!"); |
| 735 | assert(I->second == CP && "Didn't find correct element?"); |
| 736 | |
| 737 | if (HasLargeKey) // Remember the reverse mapping if needed. |
| 738 | InverseMap.erase(CP); |
| 739 | |
| 740 | // Now that we found the entry, make sure this isn't the entry that |
| 741 | // the AbstractTypeMap points to. |
Dan Gohman | e4532f3 | 2009-09-15 15:58:07 +0000 | [diff] [blame] | 742 | const TypeClass *Ty = I->first.first; |
| 743 | if (Ty->isAbstract()) |
| 744 | UpdateAbstractTypeMap(static_cast<const DerivedType *>(Ty), I); |
Owen Anderson | 9b67698 | 2009-08-04 22:55:26 +0000 | [diff] [blame] | 745 | |
| 746 | Map.erase(I); |
| 747 | } |
| 748 | |
Owen Anderson | 9b67698 | 2009-08-04 22:55:26 +0000 | [diff] [blame] | 749 | /// MoveConstantToNewSlot - If we are about to change C to be the element |
| 750 | /// specified by I, update our internal data structures to reflect this |
| 751 | /// fact. |
Owen Anderson | 9b67698 | 2009-08-04 22:55:26 +0000 | [diff] [blame] | 752 | void MoveConstantToNewSlot(ConstantClass *C, typename MapTy::iterator I) { |
| 753 | // First, remove the old location of the specified constant in the map. |
| 754 | typename MapTy::iterator OldI = FindExistingElement(C); |
| 755 | assert(OldI != Map.end() && "Constant not found in constant table!"); |
| 756 | assert(OldI->second == C && "Didn't find correct element?"); |
| 757 | |
| 758 | // If this constant is the representative element for its abstract type, |
| 759 | // update the AbstractTypeMap so that the representative element is I. |
Chris Lattner | 718da70 | 2010-07-17 06:13:52 +0000 | [diff] [blame] | 760 | // |
| 761 | // This must use getRawType() because if the type is under refinement, we |
| 762 | // will get the refineAbstractType callback below, and we don't want to |
| 763 | // kick union find in on the constant. |
| 764 | if (C->getRawType()->isAbstract()) { |
Owen Anderson | 9b67698 | 2009-08-04 22:55:26 +0000 | [diff] [blame] | 765 | typename AbstractTypeMapTy::iterator ATI = |
Chris Lattner | 718da70 | 2010-07-17 06:13:52 +0000 | [diff] [blame] | 766 | AbstractTypeMap.find(cast<DerivedType>(C->getRawType())); |
Owen Anderson | 9b67698 | 2009-08-04 22:55:26 +0000 | [diff] [blame] | 767 | assert(ATI != AbstractTypeMap.end() && |
| 768 | "Abstract type not in AbstractTypeMap?"); |
| 769 | if (ATI->second == OldI) |
| 770 | ATI->second = I; |
| 771 | } |
| 772 | |
| 773 | // Remove the old entry from the map. |
| 774 | Map.erase(OldI); |
| 775 | |
| 776 | // Update the inverse map so that we know that this constant is now |
| 777 | // located at descriptor I. |
| 778 | if (HasLargeKey) { |
| 779 | assert(I->second == C && "Bad inversemap entry!"); |
| 780 | InverseMap[C] = I; |
| 781 | } |
| 782 | } |
| 783 | |
| 784 | void refineAbstractType(const DerivedType *OldTy, const Type *NewTy) { |
Dan Gohman | e4532f3 | 2009-09-15 15:58:07 +0000 | [diff] [blame] | 785 | typename AbstractTypeMapTy::iterator I = AbstractTypeMap.find(OldTy); |
Owen Anderson | 9b67698 | 2009-08-04 22:55:26 +0000 | [diff] [blame] | 786 | |
| 787 | assert(I != AbstractTypeMap.end() && |
| 788 | "Abstract type not in AbstractTypeMap?"); |
| 789 | |
| 790 | // Convert a constant at a time until the last one is gone. The last one |
| 791 | // leaving will remove() itself, causing the AbstractTypeMapEntry to be |
| 792 | // eliminated eventually. |
| 793 | do { |
Dan Gohman | e4532f3 | 2009-09-15 15:58:07 +0000 | [diff] [blame] | 794 | ConstantClass *C = I->second->second; |
| 795 | MapKey Key(cast<TypeClass>(NewTy), |
| 796 | ConstantKeyData<ConstantClass>::getValType(C)); |
Owen Anderson | 9b67698 | 2009-08-04 22:55:26 +0000 | [diff] [blame] | 797 | |
Dan Gohman | e4532f3 | 2009-09-15 15:58:07 +0000 | [diff] [blame] | 798 | std::pair<typename MapTy::iterator, bool> IP = |
| 799 | Map.insert(std::make_pair(Key, C)); |
| 800 | if (IP.second) { |
| 801 | // The map didn't previously have an appropriate constant in the |
| 802 | // new type. |
| 803 | |
| 804 | // Remove the old entry. |
| 805 | typename MapTy::iterator OldI = |
| 806 | Map.find(MapKey(cast<TypeClass>(OldTy), IP.first->first.second)); |
| 807 | assert(OldI != Map.end() && "Constant not in map!"); |
| 808 | UpdateAbstractTypeMap(OldTy, OldI); |
| 809 | Map.erase(OldI); |
| 810 | |
| 811 | // Set the constant's type. This is done in place! |
| 812 | setType(C, NewTy); |
| 813 | |
| 814 | // Update the inverse map so that we know that this constant is now |
| 815 | // located at descriptor I. |
| 816 | if (HasLargeKey) |
| 817 | InverseMap[C] = IP.first; |
| 818 | |
| 819 | AddAbstractTypeUser(NewTy, IP.first); |
| 820 | } else { |
| 821 | // The map already had an appropriate constant in the new type, so |
| 822 | // there's no longer a need for the old constant. |
| 823 | C->uncheckedReplaceAllUsesWith(IP.first->second); |
| 824 | C->destroyConstant(); // This constant is now dead, destroy it. |
| 825 | } |
| 826 | I = AbstractTypeMap.find(OldTy); |
Owen Anderson | 9b67698 | 2009-08-04 22:55:26 +0000 | [diff] [blame] | 827 | } while (I != AbstractTypeMap.end()); |
| 828 | } |
| 829 | |
| 830 | // If the type became concrete without being refined to any other existing |
| 831 | // type, we just remove ourselves from the ATU list. |
| 832 | void typeBecameConcrete(const DerivedType *AbsTy) { |
| 833 | AbsTy->removeAbstractTypeUser(this); |
| 834 | } |
| 835 | |
| 836 | void dump() const { |
David Greene | 338a903 | 2010-01-05 01:34:26 +0000 | [diff] [blame] | 837 | DEBUG(dbgs() << "Constant.cpp: ConstantUniqueMap\n"); |
Owen Anderson | 9b67698 | 2009-08-04 22:55:26 +0000 | [diff] [blame] | 838 | } |
| 839 | }; |
| 840 | |
| 841 | } |
| 842 | |
| 843 | #endif |