Skip to content

[DAGCombiner] Be more careful about looking through extends and trunc… #91375

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 2 commits into from
May 8, 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
21 changes: 12 additions & 9 deletions llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8728,15 +8728,16 @@ static std::optional<bool> isBigEndian(const ArrayRef<int64_t> ByteOffsets,
return BigEndian;
}

// Look through one layer of truncate or extend.
static SDValue stripTruncAndExt(SDValue Value) {
switch (Value.getOpcode()) {
case ISD::TRUNCATE:
case ISD::ZERO_EXTEND:
case ISD::SIGN_EXTEND:
case ISD::ANY_EXTEND:
return stripTruncAndExt(Value.getOperand(0));
return Value.getOperand(0);
}
return Value;
return SDValue();
}

/// Match a pattern where a wide type scalar value is stored by several narrow
Expand Down Expand Up @@ -8849,16 +8850,18 @@ SDValue DAGCombiner::mergeTruncStores(StoreSDNode *N) {
}

// Stores must share the same source value with different offsets.
// Truncate and extends should be stripped to get the single source value.
if (!SourceValue)
SourceValue = WideVal;
else if (stripTruncAndExt(SourceValue) != stripTruncAndExt(WideVal))
return SDValue();
else if (SourceValue.getValueType() != WideVT) {
if (WideVal.getValueType() == WideVT ||
WideVal.getScalarValueSizeInBits() >
SourceValue.getScalarValueSizeInBits())
else if (SourceValue != WideVal) {
// Truncate and extends can be stripped to see if the values are related.
if (stripTruncAndExt(SourceValue) != WideVal &&
stripTruncAndExt(WideVal) != SourceValue)
return SDValue();

if (WideVal.getScalarValueSizeInBits() >
SourceValue.getScalarValueSizeInBits())
SourceValue = WideVal;

// Give up if the source value type is smaller than the store size.
if (SourceValue.getScalarValueSizeInBits() < WideVT.getScalarSizeInBits())
return SDValue();
Expand Down
8 changes: 6 additions & 2 deletions llvm/test/CodeGen/AArch64/pr90936.ll
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,12 @@ bb:
define void @g(i32 %arg, ptr %arg1) {
; CHECK-LABEL: g:
; CHECK: // %bb.0: // %bb
; CHECK-NEXT: and w8, w0, #0xff
; CHECK-NEXT: str w8, [x1]
; CHECK-NEXT: lsr w8, w0, #8
; CHECK-NEXT: lsr w9, w0, #16
; CHECK-NEXT: strb w0, [x1]
; CHECK-NEXT: strb wzr, [x1, #3]
; CHECK-NEXT: strb w8, [x1, #1]
; CHECK-NEXT: strb w9, [x1, #2]
; CHECK-NEXT: ret
bb:
%i = trunc i32 %arg to i8
Expand Down