blob: dd06b1e975156e2e78bbfed065ac268ed97de692 [file] [log] [blame]
Jim Stichnothf7c9a142014-04-29 10:52:43 -07001//===- 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 Scull9612d322015-07-06 14:53:25 -07009///
10/// \file
11/// This file defines a few attributes of Subzero primitive types.
12///
Jim Stichnothf7c9a142014-04-29 10:52:43 -070013//===----------------------------------------------------------------------===//
14
Jim Stichnothf7c9a142014-04-29 10:52:43 -070015#include "IceTypes.h"
16
John Porto67f8de92015-06-25 10:14:17 -070017#include "IceDefs.h"
18
Jim Stichnothf7c9a142014-04-29 10:52:43 -070019namespace Ice {
20
21namespace {
22
Karl Schimpfb262c5e2014-10-27 14:41:57 -070023const char *TargetArchName[] = {
Jan Voung08c3bcd2014-12-01 17:55:16 -080024#define X(tag, str, is_elf64, e_machine, e_flags) str,
25 TARGETARCH_TABLE
Karl Schimpfb262c5e2014-10-27 14:41:57 -070026#undef X
27};
28
Jim Stichnothfac55172014-10-01 13:06:21 -070029// Show tags match between ICETYPE_TABLE and ICETYPE_PROPS_TABLE.
Karl Schimpfd6064a12014-08-27 15:34:58 -070030
Jim Stichnothfac55172014-10-01 13:06:21 -070031// Define a temporary set of enum values based on ICETYPE_TABLE
32enum {
Andrew Scull87f80c12015-07-20 10:19:16 -070033#define X(tag, sizeLog2, align, elts, elty, str) _table_tag_##tag,
Jim Stichnothfac55172014-10-01 13:06:21 -070034 ICETYPE_TABLE
Karl Schimpfd6064a12014-08-27 15:34:58 -070035#undef X
Jim Stichnothfac55172014-10-01 13:06:21 -070036 _enum_table_tag_Names
37};
38// Define a temporary set of enum values based on ICETYPE_PROPS_TABLE
39enum {
Karl Schimpf41689df2014-09-10 14:36:07 -070040#define X(tag, IsVec, IsInt, IsFloat, IsIntArith, IsLoadStore, CompareResult) \
41 _props_table_tag_##tag,
Jim Stichnothfac55172014-10-01 13:06:21 -070042 ICETYPE_PROPS_TABLE
Karl Schimpfd6064a12014-08-27 15:34:58 -070043#undef X
Jim Stichnothfac55172014-10-01 13:06:21 -070044 _enum_props_table_tag_Names
45};
Karl Schimpfd6064a12014-08-27 15:34:58 -070046// Assert that tags in ICETYPE_TABLE are also in ICETYPE_PROPS_TABLE.
Andrew Scull87f80c12015-07-20 10:19:16 -070047#define X(tag, sizeLog2, align, elts, elty, str) \
Jim Stichnothfac55172014-10-01 13:06:21 -070048 static_assert( \
49 (unsigned)_table_tag_##tag == (unsigned)_props_table_tag_##tag, \
50 "Inconsistency between ICETYPE_PROPS_TABLE and ICETYPE_TABLE");
JF Bastien8427ea22015-01-27 12:56:49 -080051ICETYPE_TABLE
Karl Schimpfd6064a12014-08-27 15:34:58 -070052#undef X
53// Assert that tags in ICETYPE_PROPS_TABLE is in ICETYPE_TABLE.
Karl Schimpf41689df2014-09-10 14:36:07 -070054#define X(tag, IsVec, IsInt, IsFloat, IsIntArith, IsLoadStore, CompareResult) \
Jim Stichnothfac55172014-10-01 13:06:21 -070055 static_assert( \
56 (unsigned)_table_tag_##tag == (unsigned)_props_table_tag_##tag, \
57 "Inconsistency between ICETYPE_PROPS_TABLE and ICETYPE_TABLE");
58ICETYPE_PROPS_TABLE
Karl Schimpfd6064a12014-08-27 15:34:58 -070059#undef X
60
Jim Stichnothfac55172014-10-01 13:06:21 -070061// Show vector definitions match in ICETYPE_TABLE and
62// ICETYPE_PROPS_TABLE.
Karl Schimpfd6064a12014-08-27 15:34:58 -070063
Jim Stichnothfac55172014-10-01 13:06:21 -070064// Define constants for each element size in ICETYPE_TABLE.
65enum {
Andrew Scull87f80c12015-07-20 10:19:16 -070066#define X(tag, sizeLog2, align, elts, elty, str) _table_elts_##tag = elts,
Jim Stichnothfac55172014-10-01 13:06:21 -070067 ICETYPE_TABLE
Karl Schimpfd6064a12014-08-27 15:34:58 -070068#undef X
Jim Stichnothfac55172014-10-01 13:06:21 -070069 _enum_table_elts_Elements = 0
70};
71// Define constants for boolean flag if vector in ICETYPE_PROPS_TABLE.
72enum {
Karl Schimpf41689df2014-09-10 14:36:07 -070073#define X(tag, IsVec, IsInt, IsFloat, IsIntArith, IsLoadStore, CompareResult) \
74 _props_table_IsVec_##tag = IsVec,
Jim Stichnothfac55172014-10-01 13:06:21 -070075 ICETYPE_PROPS_TABLE
Karl Schimpfd6064a12014-08-27 15:34:58 -070076#undef X
Jim Stichnothfac55172014-10-01 13:06:21 -070077};
Karl Schimpfd6064a12014-08-27 15:34:58 -070078// Verify that the number of vector elements is consistent with IsVec.
Karl Schimpf41689df2014-09-10 14:36:07 -070079#define X(tag, IsVec, IsInt, IsFloat, IsIntArith, IsLoadStore, CompareResult) \
Jim Stichnothfac55172014-10-01 13:06:21 -070080 static_assert((_table_elts_##tag > 1) == _props_table_IsVec_##tag, \
81 "Inconsistent vector specification in ICETYPE_PROPS_TABLE");
JF Bastien8427ea22015-01-27 12:56:49 -080082ICETYPE_PROPS_TABLE
Karl Schimpfd6064a12014-08-27 15:34:58 -070083#undef X
Karl Schimpfd6064a12014-08-27 15:34:58 -070084
85struct TypeAttributeFields {
Andrew Scull87f80c12015-07-20 10:19:16 -070086 int8_t TypeWidthInBytesLog2;
Jim Stichnothf7c9a142014-04-29 10:52:43 -070087 size_t TypeAlignInBytes;
Matt Wala928f1292014-07-07 16:50:46 -070088 size_t TypeNumElements;
89 Type TypeElementType;
Jim Stichnothf7c9a142014-04-29 10:52:43 -070090 const char *DisplayString;
Karl Schimpfd6064a12014-08-27 15:34:58 -070091};
92
93const struct TypeAttributeFields TypeAttributes[] = {
Andrew Scull87f80c12015-07-20 10:19:16 -070094#define X(tag, sizeLog2, align, elts, elty, str) \
95 { sizeLog2, align, elts, elty, str } \
Jim Stichnothf7c9a142014-04-29 10:52:43 -070096 ,
Jim Stichnothdd842db2015-01-27 12:53:53 -080097 ICETYPE_TABLE
Jim Stichnothf7c9a142014-04-29 10:52:43 -070098#undef X
Karl Schimpfd6064a12014-08-27 15:34:58 -070099};
Jim Stichnothf7c9a142014-04-29 10:52:43 -0700100
Karl Schimpfd6064a12014-08-27 15:34:58 -0700101struct 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 Schimpf41689df2014-09-10 14:36:07 -0700110 bool TypeIsLoadStoreType;
Karl Schimpf83f9f0c2014-09-05 08:30:55 -0700111 Type CompareResultType;
Karl Schimpfd6064a12014-08-27 15:34:58 -0700112};
113
114const TypePropertyFields TypePropertiesTable[] = {
Karl Schimpf41689df2014-09-10 14:36:07 -0700115#define X(tag, IsVec, IsInt, IsFloat, IsIntArith, IsLoadStore, CompareResult) \
Karl Schimpfd6064a12014-08-27 15:34:58 -0700116 { \
Mircea Trofin59c6f5a2015-04-06 16:06:47 -0700117 IsVec, IsInt, IsInt & !IsVec, IsInt & IsVec, IsIntArith, IsFloat, \
118 IsFloat & !IsVec, IsFloat & IsVec, IsLoadStore, CompareResult \
Karl Schimpfd6064a12014-08-27 15:34:58 -0700119 } \
120 ,
Jim Stichnothdd842db2015-01-27 12:53:53 -0800121 ICETYPE_PROPS_TABLE
Karl Schimpfd6064a12014-08-27 15:34:58 -0700122#undef X
123};
Jim Stichnothf7c9a142014-04-29 10:52:43 -0700124
125} // end anonymous namespace
126
Karl Schimpfb262c5e2014-10-27 14:41:57 -0700127const 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 Stichnothf7c9a142014-04-29 10:52:43 -0700135size_t typeWidthInBytes(Type Ty) {
Andrew Scull87f80c12015-07-20 10:19:16 -0700136 int8_t Shift = typeWidthInBytesLog2(Ty);
137 return (Shift < 0) ? 0 : 1 << Shift;
138}
139
140int8_t typeWidthInBytesLog2(Type Ty) {
Jim Stichnothf7c9a142014-04-29 10:52:43 -0700141 size_t Index = static_cast<size_t>(Ty);
Karl Schimpfd6064a12014-08-27 15:34:58 -0700142 if (Index < IceType_NUM)
Andrew Scull87f80c12015-07-20 10:19:16 -0700143 return TypeAttributes[Index].TypeWidthInBytesLog2;
144 llvm_unreachable("Invalid type for typeWidthInBytesLog2()");
Karl Schimpfd6064a12014-08-27 15:34:58 -0700145 return 0;
Jim Stichnothf7c9a142014-04-29 10:52:43 -0700146}
147
148size_t typeAlignInBytes(Type Ty) {
Jim Stichnothf7c9a142014-04-29 10:52:43 -0700149 size_t Index = static_cast<size_t>(Ty);
Karl Schimpfd6064a12014-08-27 15:34:58 -0700150 if (Index < IceType_NUM)
151 return TypeAttributes[Index].TypeAlignInBytes;
152 llvm_unreachable("Invalid type for typeAlignInBytes()");
153 return 1;
Jim Stichnothf7c9a142014-04-29 10:52:43 -0700154}
155
Matt Wala928f1292014-07-07 16:50:46 -0700156size_t typeNumElements(Type Ty) {
Matt Wala928f1292014-07-07 16:50:46 -0700157 size_t Index = static_cast<size_t>(Ty);
Karl Schimpfd6064a12014-08-27 15:34:58 -0700158 if (Index < IceType_NUM)
159 return TypeAttributes[Index].TypeNumElements;
160 llvm_unreachable("Invalid type for typeNumElements()");
161 return 1;
Matt Wala928f1292014-07-07 16:50:46 -0700162}
163
164Type typeElementType(Type Ty) {
Matt Wala928f1292014-07-07 16:50:46 -0700165 size_t Index = static_cast<size_t>(Ty);
Karl Schimpfd6064a12014-08-27 15:34:58 -0700166 if (Index < IceType_NUM)
167 return TypeAttributes[Index].TypeElementType;
168 llvm_unreachable("Invalid type for typeElementType()");
169 return IceType_void;
Matt Wala928f1292014-07-07 16:50:46 -0700170}
171
Karl Schimpfd6064a12014-08-27 15:34:58 -0700172bool 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
180bool 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
188bool 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
196bool 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
204bool 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
212bool 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
220bool 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
228bool 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 Schimpf41689df2014-09-10 14:36:07 -0700236bool 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 Schimpf83f9f0c2014-09-05 08:30:55 -0700244Type 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 Schimpfd1a971a2014-09-17 15:38:17 -0700252SizeT getScalarIntBitWidth(Type Ty) {
253 assert(isScalarIntegerType(Ty));
Karl Schimpf645aa1a2014-10-08 09:05:53 -0700254 if (Ty == IceType_i1)
Karl Schimpfd1a971a2014-09-17 15:38:17 -0700255 return 1;
256 return typeWidthInBytes(Ty) * CHAR_BIT;
257}
258
Karl Schimpfd6064a12014-08-27 15:34:58 -0700259// ======================== Dump routines ======================== //
260
Jim Stichnoth78282f62014-07-27 23:14:00 -0700261const char *typeString(Type Ty) {
Jim Stichnothf7c9a142014-04-29 10:52:43 -0700262 size_t Index = static_cast<size_t>(Ty);
Karl Schimpfd6064a12014-08-27 15:34:58 -0700263 if (Index < IceType_NUM)
Jim Stichnoth78282f62014-07-27 23:14:00 -0700264 return TypeAttributes[Index].DisplayString;
Jim Stichnoth78282f62014-07-27 23:14:00 -0700265 llvm_unreachable("Invalid type for typeString");
266 return "???";
Jim Stichnothf7c9a142014-04-29 10:52:43 -0700267}
268
Karl Schimpf645aa1a2014-10-08 09:05:53 -0700269void FuncSigType::dump(Ostream &Stream) const {
Jim Stichnoth20b71f52015-06-24 15:52:24 -0700270 if (!BuildDefs::dump())
Karl Schimpfb6c96af2014-11-17 10:58:39 -0800271 return;
Karl Schimpf645aa1a2014-10-08 09:05:53 -0700272 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 Stichnothf7c9a142014-04-29 10:52:43 -0700285} // end of namespace Ice