TargetLoweringBase.cpp 75 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976
  1. //===- TargetLoweringBase.cpp - Implement the TargetLoweringBase class ----===//
  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 implements the TargetLoweringBase class.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "llvm/ADT/BitVector.h"
  13. #include "llvm/ADT/STLExtras.h"
  14. #include "llvm/ADT/SmallVector.h"
  15. #include "llvm/ADT/StringExtras.h"
  16. #include "llvm/ADT/StringRef.h"
  17. #include "llvm/ADT/Triple.h"
  18. #include "llvm/ADT/Twine.h"
  19. #include "llvm/CodeGen/Analysis.h"
  20. #include "llvm/CodeGen/ISDOpcodes.h"
  21. #include "llvm/CodeGen/MachineBasicBlock.h"
  22. #include "llvm/CodeGen/MachineFrameInfo.h"
  23. #include "llvm/CodeGen/MachineFunction.h"
  24. #include "llvm/CodeGen/MachineInstr.h"
  25. #include "llvm/CodeGen/MachineInstrBuilder.h"
  26. #include "llvm/CodeGen/MachineMemOperand.h"
  27. #include "llvm/CodeGen/MachineOperand.h"
  28. #include "llvm/CodeGen/MachineRegisterInfo.h"
  29. #include "llvm/CodeGen/RuntimeLibcalls.h"
  30. #include "llvm/CodeGen/StackMaps.h"
  31. #include "llvm/CodeGen/TargetLowering.h"
  32. #include "llvm/CodeGen/TargetOpcodes.h"
  33. #include "llvm/CodeGen/TargetRegisterInfo.h"
  34. #include "llvm/CodeGen/ValueTypes.h"
  35. #include "llvm/IR/Attributes.h"
  36. #include "llvm/IR/CallingConv.h"
  37. #include "llvm/IR/DataLayout.h"
  38. #include "llvm/IR/DerivedTypes.h"
  39. #include "llvm/IR/Function.h"
  40. #include "llvm/IR/GlobalValue.h"
  41. #include "llvm/IR/GlobalVariable.h"
  42. #include "llvm/IR/IRBuilder.h"
  43. #include "llvm/IR/Module.h"
  44. #include "llvm/IR/Type.h"
  45. #include "llvm/Support/BranchProbability.h"
  46. #include "llvm/Support/Casting.h"
  47. #include "llvm/Support/CommandLine.h"
  48. #include "llvm/Support/Compiler.h"
  49. #include "llvm/Support/ErrorHandling.h"
  50. #include "llvm/Support/MachineValueType.h"
  51. #include "llvm/Support/MathExtras.h"
  52. #include "llvm/Target/TargetMachine.h"
  53. #include <algorithm>
  54. #include <cassert>
  55. #include <cstddef>
  56. #include <cstdint>
  57. #include <cstring>
  58. #include <iterator>
  59. #include <string>
  60. #include <tuple>
  61. #include <utility>
  62. using namespace llvm;
  63. static cl::opt<bool> JumpIsExpensiveOverride(
  64. "jump-is-expensive", cl::init(false),
  65. cl::desc("Do not create extra branches to split comparison logic."),
  66. cl::Hidden);
  67. static cl::opt<unsigned> MinimumJumpTableEntries
  68. ("min-jump-table-entries", cl::init(4), cl::Hidden,
  69. cl::desc("Set minimum number of entries to use a jump table."));
  70. static cl::opt<unsigned> MaximumJumpTableSize
  71. ("max-jump-table-size", cl::init(UINT_MAX), cl::Hidden,
  72. cl::desc("Set maximum size of jump tables."));
  73. /// Minimum jump table density for normal functions.
  74. static cl::opt<unsigned>
  75. JumpTableDensity("jump-table-density", cl::init(10), cl::Hidden,
  76. cl::desc("Minimum density for building a jump table in "
  77. "a normal function"));
  78. /// Minimum jump table density for -Os or -Oz functions.
  79. static cl::opt<unsigned> OptsizeJumpTableDensity(
  80. "optsize-jump-table-density", cl::init(40), cl::Hidden,
  81. cl::desc("Minimum density for building a jump table in "
  82. "an optsize function"));
  83. static bool darwinHasSinCos(const Triple &TT) {
  84. assert(TT.isOSDarwin() && "should be called with darwin triple");
  85. // Don't bother with 32 bit x86.
  86. if (TT.getArch() == Triple::x86)
  87. return false;
  88. // Macos < 10.9 has no sincos_stret.
  89. if (TT.isMacOSX())
  90. return !TT.isMacOSXVersionLT(10, 9) && TT.isArch64Bit();
  91. // iOS < 7.0 has no sincos_stret.
  92. if (TT.isiOS())
  93. return !TT.isOSVersionLT(7, 0);
  94. // Any other darwin such as WatchOS/TvOS is new enough.
  95. return true;
  96. }
  97. // Although this default value is arbitrary, it is not random. It is assumed
  98. // that a condition that evaluates the same way by a higher percentage than this
  99. // is best represented as control flow. Therefore, the default value N should be
  100. // set such that the win from N% correct executions is greater than the loss
  101. // from (100 - N)% mispredicted executions for the majority of intended targets.
  102. static cl::opt<int> MinPercentageForPredictableBranch(
  103. "min-predictable-branch", cl::init(99),
  104. cl::desc("Minimum percentage (0-100) that a condition must be either true "
  105. "or false to assume that the condition is predictable"),
  106. cl::Hidden);
  107. void TargetLoweringBase::InitLibcalls(const Triple &TT) {
  108. #define HANDLE_LIBCALL(code, name) \
  109. setLibcallName(RTLIB::code, name);
  110. #include "llvm/IR/RuntimeLibcalls.def"
  111. #undef HANDLE_LIBCALL
  112. // Initialize calling conventions to their default.
  113. for (int LC = 0; LC < RTLIB::UNKNOWN_LIBCALL; ++LC)
  114. setLibcallCallingConv((RTLIB::Libcall)LC, CallingConv::C);
  115. // For IEEE quad-precision libcall names, PPC uses "kf" instead of "tf".
  116. if (TT.getArch() == Triple::ppc || TT.isPPC64()) {
  117. setLibcallName(RTLIB::ADD_F128, "__addkf3");
  118. setLibcallName(RTLIB::SUB_F128, "__subkf3");
  119. setLibcallName(RTLIB::MUL_F128, "__mulkf3");
  120. setLibcallName(RTLIB::DIV_F128, "__divkf3");
  121. setLibcallName(RTLIB::FPEXT_F32_F128, "__extendsfkf2");
  122. setLibcallName(RTLIB::FPEXT_F64_F128, "__extenddfkf2");
  123. setLibcallName(RTLIB::FPROUND_F128_F32, "__trunckfsf2");
  124. setLibcallName(RTLIB::FPROUND_F128_F64, "__trunckfdf2");
  125. setLibcallName(RTLIB::FPTOSINT_F128_I32, "__fixkfsi");
  126. setLibcallName(RTLIB::FPTOSINT_F128_I64, "__fixkfdi");
  127. setLibcallName(RTLIB::FPTOUINT_F128_I32, "__fixunskfsi");
  128. setLibcallName(RTLIB::FPTOUINT_F128_I64, "__fixunskfdi");
  129. setLibcallName(RTLIB::SINTTOFP_I32_F128, "__floatsikf");
  130. setLibcallName(RTLIB::SINTTOFP_I64_F128, "__floatdikf");
  131. setLibcallName(RTLIB::UINTTOFP_I32_F128, "__floatunsikf");
  132. setLibcallName(RTLIB::UINTTOFP_I64_F128, "__floatundikf");
  133. setLibcallName(RTLIB::OEQ_F128, "__eqkf2");
  134. setLibcallName(RTLIB::UNE_F128, "__nekf2");
  135. setLibcallName(RTLIB::OGE_F128, "__gekf2");
  136. setLibcallName(RTLIB::OLT_F128, "__ltkf2");
  137. setLibcallName(RTLIB::OLE_F128, "__lekf2");
  138. setLibcallName(RTLIB::OGT_F128, "__gtkf2");
  139. setLibcallName(RTLIB::UO_F128, "__unordkf2");
  140. setLibcallName(RTLIB::O_F128, "__unordkf2");
  141. }
  142. // A few names are different on particular architectures or environments.
  143. if (TT.isOSDarwin()) {
  144. // For f16/f32 conversions, Darwin uses the standard naming scheme, instead
  145. // of the gnueabi-style __gnu_*_ieee.
  146. // FIXME: What about other targets?
  147. setLibcallName(RTLIB::FPEXT_F16_F32, "__extendhfsf2");
  148. setLibcallName(RTLIB::FPROUND_F32_F16, "__truncsfhf2");
  149. // Some darwins have an optimized __bzero/bzero function.
  150. switch (TT.getArch()) {
  151. case Triple::x86:
  152. case Triple::x86_64:
  153. if (TT.isMacOSX() && !TT.isMacOSXVersionLT(10, 6))
  154. setLibcallName(RTLIB::BZERO, "__bzero");
  155. break;
  156. case Triple::aarch64:
  157. setLibcallName(RTLIB::BZERO, "bzero");
  158. break;
  159. default:
  160. break;
  161. }
  162. if (darwinHasSinCos(TT)) {
  163. setLibcallName(RTLIB::SINCOS_STRET_F32, "__sincosf_stret");
  164. setLibcallName(RTLIB::SINCOS_STRET_F64, "__sincos_stret");
  165. if (TT.isWatchABI()) {
  166. setLibcallCallingConv(RTLIB::SINCOS_STRET_F32,
  167. CallingConv::ARM_AAPCS_VFP);
  168. setLibcallCallingConv(RTLIB::SINCOS_STRET_F64,
  169. CallingConv::ARM_AAPCS_VFP);
  170. }
  171. }
  172. } else {
  173. setLibcallName(RTLIB::FPEXT_F16_F32, "__gnu_h2f_ieee");
  174. setLibcallName(RTLIB::FPROUND_F32_F16, "__gnu_f2h_ieee");
  175. }
  176. if (TT.isGNUEnvironment() || TT.isOSFuchsia() ||
  177. (TT.isAndroid() && !TT.isAndroidVersionLT(9))) {
  178. setLibcallName(RTLIB::SINCOS_F32, "sincosf");
  179. setLibcallName(RTLIB::SINCOS_F64, "sincos");
  180. setLibcallName(RTLIB::SINCOS_F80, "sincosl");
  181. setLibcallName(RTLIB::SINCOS_F128, "sincosl");
  182. setLibcallName(RTLIB::SINCOS_PPCF128, "sincosl");
  183. }
  184. if (TT.isPS4CPU()) {
  185. setLibcallName(RTLIB::SINCOS_F32, "sincosf");
  186. setLibcallName(RTLIB::SINCOS_F64, "sincos");
  187. }
  188. if (TT.isOSOpenBSD()) {
  189. setLibcallName(RTLIB::STACKPROTECTOR_CHECK_FAIL, nullptr);
  190. }
  191. }
  192. /// getFPEXT - Return the FPEXT_*_* value for the given types, or
  193. /// UNKNOWN_LIBCALL if there is none.
  194. RTLIB::Libcall RTLIB::getFPEXT(EVT OpVT, EVT RetVT) {
  195. if (OpVT == MVT::f16) {
  196. if (RetVT == MVT::f32)
  197. return FPEXT_F16_F32;
  198. } else if (OpVT == MVT::f32) {
  199. if (RetVT == MVT::f64)
  200. return FPEXT_F32_F64;
  201. if (RetVT == MVT::f128)
  202. return FPEXT_F32_F128;
  203. if (RetVT == MVT::ppcf128)
  204. return FPEXT_F32_PPCF128;
  205. } else if (OpVT == MVT::f64) {
  206. if (RetVT == MVT::f128)
  207. return FPEXT_F64_F128;
  208. else if (RetVT == MVT::ppcf128)
  209. return FPEXT_F64_PPCF128;
  210. } else if (OpVT == MVT::f80) {
  211. if (RetVT == MVT::f128)
  212. return FPEXT_F80_F128;
  213. }
  214. return UNKNOWN_LIBCALL;
  215. }
  216. /// getFPROUND - Return the FPROUND_*_* value for the given types, or
  217. /// UNKNOWN_LIBCALL if there is none.
  218. RTLIB::Libcall RTLIB::getFPROUND(EVT OpVT, EVT RetVT) {
  219. if (RetVT == MVT::f16) {
  220. if (OpVT == MVT::f32)
  221. return FPROUND_F32_F16;
  222. if (OpVT == MVT::f64)
  223. return FPROUND_F64_F16;
  224. if (OpVT == MVT::f80)
  225. return FPROUND_F80_F16;
  226. if (OpVT == MVT::f128)
  227. return FPROUND_F128_F16;
  228. if (OpVT == MVT::ppcf128)
  229. return FPROUND_PPCF128_F16;
  230. } else if (RetVT == MVT::f32) {
  231. if (OpVT == MVT::f64)
  232. return FPROUND_F64_F32;
  233. if (OpVT == MVT::f80)
  234. return FPROUND_F80_F32;
  235. if (OpVT == MVT::f128)
  236. return FPROUND_F128_F32;
  237. if (OpVT == MVT::ppcf128)
  238. return FPROUND_PPCF128_F32;
  239. } else if (RetVT == MVT::f64) {
  240. if (OpVT == MVT::f80)
  241. return FPROUND_F80_F64;
  242. if (OpVT == MVT::f128)
  243. return FPROUND_F128_F64;
  244. if (OpVT == MVT::ppcf128)
  245. return FPROUND_PPCF128_F64;
  246. } else if (RetVT == MVT::f80) {
  247. if (OpVT == MVT::f128)
  248. return FPROUND_F128_F80;
  249. }
  250. return UNKNOWN_LIBCALL;
  251. }
  252. /// getFPTOSINT - Return the FPTOSINT_*_* value for the given types, or
  253. /// UNKNOWN_LIBCALL if there is none.
  254. RTLIB::Libcall RTLIB::getFPTOSINT(EVT OpVT, EVT RetVT) {
  255. if (OpVT == MVT::f32) {
  256. if (RetVT == MVT::i32)
  257. return FPTOSINT_F32_I32;
  258. if (RetVT == MVT::i64)
  259. return FPTOSINT_F32_I64;
  260. if (RetVT == MVT::i128)
  261. return FPTOSINT_F32_I128;
  262. } else if (OpVT == MVT::f64) {
  263. if (RetVT == MVT::i32)
  264. return FPTOSINT_F64_I32;
  265. if (RetVT == MVT::i64)
  266. return FPTOSINT_F64_I64;
  267. if (RetVT == MVT::i128)
  268. return FPTOSINT_F64_I128;
  269. } else if (OpVT == MVT::f80) {
  270. if (RetVT == MVT::i32)
  271. return FPTOSINT_F80_I32;
  272. if (RetVT == MVT::i64)
  273. return FPTOSINT_F80_I64;
  274. if (RetVT == MVT::i128)
  275. return FPTOSINT_F80_I128;
  276. } else if (OpVT == MVT::f128) {
  277. if (RetVT == MVT::i32)
  278. return FPTOSINT_F128_I32;
  279. if (RetVT == MVT::i64)
  280. return FPTOSINT_F128_I64;
  281. if (RetVT == MVT::i128)
  282. return FPTOSINT_F128_I128;
  283. } else if (OpVT == MVT::ppcf128) {
  284. if (RetVT == MVT::i32)
  285. return FPTOSINT_PPCF128_I32;
  286. if (RetVT == MVT::i64)
  287. return FPTOSINT_PPCF128_I64;
  288. if (RetVT == MVT::i128)
  289. return FPTOSINT_PPCF128_I128;
  290. }
  291. return UNKNOWN_LIBCALL;
  292. }
  293. /// getFPTOUINT - Return the FPTOUINT_*_* value for the given types, or
  294. /// UNKNOWN_LIBCALL if there is none.
  295. RTLIB::Libcall RTLIB::getFPTOUINT(EVT OpVT, EVT RetVT) {
  296. if (OpVT == MVT::f32) {
  297. if (RetVT == MVT::i32)
  298. return FPTOUINT_F32_I32;
  299. if (RetVT == MVT::i64)
  300. return FPTOUINT_F32_I64;
  301. if (RetVT == MVT::i128)
  302. return FPTOUINT_F32_I128;
  303. } else if (OpVT == MVT::f64) {
  304. if (RetVT == MVT::i32)
  305. return FPTOUINT_F64_I32;
  306. if (RetVT == MVT::i64)
  307. return FPTOUINT_F64_I64;
  308. if (RetVT == MVT::i128)
  309. return FPTOUINT_F64_I128;
  310. } else if (OpVT == MVT::f80) {
  311. if (RetVT == MVT::i32)
  312. return FPTOUINT_F80_I32;
  313. if (RetVT == MVT::i64)
  314. return FPTOUINT_F80_I64;
  315. if (RetVT == MVT::i128)
  316. return FPTOUINT_F80_I128;
  317. } else if (OpVT == MVT::f128) {
  318. if (RetVT == MVT::i32)
  319. return FPTOUINT_F128_I32;
  320. if (RetVT == MVT::i64)
  321. return FPTOUINT_F128_I64;
  322. if (RetVT == MVT::i128)
  323. return FPTOUINT_F128_I128;
  324. } else if (OpVT == MVT::ppcf128) {
  325. if (RetVT == MVT::i32)
  326. return FPTOUINT_PPCF128_I32;
  327. if (RetVT == MVT::i64)
  328. return FPTOUINT_PPCF128_I64;
  329. if (RetVT == MVT::i128)
  330. return FPTOUINT_PPCF128_I128;
  331. }
  332. return UNKNOWN_LIBCALL;
  333. }
  334. /// getSINTTOFP - Return the SINTTOFP_*_* value for the given types, or
  335. /// UNKNOWN_LIBCALL if there is none.
  336. RTLIB::Libcall RTLIB::getSINTTOFP(EVT OpVT, EVT RetVT) {
  337. if (OpVT == MVT::i32) {
  338. if (RetVT == MVT::f32)
  339. return SINTTOFP_I32_F32;
  340. if (RetVT == MVT::f64)
  341. return SINTTOFP_I32_F64;
  342. if (RetVT == MVT::f80)
  343. return SINTTOFP_I32_F80;
  344. if (RetVT == MVT::f128)
  345. return SINTTOFP_I32_F128;
  346. if (RetVT == MVT::ppcf128)
  347. return SINTTOFP_I32_PPCF128;
  348. } else if (OpVT == MVT::i64) {
  349. if (RetVT == MVT::f32)
  350. return SINTTOFP_I64_F32;
  351. if (RetVT == MVT::f64)
  352. return SINTTOFP_I64_F64;
  353. if (RetVT == MVT::f80)
  354. return SINTTOFP_I64_F80;
  355. if (RetVT == MVT::f128)
  356. return SINTTOFP_I64_F128;
  357. if (RetVT == MVT::ppcf128)
  358. return SINTTOFP_I64_PPCF128;
  359. } else if (OpVT == MVT::i128) {
  360. if (RetVT == MVT::f32)
  361. return SINTTOFP_I128_F32;
  362. if (RetVT == MVT::f64)
  363. return SINTTOFP_I128_F64;
  364. if (RetVT == MVT::f80)
  365. return SINTTOFP_I128_F80;
  366. if (RetVT == MVT::f128)
  367. return SINTTOFP_I128_F128;
  368. if (RetVT == MVT::ppcf128)
  369. return SINTTOFP_I128_PPCF128;
  370. }
  371. return UNKNOWN_LIBCALL;
  372. }
  373. /// getUINTTOFP - Return the UINTTOFP_*_* value for the given types, or
  374. /// UNKNOWN_LIBCALL if there is none.
  375. RTLIB::Libcall RTLIB::getUINTTOFP(EVT OpVT, EVT RetVT) {
  376. if (OpVT == MVT::i32) {
  377. if (RetVT == MVT::f32)
  378. return UINTTOFP_I32_F32;
  379. if (RetVT == MVT::f64)
  380. return UINTTOFP_I32_F64;
  381. if (RetVT == MVT::f80)
  382. return UINTTOFP_I32_F80;
  383. if (RetVT == MVT::f128)
  384. return UINTTOFP_I32_F128;
  385. if (RetVT == MVT::ppcf128)
  386. return UINTTOFP_I32_PPCF128;
  387. } else if (OpVT == MVT::i64) {
  388. if (RetVT == MVT::f32)
  389. return UINTTOFP_I64_F32;
  390. if (RetVT == MVT::f64)
  391. return UINTTOFP_I64_F64;
  392. if (RetVT == MVT::f80)
  393. return UINTTOFP_I64_F80;
  394. if (RetVT == MVT::f128)
  395. return UINTTOFP_I64_F128;
  396. if (RetVT == MVT::ppcf128)
  397. return UINTTOFP_I64_PPCF128;
  398. } else if (OpVT == MVT::i128) {
  399. if (RetVT == MVT::f32)
  400. return UINTTOFP_I128_F32;
  401. if (RetVT == MVT::f64)
  402. return UINTTOFP_I128_F64;
  403. if (RetVT == MVT::f80)
  404. return UINTTOFP_I128_F80;
  405. if (RetVT == MVT::f128)
  406. return UINTTOFP_I128_F128;
  407. if (RetVT == MVT::ppcf128)
  408. return UINTTOFP_I128_PPCF128;
  409. }
  410. return UNKNOWN_LIBCALL;
  411. }
  412. RTLIB::Libcall RTLIB::getSYNC(unsigned Opc, MVT VT) {
  413. #define OP_TO_LIBCALL(Name, Enum) \
  414. case Name: \
  415. switch (VT.SimpleTy) { \
  416. default: \
  417. return UNKNOWN_LIBCALL; \
  418. case MVT::i8: \
  419. return Enum##_1; \
  420. case MVT::i16: \
  421. return Enum##_2; \
  422. case MVT::i32: \
  423. return Enum##_4; \
  424. case MVT::i64: \
  425. return Enum##_8; \
  426. case MVT::i128: \
  427. return Enum##_16; \
  428. }
  429. switch (Opc) {
  430. OP_TO_LIBCALL(ISD::ATOMIC_SWAP, SYNC_LOCK_TEST_AND_SET)
  431. OP_TO_LIBCALL(ISD::ATOMIC_CMP_SWAP, SYNC_VAL_COMPARE_AND_SWAP)
  432. OP_TO_LIBCALL(ISD::ATOMIC_LOAD_ADD, SYNC_FETCH_AND_ADD)
  433. OP_TO_LIBCALL(ISD::ATOMIC_LOAD_SUB, SYNC_FETCH_AND_SUB)
  434. OP_TO_LIBCALL(ISD::ATOMIC_LOAD_AND, SYNC_FETCH_AND_AND)
  435. OP_TO_LIBCALL(ISD::ATOMIC_LOAD_OR, SYNC_FETCH_AND_OR)
  436. OP_TO_LIBCALL(ISD::ATOMIC_LOAD_XOR, SYNC_FETCH_AND_XOR)
  437. OP_TO_LIBCALL(ISD::ATOMIC_LOAD_NAND, SYNC_FETCH_AND_NAND)
  438. OP_TO_LIBCALL(ISD::ATOMIC_LOAD_MAX, SYNC_FETCH_AND_MAX)
  439. OP_TO_LIBCALL(ISD::ATOMIC_LOAD_UMAX, SYNC_FETCH_AND_UMAX)
  440. OP_TO_LIBCALL(ISD::ATOMIC_LOAD_MIN, SYNC_FETCH_AND_MIN)
  441. OP_TO_LIBCALL(ISD::ATOMIC_LOAD_UMIN, SYNC_FETCH_AND_UMIN)
  442. }
  443. #undef OP_TO_LIBCALL
  444. return UNKNOWN_LIBCALL;
  445. }
  446. RTLIB::Libcall RTLIB::getMEMCPY_ELEMENT_UNORDERED_ATOMIC(uint64_t ElementSize) {
  447. switch (ElementSize) {
  448. case 1:
  449. return MEMCPY_ELEMENT_UNORDERED_ATOMIC_1;
  450. case 2:
  451. return MEMCPY_ELEMENT_UNORDERED_ATOMIC_2;
  452. case 4:
  453. return MEMCPY_ELEMENT_UNORDERED_ATOMIC_4;
  454. case 8:
  455. return MEMCPY_ELEMENT_UNORDERED_ATOMIC_8;
  456. case 16:
  457. return MEMCPY_ELEMENT_UNORDERED_ATOMIC_16;
  458. default:
  459. return UNKNOWN_LIBCALL;
  460. }
  461. }
  462. RTLIB::Libcall RTLIB::getMEMMOVE_ELEMENT_UNORDERED_ATOMIC(uint64_t ElementSize) {
  463. switch (ElementSize) {
  464. case 1:
  465. return MEMMOVE_ELEMENT_UNORDERED_ATOMIC_1;
  466. case 2:
  467. return MEMMOVE_ELEMENT_UNORDERED_ATOMIC_2;
  468. case 4:
  469. return MEMMOVE_ELEMENT_UNORDERED_ATOMIC_4;
  470. case 8:
  471. return MEMMOVE_ELEMENT_UNORDERED_ATOMIC_8;
  472. case 16:
  473. return MEMMOVE_ELEMENT_UNORDERED_ATOMIC_16;
  474. default:
  475. return UNKNOWN_LIBCALL;
  476. }
  477. }
  478. RTLIB::Libcall RTLIB::getMEMSET_ELEMENT_UNORDERED_ATOMIC(uint64_t ElementSize) {
  479. switch (ElementSize) {
  480. case 1:
  481. return MEMSET_ELEMENT_UNORDERED_ATOMIC_1;
  482. case 2:
  483. return MEMSET_ELEMENT_UNORDERED_ATOMIC_2;
  484. case 4:
  485. return MEMSET_ELEMENT_UNORDERED_ATOMIC_4;
  486. case 8:
  487. return MEMSET_ELEMENT_UNORDERED_ATOMIC_8;
  488. case 16:
  489. return MEMSET_ELEMENT_UNORDERED_ATOMIC_16;
  490. default:
  491. return UNKNOWN_LIBCALL;
  492. }
  493. }
  494. /// InitCmpLibcallCCs - Set default comparison libcall CC.
  495. static void InitCmpLibcallCCs(ISD::CondCode *CCs) {
  496. memset(CCs, ISD::SETCC_INVALID, sizeof(ISD::CondCode)*RTLIB::UNKNOWN_LIBCALL);
  497. CCs[RTLIB::OEQ_F32] = ISD::SETEQ;
  498. CCs[RTLIB::OEQ_F64] = ISD::SETEQ;
  499. CCs[RTLIB::OEQ_F128] = ISD::SETEQ;
  500. CCs[RTLIB::OEQ_PPCF128] = ISD::SETEQ;
  501. CCs[RTLIB::UNE_F32] = ISD::SETNE;
  502. CCs[RTLIB::UNE_F64] = ISD::SETNE;
  503. CCs[RTLIB::UNE_F128] = ISD::SETNE;
  504. CCs[RTLIB::UNE_PPCF128] = ISD::SETNE;
  505. CCs[RTLIB::OGE_F32] = ISD::SETGE;
  506. CCs[RTLIB::OGE_F64] = ISD::SETGE;
  507. CCs[RTLIB::OGE_F128] = ISD::SETGE;
  508. CCs[RTLIB::OGE_PPCF128] = ISD::SETGE;
  509. CCs[RTLIB::OLT_F32] = ISD::SETLT;
  510. CCs[RTLIB::OLT_F64] = ISD::SETLT;
  511. CCs[RTLIB::OLT_F128] = ISD::SETLT;
  512. CCs[RTLIB::OLT_PPCF128] = ISD::SETLT;
  513. CCs[RTLIB::OLE_F32] = ISD::SETLE;
  514. CCs[RTLIB::OLE_F64] = ISD::SETLE;
  515. CCs[RTLIB::OLE_F128] = ISD::SETLE;
  516. CCs[RTLIB::OLE_PPCF128] = ISD::SETLE;
  517. CCs[RTLIB::OGT_F32] = ISD::SETGT;
  518. CCs[RTLIB::OGT_F64] = ISD::SETGT;
  519. CCs[RTLIB::OGT_F128] = ISD::SETGT;
  520. CCs[RTLIB::OGT_PPCF128] = ISD::SETGT;
  521. CCs[RTLIB::UO_F32] = ISD::SETNE;
  522. CCs[RTLIB::UO_F64] = ISD::SETNE;
  523. CCs[RTLIB::UO_F128] = ISD::SETNE;
  524. CCs[RTLIB::UO_PPCF128] = ISD::SETNE;
  525. CCs[RTLIB::O_F32] = ISD::SETEQ;
  526. CCs[RTLIB::O_F64] = ISD::SETEQ;
  527. CCs[RTLIB::O_F128] = ISD::SETEQ;
  528. CCs[RTLIB::O_PPCF128] = ISD::SETEQ;
  529. }
  530. /// NOTE: The TargetMachine owns TLOF.
  531. TargetLoweringBase::TargetLoweringBase(const TargetMachine &tm) : TM(tm) {
  532. initActions();
  533. // Perform these initializations only once.
  534. MaxStoresPerMemset = MaxStoresPerMemcpy = MaxStoresPerMemmove =
  535. MaxLoadsPerMemcmp = 8;
  536. MaxGluedStoresPerMemcpy = 0;
  537. MaxStoresPerMemsetOptSize = MaxStoresPerMemcpyOptSize =
  538. MaxStoresPerMemmoveOptSize = MaxLoadsPerMemcmpOptSize = 4;
  539. UseUnderscoreSetJmp = false;
  540. UseUnderscoreLongJmp = false;
  541. HasMultipleConditionRegisters = false;
  542. HasExtractBitsInsn = false;
  543. JumpIsExpensive = JumpIsExpensiveOverride;
  544. PredictableSelectIsExpensive = false;
  545. EnableExtLdPromotion = false;
  546. StackPointerRegisterToSaveRestore = 0;
  547. BooleanContents = UndefinedBooleanContent;
  548. BooleanFloatContents = UndefinedBooleanContent;
  549. BooleanVectorContents = UndefinedBooleanContent;
  550. SchedPreferenceInfo = Sched::ILP;
  551. GatherAllAliasesMaxDepth = 18;
  552. // TODO: the default will be switched to 0 in the next commit, along
  553. // with the Target-specific changes necessary.
  554. MaxAtomicSizeInBitsSupported = 1024;
  555. MinCmpXchgSizeInBits = 0;
  556. SupportsUnalignedAtomics = false;
  557. std::fill(std::begin(LibcallRoutineNames), std::end(LibcallRoutineNames), nullptr);
  558. InitLibcalls(TM.getTargetTriple());
  559. InitCmpLibcallCCs(CmpLibcallCCs);
  560. }
  561. void TargetLoweringBase::initActions() {
  562. // All operations default to being supported.
  563. memset(OpActions, 0, sizeof(OpActions));
  564. memset(LoadExtActions, 0, sizeof(LoadExtActions));
  565. memset(TruncStoreActions, 0, sizeof(TruncStoreActions));
  566. memset(IndexedModeActions, 0, sizeof(IndexedModeActions));
  567. memset(CondCodeActions, 0, sizeof(CondCodeActions));
  568. std::fill(std::begin(RegClassForVT), std::end(RegClassForVT), nullptr);
  569. std::fill(std::begin(TargetDAGCombineArray),
  570. std::end(TargetDAGCombineArray), 0);
  571. for (MVT VT : MVT::fp_valuetypes()) {
  572. MVT IntVT = MVT::getIntegerVT(VT.getSizeInBits());
  573. if (IntVT.isValid()) {
  574. setOperationAction(ISD::ATOMIC_SWAP, VT, Promote);
  575. AddPromotedToType(ISD::ATOMIC_SWAP, VT, IntVT);
  576. }
  577. }
  578. // Set default actions for various operations.
  579. for (MVT VT : MVT::all_valuetypes()) {
  580. // Default all indexed load / store to expand.
  581. for (unsigned IM = (unsigned)ISD::PRE_INC;
  582. IM != (unsigned)ISD::LAST_INDEXED_MODE; ++IM) {
  583. setIndexedLoadAction(IM, VT, Expand);
  584. setIndexedStoreAction(IM, VT, Expand);
  585. }
  586. // Most backends expect to see the node which just returns the value loaded.
  587. setOperationAction(ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS, VT, Expand);
  588. // These operations default to expand.
  589. setOperationAction(ISD::FGETSIGN, VT, Expand);
  590. setOperationAction(ISD::CONCAT_VECTORS, VT, Expand);
  591. setOperationAction(ISD::FMINNUM, VT, Expand);
  592. setOperationAction(ISD::FMAXNUM, VT, Expand);
  593. setOperationAction(ISD::FMINNUM_IEEE, VT, Expand);
  594. setOperationAction(ISD::FMAXNUM_IEEE, VT, Expand);
  595. setOperationAction(ISD::FMINIMUM, VT, Expand);
  596. setOperationAction(ISD::FMAXIMUM, VT, Expand);
  597. setOperationAction(ISD::FMAD, VT, Expand);
  598. setOperationAction(ISD::SMIN, VT, Expand);
  599. setOperationAction(ISD::SMAX, VT, Expand);
  600. setOperationAction(ISD::UMIN, VT, Expand);
  601. setOperationAction(ISD::UMAX, VT, Expand);
  602. setOperationAction(ISD::ABS, VT, Expand);
  603. setOperationAction(ISD::FSHL, VT, Expand);
  604. setOperationAction(ISD::FSHR, VT, Expand);
  605. setOperationAction(ISD::SADDSAT, VT, Expand);
  606. setOperationAction(ISD::UADDSAT, VT, Expand);
  607. setOperationAction(ISD::SSUBSAT, VT, Expand);
  608. setOperationAction(ISD::USUBSAT, VT, Expand);
  609. setOperationAction(ISD::SMULFIX, VT, Expand);
  610. setOperationAction(ISD::SMULFIXSAT, VT, Expand);
  611. setOperationAction(ISD::UMULFIX, VT, Expand);
  612. // Overflow operations default to expand
  613. setOperationAction(ISD::SADDO, VT, Expand);
  614. setOperationAction(ISD::SSUBO, VT, Expand);
  615. setOperationAction(ISD::UADDO, VT, Expand);
  616. setOperationAction(ISD::USUBO, VT, Expand);
  617. setOperationAction(ISD::SMULO, VT, Expand);
  618. setOperationAction(ISD::UMULO, VT, Expand);
  619. // ADDCARRY operations default to expand
  620. setOperationAction(ISD::ADDCARRY, VT, Expand);
  621. setOperationAction(ISD::SUBCARRY, VT, Expand);
  622. setOperationAction(ISD::SETCCCARRY, VT, Expand);
  623. // ADDC/ADDE/SUBC/SUBE default to expand.
  624. setOperationAction(ISD::ADDC, VT, Expand);
  625. setOperationAction(ISD::ADDE, VT, Expand);
  626. setOperationAction(ISD::SUBC, VT, Expand);
  627. setOperationAction(ISD::SUBE, VT, Expand);
  628. // These default to Expand so they will be expanded to CTLZ/CTTZ by default.
  629. setOperationAction(ISD::CTLZ_ZERO_UNDEF, VT, Expand);
  630. setOperationAction(ISD::CTTZ_ZERO_UNDEF, VT, Expand);
  631. setOperationAction(ISD::BITREVERSE, VT, Expand);
  632. // These library functions default to expand.
  633. setOperationAction(ISD::FROUND, VT, Expand);
  634. setOperationAction(ISD::FPOWI, VT, Expand);
  635. // These operations default to expand for vector types.
  636. if (VT.isVector()) {
  637. setOperationAction(ISD::FCOPYSIGN, VT, Expand);
  638. setOperationAction(ISD::ANY_EXTEND_VECTOR_INREG, VT, Expand);
  639. setOperationAction(ISD::SIGN_EXTEND_VECTOR_INREG, VT, Expand);
  640. setOperationAction(ISD::ZERO_EXTEND_VECTOR_INREG, VT, Expand);
  641. }
  642. // Constrained floating-point operations default to expand.
  643. setOperationAction(ISD::STRICT_FADD, VT, Expand);
  644. setOperationAction(ISD::STRICT_FSUB, VT, Expand);
  645. setOperationAction(ISD::STRICT_FMUL, VT, Expand);
  646. setOperationAction(ISD::STRICT_FDIV, VT, Expand);
  647. setOperationAction(ISD::STRICT_FREM, VT, Expand);
  648. setOperationAction(ISD::STRICT_FMA, VT, Expand);
  649. setOperationAction(ISD::STRICT_FSQRT, VT, Expand);
  650. setOperationAction(ISD::STRICT_FPOW, VT, Expand);
  651. setOperationAction(ISD::STRICT_FPOWI, VT, Expand);
  652. setOperationAction(ISD::STRICT_FSIN, VT, Expand);
  653. setOperationAction(ISD::STRICT_FCOS, VT, Expand);
  654. setOperationAction(ISD::STRICT_FEXP, VT, Expand);
  655. setOperationAction(ISD::STRICT_FEXP2, VT, Expand);
  656. setOperationAction(ISD::STRICT_FLOG, VT, Expand);
  657. setOperationAction(ISD::STRICT_FLOG10, VT, Expand);
  658. setOperationAction(ISD::STRICT_FLOG2, VT, Expand);
  659. setOperationAction(ISD::STRICT_FRINT, VT, Expand);
  660. setOperationAction(ISD::STRICT_FNEARBYINT, VT, Expand);
  661. setOperationAction(ISD::STRICT_FCEIL, VT, Expand);
  662. setOperationAction(ISD::STRICT_FFLOOR, VT, Expand);
  663. setOperationAction(ISD::STRICT_FROUND, VT, Expand);
  664. setOperationAction(ISD::STRICT_FTRUNC, VT, Expand);
  665. setOperationAction(ISD::STRICT_FMAXNUM, VT, Expand);
  666. setOperationAction(ISD::STRICT_FMINNUM, VT, Expand);
  667. setOperationAction(ISD::STRICT_FP_ROUND, VT, Expand);
  668. setOperationAction(ISD::STRICT_FP_EXTEND, VT, Expand);
  669. setOperationAction(ISD::STRICT_FP_TO_SINT, VT, Expand);
  670. setOperationAction(ISD::STRICT_FP_TO_UINT, VT, Expand);
  671. // For most targets @llvm.get.dynamic.area.offset just returns 0.
  672. setOperationAction(ISD::GET_DYNAMIC_AREA_OFFSET, VT, Expand);
  673. // Vector reduction default to expand.
  674. setOperationAction(ISD::VECREDUCE_FADD, VT, Expand);
  675. setOperationAction(ISD::VECREDUCE_FMUL, VT, Expand);
  676. setOperationAction(ISD::VECREDUCE_ADD, VT, Expand);
  677. setOperationAction(ISD::VECREDUCE_MUL, VT, Expand);
  678. setOperationAction(ISD::VECREDUCE_AND, VT, Expand);
  679. setOperationAction(ISD::VECREDUCE_OR, VT, Expand);
  680. setOperationAction(ISD::VECREDUCE_XOR, VT, Expand);
  681. setOperationAction(ISD::VECREDUCE_SMAX, VT, Expand);
  682. setOperationAction(ISD::VECREDUCE_SMIN, VT, Expand);
  683. setOperationAction(ISD::VECREDUCE_UMAX, VT, Expand);
  684. setOperationAction(ISD::VECREDUCE_UMIN, VT, Expand);
  685. setOperationAction(ISD::VECREDUCE_FMAX, VT, Expand);
  686. setOperationAction(ISD::VECREDUCE_FMIN, VT, Expand);
  687. }
  688. // Most targets ignore the @llvm.prefetch intrinsic.
  689. setOperationAction(ISD::PREFETCH, MVT::Other, Expand);
  690. // Most targets also ignore the @llvm.readcyclecounter intrinsic.
  691. setOperationAction(ISD::READCYCLECOUNTER, MVT::i64, Expand);
  692. // ConstantFP nodes default to expand. Targets can either change this to
  693. // Legal, in which case all fp constants are legal, or use isFPImmLegal()
  694. // to optimize expansions for certain constants.
  695. setOperationAction(ISD::ConstantFP, MVT::f16, Expand);
  696. setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
  697. setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
  698. setOperationAction(ISD::ConstantFP, MVT::f80, Expand);
  699. setOperationAction(ISD::ConstantFP, MVT::f128, Expand);
  700. // These library functions default to expand.
  701. for (MVT VT : {MVT::f32, MVT::f64, MVT::f128}) {
  702. setOperationAction(ISD::FCBRT, VT, Expand);
  703. setOperationAction(ISD::FLOG , VT, Expand);
  704. setOperationAction(ISD::FLOG2, VT, Expand);
  705. setOperationAction(ISD::FLOG10, VT, Expand);
  706. setOperationAction(ISD::FEXP , VT, Expand);
  707. setOperationAction(ISD::FEXP2, VT, Expand);
  708. setOperationAction(ISD::FFLOOR, VT, Expand);
  709. setOperationAction(ISD::FNEARBYINT, VT, Expand);
  710. setOperationAction(ISD::FCEIL, VT, Expand);
  711. setOperationAction(ISD::FRINT, VT, Expand);
  712. setOperationAction(ISD::FTRUNC, VT, Expand);
  713. setOperationAction(ISD::FROUND, VT, Expand);
  714. setOperationAction(ISD::LROUND, VT, Expand);
  715. setOperationAction(ISD::LLROUND, VT, Expand);
  716. setOperationAction(ISD::LRINT, VT, Expand);
  717. setOperationAction(ISD::LLRINT, VT, Expand);
  718. }
  719. // Default ISD::TRAP to expand (which turns it into abort).
  720. setOperationAction(ISD::TRAP, MVT::Other, Expand);
  721. // On most systems, DEBUGTRAP and TRAP have no difference. The "Expand"
  722. // here is to inform DAG Legalizer to replace DEBUGTRAP with TRAP.
  723. setOperationAction(ISD::DEBUGTRAP, MVT::Other, Expand);
  724. }
  725. MVT TargetLoweringBase::getScalarShiftAmountTy(const DataLayout &DL,
  726. EVT) const {
  727. return MVT::getIntegerVT(DL.getPointerSizeInBits(0));
  728. }
  729. EVT TargetLoweringBase::getShiftAmountTy(EVT LHSTy, const DataLayout &DL,
  730. bool LegalTypes) const {
  731. assert(LHSTy.isInteger() && "Shift amount is not an integer type!");
  732. if (LHSTy.isVector())
  733. return LHSTy;
  734. return LegalTypes ? getScalarShiftAmountTy(DL, LHSTy)
  735. : getPointerTy(DL);
  736. }
  737. bool TargetLoweringBase::canOpTrap(unsigned Op, EVT VT) const {
  738. assert(isTypeLegal(VT));
  739. switch (Op) {
  740. default:
  741. return false;
  742. case ISD::SDIV:
  743. case ISD::UDIV:
  744. case ISD::SREM:
  745. case ISD::UREM:
  746. return true;
  747. }
  748. }
  749. void TargetLoweringBase::setJumpIsExpensive(bool isExpensive) {
  750. // If the command-line option was specified, ignore this request.
  751. if (!JumpIsExpensiveOverride.getNumOccurrences())
  752. JumpIsExpensive = isExpensive;
  753. }
  754. TargetLoweringBase::LegalizeKind
  755. TargetLoweringBase::getTypeConversion(LLVMContext &Context, EVT VT) const {
  756. // If this is a simple type, use the ComputeRegisterProp mechanism.
  757. if (VT.isSimple()) {
  758. MVT SVT = VT.getSimpleVT();
  759. assert((unsigned)SVT.SimpleTy < array_lengthof(TransformToType));
  760. MVT NVT = TransformToType[SVT.SimpleTy];
  761. LegalizeTypeAction LA = ValueTypeActions.getTypeAction(SVT);
  762. assert((LA == TypeLegal || LA == TypeSoftenFloat ||
  763. (NVT.isVector() ||
  764. ValueTypeActions.getTypeAction(NVT) != TypePromoteInteger)) &&
  765. "Promote may not follow Expand or Promote");
  766. if (LA == TypeSplitVector)
  767. return LegalizeKind(LA,
  768. EVT::getVectorVT(Context, SVT.getVectorElementType(),
  769. SVT.getVectorNumElements() / 2));
  770. if (LA == TypeScalarizeVector)
  771. return LegalizeKind(LA, SVT.getVectorElementType());
  772. return LegalizeKind(LA, NVT);
  773. }
  774. // Handle Extended Scalar Types.
  775. if (!VT.isVector()) {
  776. assert(VT.isInteger() && "Float types must be simple");
  777. unsigned BitSize = VT.getSizeInBits();
  778. // First promote to a power-of-two size, then expand if necessary.
  779. if (BitSize < 8 || !isPowerOf2_32(BitSize)) {
  780. EVT NVT = VT.getRoundIntegerType(Context);
  781. assert(NVT != VT && "Unable to round integer VT");
  782. LegalizeKind NextStep = getTypeConversion(Context, NVT);
  783. // Avoid multi-step promotion.
  784. if (NextStep.first == TypePromoteInteger)
  785. return NextStep;
  786. // Return rounded integer type.
  787. return LegalizeKind(TypePromoteInteger, NVT);
  788. }
  789. return LegalizeKind(TypeExpandInteger,
  790. EVT::getIntegerVT(Context, VT.getSizeInBits() / 2));
  791. }
  792. // Handle vector types.
  793. unsigned NumElts = VT.getVectorNumElements();
  794. EVT EltVT = VT.getVectorElementType();
  795. // Vectors with only one element are always scalarized.
  796. if (NumElts == 1)
  797. return LegalizeKind(TypeScalarizeVector, EltVT);
  798. // Try to widen vector elements until the element type is a power of two and
  799. // promote it to a legal type later on, for example:
  800. // <3 x i8> -> <4 x i8> -> <4 x i32>
  801. if (EltVT.isInteger()) {
  802. // Vectors with a number of elements that is not a power of two are always
  803. // widened, for example <3 x i8> -> <4 x i8>.
  804. if (!VT.isPow2VectorType()) {
  805. NumElts = (unsigned)NextPowerOf2(NumElts);
  806. EVT NVT = EVT::getVectorVT(Context, EltVT, NumElts);
  807. return LegalizeKind(TypeWidenVector, NVT);
  808. }
  809. // Examine the element type.
  810. LegalizeKind LK = getTypeConversion(Context, EltVT);
  811. // If type is to be expanded, split the vector.
  812. // <4 x i140> -> <2 x i140>
  813. if (LK.first == TypeExpandInteger)
  814. return LegalizeKind(TypeSplitVector,
  815. EVT::getVectorVT(Context, EltVT, NumElts / 2));
  816. // Promote the integer element types until a legal vector type is found
  817. // or until the element integer type is too big. If a legal type was not
  818. // found, fallback to the usual mechanism of widening/splitting the
  819. // vector.
  820. EVT OldEltVT = EltVT;
  821. while (true) {
  822. // Increase the bitwidth of the element to the next pow-of-two
  823. // (which is greater than 8 bits).
  824. EltVT = EVT::getIntegerVT(Context, 1 + EltVT.getSizeInBits())
  825. .getRoundIntegerType(Context);
  826. // Stop trying when getting a non-simple element type.
  827. // Note that vector elements may be greater than legal vector element
  828. // types. Example: X86 XMM registers hold 64bit element on 32bit
  829. // systems.
  830. if (!EltVT.isSimple())
  831. break;
  832. // Build a new vector type and check if it is legal.
  833. MVT NVT = MVT::getVectorVT(EltVT.getSimpleVT(), NumElts);
  834. // Found a legal promoted vector type.
  835. if (NVT != MVT() && ValueTypeActions.getTypeAction(NVT) == TypeLegal)
  836. return LegalizeKind(TypePromoteInteger,
  837. EVT::getVectorVT(Context, EltVT, NumElts));
  838. }
  839. // Reset the type to the unexpanded type if we did not find a legal vector
  840. // type with a promoted vector element type.
  841. EltVT = OldEltVT;
  842. }
  843. // Try to widen the vector until a legal type is found.
  844. // If there is no wider legal type, split the vector.
  845. while (true) {
  846. // Round up to the next power of 2.
  847. NumElts = (unsigned)NextPowerOf2(NumElts);
  848. // If there is no simple vector type with this many elements then there
  849. // cannot be a larger legal vector type. Note that this assumes that
  850. // there are no skipped intermediate vector types in the simple types.
  851. if (!EltVT.isSimple())
  852. break;
  853. MVT LargerVector = MVT::getVectorVT(EltVT.getSimpleVT(), NumElts);
  854. if (LargerVector == MVT())
  855. break;
  856. // If this type is legal then widen the vector.
  857. if (ValueTypeActions.getTypeAction(LargerVector) == TypeLegal)
  858. return LegalizeKind(TypeWidenVector, LargerVector);
  859. }
  860. // Widen odd vectors to next power of two.
  861. if (!VT.isPow2VectorType()) {
  862. EVT NVT = VT.getPow2VectorType(Context);
  863. return LegalizeKind(TypeWidenVector, NVT);
  864. }
  865. // Vectors with illegal element types are expanded.
  866. EVT NVT = EVT::getVectorVT(Context, EltVT, VT.getVectorNumElements() / 2);
  867. return LegalizeKind(TypeSplitVector, NVT);
  868. }
  869. static unsigned getVectorTypeBreakdownMVT(MVT VT, MVT &IntermediateVT,
  870. unsigned &NumIntermediates,
  871. MVT &RegisterVT,
  872. TargetLoweringBase *TLI) {
  873. // Figure out the right, legal destination reg to copy into.
  874. unsigned NumElts = VT.getVectorNumElements();
  875. MVT EltTy = VT.getVectorElementType();
  876. unsigned NumVectorRegs = 1;
  877. // FIXME: We don't support non-power-of-2-sized vectors for now. Ideally we
  878. // could break down into LHS/RHS like LegalizeDAG does.
  879. if (!isPowerOf2_32(NumElts)) {
  880. NumVectorRegs = NumElts;
  881. NumElts = 1;
  882. }
  883. // Divide the input until we get to a supported size. This will always
  884. // end with a scalar if the target doesn't support vectors.
  885. while (NumElts > 1 && !TLI->isTypeLegal(MVT::getVectorVT(EltTy, NumElts))) {
  886. NumElts >>= 1;
  887. NumVectorRegs <<= 1;
  888. }
  889. NumIntermediates = NumVectorRegs;
  890. MVT NewVT = MVT::getVectorVT(EltTy, NumElts);
  891. if (!TLI->isTypeLegal(NewVT))
  892. NewVT = EltTy;
  893. IntermediateVT = NewVT;
  894. unsigned NewVTSize = NewVT.getSizeInBits();
  895. // Convert sizes such as i33 to i64.
  896. if (!isPowerOf2_32(NewVTSize))
  897. NewVTSize = NextPowerOf2(NewVTSize);
  898. MVT DestVT = TLI->getRegisterType(NewVT);
  899. RegisterVT = DestVT;
  900. if (EVT(DestVT).bitsLT(NewVT)) // Value is expanded, e.g. i64 -> i16.
  901. return NumVectorRegs*(NewVTSize/DestVT.getSizeInBits());
  902. // Otherwise, promotion or legal types use the same number of registers as
  903. // the vector decimated to the appropriate level.
  904. return NumVectorRegs;
  905. }
  906. /// isLegalRC - Return true if the value types that can be represented by the
  907. /// specified register class are all legal.
  908. bool TargetLoweringBase::isLegalRC(const TargetRegisterInfo &TRI,
  909. const TargetRegisterClass &RC) const {
  910. for (auto I = TRI.legalclasstypes_begin(RC); *I != MVT::Other; ++I)
  911. if (isTypeLegal(*I))
  912. return true;
  913. return false;
  914. }
  915. /// Replace/modify any TargetFrameIndex operands with a targte-dependent
  916. /// sequence of memory operands that is recognized by PrologEpilogInserter.
  917. MachineBasicBlock *
  918. TargetLoweringBase::emitPatchPoint(MachineInstr &InitialMI,
  919. MachineBasicBlock *MBB) const {
  920. MachineInstr *MI = &InitialMI;
  921. MachineFunction &MF = *MI->getMF();
  922. MachineFrameInfo &MFI = MF.getFrameInfo();
  923. // We're handling multiple types of operands here:
  924. // PATCHPOINT MetaArgs - live-in, read only, direct
  925. // STATEPOINT Deopt Spill - live-through, read only, indirect
  926. // STATEPOINT Deopt Alloca - live-through, read only, direct
  927. // (We're currently conservative and mark the deopt slots read/write in
  928. // practice.)
  929. // STATEPOINT GC Spill - live-through, read/write, indirect
  930. // STATEPOINT GC Alloca - live-through, read/write, direct
  931. // The live-in vs live-through is handled already (the live through ones are
  932. // all stack slots), but we need to handle the different type of stackmap
  933. // operands and memory effects here.
  934. // MI changes inside this loop as we grow operands.
  935. for(unsigned OperIdx = 0; OperIdx != MI->getNumOperands(); ++OperIdx) {
  936. MachineOperand &MO = MI->getOperand(OperIdx);
  937. if (!MO.isFI())
  938. continue;
  939. // foldMemoryOperand builds a new MI after replacing a single FI operand
  940. // with the canonical set of five x86 addressing-mode operands.
  941. int FI = MO.getIndex();
  942. MachineInstrBuilder MIB = BuildMI(MF, MI->getDebugLoc(), MI->getDesc());
  943. // Copy operands before the frame-index.
  944. for (unsigned i = 0; i < OperIdx; ++i)
  945. MIB.add(MI->getOperand(i));
  946. // Add frame index operands recognized by stackmaps.cpp
  947. if (MFI.isStatepointSpillSlotObjectIndex(FI)) {
  948. // indirect-mem-ref tag, size, #FI, offset.
  949. // Used for spills inserted by StatepointLowering. This codepath is not
  950. // used for patchpoints/stackmaps at all, for these spilling is done via
  951. // foldMemoryOperand callback only.
  952. assert(MI->getOpcode() == TargetOpcode::STATEPOINT && "sanity");
  953. MIB.addImm(StackMaps::IndirectMemRefOp);
  954. MIB.addImm(MFI.getObjectSize(FI));
  955. MIB.add(MI->getOperand(OperIdx));
  956. MIB.addImm(0);
  957. } else {
  958. // direct-mem-ref tag, #FI, offset.
  959. // Used by patchpoint, and direct alloca arguments to statepoints
  960. MIB.addImm(StackMaps::DirectMemRefOp);
  961. MIB.add(MI->getOperand(OperIdx));
  962. MIB.addImm(0);
  963. }
  964. // Copy the operands after the frame index.
  965. for (unsigned i = OperIdx + 1; i != MI->getNumOperands(); ++i)
  966. MIB.add(MI->getOperand(i));
  967. // Inherit previous memory operands.
  968. MIB.cloneMemRefs(*MI);
  969. assert(MIB->mayLoad() && "Folded a stackmap use to a non-load!");
  970. // Add a new memory operand for this FI.
  971. assert(MFI.getObjectOffset(FI) != -1);
  972. // Note: STATEPOINT MMOs are added during SelectionDAG. STACKMAP, and
  973. // PATCHPOINT should be updated to do the same. (TODO)
  974. if (MI->getOpcode() != TargetOpcode::STATEPOINT) {
  975. auto Flags = MachineMemOperand::MOLoad;
  976. MachineMemOperand *MMO = MF.getMachineMemOperand(
  977. MachinePointerInfo::getFixedStack(MF, FI), Flags,
  978. MF.getDataLayout().getPointerSize(), MFI.getObjectAlignment(FI));
  979. MIB->addMemOperand(MF, MMO);
  980. }
  981. // Replace the instruction and update the operand index.
  982. MBB->insert(MachineBasicBlock::iterator(MI), MIB);
  983. OperIdx += (MIB->getNumOperands() - MI->getNumOperands()) - 1;
  984. MI->eraseFromParent();
  985. MI = MIB;
  986. }
  987. return MBB;
  988. }
  989. MachineBasicBlock *
  990. TargetLoweringBase::emitXRayCustomEvent(MachineInstr &MI,
  991. MachineBasicBlock *MBB) const {
  992. assert(MI.getOpcode() == TargetOpcode::PATCHABLE_EVENT_CALL &&
  993. "Called emitXRayCustomEvent on the wrong MI!");
  994. auto &MF = *MI.getMF();
  995. auto MIB = BuildMI(MF, MI.getDebugLoc(), MI.getDesc());
  996. for (unsigned OpIdx = 0; OpIdx != MI.getNumOperands(); ++OpIdx)
  997. MIB.add(MI.getOperand(OpIdx));
  998. MBB->insert(MachineBasicBlock::iterator(MI), MIB);
  999. MI.eraseFromParent();
  1000. return MBB;
  1001. }
  1002. MachineBasicBlock *
  1003. TargetLoweringBase::emitXRayTypedEvent(MachineInstr &MI,
  1004. MachineBasicBlock *MBB) const {
  1005. assert(MI.getOpcode() == TargetOpcode::PATCHABLE_TYPED_EVENT_CALL &&
  1006. "Called emitXRayTypedEvent on the wrong MI!");
  1007. auto &MF = *MI.getMF();
  1008. auto MIB = BuildMI(MF, MI.getDebugLoc(), MI.getDesc());
  1009. for (unsigned OpIdx = 0; OpIdx != MI.getNumOperands(); ++OpIdx)
  1010. MIB.add(MI.getOperand(OpIdx));
  1011. MBB->insert(MachineBasicBlock::iterator(MI), MIB);
  1012. MI.eraseFromParent();
  1013. return MBB;
  1014. }
  1015. /// findRepresentativeClass - Return the largest legal super-reg register class
  1016. /// of the register class for the specified type and its associated "cost".
  1017. // This function is in TargetLowering because it uses RegClassForVT which would
  1018. // need to be moved to TargetRegisterInfo and would necessitate moving
  1019. // isTypeLegal over as well - a massive change that would just require
  1020. // TargetLowering having a TargetRegisterInfo class member that it would use.
  1021. std::pair<const TargetRegisterClass *, uint8_t>
  1022. TargetLoweringBase::findRepresentativeClass(const TargetRegisterInfo *TRI,
  1023. MVT VT) const {
  1024. const TargetRegisterClass *RC = RegClassForVT[VT.SimpleTy];
  1025. if (!RC)
  1026. return std::make_pair(RC, 0);
  1027. // Compute the set of all super-register classes.
  1028. BitVector SuperRegRC(TRI->getNumRegClasses());
  1029. for (SuperRegClassIterator RCI(RC, TRI); RCI.isValid(); ++RCI)
  1030. SuperRegRC.setBitsInMask(RCI.getMask());
  1031. // Find the first legal register class with the largest spill size.
  1032. const TargetRegisterClass *BestRC = RC;
  1033. for (unsigned i : SuperRegRC.set_bits()) {
  1034. const TargetRegisterClass *SuperRC = TRI->getRegClass(i);
  1035. // We want the largest possible spill size.
  1036. if (TRI->getSpillSize(*SuperRC) <= TRI->getSpillSize(*BestRC))
  1037. continue;
  1038. if (!isLegalRC(*TRI, *SuperRC))
  1039. continue;
  1040. BestRC = SuperRC;
  1041. }
  1042. return std::make_pair(BestRC, 1);
  1043. }
  1044. /// computeRegisterProperties - Once all of the register classes are added,
  1045. /// this allows us to compute derived properties we expose.
  1046. void TargetLoweringBase::computeRegisterProperties(
  1047. const TargetRegisterInfo *TRI) {
  1048. static_assert(MVT::LAST_VALUETYPE <= MVT::MAX_ALLOWED_VALUETYPE,
  1049. "Too many value types for ValueTypeActions to hold!");
  1050. // Everything defaults to needing one register.
  1051. for (unsigned i = 0; i != MVT::LAST_VALUETYPE; ++i) {
  1052. NumRegistersForVT[i] = 1;
  1053. RegisterTypeForVT[i] = TransformToType[i] = (MVT::SimpleValueType)i;
  1054. }
  1055. // ...except isVoid, which doesn't need any registers.
  1056. NumRegistersForVT[MVT::isVoid] = 0;
  1057. // Find the largest integer register class.
  1058. unsigned LargestIntReg = MVT::LAST_INTEGER_VALUETYPE;
  1059. for (; RegClassForVT[LargestIntReg] == nullptr; --LargestIntReg)
  1060. assert(LargestIntReg != MVT::i1 && "No integer registers defined!");
  1061. // Every integer value type larger than this largest register takes twice as
  1062. // many registers to represent as the previous ValueType.
  1063. for (unsigned ExpandedReg = LargestIntReg + 1;
  1064. ExpandedReg <= MVT::LAST_INTEGER_VALUETYPE; ++ExpandedReg) {
  1065. NumRegistersForVT[ExpandedReg] = 2*NumRegistersForVT[ExpandedReg-1];
  1066. RegisterTypeForVT[ExpandedReg] = (MVT::SimpleValueType)LargestIntReg;
  1067. TransformToType[ExpandedReg] = (MVT::SimpleValueType)(ExpandedReg - 1);
  1068. ValueTypeActions.setTypeAction((MVT::SimpleValueType)ExpandedReg,
  1069. TypeExpandInteger);
  1070. }
  1071. // Inspect all of the ValueType's smaller than the largest integer
  1072. // register to see which ones need promotion.
  1073. unsigned LegalIntReg = LargestIntReg;
  1074. for (unsigned IntReg = LargestIntReg - 1;
  1075. IntReg >= (unsigned)MVT::i1; --IntReg) {
  1076. MVT IVT = (MVT::SimpleValueType)IntReg;
  1077. if (isTypeLegal(IVT)) {
  1078. LegalIntReg = IntReg;
  1079. } else {
  1080. RegisterTypeForVT[IntReg] = TransformToType[IntReg] =
  1081. (MVT::SimpleValueType)LegalIntReg;
  1082. ValueTypeActions.setTypeAction(IVT, TypePromoteInteger);
  1083. }
  1084. }
  1085. // ppcf128 type is really two f64's.
  1086. if (!isTypeLegal(MVT::ppcf128)) {
  1087. if (isTypeLegal(MVT::f64)) {
  1088. NumRegistersForVT[MVT::ppcf128] = 2*NumRegistersForVT[MVT::f64];
  1089. RegisterTypeForVT[MVT::ppcf128] = MVT::f64;
  1090. TransformToType[MVT::ppcf128] = MVT::f64;
  1091. ValueTypeActions.setTypeAction(MVT::ppcf128, TypeExpandFloat);
  1092. } else {
  1093. NumRegistersForVT[MVT::ppcf128] = NumRegistersForVT[MVT::i128];
  1094. RegisterTypeForVT[MVT::ppcf128] = RegisterTypeForVT[MVT::i128];
  1095. TransformToType[MVT::ppcf128] = MVT::i128;
  1096. ValueTypeActions.setTypeAction(MVT::ppcf128, TypeSoftenFloat);
  1097. }
  1098. }
  1099. // Decide how to handle f128. If the target does not have native f128 support,
  1100. // expand it to i128 and we will be generating soft float library calls.
  1101. if (!isTypeLegal(MVT::f128)) {
  1102. NumRegistersForVT[MVT::f128] = NumRegistersForVT[MVT::i128];
  1103. RegisterTypeForVT[MVT::f128] = RegisterTypeForVT[MVT::i128];
  1104. TransformToType[MVT::f128] = MVT::i128;
  1105. ValueTypeActions.setTypeAction(MVT::f128, TypeSoftenFloat);
  1106. }
  1107. // Decide how to handle f64. If the target does not have native f64 support,
  1108. // expand it to i64 and we will be generating soft float library calls.
  1109. if (!isTypeLegal(MVT::f64)) {
  1110. NumRegistersForVT[MVT::f64] = NumRegistersForVT[MVT::i64];
  1111. RegisterTypeForVT[MVT::f64] = RegisterTypeForVT[MVT::i64];
  1112. TransformToType[MVT::f64] = MVT::i64;
  1113. ValueTypeActions.setTypeAction(MVT::f64, TypeSoftenFloat);
  1114. }
  1115. // Decide how to handle f32. If the target does not have native f32 support,
  1116. // expand it to i32 and we will be generating soft float library calls.
  1117. if (!isTypeLegal(MVT::f32)) {
  1118. NumRegistersForVT[MVT::f32] = NumRegistersForVT[MVT::i32];
  1119. RegisterTypeForVT[MVT::f32] = RegisterTypeForVT[MVT::i32];
  1120. TransformToType[MVT::f32] = MVT::i32;
  1121. ValueTypeActions.setTypeAction(MVT::f32, TypeSoftenFloat);
  1122. }
  1123. // Decide how to handle f16. If the target does not have native f16 support,
  1124. // promote it to f32, because there are no f16 library calls (except for
  1125. // conversions).
  1126. if (!isTypeLegal(MVT::f16)) {
  1127. NumRegistersForVT[MVT::f16] = NumRegistersForVT[MVT::f32];
  1128. RegisterTypeForVT[MVT::f16] = RegisterTypeForVT[MVT::f32];
  1129. TransformToType[MVT::f16] = MVT::f32;
  1130. ValueTypeActions.setTypeAction(MVT::f16, TypePromoteFloat);
  1131. }
  1132. // Loop over all of the vector value types to see which need transformations.
  1133. for (unsigned i = MVT::FIRST_VECTOR_VALUETYPE;
  1134. i <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++i) {
  1135. MVT VT = (MVT::SimpleValueType) i;
  1136. if (isTypeLegal(VT))
  1137. continue;
  1138. MVT EltVT = VT.getVectorElementType();
  1139. unsigned NElts = VT.getVectorNumElements();
  1140. bool IsLegalWiderType = false;
  1141. LegalizeTypeAction PreferredAction = getPreferredVectorAction(VT);
  1142. switch (PreferredAction) {
  1143. case TypePromoteInteger:
  1144. // Try to promote the elements of integer vectors. If no legal
  1145. // promotion was found, fall through to the widen-vector method.
  1146. for (unsigned nVT = i + 1; nVT <= MVT::LAST_INTEGER_VECTOR_VALUETYPE; ++nVT) {
  1147. MVT SVT = (MVT::SimpleValueType) nVT;
  1148. // Promote vectors of integers to vectors with the same number
  1149. // of elements, with a wider element type.
  1150. if (SVT.getScalarSizeInBits() > EltVT.getSizeInBits() &&
  1151. SVT.getVectorNumElements() == NElts && isTypeLegal(SVT)) {
  1152. TransformToType[i] = SVT;
  1153. RegisterTypeForVT[i] = SVT;
  1154. NumRegistersForVT[i] = 1;
  1155. ValueTypeActions.setTypeAction(VT, TypePromoteInteger);
  1156. IsLegalWiderType = true;
  1157. break;
  1158. }
  1159. }
  1160. if (IsLegalWiderType)
  1161. break;
  1162. LLVM_FALLTHROUGH;
  1163. case TypeWidenVector:
  1164. if (isPowerOf2_32(NElts)) {
  1165. // Try to widen the vector.
  1166. for (unsigned nVT = i + 1; nVT <= MVT::LAST_VECTOR_VALUETYPE; ++nVT) {
  1167. MVT SVT = (MVT::SimpleValueType) nVT;
  1168. if (SVT.getVectorElementType() == EltVT
  1169. && SVT.getVectorNumElements() > NElts && isTypeLegal(SVT)) {
  1170. TransformToType[i] = SVT;
  1171. RegisterTypeForVT[i] = SVT;
  1172. NumRegistersForVT[i] = 1;
  1173. ValueTypeActions.setTypeAction(VT, TypeWidenVector);
  1174. IsLegalWiderType = true;
  1175. break;
  1176. }
  1177. }
  1178. if (IsLegalWiderType)
  1179. break;
  1180. } else {
  1181. // Only widen to the next power of 2 to keep consistency with EVT.
  1182. MVT NVT = VT.getPow2VectorType();
  1183. if (isTypeLegal(NVT)) {
  1184. TransformToType[i] = NVT;
  1185. ValueTypeActions.setTypeAction(VT, TypeWidenVector);
  1186. RegisterTypeForVT[i] = NVT;
  1187. NumRegistersForVT[i] = 1;
  1188. break;
  1189. }
  1190. }
  1191. LLVM_FALLTHROUGH;
  1192. case TypeSplitVector:
  1193. case TypeScalarizeVector: {
  1194. MVT IntermediateVT;
  1195. MVT RegisterVT;
  1196. unsigned NumIntermediates;
  1197. NumRegistersForVT[i] = getVectorTypeBreakdownMVT(VT, IntermediateVT,
  1198. NumIntermediates, RegisterVT, this);
  1199. RegisterTypeForVT[i] = RegisterVT;
  1200. MVT NVT = VT.getPow2VectorType();
  1201. if (NVT == VT) {
  1202. // Type is already a power of 2. The default action is to split.
  1203. TransformToType[i] = MVT::Other;
  1204. if (PreferredAction == TypeScalarizeVector)
  1205. ValueTypeActions.setTypeAction(VT, TypeScalarizeVector);
  1206. else if (PreferredAction == TypeSplitVector)
  1207. ValueTypeActions.setTypeAction(VT, TypeSplitVector);
  1208. else
  1209. // Set type action according to the number of elements.
  1210. ValueTypeActions.setTypeAction(VT, NElts == 1 ? TypeScalarizeVector
  1211. : TypeSplitVector);
  1212. } else {
  1213. TransformToType[i] = NVT;
  1214. ValueTypeActions.setTypeAction(VT, TypeWidenVector);
  1215. }
  1216. break;
  1217. }
  1218. default:
  1219. llvm_unreachable("Unknown vector legalization action!");
  1220. }
  1221. }
  1222. // Determine the 'representative' register class for each value type.
  1223. // An representative register class is the largest (meaning one which is
  1224. // not a sub-register class / subreg register class) legal register class for
  1225. // a group of value types. For example, on i386, i8, i16, and i32
  1226. // representative would be GR32; while on x86_64 it's GR64.
  1227. for (unsigned i = 0; i != MVT::LAST_VALUETYPE; ++i) {
  1228. const TargetRegisterClass* RRC;
  1229. uint8_t Cost;
  1230. std::tie(RRC, Cost) = findRepresentativeClass(TRI, (MVT::SimpleValueType)i);
  1231. RepRegClassForVT[i] = RRC;
  1232. RepRegClassCostForVT[i] = Cost;
  1233. }
  1234. }
  1235. EVT TargetLoweringBase::getSetCCResultType(const DataLayout &DL, LLVMContext &,
  1236. EVT VT) const {
  1237. assert(!VT.isVector() && "No default SetCC type for vectors!");
  1238. return getPointerTy(DL).SimpleTy;
  1239. }
  1240. MVT::SimpleValueType TargetLoweringBase::getCmpLibcallReturnType() const {
  1241. return MVT::i32; // return the default value
  1242. }
  1243. /// getVectorTypeBreakdown - Vector types are broken down into some number of
  1244. /// legal first class types. For example, MVT::v8f32 maps to 2 MVT::v4f32
  1245. /// with Altivec or SSE1, or 8 promoted MVT::f64 values with the X86 FP stack.
  1246. /// Similarly, MVT::v2i64 turns into 4 MVT::i32 values with both PPC and X86.
  1247. ///
  1248. /// This method returns the number of registers needed, and the VT for each
  1249. /// register. It also returns the VT and quantity of the intermediate values
  1250. /// before they are promoted/expanded.
  1251. unsigned TargetLoweringBase::getVectorTypeBreakdown(LLVMContext &Context, EVT VT,
  1252. EVT &IntermediateVT,
  1253. unsigned &NumIntermediates,
  1254. MVT &RegisterVT) const {
  1255. unsigned NumElts = VT.getVectorNumElements();
  1256. // If there is a wider vector type with the same element type as this one,
  1257. // or a promoted vector type that has the same number of elements which
  1258. // are wider, then we should convert to that legal vector type.
  1259. // This handles things like <2 x float> -> <4 x float> and
  1260. // <4 x i1> -> <4 x i32>.
  1261. LegalizeTypeAction TA = getTypeAction(Context, VT);
  1262. if (NumElts != 1 && (TA == TypeWidenVector || TA == TypePromoteInteger)) {
  1263. EVT RegisterEVT = getTypeToTransformTo(Context, VT);
  1264. if (isTypeLegal(RegisterEVT)) {
  1265. IntermediateVT = RegisterEVT;
  1266. RegisterVT = RegisterEVT.getSimpleVT();
  1267. NumIntermediates = 1;
  1268. return 1;
  1269. }
  1270. }
  1271. // Figure out the right, legal destination reg to copy into.
  1272. EVT EltTy = VT.getVectorElementType();
  1273. unsigned NumVectorRegs = 1;
  1274. // FIXME: We don't support non-power-of-2-sized vectors for now. Ideally we
  1275. // could break down into LHS/RHS like LegalizeDAG does.
  1276. if (!isPowerOf2_32(NumElts)) {
  1277. NumVectorRegs = NumElts;
  1278. NumElts = 1;
  1279. }
  1280. // Divide the input until we get to a supported size. This will always
  1281. // end with a scalar if the target doesn't support vectors.
  1282. while (NumElts > 1 && !isTypeLegal(
  1283. EVT::getVectorVT(Context, EltTy, NumElts))) {
  1284. NumElts >>= 1;
  1285. NumVectorRegs <<= 1;
  1286. }
  1287. NumIntermediates = NumVectorRegs;
  1288. EVT NewVT = EVT::getVectorVT(Context, EltTy, NumElts);
  1289. if (!isTypeLegal(NewVT))
  1290. NewVT = EltTy;
  1291. IntermediateVT = NewVT;
  1292. MVT DestVT = getRegisterType(Context, NewVT);
  1293. RegisterVT = DestVT;
  1294. unsigned NewVTSize = NewVT.getSizeInBits();
  1295. // Convert sizes such as i33 to i64.
  1296. if (!isPowerOf2_32(NewVTSize))
  1297. NewVTSize = NextPowerOf2(NewVTSize);
  1298. if (EVT(DestVT).bitsLT(NewVT)) // Value is expanded, e.g. i64 -> i16.
  1299. return NumVectorRegs*(NewVTSize/DestVT.getSizeInBits());
  1300. // Otherwise, promotion or legal types use the same number of registers as
  1301. // the vector decimated to the appropriate level.
  1302. return NumVectorRegs;
  1303. }
  1304. /// Get the EVTs and ArgFlags collections that represent the legalized return
  1305. /// type of the given function. This does not require a DAG or a return value,
  1306. /// and is suitable for use before any DAGs for the function are constructed.
  1307. /// TODO: Move this out of TargetLowering.cpp.
  1308. void llvm::GetReturnInfo(CallingConv::ID CC, Type *ReturnType,
  1309. AttributeList attr,
  1310. SmallVectorImpl<ISD::OutputArg> &Outs,
  1311. const TargetLowering &TLI, const DataLayout &DL) {
  1312. SmallVector<EVT, 4> ValueVTs;
  1313. ComputeValueVTs(TLI, DL, ReturnType, ValueVTs);
  1314. unsigned NumValues = ValueVTs.size();
  1315. if (NumValues == 0) return;
  1316. for (unsigned j = 0, f = NumValues; j != f; ++j) {
  1317. EVT VT = ValueVTs[j];
  1318. ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
  1319. if (attr.hasAttribute(AttributeList::ReturnIndex, Attribute::SExt))
  1320. ExtendKind = ISD::SIGN_EXTEND;
  1321. else if (attr.hasAttribute(AttributeList::ReturnIndex, Attribute::ZExt))
  1322. ExtendKind = ISD::ZERO_EXTEND;
  1323. // FIXME: C calling convention requires the return type to be promoted to
  1324. // at least 32-bit. But this is not necessary for non-C calling
  1325. // conventions. The frontend should mark functions whose return values
  1326. // require promoting with signext or zeroext attributes.
  1327. if (ExtendKind != ISD::ANY_EXTEND && VT.isInteger()) {
  1328. MVT MinVT = TLI.getRegisterType(ReturnType->getContext(), MVT::i32);
  1329. if (VT.bitsLT(MinVT))
  1330. VT = MinVT;
  1331. }
  1332. unsigned NumParts =
  1333. TLI.getNumRegistersForCallingConv(ReturnType->getContext(), CC, VT);
  1334. MVT PartVT =
  1335. TLI.getRegisterTypeForCallingConv(ReturnType->getContext(), CC, VT);
  1336. // 'inreg' on function refers to return value
  1337. ISD::ArgFlagsTy Flags = ISD::ArgFlagsTy();
  1338. if (attr.hasAttribute(AttributeList::ReturnIndex, Attribute::InReg))
  1339. Flags.setInReg();
  1340. // Propagate extension type if any
  1341. if (attr.hasAttribute(AttributeList::ReturnIndex, Attribute::SExt))
  1342. Flags.setSExt();
  1343. else if (attr.hasAttribute(AttributeList::ReturnIndex, Attribute::ZExt))
  1344. Flags.setZExt();
  1345. for (unsigned i = 0; i < NumParts; ++i)
  1346. Outs.push_back(ISD::OutputArg(Flags, PartVT, VT, /*isfixed=*/true, 0, 0));
  1347. }
  1348. }
  1349. /// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
  1350. /// function arguments in the caller parameter area. This is the actual
  1351. /// alignment, not its logarithm.
  1352. unsigned TargetLoweringBase::getByValTypeAlignment(Type *Ty,
  1353. const DataLayout &DL) const {
  1354. return DL.getABITypeAlignment(Ty);
  1355. }
  1356. bool TargetLoweringBase::allowsMemoryAccess(LLVMContext &Context,
  1357. const DataLayout &DL, EVT VT,
  1358. unsigned AddrSpace,
  1359. unsigned Alignment,
  1360. MachineMemOperand::Flags Flags,
  1361. bool *Fast) const {
  1362. // Check if the specified alignment is sufficient based on the data layout.
  1363. // TODO: While using the data layout works in practice, a better solution
  1364. // would be to implement this check directly (make this a virtual function).
  1365. // For example, the ABI alignment may change based on software platform while
  1366. // this function should only be affected by hardware implementation.
  1367. Type *Ty = VT.getTypeForEVT(Context);
  1368. if (Alignment >= DL.getABITypeAlignment(Ty)) {
  1369. // Assume that an access that meets the ABI-specified alignment is fast.
  1370. if (Fast != nullptr)
  1371. *Fast = true;
  1372. return true;
  1373. }
  1374. // This is a misaligned access.
  1375. return allowsMisalignedMemoryAccesses(VT, AddrSpace, Alignment, Flags, Fast);
  1376. }
  1377. bool TargetLoweringBase::allowsMemoryAccess(LLVMContext &Context,
  1378. const DataLayout &DL, EVT VT,
  1379. const MachineMemOperand &MMO,
  1380. bool *Fast) const {
  1381. return allowsMemoryAccess(Context, DL, VT, MMO.getAddrSpace(),
  1382. MMO.getAlignment(), MMO.getFlags(), Fast);
  1383. }
  1384. BranchProbability TargetLoweringBase::getPredictableBranchThreshold() const {
  1385. return BranchProbability(MinPercentageForPredictableBranch, 100);
  1386. }
  1387. //===----------------------------------------------------------------------===//
  1388. // TargetTransformInfo Helpers
  1389. //===----------------------------------------------------------------------===//
  1390. int TargetLoweringBase::InstructionOpcodeToISD(unsigned Opcode) const {
  1391. enum InstructionOpcodes {
  1392. #define HANDLE_INST(NUM, OPCODE, CLASS) OPCODE = NUM,
  1393. #define LAST_OTHER_INST(NUM) InstructionOpcodesCount = NUM
  1394. #include "llvm/IR/Instruction.def"
  1395. };
  1396. switch (static_cast<InstructionOpcodes>(Opcode)) {
  1397. case Ret: return 0;
  1398. case Br: return 0;
  1399. case Switch: return 0;
  1400. case IndirectBr: return 0;
  1401. case Invoke: return 0;
  1402. case CallBr: return 0;
  1403. case Resume: return 0;
  1404. case Unreachable: return 0;
  1405. case CleanupRet: return 0;
  1406. case CatchRet: return 0;
  1407. case CatchPad: return 0;
  1408. case CatchSwitch: return 0;
  1409. case CleanupPad: return 0;
  1410. case FNeg: return ISD::FNEG;
  1411. case Add: return ISD::ADD;
  1412. case FAdd: return ISD::FADD;
  1413. case Sub: return ISD::SUB;
  1414. case FSub: return ISD::FSUB;
  1415. case Mul: return ISD::MUL;
  1416. case FMul: return ISD::FMUL;
  1417. case UDiv: return ISD::UDIV;
  1418. case SDiv: return ISD::SDIV;
  1419. case FDiv: return ISD::FDIV;
  1420. case URem: return ISD::UREM;
  1421. case SRem: return ISD::SREM;
  1422. case FRem: return ISD::FREM;
  1423. case Shl: return ISD::SHL;
  1424. case LShr: return ISD::SRL;
  1425. case AShr: return ISD::SRA;
  1426. case And: return ISD::AND;
  1427. case Or: return ISD::OR;
  1428. case Xor: return ISD::XOR;
  1429. case Alloca: return 0;
  1430. case Load: return ISD::LOAD;
  1431. case Store: return ISD::STORE;
  1432. case GetElementPtr: return 0;
  1433. case Fence: return 0;
  1434. case AtomicCmpXchg: return 0;
  1435. case AtomicRMW: return 0;
  1436. case Trunc: return ISD::TRUNCATE;
  1437. case ZExt: return ISD::ZERO_EXTEND;
  1438. case SExt: return ISD::SIGN_EXTEND;
  1439. case FPToUI: return ISD::FP_TO_UINT;
  1440. case FPToSI: return ISD::FP_TO_SINT;
  1441. case UIToFP: return ISD::UINT_TO_FP;
  1442. case SIToFP: return ISD::SINT_TO_FP;
  1443. case FPTrunc: return ISD::FP_ROUND;
  1444. case FPExt: return ISD::FP_EXTEND;
  1445. case PtrToInt: return ISD::BITCAST;
  1446. case IntToPtr: return ISD::BITCAST;
  1447. case BitCast: return ISD::BITCAST;
  1448. case AddrSpaceCast: return ISD::ADDRSPACECAST;
  1449. case ICmp: return ISD::SETCC;
  1450. case FCmp: return ISD::SETCC;
  1451. case PHI: return 0;
  1452. case Call: return 0;
  1453. case Select: return ISD::SELECT;
  1454. case UserOp1: return 0;
  1455. case UserOp2: return 0;
  1456. case VAArg: return 0;
  1457. case ExtractElement: return ISD::EXTRACT_VECTOR_ELT;
  1458. case InsertElement: return ISD::INSERT_VECTOR_ELT;
  1459. case ShuffleVector: return ISD::VECTOR_SHUFFLE;
  1460. case ExtractValue: return ISD::MERGE_VALUES;
  1461. case InsertValue: return ISD::MERGE_VALUES;
  1462. case LandingPad: return 0;
  1463. }
  1464. llvm_unreachable("Unknown instruction type encountered!");
  1465. }
  1466. std::pair<int, MVT>
  1467. TargetLoweringBase::getTypeLegalizationCost(const DataLayout &DL,
  1468. Type *Ty) const {
  1469. LLVMContext &C = Ty->getContext();
  1470. EVT MTy = getValueType(DL, Ty);
  1471. int Cost = 1;
  1472. // We keep legalizing the type until we find a legal kind. We assume that
  1473. // the only operation that costs anything is the split. After splitting
  1474. // we need to handle two types.
  1475. while (true) {
  1476. LegalizeKind LK = getTypeConversion(C, MTy);
  1477. if (LK.first == TypeLegal)
  1478. return std::make_pair(Cost, MTy.getSimpleVT());
  1479. if (LK.first == TypeSplitVector || LK.first == TypeExpandInteger)
  1480. Cost *= 2;
  1481. // Do not loop with f128 type.
  1482. if (MTy == LK.second)
  1483. return std::make_pair(Cost, MTy.getSimpleVT());
  1484. // Keep legalizing the type.
  1485. MTy = LK.second;
  1486. }
  1487. }
  1488. Value *TargetLoweringBase::getDefaultSafeStackPointerLocation(IRBuilder<> &IRB,
  1489. bool UseTLS) const {
  1490. // compiler-rt provides a variable with a magic name. Targets that do not
  1491. // link with compiler-rt may also provide such a variable.
  1492. Module *M = IRB.GetInsertBlock()->getParent()->getParent();
  1493. const char *UnsafeStackPtrVar = "__safestack_unsafe_stack_ptr";
  1494. auto UnsafeStackPtr =
  1495. dyn_cast_or_null<GlobalVariable>(M->getNamedValue(UnsafeStackPtrVar));
  1496. Type *StackPtrTy = Type::getInt8PtrTy(M->getContext());
  1497. if (!UnsafeStackPtr) {
  1498. auto TLSModel = UseTLS ?
  1499. GlobalValue::InitialExecTLSModel :
  1500. GlobalValue::NotThreadLocal;
  1501. // The global variable is not defined yet, define it ourselves.
  1502. // We use the initial-exec TLS model because we do not support the
  1503. // variable living anywhere other than in the main executable.
  1504. UnsafeStackPtr = new GlobalVariable(
  1505. *M, StackPtrTy, false, GlobalValue::ExternalLinkage, nullptr,
  1506. UnsafeStackPtrVar, nullptr, TLSModel);
  1507. } else {
  1508. // The variable exists, check its type and attributes.
  1509. if (UnsafeStackPtr->getValueType() != StackPtrTy)
  1510. report_fatal_error(Twine(UnsafeStackPtrVar) + " must have void* type");
  1511. if (UseTLS != UnsafeStackPtr->isThreadLocal())
  1512. report_fatal_error(Twine(UnsafeStackPtrVar) + " must " +
  1513. (UseTLS ? "" : "not ") + "be thread-local");
  1514. }
  1515. return UnsafeStackPtr;
  1516. }
  1517. Value *TargetLoweringBase::getSafeStackPointerLocation(IRBuilder<> &IRB) const {
  1518. if (!TM.getTargetTriple().isAndroid())
  1519. return getDefaultSafeStackPointerLocation(IRB, true);
  1520. // Android provides a libc function to retrieve the address of the current
  1521. // thread's unsafe stack pointer.
  1522. Module *M = IRB.GetInsertBlock()->getParent()->getParent();
  1523. Type *StackPtrTy = Type::getInt8PtrTy(M->getContext());
  1524. FunctionCallee Fn = M->getOrInsertFunction("__safestack_pointer_address",
  1525. StackPtrTy->getPointerTo(0));
  1526. return IRB.CreateCall(Fn);
  1527. }
  1528. //===----------------------------------------------------------------------===//
  1529. // Loop Strength Reduction hooks
  1530. //===----------------------------------------------------------------------===//
  1531. /// isLegalAddressingMode - Return true if the addressing mode represented
  1532. /// by AM is legal for this target, for a load/store of the specified type.
  1533. bool TargetLoweringBase::isLegalAddressingMode(const DataLayout &DL,
  1534. const AddrMode &AM, Type *Ty,
  1535. unsigned AS, Instruction *I) const {
  1536. // The default implementation of this implements a conservative RISCy, r+r and
  1537. // r+i addr mode.
  1538. // Allows a sign-extended 16-bit immediate field.
  1539. if (AM.BaseOffs <= -(1LL << 16) || AM.BaseOffs >= (1LL << 16)-1)
  1540. return false;
  1541. // No global is ever allowed as a base.
  1542. if (AM.BaseGV)
  1543. return false;
  1544. // Only support r+r,
  1545. switch (AM.Scale) {
  1546. case 0: // "r+i" or just "i", depending on HasBaseReg.
  1547. break;
  1548. case 1:
  1549. if (AM.HasBaseReg && AM.BaseOffs) // "r+r+i" is not allowed.
  1550. return false;
  1551. // Otherwise we have r+r or r+i.
  1552. break;
  1553. case 2:
  1554. if (AM.HasBaseReg || AM.BaseOffs) // 2*r+r or 2*r+i is not allowed.
  1555. return false;
  1556. // Allow 2*r as r+r.
  1557. break;
  1558. default: // Don't allow n * r
  1559. return false;
  1560. }
  1561. return true;
  1562. }
  1563. //===----------------------------------------------------------------------===//
  1564. // Stack Protector
  1565. //===----------------------------------------------------------------------===//
  1566. // For OpenBSD return its special guard variable. Otherwise return nullptr,
  1567. // so that SelectionDAG handle SSP.
  1568. Value *TargetLoweringBase::getIRStackGuard(IRBuilder<> &IRB) const {
  1569. if (getTargetMachine().getTargetTriple().isOSOpenBSD()) {
  1570. Module &M = *IRB.GetInsertBlock()->getParent()->getParent();
  1571. PointerType *PtrTy = Type::getInt8PtrTy(M.getContext());
  1572. return M.getOrInsertGlobal("__guard_local", PtrTy);
  1573. }
  1574. return nullptr;
  1575. }
  1576. // Currently only support "standard" __stack_chk_guard.
  1577. // TODO: add LOAD_STACK_GUARD support.
  1578. void TargetLoweringBase::insertSSPDeclarations(Module &M) const {
  1579. if (!M.getNamedValue("__stack_chk_guard"))
  1580. new GlobalVariable(M, Type::getInt8PtrTy(M.getContext()), false,
  1581. GlobalVariable::ExternalLinkage,
  1582. nullptr, "__stack_chk_guard");
  1583. }
  1584. // Currently only support "standard" __stack_chk_guard.
  1585. // TODO: add LOAD_STACK_GUARD support.
  1586. Value *TargetLoweringBase::getSDagStackGuard(const Module &M) const {
  1587. return M.getNamedValue("__stack_chk_guard");
  1588. }
  1589. Function *TargetLoweringBase::getSSPStackGuardCheck(const Module &M) const {
  1590. return nullptr;
  1591. }
  1592. unsigned TargetLoweringBase::getMinimumJumpTableEntries() const {
  1593. return MinimumJumpTableEntries;
  1594. }
  1595. void TargetLoweringBase::setMinimumJumpTableEntries(unsigned Val) {
  1596. MinimumJumpTableEntries = Val;
  1597. }
  1598. unsigned TargetLoweringBase::getMinimumJumpTableDensity(bool OptForSize) const {
  1599. return OptForSize ? OptsizeJumpTableDensity : JumpTableDensity;
  1600. }
  1601. unsigned TargetLoweringBase::getMaximumJumpTableSize() const {
  1602. return MaximumJumpTableSize;
  1603. }
  1604. void TargetLoweringBase::setMaximumJumpTableSize(unsigned Val) {
  1605. MaximumJumpTableSize = Val;
  1606. }
  1607. //===----------------------------------------------------------------------===//
  1608. // Reciprocal Estimates
  1609. //===----------------------------------------------------------------------===//
  1610. /// Get the reciprocal estimate attribute string for a function that will
  1611. /// override the target defaults.
  1612. static StringRef getRecipEstimateForFunc(MachineFunction &MF) {
  1613. const Function &F = MF.getFunction();
  1614. return F.getFnAttribute("reciprocal-estimates").getValueAsString();
  1615. }
  1616. /// Construct a string for the given reciprocal operation of the given type.
  1617. /// This string should match the corresponding option to the front-end's
  1618. /// "-mrecip" flag assuming those strings have been passed through in an
  1619. /// attribute string. For example, "vec-divf" for a division of a vXf32.
  1620. static std::string getReciprocalOpName(bool IsSqrt, EVT VT) {
  1621. std::string Name = VT.isVector() ? "vec-" : "";
  1622. Name += IsSqrt ? "sqrt" : "div";
  1623. // TODO: Handle "half" or other float types?
  1624. if (VT.getScalarType() == MVT::f64) {
  1625. Name += "d";
  1626. } else {
  1627. assert(VT.getScalarType() == MVT::f32 &&
  1628. "Unexpected FP type for reciprocal estimate");
  1629. Name += "f";
  1630. }
  1631. return Name;
  1632. }
  1633. /// Return the character position and value (a single numeric character) of a
  1634. /// customized refinement operation in the input string if it exists. Return
  1635. /// false if there is no customized refinement step count.
  1636. static bool parseRefinementStep(StringRef In, size_t &Position,
  1637. uint8_t &Value) {
  1638. const char RefStepToken = ':';
  1639. Position = In.find(RefStepToken);
  1640. if (Position == StringRef::npos)
  1641. return false;
  1642. StringRef RefStepString = In.substr(Position + 1);
  1643. // Allow exactly one numeric character for the additional refinement
  1644. // step parameter.
  1645. if (RefStepString.size() == 1) {
  1646. char RefStepChar = RefStepString[0];
  1647. if (RefStepChar >= '0' && RefStepChar <= '9') {
  1648. Value = RefStepChar - '0';
  1649. return true;
  1650. }
  1651. }
  1652. report_fatal_error("Invalid refinement step for -recip.");
  1653. }
  1654. /// For the input attribute string, return one of the ReciprocalEstimate enum
  1655. /// status values (enabled, disabled, or not specified) for this operation on
  1656. /// the specified data type.
  1657. static int getOpEnabled(bool IsSqrt, EVT VT, StringRef Override) {
  1658. if (Override.empty())
  1659. return TargetLoweringBase::ReciprocalEstimate::Unspecified;
  1660. SmallVector<StringRef, 4> OverrideVector;
  1661. Override.split(OverrideVector, ',');
  1662. unsigned NumArgs = OverrideVector.size();
  1663. // Check if "all", "none", or "default" was specified.
  1664. if (NumArgs == 1) {
  1665. // Look for an optional setting of the number of refinement steps needed
  1666. // for this type of reciprocal operation.
  1667. size_t RefPos;
  1668. uint8_t RefSteps;
  1669. if (parseRefinementStep(Override, RefPos, RefSteps)) {
  1670. // Split the string for further processing.
  1671. Override = Override.substr(0, RefPos);
  1672. }
  1673. // All reciprocal types are enabled.
  1674. if (Override == "all")
  1675. return TargetLoweringBase::ReciprocalEstimate::Enabled;
  1676. // All reciprocal types are disabled.
  1677. if (Override == "none")
  1678. return TargetLoweringBase::ReciprocalEstimate::Disabled;
  1679. // Target defaults for enablement are used.
  1680. if (Override == "default")
  1681. return TargetLoweringBase::ReciprocalEstimate::Unspecified;
  1682. }
  1683. // The attribute string may omit the size suffix ('f'/'d').
  1684. std::string VTName = getReciprocalOpName(IsSqrt, VT);
  1685. std::string VTNameNoSize = VTName;
  1686. VTNameNoSize.pop_back();
  1687. static const char DisabledPrefix = '!';
  1688. for (StringRef RecipType : OverrideVector) {
  1689. size_t RefPos;
  1690. uint8_t RefSteps;
  1691. if (parseRefinementStep(RecipType, RefPos, RefSteps))
  1692. RecipType = RecipType.substr(0, RefPos);
  1693. // Ignore the disablement token for string matching.
  1694. bool IsDisabled = RecipType[0] == DisabledPrefix;
  1695. if (IsDisabled)
  1696. RecipType = RecipType.substr(1);
  1697. if (RecipType.equals(VTName) || RecipType.equals(VTNameNoSize))
  1698. return IsDisabled ? TargetLoweringBase::ReciprocalEstimate::Disabled
  1699. : TargetLoweringBase::ReciprocalEstimate::Enabled;
  1700. }
  1701. return TargetLoweringBase::ReciprocalEstimate::Unspecified;
  1702. }
  1703. /// For the input attribute string, return the customized refinement step count
  1704. /// for this operation on the specified data type. If the step count does not
  1705. /// exist, return the ReciprocalEstimate enum value for unspecified.
  1706. static int getOpRefinementSteps(bool IsSqrt, EVT VT, StringRef Override) {
  1707. if (Override.empty())
  1708. return TargetLoweringBase::ReciprocalEstimate::Unspecified;
  1709. SmallVector<StringRef, 4> OverrideVector;
  1710. Override.split(OverrideVector, ',');
  1711. unsigned NumArgs = OverrideVector.size();
  1712. // Check if "all", "default", or "none" was specified.
  1713. if (NumArgs == 1) {
  1714. // Look for an optional setting of the number of refinement steps needed
  1715. // for this type of reciprocal operation.
  1716. size_t RefPos;
  1717. uint8_t RefSteps;
  1718. if (!parseRefinementStep(Override, RefPos, RefSteps))
  1719. return TargetLoweringBase::ReciprocalEstimate::Unspecified;
  1720. // Split the string for further processing.
  1721. Override = Override.substr(0, RefPos);
  1722. assert(Override != "none" &&
  1723. "Disabled reciprocals, but specifed refinement steps?");
  1724. // If this is a general override, return the specified number of steps.
  1725. if (Override == "all" || Override == "default")
  1726. return RefSteps;
  1727. }
  1728. // The attribute string may omit the size suffix ('f'/'d').
  1729. std::string VTName = getReciprocalOpName(IsSqrt, VT);
  1730. std::string VTNameNoSize = VTName;
  1731. VTNameNoSize.pop_back();
  1732. for (StringRef RecipType : OverrideVector) {
  1733. size_t RefPos;
  1734. uint8_t RefSteps;
  1735. if (!parseRefinementStep(RecipType, RefPos, RefSteps))
  1736. continue;
  1737. RecipType = RecipType.substr(0, RefPos);
  1738. if (RecipType.equals(VTName) || RecipType.equals(VTNameNoSize))
  1739. return RefSteps;
  1740. }
  1741. return TargetLoweringBase::ReciprocalEstimate::Unspecified;
  1742. }
  1743. int TargetLoweringBase::getRecipEstimateSqrtEnabled(EVT VT,
  1744. MachineFunction &MF) const {
  1745. return getOpEnabled(true, VT, getRecipEstimateForFunc(MF));
  1746. }
  1747. int TargetLoweringBase::getRecipEstimateDivEnabled(EVT VT,
  1748. MachineFunction &MF) const {
  1749. return getOpEnabled(false, VT, getRecipEstimateForFunc(MF));
  1750. }
  1751. int TargetLoweringBase::getSqrtRefinementSteps(EVT VT,
  1752. MachineFunction &MF) const {
  1753. return getOpRefinementSteps(true, VT, getRecipEstimateForFunc(MF));
  1754. }
  1755. int TargetLoweringBase::getDivRefinementSteps(EVT VT,
  1756. MachineFunction &MF) const {
  1757. return getOpRefinementSteps(false, VT, getRecipEstimateForFunc(MF));
  1758. }
  1759. void TargetLoweringBase::finalizeLowering(MachineFunction &MF) const {
  1760. MF.getRegInfo().freezeReservedRegs(MF);
  1761. }