blob: cfa8e50245849583aa7dc357d89e3d8361d3445d [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
Jim Stichnoth92a6e5b2015-12-02 16:52:44 -080011/// \brief Defines a few attributes of Subzero primitive types.
Andrew Scull9612d322015-07-06 14:53:25 -070012///
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"
Nicolas Capens32f9cce2016-10-19 01:24:27 -040018#include "IceTargetLowering.h"
John Porto67f8de92015-06-25 10:14:17 -070019
Nicolas Capensdbd600d2016-09-01 16:53:49 -040020#include "llvm/Support/ErrorHandling.h"
21
John Porto36d6aa62016-02-26 07:19:59 -080022#include <climits>
23
Jim Stichnothf7c9a142014-04-29 10:52:43 -070024namespace Ice {
25
26namespace {
27
Karl Schimpfb262c5e2014-10-27 14:41:57 -070028const char *TargetArchName[] = {
Jan Voung08c3bcd2014-12-01 17:55:16 -080029#define X(tag, str, is_elf64, e_machine, e_flags) str,
30 TARGETARCH_TABLE
Karl Schimpfb262c5e2014-10-27 14:41:57 -070031#undef X
32};
33
Jim Stichnothfac55172014-10-01 13:06:21 -070034// Show tags match between ICETYPE_TABLE and ICETYPE_PROPS_TABLE.
Karl Schimpfd6064a12014-08-27 15:34:58 -070035
Jim Stichnothfac55172014-10-01 13:06:21 -070036// Define a temporary set of enum values based on ICETYPE_TABLE
37enum {
Jim Stichnoth2544d4d2016-01-22 13:07:46 -080038#define X(tag, sizeLog2, align, elts, elty, str, rcstr) _table_tag_##tag,
Jim Stichnothfac55172014-10-01 13:06:21 -070039 ICETYPE_TABLE
Karl Schimpfd6064a12014-08-27 15:34:58 -070040#undef X
Jim Stichnothfac55172014-10-01 13:06:21 -070041 _enum_table_tag_Names
42};
43// Define a temporary set of enum values based on ICETYPE_PROPS_TABLE
44enum {
Karl Schimpfff94f592015-09-18 10:37:44 -070045#define X(tag, IsVec, IsInt, IsFloat, IsIntArith, IsLoadStore, IsParam, \
46 CompareResult) \
Karl Schimpf41689df2014-09-10 14:36:07 -070047 _props_table_tag_##tag,
Jim Stichnothfac55172014-10-01 13:06:21 -070048 ICETYPE_PROPS_TABLE
Karl Schimpfd6064a12014-08-27 15:34:58 -070049#undef X
Jim Stichnothfac55172014-10-01 13:06:21 -070050 _enum_props_table_tag_Names
51};
Karl Schimpfd6064a12014-08-27 15:34:58 -070052// Assert that tags in ICETYPE_TABLE are also in ICETYPE_PROPS_TABLE.
Jim Stichnoth2544d4d2016-01-22 13:07:46 -080053#define X(tag, sizeLog2, align, elts, elty, str, rcstr) \
Jim Stichnothfac55172014-10-01 13:06:21 -070054 static_assert( \
55 (unsigned)_table_tag_##tag == (unsigned)_props_table_tag_##tag, \
56 "Inconsistency between ICETYPE_PROPS_TABLE and ICETYPE_TABLE");
JF Bastien8427ea22015-01-27 12:56:49 -080057ICETYPE_TABLE
Karl Schimpfd6064a12014-08-27 15:34:58 -070058#undef X
59// Assert that tags in ICETYPE_PROPS_TABLE is in ICETYPE_TABLE.
Karl Schimpfff94f592015-09-18 10:37:44 -070060#define X(tag, IsVec, IsInt, IsFloat, IsIntArith, IsLoadStore, IsParam, \
61 CompareResult) \
Jim Stichnothfac55172014-10-01 13:06:21 -070062 static_assert( \
63 (unsigned)_table_tag_##tag == (unsigned)_props_table_tag_##tag, \
64 "Inconsistency between ICETYPE_PROPS_TABLE and ICETYPE_TABLE");
65ICETYPE_PROPS_TABLE
Karl Schimpfd6064a12014-08-27 15:34:58 -070066#undef X
67
Andrew Scull57e12682015-09-16 11:30:19 -070068// Show vector definitions match in ICETYPE_TABLE and ICETYPE_PROPS_TABLE.
Karl Schimpfd6064a12014-08-27 15:34:58 -070069
Jim Stichnothfac55172014-10-01 13:06:21 -070070// Define constants for each element size in ICETYPE_TABLE.
71enum {
Jim Stichnoth2544d4d2016-01-22 13:07:46 -080072#define X(tag, sizeLog2, align, elts, elty, str, rcstr) \
73 _table_elts_##tag = elts,
Jim Stichnothfac55172014-10-01 13:06:21 -070074 ICETYPE_TABLE
Karl Schimpfd6064a12014-08-27 15:34:58 -070075#undef X
Jim Stichnothfac55172014-10-01 13:06:21 -070076 _enum_table_elts_Elements = 0
77};
78// Define constants for boolean flag if vector in ICETYPE_PROPS_TABLE.
79enum {
Karl Schimpfff94f592015-09-18 10:37:44 -070080#define X(tag, IsVec, IsInt, IsFloat, IsIntArith, IsLoadStore, IsParam, \
81 CompareResult) \
Karl Schimpf41689df2014-09-10 14:36:07 -070082 _props_table_IsVec_##tag = IsVec,
Jim Stichnothfac55172014-10-01 13:06:21 -070083 ICETYPE_PROPS_TABLE
Karl Schimpfd6064a12014-08-27 15:34:58 -070084#undef X
Jim Stichnothfac55172014-10-01 13:06:21 -070085};
Karl Schimpfd6064a12014-08-27 15:34:58 -070086// Verify that the number of vector elements is consistent with IsVec.
Karl Schimpfff94f592015-09-18 10:37:44 -070087#define X(tag, IsVec, IsInt, IsFloat, IsIntArith, IsLoadStore, IsParam, \
88 CompareResult) \
Jim Stichnothfac55172014-10-01 13:06:21 -070089 static_assert((_table_elts_##tag > 1) == _props_table_IsVec_##tag, \
90 "Inconsistent vector specification in ICETYPE_PROPS_TABLE");
JF Bastien8427ea22015-01-27 12:56:49 -080091ICETYPE_PROPS_TABLE
Karl Schimpfd6064a12014-08-27 15:34:58 -070092#undef X
Karl Schimpfd6064a12014-08-27 15:34:58 -070093
94struct TypeAttributeFields {
Andrew Scull87f80c12015-07-20 10:19:16 -070095 int8_t TypeWidthInBytesLog2;
Jim Stichnothf7c9a142014-04-29 10:52:43 -070096 size_t TypeAlignInBytes;
Matt Wala928f1292014-07-07 16:50:46 -070097 size_t TypeNumElements;
98 Type TypeElementType;
Jim Stichnothf7c9a142014-04-29 10:52:43 -070099 const char *DisplayString;
Jim Stichnoth2544d4d2016-01-22 13:07:46 -0800100 const char *RegClassString;
Karl Schimpfd6064a12014-08-27 15:34:58 -0700101};
102
103const struct TypeAttributeFields TypeAttributes[] = {
Jim Stichnoth2544d4d2016-01-22 13:07:46 -0800104#define X(tag, sizeLog2, align, elts, elty, str, rcstr) \
105 { sizeLog2, align, elts, IceType_##elty, str, rcstr } \
Jim Stichnothf7c9a142014-04-29 10:52:43 -0700106 ,
Jim Stichnothdd842db2015-01-27 12:53:53 -0800107 ICETYPE_TABLE
Jim Stichnothf7c9a142014-04-29 10:52:43 -0700108#undef X
Karl Schimpfd6064a12014-08-27 15:34:58 -0700109};
Jim Stichnothf7c9a142014-04-29 10:52:43 -0700110
Karl Schimpfd6064a12014-08-27 15:34:58 -0700111struct TypePropertyFields {
112 bool TypeIsVectorType;
113 bool TypeIsIntegerType;
114 bool TypeIsScalarIntegerType;
115 bool TypeIsVectorIntegerType;
116 bool TypeIsIntegerArithmeticType;
117 bool TypeIsFloatingType;
118 bool TypeIsScalarFloatingType;
119 bool TypeIsVectorFloatingType;
Nicolas Capense9655302017-02-13 10:21:20 -0500120 bool TypeIsBooleanType;
Karl Schimpfff94f592015-09-18 10:37:44 -0700121 bool TypeIsCallParameterType;
Karl Schimpf83f9f0c2014-09-05 08:30:55 -0700122 Type CompareResultType;
Karl Schimpfd6064a12014-08-27 15:34:58 -0700123};
124
125const TypePropertyFields TypePropertiesTable[] = {
Nicolas Capense9655302017-02-13 10:21:20 -0500126#define X(tag, IsVec, IsInt, IsFloat, IsIntArith, IsBoolean, IsParam, \
Karl Schimpfff94f592015-09-18 10:37:44 -0700127 CompareResult) \
Karl Schimpfd6064a12014-08-27 15:34:58 -0700128 { \
Mircea Trofin59c6f5a2015-04-06 16:06:47 -0700129 IsVec, IsInt, IsInt & !IsVec, IsInt & IsVec, IsIntArith, IsFloat, \
Nicolas Capense9655302017-02-13 10:21:20 -0500130 IsFloat & !IsVec, IsFloat & IsVec, IsBoolean, IsParam, \
Jim Stichnothc59288b2015-11-09 11:38:40 -0800131 IceType_##CompareResult \
Karl Schimpfd6064a12014-08-27 15:34:58 -0700132 } \
133 ,
Jim Stichnothdd842db2015-01-27 12:53:53 -0800134 ICETYPE_PROPS_TABLE
Karl Schimpfd6064a12014-08-27 15:34:58 -0700135#undef X
136};
Jim Stichnothf7c9a142014-04-29 10:52:43 -0700137
138} // end anonymous namespace
139
Karl Schimpfb262c5e2014-10-27 14:41:57 -0700140const char *targetArchString(const TargetArch Arch) {
Jim Stichnoth2d6c8262016-02-07 09:50:27 -0800141 if (Arch < TargetArch_NUM)
142 return TargetArchName[Arch];
Karl Schimpfb262c5e2014-10-27 14:41:57 -0700143 llvm_unreachable("Invalid target arch for targetArchString");
144 return "???";
145}
146
Jim Stichnothf7c9a142014-04-29 10:52:43 -0700147size_t typeWidthInBytes(Type Ty) {
Andrew Scull87f80c12015-07-20 10:19:16 -0700148 int8_t Shift = typeWidthInBytesLog2(Ty);
149 return (Shift < 0) ? 0 : 1 << Shift;
150}
151
152int8_t typeWidthInBytesLog2(Type Ty) {
Jim Stichnoth2d6c8262016-02-07 09:50:27 -0800153 if (Ty < IceType_NUM)
154 return TypeAttributes[Ty].TypeWidthInBytesLog2;
Andrew Scull87f80c12015-07-20 10:19:16 -0700155 llvm_unreachable("Invalid type for typeWidthInBytesLog2()");
Karl Schimpfd6064a12014-08-27 15:34:58 -0700156 return 0;
Jim Stichnothf7c9a142014-04-29 10:52:43 -0700157}
158
159size_t typeAlignInBytes(Type Ty) {
Jim Stichnoth2d6c8262016-02-07 09:50:27 -0800160 if (Ty < IceType_NUM)
161 return TypeAttributes[Ty].TypeAlignInBytes;
Karl Schimpfd6064a12014-08-27 15:34:58 -0700162 llvm_unreachable("Invalid type for typeAlignInBytes()");
163 return 1;
Jim Stichnothf7c9a142014-04-29 10:52:43 -0700164}
165
Matt Wala928f1292014-07-07 16:50:46 -0700166size_t typeNumElements(Type Ty) {
Jim Stichnoth2d6c8262016-02-07 09:50:27 -0800167 if (Ty < IceType_NUM)
168 return TypeAttributes[Ty].TypeNumElements;
Karl Schimpfd6064a12014-08-27 15:34:58 -0700169 llvm_unreachable("Invalid type for typeNumElements()");
170 return 1;
Matt Wala928f1292014-07-07 16:50:46 -0700171}
172
173Type typeElementType(Type Ty) {
Jim Stichnoth2d6c8262016-02-07 09:50:27 -0800174 if (Ty < IceType_NUM)
175 return TypeAttributes[Ty].TypeElementType;
Karl Schimpfd6064a12014-08-27 15:34:58 -0700176 llvm_unreachable("Invalid type for typeElementType()");
177 return IceType_void;
Matt Wala928f1292014-07-07 16:50:46 -0700178}
179
Nicolas Capens32f9cce2016-10-19 01:24:27 -0400180Type getPointerType() { return TargetLowering::getPointerType(); }
181
Karl Schimpfd6064a12014-08-27 15:34:58 -0700182bool isVectorType(Type Ty) {
Jim Stichnoth2d6c8262016-02-07 09:50:27 -0800183 if (Ty < IceType_NUM)
184 return TypePropertiesTable[Ty].TypeIsVectorType;
Karl Schimpfd6064a12014-08-27 15:34:58 -0700185 llvm_unreachable("Invalid type for isVectorType()");
186 return false;
187}
188
Nicolas Capense9655302017-02-13 10:21:20 -0500189bool isBooleanType(Type Ty) {
190 if (Ty < IceType_NUM)
191 return TypePropertiesTable[Ty].TypeIsBooleanType;
192 llvm_unreachable("Invalid type for isBooleanType()");
193 return false;
194}
195
Karl Schimpfd6064a12014-08-27 15:34:58 -0700196bool isIntegerType(Type Ty) {
Jim Stichnoth2d6c8262016-02-07 09:50:27 -0800197 if (Ty < IceType_NUM)
198 return TypePropertiesTable[Ty].TypeIsIntegerType;
Karl Schimpfd6064a12014-08-27 15:34:58 -0700199 llvm_unreachable("Invalid type for isIntegerType()");
200 return false;
201}
202
203bool isScalarIntegerType(Type Ty) {
Jim Stichnoth2d6c8262016-02-07 09:50:27 -0800204 if (Ty < IceType_NUM)
205 return TypePropertiesTable[Ty].TypeIsScalarIntegerType;
Karl Schimpfd6064a12014-08-27 15:34:58 -0700206 llvm_unreachable("Invalid type for isScalIntegerType()");
207 return false;
208}
209
210bool isVectorIntegerType(Type Ty) {
Jim Stichnoth2d6c8262016-02-07 09:50:27 -0800211 if (Ty < IceType_NUM)
212 return TypePropertiesTable[Ty].TypeIsVectorIntegerType;
Karl Schimpfd6064a12014-08-27 15:34:58 -0700213 llvm_unreachable("Invalid type for isVectorIntegerType()");
214 return false;
215}
216
217bool isIntegerArithmeticType(Type Ty) {
Jim Stichnoth2d6c8262016-02-07 09:50:27 -0800218 if (Ty < IceType_NUM)
219 return TypePropertiesTable[Ty].TypeIsIntegerArithmeticType;
Karl Schimpfd6064a12014-08-27 15:34:58 -0700220 llvm_unreachable("Invalid type for isIntegerArithmeticType()");
221 return false;
222}
223
224bool isFloatingType(Type Ty) {
Jim Stichnoth2d6c8262016-02-07 09:50:27 -0800225 if (Ty < IceType_NUM)
226 return TypePropertiesTable[Ty].TypeIsFloatingType;
Karl Schimpfd6064a12014-08-27 15:34:58 -0700227 llvm_unreachable("Invalid type for isFloatingType()");
228 return false;
229}
230
231bool isScalarFloatingType(Type Ty) {
Jim Stichnoth2d6c8262016-02-07 09:50:27 -0800232 if (Ty < IceType_NUM)
233 return TypePropertiesTable[Ty].TypeIsScalarFloatingType;
Karl Schimpfd6064a12014-08-27 15:34:58 -0700234 llvm_unreachable("Invalid type for isScalarFloatingType()");
235 return false;
236}
237
238bool isVectorFloatingType(Type Ty) {
Jim Stichnoth2d6c8262016-02-07 09:50:27 -0800239 if (Ty < IceType_NUM)
240 return TypePropertiesTable[Ty].TypeIsVectorFloatingType;
Karl Schimpfd6064a12014-08-27 15:34:58 -0700241 llvm_unreachable("Invalid type for isVectorFloatingType()");
242 return false;
243}
244
Karl Schimpf41689df2014-09-10 14:36:07 -0700245bool isLoadStoreType(Type Ty) {
Jim Stichnoth2d6c8262016-02-07 09:50:27 -0800246 if (Ty < IceType_NUM)
Nicolas Capense9655302017-02-13 10:21:20 -0500247 return Ty != IceType_void && !isBooleanType(Ty);
Karl Schimpf41689df2014-09-10 14:36:07 -0700248 llvm_unreachable("Invalid type for isLoadStoreType()");
249 return false;
250}
251
Karl Schimpfff94f592015-09-18 10:37:44 -0700252bool isCallParameterType(Type Ty) {
Jim Stichnoth2d6c8262016-02-07 09:50:27 -0800253 if (Ty < IceType_NUM)
254 return TypePropertiesTable[Ty].TypeIsCallParameterType;
Karl Schimpfff94f592015-09-18 10:37:44 -0700255 llvm_unreachable("Invalid type for isCallParameterType()");
256 return false;
257}
258
Karl Schimpf83f9f0c2014-09-05 08:30:55 -0700259Type getCompareResultType(Type Ty) {
Jim Stichnoth2d6c8262016-02-07 09:50:27 -0800260 if (Ty < IceType_NUM)
261 return TypePropertiesTable[Ty].CompareResultType;
Karl Schimpf83f9f0c2014-09-05 08:30:55 -0700262 llvm_unreachable("Invalid type for getCompareResultType");
263 return IceType_void;
264}
265
Karl Schimpfd1a971a2014-09-17 15:38:17 -0700266SizeT getScalarIntBitWidth(Type Ty) {
267 assert(isScalarIntegerType(Ty));
Karl Schimpf645aa1a2014-10-08 09:05:53 -0700268 if (Ty == IceType_i1)
Karl Schimpfd1a971a2014-09-17 15:38:17 -0700269 return 1;
270 return typeWidthInBytes(Ty) * CHAR_BIT;
271}
272
Karl Schimpfd6064a12014-08-27 15:34:58 -0700273// ======================== Dump routines ======================== //
274
Jim Stichnoth78282f62014-07-27 23:14:00 -0700275const char *typeString(Type Ty) {
Jim Stichnoth2d6c8262016-02-07 09:50:27 -0800276 if (Ty < IceType_NUM)
277 return TypeAttributes[Ty].DisplayString;
Jim Stichnoth78282f62014-07-27 23:14:00 -0700278 llvm_unreachable("Invalid type for typeString");
279 return "???";
Jim Stichnothf7c9a142014-04-29 10:52:43 -0700280}
281
Jim Stichnoth2544d4d2016-01-22 13:07:46 -0800282const char *regClassString(RegClass C) {
283 if (static_cast<size_t>(C) < IceType_NUM)
284 return TypeAttributes[C].RegClassString;
285 llvm_unreachable("Invalid type for regClassString");
286 return "???";
287}
288
Karl Schimpf645aa1a2014-10-08 09:05:53 -0700289void FuncSigType::dump(Ostream &Stream) const {
Jim Stichnoth20b71f52015-06-24 15:52:24 -0700290 if (!BuildDefs::dump())
Karl Schimpfb6c96af2014-11-17 10:58:39 -0800291 return;
Karl Schimpf645aa1a2014-10-08 09:05:53 -0700292 Stream << ReturnType << " (";
293 bool IsFirst = true;
294 for (const Type ArgTy : ArgList) {
295 if (IsFirst) {
296 IsFirst = false;
297 } else {
298 Stream << ", ";
299 }
300 Stream << ArgTy;
301 }
302 Stream << ")";
303}
304
Jim Stichnothf7c9a142014-04-29 10:52:43 -0700305} // end of namespace Ice