Index: llvm/include/llvm/CodeGen/GlobalISel/CallLowering.h =================================================================== --- llvm/include/llvm/CodeGen/GlobalISel/CallLowering.h +++ llvm/include/llvm/CodeGen/GlobalISel/CallLowering.h @@ -213,6 +213,9 @@ ISD::ArgFlagsTy getAttributesForArgIdx(const CallBase &Call, unsigned ArgIdx) const; + /// \returns Flags corresponding to the attributes on the return from \p Call. + ISD::ArgFlagsTy getAttributesForReturn(const CallBase &Call) const; + /// Adds flags to \p Flags based off of the attributes in \p Attrs. /// \p OpIdx is the index in \p Attrs to add flags from. void addArgFlagsFromAttributes(ISD::ArgFlagsTy &Flags, Index: llvm/lib/CodeGen/GlobalISel/CallLowering.cpp =================================================================== --- llvm/lib/CodeGen/GlobalISel/CallLowering.cpp +++ llvm/lib/CodeGen/GlobalISel/CallLowering.cpp @@ -67,6 +67,15 @@ return Flags; } +ISD::ArgFlagsTy +CallLowering::getAttributesForReturn(const CallBase &Call) const { + ISD::ArgFlagsTy Flags; + addFlagsUsingAttrFn(Flags, [&Call](Attribute::AttrKind Attr) { + return Call.hasRetAttr(Attr); + }); + return Flags; +} + void CallLowering::addArgFlagsFromAttributes(ISD::ArgFlagsTy &Flags, const AttributeList &Attrs, unsigned OpIdx) const { @@ -116,7 +125,7 @@ else Info.Callee = MachineOperand::CreateReg(GetCalleeReg(), false); - Info.OrigRet = ArgInfo{ResRegs, CB.getType(), ISD::ArgFlagsTy{}}; + Info.OrigRet = ArgInfo{ResRegs, CB.getType(), getAttributesForReturn(CB)}; if (!Info.OrigRet.Ty->isVoidTy()) setArgFlags(Info.OrigRet, AttributeList::ReturnIndex, DL, CB); Index: llvm/test/CodeGen/AArch64/GlobalISel/call-translator.ll =================================================================== --- llvm/test/CodeGen/AArch64/GlobalISel/call-translator.ll +++ llvm/test/CodeGen/AArch64/GlobalISel/call-translator.ll @@ -357,3 +357,79 @@ %call = call [1 x %size0type] @func.returns.array.size0.struct() ret void } + +; Test extends on return values. + +; This should notice that the return value from has_zext_return is zero +; extended. +declare zeroext i16 @has_zext_return() +define i32 @test_zext_return_from_callee() { + ; CHECK-LABEL: name: test_zext_return_from_callee + ; CHECK: bb.1 (%ir-block.0): + ; CHECK: ADJCALLSTACKDOWN 0, 0, implicit-def $sp, implicit $sp + ; CHECK: BL @has_zext_return, csr_aarch64_aapcs, implicit-def $lr, implicit $sp, implicit-def $w0 + ; CHECK: [[COPY:%[0-9]+]]:_(s32) = COPY $w0 + ; CHECK: [[TRUNC:%[0-9]+]]:_(s16) = G_TRUNC [[COPY]](s32) + ; CHECK: ADJCALLSTACKUP 0, 0, implicit-def $sp, implicit $sp + ; CHECK: [[ZEXT:%[0-9]+]]:_(s32) = G_ZEXT [[TRUNC]](s16) + ; CHECK: $w0 = COPY [[ZEXT]](s32) + ; CHECK: RET_ReallyLR implicit $w0 + %val = call i16 @has_zext_return() + %ext = zext i16 %val to i32 + ret i32 %ext +} + +; Same as above, but with zeroext explicitly on the call. Should produce the +; same codegen. +define i32 @test_zext_return_from_callee2() { + ; CHECK-LABEL: name: test_zext_return_from_callee2 + ; CHECK: bb.1 (%ir-block.0): + ; CHECK: ADJCALLSTACKDOWN 0, 0, implicit-def $sp, implicit $sp + ; CHECK: BL @has_zext_return, csr_aarch64_aapcs, implicit-def $lr, implicit $sp, implicit-def $w0 + ; CHECK: [[COPY:%[0-9]+]]:_(s32) = COPY $w0 + ; CHECK: [[TRUNC:%[0-9]+]]:_(s16) = G_TRUNC [[COPY]](s32) + ; CHECK: ADJCALLSTACKUP 0, 0, implicit-def $sp, implicit $sp + ; CHECK: [[ZEXT:%[0-9]+]]:_(s32) = G_ZEXT [[TRUNC]](s16) + ; CHECK: $w0 = COPY [[ZEXT]](s32) + ; CHECK: RET_ReallyLR implicit $w0 + %val = call zeroext i16 @has_zext_return() + %ext = zext i16 %val to i32 + ret i32 %ext +} + +; This should notice that the return value from has_sext_return is sign +; extended. +declare signext i16 @has_sext_return() +define i32 @test_sext_return_from_callee() { + ; CHECK-LABEL: name: test_sext_return_from_callee + ; CHECK: bb.1 (%ir-block.0): + ; CHECK: ADJCALLSTACKDOWN 0, 0, implicit-def $sp, implicit $sp + ; CHECK: BL @has_sext_return, csr_aarch64_aapcs, implicit-def $lr, implicit $sp, implicit-def $w0 + ; CHECK: [[COPY:%[0-9]+]]:_(s32) = COPY $w0 + ; CHECK: [[TRUNC:%[0-9]+]]:_(s16) = G_TRUNC [[COPY]](s32) + ; CHECK: ADJCALLSTACKUP 0, 0, implicit-def $sp, implicit $sp + ; CHECK: [[SEXT:%[0-9]+]]:_(s32) = G_SEXT [[TRUNC]](s16) + ; CHECK: $w0 = COPY [[SEXT]](s32) + ; CHECK: RET_ReallyLR implicit $w0 + %val = call i16 @has_sext_return() + %ext = sext i16 %val to i32 + ret i32 %ext +} + +; Same as above, but with signext explicitly on the call. Should produce the +; same codegen. +define i32 @test_sext_return_from_callee2() { + ; CHECK-LABEL: name: test_sext_return_from_callee2 + ; CHECK: bb.1 (%ir-block.0): + ; CHECK: ADJCALLSTACKDOWN 0, 0, implicit-def $sp, implicit $sp + ; CHECK: BL @has_sext_return, csr_aarch64_aapcs, implicit-def $lr, implicit $sp, implicit-def $w0 + ; CHECK: [[COPY:%[0-9]+]]:_(s32) = COPY $w0 + ; CHECK: [[TRUNC:%[0-9]+]]:_(s16) = G_TRUNC [[COPY]](s32) + ; CHECK: ADJCALLSTACKUP 0, 0, implicit-def $sp, implicit $sp + ; CHECK: [[SEXT:%[0-9]+]]:_(s32) = G_SEXT [[TRUNC]](s16) + ; CHECK: $w0 = COPY [[SEXT]](s32) + ; CHECK: RET_ReallyLR implicit $w0 + %val = call signext i16 @has_sext_return() + %ext = sext i16 %val to i32 + ret i32 %ext +}