Index: clang/lib/Basic/Targets/AVR.h =================================================================== --- clang/lib/Basic/Targets/AVR.h +++ clang/lib/Basic/Targets/AVR.h @@ -74,8 +74,7 @@ static const char *const GCCRegNames[] = { "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7", "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15", "r16", "r17", "r18", "r19", - "r20", "r21", "r22", "r23", "r24", "r25", "X", "Y", "Z", "SP" - }; + "r20", "r21", "r22", "r23", "r24", "r25", "X", "Y", "Z", "SP"}; return llvm::makeArrayRef(GCCRegNames); } @@ -169,15 +168,12 @@ bool isValidCPUName(StringRef Name) const override; void fillValidCPUList(SmallVectorImpl &Values) const override; - bool setCPU(const std::string &Name) override { - bool isValid = isValidCPUName(Name); - if (isValid) - CPU = Name; - return isValid; - } + bool setCPU(const std::string &Name) override; + StringRef getABI() const override { return ABI; } protected: std::string CPU; + StringRef ABI; }; } // namespace targets Index: clang/lib/Basic/Targets/AVR.cpp =================================================================== --- clang/lib/Basic/Targets/AVR.cpp +++ clang/lib/Basic/Targets/AVR.cpp @@ -24,7 +24,8 @@ struct LLVM_LIBRARY_VISIBILITY MCUInfo { const char *Name; const char *DefineName; - const int NumFlashBanks; // -1 means the device does not support LPM/ELPM. + const int NumFlashBanks; // Set to 0 for the devices do not support LPM/ELPM. + bool IsTiny; // Set to true for the devices belong to the avrtiny family. }; // This list should be kept up-to-date with AVRDevices.td in LLVM. @@ -267,14 +268,14 @@ {"atxmega128a1", "__AVR_ATxmega128A1__", 2}, {"atxmega128a1u", "__AVR_ATxmega128A1U__", 2}, {"atxmega128a4u", "__AVR_ATxmega128A4U__", 2}, - {"attiny4", "__AVR_ATtiny4__", 0}, - {"attiny5", "__AVR_ATtiny5__", 0}, - {"attiny9", "__AVR_ATtiny9__", 0}, - {"attiny10", "__AVR_ATtiny10__", 0}, - {"attiny20", "__AVR_ATtiny20__", 0}, - {"attiny40", "__AVR_ATtiny40__", 0}, - {"attiny102", "__AVR_ATtiny102__", 0}, - {"attiny104", "__AVR_ATtiny104__", 0}, + {"attiny4", "__AVR_ATtiny4__", 0, true}, + {"attiny5", "__AVR_ATtiny5__", 0, true}, + {"attiny9", "__AVR_ATtiny9__", 0, true}, + {"attiny10", "__AVR_ATtiny10__", 0, true}, + {"attiny20", "__AVR_ATtiny20__", 0, true}, + {"attiny40", "__AVR_ATtiny40__", 0, true}, + {"attiny102", "__AVR_ATtiny102__", 0, true}, + {"attiny104", "__AVR_ATtiny104__", 0, true}, {"attiny202", "__AVR_ATtiny202__", 1}, {"attiny402", "__AVR_ATtiny402__", 1}, {"attiny204", "__AVR_ATtiny204__", 1}, @@ -325,6 +326,27 @@ Values.push_back(Info.Name); } +bool AVRTargetInfo::setCPU(const std::string &Name) { + // Set the ABI and CPU fields if parameter Name is a family name. + if (llvm::is_contained(ValidFamilyNames, Name)) { + CPU = Name; + ABI = Name == "avrtiny" ? "avrtiny" : "avr"; + return true; + } + + // Set the ABI field if parameter Name is a device name. + auto It = llvm::find_if( + AVRMcus, [&](const MCUInfo &Info) { return Info.Name == Name; }); + if (It != std::end(AVRMcus)) { + CPU = Name; + ABI = It->IsTiny ? "avrtiny" : "avr"; + return true; + } + + // Parameter Name is neither valid family name nor valid device name. + return false; +} + void AVRTargetInfo::getTargetDefines(const LangOptions &Opts, MacroBuilder &Builder) const { Builder.defineMacro("AVR"); Index: clang/lib/CodeGen/TargetInfo.cpp =================================================================== --- clang/lib/CodeGen/TargetInfo.cpp +++ clang/lib/CodeGen/TargetInfo.cpp @@ -19,9 +19,9 @@ #include "CodeGenFunction.h" #include "clang/AST/Attr.h" #include "clang/AST/RecordLayout.h" +#include "clang/Basic/Builtins.h" #include "clang/Basic/CodeGenOptions.h" #include "clang/Basic/DiagnosticFrontend.h" -#include "clang/Basic/Builtins.h" #include "clang/CodeGen/CGFunctionInfo.h" #include "clang/CodeGen/SwiftCallingConv.h" #include "llvm/ADT/SmallBitVector.h" @@ -33,6 +33,7 @@ #include "llvm/IR/IntrinsicsNVPTX.h" #include "llvm/IR/IntrinsicsS390.h" #include "llvm/IR/Type.h" +#include "llvm/Support/MathExtras.h" #include "llvm/Support/raw_ostream.h" #include // std::sort @@ -8272,32 +8273,88 @@ namespace { class AVRABIInfo : public DefaultABIInfo { +private: + // The total amount of registers can be used to pass parameters. It is 18 on + // AVR, or 8 on AVRTiny. + const unsigned ParamRegs; + public: - AVRABIInfo(CodeGenTypes &CGT) : DefaultABIInfo(CGT) {} + AVRABIInfo(CodeGenTypes &CGT, unsigned N) + : DefaultABIInfo(CGT), ParamRegs(N) {} + + ABIArgInfo classifyReturnType(QualType Ty, bool &LargeRet) const { + if (isAggregateTypeForABI(Ty)) { + // A return struct with size less than or equal to 8 bytes is returned + // directly via registers R18-R25. + if (getContext().getTypeSize(Ty) <= 64) + return ABIArgInfo::getDirect(); + // A return struct with size larger than 8 bytes is returned via a stack + // slot, along with a pointer to it as the function's implicit argument. + LargeRet = true; + return getNaturalAlignIndirect(Ty); + } + // Otherwise we follow the default way which is compatible. + return DefaultABIInfo::classifyReturnType(Ty); + } - ABIArgInfo classifyReturnType(QualType Ty) const { - // A return struct with size less than or equal to 8 bytes is returned - // directly via registers R18-R25. - if (isAggregateTypeForABI(Ty) && getContext().getTypeSize(Ty) <= 64) + ABIArgInfo classifyArgumentType(QualType Ty, unsigned &NumRegs) const { + unsigned TySize = getContext().getTypeSize(Ty); + + // An int8 type argument always costs two registers like an int16. + if (TySize == 8 && NumRegs >= 2) { + NumRegs -= 2; + return ABIArgInfo::getExtend(Ty); + } + + // If the argument size is an odd number of bytes, round up the size + // to the next even number. + TySize = llvm::alignTo(TySize, 16); + + // Any type including an array/struct type can be passed in rgisters, + // if there are enough registers left. + if (TySize <= NumRegs * 8) { + NumRegs -= TySize / 8; return ABIArgInfo::getDirect(); - else - return DefaultABIInfo::classifyReturnType(Ty); + } + + // An argument is passed either completely in registers or completely in + // memory. Since there are not enough registers left, current argument + // and all other unprocessed arguments should be passed in memory. + // However we still need to return `ABIArgInfo::getDirect()` other than + // `ABIInfo::getNaturalAlignIndirect(Ty)`, otherwise an extra stack slot + // will be allocated, so the stack frame layout will be incompatible with + // avr-gcc. + NumRegs = 0; + return ABIArgInfo::getDirect(); } - // Just copy the original implementation of DefaultABIInfo::computeInfo(), - // since DefaultABIInfo::classify{Return,Argument}Type() are not virtual. void computeInfo(CGFunctionInfo &FI) const override { + // Decide the return type. + bool LargeRet = false; if (!getCXXABI().classifyReturnType(FI)) - FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); + FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), LargeRet); + + // Decide each argument type. The total number of registers can be used for + // arguments depends on several factors: + // 1. Arguments of varargs functions are passed on the stack. This applies + // even to the named arguments. So no register can be used. + // 2. Total 18 registers can be used on avr and 8 ones on avrtiny. + // 3. If the return type is a struct with size larger than 64 bits, two + // registers (out of 18/8) with be cost as an implicit pointer argument. + unsigned NumRegs = ParamRegs; + if (FI.isVariadic()) + NumRegs = 0; + else if (LargeRet) + NumRegs -= 2; for (auto &I : FI.arguments()) - I.info = classifyArgumentType(I.type); + I.info = classifyArgumentType(I.type, NumRegs); } }; class AVRTargetCodeGenInfo : public TargetCodeGenInfo { public: - AVRTargetCodeGenInfo(CodeGenTypes &CGT) - : TargetCodeGenInfo(std::make_unique(CGT)) {} + AVRTargetCodeGenInfo(CodeGenTypes &CGT, unsigned N) + : TargetCodeGenInfo(std::make_unique(CGT, N)) {} LangAS getGlobalVarAddressSpace(CodeGenModule &CGM, const VarDecl *D) const override { @@ -11273,8 +11330,12 @@ case llvm::Triple::mips64el: return SetCGInfo(new MIPSTargetCodeGenInfo(Types, false)); - case llvm::Triple::avr: - return SetCGInfo(new AVRTargetCodeGenInfo(Types)); + case llvm::Triple::avr: { + // R8 ~ R25 are used to pass parameters on avr, while R18 ~ R25 + // are used on avrtiny. + unsigned N = getTarget().getABI() == "avrtiny" ? 8 : 18; + return SetCGInfo(new AVRTargetCodeGenInfo(Types, N)); + } case llvm::Triple::aarch64: case llvm::Triple::aarch64_32: Index: clang/test/CodeGen/avr/argument.c =================================================================== --- /dev/null +++ clang/test/CodeGen/avr/argument.c @@ -0,0 +1,167 @@ +// RUN: %clang_cc1 -triple avr -target-cpu atmega328 -emit-llvm %s -o - \ +// RUN: | FileCheck %s --check-prefix AVR +// RUN: %clang_cc1 -triple avr -target-cpu attiny40 -emit-llvm %s -o - \ +// RUN: | FileCheck %s --check-prefix TINY + +// NOTE: All arguments are passed in memory for functions with variable arguments. +// AVR: define {{.*}} i8 @foo0(i8 {{.*}}, i8 {{.*}}, ...) +// TINY: define {{.*}} i8 @foo0(i8 {{.*}}, i8 {{.*}}, ...) +// AVR-NOT: define {{.*}} i8 @foo0(i8 {{.*}} signext {{.*}}, i8 {{.*}} signext {{.*}}, ...) +// TINY-NOT: define {{.*}} i8 @foo0(i8 {{.*}} signext {{.*}}, i8 {{.*}} signext {{.*}}, ...) +char foo0(char a, char b, ...) { + return a + b; +} + +// NOTE: The char type argument is passed via registers on both avr and avrtiny. +// AVR: define {{.*}} @foo1({{.*}}, i8 {{.*}} signext {{.*}}) +// TINY: define {{.*}} @foo1({{.*}}, i8 {{.*}} signext {{.*}}) +struct s08 { + char arr[8]; +}; +struct s06 { + char arr[6]; +}; +struct s08 foo1(struct s06 s6, char a) { + struct s08 s8; + s8.arr[a] = s6.arr[a]; + return s8; +} + +// NOTE: The char type argument is passed via registers on both avr and avrtiny. +// AVR: define {{.*}} void @foo2({{.*}}, {{.*}}, i8 {{.*}} signext {{.*}}) +// TINY: define {{.*}} void @foo2({{.*}}, {{.*}}, i8 {{.*}} signext {{.*}}) +struct s09 { + char arr[9]; +}; +struct s04 { + char arr[4]; +}; +struct s09 foo2(struct s04 s4, char a) { + struct s09 s9; + s9.arr[a] = s4.arr[a]; + return s9; +} + +// NOTE: The char type argument is passed via registers on avr but via stack on avrtiny. +// AVR: define {{.*}} void @foo3({{.*}}, {{.*}}, i8 {{.*}} signext {{.*}}) +// TINY-NOT: define {{.*}} void @foo3({{.*}}, {{.*}}, i8 {{.*}} signext {{.*}}) +// TINY: define {{.*}} void @foo3({{.*}}, {{.*}}, i8 {{.*}}) +struct s09 foo3(struct s06 s6, char a) { + struct s09 s9; + s9.arr[a] = s6.arr[a]; + return s9; +} + +// NOTE: The char type argument is passed via stack on both avr and avrtiny. +// AVR: define {{.*}} void @foo4({{.*}}, i8 {{.*}}, ...) +// TINY: define {{.*}} void @foo4({{.*}}, i8 {{.*}}, ...) +// AVR-NOT: define {{.*}} i8 @foo4({{.*}}, i8 {{.*}} signext {{.*}}, ...) +// TINY-NOT: define {{.*}} i8 @foo4({{.*}}, i8 {{.*}} signext {{.*}}, ...) +struct s09 foo4(char b, ...) { + struct s09 s9; + s9.arr[b] = b; + return s9; +} + +// NOTE: The char type argument is passed via stack on both avr and avrtiny. +// AVR: define {{.*}} void @foo5({{.*}}, {{.*}}, i8 {{.*}}) +// TINY: define {{.*}} void @foo5({{.*}}, {{.*}}, i8 {{.*}}) +// AVR-NOT: define {{.*}} void @foo5({{.*}}, {{.*}}, i8 {{.*}} signext {{.*}}) +// TINY-NOT: define {{.*}} void @foo5({{{.*}}, {.*}}, i8 {{.*}} signext {{.*}}) +struct s15 { + char arr[15]; +}; +struct s09 foo5(struct s15 sx, char a) { + struct s09 s9; + s9.arr[a] = sx.arr[a]; + return s9; +} + +// NOTE: The char type argument is passed via registers on both avr and avrtiny. +// AVR: define {{.*}} i8 @foo6({{.*}}, i8 {{.*}} signext {{.*}}) +// TINY: define {{.*}} i8 @foo6({{.*}}, i8 {{.*}} signext {{.*}}) +char foo6(struct s06 s, char b) { + return s.arr[b]; +} + +// NOTE: The char type argument is passed via stack on both avr and avrtiny. +// AVR: define {{.*}} @foo7({{.*}}, i8 {{.*}}) +// TINY: define {{.*}} @foo7({{.*}}, i8 {{.*}}) +// AVR-NOT: define {{.*}} @foo7({{.*}}, i8 {{.*}} signext {{.*}}) +// TINY-NOT: define {{.*}} @foo7({{.*}}, i8 {{.*}} signext {{.*}}) +struct s17 { + char arr[17]; +}; +char foo7(struct s17 s, char c) { + return s.arr[c]; +} + +// NOTE: The char type argument is passed via registers on avr but via stack on avrtiny. +// AVR: define {{.*}} @foo8({{.*}}, i8 {{.*}} signext {{.*}}) +// TINY-NOT: define {{.*}} @foo8({{.*}}, i8 {{.*}} signext {{.*}}) +// TINY: define {{.*}} @foo8({{.*}}, i8 {{.*}}) +struct s07 { + char arr[7]; +}; +char foo8(struct s07 s, char a) { + return s.arr[a]; +} + +// NOTE: All arguments are passed via registers on both avr and avrtiny. +// AVR: define {{.*}} i8 @foo9(i8 {{.*}} signext {{.*}}, i8 {{.*}} signext {{.*}}) +// TINY: define {{.*}} i8 @foo9(i8 {{.*}} signext {{.*}}, i8 {{.*}} signext {{.*}}) +char foo9(char a, char b) { + return a + b; +} + +// NOTE: All arguments are passed via stack on both avr and avrtiny. +// AVR: define {{.*}} i8 @foo10({{.*}}, i8 {{.*}}) +// TINY: define {{.*}} i8 @foo10({{.*}}, i8 {{.*}}) +// AVR-NOT: define {{.*}} i8 @foo10({{.*}}, i8 {{.*}} signext {{.*}}) +// TINY-NOT: define {{.*}} i8 @foo10({{.*}}, i8 {{.*}} signext {{.*}}) +struct s23 { + char arr[23]; +}; +char foo10(struct s23 s, char b) { + return s.arr[b]; +} + +// NOTE: All arguments are passed via registers on avr, while all ones are passed +// NOTE: via stack on avrtiny. +// AVR: define {{.*}} i8 @foo11({{.*}}, i8 {{.*}} signext {{.*}}) +// TINY-NOT: define {{.*}} i8 @foo11({{.*}}, i8 {{.*}} signext {{.*}}) +// TINY: define {{.*}} i8 @foo11({{.*}}, i8 {{.*}}) +char foo11(struct s09 s, char b) { + return s.arr[b]; +} + +// NOTE: All arguments are passed via registers on both avr and avrtiny. +// AVR: define {{.*}} void @foo12({{.*}}, i8 {{.*}} signext {{.*}}, i8 {{.*}} signext {{.*}}) +// TINY: define {{.*}} void @foo12({{.*}}, i8 {{.*}} signext {{.*}}, i8 {{.*}} signext {{.*}}) +struct s09 foo12(char a, char b) { + struct s09 s; + s.arr[a] = b; + return s; +} + +// NOTE: All arguments are passed via stack on both avr and avrtiny. +// AVR: define {{.*}} void @foo13({{.*}}, {{.*}}, i8 {{.*}}) +// TINY: define {{.*}} void @foo13({{.*}}, {{.*}}, i8 {{.*}}) +// AVR-NOT: define {{.*}} void @foo13({{.*}}, {{.*}}, i8 {{.*}} signext {{.*}}) +// TINY-NOT: define {{.*}} void @foo13({{.*}}, {{.*}}, i8 {{.*}} signext {{.*}}) +struct s09 foo13(struct s17 sx, char b) { + struct s09 s9; + s9.arr[b] = sx.arr[b]; + return s9; +} + +// NOTE: All arguments are passed via registers on avr, while all ones are passed +// NOTE: via stack on avrtiny. +// AVR: define {{.*}} void @foo14({{.*}}, {{.*}}, i8 {{.*}} signext {{.*}}) +// TINY-NOT: define {{.*}} void @foo14({{.*}}, {{.*}}, i8 {{.*}} signext {{.*}}) +// TINY: define {{.*}} void @foo14({{.*}}, {{.*}}, i8 {{.*}}) +struct s09 foo14(struct s07 s7, char c) { + struct s09 s9; + s9.arr[c] = s7.arr[c]; + return s9; +}