LegalizeVectorOps.cpp 29 KB

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