X86SelectionDAGInfo.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. //===-- X86SelectionDAGInfo.cpp - X86 SelectionDAG Info -------------------===//
  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 the X86SelectionDAGInfo class.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "X86SelectionDAGInfo.h"
  13. #include "X86ISelLowering.h"
  14. #include "X86InstrInfo.h"
  15. #include "X86RegisterInfo.h"
  16. #include "X86Subtarget.h"
  17. #include "llvm/CodeGen/MachineFrameInfo.h"
  18. #include "llvm/CodeGen/SelectionDAG.h"
  19. #include "llvm/CodeGen/TargetLowering.h"
  20. #include "llvm/IR/DerivedTypes.h"
  21. using namespace llvm;
  22. #define DEBUG_TYPE "x86-selectiondag-info"
  23. static cl::opt<bool>
  24. UseFSRMForMemcpy("x86-use-fsrm-for-memcpy", cl::Hidden, cl::init(false),
  25. cl::desc("Use fast short rep mov in memcpy lowering"));
  26. bool X86SelectionDAGInfo::isBaseRegConflictPossible(
  27. SelectionDAG &DAG, ArrayRef<MCPhysReg> ClobberSet) const {
  28. // We cannot use TRI->hasBasePointer() until *after* we select all basic
  29. // blocks. Legalization may introduce new stack temporaries with large
  30. // alignment requirements. Fall back to generic code if there are any
  31. // dynamic stack adjustments (hopefully rare) and the base pointer would
  32. // conflict if we had to use it.
  33. MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo();
  34. if (!MFI.hasVarSizedObjects() && !MFI.hasOpaqueSPAdjustment())
  35. return false;
  36. const X86RegisterInfo *TRI = static_cast<const X86RegisterInfo *>(
  37. DAG.getSubtarget().getRegisterInfo());
  38. return llvm::is_contained(ClobberSet, TRI->getBaseRegister());
  39. }
  40. SDValue X86SelectionDAGInfo::EmitTargetCodeForMemset(
  41. SelectionDAG &DAG, const SDLoc &dl, SDValue Chain, SDValue Dst, SDValue Val,
  42. SDValue Size, Align Alignment, bool isVolatile, bool AlwaysInline,
  43. MachinePointerInfo DstPtrInfo) const {
  44. ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
  45. const X86Subtarget &Subtarget =
  46. DAG.getMachineFunction().getSubtarget<X86Subtarget>();
  47. #ifndef NDEBUG
  48. // If the base register might conflict with our physical registers, bail out.
  49. const MCPhysReg ClobberSet[] = {X86::RCX, X86::RAX, X86::RDI,
  50. X86::ECX, X86::EAX, X86::EDI};
  51. assert(!isBaseRegConflictPossible(DAG, ClobberSet));
  52. #endif
  53. // If to a segment-relative address space, use the default lowering.
  54. if (DstPtrInfo.getAddrSpace() >= 256)
  55. return SDValue();
  56. // If not DWORD aligned or size is more than the threshold, call the library.
  57. // The libc version is likely to be faster for these cases. It can use the
  58. // address value and run time information about the CPU.
  59. if (Alignment < Align(4) || !ConstantSize ||
  60. ConstantSize->getZExtValue() > Subtarget.getMaxInlineSizeThreshold())
  61. return SDValue();
  62. uint64_t SizeVal = ConstantSize->getZExtValue();
  63. SDValue InFlag;
  64. EVT AVT;
  65. SDValue Count;
  66. unsigned BytesLeft = 0;
  67. if (auto *ValC = dyn_cast<ConstantSDNode>(Val)) {
  68. unsigned ValReg;
  69. uint64_t Val = ValC->getZExtValue() & 255;
  70. // If the value is a constant, then we can potentially use larger sets.
  71. if (Alignment > Align(2)) {
  72. // DWORD aligned
  73. AVT = MVT::i32;
  74. ValReg = X86::EAX;
  75. Val = (Val << 8) | Val;
  76. Val = (Val << 16) | Val;
  77. if (Subtarget.is64Bit() && Alignment > Align(8)) { // QWORD aligned
  78. AVT = MVT::i64;
  79. ValReg = X86::RAX;
  80. Val = (Val << 32) | Val;
  81. }
  82. } else if (Alignment == Align(2)) {
  83. // WORD aligned
  84. AVT = MVT::i16;
  85. ValReg = X86::AX;
  86. Val = (Val << 8) | Val;
  87. } else {
  88. // Byte aligned
  89. AVT = MVT::i8;
  90. ValReg = X86::AL;
  91. Count = DAG.getIntPtrConstant(SizeVal, dl);
  92. }
  93. if (AVT.bitsGT(MVT::i8)) {
  94. unsigned UBytes = AVT.getSizeInBits() / 8;
  95. Count = DAG.getIntPtrConstant(SizeVal / UBytes, dl);
  96. BytesLeft = SizeVal % UBytes;
  97. }
  98. Chain = DAG.getCopyToReg(Chain, dl, ValReg, DAG.getConstant(Val, dl, AVT),
  99. InFlag);
  100. InFlag = Chain.getValue(1);
  101. } else {
  102. AVT = MVT::i8;
  103. Count = DAG.getIntPtrConstant(SizeVal, dl);
  104. Chain = DAG.getCopyToReg(Chain, dl, X86::AL, Val, InFlag);
  105. InFlag = Chain.getValue(1);
  106. }
  107. bool Use64BitRegs = Subtarget.isTarget64BitLP64();
  108. Chain = DAG.getCopyToReg(Chain, dl, Use64BitRegs ? X86::RCX : X86::ECX,
  109. Count, InFlag);
  110. InFlag = Chain.getValue(1);
  111. Chain = DAG.getCopyToReg(Chain, dl, Use64BitRegs ? X86::RDI : X86::EDI,
  112. Dst, InFlag);
  113. InFlag = Chain.getValue(1);
  114. SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
  115. SDValue Ops[] = { Chain, DAG.getValueType(AVT), InFlag };
  116. Chain = DAG.getNode(X86ISD::REP_STOS, dl, Tys, Ops);
  117. if (BytesLeft) {
  118. // Handle the last 1 - 7 bytes.
  119. unsigned Offset = SizeVal - BytesLeft;
  120. EVT AddrVT = Dst.getValueType();
  121. EVT SizeVT = Size.getValueType();
  122. Chain =
  123. DAG.getMemset(Chain, dl,
  124. DAG.getNode(ISD::ADD, dl, AddrVT, Dst,
  125. DAG.getConstant(Offset, dl, AddrVT)),
  126. Val, DAG.getConstant(BytesLeft, dl, SizeVT), Alignment,
  127. isVolatile, AlwaysInline,
  128. /* isTailCall */ false, DstPtrInfo.getWithOffset(Offset));
  129. }
  130. // TODO: Use a Tokenfactor, as in memcpy, instead of a single chain.
  131. return Chain;
  132. }
  133. /// Emit a single REP MOVS{B,W,D,Q} instruction.
  134. static SDValue emitRepmovs(const X86Subtarget &Subtarget, SelectionDAG &DAG,
  135. const SDLoc &dl, SDValue Chain, SDValue Dst,
  136. SDValue Src, SDValue Size, MVT AVT) {
  137. const bool Use64BitRegs = Subtarget.isTarget64BitLP64();
  138. const unsigned CX = Use64BitRegs ? X86::RCX : X86::ECX;
  139. const unsigned DI = Use64BitRegs ? X86::RDI : X86::EDI;
  140. const unsigned SI = Use64BitRegs ? X86::RSI : X86::ESI;
  141. SDValue InFlag;
  142. Chain = DAG.getCopyToReg(Chain, dl, CX, Size, InFlag);
  143. InFlag = Chain.getValue(1);
  144. Chain = DAG.getCopyToReg(Chain, dl, DI, Dst, InFlag);
  145. InFlag = Chain.getValue(1);
  146. Chain = DAG.getCopyToReg(Chain, dl, SI, Src, InFlag);
  147. InFlag = Chain.getValue(1);
  148. SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
  149. SDValue Ops[] = {Chain, DAG.getValueType(AVT), InFlag};
  150. return DAG.getNode(X86ISD::REP_MOVS, dl, Tys, Ops);
  151. }
  152. /// Emit a single REP MOVSB instruction for a particular constant size.
  153. static SDValue emitRepmovsB(const X86Subtarget &Subtarget, SelectionDAG &DAG,
  154. const SDLoc &dl, SDValue Chain, SDValue Dst,
  155. SDValue Src, uint64_t Size) {
  156. return emitRepmovs(Subtarget, DAG, dl, Chain, Dst, Src,
  157. DAG.getIntPtrConstant(Size, dl), MVT::i8);
  158. }
  159. /// Returns the best type to use with repmovs depending on alignment.
  160. static MVT getOptimalRepmovsType(const X86Subtarget &Subtarget,
  161. uint64_t Align) {
  162. assert((Align != 0) && "Align is normalized");
  163. assert(isPowerOf2_64(Align) && "Align is a power of 2");
  164. switch (Align) {
  165. case 1:
  166. return MVT::i8;
  167. case 2:
  168. return MVT::i16;
  169. case 4:
  170. return MVT::i32;
  171. default:
  172. return Subtarget.is64Bit() ? MVT::i64 : MVT::i32;
  173. }
  174. }
  175. /// Returns a REP MOVS instruction, possibly with a few load/stores to implement
  176. /// a constant size memory copy. In some cases where we know REP MOVS is
  177. /// inefficient we return an empty SDValue so the calling code can either
  178. /// generate a load/store sequence or call the runtime memcpy function.
  179. static SDValue emitConstantSizeRepmov(
  180. SelectionDAG &DAG, const X86Subtarget &Subtarget, const SDLoc &dl,
  181. SDValue Chain, SDValue Dst, SDValue Src, uint64_t Size, EVT SizeVT,
  182. unsigned Align, bool isVolatile, bool AlwaysInline,
  183. MachinePointerInfo DstPtrInfo, MachinePointerInfo SrcPtrInfo) {
  184. /// TODO: Revisit next line: big copy with ERMSB on march >= haswell are very
  185. /// efficient.
  186. if (!AlwaysInline && Size > Subtarget.getMaxInlineSizeThreshold())
  187. return SDValue();
  188. /// If we have enhanced repmovs we use it.
  189. if (Subtarget.hasERMSB())
  190. return emitRepmovsB(Subtarget, DAG, dl, Chain, Dst, Src, Size);
  191. assert(!Subtarget.hasERMSB() && "No efficient RepMovs");
  192. /// We assume runtime memcpy will do a better job for unaligned copies when
  193. /// ERMS is not present.
  194. if (!AlwaysInline && (Align & 3) != 0)
  195. return SDValue();
  196. const MVT BlockType = getOptimalRepmovsType(Subtarget, Align);
  197. const uint64_t BlockBytes = BlockType.getSizeInBits() / 8;
  198. const uint64_t BlockCount = Size / BlockBytes;
  199. const uint64_t BytesLeft = Size % BlockBytes;
  200. SDValue RepMovs =
  201. emitRepmovs(Subtarget, DAG, dl, Chain, Dst, Src,
  202. DAG.getIntPtrConstant(BlockCount, dl), BlockType);
  203. /// RepMov can process the whole length.
  204. if (BytesLeft == 0)
  205. return RepMovs;
  206. assert(BytesLeft && "We have leftover at this point");
  207. /// In case we optimize for size we use repmovsb even if it's less efficient
  208. /// so we can save the loads/stores of the leftover.
  209. if (DAG.getMachineFunction().getFunction().hasMinSize())
  210. return emitRepmovsB(Subtarget, DAG, dl, Chain, Dst, Src, Size);
  211. // Handle the last 1 - 7 bytes.
  212. SmallVector<SDValue, 4> Results;
  213. Results.push_back(RepMovs);
  214. unsigned Offset = Size - BytesLeft;
  215. EVT DstVT = Dst.getValueType();
  216. EVT SrcVT = Src.getValueType();
  217. Results.push_back(DAG.getMemcpy(
  218. Chain, dl,
  219. DAG.getNode(ISD::ADD, dl, DstVT, Dst, DAG.getConstant(Offset, dl, DstVT)),
  220. DAG.getNode(ISD::ADD, dl, SrcVT, Src, DAG.getConstant(Offset, dl, SrcVT)),
  221. DAG.getConstant(BytesLeft, dl, SizeVT), llvm::Align(Align), isVolatile,
  222. /*AlwaysInline*/ true, /*isTailCall*/ false,
  223. DstPtrInfo.getWithOffset(Offset), SrcPtrInfo.getWithOffset(Offset)));
  224. return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Results);
  225. }
  226. SDValue X86SelectionDAGInfo::EmitTargetCodeForMemcpy(
  227. SelectionDAG &DAG, const SDLoc &dl, SDValue Chain, SDValue Dst, SDValue Src,
  228. SDValue Size, Align Alignment, bool isVolatile, bool AlwaysInline,
  229. MachinePointerInfo DstPtrInfo, MachinePointerInfo SrcPtrInfo) const {
  230. // If to a segment-relative address space, use the default lowering.
  231. if (DstPtrInfo.getAddrSpace() >= 256 || SrcPtrInfo.getAddrSpace() >= 256)
  232. return SDValue();
  233. // If the base registers conflict with our physical registers, use the default
  234. // lowering.
  235. const MCPhysReg ClobberSet[] = {X86::RCX, X86::RSI, X86::RDI,
  236. X86::ECX, X86::ESI, X86::EDI};
  237. if (isBaseRegConflictPossible(DAG, ClobberSet))
  238. return SDValue();
  239. const X86Subtarget &Subtarget =
  240. DAG.getMachineFunction().getSubtarget<X86Subtarget>();
  241. // If enabled and available, use fast short rep mov.
  242. if (UseFSRMForMemcpy && Subtarget.hasFSRM())
  243. return emitRepmovs(Subtarget, DAG, dl, Chain, Dst, Src, Size, MVT::i8);
  244. /// Handle constant sizes,
  245. if (ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size))
  246. return emitConstantSizeRepmov(
  247. DAG, Subtarget, dl, Chain, Dst, Src, ConstantSize->getZExtValue(),
  248. Size.getValueType(), Alignment.value(), isVolatile, AlwaysInline,
  249. DstPtrInfo, SrcPtrInfo);
  250. return SDValue();
  251. }