LLVM API Documentation
00001 //===-- CallingConvLower.cpp - Calling Conventions ------------------------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file implements the CCState class, used for lowering and implementing 00011 // calling conventions. 00012 // 00013 //===----------------------------------------------------------------------===// 00014 00015 #include "llvm/CodeGen/CallingConvLower.h" 00016 #include "llvm/CodeGen/MachineFrameInfo.h" 00017 #include "llvm/IR/DataLayout.h" 00018 #include "llvm/Support/Debug.h" 00019 #include "llvm/Support/ErrorHandling.h" 00020 #include "llvm/Support/raw_ostream.h" 00021 #include "llvm/Target/TargetLowering.h" 00022 #include "llvm/Target/TargetMachine.h" 00023 #include "llvm/Target/TargetRegisterInfo.h" 00024 #include "llvm/Target/TargetSubtargetInfo.h" 00025 using namespace llvm; 00026 00027 CCState::CCState(CallingConv::ID CC, bool isVarArg, MachineFunction &mf, 00028 SmallVectorImpl<CCValAssign> &locs, LLVMContext &C) 00029 : CallingConv(CC), IsVarArg(isVarArg), MF(mf), 00030 TRI(*MF.getSubtarget().getRegisterInfo()), Locs(locs), Context(C), 00031 CallOrPrologue(Unknown) { 00032 // No stack is used. 00033 StackOffset = 0; 00034 00035 clearByValRegsInfo(); 00036 UsedRegs.resize((TRI.getNumRegs()+31)/32); 00037 } 00038 00039 // HandleByVal - Allocate space on the stack large enough to pass an argument 00040 // by value. The size and alignment information of the argument is encoded in 00041 // its parameter attribute. 00042 void CCState::HandleByVal(unsigned ValNo, MVT ValVT, 00043 MVT LocVT, CCValAssign::LocInfo LocInfo, 00044 int MinSize, int MinAlign, 00045 ISD::ArgFlagsTy ArgFlags) { 00046 unsigned Align = ArgFlags.getByValAlign(); 00047 unsigned Size = ArgFlags.getByValSize(); 00048 if (MinSize > (int)Size) 00049 Size = MinSize; 00050 if (MinAlign > (int)Align) 00051 Align = MinAlign; 00052 MF.getFrameInfo()->ensureMaxAlignment(Align); 00053 MF.getSubtarget().getTargetLowering()->HandleByVal(this, Size, Align); 00054 Size = unsigned(RoundUpToAlignment(Size, MinAlign)); 00055 unsigned Offset = AllocateStack(Size, Align); 00056 addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo)); 00057 } 00058 00059 /// MarkAllocated - Mark a register and all of its aliases as allocated. 00060 void CCState::MarkAllocated(unsigned Reg) { 00061 for (MCRegAliasIterator AI(Reg, &TRI, true); AI.isValid(); ++AI) 00062 UsedRegs[*AI/32] |= 1 << (*AI&31); 00063 } 00064 00065 /// AnalyzeFormalArguments - Analyze an array of argument values, 00066 /// incorporating info about the formals into this state. 00067 void 00068 CCState::AnalyzeFormalArguments(const SmallVectorImpl<ISD::InputArg> &Ins, 00069 CCAssignFn Fn) { 00070 unsigned NumArgs = Ins.size(); 00071 00072 for (unsigned i = 0; i != NumArgs; ++i) { 00073 MVT ArgVT = Ins[i].VT; 00074 ISD::ArgFlagsTy ArgFlags = Ins[i].Flags; 00075 if (Fn(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, *this)) { 00076 #ifndef NDEBUG 00077 dbgs() << "Formal argument #" << i << " has unhandled type " 00078 << EVT(ArgVT).getEVTString() << '\n'; 00079 #endif 00080 llvm_unreachable(nullptr); 00081 } 00082 } 00083 } 00084 00085 /// CheckReturn - Analyze the return values of a function, returning true if 00086 /// the return can be performed without sret-demotion, and false otherwise. 00087 bool CCState::CheckReturn(const SmallVectorImpl<ISD::OutputArg> &Outs, 00088 CCAssignFn Fn) { 00089 // Determine which register each value should be copied into. 00090 for (unsigned i = 0, e = Outs.size(); i != e; ++i) { 00091 MVT VT = Outs[i].VT; 00092 ISD::ArgFlagsTy ArgFlags = Outs[i].Flags; 00093 if (Fn(i, VT, VT, CCValAssign::Full, ArgFlags, *this)) 00094 return false; 00095 } 00096 return true; 00097 } 00098 00099 /// AnalyzeReturn - Analyze the returned values of a return, 00100 /// incorporating info about the result values into this state. 00101 void CCState::AnalyzeReturn(const SmallVectorImpl<ISD::OutputArg> &Outs, 00102 CCAssignFn Fn) { 00103 // Determine which register each value should be copied into. 00104 for (unsigned i = 0, e = Outs.size(); i != e; ++i) { 00105 MVT VT = Outs[i].VT; 00106 ISD::ArgFlagsTy ArgFlags = Outs[i].Flags; 00107 if (Fn(i, VT, VT, CCValAssign::Full, ArgFlags, *this)) { 00108 #ifndef NDEBUG 00109 dbgs() << "Return operand #" << i << " has unhandled type " 00110 << EVT(VT).getEVTString() << '\n'; 00111 #endif 00112 llvm_unreachable(nullptr); 00113 } 00114 } 00115 } 00116 00117 /// AnalyzeCallOperands - Analyze the outgoing arguments to a call, 00118 /// incorporating info about the passed values into this state. 00119 void CCState::AnalyzeCallOperands(const SmallVectorImpl<ISD::OutputArg> &Outs, 00120 CCAssignFn Fn) { 00121 unsigned NumOps = Outs.size(); 00122 for (unsigned i = 0; i != NumOps; ++i) { 00123 MVT ArgVT = Outs[i].VT; 00124 ISD::ArgFlagsTy ArgFlags = Outs[i].Flags; 00125 if (Fn(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, *this)) { 00126 #ifndef NDEBUG 00127 dbgs() << "Call operand #" << i << " has unhandled type " 00128 << EVT(ArgVT).getEVTString() << '\n'; 00129 #endif 00130 llvm_unreachable(nullptr); 00131 } 00132 } 00133 } 00134 00135 /// AnalyzeCallOperands - Same as above except it takes vectors of types 00136 /// and argument flags. 00137 void CCState::AnalyzeCallOperands(SmallVectorImpl<MVT> &ArgVTs, 00138 SmallVectorImpl<ISD::ArgFlagsTy> &Flags, 00139 CCAssignFn Fn) { 00140 unsigned NumOps = ArgVTs.size(); 00141 for (unsigned i = 0; i != NumOps; ++i) { 00142 MVT ArgVT = ArgVTs[i]; 00143 ISD::ArgFlagsTy ArgFlags = Flags[i]; 00144 if (Fn(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, *this)) { 00145 #ifndef NDEBUG 00146 dbgs() << "Call operand #" << i << " has unhandled type " 00147 << EVT(ArgVT).getEVTString() << '\n'; 00148 #endif 00149 llvm_unreachable(nullptr); 00150 } 00151 } 00152 } 00153 00154 /// AnalyzeCallResult - Analyze the return values of a call, 00155 /// incorporating info about the passed values into this state. 00156 void CCState::AnalyzeCallResult(const SmallVectorImpl<ISD::InputArg> &Ins, 00157 CCAssignFn Fn) { 00158 for (unsigned i = 0, e = Ins.size(); i != e; ++i) { 00159 MVT VT = Ins[i].VT; 00160 ISD::ArgFlagsTy Flags = Ins[i].Flags; 00161 if (Fn(i, VT, VT, CCValAssign::Full, Flags, *this)) { 00162 #ifndef NDEBUG 00163 dbgs() << "Call result #" << i << " has unhandled type " 00164 << EVT(VT).getEVTString() << '\n'; 00165 #endif 00166 llvm_unreachable(nullptr); 00167 } 00168 } 00169 } 00170 00171 /// AnalyzeCallResult - Same as above except it's specialized for calls which 00172 /// produce a single value. 00173 void CCState::AnalyzeCallResult(MVT VT, CCAssignFn Fn) { 00174 if (Fn(0, VT, VT, CCValAssign::Full, ISD::ArgFlagsTy(), *this)) { 00175 #ifndef NDEBUG 00176 dbgs() << "Call result has unhandled type " 00177 << EVT(VT).getEVTString() << '\n'; 00178 #endif 00179 llvm_unreachable(nullptr); 00180 } 00181 }