CallingConvLower.cpp 10 KB

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