LegalizeTypesGeneric.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  1. //===-------- LegalizeTypesGeneric.cpp - Generic type legalization --------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file implements generic type expansion and splitting for LegalizeTypes.
  10. // The routines here perform legalization when the details of the type (such as
  11. // whether it is an integer or a float) do not matter.
  12. // Expansion is the act of changing a computation in an illegal type to be a
  13. // computation in two identical registers of a smaller type. The Lo/Hi part
  14. // is required to be stored first in memory on little/big-endian machines.
  15. // Splitting is the act of changing a computation in an illegal type to be a
  16. // computation in two not necessarily identical registers of a smaller type.
  17. // There are no requirements on how the type is represented in memory.
  18. //
  19. //===----------------------------------------------------------------------===//
  20. #include "LegalizeTypes.h"
  21. #include "llvm/IR/DataLayout.h"
  22. using namespace llvm;
  23. #define DEBUG_TYPE "legalize-types"
  24. //===----------------------------------------------------------------------===//
  25. // Generic Result Expansion.
  26. //===----------------------------------------------------------------------===//
  27. // These routines assume that the Lo/Hi part is stored first in memory on
  28. // little/big-endian machines, followed by the Hi/Lo part. This means that
  29. // they cannot be used as is on vectors, for which Lo is always stored first.
  30. void DAGTypeLegalizer::ExpandRes_MERGE_VALUES(SDNode *N, unsigned ResNo,
  31. SDValue &Lo, SDValue &Hi) {
  32. SDValue Op = DisintegrateMERGE_VALUES(N, ResNo);
  33. GetExpandedOp(Op, Lo, Hi);
  34. }
  35. void DAGTypeLegalizer::ExpandRes_BITCAST(SDNode *N, SDValue &Lo, SDValue &Hi) {
  36. EVT OutVT = N->getValueType(0);
  37. EVT NOutVT = TLI.getTypeToTransformTo(*DAG.getContext(), OutVT);
  38. SDValue InOp = N->getOperand(0);
  39. EVT InVT = InOp.getValueType();
  40. SDLoc dl(N);
  41. // Handle some special cases efficiently.
  42. switch (getTypeAction(InVT)) {
  43. case TargetLowering::TypeLegal:
  44. case TargetLowering::TypePromoteInteger:
  45. break;
  46. case TargetLowering::TypePromoteFloat:
  47. llvm_unreachable("Bitcast of a promotion-needing float should never need"
  48. "expansion");
  49. case TargetLowering::TypeSoftenFloat:
  50. SplitInteger(GetSoftenedFloat(InOp), Lo, Hi);
  51. Lo = DAG.getNode(ISD::BITCAST, dl, NOutVT, Lo);
  52. Hi = DAG.getNode(ISD::BITCAST, dl, NOutVT, Hi);
  53. return;
  54. case TargetLowering::TypeExpandInteger:
  55. case TargetLowering::TypeExpandFloat: {
  56. auto &DL = DAG.getDataLayout();
  57. // Convert the expanded pieces of the input.
  58. GetExpandedOp(InOp, Lo, Hi);
  59. if (TLI.hasBigEndianPartOrdering(InVT, DL) !=
  60. TLI.hasBigEndianPartOrdering(OutVT, DL))
  61. std::swap(Lo, Hi);
  62. Lo = DAG.getNode(ISD::BITCAST, dl, NOutVT, Lo);
  63. Hi = DAG.getNode(ISD::BITCAST, dl, NOutVT, Hi);
  64. return;
  65. }
  66. case TargetLowering::TypeSplitVector:
  67. GetSplitVector(InOp, Lo, Hi);
  68. if (TLI.hasBigEndianPartOrdering(OutVT, DAG.getDataLayout()))
  69. std::swap(Lo, Hi);
  70. Lo = DAG.getNode(ISD::BITCAST, dl, NOutVT, Lo);
  71. Hi = DAG.getNode(ISD::BITCAST, dl, NOutVT, Hi);
  72. return;
  73. case TargetLowering::TypeScalarizeVector:
  74. // Convert the element instead.
  75. SplitInteger(BitConvertToInteger(GetScalarizedVector(InOp)), Lo, Hi);
  76. Lo = DAG.getNode(ISD::BITCAST, dl, NOutVT, Lo);
  77. Hi = DAG.getNode(ISD::BITCAST, dl, NOutVT, Hi);
  78. return;
  79. case TargetLowering::TypeWidenVector: {
  80. assert(!(InVT.getVectorNumElements() & 1) && "Unsupported BITCAST");
  81. InOp = GetWidenedVector(InOp);
  82. EVT LoVT, HiVT;
  83. std::tie(LoVT, HiVT) = DAG.GetSplitDestVTs(InVT);
  84. std::tie(Lo, Hi) = DAG.SplitVector(InOp, dl, LoVT, HiVT);
  85. if (TLI.hasBigEndianPartOrdering(OutVT, DAG.getDataLayout()))
  86. std::swap(Lo, Hi);
  87. Lo = DAG.getNode(ISD::BITCAST, dl, NOutVT, Lo);
  88. Hi = DAG.getNode(ISD::BITCAST, dl, NOutVT, Hi);
  89. return;
  90. }
  91. }
  92. if (InVT.isVector() && OutVT.isInteger()) {
  93. // Handle cases like i64 = BITCAST v1i64 on x86, where the operand
  94. // is legal but the result is not.
  95. unsigned NumElems = 2;
  96. EVT ElemVT = NOutVT;
  97. EVT NVT = EVT::getVectorVT(*DAG.getContext(), ElemVT, NumElems);
  98. // If <ElemVT * N> is not a legal type, try <ElemVT/2 * (N*2)>.
  99. while (!isTypeLegal(NVT)) {
  100. unsigned NewSizeInBits = ElemVT.getSizeInBits() / 2;
  101. // If the element size is smaller than byte, bail.
  102. if (NewSizeInBits < 8)
  103. break;
  104. NumElems *= 2;
  105. ElemVT = EVT::getIntegerVT(*DAG.getContext(), NewSizeInBits);
  106. NVT = EVT::getVectorVT(*DAG.getContext(), ElemVT, NumElems);
  107. }
  108. if (isTypeLegal(NVT)) {
  109. SDValue CastInOp = DAG.getNode(ISD::BITCAST, dl, NVT, InOp);
  110. SmallVector<SDValue, 8> Vals;
  111. for (unsigned i = 0; i < NumElems; ++i)
  112. Vals.push_back(DAG.getNode(
  113. ISD::EXTRACT_VECTOR_ELT, dl, ElemVT, CastInOp,
  114. DAG.getConstant(i, dl, TLI.getVectorIdxTy(DAG.getDataLayout()))));
  115. // Build Lo, Hi pair by pairing extracted elements if needed.
  116. unsigned Slot = 0;
  117. for (unsigned e = Vals.size(); e - Slot > 2; Slot += 2, e += 1) {
  118. // Each iteration will BUILD_PAIR two nodes and append the result until
  119. // there are only two nodes left, i.e. Lo and Hi.
  120. SDValue LHS = Vals[Slot];
  121. SDValue RHS = Vals[Slot + 1];
  122. if (DAG.getDataLayout().isBigEndian())
  123. std::swap(LHS, RHS);
  124. Vals.push_back(DAG.getNode(
  125. ISD::BUILD_PAIR, dl,
  126. EVT::getIntegerVT(*DAG.getContext(), LHS.getValueSizeInBits() << 1),
  127. LHS, RHS));
  128. }
  129. Lo = Vals[Slot++];
  130. Hi = Vals[Slot++];
  131. if (DAG.getDataLayout().isBigEndian())
  132. std::swap(Lo, Hi);
  133. return;
  134. }
  135. }
  136. // Lower the bit-convert to a store/load from the stack.
  137. assert(NOutVT.isByteSized() && "Expanded type not byte sized!");
  138. // Create the stack frame object. Make sure it is aligned for both
  139. // the source and expanded destination types.
  140. unsigned Alignment = DAG.getDataLayout().getPrefTypeAlignment(
  141. NOutVT.getTypeForEVT(*DAG.getContext()));
  142. SDValue StackPtr = DAG.CreateStackTemporary(InVT, Alignment);
  143. int SPFI = cast<FrameIndexSDNode>(StackPtr.getNode())->getIndex();
  144. MachinePointerInfo PtrInfo =
  145. MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), SPFI);
  146. // Emit a store to the stack slot.
  147. SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, InOp, StackPtr, PtrInfo);
  148. // Load the first half from the stack slot.
  149. Lo = DAG.getLoad(NOutVT, dl, Store, StackPtr, PtrInfo);
  150. // Increment the pointer to the other half.
  151. unsigned IncrementSize = NOutVT.getSizeInBits() / 8;
  152. StackPtr = DAG.getNode(ISD::ADD, dl, StackPtr.getValueType(), StackPtr,
  153. DAG.getConstant(IncrementSize, dl,
  154. StackPtr.getValueType()));
  155. // Load the second half from the stack slot.
  156. Hi = DAG.getLoad(NOutVT, dl, Store, StackPtr,
  157. PtrInfo.getWithOffset(IncrementSize),
  158. MinAlign(Alignment, IncrementSize));
  159. // Handle endianness of the load.
  160. if (TLI.hasBigEndianPartOrdering(OutVT, DAG.getDataLayout()))
  161. std::swap(Lo, Hi);
  162. }
  163. void DAGTypeLegalizer::ExpandRes_BUILD_PAIR(SDNode *N, SDValue &Lo,
  164. SDValue &Hi) {
  165. // Return the operands.
  166. Lo = N->getOperand(0);
  167. Hi = N->getOperand(1);
  168. }
  169. void DAGTypeLegalizer::ExpandRes_EXTRACT_ELEMENT(SDNode *N, SDValue &Lo,
  170. SDValue &Hi) {
  171. GetExpandedOp(N->getOperand(0), Lo, Hi);
  172. SDValue Part = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() ?
  173. Hi : Lo;
  174. assert(Part.getValueType() == N->getValueType(0) &&
  175. "Type twice as big as expanded type not itself expanded!");
  176. GetPairElements(Part, Lo, Hi);
  177. }
  178. void DAGTypeLegalizer::ExpandRes_EXTRACT_VECTOR_ELT(SDNode *N, SDValue &Lo,
  179. SDValue &Hi) {
  180. SDValue OldVec = N->getOperand(0);
  181. unsigned OldElts = OldVec.getValueType().getVectorNumElements();
  182. EVT OldEltVT = OldVec.getValueType().getVectorElementType();
  183. SDLoc dl(N);
  184. // Convert to a vector of the expanded element type, for example
  185. // <3 x i64> -> <6 x i32>.
  186. EVT OldVT = N->getValueType(0);
  187. EVT NewVT = TLI.getTypeToTransformTo(*DAG.getContext(), OldVT);
  188. if (OldVT != OldEltVT) {
  189. // The result of EXTRACT_VECTOR_ELT may be larger than the element type of
  190. // the input vector. If so, extend the elements of the input vector to the
  191. // same bitwidth as the result before expanding.
  192. assert(OldEltVT.bitsLT(OldVT) && "Result type smaller then element type!");
  193. EVT NVecVT = EVT::getVectorVT(*DAG.getContext(), OldVT, OldElts);
  194. OldVec = DAG.getNode(ISD::ANY_EXTEND, dl, NVecVT, N->getOperand(0));
  195. }
  196. SDValue NewVec = DAG.getNode(ISD::BITCAST, dl,
  197. EVT::getVectorVT(*DAG.getContext(),
  198. NewVT, 2*OldElts),
  199. OldVec);
  200. // Extract the elements at 2 * Idx and 2 * Idx + 1 from the new vector.
  201. SDValue Idx = N->getOperand(1);
  202. Idx = DAG.getNode(ISD::ADD, dl, Idx.getValueType(), Idx, Idx);
  203. Lo = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, NewVT, NewVec, Idx);
  204. Idx = DAG.getNode(ISD::ADD, dl, Idx.getValueType(), Idx,
  205. DAG.getConstant(1, dl, Idx.getValueType()));
  206. Hi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, NewVT, NewVec, Idx);
  207. if (DAG.getDataLayout().isBigEndian())
  208. std::swap(Lo, Hi);
  209. }
  210. void DAGTypeLegalizer::ExpandRes_NormalLoad(SDNode *N, SDValue &Lo,
  211. SDValue &Hi) {
  212. assert(ISD::isNormalLoad(N) && "This routine only for normal loads!");
  213. SDLoc dl(N);
  214. LoadSDNode *LD = cast<LoadSDNode>(N);
  215. EVT ValueVT = LD->getValueType(0);
  216. EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), ValueVT);
  217. SDValue Chain = LD->getChain();
  218. SDValue Ptr = LD->getBasePtr();
  219. unsigned Alignment = LD->getAlignment();
  220. AAMDNodes AAInfo = LD->getAAInfo();
  221. assert(NVT.isByteSized() && "Expanded type not byte sized!");
  222. Lo = DAG.getLoad(NVT, dl, Chain, Ptr, LD->getPointerInfo(), Alignment,
  223. LD->getMemOperand()->getFlags(), AAInfo);
  224. // Increment the pointer to the other half.
  225. unsigned IncrementSize = NVT.getSizeInBits() / 8;
  226. Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
  227. DAG.getConstant(IncrementSize, dl, Ptr.getValueType()));
  228. Hi = DAG.getLoad(NVT, dl, Chain, Ptr,
  229. LD->getPointerInfo().getWithOffset(IncrementSize),
  230. MinAlign(Alignment, IncrementSize),
  231. LD->getMemOperand()->getFlags(), AAInfo);
  232. // Build a factor node to remember that this load is independent of the
  233. // other one.
  234. Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
  235. Hi.getValue(1));
  236. // Handle endianness of the load.
  237. if (TLI.hasBigEndianPartOrdering(ValueVT, DAG.getDataLayout()))
  238. std::swap(Lo, Hi);
  239. // Modified the chain - switch anything that used the old chain to use
  240. // the new one.
  241. ReplaceValueWith(SDValue(N, 1), Chain);
  242. }
  243. void DAGTypeLegalizer::ExpandRes_VAARG(SDNode *N, SDValue &Lo, SDValue &Hi) {
  244. EVT OVT = N->getValueType(0);
  245. EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), OVT);
  246. SDValue Chain = N->getOperand(0);
  247. SDValue Ptr = N->getOperand(1);
  248. SDLoc dl(N);
  249. const unsigned Align = N->getConstantOperandVal(3);
  250. Lo = DAG.getVAArg(NVT, dl, Chain, Ptr, N->getOperand(2), Align);
  251. Hi = DAG.getVAArg(NVT, dl, Lo.getValue(1), Ptr, N->getOperand(2), 0);
  252. Chain = Hi.getValue(1);
  253. // Handle endianness of the load.
  254. if (TLI.hasBigEndianPartOrdering(OVT, DAG.getDataLayout()))
  255. std::swap(Lo, Hi);
  256. // Modified the chain - switch anything that used the old chain to use
  257. // the new one.
  258. ReplaceValueWith(SDValue(N, 1), Chain);
  259. }
  260. //===--------------------------------------------------------------------===//
  261. // Generic Operand Expansion.
  262. //===--------------------------------------------------------------------===//
  263. void DAGTypeLegalizer::IntegerToVector(SDValue Op, unsigned NumElements,
  264. SmallVectorImpl<SDValue> &Ops,
  265. EVT EltVT) {
  266. assert(Op.getValueType().isInteger());
  267. SDLoc DL(Op);
  268. SDValue Parts[2];
  269. if (NumElements > 1) {
  270. NumElements >>= 1;
  271. SplitInteger(Op, Parts[0], Parts[1]);
  272. if (DAG.getDataLayout().isBigEndian())
  273. std::swap(Parts[0], Parts[1]);
  274. IntegerToVector(Parts[0], NumElements, Ops, EltVT);
  275. IntegerToVector(Parts[1], NumElements, Ops, EltVT);
  276. } else {
  277. Ops.push_back(DAG.getNode(ISD::BITCAST, DL, EltVT, Op));
  278. }
  279. }
  280. SDValue DAGTypeLegalizer::ExpandOp_BITCAST(SDNode *N) {
  281. SDLoc dl(N);
  282. if (N->getValueType(0).isVector() &&
  283. N->getOperand(0).getValueType().isInteger()) {
  284. // An illegal expanding type is being converted to a legal vector type.
  285. // Make a two element vector out of the expanded parts and convert that
  286. // instead, but only if the new vector type is legal (otherwise there
  287. // is no point, and it might create expansion loops). For example, on
  288. // x86 this turns v1i64 = BITCAST i64 into v1i64 = BITCAST v2i32.
  289. //
  290. // FIXME: I'm not sure why we are first trying to split the input into
  291. // a 2 element vector, so I'm leaving it here to maintain the current
  292. // behavior.
  293. unsigned NumElts = 2;
  294. EVT OVT = N->getOperand(0).getValueType();
  295. EVT NVT = EVT::getVectorVT(*DAG.getContext(),
  296. TLI.getTypeToTransformTo(*DAG.getContext(), OVT),
  297. NumElts);
  298. if (!isTypeLegal(NVT)) {
  299. // If we can't find a legal type by splitting the integer in half,
  300. // then we can use the node's value type.
  301. NumElts = N->getValueType(0).getVectorNumElements();
  302. NVT = N->getValueType(0);
  303. }
  304. SmallVector<SDValue, 8> Ops;
  305. IntegerToVector(N->getOperand(0), NumElts, Ops, NVT.getVectorElementType());
  306. SDValue Vec =
  307. DAG.getBuildVector(NVT, dl, makeArrayRef(Ops.data(), NumElts));
  308. return DAG.getNode(ISD::BITCAST, dl, N->getValueType(0), Vec);
  309. }
  310. // Otherwise, store to a temporary and load out again as the new type.
  311. return CreateStackStoreLoad(N->getOperand(0), N->getValueType(0));
  312. }
  313. SDValue DAGTypeLegalizer::ExpandOp_BUILD_VECTOR(SDNode *N) {
  314. // The vector type is legal but the element type needs expansion.
  315. EVT VecVT = N->getValueType(0);
  316. unsigned NumElts = VecVT.getVectorNumElements();
  317. EVT OldVT = N->getOperand(0).getValueType();
  318. EVT NewVT = TLI.getTypeToTransformTo(*DAG.getContext(), OldVT);
  319. SDLoc dl(N);
  320. assert(OldVT == VecVT.getVectorElementType() &&
  321. "BUILD_VECTOR operand type doesn't match vector element type!");
  322. // Build a vector of twice the length out of the expanded elements.
  323. // For example <3 x i64> -> <6 x i32>.
  324. SmallVector<SDValue, 16> NewElts;
  325. NewElts.reserve(NumElts*2);
  326. for (unsigned i = 0; i < NumElts; ++i) {
  327. SDValue Lo, Hi;
  328. GetExpandedOp(N->getOperand(i), Lo, Hi);
  329. if (DAG.getDataLayout().isBigEndian())
  330. std::swap(Lo, Hi);
  331. NewElts.push_back(Lo);
  332. NewElts.push_back(Hi);
  333. }
  334. EVT NewVecVT = EVT::getVectorVT(*DAG.getContext(), NewVT, NewElts.size());
  335. SDValue NewVec = DAG.getBuildVector(NewVecVT, dl, NewElts);
  336. // Convert the new vector to the old vector type.
  337. return DAG.getNode(ISD::BITCAST, dl, VecVT, NewVec);
  338. }
  339. SDValue DAGTypeLegalizer::ExpandOp_EXTRACT_ELEMENT(SDNode *N) {
  340. SDValue Lo, Hi;
  341. GetExpandedOp(N->getOperand(0), Lo, Hi);
  342. return cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() ? Hi : Lo;
  343. }
  344. SDValue DAGTypeLegalizer::ExpandOp_INSERT_VECTOR_ELT(SDNode *N) {
  345. // The vector type is legal but the element type needs expansion.
  346. EVT VecVT = N->getValueType(0);
  347. unsigned NumElts = VecVT.getVectorNumElements();
  348. SDLoc dl(N);
  349. SDValue Val = N->getOperand(1);
  350. EVT OldEVT = Val.getValueType();
  351. EVT NewEVT = TLI.getTypeToTransformTo(*DAG.getContext(), OldEVT);
  352. assert(OldEVT == VecVT.getVectorElementType() &&
  353. "Inserted element type doesn't match vector element type!");
  354. // Bitconvert to a vector of twice the length with elements of the expanded
  355. // type, insert the expanded vector elements, and then convert back.
  356. EVT NewVecVT = EVT::getVectorVT(*DAG.getContext(), NewEVT, NumElts*2);
  357. SDValue NewVec = DAG.getNode(ISD::BITCAST, dl,
  358. NewVecVT, N->getOperand(0));
  359. SDValue Lo, Hi;
  360. GetExpandedOp(Val, Lo, Hi);
  361. if (DAG.getDataLayout().isBigEndian())
  362. std::swap(Lo, Hi);
  363. SDValue Idx = N->getOperand(2);
  364. Idx = DAG.getNode(ISD::ADD, dl, Idx.getValueType(), Idx, Idx);
  365. NewVec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, NewVecVT, NewVec, Lo, Idx);
  366. Idx = DAG.getNode(ISD::ADD, dl,
  367. Idx.getValueType(), Idx,
  368. DAG.getConstant(1, dl, Idx.getValueType()));
  369. NewVec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, NewVecVT, NewVec, Hi, Idx);
  370. // Convert the new vector to the old vector type.
  371. return DAG.getNode(ISD::BITCAST, dl, VecVT, NewVec);
  372. }
  373. SDValue DAGTypeLegalizer::ExpandOp_SCALAR_TO_VECTOR(SDNode *N) {
  374. SDLoc dl(N);
  375. EVT VT = N->getValueType(0);
  376. assert(VT.getVectorElementType() == N->getOperand(0).getValueType() &&
  377. "SCALAR_TO_VECTOR operand type doesn't match vector element type!");
  378. unsigned NumElts = VT.getVectorNumElements();
  379. SmallVector<SDValue, 16> Ops(NumElts);
  380. Ops[0] = N->getOperand(0);
  381. SDValue UndefVal = DAG.getUNDEF(Ops[0].getValueType());
  382. for (unsigned i = 1; i < NumElts; ++i)
  383. Ops[i] = UndefVal;
  384. return DAG.getBuildVector(VT, dl, Ops);
  385. }
  386. SDValue DAGTypeLegalizer::ExpandOp_NormalStore(SDNode *N, unsigned OpNo) {
  387. assert(ISD::isNormalStore(N) && "This routine only for normal stores!");
  388. assert(OpNo == 1 && "Can only expand the stored value so far");
  389. SDLoc dl(N);
  390. StoreSDNode *St = cast<StoreSDNode>(N);
  391. EVT ValueVT = St->getValue().getValueType();
  392. EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), ValueVT);
  393. SDValue Chain = St->getChain();
  394. SDValue Ptr = St->getBasePtr();
  395. unsigned Alignment = St->getAlignment();
  396. AAMDNodes AAInfo = St->getAAInfo();
  397. assert(NVT.isByteSized() && "Expanded type not byte sized!");
  398. unsigned IncrementSize = NVT.getSizeInBits() / 8;
  399. SDValue Lo, Hi;
  400. GetExpandedOp(St->getValue(), Lo, Hi);
  401. if (TLI.hasBigEndianPartOrdering(ValueVT, DAG.getDataLayout()))
  402. std::swap(Lo, Hi);
  403. Lo = DAG.getStore(Chain, dl, Lo, Ptr, St->getPointerInfo(), Alignment,
  404. St->getMemOperand()->getFlags(), AAInfo);
  405. Ptr = DAG.getObjectPtrOffset(dl, Ptr, IncrementSize);
  406. Hi = DAG.getStore(Chain, dl, Hi, Ptr,
  407. St->getPointerInfo().getWithOffset(IncrementSize),
  408. MinAlign(Alignment, IncrementSize),
  409. St->getMemOperand()->getFlags(), AAInfo);
  410. return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo, Hi);
  411. }
  412. //===--------------------------------------------------------------------===//
  413. // Generic Result Splitting.
  414. //===--------------------------------------------------------------------===//
  415. // Be careful to make no assumptions about which of Lo/Hi is stored first in
  416. // memory (for vectors it is always Lo first followed by Hi in the following
  417. // bytes; for integers and floats it is Lo first if and only if the machine is
  418. // little-endian).
  419. void DAGTypeLegalizer::SplitRes_MERGE_VALUES(SDNode *N, unsigned ResNo,
  420. SDValue &Lo, SDValue &Hi) {
  421. SDValue Op = DisintegrateMERGE_VALUES(N, ResNo);
  422. GetSplitOp(Op, Lo, Hi);
  423. }
  424. void DAGTypeLegalizer::SplitRes_SELECT(SDNode *N, SDValue &Lo, SDValue &Hi) {
  425. SDValue LL, LH, RL, RH, CL, CH;
  426. SDLoc dl(N);
  427. GetSplitOp(N->getOperand(1), LL, LH);
  428. GetSplitOp(N->getOperand(2), RL, RH);
  429. SDValue Cond = N->getOperand(0);
  430. CL = CH = Cond;
  431. if (Cond.getValueType().isVector()) {
  432. if (SDValue Res = WidenVSELECTAndMask(N))
  433. std::tie(CL, CH) = DAG.SplitVector(Res->getOperand(0), dl);
  434. // It seems to improve code to generate two narrow SETCCs as opposed to
  435. // splitting a wide result vector.
  436. else if (Cond.getOpcode() == ISD::SETCC)
  437. SplitVecRes_SETCC(Cond.getNode(), CL, CH);
  438. // Check if there are already splitted versions of the vector available and
  439. // use those instead of splitting the mask operand again.
  440. else if (getTypeAction(Cond.getValueType()) ==
  441. TargetLowering::TypeSplitVector)
  442. GetSplitVector(Cond, CL, CH);
  443. else
  444. std::tie(CL, CH) = DAG.SplitVector(Cond, dl);
  445. }
  446. Lo = DAG.getNode(N->getOpcode(), dl, LL.getValueType(), CL, LL, RL);
  447. Hi = DAG.getNode(N->getOpcode(), dl, LH.getValueType(), CH, LH, RH);
  448. }
  449. void DAGTypeLegalizer::SplitRes_SELECT_CC(SDNode *N, SDValue &Lo,
  450. SDValue &Hi) {
  451. SDValue LL, LH, RL, RH;
  452. SDLoc dl(N);
  453. GetSplitOp(N->getOperand(2), LL, LH);
  454. GetSplitOp(N->getOperand(3), RL, RH);
  455. Lo = DAG.getNode(ISD::SELECT_CC, dl, LL.getValueType(), N->getOperand(0),
  456. N->getOperand(1), LL, RL, N->getOperand(4));
  457. Hi = DAG.getNode(ISD::SELECT_CC, dl, LH.getValueType(), N->getOperand(0),
  458. N->getOperand(1), LH, RH, N->getOperand(4));
  459. }
  460. void DAGTypeLegalizer::SplitRes_UNDEF(SDNode *N, SDValue &Lo, SDValue &Hi) {
  461. EVT LoVT, HiVT;
  462. std::tie(LoVT, HiVT) = DAG.GetSplitDestVTs(N->getValueType(0));
  463. Lo = DAG.getUNDEF(LoVT);
  464. Hi = DAG.getUNDEF(HiVT);
  465. }