CallingConvLower.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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/IR/DataLayout.h"
  17. #include "llvm/Support/Debug.h"
  18. #include "llvm/Support/ErrorHandling.h"
  19. #include "llvm/Support/raw_ostream.h"
  20. #include "llvm/Target/TargetLowering.h"
  21. #include "llvm/Target/TargetMachine.h"
  22. #include "llvm/Target/TargetRegisterInfo.h"
  23. #include "llvm/Target/TargetSubtargetInfo.h"
  24. using namespace llvm;
  25. CCState::CCState(CallingConv::ID CC, bool isVarArg, MachineFunction &mf,
  26. const TargetMachine &tm, SmallVectorImpl<CCValAssign> &locs,
  27. LLVMContext &C)
  28. : CallingConv(CC), IsVarArg(isVarArg), MF(mf), TM(tm),
  29. TRI(*TM.getSubtargetImpl()->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. // HandleByVal - Allocate space on the stack large enough to pass an argument
  37. // by value. 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. TM.getSubtargetImpl()->getTargetLowering()->HandleByVal(this, Size, Align);
  51. unsigned Offset = AllocateStack(Size, Align);
  52. addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
  53. }
  54. /// MarkAllocated - Mark a register and all of its aliases as allocated.
  55. void CCState::MarkAllocated(unsigned Reg) {
  56. for (MCRegAliasIterator AI(Reg, &TRI, true); AI.isValid(); ++AI)
  57. UsedRegs[*AI/32] |= 1 << (*AI&31);
  58. }
  59. /// AnalyzeFormalArguments - Analyze an array of argument values,
  60. /// incorporating info about the formals into this state.
  61. void
  62. CCState::AnalyzeFormalArguments(const SmallVectorImpl<ISD::InputArg> &Ins,
  63. CCAssignFn Fn) {
  64. unsigned NumArgs = Ins.size();
  65. for (unsigned i = 0; i != NumArgs; ++i) {
  66. MVT ArgVT = Ins[i].VT;
  67. ISD::ArgFlagsTy ArgFlags = Ins[i].Flags;
  68. if (Fn(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, *this)) {
  69. #ifndef NDEBUG
  70. dbgs() << "Formal argument #" << i << " has unhandled type "
  71. << EVT(ArgVT).getEVTString() << '\n';
  72. #endif
  73. llvm_unreachable(nullptr);
  74. }
  75. }
  76. }
  77. /// CheckReturn - Analyze the return values of a function, returning true if
  78. /// the return can be performed without sret-demotion, and false otherwise.
  79. bool CCState::CheckReturn(const SmallVectorImpl<ISD::OutputArg> &Outs,
  80. CCAssignFn Fn) {
  81. // Determine which register each value should be copied into.
  82. for (unsigned i = 0, e = Outs.size(); i != e; ++i) {
  83. MVT VT = Outs[i].VT;
  84. ISD::ArgFlagsTy ArgFlags = Outs[i].Flags;
  85. if (Fn(i, VT, VT, CCValAssign::Full, ArgFlags, *this))
  86. return false;
  87. }
  88. return true;
  89. }
  90. /// AnalyzeReturn - Analyze the returned values of a return,
  91. /// incorporating info about the result values into this state.
  92. void CCState::AnalyzeReturn(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. #ifndef NDEBUG
  100. dbgs() << "Return operand #" << i << " has unhandled type "
  101. << EVT(VT).getEVTString() << '\n';
  102. #endif
  103. llvm_unreachable(nullptr);
  104. }
  105. }
  106. }
  107. /// AnalyzeCallOperands - Analyze the outgoing arguments to a call,
  108. /// incorporating info about the passed values into this state.
  109. void CCState::AnalyzeCallOperands(const SmallVectorImpl<ISD::OutputArg> &Outs,
  110. CCAssignFn Fn) {
  111. unsigned NumOps = Outs.size();
  112. for (unsigned i = 0; i != NumOps; ++i) {
  113. MVT ArgVT = Outs[i].VT;
  114. ISD::ArgFlagsTy ArgFlags = Outs[i].Flags;
  115. if (Fn(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, *this)) {
  116. #ifndef NDEBUG
  117. dbgs() << "Call operand #" << i << " has unhandled type "
  118. << EVT(ArgVT).getEVTString() << '\n';
  119. #endif
  120. llvm_unreachable(nullptr);
  121. }
  122. }
  123. }
  124. /// AnalyzeCallOperands - Same as above except it takes vectors of types
  125. /// 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. /// AnalyzeCallResult - Analyze the return values of a call,
  143. /// incorporating info about the passed 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. /// AnalyzeCallResult - Same as above except it's specialized for calls which
  159. /// 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. }