Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 1 | //===- subzero/src/IceTypes.cpp - Primitive type properties ---------------===// |
| 2 | // |
| 3 | // The Subzero Code Generator |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file defines a few attributes of Subzero primitive types. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "IceDefs.h" |
| 15 | #include "IceTypes.h" |
| 16 | |
| 17 | namespace Ice { |
| 18 | |
| 19 | namespace { |
| 20 | |
Jim Stichnoth | fac5517 | 2014-10-01 13:06:21 -0700 | [diff] [blame] | 21 | // Show tags match between ICETYPE_TABLE and ICETYPE_PROPS_TABLE. |
Karl Schimpf | d6064a1 | 2014-08-27 15:34:58 -0700 | [diff] [blame] | 22 | |
Jim Stichnoth | fac5517 | 2014-10-01 13:06:21 -0700 | [diff] [blame] | 23 | // Define a temporary set of enum values based on ICETYPE_TABLE |
| 24 | enum { |
Karl Schimpf | d6064a1 | 2014-08-27 15:34:58 -0700 | [diff] [blame] | 25 | #define X(tag, size, align, elts, elty, str) _table_tag_##tag, |
Jim Stichnoth | fac5517 | 2014-10-01 13:06:21 -0700 | [diff] [blame] | 26 | ICETYPE_TABLE |
Karl Schimpf | d6064a1 | 2014-08-27 15:34:58 -0700 | [diff] [blame] | 27 | #undef X |
Jim Stichnoth | fac5517 | 2014-10-01 13:06:21 -0700 | [diff] [blame] | 28 | _enum_table_tag_Names |
| 29 | }; |
| 30 | // Define a temporary set of enum values based on ICETYPE_PROPS_TABLE |
| 31 | enum { |
Karl Schimpf | 41689df | 2014-09-10 14:36:07 -0700 | [diff] [blame] | 32 | #define X(tag, IsVec, IsInt, IsFloat, IsIntArith, IsLoadStore, CompareResult) \ |
| 33 | _props_table_tag_##tag, |
Jim Stichnoth | fac5517 | 2014-10-01 13:06:21 -0700 | [diff] [blame] | 34 | ICETYPE_PROPS_TABLE |
Karl Schimpf | d6064a1 | 2014-08-27 15:34:58 -0700 | [diff] [blame] | 35 | #undef X |
Jim Stichnoth | fac5517 | 2014-10-01 13:06:21 -0700 | [diff] [blame] | 36 | _enum_props_table_tag_Names |
| 37 | }; |
Karl Schimpf | d6064a1 | 2014-08-27 15:34:58 -0700 | [diff] [blame] | 38 | // Assert that tags in ICETYPE_TABLE are also in ICETYPE_PROPS_TABLE. |
| 39 | #define X(tag, size, align, elts, elty, str) \ |
Jim Stichnoth | fac5517 | 2014-10-01 13:06:21 -0700 | [diff] [blame] | 40 | static_assert( \ |
| 41 | (unsigned)_table_tag_##tag == (unsigned)_props_table_tag_##tag, \ |
| 42 | "Inconsistency between ICETYPE_PROPS_TABLE and ICETYPE_TABLE"); |
| 43 | ICETYPE_TABLE; |
Karl Schimpf | d6064a1 | 2014-08-27 15:34:58 -0700 | [diff] [blame] | 44 | #undef X |
| 45 | // Assert that tags in ICETYPE_PROPS_TABLE is in ICETYPE_TABLE. |
Karl Schimpf | 41689df | 2014-09-10 14:36:07 -0700 | [diff] [blame] | 46 | #define X(tag, IsVec, IsInt, IsFloat, IsIntArith, IsLoadStore, CompareResult) \ |
Jim Stichnoth | fac5517 | 2014-10-01 13:06:21 -0700 | [diff] [blame] | 47 | static_assert( \ |
| 48 | (unsigned)_table_tag_##tag == (unsigned)_props_table_tag_##tag, \ |
| 49 | "Inconsistency between ICETYPE_PROPS_TABLE and ICETYPE_TABLE"); |
| 50 | ICETYPE_PROPS_TABLE |
Karl Schimpf | d6064a1 | 2014-08-27 15:34:58 -0700 | [diff] [blame] | 51 | #undef X |
| 52 | |
Jim Stichnoth | fac5517 | 2014-10-01 13:06:21 -0700 | [diff] [blame] | 53 | // Show vector definitions match in ICETYPE_TABLE and |
| 54 | // ICETYPE_PROPS_TABLE. |
Karl Schimpf | d6064a1 | 2014-08-27 15:34:58 -0700 | [diff] [blame] | 55 | |
Jim Stichnoth | fac5517 | 2014-10-01 13:06:21 -0700 | [diff] [blame] | 56 | // Define constants for each element size in ICETYPE_TABLE. |
| 57 | enum { |
Karl Schimpf | d6064a1 | 2014-08-27 15:34:58 -0700 | [diff] [blame] | 58 | #define X(tag, size, align, elts, elty, str) _table_elts_##tag = elts, |
Jim Stichnoth | fac5517 | 2014-10-01 13:06:21 -0700 | [diff] [blame] | 59 | ICETYPE_TABLE |
Karl Schimpf | d6064a1 | 2014-08-27 15:34:58 -0700 | [diff] [blame] | 60 | #undef X |
Jim Stichnoth | fac5517 | 2014-10-01 13:06:21 -0700 | [diff] [blame] | 61 | _enum_table_elts_Elements = 0 |
| 62 | }; |
| 63 | // Define constants for boolean flag if vector in ICETYPE_PROPS_TABLE. |
| 64 | enum { |
Karl Schimpf | 41689df | 2014-09-10 14:36:07 -0700 | [diff] [blame] | 65 | #define X(tag, IsVec, IsInt, IsFloat, IsIntArith, IsLoadStore, CompareResult) \ |
| 66 | _props_table_IsVec_##tag = IsVec, |
Jim Stichnoth | fac5517 | 2014-10-01 13:06:21 -0700 | [diff] [blame] | 67 | ICETYPE_PROPS_TABLE |
Karl Schimpf | d6064a1 | 2014-08-27 15:34:58 -0700 | [diff] [blame] | 68 | #undef X |
Jim Stichnoth | fac5517 | 2014-10-01 13:06:21 -0700 | [diff] [blame] | 69 | }; |
Karl Schimpf | d6064a1 | 2014-08-27 15:34:58 -0700 | [diff] [blame] | 70 | // Verify that the number of vector elements is consistent with IsVec. |
Karl Schimpf | 41689df | 2014-09-10 14:36:07 -0700 | [diff] [blame] | 71 | #define X(tag, IsVec, IsInt, IsFloat, IsIntArith, IsLoadStore, CompareResult) \ |
Jim Stichnoth | fac5517 | 2014-10-01 13:06:21 -0700 | [diff] [blame] | 72 | static_assert((_table_elts_##tag > 1) == _props_table_IsVec_##tag, \ |
| 73 | "Inconsistent vector specification in ICETYPE_PROPS_TABLE"); |
| 74 | ICETYPE_PROPS_TABLE; |
Karl Schimpf | d6064a1 | 2014-08-27 15:34:58 -0700 | [diff] [blame] | 75 | #undef X |
Karl Schimpf | d6064a1 | 2014-08-27 15:34:58 -0700 | [diff] [blame] | 76 | |
| 77 | struct TypeAttributeFields { |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 78 | size_t TypeWidthInBytes; |
| 79 | size_t TypeAlignInBytes; |
Matt Wala | 928f129 | 2014-07-07 16:50:46 -0700 | [diff] [blame] | 80 | size_t TypeNumElements; |
| 81 | Type TypeElementType; |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 82 | const char *DisplayString; |
Karl Schimpf | d6064a1 | 2014-08-27 15:34:58 -0700 | [diff] [blame] | 83 | }; |
| 84 | |
| 85 | const struct TypeAttributeFields TypeAttributes[] = { |
Matt Wala | 928f129 | 2014-07-07 16:50:46 -0700 | [diff] [blame] | 86 | #define X(tag, size, align, elts, elty, str) \ |
| 87 | { size, align, elts, elty, str } \ |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 88 | , |
Jim Stichnoth | fac5517 | 2014-10-01 13:06:21 -0700 | [diff] [blame] | 89 | ICETYPE_TABLE |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 90 | #undef X |
Karl Schimpf | d6064a1 | 2014-08-27 15:34:58 -0700 | [diff] [blame] | 91 | }; |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 92 | |
Karl Schimpf | d6064a1 | 2014-08-27 15:34:58 -0700 | [diff] [blame] | 93 | struct TypePropertyFields { |
| 94 | bool TypeIsVectorType; |
| 95 | bool TypeIsIntegerType; |
| 96 | bool TypeIsScalarIntegerType; |
| 97 | bool TypeIsVectorIntegerType; |
| 98 | bool TypeIsIntegerArithmeticType; |
| 99 | bool TypeIsFloatingType; |
| 100 | bool TypeIsScalarFloatingType; |
| 101 | bool TypeIsVectorFloatingType; |
Karl Schimpf | 41689df | 2014-09-10 14:36:07 -0700 | [diff] [blame] | 102 | bool TypeIsLoadStoreType; |
Karl Schimpf | 83f9f0c | 2014-09-05 08:30:55 -0700 | [diff] [blame] | 103 | Type CompareResultType; |
Karl Schimpf | d6064a1 | 2014-08-27 15:34:58 -0700 | [diff] [blame] | 104 | }; |
| 105 | |
| 106 | const TypePropertyFields TypePropertiesTable[] = { |
Karl Schimpf | 41689df | 2014-09-10 14:36:07 -0700 | [diff] [blame] | 107 | #define X(tag, IsVec, IsInt, IsFloat, IsIntArith, IsLoadStore, CompareResult) \ |
Karl Schimpf | d6064a1 | 2014-08-27 15:34:58 -0700 | [diff] [blame] | 108 | { \ |
| 109 | IsVec, IsInt, IsInt && !IsVec, IsInt && IsVec, IsIntArith, IsFloat, \ |
Karl Schimpf | 41689df | 2014-09-10 14:36:07 -0700 | [diff] [blame] | 110 | IsFloat && !IsVec, IsFloat && IsVec, IsLoadStore, CompareResult \ |
Karl Schimpf | d6064a1 | 2014-08-27 15:34:58 -0700 | [diff] [blame] | 111 | } \ |
| 112 | , |
Jim Stichnoth | fac5517 | 2014-10-01 13:06:21 -0700 | [diff] [blame] | 113 | ICETYPE_PROPS_TABLE |
Karl Schimpf | d6064a1 | 2014-08-27 15:34:58 -0700 | [diff] [blame] | 114 | #undef X |
| 115 | }; |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 116 | |
| 117 | } // end anonymous namespace |
| 118 | |
| 119 | size_t typeWidthInBytes(Type Ty) { |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 120 | size_t Index = static_cast<size_t>(Ty); |
Karl Schimpf | d6064a1 | 2014-08-27 15:34:58 -0700 | [diff] [blame] | 121 | if (Index < IceType_NUM) |
| 122 | return TypeAttributes[Index].TypeWidthInBytes; |
| 123 | llvm_unreachable("Invalid type for typeWidthInBytes()"); |
| 124 | return 0; |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | size_t typeAlignInBytes(Type Ty) { |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 128 | size_t Index = static_cast<size_t>(Ty); |
Karl Schimpf | d6064a1 | 2014-08-27 15:34:58 -0700 | [diff] [blame] | 129 | if (Index < IceType_NUM) |
| 130 | return TypeAttributes[Index].TypeAlignInBytes; |
| 131 | llvm_unreachable("Invalid type for typeAlignInBytes()"); |
| 132 | return 1; |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 133 | } |
| 134 | |
Matt Wala | 928f129 | 2014-07-07 16:50:46 -0700 | [diff] [blame] | 135 | size_t typeNumElements(Type Ty) { |
Matt Wala | 928f129 | 2014-07-07 16:50:46 -0700 | [diff] [blame] | 136 | size_t Index = static_cast<size_t>(Ty); |
Karl Schimpf | d6064a1 | 2014-08-27 15:34:58 -0700 | [diff] [blame] | 137 | if (Index < IceType_NUM) |
| 138 | return TypeAttributes[Index].TypeNumElements; |
| 139 | llvm_unreachable("Invalid type for typeNumElements()"); |
| 140 | return 1; |
Matt Wala | 928f129 | 2014-07-07 16:50:46 -0700 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | Type typeElementType(Type Ty) { |
Matt Wala | 928f129 | 2014-07-07 16:50:46 -0700 | [diff] [blame] | 144 | size_t Index = static_cast<size_t>(Ty); |
Karl Schimpf | d6064a1 | 2014-08-27 15:34:58 -0700 | [diff] [blame] | 145 | if (Index < IceType_NUM) |
| 146 | return TypeAttributes[Index].TypeElementType; |
| 147 | llvm_unreachable("Invalid type for typeElementType()"); |
| 148 | return IceType_void; |
Matt Wala | 928f129 | 2014-07-07 16:50:46 -0700 | [diff] [blame] | 149 | } |
| 150 | |
Karl Schimpf | d6064a1 | 2014-08-27 15:34:58 -0700 | [diff] [blame] | 151 | bool isVectorType(Type Ty) { |
| 152 | size_t Index = static_cast<size_t>(Ty); |
| 153 | if (Index < IceType_NUM) |
| 154 | return TypePropertiesTable[Index].TypeIsVectorType; |
| 155 | llvm_unreachable("Invalid type for isVectorType()"); |
| 156 | return false; |
| 157 | } |
| 158 | |
| 159 | bool isIntegerType(Type Ty) { |
| 160 | size_t Index = static_cast<size_t>(Ty); |
| 161 | if (Index < IceType_NUM) |
| 162 | return TypePropertiesTable[Index].TypeIsIntegerType; |
| 163 | llvm_unreachable("Invalid type for isIntegerType()"); |
| 164 | return false; |
| 165 | } |
| 166 | |
| 167 | bool isScalarIntegerType(Type Ty) { |
| 168 | size_t Index = static_cast<size_t>(Ty); |
| 169 | if (Index < IceType_NUM) |
| 170 | return TypePropertiesTable[Index].TypeIsScalarIntegerType; |
| 171 | llvm_unreachable("Invalid type for isScalIntegerType()"); |
| 172 | return false; |
| 173 | } |
| 174 | |
| 175 | bool isVectorIntegerType(Type Ty) { |
| 176 | size_t Index = static_cast<size_t>(Ty); |
| 177 | if (Index < IceType_NUM) |
| 178 | return TypePropertiesTable[Index].TypeIsVectorIntegerType; |
| 179 | llvm_unreachable("Invalid type for isVectorIntegerType()"); |
| 180 | return false; |
| 181 | } |
| 182 | |
| 183 | bool isIntegerArithmeticType(Type Ty) { |
| 184 | size_t Index = static_cast<size_t>(Ty); |
| 185 | if (Index < IceType_NUM) |
| 186 | return TypePropertiesTable[Index].TypeIsIntegerArithmeticType; |
| 187 | llvm_unreachable("Invalid type for isIntegerArithmeticType()"); |
| 188 | return false; |
| 189 | } |
| 190 | |
| 191 | bool isFloatingType(Type Ty) { |
| 192 | size_t Index = static_cast<size_t>(Ty); |
| 193 | if (Index < IceType_NUM) |
| 194 | return TypePropertiesTable[Index].TypeIsFloatingType; |
| 195 | llvm_unreachable("Invalid type for isFloatingType()"); |
| 196 | return false; |
| 197 | } |
| 198 | |
| 199 | bool isScalarFloatingType(Type Ty) { |
| 200 | size_t Index = static_cast<size_t>(Ty); |
| 201 | if (Index < IceType_NUM) |
| 202 | return TypePropertiesTable[Index].TypeIsScalarFloatingType; |
| 203 | llvm_unreachable("Invalid type for isScalarFloatingType()"); |
| 204 | return false; |
| 205 | } |
| 206 | |
| 207 | bool isVectorFloatingType(Type Ty) { |
| 208 | size_t Index = static_cast<size_t>(Ty); |
| 209 | if (Index < IceType_NUM) |
| 210 | return TypePropertiesTable[Index].TypeIsVectorFloatingType; |
| 211 | llvm_unreachable("Invalid type for isVectorFloatingType()"); |
| 212 | return false; |
| 213 | } |
| 214 | |
Karl Schimpf | 41689df | 2014-09-10 14:36:07 -0700 | [diff] [blame] | 215 | bool isLoadStoreType(Type Ty) { |
| 216 | size_t Index = static_cast<size_t>(Ty); |
| 217 | if (Index < IceType_NUM) |
| 218 | return TypePropertiesTable[Index].TypeIsLoadStoreType; |
| 219 | llvm_unreachable("Invalid type for isLoadStoreType()"); |
| 220 | return false; |
| 221 | } |
| 222 | |
Karl Schimpf | 83f9f0c | 2014-09-05 08:30:55 -0700 | [diff] [blame] | 223 | Type getCompareResultType(Type Ty) { |
| 224 | size_t Index = static_cast<size_t>(Ty); |
| 225 | if (Index < IceType_NUM) |
| 226 | return TypePropertiesTable[Index].CompareResultType; |
| 227 | llvm_unreachable("Invalid type for getCompareResultType"); |
| 228 | return IceType_void; |
| 229 | } |
| 230 | |
Karl Schimpf | d1a971a | 2014-09-17 15:38:17 -0700 | [diff] [blame] | 231 | SizeT getScalarIntBitWidth(Type Ty) { |
| 232 | assert(isScalarIntegerType(Ty)); |
Karl Schimpf | 645aa1a | 2014-10-08 09:05:53 -0700 | [diff] [blame] | 233 | if (Ty == IceType_i1) |
Karl Schimpf | d1a971a | 2014-09-17 15:38:17 -0700 | [diff] [blame] | 234 | return 1; |
| 235 | return typeWidthInBytes(Ty) * CHAR_BIT; |
| 236 | } |
| 237 | |
Karl Schimpf | d6064a1 | 2014-08-27 15:34:58 -0700 | [diff] [blame] | 238 | // ======================== Dump routines ======================== // |
| 239 | |
Jim Stichnoth | 78282f6 | 2014-07-27 23:14:00 -0700 | [diff] [blame] | 240 | const char *typeString(Type Ty) { |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 241 | size_t Index = static_cast<size_t>(Ty); |
Karl Schimpf | d6064a1 | 2014-08-27 15:34:58 -0700 | [diff] [blame] | 242 | if (Index < IceType_NUM) |
Jim Stichnoth | 78282f6 | 2014-07-27 23:14:00 -0700 | [diff] [blame] | 243 | return TypeAttributes[Index].DisplayString; |
Jim Stichnoth | 78282f6 | 2014-07-27 23:14:00 -0700 | [diff] [blame] | 244 | llvm_unreachable("Invalid type for typeString"); |
| 245 | return "???"; |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 246 | } |
| 247 | |
Karl Schimpf | 645aa1a | 2014-10-08 09:05:53 -0700 | [diff] [blame] | 248 | void FuncSigType::dump(Ostream &Stream) const { |
| 249 | Stream << ReturnType << " ("; |
| 250 | bool IsFirst = true; |
| 251 | for (const Type ArgTy : ArgList) { |
| 252 | if (IsFirst) { |
| 253 | IsFirst = false; |
| 254 | } else { |
| 255 | Stream << ", "; |
| 256 | } |
| 257 | Stream << ArgTy; |
| 258 | } |
| 259 | Stream << ")"; |
| 260 | } |
| 261 | |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 262 | } // end of namespace Ice |