2
0

legalizevectorops.cpp 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024
  1. //===-- LegalizeVectorOps.cpp - Implement SelectionDAG::LegalizeVectors ---===//
  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 the SelectionDAG::LegalizeVectors method.
  11. //
  12. // The vector legalizer looks for vector operations which might need to be
  13. // scalarized and legalizes them. This is a separate step from Legalize because
  14. // scalarizing can introduce illegal types. For example, suppose we have an
  15. // ISD::SDIV of type v2i64 on x86-32. The type is legal (for example, addition
  16. // on a v2i64 is legal), but ISD::SDIV isn't legal, so we have to unroll the
  17. // operation, which introduces nodes with the illegal type i64 which must be
  18. // expanded. Similarly, suppose we have an ISD::SRA of type v16i8 on PowerPC;
  19. // the operation must be unrolled, which introduces nodes with the illegal
  20. // type i8 which must be promoted.
  21. //
  22. // This does not legalize vector manipulations like ISD::BUILD_VECTOR,
  23. // or operations that happen to take a vector which are custom-lowered;
  24. // the legalization for such operations never produces nodes
  25. // with illegal types, so it's okay to put off legalizing them until
  26. // SelectionDAG::Legalize runs.
  27. //
  28. //===----------------------------------------------------------------------===//
  29. #include "llvm/CodeGen/SelectionDAG.h"
  30. #include "llvm/Target/TargetLowering.h"
  31. using namespace llvm;
  32. namespace {
  33. class VectorLegalizer {
  34. SelectionDAG& DAG;
  35. const TargetLowering &TLI;
  36. bool Changed; // Keep track of whether anything changed
  37. /// For nodes that are of legal width, and that have more than one use, this
  38. /// map indicates what regularized operand to use. This allows us to avoid
  39. /// legalizing the same thing more than once.
  40. SmallDenseMap<SDValue, SDValue, 64> LegalizedNodes;
  41. /// \brief Adds a node to the translation cache.
  42. void AddLegalizedOperand(SDValue From, SDValue To) {
  43. LegalizedNodes.insert(std::make_pair(From, To));
  44. // If someone requests legalization of the new node, return itself.
  45. if (From != To)
  46. LegalizedNodes.insert(std::make_pair(To, To));
  47. }
  48. /// \brief Legalizes the given node.
  49. SDValue LegalizeOp(SDValue Op);
  50. /// \brief Assuming the node is legal, "legalize" the results.
  51. SDValue TranslateLegalizeResults(SDValue Op, SDValue Result);
  52. /// \brief Implements unrolling a VSETCC.
  53. SDValue UnrollVSETCC(SDValue Op);
  54. /// \brief Implement expand-based legalization of vector operations.
  55. ///
  56. /// This is just a high-level routine to dispatch to specific code paths for
  57. /// operations to legalize them.
  58. SDValue Expand(SDValue Op);
  59. /// \brief Implements expansion for FNEG; falls back to UnrollVectorOp if
  60. /// FSUB isn't legal.
  61. ///
  62. /// Implements expansion for UINT_TO_FLOAT; falls back to UnrollVectorOp if
  63. /// SINT_TO_FLOAT and SHR on vectors isn't legal.
  64. SDValue ExpandUINT_TO_FLOAT(SDValue Op);
  65. /// \brief Implement expansion for SIGN_EXTEND_INREG using SRL and SRA.
  66. SDValue ExpandSEXTINREG(SDValue Op);
  67. /// \brief Implement expansion for ANY_EXTEND_VECTOR_INREG.
  68. ///
  69. /// Shuffles the low lanes of the operand into place and bitcasts to the proper
  70. /// type. The contents of the bits in the extended part of each element are
  71. /// undef.
  72. SDValue ExpandANY_EXTEND_VECTOR_INREG(SDValue Op);
  73. /// \brief Implement expansion for SIGN_EXTEND_VECTOR_INREG.
  74. ///
  75. /// Shuffles the low lanes of the operand into place, bitcasts to the proper
  76. /// type, then shifts left and arithmetic shifts right to introduce a sign
  77. /// extension.
  78. SDValue ExpandSIGN_EXTEND_VECTOR_INREG(SDValue Op);
  79. /// \brief Implement expansion for ZERO_EXTEND_VECTOR_INREG.
  80. ///
  81. /// Shuffles the low lanes of the operand into place and blends zeros into
  82. /// the remaining lanes, finally bitcasting to the proper type.
  83. SDValue ExpandZERO_EXTEND_VECTOR_INREG(SDValue Op);
  84. /// \brief Expand bswap of vectors into a shuffle if legal.
  85. SDValue ExpandBSWAP(SDValue Op);
  86. /// \brief Implement vselect in terms of XOR, AND, OR when blend is not
  87. /// supported by the target.
  88. SDValue ExpandVSELECT(SDValue Op);
  89. SDValue ExpandSELECT(SDValue Op);
  90. SDValue ExpandLoad(SDValue Op);
  91. SDValue ExpandStore(SDValue Op);
  92. SDValue ExpandFNEG(SDValue Op);
  93. /// \brief Implements vector promotion.
  94. ///
  95. /// This is essentially just bitcasting the operands to a different type and
  96. /// bitcasting the result back to the original type.
  97. SDValue Promote(SDValue Op);
  98. /// \brief Implements [SU]INT_TO_FP vector promotion.
  99. ///
  100. /// This is a [zs]ext of the input operand to the next size up.
  101. SDValue PromoteINT_TO_FP(SDValue Op);
  102. /// \brief Implements FP_TO_[SU]INT vector promotion of the result type.
  103. ///
  104. /// It is promoted to the next size up integer type. The result is then
  105. /// truncated back to the original type.
  106. SDValue PromoteFP_TO_INT(SDValue Op, bool isSigned);
  107. public:
  108. /// \brief Begin legalizer the vector operations in the DAG.
  109. bool Run();
  110. VectorLegalizer(SelectionDAG& dag) :
  111. DAG(dag), TLI(dag.getTargetLoweringInfo()), Changed(false) {}
  112. };
  113. bool VectorLegalizer::Run() {
  114. // Before we start legalizing vector nodes, check if there are any vectors.
  115. bool HasVectors = false;
  116. for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
  117. E = std::prev(DAG.allnodes_end()); I != std::next(E); ++I) {
  118. // Check if the values of the nodes contain vectors. We don't need to check
  119. // the operands because we are going to check their values at some point.
  120. for (SDNode::value_iterator J = I->value_begin(), E = I->value_end();
  121. J != E; ++J)
  122. HasVectors |= J->isVector();
  123. // If we found a vector node we can start the legalization.
  124. if (HasVectors)
  125. break;
  126. }
  127. // If this basic block has no vectors then no need to legalize vectors.
  128. if (!HasVectors)
  129. return false;
  130. // The legalize process is inherently a bottom-up recursive process (users
  131. // legalize their uses before themselves). Given infinite stack space, we
  132. // could just start legalizing on the root and traverse the whole graph. In
  133. // practice however, this causes us to run out of stack space on large basic
  134. // blocks. To avoid this problem, compute an ordering of the nodes where each
  135. // node is only legalized after all of its operands are legalized.
  136. DAG.AssignTopologicalOrder();
  137. for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
  138. E = std::prev(DAG.allnodes_end()); I != std::next(E); ++I)
  139. LegalizeOp(SDValue(I, 0));
  140. // Finally, it's possible the root changed. Get the new root.
  141. SDValue OldRoot = DAG.getRoot();
  142. assert(LegalizedNodes.count(OldRoot) && "Root didn't get legalized?");
  143. DAG.setRoot(LegalizedNodes[OldRoot]);
  144. LegalizedNodes.clear();
  145. // Remove dead nodes now.
  146. DAG.RemoveDeadNodes();
  147. return Changed;
  148. }
  149. SDValue VectorLegalizer::TranslateLegalizeResults(SDValue Op, SDValue Result) {
  150. // Generic legalization: just pass the operand through.
  151. for (unsigned i = 0, e = Op.getNode()->getNumValues(); i != e; ++i)
  152. AddLegalizedOperand(Op.getValue(i), Result.getValue(i));
  153. return Result.getValue(Op.getResNo());
  154. }
  155. SDValue VectorLegalizer::LegalizeOp(SDValue Op) {
  156. // Note that LegalizeOp may be reentered even from single-use nodes, which
  157. // means that we always must cache transformed nodes.
  158. DenseMap<SDValue, SDValue>::iterator I = LegalizedNodes.find(Op);
  159. if (I != LegalizedNodes.end()) return I->second;
  160. SDNode* Node = Op.getNode();
  161. // Legalize the operands
  162. SmallVector<SDValue, 8> Ops;
  163. for (const SDValue &Op : Node->op_values())
  164. Ops.push_back(LegalizeOp(Op));
  165. SDValue Result = SDValue(DAG.UpdateNodeOperands(Op.getNode(), Ops), 0);
  166. bool HasVectorValue = false;
  167. if (Op.getOpcode() == ISD::LOAD) {
  168. LoadSDNode *LD = cast<LoadSDNode>(Op.getNode());
  169. ISD::LoadExtType ExtType = LD->getExtensionType();
  170. if (LD->getMemoryVT().isVector() && ExtType != ISD::NON_EXTLOAD)
  171. switch (TLI.getLoadExtAction(LD->getExtensionType(), LD->getValueType(0),
  172. LD->getMemoryVT())) {
  173. default: llvm_unreachable("This action is not supported yet!");
  174. case TargetLowering::Legal:
  175. return TranslateLegalizeResults(Op, Result);
  176. case TargetLowering::Custom:
  177. if (SDValue Lowered = TLI.LowerOperation(Result, DAG)) {
  178. if (Lowered == Result)
  179. return TranslateLegalizeResults(Op, Lowered);
  180. Changed = true;
  181. if (Lowered->getNumValues() != Op->getNumValues()) {
  182. // This expanded to something other than the load. Assume the
  183. // lowering code took care of any chain values, and just handle the
  184. // returned value.
  185. assert(Result.getValue(1).use_empty() &&
  186. "There are still live users of the old chain!");
  187. return LegalizeOp(Lowered);
  188. } else {
  189. return TranslateLegalizeResults(Op, Lowered);
  190. }
  191. }
  192. case TargetLowering::Expand:
  193. Changed = true;
  194. return LegalizeOp(ExpandLoad(Op));
  195. }
  196. } else if (Op.getOpcode() == ISD::STORE) {
  197. StoreSDNode *ST = cast<StoreSDNode>(Op.getNode());
  198. EVT StVT = ST->getMemoryVT();
  199. MVT ValVT = ST->getValue().getSimpleValueType();
  200. if (StVT.isVector() && ST->isTruncatingStore())
  201. switch (TLI.getTruncStoreAction(ValVT, StVT.getSimpleVT())) {
  202. default: llvm_unreachable("This action is not supported yet!");
  203. case TargetLowering::Legal:
  204. return TranslateLegalizeResults(Op, Result);
  205. case TargetLowering::Custom: {
  206. SDValue Lowered = TLI.LowerOperation(Result, DAG);
  207. Changed = Lowered != Result;
  208. return TranslateLegalizeResults(Op, Lowered);
  209. }
  210. case TargetLowering::Expand:
  211. Changed = true;
  212. return LegalizeOp(ExpandStore(Op));
  213. }
  214. } else if (Op.getOpcode() == ISD::MSCATTER)
  215. HasVectorValue = true;
  216. for (SDNode::value_iterator J = Node->value_begin(), E = Node->value_end();
  217. J != E;
  218. ++J)
  219. HasVectorValue |= J->isVector();
  220. if (!HasVectorValue)
  221. return TranslateLegalizeResults(Op, Result);
  222. EVT QueryType;
  223. switch (Op.getOpcode()) {
  224. default:
  225. return TranslateLegalizeResults(Op, Result);
  226. case ISD::ADD:
  227. case ISD::SUB:
  228. case ISD::MUL:
  229. case ISD::SDIV:
  230. case ISD::UDIV:
  231. case ISD::SREM:
  232. case ISD::UREM:
  233. case ISD::FADD:
  234. case ISD::FSUB:
  235. case ISD::FMUL:
  236. case ISD::FDIV:
  237. case ISD::FREM:
  238. case ISD::AND:
  239. case ISD::OR:
  240. case ISD::XOR:
  241. case ISD::SHL:
  242. case ISD::SRA:
  243. case ISD::SRL:
  244. case ISD::ROTL:
  245. case ISD::ROTR:
  246. case ISD::BSWAP:
  247. case ISD::CTLZ:
  248. case ISD::CTTZ:
  249. case ISD::CTLZ_ZERO_UNDEF:
  250. case ISD::CTTZ_ZERO_UNDEF:
  251. case ISD::CTPOP:
  252. case ISD::SELECT:
  253. case ISD::VSELECT:
  254. case ISD::SELECT_CC:
  255. case ISD::SETCC:
  256. case ISD::ZERO_EXTEND:
  257. case ISD::ANY_EXTEND:
  258. case ISD::TRUNCATE:
  259. case ISD::SIGN_EXTEND:
  260. case ISD::FP_TO_SINT:
  261. case ISD::FP_TO_UINT:
  262. case ISD::FNEG:
  263. case ISD::FABS:
  264. case ISD::FMINNUM:
  265. case ISD::FMAXNUM:
  266. case ISD::FCOPYSIGN:
  267. case ISD::FSQRT:
  268. case ISD::FSIN:
  269. case ISD::FCOS:
  270. case ISD::FPOWI:
  271. case ISD::FPOW:
  272. case ISD::FLOG:
  273. case ISD::FLOG2:
  274. case ISD::FLOG10:
  275. case ISD::FEXP:
  276. case ISD::FEXP2:
  277. case ISD::FCEIL:
  278. case ISD::FTRUNC:
  279. case ISD::FRINT:
  280. case ISD::FNEARBYINT:
  281. case ISD::FROUND:
  282. case ISD::FFLOOR:
  283. case ISD::FP_ROUND:
  284. case ISD::FP_EXTEND:
  285. case ISD::FMA:
  286. case ISD::SIGN_EXTEND_INREG:
  287. case ISD::ANY_EXTEND_VECTOR_INREG:
  288. case ISD::SIGN_EXTEND_VECTOR_INREG:
  289. case ISD::ZERO_EXTEND_VECTOR_INREG:
  290. case ISD::SMIN:
  291. case ISD::SMAX:
  292. case ISD::UMIN:
  293. case ISD::UMAX:
  294. QueryType = Node->getValueType(0);
  295. break;
  296. case ISD::FP_ROUND_INREG:
  297. QueryType = cast<VTSDNode>(Node->getOperand(1))->getVT();
  298. break;
  299. case ISD::SINT_TO_FP:
  300. case ISD::UINT_TO_FP:
  301. QueryType = Node->getOperand(0).getValueType();
  302. break;
  303. case ISD::MSCATTER:
  304. QueryType = cast<MaskedScatterSDNode>(Node)->getValue().getValueType();
  305. break;
  306. }
  307. switch (TLI.getOperationAction(Node->getOpcode(), QueryType)) {
  308. case TargetLowering::Promote:
  309. Result = Promote(Op);
  310. Changed = true;
  311. break;
  312. case TargetLowering::Legal:
  313. break;
  314. case TargetLowering::Custom: {
  315. SDValue Tmp1 = TLI.LowerOperation(Op, DAG);
  316. if (Tmp1.getNode()) {
  317. Result = Tmp1;
  318. break;
  319. }
  320. // FALL THROUGH
  321. }
  322. case TargetLowering::Expand:
  323. Result = Expand(Op);
  324. }
  325. // Make sure that the generated code is itself legal.
  326. if (Result != Op) {
  327. Result = LegalizeOp(Result);
  328. Changed = true;
  329. }
  330. // Note that LegalizeOp may be reentered even from single-use nodes, which
  331. // means that we always must cache transformed nodes.
  332. AddLegalizedOperand(Op, Result);
  333. return Result;
  334. }
  335. SDValue VectorLegalizer::Promote(SDValue Op) {
  336. // For a few operations there is a specific concept for promotion based on
  337. // the operand's type.
  338. switch (Op.getOpcode()) {
  339. case ISD::SINT_TO_FP:
  340. case ISD::UINT_TO_FP:
  341. // "Promote" the operation by extending the operand.
  342. return PromoteINT_TO_FP(Op);
  343. case ISD::FP_TO_UINT:
  344. case ISD::FP_TO_SINT:
  345. // Promote the operation by extending the operand.
  346. return PromoteFP_TO_INT(Op, Op->getOpcode() == ISD::FP_TO_SINT);
  347. }
  348. // There are currently two cases of vector promotion:
  349. // 1) Bitcasting a vector of integers to a different type to a vector of the
  350. // same overall length. For example, x86 promotes ISD::AND v2i32 to v1i64.
  351. // 2) Extending a vector of floats to a vector of the same number of larger
  352. // floats. For example, AArch64 promotes ISD::FADD on v4f16 to v4f32.
  353. MVT VT = Op.getSimpleValueType();
  354. assert(Op.getNode()->getNumValues() == 1 &&
  355. "Can't promote a vector with multiple results!");
  356. MVT NVT = TLI.getTypeToPromoteTo(Op.getOpcode(), VT);
  357. SDLoc dl(Op);
  358. SmallVector<SDValue, 4> Operands(Op.getNumOperands());
  359. for (unsigned j = 0; j != Op.getNumOperands(); ++j) {
  360. if (Op.getOperand(j).getValueType().isVector())
  361. if (Op.getOperand(j)
  362. .getValueType()
  363. .getVectorElementType()
  364. .isFloatingPoint() &&
  365. NVT.isVector() && NVT.getVectorElementType().isFloatingPoint())
  366. Operands[j] = DAG.getNode(ISD::FP_EXTEND, dl, NVT, Op.getOperand(j));
  367. else
  368. Operands[j] = DAG.getNode(ISD::BITCAST, dl, NVT, Op.getOperand(j));
  369. else
  370. Operands[j] = Op.getOperand(j);
  371. }
  372. Op = DAG.getNode(Op.getOpcode(), dl, NVT, Operands);
  373. if ((VT.isFloatingPoint() && NVT.isFloatingPoint()) ||
  374. (VT.isVector() && VT.getVectorElementType().isFloatingPoint() &&
  375. NVT.isVector() && NVT.getVectorElementType().isFloatingPoint()))
  376. return DAG.getNode(ISD::FP_ROUND, dl, VT, Op, DAG.getIntPtrConstant(0, dl));
  377. else
  378. return DAG.getNode(ISD::BITCAST, dl, VT, Op);
  379. }
  380. SDValue VectorLegalizer::PromoteINT_TO_FP(SDValue Op) {
  381. // INT_TO_FP operations may require the input operand be promoted even
  382. // when the type is otherwise legal.
  383. EVT VT = Op.getOperand(0).getValueType();
  384. assert(Op.getNode()->getNumValues() == 1 &&
  385. "Can't promote a vector with multiple results!");
  386. // Normal getTypeToPromoteTo() doesn't work here, as that will promote
  387. // by widening the vector w/ the same element width and twice the number
  388. // of elements. We want the other way around, the same number of elements,
  389. // each twice the width.
  390. //
  391. // Increase the bitwidth of the element to the next pow-of-two
  392. // (which is greater than 8 bits).
  393. EVT NVT = VT.widenIntegerVectorElementType(*DAG.getContext());
  394. assert(NVT.isSimple() && "Promoting to a non-simple vector type!");
  395. SDLoc dl(Op);
  396. SmallVector<SDValue, 4> Operands(Op.getNumOperands());
  397. unsigned Opc = Op.getOpcode() == ISD::UINT_TO_FP ? ISD::ZERO_EXTEND :
  398. ISD::SIGN_EXTEND;
  399. for (unsigned j = 0; j != Op.getNumOperands(); ++j) {
  400. if (Op.getOperand(j).getValueType().isVector())
  401. Operands[j] = DAG.getNode(Opc, dl, NVT, Op.getOperand(j));
  402. else
  403. Operands[j] = Op.getOperand(j);
  404. }
  405. return DAG.getNode(Op.getOpcode(), dl, Op.getValueType(), Operands);
  406. }
  407. // For FP_TO_INT we promote the result type to a vector type with wider
  408. // elements and then truncate the result. This is different from the default
  409. // PromoteVector which uses bitcast to promote thus assumning that the
  410. // promoted vector type has the same overall size.
  411. SDValue VectorLegalizer::PromoteFP_TO_INT(SDValue Op, bool isSigned) {
  412. assert(Op.getNode()->getNumValues() == 1 &&
  413. "Can't promote a vector with multiple results!");
  414. EVT VT = Op.getValueType();
  415. EVT NewVT;
  416. unsigned NewOpc;
  417. while (1) {
  418. NewVT = VT.widenIntegerVectorElementType(*DAG.getContext());
  419. assert(NewVT.isSimple() && "Promoting to a non-simple vector type!");
  420. if (TLI.isOperationLegalOrCustom(ISD::FP_TO_SINT, NewVT)) {
  421. NewOpc = ISD::FP_TO_SINT;
  422. break;
  423. }
  424. if (!isSigned && TLI.isOperationLegalOrCustom(ISD::FP_TO_UINT, NewVT)) {
  425. NewOpc = ISD::FP_TO_UINT;
  426. break;
  427. }
  428. }
  429. SDLoc loc(Op);
  430. SDValue promoted = DAG.getNode(NewOpc, SDLoc(Op), NewVT, Op.getOperand(0));
  431. return DAG.getNode(ISD::TRUNCATE, SDLoc(Op), VT, promoted);
  432. }
  433. SDValue VectorLegalizer::ExpandLoad(SDValue Op) {
  434. SDLoc dl(Op);
  435. LoadSDNode *LD = cast<LoadSDNode>(Op.getNode());
  436. SDValue Chain = LD->getChain();
  437. SDValue BasePTR = LD->getBasePtr();
  438. EVT SrcVT = LD->getMemoryVT();
  439. ISD::LoadExtType ExtType = LD->getExtensionType();
  440. SmallVector<SDValue, 8> Vals;
  441. SmallVector<SDValue, 8> LoadChains;
  442. unsigned NumElem = SrcVT.getVectorNumElements();
  443. EVT SrcEltVT = SrcVT.getScalarType();
  444. EVT DstEltVT = Op.getNode()->getValueType(0).getScalarType();
  445. if (SrcVT.getVectorNumElements() > 1 && !SrcEltVT.isByteSized()) {
  446. // When elements in a vector is not byte-addressable, we cannot directly
  447. // load each element by advancing pointer, which could only address bytes.
  448. // Instead, we load all significant words, mask bits off, and concatenate
  449. // them to form each element. Finally, they are extended to destination
  450. // scalar type to build the destination vector.
  451. EVT WideVT = TLI.getPointerTy(DAG.getDataLayout());
  452. assert(WideVT.isRound() &&
  453. "Could not handle the sophisticated case when the widest integer is"
  454. " not power of 2.");
  455. assert(WideVT.bitsGE(SrcEltVT) &&
  456. "Type is not legalized?");
  457. unsigned WideBytes = WideVT.getStoreSize();
  458. unsigned Offset = 0;
  459. unsigned RemainingBytes = SrcVT.getStoreSize();
  460. SmallVector<SDValue, 8> LoadVals;
  461. while (RemainingBytes > 0) {
  462. SDValue ScalarLoad;
  463. unsigned LoadBytes = WideBytes;
  464. if (RemainingBytes >= LoadBytes) {
  465. ScalarLoad = DAG.getLoad(WideVT, dl, Chain, BasePTR,
  466. LD->getPointerInfo().getWithOffset(Offset),
  467. LD->isVolatile(), LD->isNonTemporal(),
  468. LD->isInvariant(),
  469. MinAlign(LD->getAlignment(), Offset),
  470. LD->getAAInfo());
  471. } else {
  472. EVT LoadVT = WideVT;
  473. while (RemainingBytes < LoadBytes) {
  474. LoadBytes >>= 1; // Reduce the load size by half.
  475. LoadVT = EVT::getIntegerVT(*DAG.getContext(), LoadBytes << 3);
  476. }
  477. ScalarLoad = DAG.getExtLoad(ISD::EXTLOAD, dl, WideVT, Chain, BasePTR,
  478. LD->getPointerInfo().getWithOffset(Offset),
  479. LoadVT, LD->isVolatile(),
  480. LD->isNonTemporal(), LD->isInvariant(),
  481. MinAlign(LD->getAlignment(), Offset),
  482. LD->getAAInfo());
  483. }
  484. RemainingBytes -= LoadBytes;
  485. Offset += LoadBytes;
  486. BasePTR = DAG.getNode(ISD::ADD, dl, BasePTR.getValueType(), BasePTR,
  487. DAG.getConstant(LoadBytes, dl,
  488. BasePTR.getValueType()));
  489. LoadVals.push_back(ScalarLoad.getValue(0));
  490. LoadChains.push_back(ScalarLoad.getValue(1));
  491. }
  492. // Extract bits, pack and extend/trunc them into destination type.
  493. unsigned SrcEltBits = SrcEltVT.getSizeInBits();
  494. SDValue SrcEltBitMask = DAG.getConstant((1U << SrcEltBits) - 1, dl, WideVT);
  495. unsigned BitOffset = 0;
  496. unsigned WideIdx = 0;
  497. unsigned WideBits = WideVT.getSizeInBits();
  498. for (unsigned Idx = 0; Idx != NumElem; ++Idx) {
  499. SDValue Lo, Hi, ShAmt;
  500. if (BitOffset < WideBits) {
  501. ShAmt = DAG.getConstant(
  502. BitOffset, dl, TLI.getShiftAmountTy(WideVT, DAG.getDataLayout()));
  503. Lo = DAG.getNode(ISD::SRL, dl, WideVT, LoadVals[WideIdx], ShAmt);
  504. Lo = DAG.getNode(ISD::AND, dl, WideVT, Lo, SrcEltBitMask);
  505. }
  506. BitOffset += SrcEltBits;
  507. if (BitOffset >= WideBits) {
  508. WideIdx++;
  509. BitOffset -= WideBits;
  510. if (BitOffset > 0) {
  511. ShAmt = DAG.getConstant(
  512. SrcEltBits - BitOffset, dl,
  513. TLI.getShiftAmountTy(WideVT, DAG.getDataLayout()));
  514. Hi = DAG.getNode(ISD::SHL, dl, WideVT, LoadVals[WideIdx], ShAmt);
  515. Hi = DAG.getNode(ISD::AND, dl, WideVT, Hi, SrcEltBitMask);
  516. }
  517. }
  518. if (Hi.getNode())
  519. Lo = DAG.getNode(ISD::OR, dl, WideVT, Lo, Hi);
  520. switch (ExtType) {
  521. default: llvm_unreachable("Unknown extended-load op!");
  522. case ISD::EXTLOAD:
  523. Lo = DAG.getAnyExtOrTrunc(Lo, dl, DstEltVT);
  524. break;
  525. case ISD::ZEXTLOAD:
  526. Lo = DAG.getZExtOrTrunc(Lo, dl, DstEltVT);
  527. break;
  528. case ISD::SEXTLOAD:
  529. ShAmt =
  530. DAG.getConstant(WideBits - SrcEltBits, dl,
  531. TLI.getShiftAmountTy(WideVT, DAG.getDataLayout()));
  532. Lo = DAG.getNode(ISD::SHL, dl, WideVT, Lo, ShAmt);
  533. Lo = DAG.getNode(ISD::SRA, dl, WideVT, Lo, ShAmt);
  534. Lo = DAG.getSExtOrTrunc(Lo, dl, DstEltVT);
  535. break;
  536. }
  537. Vals.push_back(Lo);
  538. }
  539. } else {
  540. unsigned Stride = SrcVT.getScalarType().getSizeInBits()/8;
  541. for (unsigned Idx=0; Idx<NumElem; Idx++) {
  542. SDValue ScalarLoad = DAG.getExtLoad(ExtType, dl,
  543. Op.getNode()->getValueType(0).getScalarType(),
  544. Chain, BasePTR, LD->getPointerInfo().getWithOffset(Idx * Stride),
  545. SrcVT.getScalarType(),
  546. LD->isVolatile(), LD->isNonTemporal(), LD->isInvariant(),
  547. MinAlign(LD->getAlignment(), Idx * Stride), LD->getAAInfo());
  548. BasePTR = DAG.getNode(ISD::ADD, dl, BasePTR.getValueType(), BasePTR,
  549. DAG.getConstant(Stride, dl, BasePTR.getValueType()));
  550. Vals.push_back(ScalarLoad.getValue(0));
  551. LoadChains.push_back(ScalarLoad.getValue(1));
  552. }
  553. }
  554. SDValue NewChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, LoadChains);
  555. SDValue Value = DAG.getNode(ISD::BUILD_VECTOR, dl,
  556. Op.getNode()->getValueType(0), Vals);
  557. AddLegalizedOperand(Op.getValue(0), Value);
  558. AddLegalizedOperand(Op.getValue(1), NewChain);
  559. return (Op.getResNo() ? NewChain : Value);
  560. }
  561. SDValue VectorLegalizer::ExpandStore(SDValue Op) {
  562. SDLoc dl(Op);
  563. StoreSDNode *ST = cast<StoreSDNode>(Op.getNode());
  564. SDValue Chain = ST->getChain();
  565. SDValue BasePTR = ST->getBasePtr();
  566. SDValue Value = ST->getValue();
  567. EVT StVT = ST->getMemoryVT();
  568. unsigned Alignment = ST->getAlignment();
  569. bool isVolatile = ST->isVolatile();
  570. bool isNonTemporal = ST->isNonTemporal();
  571. AAMDNodes AAInfo = ST->getAAInfo();
  572. unsigned NumElem = StVT.getVectorNumElements();
  573. // The type of the data we want to save
  574. EVT RegVT = Value.getValueType();
  575. EVT RegSclVT = RegVT.getScalarType();
  576. // The type of data as saved in memory.
  577. EVT MemSclVT = StVT.getScalarType();
  578. // Cast floats into integers
  579. unsigned ScalarSize = MemSclVT.getSizeInBits();
  580. // Round odd types to the next pow of two.
  581. if (!isPowerOf2_32(ScalarSize))
  582. ScalarSize = NextPowerOf2(ScalarSize);
  583. // Store Stride in bytes
  584. unsigned Stride = ScalarSize/8;
  585. // Extract each of the elements from the original vector
  586. // and save them into memory individually.
  587. SmallVector<SDValue, 8> Stores;
  588. for (unsigned Idx = 0; Idx < NumElem; Idx++) {
  589. SDValue Ex = DAG.getNode(
  590. ISD::EXTRACT_VECTOR_ELT, dl, RegSclVT, Value,
  591. DAG.getConstant(Idx, dl, TLI.getVectorIdxTy(DAG.getDataLayout())));
  592. // This scalar TruncStore may be illegal, but we legalize it later.
  593. SDValue Store = DAG.getTruncStore(Chain, dl, Ex, BasePTR,
  594. ST->getPointerInfo().getWithOffset(Idx*Stride), MemSclVT,
  595. isVolatile, isNonTemporal, MinAlign(Alignment, Idx*Stride),
  596. AAInfo);
  597. BasePTR = DAG.getNode(ISD::ADD, dl, BasePTR.getValueType(), BasePTR,
  598. DAG.getConstant(Stride, dl, BasePTR.getValueType()));
  599. Stores.push_back(Store);
  600. }
  601. SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Stores);
  602. AddLegalizedOperand(Op, TF);
  603. return TF;
  604. }
  605. SDValue VectorLegalizer::Expand(SDValue Op) {
  606. switch (Op->getOpcode()) {
  607. case ISD::SIGN_EXTEND_INREG:
  608. return ExpandSEXTINREG(Op);
  609. case ISD::ANY_EXTEND_VECTOR_INREG:
  610. return ExpandANY_EXTEND_VECTOR_INREG(Op);
  611. case ISD::SIGN_EXTEND_VECTOR_INREG:
  612. return ExpandSIGN_EXTEND_VECTOR_INREG(Op);
  613. case ISD::ZERO_EXTEND_VECTOR_INREG:
  614. return ExpandZERO_EXTEND_VECTOR_INREG(Op);
  615. case ISD::BSWAP:
  616. return ExpandBSWAP(Op);
  617. case ISD::VSELECT:
  618. return ExpandVSELECT(Op);
  619. case ISD::SELECT:
  620. return ExpandSELECT(Op);
  621. case ISD::UINT_TO_FP:
  622. return ExpandUINT_TO_FLOAT(Op);
  623. case ISD::FNEG:
  624. return ExpandFNEG(Op);
  625. case ISD::SETCC:
  626. return UnrollVSETCC(Op);
  627. default:
  628. return DAG.UnrollVectorOp(Op.getNode());
  629. }
  630. }
  631. SDValue VectorLegalizer::ExpandSELECT(SDValue Op) {
  632. // Lower a select instruction where the condition is a scalar and the
  633. // operands are vectors. Lower this select to VSELECT and implement it
  634. // using XOR AND OR. The selector bit is broadcasted.
  635. EVT VT = Op.getValueType();
  636. SDLoc DL(Op);
  637. SDValue Mask = Op.getOperand(0);
  638. SDValue Op1 = Op.getOperand(1);
  639. SDValue Op2 = Op.getOperand(2);
  640. assert(VT.isVector() && !Mask.getValueType().isVector()
  641. && Op1.getValueType() == Op2.getValueType() && "Invalid type");
  642. unsigned NumElem = VT.getVectorNumElements();
  643. // If we can't even use the basic vector operations of
  644. // AND,OR,XOR, we will have to scalarize the op.
  645. // Notice that the operation may be 'promoted' which means that it is
  646. // 'bitcasted' to another type which is handled.
  647. // Also, we need to be able to construct a splat vector using BUILD_VECTOR.
  648. if (TLI.getOperationAction(ISD::AND, VT) == TargetLowering::Expand ||
  649. TLI.getOperationAction(ISD::XOR, VT) == TargetLowering::Expand ||
  650. TLI.getOperationAction(ISD::OR, VT) == TargetLowering::Expand ||
  651. TLI.getOperationAction(ISD::BUILD_VECTOR, VT) == TargetLowering::Expand)
  652. return DAG.UnrollVectorOp(Op.getNode());
  653. // Generate a mask operand.
  654. EVT MaskTy = VT.changeVectorElementTypeToInteger();
  655. // What is the size of each element in the vector mask.
  656. EVT BitTy = MaskTy.getScalarType();
  657. Mask = DAG.getSelect(DL, BitTy, Mask,
  658. DAG.getConstant(APInt::getAllOnesValue(BitTy.getSizeInBits()), DL,
  659. BitTy),
  660. DAG.getConstant(0, DL, BitTy));
  661. // Broadcast the mask so that the entire vector is all-one or all zero.
  662. SmallVector<SDValue, 8> Ops(NumElem, Mask);
  663. Mask = DAG.getNode(ISD::BUILD_VECTOR, DL, MaskTy, Ops);
  664. // Bitcast the operands to be the same type as the mask.
  665. // This is needed when we select between FP types because
  666. // the mask is a vector of integers.
  667. Op1 = DAG.getNode(ISD::BITCAST, DL, MaskTy, Op1);
  668. Op2 = DAG.getNode(ISD::BITCAST, DL, MaskTy, Op2);
  669. SDValue AllOnes = DAG.getConstant(
  670. APInt::getAllOnesValue(BitTy.getSizeInBits()), DL, MaskTy);
  671. SDValue NotMask = DAG.getNode(ISD::XOR, DL, MaskTy, Mask, AllOnes);
  672. Op1 = DAG.getNode(ISD::AND, DL, MaskTy, Op1, Mask);
  673. Op2 = DAG.getNode(ISD::AND, DL, MaskTy, Op2, NotMask);
  674. SDValue Val = DAG.getNode(ISD::OR, DL, MaskTy, Op1, Op2);
  675. return DAG.getNode(ISD::BITCAST, DL, Op.getValueType(), Val);
  676. }
  677. SDValue VectorLegalizer::ExpandSEXTINREG(SDValue Op) {
  678. EVT VT = Op.getValueType();
  679. // Make sure that the SRA and SHL instructions are available.
  680. if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Expand ||
  681. TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Expand)
  682. return DAG.UnrollVectorOp(Op.getNode());
  683. SDLoc DL(Op);
  684. EVT OrigTy = cast<VTSDNode>(Op->getOperand(1))->getVT();
  685. unsigned BW = VT.getScalarType().getSizeInBits();
  686. unsigned OrigBW = OrigTy.getScalarType().getSizeInBits();
  687. SDValue ShiftSz = DAG.getConstant(BW - OrigBW, DL, VT);
  688. Op = Op.getOperand(0);
  689. Op = DAG.getNode(ISD::SHL, DL, VT, Op, ShiftSz);
  690. return DAG.getNode(ISD::SRA, DL, VT, Op, ShiftSz);
  691. }
  692. // Generically expand a vector anyext in register to a shuffle of the relevant
  693. // lanes into the appropriate locations, with other lanes left undef.
  694. SDValue VectorLegalizer::ExpandANY_EXTEND_VECTOR_INREG(SDValue Op) {
  695. SDLoc DL(Op);
  696. EVT VT = Op.getValueType();
  697. int NumElements = VT.getVectorNumElements();
  698. SDValue Src = Op.getOperand(0);
  699. EVT SrcVT = Src.getValueType();
  700. int NumSrcElements = SrcVT.getVectorNumElements();
  701. // Build a base mask of undef shuffles.
  702. SmallVector<int, 16> ShuffleMask;
  703. ShuffleMask.resize(NumSrcElements, -1);
  704. // Place the extended lanes into the correct locations.
  705. int ExtLaneScale = NumSrcElements / NumElements;
  706. int EndianOffset = DAG.getDataLayout().isBigEndian() ? ExtLaneScale - 1 : 0;
  707. for (int i = 0; i < NumElements; ++i)
  708. ShuffleMask[i * ExtLaneScale + EndianOffset] = i;
  709. return DAG.getNode(
  710. ISD::BITCAST, DL, VT,
  711. DAG.getVectorShuffle(SrcVT, DL, Src, DAG.getUNDEF(SrcVT), ShuffleMask));
  712. }
  713. SDValue VectorLegalizer::ExpandSIGN_EXTEND_VECTOR_INREG(SDValue Op) {
  714. SDLoc DL(Op);
  715. EVT VT = Op.getValueType();
  716. SDValue Src = Op.getOperand(0);
  717. EVT SrcVT = Src.getValueType();
  718. // First build an any-extend node which can be legalized above when we
  719. // recurse through it.
  720. Op = DAG.getAnyExtendVectorInReg(Src, DL, VT);
  721. // Now we need sign extend. Do this by shifting the elements. Even if these
  722. // aren't legal operations, they have a better chance of being legalized
  723. // without full scalarization than the sign extension does.
  724. unsigned EltWidth = VT.getVectorElementType().getSizeInBits();
  725. unsigned SrcEltWidth = SrcVT.getVectorElementType().getSizeInBits();
  726. SDValue ShiftAmount = DAG.getConstant(EltWidth - SrcEltWidth, DL, VT);
  727. return DAG.getNode(ISD::SRA, DL, VT,
  728. DAG.getNode(ISD::SHL, DL, VT, Op, ShiftAmount),
  729. ShiftAmount);
  730. }
  731. // Generically expand a vector zext in register to a shuffle of the relevant
  732. // lanes into the appropriate locations, a blend of zero into the high bits,
  733. // and a bitcast to the wider element type.
  734. SDValue VectorLegalizer::ExpandZERO_EXTEND_VECTOR_INREG(SDValue Op) {
  735. SDLoc DL(Op);
  736. EVT VT = Op.getValueType();
  737. int NumElements = VT.getVectorNumElements();
  738. SDValue Src = Op.getOperand(0);
  739. EVT SrcVT = Src.getValueType();
  740. int NumSrcElements = SrcVT.getVectorNumElements();
  741. // Build up a zero vector to blend into this one.
  742. EVT SrcScalarVT = SrcVT.getScalarType();
  743. SDValue ScalarZero = DAG.getTargetConstant(0, DL, SrcScalarVT);
  744. SmallVector<SDValue, 4> BuildVectorOperands(NumSrcElements, ScalarZero);
  745. SDValue Zero = DAG.getNode(ISD::BUILD_VECTOR, DL, SrcVT, BuildVectorOperands);
  746. // Shuffle the incoming lanes into the correct position, and pull all other
  747. // lanes from the zero vector.
  748. SmallVector<int, 16> ShuffleMask;
  749. ShuffleMask.reserve(NumSrcElements);
  750. for (int i = 0; i < NumSrcElements; ++i)
  751. ShuffleMask.push_back(i);
  752. int ExtLaneScale = NumSrcElements / NumElements;
  753. int EndianOffset = DAG.getDataLayout().isBigEndian() ? ExtLaneScale - 1 : 0;
  754. for (int i = 0; i < NumElements; ++i)
  755. ShuffleMask[i * ExtLaneScale + EndianOffset] = NumSrcElements + i;
  756. return DAG.getNode(ISD::BITCAST, DL, VT,
  757. DAG.getVectorShuffle(SrcVT, DL, Zero, Src, ShuffleMask));
  758. }
  759. SDValue VectorLegalizer::ExpandBSWAP(SDValue Op) {
  760. EVT VT = Op.getValueType();
  761. // Generate a byte wise shuffle mask for the BSWAP.
  762. SmallVector<int, 16> ShuffleMask;
  763. int ScalarSizeInBytes = VT.getScalarSizeInBits() / 8;
  764. for (int I = 0, E = VT.getVectorNumElements(); I != E; ++I)
  765. for (int J = ScalarSizeInBytes - 1; J >= 0; --J)
  766. ShuffleMask.push_back((I * ScalarSizeInBytes) + J);
  767. EVT ByteVT = EVT::getVectorVT(*DAG.getContext(), MVT::i8, ShuffleMask.size());
  768. // Only emit a shuffle if the mask is legal.
  769. if (!TLI.isShuffleMaskLegal(ShuffleMask, ByteVT))
  770. return DAG.UnrollVectorOp(Op.getNode());
  771. SDLoc DL(Op);
  772. Op = DAG.getNode(ISD::BITCAST, DL, ByteVT, Op.getOperand(0));
  773. Op = DAG.getVectorShuffle(ByteVT, DL, Op, DAG.getUNDEF(ByteVT),
  774. ShuffleMask.data());
  775. return DAG.getNode(ISD::BITCAST, DL, VT, Op);
  776. }
  777. SDValue VectorLegalizer::ExpandVSELECT(SDValue Op) {
  778. // Implement VSELECT in terms of XOR, AND, OR
  779. // on platforms which do not support blend natively.
  780. SDLoc DL(Op);
  781. SDValue Mask = Op.getOperand(0);
  782. SDValue Op1 = Op.getOperand(1);
  783. SDValue Op2 = Op.getOperand(2);
  784. EVT VT = Mask.getValueType();
  785. // If we can't even use the basic vector operations of
  786. // AND,OR,XOR, we will have to scalarize the op.
  787. // Notice that the operation may be 'promoted' which means that it is
  788. // 'bitcasted' to another type which is handled.
  789. // This operation also isn't safe with AND, OR, XOR when the boolean
  790. // type is 0/1 as we need an all ones vector constant to mask with.
  791. // FIXME: Sign extend 1 to all ones if thats legal on the target.
  792. if (TLI.getOperationAction(ISD::AND, VT) == TargetLowering::Expand ||
  793. TLI.getOperationAction(ISD::XOR, VT) == TargetLowering::Expand ||
  794. TLI.getOperationAction(ISD::OR, VT) == TargetLowering::Expand ||
  795. TLI.getBooleanContents(Op1.getValueType()) !=
  796. TargetLowering::ZeroOrNegativeOneBooleanContent)
  797. return DAG.UnrollVectorOp(Op.getNode());
  798. // If the mask and the type are different sizes, unroll the vector op. This
  799. // can occur when getSetCCResultType returns something that is different in
  800. // size from the operand types. For example, v4i8 = select v4i32, v4i8, v4i8.
  801. if (VT.getSizeInBits() != Op1.getValueType().getSizeInBits())
  802. return DAG.UnrollVectorOp(Op.getNode());
  803. // Bitcast the operands to be the same type as the mask.
  804. // This is needed when we select between FP types because
  805. // the mask is a vector of integers.
  806. Op1 = DAG.getNode(ISD::BITCAST, DL, VT, Op1);
  807. Op2 = DAG.getNode(ISD::BITCAST, DL, VT, Op2);
  808. SDValue AllOnes = DAG.getConstant(
  809. APInt::getAllOnesValue(VT.getScalarType().getSizeInBits()), DL, VT);
  810. SDValue NotMask = DAG.getNode(ISD::XOR, DL, VT, Mask, AllOnes);
  811. Op1 = DAG.getNode(ISD::AND, DL, VT, Op1, Mask);
  812. Op2 = DAG.getNode(ISD::AND, DL, VT, Op2, NotMask);
  813. SDValue Val = DAG.getNode(ISD::OR, DL, VT, Op1, Op2);
  814. return DAG.getNode(ISD::BITCAST, DL, Op.getValueType(), Val);
  815. }
  816. SDValue VectorLegalizer::ExpandUINT_TO_FLOAT(SDValue Op) {
  817. EVT VT = Op.getOperand(0).getValueType();
  818. SDLoc DL(Op);
  819. // Make sure that the SINT_TO_FP and SRL instructions are available.
  820. if (TLI.getOperationAction(ISD::SINT_TO_FP, VT) == TargetLowering::Expand ||
  821. TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Expand)
  822. return DAG.UnrollVectorOp(Op.getNode());
  823. EVT SVT = VT.getScalarType();
  824. assert((SVT.getSizeInBits() == 64 || SVT.getSizeInBits() == 32) &&
  825. "Elements in vector-UINT_TO_FP must be 32 or 64 bits wide");
  826. unsigned BW = SVT.getSizeInBits();
  827. SDValue HalfWord = DAG.getConstant(BW/2, DL, VT);
  828. // Constants to clear the upper part of the word.
  829. // Notice that we can also use SHL+SHR, but using a constant is slightly
  830. // faster on x86.
  831. uint64_t HWMask = (SVT.getSizeInBits()==64)?0x00000000FFFFFFFF:0x0000FFFF;
  832. SDValue HalfWordMask = DAG.getConstant(HWMask, DL, VT);
  833. // Two to the power of half-word-size.
  834. SDValue TWOHW = DAG.getConstantFP((((uint64_t)1)<<(BW/2)), DL, Op.getValueType()); // HLSL Change: do the 64-bit conversion before shift not after
  835. // Clear upper part of LO, lower HI
  836. SDValue HI = DAG.getNode(ISD::SRL, DL, VT, Op.getOperand(0), HalfWord);
  837. SDValue LO = DAG.getNode(ISD::AND, DL, VT, Op.getOperand(0), HalfWordMask);
  838. // Convert hi and lo to floats
  839. // Convert the hi part back to the upper values
  840. SDValue fHI = DAG.getNode(ISD::SINT_TO_FP, DL, Op.getValueType(), HI);
  841. fHI = DAG.getNode(ISD::FMUL, DL, Op.getValueType(), fHI, TWOHW);
  842. SDValue fLO = DAG.getNode(ISD::SINT_TO_FP, DL, Op.getValueType(), LO);
  843. // Add the two halves
  844. return DAG.getNode(ISD::FADD, DL, Op.getValueType(), fHI, fLO);
  845. }
  846. SDValue VectorLegalizer::ExpandFNEG(SDValue Op) {
  847. if (TLI.isOperationLegalOrCustom(ISD::FSUB, Op.getValueType())) {
  848. SDLoc DL(Op);
  849. SDValue Zero = DAG.getConstantFP(-0.0, DL, Op.getValueType());
  850. return DAG.getNode(ISD::FSUB, DL, Op.getValueType(),
  851. Zero, Op.getOperand(0));
  852. }
  853. return DAG.UnrollVectorOp(Op.getNode());
  854. }
  855. SDValue VectorLegalizer::UnrollVSETCC(SDValue Op) {
  856. EVT VT = Op.getValueType();
  857. unsigned NumElems = VT.getVectorNumElements();
  858. EVT EltVT = VT.getVectorElementType();
  859. SDValue LHS = Op.getOperand(0), RHS = Op.getOperand(1), CC = Op.getOperand(2);
  860. EVT TmpEltVT = LHS.getValueType().getVectorElementType();
  861. SDLoc dl(Op);
  862. SmallVector<SDValue, 8> Ops(NumElems);
  863. for (unsigned i = 0; i < NumElems; ++i) {
  864. SDValue LHSElem = DAG.getNode(
  865. ISD::EXTRACT_VECTOR_ELT, dl, TmpEltVT, LHS,
  866. DAG.getConstant(i, dl, TLI.getVectorIdxTy(DAG.getDataLayout())));
  867. SDValue RHSElem = DAG.getNode(
  868. ISD::EXTRACT_VECTOR_ELT, dl, TmpEltVT, RHS,
  869. DAG.getConstant(i, dl, TLI.getVectorIdxTy(DAG.getDataLayout())));
  870. Ops[i] = DAG.getNode(ISD::SETCC, dl,
  871. TLI.getSetCCResultType(DAG.getDataLayout(),
  872. *DAG.getContext(), TmpEltVT),
  873. LHSElem, RHSElem, CC);
  874. Ops[i] = DAG.getSelect(dl, EltVT, Ops[i],
  875. DAG.getConstant(APInt::getAllOnesValue
  876. (EltVT.getSizeInBits()), dl, EltVT),
  877. DAG.getConstant(0, dl, EltVT));
  878. }
  879. return DAG.getNode(ISD::BUILD_VECTOR, dl, VT, Ops);
  880. }
  881. }
  882. bool SelectionDAG::LegalizeVectors() {
  883. return VectorLegalizer(*this).Run();
  884. }