blob: e6dbadfc3051130999bd62d1e765f26254e986d0 [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//===----------------------------------------------------------------------===//
9//
10// This file defines a few attributes of Subzero primitive types.
11//
12//===----------------------------------------------------------------------===//
13
14#include "IceDefs.h"
15#include "IceTypes.h"
16
17namespace Ice {
18
19namespace {
20
Karl Schimpfb262c5e2014-10-27 14:41:57 -070021const char *TargetArchName[] = {
Jan Voung08c3bcd2014-12-01 17:55:16 -080022#define X(tag, str, is_elf64, e_machine, e_flags) str,
23 TARGETARCH_TABLE
Karl Schimpfb262c5e2014-10-27 14:41:57 -070024#undef X
25};
26
Jim Stichnothfac55172014-10-01 13:06:21 -070027// Show tags match between ICETYPE_TABLE and ICETYPE_PROPS_TABLE.
Karl Schimpfd6064a12014-08-27 15:34:58 -070028
Jim Stichnothfac55172014-10-01 13:06:21 -070029// Define a temporary set of enum values based on ICETYPE_TABLE
30enum {
Karl Schimpfd6064a12014-08-27 15:34:58 -070031#define X(tag, size, align, elts, elty, str) _table_tag_##tag,
Jim Stichnothfac55172014-10-01 13:06:21 -070032 ICETYPE_TABLE
Karl Schimpfd6064a12014-08-27 15:34:58 -070033#undef X
Jim Stichnothfac55172014-10-01 13:06:21 -070034 _enum_table_tag_Names
35};
36// Define a temporary set of enum values based on ICETYPE_PROPS_TABLE
37enum {
Karl Schimpf41689df2014-09-10 14:36:07 -070038#define X(tag, IsVec, IsInt, IsFloat, IsIntArith, IsLoadStore, CompareResult) \
39 _props_table_tag_##tag,
Jim Stichnothfac55172014-10-01 13:06:21 -070040 ICETYPE_PROPS_TABLE
Karl Schimpfd6064a12014-08-27 15:34:58 -070041#undef X
Jim Stichnothfac55172014-10-01 13:06:21 -070042 _enum_props_table_tag_Names
43};
Karl Schimpfd6064a12014-08-27 15:34:58 -070044// Assert that tags in ICETYPE_TABLE are also in ICETYPE_PROPS_TABLE.
45#define X(tag, size, align, elts, elty, str) \
Jim Stichnothfac55172014-10-01 13:06:21 -070046 static_assert( \
47 (unsigned)_table_tag_##tag == (unsigned)_props_table_tag_##tag, \
48 "Inconsistency between ICETYPE_PROPS_TABLE and ICETYPE_TABLE");
49ICETYPE_TABLE;
Karl Schimpfd6064a12014-08-27 15:34:58 -070050#undef X
51// Assert that tags in ICETYPE_PROPS_TABLE is in ICETYPE_TABLE.
Karl Schimpf41689df2014-09-10 14:36:07 -070052#define X(tag, IsVec, IsInt, IsFloat, IsIntArith, IsLoadStore, CompareResult) \
Jim Stichnothfac55172014-10-01 13:06:21 -070053 static_assert( \
54 (unsigned)_table_tag_##tag == (unsigned)_props_table_tag_##tag, \
55 "Inconsistency between ICETYPE_PROPS_TABLE and ICETYPE_TABLE");
56ICETYPE_PROPS_TABLE
Karl Schimpfd6064a12014-08-27 15:34:58 -070057#undef X
58
Jim Stichnothfac55172014-10-01 13:06:21 -070059// Show vector definitions match in ICETYPE_TABLE and
60// ICETYPE_PROPS_TABLE.
Karl Schimpfd6064a12014-08-27 15:34:58 -070061
Jim Stichnothfac55172014-10-01 13:06:21 -070062// Define constants for each element size in ICETYPE_TABLE.
63enum {
Karl Schimpfd6064a12014-08-27 15:34:58 -070064#define X(tag, size, align, elts, elty, str) _table_elts_##tag = elts,
Jim Stichnothfac55172014-10-01 13:06:21 -070065 ICETYPE_TABLE
Karl Schimpfd6064a12014-08-27 15:34:58 -070066#undef X
Jim Stichnothfac55172014-10-01 13:06:21 -070067 _enum_table_elts_Elements = 0
68};
69// Define constants for boolean flag if vector in ICETYPE_PROPS_TABLE.
70enum {
Karl Schimpf41689df2014-09-10 14:36:07 -070071#define X(tag, IsVec, IsInt, IsFloat, IsIntArith, IsLoadStore, CompareResult) \
72 _props_table_IsVec_##tag = IsVec,
Jim Stichnothfac55172014-10-01 13:06:21 -070073 ICETYPE_PROPS_TABLE
Karl Schimpfd6064a12014-08-27 15:34:58 -070074#undef X
Jim Stichnothfac55172014-10-01 13:06:21 -070075};
Karl Schimpfd6064a12014-08-27 15:34:58 -070076// Verify that the number of vector elements is consistent with IsVec.
Karl Schimpf41689df2014-09-10 14:36:07 -070077#define X(tag, IsVec, IsInt, IsFloat, IsIntArith, IsLoadStore, CompareResult) \
Jim Stichnothfac55172014-10-01 13:06:21 -070078 static_assert((_table_elts_##tag > 1) == _props_table_IsVec_##tag, \
79 "Inconsistent vector specification in ICETYPE_PROPS_TABLE");
80ICETYPE_PROPS_TABLE;
Karl Schimpfd6064a12014-08-27 15:34:58 -070081#undef X
Karl Schimpfd6064a12014-08-27 15:34:58 -070082
83struct TypeAttributeFields {
Jim Stichnothf7c9a142014-04-29 10:52:43 -070084 size_t TypeWidthInBytes;
85 size_t TypeAlignInBytes;
Matt Wala928f1292014-07-07 16:50:46 -070086 size_t TypeNumElements;
87 Type TypeElementType;
Jim Stichnothf7c9a142014-04-29 10:52:43 -070088 const char *DisplayString;
Karl Schimpfd6064a12014-08-27 15:34:58 -070089};
90
91const struct TypeAttributeFields TypeAttributes[] = {
Matt Wala928f1292014-07-07 16:50:46 -070092#define X(tag, size, align, elts, elty, str) \
93 { size, align, elts, elty, str } \
Jim Stichnothf7c9a142014-04-29 10:52:43 -070094 ,
Jim Stichnothfac55172014-10-01 13:06:21 -070095 ICETYPE_TABLE
Jim Stichnothf7c9a142014-04-29 10:52:43 -070096#undef X
Karl Schimpfd6064a12014-08-27 15:34:58 -070097};
Jim Stichnothf7c9a142014-04-29 10:52:43 -070098
Karl Schimpfd6064a12014-08-27 15:34:58 -070099struct 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 Schimpf41689df2014-09-10 14:36:07 -0700108 bool TypeIsLoadStoreType;
Karl Schimpf83f9f0c2014-09-05 08:30:55 -0700109 Type CompareResultType;
Karl Schimpfd6064a12014-08-27 15:34:58 -0700110};
111
112const TypePropertyFields TypePropertiesTable[] = {
Karl Schimpf41689df2014-09-10 14:36:07 -0700113#define X(tag, IsVec, IsInt, IsFloat, IsIntArith, IsLoadStore, CompareResult) \
Karl Schimpfd6064a12014-08-27 15:34:58 -0700114 { \
115 IsVec, IsInt, IsInt && !IsVec, IsInt && IsVec, IsIntArith, IsFloat, \
Karl Schimpf41689df2014-09-10 14:36:07 -0700116 IsFloat && !IsVec, IsFloat && IsVec, IsLoadStore, CompareResult \
Karl Schimpfd6064a12014-08-27 15:34:58 -0700117 } \
118 ,
Jim Stichnothfac55172014-10-01 13:06:21 -0700119 ICETYPE_PROPS_TABLE
Karl Schimpfd6064a12014-08-27 15:34:58 -0700120#undef X
121};
Jim Stichnothf7c9a142014-04-29 10:52:43 -0700122
123} // end anonymous namespace
124
Karl Schimpfb262c5e2014-10-27 14:41:57 -0700125const 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 Stichnothf7c9a142014-04-29 10:52:43 -0700133size_t typeWidthInBytes(Type Ty) {
Jim Stichnothf7c9a142014-04-29 10:52:43 -0700134 size_t Index = static_cast<size_t>(Ty);
Karl Schimpfd6064a12014-08-27 15:34:58 -0700135 if (Index < IceType_NUM)
136 return TypeAttributes[Index].TypeWidthInBytes;
137 llvm_unreachable("Invalid type for typeWidthInBytes()");
138 return 0;
Jim Stichnothf7c9a142014-04-29 10:52:43 -0700139}
140
141size_t typeAlignInBytes(Type Ty) {
Jim Stichnothf7c9a142014-04-29 10:52:43 -0700142 size_t Index = static_cast<size_t>(Ty);
Karl Schimpfd6064a12014-08-27 15:34:58 -0700143 if (Index < IceType_NUM)
144 return TypeAttributes[Index].TypeAlignInBytes;
145 llvm_unreachable("Invalid type for typeAlignInBytes()");
146 return 1;
Jim Stichnothf7c9a142014-04-29 10:52:43 -0700147}
148
Matt Wala928f1292014-07-07 16:50:46 -0700149size_t typeNumElements(Type Ty) {
Matt Wala928f1292014-07-07 16:50:46 -0700150 size_t Index = static_cast<size_t>(Ty);
Karl Schimpfd6064a12014-08-27 15:34:58 -0700151 if (Index < IceType_NUM)
152 return TypeAttributes[Index].TypeNumElements;
153 llvm_unreachable("Invalid type for typeNumElements()");
154 return 1;
Matt Wala928f1292014-07-07 16:50:46 -0700155}
156
157Type typeElementType(Type Ty) {
Matt Wala928f1292014-07-07 16:50:46 -0700158 size_t Index = static_cast<size_t>(Ty);
Karl Schimpfd6064a12014-08-27 15:34:58 -0700159 if (Index < IceType_NUM)
160 return TypeAttributes[Index].TypeElementType;
161 llvm_unreachable("Invalid type for typeElementType()");
162 return IceType_void;
Matt Wala928f1292014-07-07 16:50:46 -0700163}
164
Karl Schimpfd6064a12014-08-27 15:34:58 -0700165bool 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
173bool 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
181bool 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
189bool 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
197bool 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
205bool 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
213bool 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
221bool 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 Schimpf41689df2014-09-10 14:36:07 -0700229bool 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 Schimpf83f9f0c2014-09-05 08:30:55 -0700237Type 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 Schimpfd1a971a2014-09-17 15:38:17 -0700245SizeT getScalarIntBitWidth(Type Ty) {
246 assert(isScalarIntegerType(Ty));
Karl Schimpf645aa1a2014-10-08 09:05:53 -0700247 if (Ty == IceType_i1)
Karl Schimpfd1a971a2014-09-17 15:38:17 -0700248 return 1;
249 return typeWidthInBytes(Ty) * CHAR_BIT;
250}
251
Karl Schimpfd6064a12014-08-27 15:34:58 -0700252// ======================== Dump routines ======================== //
253
Jim Stichnoth78282f62014-07-27 23:14:00 -0700254const char *typeString(Type Ty) {
Jim Stichnothf7c9a142014-04-29 10:52:43 -0700255 size_t Index = static_cast<size_t>(Ty);
Karl Schimpfd6064a12014-08-27 15:34:58 -0700256 if (Index < IceType_NUM)
Jim Stichnoth78282f62014-07-27 23:14:00 -0700257 return TypeAttributes[Index].DisplayString;
Jim Stichnoth78282f62014-07-27 23:14:00 -0700258 llvm_unreachable("Invalid type for typeString");
259 return "???";
Jim Stichnothf7c9a142014-04-29 10:52:43 -0700260}
261
Karl Schimpf645aa1a2014-10-08 09:05:53 -0700262void FuncSigType::dump(Ostream &Stream) const {
Karl Schimpfb6c96af2014-11-17 10:58:39 -0800263 if (!ALLOW_DUMP)
264 return;
Karl Schimpf645aa1a2014-10-08 09:05:53 -0700265 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 Stichnothf7c9a142014-04-29 10:52:43 -0700278} // end of namespace Ice