Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1 | //===-- Type.cpp - Implement the Type class ----------------------*- C++ -*--=// |
| 2 | // |
| 3 | // This file implements the Type class for the VMCore library. |
| 4 | // |
| 5 | //===----------------------------------------------------------------------===// |
| 6 | |
| 7 | #include "llvm/DerivedTypes.h" |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 8 | #include "llvm/SymbolTable.h" |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 9 | #include "Support/StringExtras.h" |
| 10 | #include "Support/STLExtras.h" |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 11 | |
| 12 | // DEBUG_MERGE_TYPES - Enable this #define to see how and when derived types are |
| 13 | // created and later destroyed, all in an effort to make sure that there is only |
| 14 | // a single cannonical version of a type. |
| 15 | // |
| 16 | //#define DEBUG_MERGE_TYPES 1 |
| 17 | |
| 18 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 19 | |
| 20 | //===----------------------------------------------------------------------===// |
| 21 | // Type Class Implementation |
| 22 | //===----------------------------------------------------------------------===// |
| 23 | |
| 24 | static unsigned CurUID = 0; |
| 25 | static vector<const Type *> UIDMappings; |
| 26 | |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 27 | Type::Type(const string &name, PrimitiveID id) |
| 28 | : Value(Type::TypeTy, Value::TypeVal) { |
| 29 | setDescription(name); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 30 | ID = id; |
Chris Lattner | 55fd998 | 2001-10-30 16:39:16 +0000 | [diff] [blame] | 31 | Abstract = Recursive = false; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 32 | UID = CurUID++; // Assign types UID's as they are created |
| 33 | UIDMappings.push_back(this); |
| 34 | } |
| 35 | |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 36 | void Type::setName(const string &Name, SymbolTable *ST) { |
| 37 | assert(ST && "Type::setName - Must provide symbol table argument!"); |
| 38 | |
| 39 | if (Name.size()) ST->insert(Name, this); |
| 40 | } |
| 41 | |
| 42 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 43 | const Type *Type::getUniqueIDType(unsigned UID) { |
| 44 | assert(UID < UIDMappings.size() && |
| 45 | "Type::getPrimitiveType: UID out of range!"); |
| 46 | return UIDMappings[UID]; |
| 47 | } |
| 48 | |
| 49 | const Type *Type::getPrimitiveType(PrimitiveID IDNumber) { |
| 50 | switch (IDNumber) { |
| 51 | case VoidTyID : return VoidTy; |
| 52 | case BoolTyID : return BoolTy; |
| 53 | case UByteTyID : return UByteTy; |
| 54 | case SByteTyID : return SByteTy; |
| 55 | case UShortTyID: return UShortTy; |
| 56 | case ShortTyID : return ShortTy; |
| 57 | case UIntTyID : return UIntTy; |
| 58 | case IntTyID : return IntTy; |
| 59 | case ULongTyID : return ULongTy; |
| 60 | case LongTyID : return LongTy; |
| 61 | case FloatTyID : return FloatTy; |
| 62 | case DoubleTyID: return DoubleTy; |
| 63 | case TypeTyID : return TypeTy; |
| 64 | case LabelTyID : return LabelTy; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 65 | default: |
| 66 | return 0; |
| 67 | } |
| 68 | } |
| 69 | |
Chris Lattner | 58716b9 | 2001-11-26 17:01:47 +0000 | [diff] [blame] | 70 | // isLosslesslyConvertableTo - Return true if this type can be converted to |
| 71 | // 'Ty' without any reinterpretation of bits. For example, uint to int. |
| 72 | // |
| 73 | bool Type::isLosslesslyConvertableTo(const Type *Ty) const { |
| 74 | if (this == Ty) return true; |
Chris Lattner | 7026107 | 2001-12-06 18:06:37 +0000 | [diff] [blame] | 75 | if ((!isPrimitiveType() && !isPointerType()) || |
| 76 | (!Ty->isPointerType() && !Ty->isPrimitiveType())) return false; |
Chris Lattner | 58716b9 | 2001-11-26 17:01:47 +0000 | [diff] [blame] | 77 | |
| 78 | if (getPrimitiveID() == Ty->getPrimitiveID()) |
| 79 | return true; // Handles identity cast, and cast of differing pointer types |
| 80 | |
| 81 | // Now we know that they are two differing primitive or pointer types |
| 82 | switch (getPrimitiveID()) { |
| 83 | case Type::UByteTyID: return Ty == Type::SByteTy; |
| 84 | case Type::SByteTyID: return Ty == Type::UByteTy; |
| 85 | case Type::UShortTyID: return Ty == Type::ShortTy; |
| 86 | case Type::ShortTyID: return Ty == Type::UShortTy; |
| 87 | case Type::UIntTyID: return Ty == Type::IntTy; |
| 88 | case Type::IntTyID: return Ty == Type::UIntTy; |
| 89 | case Type::ULongTyID: |
| 90 | case Type::LongTyID: |
| 91 | case Type::PointerTyID: |
| 92 | return Ty == Type::ULongTy || Ty == Type::LongTy || |
| 93 | Ty->getPrimitiveID() == Type::PointerTyID; |
| 94 | default: |
| 95 | return false; // Other types have no identity values |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | |
| 100 | bool StructType::indexValid(const Value *V) const { |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 101 | if (!isa<Constant>(V)) return false; |
Chris Lattner | 58716b9 | 2001-11-26 17:01:47 +0000 | [diff] [blame] | 102 | if (V->getType() != Type::UByteTy) return false; |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 103 | unsigned Idx = cast<ConstantUInt>(V)->getValue(); |
Chris Lattner | 58716b9 | 2001-11-26 17:01:47 +0000 | [diff] [blame] | 104 | return Idx < ETypes.size(); |
| 105 | } |
| 106 | |
| 107 | // getTypeAtIndex - Given an index value into the type, return the type of the |
| 108 | // element. For a structure type, this must be a constant value... |
| 109 | // |
| 110 | const Type *StructType::getTypeAtIndex(const Value *V) const { |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 111 | assert(isa<Constant>(V) && "Structure index must be a constant!!"); |
Chris Lattner | 58716b9 | 2001-11-26 17:01:47 +0000 | [diff] [blame] | 112 | assert(V->getType() == Type::UByteTy && "Structure index must be ubyte!"); |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 113 | unsigned Idx = cast<ConstantUInt>(V)->getValue(); |
Chris Lattner | 58716b9 | 2001-11-26 17:01:47 +0000 | [diff] [blame] | 114 | assert(Idx < ETypes.size() && "Structure index out of range!"); |
| 115 | assert(indexValid(V) && "Invalid structure index!"); // Duplicate check |
| 116 | |
| 117 | return ETypes[Idx]; |
| 118 | } |
| 119 | |
| 120 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 121 | //===----------------------------------------------------------------------===// |
| 122 | // Auxilliary classes |
| 123 | //===----------------------------------------------------------------------===// |
| 124 | // |
| 125 | // These classes are used to implement specialized behavior for each different |
| 126 | // type. |
| 127 | // |
| 128 | class SignedIntType : public Type { |
| 129 | int Size; |
| 130 | public: |
| 131 | SignedIntType(const string &Name, PrimitiveID id, int size) : Type(Name, id) { |
| 132 | Size = size; |
| 133 | } |
| 134 | |
| 135 | // isSigned - Return whether a numeric type is signed. |
| 136 | virtual bool isSigned() const { return 1; } |
Chris Lattner | 1a2cefc | 2001-07-21 19:15:26 +0000 | [diff] [blame] | 137 | |
| 138 | // isIntegral - Equivalent to isSigned() || isUnsigned, but with only a single |
| 139 | // virtual function invocation. |
| 140 | // |
Vikram S. Adve | a4e6f88 | 2001-07-21 12:32:48 +0000 | [diff] [blame] | 141 | virtual bool isIntegral() const { return 1; } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 142 | }; |
| 143 | |
| 144 | class UnsignedIntType : public Type { |
| 145 | uint64_t Size; |
| 146 | public: |
| 147 | UnsignedIntType(const string &N, PrimitiveID id, int size) : Type(N, id) { |
| 148 | Size = size; |
| 149 | } |
| 150 | |
| 151 | // isUnsigned - Return whether a numeric type is signed. |
| 152 | virtual bool isUnsigned() const { return 1; } |
Chris Lattner | 1a2cefc | 2001-07-21 19:15:26 +0000 | [diff] [blame] | 153 | |
| 154 | // isIntegral - Equivalent to isSigned() || isUnsigned, but with only a single |
| 155 | // virtual function invocation. |
| 156 | // |
Vikram S. Adve | a4e6f88 | 2001-07-21 12:32:48 +0000 | [diff] [blame] | 157 | virtual bool isIntegral() const { return 1; } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 158 | }; |
| 159 | |
| 160 | static struct TypeType : public Type { |
| 161 | TypeType() : Type("type", TypeTyID) {} |
| 162 | } TheTypeType; // Implement the type that is global. |
| 163 | |
| 164 | |
| 165 | //===----------------------------------------------------------------------===// |
| 166 | // Static 'Type' data |
| 167 | //===----------------------------------------------------------------------===// |
| 168 | |
Chris Lattner | ca24d38 | 2001-09-10 20:11:44 +0000 | [diff] [blame] | 169 | Type *Type::VoidTy = new Type("void" , VoidTyID), |
| 170 | *Type::BoolTy = new Type("bool" , BoolTyID), |
| 171 | *Type::SByteTy = new SignedIntType("sbyte" , SByteTyID, 1), |
| 172 | *Type::UByteTy = new UnsignedIntType("ubyte" , UByteTyID, 1), |
| 173 | *Type::ShortTy = new SignedIntType("short" , ShortTyID, 2), |
| 174 | *Type::UShortTy = new UnsignedIntType("ushort", UShortTyID, 2), |
| 175 | *Type::IntTy = new SignedIntType("int" , IntTyID, 4), |
| 176 | *Type::UIntTy = new UnsignedIntType("uint" , UIntTyID, 4), |
| 177 | *Type::LongTy = new SignedIntType("long" , LongTyID, 8), |
| 178 | *Type::ULongTy = new UnsignedIntType("ulong" , ULongTyID, 8), |
| 179 | *Type::FloatTy = new Type("float" , FloatTyID), |
| 180 | *Type::DoubleTy = new Type("double", DoubleTyID), |
| 181 | *Type::TypeTy = &TheTypeType, |
| 182 | *Type::LabelTy = new Type("label" , LabelTyID); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 183 | |
| 184 | |
| 185 | //===----------------------------------------------------------------------===// |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 186 | // Derived Type Constructors |
| 187 | //===----------------------------------------------------------------------===// |
| 188 | |
| 189 | MethodType::MethodType(const Type *Result, const vector<const Type*> &Params, |
Chris Lattner | 6c42c31 | 2001-12-14 16:41:56 +0000 | [diff] [blame^] | 190 | bool IsVarArgs) : DerivedType(MethodTyID), |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 191 | ResultType(PATypeHandle<Type>(Result, this)), |
Chris Lattner | e5a57ee | 2001-07-25 22:47:55 +0000 | [diff] [blame] | 192 | isVarArgs(IsVarArgs) { |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 193 | ParamTys.reserve(Params.size()); |
Chris Lattner | 56c5acb | 2001-10-13 07:01:33 +0000 | [diff] [blame] | 194 | for (unsigned i = 0; i < Params.size(); ++i) |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 195 | ParamTys.push_back(PATypeHandle<Type>(Params[i], this)); |
| 196 | |
| 197 | setDerivedTypeProperties(); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 198 | } |
| 199 | |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 200 | StructType::StructType(const vector<const Type*> &Types) |
Chris Lattner | 6c42c31 | 2001-12-14 16:41:56 +0000 | [diff] [blame^] | 201 | : CompositeType(StructTyID) { |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 202 | ETypes.reserve(Types.size()); |
Chris Lattner | 56c5acb | 2001-10-13 07:01:33 +0000 | [diff] [blame] | 203 | for (unsigned i = 0; i < Types.size(); ++i) { |
| 204 | assert(Types[i] != Type::VoidTy && "Void type in method prototype!!"); |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 205 | ETypes.push_back(PATypeHandle<Type>(Types[i], this)); |
Chris Lattner | 56c5acb | 2001-10-13 07:01:33 +0000 | [diff] [blame] | 206 | } |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 207 | setDerivedTypeProperties(); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 208 | } |
| 209 | |
Chris Lattner | 6c42c31 | 2001-12-14 16:41:56 +0000 | [diff] [blame^] | 210 | ArrayType::ArrayType(const Type *ElType, unsigned NumEl) |
| 211 | : SequentialType(ArrayTyID, ElType) { |
| 212 | NumElements = NumEl; |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 213 | setDerivedTypeProperties(); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 214 | } |
| 215 | |
Chris Lattner | 6c42c31 | 2001-12-14 16:41:56 +0000 | [diff] [blame^] | 216 | PointerType::PointerType(const Type *E) : SequentialType(PointerTyID, E) { |
| 217 | setDerivedTypeProperties(); |
| 218 | } |
| 219 | |
| 220 | OpaqueType::OpaqueType() : DerivedType(OpaqueTyID) { |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 221 | setAbstract(true); |
| 222 | setDescription("opaque"+utostr(getUniqueID())); |
| 223 | #ifdef DEBUG_MERGE_TYPES |
| 224 | cerr << "Derived new type: " << getDescription() << endl; |
| 225 | #endif |
| 226 | } |
| 227 | |
| 228 | |
| 229 | |
| 230 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 231 | //===----------------------------------------------------------------------===// |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 232 | // Derived Type setDerivedTypeProperties Function |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 233 | //===----------------------------------------------------------------------===// |
| 234 | |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 235 | // getTypeProps - This is a recursive function that walks a type hierarchy |
| 236 | // calculating the description for a type and whether or not it is abstract or |
| 237 | // recursive. Worst case it will have to do a lot of traversing if you have |
| 238 | // some whacko opaque types, but in most cases, it will do some simple stuff |
| 239 | // when it hits non-abstract types that aren't recursive. |
| 240 | // |
| 241 | static string getTypeProps(const Type *Ty, vector<const Type *> &TypeStack, |
| 242 | bool &isAbstract, bool &isRecursive) { |
| 243 | string Result; |
| 244 | if (!Ty->isAbstract() && !Ty->isRecursive() && // Base case for the recursion |
| 245 | Ty->getDescription().size()) { |
| 246 | Result = Ty->getDescription(); // Primitive = leaf type |
Chris Lattner | b00c582 | 2001-10-02 03:41:24 +0000 | [diff] [blame] | 247 | } else if (isa<OpaqueType>(Ty)) { // Base case for the recursion |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 248 | Result = Ty->getDescription(); // Opaque = leaf type |
| 249 | isAbstract = true; // This whole type is abstract! |
| 250 | } else { |
| 251 | // Check to see if the Type is already on the stack... |
| 252 | unsigned Slot = 0, CurSize = TypeStack.size(); |
| 253 | while (Slot < CurSize && TypeStack[Slot] != Ty) ++Slot; // Scan for type |
| 254 | |
| 255 | // This is another base case for the recursion. In this case, we know |
| 256 | // that we have looped back to a type that we have previously visited. |
| 257 | // Generate the appropriate upreference to handle this. |
| 258 | // |
| 259 | if (Slot < CurSize) { |
| 260 | Result = "\\" + utostr(CurSize-Slot); // Here's the upreference |
| 261 | isRecursive = true; // We know we are recursive |
| 262 | } else { // Recursive case: abstract derived type... |
| 263 | TypeStack.push_back(Ty); // Add us to the stack.. |
| 264 | |
| 265 | switch (Ty->getPrimitiveID()) { |
| 266 | case Type::MethodTyID: { |
Chris Lattner | b00c582 | 2001-10-02 03:41:24 +0000 | [diff] [blame] | 267 | const MethodType *MTy = cast<const MethodType>(Ty); |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 268 | Result = getTypeProps(MTy->getReturnType(), TypeStack, |
| 269 | isAbstract, isRecursive)+" ("; |
| 270 | for (MethodType::ParamTypes::const_iterator |
| 271 | I = MTy->getParamTypes().begin(), |
| 272 | E = MTy->getParamTypes().end(); I != E; ++I) { |
| 273 | if (I != MTy->getParamTypes().begin()) |
| 274 | Result += ", "; |
| 275 | Result += getTypeProps(*I, TypeStack, isAbstract, isRecursive); |
| 276 | } |
| 277 | if (MTy->isVarArg()) { |
| 278 | if (!MTy->getParamTypes().empty()) Result += ", "; |
| 279 | Result += "..."; |
| 280 | } |
| 281 | Result += ")"; |
| 282 | break; |
| 283 | } |
| 284 | case Type::StructTyID: { |
Chris Lattner | b00c582 | 2001-10-02 03:41:24 +0000 | [diff] [blame] | 285 | const StructType *STy = cast<const StructType>(Ty); |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 286 | Result = "{ "; |
| 287 | for (StructType::ElementTypes::const_iterator |
| 288 | I = STy->getElementTypes().begin(), |
| 289 | E = STy->getElementTypes().end(); I != E; ++I) { |
| 290 | if (I != STy->getElementTypes().begin()) |
| 291 | Result += ", "; |
| 292 | Result += getTypeProps(*I, TypeStack, isAbstract, isRecursive); |
| 293 | } |
| 294 | Result += " }"; |
| 295 | break; |
| 296 | } |
| 297 | case Type::PointerTyID: { |
Chris Lattner | b00c582 | 2001-10-02 03:41:24 +0000 | [diff] [blame] | 298 | const PointerType *PTy = cast<const PointerType>(Ty); |
Chris Lattner | 7a17675 | 2001-12-04 00:03:30 +0000 | [diff] [blame] | 299 | Result = getTypeProps(PTy->getElementType(), TypeStack, |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 300 | isAbstract, isRecursive) + " *"; |
| 301 | break; |
| 302 | } |
| 303 | case Type::ArrayTyID: { |
Chris Lattner | b00c582 | 2001-10-02 03:41:24 +0000 | [diff] [blame] | 304 | const ArrayType *ATy = cast<const ArrayType>(Ty); |
Chris Lattner | 6c42c31 | 2001-12-14 16:41:56 +0000 | [diff] [blame^] | 305 | unsigned NumElements = ATy->getNumElements(); |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 306 | Result = "["; |
Chris Lattner | 6c42c31 | 2001-12-14 16:41:56 +0000 | [diff] [blame^] | 307 | Result += utostr(NumElements) + " x "; |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 308 | Result += getTypeProps(ATy->getElementType(), TypeStack, |
| 309 | isAbstract, isRecursive) + "]"; |
| 310 | break; |
| 311 | } |
| 312 | default: |
| 313 | assert(0 && "Unhandled case in getTypeProps!"); |
| 314 | Result = "<error>"; |
| 315 | } |
| 316 | |
| 317 | TypeStack.pop_back(); // Remove self from stack... |
| 318 | } |
| 319 | } |
| 320 | return Result; |
| 321 | } |
| 322 | |
| 323 | |
| 324 | // setDerivedTypeProperties - This function is used to calculate the |
| 325 | // isAbstract, isRecursive, and the Description settings for a type. The |
| 326 | // getTypeProps function does all the dirty work. |
| 327 | // |
| 328 | void DerivedType::setDerivedTypeProperties() { |
| 329 | vector<const Type *> TypeStack; |
| 330 | bool isAbstract = false, isRecursive = false; |
| 331 | |
| 332 | setDescription(getTypeProps(this, TypeStack, isAbstract, isRecursive)); |
| 333 | setAbstract(isAbstract); |
| 334 | setRecursive(isRecursive); |
| 335 | } |
| 336 | |
| 337 | |
| 338 | //===----------------------------------------------------------------------===// |
| 339 | // Type Structural Equality Testing |
| 340 | //===----------------------------------------------------------------------===// |
| 341 | |
| 342 | // TypesEqual - Two types are considered structurally equal if they have the |
| 343 | // same "shape": Every level and element of the types have identical primitive |
| 344 | // ID's, and the graphs have the same edges/nodes in them. Nodes do not have to |
| 345 | // be pointer equals to be equivalent though. This uses an optimistic algorithm |
| 346 | // that assumes that two graphs are the same until proven otherwise. |
| 347 | // |
| 348 | static bool TypesEqual(const Type *Ty, const Type *Ty2, |
| 349 | map<const Type *, const Type *> &EqTypes) { |
| 350 | if (Ty == Ty2) return true; |
| 351 | if (Ty->getPrimitiveID() != Ty2->getPrimitiveID()) return false; |
| 352 | if (Ty->isPrimitiveType()) return true; |
Chris Lattner | 008f906 | 2001-10-24 05:12:04 +0000 | [diff] [blame] | 353 | if (isa<OpaqueType>(Ty)) |
| 354 | return false; // Two nonequal opaque types are never equal |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 355 | |
Chris Lattner | 3b1e3f4 | 2001-11-02 07:51:31 +0000 | [diff] [blame] | 356 | map<const Type*, const Type*>::iterator It = EqTypes.find(Ty); |
| 357 | if (It != EqTypes.end()) |
| 358 | return It->second == Ty2; // Looping back on a type, check for equality |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 359 | |
Chris Lattner | 3b1e3f4 | 2001-11-02 07:51:31 +0000 | [diff] [blame] | 360 | // Otherwise, add the mapping to the table to make sure we don't get |
| 361 | // recursion on the types... |
| 362 | EqTypes.insert(make_pair(Ty, Ty2)); |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 363 | |
| 364 | // Iterate over the types and make sure the the contents are equivalent... |
Chris Lattner | 74c2b76 | 2001-09-09 22:26:58 +0000 | [diff] [blame] | 365 | Type::subtype_iterator I = Ty ->subtype_begin(), IE = Ty ->subtype_end(); |
| 366 | Type::subtype_iterator I2 = Ty2->subtype_begin(), IE2 = Ty2->subtype_end(); |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 367 | for (; I != IE && I2 != IE2; ++I, ++I2) |
| 368 | if (!TypesEqual(*I, *I2, EqTypes)) return false; |
| 369 | |
Chris Lattner | 56c5acb | 2001-10-13 07:01:33 +0000 | [diff] [blame] | 370 | // Two really annoying special cases that breaks an otherwise nice simple |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 371 | // algorithm is the fact that arraytypes have sizes that differentiates types, |
Chris Lattner | 56c5acb | 2001-10-13 07:01:33 +0000 | [diff] [blame] | 372 | // and that method types can be varargs or not. Consider this now. |
| 373 | if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) { |
| 374 | if (ATy->getNumElements() != cast<const ArrayType>(Ty2)->getNumElements()) |
| 375 | return false; |
| 376 | } else if (const MethodType *MTy = dyn_cast<MethodType>(Ty)) { |
| 377 | if (MTy->isVarArg() != cast<const MethodType>(Ty2)->isVarArg()) |
| 378 | return false; |
| 379 | } |
| 380 | |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 381 | return I == IE && I2 == IE2; // Types equal if both iterators are done |
| 382 | } |
| 383 | |
| 384 | static bool TypesEqual(const Type *Ty, const Type *Ty2) { |
| 385 | map<const Type *, const Type *> EqTypes; |
| 386 | return TypesEqual(Ty, Ty2, EqTypes); |
| 387 | } |
| 388 | |
| 389 | |
| 390 | |
| 391 | //===----------------------------------------------------------------------===// |
| 392 | // Derived Type Factory Functions |
| 393 | //===----------------------------------------------------------------------===// |
| 394 | |
| 395 | // TypeMap - Make sure that only one instance of a particular type may be |
| 396 | // created on any given run of the compiler... note that this involves updating |
| 397 | // our map if an abstract type gets refined somehow... |
| 398 | // |
| 399 | template<class ValType, class TypeClass> |
| 400 | class TypeMap : public AbstractTypeUser { |
| 401 | typedef map<ValType, PATypeHandle<TypeClass> > MapTy; |
| 402 | MapTy Map; |
| 403 | public: |
| 404 | |
| 405 | ~TypeMap() { print("ON EXIT"); } |
| 406 | |
| 407 | inline TypeClass *get(const ValType &V) { |
| 408 | map<ValType, PATypeHandle<TypeClass> >::iterator I = Map.find(V); |
| 409 | // TODO: FIXME: When Types are not CONST. |
| 410 | return (I != Map.end()) ? (TypeClass*)I->second.get() : 0; |
| 411 | } |
| 412 | |
| 413 | inline void add(const ValType &V, TypeClass *T) { |
| 414 | Map.insert(make_pair(V, PATypeHandle<TypeClass>(T, this))); |
| 415 | print("add"); |
| 416 | } |
| 417 | |
| 418 | // containsEquivalent - Return true if the typemap contains a type that is |
| 419 | // structurally equivalent to the specified type. |
| 420 | // |
| 421 | inline const TypeClass *containsEquivalent(const TypeClass *Ty) { |
| 422 | for (MapTy::iterator I = Map.begin(), E = Map.end(); I != E; ++I) |
| 423 | if (I->second.get() != Ty && TypesEqual(Ty, I->second.get())) |
| 424 | return (TypeClass*)I->second.get(); // FIXME TODO when types not const |
| 425 | return 0; |
| 426 | } |
| 427 | |
| 428 | // refineAbstractType - This is called when one of the contained abstract |
| 429 | // types gets refined... this simply removes the abstract type from our table. |
| 430 | // We expect that whoever refined the type will add it back to the table, |
| 431 | // corrected. |
| 432 | // |
| 433 | virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy) { |
Chris Lattner | e244a25 | 2001-11-03 03:27:53 +0000 | [diff] [blame] | 434 | if (OldTy == NewTy) { |
| 435 | if (!OldTy->isAbstract()) { |
| 436 | // Check to see if the type just became concrete. |
| 437 | // If so, remove self from user list. |
| 438 | for (MapTy::iterator I = Map.begin(), E = Map.end(); I != E; ++I) |
| 439 | if (I->second == OldTy) |
| 440 | I->second.removeUserFromConcrete(); |
| 441 | } |
| 442 | return; |
| 443 | } |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 444 | #ifdef DEBUG_MERGE_TYPES |
| 445 | cerr << "Removing Old type from Tab: " << (void*)OldTy << ", " |
| 446 | << OldTy->getDescription() << " replacement == " << (void*)NewTy |
| 447 | << ", " << NewTy->getDescription() << endl; |
| 448 | #endif |
| 449 | for (MapTy::iterator I = Map.begin(), E = Map.end(); I != E; ++I) |
| 450 | if (I->second == OldTy) { |
| 451 | Map.erase(I); |
| 452 | print("refineAbstractType after"); |
| 453 | return; |
| 454 | } |
| 455 | assert(0 && "Abstract type not found in table!"); |
| 456 | } |
| 457 | |
| 458 | void remove(const ValType &OldVal) { |
| 459 | MapTy::iterator I = Map.find(OldVal); |
| 460 | assert(I != Map.end() && "TypeMap::remove, element not found!"); |
| 461 | Map.erase(I); |
| 462 | } |
| 463 | |
| 464 | void print(const char *Arg) { |
| 465 | #ifdef DEBUG_MERGE_TYPES |
| 466 | cerr << "TypeMap<>::" << Arg << " table contents:\n"; |
| 467 | unsigned i = 0; |
| 468 | for (MapTy::iterator I = Map.begin(), E = Map.end(); I != E; ++I) |
| 469 | cerr << " " << (++i) << ". " << I->second << " " |
| 470 | << I->second->getDescription() << endl; |
| 471 | #endif |
| 472 | } |
| 473 | }; |
| 474 | |
| 475 | |
| 476 | // ValTypeBase - This is the base class that is used by the various |
| 477 | // instantiations of TypeMap. This class is an AbstractType user that notifies |
| 478 | // the underlying TypeMap when it gets modified. |
| 479 | // |
| 480 | template<class ValType, class TypeClass> |
| 481 | class ValTypeBase : public AbstractTypeUser { |
| 482 | TypeMap<ValType, TypeClass> &MyTable; |
| 483 | protected: |
| 484 | inline ValTypeBase(TypeMap<ValType, TypeClass> &tab) : MyTable(tab) {} |
| 485 | |
| 486 | // Subclass should override this... to update self as usual |
| 487 | virtual void doRefinement(const DerivedType *OldTy, const Type *NewTy) = 0; |
Chris Lattner | e244a25 | 2001-11-03 03:27:53 +0000 | [diff] [blame] | 488 | |
| 489 | // typeBecameConcrete - This callback occurs when a contained type refines |
| 490 | // to itself, but becomes concrete in the process. Our subclass should remove |
| 491 | // itself from the ATU list of the specified type. |
| 492 | // |
| 493 | virtual void typeBecameConcrete(const DerivedType *Ty) = 0; |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 494 | |
| 495 | virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy) { |
Chris Lattner | e244a25 | 2001-11-03 03:27:53 +0000 | [diff] [blame] | 496 | if (OldTy == NewTy) { |
| 497 | if (!OldTy->isAbstract()) |
| 498 | typeBecameConcrete(OldTy); |
| 499 | return; |
| 500 | } |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 501 | TypeMap<ValType, TypeClass> &Table = MyTable; // Copy MyTable reference |
| 502 | ValType Tmp(*(ValType*)this); // Copy this. |
| 503 | PATypeHandle<TypeClass> OldType(Table.get(*(ValType*)this), this); |
| 504 | Table.remove(*(ValType*)this); // Destroy's this! |
Chris Lattner | 3b1e3f4 | 2001-11-02 07:51:31 +0000 | [diff] [blame] | 505 | #if 1 |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 506 | // Refine temporary to new state... |
| 507 | Tmp.doRefinement(OldTy, NewTy); |
| 508 | |
| 509 | Table.add((ValType&)Tmp, (TypeClass*)OldType.get()); |
Chris Lattner | 3b1e3f4 | 2001-11-02 07:51:31 +0000 | [diff] [blame] | 510 | #endif |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 511 | } |
| 512 | }; |
| 513 | |
| 514 | |
| 515 | |
| 516 | //===----------------------------------------------------------------------===// |
| 517 | // Method Type Factory and Value Class... |
| 518 | // |
| 519 | |
| 520 | // MethodValType - Define a class to hold the key that goes into the TypeMap |
| 521 | // |
| 522 | class MethodValType : public ValTypeBase<MethodValType, MethodType> { |
| 523 | PATypeHandle<Type> RetTy; |
| 524 | vector<PATypeHandle<Type> > ArgTypes; |
Chris Lattner | 56c5acb | 2001-10-13 07:01:33 +0000 | [diff] [blame] | 525 | bool isVarArg; |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 526 | public: |
| 527 | MethodValType(const Type *ret, const vector<const Type*> &args, |
Chris Lattner | 56c5acb | 2001-10-13 07:01:33 +0000 | [diff] [blame] | 528 | bool IVA, TypeMap<MethodValType, MethodType> &Tab) |
| 529 | : ValTypeBase<MethodValType, MethodType>(Tab), RetTy(ret, this), |
| 530 | isVarArg(IVA) { |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 531 | for (unsigned i = 0; i < args.size(); ++i) |
| 532 | ArgTypes.push_back(PATypeHandle<Type>(args[i], this)); |
| 533 | } |
| 534 | |
| 535 | // We *MUST* have an explicit copy ctor so that the TypeHandles think that |
| 536 | // this MethodValType owns them, not the old one! |
| 537 | // |
| 538 | MethodValType(const MethodValType &MVT) |
Chris Lattner | 56c5acb | 2001-10-13 07:01:33 +0000 | [diff] [blame] | 539 | : ValTypeBase<MethodValType, MethodType>(MVT), RetTy(MVT.RetTy, this), |
| 540 | isVarArg(MVT.isVarArg) { |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 541 | ArgTypes.reserve(MVT.ArgTypes.size()); |
| 542 | for (unsigned i = 0; i < MVT.ArgTypes.size(); ++i) |
| 543 | ArgTypes.push_back(PATypeHandle<Type>(MVT.ArgTypes[i], this)); |
| 544 | } |
| 545 | |
| 546 | // Subclass should override this... to update self as usual |
| 547 | virtual void doRefinement(const DerivedType *OldType, const Type *NewType) { |
| 548 | if (RetTy == OldType) RetTy = NewType; |
| 549 | for (unsigned i = 0; i < ArgTypes.size(); ++i) |
| 550 | if (ArgTypes[i] == OldType) ArgTypes[i] = NewType; |
| 551 | } |
| 552 | |
Chris Lattner | e244a25 | 2001-11-03 03:27:53 +0000 | [diff] [blame] | 553 | virtual void typeBecameConcrete(const DerivedType *Ty) { |
| 554 | if (RetTy == Ty) RetTy.removeUserFromConcrete(); |
| 555 | |
| 556 | for (unsigned i = 0; i < ArgTypes.size(); ++i) |
| 557 | if (ArgTypes[i] == Ty) ArgTypes[i].removeUserFromConcrete(); |
| 558 | } |
| 559 | |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 560 | inline bool operator<(const MethodValType &MTV) const { |
Chris Lattner | 56c5acb | 2001-10-13 07:01:33 +0000 | [diff] [blame] | 561 | if (RetTy.get() < MTV.RetTy.get()) return true; |
| 562 | if (RetTy.get() > MTV.RetTy.get()) return false; |
| 563 | |
| 564 | if (ArgTypes < MTV.ArgTypes) return true; |
| 565 | return (ArgTypes == MTV.ArgTypes) && isVarArg < MTV.isVarArg; |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 566 | } |
| 567 | }; |
| 568 | |
| 569 | // Define the actual map itself now... |
| 570 | static TypeMap<MethodValType, MethodType> MethodTypes; |
| 571 | |
| 572 | // MethodType::get - The factory function for the MethodType class... |
| 573 | MethodType *MethodType::get(const Type *ReturnType, |
Chris Lattner | 56c5acb | 2001-10-13 07:01:33 +0000 | [diff] [blame] | 574 | const vector<const Type*> &Params, |
| 575 | bool isVarArg) { |
| 576 | MethodValType VT(ReturnType, Params, isVarArg, MethodTypes); |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 577 | MethodType *MT = MethodTypes.get(VT); |
| 578 | if (MT) return MT; |
Chris Lattner | e5a57ee | 2001-07-25 22:47:55 +0000 | [diff] [blame] | 579 | |
Chris Lattner | 56c5acb | 2001-10-13 07:01:33 +0000 | [diff] [blame] | 580 | MethodTypes.add(VT, MT = new MethodType(ReturnType, Params, isVarArg)); |
Chris Lattner | e5a57ee | 2001-07-25 22:47:55 +0000 | [diff] [blame] | 581 | |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 582 | #ifdef DEBUG_MERGE_TYPES |
| 583 | cerr << "Derived new type: " << MT << endl; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 584 | #endif |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 585 | return MT; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 586 | } |
| 587 | |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 588 | //===----------------------------------------------------------------------===// |
| 589 | // Array Type Factory... |
| 590 | // |
| 591 | class ArrayValType : public ValTypeBase<ArrayValType, ArrayType> { |
| 592 | PATypeHandle<Type> ValTy; |
Chris Lattner | 6c42c31 | 2001-12-14 16:41:56 +0000 | [diff] [blame^] | 593 | unsigned Size; |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 594 | public: |
| 595 | ArrayValType(const Type *val, int sz, TypeMap<ArrayValType, ArrayType> &Tab) |
| 596 | : ValTypeBase<ArrayValType, ArrayType>(Tab), ValTy(val, this), Size(sz) {} |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 597 | |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 598 | // We *MUST* have an explicit copy ctor so that the ValTy thinks that this |
| 599 | // ArrayValType owns it, not the old one! |
| 600 | // |
| 601 | ArrayValType(const ArrayValType &AVT) |
| 602 | : ValTypeBase<ArrayValType, ArrayType>(AVT), ValTy(AVT.ValTy, this), |
| 603 | Size(AVT.Size) {} |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 604 | |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 605 | // Subclass should override this... to update self as usual |
| 606 | virtual void doRefinement(const DerivedType *OldType, const Type *NewType) { |
| 607 | if (ValTy == OldType) ValTy = NewType; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 608 | } |
| 609 | |
Chris Lattner | e244a25 | 2001-11-03 03:27:53 +0000 | [diff] [blame] | 610 | virtual void typeBecameConcrete(const DerivedType *Ty) { |
| 611 | assert(ValTy == Ty && |
| 612 | "Contained type became concrete but we're not using it!"); |
| 613 | ValTy.removeUserFromConcrete(); |
| 614 | } |
| 615 | |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 616 | inline bool operator<(const ArrayValType &MTV) const { |
| 617 | if (Size < MTV.Size) return true; |
| 618 | return Size == MTV.Size && ValTy.get() < MTV.ValTy.get(); |
| 619 | } |
| 620 | }; |
| 621 | |
| 622 | static TypeMap<ArrayValType, ArrayType> ArrayTypes; |
| 623 | |
Chris Lattner | 6c42c31 | 2001-12-14 16:41:56 +0000 | [diff] [blame^] | 624 | ArrayType *ArrayType::get(const Type *ElementType, unsigned NumElements) { |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 625 | assert(ElementType && "Can't get array of null types!"); |
| 626 | |
| 627 | ArrayValType AVT(ElementType, NumElements, ArrayTypes); |
| 628 | ArrayType *AT = ArrayTypes.get(AVT); |
| 629 | if (AT) return AT; // Found a match, return it! |
| 630 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 631 | // Value not found. Derive a new type! |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 632 | ArrayTypes.add(AVT, AT = new ArrayType(ElementType, NumElements)); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 633 | |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 634 | #ifdef DEBUG_MERGE_TYPES |
| 635 | cerr << "Derived new type: " << AT->getDescription() << endl; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 636 | #endif |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 637 | return AT; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 638 | } |
| 639 | |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 640 | //===----------------------------------------------------------------------===// |
| 641 | // Struct Type Factory... |
| 642 | // |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 643 | |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 644 | // StructValType - Define a class to hold the key that goes into the TypeMap |
| 645 | // |
| 646 | class StructValType : public ValTypeBase<StructValType, StructType> { |
| 647 | vector<PATypeHandle<Type> > ElTypes; |
| 648 | public: |
| 649 | StructValType(const vector<const Type*> &args, |
| 650 | TypeMap<StructValType, StructType> &Tab) |
| 651 | : ValTypeBase<StructValType, StructType>(Tab) { |
| 652 | for (unsigned i = 0; i < args.size(); ++i) |
| 653 | ElTypes.push_back(PATypeHandle<Type>(args[i], this)); |
| 654 | } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 655 | |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 656 | // We *MUST* have an explicit copy ctor so that the TypeHandles think that |
| 657 | // this StructValType owns them, not the old one! |
| 658 | // |
| 659 | StructValType(const StructValType &SVT) |
| 660 | : ValTypeBase<StructValType, StructType>(SVT){ |
| 661 | ElTypes.reserve(SVT.ElTypes.size()); |
| 662 | for (unsigned i = 0; i < SVT.ElTypes.size(); ++i) |
| 663 | ElTypes.push_back(PATypeHandle<Type>(SVT.ElTypes[i], this)); |
| 664 | } |
| 665 | |
| 666 | // Subclass should override this... to update self as usual |
| 667 | virtual void doRefinement(const DerivedType *OldType, const Type *NewType) { |
| 668 | for (unsigned i = 0; i < ElTypes.size(); ++i) |
| 669 | if (ElTypes[i] == OldType) ElTypes[i] = NewType; |
| 670 | } |
| 671 | |
Chris Lattner | e244a25 | 2001-11-03 03:27:53 +0000 | [diff] [blame] | 672 | virtual void typeBecameConcrete(const DerivedType *Ty) { |
| 673 | for (unsigned i = 0; i < ElTypes.size(); ++i) |
| 674 | if (ElTypes[i] == Ty) ElTypes[i].removeUserFromConcrete(); |
| 675 | } |
| 676 | |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 677 | inline bool operator<(const StructValType &STV) const { |
| 678 | return ElTypes < STV.ElTypes; |
| 679 | } |
| 680 | }; |
| 681 | |
| 682 | static TypeMap<StructValType, StructType> StructTypes; |
| 683 | |
| 684 | StructType *StructType::get(const vector<const Type*> &ETypes) { |
| 685 | StructValType STV(ETypes, StructTypes); |
| 686 | StructType *ST = StructTypes.get(STV); |
| 687 | if (ST) return ST; |
| 688 | |
| 689 | // Value not found. Derive a new type! |
| 690 | StructTypes.add(STV, ST = new StructType(ETypes)); |
| 691 | |
| 692 | #ifdef DEBUG_MERGE_TYPES |
| 693 | cerr << "Derived new type: " << ST->getDescription() << endl; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 694 | #endif |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 695 | return ST; |
| 696 | } |
| 697 | |
| 698 | //===----------------------------------------------------------------------===// |
| 699 | // Pointer Type Factory... |
| 700 | // |
| 701 | |
| 702 | // PointerValType - Define a class to hold the key that goes into the TypeMap |
| 703 | // |
| 704 | class PointerValType : public ValTypeBase<PointerValType, PointerType> { |
| 705 | PATypeHandle<Type> ValTy; |
| 706 | public: |
| 707 | PointerValType(const Type *val, TypeMap<PointerValType, PointerType> &Tab) |
| 708 | : ValTypeBase<PointerValType, PointerType>(Tab), ValTy(val, this) {} |
| 709 | |
| 710 | // We *MUST* have an explicit copy ctor so that the ValTy thinks that this |
| 711 | // PointerValType owns it, not the old one! |
| 712 | // |
| 713 | PointerValType(const PointerValType &PVT) |
| 714 | : ValTypeBase<PointerValType, PointerType>(PVT), ValTy(PVT.ValTy, this) {} |
| 715 | |
| 716 | // Subclass should override this... to update self as usual |
| 717 | virtual void doRefinement(const DerivedType *OldType, const Type *NewType) { |
| 718 | if (ValTy == OldType) ValTy = NewType; |
| 719 | } |
| 720 | |
Chris Lattner | e244a25 | 2001-11-03 03:27:53 +0000 | [diff] [blame] | 721 | virtual void typeBecameConcrete(const DerivedType *Ty) { |
| 722 | assert(ValTy == Ty && |
| 723 | "Contained type became concrete but we're not using it!"); |
| 724 | ValTy.removeUserFromConcrete(); |
| 725 | } |
| 726 | |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 727 | inline bool operator<(const PointerValType &MTV) const { |
| 728 | return ValTy.get() < MTV.ValTy.get(); |
| 729 | } |
| 730 | }; |
| 731 | |
| 732 | static TypeMap<PointerValType, PointerType> PointerTypes; |
| 733 | |
| 734 | PointerType *PointerType::get(const Type *ValueType) { |
| 735 | assert(ValueType && "Can't get a pointer to <null> type!"); |
| 736 | PointerValType PVT(ValueType, PointerTypes); |
| 737 | |
| 738 | PointerType *PT = PointerTypes.get(PVT); |
| 739 | if (PT) return PT; |
| 740 | |
| 741 | // Value not found. Derive a new type! |
| 742 | PointerTypes.add(PVT, PT = new PointerType(ValueType)); |
| 743 | |
| 744 | #ifdef DEBUG_MERGE_TYPES |
| 745 | cerr << "Derived new type: " << PT->getDescription() << endl; |
| 746 | #endif |
| 747 | return PT; |
| 748 | } |
| 749 | |
| 750 | |
| 751 | |
| 752 | //===----------------------------------------------------------------------===// |
| 753 | // Derived Type Refinement Functions |
| 754 | //===----------------------------------------------------------------------===// |
| 755 | |
| 756 | // removeAbstractTypeUser - Notify an abstract type that a user of the class |
| 757 | // no longer has a handle to the type. This function is called primarily by |
| 758 | // the PATypeHandle class. When there are no users of the abstract type, it |
| 759 | // is anihilated, because there is no way to get a reference to it ever again. |
| 760 | // |
| 761 | void DerivedType::removeAbstractTypeUser(AbstractTypeUser *U) const { |
| 762 | // Search from back to front because we will notify users from back to |
| 763 | // front. Also, it is likely that there will be a stack like behavior to |
| 764 | // users that register and unregister users. |
| 765 | // |
| 766 | for (unsigned i = AbstractTypeUsers.size(); i > 0; --i) { |
| 767 | if (AbstractTypeUsers[i-1] == U) { |
| 768 | AbstractTypeUsers.erase(AbstractTypeUsers.begin()+i-1); |
| 769 | |
| 770 | #ifdef DEBUG_MERGE_TYPES |
Chris Lattner | e244a25 | 2001-11-03 03:27:53 +0000 | [diff] [blame] | 771 | cerr << " removeAbstractTypeUser<" << (void*)this << ", " |
| 772 | << getDescription() << ">[" << i << "] User = " << U << endl; |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 773 | #endif |
| 774 | |
Chris Lattner | e244a25 | 2001-11-03 03:27:53 +0000 | [diff] [blame] | 775 | if (AbstractTypeUsers.empty() && isAbstract()) { |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 776 | #ifdef DEBUG_MERGE_TYPES |
Chris Lattner | e244a25 | 2001-11-03 03:27:53 +0000 | [diff] [blame] | 777 | cerr << "DELETEing unused abstract type: <" << getDescription() |
| 778 | << ">[" << (void*)this << "]" << endl; |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 779 | #endif |
| 780 | delete this; // No users of this abstract type! |
| 781 | } |
| 782 | return; |
| 783 | } |
| 784 | } |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 785 | assert(0 && "AbstractTypeUser not in user list!"); |
| 786 | } |
| 787 | |
| 788 | |
| 789 | // refineAbstractTypeTo - This function is used to when it is discovered that |
| 790 | // the 'this' abstract type is actually equivalent to the NewType specified. |
| 791 | // This causes all users of 'this' to switch to reference the more concrete |
| 792 | // type NewType and for 'this' to be deleted. |
| 793 | // |
| 794 | void DerivedType::refineAbstractTypeTo(const Type *NewType) { |
| 795 | assert(isAbstract() && "refineAbstractTypeTo: Current type is not abstract!"); |
| 796 | assert(this != NewType && "Can't refine to myself!"); |
| 797 | |
| 798 | #ifdef DEBUG_MERGE_TYPES |
| 799 | cerr << "REFINING abstract type [" << (void*)this << " " << getDescription() |
| 800 | << "] to [" << (void*)NewType << " " << NewType->getDescription() |
| 801 | << "]!\n"; |
| 802 | #endif |
| 803 | |
| 804 | |
| 805 | // Make sure to put the type to be refined to into a holder so that if IT gets |
| 806 | // refined, that we will not continue using a dead reference... |
| 807 | // |
| 808 | PATypeHolder<Type> NewTy(NewType); |
| 809 | |
| 810 | // Add a self use of the current type so that we don't delete ourself until |
| 811 | // after this while loop. We are careful to never invoke refine on ourself, |
| 812 | // so this extra reference shouldn't be a problem. Note that we must only |
| 813 | // remove a single reference at the end, but we must tolerate multiple self |
| 814 | // references because we could be refineAbstractTypeTo'ing recursively on the |
| 815 | // same type. |
| 816 | // |
| 817 | addAbstractTypeUser(this); |
| 818 | |
| 819 | // Count the number of self uses. Stop looping when sizeof(list) == NSU. |
| 820 | unsigned NumSelfUses = 0; |
| 821 | |
| 822 | // Iterate over all of the uses of this type, invoking callback. Each user |
| 823 | // should remove itself from our use list automatically. |
| 824 | // |
| 825 | while (AbstractTypeUsers.size() > NumSelfUses) { |
| 826 | AbstractTypeUser *User = AbstractTypeUsers.back(); |
| 827 | |
| 828 | if (User == this) { |
| 829 | // Move self use to the start of the list. Increment NSU. |
| 830 | swap(AbstractTypeUsers.back(), AbstractTypeUsers[NumSelfUses++]); |
| 831 | } else { |
| 832 | unsigned OldSize = AbstractTypeUsers.size(); |
| 833 | #ifdef DEBUG_MERGE_TYPES |
| 834 | cerr << " REFINING user " << OldSize-1 << " of abstract type [" |
| 835 | << (void*)this << " " << getDescription() << "] to [" |
| 836 | << (void*)NewTy.get() << " " << NewTy->getDescription() << "]!\n"; |
| 837 | #endif |
Chris Lattner | 008f906 | 2001-10-24 05:12:04 +0000 | [diff] [blame] | 838 | User->refineAbstractType(this, NewTy); |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 839 | |
Chris Lattner | e244a25 | 2001-11-03 03:27:53 +0000 | [diff] [blame] | 840 | if (AbstractTypeUsers.size() == OldSize) { |
| 841 | User->refineAbstractType(this, NewTy); |
| 842 | } |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 843 | assert(AbstractTypeUsers.size() != OldSize && |
| 844 | "AbsTyUser did not remove self from user list!"); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 845 | } |
| 846 | } |
| 847 | |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 848 | // Remove a single self use, even though there may be several here. This will |
| 849 | // probably 'delete this', so no instance variables may be used after this |
| 850 | // occurs... |
| 851 | assert(AbstractTypeUsers.back() == this && "Only self uses should be left!"); |
| 852 | removeAbstractTypeUser(this); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 853 | } |
| 854 | |
| 855 | |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 856 | // typeIsRefined - Notify AbstractTypeUsers of this type that the current type |
| 857 | // has been refined a bit. The pointer is still valid and still should be |
| 858 | // used, but the subtypes have changed. |
| 859 | // |
| 860 | void DerivedType::typeIsRefined() { |
| 861 | assert(isRefining >= 0 && isRefining <= 2 && "isRefining out of bounds!"); |
Chris Lattner | 3b1e3f4 | 2001-11-02 07:51:31 +0000 | [diff] [blame] | 862 | if (isRefining == 1) return; // Kill recursion here... |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 863 | ++isRefining; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 864 | |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 865 | #ifdef DEBUG_MERGE_TYPES |
| 866 | cerr << "typeIsREFINED type: " << (void*)this <<" "<<getDescription() << endl; |
| 867 | #endif |
| 868 | for (unsigned i = 0; i < AbstractTypeUsers.size(); ) { |
| 869 | AbstractTypeUser *ATU = AbstractTypeUsers[i]; |
| 870 | #ifdef DEBUG_MERGE_TYPES |
| 871 | cerr << " typeIsREFINED user " << i << " of abstract type [" |
| 872 | << (void*)this << " " << getDescription() << "]\n"; |
| 873 | #endif |
| 874 | ATU->refineAbstractType(this, this); |
| 875 | |
| 876 | // If the user didn't remove itself from the list, continue... |
Chris Lattner | 3b1e3f4 | 2001-11-02 07:51:31 +0000 | [diff] [blame] | 877 | if (AbstractTypeUsers.size() > i && AbstractTypeUsers[i] == ATU) { |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 878 | ++i; |
Chris Lattner | 3b1e3f4 | 2001-11-02 07:51:31 +0000 | [diff] [blame] | 879 | } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 880 | } |
| 881 | |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 882 | --isRefining; |
Chris Lattner | e244a25 | 2001-11-03 03:27:53 +0000 | [diff] [blame] | 883 | |
| 884 | #ifndef _NDEBUG |
| 885 | if (!(isAbstract() || AbstractTypeUsers.empty())) |
| 886 | for (unsigned i = 0; i < AbstractTypeUsers.size(); ++i) { |
| 887 | if (AbstractTypeUsers[i] != this) { |
| 888 | // Debugging hook |
| 889 | cerr << "FOUND FAILURE\n"; |
| 890 | AbstractTypeUsers[i]->refineAbstractType(this, this); |
| 891 | assert(0 && "Type became concrete," |
| 892 | " but it still has abstract type users hanging around!"); |
| 893 | } |
| 894 | } |
| 895 | #endif |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 896 | } |
| 897 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 898 | |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 899 | |
| 900 | |
| 901 | // refineAbstractType - Called when a contained type is found to be more |
| 902 | // concrete - this could potentially change us from an abstract type to a |
| 903 | // concrete type. |
| 904 | // |
| 905 | void MethodType::refineAbstractType(const DerivedType *OldType, |
| 906 | const Type *NewType) { |
| 907 | #ifdef DEBUG_MERGE_TYPES |
| 908 | cerr << "MethodTy::refineAbstractTy(" << (void*)OldType << "[" |
| 909 | << OldType->getDescription() << "], " << (void*)NewType << " [" |
| 910 | << NewType->getDescription() << "])\n"; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 911 | #endif |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 912 | |
Chris Lattner | e244a25 | 2001-11-03 03:27:53 +0000 | [diff] [blame] | 913 | if (!OldType->isAbstract()) { |
| 914 | if (ResultType == OldType) ResultType.removeUserFromConcrete(); |
| 915 | for (unsigned i = 0; i < ParamTys.size(); ++i) |
| 916 | if (ParamTys[i] == OldType) ParamTys[i].removeUserFromConcrete(); |
| 917 | } |
| 918 | |
Chris Lattner | 3b1e3f4 | 2001-11-02 07:51:31 +0000 | [diff] [blame] | 919 | if (OldType != NewType) { |
| 920 | if (ResultType == OldType) ResultType = NewType; |
| 921 | |
| 922 | for (unsigned i = 0; i < ParamTys.size(); ++i) |
| 923 | if (ParamTys[i] == OldType) ParamTys[i] = NewType; |
| 924 | } |
| 925 | |
| 926 | const MethodType *MT = MethodTypes.containsEquivalent(this); |
| 927 | if (MT && MT != this) { |
Chris Lattner | e244a25 | 2001-11-03 03:27:53 +0000 | [diff] [blame] | 928 | refineAbstractTypeTo(MT); // Different type altogether... |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 929 | } else { |
Chris Lattner | 3b1e3f4 | 2001-11-02 07:51:31 +0000 | [diff] [blame] | 930 | setDerivedTypeProperties(); // Update the name and isAbstract |
Chris Lattner | e244a25 | 2001-11-03 03:27:53 +0000 | [diff] [blame] | 931 | typeIsRefined(); // Same type, different contents... |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 932 | } |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 933 | } |
| 934 | |
| 935 | |
| 936 | // refineAbstractType - Called when a contained type is found to be more |
| 937 | // concrete - this could potentially change us from an abstract type to a |
| 938 | // concrete type. |
| 939 | // |
| 940 | void ArrayType::refineAbstractType(const DerivedType *OldType, |
| 941 | const Type *NewType) { |
| 942 | #ifdef DEBUG_MERGE_TYPES |
| 943 | cerr << "ArrayTy::refineAbstractTy(" << (void*)OldType << "[" |
| 944 | << OldType->getDescription() << "], " << (void*)NewType << " [" |
| 945 | << NewType->getDescription() << "])\n"; |
| 946 | #endif |
Chris Lattner | 3b1e3f4 | 2001-11-02 07:51:31 +0000 | [diff] [blame] | 947 | |
Chris Lattner | e244a25 | 2001-11-03 03:27:53 +0000 | [diff] [blame] | 948 | if (!OldType->isAbstract()) { |
Chris Lattner | 6c42c31 | 2001-12-14 16:41:56 +0000 | [diff] [blame^] | 949 | assert(getElementType() == OldType); |
Chris Lattner | e244a25 | 2001-11-03 03:27:53 +0000 | [diff] [blame] | 950 | ElementType.removeUserFromConcrete(); |
| 951 | } |
| 952 | |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 953 | ElementType = NewType; |
Chris Lattner | 3b1e3f4 | 2001-11-02 07:51:31 +0000 | [diff] [blame] | 954 | const ArrayType *AT = ArrayTypes.containsEquivalent(this); |
| 955 | if (AT && AT != this) { |
| 956 | refineAbstractTypeTo(AT); // Different type altogether... |
| 957 | } else { |
| 958 | setDerivedTypeProperties(); // Update the name and isAbstract |
| 959 | typeIsRefined(); // Same type, different contents... |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 960 | } |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 961 | } |
| 962 | |
| 963 | |
| 964 | // refineAbstractType - Called when a contained type is found to be more |
| 965 | // concrete - this could potentially change us from an abstract type to a |
| 966 | // concrete type. |
| 967 | // |
| 968 | void StructType::refineAbstractType(const DerivedType *OldType, |
| 969 | const Type *NewType) { |
| 970 | #ifdef DEBUG_MERGE_TYPES |
| 971 | cerr << "StructTy::refineAbstractTy(" << (void*)OldType << "[" |
| 972 | << OldType->getDescription() << "], " << (void*)NewType << " [" |
| 973 | << NewType->getDescription() << "])\n"; |
| 974 | #endif |
Chris Lattner | e244a25 | 2001-11-03 03:27:53 +0000 | [diff] [blame] | 975 | if (!OldType->isAbstract()) { |
| 976 | for (unsigned i = 0; i < ETypes.size(); ++i) |
| 977 | if (ETypes[i] == OldType) |
| 978 | ETypes[i].removeUserFromConcrete(); |
| 979 | } |
| 980 | |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 981 | if (OldType != NewType) { |
Chris Lattner | 3b1e3f4 | 2001-11-02 07:51:31 +0000 | [diff] [blame] | 982 | // Update old type to new type in the array... |
| 983 | for (unsigned i = 0; i < ETypes.size(); ++i) |
| 984 | if (ETypes[i] == OldType) |
| 985 | ETypes[i] = NewType; |
Chris Lattner | e244a25 | 2001-11-03 03:27:53 +0000 | [diff] [blame] | 986 | } |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 987 | |
Chris Lattner | 3b1e3f4 | 2001-11-02 07:51:31 +0000 | [diff] [blame] | 988 | const StructType *ST = StructTypes.containsEquivalent(this); |
| 989 | if (ST && ST != this) { |
| 990 | refineAbstractTypeTo(ST); // Different type altogether... |
| 991 | } else { |
| 992 | setDerivedTypeProperties(); // Update the name and isAbstract |
| 993 | typeIsRefined(); // Same type, different contents... |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 994 | } |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 995 | } |
| 996 | |
| 997 | // refineAbstractType - Called when a contained type is found to be more |
| 998 | // concrete - this could potentially change us from an abstract type to a |
| 999 | // concrete type. |
| 1000 | // |
| 1001 | void PointerType::refineAbstractType(const DerivedType *OldType, |
| 1002 | const Type *NewType) { |
| 1003 | #ifdef DEBUG_MERGE_TYPES |
| 1004 | cerr << "PointerTy::refineAbstractTy(" << (void*)OldType << "[" |
| 1005 | << OldType->getDescription() << "], " << (void*)NewType << " [" |
| 1006 | << NewType->getDescription() << "])\n"; |
| 1007 | #endif |
Chris Lattner | 3b1e3f4 | 2001-11-02 07:51:31 +0000 | [diff] [blame] | 1008 | |
Chris Lattner | e244a25 | 2001-11-03 03:27:53 +0000 | [diff] [blame] | 1009 | if (!OldType->isAbstract()) { |
Chris Lattner | 6c42c31 | 2001-12-14 16:41:56 +0000 | [diff] [blame^] | 1010 | assert(ElementType == OldType); |
| 1011 | ElementType.removeUserFromConcrete(); |
Chris Lattner | e244a25 | 2001-11-03 03:27:53 +0000 | [diff] [blame] | 1012 | } |
| 1013 | |
Chris Lattner | 6c42c31 | 2001-12-14 16:41:56 +0000 | [diff] [blame^] | 1014 | ElementType = NewType; |
Chris Lattner | 3b1e3f4 | 2001-11-02 07:51:31 +0000 | [diff] [blame] | 1015 | const PointerType *PT = PointerTypes.containsEquivalent(this); |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 1016 | |
Chris Lattner | 3b1e3f4 | 2001-11-02 07:51:31 +0000 | [diff] [blame] | 1017 | if (PT && PT != this) { |
| 1018 | refineAbstractTypeTo(PT); // Different type altogether... |
| 1019 | } else { |
| 1020 | setDerivedTypeProperties(); // Update the name and isAbstract |
| 1021 | typeIsRefined(); // Same type, different contents... |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 1022 | } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1023 | } |
| 1024 | |