TargetLoweringBase.cpp 75 KB

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