llvm.org GIT mirror llvm / 137f4d9
CodeGen: Factor out code for tail call result compatibility check; NFC git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@264959 91177308-0d34-0410-b5e6-96231b3b80d8 Matthias Braun 4 years ago
5 changed file(s) with 71 addition(s) and 105 deletion(s). Raw diff Collapse all Expand all
512512 SmallVectorImpl &Forwards, ArrayRef RegParmTypes,
513513 CCAssignFn Fn);
514514
515 /// Returns true if the results of the two calling conventions are compatible.
516 /// This is usually part of the check for tailcall eligibility.
517 static bool resultsCompatible(CallingConv::ID CalleeCC,
518 CallingConv::ID CallerCC, MachineFunction &MF,
519 LLVMContext &C,
520 const SmallVectorImpl &Ins,
521 CCAssignFn CalleeFn, CCAssignFn CallerFn);
522
515523 private:
516524 /// MarkAllocated - Mark a register and all of its aliases as allocated.
517525 void MarkAllocated(unsigned Reg);
248248 }
249249 }
250250 }
251
252 bool CCState::resultsCompatible(CallingConv::ID CalleeCC,
253 CallingConv::ID CallerCC, MachineFunction &MF,
254 LLVMContext &C,
255 const SmallVectorImpl &Ins,
256 CCAssignFn CalleeFn, CCAssignFn CallerFn) {
257 if (CalleeCC == CallerCC)
258 return true;
259 SmallVector RVLocs1;
260 CCState CCInfo1(CalleeCC, false, MF, RVLocs1, C);
261 CCInfo1.AnalyzeCallResult(Ins, CalleeFn);
262
263 SmallVector RVLocs2;
264 CCState CCInfo2(CallerCC, false, MF, RVLocs2, C);
265 CCInfo2.AnalyzeCallResult(Ins, CallerFn);
266
267 if (RVLocs1.size() != RVLocs2.size())
268 return false;
269 for (unsigned I = 0, E = RVLocs1.size(); I != E; ++I) {
270 const CCValAssign &Loc1 = RVLocs1[I];
271 const CCValAssign &Loc2 = RVLocs2[I];
272 if (Loc1.getLocInfo() != Loc2.getLocInfo())
273 return false;
274 bool RegLoc1 = Loc1.isRegLoc();
275 if (RegLoc1 != Loc2.isRegLoc())
276 return false;
277 if (RegLoc1) {
278 if (Loc1.getLocReg() != Loc2.getLocReg())
279 return false;
280 } else {
281 if (Loc1.getLocMemOffset() != Loc2.getLocMemOffset())
282 return false;
283 }
284 }
285 return true;
286 }
28112811 if (!IsTailCallConvention(CalleeCC) && CalleeCC != CallingConv::C)
28122812 return false;
28132813
2814 const MachineFunction &MF = DAG.getMachineFunction();
2814 MachineFunction &MF = DAG.getMachineFunction();
28152815 const Function *CallerF = MF.getFunction();
28162816 CallingConv::ID CallerCC = CallerF->getCallingConv();
28172817 bool CCMatch = CallerCC == CalleeCC;
28602860 assert((!isVarArg || CalleeCC == CallingConv::C) &&
28612861 "Unexpected variadic calling convention");
28622862
2863 LLVMContext &C = *DAG.getContext();
28632864 if (isVarArg && !Outs.empty()) {
28642865 // At least two cases here: if caller is fastcc then we can't have any
28652866 // memory arguments (we'd be expected to clean up the stack afterwards). If
28682869 // FIXME: for now we take the most conservative of these in both cases:
28692870 // disallow all variadic memory operands.
28702871 SmallVector ArgLocs;
2871 CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(), ArgLocs,
2872 *DAG.getContext());
2872 CCState CCInfo(CalleeCC, isVarArg, MF, ArgLocs, C);
28732873
28742874 CCInfo.AnalyzeCallOperands(Outs, CCAssignFnForCall(CalleeCC, true));
28752875 for (const CCValAssign &ArgLoc : ArgLocs)
28772877 return false;
28782878 }
28792879
2880 // If the calling conventions do not match, then we'd better make sure the
2881 // results are returned in the same way as what the caller expects.
2882 if (!CCMatch) {
2883 SmallVector RVLocs1;
2884 CCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(), RVLocs1,
2885 *DAG.getContext());
2886 CCInfo1.AnalyzeCallResult(Ins, CCAssignFnForCall(CalleeCC, isVarArg));
2887
2888 SmallVector RVLocs2;
2889 CCState CCInfo2(CallerCC, false, DAG.getMachineFunction(), RVLocs2,
2890 *DAG.getContext());
2891 CCInfo2.AnalyzeCallResult(Ins, CCAssignFnForCall(CallerCC, isVarArg));
2892
2893 if (RVLocs1.size() != RVLocs2.size())
2894 return false;
2895 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
2896 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
2897 return false;
2898 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
2899 return false;
2900 if (RVLocs1[i].isRegLoc()) {
2901 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
2902 return false;
2903 } else {
2904 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
2905 return false;
2906 }
2907 }
2908 }
2880 // Check that the call results are passed in the same way.
2881 if (!CCState::resultsCompatible(CalleeCC, CallerCC, MF, C, Ins,
2882 CCAssignFnForCall(CalleeCC, isVarArg),
2883 CCAssignFnForCall(CallerCC, isVarArg)))
2884 return false;
29092885
29102886 // Nothing more to check if the callee is taking no arguments
29112887 if (Outs.empty())
29122888 return true;
29132889
29142890 SmallVector ArgLocs;
2915 CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(), ArgLocs,
2916 *DAG.getContext());
2891 CCState CCInfo(CalleeCC, isVarArg, MF, ArgLocs, C);
29172892
29182893 CCInfo.AnalyzeCallOperands(Outs, CCAssignFnForCall(CalleeCC, isVarArg));
29192894
20992099 const SmallVectorImpl &OutVals,
21002100 const SmallVectorImpl &Ins,
21012101 SelectionDAG& DAG) const {
2102 const Function *CallerF = DAG.getMachineFunction().getFunction();
2102 MachineFunction &MF = DAG.getMachineFunction();
2103 const Function *CallerF = MF.getFunction();
21032104 CallingConv::ID CallerCC = CallerF->getCallingConv();
21042105 bool CCMatch = CallerCC == CalleeCC;
21052106
21462147 return false;
21472148 }
21482149
2149 // If the calling conventions do not match, then we'd better make sure the
2150 // results are returned in the same way as what the caller expects.
2151 if (!CCMatch) {
2152 SmallVector RVLocs1;
2153 ARMCCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(), RVLocs1,
2154 *DAG.getContext(), Call);
2155 CCInfo1.AnalyzeCallResult(Ins, CCAssignFnForNode(CalleeCC, true, isVarArg));
2156
2157 SmallVector RVLocs2;
2158 ARMCCState CCInfo2(CallerCC, false, DAG.getMachineFunction(), RVLocs2,
2159 *DAG.getContext(), Call);
2160 CCInfo2.AnalyzeCallResult(Ins, CCAssignFnForNode(CallerCC, true, isVarArg));
2161
2162 if (RVLocs1.size() != RVLocs2.size())
2163 return false;
2164 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
2165 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
2166 return false;
2167 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
2168 return false;
2169 if (RVLocs1[i].isRegLoc()) {
2170 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
2171 return false;
2172 } else {
2173 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
2174 return false;
2175 }
2176 }
2177 }
2150 // Check that the call results are passed in the same way.
2151 LLVMContext &C = *DAG.getContext();
2152 if (!CCState::resultsCompatible(CalleeCC, CallerCC, MF, C, Ins,
2153 CCAssignFnForNode(CalleeCC, true, isVarArg),
2154 CCAssignFnForNode(CallerCC, true, isVarArg)))
2155 return false;
21782156
21792157 // If Caller's vararg or byval argument has been split between registers and
21802158 // stack, do not perform tail call, since part of the argument is in caller's
21812159 // local frame.
2182 const ARMFunctionInfo *AFI_Caller = DAG.getMachineFunction().
2183 getInfo();
2160 const ARMFunctionInfo *AFI_Caller = MF.getInfo();
21842161 if (AFI_Caller->getArgRegsSaveSize())
21852162 return false;
21862163
21902167 // Check if stack adjustment is needed. For now, do not do this if any
21912168 // argument is passed on the stack.
21922169 SmallVector ArgLocs;
2193 ARMCCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(), ArgLocs,
2194 *DAG.getContext(), Call);
2170 ARMCCState CCInfo(CalleeCC, isVarArg, MF, ArgLocs, C, Call);
21952171 CCInfo.AnalyzeCallOperands(Outs,
21962172 CCAssignFnForNode(CalleeCC, false, isVarArg));
21972173 if (CCInfo.getNextStackOffset()) {
2198 MachineFunction &MF = DAG.getMachineFunction();
2199
22002174 // Check if the arguments are already laid out in the right way as
22012175 // the caller's fixed stack objects.
22022176 MachineFrameInfo *MFI = MF.getFrameInfo();
38283828
38293829 // Do not sibcall optimize vararg calls unless all arguments are passed via
38303830 // registers.
3831 LLVMContext &C = *DAG.getContext();
38313832 if (isVarArg && !Outs.empty()) {
38323833 // Optimizing for varargs on Win64 is unlikely to be safe without
38333834 // additional testing.
38353836 return false;
38363837
38373838 SmallVector ArgLocs;
3838 CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(), ArgLocs,
3839 *DAG.getContext());
3839 CCState CCInfo(CalleeCC, isVarArg, MF, ArgLocs, C);
38403840
38413841 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
38423842 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i)
38563856 }
38573857 if (Unused) {
38583858 SmallVector RVLocs;
3859 CCState CCInfo(CalleeCC, false, DAG.getMachineFunction(), RVLocs,
3860 *DAG.getContext());
3859 CCState CCInfo(CalleeCC, false, MF, RVLocs, C);
38613860 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
38623861 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
38633862 CCValAssign &VA = RVLocs[i];
38663865 }
38673866 }
38683867
3869 // If the calling conventions do not match, then we'd better make sure the
3870 // results are returned in the same way as what the caller expects.
3871 if (!CCMatch) {
3872 SmallVector RVLocs1;
3873 CCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(), RVLocs1,
3874 *DAG.getContext());
3875 CCInfo1.AnalyzeCallResult(Ins, RetCC_X86);
3876
3877 SmallVector RVLocs2;
3878 CCState CCInfo2(CallerCC, false, DAG.getMachineFunction(), RVLocs2,
3879 *DAG.getContext());
3880 CCInfo2.AnalyzeCallResult(Ins, RetCC_X86);
3881
3882 if (RVLocs1.size() != RVLocs2.size())
3883 return false;
3884 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
3885 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
3886 return false;
3887 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
3888 return false;
3889 if (RVLocs1[i].isRegLoc()) {
3890 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
3891 return false;
3892 } else {
3893 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
3894 return false;
3895 }
3896 }
3897 }
3868 // Check that the call results are passed in the same way.
3869 if (!CCState::resultsCompatible(CalleeCC, CallerCC, MF, C, Ins,
3870 RetCC_X86, RetCC_X86))
3871 return false;
38983872
38993873 unsigned StackArgsSize = 0;
39003874
39043878 // Check if stack adjustment is needed. For now, do not do this if any
39053879 // argument is passed on the stack.
39063880 SmallVector ArgLocs;
3907 CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(), ArgLocs,
3908 *DAG.getContext());
3881 CCState CCInfo(CalleeCC, isVarArg, MF, ArgLocs, C);
39093882
39103883 // Allocate shadow area for Win64
39113884 if (IsCalleeWin64)