LegalizeFloatTypes.cpp 65 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451
  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. /// GetFPLibCall - Return the right libcall for the given floating point type.
  26. static RTLIB::Libcall GetFPLibCall(EVT VT,
  27. RTLIB::Libcall Call_F32,
  28. RTLIB::Libcall Call_F64,
  29. RTLIB::Libcall Call_F80,
  30. RTLIB::Libcall Call_F128,
  31. RTLIB::Libcall Call_PPCF128) {
  32. return
  33. VT == MVT::f32 ? Call_F32 :
  34. VT == MVT::f64 ? Call_F64 :
  35. VT == MVT::f80 ? Call_F80 :
  36. VT == MVT::f128 ? Call_F128 :
  37. VT == MVT::ppcf128 ? Call_PPCF128 :
  38. RTLIB::UNKNOWN_LIBCALL;
  39. }
  40. //===----------------------------------------------------------------------===//
  41. // Result Float to Integer Conversion.
  42. //===----------------------------------------------------------------------===//
  43. void DAGTypeLegalizer::SoftenFloatResult(SDNode *N, unsigned ResNo) {
  44. DEBUG(dbgs() << "Soften float result " << ResNo << ": "; N->dump(&DAG);
  45. dbgs() << "\n");
  46. SDValue R = SDValue();
  47. switch (N->getOpcode()) {
  48. default:
  49. #ifndef NDEBUG
  50. dbgs() << "SoftenFloatResult #" << ResNo << ": ";
  51. N->dump(&DAG); dbgs() << "\n";
  52. #endif
  53. llvm_unreachable("Do not know how to soften the result of this operator!");
  54. case ISD::MERGE_VALUES:R = SoftenFloatRes_MERGE_VALUES(N, ResNo); break;
  55. case ISD::BITCAST: R = SoftenFloatRes_BITCAST(N); break;
  56. case ISD::BUILD_PAIR: R = SoftenFloatRes_BUILD_PAIR(N); break;
  57. case ISD::ConstantFP:
  58. R = SoftenFloatRes_ConstantFP(cast<ConstantFPSDNode>(N));
  59. break;
  60. case ISD::EXTRACT_VECTOR_ELT:
  61. R = SoftenFloatRes_EXTRACT_VECTOR_ELT(N); break;
  62. case ISD::FABS: R = SoftenFloatRes_FABS(N); break;
  63. case ISD::FADD: R = SoftenFloatRes_FADD(N); break;
  64. case ISD::FCEIL: R = SoftenFloatRes_FCEIL(N); break;
  65. case ISD::FCOPYSIGN: R = SoftenFloatRes_FCOPYSIGN(N); break;
  66. case ISD::FCOS: R = SoftenFloatRes_FCOS(N); break;
  67. case ISD::FDIV: R = SoftenFloatRes_FDIV(N); break;
  68. case ISD::FEXP: R = SoftenFloatRes_FEXP(N); break;
  69. case ISD::FEXP2: R = SoftenFloatRes_FEXP2(N); break;
  70. case ISD::FFLOOR: R = SoftenFloatRes_FFLOOR(N); break;
  71. case ISD::FLOG: R = SoftenFloatRes_FLOG(N); break;
  72. case ISD::FLOG2: R = SoftenFloatRes_FLOG2(N); break;
  73. case ISD::FLOG10: R = SoftenFloatRes_FLOG10(N); break;
  74. case ISD::FMA: R = SoftenFloatRes_FMA(N); break;
  75. case ISD::FMUL: R = SoftenFloatRes_FMUL(N); break;
  76. case ISD::FNEARBYINT: R = SoftenFloatRes_FNEARBYINT(N); break;
  77. case ISD::FNEG: R = SoftenFloatRes_FNEG(N); break;
  78. case ISD::FP_EXTEND: R = SoftenFloatRes_FP_EXTEND(N); break;
  79. case ISD::FP_ROUND: R = SoftenFloatRes_FP_ROUND(N); break;
  80. case ISD::FP16_TO_FP32:R = SoftenFloatRes_FP16_TO_FP32(N); break;
  81. case ISD::FPOW: R = SoftenFloatRes_FPOW(N); break;
  82. case ISD::FPOWI: R = SoftenFloatRes_FPOWI(N); break;
  83. case ISD::FREM: R = SoftenFloatRes_FREM(N); break;
  84. case ISD::FRINT: R = SoftenFloatRes_FRINT(N); break;
  85. case ISD::FSIN: R = SoftenFloatRes_FSIN(N); break;
  86. case ISD::FSQRT: R = SoftenFloatRes_FSQRT(N); break;
  87. case ISD::FSUB: R = SoftenFloatRes_FSUB(N); break;
  88. case ISD::FTRUNC: R = SoftenFloatRes_FTRUNC(N); break;
  89. case ISD::LOAD: R = SoftenFloatRes_LOAD(N); break;
  90. case ISD::SELECT: R = SoftenFloatRes_SELECT(N); break;
  91. case ISD::SELECT_CC: R = SoftenFloatRes_SELECT_CC(N); break;
  92. case ISD::SINT_TO_FP:
  93. case ISD::UINT_TO_FP: R = SoftenFloatRes_XINT_TO_FP(N); break;
  94. case ISD::UNDEF: R = SoftenFloatRes_UNDEF(N); break;
  95. case ISD::VAARG: R = SoftenFloatRes_VAARG(N); break;
  96. }
  97. // If R is null, the sub-method took care of registering the result.
  98. if (R.getNode())
  99. SetSoftenedFloat(SDValue(N, ResNo), R);
  100. }
  101. SDValue DAGTypeLegalizer::SoftenFloatRes_BITCAST(SDNode *N) {
  102. return BitConvertToInteger(N->getOperand(0));
  103. }
  104. SDValue DAGTypeLegalizer::SoftenFloatRes_MERGE_VALUES(SDNode *N,
  105. unsigned ResNo) {
  106. SDValue Op = DisintegrateMERGE_VALUES(N, ResNo);
  107. return BitConvertToInteger(Op);
  108. }
  109. SDValue DAGTypeLegalizer::SoftenFloatRes_BUILD_PAIR(SDNode *N) {
  110. // Convert the inputs to integers, and build a new pair out of them.
  111. return DAG.getNode(ISD::BUILD_PAIR, N->getDebugLoc(),
  112. TLI.getTypeToTransformTo(*DAG.getContext(),
  113. N->getValueType(0)),
  114. BitConvertToInteger(N->getOperand(0)),
  115. BitConvertToInteger(N->getOperand(1)));
  116. }
  117. SDValue DAGTypeLegalizer::SoftenFloatRes_ConstantFP(ConstantFPSDNode *N) {
  118. return DAG.getConstant(N->getValueAPF().bitcastToAPInt(),
  119. TLI.getTypeToTransformTo(*DAG.getContext(),
  120. N->getValueType(0)));
  121. }
  122. SDValue DAGTypeLegalizer::SoftenFloatRes_EXTRACT_VECTOR_ELT(SDNode *N) {
  123. SDValue NewOp = BitConvertVectorToIntegerVector(N->getOperand(0));
  124. return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, N->getDebugLoc(),
  125. NewOp.getValueType().getVectorElementType(),
  126. NewOp, N->getOperand(1));
  127. }
  128. SDValue DAGTypeLegalizer::SoftenFloatRes_FABS(SDNode *N) {
  129. EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
  130. unsigned Size = NVT.getSizeInBits();
  131. // Mask = ~(1 << (Size-1))
  132. APInt API = APInt::getAllOnesValue(Size);
  133. API.clearBit(Size-1);
  134. SDValue Mask = DAG.getConstant(API, NVT);
  135. SDValue Op = GetSoftenedFloat(N->getOperand(0));
  136. return DAG.getNode(ISD::AND, N->getDebugLoc(), NVT, Op, Mask);
  137. }
  138. SDValue DAGTypeLegalizer::SoftenFloatRes_FADD(SDNode *N) {
  139. EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
  140. SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
  141. GetSoftenedFloat(N->getOperand(1)) };
  142. return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
  143. RTLIB::ADD_F32,
  144. RTLIB::ADD_F64,
  145. RTLIB::ADD_F80,
  146. RTLIB::ADD_F128,
  147. RTLIB::ADD_PPCF128),
  148. NVT, Ops, 2, false, N->getDebugLoc());
  149. }
  150. SDValue DAGTypeLegalizer::SoftenFloatRes_FCEIL(SDNode *N) {
  151. EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
  152. SDValue Op = GetSoftenedFloat(N->getOperand(0));
  153. return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
  154. RTLIB::CEIL_F32,
  155. RTLIB::CEIL_F64,
  156. RTLIB::CEIL_F80,
  157. RTLIB::CEIL_F128,
  158. RTLIB::CEIL_PPCF128),
  159. NVT, &Op, 1, false, N->getDebugLoc());
  160. }
  161. SDValue DAGTypeLegalizer::SoftenFloatRes_FCOPYSIGN(SDNode *N) {
  162. SDValue LHS = GetSoftenedFloat(N->getOperand(0));
  163. SDValue RHS = BitConvertToInteger(N->getOperand(1));
  164. DebugLoc dl = N->getDebugLoc();
  165. EVT LVT = LHS.getValueType();
  166. EVT RVT = RHS.getValueType();
  167. unsigned LSize = LVT.getSizeInBits();
  168. unsigned RSize = RVT.getSizeInBits();
  169. // First get the sign bit of second operand.
  170. SDValue SignBit = DAG.getNode(ISD::SHL, dl, RVT, DAG.getConstant(1, RVT),
  171. DAG.getConstant(RSize - 1,
  172. TLI.getShiftAmountTy(RVT)));
  173. SignBit = DAG.getNode(ISD::AND, dl, RVT, RHS, SignBit);
  174. // Shift right or sign-extend it if the two operands have different types.
  175. int SizeDiff = RVT.getSizeInBits() - LVT.getSizeInBits();
  176. if (SizeDiff > 0) {
  177. SignBit = DAG.getNode(ISD::SRL, dl, RVT, SignBit,
  178. DAG.getConstant(SizeDiff,
  179. TLI.getShiftAmountTy(SignBit.getValueType())));
  180. SignBit = DAG.getNode(ISD::TRUNCATE, dl, LVT, SignBit);
  181. } else if (SizeDiff < 0) {
  182. SignBit = DAG.getNode(ISD::ANY_EXTEND, dl, LVT, SignBit);
  183. SignBit = DAG.getNode(ISD::SHL, dl, LVT, SignBit,
  184. DAG.getConstant(-SizeDiff,
  185. TLI.getShiftAmountTy(SignBit.getValueType())));
  186. }
  187. // Clear the sign bit of the first operand.
  188. SDValue Mask = DAG.getNode(ISD::SHL, dl, LVT, DAG.getConstant(1, LVT),
  189. DAG.getConstant(LSize - 1,
  190. TLI.getShiftAmountTy(LVT)));
  191. Mask = DAG.getNode(ISD::SUB, dl, LVT, Mask, DAG.getConstant(1, LVT));
  192. LHS = DAG.getNode(ISD::AND, dl, LVT, LHS, Mask);
  193. // Or the value with the sign bit.
  194. return DAG.getNode(ISD::OR, dl, LVT, LHS, SignBit);
  195. }
  196. SDValue DAGTypeLegalizer::SoftenFloatRes_FCOS(SDNode *N) {
  197. EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
  198. SDValue Op = GetSoftenedFloat(N->getOperand(0));
  199. return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
  200. RTLIB::COS_F32,
  201. RTLIB::COS_F64,
  202. RTLIB::COS_F80,
  203. RTLIB::COS_F128,
  204. RTLIB::COS_PPCF128),
  205. NVT, &Op, 1, false, N->getDebugLoc());
  206. }
  207. SDValue DAGTypeLegalizer::SoftenFloatRes_FDIV(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::DIV_F32,
  213. RTLIB::DIV_F64,
  214. RTLIB::DIV_F80,
  215. RTLIB::DIV_F128,
  216. RTLIB::DIV_PPCF128),
  217. NVT, Ops, 2, false, N->getDebugLoc());
  218. }
  219. SDValue DAGTypeLegalizer::SoftenFloatRes_FEXP(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::EXP_F32,
  224. RTLIB::EXP_F64,
  225. RTLIB::EXP_F80,
  226. RTLIB::EXP_F128,
  227. RTLIB::EXP_PPCF128),
  228. NVT, &Op, 1, false, N->getDebugLoc());
  229. }
  230. SDValue DAGTypeLegalizer::SoftenFloatRes_FEXP2(SDNode *N) {
  231. EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
  232. SDValue Op = GetSoftenedFloat(N->getOperand(0));
  233. return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
  234. RTLIB::EXP2_F32,
  235. RTLIB::EXP2_F64,
  236. RTLIB::EXP2_F80,
  237. RTLIB::EXP2_F128,
  238. RTLIB::EXP2_PPCF128),
  239. NVT, &Op, 1, false, N->getDebugLoc());
  240. }
  241. SDValue DAGTypeLegalizer::SoftenFloatRes_FFLOOR(SDNode *N) {
  242. EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
  243. SDValue Op = GetSoftenedFloat(N->getOperand(0));
  244. return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
  245. RTLIB::FLOOR_F32,
  246. RTLIB::FLOOR_F64,
  247. RTLIB::FLOOR_F80,
  248. RTLIB::FLOOR_F128,
  249. RTLIB::FLOOR_PPCF128),
  250. NVT, &Op, 1, false, N->getDebugLoc());
  251. }
  252. SDValue DAGTypeLegalizer::SoftenFloatRes_FLOG(SDNode *N) {
  253. EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
  254. SDValue Op = GetSoftenedFloat(N->getOperand(0));
  255. return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
  256. RTLIB::LOG_F32,
  257. RTLIB::LOG_F64,
  258. RTLIB::LOG_F80,
  259. RTLIB::LOG_F128,
  260. RTLIB::LOG_PPCF128),
  261. NVT, &Op, 1, false, N->getDebugLoc());
  262. }
  263. SDValue DAGTypeLegalizer::SoftenFloatRes_FLOG2(SDNode *N) {
  264. EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
  265. SDValue Op = GetSoftenedFloat(N->getOperand(0));
  266. return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
  267. RTLIB::LOG2_F32,
  268. RTLIB::LOG2_F64,
  269. RTLIB::LOG2_F80,
  270. RTLIB::LOG2_F128,
  271. RTLIB::LOG2_PPCF128),
  272. NVT, &Op, 1, false, N->getDebugLoc());
  273. }
  274. SDValue DAGTypeLegalizer::SoftenFloatRes_FLOG10(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::LOG10_F32,
  279. RTLIB::LOG10_F64,
  280. RTLIB::LOG10_F80,
  281. RTLIB::LOG10_F128,
  282. RTLIB::LOG10_PPCF128),
  283. NVT, &Op, 1, false, N->getDebugLoc());
  284. }
  285. SDValue DAGTypeLegalizer::SoftenFloatRes_FMA(SDNode *N) {
  286. EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
  287. SDValue Ops[3] = { GetSoftenedFloat(N->getOperand(0)),
  288. GetSoftenedFloat(N->getOperand(1)),
  289. GetSoftenedFloat(N->getOperand(2)) };
  290. return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
  291. RTLIB::FMA_F32,
  292. RTLIB::FMA_F64,
  293. RTLIB::FMA_F80,
  294. RTLIB::FMA_F128,
  295. RTLIB::FMA_PPCF128),
  296. NVT, Ops, 3, false, N->getDebugLoc());
  297. }
  298. SDValue DAGTypeLegalizer::SoftenFloatRes_FMUL(SDNode *N) {
  299. EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
  300. SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
  301. GetSoftenedFloat(N->getOperand(1)) };
  302. return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
  303. RTLIB::MUL_F32,
  304. RTLIB::MUL_F64,
  305. RTLIB::MUL_F80,
  306. RTLIB::MUL_F128,
  307. RTLIB::MUL_PPCF128),
  308. NVT, Ops, 2, false, N->getDebugLoc());
  309. }
  310. SDValue DAGTypeLegalizer::SoftenFloatRes_FNEARBYINT(SDNode *N) {
  311. EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
  312. SDValue Op = GetSoftenedFloat(N->getOperand(0));
  313. return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
  314. RTLIB::NEARBYINT_F32,
  315. RTLIB::NEARBYINT_F64,
  316. RTLIB::NEARBYINT_F80,
  317. RTLIB::NEARBYINT_F128,
  318. RTLIB::NEARBYINT_PPCF128),
  319. NVT, &Op, 1, false, N->getDebugLoc());
  320. }
  321. SDValue DAGTypeLegalizer::SoftenFloatRes_FNEG(SDNode *N) {
  322. EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
  323. // Expand Y = FNEG(X) -> Y = SUB -0.0, X
  324. SDValue Ops[2] = { DAG.getConstantFP(-0.0, N->getValueType(0)),
  325. GetSoftenedFloat(N->getOperand(0)) };
  326. return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
  327. RTLIB::SUB_F32,
  328. RTLIB::SUB_F64,
  329. RTLIB::SUB_F80,
  330. RTLIB::SUB_F128,
  331. RTLIB::SUB_PPCF128),
  332. NVT, Ops, 2, false, N->getDebugLoc());
  333. }
  334. SDValue DAGTypeLegalizer::SoftenFloatRes_FP_EXTEND(SDNode *N) {
  335. EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
  336. SDValue Op = N->getOperand(0);
  337. RTLIB::Libcall LC = RTLIB::getFPEXT(Op.getValueType(), N->getValueType(0));
  338. assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_EXTEND!");
  339. return TLI.makeLibCall(DAG, LC, NVT, &Op, 1, false, N->getDebugLoc());
  340. }
  341. // FIXME: Should we just use 'normal' FP_EXTEND / FP_TRUNC instead of special
  342. // nodes?
  343. SDValue DAGTypeLegalizer::SoftenFloatRes_FP16_TO_FP32(SDNode *N) {
  344. EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
  345. SDValue Op = N->getOperand(0);
  346. return TLI.makeLibCall(DAG, RTLIB::FPEXT_F16_F32, NVT, &Op, 1, false,
  347. N->getDebugLoc());
  348. }
  349. SDValue DAGTypeLegalizer::SoftenFloatRes_FP_ROUND(SDNode *N) {
  350. EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
  351. SDValue Op = N->getOperand(0);
  352. RTLIB::Libcall LC = RTLIB::getFPROUND(Op.getValueType(), N->getValueType(0));
  353. assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_ROUND!");
  354. return TLI.makeLibCall(DAG, LC, NVT, &Op, 1, false, N->getDebugLoc());
  355. }
  356. SDValue DAGTypeLegalizer::SoftenFloatRes_FPOW(SDNode *N) {
  357. EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
  358. SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
  359. GetSoftenedFloat(N->getOperand(1)) };
  360. return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
  361. RTLIB::POW_F32,
  362. RTLIB::POW_F64,
  363. RTLIB::POW_F80,
  364. RTLIB::POW_F128,
  365. RTLIB::POW_PPCF128),
  366. NVT, Ops, 2, false, N->getDebugLoc());
  367. }
  368. SDValue DAGTypeLegalizer::SoftenFloatRes_FPOWI(SDNode *N) {
  369. assert(N->getOperand(1).getValueType() == MVT::i32 &&
  370. "Unsupported power type!");
  371. EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
  372. SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)), N->getOperand(1) };
  373. return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
  374. RTLIB::POWI_F32,
  375. RTLIB::POWI_F64,
  376. RTLIB::POWI_F80,
  377. RTLIB::POWI_F128,
  378. RTLIB::POWI_PPCF128),
  379. NVT, Ops, 2, false, N->getDebugLoc());
  380. }
  381. SDValue DAGTypeLegalizer::SoftenFloatRes_FREM(SDNode *N) {
  382. EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
  383. SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
  384. GetSoftenedFloat(N->getOperand(1)) };
  385. return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
  386. RTLIB::REM_F32,
  387. RTLIB::REM_F64,
  388. RTLIB::REM_F80,
  389. RTLIB::REM_F128,
  390. RTLIB::REM_PPCF128),
  391. NVT, Ops, 2, false, N->getDebugLoc());
  392. }
  393. SDValue DAGTypeLegalizer::SoftenFloatRes_FRINT(SDNode *N) {
  394. EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
  395. SDValue Op = GetSoftenedFloat(N->getOperand(0));
  396. return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
  397. RTLIB::RINT_F32,
  398. RTLIB::RINT_F64,
  399. RTLIB::RINT_F80,
  400. RTLIB::RINT_F128,
  401. RTLIB::RINT_PPCF128),
  402. NVT, &Op, 1, false, N->getDebugLoc());
  403. }
  404. SDValue DAGTypeLegalizer::SoftenFloatRes_FSIN(SDNode *N) {
  405. EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
  406. SDValue Op = GetSoftenedFloat(N->getOperand(0));
  407. return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
  408. RTLIB::SIN_F32,
  409. RTLIB::SIN_F64,
  410. RTLIB::SIN_F80,
  411. RTLIB::SIN_F128,
  412. RTLIB::SIN_PPCF128),
  413. NVT, &Op, 1, false, N->getDebugLoc());
  414. }
  415. SDValue DAGTypeLegalizer::SoftenFloatRes_FSQRT(SDNode *N) {
  416. EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
  417. SDValue Op = GetSoftenedFloat(N->getOperand(0));
  418. return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
  419. RTLIB::SQRT_F32,
  420. RTLIB::SQRT_F64,
  421. RTLIB::SQRT_F80,
  422. RTLIB::SQRT_F128,
  423. RTLIB::SQRT_PPCF128),
  424. NVT, &Op, 1, false, N->getDebugLoc());
  425. }
  426. SDValue DAGTypeLegalizer::SoftenFloatRes_FSUB(SDNode *N) {
  427. EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
  428. SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
  429. GetSoftenedFloat(N->getOperand(1)) };
  430. return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
  431. RTLIB::SUB_F32,
  432. RTLIB::SUB_F64,
  433. RTLIB::SUB_F80,
  434. RTLIB::SUB_F128,
  435. RTLIB::SUB_PPCF128),
  436. NVT, Ops, 2, false, N->getDebugLoc());
  437. }
  438. SDValue DAGTypeLegalizer::SoftenFloatRes_FTRUNC(SDNode *N) {
  439. EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
  440. SDValue Op = GetSoftenedFloat(N->getOperand(0));
  441. return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
  442. RTLIB::TRUNC_F32,
  443. RTLIB::TRUNC_F64,
  444. RTLIB::TRUNC_F80,
  445. RTLIB::TRUNC_F128,
  446. RTLIB::TRUNC_PPCF128),
  447. NVT, &Op, 1, false, N->getDebugLoc());
  448. }
  449. SDValue DAGTypeLegalizer::SoftenFloatRes_LOAD(SDNode *N) {
  450. LoadSDNode *L = cast<LoadSDNode>(N);
  451. EVT VT = N->getValueType(0);
  452. EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
  453. DebugLoc dl = N->getDebugLoc();
  454. SDValue NewL;
  455. if (L->getExtensionType() == ISD::NON_EXTLOAD) {
  456. NewL = DAG.getLoad(L->getAddressingMode(), L->getExtensionType(),
  457. NVT, dl, L->getChain(), L->getBasePtr(), L->getOffset(),
  458. L->getPointerInfo(), NVT, L->isVolatile(),
  459. L->isNonTemporal(), false, L->getAlignment());
  460. // Legalized the chain result - switch anything that used the old chain to
  461. // use the new one.
  462. ReplaceValueWith(SDValue(N, 1), NewL.getValue(1));
  463. return NewL;
  464. }
  465. // Do a non-extending load followed by FP_EXTEND.
  466. NewL = DAG.getLoad(L->getAddressingMode(), ISD::NON_EXTLOAD,
  467. L->getMemoryVT(), dl, L->getChain(),
  468. L->getBasePtr(), L->getOffset(), L->getPointerInfo(),
  469. L->getMemoryVT(), L->isVolatile(),
  470. L->isNonTemporal(), false, L->getAlignment());
  471. // Legalized the chain result - switch anything that used the old chain to
  472. // use the new one.
  473. ReplaceValueWith(SDValue(N, 1), NewL.getValue(1));
  474. return BitConvertToInteger(DAG.getNode(ISD::FP_EXTEND, dl, VT, NewL));
  475. }
  476. SDValue DAGTypeLegalizer::SoftenFloatRes_SELECT(SDNode *N) {
  477. SDValue LHS = GetSoftenedFloat(N->getOperand(1));
  478. SDValue RHS = GetSoftenedFloat(N->getOperand(2));
  479. return DAG.getNode(ISD::SELECT, N->getDebugLoc(),
  480. LHS.getValueType(), N->getOperand(0),LHS,RHS);
  481. }
  482. SDValue DAGTypeLegalizer::SoftenFloatRes_SELECT_CC(SDNode *N) {
  483. SDValue LHS = GetSoftenedFloat(N->getOperand(2));
  484. SDValue RHS = GetSoftenedFloat(N->getOperand(3));
  485. return DAG.getNode(ISD::SELECT_CC, N->getDebugLoc(),
  486. LHS.getValueType(), N->getOperand(0),
  487. N->getOperand(1), LHS, RHS, N->getOperand(4));
  488. }
  489. SDValue DAGTypeLegalizer::SoftenFloatRes_UNDEF(SDNode *N) {
  490. return DAG.getUNDEF(TLI.getTypeToTransformTo(*DAG.getContext(),
  491. N->getValueType(0)));
  492. }
  493. SDValue DAGTypeLegalizer::SoftenFloatRes_VAARG(SDNode *N) {
  494. SDValue Chain = N->getOperand(0); // Get the chain.
  495. SDValue Ptr = N->getOperand(1); // Get the pointer.
  496. EVT VT = N->getValueType(0);
  497. EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
  498. DebugLoc dl = N->getDebugLoc();
  499. SDValue NewVAARG;
  500. NewVAARG = DAG.getVAArg(NVT, dl, Chain, Ptr, N->getOperand(2),
  501. N->getConstantOperandVal(3));
  502. // Legalized the chain result - switch anything that used the old chain to
  503. // use the new one.
  504. ReplaceValueWith(SDValue(N, 1), NewVAARG.getValue(1));
  505. return NewVAARG;
  506. }
  507. SDValue DAGTypeLegalizer::SoftenFloatRes_XINT_TO_FP(SDNode *N) {
  508. bool Signed = N->getOpcode() == ISD::SINT_TO_FP;
  509. EVT SVT = N->getOperand(0).getValueType();
  510. EVT RVT = N->getValueType(0);
  511. EVT NVT = EVT();
  512. DebugLoc dl = N->getDebugLoc();
  513. // If the input is not legal, eg: i1 -> fp, then it needs to be promoted to
  514. // a larger type, eg: i8 -> fp. Even if it is legal, no libcall may exactly
  515. // match. Look for an appropriate libcall.
  516. RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
  517. for (unsigned t = MVT::FIRST_INTEGER_VALUETYPE;
  518. t <= MVT::LAST_INTEGER_VALUETYPE && LC == RTLIB::UNKNOWN_LIBCALL; ++t) {
  519. NVT = (MVT::SimpleValueType)t;
  520. // The source needs to big enough to hold the operand.
  521. if (NVT.bitsGE(SVT))
  522. LC = Signed ? RTLIB::getSINTTOFP(NVT, RVT):RTLIB::getUINTTOFP (NVT, RVT);
  523. }
  524. assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported XINT_TO_FP!");
  525. // Sign/zero extend the argument if the libcall takes a larger type.
  526. SDValue Op = DAG.getNode(Signed ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND, dl,
  527. NVT, N->getOperand(0));
  528. return TLI.makeLibCall(DAG, LC,
  529. TLI.getTypeToTransformTo(*DAG.getContext(), RVT),
  530. &Op, 1, false, dl);
  531. }
  532. //===----------------------------------------------------------------------===//
  533. // Operand Float to Integer Conversion..
  534. //===----------------------------------------------------------------------===//
  535. bool DAGTypeLegalizer::SoftenFloatOperand(SDNode *N, unsigned OpNo) {
  536. DEBUG(dbgs() << "Soften float operand " << OpNo << ": "; N->dump(&DAG);
  537. dbgs() << "\n");
  538. SDValue Res = SDValue();
  539. switch (N->getOpcode()) {
  540. default:
  541. #ifndef NDEBUG
  542. dbgs() << "SoftenFloatOperand Op #" << OpNo << ": ";
  543. N->dump(&DAG); dbgs() << "\n";
  544. #endif
  545. llvm_unreachable("Do not know how to soften this operator's operand!");
  546. case ISD::BITCAST: Res = SoftenFloatOp_BITCAST(N); break;
  547. case ISD::BR_CC: Res = SoftenFloatOp_BR_CC(N); break;
  548. case ISD::FP_ROUND: Res = SoftenFloatOp_FP_ROUND(N); break;
  549. case ISD::FP_TO_SINT: Res = SoftenFloatOp_FP_TO_SINT(N); break;
  550. case ISD::FP_TO_UINT: Res = SoftenFloatOp_FP_TO_UINT(N); break;
  551. case ISD::FP32_TO_FP16:Res = SoftenFloatOp_FP32_TO_FP16(N); break;
  552. case ISD::SELECT_CC: Res = SoftenFloatOp_SELECT_CC(N); break;
  553. case ISD::SETCC: Res = SoftenFloatOp_SETCC(N); break;
  554. case ISD::STORE: Res = SoftenFloatOp_STORE(N, OpNo); break;
  555. }
  556. // If the result is null, the sub-method took care of registering results etc.
  557. if (!Res.getNode()) return false;
  558. // If the result is N, the sub-method updated N in place. Tell the legalizer
  559. // core about this.
  560. if (Res.getNode() == N)
  561. return true;
  562. assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 &&
  563. "Invalid operand expansion");
  564. ReplaceValueWith(SDValue(N, 0), Res);
  565. return false;
  566. }
  567. SDValue DAGTypeLegalizer::SoftenFloatOp_BITCAST(SDNode *N) {
  568. return DAG.getNode(ISD::BITCAST, N->getDebugLoc(), N->getValueType(0),
  569. GetSoftenedFloat(N->getOperand(0)));
  570. }
  571. SDValue DAGTypeLegalizer::SoftenFloatOp_FP_ROUND(SDNode *N) {
  572. EVT SVT = N->getOperand(0).getValueType();
  573. EVT RVT = N->getValueType(0);
  574. RTLIB::Libcall LC = RTLIB::getFPROUND(SVT, RVT);
  575. assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_ROUND libcall");
  576. SDValue Op = GetSoftenedFloat(N->getOperand(0));
  577. return TLI.makeLibCall(DAG, LC, RVT, &Op, 1, false, N->getDebugLoc());
  578. }
  579. SDValue DAGTypeLegalizer::SoftenFloatOp_BR_CC(SDNode *N) {
  580. SDValue NewLHS = N->getOperand(2), NewRHS = N->getOperand(3);
  581. ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(1))->get();
  582. EVT VT = NewLHS.getValueType();
  583. NewLHS = GetSoftenedFloat(NewLHS);
  584. NewRHS = GetSoftenedFloat(NewRHS);
  585. TLI.softenSetCCOperands(DAG, VT, NewLHS, NewRHS, CCCode, N->getDebugLoc());
  586. // If softenSetCCOperands returned a scalar, we need to compare the result
  587. // against zero to select between true and false values.
  588. if (NewRHS.getNode() == 0) {
  589. NewRHS = DAG.getConstant(0, NewLHS.getValueType());
  590. CCCode = ISD::SETNE;
  591. }
  592. // Update N to have the operands specified.
  593. return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0),
  594. DAG.getCondCode(CCCode), NewLHS, NewRHS,
  595. N->getOperand(4)),
  596. 0);
  597. }
  598. SDValue DAGTypeLegalizer::SoftenFloatOp_FP_TO_SINT(SDNode *N) {
  599. EVT RVT = N->getValueType(0);
  600. RTLIB::Libcall LC = RTLIB::getFPTOSINT(N->getOperand(0).getValueType(), RVT);
  601. assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_TO_SINT!");
  602. SDValue Op = GetSoftenedFloat(N->getOperand(0));
  603. return TLI.makeLibCall(DAG, LC, RVT, &Op, 1, false, N->getDebugLoc());
  604. }
  605. SDValue DAGTypeLegalizer::SoftenFloatOp_FP_TO_UINT(SDNode *N) {
  606. EVT RVT = N->getValueType(0);
  607. RTLIB::Libcall LC = RTLIB::getFPTOUINT(N->getOperand(0).getValueType(), RVT);
  608. assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_TO_UINT!");
  609. SDValue Op = GetSoftenedFloat(N->getOperand(0));
  610. return TLI.makeLibCall(DAG, LC, RVT, &Op, 1, false, N->getDebugLoc());
  611. }
  612. SDValue DAGTypeLegalizer::SoftenFloatOp_FP32_TO_FP16(SDNode *N) {
  613. EVT RVT = N->getValueType(0);
  614. RTLIB::Libcall LC = RTLIB::FPROUND_F32_F16;
  615. SDValue Op = GetSoftenedFloat(N->getOperand(0));
  616. return TLI.makeLibCall(DAG, LC, RVT, &Op, 1, false, N->getDebugLoc());
  617. }
  618. SDValue DAGTypeLegalizer::SoftenFloatOp_SELECT_CC(SDNode *N) {
  619. SDValue NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
  620. ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(4))->get();
  621. EVT VT = NewLHS.getValueType();
  622. NewLHS = GetSoftenedFloat(NewLHS);
  623. NewRHS = GetSoftenedFloat(NewRHS);
  624. TLI.softenSetCCOperands(DAG, VT, NewLHS, NewRHS, CCCode, N->getDebugLoc());
  625. // If softenSetCCOperands returned a scalar, we need to compare the result
  626. // against zero to select between true and false values.
  627. if (NewRHS.getNode() == 0) {
  628. NewRHS = DAG.getConstant(0, NewLHS.getValueType());
  629. CCCode = ISD::SETNE;
  630. }
  631. // Update N to have the operands specified.
  632. return SDValue(DAG.UpdateNodeOperands(N, NewLHS, NewRHS,
  633. N->getOperand(2), N->getOperand(3),
  634. DAG.getCondCode(CCCode)),
  635. 0);
  636. }
  637. SDValue DAGTypeLegalizer::SoftenFloatOp_SETCC(SDNode *N) {
  638. SDValue NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
  639. ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(2))->get();
  640. EVT VT = NewLHS.getValueType();
  641. NewLHS = GetSoftenedFloat(NewLHS);
  642. NewRHS = GetSoftenedFloat(NewRHS);
  643. TLI.softenSetCCOperands(DAG, VT, NewLHS, NewRHS, CCCode, N->getDebugLoc());
  644. // If softenSetCCOperands returned a scalar, use it.
  645. if (NewRHS.getNode() == 0) {
  646. assert(NewLHS.getValueType() == N->getValueType(0) &&
  647. "Unexpected setcc expansion!");
  648. return NewLHS;
  649. }
  650. // Otherwise, update N to have the operands specified.
  651. return SDValue(DAG.UpdateNodeOperands(N, NewLHS, NewRHS,
  652. DAG.getCondCode(CCCode)),
  653. 0);
  654. }
  655. SDValue DAGTypeLegalizer::SoftenFloatOp_STORE(SDNode *N, unsigned OpNo) {
  656. assert(ISD::isUNINDEXEDStore(N) && "Indexed store during type legalization!");
  657. assert(OpNo == 1 && "Can only soften the stored value!");
  658. StoreSDNode *ST = cast<StoreSDNode>(N);
  659. SDValue Val = ST->getValue();
  660. DebugLoc dl = N->getDebugLoc();
  661. if (ST->isTruncatingStore())
  662. // Do an FP_ROUND followed by a non-truncating store.
  663. Val = BitConvertToInteger(DAG.getNode(ISD::FP_ROUND, dl, ST->getMemoryVT(),
  664. Val, DAG.getIntPtrConstant(0)));
  665. else
  666. Val = GetSoftenedFloat(Val);
  667. return DAG.getStore(ST->getChain(), dl, Val, ST->getBasePtr(),
  668. ST->getPointerInfo(),
  669. ST->isVolatile(), ST->isNonTemporal(),
  670. ST->getAlignment());
  671. }
  672. //===----------------------------------------------------------------------===//
  673. // Float Result Expansion
  674. //===----------------------------------------------------------------------===//
  675. /// ExpandFloatResult - This method is called when the specified result of the
  676. /// specified node is found to need expansion. At this point, the node may also
  677. /// have invalid operands or may have other results that need promotion, we just
  678. /// know that (at least) one result needs expansion.
  679. void DAGTypeLegalizer::ExpandFloatResult(SDNode *N, unsigned ResNo) {
  680. DEBUG(dbgs() << "Expand float result: "; N->dump(&DAG); dbgs() << "\n");
  681. SDValue Lo, Hi;
  682. Lo = Hi = SDValue();
  683. // See if the target wants to custom expand this node.
  684. if (CustomLowerNode(N, N->getValueType(ResNo), true))
  685. return;
  686. switch (N->getOpcode()) {
  687. default:
  688. #ifndef NDEBUG
  689. dbgs() << "ExpandFloatResult #" << ResNo << ": ";
  690. N->dump(&DAG); dbgs() << "\n";
  691. #endif
  692. llvm_unreachable("Do not know how to expand the result of this operator!");
  693. case ISD::UNDEF: SplitRes_UNDEF(N, Lo, Hi); break;
  694. case ISD::SELECT: SplitRes_SELECT(N, Lo, Hi); break;
  695. case ISD::SELECT_CC: SplitRes_SELECT_CC(N, Lo, Hi); break;
  696. case ISD::MERGE_VALUES: ExpandRes_MERGE_VALUES(N, ResNo, Lo, Hi); break;
  697. case ISD::BITCAST: ExpandRes_BITCAST(N, Lo, Hi); break;
  698. case ISD::BUILD_PAIR: ExpandRes_BUILD_PAIR(N, Lo, Hi); break;
  699. case ISD::EXTRACT_ELEMENT: ExpandRes_EXTRACT_ELEMENT(N, Lo, Hi); break;
  700. case ISD::EXTRACT_VECTOR_ELT: ExpandRes_EXTRACT_VECTOR_ELT(N, Lo, Hi); break;
  701. case ISD::VAARG: ExpandRes_VAARG(N, Lo, Hi); break;
  702. case ISD::ConstantFP: ExpandFloatRes_ConstantFP(N, Lo, Hi); break;
  703. case ISD::FABS: ExpandFloatRes_FABS(N, Lo, Hi); break;
  704. case ISD::FADD: ExpandFloatRes_FADD(N, Lo, Hi); break;
  705. case ISD::FCEIL: ExpandFloatRes_FCEIL(N, Lo, Hi); break;
  706. case ISD::FCOPYSIGN: ExpandFloatRes_FCOPYSIGN(N, Lo, Hi); break;
  707. case ISD::FCOS: ExpandFloatRes_FCOS(N, Lo, Hi); break;
  708. case ISD::FDIV: ExpandFloatRes_FDIV(N, Lo, Hi); break;
  709. case ISD::FEXP: ExpandFloatRes_FEXP(N, Lo, Hi); break;
  710. case ISD::FEXP2: ExpandFloatRes_FEXP2(N, Lo, Hi); break;
  711. case ISD::FFLOOR: ExpandFloatRes_FFLOOR(N, Lo, Hi); break;
  712. case ISD::FLOG: ExpandFloatRes_FLOG(N, Lo, Hi); break;
  713. case ISD::FLOG2: ExpandFloatRes_FLOG2(N, Lo, Hi); break;
  714. case ISD::FLOG10: ExpandFloatRes_FLOG10(N, Lo, Hi); break;
  715. case ISD::FMA: ExpandFloatRes_FMA(N, Lo, Hi); break;
  716. case ISD::FMUL: ExpandFloatRes_FMUL(N, Lo, Hi); break;
  717. case ISD::FNEARBYINT: ExpandFloatRes_FNEARBYINT(N, Lo, Hi); break;
  718. case ISD::FNEG: ExpandFloatRes_FNEG(N, Lo, Hi); break;
  719. case ISD::FP_EXTEND: ExpandFloatRes_FP_EXTEND(N, Lo, Hi); break;
  720. case ISD::FPOW: ExpandFloatRes_FPOW(N, Lo, Hi); break;
  721. case ISD::FPOWI: ExpandFloatRes_FPOWI(N, Lo, Hi); break;
  722. case ISD::FRINT: ExpandFloatRes_FRINT(N, Lo, Hi); break;
  723. case ISD::FSIN: ExpandFloatRes_FSIN(N, Lo, Hi); break;
  724. case ISD::FSQRT: ExpandFloatRes_FSQRT(N, Lo, Hi); break;
  725. case ISD::FSUB: ExpandFloatRes_FSUB(N, Lo, Hi); break;
  726. case ISD::FTRUNC: ExpandFloatRes_FTRUNC(N, Lo, Hi); break;
  727. case ISD::LOAD: ExpandFloatRes_LOAD(N, Lo, Hi); break;
  728. case ISD::SINT_TO_FP:
  729. case ISD::UINT_TO_FP: ExpandFloatRes_XINT_TO_FP(N, Lo, Hi); break;
  730. case ISD::FREM: ExpandFloatRes_FREM(N, Lo, Hi); break;
  731. }
  732. // If Lo/Hi is null, the sub-method took care of registering results etc.
  733. if (Lo.getNode())
  734. SetExpandedFloat(SDValue(N, ResNo), Lo, Hi);
  735. }
  736. void DAGTypeLegalizer::ExpandFloatRes_ConstantFP(SDNode *N, SDValue &Lo,
  737. SDValue &Hi) {
  738. EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
  739. assert(NVT.getSizeInBits() == integerPartWidth &&
  740. "Do not know how to expand this float constant!");
  741. APInt C = cast<ConstantFPSDNode>(N)->getValueAPF().bitcastToAPInt();
  742. Lo = DAG.getConstantFP(APFloat(DAG.EVTToAPFloatSemantics(NVT),
  743. APInt(integerPartWidth, C.getRawData()[1])),
  744. NVT);
  745. Hi = DAG.getConstantFP(APFloat(DAG.EVTToAPFloatSemantics(NVT),
  746. APInt(integerPartWidth, C.getRawData()[0])),
  747. NVT);
  748. }
  749. void DAGTypeLegalizer::ExpandFloatRes_FABS(SDNode *N, SDValue &Lo,
  750. SDValue &Hi) {
  751. assert(N->getValueType(0) == MVT::ppcf128 &&
  752. "Logic only correct for ppcf128!");
  753. DebugLoc dl = N->getDebugLoc();
  754. SDValue Tmp;
  755. GetExpandedFloat(N->getOperand(0), Lo, Tmp);
  756. Hi = DAG.getNode(ISD::FABS, dl, Tmp.getValueType(), Tmp);
  757. // Lo = Hi==fabs(Hi) ? Lo : -Lo;
  758. Lo = DAG.getNode(ISD::SELECT_CC, dl, Lo.getValueType(), Tmp, Hi, Lo,
  759. DAG.getNode(ISD::FNEG, dl, Lo.getValueType(), Lo),
  760. DAG.getCondCode(ISD::SETEQ));
  761. }
  762. void DAGTypeLegalizer::ExpandFloatRes_FADD(SDNode *N, SDValue &Lo,
  763. SDValue &Hi) {
  764. SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
  765. RTLIB::ADD_F32, RTLIB::ADD_F64,
  766. RTLIB::ADD_F80, RTLIB::ADD_F128,
  767. RTLIB::ADD_PPCF128),
  768. N, false);
  769. GetPairElements(Call, Lo, Hi);
  770. }
  771. void DAGTypeLegalizer::ExpandFloatRes_FCEIL(SDNode *N,
  772. SDValue &Lo, SDValue &Hi) {
  773. SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
  774. RTLIB::CEIL_F32, RTLIB::CEIL_F64,
  775. RTLIB::CEIL_F80, RTLIB::CEIL_F128,
  776. RTLIB::CEIL_PPCF128),
  777. N, false);
  778. GetPairElements(Call, Lo, Hi);
  779. }
  780. void DAGTypeLegalizer::ExpandFloatRes_FCOPYSIGN(SDNode *N,
  781. SDValue &Lo, SDValue &Hi) {
  782. SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
  783. RTLIB::COPYSIGN_F32,
  784. RTLIB::COPYSIGN_F64,
  785. RTLIB::COPYSIGN_F80,
  786. RTLIB::COPYSIGN_F128,
  787. RTLIB::COPYSIGN_PPCF128),
  788. N, false);
  789. GetPairElements(Call, Lo, Hi);
  790. }
  791. void DAGTypeLegalizer::ExpandFloatRes_FCOS(SDNode *N,
  792. SDValue &Lo, SDValue &Hi) {
  793. SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
  794. RTLIB::COS_F32, RTLIB::COS_F64,
  795. RTLIB::COS_F80, RTLIB::COS_F128,
  796. RTLIB::COS_PPCF128),
  797. N, false);
  798. GetPairElements(Call, Lo, Hi);
  799. }
  800. void DAGTypeLegalizer::ExpandFloatRes_FDIV(SDNode *N, SDValue &Lo,
  801. SDValue &Hi) {
  802. SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
  803. SDValue Call = TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
  804. RTLIB::DIV_F32,
  805. RTLIB::DIV_F64,
  806. RTLIB::DIV_F80,
  807. RTLIB::DIV_F128,
  808. RTLIB::DIV_PPCF128),
  809. N->getValueType(0), Ops, 2, false,
  810. N->getDebugLoc());
  811. GetPairElements(Call, Lo, Hi);
  812. }
  813. void DAGTypeLegalizer::ExpandFloatRes_FEXP(SDNode *N,
  814. SDValue &Lo, SDValue &Hi) {
  815. SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
  816. RTLIB::EXP_F32, RTLIB::EXP_F64,
  817. RTLIB::EXP_F80, RTLIB::EXP_F128,
  818. RTLIB::EXP_PPCF128),
  819. N, false);
  820. GetPairElements(Call, Lo, Hi);
  821. }
  822. void DAGTypeLegalizer::ExpandFloatRes_FEXP2(SDNode *N,
  823. SDValue &Lo, SDValue &Hi) {
  824. SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
  825. RTLIB::EXP2_F32, RTLIB::EXP2_F64,
  826. RTLIB::EXP2_F80, RTLIB::EXP2_F128,
  827. RTLIB::EXP2_PPCF128),
  828. N, false);
  829. GetPairElements(Call, Lo, Hi);
  830. }
  831. void DAGTypeLegalizer::ExpandFloatRes_FFLOOR(SDNode *N,
  832. SDValue &Lo, SDValue &Hi) {
  833. SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
  834. RTLIB::FLOOR_F32, RTLIB::FLOOR_F64,
  835. RTLIB::FLOOR_F80, RTLIB::FLOOR_F128,
  836. RTLIB::FLOOR_PPCF128),
  837. N, false);
  838. GetPairElements(Call, Lo, Hi);
  839. }
  840. void DAGTypeLegalizer::ExpandFloatRes_FLOG(SDNode *N,
  841. SDValue &Lo, SDValue &Hi) {
  842. SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
  843. RTLIB::LOG_F32, RTLIB::LOG_F64,
  844. RTLIB::LOG_F80, RTLIB::LOG_F128,
  845. RTLIB::LOG_PPCF128),
  846. N, false);
  847. GetPairElements(Call, Lo, Hi);
  848. }
  849. void DAGTypeLegalizer::ExpandFloatRes_FLOG2(SDNode *N,
  850. SDValue &Lo, SDValue &Hi) {
  851. SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
  852. RTLIB::LOG2_F32, RTLIB::LOG2_F64,
  853. RTLIB::LOG2_F80, RTLIB::LOG2_F128,
  854. RTLIB::LOG2_PPCF128),
  855. N, false);
  856. GetPairElements(Call, Lo, Hi);
  857. }
  858. void DAGTypeLegalizer::ExpandFloatRes_FLOG10(SDNode *N,
  859. SDValue &Lo, SDValue &Hi) {
  860. SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
  861. RTLIB::LOG10_F32, RTLIB::LOG10_F64,
  862. RTLIB::LOG10_F80, RTLIB::LOG10_F128,
  863. RTLIB::LOG10_PPCF128),
  864. N, false);
  865. GetPairElements(Call, Lo, Hi);
  866. }
  867. void DAGTypeLegalizer::ExpandFloatRes_FMA(SDNode *N, SDValue &Lo,
  868. SDValue &Hi) {
  869. SDValue Ops[3] = { N->getOperand(0), N->getOperand(1), N->getOperand(2) };
  870. SDValue Call = TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
  871. RTLIB::FMA_F32,
  872. RTLIB::FMA_F64,
  873. RTLIB::FMA_F80,
  874. RTLIB::FMA_F128,
  875. RTLIB::FMA_PPCF128),
  876. N->getValueType(0), Ops, 3, false,
  877. N->getDebugLoc());
  878. GetPairElements(Call, Lo, Hi);
  879. }
  880. void DAGTypeLegalizer::ExpandFloatRes_FMUL(SDNode *N, SDValue &Lo,
  881. SDValue &Hi) {
  882. SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
  883. SDValue Call = TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
  884. RTLIB::MUL_F32,
  885. RTLIB::MUL_F64,
  886. RTLIB::MUL_F80,
  887. RTLIB::MUL_F128,
  888. RTLIB::MUL_PPCF128),
  889. N->getValueType(0), Ops, 2, false,
  890. N->getDebugLoc());
  891. GetPairElements(Call, Lo, Hi);
  892. }
  893. void DAGTypeLegalizer::ExpandFloatRes_FNEARBYINT(SDNode *N,
  894. SDValue &Lo, SDValue &Hi) {
  895. SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
  896. RTLIB::NEARBYINT_F32,
  897. RTLIB::NEARBYINT_F64,
  898. RTLIB::NEARBYINT_F80,
  899. RTLIB::NEARBYINT_F128,
  900. RTLIB::NEARBYINT_PPCF128),
  901. N, false);
  902. GetPairElements(Call, Lo, Hi);
  903. }
  904. void DAGTypeLegalizer::ExpandFloatRes_FNEG(SDNode *N, SDValue &Lo,
  905. SDValue &Hi) {
  906. DebugLoc dl = N->getDebugLoc();
  907. GetExpandedFloat(N->getOperand(0), Lo, Hi);
  908. Lo = DAG.getNode(ISD::FNEG, dl, Lo.getValueType(), Lo);
  909. Hi = DAG.getNode(ISD::FNEG, dl, Hi.getValueType(), Hi);
  910. }
  911. void DAGTypeLegalizer::ExpandFloatRes_FP_EXTEND(SDNode *N, SDValue &Lo,
  912. SDValue &Hi) {
  913. EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
  914. Hi = DAG.getNode(ISD::FP_EXTEND, N->getDebugLoc(), NVT, N->getOperand(0));
  915. Lo = DAG.getConstantFP(APFloat(DAG.EVTToAPFloatSemantics(NVT),
  916. APInt(NVT.getSizeInBits(), 0)), NVT);
  917. }
  918. void DAGTypeLegalizer::ExpandFloatRes_FPOW(SDNode *N,
  919. SDValue &Lo, SDValue &Hi) {
  920. SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
  921. RTLIB::POW_F32, RTLIB::POW_F64,
  922. RTLIB::POW_F80, RTLIB::POW_F128,
  923. RTLIB::POW_PPCF128),
  924. N, false);
  925. GetPairElements(Call, Lo, Hi);
  926. }
  927. void DAGTypeLegalizer::ExpandFloatRes_FPOWI(SDNode *N,
  928. SDValue &Lo, SDValue &Hi) {
  929. SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
  930. RTLIB::POWI_F32, RTLIB::POWI_F64,
  931. RTLIB::POWI_F80, RTLIB::POWI_F128,
  932. RTLIB::POWI_PPCF128),
  933. N, false);
  934. GetPairElements(Call, Lo, Hi);
  935. }
  936. void DAGTypeLegalizer::ExpandFloatRes_FREM(SDNode *N,
  937. SDValue &Lo, SDValue &Hi) {
  938. SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
  939. RTLIB::REM_F32, RTLIB::REM_F64,
  940. RTLIB::REM_F80, RTLIB::REM_F128,
  941. RTLIB::REM_PPCF128),
  942. N, false);
  943. GetPairElements(Call, Lo, Hi);
  944. }
  945. void DAGTypeLegalizer::ExpandFloatRes_FRINT(SDNode *N,
  946. SDValue &Lo, SDValue &Hi) {
  947. SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
  948. RTLIB::RINT_F32, RTLIB::RINT_F64,
  949. RTLIB::RINT_F80, RTLIB::RINT_F128,
  950. RTLIB::RINT_PPCF128),
  951. N, false);
  952. GetPairElements(Call, Lo, Hi);
  953. }
  954. void DAGTypeLegalizer::ExpandFloatRes_FSIN(SDNode *N,
  955. SDValue &Lo, SDValue &Hi) {
  956. SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
  957. RTLIB::SIN_F32, RTLIB::SIN_F64,
  958. RTLIB::SIN_F80, RTLIB::SIN_F128,
  959. RTLIB::SIN_PPCF128),
  960. N, false);
  961. GetPairElements(Call, Lo, Hi);
  962. }
  963. void DAGTypeLegalizer::ExpandFloatRes_FSQRT(SDNode *N,
  964. SDValue &Lo, SDValue &Hi) {
  965. SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
  966. RTLIB::SQRT_F32, RTLIB::SQRT_F64,
  967. RTLIB::SQRT_F80, RTLIB::SQRT_F128,
  968. RTLIB::SQRT_PPCF128),
  969. N, false);
  970. GetPairElements(Call, Lo, Hi);
  971. }
  972. void DAGTypeLegalizer::ExpandFloatRes_FSUB(SDNode *N, SDValue &Lo,
  973. SDValue &Hi) {
  974. SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
  975. SDValue Call = TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
  976. RTLIB::SUB_F32,
  977. RTLIB::SUB_F64,
  978. RTLIB::SUB_F80,
  979. RTLIB::SUB_F128,
  980. RTLIB::SUB_PPCF128),
  981. N->getValueType(0), Ops, 2, false,
  982. N->getDebugLoc());
  983. GetPairElements(Call, Lo, Hi);
  984. }
  985. void DAGTypeLegalizer::ExpandFloatRes_FTRUNC(SDNode *N,
  986. SDValue &Lo, SDValue &Hi) {
  987. SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
  988. RTLIB::TRUNC_F32, RTLIB::TRUNC_F64,
  989. RTLIB::TRUNC_F80, RTLIB::TRUNC_F128,
  990. RTLIB::TRUNC_PPCF128),
  991. N, false);
  992. GetPairElements(Call, Lo, Hi);
  993. }
  994. void DAGTypeLegalizer::ExpandFloatRes_LOAD(SDNode *N, SDValue &Lo,
  995. SDValue &Hi) {
  996. if (ISD::isNormalLoad(N)) {
  997. ExpandRes_NormalLoad(N, Lo, Hi);
  998. return;
  999. }
  1000. assert(ISD::isUNINDEXEDLoad(N) && "Indexed load during type legalization!");
  1001. LoadSDNode *LD = cast<LoadSDNode>(N);
  1002. SDValue Chain = LD->getChain();
  1003. SDValue Ptr = LD->getBasePtr();
  1004. DebugLoc dl = N->getDebugLoc();
  1005. EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), LD->getValueType(0));
  1006. assert(NVT.isByteSized() && "Expanded type not byte sized!");
  1007. assert(LD->getMemoryVT().bitsLE(NVT) && "Float type not round?");
  1008. Hi = DAG.getExtLoad(LD->getExtensionType(), dl, NVT, Chain, Ptr,
  1009. LD->getPointerInfo(), LD->getMemoryVT(), LD->isVolatile(),
  1010. LD->isNonTemporal(), LD->getAlignment());
  1011. // Remember the chain.
  1012. Chain = Hi.getValue(1);
  1013. // The low part is zero.
  1014. Lo = DAG.getConstantFP(APFloat(DAG.EVTToAPFloatSemantics(NVT),
  1015. APInt(NVT.getSizeInBits(), 0)), NVT);
  1016. // Modified the chain - switch anything that used the old chain to use the
  1017. // new one.
  1018. ReplaceValueWith(SDValue(LD, 1), Chain);
  1019. }
  1020. void DAGTypeLegalizer::ExpandFloatRes_XINT_TO_FP(SDNode *N, SDValue &Lo,
  1021. SDValue &Hi) {
  1022. assert(N->getValueType(0) == MVT::ppcf128 && "Unsupported XINT_TO_FP!");
  1023. EVT VT = N->getValueType(0);
  1024. EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
  1025. SDValue Src = N->getOperand(0);
  1026. EVT SrcVT = Src.getValueType();
  1027. bool isSigned = N->getOpcode() == ISD::SINT_TO_FP;
  1028. DebugLoc dl = N->getDebugLoc();
  1029. // First do an SINT_TO_FP, whether the original was signed or unsigned.
  1030. // When promoting partial word types to i32 we must honor the signedness,
  1031. // though.
  1032. if (SrcVT.bitsLE(MVT::i32)) {
  1033. // The integer can be represented exactly in an f64.
  1034. Src = DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND, dl,
  1035. MVT::i32, Src);
  1036. Lo = DAG.getConstantFP(APFloat(DAG.EVTToAPFloatSemantics(NVT),
  1037. APInt(NVT.getSizeInBits(), 0)), NVT);
  1038. Hi = DAG.getNode(ISD::SINT_TO_FP, dl, NVT, Src);
  1039. } else {
  1040. RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
  1041. if (SrcVT.bitsLE(MVT::i64)) {
  1042. Src = DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND, dl,
  1043. MVT::i64, Src);
  1044. LC = RTLIB::SINTTOFP_I64_PPCF128;
  1045. } else if (SrcVT.bitsLE(MVT::i128)) {
  1046. Src = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i128, Src);
  1047. LC = RTLIB::SINTTOFP_I128_PPCF128;
  1048. }
  1049. assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported XINT_TO_FP!");
  1050. Hi = TLI.makeLibCall(DAG, LC, VT, &Src, 1, true, dl);
  1051. GetPairElements(Hi, Lo, Hi);
  1052. }
  1053. if (isSigned)
  1054. return;
  1055. // Unsigned - fix up the SINT_TO_FP value just calculated.
  1056. Hi = DAG.getNode(ISD::BUILD_PAIR, dl, VT, Lo, Hi);
  1057. SrcVT = Src.getValueType();
  1058. // x>=0 ? (ppcf128)(iN)x : (ppcf128)(iN)x + 2^N; N=32,64,128.
  1059. static const uint64_t TwoE32[] = { 0x41f0000000000000LL, 0 };
  1060. static const uint64_t TwoE64[] = { 0x43f0000000000000LL, 0 };
  1061. static const uint64_t TwoE128[] = { 0x47f0000000000000LL, 0 };
  1062. ArrayRef<uint64_t> Parts;
  1063. switch (SrcVT.getSimpleVT().SimpleTy) {
  1064. default:
  1065. llvm_unreachable("Unsupported UINT_TO_FP!");
  1066. case MVT::i32:
  1067. Parts = TwoE32;
  1068. break;
  1069. case MVT::i64:
  1070. Parts = TwoE64;
  1071. break;
  1072. case MVT::i128:
  1073. Parts = TwoE128;
  1074. break;
  1075. }
  1076. Lo = DAG.getNode(ISD::FADD, dl, VT, Hi,
  1077. DAG.getConstantFP(APFloat(APFloat::PPCDoubleDouble,
  1078. APInt(128, Parts)),
  1079. MVT::ppcf128));
  1080. Lo = DAG.getNode(ISD::SELECT_CC, dl, VT, Src, DAG.getConstant(0, SrcVT),
  1081. Lo, Hi, DAG.getCondCode(ISD::SETLT));
  1082. GetPairElements(Lo, Lo, Hi);
  1083. }
  1084. //===----------------------------------------------------------------------===//
  1085. // Float Operand Expansion
  1086. //===----------------------------------------------------------------------===//
  1087. /// ExpandFloatOperand - This method is called when the specified operand of the
  1088. /// specified node is found to need expansion. At this point, all of the result
  1089. /// types of the node are known to be legal, but other operands of the node may
  1090. /// need promotion or expansion as well as the specified one.
  1091. bool DAGTypeLegalizer::ExpandFloatOperand(SDNode *N, unsigned OpNo) {
  1092. DEBUG(dbgs() << "Expand float operand: "; N->dump(&DAG); dbgs() << "\n");
  1093. SDValue Res = SDValue();
  1094. // See if the target wants to custom expand this node.
  1095. if (CustomLowerNode(N, N->getOperand(OpNo).getValueType(), false))
  1096. return false;
  1097. switch (N->getOpcode()) {
  1098. default:
  1099. #ifndef NDEBUG
  1100. dbgs() << "ExpandFloatOperand Op #" << OpNo << ": ";
  1101. N->dump(&DAG); dbgs() << "\n";
  1102. #endif
  1103. llvm_unreachable("Do not know how to expand this operator's operand!");
  1104. case ISD::BITCAST: Res = ExpandOp_BITCAST(N); break;
  1105. case ISD::BUILD_VECTOR: Res = ExpandOp_BUILD_VECTOR(N); break;
  1106. case ISD::EXTRACT_ELEMENT: Res = ExpandOp_EXTRACT_ELEMENT(N); break;
  1107. case ISD::BR_CC: Res = ExpandFloatOp_BR_CC(N); break;
  1108. case ISD::FP_ROUND: Res = ExpandFloatOp_FP_ROUND(N); break;
  1109. case ISD::FP_TO_SINT: Res = ExpandFloatOp_FP_TO_SINT(N); break;
  1110. case ISD::FP_TO_UINT: Res = ExpandFloatOp_FP_TO_UINT(N); break;
  1111. case ISD::SELECT_CC: Res = ExpandFloatOp_SELECT_CC(N); break;
  1112. case ISD::SETCC: Res = ExpandFloatOp_SETCC(N); break;
  1113. case ISD::STORE: Res = ExpandFloatOp_STORE(cast<StoreSDNode>(N),
  1114. OpNo); break;
  1115. }
  1116. // If the result is null, the sub-method took care of registering results etc.
  1117. if (!Res.getNode()) return false;
  1118. // If the result is N, the sub-method updated N in place. Tell the legalizer
  1119. // core about this.
  1120. if (Res.getNode() == N)
  1121. return true;
  1122. assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 &&
  1123. "Invalid operand expansion");
  1124. ReplaceValueWith(SDValue(N, 0), Res);
  1125. return false;
  1126. }
  1127. /// FloatExpandSetCCOperands - Expand the operands of a comparison. This code
  1128. /// is shared among BR_CC, SELECT_CC, and SETCC handlers.
  1129. void DAGTypeLegalizer::FloatExpandSetCCOperands(SDValue &NewLHS,
  1130. SDValue &NewRHS,
  1131. ISD::CondCode &CCCode,
  1132. DebugLoc dl) {
  1133. SDValue LHSLo, LHSHi, RHSLo, RHSHi;
  1134. GetExpandedFloat(NewLHS, LHSLo, LHSHi);
  1135. GetExpandedFloat(NewRHS, RHSLo, RHSHi);
  1136. assert(NewLHS.getValueType() == MVT::ppcf128 && "Unsupported setcc type!");
  1137. // FIXME: This generated code sucks. We want to generate
  1138. // FCMPU crN, hi1, hi2
  1139. // BNE crN, L:
  1140. // FCMPU crN, lo1, lo2
  1141. // The following can be improved, but not that much.
  1142. SDValue Tmp1, Tmp2, Tmp3;
  1143. Tmp1 = DAG.getSetCC(dl, getSetCCResultType(LHSHi.getValueType()),
  1144. LHSHi, RHSHi, ISD::SETOEQ);
  1145. Tmp2 = DAG.getSetCC(dl, getSetCCResultType(LHSLo.getValueType()),
  1146. LHSLo, RHSLo, CCCode);
  1147. Tmp3 = DAG.getNode(ISD::AND, dl, Tmp1.getValueType(), Tmp1, Tmp2);
  1148. Tmp1 = DAG.getSetCC(dl, getSetCCResultType(LHSHi.getValueType()),
  1149. LHSHi, RHSHi, ISD::SETUNE);
  1150. Tmp2 = DAG.getSetCC(dl, getSetCCResultType(LHSHi.getValueType()),
  1151. LHSHi, RHSHi, CCCode);
  1152. Tmp1 = DAG.getNode(ISD::AND, dl, Tmp1.getValueType(), Tmp1, Tmp2);
  1153. NewLHS = DAG.getNode(ISD::OR, dl, Tmp1.getValueType(), Tmp1, Tmp3);
  1154. NewRHS = SDValue(); // LHS is the result, not a compare.
  1155. }
  1156. SDValue DAGTypeLegalizer::ExpandFloatOp_BR_CC(SDNode *N) {
  1157. SDValue NewLHS = N->getOperand(2), NewRHS = N->getOperand(3);
  1158. ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(1))->get();
  1159. FloatExpandSetCCOperands(NewLHS, NewRHS, CCCode, N->getDebugLoc());
  1160. // If ExpandSetCCOperands returned a scalar, we need to compare the result
  1161. // against zero to select between true and false values.
  1162. if (NewRHS.getNode() == 0) {
  1163. NewRHS = DAG.getConstant(0, NewLHS.getValueType());
  1164. CCCode = ISD::SETNE;
  1165. }
  1166. // Update N to have the operands specified.
  1167. return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0),
  1168. DAG.getCondCode(CCCode), NewLHS, NewRHS,
  1169. N->getOperand(4)), 0);
  1170. }
  1171. SDValue DAGTypeLegalizer::ExpandFloatOp_FP_ROUND(SDNode *N) {
  1172. assert(N->getOperand(0).getValueType() == MVT::ppcf128 &&
  1173. "Logic only correct for ppcf128!");
  1174. SDValue Lo, Hi;
  1175. GetExpandedFloat(N->getOperand(0), Lo, Hi);
  1176. // Round it the rest of the way (e.g. to f32) if needed.
  1177. return DAG.getNode(ISD::FP_ROUND, N->getDebugLoc(),
  1178. N->getValueType(0), Hi, N->getOperand(1));
  1179. }
  1180. SDValue DAGTypeLegalizer::ExpandFloatOp_FP_TO_SINT(SDNode *N) {
  1181. EVT RVT = N->getValueType(0);
  1182. DebugLoc dl = N->getDebugLoc();
  1183. // Expand ppcf128 to i32 by hand for the benefit of llvm-gcc bootstrap on
  1184. // PPC (the libcall is not available). FIXME: Do this in a less hacky way.
  1185. if (RVT == MVT::i32) {
  1186. assert(N->getOperand(0).getValueType() == MVT::ppcf128 &&
  1187. "Logic only correct for ppcf128!");
  1188. SDValue Res = DAG.getNode(ISD::FP_ROUND_INREG, dl, MVT::ppcf128,
  1189. N->getOperand(0), DAG.getValueType(MVT::f64));
  1190. Res = DAG.getNode(ISD::FP_ROUND, dl, MVT::f64, Res,
  1191. DAG.getIntPtrConstant(1));
  1192. return DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, Res);
  1193. }
  1194. RTLIB::Libcall LC = RTLIB::getFPTOSINT(N->getOperand(0).getValueType(), RVT);
  1195. assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_TO_SINT!");
  1196. return TLI.makeLibCall(DAG, LC, RVT, &N->getOperand(0), 1, false, dl);
  1197. }
  1198. SDValue DAGTypeLegalizer::ExpandFloatOp_FP_TO_UINT(SDNode *N) {
  1199. EVT RVT = N->getValueType(0);
  1200. DebugLoc dl = N->getDebugLoc();
  1201. // Expand ppcf128 to i32 by hand for the benefit of llvm-gcc bootstrap on
  1202. // PPC (the libcall is not available). FIXME: Do this in a less hacky way.
  1203. if (RVT == MVT::i32) {
  1204. assert(N->getOperand(0).getValueType() == MVT::ppcf128 &&
  1205. "Logic only correct for ppcf128!");
  1206. const uint64_t TwoE31[] = {0x41e0000000000000LL, 0};
  1207. APFloat APF = APFloat(APFloat::PPCDoubleDouble, APInt(128, TwoE31));
  1208. SDValue Tmp = DAG.getConstantFP(APF, MVT::ppcf128);
  1209. // X>=2^31 ? (int)(X-2^31)+0x80000000 : (int)X
  1210. // FIXME: generated code sucks.
  1211. return DAG.getNode(ISD::SELECT_CC, dl, MVT::i32, N->getOperand(0), Tmp,
  1212. DAG.getNode(ISD::ADD, dl, MVT::i32,
  1213. DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32,
  1214. DAG.getNode(ISD::FSUB, dl,
  1215. MVT::ppcf128,
  1216. N->getOperand(0),
  1217. Tmp)),
  1218. DAG.getConstant(0x80000000, MVT::i32)),
  1219. DAG.getNode(ISD::FP_TO_SINT, dl,
  1220. MVT::i32, N->getOperand(0)),
  1221. DAG.getCondCode(ISD::SETGE));
  1222. }
  1223. RTLIB::Libcall LC = RTLIB::getFPTOUINT(N->getOperand(0).getValueType(), RVT);
  1224. assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_TO_UINT!");
  1225. return TLI.makeLibCall(DAG, LC, N->getValueType(0), &N->getOperand(0), 1,
  1226. false, dl);
  1227. }
  1228. SDValue DAGTypeLegalizer::ExpandFloatOp_SELECT_CC(SDNode *N) {
  1229. SDValue NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
  1230. ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(4))->get();
  1231. FloatExpandSetCCOperands(NewLHS, NewRHS, CCCode, N->getDebugLoc());
  1232. // If ExpandSetCCOperands returned a scalar, we need to compare the result
  1233. // against zero to select between true and false values.
  1234. if (NewRHS.getNode() == 0) {
  1235. NewRHS = DAG.getConstant(0, NewLHS.getValueType());
  1236. CCCode = ISD::SETNE;
  1237. }
  1238. // Update N to have the operands specified.
  1239. return SDValue(DAG.UpdateNodeOperands(N, NewLHS, NewRHS,
  1240. N->getOperand(2), N->getOperand(3),
  1241. DAG.getCondCode(CCCode)), 0);
  1242. }
  1243. SDValue DAGTypeLegalizer::ExpandFloatOp_SETCC(SDNode *N) {
  1244. SDValue NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
  1245. ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(2))->get();
  1246. FloatExpandSetCCOperands(NewLHS, NewRHS, CCCode, N->getDebugLoc());
  1247. // If ExpandSetCCOperands returned a scalar, use it.
  1248. if (NewRHS.getNode() == 0) {
  1249. assert(NewLHS.getValueType() == N->getValueType(0) &&
  1250. "Unexpected setcc expansion!");
  1251. return NewLHS;
  1252. }
  1253. // Otherwise, update N to have the operands specified.
  1254. return SDValue(DAG.UpdateNodeOperands(N, NewLHS, NewRHS,
  1255. DAG.getCondCode(CCCode)), 0);
  1256. }
  1257. SDValue DAGTypeLegalizer::ExpandFloatOp_STORE(SDNode *N, unsigned OpNo) {
  1258. if (ISD::isNormalStore(N))
  1259. return ExpandOp_NormalStore(N, OpNo);
  1260. assert(ISD::isUNINDEXEDStore(N) && "Indexed store during type legalization!");
  1261. assert(OpNo == 1 && "Can only expand the stored value so far");
  1262. StoreSDNode *ST = cast<StoreSDNode>(N);
  1263. SDValue Chain = ST->getChain();
  1264. SDValue Ptr = ST->getBasePtr();
  1265. EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(),
  1266. ST->getValue().getValueType());
  1267. assert(NVT.isByteSized() && "Expanded type not byte sized!");
  1268. assert(ST->getMemoryVT().bitsLE(NVT) && "Float type not round?");
  1269. (void)NVT;
  1270. SDValue Lo, Hi;
  1271. GetExpandedOp(ST->getValue(), Lo, Hi);
  1272. return DAG.getTruncStore(Chain, N->getDebugLoc(), Hi, Ptr,
  1273. ST->getPointerInfo(),
  1274. ST->getMemoryVT(), ST->isVolatile(),
  1275. ST->isNonTemporal(), ST->getAlignment());
  1276. }