TargetInfo.cpp 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990
  1. //===--- TargetInfo.cpp - Information about Target machine ----------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file implements the TargetInfo and TargetInfoImpl interfaces.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "clang/Basic/TargetInfo.h"
  13. #include "clang/Basic/AddressSpaces.h"
  14. #include "clang/Basic/CharInfo.h"
  15. #include "clang/Basic/Diagnostic.h"
  16. #include "clang/Basic/DiagnosticFrontend.h"
  17. #include "clang/Basic/LangOptions.h"
  18. #include "llvm/ADT/APFloat.h"
  19. #include "llvm/ADT/STLExtras.h"
  20. #include "llvm/Support/ErrorHandling.h"
  21. #include "llvm/Support/TargetParser.h"
  22. #include <cstdlib>
  23. using namespace clang;
  24. static const LangASMap DefaultAddrSpaceMap = {0};
  25. // The fake address space map must have a distinct entry for each
  26. // language-specific address space.
  27. static const LangASMap FakeAddrSpaceMap = {
  28. 0, // Default
  29. 1, // opencl_global
  30. 3, // opencl_local
  31. 2, // opencl_constant
  32. 0, // opencl_private
  33. 4, // opencl_generic
  34. 5, // opencl_global_device
  35. 6, // opencl_global_host
  36. 7, // cuda_device
  37. 8, // cuda_constant
  38. 9, // cuda_shared
  39. 1, // sycl_global
  40. 5, // sycl_global_device
  41. 6, // sycl_global_host
  42. 3, // sycl_local
  43. 0, // sycl_private
  44. 10, // ptr32_sptr
  45. 11, // ptr32_uptr
  46. 12, // ptr64
  47. 13, // hlsl_groupshared
  48. };
  49. // TargetInfo Constructor.
  50. TargetInfo::TargetInfo(const llvm::Triple &T) : Triple(T) {
  51. // Set defaults. Defaults are set for a 32-bit RISC platform, like PPC or
  52. // SPARC. These should be overridden by concrete targets as needed.
  53. BigEndian = !T.isLittleEndian();
  54. TLSSupported = true;
  55. VLASupported = true;
  56. NoAsmVariants = false;
  57. HasLegalHalfType = false;
  58. HalfArgsAndReturns = false;
  59. HasFloat128 = false;
  60. HasIbm128 = false;
  61. HasFloat16 = false;
  62. HasBFloat16 = false;
  63. HasLongDouble = true;
  64. HasFPReturn = true;
  65. HasStrictFP = false;
  66. PointerWidth = PointerAlign = 32;
  67. BoolWidth = BoolAlign = 8;
  68. IntWidth = IntAlign = 32;
  69. LongWidth = LongAlign = 32;
  70. LongLongWidth = LongLongAlign = 64;
  71. Int128Align = 128;
  72. // Fixed point default bit widths
  73. ShortAccumWidth = ShortAccumAlign = 16;
  74. AccumWidth = AccumAlign = 32;
  75. LongAccumWidth = LongAccumAlign = 64;
  76. ShortFractWidth = ShortFractAlign = 8;
  77. FractWidth = FractAlign = 16;
  78. LongFractWidth = LongFractAlign = 32;
  79. // Fixed point default integral and fractional bit sizes
  80. // We give the _Accum 1 fewer fractional bits than their corresponding _Fract
  81. // types by default to have the same number of fractional bits between _Accum
  82. // and _Fract types.
  83. PaddingOnUnsignedFixedPoint = false;
  84. ShortAccumScale = 7;
  85. AccumScale = 15;
  86. LongAccumScale = 31;
  87. SuitableAlign = 64;
  88. DefaultAlignForAttributeAligned = 128;
  89. MinGlobalAlign = 0;
  90. // From the glibc documentation, on GNU systems, malloc guarantees 16-byte
  91. // alignment on 64-bit systems and 8-byte alignment on 32-bit systems. See
  92. // https://www.gnu.org/software/libc/manual/html_node/Malloc-Examples.html.
  93. // This alignment guarantee also applies to Windows and Android. On Darwin
  94. // and OpenBSD, the alignment is 16 bytes on both 64-bit and 32-bit systems.
  95. if (T.isGNUEnvironment() || T.isWindowsMSVCEnvironment() || T.isAndroid())
  96. NewAlign = Triple.isArch64Bit() ? 128 : Triple.isArch32Bit() ? 64 : 0;
  97. else if (T.isOSDarwin() || T.isOSOpenBSD())
  98. NewAlign = 128;
  99. else
  100. NewAlign = 0; // Infer from basic type alignment.
  101. HalfWidth = 16;
  102. HalfAlign = 16;
  103. FloatWidth = 32;
  104. FloatAlign = 32;
  105. DoubleWidth = 64;
  106. DoubleAlign = 64;
  107. LongDoubleWidth = 64;
  108. LongDoubleAlign = 64;
  109. Float128Align = 128;
  110. Ibm128Align = 128;
  111. LargeArrayMinWidth = 0;
  112. LargeArrayAlign = 0;
  113. MaxAtomicPromoteWidth = MaxAtomicInlineWidth = 0;
  114. MaxVectorAlign = 0;
  115. MaxTLSAlign = 0;
  116. SimdDefaultAlign = 0;
  117. SizeType = UnsignedLong;
  118. PtrDiffType = SignedLong;
  119. IntMaxType = SignedLongLong;
  120. IntPtrType = SignedLong;
  121. WCharType = SignedInt;
  122. WIntType = SignedInt;
  123. Char16Type = UnsignedShort;
  124. Char32Type = UnsignedInt;
  125. Int64Type = SignedLongLong;
  126. Int16Type = SignedShort;
  127. SigAtomicType = SignedInt;
  128. ProcessIDType = SignedInt;
  129. UseSignedCharForObjCBool = true;
  130. UseBitFieldTypeAlignment = true;
  131. UseZeroLengthBitfieldAlignment = false;
  132. UseLeadingZeroLengthBitfield = true;
  133. UseExplicitBitFieldAlignment = true;
  134. ZeroLengthBitfieldBoundary = 0;
  135. MaxAlignedAttribute = 0;
  136. HalfFormat = &llvm::APFloat::IEEEhalf();
  137. FloatFormat = &llvm::APFloat::IEEEsingle();
  138. DoubleFormat = &llvm::APFloat::IEEEdouble();
  139. LongDoubleFormat = &llvm::APFloat::IEEEdouble();
  140. Float128Format = &llvm::APFloat::IEEEquad();
  141. Ibm128Format = &llvm::APFloat::PPCDoubleDouble();
  142. MCountName = "mcount";
  143. UserLabelPrefix = "_";
  144. RegParmMax = 0;
  145. SSERegParmMax = 0;
  146. HasAlignMac68kSupport = false;
  147. HasBuiltinMSVaList = false;
  148. IsRenderScriptTarget = false;
  149. HasAArch64SVETypes = false;
  150. HasRISCVVTypes = false;
  151. AllowAMDGPUUnsafeFPAtomics = false;
  152. ARMCDECoprocMask = 0;
  153. // Default to no types using fpret.
  154. RealTypeUsesObjCFPRetMask = 0;
  155. // Default to not using fp2ret for __Complex long double
  156. ComplexLongDoubleUsesFP2Ret = false;
  157. // Set the C++ ABI based on the triple.
  158. TheCXXABI.set(Triple.isKnownWindowsMSVCEnvironment()
  159. ? TargetCXXABI::Microsoft
  160. : TargetCXXABI::GenericItanium);
  161. // Default to an empty address space map.
  162. AddrSpaceMap = &DefaultAddrSpaceMap;
  163. UseAddrSpaceMapMangling = false;
  164. // Default to an unknown platform name.
  165. PlatformName = "unknown";
  166. PlatformMinVersion = VersionTuple();
  167. MaxOpenCLWorkGroupSize = 1024;
  168. MaxBitIntWidth.reset();
  169. }
  170. // Out of line virtual dtor for TargetInfo.
  171. TargetInfo::~TargetInfo() {}
  172. void TargetInfo::resetDataLayout(StringRef DL, const char *ULP) {
  173. DataLayoutString = DL.str();
  174. UserLabelPrefix = ULP;
  175. }
  176. bool
  177. TargetInfo::checkCFProtectionBranchSupported(DiagnosticsEngine &Diags) const {
  178. Diags.Report(diag::err_opt_not_valid_on_target) << "cf-protection=branch";
  179. return false;
  180. }
  181. bool
  182. TargetInfo::checkCFProtectionReturnSupported(DiagnosticsEngine &Diags) const {
  183. Diags.Report(diag::err_opt_not_valid_on_target) << "cf-protection=return";
  184. return false;
  185. }
  186. /// getTypeName - Return the user string for the specified integer type enum.
  187. /// For example, SignedShort -> "short".
  188. const char *TargetInfo::getTypeName(IntType T) {
  189. switch (T) {
  190. default: llvm_unreachable("not an integer!");
  191. case SignedChar: return "signed char";
  192. case UnsignedChar: return "unsigned char";
  193. case SignedShort: return "short";
  194. case UnsignedShort: return "unsigned short";
  195. case SignedInt: return "int";
  196. case UnsignedInt: return "unsigned int";
  197. case SignedLong: return "long int";
  198. case UnsignedLong: return "long unsigned int";
  199. case SignedLongLong: return "long long int";
  200. case UnsignedLongLong: return "long long unsigned int";
  201. }
  202. }
  203. /// getTypeConstantSuffix - Return the constant suffix for the specified
  204. /// integer type enum. For example, SignedLong -> "L".
  205. const char *TargetInfo::getTypeConstantSuffix(IntType T) const {
  206. switch (T) {
  207. default: llvm_unreachable("not an integer!");
  208. case SignedChar:
  209. case SignedShort:
  210. case SignedInt: return "";
  211. case SignedLong: return "L";
  212. case SignedLongLong: return "LL";
  213. case UnsignedChar:
  214. if (getCharWidth() < getIntWidth())
  215. return "";
  216. [[fallthrough]];
  217. case UnsignedShort:
  218. if (getShortWidth() < getIntWidth())
  219. return "";
  220. [[fallthrough]];
  221. case UnsignedInt: return "U";
  222. case UnsignedLong: return "UL";
  223. case UnsignedLongLong: return "ULL";
  224. }
  225. }
  226. /// getTypeFormatModifier - Return the printf format modifier for the
  227. /// specified integer type enum. For example, SignedLong -> "l".
  228. const char *TargetInfo::getTypeFormatModifier(IntType T) {
  229. switch (T) {
  230. default: llvm_unreachable("not an integer!");
  231. case SignedChar:
  232. case UnsignedChar: return "hh";
  233. case SignedShort:
  234. case UnsignedShort: return "h";
  235. case SignedInt:
  236. case UnsignedInt: return "";
  237. case SignedLong:
  238. case UnsignedLong: return "l";
  239. case SignedLongLong:
  240. case UnsignedLongLong: return "ll";
  241. }
  242. }
  243. /// getTypeWidth - Return the width (in bits) of the specified integer type
  244. /// enum. For example, SignedInt -> getIntWidth().
  245. unsigned TargetInfo::getTypeWidth(IntType T) const {
  246. switch (T) {
  247. default: llvm_unreachable("not an integer!");
  248. case SignedChar:
  249. case UnsignedChar: return getCharWidth();
  250. case SignedShort:
  251. case UnsignedShort: return getShortWidth();
  252. case SignedInt:
  253. case UnsignedInt: return getIntWidth();
  254. case SignedLong:
  255. case UnsignedLong: return getLongWidth();
  256. case SignedLongLong:
  257. case UnsignedLongLong: return getLongLongWidth();
  258. };
  259. }
  260. TargetInfo::IntType TargetInfo::getIntTypeByWidth(
  261. unsigned BitWidth, bool IsSigned) const {
  262. if (getCharWidth() == BitWidth)
  263. return IsSigned ? SignedChar : UnsignedChar;
  264. if (getShortWidth() == BitWidth)
  265. return IsSigned ? SignedShort : UnsignedShort;
  266. if (getIntWidth() == BitWidth)
  267. return IsSigned ? SignedInt : UnsignedInt;
  268. if (getLongWidth() == BitWidth)
  269. return IsSigned ? SignedLong : UnsignedLong;
  270. if (getLongLongWidth() == BitWidth)
  271. return IsSigned ? SignedLongLong : UnsignedLongLong;
  272. return NoInt;
  273. }
  274. TargetInfo::IntType TargetInfo::getLeastIntTypeByWidth(unsigned BitWidth,
  275. bool IsSigned) const {
  276. if (getCharWidth() >= BitWidth)
  277. return IsSigned ? SignedChar : UnsignedChar;
  278. if (getShortWidth() >= BitWidth)
  279. return IsSigned ? SignedShort : UnsignedShort;
  280. if (getIntWidth() >= BitWidth)
  281. return IsSigned ? SignedInt : UnsignedInt;
  282. if (getLongWidth() >= BitWidth)
  283. return IsSigned ? SignedLong : UnsignedLong;
  284. if (getLongLongWidth() >= BitWidth)
  285. return IsSigned ? SignedLongLong : UnsignedLongLong;
  286. return NoInt;
  287. }
  288. FloatModeKind TargetInfo::getRealTypeByWidth(unsigned BitWidth,
  289. FloatModeKind ExplicitType) const {
  290. if (getHalfWidth() == BitWidth)
  291. return FloatModeKind::Half;
  292. if (getFloatWidth() == BitWidth)
  293. return FloatModeKind::Float;
  294. if (getDoubleWidth() == BitWidth)
  295. return FloatModeKind::Double;
  296. switch (BitWidth) {
  297. case 96:
  298. if (&getLongDoubleFormat() == &llvm::APFloat::x87DoubleExtended())
  299. return FloatModeKind::LongDouble;
  300. break;
  301. case 128:
  302. // The caller explicitly asked for an IEEE compliant type but we still
  303. // have to check if the target supports it.
  304. if (ExplicitType == FloatModeKind::Float128)
  305. return hasFloat128Type() ? FloatModeKind::Float128
  306. : FloatModeKind::NoFloat;
  307. if (ExplicitType == FloatModeKind::Ibm128)
  308. return hasIbm128Type() ? FloatModeKind::Ibm128
  309. : FloatModeKind::NoFloat;
  310. if (&getLongDoubleFormat() == &llvm::APFloat::PPCDoubleDouble() ||
  311. &getLongDoubleFormat() == &llvm::APFloat::IEEEquad())
  312. return FloatModeKind::LongDouble;
  313. if (hasFloat128Type())
  314. return FloatModeKind::Float128;
  315. break;
  316. }
  317. return FloatModeKind::NoFloat;
  318. }
  319. /// getTypeAlign - Return the alignment (in bits) of the specified integer type
  320. /// enum. For example, SignedInt -> getIntAlign().
  321. unsigned TargetInfo::getTypeAlign(IntType T) const {
  322. switch (T) {
  323. default: llvm_unreachable("not an integer!");
  324. case SignedChar:
  325. case UnsignedChar: return getCharAlign();
  326. case SignedShort:
  327. case UnsignedShort: return getShortAlign();
  328. case SignedInt:
  329. case UnsignedInt: return getIntAlign();
  330. case SignedLong:
  331. case UnsignedLong: return getLongAlign();
  332. case SignedLongLong:
  333. case UnsignedLongLong: return getLongLongAlign();
  334. };
  335. }
  336. /// isTypeSigned - Return whether an integer types is signed. Returns true if
  337. /// the type is signed; false otherwise.
  338. bool TargetInfo::isTypeSigned(IntType T) {
  339. switch (T) {
  340. default: llvm_unreachable("not an integer!");
  341. case SignedChar:
  342. case SignedShort:
  343. case SignedInt:
  344. case SignedLong:
  345. case SignedLongLong:
  346. return true;
  347. case UnsignedChar:
  348. case UnsignedShort:
  349. case UnsignedInt:
  350. case UnsignedLong:
  351. case UnsignedLongLong:
  352. return false;
  353. };
  354. }
  355. /// adjust - Set forced language options.
  356. /// Apply changes to the target information with respect to certain
  357. /// language options which change the target configuration and adjust
  358. /// the language based on the target options where applicable.
  359. void TargetInfo::adjust(DiagnosticsEngine &Diags, LangOptions &Opts) {
  360. if (Opts.NoBitFieldTypeAlign)
  361. UseBitFieldTypeAlignment = false;
  362. switch (Opts.WCharSize) {
  363. default: llvm_unreachable("invalid wchar_t width");
  364. case 0: break;
  365. case 1: WCharType = Opts.WCharIsSigned ? SignedChar : UnsignedChar; break;
  366. case 2: WCharType = Opts.WCharIsSigned ? SignedShort : UnsignedShort; break;
  367. case 4: WCharType = Opts.WCharIsSigned ? SignedInt : UnsignedInt; break;
  368. }
  369. if (Opts.AlignDouble) {
  370. DoubleAlign = LongLongAlign = 64;
  371. LongDoubleAlign = 64;
  372. }
  373. if (Opts.OpenCL) {
  374. // OpenCL C requires specific widths for types, irrespective of
  375. // what these normally are for the target.
  376. // We also define long long and long double here, although the
  377. // OpenCL standard only mentions these as "reserved".
  378. IntWidth = IntAlign = 32;
  379. LongWidth = LongAlign = 64;
  380. LongLongWidth = LongLongAlign = 128;
  381. HalfWidth = HalfAlign = 16;
  382. FloatWidth = FloatAlign = 32;
  383. // Embedded 32-bit targets (OpenCL EP) might have double C type
  384. // defined as float. Let's not override this as it might lead
  385. // to generating illegal code that uses 64bit doubles.
  386. if (DoubleWidth != FloatWidth) {
  387. DoubleWidth = DoubleAlign = 64;
  388. DoubleFormat = &llvm::APFloat::IEEEdouble();
  389. }
  390. LongDoubleWidth = LongDoubleAlign = 128;
  391. unsigned MaxPointerWidth = getMaxPointerWidth();
  392. assert(MaxPointerWidth == 32 || MaxPointerWidth == 64);
  393. bool Is32BitArch = MaxPointerWidth == 32;
  394. SizeType = Is32BitArch ? UnsignedInt : UnsignedLong;
  395. PtrDiffType = Is32BitArch ? SignedInt : SignedLong;
  396. IntPtrType = Is32BitArch ? SignedInt : SignedLong;
  397. IntMaxType = SignedLongLong;
  398. Int64Type = SignedLong;
  399. HalfFormat = &llvm::APFloat::IEEEhalf();
  400. FloatFormat = &llvm::APFloat::IEEEsingle();
  401. LongDoubleFormat = &llvm::APFloat::IEEEquad();
  402. // OpenCL C v3.0 s6.7.5 - The generic address space requires support for
  403. // OpenCL C 2.0 or OpenCL C 3.0 with the __opencl_c_generic_address_space
  404. // feature
  405. // OpenCL C v3.0 s6.2.1 - OpenCL pipes require support of OpenCL C 2.0
  406. // or later and __opencl_c_pipes feature
  407. // FIXME: These language options are also defined in setLangDefaults()
  408. // for OpenCL C 2.0 but with no access to target capabilities. Target
  409. // should be immutable once created and thus these language options need
  410. // to be defined only once.
  411. if (Opts.getOpenCLCompatibleVersion() == 300) {
  412. const auto &OpenCLFeaturesMap = getSupportedOpenCLOpts();
  413. Opts.OpenCLGenericAddressSpace = hasFeatureEnabled(
  414. OpenCLFeaturesMap, "__opencl_c_generic_address_space");
  415. Opts.OpenCLPipes =
  416. hasFeatureEnabled(OpenCLFeaturesMap, "__opencl_c_pipes");
  417. Opts.Blocks =
  418. hasFeatureEnabled(OpenCLFeaturesMap, "__opencl_c_device_enqueue");
  419. }
  420. }
  421. if (Opts.DoubleSize) {
  422. if (Opts.DoubleSize == 32) {
  423. DoubleWidth = 32;
  424. LongDoubleWidth = 32;
  425. DoubleFormat = &llvm::APFloat::IEEEsingle();
  426. LongDoubleFormat = &llvm::APFloat::IEEEsingle();
  427. } else if (Opts.DoubleSize == 64) {
  428. DoubleWidth = 64;
  429. LongDoubleWidth = 64;
  430. DoubleFormat = &llvm::APFloat::IEEEdouble();
  431. LongDoubleFormat = &llvm::APFloat::IEEEdouble();
  432. }
  433. }
  434. if (Opts.LongDoubleSize) {
  435. if (Opts.LongDoubleSize == DoubleWidth) {
  436. LongDoubleWidth = DoubleWidth;
  437. LongDoubleAlign = DoubleAlign;
  438. LongDoubleFormat = DoubleFormat;
  439. } else if (Opts.LongDoubleSize == 128) {
  440. LongDoubleWidth = LongDoubleAlign = 128;
  441. LongDoubleFormat = &llvm::APFloat::IEEEquad();
  442. } else if (Opts.LongDoubleSize == 80) {
  443. LongDoubleFormat = &llvm::APFloat::x87DoubleExtended();
  444. if (getTriple().isWindowsMSVCEnvironment()) {
  445. LongDoubleWidth = 128;
  446. LongDoubleAlign = 128;
  447. } else { // Linux
  448. if (getTriple().getArch() == llvm::Triple::x86) {
  449. LongDoubleWidth = 96;
  450. LongDoubleAlign = 32;
  451. } else {
  452. LongDoubleWidth = 128;
  453. LongDoubleAlign = 128;
  454. }
  455. }
  456. }
  457. }
  458. if (Opts.NewAlignOverride)
  459. NewAlign = Opts.NewAlignOverride * getCharWidth();
  460. // Each unsigned fixed point type has the same number of fractional bits as
  461. // its corresponding signed type.
  462. PaddingOnUnsignedFixedPoint |= Opts.PaddingOnUnsignedFixedPoint;
  463. CheckFixedPointBits();
  464. if (Opts.ProtectParens && !checkArithmeticFenceSupported()) {
  465. Diags.Report(diag::err_opt_not_valid_on_target) << "-fprotect-parens";
  466. Opts.ProtectParens = false;
  467. }
  468. if (Opts.MaxBitIntWidth)
  469. MaxBitIntWidth = static_cast<unsigned>(Opts.MaxBitIntWidth);
  470. if (Opts.FakeAddressSpaceMap)
  471. AddrSpaceMap = &FakeAddrSpaceMap;
  472. }
  473. bool TargetInfo::initFeatureMap(
  474. llvm::StringMap<bool> &Features, DiagnosticsEngine &Diags, StringRef CPU,
  475. const std::vector<std::string> &FeatureVec) const {
  476. for (const auto &F : FeatureVec) {
  477. StringRef Name = F;
  478. if (Name.empty())
  479. continue;
  480. // Apply the feature via the target.
  481. if (Name[0] != '+' && Name[0] != '-')
  482. Diags.Report(diag::warn_fe_backend_invalid_feature_flag) << Name;
  483. else
  484. setFeatureEnabled(Features, Name.substr(1), Name[0] == '+');
  485. }
  486. return true;
  487. }
  488. ParsedTargetAttr TargetInfo::parseTargetAttr(StringRef Features) const {
  489. ParsedTargetAttr Ret;
  490. if (Features == "default")
  491. return Ret;
  492. SmallVector<StringRef, 1> AttrFeatures;
  493. Features.split(AttrFeatures, ",");
  494. // Grab the various features and prepend a "+" to turn on the feature to
  495. // the backend and add them to our existing set of features.
  496. for (auto &Feature : AttrFeatures) {
  497. // Go ahead and trim whitespace rather than either erroring or
  498. // accepting it weirdly.
  499. Feature = Feature.trim();
  500. // TODO: Support the fpmath option. It will require checking
  501. // overall feature validity for the function with the rest of the
  502. // attributes on the function.
  503. if (Feature.startswith("fpmath="))
  504. continue;
  505. if (Feature.startswith("branch-protection=")) {
  506. Ret.BranchProtection = Feature.split('=').second.trim();
  507. continue;
  508. }
  509. // While we're here iterating check for a different target cpu.
  510. if (Feature.startswith("arch=")) {
  511. if (!Ret.CPU.empty())
  512. Ret.Duplicate = "arch=";
  513. else
  514. Ret.CPU = Feature.split("=").second.trim();
  515. } else if (Feature.startswith("tune=")) {
  516. if (!Ret.Tune.empty())
  517. Ret.Duplicate = "tune=";
  518. else
  519. Ret.Tune = Feature.split("=").second.trim();
  520. } else if (Feature.startswith("no-"))
  521. Ret.Features.push_back("-" + Feature.split("-").second.str());
  522. else
  523. Ret.Features.push_back("+" + Feature.str());
  524. }
  525. return Ret;
  526. }
  527. TargetInfo::CallingConvKind
  528. TargetInfo::getCallingConvKind(bool ClangABICompat4) const {
  529. if (getCXXABI() != TargetCXXABI::Microsoft &&
  530. (ClangABICompat4 || getTriple().isPS4()))
  531. return CCK_ClangABI4OrPS4;
  532. return CCK_Default;
  533. }
  534. bool TargetInfo::areDefaultedSMFStillPOD(const LangOptions &LangOpts) const {
  535. return LangOpts.getClangABICompat() > LangOptions::ClangABI::Ver15;
  536. }
  537. LangAS TargetInfo::getOpenCLTypeAddrSpace(OpenCLTypeKind TK) const {
  538. switch (TK) {
  539. case OCLTK_Image:
  540. case OCLTK_Pipe:
  541. return LangAS::opencl_global;
  542. case OCLTK_Sampler:
  543. return LangAS::opencl_constant;
  544. default:
  545. return LangAS::Default;
  546. }
  547. }
  548. //===----------------------------------------------------------------------===//
  549. static StringRef removeGCCRegisterPrefix(StringRef Name) {
  550. if (Name[0] == '%' || Name[0] == '#')
  551. Name = Name.substr(1);
  552. return Name;
  553. }
  554. /// isValidClobber - Returns whether the passed in string is
  555. /// a valid clobber in an inline asm statement. This is used by
  556. /// Sema.
  557. bool TargetInfo::isValidClobber(StringRef Name) const {
  558. return (isValidGCCRegisterName(Name) || Name == "memory" || Name == "cc" ||
  559. Name == "unwind");
  560. }
  561. /// isValidGCCRegisterName - Returns whether the passed in string
  562. /// is a valid register name according to GCC. This is used by Sema for
  563. /// inline asm statements.
  564. bool TargetInfo::isValidGCCRegisterName(StringRef Name) const {
  565. if (Name.empty())
  566. return false;
  567. // Get rid of any register prefix.
  568. Name = removeGCCRegisterPrefix(Name);
  569. if (Name.empty())
  570. return false;
  571. ArrayRef<const char *> Names = getGCCRegNames();
  572. // If we have a number it maps to an entry in the register name array.
  573. if (isDigit(Name[0])) {
  574. unsigned n;
  575. if (!Name.getAsInteger(0, n))
  576. return n < Names.size();
  577. }
  578. // Check register names.
  579. if (llvm::is_contained(Names, Name))
  580. return true;
  581. // Check any additional names that we have.
  582. for (const AddlRegName &ARN : getGCCAddlRegNames())
  583. for (const char *AN : ARN.Names) {
  584. if (!AN)
  585. break;
  586. // Make sure the register that the additional name is for is within
  587. // the bounds of the register names from above.
  588. if (AN == Name && ARN.RegNum < Names.size())
  589. return true;
  590. }
  591. // Now check aliases.
  592. for (const GCCRegAlias &GRA : getGCCRegAliases())
  593. for (const char *A : GRA.Aliases) {
  594. if (!A)
  595. break;
  596. if (A == Name)
  597. return true;
  598. }
  599. return false;
  600. }
  601. StringRef TargetInfo::getNormalizedGCCRegisterName(StringRef Name,
  602. bool ReturnCanonical) const {
  603. assert(isValidGCCRegisterName(Name) && "Invalid register passed in");
  604. // Get rid of any register prefix.
  605. Name = removeGCCRegisterPrefix(Name);
  606. ArrayRef<const char *> Names = getGCCRegNames();
  607. // First, check if we have a number.
  608. if (isDigit(Name[0])) {
  609. unsigned n;
  610. if (!Name.getAsInteger(0, n)) {
  611. assert(n < Names.size() && "Out of bounds register number!");
  612. return Names[n];
  613. }
  614. }
  615. // Check any additional names that we have.
  616. for (const AddlRegName &ARN : getGCCAddlRegNames())
  617. for (const char *AN : ARN.Names) {
  618. if (!AN)
  619. break;
  620. // Make sure the register that the additional name is for is within
  621. // the bounds of the register names from above.
  622. if (AN == Name && ARN.RegNum < Names.size())
  623. return ReturnCanonical ? Names[ARN.RegNum] : Name;
  624. }
  625. // Now check aliases.
  626. for (const GCCRegAlias &RA : getGCCRegAliases())
  627. for (const char *A : RA.Aliases) {
  628. if (!A)
  629. break;
  630. if (A == Name)
  631. return RA.Register;
  632. }
  633. return Name;
  634. }
  635. bool TargetInfo::validateOutputConstraint(ConstraintInfo &Info) const {
  636. const char *Name = Info.getConstraintStr().c_str();
  637. // An output constraint must start with '=' or '+'
  638. if (*Name != '=' && *Name != '+')
  639. return false;
  640. if (*Name == '+')
  641. Info.setIsReadWrite();
  642. Name++;
  643. while (*Name) {
  644. switch (*Name) {
  645. default:
  646. if (!validateAsmConstraint(Name, Info)) {
  647. // FIXME: We temporarily return false
  648. // so we can add more constraints as we hit it.
  649. // Eventually, an unknown constraint should just be treated as 'g'.
  650. return false;
  651. }
  652. break;
  653. case '&': // early clobber.
  654. Info.setEarlyClobber();
  655. break;
  656. case '%': // commutative.
  657. // FIXME: Check that there is a another register after this one.
  658. break;
  659. case 'r': // general register.
  660. Info.setAllowsRegister();
  661. break;
  662. case 'm': // memory operand.
  663. case 'o': // offsetable memory operand.
  664. case 'V': // non-offsetable memory operand.
  665. case '<': // autodecrement memory operand.
  666. case '>': // autoincrement memory operand.
  667. Info.setAllowsMemory();
  668. break;
  669. case 'g': // general register, memory operand or immediate integer.
  670. case 'X': // any operand.
  671. Info.setAllowsRegister();
  672. Info.setAllowsMemory();
  673. break;
  674. case ',': // multiple alternative constraint. Pass it.
  675. // Handle additional optional '=' or '+' modifiers.
  676. if (Name[1] == '=' || Name[1] == '+')
  677. Name++;
  678. break;
  679. case '#': // Ignore as constraint.
  680. while (Name[1] && Name[1] != ',')
  681. Name++;
  682. break;
  683. case '?': // Disparage slightly code.
  684. case '!': // Disparage severely.
  685. case '*': // Ignore for choosing register preferences.
  686. case 'i': // Ignore i,n,E,F as output constraints (match from the other
  687. // chars)
  688. case 'n':
  689. case 'E':
  690. case 'F':
  691. break; // Pass them.
  692. }
  693. Name++;
  694. }
  695. // Early clobber with a read-write constraint which doesn't permit registers
  696. // is invalid.
  697. if (Info.earlyClobber() && Info.isReadWrite() && !Info.allowsRegister())
  698. return false;
  699. // If a constraint allows neither memory nor register operands it contains
  700. // only modifiers. Reject it.
  701. return Info.allowsMemory() || Info.allowsRegister();
  702. }
  703. bool TargetInfo::resolveSymbolicName(const char *&Name,
  704. ArrayRef<ConstraintInfo> OutputConstraints,
  705. unsigned &Index) const {
  706. assert(*Name == '[' && "Symbolic name did not start with '['");
  707. Name++;
  708. const char *Start = Name;
  709. while (*Name && *Name != ']')
  710. Name++;
  711. if (!*Name) {
  712. // Missing ']'
  713. return false;
  714. }
  715. std::string SymbolicName(Start, Name - Start);
  716. for (Index = 0; Index != OutputConstraints.size(); ++Index)
  717. if (SymbolicName == OutputConstraints[Index].getName())
  718. return true;
  719. return false;
  720. }
  721. bool TargetInfo::validateInputConstraint(
  722. MutableArrayRef<ConstraintInfo> OutputConstraints,
  723. ConstraintInfo &Info) const {
  724. const char *Name = Info.ConstraintStr.c_str();
  725. if (!*Name)
  726. return false;
  727. while (*Name) {
  728. switch (*Name) {
  729. default:
  730. // Check if we have a matching constraint
  731. if (*Name >= '0' && *Name <= '9') {
  732. const char *DigitStart = Name;
  733. while (Name[1] >= '0' && Name[1] <= '9')
  734. Name++;
  735. const char *DigitEnd = Name;
  736. unsigned i;
  737. if (StringRef(DigitStart, DigitEnd - DigitStart + 1)
  738. .getAsInteger(10, i))
  739. return false;
  740. // Check if matching constraint is out of bounds.
  741. if (i >= OutputConstraints.size()) return false;
  742. // A number must refer to an output only operand.
  743. if (OutputConstraints[i].isReadWrite())
  744. return false;
  745. // If the constraint is already tied, it must be tied to the
  746. // same operand referenced to by the number.
  747. if (Info.hasTiedOperand() && Info.getTiedOperand() != i)
  748. return false;
  749. // The constraint should have the same info as the respective
  750. // output constraint.
  751. Info.setTiedOperand(i, OutputConstraints[i]);
  752. } else if (!validateAsmConstraint(Name, Info)) {
  753. // FIXME: This error return is in place temporarily so we can
  754. // add more constraints as we hit it. Eventually, an unknown
  755. // constraint should just be treated as 'g'.
  756. return false;
  757. }
  758. break;
  759. case '[': {
  760. unsigned Index = 0;
  761. if (!resolveSymbolicName(Name, OutputConstraints, Index))
  762. return false;
  763. // If the constraint is already tied, it must be tied to the
  764. // same operand referenced to by the number.
  765. if (Info.hasTiedOperand() && Info.getTiedOperand() != Index)
  766. return false;
  767. // A number must refer to an output only operand.
  768. if (OutputConstraints[Index].isReadWrite())
  769. return false;
  770. Info.setTiedOperand(Index, OutputConstraints[Index]);
  771. break;
  772. }
  773. case '%': // commutative
  774. // FIXME: Fail if % is used with the last operand.
  775. break;
  776. case 'i': // immediate integer.
  777. break;
  778. case 'n': // immediate integer with a known value.
  779. Info.setRequiresImmediate();
  780. break;
  781. case 'I': // Various constant constraints with target-specific meanings.
  782. case 'J':
  783. case 'K':
  784. case 'L':
  785. case 'M':
  786. case 'N':
  787. case 'O':
  788. case 'P':
  789. if (!validateAsmConstraint(Name, Info))
  790. return false;
  791. break;
  792. case 'r': // general register.
  793. Info.setAllowsRegister();
  794. break;
  795. case 'm': // memory operand.
  796. case 'o': // offsettable memory operand.
  797. case 'V': // non-offsettable memory operand.
  798. case '<': // autodecrement memory operand.
  799. case '>': // autoincrement memory operand.
  800. Info.setAllowsMemory();
  801. break;
  802. case 'g': // general register, memory operand or immediate integer.
  803. case 'X': // any operand.
  804. Info.setAllowsRegister();
  805. Info.setAllowsMemory();
  806. break;
  807. case 'E': // immediate floating point.
  808. case 'F': // immediate floating point.
  809. case 'p': // address operand.
  810. break;
  811. case ',': // multiple alternative constraint. Ignore comma.
  812. break;
  813. case '#': // Ignore as constraint.
  814. while (Name[1] && Name[1] != ',')
  815. Name++;
  816. break;
  817. case '?': // Disparage slightly code.
  818. case '!': // Disparage severely.
  819. case '*': // Ignore for choosing register preferences.
  820. break; // Pass them.
  821. }
  822. Name++;
  823. }
  824. return true;
  825. }
  826. void TargetInfo::CheckFixedPointBits() const {
  827. // Check that the number of fractional and integral bits (and maybe sign) can
  828. // fit into the bits given for a fixed point type.
  829. assert(ShortAccumScale + getShortAccumIBits() + 1 <= ShortAccumWidth);
  830. assert(AccumScale + getAccumIBits() + 1 <= AccumWidth);
  831. assert(LongAccumScale + getLongAccumIBits() + 1 <= LongAccumWidth);
  832. assert(getUnsignedShortAccumScale() + getUnsignedShortAccumIBits() <=
  833. ShortAccumWidth);
  834. assert(getUnsignedAccumScale() + getUnsignedAccumIBits() <= AccumWidth);
  835. assert(getUnsignedLongAccumScale() + getUnsignedLongAccumIBits() <=
  836. LongAccumWidth);
  837. assert(getShortFractScale() + 1 <= ShortFractWidth);
  838. assert(getFractScale() + 1 <= FractWidth);
  839. assert(getLongFractScale() + 1 <= LongFractWidth);
  840. assert(getUnsignedShortFractScale() <= ShortFractWidth);
  841. assert(getUnsignedFractScale() <= FractWidth);
  842. assert(getUnsignedLongFractScale() <= LongFractWidth);
  843. // Each unsigned fract type has either the same number of fractional bits
  844. // as, or one more fractional bit than, its corresponding signed fract type.
  845. assert(getShortFractScale() == getUnsignedShortFractScale() ||
  846. getShortFractScale() == getUnsignedShortFractScale() - 1);
  847. assert(getFractScale() == getUnsignedFractScale() ||
  848. getFractScale() == getUnsignedFractScale() - 1);
  849. assert(getLongFractScale() == getUnsignedLongFractScale() ||
  850. getLongFractScale() == getUnsignedLongFractScale() - 1);
  851. // When arranged in order of increasing rank (see 6.3.1.3a), the number of
  852. // fractional bits is nondecreasing for each of the following sets of
  853. // fixed-point types:
  854. // - signed fract types
  855. // - unsigned fract types
  856. // - signed accum types
  857. // - unsigned accum types.
  858. assert(getLongFractScale() >= getFractScale() &&
  859. getFractScale() >= getShortFractScale());
  860. assert(getUnsignedLongFractScale() >= getUnsignedFractScale() &&
  861. getUnsignedFractScale() >= getUnsignedShortFractScale());
  862. assert(LongAccumScale >= AccumScale && AccumScale >= ShortAccumScale);
  863. assert(getUnsignedLongAccumScale() >= getUnsignedAccumScale() &&
  864. getUnsignedAccumScale() >= getUnsignedShortAccumScale());
  865. // When arranged in order of increasing rank (see 6.3.1.3a), the number of
  866. // integral bits is nondecreasing for each of the following sets of
  867. // fixed-point types:
  868. // - signed accum types
  869. // - unsigned accum types
  870. assert(getLongAccumIBits() >= getAccumIBits() &&
  871. getAccumIBits() >= getShortAccumIBits());
  872. assert(getUnsignedLongAccumIBits() >= getUnsignedAccumIBits() &&
  873. getUnsignedAccumIBits() >= getUnsignedShortAccumIBits());
  874. // Each signed accum type has at least as many integral bits as its
  875. // corresponding unsigned accum type.
  876. assert(getShortAccumIBits() >= getUnsignedShortAccumIBits());
  877. assert(getAccumIBits() >= getUnsignedAccumIBits());
  878. assert(getLongAccumIBits() >= getUnsignedLongAccumIBits());
  879. }
  880. void TargetInfo::copyAuxTarget(const TargetInfo *Aux) {
  881. auto *Target = static_cast<TransferrableTargetInfo*>(this);
  882. auto *Src = static_cast<const TransferrableTargetInfo*>(Aux);
  883. *Target = *Src;
  884. }