LegalizeVectorOps.cpp 29 KB

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