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