LegalizeFloatTypes.cpp 87 KB

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