Skip to content

Commit d5167c8

Browse files
authored
[DAGCombiner] Allow tryToFoldExtOfLoad to use a sextload for zext nneg. (#81714)
If the load is used by any signed setccs, we can use a sextload instead of zextload. Then we don't have to give up on extending the load.
1 parent 8074761 commit d5167c8

File tree

2 files changed

+57
-10
lines changed

2 files changed

+57
-10
lines changed

llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13171,20 +13171,39 @@ static SDValue tryToFoldExtOfExtload(SelectionDAG &DAG, DAGCombiner &Combiner,
1317113171

1317213172
// fold ([s|z]ext (load x)) -> ([s|z]ext (truncate ([s|z]extload x)))
1317313173
// Only generate vector extloads when 1) they're legal, and 2) they are
13174-
// deemed desirable by the target.
13174+
// deemed desirable by the target. NonNegZExt can be set to true if a zero
13175+
// extend has the nonneg flag to allow use of sextload if profitable.
1317513176
static SDValue tryToFoldExtOfLoad(SelectionDAG &DAG, DAGCombiner &Combiner,
1317613177
const TargetLowering &TLI, EVT VT,
1317713178
bool LegalOperations, SDNode *N, SDValue N0,
1317813179
ISD::LoadExtType ExtLoadType,
13179-
ISD::NodeType ExtOpc) {
13180+
ISD::NodeType ExtOpc,
13181+
bool NonNegZExt = false) {
13182+
if (!ISD::isNON_EXTLoad(N0.getNode()) || !ISD::isUNINDEXEDLoad(N0.getNode()))
13183+
return {};
13184+
13185+
// If this is zext nneg, see if it would make sense to treat it as a sext.
13186+
if (NonNegZExt) {
13187+
assert(ExtLoadType == ISD::ZEXTLOAD && ExtOpc == ISD::ZERO_EXTEND &&
13188+
"Unexpected load type or opcode");
13189+
for (SDNode *User : N0->uses()) {
13190+
if (User->getOpcode() == ISD::SETCC) {
13191+
ISD::CondCode CC = cast<CondCodeSDNode>(User->getOperand(2))->get();
13192+
if (ISD::isSignedIntSetCC(CC)) {
13193+
ExtLoadType = ISD::SEXTLOAD;
13194+
ExtOpc = ISD::SIGN_EXTEND;
13195+
break;
13196+
}
13197+
}
13198+
}
13199+
}
13200+
1318013201
// TODO: isFixedLengthVector() should be removed and any negative effects on
1318113202
// code generation being the result of that target's implementation of
1318213203
// isVectorLoadExtDesirable().
13183-
if (!ISD::isNON_EXTLoad(N0.getNode()) ||
13184-
!ISD::isUNINDEXEDLoad(N0.getNode()) ||
13185-
((LegalOperations || VT.isFixedLengthVector() ||
13186-
!cast<LoadSDNode>(N0)->isSimple()) &&
13187-
!TLI.isLoadExtLegal(ExtLoadType, VT, N0.getValueType())))
13204+
if ((LegalOperations || VT.isFixedLengthVector() ||
13205+
!cast<LoadSDNode>(N0)->isSimple()) &&
13206+
!TLI.isLoadExtLegal(ExtLoadType, VT, N0.getValueType()))
1318813207
return {};
1318913208

1319013209
bool DoXform = true;
@@ -13780,9 +13799,9 @@ SDValue DAGCombiner::visitZERO_EXTEND(SDNode *N) {
1378013799
}
1378113800

1378213801
// Try to simplify (zext (load x)).
13783-
if (SDValue foldedExt =
13784-
tryToFoldExtOfLoad(DAG, *this, TLI, VT, LegalOperations, N, N0,
13785-
ISD::ZEXTLOAD, ISD::ZERO_EXTEND))
13802+
if (SDValue foldedExt = tryToFoldExtOfLoad(
13803+
DAG, *this, TLI, VT, LegalOperations, N, N0, ISD::ZEXTLOAD,
13804+
ISD::ZERO_EXTEND, N->getFlags().hasNonNeg()))
1378613805
return foldedExt;
1378713806

1378813807
if (SDValue foldedExt =
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 4
2+
; RUN: llc < %s -mtriple=riscv64 | FileCheck %s
3+
4+
define i8 @zext_nonneg_load_i16(ptr %x, ptr %y) {
5+
; CHECK-LABEL: zext_nonneg_load_i16:
6+
; CHECK: # %bb.0:
7+
; CHECK-NEXT: lh a0, 0(a0)
8+
; CHECK-NEXT: bltz a0, .LBB0_2
9+
; CHECK-NEXT: # %bb.1: # %cont
10+
; CHECK-NEXT: add a0, a1, a0
11+
; CHECK-NEXT: lbu a0, 0(a0)
12+
; CHECK-NEXT: ret
13+
; CHECK-NEXT: .LBB0_2: # %exit
14+
; CHECK-NEXT: li a0, 0
15+
; CHECK-NEXT: ret
16+
%a = load i16, ptr %x
17+
%b = icmp slt i16 %a, 0
18+
br i1 %b, label %exit, label %cont
19+
20+
cont:
21+
%c = zext nneg i16 %a to i64
22+
%d = getelementptr i8, ptr %y, i64 %c
23+
%e = load i8, ptr %d
24+
ret i8 %e
25+
26+
exit:
27+
ret i8 0
28+
}

0 commit comments

Comments
 (0)