Skip to content

[DAGCombiner] Allow tryToFoldExtOfLoad to use a sextload for zext nneg. #81714

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

Merged
merged 6 commits into from
Feb 17, 2024
Merged
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
39 changes: 29 additions & 10 deletions llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13154,20 +13154,39 @@ static SDValue tryToFoldExtOfExtload(SelectionDAG &DAG, DAGCombiner &Combiner,

// fold ([s|z]ext (load x)) -> ([s|z]ext (truncate ([s|z]extload x)))
// Only generate vector extloads when 1) they're legal, and 2) they are
// deemed desirable by the target.
// deemed desirable by the target. NonNegZExt can be set to true if a zero
// extend has the nonneg flag to allow use of sextload if profitable.
static SDValue tryToFoldExtOfLoad(SelectionDAG &DAG, DAGCombiner &Combiner,
const TargetLowering &TLI, EVT VT,
bool LegalOperations, SDNode *N, SDValue N0,
ISD::LoadExtType ExtLoadType,
ISD::NodeType ExtOpc) {
ISD::NodeType ExtOpc,
bool NonNegZExt = false) {
if (!ISD::isNON_EXTLoad(N0.getNode()) || !ISD::isUNINDEXEDLoad(N0.getNode()))
return {};

// If this is zext nneg, see if it would make sense to treat it as a sext.
if (NonNegZExt) {
assert(ExtLoadType == ISD::ZEXTLOAD && ExtOpc == ISD::ZERO_EXTEND &&
"Unexpected load type or opcode");
for (SDNode *User : N0->uses()) {
if (User->getOpcode() == ISD::SETCC) {
ISD::CondCode CC = cast<CondCodeSDNode>(User->getOperand(2))->get();
if (ISD::isSignedIntSetCC(CC)) {
ExtLoadType = ISD::SEXTLOAD;
ExtOpc = ISD::SIGN_EXTEND;
break;
}
}
}
}

// TODO: isFixedLengthVector() should be removed and any negative effects on
// code generation being the result of that target's implementation of
// isVectorLoadExtDesirable().
if (!ISD::isNON_EXTLoad(N0.getNode()) ||
!ISD::isUNINDEXEDLoad(N0.getNode()) ||
((LegalOperations || VT.isFixedLengthVector() ||
!cast<LoadSDNode>(N0)->isSimple()) &&
!TLI.isLoadExtLegal(ExtLoadType, VT, N0.getValueType())))
if ((LegalOperations || VT.isFixedLengthVector() ||
!cast<LoadSDNode>(N0)->isSimple()) &&
!TLI.isLoadExtLegal(ExtLoadType, VT, N0.getValueType()))
return {};

bool DoXform = true;
Expand Down Expand Up @@ -13763,9 +13782,9 @@ SDValue DAGCombiner::visitZERO_EXTEND(SDNode *N) {
}

// Try to simplify (zext (load x)).
if (SDValue foldedExt =
tryToFoldExtOfLoad(DAG, *this, TLI, VT, LegalOperations, N, N0,
ISD::ZEXTLOAD, ISD::ZERO_EXTEND))
if (SDValue foldedExt = tryToFoldExtOfLoad(
DAG, *this, TLI, VT, LegalOperations, N, N0, ISD::ZEXTLOAD,
ISD::ZERO_EXTEND, N->getFlags().hasNonNeg()))
return foldedExt;

if (SDValue foldedExt =
Expand Down
28 changes: 28 additions & 0 deletions llvm/test/CodeGen/RISCV/load-setcc-combine.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 4
; RUN: llc < %s -mtriple=riscv64 | FileCheck %s

define i8 @zext_nonneg_load_i16(ptr %x, ptr %y) {
; CHECK-LABEL: zext_nonneg_load_i16:
; CHECK: # %bb.0:
; CHECK-NEXT: lh a0, 0(a0)
; CHECK-NEXT: bltz a0, .LBB0_2
; CHECK-NEXT: # %bb.1: # %cont
; CHECK-NEXT: add a0, a1, a0
; CHECK-NEXT: lbu a0, 0(a0)
; CHECK-NEXT: ret
; CHECK-NEXT: .LBB0_2: # %exit
; CHECK-NEXT: li a0, 0
; CHECK-NEXT: ret
%a = load i16, ptr %x
%b = icmp slt i16 %a, 0
br i1 %b, label %exit, label %cont

cont:
%c = zext nneg i16 %a to i64
%d = getelementptr i8, ptr %y, i64 %c
%e = load i8, ptr %d
ret i8 %e

exit:
ret i8 0
}