blob: 6b81d8ac9e8765aef9dee0736bbcee4972373e8a [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
Jim Stichnothfac55172014-10-01 13:06:21 -070021// Show tags match between ICETYPE_TABLE and ICETYPE_PROPS_TABLE.
Karl Schimpfd6064a12014-08-27 15:34:58 -070022
Jim Stichnothfac55172014-10-01 13:06:21 -070023// Define a temporary set of enum values based on ICETYPE_TABLE
24enum {
Karl Schimpfd6064a12014-08-27 15:34:58 -070025#define X(tag, size, align, elts, elty, str) _table_tag_##tag,
Jim Stichnothfac55172014-10-01 13:06:21 -070026 ICETYPE_TABLE
Karl Schimpfd6064a12014-08-27 15:34:58 -070027#undef X
Jim Stichnothfac55172014-10-01 13:06:21 -070028 _enum_table_tag_Names
29};
30// Define a temporary set of enum values based on ICETYPE_PROPS_TABLE
31enum {
Karl Schimpf41689df2014-09-10 14:36:07 -070032#define X(tag, IsVec, IsInt, IsFloat, IsIntArith, IsLoadStore, CompareResult) \
33 _props_table_tag_##tag,
Jim Stichnothfac55172014-10-01 13:06:21 -070034 ICETYPE_PROPS_TABLE
Karl Schimpfd6064a12014-08-27 15:34:58 -070035#undef X
Jim Stichnothfac55172014-10-01 13:06:21 -070036 _enum_props_table_tag_Names
37};
Karl Schimpfd6064a12014-08-27 15:34:58 -070038// Assert that tags in ICETYPE_TABLE are also in ICETYPE_PROPS_TABLE.
39#define X(tag, size, align, elts, elty, str) \
Jim Stichnothfac55172014-10-01 13:06:21 -070040 static_assert( \
41 (unsigned)_table_tag_##tag == (unsigned)_props_table_tag_##tag, \
42 "Inconsistency between ICETYPE_PROPS_TABLE and ICETYPE_TABLE");
43ICETYPE_TABLE;
Karl Schimpfd6064a12014-08-27 15:34:58 -070044#undef X
45// Assert that tags in ICETYPE_PROPS_TABLE is in ICETYPE_TABLE.
Karl Schimpf41689df2014-09-10 14:36:07 -070046#define X(tag, IsVec, IsInt, IsFloat, IsIntArith, IsLoadStore, CompareResult) \
Jim Stichnothfac55172014-10-01 13:06:21 -070047 static_assert( \
48 (unsigned)_table_tag_##tag == (unsigned)_props_table_tag_##tag, \
49 "Inconsistency between ICETYPE_PROPS_TABLE and ICETYPE_TABLE");
50ICETYPE_PROPS_TABLE
Karl Schimpfd6064a12014-08-27 15:34:58 -070051#undef X
52
Jim Stichnothfac55172014-10-01 13:06:21 -070053// Show vector definitions match in ICETYPE_TABLE and
54// ICETYPE_PROPS_TABLE.
Karl Schimpfd6064a12014-08-27 15:34:58 -070055
Jim Stichnothfac55172014-10-01 13:06:21 -070056// Define constants for each element size in ICETYPE_TABLE.
57enum {
Karl Schimpfd6064a12014-08-27 15:34:58 -070058#define X(tag, size, align, elts, elty, str) _table_elts_##tag = elts,
Jim Stichnothfac55172014-10-01 13:06:21 -070059 ICETYPE_TABLE
Karl Schimpfd6064a12014-08-27 15:34:58 -070060#undef X
Jim Stichnothfac55172014-10-01 13:06:21 -070061 _enum_table_elts_Elements = 0
62};
63// Define constants for boolean flag if vector in ICETYPE_PROPS_TABLE.
64enum {
Karl Schimpf41689df2014-09-10 14:36:07 -070065#define X(tag, IsVec, IsInt, IsFloat, IsIntArith, IsLoadStore, CompareResult) \
66 _props_table_IsVec_##tag = IsVec,
Jim Stichnothfac55172014-10-01 13:06:21 -070067 ICETYPE_PROPS_TABLE
Karl Schimpfd6064a12014-08-27 15:34:58 -070068#undef X
Jim Stichnothfac55172014-10-01 13:06:21 -070069};
Karl Schimpfd6064a12014-08-27 15:34:58 -070070// Verify that the number of vector elements is consistent with IsVec.
Karl Schimpf41689df2014-09-10 14:36:07 -070071#define X(tag, IsVec, IsInt, IsFloat, IsIntArith, IsLoadStore, CompareResult) \
Jim Stichnothfac55172014-10-01 13:06:21 -070072 static_assert((_table_elts_##tag > 1) == _props_table_IsVec_##tag, \
73 "Inconsistent vector specification in ICETYPE_PROPS_TABLE");
74ICETYPE_PROPS_TABLE;
Karl Schimpfd6064a12014-08-27 15:34:58 -070075#undef X
Karl Schimpfd6064a12014-08-27 15:34:58 -070076
77struct TypeAttributeFields {
Jim Stichnothf7c9a142014-04-29 10:52:43 -070078 size_t TypeWidthInBytes;
79 size_t TypeAlignInBytes;
Matt Wala928f1292014-07-07 16:50:46 -070080 size_t TypeNumElements;
81 Type TypeElementType;
Jim Stichnothf7c9a142014-04-29 10:52:43 -070082 const char *DisplayString;
Karl Schimpfd6064a12014-08-27 15:34:58 -070083};
84
85const struct TypeAttributeFields TypeAttributes[] = {
Matt Wala928f1292014-07-07 16:50:46 -070086#define X(tag, size, align, elts, elty, str) \
87 { size, align, elts, elty, str } \
Jim Stichnothf7c9a142014-04-29 10:52:43 -070088 ,
Jim Stichnothfac55172014-10-01 13:06:21 -070089 ICETYPE_TABLE
Jim Stichnothf7c9a142014-04-29 10:52:43 -070090#undef X
Karl Schimpfd6064a12014-08-27 15:34:58 -070091};
Jim Stichnothf7c9a142014-04-29 10:52:43 -070092
Karl Schimpfd6064a12014-08-27 15:34:58 -070093struct 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 Schimpf41689df2014-09-10 14:36:07 -0700102 bool TypeIsLoadStoreType;
Karl Schimpf83f9f0c2014-09-05 08:30:55 -0700103 Type CompareResultType;
Karl Schimpfd6064a12014-08-27 15:34:58 -0700104};
105
106const TypePropertyFields TypePropertiesTable[] = {
Karl Schimpf41689df2014-09-10 14:36:07 -0700107#define X(tag, IsVec, IsInt, IsFloat, IsIntArith, IsLoadStore, CompareResult) \
Karl Schimpfd6064a12014-08-27 15:34:58 -0700108 { \
109 IsVec, IsInt, IsInt && !IsVec, IsInt && IsVec, IsIntArith, IsFloat, \
Karl Schimpf41689df2014-09-10 14:36:07 -0700110 IsFloat && !IsVec, IsFloat && IsVec, IsLoadStore, CompareResult \
Karl Schimpfd6064a12014-08-27 15:34:58 -0700111 } \
112 ,
Jim Stichnothfac55172014-10-01 13:06:21 -0700113 ICETYPE_PROPS_TABLE
Karl Schimpfd6064a12014-08-27 15:34:58 -0700114#undef X
115};
Jim Stichnothf7c9a142014-04-29 10:52:43 -0700116
117} // end anonymous namespace
118
119size_t typeWidthInBytes(Type Ty) {
Jim Stichnothf7c9a142014-04-29 10:52:43 -0700120 size_t Index = static_cast<size_t>(Ty);
Karl Schimpfd6064a12014-08-27 15:34:58 -0700121 if (Index < IceType_NUM)
122 return TypeAttributes[Index].TypeWidthInBytes;
123 llvm_unreachable("Invalid type for typeWidthInBytes()");
124 return 0;
Jim Stichnothf7c9a142014-04-29 10:52:43 -0700125}
126
127size_t typeAlignInBytes(Type Ty) {
Jim Stichnothf7c9a142014-04-29 10:52:43 -0700128 size_t Index = static_cast<size_t>(Ty);
Karl Schimpfd6064a12014-08-27 15:34:58 -0700129 if (Index < IceType_NUM)
130 return TypeAttributes[Index].TypeAlignInBytes;
131 llvm_unreachable("Invalid type for typeAlignInBytes()");
132 return 1;
Jim Stichnothf7c9a142014-04-29 10:52:43 -0700133}
134
Matt Wala928f1292014-07-07 16:50:46 -0700135size_t typeNumElements(Type Ty) {
Matt Wala928f1292014-07-07 16:50:46 -0700136 size_t Index = static_cast<size_t>(Ty);
Karl Schimpfd6064a12014-08-27 15:34:58 -0700137 if (Index < IceType_NUM)
138 return TypeAttributes[Index].TypeNumElements;
139 llvm_unreachable("Invalid type for typeNumElements()");
140 return 1;
Matt Wala928f1292014-07-07 16:50:46 -0700141}
142
143Type typeElementType(Type Ty) {
Matt Wala928f1292014-07-07 16:50:46 -0700144 size_t Index = static_cast<size_t>(Ty);
Karl Schimpfd6064a12014-08-27 15:34:58 -0700145 if (Index < IceType_NUM)
146 return TypeAttributes[Index].TypeElementType;
147 llvm_unreachable("Invalid type for typeElementType()");
148 return IceType_void;
Matt Wala928f1292014-07-07 16:50:46 -0700149}
150
Karl Schimpfd6064a12014-08-27 15:34:58 -0700151bool 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
159bool 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
167bool 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
175bool 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
183bool 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
191bool 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
199bool 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
207bool 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 Schimpf41689df2014-09-10 14:36:07 -0700215bool 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 Schimpf83f9f0c2014-09-05 08:30:55 -0700223Type 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 Schimpfd1a971a2014-09-17 15:38:17 -0700231SizeT getScalarIntBitWidth(Type Ty) {
232 assert(isScalarIntegerType(Ty));
Karl Schimpf645aa1a2014-10-08 09:05:53 -0700233 if (Ty == IceType_i1)
Karl Schimpfd1a971a2014-09-17 15:38:17 -0700234 return 1;
235 return typeWidthInBytes(Ty) * CHAR_BIT;
236}
237
Karl Schimpfd6064a12014-08-27 15:34:58 -0700238// ======================== Dump routines ======================== //
239
Jim Stichnoth78282f62014-07-27 23:14:00 -0700240const char *typeString(Type Ty) {
Jim Stichnothf7c9a142014-04-29 10:52:43 -0700241 size_t Index = static_cast<size_t>(Ty);
Karl Schimpfd6064a12014-08-27 15:34:58 -0700242 if (Index < IceType_NUM)
Jim Stichnoth78282f62014-07-27 23:14:00 -0700243 return TypeAttributes[Index].DisplayString;
Jim Stichnoth78282f62014-07-27 23:14:00 -0700244 llvm_unreachable("Invalid type for typeString");
245 return "???";
Jim Stichnothf7c9a142014-04-29 10:52:43 -0700246}
247
Karl Schimpf645aa1a2014-10-08 09:05:53 -0700248void 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 Stichnothf7c9a142014-04-29 10:52:43 -0700262} // end of namespace Ice