llvm.org GIT mirror llvm / 61c70e9
remove unions from LLVM IR. They are severely buggy and not being actively maintained, improved, or extended. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@112356 91177308-0d34-0410-b5e6-96231b3b80d8 Chris Lattner 8 years ago
33 changed file(s) with 35 addition(s) and 822 deletion(s). Raw diff Collapse all Expand all
13661366

13671367
13681368
1369
1370
1371
1372
1373
1374
1375

[UNION, ...eltty...]

1376
1377

The UNION record (code 17) adds a union type to

1378 the type table. The eltty operand fields are zero or more type
1379 indices representing the element types of the union.
1380

1381
1382
1383
13841369
13851370
13861371
7373
  • Array Type
  • 7474
  • Structure Type
  • 7575
  • Packed Structure Type
  • 76
  • Union Type
  • 7776
  • Vector Type
  • 7877
    7978
    14741473 pointer,
    14751474 vector,
    14761475 structure,
    1477 union,
    14781476 array,
    14791477 label,
    14801478 metadata.
    14941492 pointer,
    14951493 structure,
    14961494 packed structure,
    1497 union,
    14981495 vector,
    14991496 opaque.
    15001497
    16421639
    16431640

    Aggregate Types are a subset of derived types that can contain multiple

    16441641 member types. Arrays,
    1645 structs, vectors and
    1646 unions are aggregate types.

    1642 structs, and vectors are
    1643 aggregate types.

    16471644
    16481645
    16491646
    17131710
    Overview:
    17141711

    The function type can be thought of as a function signature. It consists of

    17151712 a return type and a list of formal parameter types. The return type of a
    1716 function type is a scalar type, a void type, a struct type, or a union
    1717 type. If the return type is a struct type then all struct elements must be
    1718 of first class types, and the struct must have at least one element.

    1713 function type is a first class type or a void type.

    17191714
    17201715
    Syntax:
    17211716
    
                      
                    
    18371832
    18381833
    18391834
    1840
    1841
    1842
    1843
    1844
    Overview:
    1845

    A union type describes an object with size and alignment suitable for

    1846 an object of any one of a given set of types (also known as an "untagged"
    1847 union). It is similar in concept and usage to a
    1848 struct, except that all members of the union
    1849 have an offset of zero. The elements of a union may be any type that has a
    1850 size. Unions must have at least one member - empty unions are not allowed.
    1851

    1852
    1853

    The size of the union as a whole will be the size of its largest member,

    1854 and the alignment requirements of the union as a whole will be the largest
    1855 alignment requirement of any member.

    1856
    1857

    Union members are accessed using 'load and

    1858 'store' by getting a pointer to a field with
    1859 the 'getelementptr' instruction.
    1860 Since all members are at offset zero, the getelementptr instruction does
    1861 not affect the address, only the type of the resulting pointer.

    1862
    1863
    Syntax:
    1864
    
                      
                    
    1865 union { <type list> }
    1866
    1867
    1868
    Examples:
    1869
    1870
    1871 union { i32, i32*, float }
    1872 A union of three types: an i32, a pointer to
    1873 an i32, and a float.
    1874
    1875
    1876 union { float, i32 (i32) * }
    1877 A union, where the first element is a float and the
    1878 second element is a pointer to a
    1879 function that takes an i32, returning
    1880 an i32.
    1881
    1882
    1883
    1884
    1885
    1886
    18871835
    18881836
    18891837
    21232071 Structure constants must have structure type, and
    21242072 the number and types of elements must match those specified by the
    21252073 type.
    2126
    2127
    Union constants
    2128
    Union constants are represented with notation similar to a structure with
    2129 a single element - that is, a single typed element surrounded
    2130 by braces ({})). For example: "{ i32 4 }". The
    2131 union type can be initialized with a single-element
    2132 struct as long as the type of the struct element matches the type of
    2133 one of the union members.
    21342074
    21352075
    Array constants
    21362076
    Array constants are represented with notation similar to array type
    41524092
    41534093
    Arguments:
    41544094

    The first operand of an 'extractvalue' instruction is a value

    4155 of struct, union or
    4095 of struct or
    41564096 array type. The operands are constant indices to
    41574097 specify which value to extract in a similar manner as indices in a
    41584098 'getelementptr' instruction.

    41864126
    41874127
    Arguments:
    41884128

    The first operand of an 'insertvalue' instruction is a value

    4189 of struct, union or
    4129 of struct or
    41904130 array type. The second operand is a first-class
    41914131 value to insert. The following operands are constant indices indicating
    41924132 the position at which to insert the value in a similar manner as indices in a
    44194359 indexes a value of the type pointed to (not necessarily the value directly
    44204360 pointed to, since the first index can be non-zero), etc. The first type
    44214361 indexed into must be a pointer value, subsequent types can be arrays,
    4422 vectors, structs and unions. Note that subsequent types being indexed into
    4362 vectors, and structs. Note that subsequent types being indexed into
    44234363 can never be pointers, since that would require loading the pointer before
    44244364 continuing calculation.

    44254365
    44264366

    The type of each index argument depends on the type it is indexing into.

    4427 When indexing into a (optionally packed) structure or union, only i32
    4367 When indexing into a (optionally packed) structure, only i32
    44284368 integer constants are allowed. When indexing into an array, pointer
    44294369 or vector, integers of any width are allowed, and they are not required to be
    44304370 constant.

    6666 include/llvm/Analysis/LiveValues.h => Dan
    6767 lib/Transforms/IPO/MergeFunctions.cpp => consider for 2.8.
    6868 llvm/Analysis/PointerTracking.h => Edwin wants this, consider for 2.8.
    69 ABCD, GEPSplitterPass
    69 GEPSplitterPass
    7070 MSIL backend?
    71 lib/Transforms/Utils/SSI.cpp -> ABCD depends on it.
    7271 -->
    7372
    7473
    9393 TYPE_CODE_FP128 = 14, // LONG DOUBLE (112 bit mantissa)
    9494 TYPE_CODE_PPC_FP128= 15, // PPC LONG DOUBLE (2 doubles)
    9595
    96 TYPE_CODE_METADATA = 16, // METADATA
    97 TYPE_CODE_UNION = 17 // UNION: [eltty x N]
    96 TYPE_CODE_METADATA = 16 // METADATA
    9897 };
    9998
    10099 // The type symbol table only has one code (TST_ENTRY_CODE).
    3232 class ArrayType;
    3333 class IntegerType;
    3434 class StructType;
    35 class UnionType;
    3635 class PointerType;
    3736 class VectorType;
    3837
    458457
    459458 DEFINE_TRANSPARENT_CASTED_OPERAND_ACCESSORS(ConstantStruct, Constant)
    460459
    461 //===----------------------------------------------------------------------===//
    462 // ConstantUnion - Constant Union Declarations
    463 //
    464 class ConstantUnion : public Constant {
    465 friend struct ConstantCreator;
    466 ConstantUnion(const ConstantUnion &); // DO NOT IMPLEMENT
    467 protected:
    468 ConstantUnion(const UnionType *T, Constant* Val);
    469 public:
    470 // ConstantUnion accessors
    471 static Constant *get(const UnionType *T, Constant* V);
    472
    473 /// Transparently provide more efficient getOperand methods.
    474 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
    475
    476 /// getType() specialization - Reduce amount of casting...
    477 ///
    478 inline const UnionType *getType() const {
    479 return reinterpret_cast(Value::getType());
    480 }
    481
    482 /// isNullValue - Return true if this is the value that would be returned by
    483 /// getNullValue. This always returns false because zero structs are always
    484 /// created as ConstantAggregateZero objects.
    485 virtual bool isNullValue() const {
    486 return false;
    487 }
    488
    489 virtual void destroyConstant();
    490 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
    491
    492 /// Methods for support type inquiry through isa, cast, and dyn_cast:
    493 static inline bool classof(const ConstantUnion *) { return true; }
    494 static bool classof(const Value *V) {
    495 return V->getValueID() == ConstantUnionVal;
    496 }
    497 };
    498
    499 template <>
    500 struct OperandTraits : public FixedNumOperandTraits<1> {
    501 };
    502
    503 DEFINE_TRANSPARENT_CASTED_OPERAND_ACCESSORS(ConstantUnion, Constant)
    504460
    505461 //===----------------------------------------------------------------------===//
    506462 /// ConstantVector - Constant Vector Declarations
    2626 class FunctionValType;
    2727 class ArrayValType;
    2828 class StructValType;
    29 class UnionValType;
    3029 class PointerValType;
    3130 class VectorValType;
    3231 class IntegerValType;
    225224 return T->getTypeID() == ArrayTyID ||
    226225 T->getTypeID() == StructTyID ||
    227226 T->getTypeID() == PointerTyID ||
    228 T->getTypeID() == VectorTyID ||
    229 T->getTypeID() == UnionTyID;
    227 T->getTypeID() == VectorTyID;
    230228 }
    231229 };
    232230
    296294
    297295 bool isPacked() const { return (0 != getSubclassData()) ? true : false; }
    298296 };
    299
    300
    301 /// UnionType - Class to represent union types. A union type is similar to
    302 /// a structure, except that all member fields begin at offset 0.
    303 ///
    304 class UnionType : public CompositeType {
    305 friend class TypeMap;
    306 UnionType(const UnionType &); // Do not implement
    307 const UnionType &operator=(const UnionType &); // Do not implement
    308 UnionType(LLVMContext &C, const Type* const* Types, unsigned NumTypes);
    309 public:
    310 /// UnionType::get - This static method is the primary way to create a
    311 /// UnionType.
    312 static UnionType *get(const Type* const* Types, unsigned NumTypes);
    313
    314 /// UnionType::get - This static method is a convenience method for
    315 /// creating union types by specifying the elements as arguments.
    316 static UnionType *get(const Type *type, ...) END_WITH_NULL;
    317
    318 /// isValidElementType - Return true if the specified type is valid as a
    319 /// element type.
    320 static bool isValidElementType(const Type *ElemTy);
    321
    322 /// Given an element type, return the member index of that type, or -1
    323 /// if there is no such member type.
    324 int getElementTypeIndex(const Type *ElemTy) const;
    325
    326 // Iterator access to the elements
    327 typedef Type::subtype_iterator element_iterator;
    328 element_iterator element_begin() const { return ContainedTys; }
    329 element_iterator element_end() const { return &ContainedTys[NumContainedTys];}
    330
    331 // Random access to the elements
    332 unsigned getNumElements() const { return NumContainedTys; }
    333 const Type *getElementType(unsigned N) const {
    334 assert(N < NumContainedTys && "Element number out of range!");
    335 return ContainedTys[N];
    336 }
    337
    338 /// getTypeAtIndex - Given an index value into the type, return the type of
    339 /// the element. For a union type, this must be a constant value...
    340 ///
    341 virtual const Type *getTypeAtIndex(const Value *V) const;
    342 virtual const Type *getTypeAtIndex(unsigned Idx) const;
    343 virtual bool indexValid(const Value *V) const;
    344 virtual bool indexValid(unsigned Idx) const;
    345
    346 // Implement the AbstractTypeUser interface.
    347 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
    348 virtual void typeBecameConcrete(const DerivedType *AbsTy);
    349
    350 // Methods for support type inquiry through isa, cast, and dyn_cast:
    351 static inline bool classof(const UnionType *) { return true; }
    352 static inline bool classof(const Type *T) {
    353 return T->getTypeID() == UnionTyID;
    354 }
    355 };
    356
    357297
    358298 /// SequentialType - This is the superclass of the array, pointer and vector
    359299 /// type classes. All of these represent "arrays" in memory. The array type
    8181 IntegerTyID, ///< 8: Arbitrary bit width integers
    8282 FunctionTyID, ///< 9: Functions
    8383 StructTyID, ///< 10: Structures
    84 UnionTyID, ///< 11: Unions
    85 ArrayTyID, ///< 12: Arrays
    86 PointerTyID, ///< 13: Pointers
    87 OpaqueTyID, ///< 14: Opaque: type with unknown structure
    88 VectorTyID, ///< 15: SIMD 'packed' format, or other vector type
    84 ArrayTyID, ///< 11: Arrays
    85 PointerTyID, ///< 12: Pointers
    86 OpaqueTyID, ///< 13: Opaque: type with unknown structure
    87 VectorTyID, ///< 14: SIMD 'packed' format, or other vector type
    8988
    9089 NumTypeIDs, // Must remain as last defined ID
    9190 LastPrimitiveTyID = MetadataTyID,
    242241 ///
    243242 bool isStructTy() const { return ID == StructTyID; }
    244243
    245 /// isUnionTy - True if this is an instance of UnionType.
    246 ///
    247 bool isUnionTy() const { return ID == UnionTyID; }
    248
    249244 /// isArrayTy - True if this is an instance of ArrayType.
    250245 ///
    251246 bool isArrayTy() const { return ID == ArrayTyID; }
    305300 /// does not include vector types.
    306301 ///
    307302 inline bool isAggregateType() const {
    308 return ID == StructTyID || ID == ArrayTyID || ID == UnionTyID;
    303 return ID == StructTyID || ID == ArrayTyID;
    309304 }
    310305
    311306 /// isSized - Return true if it makes sense to take the size of this type. To
    318313 return true;
    319314 // If it is not something that can have a size (e.g. a function or label),
    320315 // it doesn't have a size.
    321 if (ID != StructTyID && ID != ArrayTyID && ID != VectorTyID &&
    322 ID != UnionTyID)
    316 if (ID != StructTyID && ID != ArrayTyID && ID != VectorTyID)
    323317 return false;
    324318 // If it is something that can have a size and it's concrete, it definitely
    325319 // has a size, otherwise we have to try harder to decide.
    214214 ConstantFPVal, // This is an instance of ConstantFP
    215215 ConstantArrayVal, // This is an instance of ConstantArray
    216216 ConstantStructVal, // This is an instance of ConstantStruct
    217 ConstantUnionVal, // This is an instance of ConstantUnion
    218217 ConstantVectorVal, // This is an instance of ConstantVector
    219218 ConstantPointerNullVal, // This is an instance of ConstantPointerNull
    220219 MDNodeVal, // This is an instance of MDNode
    203203 LLVMPointerTypeKind, /**< Pointers */
    204204 LLVMOpaqueTypeKind, /**< Opaque: type with unknown structure */
    205205 LLVMVectorTypeKind, /**< SIMD 'packed' format, or other vector type */
    206 LLVMMetadataTypeKind, /**< Metadata */
    207 LLVMUnionTypeKind /**< Unions */
    206 LLVMMetadataTypeKind /**< Metadata */
    208207 } LLVMTypeKind;
    209208
    210209 typedef enum {
    393392 unsigned LLVMCountStructElementTypes(LLVMTypeRef StructTy);
    394393 void LLVMGetStructElementTypes(LLVMTypeRef StructTy, LLVMTypeRef *Dest);
    395394 LLVMBool LLVMIsPackedStruct(LLVMTypeRef StructTy);
    396
    397 /* Operations on union types */
    398 LLVMTypeRef LLVMUnionTypeInContext(LLVMContextRef C, LLVMTypeRef *ElementTypes,
    399 unsigned ElementCount);
    400 LLVMTypeRef LLVMUnionType(LLVMTypeRef *ElementTypes, unsigned ElementCount);
    401 unsigned LLVMCountUnionElementTypes(LLVMTypeRef UnionTy);
    402 void LLVMGetUnionElementTypes(LLVMTypeRef UnionTy, LLVMTypeRef *Dest);
    403395
    404396 /* Operations on array, pointer, and vector types (sequence types) */
    405397 LLVMTypeRef LLVMArrayType(LLVMTypeRef ElementType, unsigned ElementCount);
    573565 LLVMValueRef LLVMConstStruct(LLVMValueRef *ConstantVals, unsigned Count,
    574566 LLVMBool Packed);
    575567 LLVMValueRef LLVMConstVector(LLVMValueRef *ScalarConstantVals, unsigned Size);
    576 LLVMValueRef LLVMConstUnion(LLVMTypeRef Ty, LLVMValueRef Val);
    577568
    578569 /* Constant expressions */
    579570 LLVMOpcode LLVMGetConstOpcode(LLVMValueRef ConstantVal);
    572572
    573573 KEYWORD(type);
    574574 KEYWORD(opaque);
    575 KEYWORD(union);
    576575
    577576 KEYWORD(eq); KEYWORD(ne); KEYWORD(slt); KEYWORD(sgt); KEYWORD(sle);
    578577 KEYWORD(sge); KEYWORD(ult); KEYWORD(ugt); KEYWORD(ule); KEYWORD(uge);
    13581358 if (ParseStructType(Result, false))
    13591359 return true;
    13601360 break;
    1361 case lltok::kw_union:
    1362 // TypeRec ::= 'union' '{' ... '}'
    1363 if (ParseUnionType(Result))
    1364 return true;
    1365 break;
    13661361 case lltok::lsquare:
    13671362 // TypeRec ::= '[' ... ']'
    13681363 Lex.Lex(); // eat the lsquare.
    16691664 for (unsigned i = 0, e = ParamsList.size(); i != e; ++i)
    16701665 ParamsListTy.push_back(ParamsList[i].get());
    16711666 Result = HandleUpRefs(StructType::get(Context, ParamsListTy, Packed));
    1672 return false;
    1673 }
    1674
    1675 /// ParseUnionType
    1676 /// TypeRec
    1677 /// ::= 'union' '{' TypeRec (',' TypeRec)* '}'
    1678 bool LLParser::ParseUnionType(PATypeHolder &Result) {
    1679 assert(Lex.getKind() == lltok::kw_union);
    1680 Lex.Lex(); // Consume the 'union'
    1681
    1682 if (ParseToken(lltok::lbrace, "'{' expected after 'union'")) return true;
    1683
    1684 SmallVector ParamsList;
    1685 do {
    1686 LocTy EltTyLoc = Lex.getLoc();
    1687 if (ParseTypeRec(Result)) return true;
    1688 ParamsList.push_back(Result);
    1689
    1690 if (Result->isVoidTy())
    1691 return Error(EltTyLoc, "union element can not have void type");
    1692 if (!UnionType::isValidElementType(Result))
    1693 return Error(EltTyLoc, "invalid element type for union");
    1694
    1695 } while (EatIfPresent(lltok::comma)) ;
    1696
    1697 if (ParseToken(lltok::rbrace, "expected '}' at end of union"))
    1698 return true;
    1699
    1700 SmallVector ParamsListTy;
    1701 for (unsigned i = 0, e = ParamsList.size(); i != e; ++i)
    1702 ParamsListTy.push_back(ParamsList[i].get());
    1703 Result = HandleUpRefs(UnionType::get(&ParamsListTy[0], ParamsListTy.size()));
    17041667 return false;
    17051668 }
    17061669
    26552618 V = Constant::getNullValue(Ty);
    26562619 return false;
    26572620 case ValID::t_Constant:
    2658 if (ID.ConstantVal->getType() != Ty) {
    2659 // Allow a constant struct with a single member to be converted
    2660 // to a union, if the union has a member which is the same type
    2661 // as the struct member.
    2662 if (const UnionType* utype = dyn_cast(Ty)) {
    2663 return ParseUnionValue(utype, ID, V);
    2664 }
    2665
    2621 if (ID.ConstantVal->getType() != Ty)
    26662622 return Error(ID.Loc, "constant expression type mismatch");
    2667 }
    26682623
    26692624 V = ID.ConstantVal;
    26702625 return false;
    26932648 return Error(Loc, "expected a basic block");
    26942649 BB = cast(V);
    26952650 return false;
    2696 }
    2697
    2698 bool LLParser::ParseUnionValue(const UnionType* utype, ValID &ID, Value *&V) {
    2699 if (const StructType* stype = dyn_cast(ID.ConstantVal->getType())) {
    2700 if (stype->getNumContainedTypes() != 1)
    2701 return Error(ID.Loc, "constant expression type mismatch");
    2702 int index = utype->getElementTypeIndex(stype->getContainedType(0));
    2703 if (index < 0)
    2704 return Error(ID.Loc, "initializer type is not a member of the union");
    2705
    2706 V = ConstantUnion::get(
    2707 utype, cast(ID.ConstantVal->getOperand(0)));
    2708 return false;
    2709 }
    2710
    2711 return Error(ID.Loc, "constant expression type mismatch");
    27122651 }
    27132652
    27142653
    3131 class GlobalValue;
    3232 class MDString;
    3333 class MDNode;
    34 class UnionType;
    3534
    3635 /// ValID - Represents a reference of a definition of some sort with no type.
    3736 /// There are several cases where we have to parse the value but where the
    228227 }
    229228 bool ParseTypeRec(PATypeHolder &H);
    230229 bool ParseStructType(PATypeHolder &H, bool Packed);
    231 bool ParseUnionType(PATypeHolder &H);
    232230 bool ParseArrayVectorType(PATypeHolder &H, bool isVector);
    233231 bool ParseFunctionType(PATypeHolder &Result);
    234232 PATypeHolder HandleUpRefs(const Type *Ty);
    297295 return ParseTypeAndBasicBlock(BB, Loc, PFS);
    298296 }
    299297
    300 bool ParseUnionValue(const UnionType* utype, ValID &ID, Value *&V);
    301298
    302299 struct ParamInfo {
    303300 LocTy Loc;
    9797
    9898 kw_type,
    9999 kw_opaque,
    100 kw_union,
    101100
    102101 kw_eq, kw_ne, kw_slt, kw_sgt, kw_sle, kw_sge, kw_ult, kw_ugt, kw_ule,
    103102 kw_uge, kw_oeq, kw_one, kw_olt, kw_ogt, kw_ole, kw_oge, kw_ord, kw_uno,
    296296 } else if (ConstantStruct *UserCS = dyn_cast(UserC)) {
    297297 NewC = ConstantStruct::get(Context, &NewOps[0], NewOps.size(),
    298298 UserCS->getType()->isPacked());
    299 } else if (ConstantUnion *UserCU = dyn_cast(UserC)) {
    300 NewC = ConstantUnion::get(UserCU->getType(), NewOps[0]);
    301299 } else if (isa(UserC)) {
    302300 NewC = ConstantVector::get(&NewOps[0], NewOps.size());
    303301 } else {
    588586 for (unsigned i = 1, e = Record.size(); i != e; ++i)
    589587 EltTys.push_back(getTypeByID(Record[i], true));
    590588 ResultTy = StructType::get(Context, EltTys, Record[0]);
    591 break;
    592 }
    593 case bitc::TYPE_CODE_UNION: { // UNION: [eltty x N]
    594 SmallVector EltTys;
    595 for (unsigned i = 0, e = Record.size(); i != e; ++i)
    596 EltTys.push_back(getTypeByID(Record[i], true));
    597 ResultTy = UnionType::get(&EltTys[0], EltTys.size());
    598589 break;
    599590 }
    600591 case bitc::TYPE_CODE_ARRAY: // ARRAY: [numelts, eltty]
    10131004 Elts.push_back(ValueList.getConstantFwdRef(Record[i],
    10141005 STy->getElementType(i)));
    10151006 V = ConstantStruct::get(STy, Elts);
    1016 } else if (const UnionType *UnTy = dyn_cast(CurTy)) {
    1017 uint64_t Index = Record[0];
    1018 Constant *Val = ValueList.getConstantFwdRef(Record[1],
    1019 UnTy->getElementType(Index));
    1020 V = ConstantUnion::get(UnTy, Val);
    10211007 } else if (const ArrayType *ATy = dyn_cast(CurTy)) {
    10221008 const Type *EltTy = ATy->getElementType();
    10231009 for (unsigned i = 0; i != Size; ++i)
    179179 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
    180180 Log2_32_Ceil(VE.getTypes().size()+1)));
    181181 unsigned StructAbbrev = Stream.EmitAbbrev(Abbv);
    182
    183 // Abbrev for TYPE_CODE_UNION.
    184 Abbv = new BitCodeAbbrev();
    185 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_UNION));
    186 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
    187 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
    188 Log2_32_Ceil(VE.getTypes().size()+1)));
    189 unsigned UnionAbbrev = Stream.EmitAbbrev(Abbv);
    190182
    191183 // Abbrev for TYPE_CODE_ARRAY.
    192184 Abbv = new BitCodeAbbrev();
    255247 E = ST->element_end(); I != E; ++I)
    256248 TypeVals.push_back(VE.getTypeID(*I));
    257249 AbbrevToUse = StructAbbrev;
    258 break;
    259 }
    260 case Type::UnionTyID: {
    261 const UnionType *UT = cast(T);
    262 // UNION: [eltty x N]
    263 Code = bitc::TYPE_CODE_UNION;
    264 // Output all of the element types.
    265 for (UnionType::element_iterator I = UT->element_begin(),
    266 E = UT->element_end(); I != E; ++I)
    267 TypeVals.push_back(VE.getTypeID(*I));
    268 AbbrevToUse = UnionAbbrev;
    269250 break;
    270251 }
    271252 case Type::ArrayTyID: {
    809790 Code = bitc::CST_CODE_AGGREGATE;
    810791 for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i)
    811792 Record.push_back(VE.getValueID(C->getOperand(i)));
    812 AbbrevToUse = AggregateAbbrev;
    813 } else if (isa(C)) {
    814 Code = bitc::CST_CODE_AGGREGATE;
    815
    816 // Unions only have one entry but we must send type along with it.
    817 const Type *EntryKind = C->getOperand(0)->getType();
    818
    819 const UnionType *UnTy = cast(C->getType());
    820 int UnionIndex = UnTy->getElementTypeIndex(EntryKind);
    821 assert(UnionIndex != -1 && "Constant union contains invalid entry");
    822
    823 Record.push_back(UnionIndex);
    824 Record.push_back(VE.getValueID(C->getOperand(0)));
    825
    826793 AbbrevToUse = AggregateAbbrev;
    827794 } else if (const ConstantExpr *CE = dyn_cast(C)) {
    828795 switch (CE->getOpcode()) {
    14321432 "Layout of constant struct may be incorrect!");
    14331433 }
    14341434
    1435 static void EmitGlobalConstantUnion(const ConstantUnion *CU,
    1436 unsigned AddrSpace, AsmPrinter &AP) {
    1437 const TargetData *TD = AP.TM.getTargetData();
    1438 unsigned Size = TD->getTypeAllocSize(CU->getType());
    1439
    1440 const Constant *Contents = CU->getOperand(0);
    1441 unsigned FilledSize = TD->getTypeAllocSize(Contents->getType());
    1442
    1443 // Print the actually filled part
    1444 EmitGlobalConstantImpl(Contents, AddrSpace, AP);
    1445
    1446 // And pad with enough zeroes
    1447 AP.OutStreamer.EmitZeros(Size-FilledSize, AddrSpace);
    1448 }
    1449
    14501435 static void EmitGlobalConstantFP(const ConstantFP *CFP, unsigned AddrSpace,
    14511436 AsmPrinter &AP) {
    14521437 // FP Constants are printed as integer constants to avoid losing
    15721557 return;
    15731558 }
    15741559
    1575 if (const ConstantUnion *CVU = dyn_cast(CV))
    1576 return EmitGlobalConstantUnion(CVU, AddrSpace, AP);
    1577
    15781560 if (const ConstantVector *V = dyn_cast(CV))
    15791561 return EmitGlobalConstantVector(V, AddrSpace, AP);
    15801562
    28032803 }
    28042804
    28052805 Ty = StTy->getElementType(Field);
    2806 } else if (const UnionType *UnTy = dyn_cast(Ty)) {
    2807 unsigned Field = cast(Idx)->getZExtValue();
    2808
    2809 // Offset canonically 0 for unions, but type changes
    2810 Ty = UnTy->getElementType(Field);
    28112806 } else {
    28122807 Ty = cast(Ty)->getElementType();
    28132808
    453453 case Type::StructTyID:
    454454 // Get the layout annotation... which is lazily created on demand.
    455455 return getStructLayout(cast(Ty))->getSizeInBits();
    456 case Type::UnionTyID: {
    457 const UnionType *UnTy = cast(Ty);
    458 uint64_t Size = 0;
    459 for (UnionType::element_iterator i = UnTy->element_begin(),
    460 e = UnTy->element_end(); i != e; ++i) {
    461 Size = std::max(Size, getTypeSizeInBits(*i));
    462 }
    463 return Size;
    464 }
    465456 case Type::IntegerTyID:
    466457 return cast(Ty)->getBitWidth();
    467458 case Type::VoidTyID:
    518509 unsigned Align = getAlignmentInfo(AGGREGATE_ALIGN, 0, abi_or_pref, Ty);
    519510 return std::max(Align, Layout->getAlignment());
    520511 }
    521 case Type::UnionTyID: {
    522 const UnionType *UnTy = cast(Ty);
    523 unsigned Align = 1;
    524
    525 // Unions need the maximum alignment of all their entries
    526 for (UnionType::element_iterator i = UnTy->element_begin(),
    527 e = UnTy->element_end(); i != e; ++i) {
    528 Align = std::max(Align, getAlignment(*i, abi_or_pref));
    529 }
    530 return Align;
    531 }
    532512 case Type::IntegerTyID:
    533513 case Type::VoidTyID:
    534514 AlignType = INTEGER_ALIGN;
    613593
    614594 // Update Ty to refer to current element
    615595 Ty = STy->getElementType(FieldNo);
    616 } else if (const UnionType *UnTy = dyn_cast(*TI)) {
    617 unsigned FieldNo = cast(Indices[CurIDX])->getZExtValue();
    618
    619 // Offset into union is canonically 0, but type changes
    620 Ty = UnTy->getElementType(FieldNo);
    621596 } else {
    622597 // Update Ty to refer to current element
    623598 Ty = cast(Ty)->getElementType();
    219219 return true;
    220220 }
    221221
    222 case Type::UnionTyID: {
    223 const UnionType *UTy1 = cast(Ty1);
    224 const UnionType *UTy2 = cast(Ty2);
    225
    226 if (UTy1->getNumElements() != UTy2->getNumElements())
    227 return false;
    228
    229 for (unsigned i = 0, e = UTy1->getNumElements(); i != e; ++i) {
    230 if (!isEquivalentType(UTy1->getElementType(i), UTy2->getElementType(i)))
    231 return false;
    232 }
    233 return true;
    234 }
    235
    236222 case Type::FunctionTyID: {
    237223 const FunctionType *FTy1 = cast(Ty1);
    238224 const FunctionType *FTy2 = cast(Ty2);
    235235 OS << '}';
    236236 if (STy->isPacked())
    237237 OS << '>';
    238 break;
    239 }
    240 case Type::UnionTyID: {
    241 const UnionType *UTy = cast(Ty);
    242 OS << "union {";
    243 for (StructType::element_iterator I = UTy->element_begin(),
    244 E = UTy->element_end(); I != E; ++I) {
    245 OS << ' ';
    246 CalcTypeName(*I, TypeStack, OS);
    247 if (llvm::next(I) == UTy->element_end())
    248 OS << ' ';
    249 else
    250 OS << ',';
    251 }
    252 OS << '}';
    253238 break;
    254239 }
    255240 case Type::PointerTyID: {
    10411026 return;
    10421027 }
    10431028
    1044 if (const ConstantUnion *CU = dyn_cast(CV)) {
    1045 Out << "{ ";
    1046 TypePrinter.print(CU->getOperand(0)->getType(), Out);
    1047 Out << ' ';
    1048 WriteAsOperandInternal(Out, CU->getOperand(0), &TypePrinter, Machine,
    1049 Context);
    1050 Out << " }";
    1051 return;
    1052 }
    1053
    10541029 if (const ConstantVector *CP = dyn_cast(CV)) {
    10551030 const Type *ETy = CP->getType()->getElementType();
    10561031 assert(CP->getNumOperands() > 0 &&
    356356 }
    357357 }
    358358
    359 if (const UnionType *UTy = dyn_cast(Ty)) {
    360 unsigned NumElems = UTy->getNumElements();
    361 // Check for a union with all members having the same size.
    362 Constant *MemberSize =
    363 getFoldedSizeOf(UTy->getElementType(0), DestTy, true);
    364 bool AllSame = true;
    365 for (unsigned i = 1; i != NumElems; ++i)
    366 if (MemberSize !=
    367 getFoldedSizeOf(UTy->getElementType(i), DestTy, true)) {
    368 AllSame = false;
    369 break;
    370 }
    371 if (AllSame)
    372 return MemberSize;
    373 }
    374
    375359 // Pointer size doesn't depend on the pointee type, so canonicalize them
    376360 // to an arbitrary pointee.
    377361 if (const PointerType *PTy = dyn_cast(Ty))
    430414 bool AllSame = true;
    431415 for (unsigned i = 1; i != NumElems; ++i)
    432416 if (MemberAlign != getFoldedAlignOf(STy->getElementType(i), DestTy, true)) {
    433 AllSame = false;
    434 break;
    435 }
    436 if (AllSame)
    437 return MemberAlign;
    438 }
    439
    440 if (const UnionType *UTy = dyn_cast(Ty)) {
    441 // Union alignment is the maximum alignment of any member.
    442 // Without target data, we can't compare much, but we can check to see
    443 // if all the members have the same alignment.
    444 unsigned NumElems = UTy->getNumElements();
    445 // Check for a union with all members having the same alignment.
    446 Constant *MemberAlign =
    447 getFoldedAlignOf(UTy->getElementType(0), DestTy, true);
    448 bool AllSame = true;
    449 for (unsigned i = 1; i != NumElems; ++i)
    450 if (MemberAlign != getFoldedAlignOf(UTy->getElementType(i), DestTy, true)) {
    451417 AllSame = false;
    452418 break;
    453419 }
    908874 unsigned numOps;
    909875 if (const ArrayType *AR = dyn_cast(AggTy))
    910876 numOps = AR->getNumElements();
    911 else if (AggTy->isUnionTy())
    912 numOps = 1;
    913877 else
    914878 numOps = cast(AggTy)->getNumElements();
    915879
    926890
    927891 if (const StructType* ST = dyn_cast(AggTy))
    928892 return ConstantStruct::get(ST->getContext(), Ops, ST->isPacked());
    929 if (const UnionType* UT = dyn_cast(AggTy)) {
    930 assert(Ops.size() == 1 && "Union can only contain a single value!");
    931 return ConstantUnion::get(UT, Ops[0]);
    932 }
    933893 return ConstantArray::get(cast(AggTy), Ops);
    934894 }
    935895
    5858 case Type::PointerTyID:
    5959 return ConstantPointerNull::get(cast(Ty));
    6060 case Type::StructTyID:
    61 case Type::UnionTyID:
    6261 case Type::ArrayTyID:
    6362 case Type::VectorTyID:
    6463 return ConstantAggregateZero::get(Ty);
    585584 // FIXME: make this the primary ctor method.
    586585 return get(Context, std::vector(Vals, Vals+NumVals), Packed);
    587586 }
    588
    589 ConstantUnion::ConstantUnion(const UnionType *T, Constant* V)
    590 : Constant(T, ConstantUnionVal,
    591 OperandTraits::op_end(this) - 1, 1) {
    592 Use *OL = OperandList;
    593 assert(T->getElementTypeIndex(V->getType()) >= 0 &&
    594 "Initializer for union element isn't a member of union type!");
    595 *OL = V;
    596 }
    597
    598 // ConstantUnion accessors.
    599 Constant* ConstantUnion::get(const UnionType* T, Constant* V) {
    600 LLVMContextImpl* pImpl = T->getContext().pImpl;
    601
    602 // Create a ConstantAggregateZero value if all elements are zeros...
    603 if (!V->isNullValue())
    604 return pImpl->UnionConstants.getOrCreate(T, V);
    605
    606 return ConstantAggregateZero::get(T);
    607 }
    608
    609587
    610588 ConstantVector::ConstantVector(const VectorType *T,
    611589 const std::vector &V)
    945923 // Factory Function Implementation
    946924
    947925 ConstantAggregateZero* ConstantAggregateZero::get(const Type* Ty) {
    948 assert((Ty->isStructTy() || Ty->isUnionTy()
    949 || Ty->isArrayTy() || Ty->isVectorTy()) &&
    926 assert((Ty->isStructTy() || Ty->isArrayTy() || Ty->isVectorTy()) &&
    950927 "Cannot create an aggregate zero of non-aggregate type!");
    951928
    952929 LLVMContextImpl *pImpl = Ty->getContext().pImpl;
    10281005 //
    10291006 void ConstantStruct::destroyConstant() {
    10301007 getRawType()->getContext().pImpl->StructConstants.remove(this);
    1031 destroyConstantImpl();
    1032 }
    1033
    1034 // destroyConstant - Remove the constant from the constant table...
    1035 //
    1036 void ConstantUnion::destroyConstant() {
    1037 getRawType()->getContext().pImpl->UnionConstants.remove(this);
    10381008 destroyConstantImpl();
    10391009 }
    10401010
    21032073
    21042074 // Update to the new value.
    21052075 setOperand(OperandToUpdate, ToC);
    2106 return;
    2107 }
    2108 }
    2109
    2110 assert(Replacement != this && "I didn't contain From!");
    2111
    2112 // Everyone using this now uses the replacement.
    2113 uncheckedReplaceAllUsesWith(Replacement);
    2114
    2115 // Delete the old constant!
    2116 destroyConstant();
    2117 }
    2118
    2119 void ConstantUnion::replaceUsesOfWithOnConstant(Value *From, Value *To,
    2120 Use *U) {
    2121 assert(isa(To) && "Cannot make Constant refer to non-constant!");
    2122 Constant *ToC = cast(To);
    2123
    2124 assert(U == OperandList && "Union constants can only have one use!");
    2125 assert(getNumOperands() == 1 && "Union constants can only have one use!");
    2126 assert(getOperand(0) == From && "ReplaceAllUsesWith broken!");
    2127
    2128 std::pair Lookup;
    2129 Lookup.first.first = cast(getRawType());
    2130 Lookup.second = this;
    2131 Lookup.first.second = ToC;
    2132
    2133 LLVMContextImpl *pImpl = getRawType()->getContext().pImpl;
    2134
    2135 Constant *Replacement = 0;
    2136 if (ToC->isNullValue()) {
    2137 Replacement = ConstantAggregateZero::get(getRawType());
    2138 } else {
    2139 // Check to see if we have this union type already.
    2140 bool Exists;
    2141 LLVMContextImpl::UnionConstantsTy::MapTy::iterator I =
    2142 pImpl->UnionConstants.InsertOrGetItem(Lookup, Exists);
    2143
    2144 if (Exists) {
    2145 Replacement = I->second;
    2146 } else {
    2147 // Okay, the new shape doesn't exist in the system yet. Instead of
    2148 // creating a new constant union, inserting it, replaceallusesof'ing the
    2149 // old with the new, then deleting the old... just update the current one
    2150 // in place!
    2151 pImpl->UnionConstants.MoveConstantToNewSlot(this, I);
    2152
    2153 // Update to the new value.
    2154 setOperand(0, ToC);
    21552076 return;
    21562077 }
    21572078 }
    507507 for (unsigned i = 0, e = CS->getNumOperands(); i != e; ++i)
    508508 Elements.push_back(cast(CS->getOperand(i)));
    509509 return Elements;
    510 }
    511 };
    512
    513 template<>
    514 struct ConstantKeyData {
    515 typedef Constant* ValType;
    516 static ValType getValType(ConstantUnion *CU) {
    517 return cast(CU->getOperand(0));
    518510 }
    519511 };
    520512
    155155 return LLVMFunctionTypeKind;
    156156 case Type::StructTyID:
    157157 return LLVMStructTypeKind;
    158 case Type::UnionTyID:
    159 return LLVMUnionTypeKind;
    160158 case Type::ArrayTyID:
    161159 return LLVMArrayTypeKind;
    162160 case Type::PointerTyID:
    315313 return unwrap(StructTy)->isPacked();
    316314 }
    317315
    318 /*--.. Operations on union types ..........................................--*/
    319
    320 LLVMTypeRef LLVMUnionTypeInContext(LLVMContextRef C, LLVMTypeRef *ElementTypes,
    321 unsigned ElementCount) {
    322 SmallVector Tys;
    323 for (LLVMTypeRef *I = ElementTypes,
    324 *E = ElementTypes + ElementCount; I != E; ++I)
    325 Tys.push_back(unwrap(*I));
    326
    327 return wrap(UnionType::get(&Tys[0], Tys.size()));
    328 }
    329
    330 LLVMTypeRef LLVMUnionType(LLVMTypeRef *ElementTypes, unsigned ElementCount) {
    331 return LLVMUnionTypeInContext(LLVMGetGlobalContext(), ElementTypes,
    332 ElementCount);
    333 }
    334
    335 unsigned LLVMCountUnionElementTypes(LLVMTypeRef UnionTy) {
    336 return unwrap(UnionTy)->getNumElements();
    337 }
    338
    339 void LLVMGetUnionElementTypes(LLVMTypeRef UnionTy, LLVMTypeRef *Dest) {
    340 UnionType *Ty = unwrap(UnionTy);
    341 for (FunctionType::param_iterator I = Ty->element_begin(),
    342 E = Ty->element_end(); I != E; ++I)
    343 *Dest++ = wrap(*I);
    344 }
    345
    346316 /*--.. Operations on array, pointer, and vector types (sequence types) .....--*/
    347317
    348318 LLVMTypeRef LLVMArrayType(LLVMTypeRef ElementType, unsigned ElementCount) {
    627597 return wrap(ConstantVector::get(
    628598 unwrap(ScalarConstantVals, Size), Size));
    629599 }
    630 LLVMValueRef LLVMConstUnion(LLVMTypeRef Ty, LLVMValueRef Val) {
    631 return wrap(ConstantUnion::get(unwrap(Ty), unwrap(Val)));
    632 }
    633
    634600 /*--.. Constant expressions ................................................--*/
    635601
    636602 LLVMOpcode LLVMGetConstOpcode(LLVMValueRef ConstantVal) {
    5656 DropReferences());
    5757 std::for_each(StructConstants.map_begin(), StructConstants.map_end(),
    5858 DropReferences());
    59 std::for_each(UnionConstants.map_begin(), UnionConstants.map_end(),
    60 DropReferences());
    6159 std::for_each(VectorConstants.map_begin(), VectorConstants.map_end(),
    6260 DropReferences());
    6361 ExprConstants.freeConstants();
    6462 ArrayConstants.freeConstants();
    6563 StructConstants.freeConstants();
    66 UnionConstants.freeConstants();
    6764 VectorConstants.freeConstants();
    6865 AggZeroConstants.freeConstants();
    6966 NullPtrConstants.freeConstants();
    143143 ConstantStruct, true /*largekey*/> StructConstantsTy;
    144144 StructConstantsTy StructConstants;
    145145
    146 typedef ConstantUniqueMap
    147 UnionConstantsTy;
    148 UnionConstantsTy UnionConstants;
    149
    150146 typedef ConstantUniqueMap, VectorType,
    151147 ConstantVector> VectorConstantsTy;
    152148 VectorConstantsTy VectorConstants;
    191187 TypeMap PointerTypes;
    192188 TypeMap FunctionTypes;
    193189 TypeMap StructTypes;
    194 TypeMap UnionTypes;
    195190 TypeMap IntegerTypes;
    196191
    197192 // Opaque types are not structurally uniqued, so don't use TypeMap.
    4949
    5050 /// Because of the way Type subclasses are allocated, this function is necessary
    5151 /// to use the correct kind of "delete" operator to deallocate the Type object.
    52 /// Some type objects (FunctionTy, StructTy, UnionTy) allocate additional space
    52 /// Some type objects (FunctionTy, StructTy) allocate additional space
    5353 /// after the space for their derived type to hold the contained types array of
    5454 /// PATypeHandles. Using this allocation scheme means all the PATypeHandles are
    5555 /// allocated with the type object, decreasing allocations and eliminating the
    6565 // Structures and Functions allocate their contained types past the end of
    6666 // the type object itself. These need to be destroyed differently than the
    6767 // other types.
    68 if (this->isFunctionTy() || this->isStructTy() ||
    69 this->isUnionTy()) {
    68 if (this->isFunctionTy() || this->isStructTy()) {
    7069 // First, make sure we destruct any PATypeHandles allocated by these
    7170 // subclasses. They must be manually destructed.
    7271 for (unsigned i = 0; i < NumContainedTys; ++i)
    7675 // to delete this as an array of char.
    7776 if (this->isFunctionTy())
    7877 static_cast(this)->FunctionType::~FunctionType();
    79 else if (this->isStructTy())
    78 else {
    79 assert(isStructTy());
    8080 static_cast(this)->StructType::~StructType();
    81 else
    82 static_cast(this)->UnionType::~UnionType();
    81 }
    8382
    8483 // Finally, remove the memory as an array deallocation of the chars it was
    8584 // constructed from.
    233232 if (const VectorType *PTy = dyn_cast(this))
    234233 return PTy->getElementType()->isSized();
    235234
    236 if (!this->isStructTy() && !this->isUnionTy())
    235 if (!this->isStructTy())
    237236 return false;
    238237
    239238 // Okay, our struct is sized if all of the elements are...
    317316 return ContainedTys[Idx];
    318317 }
    319318
    320
    321 bool UnionType::indexValid(const Value *V) const {
    322 // Union indexes require 32-bit integer constants.
    323 if (V->getType()->isIntegerTy(32))
    324 if (const ConstantInt *CU = dyn_cast(V))
    325 return indexValid(CU->getZExtValue());
    326 return false;
    327 }
    328
    329 bool UnionType::indexValid(unsigned V) const {
    330 return V < NumContainedTys;
    331 }
    332
    333 // getTypeAtIndex - Given an index value into the type, return the type of the
    334 // element. For a structure type, this must be a constant value...
    335 //
    336 const Type *UnionType::getTypeAtIndex(const Value *V) const {
    337 unsigned Idx = (unsigned)cast(V)->getZExtValue();
    338 return getTypeAtIndex(Idx);
    339 }
    340
    341 const Type *UnionType::getTypeAtIndex(unsigned Idx) const {
    342 assert(indexValid(Idx) && "Invalid structure index!");
    343 return ContainedTys[Idx];
    344 }
    345319
    346320 //===----------------------------------------------------------------------===//
    347321 // Primitive 'Type' data
    506480 setAbstract(isAbstract);
    507481 }
    508482
    509 UnionType::UnionType(LLVMContext &C,const Type* const* Types, unsigned NumTypes)
    510 : CompositeType(C, UnionTyID) {
    511 ContainedTys = reinterpret_cast(this + 1);
    512 NumContainedTys = NumTypes;
    513 bool isAbstract = false;
    514 for (unsigned i = 0; i < NumTypes; ++i) {
    515 assert(Types[i] && " type for union field!");
    516 assert(isValidElementType(Types[i]) &&
    517 "Invalid type for union element!");
    518 new (&ContainedTys[i]) PATypeHandle(Types[i], this);
    519 isAbstract |= Types[i]->isAbstract();
    520 }
    521
    522 // Calculate whether or not this type is abstract
    523 setAbstract(isAbstract);
    524 }
    525
    526483 ArrayType::ArrayType(const Type *ElType, uint64_t NumEl)
    527484 : SequentialType(ArrayTyID, ElType) {
    528485 NumElements = NumEl;
    710667 return true;
    711668 }
    712669
    713 if (const UnionType *UTy = dyn_cast(Ty)) {
    714 const UnionType *UTy2 = cast(Ty2);
    715 if (UTy->getNumElements() != UTy2->getNumElements()) return false;
    716 for (unsigned i = 0, e = UTy2->getNumElements(); i != e; ++i)
    717 if (!TypesEqual(UTy->getElementType(i), UTy2->getElementType(i), EqTypes))
    718 return false;
    719 return true;
    720 }
    721
    722670 if (const ArrayType *ATy = dyn_cast(Ty)) {
    723671 const ArrayType *ATy2 = cast(Ty2);
    724672 return ATy->getNumElements() == ATy2->getNumElements() &&
    986934
    987935
    988936 //===----------------------------------------------------------------------===//
    989 // Union Type Factory...
    990 //
    991
    992 UnionType *UnionType::get(const Type* const* Types, unsigned NumTypes) {
    993 assert(NumTypes > 0 && "union must have at least one member type!");
    994 UnionValType UTV(Types, NumTypes);
    995 UnionType *UT = 0;
    996
    997 LLVMContextImpl *pImpl = Types[0]->getContext().pImpl;
    998
    999 UT = pImpl->UnionTypes.get(UTV);
    1000
    1001 if (!UT) {
    1002 // Value not found. Derive a new type!
    1003 UT = (UnionType*) operator new(sizeof(UnionType) +
    1004 sizeof(PATypeHandle) * NumTypes);
    1005 new (UT) UnionType(Types[0]->getContext(), Types, NumTypes);
    1006 pImpl->UnionTypes.add(UTV, UT);
    1007 }
    1008 #ifdef DEBUG_MERGE_TYPES
    1009 DEBUG(dbgs() << "Derived new type: " << *UT << "\n");
    1010 #endif
    1011 return UT;
    1012 }
    1013
    1014 UnionType *UnionType::get(const Type *type, ...) {
    1015 va_list ap;
    1016 SmallVector UnionFields;
    1017 va_start(ap, type);
    1018 while (type) {
    1019 UnionFields.push_back(type);
    1020 type = va_arg(ap, llvm::Type*);
    1021 }
    1022 unsigned NumTypes = UnionFields.size();
    1023 assert(NumTypes > 0 && "union must have at least one member type!");
    1024 return llvm::UnionType::get(&UnionFields[0], NumTypes);
    1025 }
    1026
    1027 bool UnionType::isValidElementType(const Type *ElemTy) {
    1028 return !ElemTy->isVoidTy() && !ElemTy->isLabelTy() &&
    1029 !ElemTy->isMetadataTy() && !ElemTy->isFunctionTy();
    1030 }
    1031
    1032 int UnionType::getElementTypeIndex(const Type *ElemTy) const {
    1033 int index = 0;
    1034 for (UnionType::element_iterator I = element_begin(), E = element_end();
    1035 I != E; ++I, ++index) {
    1036 if (ElemTy == *I) return index;
    1037 }
    1038
    1039 return -1;
    1040 }
    1041
    1042 //===----------------------------------------------------------------------===//
    1043937 // Pointer Type Factory...
    1044938 //
    1045939
    12901184 // concrete - this could potentially change us from an abstract type to a
    12911185 // concrete type.
    12921186 //
    1293 void UnionType::refineAbstractType(const DerivedType *OldType,
    1294 const Type *NewType) {
    1295 LLVMContextImpl *pImpl = OldType->getContext().pImpl;
    1296 pImpl->UnionTypes.RefineAbstractType(this, OldType, NewType);
    1297 }
    1298
    1299 void UnionType::typeBecameConcrete(const DerivedType *AbsTy) {
    1300 LLVMContextImpl *pImpl = AbsTy->getContext().pImpl;
    1301 pImpl->UnionTypes.TypeBecameConcrete(this, AbsTy);
    1302 }
    1303
    1304 // refineAbstractType - Called when a contained type is found to be more
    1305 // concrete - this could potentially change us from an abstract type to a
    1306 // concrete type.
    1307 //
    13081187 void PointerType::refineAbstractType(const DerivedType *OldType,
    13091188 const Type *NewType) {
    13101189 LLVMContextImpl *pImpl = OldType->getContext().pImpl;
    176176 if (ElTypes < STV.ElTypes) return true;
    177177 else if (ElTypes > STV.ElTypes) return false;
    178178 else return (int)packed < (int)STV.packed;
    179 }
    180 };
    181
    182 // UnionValType - Define a class to hold the key that goes into the TypeMap
    183 //
    184 class UnionValType {
    185 std::vector ElTypes;
    186 public:
    187 UnionValType(const Type* const* Types, unsigned NumTypes)
    188 : ElTypes(&Types[0], &Types[NumTypes]) {}
    189
    190 static UnionValType get(const UnionType *UT) {
    191 std::vector ElTypes;
    192 ElTypes.reserve(UT->getNumElements());
    193 for (unsigned i = 0, e = UT->getNumElements(); i != e; ++i)
    194 ElTypes.push_back(UT->getElementType(i));
    195
    196 return UnionValType(&ElTypes[0], ElTypes.size());
    197 }
    198
    199 static unsigned hashTypeStructure(const UnionType *UT) {
    200 return UT->getNumElements();
    201 }
    202
    203 inline bool operator<(const UnionValType &UTV) const {
    204 return (ElTypes < UTV.ElTypes);
    205179 }
    206180 };
    207181
    15591559 "Function type with invalid parameter type", ElTy, FTy);
    15601560 VerifyType(ElTy);
    15611561 }
    1562 } break;
    1562 break;
    1563 }
    15631564 case Type::StructTyID: {
    15641565 const StructType *STy = cast(Ty);
    15651566 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
    15681569 "Structure type with invalid element type", ElTy, STy);
    15691570 VerifyType(ElTy);
    15701571 }
    1571 } break;
    1572 case Type::UnionTyID: {
    1573 const UnionType *UTy = cast(Ty);
    1574 for (unsigned i = 0, e = UTy->getNumElements(); i != e; ++i) {
    1575 const Type *ElTy = UTy->getElementType(i);
    1576 Assert2(UnionType::isValidElementType(ElTy),
    1577 "Union type with invalid element type", ElTy, UTy);
    1578 VerifyType(ElTy);
    1579 }
    1580 } break;
    1572 break;
    1573 }
    15811574 case Type::ArrayTyID: {
    15821575 const ArrayType *ATy = cast(Ty);
    15831576 Assert1(ArrayType::isValidElementType(ATy->getElementType()),
    15841577 "Array type with invalid element type", ATy);
    15851578 VerifyType(ATy->getElementType());
    1586 } break;
    1579 break;
    1580 }
    15871581 case Type::PointerTyID: {
    15881582 const PointerType *PTy = cast(Ty);
    15891583 Assert1(PointerType::isValidElementType(PTy->getElementType()),
    15901584 "Pointer type with invalid element type", PTy);
    15911585 VerifyType(PTy->getElementType());
    1592 } break;
    1586 break;
    1587 }
    15931588 case Type::VectorTyID: {
    15941589 const VectorType *VTy = cast(Ty);
    15951590 Assert1(VectorType::isValidElementType(VTy->getElementType()),
    15961591 "Vector type with invalid element type", VTy);
    15971592 VerifyType(VTy->getElementType());
    1598 } break;
    1593 break;
    1594 }
    15991595 default:
    16001596 break;
    16011597 }
    +0
    -3
    test/Assembler/2010-01-06-UnionType.ll less more
    None ; RUN: llvm-as %s -o /dev/null
    1
    2 %X = type union { i32, i32* }
    +0
    -14
    test/Feature/unions.ll less more
    None ; RUN: llvm-as < %s | llvm-dis > %t1.ll
    1 ; RUN: llvm-as %t1.ll -o - | llvm-dis > %t2.ll
    2 ; RUN: diff %t1.ll %t2.ll
    3
    4 %union.anon = type union { i8, i32, float }
    5
    6 @union1 = constant union { i32, i8 } { i32 4 }
    7 @union2 = constant union { i32, i8 } insertvalue(union { i32, i8 } undef, i32 4, 0)
    8 @union3 = common global %union.anon zeroinitializer, align 8
    9
    10 define void @"Unions" () {
    11 ret void
    12 }
    13
    7070 ; PLAIN: @g = constant i64 ptrtoint (double* getelementptr (%0* null, i64 0, i32 1) to i64)
    7171 ; PLAIN: @h = constant i64 ptrtoint (i1** getelementptr (i1** null, i32 1) to i64)
    7272 ; PLAIN: @i = constant i64 ptrtoint (i1** getelementptr (%2* null, i64 0, i32 1) to i64)
    73 ; PLAIN: @j = constant i64 ptrtoint (double* getelementptr (%0* null, i64 0, i32 1) to i64)
    74 ; PLAIN: @k = constant i64 ptrtoint (double* getelementptr (double* null, i32 1) to i64)
    7573 ; OPT: @a = constant i64 mul (i64 ptrtoint (double* getelementptr (double* null, i32 1) to i64), i64 2310)
    7674 ; OPT: @b = constant i64 ptrtoint (double* getelementptr (%0* null, i64 0, i32 1) to i64)
    7775 ; OPT: @c = constant i64 mul (i64 ptrtoint (double* getelementptr (double* null, i32 1) to i64), i64 2)
    8179 ; OPT: @g = constant i64 ptrtoint (double* getelementptr (%0* null, i64 0, i32 1) to i64)
    8280 ; OPT: @h = constant i64 ptrtoint (i1** getelementptr (i1** null, i32 1) to i64)
    8381 ; OPT: @i = constant i64 ptrtoint (i1** getelementptr (%2* null, i64 0, i32 1) to i64)
    84 ; OPT: @j = constant i64 ptrtoint (double* getelementptr (%0* null, i64 0, i32 1) to i64)
    85 ; OPT: @k = constant i64 ptrtoint (double* getelementptr (double* null, i32 1) to i64)
    8682 ; TO: @a = constant i64 18480
    8783 ; TO: @b = constant i64 8
    8884 ; TO: @c = constant i64 16
    9288 ; TO: @g = constant i64 8
    9389 ; TO: @h = constant i64 8
    9490 ; TO: @i = constant i64 8
    95 ; TO: @j = constant i64 8
    96 ; TO: @k = constant i64 8
    9791
    9892 @a = constant i64 mul (i64 3, i64 mul (i64 ptrtoint ({[7 x double], [7 x double]}* getelementptr ({[7 x double], [7 x double]}* null, i64 11) to i64), i64 5))
    9993 @b = constant i64 ptrtoint ([13 x double]* getelementptr ({i1, [13 x double]}* null, i64 0, i32 1) to i64)
    10498 @g = constant i64 ptrtoint ({double, double}* getelementptr ({i1, {double, double}}* null, i64 0, i32 1) to i64)
    10599 @h = constant i64 ptrtoint (double** getelementptr (double** null, i64 1) to i64)
    106100 @i = constant i64 ptrtoint (double** getelementptr ({i1, double*}* null, i64 0, i32 1) to i64)
    107 @j = constant i64 ptrtoint (union {double, double}* getelementptr ({i1, union {double, double}}* null, i64 0, i32 1) to i64)
    108 @k = constant i64 ptrtoint (union {double, double}* getelementptr (union {double, double}* null, i64 1) to i64)
    109101
    110102 ; The target-dependent folder should cast GEP indices to integer-sized pointers.
    111103
    274266 ; PLAIN: %t = bitcast i64 ptrtoint (i1** getelementptr (%2* null, i64 0, i32 1) to i64) to i64
    275267 ; PLAIN: ret i64 %t
    276268 ; PLAIN: }
    277 ; PLAIN: define i64 @fj() nounwind {
    278 ; PLAIN: %t = bitcast i64 ptrtoint (double* getelementptr (%0* null, i64 0, i32 1) to i64) to i64
    279 ; PLAIN: ret i64 %t
    280 ; PLAIN: }
    281 ; PLAIN: define i64 @fk() nounwind {
    282 ; PLAIN: %t = bitcast i64 ptrtoint (double* getelementptr (double* null, i32 1) to i64) to i64
    283 ; PLAIN: ret i64 %t
    284 ; PLAIN: }
    285269 ; OPT: define i64 @fa() nounwind {
    286270 ; OPT: ret i64 mul (i64 ptrtoint (double* getelementptr (double* null, i32 1) to i64), i64 2310)
    287271 ; OPT: }
    309293 ; OPT: define i64 @fi() nounwind {
    310294 ; OPT: ret i64 ptrtoint (i1** getelementptr (%2* null, i64 0, i32 1) to i64)
    311295 ; OPT: }
    312 ; OPT: define i64 @fj() nounwind {
    313 ; OPT: ret i64 ptrtoint (double* getelementptr (%0* null, i64 0, i32 1) to i64)
    314 ; OPT: }
    315 ; OPT: define i64 @fk() nounwind {
    316 ; OPT: ret i64 ptrtoint (double* getelementptr (double* null, i32 1) to i64)
    317 ; OPT: }
    318296 ; TO: define i64 @fa() nounwind {
    319297 ; TO: ret i64 18480
    320298 ; TO: }
    340318 ; TO: ret i64 8
    341319 ; TO: }
    342320 ; TO: define i64 @fi() nounwind {
    343 ; TO: ret i64 8
    344 ; TO: }
    345 ; TO: define i64 @fj() nounwind {
    346 ; TO: ret i64 8
    347 ; TO: }
    348 ; TO: define i64 @fk() nounwind {
    349321 ; TO: ret i64 8
    350322 ; TO: }
    351323 ; SCEV: Classifying expressions for: @fa
    375347 ; SCEV: Classifying expressions for: @fi
    376348 ; SCEV: %t = bitcast i64 ptrtoint (i1** getelementptr (%2* null, i64 0, i32 1) to i64) to i64
    377349 ; SCEV: --> alignof(i1*)
    378 ; SCEV: Classifying expressions for: @fj
    379 ; SCEV: %t = bitcast i64 ptrtoint (double* getelementptr (%0* null, i64 0, i32 1) to i64) to i64
    380 ; SCEV: --> alignof(double)
    381 ; SCEV: Classifying expressions for: @fk
    382 ; SCEV: %t = bitcast i64 ptrtoint (double* getelementptr (double* null, i32 1) to i64) to i64
    383 ; SCEV: --> sizeof(double)
    384350
    385351 define i64 @fa() nounwind {
    386352 %t = bitcast i64 mul (i64 3, i64 mul (i64 ptrtoint ({[7 x double], [7 x double]}* getelementptr ({[7 x double], [7 x double]}* null, i64 11) to i64), i64 5)) to i64
    416382 }
    417383 define i64 @fi() nounwind {
    418384 %t = bitcast i64 ptrtoint (double** getelementptr ({i1, double*}* null, i64 0, i32 1) to i64) to i64
    419 ret i64 %t
    420 }
    421 define i64 @fj() nounwind {
    422 %t = bitcast i64 ptrtoint (union {double, double}* getelementptr ({i1, union {double, double}}* null, i64 0, i32 1) to i64) to i64
    423 ret i64 %t
    424 }
    425 define i64 @fk() nounwind {
    426 %t = bitcast i64 ptrtoint (union {double, double}* getelementptr (union {double, double}* null, i64 1) to i64) to i64
    427385 ret i64 %t
    428386 }
    429387