X86CallLowering.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. //===- llvm/lib/Target/X86/X86CallLowering.cpp - Call lowering ------------===//
  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. /// \file
  10. /// This file implements the lowering of LLVM calls to machine code calls for
  11. /// GlobalISel.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #include "X86CallLowering.h"
  15. #include "X86CallingConv.h"
  16. #include "X86ISelLowering.h"
  17. #include "X86InstrInfo.h"
  18. #include "X86RegisterInfo.h"
  19. #include "X86Subtarget.h"
  20. #include "llvm/ADT/ArrayRef.h"
  21. #include "llvm/ADT/SmallVector.h"
  22. #include "llvm/CodeGen/Analysis.h"
  23. #include "llvm/CodeGen/CallingConvLower.h"
  24. #include "llvm/CodeGen/GlobalISel/MachineIRBuilder.h"
  25. #include "llvm/CodeGen/GlobalISel/Utils.h"
  26. #include "llvm/CodeGen/LowLevelType.h"
  27. #include "llvm/CodeGen/MachineBasicBlock.h"
  28. #include "llvm/CodeGen/MachineFrameInfo.h"
  29. #include "llvm/CodeGen/MachineFunction.h"
  30. #include "llvm/CodeGen/MachineInstrBuilder.h"
  31. #include "llvm/CodeGen/MachineMemOperand.h"
  32. #include "llvm/CodeGen/MachineOperand.h"
  33. #include "llvm/CodeGen/MachineRegisterInfo.h"
  34. #include "llvm/CodeGen/TargetInstrInfo.h"
  35. #include "llvm/CodeGen/TargetSubtargetInfo.h"
  36. #include "llvm/CodeGen/ValueTypes.h"
  37. #include "llvm/IR/Attributes.h"
  38. #include "llvm/IR/DataLayout.h"
  39. #include "llvm/IR/Function.h"
  40. #include "llvm/IR/Value.h"
  41. #include "llvm/MC/MCRegisterInfo.h"
  42. #include "llvm/Support/LowLevelTypeImpl.h"
  43. #include "llvm/Support/MachineValueType.h"
  44. #include <cassert>
  45. #include <cstdint>
  46. using namespace llvm;
  47. X86CallLowering::X86CallLowering(const X86TargetLowering &TLI)
  48. : CallLowering(&TLI) {}
  49. namespace {
  50. struct X86OutgoingValueAssigner : public CallLowering::OutgoingValueAssigner {
  51. private:
  52. uint64_t StackSize = 0;
  53. unsigned NumXMMRegs = 0;
  54. public:
  55. uint64_t getStackSize() { return StackSize; }
  56. unsigned getNumXmmRegs() { return NumXMMRegs; }
  57. X86OutgoingValueAssigner(CCAssignFn *AssignFn_)
  58. : CallLowering::OutgoingValueAssigner(AssignFn_) {}
  59. bool assignArg(unsigned ValNo, EVT OrigVT, MVT ValVT, MVT LocVT,
  60. CCValAssign::LocInfo LocInfo,
  61. const CallLowering::ArgInfo &Info, ISD::ArgFlagsTy Flags,
  62. CCState &State) override {
  63. bool Res = AssignFn(ValNo, ValVT, LocVT, LocInfo, Flags, State);
  64. StackSize = State.getNextStackOffset();
  65. static const MCPhysReg XMMArgRegs[] = {X86::XMM0, X86::XMM1, X86::XMM2,
  66. X86::XMM3, X86::XMM4, X86::XMM5,
  67. X86::XMM6, X86::XMM7};
  68. if (!Info.IsFixed)
  69. NumXMMRegs = State.getFirstUnallocated(XMMArgRegs);
  70. return Res;
  71. }
  72. };
  73. struct X86OutgoingValueHandler : public CallLowering::OutgoingValueHandler {
  74. X86OutgoingValueHandler(MachineIRBuilder &MIRBuilder,
  75. MachineRegisterInfo &MRI, MachineInstrBuilder &MIB)
  76. : OutgoingValueHandler(MIRBuilder, MRI), MIB(MIB),
  77. DL(MIRBuilder.getMF().getDataLayout()),
  78. STI(MIRBuilder.getMF().getSubtarget<X86Subtarget>()) {}
  79. Register getStackAddress(uint64_t Size, int64_t Offset,
  80. MachinePointerInfo &MPO,
  81. ISD::ArgFlagsTy Flags) override {
  82. LLT p0 = LLT::pointer(0, DL.getPointerSizeInBits(0));
  83. LLT SType = LLT::scalar(DL.getPointerSizeInBits(0));
  84. auto SPReg =
  85. MIRBuilder.buildCopy(p0, STI.getRegisterInfo()->getStackRegister());
  86. auto OffsetReg = MIRBuilder.buildConstant(SType, Offset);
  87. auto AddrReg = MIRBuilder.buildPtrAdd(p0, SPReg, OffsetReg);
  88. MPO = MachinePointerInfo::getStack(MIRBuilder.getMF(), Offset);
  89. return AddrReg.getReg(0);
  90. }
  91. void assignValueToReg(Register ValVReg, Register PhysReg,
  92. CCValAssign VA) override {
  93. MIB.addUse(PhysReg, RegState::Implicit);
  94. Register ExtReg = extendRegister(ValVReg, VA);
  95. MIRBuilder.buildCopy(PhysReg, ExtReg);
  96. }
  97. void assignValueToAddress(Register ValVReg, Register Addr, LLT MemTy,
  98. MachinePointerInfo &MPO, CCValAssign &VA) override {
  99. MachineFunction &MF = MIRBuilder.getMF();
  100. Register ExtReg = extendRegister(ValVReg, VA);
  101. auto *MMO = MF.getMachineMemOperand(MPO, MachineMemOperand::MOStore, MemTy,
  102. inferAlignFromPtrInfo(MF, MPO));
  103. MIRBuilder.buildStore(ExtReg, Addr, *MMO);
  104. }
  105. protected:
  106. MachineInstrBuilder &MIB;
  107. const DataLayout &DL;
  108. const X86Subtarget &STI;
  109. };
  110. } // end anonymous namespace
  111. bool X86CallLowering::lowerReturn(MachineIRBuilder &MIRBuilder,
  112. const Value *Val, ArrayRef<Register> VRegs,
  113. FunctionLoweringInfo &FLI) const {
  114. assert(((Val && !VRegs.empty()) || (!Val && VRegs.empty())) &&
  115. "Return value without a vreg");
  116. auto MIB = MIRBuilder.buildInstrNoInsert(X86::RET).addImm(0);
  117. if (!VRegs.empty()) {
  118. MachineFunction &MF = MIRBuilder.getMF();
  119. const Function &F = MF.getFunction();
  120. MachineRegisterInfo &MRI = MF.getRegInfo();
  121. const DataLayout &DL = MF.getDataLayout();
  122. ArgInfo OrigRetInfo(VRegs, Val->getType(), 0);
  123. setArgFlags(OrigRetInfo, AttributeList::ReturnIndex, DL, F);
  124. SmallVector<ArgInfo, 4> SplitRetInfos;
  125. splitToValueTypes(OrigRetInfo, SplitRetInfos, DL, F.getCallingConv());
  126. X86OutgoingValueAssigner Assigner(RetCC_X86);
  127. X86OutgoingValueHandler Handler(MIRBuilder, MRI, MIB);
  128. if (!determineAndHandleAssignments(Handler, Assigner, SplitRetInfos,
  129. MIRBuilder, F.getCallingConv(),
  130. F.isVarArg()))
  131. return false;
  132. }
  133. MIRBuilder.insertInstr(MIB);
  134. return true;
  135. }
  136. namespace {
  137. struct X86IncomingValueHandler : public CallLowering::IncomingValueHandler {
  138. X86IncomingValueHandler(MachineIRBuilder &MIRBuilder,
  139. MachineRegisterInfo &MRI)
  140. : IncomingValueHandler(MIRBuilder, MRI),
  141. DL(MIRBuilder.getMF().getDataLayout()) {}
  142. Register getStackAddress(uint64_t Size, int64_t Offset,
  143. MachinePointerInfo &MPO,
  144. ISD::ArgFlagsTy Flags) override {
  145. auto &MFI = MIRBuilder.getMF().getFrameInfo();
  146. // Byval is assumed to be writable memory, but other stack passed arguments
  147. // are not.
  148. const bool IsImmutable = !Flags.isByVal();
  149. int FI = MFI.CreateFixedObject(Size, Offset, IsImmutable);
  150. MPO = MachinePointerInfo::getFixedStack(MIRBuilder.getMF(), FI);
  151. return MIRBuilder
  152. .buildFrameIndex(LLT::pointer(0, DL.getPointerSizeInBits(0)), FI)
  153. .getReg(0);
  154. }
  155. void assignValueToAddress(Register ValVReg, Register Addr, LLT MemTy,
  156. MachinePointerInfo &MPO, CCValAssign &VA) override {
  157. MachineFunction &MF = MIRBuilder.getMF();
  158. auto *MMO = MF.getMachineMemOperand(
  159. MPO, MachineMemOperand::MOLoad | MachineMemOperand::MOInvariant, MemTy,
  160. inferAlignFromPtrInfo(MF, MPO));
  161. MIRBuilder.buildLoad(ValVReg, Addr, *MMO);
  162. }
  163. void assignValueToReg(Register ValVReg, Register PhysReg,
  164. CCValAssign VA) override {
  165. markPhysRegUsed(PhysReg);
  166. IncomingValueHandler::assignValueToReg(ValVReg, PhysReg, VA);
  167. }
  168. /// How the physical register gets marked varies between formal
  169. /// parameters (it's a basic-block live-in), and a call instruction
  170. /// (it's an implicit-def of the BL).
  171. virtual void markPhysRegUsed(unsigned PhysReg) = 0;
  172. protected:
  173. const DataLayout &DL;
  174. };
  175. struct FormalArgHandler : public X86IncomingValueHandler {
  176. FormalArgHandler(MachineIRBuilder &MIRBuilder, MachineRegisterInfo &MRI)
  177. : X86IncomingValueHandler(MIRBuilder, MRI) {}
  178. void markPhysRegUsed(unsigned PhysReg) override {
  179. MIRBuilder.getMRI()->addLiveIn(PhysReg);
  180. MIRBuilder.getMBB().addLiveIn(PhysReg);
  181. }
  182. };
  183. struct CallReturnHandler : public X86IncomingValueHandler {
  184. CallReturnHandler(MachineIRBuilder &MIRBuilder, MachineRegisterInfo &MRI,
  185. MachineInstrBuilder &MIB)
  186. : X86IncomingValueHandler(MIRBuilder, MRI), MIB(MIB) {}
  187. void markPhysRegUsed(unsigned PhysReg) override {
  188. MIB.addDef(PhysReg, RegState::Implicit);
  189. }
  190. protected:
  191. MachineInstrBuilder &MIB;
  192. };
  193. } // end anonymous namespace
  194. bool X86CallLowering::lowerFormalArguments(MachineIRBuilder &MIRBuilder,
  195. const Function &F,
  196. ArrayRef<ArrayRef<Register>> VRegs,
  197. FunctionLoweringInfo &FLI) const {
  198. if (F.arg_empty())
  199. return true;
  200. // TODO: handle variadic function
  201. if (F.isVarArg())
  202. return false;
  203. MachineFunction &MF = MIRBuilder.getMF();
  204. MachineRegisterInfo &MRI = MF.getRegInfo();
  205. auto DL = MF.getDataLayout();
  206. SmallVector<ArgInfo, 8> SplitArgs;
  207. unsigned Idx = 0;
  208. for (const auto &Arg : F.args()) {
  209. // TODO: handle not simple cases.
  210. if (Arg.hasAttribute(Attribute::ByVal) ||
  211. Arg.hasAttribute(Attribute::InReg) ||
  212. Arg.hasAttribute(Attribute::StructRet) ||
  213. Arg.hasAttribute(Attribute::SwiftSelf) ||
  214. Arg.hasAttribute(Attribute::SwiftError) ||
  215. Arg.hasAttribute(Attribute::Nest) || VRegs[Idx].size() > 1)
  216. return false;
  217. ArgInfo OrigArg(VRegs[Idx], Arg.getType(), Idx);
  218. setArgFlags(OrigArg, Idx + AttributeList::FirstArgIndex, DL, F);
  219. splitToValueTypes(OrigArg, SplitArgs, DL, F.getCallingConv());
  220. Idx++;
  221. }
  222. MachineBasicBlock &MBB = MIRBuilder.getMBB();
  223. if (!MBB.empty())
  224. MIRBuilder.setInstr(*MBB.begin());
  225. X86OutgoingValueAssigner Assigner(CC_X86);
  226. FormalArgHandler Handler(MIRBuilder, MRI);
  227. if (!determineAndHandleAssignments(Handler, Assigner, SplitArgs, MIRBuilder,
  228. F.getCallingConv(), F.isVarArg()))
  229. return false;
  230. // Move back to the end of the basic block.
  231. MIRBuilder.setMBB(MBB);
  232. return true;
  233. }
  234. bool X86CallLowering::lowerCall(MachineIRBuilder &MIRBuilder,
  235. CallLoweringInfo &Info) const {
  236. MachineFunction &MF = MIRBuilder.getMF();
  237. const Function &F = MF.getFunction();
  238. MachineRegisterInfo &MRI = MF.getRegInfo();
  239. const DataLayout &DL = F.getParent()->getDataLayout();
  240. const X86Subtarget &STI = MF.getSubtarget<X86Subtarget>();
  241. const TargetInstrInfo &TII = *STI.getInstrInfo();
  242. const X86RegisterInfo *TRI = STI.getRegisterInfo();
  243. // Handle only Linux C, X86_64_SysV calling conventions for now.
  244. if (!STI.isTargetLinux() || !(Info.CallConv == CallingConv::C ||
  245. Info.CallConv == CallingConv::X86_64_SysV))
  246. return false;
  247. unsigned AdjStackDown = TII.getCallFrameSetupOpcode();
  248. auto CallSeqStart = MIRBuilder.buildInstr(AdjStackDown);
  249. // Create a temporarily-floating call instruction so we can add the implicit
  250. // uses of arg registers.
  251. bool Is64Bit = STI.is64Bit();
  252. unsigned CallOpc = Info.Callee.isReg()
  253. ? (Is64Bit ? X86::CALL64r : X86::CALL32r)
  254. : (Is64Bit ? X86::CALL64pcrel32 : X86::CALLpcrel32);
  255. auto MIB = MIRBuilder.buildInstrNoInsert(CallOpc)
  256. .add(Info.Callee)
  257. .addRegMask(TRI->getCallPreservedMask(MF, Info.CallConv));
  258. SmallVector<ArgInfo, 8> SplitArgs;
  259. for (const auto &OrigArg : Info.OrigArgs) {
  260. // TODO: handle not simple cases.
  261. if (OrigArg.Flags[0].isByVal())
  262. return false;
  263. if (OrigArg.Regs.size() > 1)
  264. return false;
  265. splitToValueTypes(OrigArg, SplitArgs, DL, Info.CallConv);
  266. }
  267. // Do the actual argument marshalling.
  268. X86OutgoingValueAssigner Assigner(CC_X86);
  269. X86OutgoingValueHandler Handler(MIRBuilder, MRI, MIB);
  270. if (!determineAndHandleAssignments(Handler, Assigner, SplitArgs, MIRBuilder,
  271. Info.CallConv, Info.IsVarArg))
  272. return false;
  273. bool IsFixed = Info.OrigArgs.empty() ? true : Info.OrigArgs.back().IsFixed;
  274. if (STI.is64Bit() && !IsFixed && !STI.isCallingConvWin64(Info.CallConv)) {
  275. // From AMD64 ABI document:
  276. // For calls that may call functions that use varargs or stdargs
  277. // (prototype-less calls or calls to functions containing ellipsis (...) in
  278. // the declaration) %al is used as hidden argument to specify the number
  279. // of SSE registers used. The contents of %al do not need to match exactly
  280. // the number of registers, but must be an ubound on the number of SSE
  281. // registers used and is in the range 0 - 8 inclusive.
  282. MIRBuilder.buildInstr(X86::MOV8ri)
  283. .addDef(X86::AL)
  284. .addImm(Assigner.getNumXmmRegs());
  285. MIB.addUse(X86::AL, RegState::Implicit);
  286. }
  287. // Now we can add the actual call instruction to the correct basic block.
  288. MIRBuilder.insertInstr(MIB);
  289. // If Callee is a reg, since it is used by a target specific
  290. // instruction, it must have a register class matching the
  291. // constraint of that instruction.
  292. if (Info.Callee.isReg())
  293. MIB->getOperand(0).setReg(constrainOperandRegClass(
  294. MF, *TRI, MRI, *MF.getSubtarget().getInstrInfo(),
  295. *MF.getSubtarget().getRegBankInfo(), *MIB, MIB->getDesc(), Info.Callee,
  296. 0));
  297. // Finally we can copy the returned value back into its virtual-register. In
  298. // symmetry with the arguments, the physical register must be an
  299. // implicit-define of the call instruction.
  300. if (!Info.OrigRet.Ty->isVoidTy()) {
  301. if (Info.OrigRet.Regs.size() > 1)
  302. return false;
  303. SplitArgs.clear();
  304. SmallVector<Register, 8> NewRegs;
  305. splitToValueTypes(Info.OrigRet, SplitArgs, DL, Info.CallConv);
  306. X86OutgoingValueAssigner Assigner(RetCC_X86);
  307. CallReturnHandler Handler(MIRBuilder, MRI, MIB);
  308. if (!determineAndHandleAssignments(Handler, Assigner, SplitArgs, MIRBuilder,
  309. Info.CallConv, Info.IsVarArg))
  310. return false;
  311. if (!NewRegs.empty())
  312. MIRBuilder.buildMerge(Info.OrigRet.Regs[0], NewRegs);
  313. }
  314. CallSeqStart.addImm(Assigner.getStackSize())
  315. .addImm(0 /* see getFrameTotalSize */)
  316. .addImm(0 /* see getFrameAdjustment */);
  317. unsigned AdjStackUp = TII.getCallFrameDestroyOpcode();
  318. MIRBuilder.buildInstr(AdjStackUp)
  319. .addImm(Assigner.getStackSize())
  320. .addImm(0 /* NumBytesForCalleeToPop */);
  321. return true;
  322. }