[PowerPC/BlockPlacement] Allow target to provide a per-loop alignment preference
[oota-llvm.git] / lib / CodeGen / CallingConvLower.cpp
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/CodeGen/MachineRegisterInfo.h"
18 #include "llvm/IR/DataLayout.h"
19 #include "llvm/Support/Debug.h"
20 #include "llvm/Support/ErrorHandling.h"
21 #include "llvm/Support/SaveAndRestore.h"
22 #include "llvm/Support/raw_ostream.h"
23 #include "llvm/Target/TargetLowering.h"
24 #include "llvm/Target/TargetRegisterInfo.h"
25 #include "llvm/Target/TargetSubtargetInfo.h"
26 using namespace llvm;
27
28 CCState::CCState(CallingConv::ID CC, bool isVarArg, MachineFunction &mf,
29                  SmallVectorImpl<CCValAssign> &locs, LLVMContext &C)
30     : CallingConv(CC), IsVarArg(isVarArg), MF(mf),
31       TRI(*MF.getSubtarget().getRegisterInfo()), Locs(locs), Context(C),
32       CallOrPrologue(Unknown) {
33   // No stack is used.
34   StackOffset = 0;
35
36   clearByValRegsInfo();
37   UsedRegs.resize((TRI.getNumRegs()+31)/32);
38 }
39
40 // HandleByVal - Allocate space on the stack large enough to pass an argument
41 // by value. The size and alignment information of the argument is encoded in
42 // its parameter attribute.
43 void CCState::HandleByVal(unsigned ValNo, MVT ValVT,
44                           MVT LocVT, CCValAssign::LocInfo LocInfo,
45                           int MinSize, int MinAlign,
46                           ISD::ArgFlagsTy ArgFlags) {
47   unsigned Align = ArgFlags.getByValAlign();
48   unsigned Size  = ArgFlags.getByValSize();
49   if (MinSize > (int)Size)
50     Size = MinSize;
51   if (MinAlign > (int)Align)
52     Align = MinAlign;
53   MF.getFrameInfo()->ensureMaxAlignment(Align);
54   MF.getSubtarget().getTargetLowering()->HandleByVal(this, Size, Align);
55   Size = unsigned(RoundUpToAlignment(Size, MinAlign));
56   unsigned Offset = AllocateStack(Size, Align);
57   addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
58 }
59
60 /// MarkAllocated - Mark a register and all of its aliases as allocated.
61 void CCState::MarkAllocated(unsigned Reg) {
62   for (MCRegAliasIterator AI(Reg, &TRI, true); AI.isValid(); ++AI)
63     UsedRegs[*AI/32] |= 1 << (*AI&31);
64 }
65
66 /// AnalyzeFormalArguments - Analyze an array of argument values,
67 /// incorporating info about the formals into this state.
68 void
69 CCState::AnalyzeFormalArguments(const SmallVectorImpl<ISD::InputArg> &Ins,
70                                 CCAssignFn Fn) {
71   unsigned NumArgs = Ins.size();
72
73   for (unsigned i = 0; i != NumArgs; ++i) {
74     MVT ArgVT = Ins[i].VT;
75     ISD::ArgFlagsTy ArgFlags = Ins[i].Flags;
76     if (Fn(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, *this)) {
77 #ifndef NDEBUG
78       dbgs() << "Formal argument #" << i << " has unhandled type "
79              << EVT(ArgVT).getEVTString() << '\n';
80 #endif
81       llvm_unreachable(nullptr);
82     }
83   }
84 }
85
86 /// CheckReturn - Analyze the return values of a function, returning true if
87 /// the return can be performed without sret-demotion, and false otherwise.
88 bool CCState::CheckReturn(const SmallVectorImpl<ISD::OutputArg> &Outs,
89                           CCAssignFn Fn) {
90   // Determine which register each value should be copied into.
91   for (unsigned i = 0, e = Outs.size(); i != e; ++i) {
92     MVT VT = Outs[i].VT;
93     ISD::ArgFlagsTy ArgFlags = Outs[i].Flags;
94     if (Fn(i, VT, VT, CCValAssign::Full, ArgFlags, *this))
95       return false;
96   }
97   return true;
98 }
99
100 /// AnalyzeReturn - Analyze the returned values of a return,
101 /// incorporating info about the result values into this state.
102 void CCState::AnalyzeReturn(const SmallVectorImpl<ISD::OutputArg> &Outs,
103                             CCAssignFn Fn) {
104   // Determine which register each value should be copied into.
105   for (unsigned i = 0, e = Outs.size(); i != e; ++i) {
106     MVT VT = Outs[i].VT;
107     ISD::ArgFlagsTy ArgFlags = Outs[i].Flags;
108     if (Fn(i, VT, VT, CCValAssign::Full, ArgFlags, *this)) {
109 #ifndef NDEBUG
110       dbgs() << "Return operand #" << i << " has unhandled type "
111              << EVT(VT).getEVTString() << '\n';
112 #endif
113       llvm_unreachable(nullptr);
114     }
115   }
116 }
117
118 /// AnalyzeCallOperands - Analyze the outgoing arguments to a call,
119 /// incorporating info about the passed values into this state.
120 void CCState::AnalyzeCallOperands(const SmallVectorImpl<ISD::OutputArg> &Outs,
121                                   CCAssignFn Fn) {
122   unsigned NumOps = Outs.size();
123   for (unsigned i = 0; i != NumOps; ++i) {
124     MVT ArgVT = Outs[i].VT;
125     ISD::ArgFlagsTy ArgFlags = Outs[i].Flags;
126     if (Fn(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, *this)) {
127 #ifndef NDEBUG
128       dbgs() << "Call operand #" << i << " has unhandled type "
129              << EVT(ArgVT).getEVTString() << '\n';
130 #endif
131       llvm_unreachable(nullptr);
132     }
133   }
134 }
135
136 /// AnalyzeCallOperands - Same as above except it takes vectors of types
137 /// and argument flags.
138 void CCState::AnalyzeCallOperands(SmallVectorImpl<MVT> &ArgVTs,
139                                   SmallVectorImpl<ISD::ArgFlagsTy> &Flags,
140                                   CCAssignFn Fn) {
141   unsigned NumOps = ArgVTs.size();
142   for (unsigned i = 0; i != NumOps; ++i) {
143     MVT ArgVT = ArgVTs[i];
144     ISD::ArgFlagsTy ArgFlags = Flags[i];
145     if (Fn(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, *this)) {
146 #ifndef NDEBUG
147       dbgs() << "Call operand #" << i << " has unhandled type "
148              << EVT(ArgVT).getEVTString() << '\n';
149 #endif
150       llvm_unreachable(nullptr);
151     }
152   }
153 }
154
155 /// AnalyzeCallResult - Analyze the return values of a call,
156 /// incorporating info about the passed values into this state.
157 void CCState::AnalyzeCallResult(const SmallVectorImpl<ISD::InputArg> &Ins,
158                                 CCAssignFn Fn) {
159   for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
160     MVT VT = Ins[i].VT;
161     ISD::ArgFlagsTy Flags = Ins[i].Flags;
162     if (Fn(i, VT, VT, CCValAssign::Full, Flags, *this)) {
163 #ifndef NDEBUG
164       dbgs() << "Call result #" << i << " has unhandled type "
165              << EVT(VT).getEVTString() << '\n';
166 #endif
167       llvm_unreachable(nullptr);
168     }
169   }
170 }
171
172 /// AnalyzeCallResult - Same as above except it's specialized for calls which
173 /// produce a single value.
174 void CCState::AnalyzeCallResult(MVT VT, CCAssignFn Fn) {
175   if (Fn(0, VT, VT, CCValAssign::Full, ISD::ArgFlagsTy(), *this)) {
176 #ifndef NDEBUG
177     dbgs() << "Call result has unhandled type "
178            << EVT(VT).getEVTString() << '\n';
179 #endif
180     llvm_unreachable(nullptr);
181   }
182 }
183
184 void CCState::getRemainingRegParmsForType(SmallVectorImpl<MCPhysReg> &Regs,
185                                           MVT VT, CCAssignFn Fn) {
186   unsigned SavedStackOffset = StackOffset;
187   unsigned NumLocs = Locs.size();
188
189   // Allocate something of this value type repeatedly with just the inreg flag
190   // set until we get assigned a location in memory.
191   ISD::ArgFlagsTy Flags;
192   Flags.setInReg();
193   bool HaveRegParm = true;
194   while (HaveRegParm) {
195     if (Fn(0, VT, VT, CCValAssign::Full, Flags, *this)) {
196 #ifndef NDEBUG
197       dbgs() << "Call has unhandled type " << EVT(VT).getEVTString()
198              << " while computing remaining regparms\n";
199 #endif
200       llvm_unreachable(nullptr);
201     }
202     HaveRegParm = Locs.back().isRegLoc();
203   }
204
205   // Copy all the registers from the value locations we added.
206   assert(NumLocs < Locs.size() && "CC assignment failed to add location");
207   for (unsigned I = NumLocs, E = Locs.size(); I != E; ++I)
208     if (Locs[I].isRegLoc())
209       Regs.push_back(MCPhysReg(Locs[I].getLocReg()));
210
211   // Clear the assigned values and stack memory. We leave the registers marked
212   // as allocated so that future queries don't return the same registers, i.e.
213   // when i64 and f64 are both passed in GPRs.
214   StackOffset = SavedStackOffset;
215   Locs.resize(NumLocs);
216 }
217
218 void CCState::analyzeMustTailForwardedRegisters(
219     SmallVectorImpl<ForwardedRegister> &Forwards, ArrayRef<MVT> RegParmTypes,
220     CCAssignFn Fn) {
221   // Oftentimes calling conventions will not user register parameters for
222   // variadic functions, so we need to assume we're not variadic so that we get
223   // all the registers that might be used in a non-variadic call.
224   SaveAndRestore<bool> SavedVarArg(IsVarArg, false);
225
226   for (MVT RegVT : RegParmTypes) {
227     SmallVector<MCPhysReg, 8> RemainingRegs;
228     getRemainingRegParmsForType(RemainingRegs, RegVT, Fn);
229     const TargetLowering *TL = MF.getSubtarget().getTargetLowering();
230     const TargetRegisterClass *RC = TL->getRegClassFor(RegVT);
231     for (MCPhysReg PReg : RemainingRegs) {
232       unsigned VReg = MF.addLiveIn(PReg, RC);
233       Forwards.push_back(ForwardedRegister(VReg, PReg, RegVT));
234     }
235   }
236 }