LegalizeFloatTypes.cpp 93 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195
  1. //===-------- LegalizeFloatTypes.cpp - Legalization of float types --------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file implements float type expansion and softening for LegalizeTypes.
  11. // Softening is the act of turning a computation in an illegal floating point
  12. // type into a computation in an integer type of the same size; also known as
  13. // "soft float". For example, turning f32 arithmetic into operations using i32.
  14. // The resulting integer value is the same as what you would get by performing
  15. // the floating point operation and bitcasting the result to the integer type.
  16. // Expansion is the act of changing a computation in an illegal type to be a
  17. // computation in two identical registers of a smaller type. For example,
  18. // implementing ppcf128 arithmetic in two f64 registers.
  19. //
  20. //===----------------------------------------------------------------------===//
  21. #include "LegalizeTypes.h"
  22. #include "llvm/Support/ErrorHandling.h"
  23. #include "llvm/Support/raw_ostream.h"
  24. using namespace llvm;
  25. #define DEBUG_TYPE "legalize-types"
  26. /// GetFPLibCall - Return the right libcall for the given floating point type.
  27. static RTLIB::Libcall GetFPLibCall(EVT VT,
  28. RTLIB::Libcall Call_F32,
  29. RTLIB::Libcall Call_F64,
  30. RTLIB::Libcall Call_F80,
  31. RTLIB::Libcall Call_F128,
  32. RTLIB::Libcall Call_PPCF128) {
  33. return
  34. VT == MVT::f32 ? Call_F32 :
  35. VT == MVT::f64 ? Call_F64 :
  36. VT == MVT::f80 ? Call_F80 :
  37. VT == MVT::f128 ? Call_F128 :
  38. VT == MVT::ppcf128 ? Call_PPCF128 :
  39. RTLIB::UNKNOWN_LIBCALL;
  40. }
  41. //===----------------------------------------------------------------------===//
  42. // Convert Float Results to Integer for Non-HW-supported Operations.
  43. //===----------------------------------------------------------------------===//
  44. bool DAGTypeLegalizer::SoftenFloatResult(SDNode *N, unsigned ResNo) {
  45. LLVM_DEBUG(dbgs() << "Soften float result " << ResNo << ": "; N->dump(&DAG);
  46. dbgs() << "\n");
  47. SDValue R = SDValue();
  48. switch (N->getOpcode()) {
  49. default:
  50. #ifndef NDEBUG
  51. dbgs() << "SoftenFloatResult #" << ResNo << ": ";
  52. N->dump(&DAG); dbgs() << "\n";
  53. #endif
  54. llvm_unreachable("Do not know how to soften the result of this operator!");
  55. case ISD::Register:
  56. case ISD::CopyFromReg:
  57. case ISD::CopyToReg:
  58. assert(isLegalInHWReg(N->getValueType(ResNo)) &&
  59. "Unsupported SoftenFloatRes opcode!");
  60. // Only when isLegalInHWReg, we can skip check of the operands.
  61. R = SDValue(N, ResNo);
  62. break;
  63. case ISD::MERGE_VALUES:R = SoftenFloatRes_MERGE_VALUES(N, ResNo); break;
  64. case ISD::BITCAST: R = SoftenFloatRes_BITCAST(N, ResNo); break;
  65. case ISD::BUILD_PAIR: R = SoftenFloatRes_BUILD_PAIR(N); break;
  66. case ISD::ConstantFP: R = SoftenFloatRes_ConstantFP(N, ResNo); break;
  67. case ISD::EXTRACT_VECTOR_ELT:
  68. R = SoftenFloatRes_EXTRACT_VECTOR_ELT(N, ResNo); break;
  69. case ISD::FABS: R = SoftenFloatRes_FABS(N, ResNo); break;
  70. case ISD::FMINNUM: R = SoftenFloatRes_FMINNUM(N); break;
  71. case ISD::FMAXNUM: R = SoftenFloatRes_FMAXNUM(N); break;
  72. case ISD::FADD: R = SoftenFloatRes_FADD(N); break;
  73. case ISD::FCEIL: R = SoftenFloatRes_FCEIL(N); break;
  74. case ISD::FCOPYSIGN: R = SoftenFloatRes_FCOPYSIGN(N, ResNo); break;
  75. case ISD::FCOS: R = SoftenFloatRes_FCOS(N); break;
  76. case ISD::FDIV: R = SoftenFloatRes_FDIV(N); break;
  77. case ISD::FEXP: R = SoftenFloatRes_FEXP(N); break;
  78. case ISD::FEXP2: R = SoftenFloatRes_FEXP2(N); break;
  79. case ISD::FFLOOR: R = SoftenFloatRes_FFLOOR(N); break;
  80. case ISD::FLOG: R = SoftenFloatRes_FLOG(N); break;
  81. case ISD::FLOG2: R = SoftenFloatRes_FLOG2(N); break;
  82. case ISD::FLOG10: R = SoftenFloatRes_FLOG10(N); break;
  83. case ISD::FMA: R = SoftenFloatRes_FMA(N); break;
  84. case ISD::FMUL: R = SoftenFloatRes_FMUL(N); break;
  85. case ISD::FNEARBYINT: R = SoftenFloatRes_FNEARBYINT(N); break;
  86. case ISD::FNEG: R = SoftenFloatRes_FNEG(N, ResNo); break;
  87. case ISD::FP_EXTEND: R = SoftenFloatRes_FP_EXTEND(N); break;
  88. case ISD::FP_ROUND: R = SoftenFloatRes_FP_ROUND(N); break;
  89. case ISD::FP16_TO_FP: R = SoftenFloatRes_FP16_TO_FP(N); break;
  90. case ISD::FPOW: R = SoftenFloatRes_FPOW(N); break;
  91. case ISD::FPOWI: R = SoftenFloatRes_FPOWI(N); break;
  92. case ISD::FREM: R = SoftenFloatRes_FREM(N); break;
  93. case ISD::FRINT: R = SoftenFloatRes_FRINT(N); break;
  94. case ISD::FROUND: R = SoftenFloatRes_FROUND(N); break;
  95. case ISD::FSIN: R = SoftenFloatRes_FSIN(N); break;
  96. case ISD::FSQRT: R = SoftenFloatRes_FSQRT(N); break;
  97. case ISD::FSUB: R = SoftenFloatRes_FSUB(N); break;
  98. case ISD::FTRUNC: R = SoftenFloatRes_FTRUNC(N); break;
  99. case ISD::LOAD: R = SoftenFloatRes_LOAD(N, ResNo); break;
  100. case ISD::ATOMIC_SWAP: R = BitcastToInt_ATOMIC_SWAP(N); break;
  101. case ISD::SELECT: R = SoftenFloatRes_SELECT(N, ResNo); break;
  102. case ISD::SELECT_CC: R = SoftenFloatRes_SELECT_CC(N, ResNo); break;
  103. case ISD::SINT_TO_FP:
  104. case ISD::UINT_TO_FP: R = SoftenFloatRes_XINT_TO_FP(N); break;
  105. case ISD::UNDEF: R = SoftenFloatRes_UNDEF(N); break;
  106. case ISD::VAARG: R = SoftenFloatRes_VAARG(N); break;
  107. }
  108. if (R.getNode() && R.getNode() != N) {
  109. SetSoftenedFloat(SDValue(N, ResNo), R);
  110. // Return true only if the node is changed, assuming that the operands
  111. // are also converted when necessary.
  112. return true;
  113. }
  114. // Otherwise, return false to tell caller to scan operands.
  115. return false;
  116. }
  117. SDValue DAGTypeLegalizer::SoftenFloatRes_BITCAST(SDNode *N, unsigned ResNo) {
  118. if (isLegalInHWReg(N->getValueType(ResNo)))
  119. return SDValue(N, ResNo);
  120. return BitConvertToInteger(N->getOperand(0));
  121. }
  122. SDValue DAGTypeLegalizer::SoftenFloatRes_MERGE_VALUES(SDNode *N,
  123. unsigned ResNo) {
  124. SDValue Op = DisintegrateMERGE_VALUES(N, ResNo);
  125. return BitConvertToInteger(Op);
  126. }
  127. SDValue DAGTypeLegalizer::SoftenFloatRes_BUILD_PAIR(SDNode *N) {
  128. // Convert the inputs to integers, and build a new pair out of them.
  129. return DAG.getNode(ISD::BUILD_PAIR, SDLoc(N),
  130. TLI.getTypeToTransformTo(*DAG.getContext(),
  131. N->getValueType(0)),
  132. BitConvertToInteger(N->getOperand(0)),
  133. BitConvertToInteger(N->getOperand(1)));
  134. }
  135. SDValue DAGTypeLegalizer::SoftenFloatRes_ConstantFP(SDNode *N, unsigned ResNo) {
  136. // When LegalInHWReg, we can load better from the constant pool.
  137. if (isLegalInHWReg(N->getValueType(ResNo)))
  138. return SDValue(N, ResNo);
  139. ConstantFPSDNode *CN = cast<ConstantFPSDNode>(N);
  140. // In ppcf128, the high 64 bits are always first in memory regardless
  141. // of Endianness. LLVM's APFloat representation is not Endian sensitive,
  142. // and so always converts into a 128-bit APInt in a non-Endian-sensitive
  143. // way. However, APInt's are serialized in an Endian-sensitive fashion,
  144. // so on big-Endian targets, the two doubles are output in the wrong
  145. // order. Fix this by manually flipping the order of the high 64 bits
  146. // and the low 64 bits here.
  147. if (DAG.getDataLayout().isBigEndian() &&
  148. CN->getValueType(0).getSimpleVT() == llvm::MVT::ppcf128) {
  149. uint64_t words[2] = { CN->getValueAPF().bitcastToAPInt().getRawData()[1],
  150. CN->getValueAPF().bitcastToAPInt().getRawData()[0] };
  151. APInt Val(128, words);
  152. return DAG.getConstant(Val, SDLoc(CN),
  153. TLI.getTypeToTransformTo(*DAG.getContext(),
  154. CN->getValueType(0)));
  155. } else {
  156. return DAG.getConstant(CN->getValueAPF().bitcastToAPInt(), SDLoc(CN),
  157. TLI.getTypeToTransformTo(*DAG.getContext(),
  158. CN->getValueType(0)));
  159. }
  160. }
  161. SDValue DAGTypeLegalizer::SoftenFloatRes_EXTRACT_VECTOR_ELT(SDNode *N, unsigned ResNo) {
  162. // When LegalInHWReg, keep the extracted value in register.
  163. if (isLegalInHWReg(N->getValueType(ResNo)))
  164. return SDValue(N, ResNo);
  165. SDValue NewOp = BitConvertVectorToIntegerVector(N->getOperand(0));
  166. return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(N),
  167. NewOp.getValueType().getVectorElementType(),
  168. NewOp, N->getOperand(1));
  169. }
  170. SDValue DAGTypeLegalizer::SoftenFloatRes_FABS(SDNode *N, unsigned ResNo) {
  171. // When LegalInHWReg, FABS can be implemented as native bitwise operations.
  172. if (isLegalInHWReg(N->getValueType(ResNo)))
  173. return SDValue(N, ResNo);
  174. EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
  175. unsigned Size = NVT.getSizeInBits();
  176. // Mask = ~(1 << (Size-1))
  177. APInt API = APInt::getAllOnesValue(Size);
  178. API.clearBit(Size - 1);
  179. SDValue Mask = DAG.getConstant(API, SDLoc(N), NVT);
  180. SDValue Op = GetSoftenedFloat(N->getOperand(0));
  181. return DAG.getNode(ISD::AND, SDLoc(N), NVT, Op, Mask);
  182. }
  183. SDValue DAGTypeLegalizer::SoftenFloatRes_FMINNUM(SDNode *N) {
  184. EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
  185. SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
  186. GetSoftenedFloat(N->getOperand(1)) };
  187. return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
  188. RTLIB::FMIN_F32,
  189. RTLIB::FMIN_F64,
  190. RTLIB::FMIN_F80,
  191. RTLIB::FMIN_F128,
  192. RTLIB::FMIN_PPCF128),
  193. NVT, Ops, false, SDLoc(N)).first;
  194. }
  195. SDValue DAGTypeLegalizer::SoftenFloatRes_FMAXNUM(SDNode *N) {
  196. EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
  197. SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
  198. GetSoftenedFloat(N->getOperand(1)) };
  199. return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
  200. RTLIB::FMAX_F32,
  201. RTLIB::FMAX_F64,
  202. RTLIB::FMAX_F80,
  203. RTLIB::FMAX_F128,
  204. RTLIB::FMAX_PPCF128),
  205. NVT, Ops, false, SDLoc(N)).first;
  206. }
  207. SDValue DAGTypeLegalizer::SoftenFloatRes_FADD(SDNode *N) {
  208. EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
  209. SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
  210. GetSoftenedFloat(N->getOperand(1)) };
  211. return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
  212. RTLIB::ADD_F32,
  213. RTLIB::ADD_F64,
  214. RTLIB::ADD_F80,
  215. RTLIB::ADD_F128,
  216. RTLIB::ADD_PPCF128),
  217. NVT, Ops, false, SDLoc(N)).first;
  218. }
  219. SDValue DAGTypeLegalizer::SoftenFloatRes_FCEIL(SDNode *N) {
  220. EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
  221. SDValue Op = GetSoftenedFloat(N->getOperand(0));
  222. return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
  223. RTLIB::CEIL_F32,
  224. RTLIB::CEIL_F64,
  225. RTLIB::CEIL_F80,
  226. RTLIB::CEIL_F128,
  227. RTLIB::CEIL_PPCF128),
  228. NVT, Op, false, SDLoc(N)).first;
  229. }
  230. SDValue DAGTypeLegalizer::SoftenFloatRes_FCOPYSIGN(SDNode *N, unsigned ResNo) {
  231. // When LegalInHWReg, FCOPYSIGN can be implemented as native bitwise operations.
  232. if (isLegalInHWReg(N->getValueType(ResNo)))
  233. return SDValue(N, ResNo);
  234. SDValue LHS = GetSoftenedFloat(N->getOperand(0));
  235. SDValue RHS = BitConvertToInteger(N->getOperand(1));
  236. SDLoc dl(N);
  237. EVT LVT = LHS.getValueType();
  238. EVT RVT = RHS.getValueType();
  239. unsigned LSize = LVT.getSizeInBits();
  240. unsigned RSize = RVT.getSizeInBits();
  241. // First get the sign bit of second operand.
  242. SDValue SignBit = DAG.getNode(
  243. ISD::SHL, dl, RVT, DAG.getConstant(1, dl, RVT),
  244. DAG.getConstant(RSize - 1, dl,
  245. TLI.getShiftAmountTy(RVT, DAG.getDataLayout())));
  246. SignBit = DAG.getNode(ISD::AND, dl, RVT, RHS, SignBit);
  247. // Shift right or sign-extend it if the two operands have different types.
  248. int SizeDiff = RVT.getSizeInBits() - LVT.getSizeInBits();
  249. if (SizeDiff > 0) {
  250. SignBit =
  251. DAG.getNode(ISD::SRL, dl, RVT, SignBit,
  252. DAG.getConstant(SizeDiff, dl,
  253. TLI.getShiftAmountTy(SignBit.getValueType(),
  254. DAG.getDataLayout())));
  255. SignBit = DAG.getNode(ISD::TRUNCATE, dl, LVT, SignBit);
  256. } else if (SizeDiff < 0) {
  257. SignBit = DAG.getNode(ISD::ANY_EXTEND, dl, LVT, SignBit);
  258. SignBit =
  259. DAG.getNode(ISD::SHL, dl, LVT, SignBit,
  260. DAG.getConstant(-SizeDiff, dl,
  261. TLI.getShiftAmountTy(SignBit.getValueType(),
  262. DAG.getDataLayout())));
  263. }
  264. // Clear the sign bit of the first operand.
  265. SDValue Mask = DAG.getNode(
  266. ISD::SHL, dl, LVT, DAG.getConstant(1, dl, LVT),
  267. DAG.getConstant(LSize - 1, dl,
  268. TLI.getShiftAmountTy(LVT, DAG.getDataLayout())));
  269. Mask = DAG.getNode(ISD::SUB, dl, LVT, Mask, DAG.getConstant(1, dl, LVT));
  270. LHS = DAG.getNode(ISD::AND, dl, LVT, LHS, Mask);
  271. // Or the value with the sign bit.
  272. return DAG.getNode(ISD::OR, dl, LVT, LHS, SignBit);
  273. }
  274. SDValue DAGTypeLegalizer::SoftenFloatRes_FCOS(SDNode *N) {
  275. EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
  276. SDValue Op = GetSoftenedFloat(N->getOperand(0));
  277. return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
  278. RTLIB::COS_F32,
  279. RTLIB::COS_F64,
  280. RTLIB::COS_F80,
  281. RTLIB::COS_F128,
  282. RTLIB::COS_PPCF128),
  283. NVT, Op, false, SDLoc(N)).first;
  284. }
  285. SDValue DAGTypeLegalizer::SoftenFloatRes_FDIV(SDNode *N) {
  286. EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
  287. SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
  288. GetSoftenedFloat(N->getOperand(1)) };
  289. return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
  290. RTLIB::DIV_F32,
  291. RTLIB::DIV_F64,
  292. RTLIB::DIV_F80,
  293. RTLIB::DIV_F128,
  294. RTLIB::DIV_PPCF128),
  295. NVT, Ops, false, SDLoc(N)).first;
  296. }
  297. SDValue DAGTypeLegalizer::SoftenFloatRes_FEXP(SDNode *N) {
  298. EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
  299. SDValue Op = GetSoftenedFloat(N->getOperand(0));
  300. return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
  301. RTLIB::EXP_F32,
  302. RTLIB::EXP_F64,
  303. RTLIB::EXP_F80,
  304. RTLIB::EXP_F128,
  305. RTLIB::EXP_PPCF128),
  306. NVT, Op, false, SDLoc(N)).first;
  307. }
  308. SDValue DAGTypeLegalizer::SoftenFloatRes_FEXP2(SDNode *N) {
  309. EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
  310. SDValue Op = GetSoftenedFloat(N->getOperand(0));
  311. return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
  312. RTLIB::EXP2_F32,
  313. RTLIB::EXP2_F64,
  314. RTLIB::EXP2_F80,
  315. RTLIB::EXP2_F128,
  316. RTLIB::EXP2_PPCF128),
  317. NVT, Op, false, SDLoc(N)).first;
  318. }
  319. SDValue DAGTypeLegalizer::SoftenFloatRes_FFLOOR(SDNode *N) {
  320. EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
  321. SDValue Op = GetSoftenedFloat(N->getOperand(0));
  322. return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
  323. RTLIB::FLOOR_F32,
  324. RTLIB::FLOOR_F64,
  325. RTLIB::FLOOR_F80,
  326. RTLIB::FLOOR_F128,
  327. RTLIB::FLOOR_PPCF128),
  328. NVT, Op, false, SDLoc(N)).first;
  329. }
  330. SDValue DAGTypeLegalizer::SoftenFloatRes_FLOG(SDNode *N) {
  331. EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
  332. SDValue Op = GetSoftenedFloat(N->getOperand(0));
  333. return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
  334. RTLIB::LOG_F32,
  335. RTLIB::LOG_F64,
  336. RTLIB::LOG_F80,
  337. RTLIB::LOG_F128,
  338. RTLIB::LOG_PPCF128),
  339. NVT, Op, false, SDLoc(N)).first;
  340. }
  341. SDValue DAGTypeLegalizer::SoftenFloatRes_FLOG2(SDNode *N) {
  342. EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
  343. SDValue Op = GetSoftenedFloat(N->getOperand(0));
  344. return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
  345. RTLIB::LOG2_F32,
  346. RTLIB::LOG2_F64,
  347. RTLIB::LOG2_F80,
  348. RTLIB::LOG2_F128,
  349. RTLIB::LOG2_PPCF128),
  350. NVT, Op, false, SDLoc(N)).first;
  351. }
  352. SDValue DAGTypeLegalizer::SoftenFloatRes_FLOG10(SDNode *N) {
  353. EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
  354. SDValue Op = GetSoftenedFloat(N->getOperand(0));
  355. return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
  356. RTLIB::LOG10_F32,
  357. RTLIB::LOG10_F64,
  358. RTLIB::LOG10_F80,
  359. RTLIB::LOG10_F128,
  360. RTLIB::LOG10_PPCF128),
  361. NVT, Op, false, SDLoc(N)).first;
  362. }
  363. SDValue DAGTypeLegalizer::SoftenFloatRes_FMA(SDNode *N) {
  364. EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
  365. SDValue Ops[3] = { GetSoftenedFloat(N->getOperand(0)),
  366. GetSoftenedFloat(N->getOperand(1)),
  367. GetSoftenedFloat(N->getOperand(2)) };
  368. return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
  369. RTLIB::FMA_F32,
  370. RTLIB::FMA_F64,
  371. RTLIB::FMA_F80,
  372. RTLIB::FMA_F128,
  373. RTLIB::FMA_PPCF128),
  374. NVT, Ops, false, SDLoc(N)).first;
  375. }
  376. SDValue DAGTypeLegalizer::SoftenFloatRes_FMUL(SDNode *N) {
  377. EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
  378. SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
  379. GetSoftenedFloat(N->getOperand(1)) };
  380. return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
  381. RTLIB::MUL_F32,
  382. RTLIB::MUL_F64,
  383. RTLIB::MUL_F80,
  384. RTLIB::MUL_F128,
  385. RTLIB::MUL_PPCF128),
  386. NVT, Ops, false, SDLoc(N)).first;
  387. }
  388. SDValue DAGTypeLegalizer::SoftenFloatRes_FNEARBYINT(SDNode *N) {
  389. EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
  390. SDValue Op = GetSoftenedFloat(N->getOperand(0));
  391. return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
  392. RTLIB::NEARBYINT_F32,
  393. RTLIB::NEARBYINT_F64,
  394. RTLIB::NEARBYINT_F80,
  395. RTLIB::NEARBYINT_F128,
  396. RTLIB::NEARBYINT_PPCF128),
  397. NVT, Op, false, SDLoc(N)).first;
  398. }
  399. SDValue DAGTypeLegalizer::SoftenFloatRes_FNEG(SDNode *N, unsigned ResNo) {
  400. // When LegalInHWReg, FNEG can be implemented as native bitwise operations.
  401. if (isLegalInHWReg(N->getValueType(ResNo)))
  402. return SDValue(N, ResNo);
  403. EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
  404. SDLoc dl(N);
  405. // Expand Y = FNEG(X) -> Y = SUB -0.0, X
  406. SDValue Ops[2] = { DAG.getConstantFP(-0.0, dl, N->getValueType(0)),
  407. GetSoftenedFloat(N->getOperand(0)) };
  408. return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
  409. RTLIB::SUB_F32,
  410. RTLIB::SUB_F64,
  411. RTLIB::SUB_F80,
  412. RTLIB::SUB_F128,
  413. RTLIB::SUB_PPCF128),
  414. NVT, Ops, false, dl).first;
  415. }
  416. SDValue DAGTypeLegalizer::SoftenFloatRes_FP_EXTEND(SDNode *N) {
  417. EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
  418. SDValue Op = N->getOperand(0);
  419. // There's only a libcall for f16 -> f32, so proceed in two stages. Also, it's
  420. // entirely possible for both f16 and f32 to be legal, so use the fully
  421. // hard-float FP_EXTEND rather than FP16_TO_FP.
  422. if (Op.getValueType() == MVT::f16 && N->getValueType(0) != MVT::f32) {
  423. Op = DAG.getNode(ISD::FP_EXTEND, SDLoc(N), MVT::f32, Op);
  424. if (getTypeAction(MVT::f32) == TargetLowering::TypeSoftenFloat)
  425. AddToWorklist(Op.getNode());
  426. }
  427. if (getTypeAction(Op.getValueType()) == TargetLowering::TypePromoteFloat) {
  428. Op = GetPromotedFloat(Op);
  429. // If the promotion did the FP_EXTEND to the destination type for us,
  430. // there's nothing left to do here.
  431. if (Op.getValueType() == N->getValueType(0)) {
  432. return BitConvertToInteger(Op);
  433. }
  434. }
  435. RTLIB::Libcall LC = RTLIB::getFPEXT(Op.getValueType(), N->getValueType(0));
  436. assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_EXTEND!");
  437. return TLI.makeLibCall(DAG, LC, NVT, Op, false, SDLoc(N)).first;
  438. }
  439. // FIXME: Should we just use 'normal' FP_EXTEND / FP_TRUNC instead of special
  440. // nodes?
  441. SDValue DAGTypeLegalizer::SoftenFloatRes_FP16_TO_FP(SDNode *N) {
  442. EVT MidVT = TLI.getTypeToTransformTo(*DAG.getContext(), MVT::f32);
  443. SDValue Op = N->getOperand(0);
  444. SDValue Res32 = TLI.makeLibCall(DAG, RTLIB::FPEXT_F16_F32, MidVT, Op,
  445. false, SDLoc(N)).first;
  446. if (N->getValueType(0) == MVT::f32)
  447. return Res32;
  448. EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
  449. RTLIB::Libcall LC = RTLIB::getFPEXT(MVT::f32, N->getValueType(0));
  450. assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_EXTEND!");
  451. return TLI.makeLibCall(DAG, LC, NVT, Res32, false, SDLoc(N)).first;
  452. }
  453. SDValue DAGTypeLegalizer::SoftenFloatRes_FP_ROUND(SDNode *N) {
  454. EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
  455. SDValue Op = N->getOperand(0);
  456. if (N->getValueType(0) == MVT::f16) {
  457. // Semi-soften first, to FP_TO_FP16, so that targets which support f16 as a
  458. // storage-only type get a chance to select things.
  459. return DAG.getNode(ISD::FP_TO_FP16, SDLoc(N), NVT, Op);
  460. }
  461. RTLIB::Libcall LC = RTLIB::getFPROUND(Op.getValueType(), N->getValueType(0));
  462. assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_ROUND!");
  463. return TLI.makeLibCall(DAG, LC, NVT, Op, false, SDLoc(N)).first;
  464. }
  465. SDValue DAGTypeLegalizer::SoftenFloatRes_FPOW(SDNode *N) {
  466. EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
  467. SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
  468. GetSoftenedFloat(N->getOperand(1)) };
  469. return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
  470. RTLIB::POW_F32,
  471. RTLIB::POW_F64,
  472. RTLIB::POW_F80,
  473. RTLIB::POW_F128,
  474. RTLIB::POW_PPCF128),
  475. NVT, Ops, false, SDLoc(N)).first;
  476. }
  477. SDValue DAGTypeLegalizer::SoftenFloatRes_FPOWI(SDNode *N) {
  478. assert(N->getOperand(1).getValueType() == MVT::i32 &&
  479. "Unsupported power type!");
  480. EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
  481. SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)), N->getOperand(1) };
  482. return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
  483. RTLIB::POWI_F32,
  484. RTLIB::POWI_F64,
  485. RTLIB::POWI_F80,
  486. RTLIB::POWI_F128,
  487. RTLIB::POWI_PPCF128),
  488. NVT, Ops, false, SDLoc(N)).first;
  489. }
  490. SDValue DAGTypeLegalizer::SoftenFloatRes_FREM(SDNode *N) {
  491. EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
  492. SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
  493. GetSoftenedFloat(N->getOperand(1)) };
  494. return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
  495. RTLIB::REM_F32,
  496. RTLIB::REM_F64,
  497. RTLIB::REM_F80,
  498. RTLIB::REM_F128,
  499. RTLIB::REM_PPCF128),
  500. NVT, Ops, false, SDLoc(N)).first;
  501. }
  502. SDValue DAGTypeLegalizer::SoftenFloatRes_FRINT(SDNode *N) {
  503. EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
  504. SDValue Op = GetSoftenedFloat(N->getOperand(0));
  505. return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
  506. RTLIB::RINT_F32,
  507. RTLIB::RINT_F64,
  508. RTLIB::RINT_F80,
  509. RTLIB::RINT_F128,
  510. RTLIB::RINT_PPCF128),
  511. NVT, Op, false, SDLoc(N)).first;
  512. }
  513. SDValue DAGTypeLegalizer::SoftenFloatRes_FROUND(SDNode *N) {
  514. EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
  515. SDValue Op = GetSoftenedFloat(N->getOperand(0));
  516. return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
  517. RTLIB::ROUND_F32,
  518. RTLIB::ROUND_F64,
  519. RTLIB::ROUND_F80,
  520. RTLIB::ROUND_F128,
  521. RTLIB::ROUND_PPCF128),
  522. NVT, Op, false, SDLoc(N)).first;
  523. }
  524. SDValue DAGTypeLegalizer::SoftenFloatRes_FSIN(SDNode *N) {
  525. EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
  526. SDValue Op = GetSoftenedFloat(N->getOperand(0));
  527. return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
  528. RTLIB::SIN_F32,
  529. RTLIB::SIN_F64,
  530. RTLIB::SIN_F80,
  531. RTLIB::SIN_F128,
  532. RTLIB::SIN_PPCF128),
  533. NVT, Op, false, SDLoc(N)).first;
  534. }
  535. SDValue DAGTypeLegalizer::SoftenFloatRes_FSQRT(SDNode *N) {
  536. EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
  537. SDValue Op = GetSoftenedFloat(N->getOperand(0));
  538. return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
  539. RTLIB::SQRT_F32,
  540. RTLIB::SQRT_F64,
  541. RTLIB::SQRT_F80,
  542. RTLIB::SQRT_F128,
  543. RTLIB::SQRT_PPCF128),
  544. NVT, Op, false, SDLoc(N)).first;
  545. }
  546. SDValue DAGTypeLegalizer::SoftenFloatRes_FSUB(SDNode *N) {
  547. EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
  548. SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
  549. GetSoftenedFloat(N->getOperand(1)) };
  550. return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
  551. RTLIB::SUB_F32,
  552. RTLIB::SUB_F64,
  553. RTLIB::SUB_F80,
  554. RTLIB::SUB_F128,
  555. RTLIB::SUB_PPCF128),
  556. NVT, Ops, false, SDLoc(N)).first;
  557. }
  558. SDValue DAGTypeLegalizer::SoftenFloatRes_FTRUNC(SDNode *N) {
  559. EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
  560. if (N->getValueType(0) == MVT::f16)
  561. return DAG.getNode(ISD::FP_TO_FP16, SDLoc(N), NVT, N->getOperand(0));
  562. SDValue Op = GetSoftenedFloat(N->getOperand(0));
  563. return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
  564. RTLIB::TRUNC_F32,
  565. RTLIB::TRUNC_F64,
  566. RTLIB::TRUNC_F80,
  567. RTLIB::TRUNC_F128,
  568. RTLIB::TRUNC_PPCF128),
  569. NVT, Op, false, SDLoc(N)).first;
  570. }
  571. SDValue DAGTypeLegalizer::SoftenFloatRes_LOAD(SDNode *N, unsigned ResNo) {
  572. bool LegalInHWReg = isLegalInHWReg(N->getValueType(ResNo));
  573. LoadSDNode *L = cast<LoadSDNode>(N);
  574. EVT VT = N->getValueType(0);
  575. EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
  576. SDLoc dl(N);
  577. auto MMOFlags =
  578. L->getMemOperand()->getFlags() &
  579. ~(MachineMemOperand::MOInvariant | MachineMemOperand::MODereferenceable);
  580. SDValue NewL;
  581. if (L->getExtensionType() == ISD::NON_EXTLOAD) {
  582. NewL = DAG.getLoad(L->getAddressingMode(), L->getExtensionType(), NVT, dl,
  583. L->getChain(), L->getBasePtr(), L->getOffset(),
  584. L->getPointerInfo(), NVT, L->getAlignment(), MMOFlags,
  585. L->getAAInfo());
  586. // Legalized the chain result - switch anything that used the old chain to
  587. // use the new one.
  588. if (N != NewL.getValue(1).getNode())
  589. ReplaceValueWith(SDValue(N, 1), NewL.getValue(1));
  590. return NewL;
  591. }
  592. // Do a non-extending load followed by FP_EXTEND.
  593. NewL = DAG.getLoad(L->getAddressingMode(), ISD::NON_EXTLOAD, L->getMemoryVT(),
  594. dl, L->getChain(), L->getBasePtr(), L->getOffset(),
  595. L->getPointerInfo(), L->getMemoryVT(), L->getAlignment(),
  596. MMOFlags, L->getAAInfo());
  597. // Legalized the chain result - switch anything that used the old chain to
  598. // use the new one.
  599. ReplaceValueWith(SDValue(N, 1), NewL.getValue(1));
  600. auto ExtendNode = DAG.getNode(ISD::FP_EXTEND, dl, VT, NewL);
  601. if (LegalInHWReg)
  602. return ExtendNode;
  603. return BitConvertToInteger(ExtendNode);
  604. }
  605. SDValue DAGTypeLegalizer::SoftenFloatRes_SELECT(SDNode *N, unsigned ResNo) {
  606. if (isLegalInHWReg(N->getValueType(ResNo)))
  607. return SDValue(N, ResNo);
  608. SDValue LHS = GetSoftenedFloat(N->getOperand(1));
  609. SDValue RHS = GetSoftenedFloat(N->getOperand(2));
  610. return DAG.getSelect(SDLoc(N),
  611. LHS.getValueType(), N->getOperand(0), LHS, RHS);
  612. }
  613. SDValue DAGTypeLegalizer::SoftenFloatRes_SELECT_CC(SDNode *N, unsigned ResNo) {
  614. if (isLegalInHWReg(N->getValueType(ResNo)))
  615. return SDValue(N, ResNo);
  616. SDValue LHS = GetSoftenedFloat(N->getOperand(2));
  617. SDValue RHS = GetSoftenedFloat(N->getOperand(3));
  618. return DAG.getNode(ISD::SELECT_CC, SDLoc(N),
  619. LHS.getValueType(), N->getOperand(0),
  620. N->getOperand(1), LHS, RHS, N->getOperand(4));
  621. }
  622. SDValue DAGTypeLegalizer::SoftenFloatRes_UNDEF(SDNode *N) {
  623. return DAG.getUNDEF(TLI.getTypeToTransformTo(*DAG.getContext(),
  624. N->getValueType(0)));
  625. }
  626. SDValue DAGTypeLegalizer::SoftenFloatRes_VAARG(SDNode *N) {
  627. SDValue Chain = N->getOperand(0); // Get the chain.
  628. SDValue Ptr = N->getOperand(1); // Get the pointer.
  629. EVT VT = N->getValueType(0);
  630. EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
  631. SDLoc dl(N);
  632. SDValue NewVAARG;
  633. NewVAARG = DAG.getVAArg(NVT, dl, Chain, Ptr, N->getOperand(2),
  634. N->getConstantOperandVal(3));
  635. // Legalized the chain result - switch anything that used the old chain to
  636. // use the new one.
  637. if (N != NewVAARG.getValue(1).getNode())
  638. ReplaceValueWith(SDValue(N, 1), NewVAARG.getValue(1));
  639. return NewVAARG;
  640. }
  641. SDValue DAGTypeLegalizer::SoftenFloatRes_XINT_TO_FP(SDNode *N) {
  642. bool Signed = N->getOpcode() == ISD::SINT_TO_FP;
  643. EVT SVT = N->getOperand(0).getValueType();
  644. EVT RVT = N->getValueType(0);
  645. EVT NVT = EVT();
  646. SDLoc dl(N);
  647. // If the input is not legal, eg: i1 -> fp, then it needs to be promoted to
  648. // a larger type, eg: i8 -> fp. Even if it is legal, no libcall may exactly
  649. // match. Look for an appropriate libcall.
  650. RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
  651. for (unsigned t = MVT::FIRST_INTEGER_VALUETYPE;
  652. t <= MVT::LAST_INTEGER_VALUETYPE && LC == RTLIB::UNKNOWN_LIBCALL; ++t) {
  653. NVT = (MVT::SimpleValueType)t;
  654. // The source needs to big enough to hold the operand.
  655. if (NVT.bitsGE(SVT))
  656. LC = Signed ? RTLIB::getSINTTOFP(NVT, RVT):RTLIB::getUINTTOFP (NVT, RVT);
  657. }
  658. assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported XINT_TO_FP!");
  659. // Sign/zero extend the argument if the libcall takes a larger type.
  660. SDValue Op = DAG.getNode(Signed ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND, dl,
  661. NVT, N->getOperand(0));
  662. return TLI.makeLibCall(DAG, LC,
  663. TLI.getTypeToTransformTo(*DAG.getContext(), RVT),
  664. Op, Signed, dl).first;
  665. }
  666. //===----------------------------------------------------------------------===//
  667. // Convert Float Operand to Integer for Non-HW-supported Operations.
  668. //===----------------------------------------------------------------------===//
  669. bool DAGTypeLegalizer::SoftenFloatOperand(SDNode *N, unsigned OpNo) {
  670. LLVM_DEBUG(dbgs() << "Soften float operand " << OpNo << ": "; N->dump(&DAG);
  671. dbgs() << "\n");
  672. SDValue Res = SDValue();
  673. switch (N->getOpcode()) {
  674. default:
  675. if (CanSkipSoftenFloatOperand(N, OpNo))
  676. return false;
  677. #ifndef NDEBUG
  678. dbgs() << "SoftenFloatOperand Op #" << OpNo << ": ";
  679. N->dump(&DAG); dbgs() << "\n";
  680. #endif
  681. llvm_unreachable("Do not know how to soften this operator's operand!");
  682. case ISD::BITCAST: Res = SoftenFloatOp_BITCAST(N); break;
  683. case ISD::CopyToReg: Res = SoftenFloatOp_COPY_TO_REG(N); break;
  684. case ISD::BR_CC: Res = SoftenFloatOp_BR_CC(N); break;
  685. case ISD::FABS: Res = SoftenFloatOp_FABS(N); break;
  686. case ISD::FCOPYSIGN: Res = SoftenFloatOp_FCOPYSIGN(N); break;
  687. case ISD::FNEG: Res = SoftenFloatOp_FNEG(N); break;
  688. case ISD::FP_EXTEND: Res = SoftenFloatOp_FP_EXTEND(N); break;
  689. case ISD::FP_TO_FP16: // Same as FP_ROUND for softening purposes
  690. case ISD::FP_ROUND: Res = SoftenFloatOp_FP_ROUND(N); break;
  691. case ISD::FP_TO_SINT:
  692. case ISD::FP_TO_UINT: Res = SoftenFloatOp_FP_TO_XINT(N); break;
  693. case ISD::SELECT: Res = SoftenFloatOp_SELECT(N); break;
  694. case ISD::SELECT_CC: Res = SoftenFloatOp_SELECT_CC(N); break;
  695. case ISD::SETCC: Res = SoftenFloatOp_SETCC(N); break;
  696. case ISD::STORE:
  697. Res = SoftenFloatOp_STORE(N, OpNo);
  698. // Do not try to analyze or soften this node again if the value is
  699. // or can be held in a register. In that case, Res.getNode() should
  700. // be equal to N.
  701. if (Res.getNode() == N &&
  702. isLegalInHWReg(N->getOperand(OpNo).getValueType()))
  703. return false;
  704. // Otherwise, we need to reanalyze and lower the new Res nodes.
  705. break;
  706. }
  707. // If the result is null, the sub-method took care of registering results etc.
  708. if (!Res.getNode()) return false;
  709. // If the result is N, the sub-method updated N in place. Tell the legalizer
  710. // core about this to re-analyze.
  711. if (Res.getNode() == N)
  712. return true;
  713. assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 &&
  714. "Invalid operand expansion");
  715. ReplaceValueWith(SDValue(N, 0), Res);
  716. return false;
  717. }
  718. bool DAGTypeLegalizer::CanSkipSoftenFloatOperand(SDNode *N, unsigned OpNo) {
  719. if (!isLegalInHWReg(N->getOperand(OpNo).getValueType()))
  720. return false;
  721. // When the operand type can be kept in registers there is nothing to do for
  722. // the following opcodes.
  723. switch (N->getOperand(OpNo).getOpcode()) {
  724. case ISD::BITCAST:
  725. case ISD::ConstantFP:
  726. case ISD::CopyFromReg:
  727. case ISD::CopyToReg:
  728. case ISD::FABS:
  729. case ISD::FCOPYSIGN:
  730. case ISD::FNEG:
  731. case ISD::Register:
  732. case ISD::SELECT:
  733. case ISD::SELECT_CC:
  734. return true;
  735. }
  736. switch (N->getOpcode()) {
  737. case ISD::ConstantFP: // Leaf node.
  738. case ISD::CopyFromReg: // Operand is a register that we know to be left
  739. // unchanged by SoftenFloatResult().
  740. case ISD::Register: // Leaf node.
  741. return true;
  742. }
  743. return false;
  744. }
  745. SDValue DAGTypeLegalizer::SoftenFloatOp_BITCAST(SDNode *N) {
  746. return DAG.getNode(ISD::BITCAST, SDLoc(N), N->getValueType(0),
  747. GetSoftenedFloat(N->getOperand(0)));
  748. }
  749. SDValue DAGTypeLegalizer::SoftenFloatOp_COPY_TO_REG(SDNode *N) {
  750. SDValue Op1 = GetSoftenedFloat(N->getOperand(1));
  751. SDValue Op2 = GetSoftenedFloat(N->getOperand(2));
  752. if (Op1 == N->getOperand(1) && Op2 == N->getOperand(2))
  753. return SDValue();
  754. if (N->getNumOperands() == 3)
  755. return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0), Op1, Op2), 0);
  756. return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0), Op1, Op2,
  757. N->getOperand(3)),
  758. 0);
  759. }
  760. SDValue DAGTypeLegalizer::SoftenFloatOp_FP_EXTEND(SDNode *N) {
  761. // If we get here, the result must be legal but the source illegal.
  762. EVT SVT = N->getOperand(0).getValueType();
  763. EVT RVT = N->getValueType(0);
  764. SDValue Op = GetSoftenedFloat(N->getOperand(0));
  765. if (SVT == MVT::f16)
  766. return DAG.getNode(ISD::FP16_TO_FP, SDLoc(N), RVT, Op);
  767. RTLIB::Libcall LC = RTLIB::getFPEXT(SVT, RVT);
  768. assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_EXTEND libcall");
  769. return TLI.makeLibCall(DAG, LC, RVT, Op, false, SDLoc(N)).first;
  770. }
  771. SDValue DAGTypeLegalizer::SoftenFloatOp_FP_ROUND(SDNode *N) {
  772. // We actually deal with the partially-softened FP_TO_FP16 node too, which
  773. // returns an i16 so doesn't meet the constraints necessary for FP_ROUND.
  774. assert(N->getOpcode() == ISD::FP_ROUND || N->getOpcode() == ISD::FP_TO_FP16);
  775. EVT SVT = N->getOperand(0).getValueType();
  776. EVT RVT = N->getValueType(0);
  777. EVT FloatRVT = N->getOpcode() == ISD::FP_TO_FP16 ? MVT::f16 : RVT;
  778. RTLIB::Libcall LC = RTLIB::getFPROUND(SVT, FloatRVT);
  779. assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_ROUND libcall");
  780. SDValue Op = GetSoftenedFloat(N->getOperand(0));
  781. return TLI.makeLibCall(DAG, LC, RVT, Op, false, SDLoc(N)).first;
  782. }
  783. SDValue DAGTypeLegalizer::SoftenFloatOp_BR_CC(SDNode *N) {
  784. SDValue NewLHS = N->getOperand(2), NewRHS = N->getOperand(3);
  785. ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(1))->get();
  786. EVT VT = NewLHS.getValueType();
  787. NewLHS = GetSoftenedFloat(NewLHS);
  788. NewRHS = GetSoftenedFloat(NewRHS);
  789. TLI.softenSetCCOperands(DAG, VT, NewLHS, NewRHS, CCCode, SDLoc(N));
  790. // If softenSetCCOperands returned a scalar, we need to compare the result
  791. // against zero to select between true and false values.
  792. if (!NewRHS.getNode()) {
  793. NewRHS = DAG.getConstant(0, SDLoc(N), NewLHS.getValueType());
  794. CCCode = ISD::SETNE;
  795. }
  796. // Update N to have the operands specified.
  797. return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0),
  798. DAG.getCondCode(CCCode), NewLHS, NewRHS,
  799. N->getOperand(4)),
  800. 0);
  801. }
  802. SDValue DAGTypeLegalizer::SoftenFloatOp_FABS(SDNode *N) {
  803. SDValue Op = GetSoftenedFloat(N->getOperand(0));
  804. if (Op == N->getOperand(0))
  805. return SDValue();
  806. return SDValue(DAG.UpdateNodeOperands(N, Op), 0);
  807. }
  808. SDValue DAGTypeLegalizer::SoftenFloatOp_FCOPYSIGN(SDNode *N) {
  809. SDValue Op0 = GetSoftenedFloat(N->getOperand(0));
  810. SDValue Op1 = GetSoftenedFloat(N->getOperand(1));
  811. if (Op0 == N->getOperand(0) && Op1 == N->getOperand(1))
  812. return SDValue();
  813. return SDValue(DAG.UpdateNodeOperands(N, Op0, Op1), 0);
  814. }
  815. SDValue DAGTypeLegalizer::SoftenFloatOp_FNEG(SDNode *N) {
  816. SDValue Op = GetSoftenedFloat(N->getOperand(0));
  817. if (Op == N->getOperand(0))
  818. return SDValue();
  819. return SDValue(DAG.UpdateNodeOperands(N, Op), 0);
  820. }
  821. SDValue DAGTypeLegalizer::SoftenFloatOp_FP_TO_XINT(SDNode *N) {
  822. bool Signed = N->getOpcode() == ISD::FP_TO_SINT;
  823. EVT SVT = N->getOperand(0).getValueType();
  824. EVT RVT = N->getValueType(0);
  825. EVT NVT = EVT();
  826. SDLoc dl(N);
  827. // If the result is not legal, eg: fp -> i1, then it needs to be promoted to
  828. // a larger type, eg: fp -> i32. Even if it is legal, no libcall may exactly
  829. // match, eg. we don't have fp -> i8 conversions.
  830. // Look for an appropriate libcall.
  831. RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
  832. for (unsigned IntVT = MVT::FIRST_INTEGER_VALUETYPE;
  833. IntVT <= MVT::LAST_INTEGER_VALUETYPE && LC == RTLIB::UNKNOWN_LIBCALL;
  834. ++IntVT) {
  835. NVT = (MVT::SimpleValueType)IntVT;
  836. // The type needs to big enough to hold the result.
  837. if (NVT.bitsGE(RVT))
  838. LC = Signed ? RTLIB::getFPTOSINT(SVT, NVT):RTLIB::getFPTOUINT(SVT, NVT);
  839. }
  840. assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_TO_XINT!");
  841. SDValue Op = GetSoftenedFloat(N->getOperand(0));
  842. SDValue Res = TLI.makeLibCall(DAG, LC, NVT, Op, false, dl).first;
  843. // Truncate the result if the libcall returns a larger type.
  844. return DAG.getNode(ISD::TRUNCATE, dl, RVT, Res);
  845. }
  846. SDValue DAGTypeLegalizer::SoftenFloatOp_SELECT(SDNode *N) {
  847. SDValue Op1 = GetSoftenedFloat(N->getOperand(1));
  848. SDValue Op2 = GetSoftenedFloat(N->getOperand(2));
  849. if (Op1 == N->getOperand(1) && Op2 == N->getOperand(2))
  850. return SDValue();
  851. return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0), Op1, Op2),
  852. 0);
  853. }
  854. SDValue DAGTypeLegalizer::SoftenFloatOp_SELECT_CC(SDNode *N) {
  855. SDValue NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
  856. ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(4))->get();
  857. EVT VT = NewLHS.getValueType();
  858. NewLHS = GetSoftenedFloat(NewLHS);
  859. NewRHS = GetSoftenedFloat(NewRHS);
  860. TLI.softenSetCCOperands(DAG, VT, NewLHS, NewRHS, CCCode, SDLoc(N));
  861. // If softenSetCCOperands returned a scalar, we need to compare the result
  862. // against zero to select between true and false values.
  863. if (!NewRHS.getNode()) {
  864. NewRHS = DAG.getConstant(0, SDLoc(N), NewLHS.getValueType());
  865. CCCode = ISD::SETNE;
  866. }
  867. // Update N to have the operands specified.
  868. return SDValue(DAG.UpdateNodeOperands(N, NewLHS, NewRHS,
  869. N->getOperand(2), N->getOperand(3),
  870. DAG.getCondCode(CCCode)),
  871. 0);
  872. }
  873. SDValue DAGTypeLegalizer::SoftenFloatOp_SETCC(SDNode *N) {
  874. SDValue NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
  875. ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(2))->get();
  876. EVT VT = NewLHS.getValueType();
  877. NewLHS = GetSoftenedFloat(NewLHS);
  878. NewRHS = GetSoftenedFloat(NewRHS);
  879. TLI.softenSetCCOperands(DAG, VT, NewLHS, NewRHS, CCCode, SDLoc(N));
  880. // If softenSetCCOperands returned a scalar, use it.
  881. if (!NewRHS.getNode()) {
  882. assert(NewLHS.getValueType() == N->getValueType(0) &&
  883. "Unexpected setcc expansion!");
  884. return NewLHS;
  885. }
  886. // Otherwise, update N to have the operands specified.
  887. return SDValue(DAG.UpdateNodeOperands(N, NewLHS, NewRHS,
  888. DAG.getCondCode(CCCode)),
  889. 0);
  890. }
  891. SDValue DAGTypeLegalizer::SoftenFloatOp_STORE(SDNode *N, unsigned OpNo) {
  892. assert(ISD::isUNINDEXEDStore(N) && "Indexed store during type legalization!");
  893. assert(OpNo == 1 && "Can only soften the stored value!");
  894. StoreSDNode *ST = cast<StoreSDNode>(N);
  895. SDValue Val = ST->getValue();
  896. SDLoc dl(N);
  897. if (ST->isTruncatingStore())
  898. // Do an FP_ROUND followed by a non-truncating store.
  899. Val = BitConvertToInteger(DAG.getNode(ISD::FP_ROUND, dl, ST->getMemoryVT(),
  900. Val, DAG.getIntPtrConstant(0, dl)));
  901. else
  902. Val = GetSoftenedFloat(Val);
  903. return DAG.getStore(ST->getChain(), dl, Val, ST->getBasePtr(),
  904. ST->getMemOperand());
  905. }
  906. //===----------------------------------------------------------------------===//
  907. // Float Result Expansion
  908. //===----------------------------------------------------------------------===//
  909. /// ExpandFloatResult - This method is called when the specified result of the
  910. /// specified node is found to need expansion. At this point, the node may also
  911. /// have invalid operands or may have other results that need promotion, we just
  912. /// know that (at least) one result needs expansion.
  913. void DAGTypeLegalizer::ExpandFloatResult(SDNode *N, unsigned ResNo) {
  914. LLVM_DEBUG(dbgs() << "Expand float result: "; N->dump(&DAG); dbgs() << "\n");
  915. SDValue Lo, Hi;
  916. Lo = Hi = SDValue();
  917. // See if the target wants to custom expand this node.
  918. if (CustomLowerNode(N, N->getValueType(ResNo), true))
  919. return;
  920. switch (N->getOpcode()) {
  921. default:
  922. #ifndef NDEBUG
  923. dbgs() << "ExpandFloatResult #" << ResNo << ": ";
  924. N->dump(&DAG); dbgs() << "\n";
  925. #endif
  926. llvm_unreachable("Do not know how to expand the result of this operator!");
  927. case ISD::UNDEF: SplitRes_UNDEF(N, Lo, Hi); break;
  928. case ISD::SELECT: SplitRes_SELECT(N, Lo, Hi); break;
  929. case ISD::SELECT_CC: SplitRes_SELECT_CC(N, Lo, Hi); break;
  930. case ISD::MERGE_VALUES: ExpandRes_MERGE_VALUES(N, ResNo, Lo, Hi); break;
  931. case ISD::BITCAST: ExpandRes_BITCAST(N, Lo, Hi); break;
  932. case ISD::BUILD_PAIR: ExpandRes_BUILD_PAIR(N, Lo, Hi); break;
  933. case ISD::EXTRACT_ELEMENT: ExpandRes_EXTRACT_ELEMENT(N, Lo, Hi); break;
  934. case ISD::EXTRACT_VECTOR_ELT: ExpandRes_EXTRACT_VECTOR_ELT(N, Lo, Hi); break;
  935. case ISD::VAARG: ExpandRes_VAARG(N, Lo, Hi); break;
  936. case ISD::ConstantFP: ExpandFloatRes_ConstantFP(N, Lo, Hi); break;
  937. case ISD::FABS: ExpandFloatRes_FABS(N, Lo, Hi); break;
  938. case ISD::FMINNUM: ExpandFloatRes_FMINNUM(N, Lo, Hi); break;
  939. case ISD::FMAXNUM: ExpandFloatRes_FMAXNUM(N, Lo, Hi); break;
  940. case ISD::FADD: ExpandFloatRes_FADD(N, Lo, Hi); break;
  941. case ISD::FCEIL: ExpandFloatRes_FCEIL(N, Lo, Hi); break;
  942. case ISD::FCOPYSIGN: ExpandFloatRes_FCOPYSIGN(N, Lo, Hi); break;
  943. case ISD::FCOS: ExpandFloatRes_FCOS(N, Lo, Hi); break;
  944. case ISD::FDIV: ExpandFloatRes_FDIV(N, Lo, Hi); break;
  945. case ISD::FEXP: ExpandFloatRes_FEXP(N, Lo, Hi); break;
  946. case ISD::FEXP2: ExpandFloatRes_FEXP2(N, Lo, Hi); break;
  947. case ISD::FFLOOR: ExpandFloatRes_FFLOOR(N, Lo, Hi); break;
  948. case ISD::FLOG: ExpandFloatRes_FLOG(N, Lo, Hi); break;
  949. case ISD::FLOG2: ExpandFloatRes_FLOG2(N, Lo, Hi); break;
  950. case ISD::FLOG10: ExpandFloatRes_FLOG10(N, Lo, Hi); break;
  951. case ISD::FMA: ExpandFloatRes_FMA(N, Lo, Hi); break;
  952. case ISD::FMUL: ExpandFloatRes_FMUL(N, Lo, Hi); break;
  953. case ISD::FNEARBYINT: ExpandFloatRes_FNEARBYINT(N, Lo, Hi); break;
  954. case ISD::FNEG: ExpandFloatRes_FNEG(N, Lo, Hi); break;
  955. case ISD::FP_EXTEND: ExpandFloatRes_FP_EXTEND(N, Lo, Hi); break;
  956. case ISD::FPOW: ExpandFloatRes_FPOW(N, Lo, Hi); break;
  957. case ISD::FPOWI: ExpandFloatRes_FPOWI(N, Lo, Hi); break;
  958. case ISD::FRINT: ExpandFloatRes_FRINT(N, Lo, Hi); break;
  959. case ISD::FROUND: ExpandFloatRes_FROUND(N, Lo, Hi); break;
  960. case ISD::FSIN: ExpandFloatRes_FSIN(N, Lo, Hi); break;
  961. case ISD::FSQRT: ExpandFloatRes_FSQRT(N, Lo, Hi); break;
  962. case ISD::FSUB: ExpandFloatRes_FSUB(N, Lo, Hi); break;
  963. case ISD::FTRUNC: ExpandFloatRes_FTRUNC(N, Lo, Hi); break;
  964. case ISD::LOAD: ExpandFloatRes_LOAD(N, Lo, Hi); break;
  965. case ISD::SINT_TO_FP:
  966. case ISD::UINT_TO_FP: ExpandFloatRes_XINT_TO_FP(N, Lo, Hi); break;
  967. case ISD::FREM: ExpandFloatRes_FREM(N, Lo, Hi); break;
  968. }
  969. // If Lo/Hi is null, the sub-method took care of registering results etc.
  970. if (Lo.getNode())
  971. SetExpandedFloat(SDValue(N, ResNo), Lo, Hi);
  972. }
  973. void DAGTypeLegalizer::ExpandFloatRes_ConstantFP(SDNode *N, SDValue &Lo,
  974. SDValue &Hi) {
  975. EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
  976. assert(NVT.getSizeInBits() == 64 &&
  977. "Do not know how to expand this float constant!");
  978. APInt C = cast<ConstantFPSDNode>(N)->getValueAPF().bitcastToAPInt();
  979. SDLoc dl(N);
  980. Lo = DAG.getConstantFP(APFloat(DAG.EVTToAPFloatSemantics(NVT),
  981. APInt(64, C.getRawData()[1])),
  982. dl, NVT);
  983. Hi = DAG.getConstantFP(APFloat(DAG.EVTToAPFloatSemantics(NVT),
  984. APInt(64, C.getRawData()[0])),
  985. dl, NVT);
  986. }
  987. void DAGTypeLegalizer::ExpandFloatRes_FABS(SDNode *N, SDValue &Lo,
  988. SDValue &Hi) {
  989. assert(N->getValueType(0) == MVT::ppcf128 &&
  990. "Logic only correct for ppcf128!");
  991. SDLoc dl(N);
  992. SDValue Tmp;
  993. GetExpandedFloat(N->getOperand(0), Lo, Tmp);
  994. Hi = DAG.getNode(ISD::FABS, dl, Tmp.getValueType(), Tmp);
  995. // Lo = Hi==fabs(Hi) ? Lo : -Lo;
  996. Lo = DAG.getSelectCC(dl, Tmp, Hi, Lo,
  997. DAG.getNode(ISD::FNEG, dl, Lo.getValueType(), Lo),
  998. ISD::SETEQ);
  999. }
  1000. void DAGTypeLegalizer::ExpandFloatRes_FMINNUM(SDNode *N, SDValue &Lo,
  1001. SDValue &Hi) {
  1002. SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
  1003. RTLIB::FMIN_F32, RTLIB::FMIN_F64,
  1004. RTLIB::FMIN_F80, RTLIB::FMIN_F128,
  1005. RTLIB::FMIN_PPCF128),
  1006. N, false);
  1007. GetPairElements(Call, Lo, Hi);
  1008. }
  1009. void DAGTypeLegalizer::ExpandFloatRes_FMAXNUM(SDNode *N, SDValue &Lo,
  1010. SDValue &Hi) {
  1011. SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
  1012. RTLIB::FMAX_F32, RTLIB::FMAX_F64,
  1013. RTLIB::FMAX_F80, RTLIB::FMAX_F128,
  1014. RTLIB::FMAX_PPCF128),
  1015. N, false);
  1016. GetPairElements(Call, Lo, Hi);
  1017. }
  1018. void DAGTypeLegalizer::ExpandFloatRes_FADD(SDNode *N, SDValue &Lo,
  1019. SDValue &Hi) {
  1020. SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
  1021. RTLIB::ADD_F32, RTLIB::ADD_F64,
  1022. RTLIB::ADD_F80, RTLIB::ADD_F128,
  1023. RTLIB::ADD_PPCF128),
  1024. N, false);
  1025. GetPairElements(Call, Lo, Hi);
  1026. }
  1027. void DAGTypeLegalizer::ExpandFloatRes_FCEIL(SDNode *N,
  1028. SDValue &Lo, SDValue &Hi) {
  1029. SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
  1030. RTLIB::CEIL_F32, RTLIB::CEIL_F64,
  1031. RTLIB::CEIL_F80, RTLIB::CEIL_F128,
  1032. RTLIB::CEIL_PPCF128),
  1033. N, false);
  1034. GetPairElements(Call, Lo, Hi);
  1035. }
  1036. void DAGTypeLegalizer::ExpandFloatRes_FCOPYSIGN(SDNode *N,
  1037. SDValue &Lo, SDValue &Hi) {
  1038. SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
  1039. RTLIB::COPYSIGN_F32,
  1040. RTLIB::COPYSIGN_F64,
  1041. RTLIB::COPYSIGN_F80,
  1042. RTLIB::COPYSIGN_F128,
  1043. RTLIB::COPYSIGN_PPCF128),
  1044. N, false);
  1045. GetPairElements(Call, Lo, Hi);
  1046. }
  1047. void DAGTypeLegalizer::ExpandFloatRes_FCOS(SDNode *N,
  1048. SDValue &Lo, SDValue &Hi) {
  1049. SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
  1050. RTLIB::COS_F32, RTLIB::COS_F64,
  1051. RTLIB::COS_F80, RTLIB::COS_F128,
  1052. RTLIB::COS_PPCF128),
  1053. N, false);
  1054. GetPairElements(Call, Lo, Hi);
  1055. }
  1056. void DAGTypeLegalizer::ExpandFloatRes_FDIV(SDNode *N, SDValue &Lo,
  1057. SDValue &Hi) {
  1058. SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
  1059. SDValue Call = TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
  1060. RTLIB::DIV_F32,
  1061. RTLIB::DIV_F64,
  1062. RTLIB::DIV_F80,
  1063. RTLIB::DIV_F128,
  1064. RTLIB::DIV_PPCF128),
  1065. N->getValueType(0), Ops, false,
  1066. SDLoc(N)).first;
  1067. GetPairElements(Call, Lo, Hi);
  1068. }
  1069. void DAGTypeLegalizer::ExpandFloatRes_FEXP(SDNode *N,
  1070. SDValue &Lo, SDValue &Hi) {
  1071. SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
  1072. RTLIB::EXP_F32, RTLIB::EXP_F64,
  1073. RTLIB::EXP_F80, RTLIB::EXP_F128,
  1074. RTLIB::EXP_PPCF128),
  1075. N, false);
  1076. GetPairElements(Call, Lo, Hi);
  1077. }
  1078. void DAGTypeLegalizer::ExpandFloatRes_FEXP2(SDNode *N,
  1079. SDValue &Lo, SDValue &Hi) {
  1080. SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
  1081. RTLIB::EXP2_F32, RTLIB::EXP2_F64,
  1082. RTLIB::EXP2_F80, RTLIB::EXP2_F128,
  1083. RTLIB::EXP2_PPCF128),
  1084. N, false);
  1085. GetPairElements(Call, Lo, Hi);
  1086. }
  1087. void DAGTypeLegalizer::ExpandFloatRes_FFLOOR(SDNode *N,
  1088. SDValue &Lo, SDValue &Hi) {
  1089. SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
  1090. RTLIB::FLOOR_F32, RTLIB::FLOOR_F64,
  1091. RTLIB::FLOOR_F80, RTLIB::FLOOR_F128,
  1092. RTLIB::FLOOR_PPCF128),
  1093. N, false);
  1094. GetPairElements(Call, Lo, Hi);
  1095. }
  1096. void DAGTypeLegalizer::ExpandFloatRes_FLOG(SDNode *N,
  1097. SDValue &Lo, SDValue &Hi) {
  1098. SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
  1099. RTLIB::LOG_F32, RTLIB::LOG_F64,
  1100. RTLIB::LOG_F80, RTLIB::LOG_F128,
  1101. RTLIB::LOG_PPCF128),
  1102. N, false);
  1103. GetPairElements(Call, Lo, Hi);
  1104. }
  1105. void DAGTypeLegalizer::ExpandFloatRes_FLOG2(SDNode *N,
  1106. SDValue &Lo, SDValue &Hi) {
  1107. SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
  1108. RTLIB::LOG2_F32, RTLIB::LOG2_F64,
  1109. RTLIB::LOG2_F80, RTLIB::LOG2_F128,
  1110. RTLIB::LOG2_PPCF128),
  1111. N, false);
  1112. GetPairElements(Call, Lo, Hi);
  1113. }
  1114. void DAGTypeLegalizer::ExpandFloatRes_FLOG10(SDNode *N,
  1115. SDValue &Lo, SDValue &Hi) {
  1116. SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
  1117. RTLIB::LOG10_F32, RTLIB::LOG10_F64,
  1118. RTLIB::LOG10_F80, RTLIB::LOG10_F128,
  1119. RTLIB::LOG10_PPCF128),
  1120. N, false);
  1121. GetPairElements(Call, Lo, Hi);
  1122. }
  1123. void DAGTypeLegalizer::ExpandFloatRes_FMA(SDNode *N, SDValue &Lo,
  1124. SDValue &Hi) {
  1125. SDValue Ops[3] = { N->getOperand(0), N->getOperand(1), N->getOperand(2) };
  1126. SDValue Call = TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
  1127. RTLIB::FMA_F32,
  1128. RTLIB::FMA_F64,
  1129. RTLIB::FMA_F80,
  1130. RTLIB::FMA_F128,
  1131. RTLIB::FMA_PPCF128),
  1132. N->getValueType(0), Ops, false,
  1133. SDLoc(N)).first;
  1134. GetPairElements(Call, Lo, Hi);
  1135. }
  1136. void DAGTypeLegalizer::ExpandFloatRes_FMUL(SDNode *N, SDValue &Lo,
  1137. SDValue &Hi) {
  1138. SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
  1139. SDValue Call = TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
  1140. RTLIB::MUL_F32,
  1141. RTLIB::MUL_F64,
  1142. RTLIB::MUL_F80,
  1143. RTLIB::MUL_F128,
  1144. RTLIB::MUL_PPCF128),
  1145. N->getValueType(0), Ops, false,
  1146. SDLoc(N)).first;
  1147. GetPairElements(Call, Lo, Hi);
  1148. }
  1149. void DAGTypeLegalizer::ExpandFloatRes_FNEARBYINT(SDNode *N,
  1150. SDValue &Lo, SDValue &Hi) {
  1151. SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
  1152. RTLIB::NEARBYINT_F32,
  1153. RTLIB::NEARBYINT_F64,
  1154. RTLIB::NEARBYINT_F80,
  1155. RTLIB::NEARBYINT_F128,
  1156. RTLIB::NEARBYINT_PPCF128),
  1157. N, false);
  1158. GetPairElements(Call, Lo, Hi);
  1159. }
  1160. void DAGTypeLegalizer::ExpandFloatRes_FNEG(SDNode *N, SDValue &Lo,
  1161. SDValue &Hi) {
  1162. SDLoc dl(N);
  1163. GetExpandedFloat(N->getOperand(0), Lo, Hi);
  1164. Lo = DAG.getNode(ISD::FNEG, dl, Lo.getValueType(), Lo);
  1165. Hi = DAG.getNode(ISD::FNEG, dl, Hi.getValueType(), Hi);
  1166. }
  1167. void DAGTypeLegalizer::ExpandFloatRes_FP_EXTEND(SDNode *N, SDValue &Lo,
  1168. SDValue &Hi) {
  1169. EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
  1170. SDLoc dl(N);
  1171. Hi = DAG.getNode(ISD::FP_EXTEND, dl, NVT, N->getOperand(0));
  1172. Lo = DAG.getConstantFP(APFloat(DAG.EVTToAPFloatSemantics(NVT),
  1173. APInt(NVT.getSizeInBits(), 0)), dl, NVT);
  1174. }
  1175. void DAGTypeLegalizer::ExpandFloatRes_FPOW(SDNode *N,
  1176. SDValue &Lo, SDValue &Hi) {
  1177. SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
  1178. RTLIB::POW_F32, RTLIB::POW_F64,
  1179. RTLIB::POW_F80, RTLIB::POW_F128,
  1180. RTLIB::POW_PPCF128),
  1181. N, false);
  1182. GetPairElements(Call, Lo, Hi);
  1183. }
  1184. void DAGTypeLegalizer::ExpandFloatRes_FPOWI(SDNode *N,
  1185. SDValue &Lo, SDValue &Hi) {
  1186. SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
  1187. RTLIB::POWI_F32, RTLIB::POWI_F64,
  1188. RTLIB::POWI_F80, RTLIB::POWI_F128,
  1189. RTLIB::POWI_PPCF128),
  1190. N, false);
  1191. GetPairElements(Call, Lo, Hi);
  1192. }
  1193. void DAGTypeLegalizer::ExpandFloatRes_FREM(SDNode *N,
  1194. SDValue &Lo, SDValue &Hi) {
  1195. SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
  1196. RTLIB::REM_F32, RTLIB::REM_F64,
  1197. RTLIB::REM_F80, RTLIB::REM_F128,
  1198. RTLIB::REM_PPCF128),
  1199. N, false);
  1200. GetPairElements(Call, Lo, Hi);
  1201. }
  1202. void DAGTypeLegalizer::ExpandFloatRes_FRINT(SDNode *N,
  1203. SDValue &Lo, SDValue &Hi) {
  1204. SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
  1205. RTLIB::RINT_F32, RTLIB::RINT_F64,
  1206. RTLIB::RINT_F80, RTLIB::RINT_F128,
  1207. RTLIB::RINT_PPCF128),
  1208. N, false);
  1209. GetPairElements(Call, Lo, Hi);
  1210. }
  1211. void DAGTypeLegalizer::ExpandFloatRes_FROUND(SDNode *N,
  1212. SDValue &Lo, SDValue &Hi) {
  1213. SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
  1214. RTLIB::ROUND_F32,
  1215. RTLIB::ROUND_F64,
  1216. RTLIB::ROUND_F80,
  1217. RTLIB::ROUND_F128,
  1218. RTLIB::ROUND_PPCF128),
  1219. N, false);
  1220. GetPairElements(Call, Lo, Hi);
  1221. }
  1222. void DAGTypeLegalizer::ExpandFloatRes_FSIN(SDNode *N,
  1223. SDValue &Lo, SDValue &Hi) {
  1224. SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
  1225. RTLIB::SIN_F32, RTLIB::SIN_F64,
  1226. RTLIB::SIN_F80, RTLIB::SIN_F128,
  1227. RTLIB::SIN_PPCF128),
  1228. N, false);
  1229. GetPairElements(Call, Lo, Hi);
  1230. }
  1231. void DAGTypeLegalizer::ExpandFloatRes_FSQRT(SDNode *N,
  1232. SDValue &Lo, SDValue &Hi) {
  1233. SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
  1234. RTLIB::SQRT_F32, RTLIB::SQRT_F64,
  1235. RTLIB::SQRT_F80, RTLIB::SQRT_F128,
  1236. RTLIB::SQRT_PPCF128),
  1237. N, false);
  1238. GetPairElements(Call, Lo, Hi);
  1239. }
  1240. void DAGTypeLegalizer::ExpandFloatRes_FSUB(SDNode *N, SDValue &Lo,
  1241. SDValue &Hi) {
  1242. SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
  1243. SDValue Call = TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
  1244. RTLIB::SUB_F32,
  1245. RTLIB::SUB_F64,
  1246. RTLIB::SUB_F80,
  1247. RTLIB::SUB_F128,
  1248. RTLIB::SUB_PPCF128),
  1249. N->getValueType(0), Ops, false,
  1250. SDLoc(N)).first;
  1251. GetPairElements(Call, Lo, Hi);
  1252. }
  1253. void DAGTypeLegalizer::ExpandFloatRes_FTRUNC(SDNode *N,
  1254. SDValue &Lo, SDValue &Hi) {
  1255. SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
  1256. RTLIB::TRUNC_F32, RTLIB::TRUNC_F64,
  1257. RTLIB::TRUNC_F80, RTLIB::TRUNC_F128,
  1258. RTLIB::TRUNC_PPCF128),
  1259. N, false);
  1260. GetPairElements(Call, Lo, Hi);
  1261. }
  1262. void DAGTypeLegalizer::ExpandFloatRes_LOAD(SDNode *N, SDValue &Lo,
  1263. SDValue &Hi) {
  1264. if (ISD::isNormalLoad(N)) {
  1265. ExpandRes_NormalLoad(N, Lo, Hi);
  1266. return;
  1267. }
  1268. assert(ISD::isUNINDEXEDLoad(N) && "Indexed load during type legalization!");
  1269. LoadSDNode *LD = cast<LoadSDNode>(N);
  1270. SDValue Chain = LD->getChain();
  1271. SDValue Ptr = LD->getBasePtr();
  1272. SDLoc dl(N);
  1273. EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), LD->getValueType(0));
  1274. assert(NVT.isByteSized() && "Expanded type not byte sized!");
  1275. assert(LD->getMemoryVT().bitsLE(NVT) && "Float type not round?");
  1276. Hi = DAG.getExtLoad(LD->getExtensionType(), dl, NVT, Chain, Ptr,
  1277. LD->getMemoryVT(), LD->getMemOperand());
  1278. // Remember the chain.
  1279. Chain = Hi.getValue(1);
  1280. // The low part is zero.
  1281. Lo = DAG.getConstantFP(APFloat(DAG.EVTToAPFloatSemantics(NVT),
  1282. APInt(NVT.getSizeInBits(), 0)), dl, NVT);
  1283. // Modified the chain - switch anything that used the old chain to use the
  1284. // new one.
  1285. ReplaceValueWith(SDValue(LD, 1), Chain);
  1286. }
  1287. void DAGTypeLegalizer::ExpandFloatRes_XINT_TO_FP(SDNode *N, SDValue &Lo,
  1288. SDValue &Hi) {
  1289. assert(N->getValueType(0) == MVT::ppcf128 && "Unsupported XINT_TO_FP!");
  1290. EVT VT = N->getValueType(0);
  1291. EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
  1292. SDValue Src = N->getOperand(0);
  1293. EVT SrcVT = Src.getValueType();
  1294. bool isSigned = N->getOpcode() == ISD::SINT_TO_FP;
  1295. SDLoc dl(N);
  1296. // First do an SINT_TO_FP, whether the original was signed or unsigned.
  1297. // When promoting partial word types to i32 we must honor the signedness,
  1298. // though.
  1299. if (SrcVT.bitsLE(MVT::i32)) {
  1300. // The integer can be represented exactly in an f64.
  1301. Src = DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND, dl,
  1302. MVT::i32, Src);
  1303. Lo = DAG.getConstantFP(APFloat(DAG.EVTToAPFloatSemantics(NVT),
  1304. APInt(NVT.getSizeInBits(), 0)), dl, NVT);
  1305. Hi = DAG.getNode(ISD::SINT_TO_FP, dl, NVT, Src);
  1306. } else {
  1307. RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
  1308. if (SrcVT.bitsLE(MVT::i64)) {
  1309. Src = DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND, dl,
  1310. MVT::i64, Src);
  1311. LC = RTLIB::SINTTOFP_I64_PPCF128;
  1312. } else if (SrcVT.bitsLE(MVT::i128)) {
  1313. Src = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i128, Src);
  1314. LC = RTLIB::SINTTOFP_I128_PPCF128;
  1315. }
  1316. assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported XINT_TO_FP!");
  1317. Hi = TLI.makeLibCall(DAG, LC, VT, Src, true, dl).first;
  1318. GetPairElements(Hi, Lo, Hi);
  1319. }
  1320. if (isSigned)
  1321. return;
  1322. // Unsigned - fix up the SINT_TO_FP value just calculated.
  1323. Hi = DAG.getNode(ISD::BUILD_PAIR, dl, VT, Lo, Hi);
  1324. SrcVT = Src.getValueType();
  1325. // x>=0 ? (ppcf128)(iN)x : (ppcf128)(iN)x + 2^N; N=32,64,128.
  1326. static const uint64_t TwoE32[] = { 0x41f0000000000000LL, 0 };
  1327. static const uint64_t TwoE64[] = { 0x43f0000000000000LL, 0 };
  1328. static const uint64_t TwoE128[] = { 0x47f0000000000000LL, 0 };
  1329. ArrayRef<uint64_t> Parts;
  1330. switch (SrcVT.getSimpleVT().SimpleTy) {
  1331. default:
  1332. llvm_unreachable("Unsupported UINT_TO_FP!");
  1333. case MVT::i32:
  1334. Parts = TwoE32;
  1335. break;
  1336. case MVT::i64:
  1337. Parts = TwoE64;
  1338. break;
  1339. case MVT::i128:
  1340. Parts = TwoE128;
  1341. break;
  1342. }
  1343. // TODO: Are there fast-math-flags to propagate to this FADD?
  1344. Lo = DAG.getNode(ISD::FADD, dl, VT, Hi,
  1345. DAG.getConstantFP(APFloat(APFloat::PPCDoubleDouble(),
  1346. APInt(128, Parts)),
  1347. dl, MVT::ppcf128));
  1348. Lo = DAG.getSelectCC(dl, Src, DAG.getConstant(0, dl, SrcVT),
  1349. Lo, Hi, ISD::SETLT);
  1350. GetPairElements(Lo, Lo, Hi);
  1351. }
  1352. //===----------------------------------------------------------------------===//
  1353. // Float Operand Expansion
  1354. //===----------------------------------------------------------------------===//
  1355. /// ExpandFloatOperand - This method is called when the specified operand of the
  1356. /// specified node is found to need expansion. At this point, all of the result
  1357. /// types of the node are known to be legal, but other operands of the node may
  1358. /// need promotion or expansion as well as the specified one.
  1359. bool DAGTypeLegalizer::ExpandFloatOperand(SDNode *N, unsigned OpNo) {
  1360. LLVM_DEBUG(dbgs() << "Expand float operand: "; N->dump(&DAG); dbgs() << "\n");
  1361. SDValue Res = SDValue();
  1362. // See if the target wants to custom expand this node.
  1363. if (CustomLowerNode(N, N->getOperand(OpNo).getValueType(), false))
  1364. return false;
  1365. switch (N->getOpcode()) {
  1366. default:
  1367. #ifndef NDEBUG
  1368. dbgs() << "ExpandFloatOperand Op #" << OpNo << ": ";
  1369. N->dump(&DAG); dbgs() << "\n";
  1370. #endif
  1371. llvm_unreachable("Do not know how to expand this operator's operand!");
  1372. case ISD::BITCAST: Res = ExpandOp_BITCAST(N); break;
  1373. case ISD::BUILD_VECTOR: Res = ExpandOp_BUILD_VECTOR(N); break;
  1374. case ISD::EXTRACT_ELEMENT: Res = ExpandOp_EXTRACT_ELEMENT(N); break;
  1375. case ISD::BR_CC: Res = ExpandFloatOp_BR_CC(N); break;
  1376. case ISD::FCOPYSIGN: Res = ExpandFloatOp_FCOPYSIGN(N); break;
  1377. case ISD::FP_ROUND: Res = ExpandFloatOp_FP_ROUND(N); break;
  1378. case ISD::FP_TO_SINT: Res = ExpandFloatOp_FP_TO_SINT(N); break;
  1379. case ISD::FP_TO_UINT: Res = ExpandFloatOp_FP_TO_UINT(N); break;
  1380. case ISD::SELECT_CC: Res = ExpandFloatOp_SELECT_CC(N); break;
  1381. case ISD::SETCC: Res = ExpandFloatOp_SETCC(N); break;
  1382. case ISD::STORE: Res = ExpandFloatOp_STORE(cast<StoreSDNode>(N),
  1383. OpNo); break;
  1384. }
  1385. // If the result is null, the sub-method took care of registering results etc.
  1386. if (!Res.getNode()) return false;
  1387. // If the result is N, the sub-method updated N in place. Tell the legalizer
  1388. // core about this.
  1389. if (Res.getNode() == N)
  1390. return true;
  1391. assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 &&
  1392. "Invalid operand expansion");
  1393. ReplaceValueWith(SDValue(N, 0), Res);
  1394. return false;
  1395. }
  1396. /// FloatExpandSetCCOperands - Expand the operands of a comparison. This code
  1397. /// is shared among BR_CC, SELECT_CC, and SETCC handlers.
  1398. void DAGTypeLegalizer::FloatExpandSetCCOperands(SDValue &NewLHS,
  1399. SDValue &NewRHS,
  1400. ISD::CondCode &CCCode,
  1401. const SDLoc &dl) {
  1402. SDValue LHSLo, LHSHi, RHSLo, RHSHi;
  1403. GetExpandedFloat(NewLHS, LHSLo, LHSHi);
  1404. GetExpandedFloat(NewRHS, RHSLo, RHSHi);
  1405. assert(NewLHS.getValueType() == MVT::ppcf128 && "Unsupported setcc type!");
  1406. // FIXME: This generated code sucks. We want to generate
  1407. // FCMPU crN, hi1, hi2
  1408. // BNE crN, L:
  1409. // FCMPU crN, lo1, lo2
  1410. // The following can be improved, but not that much.
  1411. SDValue Tmp1, Tmp2, Tmp3;
  1412. Tmp1 = DAG.getSetCC(dl, getSetCCResultType(LHSHi.getValueType()),
  1413. LHSHi, RHSHi, ISD::SETOEQ);
  1414. Tmp2 = DAG.getSetCC(dl, getSetCCResultType(LHSLo.getValueType()),
  1415. LHSLo, RHSLo, CCCode);
  1416. Tmp3 = DAG.getNode(ISD::AND, dl, Tmp1.getValueType(), Tmp1, Tmp2);
  1417. Tmp1 = DAG.getSetCC(dl, getSetCCResultType(LHSHi.getValueType()),
  1418. LHSHi, RHSHi, ISD::SETUNE);
  1419. Tmp2 = DAG.getSetCC(dl, getSetCCResultType(LHSHi.getValueType()),
  1420. LHSHi, RHSHi, CCCode);
  1421. Tmp1 = DAG.getNode(ISD::AND, dl, Tmp1.getValueType(), Tmp1, Tmp2);
  1422. NewLHS = DAG.getNode(ISD::OR, dl, Tmp1.getValueType(), Tmp1, Tmp3);
  1423. NewRHS = SDValue(); // LHS is the result, not a compare.
  1424. }
  1425. SDValue DAGTypeLegalizer::ExpandFloatOp_BR_CC(SDNode *N) {
  1426. SDValue NewLHS = N->getOperand(2), NewRHS = N->getOperand(3);
  1427. ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(1))->get();
  1428. FloatExpandSetCCOperands(NewLHS, NewRHS, CCCode, SDLoc(N));
  1429. // If ExpandSetCCOperands returned a scalar, we need to compare the result
  1430. // against zero to select between true and false values.
  1431. if (!NewRHS.getNode()) {
  1432. NewRHS = DAG.getConstant(0, SDLoc(N), NewLHS.getValueType());
  1433. CCCode = ISD::SETNE;
  1434. }
  1435. // Update N to have the operands specified.
  1436. return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0),
  1437. DAG.getCondCode(CCCode), NewLHS, NewRHS,
  1438. N->getOperand(4)), 0);
  1439. }
  1440. SDValue DAGTypeLegalizer::ExpandFloatOp_FCOPYSIGN(SDNode *N) {
  1441. assert(N->getOperand(1).getValueType() == MVT::ppcf128 &&
  1442. "Logic only correct for ppcf128!");
  1443. SDValue Lo, Hi;
  1444. GetExpandedFloat(N->getOperand(1), Lo, Hi);
  1445. // The ppcf128 value is providing only the sign; take it from the
  1446. // higher-order double (which must have the larger magnitude).
  1447. return DAG.getNode(ISD::FCOPYSIGN, SDLoc(N),
  1448. N->getValueType(0), N->getOperand(0), Hi);
  1449. }
  1450. SDValue DAGTypeLegalizer::ExpandFloatOp_FP_ROUND(SDNode *N) {
  1451. assert(N->getOperand(0).getValueType() == MVT::ppcf128 &&
  1452. "Logic only correct for ppcf128!");
  1453. SDValue Lo, Hi;
  1454. GetExpandedFloat(N->getOperand(0), Lo, Hi);
  1455. // Round it the rest of the way (e.g. to f32) if needed.
  1456. return DAG.getNode(ISD::FP_ROUND, SDLoc(N),
  1457. N->getValueType(0), Hi, N->getOperand(1));
  1458. }
  1459. SDValue DAGTypeLegalizer::ExpandFloatOp_FP_TO_SINT(SDNode *N) {
  1460. EVT RVT = N->getValueType(0);
  1461. SDLoc dl(N);
  1462. RTLIB::Libcall LC = RTLIB::getFPTOSINT(N->getOperand(0).getValueType(), RVT);
  1463. assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_TO_SINT!");
  1464. return TLI.makeLibCall(DAG, LC, RVT, N->getOperand(0), false, dl).first;
  1465. }
  1466. SDValue DAGTypeLegalizer::ExpandFloatOp_FP_TO_UINT(SDNode *N) {
  1467. EVT RVT = N->getValueType(0);
  1468. SDLoc dl(N);
  1469. RTLIB::Libcall LC = RTLIB::getFPTOUINT(N->getOperand(0).getValueType(), RVT);
  1470. assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_TO_UINT!");
  1471. return TLI.makeLibCall(DAG, LC, N->getValueType(0), N->getOperand(0),
  1472. false, dl).first;
  1473. }
  1474. SDValue DAGTypeLegalizer::ExpandFloatOp_SELECT_CC(SDNode *N) {
  1475. SDValue NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
  1476. ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(4))->get();
  1477. FloatExpandSetCCOperands(NewLHS, NewRHS, CCCode, SDLoc(N));
  1478. // If ExpandSetCCOperands returned a scalar, we need to compare the result
  1479. // against zero to select between true and false values.
  1480. if (!NewRHS.getNode()) {
  1481. NewRHS = DAG.getConstant(0, SDLoc(N), NewLHS.getValueType());
  1482. CCCode = ISD::SETNE;
  1483. }
  1484. // Update N to have the operands specified.
  1485. return SDValue(DAG.UpdateNodeOperands(N, NewLHS, NewRHS,
  1486. N->getOperand(2), N->getOperand(3),
  1487. DAG.getCondCode(CCCode)), 0);
  1488. }
  1489. SDValue DAGTypeLegalizer::ExpandFloatOp_SETCC(SDNode *N) {
  1490. SDValue NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
  1491. ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(2))->get();
  1492. FloatExpandSetCCOperands(NewLHS, NewRHS, CCCode, SDLoc(N));
  1493. // If ExpandSetCCOperands returned a scalar, use it.
  1494. if (!NewRHS.getNode()) {
  1495. assert(NewLHS.getValueType() == N->getValueType(0) &&
  1496. "Unexpected setcc expansion!");
  1497. return NewLHS;
  1498. }
  1499. // Otherwise, update N to have the operands specified.
  1500. return SDValue(DAG.UpdateNodeOperands(N, NewLHS, NewRHS,
  1501. DAG.getCondCode(CCCode)), 0);
  1502. }
  1503. SDValue DAGTypeLegalizer::ExpandFloatOp_STORE(SDNode *N, unsigned OpNo) {
  1504. if (ISD::isNormalStore(N))
  1505. return ExpandOp_NormalStore(N, OpNo);
  1506. assert(ISD::isUNINDEXEDStore(N) && "Indexed store during type legalization!");
  1507. assert(OpNo == 1 && "Can only expand the stored value so far");
  1508. StoreSDNode *ST = cast<StoreSDNode>(N);
  1509. SDValue Chain = ST->getChain();
  1510. SDValue Ptr = ST->getBasePtr();
  1511. EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(),
  1512. ST->getValue().getValueType());
  1513. assert(NVT.isByteSized() && "Expanded type not byte sized!");
  1514. assert(ST->getMemoryVT().bitsLE(NVT) && "Float type not round?");
  1515. (void)NVT;
  1516. SDValue Lo, Hi;
  1517. GetExpandedOp(ST->getValue(), Lo, Hi);
  1518. return DAG.getTruncStore(Chain, SDLoc(N), Hi, Ptr,
  1519. ST->getMemoryVT(), ST->getMemOperand());
  1520. }
  1521. //===----------------------------------------------------------------------===//
  1522. // Float Operand Promotion
  1523. //===----------------------------------------------------------------------===//
  1524. //
  1525. static ISD::NodeType GetPromotionOpcode(EVT OpVT, EVT RetVT) {
  1526. if (OpVT == MVT::f16) {
  1527. return ISD::FP16_TO_FP;
  1528. } else if (RetVT == MVT::f16) {
  1529. return ISD::FP_TO_FP16;
  1530. }
  1531. report_fatal_error("Attempt at an invalid promotion-related conversion");
  1532. }
  1533. bool DAGTypeLegalizer::PromoteFloatOperand(SDNode *N, unsigned OpNo) {
  1534. SDValue R = SDValue();
  1535. if (CustomLowerNode(N, N->getOperand(OpNo).getValueType(), false)) {
  1536. LLVM_DEBUG(dbgs() << "Node has been custom lowered, done\n");
  1537. return false;
  1538. }
  1539. // Nodes that use a promotion-requiring floating point operand, but doesn't
  1540. // produce a promotion-requiring floating point result, need to be legalized
  1541. // to use the promoted float operand. Nodes that produce at least one
  1542. // promotion-requiring floating point result have their operands legalized as
  1543. // a part of PromoteFloatResult.
  1544. switch (N->getOpcode()) {
  1545. default:
  1546. llvm_unreachable("Do not know how to promote this operator's operand!");
  1547. case ISD::BITCAST: R = PromoteFloatOp_BITCAST(N, OpNo); break;
  1548. case ISD::FCOPYSIGN: R = PromoteFloatOp_FCOPYSIGN(N, OpNo); break;
  1549. case ISD::FP_TO_SINT:
  1550. case ISD::FP_TO_UINT: R = PromoteFloatOp_FP_TO_XINT(N, OpNo); break;
  1551. case ISD::FP_EXTEND: R = PromoteFloatOp_FP_EXTEND(N, OpNo); break;
  1552. case ISD::SELECT_CC: R = PromoteFloatOp_SELECT_CC(N, OpNo); break;
  1553. case ISD::SETCC: R = PromoteFloatOp_SETCC(N, OpNo); break;
  1554. case ISD::STORE: R = PromoteFloatOp_STORE(N, OpNo); break;
  1555. }
  1556. if (R.getNode())
  1557. ReplaceValueWith(SDValue(N, 0), R);
  1558. return false;
  1559. }
  1560. SDValue DAGTypeLegalizer::PromoteFloatOp_BITCAST(SDNode *N, unsigned OpNo) {
  1561. SDValue Op = N->getOperand(0);
  1562. EVT OpVT = Op->getValueType(0);
  1563. SDValue Promoted = GetPromotedFloat(N->getOperand(0));
  1564. EVT PromotedVT = Promoted->getValueType(0);
  1565. // Convert the promoted float value to the desired IVT.
  1566. EVT IVT = EVT::getIntegerVT(*DAG.getContext(), OpVT.getSizeInBits());
  1567. SDValue Convert = DAG.getNode(GetPromotionOpcode(PromotedVT, OpVT), SDLoc(N),
  1568. IVT, Promoted);
  1569. // The final result type might not be an scalar so we need a bitcast. The
  1570. // bitcast will be further legalized if needed.
  1571. return DAG.getBitcast(N->getValueType(0), Convert);
  1572. }
  1573. // Promote Operand 1 of FCOPYSIGN. Operand 0 ought to be handled by
  1574. // PromoteFloatRes_FCOPYSIGN.
  1575. SDValue DAGTypeLegalizer::PromoteFloatOp_FCOPYSIGN(SDNode *N, unsigned OpNo) {
  1576. assert (OpNo == 1 && "Only Operand 1 must need promotion here");
  1577. SDValue Op1 = GetPromotedFloat(N->getOperand(1));
  1578. return DAG.getNode(N->getOpcode(), SDLoc(N), N->getValueType(0),
  1579. N->getOperand(0), Op1);
  1580. }
  1581. // Convert the promoted float value to the desired integer type
  1582. SDValue DAGTypeLegalizer::PromoteFloatOp_FP_TO_XINT(SDNode *N, unsigned OpNo) {
  1583. SDValue Op = GetPromotedFloat(N->getOperand(0));
  1584. return DAG.getNode(N->getOpcode(), SDLoc(N), N->getValueType(0), Op);
  1585. }
  1586. SDValue DAGTypeLegalizer::PromoteFloatOp_FP_EXTEND(SDNode *N, unsigned OpNo) {
  1587. SDValue Op = GetPromotedFloat(N->getOperand(0));
  1588. EVT VT = N->getValueType(0);
  1589. // Desired VT is same as promoted type. Use promoted float directly.
  1590. if (VT == Op->getValueType(0))
  1591. return Op;
  1592. // Else, extend the promoted float value to the desired VT.
  1593. return DAG.getNode(ISD::FP_EXTEND, SDLoc(N), VT, Op);
  1594. }
  1595. // Promote the float operands used for comparison. The true- and false-
  1596. // operands have the same type as the result and are promoted, if needed, by
  1597. // PromoteFloatRes_SELECT_CC
  1598. SDValue DAGTypeLegalizer::PromoteFloatOp_SELECT_CC(SDNode *N, unsigned OpNo) {
  1599. SDValue LHS = GetPromotedFloat(N->getOperand(0));
  1600. SDValue RHS = GetPromotedFloat(N->getOperand(1));
  1601. return DAG.getNode(ISD::SELECT_CC, SDLoc(N), N->getValueType(0),
  1602. LHS, RHS, N->getOperand(2), N->getOperand(3),
  1603. N->getOperand(4));
  1604. }
  1605. // Construct a SETCC that compares the promoted values and sets the conditional
  1606. // code.
  1607. SDValue DAGTypeLegalizer::PromoteFloatOp_SETCC(SDNode *N, unsigned OpNo) {
  1608. EVT VT = N->getValueType(0);
  1609. EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
  1610. SDValue Op0 = GetPromotedFloat(N->getOperand(0));
  1611. SDValue Op1 = GetPromotedFloat(N->getOperand(1));
  1612. ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(2))->get();
  1613. return DAG.getSetCC(SDLoc(N), NVT, Op0, Op1, CCCode);
  1614. }
  1615. // Lower the promoted Float down to the integer value of same size and construct
  1616. // a STORE of the integer value.
  1617. SDValue DAGTypeLegalizer::PromoteFloatOp_STORE(SDNode *N, unsigned OpNo) {
  1618. StoreSDNode *ST = cast<StoreSDNode>(N);
  1619. SDValue Val = ST->getValue();
  1620. SDLoc DL(N);
  1621. SDValue Promoted = GetPromotedFloat(Val);
  1622. EVT VT = ST->getOperand(1).getValueType();
  1623. EVT IVT = EVT::getIntegerVT(*DAG.getContext(), VT.getSizeInBits());
  1624. SDValue NewVal;
  1625. NewVal = DAG.getNode(GetPromotionOpcode(Promoted.getValueType(), VT), DL,
  1626. IVT, Promoted);
  1627. return DAG.getStore(ST->getChain(), DL, NewVal, ST->getBasePtr(),
  1628. ST->getMemOperand());
  1629. }
  1630. //===----------------------------------------------------------------------===//
  1631. // Float Result Promotion
  1632. //===----------------------------------------------------------------------===//
  1633. void DAGTypeLegalizer::PromoteFloatResult(SDNode *N, unsigned ResNo) {
  1634. SDValue R = SDValue();
  1635. switch (N->getOpcode()) {
  1636. // These opcodes cannot appear if promotion of FP16 is done in the backend
  1637. // instead of Clang
  1638. case ISD::FP16_TO_FP:
  1639. case ISD::FP_TO_FP16:
  1640. default:
  1641. llvm_unreachable("Do not know how to promote this operator's result!");
  1642. case ISD::BITCAST: R = PromoteFloatRes_BITCAST(N); break;
  1643. case ISD::ConstantFP: R = PromoteFloatRes_ConstantFP(N); break;
  1644. case ISD::EXTRACT_VECTOR_ELT:
  1645. R = PromoteFloatRes_EXTRACT_VECTOR_ELT(N); break;
  1646. case ISD::FCOPYSIGN: R = PromoteFloatRes_FCOPYSIGN(N); break;
  1647. // Unary FP Operations
  1648. case ISD::FABS:
  1649. case ISD::FCEIL:
  1650. case ISD::FCOS:
  1651. case ISD::FEXP:
  1652. case ISD::FEXP2:
  1653. case ISD::FFLOOR:
  1654. case ISD::FLOG:
  1655. case ISD::FLOG2:
  1656. case ISD::FLOG10:
  1657. case ISD::FNEARBYINT:
  1658. case ISD::FNEG:
  1659. case ISD::FRINT:
  1660. case ISD::FROUND:
  1661. case ISD::FSIN:
  1662. case ISD::FSQRT:
  1663. case ISD::FTRUNC:
  1664. case ISD::FCANONICALIZE: R = PromoteFloatRes_UnaryOp(N); break;
  1665. // Binary FP Operations
  1666. case ISD::FADD:
  1667. case ISD::FDIV:
  1668. case ISD::FMAXIMUM:
  1669. case ISD::FMINIMUM:
  1670. case ISD::FMAXNUM:
  1671. case ISD::FMINNUM:
  1672. case ISD::FMUL:
  1673. case ISD::FPOW:
  1674. case ISD::FREM:
  1675. case ISD::FSUB: R = PromoteFloatRes_BinOp(N); break;
  1676. case ISD::FMA: // FMA is same as FMAD
  1677. case ISD::FMAD: R = PromoteFloatRes_FMAD(N); break;
  1678. case ISD::FPOWI: R = PromoteFloatRes_FPOWI(N); break;
  1679. case ISD::FP_ROUND: R = PromoteFloatRes_FP_ROUND(N); break;
  1680. case ISD::LOAD: R = PromoteFloatRes_LOAD(N); break;
  1681. case ISD::SELECT: R = PromoteFloatRes_SELECT(N); break;
  1682. case ISD::SELECT_CC: R = PromoteFloatRes_SELECT_CC(N); break;
  1683. case ISD::SINT_TO_FP:
  1684. case ISD::UINT_TO_FP: R = PromoteFloatRes_XINT_TO_FP(N); break;
  1685. case ISD::UNDEF: R = PromoteFloatRes_UNDEF(N); break;
  1686. case ISD::ATOMIC_SWAP: R = BitcastToInt_ATOMIC_SWAP(N); break;
  1687. }
  1688. if (R.getNode())
  1689. SetPromotedFloat(SDValue(N, ResNo), R);
  1690. }
  1691. // Bitcast from i16 to f16: convert the i16 to a f32 value instead.
  1692. // At this point, it is not possible to determine if the bitcast value is
  1693. // eventually stored to memory or promoted to f32 or promoted to a floating
  1694. // point at a higher precision. Some of these cases are handled by FP_EXTEND,
  1695. // STORE promotion handlers.
  1696. SDValue DAGTypeLegalizer::PromoteFloatRes_BITCAST(SDNode *N) {
  1697. EVT VT = N->getValueType(0);
  1698. EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
  1699. // Input type isn't guaranteed to be a scalar int so bitcast if not. The
  1700. // bitcast will be legalized further if necessary.
  1701. EVT IVT = EVT::getIntegerVT(*DAG.getContext(),
  1702. N->getOperand(0).getValueType().getSizeInBits());
  1703. SDValue Cast = DAG.getBitcast(IVT, N->getOperand(0));
  1704. return DAG.getNode(GetPromotionOpcode(VT, NVT), SDLoc(N), NVT, Cast);
  1705. }
  1706. SDValue DAGTypeLegalizer::PromoteFloatRes_ConstantFP(SDNode *N) {
  1707. ConstantFPSDNode *CFPNode = cast<ConstantFPSDNode>(N);
  1708. EVT VT = N->getValueType(0);
  1709. SDLoc DL(N);
  1710. // Get the (bit-cast) APInt of the APFloat and build an integer constant
  1711. EVT IVT = EVT::getIntegerVT(*DAG.getContext(), VT.getSizeInBits());
  1712. SDValue C = DAG.getConstant(CFPNode->getValueAPF().bitcastToAPInt(), DL,
  1713. IVT);
  1714. // Convert the Constant to the desired FP type
  1715. // FIXME We might be able to do the conversion during compilation and get rid
  1716. // of it from the object code
  1717. EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
  1718. return DAG.getNode(GetPromotionOpcode(VT, NVT), DL, NVT, C);
  1719. }
  1720. // If the Index operand is a constant, try to redirect the extract operation to
  1721. // the correct legalized vector. If not, bit-convert the input vector to
  1722. // equivalent integer vector. Extract the element as an (bit-cast) integer
  1723. // value and convert it to the promoted type.
  1724. SDValue DAGTypeLegalizer::PromoteFloatRes_EXTRACT_VECTOR_ELT(SDNode *N) {
  1725. SDLoc DL(N);
  1726. // If the index is constant, try to extract the value from the legalized
  1727. // vector type.
  1728. if (isa<ConstantSDNode>(N->getOperand(1))) {
  1729. SDValue Vec = N->getOperand(0);
  1730. SDValue Idx = N->getOperand(1);
  1731. EVT VecVT = Vec->getValueType(0);
  1732. EVT EltVT = VecVT.getVectorElementType();
  1733. uint64_t IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
  1734. switch (getTypeAction(VecVT)) {
  1735. default: break;
  1736. case TargetLowering::TypeScalarizeVector: {
  1737. SDValue Res = GetScalarizedVector(N->getOperand(0));
  1738. ReplaceValueWith(SDValue(N, 0), Res);
  1739. return SDValue();
  1740. }
  1741. case TargetLowering::TypeWidenVector: {
  1742. Vec = GetWidenedVector(Vec);
  1743. SDValue Res = DAG.getNode(N->getOpcode(), DL, EltVT, Vec, Idx);
  1744. ReplaceValueWith(SDValue(N, 0), Res);
  1745. return SDValue();
  1746. }
  1747. case TargetLowering::TypeSplitVector: {
  1748. SDValue Lo, Hi;
  1749. GetSplitVector(Vec, Lo, Hi);
  1750. uint64_t LoElts = Lo.getValueType().getVectorNumElements();
  1751. SDValue Res;
  1752. if (IdxVal < LoElts)
  1753. Res = DAG.getNode(N->getOpcode(), DL, EltVT, Lo, Idx);
  1754. else
  1755. Res = DAG.getNode(N->getOpcode(), DL, EltVT, Hi,
  1756. DAG.getConstant(IdxVal - LoElts, DL,
  1757. Idx.getValueType()));
  1758. ReplaceValueWith(SDValue(N, 0), Res);
  1759. return SDValue();
  1760. }
  1761. }
  1762. }
  1763. // Bit-convert the input vector to the equivalent integer vector
  1764. SDValue NewOp = BitConvertVectorToIntegerVector(N->getOperand(0));
  1765. EVT IVT = NewOp.getValueType().getVectorElementType();
  1766. // Extract the element as an (bit-cast) integer value
  1767. SDValue NewVal = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, IVT,
  1768. NewOp, N->getOperand(1));
  1769. // Convert the element to the desired FP type
  1770. EVT VT = N->getValueType(0);
  1771. EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
  1772. return DAG.getNode(GetPromotionOpcode(VT, NVT), SDLoc(N), NVT, NewVal);
  1773. }
  1774. // FCOPYSIGN(X, Y) returns the value of X with the sign of Y. If the result
  1775. // needs promotion, so does the argument X. Note that Y, if needed, will be
  1776. // handled during operand promotion.
  1777. SDValue DAGTypeLegalizer::PromoteFloatRes_FCOPYSIGN(SDNode *N) {
  1778. EVT VT = N->getValueType(0);
  1779. EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
  1780. SDValue Op0 = GetPromotedFloat(N->getOperand(0));
  1781. SDValue Op1 = N->getOperand(1);
  1782. return DAG.getNode(N->getOpcode(), SDLoc(N), NVT, Op0, Op1);
  1783. }
  1784. // Unary operation where the result and the operand have PromoteFloat type
  1785. // action. Construct a new SDNode with the promoted float value of the old
  1786. // operand.
  1787. SDValue DAGTypeLegalizer::PromoteFloatRes_UnaryOp(SDNode *N) {
  1788. EVT VT = N->getValueType(0);
  1789. EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
  1790. SDValue Op = GetPromotedFloat(N->getOperand(0));
  1791. return DAG.getNode(N->getOpcode(), SDLoc(N), NVT, Op);
  1792. }
  1793. // Binary operations where the result and both operands have PromoteFloat type
  1794. // action. Construct a new SDNode with the promoted float values of the old
  1795. // operands.
  1796. SDValue DAGTypeLegalizer::PromoteFloatRes_BinOp(SDNode *N) {
  1797. EVT VT = N->getValueType(0);
  1798. EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
  1799. SDValue Op0 = GetPromotedFloat(N->getOperand(0));
  1800. SDValue Op1 = GetPromotedFloat(N->getOperand(1));
  1801. return DAG.getNode(N->getOpcode(), SDLoc(N), NVT, Op0, Op1, N->getFlags());
  1802. }
  1803. SDValue DAGTypeLegalizer::PromoteFloatRes_FMAD(SDNode *N) {
  1804. EVT VT = N->getValueType(0);
  1805. EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
  1806. SDValue Op0 = GetPromotedFloat(N->getOperand(0));
  1807. SDValue Op1 = GetPromotedFloat(N->getOperand(1));
  1808. SDValue Op2 = GetPromotedFloat(N->getOperand(2));
  1809. return DAG.getNode(N->getOpcode(), SDLoc(N), NVT, Op0, Op1, Op2);
  1810. }
  1811. // Promote the Float (first) operand and retain the Integer (second) operand
  1812. SDValue DAGTypeLegalizer::PromoteFloatRes_FPOWI(SDNode *N) {
  1813. EVT VT = N->getValueType(0);
  1814. EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
  1815. SDValue Op0 = GetPromotedFloat(N->getOperand(0));
  1816. SDValue Op1 = N->getOperand(1);
  1817. return DAG.getNode(N->getOpcode(), SDLoc(N), NVT, Op0, Op1);
  1818. }
  1819. // Explicit operation to reduce precision. Reduce the value to half precision
  1820. // and promote it back to the legal type.
  1821. SDValue DAGTypeLegalizer::PromoteFloatRes_FP_ROUND(SDNode *N) {
  1822. SDLoc DL(N);
  1823. SDValue Op = N->getOperand(0);
  1824. EVT VT = N->getValueType(0);
  1825. EVT OpVT = Op->getValueType(0);
  1826. EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
  1827. EVT IVT = EVT::getIntegerVT(*DAG.getContext(), VT.getSizeInBits());
  1828. // Round promoted float to desired precision
  1829. SDValue Round = DAG.getNode(GetPromotionOpcode(OpVT, VT), DL, IVT, Op);
  1830. // Promote it back to the legal output type
  1831. return DAG.getNode(GetPromotionOpcode(VT, NVT), DL, NVT, Round);
  1832. }
  1833. SDValue DAGTypeLegalizer::PromoteFloatRes_LOAD(SDNode *N) {
  1834. LoadSDNode *L = cast<LoadSDNode>(N);
  1835. EVT VT = N->getValueType(0);
  1836. // Load the value as an integer value with the same number of bits.
  1837. EVT IVT = EVT::getIntegerVT(*DAG.getContext(), VT.getSizeInBits());
  1838. SDValue newL = DAG.getLoad(L->getAddressingMode(), L->getExtensionType(), IVT,
  1839. SDLoc(N), L->getChain(), L->getBasePtr(),
  1840. L->getOffset(), L->getPointerInfo(), IVT,
  1841. L->getAlignment(),
  1842. L->getMemOperand()->getFlags(),
  1843. L->getAAInfo());
  1844. // Legalize the chain result by replacing uses of the old value chain with the
  1845. // new one
  1846. ReplaceValueWith(SDValue(N, 1), newL.getValue(1));
  1847. // Convert the integer value to the desired FP type
  1848. EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
  1849. return DAG.getNode(GetPromotionOpcode(VT, NVT), SDLoc(N), NVT, newL);
  1850. }
  1851. // Construct a new SELECT node with the promoted true- and false- values.
  1852. SDValue DAGTypeLegalizer::PromoteFloatRes_SELECT(SDNode *N) {
  1853. SDValue TrueVal = GetPromotedFloat(N->getOperand(1));
  1854. SDValue FalseVal = GetPromotedFloat(N->getOperand(2));
  1855. return DAG.getNode(ISD::SELECT, SDLoc(N), TrueVal->getValueType(0),
  1856. N->getOperand(0), TrueVal, FalseVal);
  1857. }
  1858. // Construct a new SELECT_CC node with the promoted true- and false- values.
  1859. // The operands used for comparison are promoted by PromoteFloatOp_SELECT_CC.
  1860. SDValue DAGTypeLegalizer::PromoteFloatRes_SELECT_CC(SDNode *N) {
  1861. SDValue TrueVal = GetPromotedFloat(N->getOperand(2));
  1862. SDValue FalseVal = GetPromotedFloat(N->getOperand(3));
  1863. return DAG.getNode(ISD::SELECT_CC, SDLoc(N),
  1864. TrueVal.getNode()->getValueType(0), N->getOperand(0),
  1865. N->getOperand(1), TrueVal, FalseVal, N->getOperand(4));
  1866. }
  1867. // Construct a SDNode that transforms the SINT or UINT operand to the promoted
  1868. // float type.
  1869. SDValue DAGTypeLegalizer::PromoteFloatRes_XINT_TO_FP(SDNode *N) {
  1870. SDLoc DL(N);
  1871. EVT VT = N->getValueType(0);
  1872. EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
  1873. SDValue NV = DAG.getNode(N->getOpcode(), DL, NVT, N->getOperand(0));
  1874. // Round the value to the desired precision (that of the source type).
  1875. return DAG.getNode(
  1876. ISD::FP_EXTEND, DL, NVT,
  1877. DAG.getNode(ISD::FP_ROUND, DL, VT, NV, DAG.getIntPtrConstant(0, DL)));
  1878. }
  1879. SDValue DAGTypeLegalizer::PromoteFloatRes_UNDEF(SDNode *N) {
  1880. return DAG.getUNDEF(TLI.getTypeToTransformTo(*DAG.getContext(),
  1881. N->getValueType(0)));
  1882. }
  1883. SDValue DAGTypeLegalizer::BitcastToInt_ATOMIC_SWAP(SDNode *N) {
  1884. EVT VT = N->getValueType(0);
  1885. EVT NFPVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
  1886. AtomicSDNode *AM = cast<AtomicSDNode>(N);
  1887. SDLoc SL(N);
  1888. SDValue CastVal = BitConvertToInteger(AM->getVal());
  1889. EVT CastVT = CastVal.getValueType();
  1890. SDValue NewAtomic
  1891. = DAG.getAtomic(ISD::ATOMIC_SWAP, SL, CastVT,
  1892. DAG.getVTList(CastVT, MVT::Other),
  1893. { AM->getChain(), AM->getBasePtr(), CastVal },
  1894. AM->getMemOperand());
  1895. SDValue ResultCast = DAG.getNode(GetPromotionOpcode(VT, NFPVT), SL, NFPVT,
  1896. NewAtomic);
  1897. // Legalize the chain result by replacing uses of the old value chain with the
  1898. // new one
  1899. ReplaceValueWith(SDValue(N, 1), NewAtomic.getValue(1));
  1900. return ResultCast;
  1901. }