Dan Gohman | 456e281 | 2009-03-31 16:51:18 +0000 | [diff] [blame] | 1 | //===-- CallingConvLower.cpp - Calling Conventions ------------------------===// |
Chris Lattner | 362e98a | 2007-02-27 04:43:02 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Chris Lattner | 362e98a | 2007-02-27 04:43:02 +0000 | [diff] [blame] | 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" |
Eric Christopher | 471e422 | 2011-06-08 23:55:35 +0000 | [diff] [blame] | 16 | #include "llvm/CodeGen/MachineFrameInfo.h" |
David Greene | ad61b12 | 2010-01-05 01:24:50 +0000 | [diff] [blame] | 17 | #include "llvm/Support/Debug.h" |
Torok Edwin | 7d696d8 | 2009-07-11 13:10:19 +0000 | [diff] [blame] | 18 | #include "llvm/Support/ErrorHandling.h" |
| 19 | #include "llvm/Support/raw_ostream.h" |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 20 | #include "llvm/Target/TargetRegisterInfo.h" |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 21 | #include "llvm/DataLayout.h" |
Chris Lattner | 13513b7 | 2007-02-27 05:13:54 +0000 | [diff] [blame] | 22 | #include "llvm/Target/TargetMachine.h" |
Stuart Hastings | f222e59 | 2011-02-28 17:17:53 +0000 | [diff] [blame] | 23 | #include "llvm/Target/TargetLowering.h" |
Chris Lattner | 362e98a | 2007-02-27 04:43:02 +0000 | [diff] [blame] | 24 | using namespace llvm; |
| 25 | |
Eric Christopher | 471e422 | 2011-06-08 23:55:35 +0000 | [diff] [blame] | 26 | CCState::CCState(CallingConv::ID CC, bool isVarArg, MachineFunction &mf, |
Cameron Zwarich | a0f803a | 2011-06-10 20:31:39 +0000 | [diff] [blame] | 27 | const TargetMachine &tm, SmallVector<CCValAssign, 16> &locs, |
| 28 | LLVMContext &C) |
Eric Christopher | 471e422 | 2011-06-08 23:55:35 +0000 | [diff] [blame] | 29 | : CallingConv(CC), IsVarArg(isVarArg), MF(mf), TM(tm), |
Stuart Hastings | c731587 | 2011-04-20 16:47:52 +0000 | [diff] [blame] | 30 | TRI(*TM.getRegisterInfo()), Locs(locs), Context(C), |
Cameron Zwarich | a50760f | 2011-06-10 20:37:36 +0000 | [diff] [blame] | 31 | CallOrPrologue(Unknown) { |
Chris Lattner | 362e98a | 2007-02-27 04:43:02 +0000 | [diff] [blame] | 32 | // No stack is used. |
| 33 | StackOffset = 0; |
Eric Christopher | 471e422 | 2011-06-08 23:55:35 +0000 | [diff] [blame] | 34 | |
Stuart Hastings | c731587 | 2011-04-20 16:47:52 +0000 | [diff] [blame] | 35 | clearFirstByValReg(); |
Dan Gohman | 117b64b | 2008-06-30 20:25:31 +0000 | [diff] [blame] | 36 | UsedRegs.resize((TRI.getNumRegs()+31)/32); |
Chris Lattner | 362e98a | 2007-02-27 04:43:02 +0000 | [diff] [blame] | 37 | } |
| 38 | |
Eric Christopher | 471e422 | 2011-06-08 23:55:35 +0000 | [diff] [blame] | 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. |
Duncan Sands | 1e96bab | 2010-11-04 10:49:57 +0000 | [diff] [blame] | 42 | void CCState::HandleByVal(unsigned ValNo, MVT ValVT, |
Duncan Sands | 1440e8b | 2010-11-03 11:35:31 +0000 | [diff] [blame] | 43 | MVT LocVT, CCValAssign::LocInfo LocInfo, |
Evan Cheng | 4c46fc6 | 2008-01-15 07:49:36 +0000 | [diff] [blame] | 44 | int MinSize, int MinAlign, |
Duncan Sands | 276dcbd | 2008-03-21 09:14:45 +0000 | [diff] [blame] | 45 | ISD::ArgFlagsTy ArgFlags) { |
| 46 | unsigned Align = ArgFlags.getByValAlign(); |
| 47 | unsigned Size = ArgFlags.getByValSize(); |
Evan Cheng | 4c46fc6 | 2008-01-15 07:49:36 +0000 | [diff] [blame] | 48 | if (MinSize > (int)Size) |
| 49 | Size = MinSize; |
| 50 | if (MinAlign > (int)Align) |
| 51 | Align = MinAlign; |
Chad Rosier | 2531a64 | 2012-06-19 22:59:12 +0000 | [diff] [blame] | 52 | MF.getFrameInfo()->ensureMaxAlignment(Align); |
Stepan Dyatkovskiy | b52ba9f | 2012-10-16 07:16:47 +0000 | [diff] [blame] | 53 | TM.getTargetLowering()->HandleByVal(this, Size, Align); |
Stuart Hastings | 2aa0f23 | 2011-05-26 04:09:49 +0000 | [diff] [blame] | 54 | unsigned Offset = AllocateStack(Size, Align); |
| 55 | addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo)); |
Rafael Espindola | 594d37e | 2007-08-10 14:44:42 +0000 | [diff] [blame] | 56 | } |
Chris Lattner | 362e98a | 2007-02-27 04:43:02 +0000 | [diff] [blame] | 57 | |
| 58 | /// MarkAllocated - Mark a register and all of its aliases as allocated. |
| 59 | void CCState::MarkAllocated(unsigned Reg) { |
Jakob Stoklund Olesen | 396618b | 2012-06-01 23:28:30 +0000 | [diff] [blame] | 60 | for (MCRegAliasIterator AI(Reg, &TRI, true); AI.isValid(); ++AI) |
| 61 | UsedRegs[*AI/32] |= 1 << (*AI&31); |
Chris Lattner | 362e98a | 2007-02-27 04:43:02 +0000 | [diff] [blame] | 62 | } |
Chris Lattner | fb39f99 | 2007-02-28 06:56:37 +0000 | [diff] [blame] | 63 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 64 | /// AnalyzeFormalArguments - Analyze an array of argument values, |
Chris Lattner | 66baf26 | 2007-02-28 07:09:40 +0000 | [diff] [blame] | 65 | /// incorporating info about the formals into this state. |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 66 | void |
| 67 | CCState::AnalyzeFormalArguments(const SmallVectorImpl<ISD::InputArg> &Ins, |
| 68 | CCAssignFn Fn) { |
| 69 | unsigned NumArgs = Ins.size(); |
| 70 | |
Chris Lattner | 66baf26 | 2007-02-28 07:09:40 +0000 | [diff] [blame] | 71 | for (unsigned i = 0; i != NumArgs; ++i) { |
Duncan Sands | 1440e8b | 2010-11-03 11:35:31 +0000 | [diff] [blame] | 72 | MVT ArgVT = Ins[i].VT; |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 73 | ISD::ArgFlagsTy ArgFlags = Ins[i].Flags; |
Chris Lattner | 66baf26 | 2007-02-28 07:09:40 +0000 | [diff] [blame] | 74 | if (Fn(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, *this)) { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 75 | #ifndef NDEBUG |
David Greene | ad61b12 | 2010-01-05 01:24:50 +0000 | [diff] [blame] | 76 | dbgs() << "Formal argument #" << i << " has unhandled type " |
Craig Topper | 32631d1 | 2012-11-14 05:20:09 +0000 | [diff] [blame^] | 77 | << EVT(ArgVT).getEVTString() << '\n'; |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 78 | #endif |
| 79 | llvm_unreachable(0); |
Chris Lattner | 66baf26 | 2007-02-28 07:09:40 +0000 | [diff] [blame] | 80 | } |
| 81 | } |
| 82 | } |
| 83 | |
Kenneth Uildriks | b4997ae | 2009-11-07 02:11:54 +0000 | [diff] [blame] | 84 | /// CheckReturn - Analyze the return values of a function, returning true if |
| 85 | /// the return can be performed without sret-demotion, and false otherwise. |
Dan Gohman | 84023e0 | 2010-07-10 09:00:22 +0000 | [diff] [blame] | 86 | bool CCState::CheckReturn(const SmallVectorImpl<ISD::OutputArg> &Outs, |
Kenneth Uildriks | b4997ae | 2009-11-07 02:11:54 +0000 | [diff] [blame] | 87 | CCAssignFn Fn) { |
| 88 | // Determine which register each value should be copied into. |
Dan Gohman | 84023e0 | 2010-07-10 09:00:22 +0000 | [diff] [blame] | 89 | for (unsigned i = 0, e = Outs.size(); i != e; ++i) { |
Duncan Sands | 1440e8b | 2010-11-03 11:35:31 +0000 | [diff] [blame] | 90 | MVT VT = Outs[i].VT; |
Dan Gohman | 84023e0 | 2010-07-10 09:00:22 +0000 | [diff] [blame] | 91 | ISD::ArgFlagsTy ArgFlags = Outs[i].Flags; |
Kenneth Uildriks | b4997ae | 2009-11-07 02:11:54 +0000 | [diff] [blame] | 92 | if (Fn(i, VT, VT, CCValAssign::Full, ArgFlags, *this)) |
| 93 | return false; |
| 94 | } |
| 95 | return true; |
| 96 | } |
| 97 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 98 | /// AnalyzeReturn - Analyze the returned values of a return, |
Chris Lattner | 66baf26 | 2007-02-28 07:09:40 +0000 | [diff] [blame] | 99 | /// incorporating info about the result values into this state. |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 100 | void CCState::AnalyzeReturn(const SmallVectorImpl<ISD::OutputArg> &Outs, |
| 101 | CCAssignFn Fn) { |
Chris Lattner | 66baf26 | 2007-02-28 07:09:40 +0000 | [diff] [blame] | 102 | // Determine which register each value should be copied into. |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 103 | for (unsigned i = 0, e = Outs.size(); i != e; ++i) { |
Duncan Sands | 1440e8b | 2010-11-03 11:35:31 +0000 | [diff] [blame] | 104 | MVT VT = Outs[i].VT; |
Dan Gohman | aa8c194 | 2010-07-06 15:39:54 +0000 | [diff] [blame] | 105 | ISD::ArgFlagsTy ArgFlags = Outs[i].Flags; |
| 106 | if (Fn(i, VT, VT, CCValAssign::Full, ArgFlags, *this)) { |
| 107 | #ifndef NDEBUG |
| 108 | dbgs() << "Return operand #" << i << " has unhandled type " |
Craig Topper | 32631d1 | 2012-11-14 05:20:09 +0000 | [diff] [blame^] | 109 | << EVT(VT).getEVTString() << '\n'; |
Dan Gohman | aa8c194 | 2010-07-06 15:39:54 +0000 | [diff] [blame] | 110 | #endif |
| 111 | llvm_unreachable(0); |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 116 | /// AnalyzeCallOperands - Analyze the outgoing arguments to a call, |
| 117 | /// incorporating info about the passed values into this state. |
| 118 | void CCState::AnalyzeCallOperands(const SmallVectorImpl<ISD::OutputArg> &Outs, |
| 119 | CCAssignFn Fn) { |
| 120 | unsigned NumOps = Outs.size(); |
Chris Lattner | fb39f99 | 2007-02-28 06:56:37 +0000 | [diff] [blame] | 121 | for (unsigned i = 0; i != NumOps; ++i) { |
Duncan Sands | 1440e8b | 2010-11-03 11:35:31 +0000 | [diff] [blame] | 122 | MVT ArgVT = Outs[i].VT; |
Dan Gohman | aa8c194 | 2010-07-06 15:39:54 +0000 | [diff] [blame] | 123 | ISD::ArgFlagsTy ArgFlags = Outs[i].Flags; |
| 124 | if (Fn(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, *this)) { |
| 125 | #ifndef NDEBUG |
| 126 | dbgs() << "Call operand #" << i << " has unhandled type " |
Craig Topper | 32631d1 | 2012-11-14 05:20:09 +0000 | [diff] [blame^] | 127 | << EVT(ArgVT).getEVTString() << '\n'; |
Dan Gohman | aa8c194 | 2010-07-06 15:39:54 +0000 | [diff] [blame] | 128 | #endif |
| 129 | llvm_unreachable(0); |
| 130 | } |
| 131 | } |
| 132 | } |
| 133 | |
Evan Cheng | c89d2fe | 2008-09-05 16:59:26 +0000 | [diff] [blame] | 134 | /// AnalyzeCallOperands - Same as above except it takes vectors of types |
| 135 | /// and argument flags. |
Duncan Sands | 1440e8b | 2010-11-03 11:35:31 +0000 | [diff] [blame] | 136 | void CCState::AnalyzeCallOperands(SmallVectorImpl<MVT> &ArgVTs, |
Evan Cheng | c89d2fe | 2008-09-05 16:59:26 +0000 | [diff] [blame] | 137 | SmallVectorImpl<ISD::ArgFlagsTy> &Flags, |
| 138 | CCAssignFn Fn) { |
| 139 | unsigned NumOps = ArgVTs.size(); |
| 140 | for (unsigned i = 0; i != NumOps; ++i) { |
Duncan Sands | 1440e8b | 2010-11-03 11:35:31 +0000 | [diff] [blame] | 141 | MVT ArgVT = ArgVTs[i]; |
Evan Cheng | c89d2fe | 2008-09-05 16:59:26 +0000 | [diff] [blame] | 142 | ISD::ArgFlagsTy ArgFlags = Flags[i]; |
| 143 | if (Fn(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, *this)) { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 144 | #ifndef NDEBUG |
David Greene | ad61b12 | 2010-01-05 01:24:50 +0000 | [diff] [blame] | 145 | dbgs() << "Call operand #" << i << " has unhandled type " |
Craig Topper | 32631d1 | 2012-11-14 05:20:09 +0000 | [diff] [blame^] | 146 | << EVT(ArgVT).getEVTString() << '\n'; |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 147 | #endif |
| 148 | llvm_unreachable(0); |
Evan Cheng | c89d2fe | 2008-09-05 16:59:26 +0000 | [diff] [blame] | 149 | } |
| 150 | } |
| 151 | } |
| 152 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 153 | /// AnalyzeCallResult - Analyze the return values of a call, |
Chris Lattner | 66baf26 | 2007-02-28 07:09:40 +0000 | [diff] [blame] | 154 | /// incorporating info about the passed values into this state. |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 155 | void CCState::AnalyzeCallResult(const SmallVectorImpl<ISD::InputArg> &Ins, |
| 156 | CCAssignFn Fn) { |
| 157 | for (unsigned i = 0, e = Ins.size(); i != e; ++i) { |
Duncan Sands | 1440e8b | 2010-11-03 11:35:31 +0000 | [diff] [blame] | 158 | MVT VT = Ins[i].VT; |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 159 | ISD::ArgFlagsTy Flags = Ins[i].Flags; |
Dale Johannesen | 86098bd | 2008-09-26 19:31:26 +0000 | [diff] [blame] | 160 | if (Fn(i, VT, VT, CCValAssign::Full, Flags, *this)) { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 161 | #ifndef NDEBUG |
David Greene | ad61b12 | 2010-01-05 01:24:50 +0000 | [diff] [blame] | 162 | dbgs() << "Call result #" << i << " has unhandled type " |
Craig Topper | 32631d1 | 2012-11-14 05:20:09 +0000 | [diff] [blame^] | 163 | << EVT(VT).getEVTString() << '\n'; |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 164 | #endif |
| 165 | llvm_unreachable(0); |
Chris Lattner | fb39f99 | 2007-02-28 06:56:37 +0000 | [diff] [blame] | 166 | } |
| 167 | } |
| 168 | } |
Evan Cheng | c7fcfa0 | 2008-09-07 09:02:18 +0000 | [diff] [blame] | 169 | |
| 170 | /// AnalyzeCallResult - Same as above except it's specialized for calls which |
| 171 | /// produce a single value. |
Duncan Sands | 1440e8b | 2010-11-03 11:35:31 +0000 | [diff] [blame] | 172 | void CCState::AnalyzeCallResult(MVT VT, CCAssignFn Fn) { |
Evan Cheng | c7fcfa0 | 2008-09-07 09:02:18 +0000 | [diff] [blame] | 173 | if (Fn(0, VT, VT, CCValAssign::Full, ISD::ArgFlagsTy(), *this)) { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 174 | #ifndef NDEBUG |
David Greene | ad61b12 | 2010-01-05 01:24:50 +0000 | [diff] [blame] | 175 | dbgs() << "Call result has unhandled type " |
Craig Topper | 32631d1 | 2012-11-14 05:20:09 +0000 | [diff] [blame^] | 176 | << EVT(VT).getEVTString() << '\n'; |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 177 | #endif |
| 178 | llvm_unreachable(0); |
Evan Cheng | c7fcfa0 | 2008-09-07 09:02:18 +0000 | [diff] [blame] | 179 | } |
| 180 | } |