[clang] [clang][PowerPC] Add flag to enable compatibility with GNU for complex arguments (PR #77732)

zhijian lin via cfe-commits cfe-commits at lists.llvm.org
Wed Mar 20 08:17:24 PDT 2024


================
@@ -337,12 +347,58 @@ CharUnits PPC32_SVR4_ABIInfo::getParamTypeAlignment(QualType Ty) const {
   return CharUnits::fromQuantity(4);
 }
 
+ABIArgInfo PPC32_SVR4_ABIInfo::handleComplex(uint64_t &TypeSize) const {
+  llvm::Type *ElemTy;
+  unsigned RegsNeeded; // Registers Needed for Complex.
+
+  if (TypeSize == 64) {
+    ElemTy = llvm::Type::getInt64Ty(getVMContext());
+    RegsNeeded = 1;
+  } else {
+    ElemTy = llvm::Type::getInt32Ty(getVMContext());
+    RegsNeeded = (TypeSize + 31) / 32;
+  }
+  return ABIArgInfo::getDirect(llvm::ArrayType::get(ElemTy, RegsNeeded));
+}
+
+ABIArgInfo PPC32_SVR4_ABIInfo::classifyArgumentType(QualType Ty,
+                                                    int &ArgGPRsLeft) const {
+  assert(ArgGPRsLeft <= NumArgGPRs && "Arg GPR tracking underflow");
+  Ty = useFirstFieldIfTransparentUnion(Ty);
+
+  ASTContext &Context = getContext();
+
+  uint64_t TypeSize = Context.getTypeSize(Ty);
+
+  if (IsComplexInRegABI && Ty->isAnyComplexType() &&
+      TypeSize <= RegLen * ArgGPRsLeft) {
+    assert(Ty->isAnyComplexType() && "Ty must be Complex type.");
+    ArgGPRsLeft -= TypeSize / RegLen;
+    return handleComplex(TypeSize);
+  }
+
+  if (ArgGPRsLeft) {
+    // Records with non-trivial destructors/copy-constructors should not be
+    // passed by value.
+    if (isAggregateTypeForABI(Ty))
+      ArgGPRsLeft -= 1;
+    else if (!Ty->isFloatingType()) {
+      // For other premitive types.
+      if (TypeSize > RegLen && TypeSize <= 2 * RegLen)
+        ArgGPRsLeft -= TypeSize / RegLen;
----------------
diggerlin wrote:

for a test case `foo(_Complex long double foo4(int x1, long  long y1, int x2, _Complex double z,)`
 
 `int x1` will have r3, 
`long long y1` will have r5,r6 (skip r4 since re is even.)
`int x2` will have r7, 
` _Complex double z`  need to occupy 4 registers, there is no enough register , it can not pass `_Complex double z` in r8,r9,r10 , you have to pass ` _Complex double z` in by pointer (you can not call `handleComplex()`). 

but in your code, 
ArgGPRsLeft is 7 after int x1,
ArgGPRsLeft is 5 after int y1
ArgGPRsLeft is 4 after int x2

there is still have 4 registers left for ` _Complex double z`, you can call `handleComplex()`,  you can pass ` _Complex double z by value 


https://github.com/llvm/llvm-project/pull/77732


More information about the cfe-commits mailing list