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