[llvm] [SelectionDAG] Require same MMO base in relative-align no-alias proof (PR #217880)
via llvm-commits
llvm-commits at lists.llvm.org
Sun Aug 23 20:30:21 PDT 2026
https://github.com/chenqiant updated https://github.com/llvm/llvm-project/pull/217880
>From 6fabf9d4398361e54735cf8acf51b538626db99b Mon Sep 17 00:00:00 2001
From: chenqian <chen.qian at espressif.com>
Date: Mon, 24 Aug 2026 10:53:19 +0800
Subject: [PATCH 1/2] [RISCV] Pin load-all-then-store-all for overlapping
12-byte memmove
A 12-byte memmove expands to three i32 pairs. Backward overlap is only
correct if every source word is loaded before the first store.
Withdraw the DAGCombiner same-MMO-Value guard: the relative-align
no-alias proof is base-independent, and dst/src alignments 16 vs 8
never entered that proof.
Assisted-by: Cursor
Co-authored-by: Cursor <cursoragent at cursor.com>
---
.../CodeGen/RISCV/memmove-overlap-order.ll | 24 +++++++++++++++++++
1 file changed, 24 insertions(+)
create mode 100644 llvm/test/CodeGen/RISCV/memmove-overlap-order.ll
diff --git a/llvm/test/CodeGen/RISCV/memmove-overlap-order.ll b/llvm/test/CodeGen/RISCV/memmove-overlap-order.ll
new file mode 100644
index 0000000000000..9b877bb91ba41
--- /dev/null
+++ b/llvm/test/CodeGen/RISCV/memmove-overlap-order.ll
@@ -0,0 +1,24 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
+; RUN: llc -mtriple=riscv32 -O2 < %s | FileCheck %s
+
+; A 12-byte memmove with dst 16-byte aligned and src 8-byte aligned lowers to
+; three i32 load/store pairs (offsets 0/4/8). For a backward copy (runtime
+; dst > src with 0 < dst-src < 12) every source chunk must be loaded before
+; the first store, otherwise a store to dst[0] clobbers src[4..8) before it
+; is read. Pin load-all-then-store-all so a later DAG combine cannot silently
+; reintroduce store-before-load.
+define void @memmove_overlap_12(ptr %dst, ptr %src) {
+; CHECK-LABEL: memmove_overlap_12:
+; CHECK: # %bb.0:
+; CHECK-NEXT: lw [[L0:.*]], 0(a1)
+; CHECK-NEXT: lw [[L4:.*]], 4(a1)
+; CHECK-NEXT: lw [[L8:.*]], 8(a1)
+; CHECK-NEXT: sw [[L0]], 0(a0)
+; CHECK-NEXT: sw [[L4]], 4(a0)
+; CHECK-NEXT: sw [[L8]], 8(a0)
+; CHECK-NEXT: ret
+entry:
+ tail call void @llvm.memmove.p0.p0.i32(ptr align 16 %dst, ptr align 8 %src, i32 12, i1 false)
+ ret void
+}
+declare void @llvm.memmove.p0.p0.i32(ptr, ptr, i32, i1)
>From 9fa0e6c70789024e064d6d10f305d30f17dd2bf4 Mon Sep 17 00:00:00 2001
From: chenqian <chen.qian at espressif.com>
Date: Mon, 24 Aug 2026 11:29:41 +0800
Subject: [PATCH 2/2] [SelectionDAG] Require same MMO Value for relative-align
no-alias
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
The relative-align window in DAGCombiner::mayAlias only runs when both
MMOs share getBaseAlign(). On esp_22.x that fired across memmove %dst vs
%src (both align 8) and interleaved lw/sw. Require the same non-null
getValue() first.
RISCV lit uses align-8 memmove (8 and 12 bytes), not align 16 vs 8 —
unequal alignments never entered the proof.
Assisted-by: Cursor
Co-authored-by: Cursor <cursoragent at cursor.com>
---
llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp | 8 +++--
.../CodeGen/RISCV/memmove-overlap-order.ll | 36 +++++++++++++------
2 files changed, 32 insertions(+), 12 deletions(-)
diff --git a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
index 09e26fe2aa58c..e93c85bc3a0c5 100644
--- a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
@@ -32061,7 +32061,9 @@ bool DAGCombiner::mayAlias(SDNode *Op0, SDNode *Op1) const {
// alignment compared to the size and offset of the access, we may be able
// to prove they do not alias. This check is conservative for now to catch
// cases created by splitting vector types, it only works when the offsets are
- // multiples of the size of the data.
+ // multiples of the size of the data. The offsets are relative to each MMO's
+ // Value, so the window-separation argument is only sound when both accesses
+ // are relative to the same object.
int64_t SrcValOffset0 = MUC0.MMO->getOffset();
int64_t SrcValOffset1 = MUC1.MMO->getOffset();
Align OrigAlignment0 = MUC0.MMO->getBaseAlign();
@@ -32069,7 +32071,9 @@ bool DAGCombiner::mayAlias(SDNode *Op0, SDNode *Op1) const {
LocationSize Size0 = MUC0.NumBytes;
LocationSize Size1 = MUC1.NumBytes;
- if (OrigAlignment0 == OrigAlignment1 && SrcValOffset0 != SrcValOffset1 &&
+ if (MUC0.MMO->getValue() && MUC1.MMO->getValue() &&
+ MUC0.MMO->getValue() == MUC1.MMO->getValue() &&
+ OrigAlignment0 == OrigAlignment1 && SrcValOffset0 != SrcValOffset1 &&
Size0.hasValue() && Size1.hasValue() && !Size0.isScalable() &&
!Size1.isScalable() && Size0 == Size1 &&
OrigAlignment0 > Size0.getValue().getKnownMinValue() &&
diff --git a/llvm/test/CodeGen/RISCV/memmove-overlap-order.ll b/llvm/test/CodeGen/RISCV/memmove-overlap-order.ll
index 9b877bb91ba41..233259d570734 100644
--- a/llvm/test/CodeGen/RISCV/memmove-overlap-order.ll
+++ b/llvm/test/CodeGen/RISCV/memmove-overlap-order.ll
@@ -1,14 +1,30 @@
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
; RUN: llc -mtriple=riscv32 -O2 < %s | FileCheck %s
-; A 12-byte memmove with dst 16-byte aligned and src 8-byte aligned lowers to
-; three i32 load/store pairs (offsets 0/4/8). For a backward copy (runtime
-; dst > src with 0 < dst-src < 12) every source chunk must be loaded before
-; the first store, otherwise a store to dst[0] clobbers src[4..8) before it
-; is read. Pin load-all-then-store-all so a later DAG combine cannot silently
-; reintroduce store-before-load.
-define void @memmove_overlap_12(ptr %dst, ptr %src) {
-; CHECK-LABEL: memmove_overlap_12:
+; Both pointers align 8, 8-byte memmove → two i32 pairs. This is the case that
+; actually enters DAGCombiner::mayAlias's relative-align proof
+; (OrigAlignment0 == OrigAlignment1 && align > size).
+;
+; On esp_22.x without the same-Value guard, llc interleaved:
+; lw 0(a1) ; sw 0(a0) ; lw 4(a1) ; sw 4(a0)
+; If the runtime objects overlap backward, that store to dst[0] can clobber
+; src[4] before it is read. After the guard, expansion is load-all then
+; store-all (same order current main already emits for this IR).
+define void @memmove_align8_8(ptr %dst, ptr %src) {
+; CHECK-LABEL: memmove_align8_8:
+; CHECK: # %bb.0:
+; CHECK-NEXT: lw [[L0:.*]], 0(a1)
+; CHECK-NEXT: lw [[L4:.*]], 4(a1)
+; CHECK-NEXT: sw [[L0]], 0(a0)
+; CHECK-NEXT: sw [[L4]], 4(a0)
+; CHECK-NEXT: ret
+ tail call void @llvm.memmove.p0.p0.i32(ptr align 8 %dst, ptr align 8 %src, i32 8, i1 false)
+ ret void
+}
+
+; 12-byte, both align 8: three i32 pairs. Same proof, extra offsets 0/4/8.
+define void @memmove_align8_12(ptr %dst, ptr %src) {
+; CHECK-LABEL: memmove_align8_12:
; CHECK: # %bb.0:
; CHECK-NEXT: lw [[L0:.*]], 0(a1)
; CHECK-NEXT: lw [[L4:.*]], 4(a1)
@@ -17,8 +33,8 @@ define void @memmove_overlap_12(ptr %dst, ptr %src) {
; CHECK-NEXT: sw [[L4]], 4(a0)
; CHECK-NEXT: sw [[L8]], 8(a0)
; CHECK-NEXT: ret
-entry:
- tail call void @llvm.memmove.p0.p0.i32(ptr align 16 %dst, ptr align 8 %src, i32 12, i1 false)
+ tail call void @llvm.memmove.p0.p0.i32(ptr align 8 %dst, ptr align 8 %src, i32 12, i1 false)
ret void
}
+
declare void @llvm.memmove.p0.p0.i32(ptr, ptr, i32, i1)
More information about the llvm-commits
mailing list