Skip to content

Commit 8972e9f

Browse files
committed
[DAG] Improve known bits of Zext/Sext loads with range metadata
This extends the known bits for extending loads which have range metadata, handling the range metadata on the original memory type, extending that to the correct BitWidths.
1 parent 2e3de99 commit 8972e9f

File tree

2 files changed

+30
-22
lines changed

2 files changed

+30
-22
lines changed

llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3612,32 +3612,42 @@ KnownBits SelectionDAG::computeKnownBits(SDValue Op, const APInt &DemandedElts,
36123612
}
36133613
}
36143614
}
3615-
} else if (ISD::isZEXTLoad(Op.getNode()) && Op.getResNo() == 0) {
3616-
// If this is a ZEXTLoad and we are looking at the loaded value.
3617-
EVT VT = LD->getMemoryVT();
3618-
unsigned MemBits = VT.getScalarSizeInBits();
3619-
Known.Zero.setBitsFrom(MemBits);
3620-
} else if (const MDNode *Ranges = LD->getRanges()) {
3621-
EVT VT = LD->getValueType(0);
3622-
3623-
// TODO: Handle for extending loads
3624-
if (LD->getExtensionType() == ISD::NON_EXTLOAD) {
3615+
} else if (Op.getResNo() == 0) {
3616+
KnownBits Known0(!LD->getMemoryVT().isScalableVT()
3617+
? LD->getMemoryVT().getSizeInBits()
3618+
: BitWidth);
3619+
EVT VT = Op.getValueType();
3620+
// Fill in any known bits from range information. There are 3 types being
3621+
// used. The results VT (same vector elt size as BitWidth), the loaded
3622+
// MemoryVT (which may or may not be vector) and the range VTs original
3623+
// type. The range matadata needs the full range (i.e
3624+
// MemoryVT().getSizeInBits()), which is truncated to the correct elt size
3625+
// if it is know. These are then extended to the original VT sizes below.
3626+
if (const MDNode *MD = LD->getRanges()) {
3627+
computeKnownBitsFromRangeMetadata(*MD, Known0);
36253628
if (VT.isVector()) {
36263629
// Handle truncation to the first demanded element.
36273630
// TODO: Figure out which demanded elements are covered
36283631
if (DemandedElts != 1 || !getDataLayout().isLittleEndian())
36293632
break;
3633+
Known0 = Known0.trunc(BitWidth);
3634+
}
3635+
}
36303636

3631-
// Handle the case where a load has a vector type, but scalar memory
3632-
// with an attached range.
3633-
EVT MemVT = LD->getMemoryVT();
3634-
KnownBits KnownFull(MemVT.getSizeInBits());
3637+
if (LD->getMemoryVT().isVector())
3638+
Known0 = Known0.trunc(LD->getMemoryVT().getScalarSizeInBits());
36353639

3636-
computeKnownBitsFromRangeMetadata(*Ranges, KnownFull);
3637-
Known = KnownFull.trunc(BitWidth);
3638-
} else
3639-
computeKnownBitsFromRangeMetadata(*Ranges, Known);
3640-
}
3640+
// Extend the Known bits from memory to the size of the result.
3641+
if (ISD::isZEXTLoad(Op.getNode()))
3642+
Known = Known0.zext(BitWidth);
3643+
else if (ISD::isSEXTLoad(Op.getNode()))
3644+
Known = Known0.sext(BitWidth);
3645+
else if (ISD::isEXTLoad(Op.getNode()))
3646+
Known = Known0.anyext(BitWidth);
3647+
else
3648+
Known = Known0;
3649+
assert(Known.getBitWidth() == BitWidth);
3650+
return Known;
36413651
}
36423652
break;
36433653
}

llvm/test/CodeGen/AArch64/setcc_knownbits.ll

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,7 @@ define noundef i1 @logger(i32 noundef %logLevel, ptr %ea, ptr %pll) {
2121
; CHECK-NEXT: ret
2222
; CHECK-NEXT: .LBB1_2: // %land.rhs
2323
; CHECK-NEXT: ldr x8, [x1]
24-
; CHECK-NEXT: ldrb w8, [x8]
25-
; CHECK-NEXT: cmp w8, #0
26-
; CHECK-NEXT: cset w0, ne
24+
; CHECK-NEXT: ldrb w0, [x8]
2725
; CHECK-NEXT: ret
2826
entry:
2927
%0 = load i32, ptr %pll, align 4

0 commit comments

Comments
 (0)