CallingConvLower.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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/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. using namespace llvm;
  24. CCState::CCState(CallingConv::ID CC, bool isVarArg, MachineFunction &mf,
  25. const TargetMachine &tm, SmallVector<CCValAssign, 16> &locs,
  26. LLVMContext &C)
  27. : CallingConv(CC), IsVarArg(isVarArg), MF(mf), TM(tm),
  28. TRI(*TM.getRegisterInfo()), Locs(locs), Context(C),
  29. CallOrPrologue(Unknown) {
  30. // No stack is used.
  31. StackOffset = 0;
  32. clearFirstByValReg();
  33. UsedRegs.resize((TRI.getNumRegs()+31)/32);
  34. }
  35. // HandleByVal - Allocate space on the stack large enough to pass an argument
  36. // by value. The size and alignment information of the argument is encoded in
  37. // its parameter attribute.
  38. void CCState::HandleByVal(unsigned ValNo, MVT ValVT,
  39. MVT LocVT, CCValAssign::LocInfo LocInfo,
  40. int MinSize, int MinAlign,
  41. ISD::ArgFlagsTy ArgFlags) {
  42. unsigned Align = ArgFlags.getByValAlign();
  43. unsigned Size = ArgFlags.getByValSize();
  44. if (MinSize > (int)Size)
  45. Size = MinSize;
  46. if (MinAlign > (int)Align)
  47. Align = MinAlign;
  48. MF.getFrameInfo()->ensureMaxAlignment(Align);
  49. TM.getTargetLowering()->HandleByVal(this, Size, Align);
  50. unsigned Offset = AllocateStack(Size, Align);
  51. addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
  52. }
  53. /// MarkAllocated - Mark a register and all of its aliases as allocated.
  54. void CCState::MarkAllocated(unsigned Reg) {
  55. for (MCRegAliasIterator AI(Reg, &TRI, true); AI.isValid(); ++AI)
  56. UsedRegs[*AI/32] |= 1 << (*AI&31);
  57. }
  58. /// AnalyzeFormalArguments - Analyze an array of argument values,
  59. /// incorporating info about the formals into this state.
  60. void
  61. CCState::AnalyzeFormalArguments(const SmallVectorImpl<ISD::InputArg> &Ins,
  62. CCAssignFn Fn) {
  63. unsigned NumArgs = Ins.size();
  64. for (unsigned i = 0; i != NumArgs; ++i) {
  65. MVT ArgVT = Ins[i].VT;
  66. ISD::ArgFlagsTy ArgFlags = Ins[i].Flags;
  67. if (Fn(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, *this)) {
  68. #ifndef NDEBUG
  69. dbgs() << "Formal argument #" << i << " has unhandled type "
  70. << EVT(ArgVT).getEVTString() << '\n';
  71. #endif
  72. llvm_unreachable(0);
  73. }
  74. }
  75. }
  76. /// CheckReturn - Analyze the return values of a function, returning true if
  77. /// the return can be performed without sret-demotion, and false otherwise.
  78. bool CCState::CheckReturn(const SmallVectorImpl<ISD::OutputArg> &Outs,
  79. CCAssignFn Fn) {
  80. // Determine which register each value should be copied into.
  81. for (unsigned i = 0, e = Outs.size(); i != e; ++i) {
  82. MVT VT = Outs[i].VT;
  83. ISD::ArgFlagsTy ArgFlags = Outs[i].Flags;
  84. if (Fn(i, VT, VT, CCValAssign::Full, ArgFlags, *this))
  85. return false;
  86. }
  87. return true;
  88. }
  89. /// AnalyzeReturn - Analyze the returned values of a return,
  90. /// incorporating info about the result values into this state.
  91. void CCState::AnalyzeReturn(const SmallVectorImpl<ISD::OutputArg> &Outs,
  92. CCAssignFn Fn) {
  93. // Determine which register each value should be copied into.
  94. for (unsigned i = 0, e = Outs.size(); i != e; ++i) {
  95. MVT VT = Outs[i].VT;
  96. ISD::ArgFlagsTy ArgFlags = Outs[i].Flags;
  97. if (Fn(i, VT, VT, CCValAssign::Full, ArgFlags, *this)) {
  98. #ifndef NDEBUG
  99. dbgs() << "Return operand #" << i << " has unhandled type "
  100. << EVT(VT).getEVTString() << '\n';
  101. #endif
  102. llvm_unreachable(0);
  103. }
  104. }
  105. }
  106. /// AnalyzeCallOperands - Analyze the outgoing arguments to a call,
  107. /// incorporating info about the passed values into this state.
  108. void CCState::AnalyzeCallOperands(const SmallVectorImpl<ISD::OutputArg> &Outs,
  109. CCAssignFn Fn) {
  110. unsigned NumOps = Outs.size();
  111. for (unsigned i = 0; i != NumOps; ++i) {
  112. MVT ArgVT = Outs[i].VT;
  113. ISD::ArgFlagsTy ArgFlags = Outs[i].Flags;
  114. if (Fn(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, *this)) {
  115. #ifndef NDEBUG
  116. dbgs() << "Call operand #" << i << " has unhandled type "
  117. << EVT(ArgVT).getEVTString() << '\n';
  118. #endif
  119. llvm_unreachable(0);
  120. }
  121. }
  122. }
  123. /// AnalyzeCallOperands - Same as above except it takes vectors of types
  124. /// and argument flags.
  125. void CCState::AnalyzeCallOperands(SmallVectorImpl<MVT> &ArgVTs,
  126. SmallVectorImpl<ISD::ArgFlagsTy> &Flags,
  127. CCAssignFn Fn) {
  128. unsigned NumOps = ArgVTs.size();
  129. for (unsigned i = 0; i != NumOps; ++i) {
  130. MVT ArgVT = ArgVTs[i];
  131. ISD::ArgFlagsTy ArgFlags = Flags[i];
  132. if (Fn(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, *this)) {
  133. #ifndef NDEBUG
  134. dbgs() << "Call operand #" << i << " has unhandled type "
  135. << EVT(ArgVT).getEVTString() << '\n';
  136. #endif
  137. llvm_unreachable(0);
  138. }
  139. }
  140. }
  141. /// AnalyzeCallResult - Analyze the return values of a call,
  142. /// incorporating info about the passed values into this state.
  143. void CCState::AnalyzeCallResult(const SmallVectorImpl<ISD::InputArg> &Ins,
  144. CCAssignFn Fn) {
  145. for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
  146. MVT VT = Ins[i].VT;
  147. ISD::ArgFlagsTy Flags = Ins[i].Flags;
  148. if (Fn(i, VT, VT, CCValAssign::Full, Flags, *this)) {
  149. #ifndef NDEBUG
  150. dbgs() << "Call result #" << i << " has unhandled type "
  151. << EVT(VT).getEVTString() << '\n';
  152. #endif
  153. llvm_unreachable(0);
  154. }
  155. }
  156. }
  157. /// AnalyzeCallResult - Same as above except it's specialized for calls which
  158. /// 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(0);
  166. }
  167. }