Skip to content

Commit 4793c2c

Browse files
authored
[DAGCombiner][RISCV] Prefer to sext i32 non-negative values (#65984)
By default, `DAGCombiner` folds `sext x` to `zext x` when `x` is non-negative. It will generate redundant `zext` inst seq on riscv64 (typically `slli (srli x, 32), 32`). godbolt: https://godbolt.org/z/osf6adP1o This patch applies the transform iff `zext` is **cheaper** than `sext`.
1 parent ea42c4a commit 4793c2c

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13271,7 +13271,8 @@ SDValue DAGCombiner::visitSIGN_EXTEND(SDNode *N) {
1327113271
return V;
1327213272

1327313273
// fold (sext x) -> (zext x) if the sign bit is known zero.
13274-
if ((!LegalOperations || TLI.isOperationLegal(ISD::ZERO_EXTEND, VT)) &&
13274+
if (!TLI.isSExtCheaperThanZExt(N0.getValueType(), VT) &&
13275+
(!LegalOperations || TLI.isOperationLegal(ISD::ZERO_EXTEND, VT)) &&
1327513276
DAG.SignBitIsZero(N0))
1327613277
return DAG.getNode(ISD::ZERO_EXTEND, DL, VT, N0);
1327713278

llvm/test/CodeGen/RISCV/aext-to-sext.ll

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
2-
; RUN: llc -mtriple=riscv64 -verify-machineinstrs < %s \
2+
; RUN: llc -mtriple=riscv64 -mattr=+m -verify-machineinstrs < %s \
33
; RUN: | FileCheck %s -check-prefix=RV64I
44

55
; Make sure we don't generate an addi in the loop in
@@ -100,3 +100,14 @@ merge:
100100
%d = zext i32 %b to i64
101101
ret i64 %d
102102
}
103+
104+
; We prefer to sign extend i32 non-negative values. The default behavior in
105+
; DAGCombiner is zero extend. We have a target hook to override it.
106+
define signext i32 @square(i32 signext %num) {
107+
; RV64I-LABEL: square:
108+
; RV64I: # %bb.0:
109+
; RV64I-NEXT: mulw a0, a0, a0
110+
; RV64I-NEXT: ret
111+
%mul = mul nsw i32 %num, %num
112+
ret i32 %mul
113+
}

0 commit comments

Comments
 (0)