2
0

CallingConvLower.cpp 8.9 KB

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