[llvm] [AVR] Temp fix for getPostIndexedAddressParts() (PR #145040)

Tom Vijlbrief via llvm-commits llvm-commits at lists.llvm.org
Sat Jun 21 12:34:27 PDT 2025


https://github.com/tomtor updated https://github.com/llvm/llvm-project/pull/145040

>From 938ee7a850ac98a67a9bd505e0d1e9c6249fe6fc Mon Sep 17 00:00:00 2001
From: Tom Vijlbrief <tvijlbrief at gmail.com>
Date: Tue, 17 Jun 2025 15:11:51 +0200
Subject: [PATCH 1/3] [AVR] Temp fix in getPostIndexedAddressParts()

---
 llvm/lib/Target/AVR/AVRISelLowering.cpp |  6 ++++++
 llvm/test/CodeGen/AVR/issue-145040.ll   | 23 +++++++++++++++++++++++
 2 files changed, 29 insertions(+)
 create mode 100644 llvm/test/CodeGen/AVR/issue-145040.ll

diff --git a/llvm/lib/Target/AVR/AVRISelLowering.cpp b/llvm/lib/Target/AVR/AVRISelLowering.cpp
index 9747ad0c5cd58..452df0cd0b32e 100644
--- a/llvm/lib/Target/AVR/AVRISelLowering.cpp
+++ b/llvm/lib/Target/AVR/AVRISelLowering.cpp
@@ -1114,6 +1114,12 @@ bool AVRTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
       if (AVR::isProgramMemoryAccess(LD))
         return false;
 
+    // FIXME: We temporarily apply a test which prevents generating incorrect code
+    // (see https://github.com/llvm/llvm-project/issues/143247 )
+    // until we determined and fixed the root cause.
+    if (Op->getOperand(0)->hasOneUse())
+      return false;
+
     Base = Op->getOperand(0);
     Offset = DAG.getConstant(RHSC, DL, MVT::i8);
     AM = ISD::POST_INC;
diff --git a/llvm/test/CodeGen/AVR/issue-145040.ll b/llvm/test/CodeGen/AVR/issue-145040.ll
new file mode 100644
index 0000000000000..481c5fa852a1d
--- /dev/null
+++ b/llvm/test/CodeGen/AVR/issue-145040.ll
@@ -0,0 +1,23 @@
+; RUN: llc < %s -O=2 -mtriple=avr-none --mcpu=avr128db28 -verify-machineinstrs | FileCheck %s
+
+declare dso_local void @nil(i16 noundef) local_unnamed_addr addrspace(1) #1
+!3 = !{!4, !4, i64 0}
+!4 = !{!"omnipotent char", !5, i64 0}
+!5 = !{!"Simple C/C++ TBAA"}
+
+define void @complex_sbi() {
+; CHECK: sbi 1, 7
+entry:
+  br label %while.cond
+while.cond:                                       ; preds = %while.cond, %entry
+  %s.0 = phi i16 [ 0, %entry ], [ %inc, %while.cond ]
+  %inc = add nuw nsw i16 %s.0, 1
+  %0 = load volatile i8, ptr inttoptr (i16 1 to ptr), align 1, !tbaa !3
+  %or = or i8 %0, -128
+  store volatile i8 %or, ptr inttoptr (i16 1 to ptr), align 1, !tbaa !3
+  %and = and i16 %inc, 15
+  %add = add nuw nsw i16 %and, 1
+  tail call addrspace(1) void @nil(i16 noundef %add) #2
+  br label %while.cond
+}
+

>From 7dc79da163eb0655505e00dddcc65da865844771 Mon Sep 17 00:00:00 2001
From: Tom Vijlbrief <tvijlbrief at gmail.com>
Date: Sat, 21 Jun 2025 19:01:53 +0200
Subject: [PATCH 2/3] fix comment length

---
 llvm/lib/Target/AVR/AVRISelLowering.cpp | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/llvm/lib/Target/AVR/AVRISelLowering.cpp b/llvm/lib/Target/AVR/AVRISelLowering.cpp
index 452df0cd0b32e..9866d9820941d 100644
--- a/llvm/lib/Target/AVR/AVRISelLowering.cpp
+++ b/llvm/lib/Target/AVR/AVRISelLowering.cpp
@@ -1114,8 +1114,8 @@ bool AVRTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
       if (AVR::isProgramMemoryAccess(LD))
         return false;
 
-    // FIXME: We temporarily apply a test which prevents generating incorrect code
-    // (see https://github.com/llvm/llvm-project/issues/143247 )
+    // FIXME: We temporarily apply a test which prevents generating incorrect
+    // code (see https://github.com/llvm/llvm-project/issues/143247 )
     // until we determined and fixed the root cause.
     if (Op->getOperand(0)->hasOneUse())
       return false;

>From d46bebcc831da4a8a98ebfe8efc0042c2c576a9e Mon Sep 17 00:00:00 2001
From: Tom Vijlbrief <tvijlbrief at gmail.com>
Date: Sat, 21 Jun 2025 21:34:11 +0200
Subject: [PATCH 3/3] Try Patryk27 fix

---
 llvm/lib/Target/AVR/AVRISelLowering.cpp | 16 ++++++++++++----
 1 file changed, 12 insertions(+), 4 deletions(-)

diff --git a/llvm/lib/Target/AVR/AVRISelLowering.cpp b/llvm/lib/Target/AVR/AVRISelLowering.cpp
index 9866d9820941d..bfd7f1c6b07eb 100644
--- a/llvm/lib/Target/AVR/AVRISelLowering.cpp
+++ b/llvm/lib/Target/AVR/AVRISelLowering.cpp
@@ -1114,14 +1114,22 @@ bool AVRTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
       if (AVR::isProgramMemoryAccess(LD))
         return false;
 
-    // FIXME: We temporarily apply a test which prevents generating incorrect
-    // code (see https://github.com/llvm/llvm-project/issues/143247 )
-    // until we determined and fixed the root cause.
-    if (Op->getOperand(0)->hasOneUse())
+    SDValue Ptr;
+    if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
+      Ptr = LD->getBasePtr();
+    } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
+      Ptr = ST->getBasePtr();
+    } else
       return false;
 
     Base = Op->getOperand(0);
     Offset = DAG.getConstant(RHSC, DL, MVT::i8);
+
+    // Post-indexing updates the base, so it's not a valid transform
+    // if that's not the same as the load's pointer.
+    if (Ptr != Base)
+      return false;
+
     AM = ISD::POST_INC;
 
     return true;



More information about the llvm-commits mailing list