Index: clang/lib/CodeGen/TargetInfo.cpp =================================================================== --- clang/lib/CodeGen/TargetInfo.cpp +++ clang/lib/CodeGen/TargetInfo.cpp @@ -8087,14 +8087,39 @@ } //===----------------------------------------------------------------------===// -// AVR ABI Implementation. +// AVR ABI Implementation. Documented at +// https://gcc.gnu.org/wiki/avr-gcc#Calling_Convention +// https://gcc.gnu.org/wiki/avr-gcc#Reduced_Tiny //===----------------------------------------------------------------------===// namespace { +class AVRABIInfo : public DefaultABIInfo { +public: + AVRABIInfo(CodeGenTypes &CGT) : DefaultABIInfo(CGT) {} + + ABIArgInfo classifyReturnType(QualType Ty) const { + // A return struct with size less equal than 8 bytes is returned + // directly via registers R18-R25. + if (isAggregateTypeForABI(Ty) && getContext().getTypeSize(Ty) <= 64) + return ABIArgInfo::getDirect(); + else + return DefaultABIInfo::classifyReturnType(Ty); + } + + // Just copy the original implementation of DefaultABIInfo::computeInfo(), + // since DefaultABIInfo::classify{Return,Argument}Type() are not virtual. + void computeInfo(CGFunctionInfo &FI) const override { + if (!getCXXABI().classifyReturnType(FI)) + FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); + for (auto &I : FI.arguments()) + I.info = classifyArgumentType(I.type); + } +}; + class AVRTargetCodeGenInfo : public TargetCodeGenInfo { public: AVRTargetCodeGenInfo(CodeGenTypes &CGT) - : TargetCodeGenInfo(std::make_unique(CGT)) {} + : TargetCodeGenInfo(std::make_unique(CGT)) {} void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &CGM) const override { Index: clang/test/CodeGen/avr/struct.c =================================================================== --- /dev/null +++ clang/test/CodeGen/avr/struct.c @@ -0,0 +1,32 @@ +// RUN: %clang_cc1 -triple avr -emit-llvm %s -o - | FileCheck %s + +// Structure that is more than 8 bytes. +struct s10 { + int a, b, c, d, e; +}; + +// Structure that is less than 8 bytes. +struct s06 { + int a, b, c; +}; + +struct s10 foo10(int a, int b, int c) { + struct s10 a0; + a0.a = c; + a0.b = b; + a0.c = a; + return a0; +} + +struct s06 foo06(int a, int b, int c) { + struct s06 a0; + a0.a = a; + a0.b = b; + a0.c = c; + return a0; +} + +// CHECK: %struct.s10 = type { i16, i16, i16, i16, i16 } +// CHECK: %struct.s06 = type { i16, i16, i16 } +// CHECK: define{{.*}} void @foo10(%struct.s10* {{.*}}, i16 %a, i16 %b, i16 %c) +// CHECK: define{{.*}} %struct.s06 @foo06(i16 %a, i16 %b, i16 %c)