CallingConvLower.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. //===-- CallingConvLower.cpp - Calling Conventions ------------------------===//
  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 CCState class, used for lowering and implementing
  10. // calling conventions.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "llvm/CodeGen/CallingConvLower.h"
  14. #include "llvm/CodeGen/MachineFrameInfo.h"
  15. #include "llvm/CodeGen/MachineFunction.h"
  16. #include "llvm/CodeGen/MachineRegisterInfo.h"
  17. #include "llvm/CodeGen/TargetLowering.h"
  18. #include "llvm/CodeGen/TargetRegisterInfo.h"
  19. #include "llvm/CodeGen/TargetSubtargetInfo.h"
  20. #include "llvm/IR/DataLayout.h"
  21. #include "llvm/Support/Debug.h"
  22. #include "llvm/Support/ErrorHandling.h"
  23. #include "llvm/Support/SaveAndRestore.h"
  24. #include "llvm/Support/raw_ostream.h"
  25. #include <algorithm>
  26. using namespace llvm;
  27. CCState::CCState(CallingConv::ID CC, bool isVarArg, MachineFunction &mf,
  28. SmallVectorImpl<CCValAssign> &locs, LLVMContext &C)
  29. : CallingConv(CC), IsVarArg(isVarArg), MF(mf),
  30. TRI(*MF.getSubtarget().getRegisterInfo()), Locs(locs), Context(C) {
  31. // No stack is used.
  32. StackOffset = 0;
  33. clearByValRegsInfo();
  34. UsedRegs.resize((TRI.getNumRegs()+31)/32);
  35. }
  36. /// Allocate space on the stack large enough to pass an argument by value.
  37. /// The size and alignment information of the argument is encoded in
  38. /// its parameter attribute.
  39. void CCState::HandleByVal(unsigned ValNo, MVT ValVT, MVT LocVT,
  40. CCValAssign::LocInfo LocInfo, int MinSize,
  41. Align MinAlign, ISD::ArgFlagsTy ArgFlags) {
  42. Align Alignment = ArgFlags.getNonZeroByValAlign();
  43. unsigned Size = ArgFlags.getByValSize();
  44. if (MinSize > (int)Size)
  45. Size = MinSize;
  46. if (MinAlign > Alignment)
  47. Alignment = MinAlign;
  48. ensureMaxAlignment(Alignment);
  49. MF.getSubtarget().getTargetLowering()->HandleByVal(this, Size, Alignment);
  50. Size = unsigned(alignTo(Size, MinAlign));
  51. unsigned Offset = AllocateStack(Size, Alignment);
  52. addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
  53. }
  54. /// Mark a register and all of its aliases as allocated.
  55. void CCState::MarkAllocated(MCPhysReg Reg) {
  56. for (MCRegAliasIterator AI(Reg, &TRI, true); AI.isValid(); ++AI)
  57. UsedRegs[*AI / 32] |= 1 << (*AI & 31);
  58. }
  59. void CCState::MarkUnallocated(MCPhysReg Reg) {
  60. for (MCRegAliasIterator AI(Reg, &TRI, true); AI.isValid(); ++AI)
  61. UsedRegs[*AI / 32] &= ~(1 << (*AI & 31));
  62. }
  63. bool CCState::IsShadowAllocatedReg(MCRegister Reg) const {
  64. if (!isAllocated(Reg))
  65. return false;
  66. for (auto const &ValAssign : Locs) {
  67. if (ValAssign.isRegLoc()) {
  68. for (MCRegAliasIterator AI(ValAssign.getLocReg(), &TRI, true);
  69. AI.isValid(); ++AI) {
  70. if (*AI == Reg)
  71. return false;
  72. }
  73. }
  74. }
  75. return true;
  76. }
  77. /// Analyze an array of argument values,
  78. /// incorporating info about the formals into this state.
  79. void
  80. CCState::AnalyzeFormalArguments(const SmallVectorImpl<ISD::InputArg> &Ins,
  81. CCAssignFn Fn) {
  82. unsigned NumArgs = Ins.size();
  83. for (unsigned i = 0; i != NumArgs; ++i) {
  84. MVT ArgVT = Ins[i].VT;
  85. ISD::ArgFlagsTy ArgFlags = Ins[i].Flags;
  86. if (Fn(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, *this))
  87. report_fatal_error("unable to allocate function argument #" + Twine(i));
  88. }
  89. }
  90. /// Analyze the return values of a function, returning true if the return can
  91. /// be performed without sret-demotion and false otherwise.
  92. bool CCState::CheckReturn(const SmallVectorImpl<ISD::OutputArg> &Outs,
  93. CCAssignFn Fn) {
  94. // Determine which register each value should be copied into.
  95. for (unsigned i = 0, e = Outs.size(); i != e; ++i) {
  96. MVT VT = Outs[i].VT;
  97. ISD::ArgFlagsTy ArgFlags = Outs[i].Flags;
  98. if (Fn(i, VT, VT, CCValAssign::Full, ArgFlags, *this))
  99. return false;
  100. }
  101. return true;
  102. }
  103. /// Analyze the returned values of a return,
  104. /// incorporating info about the result values into this state.
  105. void CCState::AnalyzeReturn(const SmallVectorImpl<ISD::OutputArg> &Outs,
  106. CCAssignFn Fn) {
  107. // Determine which register each value should be copied into.
  108. for (unsigned i = 0, e = Outs.size(); i != e; ++i) {
  109. MVT VT = Outs[i].VT;
  110. ISD::ArgFlagsTy ArgFlags = Outs[i].Flags;
  111. if (Fn(i, VT, VT, CCValAssign::Full, ArgFlags, *this))
  112. report_fatal_error("unable to allocate function return #" + Twine(i));
  113. }
  114. }
  115. /// Analyze the outgoing arguments to a call,
  116. /// incorporating info about the passed values into this state.
  117. void CCState::AnalyzeCallOperands(const SmallVectorImpl<ISD::OutputArg> &Outs,
  118. CCAssignFn Fn) {
  119. unsigned NumOps = Outs.size();
  120. for (unsigned i = 0; i != NumOps; ++i) {
  121. MVT ArgVT = Outs[i].VT;
  122. ISD::ArgFlagsTy ArgFlags = Outs[i].Flags;
  123. if (Fn(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, *this)) {
  124. #ifndef NDEBUG
  125. dbgs() << "Call operand #" << i << " has unhandled type "
  126. << EVT(ArgVT).getEVTString() << '\n';
  127. #endif
  128. llvm_unreachable(nullptr);
  129. }
  130. }
  131. }
  132. /// Same as above except it takes vectors of types and argument flags.
  133. void CCState::AnalyzeCallOperands(SmallVectorImpl<MVT> &ArgVTs,
  134. SmallVectorImpl<ISD::ArgFlagsTy> &Flags,
  135. CCAssignFn Fn) {
  136. unsigned NumOps = ArgVTs.size();
  137. for (unsigned i = 0; i != NumOps; ++i) {
  138. MVT ArgVT = ArgVTs[i];
  139. ISD::ArgFlagsTy ArgFlags = Flags[i];
  140. if (Fn(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, *this)) {
  141. #ifndef NDEBUG
  142. dbgs() << "Call operand #" << i << " has unhandled type "
  143. << EVT(ArgVT).getEVTString() << '\n';
  144. #endif
  145. llvm_unreachable(nullptr);
  146. }
  147. }
  148. }
  149. /// Analyze the return values of a call, incorporating info about the passed
  150. /// values into this state.
  151. void CCState::AnalyzeCallResult(const SmallVectorImpl<ISD::InputArg> &Ins,
  152. CCAssignFn Fn) {
  153. for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
  154. MVT VT = Ins[i].VT;
  155. ISD::ArgFlagsTy Flags = Ins[i].Flags;
  156. if (Fn(i, VT, VT, CCValAssign::Full, Flags, *this)) {
  157. #ifndef NDEBUG
  158. dbgs() << "Call result #" << i << " has unhandled type "
  159. << EVT(VT).getEVTString() << '\n';
  160. #endif
  161. llvm_unreachable(nullptr);
  162. }
  163. }
  164. }
  165. /// Same as above except it's specialized for calls that produce a single value.
  166. void CCState::AnalyzeCallResult(MVT VT, CCAssignFn Fn) {
  167. if (Fn(0, VT, VT, CCValAssign::Full, ISD::ArgFlagsTy(), *this)) {
  168. #ifndef NDEBUG
  169. dbgs() << "Call result has unhandled type "
  170. << EVT(VT).getEVTString() << '\n';
  171. #endif
  172. llvm_unreachable(nullptr);
  173. }
  174. }
  175. void CCState::ensureMaxAlignment(Align Alignment) {
  176. if (!AnalyzingMustTailForwardedRegs)
  177. MF.getFrameInfo().ensureMaxAlignment(Alignment);
  178. }
  179. static bool isValueTypeInRegForCC(CallingConv::ID CC, MVT VT) {
  180. if (VT.isVector())
  181. return true; // Assume -msse-regparm might be in effect.
  182. if (!VT.isInteger())
  183. return false;
  184. return (CC == CallingConv::X86_VectorCall || CC == CallingConv::X86_FastCall);
  185. }
  186. void CCState::getRemainingRegParmsForType(SmallVectorImpl<MCPhysReg> &Regs,
  187. MVT VT, CCAssignFn Fn) {
  188. unsigned SavedStackOffset = StackOffset;
  189. Align SavedMaxStackArgAlign = MaxStackArgAlign;
  190. unsigned NumLocs = Locs.size();
  191. // Set the 'inreg' flag if it is used for this calling convention.
  192. ISD::ArgFlagsTy Flags;
  193. if (isValueTypeInRegForCC(CallingConv, VT))
  194. Flags.setInReg();
  195. // Allocate something of this value type repeatedly until we get assigned a
  196. // location in memory.
  197. bool HaveRegParm;
  198. do {
  199. if (Fn(0, VT, VT, CCValAssign::Full, Flags, *this)) {
  200. #ifndef NDEBUG
  201. dbgs() << "Call has unhandled type " << EVT(VT).getEVTString()
  202. << " while computing remaining regparms\n";
  203. #endif
  204. llvm_unreachable(nullptr);
  205. }
  206. HaveRegParm = Locs.back().isRegLoc();
  207. } while (HaveRegParm);
  208. // Copy all the registers from the value locations we added.
  209. assert(NumLocs < Locs.size() && "CC assignment failed to add location");
  210. for (unsigned I = NumLocs, E = Locs.size(); I != E; ++I)
  211. if (Locs[I].isRegLoc())
  212. Regs.push_back(MCPhysReg(Locs[I].getLocReg()));
  213. // Clear the assigned values and stack memory. We leave the registers marked
  214. // as allocated so that future queries don't return the same registers, i.e.
  215. // when i64 and f64 are both passed in GPRs.
  216. StackOffset = SavedStackOffset;
  217. MaxStackArgAlign = SavedMaxStackArgAlign;
  218. Locs.resize(NumLocs);
  219. }
  220. void CCState::analyzeMustTailForwardedRegisters(
  221. SmallVectorImpl<ForwardedRegister> &Forwards, ArrayRef<MVT> RegParmTypes,
  222. CCAssignFn Fn) {
  223. // Oftentimes calling conventions will not user register parameters for
  224. // variadic functions, so we need to assume we're not variadic so that we get
  225. // all the registers that might be used in a non-variadic call.
  226. SaveAndRestore<bool> SavedVarArg(IsVarArg, false);
  227. SaveAndRestore<bool> SavedMustTail(AnalyzingMustTailForwardedRegs, true);
  228. for (MVT RegVT : RegParmTypes) {
  229. SmallVector<MCPhysReg, 8> RemainingRegs;
  230. getRemainingRegParmsForType(RemainingRegs, RegVT, Fn);
  231. const TargetLowering *TL = MF.getSubtarget().getTargetLowering();
  232. const TargetRegisterClass *RC = TL->getRegClassFor(RegVT);
  233. for (MCPhysReg PReg : RemainingRegs) {
  234. Register VReg = MF.addLiveIn(PReg, RC);
  235. Forwards.push_back(ForwardedRegister(VReg, PReg, RegVT));
  236. }
  237. }
  238. }
  239. bool CCState::resultsCompatible(CallingConv::ID CalleeCC,
  240. CallingConv::ID CallerCC, MachineFunction &MF,
  241. LLVMContext &C,
  242. const SmallVectorImpl<ISD::InputArg> &Ins,
  243. CCAssignFn CalleeFn, CCAssignFn CallerFn) {
  244. if (CalleeCC == CallerCC)
  245. return true;
  246. SmallVector<CCValAssign, 4> RVLocs1;
  247. CCState CCInfo1(CalleeCC, false, MF, RVLocs1, C);
  248. CCInfo1.AnalyzeCallResult(Ins, CalleeFn);
  249. SmallVector<CCValAssign, 4> RVLocs2;
  250. CCState CCInfo2(CallerCC, false, MF, RVLocs2, C);
  251. CCInfo2.AnalyzeCallResult(Ins, CallerFn);
  252. if (RVLocs1.size() != RVLocs2.size())
  253. return false;
  254. for (unsigned I = 0, E = RVLocs1.size(); I != E; ++I) {
  255. const CCValAssign &Loc1 = RVLocs1[I];
  256. const CCValAssign &Loc2 = RVLocs2[I];
  257. if ( // Must both be in registers, or both in memory
  258. Loc1.isRegLoc() != Loc2.isRegLoc() ||
  259. // Must fill the same part of their locations
  260. Loc1.getLocInfo() != Loc2.getLocInfo() ||
  261. // Memory offset/register number must be the same
  262. Loc1.getExtraInfo() != Loc2.getExtraInfo())
  263. return false;
  264. }
  265. return true;
  266. }