LegalizeVectorOps.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751
  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. /// LegalizedNodes - For nodes that are of legal width, and that have more
  38. /// than one use, this map indicates what regularized operand to use. This
  39. /// allows us to avoid legalizing the same thing more than once.
  40. SmallDenseMap<SDValue, SDValue, 64> LegalizedNodes;
  41. // 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. // Legalizes the given node
  49. SDValue LegalizeOp(SDValue Op);
  50. // Assuming the node is legal, "legalize" the results
  51. SDValue TranslateLegalizeResults(SDValue Op, SDValue Result);
  52. // Implements unrolling a VSETCC.
  53. SDValue UnrollVSETCC(SDValue Op);
  54. // Implements expansion for FNEG; falls back to UnrollVectorOp if FSUB
  55. // isn't legal.
  56. // Implements expansion for UINT_TO_FLOAT; falls back to UnrollVectorOp if
  57. // SINT_TO_FLOAT and SHR on vectors isn't legal.
  58. SDValue ExpandUINT_TO_FLOAT(SDValue Op);
  59. // Implement expansion for SIGN_EXTEND_INREG using SRL and SRA.
  60. SDValue ExpandSEXTINREG(SDValue Op);
  61. // Implement vselect in terms of XOR, AND, OR when blend is not supported
  62. // by the target.
  63. SDValue ExpandVSELECT(SDValue Op);
  64. SDValue ExpandSELECT(SDValue Op);
  65. SDValue ExpandLoad(SDValue Op);
  66. SDValue ExpandStore(SDValue Op);
  67. SDValue ExpandFNEG(SDValue Op);
  68. // Implements vector promotion; this is essentially just bitcasting the
  69. // operands to a different type and bitcasting the result back to the
  70. // original type.
  71. SDValue PromoteVectorOp(SDValue Op);
  72. // Implements [SU]INT_TO_FP vector promotion; this is a [zs]ext of the input
  73. // operand to the next size up.
  74. SDValue PromoteVectorOpINT_TO_FP(SDValue Op);
  75. public:
  76. bool Run();
  77. VectorLegalizer(SelectionDAG& dag) :
  78. DAG(dag), TLI(dag.getTargetLoweringInfo()), Changed(false) {}
  79. };
  80. bool VectorLegalizer::Run() {
  81. // The legalize process is inherently a bottom-up recursive process (users
  82. // legalize their uses before themselves). Given infinite stack space, we
  83. // could just start legalizing on the root and traverse the whole graph. In
  84. // practice however, this causes us to run out of stack space on large basic
  85. // blocks. To avoid this problem, compute an ordering of the nodes where each
  86. // node is only legalized after all of its operands are legalized.
  87. DAG.AssignTopologicalOrder();
  88. for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
  89. E = prior(DAG.allnodes_end()); I != llvm::next(E); ++I)
  90. LegalizeOp(SDValue(I, 0));
  91. // Finally, it's possible the root changed. Get the new root.
  92. SDValue OldRoot = DAG.getRoot();
  93. assert(LegalizedNodes.count(OldRoot) && "Root didn't get legalized?");
  94. DAG.setRoot(LegalizedNodes[OldRoot]);
  95. LegalizedNodes.clear();
  96. // Remove dead nodes now.
  97. DAG.RemoveDeadNodes();
  98. return Changed;
  99. }
  100. SDValue VectorLegalizer::TranslateLegalizeResults(SDValue Op, SDValue Result) {
  101. // Generic legalization: just pass the operand through.
  102. for (unsigned i = 0, e = Op.getNode()->getNumValues(); i != e; ++i)
  103. AddLegalizedOperand(Op.getValue(i), Result.getValue(i));
  104. return Result.getValue(Op.getResNo());
  105. }
  106. SDValue VectorLegalizer::LegalizeOp(SDValue Op) {
  107. // Note that LegalizeOp may be reentered even from single-use nodes, which
  108. // means that we always must cache transformed nodes.
  109. DenseMap<SDValue, SDValue>::iterator I = LegalizedNodes.find(Op);
  110. if (I != LegalizedNodes.end()) return I->second;
  111. SDNode* Node = Op.getNode();
  112. // Legalize the operands
  113. SmallVector<SDValue, 8> Ops;
  114. for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
  115. Ops.push_back(LegalizeOp(Node->getOperand(i)));
  116. SDValue Result =
  117. SDValue(DAG.UpdateNodeOperands(Op.getNode(), Ops.data(), Ops.size()), 0);
  118. if (Op.getOpcode() == ISD::LOAD) {
  119. LoadSDNode *LD = cast<LoadSDNode>(Op.getNode());
  120. ISD::LoadExtType ExtType = LD->getExtensionType();
  121. if (LD->getMemoryVT().isVector() && ExtType != ISD::NON_EXTLOAD) {
  122. if (TLI.isLoadExtLegal(LD->getExtensionType(), LD->getMemoryVT()))
  123. return TranslateLegalizeResults(Op, Result);
  124. Changed = true;
  125. return LegalizeOp(ExpandLoad(Op));
  126. }
  127. } else if (Op.getOpcode() == ISD::STORE) {
  128. StoreSDNode *ST = cast<StoreSDNode>(Op.getNode());
  129. EVT StVT = ST->getMemoryVT();
  130. MVT ValVT = ST->getValue().getSimpleValueType();
  131. if (StVT.isVector() && ST->isTruncatingStore())
  132. switch (TLI.getTruncStoreAction(ValVT, StVT.getSimpleVT())) {
  133. default: llvm_unreachable("This action is not supported yet!");
  134. case TargetLowering::Legal:
  135. return TranslateLegalizeResults(Op, Result);
  136. case TargetLowering::Custom:
  137. Changed = true;
  138. return LegalizeOp(TLI.LowerOperation(Result, DAG));
  139. case TargetLowering::Expand:
  140. Changed = true;
  141. return LegalizeOp(ExpandStore(Op));
  142. }
  143. }
  144. bool HasVectorValue = false;
  145. for (SDNode::value_iterator J = Node->value_begin(), E = Node->value_end();
  146. J != E;
  147. ++J)
  148. HasVectorValue |= J->isVector();
  149. if (!HasVectorValue)
  150. return TranslateLegalizeResults(Op, Result);
  151. EVT QueryType;
  152. switch (Op.getOpcode()) {
  153. default:
  154. return TranslateLegalizeResults(Op, Result);
  155. case ISD::ADD:
  156. case ISD::SUB:
  157. case ISD::MUL:
  158. case ISD::SDIV:
  159. case ISD::UDIV:
  160. case ISD::SREM:
  161. case ISD::UREM:
  162. case ISD::FADD:
  163. case ISD::FSUB:
  164. case ISD::FMUL:
  165. case ISD::FDIV:
  166. case ISD::FREM:
  167. case ISD::AND:
  168. case ISD::OR:
  169. case ISD::XOR:
  170. case ISD::SHL:
  171. case ISD::SRA:
  172. case ISD::SRL:
  173. case ISD::ROTL:
  174. case ISD::ROTR:
  175. case ISD::CTLZ:
  176. case ISD::CTTZ:
  177. case ISD::CTLZ_ZERO_UNDEF:
  178. case ISD::CTTZ_ZERO_UNDEF:
  179. case ISD::CTPOP:
  180. case ISD::SELECT:
  181. case ISD::VSELECT:
  182. case ISD::SELECT_CC:
  183. case ISD::SETCC:
  184. case ISD::ZERO_EXTEND:
  185. case ISD::ANY_EXTEND:
  186. case ISD::TRUNCATE:
  187. case ISD::SIGN_EXTEND:
  188. case ISD::FP_TO_SINT:
  189. case ISD::FP_TO_UINT:
  190. case ISD::FNEG:
  191. case ISD::FABS:
  192. case ISD::FSQRT:
  193. case ISD::FSIN:
  194. case ISD::FCOS:
  195. case ISD::FPOWI:
  196. case ISD::FPOW:
  197. case ISD::FLOG:
  198. case ISD::FLOG2:
  199. case ISD::FLOG10:
  200. case ISD::FEXP:
  201. case ISD::FEXP2:
  202. case ISD::FCEIL:
  203. case ISD::FTRUNC:
  204. case ISD::FRINT:
  205. case ISD::FNEARBYINT:
  206. case ISD::FFLOOR:
  207. case ISD::FP_ROUND:
  208. case ISD::FP_EXTEND:
  209. case ISD::FMA:
  210. case ISD::SIGN_EXTEND_INREG:
  211. QueryType = Node->getValueType(0);
  212. break;
  213. case ISD::FP_ROUND_INREG:
  214. QueryType = cast<VTSDNode>(Node->getOperand(1))->getVT();
  215. break;
  216. case ISD::SINT_TO_FP:
  217. case ISD::UINT_TO_FP:
  218. QueryType = Node->getOperand(0).getValueType();
  219. break;
  220. }
  221. switch (TLI.getOperationAction(Node->getOpcode(), QueryType)) {
  222. case TargetLowering::Promote:
  223. switch (Op.getOpcode()) {
  224. default:
  225. // "Promote" the operation by bitcasting
  226. Result = PromoteVectorOp(Op);
  227. Changed = true;
  228. break;
  229. case ISD::SINT_TO_FP:
  230. case ISD::UINT_TO_FP:
  231. // "Promote" the operation by extending the operand.
  232. Result = PromoteVectorOpINT_TO_FP(Op);
  233. Changed = true;
  234. break;
  235. }
  236. break;
  237. case TargetLowering::Legal: break;
  238. case TargetLowering::Custom: {
  239. SDValue Tmp1 = TLI.LowerOperation(Op, DAG);
  240. if (Tmp1.getNode()) {
  241. Result = Tmp1;
  242. break;
  243. }
  244. // FALL THROUGH
  245. }
  246. case TargetLowering::Expand:
  247. if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG)
  248. Result = ExpandSEXTINREG(Op);
  249. else if (Node->getOpcode() == ISD::VSELECT)
  250. Result = ExpandVSELECT(Op);
  251. else if (Node->getOpcode() == ISD::SELECT)
  252. Result = ExpandSELECT(Op);
  253. else if (Node->getOpcode() == ISD::UINT_TO_FP)
  254. Result = ExpandUINT_TO_FLOAT(Op);
  255. else if (Node->getOpcode() == ISD::FNEG)
  256. Result = ExpandFNEG(Op);
  257. else if (Node->getOpcode() == ISD::SETCC)
  258. Result = UnrollVSETCC(Op);
  259. else
  260. Result = DAG.UnrollVectorOp(Op.getNode());
  261. break;
  262. }
  263. // Make sure that the generated code is itself legal.
  264. if (Result != Op) {
  265. Result = LegalizeOp(Result);
  266. Changed = true;
  267. }
  268. // Note that LegalizeOp may be reentered even from single-use nodes, which
  269. // means that we always must cache transformed nodes.
  270. AddLegalizedOperand(Op, Result);
  271. return Result;
  272. }
  273. SDValue VectorLegalizer::PromoteVectorOp(SDValue Op) {
  274. // Vector "promotion" is basically just bitcasting and doing the operation
  275. // in a different type. For example, x86 promotes ISD::AND on v2i32 to
  276. // v1i64.
  277. MVT VT = Op.getSimpleValueType();
  278. assert(Op.getNode()->getNumValues() == 1 &&
  279. "Can't promote a vector with multiple results!");
  280. MVT NVT = TLI.getTypeToPromoteTo(Op.getOpcode(), VT);
  281. DebugLoc dl = Op.getDebugLoc();
  282. SmallVector<SDValue, 4> Operands(Op.getNumOperands());
  283. for (unsigned j = 0; j != Op.getNumOperands(); ++j) {
  284. if (Op.getOperand(j).getValueType().isVector())
  285. Operands[j] = DAG.getNode(ISD::BITCAST, dl, NVT, Op.getOperand(j));
  286. else
  287. Operands[j] = Op.getOperand(j);
  288. }
  289. Op = DAG.getNode(Op.getOpcode(), dl, NVT, &Operands[0], Operands.size());
  290. return DAG.getNode(ISD::BITCAST, dl, VT, Op);
  291. }
  292. SDValue VectorLegalizer::PromoteVectorOpINT_TO_FP(SDValue Op) {
  293. // INT_TO_FP operations may require the input operand be promoted even
  294. // when the type is otherwise legal.
  295. EVT VT = Op.getOperand(0).getValueType();
  296. assert(Op.getNode()->getNumValues() == 1 &&
  297. "Can't promote a vector with multiple results!");
  298. // Normal getTypeToPromoteTo() doesn't work here, as that will promote
  299. // by widening the vector w/ the same element width and twice the number
  300. // of elements. We want the other way around, the same number of elements,
  301. // each twice the width.
  302. //
  303. // Increase the bitwidth of the element to the next pow-of-two
  304. // (which is greater than 8 bits).
  305. unsigned NumElts = VT.getVectorNumElements();
  306. EVT EltVT = VT.getVectorElementType();
  307. EltVT = EVT::getIntegerVT(*DAG.getContext(), 2 * EltVT.getSizeInBits());
  308. assert(EltVT.isSimple() && "Promoting to a non-simple vector type!");
  309. // Build a new vector type and check if it is legal.
  310. MVT NVT = MVT::getVectorVT(EltVT.getSimpleVT(), NumElts);
  311. DebugLoc dl = Op.getDebugLoc();
  312. SmallVector<SDValue, 4> Operands(Op.getNumOperands());
  313. unsigned Opc = Op.getOpcode() == ISD::UINT_TO_FP ? ISD::ZERO_EXTEND :
  314. ISD::SIGN_EXTEND;
  315. for (unsigned j = 0; j != Op.getNumOperands(); ++j) {
  316. if (Op.getOperand(j).getValueType().isVector())
  317. Operands[j] = DAG.getNode(Opc, dl, NVT, Op.getOperand(j));
  318. else
  319. Operands[j] = Op.getOperand(j);
  320. }
  321. return DAG.getNode(Op.getOpcode(), dl, Op.getValueType(), &Operands[0],
  322. Operands.size());
  323. }
  324. SDValue VectorLegalizer::ExpandLoad(SDValue Op) {
  325. DebugLoc dl = Op.getDebugLoc();
  326. LoadSDNode *LD = cast<LoadSDNode>(Op.getNode());
  327. SDValue Chain = LD->getChain();
  328. SDValue BasePTR = LD->getBasePtr();
  329. EVT SrcVT = LD->getMemoryVT();
  330. ISD::LoadExtType ExtType = LD->getExtensionType();
  331. SmallVector<SDValue, 8> Vals;
  332. SmallVector<SDValue, 8> LoadChains;
  333. unsigned NumElem = SrcVT.getVectorNumElements();
  334. EVT SrcEltVT = SrcVT.getScalarType();
  335. EVT DstEltVT = Op.getNode()->getValueType(0).getScalarType();
  336. if (SrcVT.getVectorNumElements() > 1 && !SrcEltVT.isByteSized()) {
  337. // When elements in a vector is not byte-addressable, we cannot directly
  338. // load each element by advancing pointer, which could only address bytes.
  339. // Instead, we load all significant words, mask bits off, and concatenate
  340. // them to form each element. Finally, they are extended to destination
  341. // scalar type to build the destination vector.
  342. EVT WideVT = TLI.getPointerTy();
  343. assert(WideVT.isRound() &&
  344. "Could not handle the sophisticated case when the widest integer is"
  345. " not power of 2.");
  346. assert(WideVT.bitsGE(SrcEltVT) &&
  347. "Type is not legalized?");
  348. unsigned WideBytes = WideVT.getStoreSize();
  349. unsigned Offset = 0;
  350. unsigned RemainingBytes = SrcVT.getStoreSize();
  351. SmallVector<SDValue, 8> LoadVals;
  352. while (RemainingBytes > 0) {
  353. SDValue ScalarLoad;
  354. unsigned LoadBytes = WideBytes;
  355. if (RemainingBytes >= LoadBytes) {
  356. ScalarLoad = DAG.getLoad(WideVT, dl, Chain, BasePTR,
  357. LD->getPointerInfo().getWithOffset(Offset),
  358. LD->isVolatile(), LD->isNonTemporal(),
  359. LD->isInvariant(), LD->getAlignment());
  360. } else {
  361. EVT LoadVT = WideVT;
  362. while (RemainingBytes < LoadBytes) {
  363. LoadBytes >>= 1; // Reduce the load size by half.
  364. LoadVT = EVT::getIntegerVT(*DAG.getContext(), LoadBytes << 3);
  365. }
  366. ScalarLoad = DAG.getExtLoad(ISD::EXTLOAD, dl, WideVT, Chain, BasePTR,
  367. LD->getPointerInfo().getWithOffset(Offset),
  368. LoadVT, LD->isVolatile(),
  369. LD->isNonTemporal(), LD->getAlignment());
  370. }
  371. RemainingBytes -= LoadBytes;
  372. Offset += LoadBytes;
  373. BasePTR = DAG.getNode(ISD::ADD, dl, BasePTR.getValueType(), BasePTR,
  374. DAG.getIntPtrConstant(LoadBytes));
  375. LoadVals.push_back(ScalarLoad.getValue(0));
  376. LoadChains.push_back(ScalarLoad.getValue(1));
  377. }
  378. // Extract bits, pack and extend/trunc them into destination type.
  379. unsigned SrcEltBits = SrcEltVT.getSizeInBits();
  380. SDValue SrcEltBitMask = DAG.getConstant((1U << SrcEltBits) - 1, WideVT);
  381. unsigned BitOffset = 0;
  382. unsigned WideIdx = 0;
  383. unsigned WideBits = WideVT.getSizeInBits();
  384. for (unsigned Idx = 0; Idx != NumElem; ++Idx) {
  385. SDValue Lo, Hi, ShAmt;
  386. if (BitOffset < WideBits) {
  387. ShAmt = DAG.getConstant(BitOffset, TLI.getShiftAmountTy(WideVT));
  388. Lo = DAG.getNode(ISD::SRL, dl, WideVT, LoadVals[WideIdx], ShAmt);
  389. Lo = DAG.getNode(ISD::AND, dl, WideVT, Lo, SrcEltBitMask);
  390. }
  391. BitOffset += SrcEltBits;
  392. if (BitOffset >= WideBits) {
  393. WideIdx++;
  394. Offset -= WideBits;
  395. if (Offset > 0) {
  396. ShAmt = DAG.getConstant(SrcEltBits - Offset,
  397. TLI.getShiftAmountTy(WideVT));
  398. Hi = DAG.getNode(ISD::SHL, dl, WideVT, LoadVals[WideIdx], ShAmt);
  399. Hi = DAG.getNode(ISD::AND, dl, WideVT, Hi, SrcEltBitMask);
  400. }
  401. }
  402. if (Hi.getNode())
  403. Lo = DAG.getNode(ISD::OR, dl, WideVT, Lo, Hi);
  404. switch (ExtType) {
  405. default: llvm_unreachable("Unknown extended-load op!");
  406. case ISD::EXTLOAD:
  407. Lo = DAG.getAnyExtOrTrunc(Lo, dl, DstEltVT);
  408. break;
  409. case ISD::ZEXTLOAD:
  410. Lo = DAG.getZExtOrTrunc(Lo, dl, DstEltVT);
  411. break;
  412. case ISD::SEXTLOAD:
  413. ShAmt = DAG.getConstant(WideBits - SrcEltBits,
  414. TLI.getShiftAmountTy(WideVT));
  415. Lo = DAG.getNode(ISD::SHL, dl, WideVT, Lo, ShAmt);
  416. Lo = DAG.getNode(ISD::SRA, dl, WideVT, Lo, ShAmt);
  417. Lo = DAG.getSExtOrTrunc(Lo, dl, DstEltVT);
  418. break;
  419. }
  420. Vals.push_back(Lo);
  421. }
  422. } else {
  423. unsigned Stride = SrcVT.getScalarType().getSizeInBits()/8;
  424. for (unsigned Idx=0; Idx<NumElem; Idx++) {
  425. SDValue ScalarLoad = DAG.getExtLoad(ExtType, dl,
  426. Op.getNode()->getValueType(0).getScalarType(),
  427. Chain, BasePTR, LD->getPointerInfo().getWithOffset(Idx * Stride),
  428. SrcVT.getScalarType(),
  429. LD->isVolatile(), LD->isNonTemporal(),
  430. LD->getAlignment());
  431. BasePTR = DAG.getNode(ISD::ADD, dl, BasePTR.getValueType(), BasePTR,
  432. DAG.getIntPtrConstant(Stride));
  433. Vals.push_back(ScalarLoad.getValue(0));
  434. LoadChains.push_back(ScalarLoad.getValue(1));
  435. }
  436. }
  437. SDValue NewChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
  438. &LoadChains[0], LoadChains.size());
  439. SDValue Value = DAG.getNode(ISD::BUILD_VECTOR, dl,
  440. Op.getNode()->getValueType(0), &Vals[0], Vals.size());
  441. AddLegalizedOperand(Op.getValue(0), Value);
  442. AddLegalizedOperand(Op.getValue(1), NewChain);
  443. return (Op.getResNo() ? NewChain : Value);
  444. }
  445. SDValue VectorLegalizer::ExpandStore(SDValue Op) {
  446. DebugLoc dl = Op.getDebugLoc();
  447. StoreSDNode *ST = cast<StoreSDNode>(Op.getNode());
  448. SDValue Chain = ST->getChain();
  449. SDValue BasePTR = ST->getBasePtr();
  450. SDValue Value = ST->getValue();
  451. EVT StVT = ST->getMemoryVT();
  452. unsigned Alignment = ST->getAlignment();
  453. bool isVolatile = ST->isVolatile();
  454. bool isNonTemporal = ST->isNonTemporal();
  455. unsigned NumElem = StVT.getVectorNumElements();
  456. // The type of the data we want to save
  457. EVT RegVT = Value.getValueType();
  458. EVT RegSclVT = RegVT.getScalarType();
  459. // The type of data as saved in memory.
  460. EVT MemSclVT = StVT.getScalarType();
  461. // Cast floats into integers
  462. unsigned ScalarSize = MemSclVT.getSizeInBits();
  463. // Round odd types to the next pow of two.
  464. if (!isPowerOf2_32(ScalarSize))
  465. ScalarSize = NextPowerOf2(ScalarSize);
  466. // Store Stride in bytes
  467. unsigned Stride = ScalarSize/8;
  468. // Extract each of the elements from the original vector
  469. // and save them into memory individually.
  470. SmallVector<SDValue, 8> Stores;
  471. for (unsigned Idx = 0; Idx < NumElem; Idx++) {
  472. SDValue Ex = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
  473. RegSclVT, Value, DAG.getIntPtrConstant(Idx));
  474. // This scalar TruncStore may be illegal, but we legalize it later.
  475. SDValue Store = DAG.getTruncStore(Chain, dl, Ex, BasePTR,
  476. ST->getPointerInfo().getWithOffset(Idx*Stride), MemSclVT,
  477. isVolatile, isNonTemporal, Alignment);
  478. BasePTR = DAG.getNode(ISD::ADD, dl, BasePTR.getValueType(), BasePTR,
  479. DAG.getIntPtrConstant(Stride));
  480. Stores.push_back(Store);
  481. }
  482. SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
  483. &Stores[0], Stores.size());
  484. AddLegalizedOperand(Op, TF);
  485. return TF;
  486. }
  487. SDValue VectorLegalizer::ExpandSELECT(SDValue Op) {
  488. // Lower a select instruction where the condition is a scalar and the
  489. // operands are vectors. Lower this select to VSELECT and implement it
  490. // using XOR AND OR. The selector bit is broadcasted.
  491. EVT VT = Op.getValueType();
  492. DebugLoc DL = Op.getDebugLoc();
  493. SDValue Mask = Op.getOperand(0);
  494. SDValue Op1 = Op.getOperand(1);
  495. SDValue Op2 = Op.getOperand(2);
  496. assert(VT.isVector() && !Mask.getValueType().isVector()
  497. && Op1.getValueType() == Op2.getValueType() && "Invalid type");
  498. unsigned NumElem = VT.getVectorNumElements();
  499. // If we can't even use the basic vector operations of
  500. // AND,OR,XOR, we will have to scalarize the op.
  501. // Notice that the operation may be 'promoted' which means that it is
  502. // 'bitcasted' to another type which is handled.
  503. // Also, we need to be able to construct a splat vector using BUILD_VECTOR.
  504. if (TLI.getOperationAction(ISD::AND, VT) == TargetLowering::Expand ||
  505. TLI.getOperationAction(ISD::XOR, VT) == TargetLowering::Expand ||
  506. TLI.getOperationAction(ISD::OR, VT) == TargetLowering::Expand ||
  507. TLI.getOperationAction(ISD::BUILD_VECTOR, VT) == TargetLowering::Expand)
  508. return DAG.UnrollVectorOp(Op.getNode());
  509. // Generate a mask operand.
  510. EVT MaskTy = TLI.getSetCCResultType(VT);
  511. assert(MaskTy.isVector() && "Invalid CC type");
  512. assert(MaskTy.getSizeInBits() == Op1.getValueType().getSizeInBits()
  513. && "Invalid mask size");
  514. // What is the size of each element in the vector mask.
  515. EVT BitTy = MaskTy.getScalarType();
  516. Mask = DAG.getNode(ISD::SELECT, DL, BitTy, Mask,
  517. DAG.getConstant(APInt::getAllOnesValue(BitTy.getSizeInBits()), BitTy),
  518. DAG.getConstant(0, BitTy));
  519. // Broadcast the mask so that the entire vector is all-one or all zero.
  520. SmallVector<SDValue, 8> Ops(NumElem, Mask);
  521. Mask = DAG.getNode(ISD::BUILD_VECTOR, DL, MaskTy, &Ops[0], Ops.size());
  522. // Bitcast the operands to be the same type as the mask.
  523. // This is needed when we select between FP types because
  524. // the mask is a vector of integers.
  525. Op1 = DAG.getNode(ISD::BITCAST, DL, MaskTy, Op1);
  526. Op2 = DAG.getNode(ISD::BITCAST, DL, MaskTy, Op2);
  527. SDValue AllOnes = DAG.getConstant(
  528. APInt::getAllOnesValue(BitTy.getSizeInBits()), MaskTy);
  529. SDValue NotMask = DAG.getNode(ISD::XOR, DL, MaskTy, Mask, AllOnes);
  530. Op1 = DAG.getNode(ISD::AND, DL, MaskTy, Op1, Mask);
  531. Op2 = DAG.getNode(ISD::AND, DL, MaskTy, Op2, NotMask);
  532. SDValue Val = DAG.getNode(ISD::OR, DL, MaskTy, Op1, Op2);
  533. return DAG.getNode(ISD::BITCAST, DL, Op.getValueType(), Val);
  534. }
  535. SDValue VectorLegalizer::ExpandSEXTINREG(SDValue Op) {
  536. EVT VT = Op.getValueType();
  537. // Make sure that the SRA and SHL instructions are available.
  538. if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Expand ||
  539. TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Expand)
  540. return DAG.UnrollVectorOp(Op.getNode());
  541. DebugLoc DL = Op.getDebugLoc();
  542. EVT OrigTy = cast<VTSDNode>(Op->getOperand(1))->getVT();
  543. unsigned BW = VT.getScalarType().getSizeInBits();
  544. unsigned OrigBW = OrigTy.getScalarType().getSizeInBits();
  545. SDValue ShiftSz = DAG.getConstant(BW - OrigBW, VT);
  546. Op = Op.getOperand(0);
  547. Op = DAG.getNode(ISD::SHL, DL, VT, Op, ShiftSz);
  548. return DAG.getNode(ISD::SRA, DL, VT, Op, ShiftSz);
  549. }
  550. SDValue VectorLegalizer::ExpandVSELECT(SDValue Op) {
  551. // Implement VSELECT in terms of XOR, AND, OR
  552. // on platforms which do not support blend natively.
  553. EVT VT = Op.getOperand(0).getValueType();
  554. DebugLoc DL = Op.getDebugLoc();
  555. SDValue Mask = Op.getOperand(0);
  556. SDValue Op1 = Op.getOperand(1);
  557. SDValue Op2 = Op.getOperand(2);
  558. // If we can't even use the basic vector operations of
  559. // AND,OR,XOR, we will have to scalarize the op.
  560. // Notice that the operation may be 'promoted' which means that it is
  561. // 'bitcasted' to another type which is handled.
  562. // This operation also isn't safe with AND, OR, XOR when the boolean
  563. // type is 0/1 as we need an all ones vector constant to mask with.
  564. // FIXME: Sign extend 1 to all ones if thats legal on the target.
  565. if (TLI.getOperationAction(ISD::AND, VT) == TargetLowering::Expand ||
  566. TLI.getOperationAction(ISD::XOR, VT) == TargetLowering::Expand ||
  567. TLI.getOperationAction(ISD::OR, VT) == TargetLowering::Expand ||
  568. TLI.getBooleanContents(true) !=
  569. TargetLowering::ZeroOrNegativeOneBooleanContent)
  570. return DAG.UnrollVectorOp(Op.getNode());
  571. assert(VT.getSizeInBits() == Op1.getValueType().getSizeInBits()
  572. && "Invalid mask size");
  573. // Bitcast the operands to be the same type as the mask.
  574. // This is needed when we select between FP types because
  575. // the mask is a vector of integers.
  576. Op1 = DAG.getNode(ISD::BITCAST, DL, VT, Op1);
  577. Op2 = DAG.getNode(ISD::BITCAST, DL, VT, Op2);
  578. SDValue AllOnes = DAG.getConstant(
  579. APInt::getAllOnesValue(VT.getScalarType().getSizeInBits()), VT);
  580. SDValue NotMask = DAG.getNode(ISD::XOR, DL, VT, Mask, AllOnes);
  581. Op1 = DAG.getNode(ISD::AND, DL, VT, Op1, Mask);
  582. Op2 = DAG.getNode(ISD::AND, DL, VT, Op2, NotMask);
  583. SDValue Val = DAG.getNode(ISD::OR, DL, VT, Op1, Op2);
  584. return DAG.getNode(ISD::BITCAST, DL, Op.getValueType(), Val);
  585. }
  586. SDValue VectorLegalizer::ExpandUINT_TO_FLOAT(SDValue Op) {
  587. EVT VT = Op.getOperand(0).getValueType();
  588. DebugLoc DL = Op.getDebugLoc();
  589. // Make sure that the SINT_TO_FP and SRL instructions are available.
  590. if (TLI.getOperationAction(ISD::SINT_TO_FP, VT) == TargetLowering::Expand ||
  591. TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Expand)
  592. return DAG.UnrollVectorOp(Op.getNode());
  593. EVT SVT = VT.getScalarType();
  594. assert((SVT.getSizeInBits() == 64 || SVT.getSizeInBits() == 32) &&
  595. "Elements in vector-UINT_TO_FP must be 32 or 64 bits wide");
  596. unsigned BW = SVT.getSizeInBits();
  597. SDValue HalfWord = DAG.getConstant(BW/2, VT);
  598. // Constants to clear the upper part of the word.
  599. // Notice that we can also use SHL+SHR, but using a constant is slightly
  600. // faster on x86.
  601. uint64_t HWMask = (SVT.getSizeInBits()==64)?0x00000000FFFFFFFF:0x0000FFFF;
  602. SDValue HalfWordMask = DAG.getConstant(HWMask, VT);
  603. // Two to the power of half-word-size.
  604. SDValue TWOHW = DAG.getConstantFP((1<<(BW/2)), Op.getValueType());
  605. // Clear upper part of LO, lower HI
  606. SDValue HI = DAG.getNode(ISD::SRL, DL, VT, Op.getOperand(0), HalfWord);
  607. SDValue LO = DAG.getNode(ISD::AND, DL, VT, Op.getOperand(0), HalfWordMask);
  608. // Convert hi and lo to floats
  609. // Convert the hi part back to the upper values
  610. SDValue fHI = DAG.getNode(ISD::SINT_TO_FP, DL, Op.getValueType(), HI);
  611. fHI = DAG.getNode(ISD::FMUL, DL, Op.getValueType(), fHI, TWOHW);
  612. SDValue fLO = DAG.getNode(ISD::SINT_TO_FP, DL, Op.getValueType(), LO);
  613. // Add the two halves
  614. return DAG.getNode(ISD::FADD, DL, Op.getValueType(), fHI, fLO);
  615. }
  616. SDValue VectorLegalizer::ExpandFNEG(SDValue Op) {
  617. if (TLI.isOperationLegalOrCustom(ISD::FSUB, Op.getValueType())) {
  618. SDValue Zero = DAG.getConstantFP(-0.0, Op.getValueType());
  619. return DAG.getNode(ISD::FSUB, Op.getDebugLoc(), Op.getValueType(),
  620. Zero, Op.getOperand(0));
  621. }
  622. return DAG.UnrollVectorOp(Op.getNode());
  623. }
  624. SDValue VectorLegalizer::UnrollVSETCC(SDValue Op) {
  625. EVT VT = Op.getValueType();
  626. unsigned NumElems = VT.getVectorNumElements();
  627. EVT EltVT = VT.getVectorElementType();
  628. SDValue LHS = Op.getOperand(0), RHS = Op.getOperand(1), CC = Op.getOperand(2);
  629. EVT TmpEltVT = LHS.getValueType().getVectorElementType();
  630. DebugLoc dl = Op.getDebugLoc();
  631. SmallVector<SDValue, 8> Ops(NumElems);
  632. for (unsigned i = 0; i < NumElems; ++i) {
  633. SDValue LHSElem = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, TmpEltVT, LHS,
  634. DAG.getIntPtrConstant(i));
  635. SDValue RHSElem = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, TmpEltVT, RHS,
  636. DAG.getIntPtrConstant(i));
  637. Ops[i] = DAG.getNode(ISD::SETCC, dl, TLI.getSetCCResultType(TmpEltVT),
  638. LHSElem, RHSElem, CC);
  639. Ops[i] = DAG.getNode(ISD::SELECT, dl, EltVT, Ops[i],
  640. DAG.getConstant(APInt::getAllOnesValue
  641. (EltVT.getSizeInBits()), EltVT),
  642. DAG.getConstant(0, EltVT));
  643. }
  644. return DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &Ops[0], NumElems);
  645. }
  646. }
  647. bool SelectionDAG::LegalizeVectors() {
  648. return VectorLegalizer(*this).Run();
  649. }