TargetLoweringBase.cpp 88 KB

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