diff --git a/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp b/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp --- a/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp +++ b/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp @@ -22,6 +22,7 @@ #include "llvm/ADT/APFloat.h" #include "llvm/ADT/APInt.h" #include "llvm/ADT/ArrayRef.h" +#include "llvm/ADT/Optional.h" #include "llvm/ADT/STLExtras.h" #include "llvm/ADT/SmallSet.h" #include "llvm/ADT/SmallVector.h" @@ -17107,16 +17108,37 @@ Addr}); } -bool AArch64TargetLowering::functionArgumentNeedsConsecutiveRegisters( - Type *Ty, CallingConv::ID CallConv, bool isVarArg) const { - if (Ty->isArrayTy()) +// Recursively walk an aggregate type that may contain nested +// aggreagates. Check that all the non-aggregate fields found +// are the same type. +// Inititally fieldTy will be empty and will be set when we +// find the first non-aggregate type. +static bool aggregateHasSameTypeFields(Type *Ty, + llvm::Optional &fieldTy) { + if (Ty->isAggregateType()) { + // Walk all the sub types of this aggregate + auto subtypes_end = Ty->subtype_end(); + for (auto it = Ty->subtype_begin(); it != subtypes_end; ++it) { + if (!aggregateHasSameTypeFields(*it, fieldTy)) + return false; + } return true; + } else { + if (!fieldTy) + fieldTy = Ty; + return Ty == *fieldTy; + } +} - const TypeSize &TySize = Ty->getPrimitiveSizeInBits(); - if (TySize.isScalable() && TySize.getKnownMinSize() > 128) - return true; +bool AArch64TargetLowering::functionArgumentNeedsConsecutiveRegisters( + Type *Ty, CallingConv::ID CallConv, bool isVarArg) const { + if (!Ty->isArrayTy()) { + const TypeSize &TySize = Ty->getPrimitiveSizeInBits(); + return TySize.isScalable() && TySize.getKnownMinSize() > 128; + } - return false; + llvm::Optional fieldType; + return aggregateHasSameTypeFields(Ty, fieldType); } bool AArch64TargetLowering::shouldNormalizeToSelectSequence(LLVMContext &, diff --git a/llvm/test/CodeGen/AArch64/argument-blocks-array-of-struct.ll b/llvm/test/CodeGen/AArch64/argument-blocks-array-of-struct.ll new file mode 100644 --- /dev/null +++ b/llvm/test/CodeGen/AArch64/argument-blocks-array-of-struct.ll @@ -0,0 +1,327 @@ +; RUN: llc -mtriple=aarch64-none-linux-gnu -o - %s | FileCheck %s + +;; Check that the llvm aarch64 backend can handle arrays of +;; structs and vice versa when passed from IR. +;; (this layering is something clang would normally simplify) +;; +;; Some of these examples are not ABI compliant and they're not +;; meant to be. For instance according to the ABI an aggregate +;; with more than 4 members must go in memory. This restriction +;; is applied earlier in the compilation process so here we do +;; see 8 member types in registers. +;; +;; When we have more than 8 members we simply run out of registers +;; and that's what produces the 8 limit here. + +;; Plain arrays + +define [ 0 x double ] @array_0() { +; CHECK-LABEL: array_0: +; CHECK: ret + ret [ 0 x double ] zeroinitializer +} + +define [ 1 x double ] @array_1() { +; CHECK-LABEL: array_1: +; CHECK: movi d0, #0000000000000000 +; CHECK-NEXT: ret + ret [ 1 x double ] zeroinitializer +} + +define [ 8 x double ] @array_8() { +; CHECK-LABEL: array_8: +; CHECK: movi d0, #0000000000000000 +; CHECK-NEXT: movi d1, #0000000000000000 +; CHECK-NEXT: movi d2, #0000000000000000 +; CHECK-NEXT: movi d3, #0000000000000000 +; CHECK-NEXT: movi d4, #0000000000000000 +; CHECK-NEXT: movi d5, #0000000000000000 +; CHECK-NEXT: movi d6, #0000000000000000 +; CHECK-NEXT: movi d7, #0000000000000000 +; CHECK-NEXT: ret + ret [ 8 x double ] zeroinitializer +} + +;; > 8 items goes on the stack + +define [ 9 x double ] @array_9() { +; CHECK-LABEL: array_9: +; CHECK: movi v0.2d, #0000000000000000 +; CHECK-NEXT: str xzr, [x8, #64] +; CHECK-NEXT: stp q0, q0, [x8, #32] +; CHECK-NEXT: stp q0, q0, [x8] +; CHECK-NEXT: ret + ret [ 9 x double ] zeroinitializer +} + +;; Won't use any registers, just checking for assumptions. +%T_STRUCT_0M = type { } + +define %T_STRUCT_0M @struct_zero_fields() { +; CHECK-LABEL: struct_zero_fields: +; CHECK: ret + ret %T_STRUCT_0M zeroinitializer +} + +define [ 1 x %T_STRUCT_0M ] @array_of_struct_zero_fields() { +; CHECK-LABEL: array_of_struct_zero_fields: +; CHECK: ret + ret [ 1 x %T_STRUCT_0M ] zeroinitializer +} + +define [ 2 x %T_STRUCT_0M ] @array_of_struct_zero_fields_in_struct() { +; CHECK-LABEL: array_of_struct_zero_fields_in_struct: +; CHECK: ret + ret [ 2 x %T_STRUCT_0M ] zeroinitializer +} + +%T_STRUCT_1M = type { i32 } + +define %T_STRUCT_1M @struct_one_field() { +; CHECK-LABEL: struct_one_field: +; CHECK: w0, wzr +; CHECK-NEXT: ret + ret %T_STRUCT_1M zeroinitializer +} + +define [ 1 x %T_STRUCT_1M ] @array_of_struct_one_field() { +; CHECK-LABEL: array_of_struct_one_field: +; CHECK: w0, wzr +; CHECK-NEXT: ret + ret [ 1 x %T_STRUCT_1M ] zeroinitializer +} + +;; This one will be a reg block +define [ 2 x %T_STRUCT_1M ] @array_of_struct_one_field_2() { +; CHECK-LABEL: array_of_struct_one_field_2: +; CHECK: w0, wzr +; CHECK: w1, wzr +; CHECK-NEXT: ret + ret [ 2 x %T_STRUCT_1M ] zeroinitializer +} + +;; Different types for each field, will not be put in a reg block +%T_STRUCT_DIFFM = type { double, i32 } + +define %T_STRUCT_DIFFM @struct_different_field_types() { +; CHECK-LABEL: struct_different_field_types: +; CHECK: movi d0, #0000000000000000 +; CHECK-NEXT: w0, wzr +; CHECK-NEXT: ret + ret %T_STRUCT_DIFFM zeroinitializer +} + +define [ 1 x %T_STRUCT_DIFFM ] @array_of_struct_different_field_types() { +; CHECK-LABEL: array_of_struct_different_field_types: +; CHECK: movi d0, #0000000000000000 +; CHECK-NEXT: w0, wzr +; CHECK-NEXT: ret + ret [ 1 x %T_STRUCT_DIFFM ] zeroinitializer +} + +define [ 2 x %T_STRUCT_DIFFM ] @array_of_struct_different_field_types_2() { +; CHECK-LABEL: array_of_struct_different_field_types_2: +; CHECK: movi d0, #0000000000000000 +; CHECK-NEXT: movi d1, #0000000000000000 +; CHECK-NEXT: w0, wzr +; CHECK-NEXT: w1, wzr +; CHECK-NEXT: ret + ret [ 2 x %T_STRUCT_DIFFM ] zeroinitializer +} + +;; Each field is the same type, can be put in a reg block +%T_STRUCT_SAMEM = type { double, double } + +;; Here isn't a block as such, we just allocate two consecutive registers +define %T_STRUCT_SAMEM @struct_same_field_types() { +; CHECK-LABEL: struct_same_field_types: +; CHECK: movi d0, #0000000000000000 +; CHECK-NEXT: movi d1, #0000000000000000 +; CHECK-NEXT: ret + ret %T_STRUCT_SAMEM zeroinitializer +} + +define [ 1 x %T_STRUCT_SAMEM ] @array_of_struct_same_field_types() { +; CHECK-LABEL: array_of_struct_same_field_types: +; CHECK: movi d0, #0000000000000000 +; CHECK-NEXT: movi d1, #0000000000000000 +; CHECK-NEXT: ret + ret [ 1 x %T_STRUCT_SAMEM ] zeroinitializer +} + +define [ 2 x %T_STRUCT_SAMEM ] @array_of_struct_same_field_types_2() { +; CHECK-LABEL: array_of_struct_same_field_types_2: +; CHECK: movi d0, #0000000000000000 +; CHECK-NEXT: movi d1, #0000000000000000 +; CHECK-NEXT: movi d2, #0000000000000000 +; CHECK-NEXT: movi d3, #0000000000000000 +; CHECK-NEXT: ret + ret [ 2 x %T_STRUCT_SAMEM ] zeroinitializer +} + +;; Same field type but integer this time. Put into x registers instead. +%T_STRUCT_SAMEM_INT = type { i64, i64 } + +define %T_STRUCT_SAMEM_INT @struct_same_field_types_int() { +; CHECK-LABEL: struct_same_field_types_int: +; CHECK: x0, xzr +; CHECK-NEXT: x1, xzr +; CHECK-NEXT: ret + ret %T_STRUCT_SAMEM_INT zeroinitializer +} + +define [ 1 x %T_STRUCT_SAMEM_INT ] @array_of_struct_same_field_types_int() { +; CHECK-LABEL: array_of_struct_same_field_types_int: +; CHECK: x0, xzr +; CHECK-NEXT: x1, xzr +; CHECK-NEXT: ret + ret [ 1 x %T_STRUCT_SAMEM_INT ] zeroinitializer +} + +define [ 2 x %T_STRUCT_SAMEM_INT ] @array_of_struct_same_field_types_int_2() { +; CHECK-LABEL: array_of_struct_same_field_types_int_2: +; CHECK: x0, xzr +; CHECK-NEXT: x1, xzr +; CHECK-NEXT: x2, xzr +; CHECK-NEXT: x3, xzr +; CHECK-NEXT: ret + ret [ 2 x %T_STRUCT_SAMEM_INT ] zeroinitializer +} + +;; An aggregate of more than 8 items must go in memory. +;; 4x2 struct fields = 8 items so it goes in a block. + +define [ 4 x %T_STRUCT_SAMEM ] @array_of_struct_8_fields() { +; CHECK-LABEL: array_of_struct_8_fields: +; CHECK: movi d0, #0000000000000000 +; CHECK-NEXT: movi d1, #0000000000000000 +; CHECK-NEXT: movi d2, #0000000000000000 +; CHECK-NEXT: movi d3, #0000000000000000 +; CHECK-NEXT: movi d4, #0000000000000000 +; CHECK-NEXT: movi d5, #0000000000000000 +; CHECK-NEXT: movi d6, #0000000000000000 +; CHECK-NEXT: movi d7, #0000000000000000 +; CHECK-NEXT: ret + ret [ 4 x %T_STRUCT_SAMEM ] zeroinitializer +} + +;; 5x2 fields = 10 so it is returned in memory. + +define [ 5 x %T_STRUCT_SAMEM ] @array_of_struct_in_memory() { +; CHECK-LABEL: array_of_struct_in_memory: +; CHECK: movi v0.2d, #0000000000000000 +; CHECK-NEXT: stp q0, q0, [x8, #48] +; CHECK-NEXT: stp q0, q0, [x8, #16] +; CHECK-NEXT: str q0, [x8] +; CHECK-NEXT: ret + ret [ 5 x %T_STRUCT_SAMEM ] zeroinitializer +} + +;; A struct whose field is an array. +%T_STRUCT_ARRAYM = type { [ 2 x double ]}; + +define %T_STRUCT_ARRAYM @struct_array_field() { +; CHECK-LABEL: struct_array_field: +; CHECK: movi d0, #0000000000000000 +; CHECK-NEXT: movi d1, #0000000000000000 +; CHECK-NEXT: ret + ret %T_STRUCT_ARRAYM zeroinitializer +} + +define [ 1 x %T_STRUCT_ARRAYM ] @array_of_struct_array_field() { +; CHECK-LABEL: array_of_struct_array_field: +; CHECK: movi d0, #0000000000000000 +; CHECK-NEXT: movi d1, #0000000000000000 +; CHECK-NEXT: ret + ret [ 1 x %T_STRUCT_ARRAYM ] zeroinitializer +} + +define [ 2 x %T_STRUCT_ARRAYM ] @array_of_struct_array_field_2() { +; CHECK-LABEL: array_of_struct_array_field_2: +; CHECK: movi d0, #0000000000000000 +; CHECK-NEXT: movi d1, #0000000000000000 +; CHECK-NEXT: movi d2, #0000000000000000 +; CHECK-NEXT: movi d3, #0000000000000000 +; CHECK-NEXT: ret + ret [ 2 x %T_STRUCT_ARRAYM ] zeroinitializer +} + +;; All non-aggregate fields must have the same type, all through the +;; overall aggreagate. This is false here because of the i32. +%T_NESTED_STRUCT_DIFFM = type { + [ 1 x { { double, double } } ], + [ 1 x { { double, i32 } } ] +}; + +define %T_NESTED_STRUCT_DIFFM @struct_nested_different_field_types() { +; CHECK-LABEL: struct_nested_different_field_types: +; CHECK: movi d0, #0000000000000000 +; CHECK: movi d1, #0000000000000000 +; CHECK: movi d2, #0000000000000000 +; CHECK-NEXT: w0, wzr +; CHECK-NEXT: ret + ret %T_NESTED_STRUCT_DIFFM zeroinitializer +} + +define [ 1 x %T_NESTED_STRUCT_DIFFM ] @array_of_struct_nested_different_field_types() { +; CHECK-LABEL: array_of_struct_nested_different_field_types: +; CHECK: movi d0, #0000000000000000 +; CHECK: movi d1, #0000000000000000 +; CHECK: movi d2, #0000000000000000 +; CHECK-NEXT: w0, wzr +; CHECK-NEXT: ret + ret [ 1 x %T_NESTED_STRUCT_DIFFM ] zeroinitializer +} + +define [ 2 x %T_NESTED_STRUCT_DIFFM ] @array_of_struct_nested_different_field_types_2() { +; CHECK-LABEL: array_of_struct_nested_different_field_types_2: +; CHECK: movi d0, #0000000000000000 +; CHECK: movi d1, #0000000000000000 +; CHECK: movi d2, #0000000000000000 +; CHECK-NEXT: movi d3, #0000000000000000 +; CHECK-NEXT: movi d4, #0000000000000000 +; CHECK-NEXT: movi d5, #0000000000000000 +; CHECK-NEXT: w0, wzr +; CHECK-NEXT: w1, wzr +; CHECK-NEXT: ret + ret [ 2 x %T_NESTED_STRUCT_DIFFM ] zeroinitializer +} + +;; All fields here are the same type, more nesting to stress the recursive walk. +%T_NESTED_STRUCT_SAMEM = type { + { { double} }, + { [ 2 x { double, double } ] } +}; + +define %T_NESTED_STRUCT_SAMEM @struct_nested_same_field_types() { +; CHECK-LABEL: struct_nested_same_field_types: +; CHECK: movi d0, #0000000000000000 +; CHECK-NEXT: movi d1, #0000000000000000 +; CHECK-NEXT: movi d2, #0000000000000000 +; CHECK-NEXT: movi d3, #0000000000000000 +; CHECK-NEXT: movi d4, #0000000000000000 +; CHECK-NEXT: ret + ret %T_NESTED_STRUCT_SAMEM zeroinitializer +} + +define [ 1 x %T_NESTED_STRUCT_SAMEM ] @array_of_struct_nested_same_field_types() { +; CHECK-LABEL: array_of_struct_nested_same_field_types: +; CHECK: movi d0, #0000000000000000 +; CHECK-NEXT: movi d1, #0000000000000000 +; CHECK-NEXT: movi d2, #0000000000000000 +; CHECK-NEXT: movi d3, #0000000000000000 +; CHECK-NEXT: movi d4, #0000000000000000 +; CHECK-NEXT: ret + ret [ 1 x %T_NESTED_STRUCT_SAMEM ] zeroinitializer +} + +;; 2 x (1 + (2 x 2)) = 10 so this is returned in memory +define [ 2 x %T_NESTED_STRUCT_SAMEM ] @array_of_struct_nested_same_field_types_2() { +; CHECK-LABEL: array_of_struct_nested_same_field_types_2: +; CHECK: movi v0.2d, #0000000000000000 +; CHECK-NEXT: stp q0, q0, [x8, #48] +; CHECK-NEXT: stp q0, q0, [x8, #16] +; CHECK-NEXT: str q0, [x8] +; CHECK-NEXT: ret + ret [ 2 x %T_NESTED_STRUCT_SAMEM ] zeroinitializer +}