When trying to pass an argument on the stack, assume LLVM will do the right
thing for non-aggregate types.
 - Otherwise we unnecessarily pin values to the stack and currently end up
   triggering a backend bug in one case.

 - This loose cooperation with LLVM to implement the ABI is pretty ugly.

 - <rdar://problem/6918722> [irgen] clang miscompile of many pointer varargs on
   x86-64


git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@72419 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/CodeGen/CGCall.cpp b/lib/CodeGen/CGCall.cpp
index ce98f8c..ea0b887 100644
--- a/lib/CodeGen/CGCall.cpp
+++ b/lib/CodeGen/CGCall.cpp
@@ -571,6 +571,11 @@
                              const llvm::Type *CoerceTo,
                              ASTContext &Context) const;
 
+  /// getIndirectResult - Give a source type \arg Ty, return a suitable result
+  /// such that the argument will be passed in memory.
+  ABIArgInfo getIndirectResult(QualType Ty,
+                               ASTContext &Context) const;
+
   ABIArgInfo classifyReturnType(QualType RetTy, 
                                 ASTContext &Context) const;  
 
@@ -871,6 +876,17 @@
   return ABIArgInfo::getCoerce(CoerceTo);
 }
 
+ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty,
+                                            ASTContext &Context) const {
+  // If this is a scalar LLVM value then assume LLVM will pass it in the right
+  // place naturally.
+  if (!CodeGenFunction::hasAggregateLLVMType(Ty))
+    return ABIArgInfo::getDirect();
+
+  // FIXME: Set alignment correctly.
+  return ABIArgInfo::getIndirect(0);
+}
+
 ABIArgInfo X86_64ABIInfo::classifyReturnType(QualType RetTy,
                                             ASTContext &Context) const {
   // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
@@ -895,7 +911,7 @@
     // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
     // hidden argument.
   case Memory:
-    return ABIArgInfo::getIndirect(0);
+    return getIndirectResult(RetTy, Context);
 
     // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
     // available register of the sequence %rax, %rdx is used.
@@ -991,7 +1007,7 @@
     // COMPLEX_X87, it is passed in memory.
   case X87:
   case ComplexX87:
-    return ABIArgInfo::getIndirect(0);
+    return getIndirectResult(Ty, Context);
 
   case SSEUp:
   case X87Up:
@@ -1076,7 +1092,7 @@
       freeIntRegs -= neededInt;
       freeSSERegs -= neededSSE;
     } else {
-      it->info = ABIArgInfo::getIndirect(0);
+      it->info = getIndirectResult(it->type, Context);
     }
   }
 }