[llvm-commits] CVS: llvm/lib/Target/X86/X86ISelLowering.cpp X86ISelLowering.h

Chris Lattner lattner at cs.uiuc.edu
Tue May 23 11:50:50 PDT 2006



Changes in directory llvm/lib/Target/X86:

X86ISelLowering.cpp updated: 1.207 -> 1.208
X86ISelLowering.h updated: 1.62 -> 1.63
---
Log message:

Implement an annoying part of the Darwin/X86 abi: the callee of a struct
return argument pops the hidden struct pointer if present, not the caller.

For example, in this testcase:

struct X { int D, E, F, G; };
struct X bar() {
  struct X a;
  a.D = 0;
  a.E = 1;
  a.F = 2;
  a.G = 3;
  return a;
}
void foo(struct X *P) {
  *P = bar();
}

We used to emit:

_foo:
        subl $28, %esp
        movl 32(%esp), %eax
        movl %eax, (%esp)
        call _bar
        addl $28, %esp
        ret
_bar:
        movl 4(%esp), %eax
        movl $0, (%eax)
        movl $1, 4(%eax)
        movl $2, 8(%eax)
        movl $3, 12(%eax)
        ret

This is correct on Linux/X86 but not Darwin/X86.  With this patch, we now
emit:

_foo:
        subl $28, %esp
        movl 32(%esp), %eax
        movl %eax, (%esp)
        call _bar
***     addl $24, %esp
        ret
_bar:
        movl 4(%esp), %eax
        movl $0, (%eax)
        movl $1, 4(%eax)
        movl $2, 8(%eax)
        movl $3, 12(%eax)
***     ret $4

For the record, GCC emits (which is functionally equivalent to our new code):

_bar:
        movl    4(%esp), %eax
        movl    $3, 12(%eax)
        movl    $2, 8(%eax)
        movl    $1, 4(%eax)
        movl    $0, (%eax)
        ret     $4
_foo:
        pushl   %esi
        subl    $40, %esp
        movl    48(%esp), %esi
        leal    16(%esp), %eax
        movl    %eax, (%esp)
        call    _bar
        subl    $4, %esp
        movl    16(%esp), %eax
        movl    %eax, (%esi)
        movl    20(%esp), %eax
        movl    %eax, 4(%esi)
        movl    24(%esp), %eax
        movl    %eax, 8(%esi)
        movl    28(%esp), %eax
        movl    %eax, 12(%esi)
        addl    $40, %esp
        popl    %esi
        ret

This fixes SingleSource/Benchmarks/CoyoteBench/fftbench with LLC and the
JIT, and fixes the X86-backend portion of PR729: http://llvm.cs.uiuc.edu/PR729 .  The CBE still needs to
be updated.



---
Diffs of the changes:  (+19 -3)

 X86ISelLowering.cpp |   20 ++++++++++++++++++--
 X86ISelLowering.h   |    2 +-
 2 files changed, 19 insertions(+), 3 deletions(-)


Index: llvm/lib/Target/X86/X86ISelLowering.cpp
diff -u llvm/lib/Target/X86/X86ISelLowering.cpp:1.207 llvm/lib/Target/X86/X86ISelLowering.cpp:1.208
--- llvm/lib/Target/X86/X86ISelLowering.cpp:1.207	Fri May 19 16:34:04 2006
+++ llvm/lib/Target/X86/X86ISelLowering.cpp	Tue May 23 13:50:38 2006
@@ -393,7 +393,8 @@
 
   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 @@
   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 @@
 std::pair<SDOperand, SDOperand>
 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 @@
   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);


Index: llvm/lib/Target/X86/X86ISelLowering.h
diff -u llvm/lib/Target/X86/X86ISelLowering.h:1.62 llvm/lib/Target/X86/X86ISelLowering.h:1.63
--- llvm/lib/Target/X86/X86ISelLowering.h:1.62	Wed May 17 14:07:40 2006
+++ llvm/lib/Target/X86/X86ISelLowering.h	Tue May 23 13:50:38 2006
@@ -374,7 +374,7 @@
     void LowerCCCArguments(SDOperand Op, SelectionDAG &DAG);
     std::pair<SDOperand, SDOperand>
     LowerCCCCallTo(SDOperand Chain, const Type *RetTy, bool isVarArg,
-                   bool isTailCall,
+                   bool isTailCall, unsigned CallingConv,
                    SDOperand Callee, ArgListTy &Args, SelectionDAG &DAG);
 
     // Fast Calling Convention implementation.






More information about the llvm-commits mailing list