TargetLoweringBase.cpp 88 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372
  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/Analysis/Loads.h"
  20. #include "llvm/Analysis/TargetTransformInfo.h"
  21. #include "llvm/CodeGen/Analysis.h"
  22. #include "llvm/CodeGen/ISDOpcodes.h"
  23. #include "llvm/CodeGen/MachineBasicBlock.h"
  24. #include "llvm/CodeGen/MachineFrameInfo.h"
  25. #include "llvm/CodeGen/MachineFunction.h"
  26. #include "llvm/CodeGen/MachineInstr.h"
  27. #include "llvm/CodeGen/MachineInstrBuilder.h"
  28. #include "llvm/CodeGen/MachineMemOperand.h"
  29. #include "llvm/CodeGen/MachineOperand.h"
  30. #include "llvm/CodeGen/MachineRegisterInfo.h"
  31. #include "llvm/CodeGen/RuntimeLibcalls.h"
  32. #include "llvm/CodeGen/StackMaps.h"
  33. #include "llvm/CodeGen/TargetLowering.h"
  34. #include "llvm/CodeGen/TargetOpcodes.h"
  35. #include "llvm/CodeGen/TargetRegisterInfo.h"
  36. #include "llvm/CodeGen/ValueTypes.h"
  37. #include "llvm/IR/Attributes.h"
  38. #include "llvm/IR/CallingConv.h"
  39. #include "llvm/IR/DataLayout.h"
  40. #include "llvm/IR/DerivedTypes.h"
  41. #include "llvm/IR/Function.h"
  42. #include "llvm/IR/GlobalValue.h"
  43. #include "llvm/IR/GlobalVariable.h"
  44. #include "llvm/IR/IRBuilder.h"
  45. #include "llvm/IR/Module.h"
  46. #include "llvm/IR/Type.h"
  47. #include "llvm/Support/Casting.h"
  48. #include "llvm/Support/CommandLine.h"
  49. #include "llvm/Support/Compiler.h"
  50. #include "llvm/Support/ErrorHandling.h"
  51. #include "llvm/Support/MachineValueType.h"
  52. #include "llvm/Support/MathExtras.h"
  53. #include "llvm/Target/TargetMachine.h"
  54. #include "llvm/Target/TargetOptions.h"
  55. #include "llvm/Transforms/Utils/SizeOpts.h"
  56. #include <algorithm>
  57. #include <cassert>
  58. #include <cstdint>
  59. #include <cstring>
  60. #include <iterator>
  61. #include <string>
  62. #include <tuple>
  63. #include <utility>
  64. using namespace llvm;
  65. static cl::opt<bool> JumpIsExpensiveOverride(
  66. "jump-is-expensive", cl::init(false),
  67. cl::desc("Do not create extra branches to split comparison logic."),
  68. cl::Hidden);
  69. static cl::opt<unsigned> MinimumJumpTableEntries
  70. ("min-jump-table-entries", cl::init(4), cl::Hidden,
  71. cl::desc("Set minimum number of entries to use a jump table."));
  72. static cl::opt<unsigned> MaximumJumpTableSize
  73. ("max-jump-table-size", cl::init(UINT_MAX), cl::Hidden,
  74. cl::desc("Set maximum size of jump tables."));
  75. /// Minimum jump table density for normal functions.
  76. static cl::opt<unsigned>
  77. JumpTableDensity("jump-table-density", cl::init(10), cl::Hidden,
  78. cl::desc("Minimum density for building a jump table in "
  79. "a normal function"));
  80. /// Minimum jump table density for -Os or -Oz functions.
  81. static cl::opt<unsigned> OptsizeJumpTableDensity(
  82. "optsize-jump-table-density", cl::init(40), cl::Hidden,
  83. cl::desc("Minimum density for building a jump table in "
  84. "an optsize function"));
  85. // FIXME: This option is only to test if the strict fp operation processed
  86. // correctly by preventing mutating strict fp operation to normal fp operation
  87. // during development. When the backend supports strict float operation, this
  88. // option will be meaningless.
  89. static cl::opt<bool> DisableStrictNodeMutation("disable-strictnode-mutation",
  90. cl::desc("Don't mutate strict-float node to a legalize node"),
  91. cl::init(false), cl::Hidden);
  92. static bool darwinHasSinCos(const Triple &TT) {
  93. assert(TT.isOSDarwin() && "should be called with darwin triple");
  94. // Don't bother with 32 bit x86.
  95. if (TT.getArch() == Triple::x86)
  96. return false;
  97. // Macos < 10.9 has no sincos_stret.
  98. if (TT.isMacOSX())
  99. return !TT.isMacOSXVersionLT(10, 9) && TT.isArch64Bit();
  100. // iOS < 7.0 has no sincos_stret.
  101. if (TT.isiOS())
  102. return !TT.isOSVersionLT(7, 0);
  103. // Any other darwin such as WatchOS/TvOS is new enough.
  104. return true;
  105. }
  106. void TargetLoweringBase::InitLibcalls(const Triple &TT) {
  107. #define HANDLE_LIBCALL(code, name) \
  108. setLibcallName(RTLIB::code, name);
  109. #include "llvm/IR/RuntimeLibcalls.def"
  110. #undef HANDLE_LIBCALL
  111. // Initialize calling conventions to their default.
  112. for (int LC = 0; LC < RTLIB::UNKNOWN_LIBCALL; ++LC)
  113. setLibcallCallingConv((RTLIB::Libcall)LC, CallingConv::C);
  114. // For IEEE quad-precision libcall names, PPC uses "kf" instead of "tf".
  115. if (TT.isPPC()) {
  116. setLibcallName(RTLIB::ADD_F128, "__addkf3");
  117. setLibcallName(RTLIB::SUB_F128, "__subkf3");
  118. setLibcallName(RTLIB::MUL_F128, "__mulkf3");
  119. setLibcallName(RTLIB::DIV_F128, "__divkf3");
  120. setLibcallName(RTLIB::POWI_F128, "__powikf2");
  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::FPTOSINT_F128_I128, "__fixkfti");
  128. setLibcallName(RTLIB::FPTOUINT_F128_I32, "__fixunskfsi");
  129. setLibcallName(RTLIB::FPTOUINT_F128_I64, "__fixunskfdi");
  130. setLibcallName(RTLIB::FPTOUINT_F128_I128, "__fixunskfti");
  131. setLibcallName(RTLIB::SINTTOFP_I32_F128, "__floatsikf");
  132. setLibcallName(RTLIB::SINTTOFP_I64_F128, "__floatdikf");
  133. setLibcallName(RTLIB::SINTTOFP_I128_F128, "__floattikf");
  134. setLibcallName(RTLIB::UINTTOFP_I32_F128, "__floatunsikf");
  135. setLibcallName(RTLIB::UINTTOFP_I64_F128, "__floatundikf");
  136. setLibcallName(RTLIB::UINTTOFP_I128_F128, "__floatuntikf");
  137. setLibcallName(RTLIB::OEQ_F128, "__eqkf2");
  138. setLibcallName(RTLIB::UNE_F128, "__nekf2");
  139. setLibcallName(RTLIB::OGE_F128, "__gekf2");
  140. setLibcallName(RTLIB::OLT_F128, "__ltkf2");
  141. setLibcallName(RTLIB::OLE_F128, "__lekf2");
  142. setLibcallName(RTLIB::OGT_F128, "__gtkf2");
  143. setLibcallName(RTLIB::UO_F128, "__unordkf2");
  144. }
  145. // A few names are different on particular architectures or environments.
  146. if (TT.isOSDarwin()) {
  147. // For f16/f32 conversions, Darwin uses the standard naming scheme, instead
  148. // of the gnueabi-style __gnu_*_ieee.
  149. // FIXME: What about other targets?
  150. setLibcallName(RTLIB::FPEXT_F16_F32, "__extendhfsf2");
  151. setLibcallName(RTLIB::FPROUND_F32_F16, "__truncsfhf2");
  152. // Some darwins have an optimized __bzero/bzero function.
  153. switch (TT.getArch()) {
  154. case Triple::x86:
  155. case Triple::x86_64:
  156. if (TT.isMacOSX() && !TT.isMacOSXVersionLT(10, 6))
  157. setLibcallName(RTLIB::BZERO, "__bzero");
  158. break;
  159. case Triple::aarch64:
  160. case Triple::aarch64_32:
  161. setLibcallName(RTLIB::BZERO, "bzero");
  162. break;
  163. default:
  164. break;
  165. }
  166. if (darwinHasSinCos(TT)) {
  167. setLibcallName(RTLIB::SINCOS_STRET_F32, "__sincosf_stret");
  168. setLibcallName(RTLIB::SINCOS_STRET_F64, "__sincos_stret");
  169. if (TT.isWatchABI()) {
  170. setLibcallCallingConv(RTLIB::SINCOS_STRET_F32,
  171. CallingConv::ARM_AAPCS_VFP);
  172. setLibcallCallingConv(RTLIB::SINCOS_STRET_F64,
  173. CallingConv::ARM_AAPCS_VFP);
  174. }
  175. }
  176. } else {
  177. setLibcallName(RTLIB::FPEXT_F16_F32, "__gnu_h2f_ieee");
  178. setLibcallName(RTLIB::FPROUND_F32_F16, "__gnu_f2h_ieee");
  179. }
  180. if (TT.isGNUEnvironment() || TT.isOSFuchsia() ||
  181. (TT.isAndroid() && !TT.isAndroidVersionLT(9))) {
  182. setLibcallName(RTLIB::SINCOS_F32, "sincosf");
  183. setLibcallName(RTLIB::SINCOS_F64, "sincos");
  184. setLibcallName(RTLIB::SINCOS_F80, "sincosl");
  185. setLibcallName(RTLIB::SINCOS_F128, "sincosl");
  186. setLibcallName(RTLIB::SINCOS_PPCF128, "sincosl");
  187. }
  188. if (TT.isPS()) {
  189. setLibcallName(RTLIB::SINCOS_F32, "sincosf");
  190. setLibcallName(RTLIB::SINCOS_F64, "sincos");
  191. }
  192. if (TT.isOSOpenBSD()) {
  193. setLibcallName(RTLIB::STACKPROTECTOR_CHECK_FAIL, nullptr);
  194. }
  195. }
  196. /// GetFPLibCall - Helper to return the right libcall for the given floating
  197. /// point type, or UNKNOWN_LIBCALL if there is none.
  198. RTLIB::Libcall RTLIB::getFPLibCall(EVT VT,
  199. RTLIB::Libcall Call_F32,
  200. RTLIB::Libcall Call_F64,
  201. RTLIB::Libcall Call_F80,
  202. RTLIB::Libcall Call_F128,
  203. RTLIB::Libcall Call_PPCF128) {
  204. return
  205. VT == MVT::f32 ? Call_F32 :
  206. VT == MVT::f64 ? Call_F64 :
  207. VT == MVT::f80 ? Call_F80 :
  208. VT == MVT::f128 ? Call_F128 :
  209. VT == MVT::ppcf128 ? Call_PPCF128 :
  210. RTLIB::UNKNOWN_LIBCALL;
  211. }
  212. /// getFPEXT - Return the FPEXT_*_* value for the given types, or
  213. /// UNKNOWN_LIBCALL if there is none.
  214. RTLIB::Libcall RTLIB::getFPEXT(EVT OpVT, EVT RetVT) {
  215. if (OpVT == MVT::f16) {
  216. if (RetVT == MVT::f32)
  217. return FPEXT_F16_F32;
  218. if (RetVT == MVT::f64)
  219. return FPEXT_F16_F64;
  220. if (RetVT == MVT::f80)
  221. return FPEXT_F16_F80;
  222. if (RetVT == MVT::f128)
  223. return FPEXT_F16_F128;
  224. } else if (OpVT == MVT::f32) {
  225. if (RetVT == MVT::f64)
  226. return FPEXT_F32_F64;
  227. if (RetVT == MVT::f128)
  228. return FPEXT_F32_F128;
  229. if (RetVT == MVT::ppcf128)
  230. return FPEXT_F32_PPCF128;
  231. } else if (OpVT == MVT::f64) {
  232. if (RetVT == MVT::f128)
  233. return FPEXT_F64_F128;
  234. else if (RetVT == MVT::ppcf128)
  235. return FPEXT_F64_PPCF128;
  236. } else if (OpVT == MVT::f80) {
  237. if (RetVT == MVT::f128)
  238. return FPEXT_F80_F128;
  239. }
  240. return UNKNOWN_LIBCALL;
  241. }
  242. /// getFPROUND - Return the FPROUND_*_* value for the given types, or
  243. /// UNKNOWN_LIBCALL if there is none.
  244. RTLIB::Libcall RTLIB::getFPROUND(EVT OpVT, EVT RetVT) {
  245. if (RetVT == MVT::f16) {
  246. if (OpVT == MVT::f32)
  247. return FPROUND_F32_F16;
  248. if (OpVT == MVT::f64)
  249. return FPROUND_F64_F16;
  250. if (OpVT == MVT::f80)
  251. return FPROUND_F80_F16;
  252. if (OpVT == MVT::f128)
  253. return FPROUND_F128_F16;
  254. if (OpVT == MVT::ppcf128)
  255. return FPROUND_PPCF128_F16;
  256. } else if (RetVT == MVT::bf16) {
  257. if (OpVT == MVT::f32)
  258. return FPROUND_F32_BF16;
  259. if (OpVT == MVT::f64)
  260. return FPROUND_F64_BF16;
  261. } else if (RetVT == MVT::f32) {
  262. if (OpVT == MVT::f64)
  263. return FPROUND_F64_F32;
  264. if (OpVT == MVT::f80)
  265. return FPROUND_F80_F32;
  266. if (OpVT == MVT::f128)
  267. return FPROUND_F128_F32;
  268. if (OpVT == MVT::ppcf128)
  269. return FPROUND_PPCF128_F32;
  270. } else if (RetVT == MVT::f64) {
  271. if (OpVT == MVT::f80)
  272. return FPROUND_F80_F64;
  273. if (OpVT == MVT::f128)
  274. return FPROUND_F128_F64;
  275. if (OpVT == MVT::ppcf128)
  276. return FPROUND_PPCF128_F64;
  277. } else if (RetVT == MVT::f80) {
  278. if (OpVT == MVT::f128)
  279. return FPROUND_F128_F80;
  280. }
  281. return UNKNOWN_LIBCALL;
  282. }
  283. /// getFPTOSINT - Return the FPTOSINT_*_* value for the given types, or
  284. /// UNKNOWN_LIBCALL if there is none.
  285. RTLIB::Libcall RTLIB::getFPTOSINT(EVT OpVT, EVT RetVT) {
  286. if (OpVT == MVT::f16) {
  287. if (RetVT == MVT::i32)
  288. return FPTOSINT_F16_I32;
  289. if (RetVT == MVT::i64)
  290. return FPTOSINT_F16_I64;
  291. if (RetVT == MVT::i128)
  292. return FPTOSINT_F16_I128;
  293. } else if (OpVT == MVT::f32) {
  294. if (RetVT == MVT::i32)
  295. return FPTOSINT_F32_I32;
  296. if (RetVT == MVT::i64)
  297. return FPTOSINT_F32_I64;
  298. if (RetVT == MVT::i128)
  299. return FPTOSINT_F32_I128;
  300. } else if (OpVT == MVT::f64) {
  301. if (RetVT == MVT::i32)
  302. return FPTOSINT_F64_I32;
  303. if (RetVT == MVT::i64)
  304. return FPTOSINT_F64_I64;
  305. if (RetVT == MVT::i128)
  306. return FPTOSINT_F64_I128;
  307. } else if (OpVT == MVT::f80) {
  308. if (RetVT == MVT::i32)
  309. return FPTOSINT_F80_I32;
  310. if (RetVT == MVT::i64)
  311. return FPTOSINT_F80_I64;
  312. if (RetVT == MVT::i128)
  313. return FPTOSINT_F80_I128;
  314. } else if (OpVT == MVT::f128) {
  315. if (RetVT == MVT::i32)
  316. return FPTOSINT_F128_I32;
  317. if (RetVT == MVT::i64)
  318. return FPTOSINT_F128_I64;
  319. if (RetVT == MVT::i128)
  320. return FPTOSINT_F128_I128;
  321. } else if (OpVT == MVT::ppcf128) {
  322. if (RetVT == MVT::i32)
  323. return FPTOSINT_PPCF128_I32;
  324. if (RetVT == MVT::i64)
  325. return FPTOSINT_PPCF128_I64;
  326. if (RetVT == MVT::i128)
  327. return FPTOSINT_PPCF128_I128;
  328. }
  329. return UNKNOWN_LIBCALL;
  330. }
  331. /// getFPTOUINT - Return the FPTOUINT_*_* value for the given types, or
  332. /// UNKNOWN_LIBCALL if there is none.
  333. RTLIB::Libcall RTLIB::getFPTOUINT(EVT OpVT, EVT RetVT) {
  334. if (OpVT == MVT::f16) {
  335. if (RetVT == MVT::i32)
  336. return FPTOUINT_F16_I32;
  337. if (RetVT == MVT::i64)
  338. return FPTOUINT_F16_I64;
  339. if (RetVT == MVT::i128)
  340. return FPTOUINT_F16_I128;
  341. } else if (OpVT == MVT::f32) {
  342. if (RetVT == MVT::i32)
  343. return FPTOUINT_F32_I32;
  344. if (RetVT == MVT::i64)
  345. return FPTOUINT_F32_I64;
  346. if (RetVT == MVT::i128)
  347. return FPTOUINT_F32_I128;
  348. } else if (OpVT == MVT::f64) {
  349. if (RetVT == MVT::i32)
  350. return FPTOUINT_F64_I32;
  351. if (RetVT == MVT::i64)
  352. return FPTOUINT_F64_I64;
  353. if (RetVT == MVT::i128)
  354. return FPTOUINT_F64_I128;
  355. } else if (OpVT == MVT::f80) {
  356. if (RetVT == MVT::i32)
  357. return FPTOUINT_F80_I32;
  358. if (RetVT == MVT::i64)
  359. return FPTOUINT_F80_I64;
  360. if (RetVT == MVT::i128)
  361. return FPTOUINT_F80_I128;
  362. } else if (OpVT == MVT::f128) {
  363. if (RetVT == MVT::i32)
  364. return FPTOUINT_F128_I32;
  365. if (RetVT == MVT::i64)
  366. return FPTOUINT_F128_I64;
  367. if (RetVT == MVT::i128)
  368. return FPTOUINT_F128_I128;
  369. } else if (OpVT == MVT::ppcf128) {
  370. if (RetVT == MVT::i32)
  371. return FPTOUINT_PPCF128_I32;
  372. if (RetVT == MVT::i64)
  373. return FPTOUINT_PPCF128_I64;
  374. if (RetVT == MVT::i128)
  375. return FPTOUINT_PPCF128_I128;
  376. }
  377. return UNKNOWN_LIBCALL;
  378. }
  379. /// getSINTTOFP - Return the SINTTOFP_*_* value for the given types, or
  380. /// UNKNOWN_LIBCALL if there is none.
  381. RTLIB::Libcall RTLIB::getSINTTOFP(EVT OpVT, EVT RetVT) {
  382. if (OpVT == MVT::i32) {
  383. if (RetVT == MVT::f16)
  384. return SINTTOFP_I32_F16;
  385. if (RetVT == MVT::f32)
  386. return SINTTOFP_I32_F32;
  387. if (RetVT == MVT::f64)
  388. return SINTTOFP_I32_F64;
  389. if (RetVT == MVT::f80)
  390. return SINTTOFP_I32_F80;
  391. if (RetVT == MVT::f128)
  392. return SINTTOFP_I32_F128;
  393. if (RetVT == MVT::ppcf128)
  394. return SINTTOFP_I32_PPCF128;
  395. } else if (OpVT == MVT::i64) {
  396. if (RetVT == MVT::f16)
  397. return SINTTOFP_I64_F16;
  398. if (RetVT == MVT::f32)
  399. return SINTTOFP_I64_F32;
  400. if (RetVT == MVT::f64)
  401. return SINTTOFP_I64_F64;
  402. if (RetVT == MVT::f80)
  403. return SINTTOFP_I64_F80;
  404. if (RetVT == MVT::f128)
  405. return SINTTOFP_I64_F128;
  406. if (RetVT == MVT::ppcf128)
  407. return SINTTOFP_I64_PPCF128;
  408. } else if (OpVT == MVT::i128) {
  409. if (RetVT == MVT::f16)
  410. return SINTTOFP_I128_F16;
  411. if (RetVT == MVT::f32)
  412. return SINTTOFP_I128_F32;
  413. if (RetVT == MVT::f64)
  414. return SINTTOFP_I128_F64;
  415. if (RetVT == MVT::f80)
  416. return SINTTOFP_I128_F80;
  417. if (RetVT == MVT::f128)
  418. return SINTTOFP_I128_F128;
  419. if (RetVT == MVT::ppcf128)
  420. return SINTTOFP_I128_PPCF128;
  421. }
  422. return UNKNOWN_LIBCALL;
  423. }
  424. /// getUINTTOFP - Return the UINTTOFP_*_* value for the given types, or
  425. /// UNKNOWN_LIBCALL if there is none.
  426. RTLIB::Libcall RTLIB::getUINTTOFP(EVT OpVT, EVT RetVT) {
  427. if (OpVT == MVT::i32) {
  428. if (RetVT == MVT::f16)
  429. return UINTTOFP_I32_F16;
  430. if (RetVT == MVT::f32)
  431. return UINTTOFP_I32_F32;
  432. if (RetVT == MVT::f64)
  433. return UINTTOFP_I32_F64;
  434. if (RetVT == MVT::f80)
  435. return UINTTOFP_I32_F80;
  436. if (RetVT == MVT::f128)
  437. return UINTTOFP_I32_F128;
  438. if (RetVT == MVT::ppcf128)
  439. return UINTTOFP_I32_PPCF128;
  440. } else if (OpVT == MVT::i64) {
  441. if (RetVT == MVT::f16)
  442. return UINTTOFP_I64_F16;
  443. if (RetVT == MVT::f32)
  444. return UINTTOFP_I64_F32;
  445. if (RetVT == MVT::f64)
  446. return UINTTOFP_I64_F64;
  447. if (RetVT == MVT::f80)
  448. return UINTTOFP_I64_F80;
  449. if (RetVT == MVT::f128)
  450. return UINTTOFP_I64_F128;
  451. if (RetVT == MVT::ppcf128)
  452. return UINTTOFP_I64_PPCF128;
  453. } else if (OpVT == MVT::i128) {
  454. if (RetVT == MVT::f16)
  455. return UINTTOFP_I128_F16;
  456. if (RetVT == MVT::f32)
  457. return UINTTOFP_I128_F32;
  458. if (RetVT == MVT::f64)
  459. return UINTTOFP_I128_F64;
  460. if (RetVT == MVT::f80)
  461. return UINTTOFP_I128_F80;
  462. if (RetVT == MVT::f128)
  463. return UINTTOFP_I128_F128;
  464. if (RetVT == MVT::ppcf128)
  465. return UINTTOFP_I128_PPCF128;
  466. }
  467. return UNKNOWN_LIBCALL;
  468. }
  469. RTLIB::Libcall RTLIB::getPOWI(EVT RetVT) {
  470. return getFPLibCall(RetVT, POWI_F32, POWI_F64, POWI_F80, POWI_F128,
  471. POWI_PPCF128);
  472. }
  473. RTLIB::Libcall RTLIB::getOUTLINE_ATOMIC(unsigned Opc, AtomicOrdering Order,
  474. MVT VT) {
  475. unsigned ModeN, ModelN;
  476. switch (VT.SimpleTy) {
  477. case MVT::i8:
  478. ModeN = 0;
  479. break;
  480. case MVT::i16:
  481. ModeN = 1;
  482. break;
  483. case MVT::i32:
  484. ModeN = 2;
  485. break;
  486. case MVT::i64:
  487. ModeN = 3;
  488. break;
  489. case MVT::i128:
  490. ModeN = 4;
  491. break;
  492. default:
  493. return UNKNOWN_LIBCALL;
  494. }
  495. switch (Order) {
  496. case AtomicOrdering::Monotonic:
  497. ModelN = 0;
  498. break;
  499. case AtomicOrdering::Acquire:
  500. ModelN = 1;
  501. break;
  502. case AtomicOrdering::Release:
  503. ModelN = 2;
  504. break;
  505. case AtomicOrdering::AcquireRelease:
  506. case AtomicOrdering::SequentiallyConsistent:
  507. ModelN = 3;
  508. break;
  509. default:
  510. return UNKNOWN_LIBCALL;
  511. }
  512. #define LCALLS(A, B) \
  513. { A##B##_RELAX, A##B##_ACQ, A##B##_REL, A##B##_ACQ_REL }
  514. #define LCALL5(A) \
  515. LCALLS(A, 1), LCALLS(A, 2), LCALLS(A, 4), LCALLS(A, 8), LCALLS(A, 16)
  516. switch (Opc) {
  517. case ISD::ATOMIC_CMP_SWAP: {
  518. const Libcall LC[5][4] = {LCALL5(OUTLINE_ATOMIC_CAS)};
  519. return LC[ModeN][ModelN];
  520. }
  521. case ISD::ATOMIC_SWAP: {
  522. const Libcall LC[5][4] = {LCALL5(OUTLINE_ATOMIC_SWP)};
  523. return LC[ModeN][ModelN];
  524. }
  525. case ISD::ATOMIC_LOAD_ADD: {
  526. const Libcall LC[5][4] = {LCALL5(OUTLINE_ATOMIC_LDADD)};
  527. return LC[ModeN][ModelN];
  528. }
  529. case ISD::ATOMIC_LOAD_OR: {
  530. const Libcall LC[5][4] = {LCALL5(OUTLINE_ATOMIC_LDSET)};
  531. return LC[ModeN][ModelN];
  532. }
  533. case ISD::ATOMIC_LOAD_CLR: {
  534. const Libcall LC[5][4] = {LCALL5(OUTLINE_ATOMIC_LDCLR)};
  535. return LC[ModeN][ModelN];
  536. }
  537. case ISD::ATOMIC_LOAD_XOR: {
  538. const Libcall LC[5][4] = {LCALL5(OUTLINE_ATOMIC_LDEOR)};
  539. return LC[ModeN][ModelN];
  540. }
  541. default:
  542. return UNKNOWN_LIBCALL;
  543. }
  544. #undef LCALLS
  545. #undef LCALL5
  546. }
  547. RTLIB::Libcall RTLIB::getSYNC(unsigned Opc, MVT VT) {
  548. #define OP_TO_LIBCALL(Name, Enum) \
  549. case Name: \
  550. switch (VT.SimpleTy) { \
  551. default: \
  552. return UNKNOWN_LIBCALL; \
  553. case MVT::i8: \
  554. return Enum##_1; \
  555. case MVT::i16: \
  556. return Enum##_2; \
  557. case MVT::i32: \
  558. return Enum##_4; \
  559. case MVT::i64: \
  560. return Enum##_8; \
  561. case MVT::i128: \
  562. return Enum##_16; \
  563. }
  564. switch (Opc) {
  565. OP_TO_LIBCALL(ISD::ATOMIC_SWAP, SYNC_LOCK_TEST_AND_SET)
  566. OP_TO_LIBCALL(ISD::ATOMIC_CMP_SWAP, SYNC_VAL_COMPARE_AND_SWAP)
  567. OP_TO_LIBCALL(ISD::ATOMIC_LOAD_ADD, SYNC_FETCH_AND_ADD)
  568. OP_TO_LIBCALL(ISD::ATOMIC_LOAD_SUB, SYNC_FETCH_AND_SUB)
  569. OP_TO_LIBCALL(ISD::ATOMIC_LOAD_AND, SYNC_FETCH_AND_AND)
  570. OP_TO_LIBCALL(ISD::ATOMIC_LOAD_OR, SYNC_FETCH_AND_OR)
  571. OP_TO_LIBCALL(ISD::ATOMIC_LOAD_XOR, SYNC_FETCH_AND_XOR)
  572. OP_TO_LIBCALL(ISD::ATOMIC_LOAD_NAND, SYNC_FETCH_AND_NAND)
  573. OP_TO_LIBCALL(ISD::ATOMIC_LOAD_MAX, SYNC_FETCH_AND_MAX)
  574. OP_TO_LIBCALL(ISD::ATOMIC_LOAD_UMAX, SYNC_FETCH_AND_UMAX)
  575. OP_TO_LIBCALL(ISD::ATOMIC_LOAD_MIN, SYNC_FETCH_AND_MIN)
  576. OP_TO_LIBCALL(ISD::ATOMIC_LOAD_UMIN, SYNC_FETCH_AND_UMIN)
  577. }
  578. #undef OP_TO_LIBCALL
  579. return UNKNOWN_LIBCALL;
  580. }
  581. RTLIB::Libcall RTLIB::getMEMCPY_ELEMENT_UNORDERED_ATOMIC(uint64_t ElementSize) {
  582. switch (ElementSize) {
  583. case 1:
  584. return MEMCPY_ELEMENT_UNORDERED_ATOMIC_1;
  585. case 2:
  586. return MEMCPY_ELEMENT_UNORDERED_ATOMIC_2;
  587. case 4:
  588. return MEMCPY_ELEMENT_UNORDERED_ATOMIC_4;
  589. case 8:
  590. return MEMCPY_ELEMENT_UNORDERED_ATOMIC_8;
  591. case 16:
  592. return MEMCPY_ELEMENT_UNORDERED_ATOMIC_16;
  593. default:
  594. return UNKNOWN_LIBCALL;
  595. }
  596. }
  597. RTLIB::Libcall RTLIB::getMEMMOVE_ELEMENT_UNORDERED_ATOMIC(uint64_t ElementSize) {
  598. switch (ElementSize) {
  599. case 1:
  600. return MEMMOVE_ELEMENT_UNORDERED_ATOMIC_1;
  601. case 2:
  602. return MEMMOVE_ELEMENT_UNORDERED_ATOMIC_2;
  603. case 4:
  604. return MEMMOVE_ELEMENT_UNORDERED_ATOMIC_4;
  605. case 8:
  606. return MEMMOVE_ELEMENT_UNORDERED_ATOMIC_8;
  607. case 16:
  608. return MEMMOVE_ELEMENT_UNORDERED_ATOMIC_16;
  609. default:
  610. return UNKNOWN_LIBCALL;
  611. }
  612. }
  613. RTLIB::Libcall RTLIB::getMEMSET_ELEMENT_UNORDERED_ATOMIC(uint64_t ElementSize) {
  614. switch (ElementSize) {
  615. case 1:
  616. return MEMSET_ELEMENT_UNORDERED_ATOMIC_1;
  617. case 2:
  618. return MEMSET_ELEMENT_UNORDERED_ATOMIC_2;
  619. case 4:
  620. return MEMSET_ELEMENT_UNORDERED_ATOMIC_4;
  621. case 8:
  622. return MEMSET_ELEMENT_UNORDERED_ATOMIC_8;
  623. case 16:
  624. return MEMSET_ELEMENT_UNORDERED_ATOMIC_16;
  625. default:
  626. return UNKNOWN_LIBCALL;
  627. }
  628. }
  629. /// InitCmpLibcallCCs - Set default comparison libcall CC.
  630. static void InitCmpLibcallCCs(ISD::CondCode *CCs) {
  631. std::fill(CCs, CCs + RTLIB::UNKNOWN_LIBCALL, ISD::SETCC_INVALID);
  632. CCs[RTLIB::OEQ_F32] = ISD::SETEQ;
  633. CCs[RTLIB::OEQ_F64] = ISD::SETEQ;
  634. CCs[RTLIB::OEQ_F128] = ISD::SETEQ;
  635. CCs[RTLIB::OEQ_PPCF128] = ISD::SETEQ;
  636. CCs[RTLIB::UNE_F32] = ISD::SETNE;
  637. CCs[RTLIB::UNE_F64] = ISD::SETNE;
  638. CCs[RTLIB::UNE_F128] = ISD::SETNE;
  639. CCs[RTLIB::UNE_PPCF128] = ISD::SETNE;
  640. CCs[RTLIB::OGE_F32] = ISD::SETGE;
  641. CCs[RTLIB::OGE_F64] = ISD::SETGE;
  642. CCs[RTLIB::OGE_F128] = ISD::SETGE;
  643. CCs[RTLIB::OGE_PPCF128] = ISD::SETGE;
  644. CCs[RTLIB::OLT_F32] = ISD::SETLT;
  645. CCs[RTLIB::OLT_F64] = ISD::SETLT;
  646. CCs[RTLIB::OLT_F128] = ISD::SETLT;
  647. CCs[RTLIB::OLT_PPCF128] = ISD::SETLT;
  648. CCs[RTLIB::OLE_F32] = ISD::SETLE;
  649. CCs[RTLIB::OLE_F64] = ISD::SETLE;
  650. CCs[RTLIB::OLE_F128] = ISD::SETLE;
  651. CCs[RTLIB::OLE_PPCF128] = ISD::SETLE;
  652. CCs[RTLIB::OGT_F32] = ISD::SETGT;
  653. CCs[RTLIB::OGT_F64] = ISD::SETGT;
  654. CCs[RTLIB::OGT_F128] = ISD::SETGT;
  655. CCs[RTLIB::OGT_PPCF128] = ISD::SETGT;
  656. CCs[RTLIB::UO_F32] = ISD::SETNE;
  657. CCs[RTLIB::UO_F64] = ISD::SETNE;
  658. CCs[RTLIB::UO_F128] = ISD::SETNE;
  659. CCs[RTLIB::UO_PPCF128] = ISD::SETNE;
  660. }
  661. /// NOTE: The TargetMachine owns TLOF.
  662. TargetLoweringBase::TargetLoweringBase(const TargetMachine &tm) : TM(tm) {
  663. initActions();
  664. // Perform these initializations only once.
  665. MaxStoresPerMemset = MaxStoresPerMemcpy = MaxStoresPerMemmove =
  666. MaxLoadsPerMemcmp = 8;
  667. MaxGluedStoresPerMemcpy = 0;
  668. MaxStoresPerMemsetOptSize = MaxStoresPerMemcpyOptSize =
  669. MaxStoresPerMemmoveOptSize = MaxLoadsPerMemcmpOptSize = 4;
  670. HasMultipleConditionRegisters = false;
  671. HasExtractBitsInsn = false;
  672. JumpIsExpensive = JumpIsExpensiveOverride;
  673. PredictableSelectIsExpensive = false;
  674. EnableExtLdPromotion = false;
  675. StackPointerRegisterToSaveRestore = 0;
  676. BooleanContents = UndefinedBooleanContent;
  677. BooleanFloatContents = UndefinedBooleanContent;
  678. BooleanVectorContents = UndefinedBooleanContent;
  679. SchedPreferenceInfo = Sched::ILP;
  680. GatherAllAliasesMaxDepth = 18;
  681. IsStrictFPEnabled = DisableStrictNodeMutation;
  682. MaxBytesForAlignment = 0;
  683. // TODO: the default will be switched to 0 in the next commit, along
  684. // with the Target-specific changes necessary.
  685. MaxAtomicSizeInBitsSupported = 1024;
  686. // Assume that even with libcalls, no target supports wider than 128 bit
  687. // division.
  688. MaxDivRemBitWidthSupported = 128;
  689. MaxLargeFPConvertBitWidthSupported = llvm::IntegerType::MAX_INT_BITS;
  690. MinCmpXchgSizeInBits = 0;
  691. SupportsUnalignedAtomics = false;
  692. std::fill(std::begin(LibcallRoutineNames), std::end(LibcallRoutineNames), nullptr);
  693. InitLibcalls(TM.getTargetTriple());
  694. InitCmpLibcallCCs(CmpLibcallCCs);
  695. }
  696. void TargetLoweringBase::initActions() {
  697. // All operations default to being supported.
  698. memset(OpActions, 0, sizeof(OpActions));
  699. memset(LoadExtActions, 0, sizeof(LoadExtActions));
  700. memset(TruncStoreActions, 0, sizeof(TruncStoreActions));
  701. memset(IndexedModeActions, 0, sizeof(IndexedModeActions));
  702. memset(CondCodeActions, 0, sizeof(CondCodeActions));
  703. std::fill(std::begin(RegClassForVT), std::end(RegClassForVT), nullptr);
  704. std::fill(std::begin(TargetDAGCombineArray),
  705. std::end(TargetDAGCombineArray), 0);
  706. // We're somewhat special casing MVT::i2 and MVT::i4. Ideally we want to
  707. // remove this and targets should individually set these types if not legal.
  708. for (ISD::NodeType NT : enum_seq(ISD::DELETED_NODE, ISD::BUILTIN_OP_END,
  709. force_iteration_on_noniterable_enum)) {
  710. for (MVT VT : {MVT::i2, MVT::i4})
  711. OpActions[(unsigned)VT.SimpleTy][NT] = Expand;
  712. }
  713. for (MVT AVT : MVT::all_valuetypes()) {
  714. for (MVT VT : {MVT::i2, MVT::i4, MVT::v128i2, MVT::v64i4}) {
  715. setTruncStoreAction(AVT, VT, Expand);
  716. setLoadExtAction(ISD::EXTLOAD, AVT, VT, Expand);
  717. setLoadExtAction(ISD::ZEXTLOAD, AVT, VT, Expand);
  718. }
  719. }
  720. for (unsigned IM = (unsigned)ISD::PRE_INC;
  721. IM != (unsigned)ISD::LAST_INDEXED_MODE; ++IM) {
  722. for (MVT VT : {MVT::i2, MVT::i4}) {
  723. setIndexedLoadAction(IM, VT, Expand);
  724. setIndexedStoreAction(IM, VT, Expand);
  725. setIndexedMaskedLoadAction(IM, VT, Expand);
  726. setIndexedMaskedStoreAction(IM, VT, Expand);
  727. }
  728. }
  729. for (MVT VT : MVT::fp_valuetypes()) {
  730. MVT IntVT = MVT::getIntegerVT(VT.getFixedSizeInBits());
  731. if (IntVT.isValid()) {
  732. setOperationAction(ISD::ATOMIC_SWAP, VT, Promote);
  733. AddPromotedToType(ISD::ATOMIC_SWAP, VT, IntVT);
  734. }
  735. }
  736. // Set default actions for various operations.
  737. for (MVT VT : MVT::all_valuetypes()) {
  738. // Default all indexed load / store to expand.
  739. for (unsigned IM = (unsigned)ISD::PRE_INC;
  740. IM != (unsigned)ISD::LAST_INDEXED_MODE; ++IM) {
  741. setIndexedLoadAction(IM, VT, Expand);
  742. setIndexedStoreAction(IM, VT, Expand);
  743. setIndexedMaskedLoadAction(IM, VT, Expand);
  744. setIndexedMaskedStoreAction(IM, VT, Expand);
  745. }
  746. // Most backends expect to see the node which just returns the value loaded.
  747. setOperationAction(ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS, VT, Expand);
  748. // These operations default to expand.
  749. setOperationAction({ISD::FGETSIGN, ISD::CONCAT_VECTORS,
  750. ISD::FMINNUM, ISD::FMAXNUM,
  751. ISD::FMINNUM_IEEE, ISD::FMAXNUM_IEEE,
  752. ISD::FMINIMUM, ISD::FMAXIMUM,
  753. ISD::FMAD, ISD::SMIN,
  754. ISD::SMAX, ISD::UMIN,
  755. ISD::UMAX, ISD::ABS,
  756. ISD::FSHL, ISD::FSHR,
  757. ISD::SADDSAT, ISD::UADDSAT,
  758. ISD::SSUBSAT, ISD::USUBSAT,
  759. ISD::SSHLSAT, ISD::USHLSAT,
  760. ISD::SMULFIX, ISD::SMULFIXSAT,
  761. ISD::UMULFIX, ISD::UMULFIXSAT,
  762. ISD::SDIVFIX, ISD::SDIVFIXSAT,
  763. ISD::UDIVFIX, ISD::UDIVFIXSAT,
  764. ISD::FP_TO_SINT_SAT, ISD::FP_TO_UINT_SAT,
  765. ISD::IS_FPCLASS},
  766. VT, Expand);
  767. // Overflow operations default to expand
  768. setOperationAction({ISD::SADDO, ISD::SSUBO, ISD::UADDO, ISD::USUBO,
  769. ISD::SMULO, ISD::UMULO},
  770. VT, Expand);
  771. // ADDCARRY operations default to expand
  772. setOperationAction({ISD::ADDCARRY, ISD::SUBCARRY, ISD::SETCCCARRY,
  773. ISD::SADDO_CARRY, ISD::SSUBO_CARRY},
  774. VT, Expand);
  775. // ADDC/ADDE/SUBC/SUBE default to expand.
  776. setOperationAction({ISD::ADDC, ISD::ADDE, ISD::SUBC, ISD::SUBE}, VT,
  777. Expand);
  778. // Halving adds
  779. setOperationAction(
  780. {ISD::AVGFLOORS, ISD::AVGFLOORU, ISD::AVGCEILS, ISD::AVGCEILU}, VT,
  781. Expand);
  782. // Absolute difference
  783. setOperationAction({ISD::ABDS, ISD::ABDU}, VT, Expand);
  784. // These default to Expand so they will be expanded to CTLZ/CTTZ by default.
  785. setOperationAction({ISD::CTLZ_ZERO_UNDEF, ISD::CTTZ_ZERO_UNDEF}, VT,
  786. Expand);
  787. setOperationAction({ISD::BITREVERSE, ISD::PARITY}, VT, Expand);
  788. // These library functions default to expand.
  789. setOperationAction({ISD::FROUND, ISD::FROUNDEVEN, ISD::FPOWI}, VT, Expand);
  790. // These operations default to expand for vector types.
  791. if (VT.isVector())
  792. setOperationAction({ISD::FCOPYSIGN, ISD::SIGN_EXTEND_INREG,
  793. ISD::ANY_EXTEND_VECTOR_INREG,
  794. ISD::SIGN_EXTEND_VECTOR_INREG,
  795. ISD::ZERO_EXTEND_VECTOR_INREG, ISD::SPLAT_VECTOR},
  796. VT, Expand);
  797. // Constrained floating-point operations default to expand.
  798. #define DAG_INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC, DAGN) \
  799. setOperationAction(ISD::STRICT_##DAGN, VT, Expand);
  800. #include "llvm/IR/ConstrainedOps.def"
  801. // For most targets @llvm.get.dynamic.area.offset just returns 0.
  802. setOperationAction(ISD::GET_DYNAMIC_AREA_OFFSET, VT, Expand);
  803. // Vector reduction default to expand.
  804. setOperationAction(
  805. {ISD::VECREDUCE_FADD, ISD::VECREDUCE_FMUL, ISD::VECREDUCE_ADD,
  806. ISD::VECREDUCE_MUL, ISD::VECREDUCE_AND, ISD::VECREDUCE_OR,
  807. ISD::VECREDUCE_XOR, ISD::VECREDUCE_SMAX, ISD::VECREDUCE_SMIN,
  808. ISD::VECREDUCE_UMAX, ISD::VECREDUCE_UMIN, ISD::VECREDUCE_FMAX,
  809. ISD::VECREDUCE_FMIN, ISD::VECREDUCE_SEQ_FADD, ISD::VECREDUCE_SEQ_FMUL},
  810. VT, Expand);
  811. // Named vector shuffles default to expand.
  812. setOperationAction(ISD::VECTOR_SPLICE, VT, Expand);
  813. // VP_SREM/UREM default to expand.
  814. // TODO: Expand all VP intrinsics.
  815. setOperationAction(ISD::VP_SREM, VT, Expand);
  816. setOperationAction(ISD::VP_UREM, VT, Expand);
  817. }
  818. // Most targets ignore the @llvm.prefetch intrinsic.
  819. setOperationAction(ISD::PREFETCH, MVT::Other, Expand);
  820. // Most targets also ignore the @llvm.readcyclecounter intrinsic.
  821. setOperationAction(ISD::READCYCLECOUNTER, MVT::i64, Expand);
  822. // ConstantFP nodes default to expand. Targets can either change this to
  823. // Legal, in which case all fp constants are legal, or use isFPImmLegal()
  824. // to optimize expansions for certain constants.
  825. setOperationAction(ISD::ConstantFP,
  826. {MVT::f16, MVT::f32, MVT::f64, MVT::f80, MVT::f128},
  827. Expand);
  828. // These library functions default to expand.
  829. setOperationAction({ISD::FCBRT, ISD::FLOG, ISD::FLOG2, ISD::FLOG10, ISD::FEXP,
  830. ISD::FEXP2, ISD::FFLOOR, ISD::FNEARBYINT, ISD::FCEIL,
  831. ISD::FRINT, ISD::FTRUNC, ISD::LROUND, ISD::LLROUND,
  832. ISD::LRINT, ISD::LLRINT},
  833. {MVT::f32, MVT::f64, MVT::f128}, Expand);
  834. // Default ISD::TRAP to expand (which turns it into abort).
  835. setOperationAction(ISD::TRAP, MVT::Other, Expand);
  836. // On most systems, DEBUGTRAP and TRAP have no difference. The "Expand"
  837. // here is to inform DAG Legalizer to replace DEBUGTRAP with TRAP.
  838. setOperationAction(ISD::DEBUGTRAP, MVT::Other, Expand);
  839. setOperationAction(ISD::UBSANTRAP, MVT::Other, Expand);
  840. }
  841. MVT TargetLoweringBase::getScalarShiftAmountTy(const DataLayout &DL,
  842. EVT) const {
  843. return MVT::getIntegerVT(DL.getPointerSizeInBits(0));
  844. }
  845. EVT TargetLoweringBase::getShiftAmountTy(EVT LHSTy, const DataLayout &DL,
  846. bool LegalTypes) const {
  847. assert(LHSTy.isInteger() && "Shift amount is not an integer type!");
  848. if (LHSTy.isVector())
  849. return LHSTy;
  850. MVT ShiftVT =
  851. LegalTypes ? getScalarShiftAmountTy(DL, LHSTy) : getPointerTy(DL);
  852. // If any possible shift value won't fit in the prefered type, just use
  853. // something safe. Assume it will be legalized when the shift is expanded.
  854. if (ShiftVT.getSizeInBits() < Log2_32_Ceil(LHSTy.getSizeInBits()))
  855. ShiftVT = MVT::i32;
  856. assert(ShiftVT.getSizeInBits() >= Log2_32_Ceil(LHSTy.getSizeInBits()) &&
  857. "ShiftVT is still too small!");
  858. return ShiftVT;
  859. }
  860. bool TargetLoweringBase::canOpTrap(unsigned Op, EVT VT) const {
  861. assert(isTypeLegal(VT));
  862. switch (Op) {
  863. default:
  864. return false;
  865. case ISD::SDIV:
  866. case ISD::UDIV:
  867. case ISD::SREM:
  868. case ISD::UREM:
  869. return true;
  870. }
  871. }
  872. bool TargetLoweringBase::isFreeAddrSpaceCast(unsigned SrcAS,
  873. unsigned DestAS) const {
  874. return TM.isNoopAddrSpaceCast(SrcAS, DestAS);
  875. }
  876. void TargetLoweringBase::setJumpIsExpensive(bool isExpensive) {
  877. // If the command-line option was specified, ignore this request.
  878. if (!JumpIsExpensiveOverride.getNumOccurrences())
  879. JumpIsExpensive = isExpensive;
  880. }
  881. TargetLoweringBase::LegalizeKind
  882. TargetLoweringBase::getTypeConversion(LLVMContext &Context, EVT VT) const {
  883. // If this is a simple type, use the ComputeRegisterProp mechanism.
  884. if (VT.isSimple()) {
  885. MVT SVT = VT.getSimpleVT();
  886. assert((unsigned)SVT.SimpleTy < std::size(TransformToType));
  887. MVT NVT = TransformToType[SVT.SimpleTy];
  888. LegalizeTypeAction LA = ValueTypeActions.getTypeAction(SVT);
  889. assert((LA == TypeLegal || LA == TypeSoftenFloat ||
  890. LA == TypeSoftPromoteHalf ||
  891. (NVT.isVector() ||
  892. ValueTypeActions.getTypeAction(NVT) != TypePromoteInteger)) &&
  893. "Promote may not follow Expand or Promote");
  894. if (LA == TypeSplitVector)
  895. return LegalizeKind(LA, EVT(SVT).getHalfNumVectorElementsVT(Context));
  896. if (LA == TypeScalarizeVector)
  897. return LegalizeKind(LA, SVT.getVectorElementType());
  898. return LegalizeKind(LA, NVT);
  899. }
  900. // Handle Extended Scalar Types.
  901. if (!VT.isVector()) {
  902. assert(VT.isInteger() && "Float types must be simple");
  903. unsigned BitSize = VT.getSizeInBits();
  904. // First promote to a power-of-two size, then expand if necessary.
  905. if (BitSize < 8 || !isPowerOf2_32(BitSize)) {
  906. EVT NVT = VT.getRoundIntegerType(Context);
  907. assert(NVT != VT && "Unable to round integer VT");
  908. LegalizeKind NextStep = getTypeConversion(Context, NVT);
  909. // Avoid multi-step promotion.
  910. if (NextStep.first == TypePromoteInteger)
  911. return NextStep;
  912. // Return rounded integer type.
  913. return LegalizeKind(TypePromoteInteger, NVT);
  914. }
  915. return LegalizeKind(TypeExpandInteger,
  916. EVT::getIntegerVT(Context, VT.getSizeInBits() / 2));
  917. }
  918. // Handle vector types.
  919. ElementCount NumElts = VT.getVectorElementCount();
  920. EVT EltVT = VT.getVectorElementType();
  921. // Vectors with only one element are always scalarized.
  922. if (NumElts.isScalar())
  923. return LegalizeKind(TypeScalarizeVector, EltVT);
  924. // Try to widen vector elements until the element type is a power of two and
  925. // promote it to a legal type later on, for example:
  926. // <3 x i8> -> <4 x i8> -> <4 x i32>
  927. if (EltVT.isInteger()) {
  928. // Vectors with a number of elements that is not a power of two are always
  929. // widened, for example <3 x i8> -> <4 x i8>.
  930. if (!VT.isPow2VectorType()) {
  931. NumElts = NumElts.coefficientNextPowerOf2();
  932. EVT NVT = EVT::getVectorVT(Context, EltVT, NumElts);
  933. return LegalizeKind(TypeWidenVector, NVT);
  934. }
  935. // Examine the element type.
  936. LegalizeKind LK = getTypeConversion(Context, EltVT);
  937. // If type is to be expanded, split the vector.
  938. // <4 x i140> -> <2 x i140>
  939. if (LK.first == TypeExpandInteger) {
  940. if (VT.getVectorElementCount().isScalable())
  941. return LegalizeKind(TypeScalarizeScalableVector, EltVT);
  942. return LegalizeKind(TypeSplitVector,
  943. VT.getHalfNumVectorElementsVT(Context));
  944. }
  945. // Promote the integer element types until a legal vector type is found
  946. // or until the element integer type is too big. If a legal type was not
  947. // found, fallback to the usual mechanism of widening/splitting the
  948. // vector.
  949. EVT OldEltVT = EltVT;
  950. while (true) {
  951. // Increase the bitwidth of the element to the next pow-of-two
  952. // (which is greater than 8 bits).
  953. EltVT = EVT::getIntegerVT(Context, 1 + EltVT.getSizeInBits())
  954. .getRoundIntegerType(Context);
  955. // Stop trying when getting a non-simple element type.
  956. // Note that vector elements may be greater than legal vector element
  957. // types. Example: X86 XMM registers hold 64bit element on 32bit
  958. // systems.
  959. if (!EltVT.isSimple())
  960. break;
  961. // Build a new vector type and check if it is legal.
  962. MVT NVT = MVT::getVectorVT(EltVT.getSimpleVT(), NumElts);
  963. // Found a legal promoted vector type.
  964. if (NVT != MVT() && ValueTypeActions.getTypeAction(NVT) == TypeLegal)
  965. return LegalizeKind(TypePromoteInteger,
  966. EVT::getVectorVT(Context, EltVT, NumElts));
  967. }
  968. // Reset the type to the unexpanded type if we did not find a legal vector
  969. // type with a promoted vector element type.
  970. EltVT = OldEltVT;
  971. }
  972. // Try to widen the vector until a legal type is found.
  973. // If there is no wider legal type, split the vector.
  974. while (true) {
  975. // Round up to the next power of 2.
  976. NumElts = NumElts.coefficientNextPowerOf2();
  977. // If there is no simple vector type with this many elements then there
  978. // cannot be a larger legal vector type. Note that this assumes that
  979. // there are no skipped intermediate vector types in the simple types.
  980. if (!EltVT.isSimple())
  981. break;
  982. MVT LargerVector = MVT::getVectorVT(EltVT.getSimpleVT(), NumElts);
  983. if (LargerVector == MVT())
  984. break;
  985. // If this type is legal then widen the vector.
  986. if (ValueTypeActions.getTypeAction(LargerVector) == TypeLegal)
  987. return LegalizeKind(TypeWidenVector, LargerVector);
  988. }
  989. // Widen odd vectors to next power of two.
  990. if (!VT.isPow2VectorType()) {
  991. EVT NVT = VT.getPow2VectorType(Context);
  992. return LegalizeKind(TypeWidenVector, NVT);
  993. }
  994. if (VT.getVectorElementCount() == ElementCount::getScalable(1))
  995. return LegalizeKind(TypeScalarizeScalableVector, EltVT);
  996. // Vectors with illegal element types are expanded.
  997. EVT NVT = EVT::getVectorVT(Context, EltVT,
  998. VT.getVectorElementCount().divideCoefficientBy(2));
  999. return LegalizeKind(TypeSplitVector, NVT);
  1000. }
  1001. static unsigned getVectorTypeBreakdownMVT(MVT VT, MVT &IntermediateVT,
  1002. unsigned &NumIntermediates,
  1003. MVT &RegisterVT,
  1004. TargetLoweringBase *TLI) {
  1005. // Figure out the right, legal destination reg to copy into.
  1006. ElementCount EC = VT.getVectorElementCount();
  1007. MVT EltTy = VT.getVectorElementType();
  1008. unsigned NumVectorRegs = 1;
  1009. // Scalable vectors cannot be scalarized, so splitting or widening is
  1010. // required.
  1011. if (VT.isScalableVector() && !isPowerOf2_32(EC.getKnownMinValue()))
  1012. llvm_unreachable(
  1013. "Splitting or widening of non-power-of-2 MVTs is not implemented.");
  1014. // FIXME: We don't support non-power-of-2-sized vectors for now.
  1015. // Ideally we could break down into LHS/RHS like LegalizeDAG does.
  1016. if (!isPowerOf2_32(EC.getKnownMinValue())) {
  1017. // Split EC to unit size (scalable property is preserved).
  1018. NumVectorRegs = EC.getKnownMinValue();
  1019. EC = ElementCount::getFixed(1);
  1020. }
  1021. // Divide the input until we get to a supported size. This will
  1022. // always end up with an EC that represent a scalar or a scalable
  1023. // scalar.
  1024. while (EC.getKnownMinValue() > 1 &&
  1025. !TLI->isTypeLegal(MVT::getVectorVT(EltTy, EC))) {
  1026. EC = EC.divideCoefficientBy(2);
  1027. NumVectorRegs <<= 1;
  1028. }
  1029. NumIntermediates = NumVectorRegs;
  1030. MVT NewVT = MVT::getVectorVT(EltTy, EC);
  1031. if (!TLI->isTypeLegal(NewVT))
  1032. NewVT = EltTy;
  1033. IntermediateVT = NewVT;
  1034. unsigned LaneSizeInBits = NewVT.getScalarSizeInBits();
  1035. // Convert sizes such as i33 to i64.
  1036. if (!isPowerOf2_32(LaneSizeInBits))
  1037. LaneSizeInBits = NextPowerOf2(LaneSizeInBits);
  1038. MVT DestVT = TLI->getRegisterType(NewVT);
  1039. RegisterVT = DestVT;
  1040. if (EVT(DestVT).bitsLT(NewVT)) // Value is expanded, e.g. i64 -> i16.
  1041. return NumVectorRegs * (LaneSizeInBits / DestVT.getScalarSizeInBits());
  1042. // Otherwise, promotion or legal types use the same number of registers as
  1043. // the vector decimated to the appropriate level.
  1044. return NumVectorRegs;
  1045. }
  1046. /// isLegalRC - Return true if the value types that can be represented by the
  1047. /// specified register class are all legal.
  1048. bool TargetLoweringBase::isLegalRC(const TargetRegisterInfo &TRI,
  1049. const TargetRegisterClass &RC) const {
  1050. for (const auto *I = TRI.legalclasstypes_begin(RC); *I != MVT::Other; ++I)
  1051. if (isTypeLegal(*I))
  1052. return true;
  1053. return false;
  1054. }
  1055. /// Replace/modify any TargetFrameIndex operands with a targte-dependent
  1056. /// sequence of memory operands that is recognized by PrologEpilogInserter.
  1057. MachineBasicBlock *
  1058. TargetLoweringBase::emitPatchPoint(MachineInstr &InitialMI,
  1059. MachineBasicBlock *MBB) const {
  1060. MachineInstr *MI = &InitialMI;
  1061. MachineFunction &MF = *MI->getMF();
  1062. MachineFrameInfo &MFI = MF.getFrameInfo();
  1063. // We're handling multiple types of operands here:
  1064. // PATCHPOINT MetaArgs - live-in, read only, direct
  1065. // STATEPOINT Deopt Spill - live-through, read only, indirect
  1066. // STATEPOINT Deopt Alloca - live-through, read only, direct
  1067. // (We're currently conservative and mark the deopt slots read/write in
  1068. // practice.)
  1069. // STATEPOINT GC Spill - live-through, read/write, indirect
  1070. // STATEPOINT GC Alloca - live-through, read/write, direct
  1071. // The live-in vs live-through is handled already (the live through ones are
  1072. // all stack slots), but we need to handle the different type of stackmap
  1073. // operands and memory effects here.
  1074. if (llvm::none_of(MI->operands(),
  1075. [](MachineOperand &Operand) { return Operand.isFI(); }))
  1076. return MBB;
  1077. MachineInstrBuilder MIB = BuildMI(MF, MI->getDebugLoc(), MI->getDesc());
  1078. // Inherit previous memory operands.
  1079. MIB.cloneMemRefs(*MI);
  1080. for (unsigned i = 0; i < MI->getNumOperands(); ++i) {
  1081. MachineOperand &MO = MI->getOperand(i);
  1082. if (!MO.isFI()) {
  1083. // Index of Def operand this Use it tied to.
  1084. // Since Defs are coming before Uses, if Use is tied, then
  1085. // index of Def must be smaller that index of that Use.
  1086. // Also, Defs preserve their position in new MI.
  1087. unsigned TiedTo = i;
  1088. if (MO.isReg() && MO.isTied())
  1089. TiedTo = MI->findTiedOperandIdx(i);
  1090. MIB.add(MO);
  1091. if (TiedTo < i)
  1092. MIB->tieOperands(TiedTo, MIB->getNumOperands() - 1);
  1093. continue;
  1094. }
  1095. // foldMemoryOperand builds a new MI after replacing a single FI operand
  1096. // with the canonical set of five x86 addressing-mode operands.
  1097. int FI = MO.getIndex();
  1098. // Add frame index operands recognized by stackmaps.cpp
  1099. if (MFI.isStatepointSpillSlotObjectIndex(FI)) {
  1100. // indirect-mem-ref tag, size, #FI, offset.
  1101. // Used for spills inserted by StatepointLowering. This codepath is not
  1102. // used for patchpoints/stackmaps at all, for these spilling is done via
  1103. // foldMemoryOperand callback only.
  1104. assert(MI->getOpcode() == TargetOpcode::STATEPOINT && "sanity");
  1105. MIB.addImm(StackMaps::IndirectMemRefOp);
  1106. MIB.addImm(MFI.getObjectSize(FI));
  1107. MIB.add(MO);
  1108. MIB.addImm(0);
  1109. } else {
  1110. // direct-mem-ref tag, #FI, offset.
  1111. // Used by patchpoint, and direct alloca arguments to statepoints
  1112. MIB.addImm(StackMaps::DirectMemRefOp);
  1113. MIB.add(MO);
  1114. MIB.addImm(0);
  1115. }
  1116. assert(MIB->mayLoad() && "Folded a stackmap use to a non-load!");
  1117. // Add a new memory operand for this FI.
  1118. assert(MFI.getObjectOffset(FI) != -1);
  1119. // Note: STATEPOINT MMOs are added during SelectionDAG. STACKMAP, and
  1120. // PATCHPOINT should be updated to do the same. (TODO)
  1121. if (MI->getOpcode() != TargetOpcode::STATEPOINT) {
  1122. auto Flags = MachineMemOperand::MOLoad;
  1123. MachineMemOperand *MMO = MF.getMachineMemOperand(
  1124. MachinePointerInfo::getFixedStack(MF, FI), Flags,
  1125. MF.getDataLayout().getPointerSize(), MFI.getObjectAlign(FI));
  1126. MIB->addMemOperand(MF, MMO);
  1127. }
  1128. }
  1129. MBB->insert(MachineBasicBlock::iterator(MI), MIB);
  1130. MI->eraseFromParent();
  1131. return MBB;
  1132. }
  1133. /// findRepresentativeClass - Return the largest legal super-reg register class
  1134. /// of the register class for the specified type and its associated "cost".
  1135. // This function is in TargetLowering because it uses RegClassForVT which would
  1136. // need to be moved to TargetRegisterInfo and would necessitate moving
  1137. // isTypeLegal over as well - a massive change that would just require
  1138. // TargetLowering having a TargetRegisterInfo class member that it would use.
  1139. std::pair<const TargetRegisterClass *, uint8_t>
  1140. TargetLoweringBase::findRepresentativeClass(const TargetRegisterInfo *TRI,
  1141. MVT VT) const {
  1142. const TargetRegisterClass *RC = RegClassForVT[VT.SimpleTy];
  1143. if (!RC)
  1144. return std::make_pair(RC, 0);
  1145. // Compute the set of all super-register classes.
  1146. BitVector SuperRegRC(TRI->getNumRegClasses());
  1147. for (SuperRegClassIterator RCI(RC, TRI); RCI.isValid(); ++RCI)
  1148. SuperRegRC.setBitsInMask(RCI.getMask());
  1149. // Find the first legal register class with the largest spill size.
  1150. const TargetRegisterClass *BestRC = RC;
  1151. for (unsigned i : SuperRegRC.set_bits()) {
  1152. const TargetRegisterClass *SuperRC = TRI->getRegClass(i);
  1153. // We want the largest possible spill size.
  1154. if (TRI->getSpillSize(*SuperRC) <= TRI->getSpillSize(*BestRC))
  1155. continue;
  1156. if (!isLegalRC(*TRI, *SuperRC))
  1157. continue;
  1158. BestRC = SuperRC;
  1159. }
  1160. return std::make_pair(BestRC, 1);
  1161. }
  1162. /// computeRegisterProperties - Once all of the register classes are added,
  1163. /// this allows us to compute derived properties we expose.
  1164. void TargetLoweringBase::computeRegisterProperties(
  1165. const TargetRegisterInfo *TRI) {
  1166. static_assert(MVT::VALUETYPE_SIZE <= MVT::MAX_ALLOWED_VALUETYPE,
  1167. "Too many value types for ValueTypeActions to hold!");
  1168. // Everything defaults to needing one register.
  1169. for (unsigned i = 0; i != MVT::VALUETYPE_SIZE; ++i) {
  1170. NumRegistersForVT[i] = 1;
  1171. RegisterTypeForVT[i] = TransformToType[i] = (MVT::SimpleValueType)i;
  1172. }
  1173. // ...except isVoid, which doesn't need any registers.
  1174. NumRegistersForVT[MVT::isVoid] = 0;
  1175. // Find the largest integer register class.
  1176. unsigned LargestIntReg = MVT::LAST_INTEGER_VALUETYPE;
  1177. for (; RegClassForVT[LargestIntReg] == nullptr; --LargestIntReg)
  1178. assert(LargestIntReg != MVT::i1 && "No integer registers defined!");
  1179. // Every integer value type larger than this largest register takes twice as
  1180. // many registers to represent as the previous ValueType.
  1181. for (unsigned ExpandedReg = LargestIntReg + 1;
  1182. ExpandedReg <= MVT::LAST_INTEGER_VALUETYPE; ++ExpandedReg) {
  1183. NumRegistersForVT[ExpandedReg] = 2*NumRegistersForVT[ExpandedReg-1];
  1184. RegisterTypeForVT[ExpandedReg] = (MVT::SimpleValueType)LargestIntReg;
  1185. TransformToType[ExpandedReg] = (MVT::SimpleValueType)(ExpandedReg - 1);
  1186. ValueTypeActions.setTypeAction((MVT::SimpleValueType)ExpandedReg,
  1187. TypeExpandInteger);
  1188. }
  1189. // Inspect all of the ValueType's smaller than the largest integer
  1190. // register to see which ones need promotion.
  1191. unsigned LegalIntReg = LargestIntReg;
  1192. for (unsigned IntReg = LargestIntReg - 1;
  1193. IntReg >= (unsigned)MVT::i1; --IntReg) {
  1194. MVT IVT = (MVT::SimpleValueType)IntReg;
  1195. if (isTypeLegal(IVT)) {
  1196. LegalIntReg = IntReg;
  1197. } else {
  1198. RegisterTypeForVT[IntReg] = TransformToType[IntReg] =
  1199. (MVT::SimpleValueType)LegalIntReg;
  1200. ValueTypeActions.setTypeAction(IVT, TypePromoteInteger);
  1201. }
  1202. }
  1203. // ppcf128 type is really two f64's.
  1204. if (!isTypeLegal(MVT::ppcf128)) {
  1205. if (isTypeLegal(MVT::f64)) {
  1206. NumRegistersForVT[MVT::ppcf128] = 2*NumRegistersForVT[MVT::f64];
  1207. RegisterTypeForVT[MVT::ppcf128] = MVT::f64;
  1208. TransformToType[MVT::ppcf128] = MVT::f64;
  1209. ValueTypeActions.setTypeAction(MVT::ppcf128, TypeExpandFloat);
  1210. } else {
  1211. NumRegistersForVT[MVT::ppcf128] = NumRegistersForVT[MVT::i128];
  1212. RegisterTypeForVT[MVT::ppcf128] = RegisterTypeForVT[MVT::i128];
  1213. TransformToType[MVT::ppcf128] = MVT::i128;
  1214. ValueTypeActions.setTypeAction(MVT::ppcf128, TypeSoftenFloat);
  1215. }
  1216. }
  1217. // Decide how to handle f128. If the target does not have native f128 support,
  1218. // expand it to i128 and we will be generating soft float library calls.
  1219. if (!isTypeLegal(MVT::f128)) {
  1220. NumRegistersForVT[MVT::f128] = NumRegistersForVT[MVT::i128];
  1221. RegisterTypeForVT[MVT::f128] = RegisterTypeForVT[MVT::i128];
  1222. TransformToType[MVT::f128] = MVT::i128;
  1223. ValueTypeActions.setTypeAction(MVT::f128, TypeSoftenFloat);
  1224. }
  1225. // Decide how to handle f80. If the target does not have native f80 support,
  1226. // expand it to i96 and we will be generating soft float library calls.
  1227. if (!isTypeLegal(MVT::f80)) {
  1228. NumRegistersForVT[MVT::f80] = 3*NumRegistersForVT[MVT::i32];
  1229. RegisterTypeForVT[MVT::f80] = RegisterTypeForVT[MVT::i32];
  1230. TransformToType[MVT::f80] = MVT::i32;
  1231. ValueTypeActions.setTypeAction(MVT::f80, TypeSoftenFloat);
  1232. }
  1233. // Decide how to handle f64. If the target does not have native f64 support,
  1234. // expand it to i64 and we will be generating soft float library calls.
  1235. if (!isTypeLegal(MVT::f64)) {
  1236. NumRegistersForVT[MVT::f64] = NumRegistersForVT[MVT::i64];
  1237. RegisterTypeForVT[MVT::f64] = RegisterTypeForVT[MVT::i64];
  1238. TransformToType[MVT::f64] = MVT::i64;
  1239. ValueTypeActions.setTypeAction(MVT::f64, TypeSoftenFloat);
  1240. }
  1241. // Decide how to handle f32. If the target does not have native f32 support,
  1242. // expand it to i32 and we will be generating soft float library calls.
  1243. if (!isTypeLegal(MVT::f32)) {
  1244. NumRegistersForVT[MVT::f32] = NumRegistersForVT[MVT::i32];
  1245. RegisterTypeForVT[MVT::f32] = RegisterTypeForVT[MVT::i32];
  1246. TransformToType[MVT::f32] = MVT::i32;
  1247. ValueTypeActions.setTypeAction(MVT::f32, TypeSoftenFloat);
  1248. }
  1249. // Decide how to handle f16. If the target does not have native f16 support,
  1250. // promote it to f32, because there are no f16 library calls (except for
  1251. // conversions).
  1252. if (!isTypeLegal(MVT::f16)) {
  1253. // Allow targets to control how we legalize half.
  1254. if (softPromoteHalfType()) {
  1255. NumRegistersForVT[MVT::f16] = NumRegistersForVT[MVT::i16];
  1256. RegisterTypeForVT[MVT::f16] = RegisterTypeForVT[MVT::i16];
  1257. TransformToType[MVT::f16] = MVT::f32;
  1258. ValueTypeActions.setTypeAction(MVT::f16, TypeSoftPromoteHalf);
  1259. } else {
  1260. NumRegistersForVT[MVT::f16] = NumRegistersForVT[MVT::f32];
  1261. RegisterTypeForVT[MVT::f16] = RegisterTypeForVT[MVT::f32];
  1262. TransformToType[MVT::f16] = MVT::f32;
  1263. ValueTypeActions.setTypeAction(MVT::f16, TypePromoteFloat);
  1264. }
  1265. }
  1266. // Decide how to handle bf16. If the target does not have native bf16 support,
  1267. // promote it to f32, because there are no bf16 library calls (except for
  1268. // converting from f32 to bf16).
  1269. if (!isTypeLegal(MVT::bf16)) {
  1270. NumRegistersForVT[MVT::bf16] = NumRegistersForVT[MVT::f32];
  1271. RegisterTypeForVT[MVT::bf16] = RegisterTypeForVT[MVT::f32];
  1272. TransformToType[MVT::bf16] = MVT::f32;
  1273. ValueTypeActions.setTypeAction(MVT::bf16, TypeSoftPromoteHalf);
  1274. }
  1275. // Loop over all of the vector value types to see which need transformations.
  1276. for (unsigned i = MVT::FIRST_VECTOR_VALUETYPE;
  1277. i <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++i) {
  1278. MVT VT = (MVT::SimpleValueType) i;
  1279. if (isTypeLegal(VT))
  1280. continue;
  1281. MVT EltVT = VT.getVectorElementType();
  1282. ElementCount EC = VT.getVectorElementCount();
  1283. bool IsLegalWiderType = false;
  1284. bool IsScalable = VT.isScalableVector();
  1285. LegalizeTypeAction PreferredAction = getPreferredVectorAction(VT);
  1286. switch (PreferredAction) {
  1287. case TypePromoteInteger: {
  1288. MVT::SimpleValueType EndVT = IsScalable ?
  1289. MVT::LAST_INTEGER_SCALABLE_VECTOR_VALUETYPE :
  1290. MVT::LAST_INTEGER_FIXEDLEN_VECTOR_VALUETYPE;
  1291. // Try to promote the elements of integer vectors. If no legal
  1292. // promotion was found, fall through to the widen-vector method.
  1293. for (unsigned nVT = i + 1;
  1294. (MVT::SimpleValueType)nVT <= EndVT; ++nVT) {
  1295. MVT SVT = (MVT::SimpleValueType) nVT;
  1296. // Promote vectors of integers to vectors with the same number
  1297. // of elements, with a wider element type.
  1298. if (SVT.getScalarSizeInBits() > EltVT.getFixedSizeInBits() &&
  1299. SVT.getVectorElementCount() == EC && isTypeLegal(SVT)) {
  1300. TransformToType[i] = SVT;
  1301. RegisterTypeForVT[i] = SVT;
  1302. NumRegistersForVT[i] = 1;
  1303. ValueTypeActions.setTypeAction(VT, TypePromoteInteger);
  1304. IsLegalWiderType = true;
  1305. break;
  1306. }
  1307. }
  1308. if (IsLegalWiderType)
  1309. break;
  1310. [[fallthrough]];
  1311. }
  1312. case TypeWidenVector:
  1313. if (isPowerOf2_32(EC.getKnownMinValue())) {
  1314. // Try to widen the vector.
  1315. for (unsigned nVT = i + 1; nVT <= MVT::LAST_VECTOR_VALUETYPE; ++nVT) {
  1316. MVT SVT = (MVT::SimpleValueType) nVT;
  1317. if (SVT.getVectorElementType() == EltVT &&
  1318. SVT.isScalableVector() == IsScalable &&
  1319. SVT.getVectorElementCount().getKnownMinValue() >
  1320. EC.getKnownMinValue() &&
  1321. isTypeLegal(SVT)) {
  1322. TransformToType[i] = SVT;
  1323. RegisterTypeForVT[i] = SVT;
  1324. NumRegistersForVT[i] = 1;
  1325. ValueTypeActions.setTypeAction(VT, TypeWidenVector);
  1326. IsLegalWiderType = true;
  1327. break;
  1328. }
  1329. }
  1330. if (IsLegalWiderType)
  1331. break;
  1332. } else {
  1333. // Only widen to the next power of 2 to keep consistency with EVT.
  1334. MVT NVT = VT.getPow2VectorType();
  1335. if (isTypeLegal(NVT)) {
  1336. TransformToType[i] = NVT;
  1337. ValueTypeActions.setTypeAction(VT, TypeWidenVector);
  1338. RegisterTypeForVT[i] = NVT;
  1339. NumRegistersForVT[i] = 1;
  1340. break;
  1341. }
  1342. }
  1343. [[fallthrough]];
  1344. case TypeSplitVector:
  1345. case TypeScalarizeVector: {
  1346. MVT IntermediateVT;
  1347. MVT RegisterVT;
  1348. unsigned NumIntermediates;
  1349. unsigned NumRegisters = getVectorTypeBreakdownMVT(VT, IntermediateVT,
  1350. NumIntermediates, RegisterVT, this);
  1351. NumRegistersForVT[i] = NumRegisters;
  1352. assert(NumRegistersForVT[i] == NumRegisters &&
  1353. "NumRegistersForVT size cannot represent NumRegisters!");
  1354. RegisterTypeForVT[i] = RegisterVT;
  1355. MVT NVT = VT.getPow2VectorType();
  1356. if (NVT == VT) {
  1357. // Type is already a power of 2. The default action is to split.
  1358. TransformToType[i] = MVT::Other;
  1359. if (PreferredAction == TypeScalarizeVector)
  1360. ValueTypeActions.setTypeAction(VT, TypeScalarizeVector);
  1361. else if (PreferredAction == TypeSplitVector)
  1362. ValueTypeActions.setTypeAction(VT, TypeSplitVector);
  1363. else if (EC.getKnownMinValue() > 1)
  1364. ValueTypeActions.setTypeAction(VT, TypeSplitVector);
  1365. else
  1366. ValueTypeActions.setTypeAction(VT, EC.isScalable()
  1367. ? TypeScalarizeScalableVector
  1368. : TypeScalarizeVector);
  1369. } else {
  1370. TransformToType[i] = NVT;
  1371. ValueTypeActions.setTypeAction(VT, TypeWidenVector);
  1372. }
  1373. break;
  1374. }
  1375. default:
  1376. llvm_unreachable("Unknown vector legalization action!");
  1377. }
  1378. }
  1379. // Determine the 'representative' register class for each value type.
  1380. // An representative register class is the largest (meaning one which is
  1381. // not a sub-register class / subreg register class) legal register class for
  1382. // a group of value types. For example, on i386, i8, i16, and i32
  1383. // representative would be GR32; while on x86_64 it's GR64.
  1384. for (unsigned i = 0; i != MVT::VALUETYPE_SIZE; ++i) {
  1385. const TargetRegisterClass* RRC;
  1386. uint8_t Cost;
  1387. std::tie(RRC, Cost) = findRepresentativeClass(TRI, (MVT::SimpleValueType)i);
  1388. RepRegClassForVT[i] = RRC;
  1389. RepRegClassCostForVT[i] = Cost;
  1390. }
  1391. }
  1392. EVT TargetLoweringBase::getSetCCResultType(const DataLayout &DL, LLVMContext &,
  1393. EVT VT) const {
  1394. assert(!VT.isVector() && "No default SetCC type for vectors!");
  1395. return getPointerTy(DL).SimpleTy;
  1396. }
  1397. MVT::SimpleValueType TargetLoweringBase::getCmpLibcallReturnType() const {
  1398. return MVT::i32; // return the default value
  1399. }
  1400. /// getVectorTypeBreakdown - Vector types are broken down into some number of
  1401. /// legal first class types. For example, MVT::v8f32 maps to 2 MVT::v4f32
  1402. /// with Altivec or SSE1, or 8 promoted MVT::f64 values with the X86 FP stack.
  1403. /// Similarly, MVT::v2i64 turns into 4 MVT::i32 values with both PPC and X86.
  1404. ///
  1405. /// This method returns the number of registers needed, and the VT for each
  1406. /// register. It also returns the VT and quantity of the intermediate values
  1407. /// before they are promoted/expanded.
  1408. unsigned TargetLoweringBase::getVectorTypeBreakdown(LLVMContext &Context,
  1409. EVT VT, EVT &IntermediateVT,
  1410. unsigned &NumIntermediates,
  1411. MVT &RegisterVT) const {
  1412. ElementCount EltCnt = VT.getVectorElementCount();
  1413. // If there is a wider vector type with the same element type as this one,
  1414. // or a promoted vector type that has the same number of elements which
  1415. // are wider, then we should convert to that legal vector type.
  1416. // This handles things like <2 x float> -> <4 x float> and
  1417. // <4 x i1> -> <4 x i32>.
  1418. LegalizeTypeAction TA = getTypeAction(Context, VT);
  1419. if (!EltCnt.isScalar() &&
  1420. (TA == TypeWidenVector || TA == TypePromoteInteger)) {
  1421. EVT RegisterEVT = getTypeToTransformTo(Context, VT);
  1422. if (isTypeLegal(RegisterEVT)) {
  1423. IntermediateVT = RegisterEVT;
  1424. RegisterVT = RegisterEVT.getSimpleVT();
  1425. NumIntermediates = 1;
  1426. return 1;
  1427. }
  1428. }
  1429. // Figure out the right, legal destination reg to copy into.
  1430. EVT EltTy = VT.getVectorElementType();
  1431. unsigned NumVectorRegs = 1;
  1432. // Scalable vectors cannot be scalarized, so handle the legalisation of the
  1433. // types like done elsewhere in SelectionDAG.
  1434. if (EltCnt.isScalable()) {
  1435. LegalizeKind LK;
  1436. EVT PartVT = VT;
  1437. do {
  1438. // Iterate until we've found a legal (part) type to hold VT.
  1439. LK = getTypeConversion(Context, PartVT);
  1440. PartVT = LK.second;
  1441. } while (LK.first != TypeLegal);
  1442. if (!PartVT.isVector()) {
  1443. report_fatal_error(
  1444. "Don't know how to legalize this scalable vector type");
  1445. }
  1446. NumIntermediates =
  1447. divideCeil(VT.getVectorElementCount().getKnownMinValue(),
  1448. PartVT.getVectorElementCount().getKnownMinValue());
  1449. IntermediateVT = PartVT;
  1450. RegisterVT = getRegisterType(Context, IntermediateVT);
  1451. return NumIntermediates;
  1452. }
  1453. // FIXME: We don't support non-power-of-2-sized vectors for now. Ideally
  1454. // we could break down into LHS/RHS like LegalizeDAG does.
  1455. if (!isPowerOf2_32(EltCnt.getKnownMinValue())) {
  1456. NumVectorRegs = EltCnt.getKnownMinValue();
  1457. EltCnt = ElementCount::getFixed(1);
  1458. }
  1459. // Divide the input until we get to a supported size. This will always
  1460. // end with a scalar if the target doesn't support vectors.
  1461. while (EltCnt.getKnownMinValue() > 1 &&
  1462. !isTypeLegal(EVT::getVectorVT(Context, EltTy, EltCnt))) {
  1463. EltCnt = EltCnt.divideCoefficientBy(2);
  1464. NumVectorRegs <<= 1;
  1465. }
  1466. NumIntermediates = NumVectorRegs;
  1467. EVT NewVT = EVT::getVectorVT(Context, EltTy, EltCnt);
  1468. if (!isTypeLegal(NewVT))
  1469. NewVT = EltTy;
  1470. IntermediateVT = NewVT;
  1471. MVT DestVT = getRegisterType(Context, NewVT);
  1472. RegisterVT = DestVT;
  1473. if (EVT(DestVT).bitsLT(NewVT)) { // Value is expanded, e.g. i64 -> i16.
  1474. TypeSize NewVTSize = NewVT.getSizeInBits();
  1475. // Convert sizes such as i33 to i64.
  1476. if (!isPowerOf2_32(NewVTSize.getKnownMinValue()))
  1477. NewVTSize = NewVTSize.coefficientNextPowerOf2();
  1478. return NumVectorRegs*(NewVTSize/DestVT.getSizeInBits());
  1479. }
  1480. // Otherwise, promotion or legal types use the same number of registers as
  1481. // the vector decimated to the appropriate level.
  1482. return NumVectorRegs;
  1483. }
  1484. bool TargetLoweringBase::isSuitableForJumpTable(const SwitchInst *SI,
  1485. uint64_t NumCases,
  1486. uint64_t Range,
  1487. ProfileSummaryInfo *PSI,
  1488. BlockFrequencyInfo *BFI) const {
  1489. // FIXME: This function check the maximum table size and density, but the
  1490. // minimum size is not checked. It would be nice if the minimum size is
  1491. // also combined within this function. Currently, the minimum size check is
  1492. // performed in findJumpTable() in SelectionDAGBuiler and
  1493. // getEstimatedNumberOfCaseClusters() in BasicTTIImpl.
  1494. const bool OptForSize =
  1495. SI->getParent()->getParent()->hasOptSize() ||
  1496. llvm::shouldOptimizeForSize(SI->getParent(), PSI, BFI);
  1497. const unsigned MinDensity = getMinimumJumpTableDensity(OptForSize);
  1498. const unsigned MaxJumpTableSize = getMaximumJumpTableSize();
  1499. // Check whether the number of cases is small enough and
  1500. // the range is dense enough for a jump table.
  1501. return (OptForSize || Range <= MaxJumpTableSize) &&
  1502. (NumCases * 100 >= Range * MinDensity);
  1503. }
  1504. MVT TargetLoweringBase::getPreferredSwitchConditionType(LLVMContext &Context,
  1505. EVT ConditionVT) const {
  1506. return getRegisterType(Context, ConditionVT);
  1507. }
  1508. /// Get the EVTs and ArgFlags collections that represent the legalized return
  1509. /// type of the given function. This does not require a DAG or a return value,
  1510. /// and is suitable for use before any DAGs for the function are constructed.
  1511. /// TODO: Move this out of TargetLowering.cpp.
  1512. void llvm::GetReturnInfo(CallingConv::ID CC, Type *ReturnType,
  1513. AttributeList attr,
  1514. SmallVectorImpl<ISD::OutputArg> &Outs,
  1515. const TargetLowering &TLI, const DataLayout &DL) {
  1516. SmallVector<EVT, 4> ValueVTs;
  1517. ComputeValueVTs(TLI, DL, ReturnType, ValueVTs);
  1518. unsigned NumValues = ValueVTs.size();
  1519. if (NumValues == 0) return;
  1520. for (unsigned j = 0, f = NumValues; j != f; ++j) {
  1521. EVT VT = ValueVTs[j];
  1522. ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
  1523. if (attr.hasRetAttr(Attribute::SExt))
  1524. ExtendKind = ISD::SIGN_EXTEND;
  1525. else if (attr.hasRetAttr(Attribute::ZExt))
  1526. ExtendKind = ISD::ZERO_EXTEND;
  1527. // FIXME: C calling convention requires the return type to be promoted to
  1528. // at least 32-bit. But this is not necessary for non-C calling
  1529. // conventions. The frontend should mark functions whose return values
  1530. // require promoting with signext or zeroext attributes.
  1531. if (ExtendKind != ISD::ANY_EXTEND && VT.isInteger()) {
  1532. MVT MinVT = TLI.getRegisterType(ReturnType->getContext(), MVT::i32);
  1533. if (VT.bitsLT(MinVT))
  1534. VT = MinVT;
  1535. }
  1536. unsigned NumParts =
  1537. TLI.getNumRegistersForCallingConv(ReturnType->getContext(), CC, VT);
  1538. MVT PartVT =
  1539. TLI.getRegisterTypeForCallingConv(ReturnType->getContext(), CC, VT);
  1540. // 'inreg' on function refers to return value
  1541. ISD::ArgFlagsTy Flags = ISD::ArgFlagsTy();
  1542. if (attr.hasRetAttr(Attribute::InReg))
  1543. Flags.setInReg();
  1544. // Propagate extension type if any
  1545. if (attr.hasRetAttr(Attribute::SExt))
  1546. Flags.setSExt();
  1547. else if (attr.hasRetAttr(Attribute::ZExt))
  1548. Flags.setZExt();
  1549. for (unsigned i = 0; i < NumParts; ++i)
  1550. Outs.push_back(ISD::OutputArg(Flags, PartVT, VT, /*isfixed=*/true, 0, 0));
  1551. }
  1552. }
  1553. /// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
  1554. /// function arguments in the caller parameter area. This is the actual
  1555. /// alignment, not its logarithm.
  1556. uint64_t TargetLoweringBase::getByValTypeAlignment(Type *Ty,
  1557. const DataLayout &DL) const {
  1558. return DL.getABITypeAlign(Ty).value();
  1559. }
  1560. bool TargetLoweringBase::allowsMemoryAccessForAlignment(
  1561. LLVMContext &Context, const DataLayout &DL, EVT VT, unsigned AddrSpace,
  1562. Align Alignment, MachineMemOperand::Flags Flags, unsigned *Fast) const {
  1563. // Check if the specified alignment is sufficient based on the data layout.
  1564. // TODO: While using the data layout works in practice, a better solution
  1565. // would be to implement this check directly (make this a virtual function).
  1566. // For example, the ABI alignment may change based on software platform while
  1567. // this function should only be affected by hardware implementation.
  1568. Type *Ty = VT.getTypeForEVT(Context);
  1569. if (VT.isZeroSized() || Alignment >= DL.getABITypeAlign(Ty)) {
  1570. // Assume that an access that meets the ABI-specified alignment is fast.
  1571. if (Fast != nullptr)
  1572. *Fast = 1;
  1573. return true;
  1574. }
  1575. // This is a misaligned access.
  1576. return allowsMisalignedMemoryAccesses(VT, AddrSpace, Alignment, Flags, Fast);
  1577. }
  1578. bool TargetLoweringBase::allowsMemoryAccessForAlignment(
  1579. LLVMContext &Context, const DataLayout &DL, EVT VT,
  1580. const MachineMemOperand &MMO, unsigned *Fast) const {
  1581. return allowsMemoryAccessForAlignment(Context, DL, VT, MMO.getAddrSpace(),
  1582. MMO.getAlign(), MMO.getFlags(), Fast);
  1583. }
  1584. bool TargetLoweringBase::allowsMemoryAccess(LLVMContext &Context,
  1585. const DataLayout &DL, EVT VT,
  1586. unsigned AddrSpace, Align Alignment,
  1587. MachineMemOperand::Flags Flags,
  1588. unsigned *Fast) const {
  1589. return allowsMemoryAccessForAlignment(Context, DL, VT, AddrSpace, Alignment,
  1590. Flags, Fast);
  1591. }
  1592. bool TargetLoweringBase::allowsMemoryAccess(LLVMContext &Context,
  1593. const DataLayout &DL, EVT VT,
  1594. const MachineMemOperand &MMO,
  1595. unsigned *Fast) const {
  1596. return allowsMemoryAccess(Context, DL, VT, MMO.getAddrSpace(), MMO.getAlign(),
  1597. MMO.getFlags(), Fast);
  1598. }
  1599. bool TargetLoweringBase::allowsMemoryAccess(LLVMContext &Context,
  1600. const DataLayout &DL, LLT Ty,
  1601. const MachineMemOperand &MMO,
  1602. unsigned *Fast) const {
  1603. EVT VT = getApproximateEVTForLLT(Ty, DL, Context);
  1604. return allowsMemoryAccess(Context, DL, VT, MMO.getAddrSpace(), MMO.getAlign(),
  1605. MMO.getFlags(), Fast);
  1606. }
  1607. //===----------------------------------------------------------------------===//
  1608. // TargetTransformInfo Helpers
  1609. //===----------------------------------------------------------------------===//
  1610. int TargetLoweringBase::InstructionOpcodeToISD(unsigned Opcode) const {
  1611. enum InstructionOpcodes {
  1612. #define HANDLE_INST(NUM, OPCODE, CLASS) OPCODE = NUM,
  1613. #define LAST_OTHER_INST(NUM) InstructionOpcodesCount = NUM
  1614. #include "llvm/IR/Instruction.def"
  1615. };
  1616. switch (static_cast<InstructionOpcodes>(Opcode)) {
  1617. case Ret: return 0;
  1618. case Br: return 0;
  1619. case Switch: return 0;
  1620. case IndirectBr: return 0;
  1621. case Invoke: return 0;
  1622. case CallBr: return 0;
  1623. case Resume: return 0;
  1624. case Unreachable: return 0;
  1625. case CleanupRet: return 0;
  1626. case CatchRet: return 0;
  1627. case CatchPad: return 0;
  1628. case CatchSwitch: return 0;
  1629. case CleanupPad: return 0;
  1630. case FNeg: return ISD::FNEG;
  1631. case Add: return ISD::ADD;
  1632. case FAdd: return ISD::FADD;
  1633. case Sub: return ISD::SUB;
  1634. case FSub: return ISD::FSUB;
  1635. case Mul: return ISD::MUL;
  1636. case FMul: return ISD::FMUL;
  1637. case UDiv: return ISD::UDIV;
  1638. case SDiv: return ISD::SDIV;
  1639. case FDiv: return ISD::FDIV;
  1640. case URem: return ISD::UREM;
  1641. case SRem: return ISD::SREM;
  1642. case FRem: return ISD::FREM;
  1643. case Shl: return ISD::SHL;
  1644. case LShr: return ISD::SRL;
  1645. case AShr: return ISD::SRA;
  1646. case And: return ISD::AND;
  1647. case Or: return ISD::OR;
  1648. case Xor: return ISD::XOR;
  1649. case Alloca: return 0;
  1650. case Load: return ISD::LOAD;
  1651. case Store: return ISD::STORE;
  1652. case GetElementPtr: return 0;
  1653. case Fence: return 0;
  1654. case AtomicCmpXchg: return 0;
  1655. case AtomicRMW: return 0;
  1656. case Trunc: return ISD::TRUNCATE;
  1657. case ZExt: return ISD::ZERO_EXTEND;
  1658. case SExt: return ISD::SIGN_EXTEND;
  1659. case FPToUI: return ISD::FP_TO_UINT;
  1660. case FPToSI: return ISD::FP_TO_SINT;
  1661. case UIToFP: return ISD::UINT_TO_FP;
  1662. case SIToFP: return ISD::SINT_TO_FP;
  1663. case FPTrunc: return ISD::FP_ROUND;
  1664. case FPExt: return ISD::FP_EXTEND;
  1665. case PtrToInt: return ISD::BITCAST;
  1666. case IntToPtr: return ISD::BITCAST;
  1667. case BitCast: return ISD::BITCAST;
  1668. case AddrSpaceCast: return ISD::ADDRSPACECAST;
  1669. case ICmp: return ISD::SETCC;
  1670. case FCmp: return ISD::SETCC;
  1671. case PHI: return 0;
  1672. case Call: return 0;
  1673. case Select: return ISD::SELECT;
  1674. case UserOp1: return 0;
  1675. case UserOp2: return 0;
  1676. case VAArg: return 0;
  1677. case ExtractElement: return ISD::EXTRACT_VECTOR_ELT;
  1678. case InsertElement: return ISD::INSERT_VECTOR_ELT;
  1679. case ShuffleVector: return ISD::VECTOR_SHUFFLE;
  1680. case ExtractValue: return ISD::MERGE_VALUES;
  1681. case InsertValue: return ISD::MERGE_VALUES;
  1682. case LandingPad: return 0;
  1683. case Freeze: return ISD::FREEZE;
  1684. }
  1685. llvm_unreachable("Unknown instruction type encountered!");
  1686. }
  1687. Value *
  1688. TargetLoweringBase::getDefaultSafeStackPointerLocation(IRBuilderBase &IRB,
  1689. bool UseTLS) const {
  1690. // compiler-rt provides a variable with a magic name. Targets that do not
  1691. // link with compiler-rt may also provide such a variable.
  1692. Module *M = IRB.GetInsertBlock()->getParent()->getParent();
  1693. const char *UnsafeStackPtrVar = "__safestack_unsafe_stack_ptr";
  1694. auto UnsafeStackPtr =
  1695. dyn_cast_or_null<GlobalVariable>(M->getNamedValue(UnsafeStackPtrVar));
  1696. Type *StackPtrTy = Type::getInt8PtrTy(M->getContext());
  1697. if (!UnsafeStackPtr) {
  1698. auto TLSModel = UseTLS ?
  1699. GlobalValue::InitialExecTLSModel :
  1700. GlobalValue::NotThreadLocal;
  1701. // The global variable is not defined yet, define it ourselves.
  1702. // We use the initial-exec TLS model because we do not support the
  1703. // variable living anywhere other than in the main executable.
  1704. UnsafeStackPtr = new GlobalVariable(
  1705. *M, StackPtrTy, false, GlobalValue::ExternalLinkage, nullptr,
  1706. UnsafeStackPtrVar, nullptr, TLSModel);
  1707. } else {
  1708. // The variable exists, check its type and attributes.
  1709. if (UnsafeStackPtr->getValueType() != StackPtrTy)
  1710. report_fatal_error(Twine(UnsafeStackPtrVar) + " must have void* type");
  1711. if (UseTLS != UnsafeStackPtr->isThreadLocal())
  1712. report_fatal_error(Twine(UnsafeStackPtrVar) + " must " +
  1713. (UseTLS ? "" : "not ") + "be thread-local");
  1714. }
  1715. return UnsafeStackPtr;
  1716. }
  1717. Value *
  1718. TargetLoweringBase::getSafeStackPointerLocation(IRBuilderBase &IRB) const {
  1719. if (!TM.getTargetTriple().isAndroid())
  1720. return getDefaultSafeStackPointerLocation(IRB, true);
  1721. // Android provides a libc function to retrieve the address of the current
  1722. // thread's unsafe stack pointer.
  1723. Module *M = IRB.GetInsertBlock()->getParent()->getParent();
  1724. Type *StackPtrTy = Type::getInt8PtrTy(M->getContext());
  1725. FunctionCallee Fn = M->getOrInsertFunction("__safestack_pointer_address",
  1726. StackPtrTy->getPointerTo(0));
  1727. return IRB.CreateCall(Fn);
  1728. }
  1729. //===----------------------------------------------------------------------===//
  1730. // Loop Strength Reduction hooks
  1731. //===----------------------------------------------------------------------===//
  1732. /// isLegalAddressingMode - Return true if the addressing mode represented
  1733. /// by AM is legal for this target, for a load/store of the specified type.
  1734. bool TargetLoweringBase::isLegalAddressingMode(const DataLayout &DL,
  1735. const AddrMode &AM, Type *Ty,
  1736. unsigned AS, Instruction *I) const {
  1737. // The default implementation of this implements a conservative RISCy, r+r and
  1738. // r+i addr mode.
  1739. // Allows a sign-extended 16-bit immediate field.
  1740. if (AM.BaseOffs <= -(1LL << 16) || AM.BaseOffs >= (1LL << 16)-1)
  1741. return false;
  1742. // No global is ever allowed as a base.
  1743. if (AM.BaseGV)
  1744. return false;
  1745. // Only support r+r,
  1746. switch (AM.Scale) {
  1747. case 0: // "r+i" or just "i", depending on HasBaseReg.
  1748. break;
  1749. case 1:
  1750. if (AM.HasBaseReg && AM.BaseOffs) // "r+r+i" is not allowed.
  1751. return false;
  1752. // Otherwise we have r+r or r+i.
  1753. break;
  1754. case 2:
  1755. if (AM.HasBaseReg || AM.BaseOffs) // 2*r+r or 2*r+i is not allowed.
  1756. return false;
  1757. // Allow 2*r as r+r.
  1758. break;
  1759. default: // Don't allow n * r
  1760. return false;
  1761. }
  1762. return true;
  1763. }
  1764. //===----------------------------------------------------------------------===//
  1765. // Stack Protector
  1766. //===----------------------------------------------------------------------===//
  1767. // For OpenBSD return its special guard variable. Otherwise return nullptr,
  1768. // so that SelectionDAG handle SSP.
  1769. Value *TargetLoweringBase::getIRStackGuard(IRBuilderBase &IRB) const {
  1770. if (getTargetMachine().getTargetTriple().isOSOpenBSD()) {
  1771. Module &M = *IRB.GetInsertBlock()->getParent()->getParent();
  1772. PointerType *PtrTy = Type::getInt8PtrTy(M.getContext());
  1773. Constant *C = M.getOrInsertGlobal("__guard_local", PtrTy);
  1774. if (GlobalVariable *G = dyn_cast_or_null<GlobalVariable>(C))
  1775. G->setVisibility(GlobalValue::HiddenVisibility);
  1776. return C;
  1777. }
  1778. return nullptr;
  1779. }
  1780. // Currently only support "standard" __stack_chk_guard.
  1781. // TODO: add LOAD_STACK_GUARD support.
  1782. void TargetLoweringBase::insertSSPDeclarations(Module &M) const {
  1783. if (!M.getNamedValue("__stack_chk_guard")) {
  1784. auto *GV = new GlobalVariable(M, Type::getInt8PtrTy(M.getContext()), false,
  1785. GlobalVariable::ExternalLinkage, nullptr,
  1786. "__stack_chk_guard");
  1787. // FreeBSD has "__stack_chk_guard" defined externally on libc.so
  1788. if (TM.getRelocationModel() == Reloc::Static &&
  1789. !TM.getTargetTriple().isWindowsGNUEnvironment() &&
  1790. !TM.getTargetTriple().isOSFreeBSD())
  1791. GV->setDSOLocal(true);
  1792. }
  1793. }
  1794. // Currently only support "standard" __stack_chk_guard.
  1795. // TODO: add LOAD_STACK_GUARD support.
  1796. Value *TargetLoweringBase::getSDagStackGuard(const Module &M) const {
  1797. return M.getNamedValue("__stack_chk_guard");
  1798. }
  1799. Function *TargetLoweringBase::getSSPStackGuardCheck(const Module &M) const {
  1800. return nullptr;
  1801. }
  1802. unsigned TargetLoweringBase::getMinimumJumpTableEntries() const {
  1803. return MinimumJumpTableEntries;
  1804. }
  1805. void TargetLoweringBase::setMinimumJumpTableEntries(unsigned Val) {
  1806. MinimumJumpTableEntries = Val;
  1807. }
  1808. unsigned TargetLoweringBase::getMinimumJumpTableDensity(bool OptForSize) const {
  1809. return OptForSize ? OptsizeJumpTableDensity : JumpTableDensity;
  1810. }
  1811. unsigned TargetLoweringBase::getMaximumJumpTableSize() const {
  1812. return MaximumJumpTableSize;
  1813. }
  1814. void TargetLoweringBase::setMaximumJumpTableSize(unsigned Val) {
  1815. MaximumJumpTableSize = Val;
  1816. }
  1817. bool TargetLoweringBase::isJumpTableRelative() const {
  1818. return getTargetMachine().isPositionIndependent();
  1819. }
  1820. Align TargetLoweringBase::getPrefLoopAlignment(MachineLoop *ML) const {
  1821. if (TM.Options.LoopAlignment)
  1822. return Align(TM.Options.LoopAlignment);
  1823. return PrefLoopAlignment;
  1824. }
  1825. unsigned TargetLoweringBase::getMaxPermittedBytesForAlignment(
  1826. MachineBasicBlock *MBB) const {
  1827. return MaxBytesForAlignment;
  1828. }
  1829. //===----------------------------------------------------------------------===//
  1830. // Reciprocal Estimates
  1831. //===----------------------------------------------------------------------===//
  1832. /// Get the reciprocal estimate attribute string for a function that will
  1833. /// override the target defaults.
  1834. static StringRef getRecipEstimateForFunc(MachineFunction &MF) {
  1835. const Function &F = MF.getFunction();
  1836. return F.getFnAttribute("reciprocal-estimates").getValueAsString();
  1837. }
  1838. /// Construct a string for the given reciprocal operation of the given type.
  1839. /// This string should match the corresponding option to the front-end's
  1840. /// "-mrecip" flag assuming those strings have been passed through in an
  1841. /// attribute string. For example, "vec-divf" for a division of a vXf32.
  1842. static std::string getReciprocalOpName(bool IsSqrt, EVT VT) {
  1843. std::string Name = VT.isVector() ? "vec-" : "";
  1844. Name += IsSqrt ? "sqrt" : "div";
  1845. // TODO: Handle other float types?
  1846. if (VT.getScalarType() == MVT::f64) {
  1847. Name += "d";
  1848. } else if (VT.getScalarType() == MVT::f16) {
  1849. Name += "h";
  1850. } else {
  1851. assert(VT.getScalarType() == MVT::f32 &&
  1852. "Unexpected FP type for reciprocal estimate");
  1853. Name += "f";
  1854. }
  1855. return Name;
  1856. }
  1857. /// Return the character position and value (a single numeric character) of a
  1858. /// customized refinement operation in the input string if it exists. Return
  1859. /// false if there is no customized refinement step count.
  1860. static bool parseRefinementStep(StringRef In, size_t &Position,
  1861. uint8_t &Value) {
  1862. const char RefStepToken = ':';
  1863. Position = In.find(RefStepToken);
  1864. if (Position == StringRef::npos)
  1865. return false;
  1866. StringRef RefStepString = In.substr(Position + 1);
  1867. // Allow exactly one numeric character for the additional refinement
  1868. // step parameter.
  1869. if (RefStepString.size() == 1) {
  1870. char RefStepChar = RefStepString[0];
  1871. if (isDigit(RefStepChar)) {
  1872. Value = RefStepChar - '0';
  1873. return true;
  1874. }
  1875. }
  1876. report_fatal_error("Invalid refinement step for -recip.");
  1877. }
  1878. /// For the input attribute string, return one of the ReciprocalEstimate enum
  1879. /// status values (enabled, disabled, or not specified) for this operation on
  1880. /// the specified data type.
  1881. static int getOpEnabled(bool IsSqrt, EVT VT, StringRef Override) {
  1882. if (Override.empty())
  1883. return TargetLoweringBase::ReciprocalEstimate::Unspecified;
  1884. SmallVector<StringRef, 4> OverrideVector;
  1885. Override.split(OverrideVector, ',');
  1886. unsigned NumArgs = OverrideVector.size();
  1887. // Check if "all", "none", or "default" was specified.
  1888. if (NumArgs == 1) {
  1889. // Look for an optional setting of the number of refinement steps needed
  1890. // for this type of reciprocal operation.
  1891. size_t RefPos;
  1892. uint8_t RefSteps;
  1893. if (parseRefinementStep(Override, RefPos, RefSteps)) {
  1894. // Split the string for further processing.
  1895. Override = Override.substr(0, RefPos);
  1896. }
  1897. // All reciprocal types are enabled.
  1898. if (Override == "all")
  1899. return TargetLoweringBase::ReciprocalEstimate::Enabled;
  1900. // All reciprocal types are disabled.
  1901. if (Override == "none")
  1902. return TargetLoweringBase::ReciprocalEstimate::Disabled;
  1903. // Target defaults for enablement are used.
  1904. if (Override == "default")
  1905. return TargetLoweringBase::ReciprocalEstimate::Unspecified;
  1906. }
  1907. // The attribute string may omit the size suffix ('f'/'d').
  1908. std::string VTName = getReciprocalOpName(IsSqrt, VT);
  1909. std::string VTNameNoSize = VTName;
  1910. VTNameNoSize.pop_back();
  1911. static const char DisabledPrefix = '!';
  1912. for (StringRef RecipType : OverrideVector) {
  1913. size_t RefPos;
  1914. uint8_t RefSteps;
  1915. if (parseRefinementStep(RecipType, RefPos, RefSteps))
  1916. RecipType = RecipType.substr(0, RefPos);
  1917. // Ignore the disablement token for string matching.
  1918. bool IsDisabled = RecipType[0] == DisabledPrefix;
  1919. if (IsDisabled)
  1920. RecipType = RecipType.substr(1);
  1921. if (RecipType.equals(VTName) || RecipType.equals(VTNameNoSize))
  1922. return IsDisabled ? TargetLoweringBase::ReciprocalEstimate::Disabled
  1923. : TargetLoweringBase::ReciprocalEstimate::Enabled;
  1924. }
  1925. return TargetLoweringBase::ReciprocalEstimate::Unspecified;
  1926. }
  1927. /// For the input attribute string, return the customized refinement step count
  1928. /// for this operation on the specified data type. If the step count does not
  1929. /// exist, return the ReciprocalEstimate enum value for unspecified.
  1930. static int getOpRefinementSteps(bool IsSqrt, EVT VT, StringRef Override) {
  1931. if (Override.empty())
  1932. return TargetLoweringBase::ReciprocalEstimate::Unspecified;
  1933. SmallVector<StringRef, 4> OverrideVector;
  1934. Override.split(OverrideVector, ',');
  1935. unsigned NumArgs = OverrideVector.size();
  1936. // Check if "all", "default", or "none" was specified.
  1937. if (NumArgs == 1) {
  1938. // Look for an optional setting of the number of refinement steps needed
  1939. // for this type of reciprocal operation.
  1940. size_t RefPos;
  1941. uint8_t RefSteps;
  1942. if (!parseRefinementStep(Override, RefPos, RefSteps))
  1943. return TargetLoweringBase::ReciprocalEstimate::Unspecified;
  1944. // Split the string for further processing.
  1945. Override = Override.substr(0, RefPos);
  1946. assert(Override != "none" &&
  1947. "Disabled reciprocals, but specifed refinement steps?");
  1948. // If this is a general override, return the specified number of steps.
  1949. if (Override == "all" || Override == "default")
  1950. return RefSteps;
  1951. }
  1952. // The attribute string may omit the size suffix ('f'/'d').
  1953. std::string VTName = getReciprocalOpName(IsSqrt, VT);
  1954. std::string VTNameNoSize = VTName;
  1955. VTNameNoSize.pop_back();
  1956. for (StringRef RecipType : OverrideVector) {
  1957. size_t RefPos;
  1958. uint8_t RefSteps;
  1959. if (!parseRefinementStep(RecipType, RefPos, RefSteps))
  1960. continue;
  1961. RecipType = RecipType.substr(0, RefPos);
  1962. if (RecipType.equals(VTName) || RecipType.equals(VTNameNoSize))
  1963. return RefSteps;
  1964. }
  1965. return TargetLoweringBase::ReciprocalEstimate::Unspecified;
  1966. }
  1967. int TargetLoweringBase::getRecipEstimateSqrtEnabled(EVT VT,
  1968. MachineFunction &MF) const {
  1969. return getOpEnabled(true, VT, getRecipEstimateForFunc(MF));
  1970. }
  1971. int TargetLoweringBase::getRecipEstimateDivEnabled(EVT VT,
  1972. MachineFunction &MF) const {
  1973. return getOpEnabled(false, VT, getRecipEstimateForFunc(MF));
  1974. }
  1975. int TargetLoweringBase::getSqrtRefinementSteps(EVT VT,
  1976. MachineFunction &MF) const {
  1977. return getOpRefinementSteps(true, VT, getRecipEstimateForFunc(MF));
  1978. }
  1979. int TargetLoweringBase::getDivRefinementSteps(EVT VT,
  1980. MachineFunction &MF) const {
  1981. return getOpRefinementSteps(false, VT, getRecipEstimateForFunc(MF));
  1982. }
  1983. bool TargetLoweringBase::isLoadBitCastBeneficial(
  1984. EVT LoadVT, EVT BitcastVT, const SelectionDAG &DAG,
  1985. const MachineMemOperand &MMO) const {
  1986. // Single-element vectors are scalarized, so we should generally avoid having
  1987. // any memory operations on such types, as they would get scalarized too.
  1988. if (LoadVT.isFixedLengthVector() && BitcastVT.isFixedLengthVector() &&
  1989. BitcastVT.getVectorNumElements() == 1)
  1990. return false;
  1991. // Don't do if we could do an indexed load on the original type, but not on
  1992. // the new one.
  1993. if (!LoadVT.isSimple() || !BitcastVT.isSimple())
  1994. return true;
  1995. MVT LoadMVT = LoadVT.getSimpleVT();
  1996. // Don't bother doing this if it's just going to be promoted again later, as
  1997. // doing so might interfere with other combines.
  1998. if (getOperationAction(ISD::LOAD, LoadMVT) == Promote &&
  1999. getTypeToPromoteTo(ISD::LOAD, LoadMVT) == BitcastVT.getSimpleVT())
  2000. return false;
  2001. unsigned Fast = 0;
  2002. return allowsMemoryAccess(*DAG.getContext(), DAG.getDataLayout(), BitcastVT,
  2003. MMO, &Fast) &&
  2004. Fast;
  2005. }
  2006. void TargetLoweringBase::finalizeLowering(MachineFunction &MF) const {
  2007. MF.getRegInfo().freezeReservedRegs(MF);
  2008. }
  2009. MachineMemOperand::Flags TargetLoweringBase::getLoadMemOperandFlags(
  2010. const LoadInst &LI, const DataLayout &DL, AssumptionCache *AC,
  2011. const TargetLibraryInfo *LibInfo) const {
  2012. MachineMemOperand::Flags Flags = MachineMemOperand::MOLoad;
  2013. if (LI.isVolatile())
  2014. Flags |= MachineMemOperand::MOVolatile;
  2015. if (LI.hasMetadata(LLVMContext::MD_nontemporal))
  2016. Flags |= MachineMemOperand::MONonTemporal;
  2017. if (LI.hasMetadata(LLVMContext::MD_invariant_load))
  2018. Flags |= MachineMemOperand::MOInvariant;
  2019. if (isDereferenceableAndAlignedPointer(LI.getPointerOperand(), LI.getType(),
  2020. LI.getAlign(), DL, &LI, AC,
  2021. /*DT=*/nullptr, LibInfo))
  2022. Flags |= MachineMemOperand::MODereferenceable;
  2023. Flags |= getTargetMMOFlags(LI);
  2024. return Flags;
  2025. }
  2026. MachineMemOperand::Flags
  2027. TargetLoweringBase::getStoreMemOperandFlags(const StoreInst &SI,
  2028. const DataLayout &DL) const {
  2029. MachineMemOperand::Flags Flags = MachineMemOperand::MOStore;
  2030. if (SI.isVolatile())
  2031. Flags |= MachineMemOperand::MOVolatile;
  2032. if (SI.hasMetadata(LLVMContext::MD_nontemporal))
  2033. Flags |= MachineMemOperand::MONonTemporal;
  2034. // FIXME: Not preserving dereferenceable
  2035. Flags |= getTargetMMOFlags(SI);
  2036. return Flags;
  2037. }
  2038. MachineMemOperand::Flags
  2039. TargetLoweringBase::getAtomicMemOperandFlags(const Instruction &AI,
  2040. const DataLayout &DL) const {
  2041. auto Flags = MachineMemOperand::MOLoad | MachineMemOperand::MOStore;
  2042. if (const AtomicRMWInst *RMW = dyn_cast<AtomicRMWInst>(&AI)) {
  2043. if (RMW->isVolatile())
  2044. Flags |= MachineMemOperand::MOVolatile;
  2045. } else if (const AtomicCmpXchgInst *CmpX = dyn_cast<AtomicCmpXchgInst>(&AI)) {
  2046. if (CmpX->isVolatile())
  2047. Flags |= MachineMemOperand::MOVolatile;
  2048. } else
  2049. llvm_unreachable("not an atomic instruction");
  2050. // FIXME: Not preserving dereferenceable
  2051. Flags |= getTargetMMOFlags(AI);
  2052. return Flags;
  2053. }
  2054. Instruction *TargetLoweringBase::emitLeadingFence(IRBuilderBase &Builder,
  2055. Instruction *Inst,
  2056. AtomicOrdering Ord) const {
  2057. if (isReleaseOrStronger(Ord) && Inst->hasAtomicStore())
  2058. return Builder.CreateFence(Ord);
  2059. else
  2060. return nullptr;
  2061. }
  2062. Instruction *TargetLoweringBase::emitTrailingFence(IRBuilderBase &Builder,
  2063. Instruction *Inst,
  2064. AtomicOrdering Ord) const {
  2065. if (isAcquireOrStronger(Ord))
  2066. return Builder.CreateFence(Ord);
  2067. else
  2068. return nullptr;
  2069. }
  2070. //===----------------------------------------------------------------------===//
  2071. // GlobalISel Hooks
  2072. //===----------------------------------------------------------------------===//
  2073. bool TargetLoweringBase::shouldLocalize(const MachineInstr &MI,
  2074. const TargetTransformInfo *TTI) const {
  2075. auto &MF = *MI.getMF();
  2076. auto &MRI = MF.getRegInfo();
  2077. // Assuming a spill and reload of a value has a cost of 1 instruction each,
  2078. // this helper function computes the maximum number of uses we should consider
  2079. // for remat. E.g. on arm64 global addresses take 2 insts to materialize. We
  2080. // break even in terms of code size when the original MI has 2 users vs
  2081. // choosing to potentially spill. Any more than 2 users we we have a net code
  2082. // size increase. This doesn't take into account register pressure though.
  2083. auto maxUses = [](unsigned RematCost) {
  2084. // A cost of 1 means remats are basically free.
  2085. if (RematCost == 1)
  2086. return std::numeric_limits<unsigned>::max();
  2087. if (RematCost == 2)
  2088. return 2U;
  2089. // Remat is too expensive, only sink if there's one user.
  2090. if (RematCost > 2)
  2091. return 1U;
  2092. llvm_unreachable("Unexpected remat cost");
  2093. };
  2094. switch (MI.getOpcode()) {
  2095. default:
  2096. return false;
  2097. // Constants-like instructions should be close to their users.
  2098. // We don't want long live-ranges for them.
  2099. case TargetOpcode::G_CONSTANT:
  2100. case TargetOpcode::G_FCONSTANT:
  2101. case TargetOpcode::G_FRAME_INDEX:
  2102. case TargetOpcode::G_INTTOPTR:
  2103. return true;
  2104. case TargetOpcode::G_GLOBAL_VALUE: {
  2105. unsigned RematCost = TTI->getGISelRematGlobalCost();
  2106. Register Reg = MI.getOperand(0).getReg();
  2107. unsigned MaxUses = maxUses(RematCost);
  2108. if (MaxUses == UINT_MAX)
  2109. return true; // Remats are "free" so always localize.
  2110. return MRI.hasAtMostUserInstrs(Reg, MaxUses);
  2111. }
  2112. }
  2113. }