blob: 9b736d113d684bd6278f4e831c6bc56b000a34ba [file] [log] [blame]
Chris Lattner1e27fe12006-10-14 07:06:20 +00001//===--- TargetInfo.cpp - Information about Target machine ----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner1e27fe12006-10-14 07:06:20 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the TargetInfo and TargetInfoImpl interfaces.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Basic/TargetInfo.h"
Sven van Haastregtefb4d4c2017-08-15 09:38:18 +000015#include "clang/AST/Type.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000016#include "clang/Basic/AddressSpaces.h"
Jordan Rosea7d03842013-02-08 22:30:41 +000017#include "clang/Basic/CharInfo.h"
John Thompsoned4e2952009-11-05 20:14:16 +000018#include "clang/Basic/LangOptions.h"
Chris Lattnerec0a6d92007-09-22 18:29:59 +000019#include "llvm/ADT/APFloat.h"
Anders Carlsson290aa852007-11-25 00:25:21 +000020#include "llvm/ADT/STLExtras.h"
David Blaikie76bd3c82011-09-23 05:35:21 +000021#include "llvm/Support/ErrorHandling.h"
Saleem Abdulrasool729379a2017-10-06 23:09:55 +000022#include "llvm/Support/TargetParser.h"
Chris Lattnerc4f02b62008-04-06 04:02:29 +000023#include <cstdlib>
Chris Lattner1e27fe12006-10-14 07:06:20 +000024using namespace clang;
25
Peter Collingbourne599cb8e2011-03-18 22:38:29 +000026static const LangAS::Map DefaultAddrSpaceMap = { 0 };
27
Chris Lattner1df27862008-03-08 08:59:43 +000028// TargetInfo Constructor.
Benjamin Kramerb1276b42013-06-29 16:37:14 +000029TargetInfo::TargetInfo(const llvm::Triple &T) : TargetOpts(), Triple(T) {
Daniel Dunbar3d9289c2010-04-15 06:18:39 +000030 // Set defaults. Defaults are set for a 32-bit RISC platform, like PPC or
31 // SPARC. These should be overridden by concrete targets as needed.
Matt Arsenaultf333de32016-09-07 07:08:02 +000032 BigEndian = !T.isLittleEndian();
Eli Friedmand88c8a12009-04-19 21:38:35 +000033 TLSSupported = true;
Chris Lattner1a8f3942010-04-23 16:29:58 +000034 NoAsmVariants = false;
Nemanja Ivanovicbb1ea2d2016-05-09 08:52:33 +000035 HasFloat128 = false;
Chris Lattner5e2ef0c2008-05-09 06:08:39 +000036 PointerWidth = PointerAlign = 32;
Roman Divacky965b0b72011-01-06 08:27:10 +000037 BoolWidth = BoolAlign = 8;
Chris Lattnerb781dc792008-05-08 05:58:21 +000038 IntWidth = IntAlign = 32;
Chris Lattner5a9aaaf2008-05-09 05:50:02 +000039 LongWidth = LongAlign = 32;
40 LongLongWidth = LongLongAlign = 64;
Nick Lewyckyf59e1292011-12-16 22:34:14 +000041 SuitableAlign = 64;
Ulrich Weigandca3cb7f2015-04-21 17:29:35 +000042 DefaultAlignForAttributeAligned = 128;
Ulrich Weigandfa806422013-05-06 16:23:57 +000043 MinGlobalAlign = 0;
Richard Smith59139022016-09-30 22:41:36 +000044 // From the glibc documentation, on GNU systems, malloc guarantees 16-byte
45 // alignment on 64-bit systems and 8-byte alignment on 32-bit systems. See
46 // https://www.gnu.org/software/libc/manual/html_node/Malloc-Examples.html
47 if (T.isGNUEnvironment())
48 NewAlign = Triple.isArch64Bit() ? 128 : Triple.isArch32Bit() ? 64 : 0;
49 else
50 NewAlign = 0; // Infer from basic type alignment.
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +000051 HalfWidth = 16;
52 HalfAlign = 16;
Eli Friedmanb5366062008-05-20 14:21:01 +000053 FloatWidth = 32;
54 FloatAlign = 32;
Nate Begeman65bea232008-04-18 17:17:24 +000055 DoubleWidth = 64;
Eli Friedmanb5366062008-05-20 14:21:01 +000056 DoubleAlign = 64;
57 LongDoubleWidth = 64;
58 LongDoubleAlign = 64;
Nemanja Ivanovicbb1ea2d2016-05-09 08:52:33 +000059 Float128Align = 128;
Rafael Espindolae971b9a2010-06-04 23:15:27 +000060 LargeArrayMinWidth = 0;
61 LargeArrayAlign = 0;
Eli Friedman4b72fdd2011-10-14 20:59:01 +000062 MaxAtomicPromoteWidth = MaxAtomicInlineWidth = 0;
Chad Rosiercc40ea72012-07-13 23:57:43 +000063 MaxVectorAlign = 0;
Paul Robinsond30e2ee2015-07-14 20:52:32 +000064 MaxTLSAlign = 0;
Alexey Bataev00396512015-07-02 03:40:19 +000065 SimdDefaultAlign = 0;
Anders Carlsson2a79a902008-10-31 16:05:19 +000066 SizeType = UnsignedLong;
Eli Friedmand50881c2008-11-02 02:43:55 +000067 PtrDiffType = SignedLong;
Sanjiv Guptad7959242008-10-31 09:52:39 +000068 IntMaxType = SignedLongLong;
Chris Lattner7e4c81c2009-02-13 22:28:55 +000069 IntPtrType = SignedLong;
Eli Friedmand50881c2008-11-02 02:43:55 +000070 WCharType = SignedInt;
Chris Lattner67204922009-10-21 04:59:34 +000071 WIntType = SignedInt;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +000072 Char16Type = UnsignedShort;
73 Char32Type = UnsignedInt;
Eli Friedman2857ccb2009-07-01 03:36:11 +000074 Int64Type = SignedLongLong;
Edward O'Callaghan847f2a12009-11-21 00:49:54 +000075 SigAtomicType = SignedInt;
Eli Friedman4e91899e2012-11-27 02:58:24 +000076 ProcessIDType = SignedInt;
Fariborz Jahanian29898f42012-04-16 21:03:30 +000077 UseSignedCharForObjCBool = true;
Daniel Dunbar1da65112010-04-15 15:06:18 +000078 UseBitFieldTypeAlignment = true;
Chad Rosier18903ee2011-08-04 01:21:14 +000079 UseZeroLengthBitfieldAlignment = false;
Sunil Srivastava0ce2f222016-02-05 20:50:02 +000080 UseExplicitBitFieldAlignment = true;
Chad Rosier18903ee2011-08-04 01:21:14 +000081 ZeroLengthBitfieldBoundary = 0;
Stephan Bergmann17c7f702016-12-14 11:57:17 +000082 HalfFormat = &llvm::APFloat::IEEEhalf();
83 FloatFormat = &llvm::APFloat::IEEEsingle();
84 DoubleFormat = &llvm::APFloat::IEEEdouble();
85 LongDoubleFormat = &llvm::APFloat::IEEEdouble();
86 Float128Format = &llvm::APFloat::IEEEquad();
Roman Divacky178e01602011-02-10 16:52:03 +000087 MCountName = "mcount";
Abramo Bagnara41bfb662011-09-08 14:20:25 +000088 RegParmMax = 0;
89 SSERegParmMax = 0;
Daniel Dunbarbd606522010-05-27 00:35:16 +000090 HasAlignMac68kSupport = false;
Charles Davisc7d5c942015-09-17 20:55:33 +000091 HasBuiltinMSVaList = false;
Pirama Arumuga Nainarbb846a32016-07-27 19:01:51 +000092 IsRenderScriptTarget = false;
Daniel Dunbar6f2e8392010-07-14 23:39:36 +000093
94 // Default to no types using fpret.
95 RealTypeUsesObjCFPRet = 0;
John McCall86353412010-08-21 22:46:04 +000096
Anders Carlsson2f1a6c32011-10-31 16:27:11 +000097 // Default to not using fp2ret for __Complex long double
98 ComplexLongDoubleUsesFP2Ret = false;
99
Hans Wennborgc9bd88e2014-01-14 19:35:09 +0000100 // Set the C++ ABI based on the triple.
Saleem Abdulrasool377066a2014-03-27 22:50:18 +0000101 TheCXXABI.set(Triple.isKnownWindowsMSVCEnvironment()
Hans Wennborgc9bd88e2014-01-14 19:35:09 +0000102 ? TargetCXXABI::Microsoft
103 : TargetCXXABI::GenericItanium);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000104
105 // Default to an empty address space map.
106 AddrSpaceMap = &DefaultAddrSpaceMap;
David Tweed31d09b02013-09-13 12:04:22 +0000107 UseAddrSpaceMapMangling = false;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000108
109 // Default to an unknown platform name.
110 PlatformName = "unknown";
111 PlatformMinVersion = VersionTuple();
Chris Lattner1df27862008-03-08 08:59:43 +0000112}
113
Chris Lattnerc3a669b2008-03-08 08:24:01 +0000114// Out of line virtual dtor for TargetInfo.
Angel Garcia Gomez637d1e62015-10-20 13:23:58 +0000115TargetInfo::~TargetInfo() {}
Chris Lattner2194ddc2006-10-14 18:32:26 +0000116
Chris Lattnera91c30f2009-02-06 05:04:11 +0000117/// getTypeName - Return the user string for the specified integer type enum.
118/// For example, SignedShort -> "short".
119const char *TargetInfo::getTypeName(IntType T) {
120 switch (T) {
David Blaikie83d382b2011-09-23 05:06:16 +0000121 default: llvm_unreachable("not an integer!");
Joerg Sonnenberger3d9478c2014-07-28 21:06:22 +0000122 case SignedChar: return "signed char";
Stepan Dyatkovskiy5a637922013-09-05 11:23:21 +0000123 case UnsignedChar: return "unsigned char";
Chris Lattnera91c30f2009-02-06 05:04:11 +0000124 case SignedShort: return "short";
125 case UnsignedShort: return "unsigned short";
126 case SignedInt: return "int";
127 case UnsignedInt: return "unsigned int";
128 case SignedLong: return "long int";
129 case UnsignedLong: return "long unsigned int";
130 case SignedLongLong: return "long long int";
131 case UnsignedLongLong: return "long long unsigned int";
132 }
133}
134
Chris Lattner72cdcac2009-10-21 06:24:21 +0000135/// getTypeConstantSuffix - Return the constant suffix for the specified
136/// integer type enum. For example, SignedLong -> "L".
Joerg Sonnenberger587deea2014-07-17 20:12:32 +0000137const char *TargetInfo::getTypeConstantSuffix(IntType T) const {
Chris Lattner72cdcac2009-10-21 06:24:21 +0000138 switch (T) {
David Blaikie83d382b2011-09-23 05:06:16 +0000139 default: llvm_unreachable("not an integer!");
Stepan Dyatkovskiy5a637922013-09-05 11:23:21 +0000140 case SignedChar:
Chris Lattner72cdcac2009-10-21 06:24:21 +0000141 case SignedShort:
142 case SignedInt: return "";
143 case SignedLong: return "L";
144 case SignedLongLong: return "LL";
Stepan Dyatkovskiy5a637922013-09-05 11:23:21 +0000145 case UnsignedChar:
Joerg Sonnenberger587deea2014-07-17 20:12:32 +0000146 if (getCharWidth() < getIntWidth())
147 return "";
Galina Kistanova96088f42017-06-03 06:38:22 +0000148 LLVM_FALLTHROUGH;
Chris Lattner72cdcac2009-10-21 06:24:21 +0000149 case UnsignedShort:
Joerg Sonnenberger587deea2014-07-17 20:12:32 +0000150 if (getShortWidth() < getIntWidth())
151 return "";
Galina Kistanova96088f42017-06-03 06:38:22 +0000152 LLVM_FALLTHROUGH;
Chris Lattner72cdcac2009-10-21 06:24:21 +0000153 case UnsignedInt: return "U";
154 case UnsignedLong: return "UL";
155 case UnsignedLongLong: return "ULL";
156 }
157}
158
Joerg Sonnenbergerbe324f92014-07-15 11:30:00 +0000159/// getTypeFormatModifier - Return the printf format modifier for the
160/// specified integer type enum. For example, SignedLong -> "l".
161
162const char *TargetInfo::getTypeFormatModifier(IntType T) {
163 switch (T) {
164 default: llvm_unreachable("not an integer!");
165 case SignedChar:
166 case UnsignedChar: return "hh";
167 case SignedShort:
168 case UnsignedShort: return "h";
169 case SignedInt:
170 case UnsignedInt: return "";
171 case SignedLong:
172 case UnsignedLong: return "l";
173 case SignedLongLong:
174 case UnsignedLongLong: return "ll";
175 }
176}
177
Michael J. Spencerfeb799c2010-10-18 07:10:59 +0000178/// getTypeWidth - Return the width (in bits) of the specified integer type
Chris Lattner72cdcac2009-10-21 06:24:21 +0000179/// enum. For example, SignedInt -> getIntWidth().
180unsigned TargetInfo::getTypeWidth(IntType T) const {
181 switch (T) {
David Blaikie83d382b2011-09-23 05:06:16 +0000182 default: llvm_unreachable("not an integer!");
Stepan Dyatkovskiy5a637922013-09-05 11:23:21 +0000183 case SignedChar:
184 case UnsignedChar: return getCharWidth();
Chris Lattnere4a8c642009-11-05 21:21:32 +0000185 case SignedShort:
Chris Lattner72cdcac2009-10-21 06:24:21 +0000186 case UnsignedShort: return getShortWidth();
Chris Lattnere4a8c642009-11-05 21:21:32 +0000187 case SignedInt:
Chris Lattner72cdcac2009-10-21 06:24:21 +0000188 case UnsignedInt: return getIntWidth();
Chris Lattnere4a8c642009-11-05 21:21:32 +0000189 case SignedLong:
Chris Lattner72cdcac2009-10-21 06:24:21 +0000190 case UnsignedLong: return getLongWidth();
Chris Lattnere4a8c642009-11-05 21:21:32 +0000191 case SignedLongLong:
Chris Lattner72cdcac2009-10-21 06:24:21 +0000192 case UnsignedLongLong: return getLongLongWidth();
193 };
194}
195
Stepan Dyatkovskiy5a637922013-09-05 11:23:21 +0000196TargetInfo::IntType TargetInfo::getIntTypeByWidth(
197 unsigned BitWidth, bool IsSigned) const {
198 if (getCharWidth() == BitWidth)
199 return IsSigned ? SignedChar : UnsignedChar;
200 if (getShortWidth() == BitWidth)
201 return IsSigned ? SignedShort : UnsignedShort;
202 if (getIntWidth() == BitWidth)
203 return IsSigned ? SignedInt : UnsignedInt;
204 if (getLongWidth() == BitWidth)
205 return IsSigned ? SignedLong : UnsignedLong;
206 if (getLongLongWidth() == BitWidth)
207 return IsSigned ? SignedLongLong : UnsignedLongLong;
208 return NoInt;
209}
210
JF Bastienab8d0a02014-06-25 01:31:33 +0000211TargetInfo::IntType TargetInfo::getLeastIntTypeByWidth(unsigned BitWidth,
212 bool IsSigned) const {
213 if (getCharWidth() >= BitWidth)
214 return IsSigned ? SignedChar : UnsignedChar;
215 if (getShortWidth() >= BitWidth)
216 return IsSigned ? SignedShort : UnsignedShort;
217 if (getIntWidth() >= BitWidth)
218 return IsSigned ? SignedInt : UnsignedInt;
219 if (getLongWidth() >= BitWidth)
220 return IsSigned ? SignedLong : UnsignedLong;
221 if (getLongLongWidth() >= BitWidth)
222 return IsSigned ? SignedLongLong : UnsignedLongLong;
223 return NoInt;
224}
225
Stepan Dyatkovskiy5a637922013-09-05 11:23:21 +0000226TargetInfo::RealType TargetInfo::getRealTypeByWidth(unsigned BitWidth) const {
227 if (getFloatWidth() == BitWidth)
228 return Float;
229 if (getDoubleWidth() == BitWidth)
230 return Double;
231
232 switch (BitWidth) {
233 case 96:
Stephan Bergmann17c7f702016-12-14 11:57:17 +0000234 if (&getLongDoubleFormat() == &llvm::APFloat::x87DoubleExtended())
Stepan Dyatkovskiy5a637922013-09-05 11:23:21 +0000235 return LongDouble;
236 break;
237 case 128:
Stephan Bergmann17c7f702016-12-14 11:57:17 +0000238 if (&getLongDoubleFormat() == &llvm::APFloat::PPCDoubleDouble() ||
239 &getLongDoubleFormat() == &llvm::APFloat::IEEEquad())
Stepan Dyatkovskiy5a637922013-09-05 11:23:21 +0000240 return LongDouble;
Nemanja Ivanovicbb1ea2d2016-05-09 08:52:33 +0000241 if (hasFloat128Type())
242 return Float128;
Stepan Dyatkovskiy5a637922013-09-05 11:23:21 +0000243 break;
244 }
245
246 return NoFloat;
247}
248
Michael J. Spencerfeb799c2010-10-18 07:10:59 +0000249/// getTypeAlign - Return the alignment (in bits) of the specified integer type
Chris Lattnere4a8c642009-11-05 21:21:32 +0000250/// enum. For example, SignedInt -> getIntAlign().
251unsigned TargetInfo::getTypeAlign(IntType T) const {
252 switch (T) {
David Blaikie83d382b2011-09-23 05:06:16 +0000253 default: llvm_unreachable("not an integer!");
Stepan Dyatkovskiy5a637922013-09-05 11:23:21 +0000254 case SignedChar:
255 case UnsignedChar: return getCharAlign();
Chris Lattnere4a8c642009-11-05 21:21:32 +0000256 case SignedShort:
257 case UnsignedShort: return getShortAlign();
258 case SignedInt:
259 case UnsignedInt: return getIntAlign();
260 case SignedLong:
261 case UnsignedLong: return getLongAlign();
262 case SignedLongLong:
263 case UnsignedLongLong: return getLongLongAlign();
264 };
265}
266
Chris Lattnerc0c04392009-10-25 22:49:18 +0000267/// isTypeSigned - Return whether an integer types is signed. Returns true if
Chris Lattner72cdcac2009-10-21 06:24:21 +0000268/// the type is signed; false otherwise.
Chris Lattnerad3467e2010-12-25 23:25:43 +0000269bool TargetInfo::isTypeSigned(IntType T) {
Chris Lattner72cdcac2009-10-21 06:24:21 +0000270 switch (T) {
David Blaikie83d382b2011-09-23 05:06:16 +0000271 default: llvm_unreachable("not an integer!");
Stepan Dyatkovskiy5a637922013-09-05 11:23:21 +0000272 case SignedChar:
Chris Lattner72cdcac2009-10-21 06:24:21 +0000273 case SignedShort:
274 case SignedInt:
275 case SignedLong:
Michael J. Spencerfeb799c2010-10-18 07:10:59 +0000276 case SignedLongLong:
Chris Lattner72cdcac2009-10-21 06:24:21 +0000277 return true;
Stepan Dyatkovskiy5a637922013-09-05 11:23:21 +0000278 case UnsignedChar:
Chris Lattner72cdcac2009-10-21 06:24:21 +0000279 case UnsignedShort:
280 case UnsignedInt:
281 case UnsignedLong:
Michael J. Spencerfeb799c2010-10-18 07:10:59 +0000282 case UnsignedLongLong:
Chris Lattner72cdcac2009-10-21 06:24:21 +0000283 return false;
284 };
285}
286
Alp Toker74437972014-07-06 05:14:24 +0000287/// adjust - Set forced language options.
John Thompsoned4e2952009-11-05 20:14:16 +0000288/// Apply changes to the target information with respect to certain
Eric Christopher3646e622017-03-22 06:36:09 +0000289/// language options which change the target configuration and adjust
290/// the language based on the target options where applicable.
291void TargetInfo::adjust(LangOptions &Opts) {
Daniel Dunbar9302f602010-04-15 15:06:22 +0000292 if (Opts.NoBitFieldTypeAlign)
293 UseBitFieldTypeAlignment = false;
Saleem Abdulrasool729379a2017-10-06 23:09:55 +0000294
295 switch (Opts.WCharSize) {
296 default: llvm_unreachable("invalid wchar_t width");
297 case 0: break;
298 case 1: WCharType = Opts.WCharIsSigned ? SignedChar : UnsignedChar; break;
299 case 2: WCharType = Opts.WCharIsSigned ? SignedShort : UnsignedShort; break;
300 case 4: WCharType = Opts.WCharIsSigned ? SignedInt : UnsignedInt; break;
301 }
302
Reid Kleckner8195f692016-05-04 02:58:24 +0000303 if (Opts.AlignDouble) {
304 DoubleAlign = LongLongAlign = 64;
305 LongDoubleAlign = 64;
306 }
David Tweed2da64382013-09-09 09:17:24 +0000307
308 if (Opts.OpenCL) {
309 // OpenCL C requires specific widths for types, irrespective of
310 // what these normally are for the target.
311 // We also define long long and long double here, although the
312 // OpenCL standard only mentions these as "reserved".
313 IntWidth = IntAlign = 32;
314 LongWidth = LongAlign = 64;
315 LongLongWidth = LongLongAlign = 128;
316 HalfWidth = HalfAlign = 16;
317 FloatWidth = FloatAlign = 32;
Eric Christopherb093d692015-10-09 18:39:59 +0000318
319 // Embedded 32-bit targets (OpenCL EP) might have double C type
320 // defined as float. Let's not override this as it might lead
Pekka Jaaskelainen43c39d52013-12-18 18:15:03 +0000321 // to generating illegal code that uses 64bit doubles.
322 if (DoubleWidth != FloatWidth) {
323 DoubleWidth = DoubleAlign = 64;
Stephan Bergmann17c7f702016-12-14 11:57:17 +0000324 DoubleFormat = &llvm::APFloat::IEEEdouble();
Pekka Jaaskelainen43c39d52013-12-18 18:15:03 +0000325 }
David Tweed2da64382013-09-09 09:17:24 +0000326 LongDoubleWidth = LongDoubleAlign = 128;
327
Yaxun Liu26f75662016-08-19 05:17:25 +0000328 unsigned MaxPointerWidth = getMaxPointerWidth();
329 assert(MaxPointerWidth == 32 || MaxPointerWidth == 64);
330 bool Is32BitArch = MaxPointerWidth == 32;
David Tweed6c2149b2013-09-10 08:00:34 +0000331 SizeType = Is32BitArch ? UnsignedInt : UnsignedLong;
332 PtrDiffType = Is32BitArch ? SignedInt : SignedLong;
333 IntPtrType = Is32BitArch ? SignedInt : SignedLong;
David Tweed2da64382013-09-09 09:17:24 +0000334
335 IntMaxType = SignedLongLong;
David Tweed2da64382013-09-09 09:17:24 +0000336 Int64Type = SignedLong;
337
Stephan Bergmann17c7f702016-12-14 11:57:17 +0000338 HalfFormat = &llvm::APFloat::IEEEhalf();
339 FloatFormat = &llvm::APFloat::IEEEsingle();
340 LongDoubleFormat = &llvm::APFloat::IEEEquad();
David Tweed2da64382013-09-09 09:17:24 +0000341 }
Richard Smith59139022016-09-30 22:41:36 +0000342
343 if (Opts.NewAlignOverride)
344 NewAlign = Opts.NewAlignOverride * getCharWidth();
John Thompsoned4e2952009-11-05 20:14:16 +0000345}
Chris Lattner72cdcac2009-10-21 06:24:21 +0000346
Eric Christopher8c47b422015-10-09 18:39:55 +0000347bool TargetInfo::initFeatureMap(
348 llvm::StringMap<bool> &Features, DiagnosticsEngine &Diags, StringRef CPU,
349 const std::vector<std::string> &FeatureVec) const {
Eric Christophere9cdcae2015-09-01 18:13:20 +0000350 for (const auto &F : FeatureVec) {
Craig Topper8459aa82015-10-21 16:31:31 +0000351 StringRef Name = F;
Eric Christophere9cdcae2015-09-01 18:13:20 +0000352 // Apply the feature via the target.
353 bool Enabled = Name[0] == '+';
Craig Topper8459aa82015-10-21 16:31:31 +0000354 setFeatureEnabled(Features, Name.substr(1), Enabled);
Eric Christophere9cdcae2015-09-01 18:13:20 +0000355 }
356 return true;
357}
358
Sven van Haastregtefb4d4c2017-08-15 09:38:18 +0000359LangAS::ID TargetInfo::getOpenCLTypeAddrSpace(const Type *T) const {
360 auto BT = dyn_cast<BuiltinType>(T);
361
362 if (!BT) {
363 if (isa<PipeType>(T))
364 return LangAS::opencl_global;
365
366 return LangAS::Default;
367 }
368
369 switch (BT->getKind()) {
370#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
371 case BuiltinType::Id: \
372 return LangAS::opencl_global;
373#include "clang/Basic/OpenCLImageTypes.def"
374
375 case BuiltinType::OCLSampler:
376 return LangAS::opencl_constant;
377
378 default:
379 return LangAS::Default;
380 }
381}
382
Chris Lattnerec0a6d92007-09-22 18:29:59 +0000383//===----------------------------------------------------------------------===//
Chris Lattnerec0a6d92007-09-22 18:29:59 +0000384
Chris Lattner10a5b382007-01-29 05:24:35 +0000385
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000386static StringRef removeGCCRegisterPrefix(StringRef Name) {
Anders Carlsson3ed4aea2008-02-06 00:11:32 +0000387 if (Name[0] == '%' || Name[0] == '#')
Anders Carlssonc7c5baa2010-01-30 19:12:25 +0000388 Name = Name.substr(1);
Michael J. Spencerfeb799c2010-10-18 07:10:59 +0000389
Anders Carlssonc7c5baa2010-01-30 19:12:25 +0000390 return Name;
Anders Carlsson3ed4aea2008-02-06 00:11:32 +0000391}
392
Eric Christopherfd9a5f42011-06-28 18:20:53 +0000393/// isValidClobber - Returns whether the passed in string is
394/// a valid clobber in an inline asm statement. This is used by
395/// Sema.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000396bool TargetInfo::isValidClobber(StringRef Name) const {
Eric Christopherfd9a5f42011-06-28 18:20:53 +0000397 return (isValidGCCRegisterName(Name) ||
Eric Christopherb093d692015-10-09 18:39:59 +0000398 Name == "memory" || Name == "cc");
Eric Christopherfd9a5f42011-06-28 18:20:53 +0000399}
400
Anders Carlsson5fa3f342007-11-24 23:38:12 +0000401/// isValidGCCRegisterName - Returns whether the passed in string
402/// is a valid register name according to GCC. This is used by Sema for
403/// inline asm statements.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000404bool TargetInfo::isValidGCCRegisterName(StringRef Name) const {
Anders Carlssonc7c5baa2010-01-30 19:12:25 +0000405 if (Name.empty())
406 return false;
Michael J. Spencerfeb799c2010-10-18 07:10:59 +0000407
Anders Carlsson290aa852007-11-25 00:25:21 +0000408 // Get rid of any register prefix.
Anders Carlssonc7c5baa2010-01-30 19:12:25 +0000409 Name = removeGCCRegisterPrefix(Name);
Olivier Goffartfc8f8932014-08-17 13:19:48 +0000410 if (Name.empty())
Craig Topperde330c72015-10-21 04:52:34 +0000411 return false;
Anders Carlsson3ed4aea2008-02-06 00:11:32 +0000412
Craig Topperf054e3a2015-10-19 03:52:27 +0000413 ArrayRef<const char *> Names = getGCCRegNames();
Mike Stump11289f42009-09-09 15:08:12 +0000414
Anders Carlsson290aa852007-11-25 00:25:21 +0000415 // If we have a number it maps to an entry in the register name array.
Jordan Rosea7d03842013-02-08 22:30:41 +0000416 if (isDigit(Name[0])) {
Craig Topper67288a92015-10-21 04:52:36 +0000417 unsigned n;
Anders Carlssonc7c5baa2010-01-30 19:12:25 +0000418 if (!Name.getAsInteger(0, n))
Craig Topper67288a92015-10-21 04:52:36 +0000419 return n < Names.size();
Anders Carlsson290aa852007-11-25 00:25:21 +0000420 }
421
422 // Check register names.
Craig Topper335c8f92015-10-21 04:52:38 +0000423 if (std::find(Names.begin(), Names.end(), Name) != Names.end())
424 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000425
Eric Christophercdd36352011-06-21 00:05:20 +0000426 // Check any additional names that we have.
Craig Topperdd2b74a2015-10-21 04:52:40 +0000427 for (const AddlRegName &ARN : getGCCAddlRegNames())
428 for (const char *AN : ARN.Names) {
429 if (!AN)
Eric Christopherb093d692015-10-09 18:39:59 +0000430 break;
Eric Christophercdd36352011-06-21 00:05:20 +0000431 // Make sure the register that the additional name is for is within
432 // the bounds of the register names from above.
Craig Topperdd2b74a2015-10-21 04:52:40 +0000433 if (AN == Name && ARN.RegNum < Names.size())
Anders Carlsson290aa852007-11-25 00:25:21 +0000434 return true;
435 }
Craig Topperdd2b74a2015-10-21 04:52:40 +0000436
437 // Now check aliases.
438 for (const GCCRegAlias &GRA : getGCCRegAliases())
439 for (const char *A : GRA.Aliases) {
440 if (!A)
441 break;
442 if (A == Name)
443 return true;
444 }
Mike Stump11289f42009-09-09 15:08:12 +0000445
Anders Carlsson5fa3f342007-11-24 23:38:12 +0000446 return false;
447}
Anders Carlssonf511f642007-11-27 04:11:28 +0000448
Marina Yatsinac42fd032016-12-26 12:23:42 +0000449StringRef TargetInfo::getNormalizedGCCRegisterName(StringRef Name,
450 bool ReturnCanonical) const {
Anders Carlssonf511f642007-11-27 04:11:28 +0000451 assert(isValidGCCRegisterName(Name) && "Invalid register passed in");
Mike Stump11289f42009-09-09 15:08:12 +0000452
Anders Carlssonc7c5baa2010-01-30 19:12:25 +0000453 // Get rid of any register prefix.
454 Name = removeGCCRegisterPrefix(Name);
Mike Stump11289f42009-09-09 15:08:12 +0000455
Craig Topperf054e3a2015-10-19 03:52:27 +0000456 ArrayRef<const char *> Names = getGCCRegNames();
Anders Carlssonf511f642007-11-27 04:11:28 +0000457
458 // First, check if we have a number.
Jordan Rosea7d03842013-02-08 22:30:41 +0000459 if (isDigit(Name[0])) {
Craig Topper67288a92015-10-21 04:52:36 +0000460 unsigned n;
Anders Carlssonc7c5baa2010-01-30 19:12:25 +0000461 if (!Name.getAsInteger(0, n)) {
Craig Topper67288a92015-10-21 04:52:36 +0000462 assert(n < Names.size() && "Out of bounds register number!");
Anders Carlssonf511f642007-11-27 04:11:28 +0000463 return Names[n];
464 }
465 }
Mike Stump11289f42009-09-09 15:08:12 +0000466
Eric Christophercdd36352011-06-21 00:05:20 +0000467 // Check any additional names that we have.
Craig Topperdd2b74a2015-10-21 04:52:40 +0000468 for (const AddlRegName &ARN : getGCCAddlRegNames())
469 for (const char *AN : ARN.Names) {
470 if (!AN)
Eric Christopherb093d692015-10-09 18:39:59 +0000471 break;
Eric Christophercdd36352011-06-21 00:05:20 +0000472 // Make sure the register that the additional name is for is within
473 // the bounds of the register names from above.
Craig Topperdd2b74a2015-10-21 04:52:40 +0000474 if (AN == Name && ARN.RegNum < Names.size())
Marina Yatsinac42fd032016-12-26 12:23:42 +0000475 return ReturnCanonical ? Names[ARN.RegNum] : Name;
Eric Christophercdd36352011-06-21 00:05:20 +0000476 }
477
Anders Carlssonf511f642007-11-27 04:11:28 +0000478 // Now check aliases.
Craig Topperdd2b74a2015-10-21 04:52:40 +0000479 for (const GCCRegAlias &RA : getGCCRegAliases())
480 for (const char *A : RA.Aliases) {
481 if (!A)
Anders Carlssonf511f642007-11-27 04:11:28 +0000482 break;
Craig Topperdd2b74a2015-10-21 04:52:40 +0000483 if (A == Name)
484 return RA.Register;
Anders Carlssonf511f642007-11-27 04:11:28 +0000485 }
Mike Stump11289f42009-09-09 15:08:12 +0000486
Anders Carlssonf511f642007-11-27 04:11:28 +0000487 return Name;
488}
489
Chris Lattnerc3f4c7b2009-04-26 17:19:08 +0000490bool TargetInfo::validateOutputConstraint(ConstraintInfo &Info) const {
491 const char *Name = Info.getConstraintStr().c_str();
Anders Carlssonf511f642007-11-27 04:11:28 +0000492 // An output constraint must start with '=' or '+'
493 if (*Name != '=' && *Name != '+')
494 return false;
495
496 if (*Name == '+')
Chris Lattnerd9725f72009-04-26 07:16:29 +0000497 Info.setIsReadWrite();
Anders Carlssonf511f642007-11-27 04:11:28 +0000498
499 Name++;
500 while (*Name) {
501 switch (*Name) {
502 default:
Chris Lattnerd9725f72009-04-26 07:16:29 +0000503 if (!validateAsmConstraint(Name, Info)) {
Ted Kremenekb44485b2008-04-24 16:36:38 +0000504 // FIXME: We temporarily return false
Anders Carlssonf511f642007-11-27 04:11:28 +0000505 // so we can add more constraints as we hit it.
506 // Eventually, an unknown constraint should just be treated as 'g'.
Ted Kremenekb44485b2008-04-24 16:36:38 +0000507 return false;
Anders Carlssonf511f642007-11-27 04:11:28 +0000508 }
David Majnemera0040df2015-01-10 10:43:19 +0000509 break;
Anders Carlssonf511f642007-11-27 04:11:28 +0000510 case '&': // early clobber.
David Majnemera0040df2015-01-10 10:43:19 +0000511 Info.setEarlyClobber();
Anders Carlssonf511f642007-11-27 04:11:28 +0000512 break;
Chris Lattnerf3154712009-10-13 04:32:07 +0000513 case '%': // commutative.
514 // FIXME: Check that there is a another register after this one.
515 break;
Anders Carlssonf511f642007-11-27 04:11:28 +0000516 case 'r': // general register.
Chris Lattnerd9725f72009-04-26 07:16:29 +0000517 Info.setAllowsRegister();
Anders Carlssonf511f642007-11-27 04:11:28 +0000518 break;
519 case 'm': // memory operand.
Dale Johannesen8499f472010-09-07 18:40:41 +0000520 case 'o': // offsetable memory operand.
521 case 'V': // non-offsetable memory operand.
522 case '<': // autodecrement memory operand.
523 case '>': // autoincrement memory operand.
Chris Lattnerd9725f72009-04-26 07:16:29 +0000524 Info.setAllowsMemory();
Anders Carlssonf511f642007-11-27 04:11:28 +0000525 break;
526 case 'g': // general register, memory operand or immediate integer.
Anders Carlssone70cde12009-01-18 02:12:04 +0000527 case 'X': // any operand.
Chris Lattnerd9725f72009-04-26 07:16:29 +0000528 Info.setAllowsRegister();
529 Info.setAllowsMemory();
Anders Carlssonf511f642007-11-27 04:11:28 +0000530 break;
John Thompsona5c7d702010-08-10 19:20:14 +0000531 case ',': // multiple alternative constraint. Pass it.
John Thompson46667af2010-08-11 00:58:20 +0000532 // Handle additional optional '=' or '+' modifiers.
John Thompson12240612010-09-18 01:15:13 +0000533 if (Name[1] == '=' || Name[1] == '+')
John Thompson46667af2010-08-11 00:58:20 +0000534 Name++;
John Thompsona5c7d702010-08-10 19:20:14 +0000535 break;
David Majnemer50cb0552015-01-11 08:52:38 +0000536 case '#': // Ignore as constraint.
537 while (Name[1] && Name[1] != ',')
538 Name++;
David Majnemerc71a5662015-01-11 09:39:03 +0000539 break;
John Thompsona5c7d702010-08-10 19:20:14 +0000540 case '?': // Disparage slightly code.
Dale Johannesen8499f472010-09-07 18:40:41 +0000541 case '!': // Disparage severely.
Ulrich Weigand7bcc7ec2012-10-29 12:20:54 +0000542 case '*': // Ignore for choosing register preferences.
Marina Yatsina33eb7752017-06-26 15:55:51 +0000543 case 'i': // Ignore i,n,E,F as output constraints (match from the other
544 // chars)
545 case 'n':
546 case 'E':
547 case 'F':
John Thompsona5c7d702010-08-10 19:20:14 +0000548 break; // Pass them.
Anders Carlssonf511f642007-11-27 04:11:28 +0000549 }
Mike Stump11289f42009-09-09 15:08:12 +0000550
Anders Carlssonf511f642007-11-27 04:11:28 +0000551 Name++;
552 }
Mike Stump11289f42009-09-09 15:08:12 +0000553
David Majnemera0040df2015-01-10 10:43:19 +0000554 // Early clobber with a read-write constraint which doesn't permit registers
555 // is invalid.
556 if (Info.earlyClobber() && Info.isReadWrite() && !Info.allowsRegister())
557 return false;
558
Benjamin Kramer1e4a8862013-04-18 22:49:48 +0000559 // If a constraint allows neither memory nor register operands it contains
560 // only modifiers. Reject it.
Benjamin Kramer7ee3b9c2013-04-18 13:23:23 +0000561 return Info.allowsMemory() || Info.allowsRegister();
Anders Carlssonf511f642007-11-27 04:11:28 +0000562}
563
Anders Carlssona79203b2009-01-18 01:56:57 +0000564bool TargetInfo::resolveSymbolicName(const char *&Name,
Craig Topper55765ca2015-10-21 02:34:10 +0000565 ArrayRef<ConstraintInfo> OutputConstraints,
Chris Lattnerd9725f72009-04-26 07:16:29 +0000566 unsigned &Index) const {
Anders Carlssona79203b2009-01-18 01:56:57 +0000567 assert(*Name == '[' && "Symbolic name did not start with '['");
Anders Carlssona79203b2009-01-18 01:56:57 +0000568 Name++;
569 const char *Start = Name;
570 while (*Name && *Name != ']')
571 Name++;
Mike Stump11289f42009-09-09 15:08:12 +0000572
Anders Carlssona79203b2009-01-18 01:56:57 +0000573 if (!*Name) {
574 // Missing ']'
575 return false;
576 }
Mike Stump11289f42009-09-09 15:08:12 +0000577
Anders Carlssona79203b2009-01-18 01:56:57 +0000578 std::string SymbolicName(Start, Name - Start);
Mike Stump11289f42009-09-09 15:08:12 +0000579
Craig Topper55765ca2015-10-21 02:34:10 +0000580 for (Index = 0; Index != OutputConstraints.size(); ++Index)
Chris Lattnerc16d4762009-04-26 17:57:12 +0000581 if (SymbolicName == OutputConstraints[Index].getName())
Anders Carlssona79203b2009-01-18 01:56:57 +0000582 return true;
Anders Carlssona79203b2009-01-18 01:56:57 +0000583
584 return false;
585}
586
Craig Topper55765ca2015-10-21 02:34:10 +0000587bool TargetInfo::validateInputConstraint(
588 MutableArrayRef<ConstraintInfo> OutputConstraints,
589 ConstraintInfo &Info) const {
Chris Lattnerc3f4c7b2009-04-26 17:19:08 +0000590 const char *Name = Info.ConstraintStr.c_str();
591
Duncan P. N. Exon Smithd68c7aa2013-12-16 03:20:06 +0000592 if (!*Name)
593 return false;
594
Anders Carlssonf511f642007-11-27 04:11:28 +0000595 while (*Name) {
596 switch (*Name) {
597 default:
598 // Check if we have a matching constraint
599 if (*Name >= '0' && *Name <= '9') {
David Majnemerb6b56432015-01-11 10:22:41 +0000600 const char *DigitStart = Name;
601 while (Name[1] >= '0' && Name[1] <= '9')
602 Name++;
603 const char *DigitEnd = Name;
604 unsigned i;
605 if (StringRef(DigitStart, DigitEnd - DigitStart + 1)
606 .getAsInteger(10, i))
607 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000608
Anders Carlssonf511f642007-11-27 04:11:28 +0000609 // Check if matching constraint is out of bounds.
Craig Topper55765ca2015-10-21 02:34:10 +0000610 if (i >= OutputConstraints.size()) return false;
Mike Stump11289f42009-09-09 15:08:12 +0000611
Anders Carlssonda1f5fc2010-11-03 02:22:29 +0000612 // A number must refer to an output only operand.
613 if (OutputConstraints[i].isReadWrite())
614 return false;
615
Eric Christopherb093d692015-10-09 18:39:59 +0000616 // If the constraint is already tied, it must be tied to the
Anders Carlsson2d5f8b42010-11-03 02:54:51 +0000617 // same operand referenced to by the number.
618 if (Info.hasTiedOperand() && Info.getTiedOperand() != i)
619 return false;
620
Mike Stump11289f42009-09-09 15:08:12 +0000621 // The constraint should have the same info as the respective
Anders Carlsson570c3572009-01-27 20:38:24 +0000622 // output constraint.
Chris Lattner4c92b512009-04-26 18:05:25 +0000623 Info.setTiedOperand(i, OutputConstraints[i]);
Chris Lattnerd9725f72009-04-26 07:16:29 +0000624 } else if (!validateAsmConstraint(Name, Info)) {
Daniel Dunbar81128e02008-08-25 09:46:27 +0000625 // FIXME: This error return is in place temporarily so we can
626 // add more constraints as we hit it. Eventually, an unknown
627 // constraint should just be treated as 'g'.
628 return false;
Anders Carlssona79203b2009-01-18 01:56:57 +0000629 }
630 break;
631 case '[': {
632 unsigned Index = 0;
Craig Topper55765ca2015-10-21 02:34:10 +0000633 if (!resolveSymbolicName(Name, OutputConstraints, Index))
Anders Carlssona79203b2009-01-18 01:56:57 +0000634 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000635
Eric Christopherb093d692015-10-09 18:39:59 +0000636 // If the constraint is already tied, it must be tied to the
Anders Carlsson2d5f8b42010-11-03 02:54:51 +0000637 // same operand referenced to by the number.
638 if (Info.hasTiedOperand() && Info.getTiedOperand() != Index)
639 return false;
640
David Majnemer55164f92015-01-11 09:57:13 +0000641 // A number must refer to an output only operand.
642 if (OutputConstraints[Index].isReadWrite())
643 return false;
644
Eli Friedman854f1102010-08-11 23:03:37 +0000645 Info.setTiedOperand(Index, OutputConstraints[Index]);
Anders Carlssona79203b2009-01-18 01:56:57 +0000646 break;
Mike Stump11289f42009-09-09 15:08:12 +0000647 }
Anders Carlsson050f4942007-12-08 19:32:57 +0000648 case '%': // commutative
649 // FIXME: Fail if % is used with the last operand.
650 break;
Anders Carlssonf511f642007-11-27 04:11:28 +0000651 case 'i': // immediate integer.
Anders Carlssona3a96af2008-03-09 06:02:02 +0000652 case 'n': // immediate integer with a known value.
Anders Carlssonf511f642007-11-27 04:11:28 +0000653 break;
Chris Lattnerdbcc5ca2009-05-06 04:33:31 +0000654 case 'I': // Various constant constraints with target-specific meanings.
655 case 'J':
656 case 'K':
657 case 'L':
658 case 'M':
659 case 'N':
660 case 'O':
661 case 'P':
Saleem Abdulrasoola2823572015-01-06 04:26:34 +0000662 if (!validateAsmConstraint(Name, Info))
663 return false;
Chris Lattnerdbcc5ca2009-05-06 04:33:31 +0000664 break;
Anders Carlssonf511f642007-11-27 04:11:28 +0000665 case 'r': // general register.
Chris Lattnerd9725f72009-04-26 07:16:29 +0000666 Info.setAllowsRegister();
Anders Carlssonf511f642007-11-27 04:11:28 +0000667 break;
668 case 'm': // memory operand.
Dale Johannesen8499f472010-09-07 18:40:41 +0000669 case 'o': // offsettable memory operand.
670 case 'V': // non-offsettable memory operand.
671 case '<': // autodecrement memory operand.
672 case '>': // autoincrement memory operand.
Chris Lattnerd9725f72009-04-26 07:16:29 +0000673 Info.setAllowsMemory();
Anders Carlssonf511f642007-11-27 04:11:28 +0000674 break;
675 case 'g': // general register, memory operand or immediate integer.
Anders Carlssone70cde12009-01-18 02:12:04 +0000676 case 'X': // any operand.
Chris Lattnerd9725f72009-04-26 07:16:29 +0000677 Info.setAllowsRegister();
678 Info.setAllowsMemory();
Anders Carlssonf511f642007-11-27 04:11:28 +0000679 break;
John Thompsonc467aa22010-09-21 22:04:54 +0000680 case 'E': // immediate floating point.
681 case 'F': // immediate floating point.
682 case 'p': // address operand.
683 break;
John Thompsona5c7d702010-08-10 19:20:14 +0000684 case ',': // multiple alternative constraint. Ignore comma.
685 break;
David Majnemerc71a5662015-01-11 09:39:03 +0000686 case '#': // Ignore as constraint.
687 while (Name[1] && Name[1] != ',')
688 Name++;
David Majnemerc71a5662015-01-11 09:39:03 +0000689 break;
John Thompsona5c7d702010-08-10 19:20:14 +0000690 case '?': // Disparage slightly code.
Chris Lattner57540c52011-04-15 05:22:18 +0000691 case '!': // Disparage severely.
Ulrich Weigand7bcc7ec2012-10-29 12:20:54 +0000692 case '*': // Ignore for choosing register preferences.
John Thompsona5c7d702010-08-10 19:20:14 +0000693 break; // Pass them.
Anders Carlssonf511f642007-11-27 04:11:28 +0000694 }
Mike Stump11289f42009-09-09 15:08:12 +0000695
Anders Carlssonf511f642007-11-27 04:11:28 +0000696 Name++;
697 }
Mike Stump11289f42009-09-09 15:08:12 +0000698
Anders Carlssonf511f642007-11-27 04:11:28 +0000699 return true;
700}