Skip to content

[GISel] Convert zext nneg to sext if it is cheaper #93842

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions llvm/lib/CodeGen/GlobalISel/IRTranslator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1569,6 +1569,17 @@ bool IRTranslator::translateCast(unsigned Opcode, const User &U,

Register Op = getOrCreateVReg(*U.getOperand(0));
Register Res = getOrCreateVReg(U);

// Convert zext nneg to sext if it is preferred form for the target.
if (Opcode == TargetOpcode::G_ZEXT && (Flags & MachineInstr::NonNeg)) {
EVT SrcVT = TLI->getValueType(*DL, U.getOperand(0)->getType());
EVT DstVT = TLI->getValueType(*DL, U.getType());
if (TLI->isSExtCheaperThanZExt(SrcVT, DstVT)) {
Opcode = TargetOpcode::G_SEXT;
Flags = 0;
}
}

MIRBuilder.buildInstr(Opcode, {Res}, {Op}, Flags);
return true;
}
Expand Down
10 changes: 10 additions & 0 deletions llvm/test/CodeGen/RISCV/GlobalISel/alu-roundtrip-rv64.ll
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,13 @@ entry:
%0 = urem i64 %a, %b
ret i64 %0
}

define i64 @zext_nneg_i32_i64(i32 %a) {
; RV64IM-LABEL: zext_nneg_i32_i64:
; RV64IM: # %bb.0: # %entry
; RV64IM-NEXT: sext.w a0, a0
; RV64IM-NEXT: ret
entry:
%b = zext nneg i32 %a to i64
ret i64 %b
}
18 changes: 18 additions & 0 deletions llvm/test/CodeGen/RISCV/GlobalISel/irtranslator/zext-nneg-rv64.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
; NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py UTC_ARGS: --version 5
; RUN: llc -mtriple=riscv64 -global-isel -stop-after=irtranslator -verify-machineinstrs < %s \
; RUN: | FileCheck -check-prefix=RV64I %s

define i64 @zext_nneg_i32_i64(i32 %a) {
; RV64I-LABEL: name: zext_nneg_i32_i64
; RV64I: bb.1.entry:
; RV64I-NEXT: liveins: $x10
; RV64I-NEXT: {{ $}}
; RV64I-NEXT: [[COPY:%[0-9]+]]:_(s64) = COPY $x10
; RV64I-NEXT: [[TRUNC:%[0-9]+]]:_(s32) = G_TRUNC [[COPY]](s64)
; RV64I-NEXT: [[SEXT:%[0-9]+]]:_(s64) = G_SEXT [[TRUNC]](s32)
; RV64I-NEXT: $x10 = COPY [[SEXT]](s64)
; RV64I-NEXT: PseudoRET implicit $x10
entry:
%b = zext nneg i32 %a to i64
ret i64 %b
}
Loading