diff --git a/lib/Target/X86/X86ISelLowering.cpp b/lib/Target/X86/X86ISelLowering.cpp index 78600804f68..1317510740b 100644 --- a/lib/Target/X86/X86ISelLowering.cpp +++ b/lib/Target/X86/X86ISelLowering.cpp @@ -393,7 +393,8 @@ X86TargetLowering::LowerCallTo(SDOperand Chain, const Type *RetTy, if (CallingConv == CallingConv::Fast && EnableFastCC) return LowerFastCCCallTo(Chain, RetTy, isTailCall, Callee, Args, DAG); - return LowerCCCCallTo(Chain, RetTy, isVarArg, isTailCall, Callee, Args, DAG); + return LowerCCCCallTo(Chain, RetTy, isVarArg, isTailCall, CallingConv, + Callee, Args, DAG); } //===----------------------------------------------------------------------===// @@ -520,6 +521,12 @@ void X86TargetLowering::PreprocessCCCArguments(std::vector &Args, ReturnAddrIndex = 0; // No return address slot generated yet. BytesToPopOnReturn = 0; // Callee pops nothing. BytesCallerReserves = ArgOffset; + + // If this is a struct return on Darwin/X86, the callee pops the hidden struct + // pointer. + if (F.getCallingConv() == CallingConv::CSRet && + Subtarget->isTargetDarwin()) + BytesToPopOnReturn = 4; } void X86TargetLowering::LowerCCCArguments(SDOperand Op, SelectionDAG &DAG) { @@ -551,6 +558,7 @@ void X86TargetLowering::LowerCCCArguments(SDOperand Op, SelectionDAG &DAG) { std::pair X86TargetLowering::LowerCCCCallTo(SDOperand Chain, const Type *RetTy, bool isVarArg, bool isTailCall, + unsigned CallingConv, SDOperand Callee, ArgListTy &Args, SelectionDAG &DAG) { // Count how many bytes are to be pushed on the stack. @@ -704,13 +712,21 @@ X86TargetLowering::LowerCCCCallTo(SDOperand Chain, const Type *RetTy, Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops); InFlag = Chain.getValue(1); + // Create the CALLSEQ_END node. + unsigned NumBytesForCalleeToPush = 0; + + // If this is is a call to a struct-return function on Darwin/X86, the callee + // pops the hidden struct pointer, so we have to push it back. + if (CallingConv == CallingConv::CSRet && Subtarget->isTargetDarwin()) + NumBytesForCalleeToPush = 4; + NodeTys.clear(); NodeTys.push_back(MVT::Other); // Returns a chain NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use. Ops.clear(); Ops.push_back(Chain); Ops.push_back(DAG.getConstant(NumBytes, getPointerTy())); - Ops.push_back(DAG.getConstant(0, getPointerTy())); + Ops.push_back(DAG.getConstant(NumBytesForCalleeToPush, getPointerTy())); Ops.push_back(InFlag); Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, Ops); InFlag = Chain.getValue(1); diff --git a/lib/Target/X86/X86ISelLowering.h b/lib/Target/X86/X86ISelLowering.h index 0d93ec4294f..ea8793339f2 100644 --- a/lib/Target/X86/X86ISelLowering.h +++ b/lib/Target/X86/X86ISelLowering.h @@ -374,7 +374,7 @@ namespace llvm { void LowerCCCArguments(SDOperand Op, SelectionDAG &DAG); std::pair LowerCCCCallTo(SDOperand Chain, const Type *RetTy, bool isVarArg, - bool isTailCall, + bool isTailCall, unsigned CallingConv, SDOperand Callee, ArgListTy &Args, SelectionDAG &DAG); // Fast Calling Convention implementation.