CallingConvLower.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. //===-- llvm/CallingConvLower.h - Calling Conventions -----------*- C++ -*-===//
  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 declares the CCState and CCValAssign classes, used for lowering
  11. // and implementing calling conventions.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #ifndef LLVM_CODEGEN_CALLINGCONVLOWER_H
  15. #define LLVM_CODEGEN_CALLINGCONVLOWER_H
  16. #include "llvm/ADT/SmallVector.h"
  17. #include "llvm/CodeGen/MachineFrameInfo.h"
  18. #include "llvm/CodeGen/MachineFunction.h"
  19. #include "llvm/IR/CallingConv.h"
  20. #include "llvm/MC/MCRegisterInfo.h"
  21. #include "llvm/Target/TargetCallingConv.h"
  22. namespace llvm {
  23. class CCState;
  24. class MVT;
  25. class TargetMachine;
  26. class TargetRegisterInfo;
  27. /// CCValAssign - Represent assignment of one arg/retval to a location.
  28. class CCValAssign {
  29. public:
  30. enum LocInfo {
  31. Full, // The value fills the full location.
  32. SExt, // The value is sign extended in the location.
  33. ZExt, // The value is zero extended in the location.
  34. AExt, // The value is extended with undefined upper bits.
  35. SExtUpper, // The value is in the upper bits of the location and should be
  36. // sign extended when retrieved.
  37. ZExtUpper, // The value is in the upper bits of the location and should be
  38. // zero extended when retrieved.
  39. AExtUpper, // The value is in the upper bits of the location and should be
  40. // extended with undefined upper bits when retrieved.
  41. BCvt, // The value is bit-converted in the location.
  42. VExt, // The value is vector-widened in the location.
  43. // FIXME: Not implemented yet. Code that uses AExt to mean
  44. // vector-widen should be fixed to use VExt instead.
  45. FPExt, // The floating-point value is fp-extended in the location.
  46. Indirect // The location contains pointer to the value.
  47. // TODO: a subset of the value is in the location.
  48. };
  49. private:
  50. /// ValNo - This is the value number begin assigned (e.g. an argument number).
  51. unsigned ValNo;
  52. /// Loc is either a stack offset or a register number.
  53. unsigned Loc;
  54. /// isMem - True if this is a memory loc, false if it is a register loc.
  55. unsigned isMem : 1;
  56. /// isCustom - True if this arg/retval requires special handling.
  57. unsigned isCustom : 1;
  58. /// Information about how the value is assigned.
  59. LocInfo HTP : 6;
  60. /// ValVT - The type of the value being assigned.
  61. MVT ValVT;
  62. /// LocVT - The type of the location being assigned to.
  63. MVT LocVT;
  64. public:
  65. static CCValAssign getReg(unsigned ValNo, MVT ValVT,
  66. unsigned RegNo, MVT LocVT,
  67. LocInfo HTP) {
  68. CCValAssign Ret;
  69. Ret.ValNo = ValNo;
  70. Ret.Loc = RegNo;
  71. Ret.isMem = false;
  72. Ret.isCustom = false;
  73. Ret.HTP = HTP;
  74. Ret.ValVT = ValVT;
  75. Ret.LocVT = LocVT;
  76. return Ret;
  77. }
  78. static CCValAssign getCustomReg(unsigned ValNo, MVT ValVT,
  79. unsigned RegNo, MVT LocVT,
  80. LocInfo HTP) {
  81. CCValAssign Ret;
  82. Ret = getReg(ValNo, ValVT, RegNo, LocVT, HTP);
  83. Ret.isCustom = true;
  84. return Ret;
  85. }
  86. static CCValAssign getMem(unsigned ValNo, MVT ValVT,
  87. unsigned Offset, MVT LocVT,
  88. LocInfo HTP) {
  89. CCValAssign Ret;
  90. Ret.ValNo = ValNo;
  91. Ret.Loc = Offset;
  92. Ret.isMem = true;
  93. Ret.isCustom = false;
  94. Ret.HTP = HTP;
  95. Ret.ValVT = ValVT;
  96. Ret.LocVT = LocVT;
  97. return Ret;
  98. }
  99. static CCValAssign getCustomMem(unsigned ValNo, MVT ValVT,
  100. unsigned Offset, MVT LocVT,
  101. LocInfo HTP) {
  102. CCValAssign Ret;
  103. Ret = getMem(ValNo, ValVT, Offset, LocVT, HTP);
  104. Ret.isCustom = true;
  105. return Ret;
  106. }
  107. // There is no need to differentiate between a pending CCValAssign and other
  108. // kinds, as they are stored in a different list.
  109. static CCValAssign getPending(unsigned ValNo, MVT ValVT, MVT LocVT,
  110. LocInfo HTP, unsigned ExtraInfo = 0) {
  111. return getReg(ValNo, ValVT, ExtraInfo, LocVT, HTP);
  112. }
  113. void convertToReg(unsigned RegNo) {
  114. Loc = RegNo;
  115. isMem = false;
  116. }
  117. void convertToMem(unsigned Offset) {
  118. Loc = Offset;
  119. isMem = true;
  120. }
  121. unsigned getValNo() const { return ValNo; }
  122. MVT getValVT() const { return ValVT; }
  123. bool isRegLoc() const { return !isMem; }
  124. bool isMemLoc() const { return isMem; }
  125. bool needsCustom() const { return isCustom; }
  126. unsigned getLocReg() const { assert(isRegLoc()); return Loc; }
  127. unsigned getLocMemOffset() const { assert(isMemLoc()); return Loc; }
  128. unsigned getExtraInfo() const { return Loc; }
  129. MVT getLocVT() const { return LocVT; }
  130. LocInfo getLocInfo() const { return HTP; }
  131. bool isExtInLoc() const {
  132. return (HTP == AExt || HTP == SExt || HTP == ZExt);
  133. }
  134. bool isUpperBitsInLoc() const {
  135. return HTP == AExtUpper || HTP == SExtUpper || HTP == ZExtUpper;
  136. }
  137. };
  138. /// Describes a register that needs to be forwarded from the prologue to a
  139. /// musttail call.
  140. struct ForwardedRegister {
  141. ForwardedRegister(unsigned VReg, MCPhysReg PReg, MVT VT)
  142. : VReg(VReg), PReg(PReg), VT(VT) {}
  143. unsigned VReg;
  144. MCPhysReg PReg;
  145. MVT VT;
  146. };
  147. /// CCAssignFn - This function assigns a location for Val, updating State to
  148. /// reflect the change. It returns 'true' if it failed to handle Val.
  149. typedef bool CCAssignFn(unsigned ValNo, MVT ValVT,
  150. MVT LocVT, CCValAssign::LocInfo LocInfo,
  151. ISD::ArgFlagsTy ArgFlags, CCState &State);
  152. /// CCCustomFn - This function assigns a location for Val, possibly updating
  153. /// all args to reflect changes and indicates if it handled it. It must set
  154. /// isCustom if it handles the arg and returns true.
  155. typedef bool CCCustomFn(unsigned &ValNo, MVT &ValVT,
  156. MVT &LocVT, CCValAssign::LocInfo &LocInfo,
  157. ISD::ArgFlagsTy &ArgFlags, CCState &State);
  158. /// ParmContext - This enum tracks whether calling convention lowering is in
  159. /// the context of prologue or call generation. Not all backends make use of
  160. /// this information.
  161. typedef enum { Unknown, Prologue, Call } ParmContext;
  162. /// CCState - This class holds information needed while lowering arguments and
  163. /// return values. It captures which registers are already assigned and which
  164. /// stack slots are used. It provides accessors to allocate these values.
  165. class CCState {
  166. private:
  167. CallingConv::ID CallingConv;
  168. bool IsVarArg;
  169. MachineFunction &MF;
  170. const TargetRegisterInfo &TRI;
  171. SmallVectorImpl<CCValAssign> &Locs;
  172. LLVMContext &Context;
  173. unsigned StackOffset;
  174. SmallVector<uint32_t, 16> UsedRegs;
  175. SmallVector<CCValAssign, 4> PendingLocs;
  176. // ByValInfo and SmallVector<ByValInfo, 4> ByValRegs:
  177. //
  178. // Vector of ByValInfo instances (ByValRegs) is introduced for byval registers
  179. // tracking.
  180. // Or, in another words it tracks byval parameters that are stored in
  181. // general purpose registers.
  182. //
  183. // For 4 byte stack alignment,
  184. // instance index means byval parameter number in formal
  185. // arguments set. Assume, we have some "struct_type" with size = 4 bytes,
  186. // then, for function "foo":
  187. //
  188. // i32 foo(i32 %p, %struct_type* %r, i32 %s, %struct_type* %t)
  189. //
  190. // ByValRegs[0] describes how "%r" is stored (Begin == r1, End == r2)
  191. // ByValRegs[1] describes how "%t" is stored (Begin == r3, End == r4).
  192. //
  193. // In case of 8 bytes stack alignment,
  194. // ByValRegs may also contain information about wasted registers.
  195. // In function shown above, r3 would be wasted according to AAPCS rules.
  196. // And in that case ByValRegs[1].Waste would be "true".
  197. // ByValRegs vector size still would be 2,
  198. // while "%t" goes to the stack: it wouldn't be described in ByValRegs.
  199. //
  200. // Supposed use-case for this collection:
  201. // 1. Initially ByValRegs is empty, InRegsParamsProcessed is 0.
  202. // 2. HandleByVal fillups ByValRegs.
  203. // 3. Argument analysis (LowerFormatArguments, for example). After
  204. // some byval argument was analyzed, InRegsParamsProcessed is increased.
  205. struct ByValInfo {
  206. ByValInfo(unsigned B, unsigned E, bool IsWaste = false) :
  207. Begin(B), End(E), Waste(IsWaste) {}
  208. // First register allocated for current parameter.
  209. unsigned Begin;
  210. // First after last register allocated for current parameter.
  211. unsigned End;
  212. // Means that current range of registers doesn't belong to any
  213. // parameters. It was wasted due to stack alignment rules.
  214. // For more information see:
  215. // AAPCS, 5.5 Parameter Passing, Stage C, C.3.
  216. bool Waste;
  217. };
  218. SmallVector<ByValInfo, 4 > ByValRegs;
  219. // InRegsParamsProcessed - shows how many instances of ByValRegs was proceed
  220. // during argument analysis.
  221. unsigned InRegsParamsProcessed;
  222. protected:
  223. ParmContext CallOrPrologue;
  224. public:
  225. CCState(CallingConv::ID CC, bool isVarArg, MachineFunction &MF,
  226. SmallVectorImpl<CCValAssign> &locs, LLVMContext &C);
  227. void addLoc(const CCValAssign &V) {
  228. Locs.push_back(V);
  229. }
  230. LLVMContext &getContext() const { return Context; }
  231. MachineFunction &getMachineFunction() const { return MF; }
  232. CallingConv::ID getCallingConv() const { return CallingConv; }
  233. bool isVarArg() const { return IsVarArg; }
  234. unsigned getNextStackOffset() const { return StackOffset; }
  235. /// isAllocated - Return true if the specified register (or an alias) is
  236. /// allocated.
  237. bool isAllocated(unsigned Reg) const {
  238. return UsedRegs[Reg/32] & (1 << (Reg&31));
  239. }
  240. /// AnalyzeFormalArguments - Analyze an array of argument values,
  241. /// incorporating info about the formals into this state.
  242. void AnalyzeFormalArguments(const SmallVectorImpl<ISD::InputArg> &Ins,
  243. CCAssignFn Fn);
  244. /// AnalyzeReturn - Analyze the returned values of a return,
  245. /// incorporating info about the result values into this state.
  246. void AnalyzeReturn(const SmallVectorImpl<ISD::OutputArg> &Outs,
  247. CCAssignFn Fn);
  248. /// CheckReturn - Analyze the return values of a function, returning
  249. /// true if the return can be performed without sret-demotion, and
  250. /// false otherwise.
  251. bool CheckReturn(const SmallVectorImpl<ISD::OutputArg> &ArgsFlags,
  252. CCAssignFn Fn);
  253. /// AnalyzeCallOperands - Analyze the outgoing arguments to a call,
  254. /// incorporating info about the passed values into this state.
  255. void AnalyzeCallOperands(const SmallVectorImpl<ISD::OutputArg> &Outs,
  256. CCAssignFn Fn);
  257. /// AnalyzeCallOperands - Same as above except it takes vectors of types
  258. /// and argument flags.
  259. void AnalyzeCallOperands(SmallVectorImpl<MVT> &ArgVTs,
  260. SmallVectorImpl<ISD::ArgFlagsTy> &Flags,
  261. CCAssignFn Fn);
  262. /// AnalyzeCallResult - Analyze the return values of a call,
  263. /// incorporating info about the passed values into this state.
  264. void AnalyzeCallResult(const SmallVectorImpl<ISD::InputArg> &Ins,
  265. CCAssignFn Fn);
  266. /// AnalyzeCallResult - Same as above except it's specialized for calls which
  267. /// produce a single value.
  268. void AnalyzeCallResult(MVT VT, CCAssignFn Fn);
  269. /// getFirstUnallocated - Return the index of the first unallocated register
  270. /// in the set, or Regs.size() if they are all allocated.
  271. unsigned getFirstUnallocated(ArrayRef<MCPhysReg> Regs) const {
  272. for (unsigned i = 0; i < Regs.size(); ++i)
  273. if (!isAllocated(Regs[i]))
  274. return i;
  275. return Regs.size();
  276. }
  277. /// AllocateReg - Attempt to allocate one register. If it is not available,
  278. /// return zero. Otherwise, return the register, marking it and any aliases
  279. /// as allocated.
  280. unsigned AllocateReg(unsigned Reg) {
  281. if (isAllocated(Reg)) return 0;
  282. MarkAllocated(Reg);
  283. return Reg;
  284. }
  285. /// Version of AllocateReg with extra register to be shadowed.
  286. unsigned AllocateReg(unsigned Reg, unsigned ShadowReg) {
  287. if (isAllocated(Reg)) return 0;
  288. MarkAllocated(Reg);
  289. MarkAllocated(ShadowReg);
  290. return Reg;
  291. }
  292. /// AllocateReg - Attempt to allocate one of the specified registers. If none
  293. /// are available, return zero. Otherwise, return the first one available,
  294. /// marking it and any aliases as allocated.
  295. unsigned AllocateReg(ArrayRef<MCPhysReg> Regs) {
  296. unsigned FirstUnalloc = getFirstUnallocated(Regs);
  297. if (FirstUnalloc == Regs.size())
  298. return 0; // Didn't find the reg.
  299. // Mark the register and any aliases as allocated.
  300. unsigned Reg = Regs[FirstUnalloc];
  301. MarkAllocated(Reg);
  302. return Reg;
  303. }
  304. /// AllocateRegBlock - Attempt to allocate a block of RegsRequired consecutive
  305. /// registers. If this is not possible, return zero. Otherwise, return the first
  306. /// register of the block that were allocated, marking the entire block as allocated.
  307. unsigned AllocateRegBlock(ArrayRef<uint16_t> Regs, unsigned RegsRequired) {
  308. if (RegsRequired > Regs.size())
  309. return 0;
  310. for (unsigned StartIdx = 0; StartIdx <= Regs.size() - RegsRequired;
  311. ++StartIdx) {
  312. bool BlockAvailable = true;
  313. // Check for already-allocated regs in this block
  314. for (unsigned BlockIdx = 0; BlockIdx < RegsRequired; ++BlockIdx) {
  315. if (isAllocated(Regs[StartIdx + BlockIdx])) {
  316. BlockAvailable = false;
  317. break;
  318. }
  319. }
  320. if (BlockAvailable) {
  321. // Mark the entire block as allocated
  322. for (unsigned BlockIdx = 0; BlockIdx < RegsRequired; ++BlockIdx) {
  323. MarkAllocated(Regs[StartIdx + BlockIdx]);
  324. }
  325. return Regs[StartIdx];
  326. }
  327. }
  328. // No block was available
  329. return 0;
  330. }
  331. /// Version of AllocateReg with list of registers to be shadowed.
  332. unsigned AllocateReg(ArrayRef<MCPhysReg> Regs, const MCPhysReg *ShadowRegs) {
  333. unsigned FirstUnalloc = getFirstUnallocated(Regs);
  334. if (FirstUnalloc == Regs.size())
  335. return 0; // Didn't find the reg.
  336. // Mark the register and any aliases as allocated.
  337. unsigned Reg = Regs[FirstUnalloc], ShadowReg = ShadowRegs[FirstUnalloc];
  338. MarkAllocated(Reg);
  339. MarkAllocated(ShadowReg);
  340. return Reg;
  341. }
  342. /// AllocateStack - Allocate a chunk of stack space with the specified size
  343. /// and alignment.
  344. unsigned AllocateStack(unsigned Size, unsigned Align) {
  345. assert(Align && ((Align - 1) & Align) == 0); // Align is power of 2.
  346. StackOffset = ((StackOffset + Align - 1) & ~(Align - 1));
  347. unsigned Result = StackOffset;
  348. StackOffset += Size;
  349. MF.getFrameInfo()->ensureMaxAlignment(Align);
  350. return Result;
  351. }
  352. /// Version of AllocateStack with extra register to be shadowed.
  353. unsigned AllocateStack(unsigned Size, unsigned Align, unsigned ShadowReg) {
  354. MarkAllocated(ShadowReg);
  355. return AllocateStack(Size, Align);
  356. }
  357. /// Version of AllocateStack with list of extra registers to be shadowed.
  358. /// Note that, unlike AllocateReg, this shadows ALL of the shadow registers.
  359. unsigned AllocateStack(unsigned Size, unsigned Align,
  360. ArrayRef<MCPhysReg> ShadowRegs) {
  361. for (unsigned i = 0; i < ShadowRegs.size(); ++i)
  362. MarkAllocated(ShadowRegs[i]);
  363. return AllocateStack(Size, Align);
  364. }
  365. // HandleByVal - Allocate a stack slot large enough to pass an argument by
  366. // value. The size and alignment information of the argument is encoded in its
  367. // parameter attribute.
  368. void HandleByVal(unsigned ValNo, MVT ValVT,
  369. MVT LocVT, CCValAssign::LocInfo LocInfo,
  370. int MinSize, int MinAlign, ISD::ArgFlagsTy ArgFlags);
  371. // Returns count of byval arguments that are to be stored (even partly)
  372. // in registers.
  373. unsigned getInRegsParamsCount() const { return ByValRegs.size(); }
  374. // Returns count of byval in-regs arguments proceed.
  375. unsigned getInRegsParamsProcessed() const { return InRegsParamsProcessed; }
  376. // Get information about N-th byval parameter that is stored in registers.
  377. // Here "ByValParamIndex" is N.
  378. void getInRegsParamInfo(unsigned InRegsParamRecordIndex,
  379. unsigned& BeginReg, unsigned& EndReg) const {
  380. assert(InRegsParamRecordIndex < ByValRegs.size() &&
  381. "Wrong ByVal parameter index");
  382. const ByValInfo& info = ByValRegs[InRegsParamRecordIndex];
  383. BeginReg = info.Begin;
  384. EndReg = info.End;
  385. }
  386. // Add information about parameter that is kept in registers.
  387. void addInRegsParamInfo(unsigned RegBegin, unsigned RegEnd) {
  388. ByValRegs.push_back(ByValInfo(RegBegin, RegEnd));
  389. }
  390. // Goes either to next byval parameter (excluding "waste" record), or
  391. // to the end of collection.
  392. // Returns false, if end is reached.
  393. bool nextInRegsParam() {
  394. unsigned e = ByValRegs.size();
  395. if (InRegsParamsProcessed < e)
  396. ++InRegsParamsProcessed;
  397. return InRegsParamsProcessed < e;
  398. }
  399. // Clear byval registers tracking info.
  400. void clearByValRegsInfo() {
  401. InRegsParamsProcessed = 0;
  402. ByValRegs.clear();
  403. }
  404. // Rewind byval registers tracking info.
  405. void rewindByValRegsInfo() {
  406. InRegsParamsProcessed = 0;
  407. }
  408. ParmContext getCallOrPrologue() const { return CallOrPrologue; }
  409. // Get list of pending assignments
  410. SmallVectorImpl<llvm::CCValAssign> &getPendingLocs() {
  411. return PendingLocs;
  412. }
  413. /// Compute the remaining unused register parameters that would be used for
  414. /// the given value type. This is useful when varargs are passed in the
  415. /// registers that normal prototyped parameters would be passed in, or for
  416. /// implementing perfect forwarding.
  417. void getRemainingRegParmsForType(SmallVectorImpl<MCPhysReg> &Regs, MVT VT,
  418. CCAssignFn Fn);
  419. /// Compute the set of registers that need to be preserved and forwarded to
  420. /// any musttail calls.
  421. void analyzeMustTailForwardedRegisters(
  422. SmallVectorImpl<ForwardedRegister> &Forwards, ArrayRef<MVT> RegParmTypes,
  423. CCAssignFn Fn);
  424. private:
  425. /// MarkAllocated - Mark a register and all of its aliases as allocated.
  426. void MarkAllocated(unsigned Reg);
  427. };
  428. } // end namespace llvm
  429. #endif