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