LegalizeFloatTypes.cpp 61 KB

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