[llvm] [AArch64][ARM] Do not generate postinc addressing mode for volatile accesses (PR #196305)

via llvm-commits llvm-commits at lists.llvm.org
Thu May 7 05:45:58 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-backend-arm

Author: Nikita Popov (nikic)

<details>
<summary>Changes</summary>

Instructions performing register writeback do not set a valid instruction syndrome, making it impossible to handle MMIO in protected hypervisors. Suppress the use of postinc addressing modes for volatile accesses, which may be used to interact with MMIO.

The original report was against AArch64, but going by the ARM docs, I assume that the issue applies equally to ARM, so making the same change there.

Fixes https://github.com/llvm/llvm-project/issues/173014.

---
Full diff: https://github.com/llvm/llvm-project/pull/196305.diff


4 Files Affected:

- (modified) llvm/lib/Target/AArch64/AArch64ISelLowering.cpp (+9) 
- (modified) llvm/lib/Target/ARM/ARMISelLowering.cpp (+11) 
- (added) llvm/test/CodeGen/AArch64/volatile-postinc.ll (+58) 
- (added) llvm/test/CodeGen/ARM/volatile-postinc.ll (+56) 


``````````diff
diff --git a/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp b/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp
index 8c53ea0dd94eb..b09d039b8a9b8 100644
--- a/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp
+++ b/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp
@@ -29866,15 +29866,24 @@ bool AArch64TargetLowering::getPostIndexedAddressParts(
     ISD::MemIndexedMode &AM, SelectionDAG &DAG) const {
   EVT VT;
   SDValue Ptr;
+  bool IsVolatile;
   if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
     VT = LD->getMemoryVT();
     Ptr = LD->getBasePtr();
+    IsVolatile = LD->isVolatile();
   } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
     VT = ST->getMemoryVT();
     Ptr = ST->getBasePtr();
+    IsVolatile = ST->isVolatile();
   } else
     return false;
 
+  // Do not use post-inc addressing mode for volatile accesses. Instructions
+  // performing register writeback do not set a valid instruction syndrome,
+  // making it impossible to handle MMIO in protected hypervisors.
+  if (IsVolatile)
+    return false;
+
   if (!getIndexedAddressParts(N, Op, Base, Offset, DAG))
     return false;
   // Post-indexing updates the base, so it's not a valid transform
diff --git a/llvm/lib/Target/ARM/ARMISelLowering.cpp b/llvm/lib/Target/ARM/ARMISelLowering.cpp
index cbff40f697b9b..29429e9c6da25 100644
--- a/llvm/lib/Target/ARM/ARMISelLowering.cpp
+++ b/llvm/lib/Target/ARM/ARMISelLowering.cpp
@@ -19943,33 +19943,44 @@ bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
   Align Alignment;
   bool isSEXTLoad = false, isNonExt;
   bool IsMasked = false;
+  bool IsVolatile;
   if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
     VT = LD->getMemoryVT();
     Ptr = LD->getBasePtr();
     Alignment = LD->getAlign();
     isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
     isNonExt = LD->getExtensionType() == ISD::NON_EXTLOAD;
+    IsVolatile = LD->isVolatile();
   } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
     VT = ST->getMemoryVT();
     Ptr = ST->getBasePtr();
     Alignment = ST->getAlign();
     isNonExt = !ST->isTruncatingStore();
+    IsVolatile = ST->isVolatile();
   } else if (MaskedLoadSDNode *LD = dyn_cast<MaskedLoadSDNode>(N)) {
     VT = LD->getMemoryVT();
     Ptr = LD->getBasePtr();
     Alignment = LD->getAlign();
     isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
     isNonExt = LD->getExtensionType() == ISD::NON_EXTLOAD;
+    IsVolatile = LD->isVolatile();
     IsMasked = true;
   } else if (MaskedStoreSDNode *ST = dyn_cast<MaskedStoreSDNode>(N)) {
     VT = ST->getMemoryVT();
     Ptr = ST->getBasePtr();
     Alignment = ST->getAlign();
     isNonExt = !ST->isTruncatingStore();
+    IsVolatile = ST->isVolatile();
     IsMasked = true;
   } else
     return false;
 
+  // Do not use post-inc addressing mode for volatile accesses. Instructions
+  // performing register writeback do not set a valid instruction syndrome,
+  // making it impossible to handle MMIO in protected hypervisors.
+  if (IsVolatile)
+    return false;
+
   if (Subtarget->isThumb1Only()) {
     // Thumb-1 can do a limited post-inc load or store as an updating LDM. It
     // must be non-extending/truncating, i32, with an offset of 4.
diff --git a/llvm/test/CodeGen/AArch64/volatile-postinc.ll b/llvm/test/CodeGen/AArch64/volatile-postinc.ll
new file mode 100644
index 0000000000000..c3e1bff157c4f
--- /dev/null
+++ b/llvm/test/CodeGen/AArch64/volatile-postinc.ll
@@ -0,0 +1,58 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 6
+; RUN: llc -mtriple=aarch64-unknown-linux-gnu < %s | FileCheck %s
+
+; Make sure postinc addressing mode is not used for volatile accesses,
+; see https://github.com/llvm/llvm-project/issues/173014.
+
+define void @volatile_store(ptr %start, ptr %end) {
+; CHECK-LABEL: volatile_store:
+; CHECK:       // %bb.0: // %entry
+; CHECK-NEXT:    mov w8, #42 // =0x2a
+; CHECK-NEXT:  .LBB0_1: // %loop
+; CHECK-NEXT:    // =>This Inner Loop Header: Depth=1
+; CHECK-NEXT:    mov x9, x0
+; CHECK-NEXT:    add x0, x0, #4
+; CHECK-NEXT:    cmp x0, x1
+; CHECK-NEXT:    str w8, [x9]
+; CHECK-NEXT:    b.ne .LBB0_1
+; CHECK-NEXT:  // %bb.2: // %bb2
+; CHECK-NEXT:    ret
+entry:
+  br label %loop
+
+loop:
+  %iv = phi ptr [ %iv.next, %loop ], [ %start, %entry ]
+  %iv.next = getelementptr inbounds nuw i8, ptr %iv, i64 4
+  store volatile i32 42, ptr %iv, align 4
+  %cmp = icmp eq ptr %iv.next, %end
+  br i1 %cmp, label %bb2, label %loop
+
+bb2:
+  ret void
+}
+
+define void @volatile_load(ptr %start, ptr %end) {
+; CHECK-LABEL: volatile_load:
+; CHECK:       // %bb.0: // %entry
+; CHECK-NEXT:  .LBB1_1: // %loop
+; CHECK-NEXT:    // =>This Inner Loop Header: Depth=1
+; CHECK-NEXT:    mov x8, x0
+; CHECK-NEXT:    add x0, x0, #4
+; CHECK-NEXT:    ldr wzr, [x8]
+; CHECK-NEXT:    cmp x0, x1
+; CHECK-NEXT:    b.ne .LBB1_1
+; CHECK-NEXT:  // %bb.2: // %bb2
+; CHECK-NEXT:    ret
+entry:
+  br label %loop
+
+loop:
+  %iv = phi ptr [ %iv.next, %loop ], [ %start, %entry ]
+  %iv.next = getelementptr inbounds nuw i8, ptr %iv, i64 4
+  load volatile i32, ptr %iv, align 4
+  %cmp = icmp eq ptr %iv.next, %end
+  br i1 %cmp, label %bb2, label %loop
+
+bb2:
+  ret void
+}
diff --git a/llvm/test/CodeGen/ARM/volatile-postinc.ll b/llvm/test/CodeGen/ARM/volatile-postinc.ll
new file mode 100644
index 0000000000000..4987521722440
--- /dev/null
+++ b/llvm/test/CodeGen/ARM/volatile-postinc.ll
@@ -0,0 +1,56 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 6
+; RUN: llc -mtriple=arm-eabi < %s | FileCheck %s
+
+; Make sure postinc addressing mode is not used for volatile accesses,
+; see https://github.com/llvm/llvm-project/issues/173014.
+
+define void @volatile_store(ptr %start, ptr %end) {
+; CHECK-LABEL: volatile_store:
+; CHECK:       @ %bb.0: @ %entry
+; CHECK-NEXT:    mov r2, #42
+; CHECK-NEXT:  .LBB0_1: @ %loop
+; CHECK-NEXT:    @ =>This Inner Loop Header: Depth=1
+; CHECK-NEXT:    str r2, [r0]
+; CHECK-NEXT:    add r0, r0, #4
+; CHECK-NEXT:    cmp r0, r1
+; CHECK-NEXT:    bne .LBB0_1
+; CHECK-NEXT:  @ %bb.2: @ %bb2
+; CHECK-NEXT:    mov pc, lr
+entry:
+  br label %loop
+
+loop:
+  %iv = phi ptr [ %iv.next, %loop ], [ %start, %entry ]
+  %iv.next = getelementptr inbounds nuw i8, ptr %iv, i64 4
+  store volatile i32 42, ptr %iv, align 4
+  %cmp = icmp eq ptr %iv.next, %end
+  br i1 %cmp, label %bb2, label %loop
+
+bb2:
+  ret void
+}
+
+define void @volatile_load(ptr %start, ptr %end) {
+; CHECK-LABEL: volatile_load:
+; CHECK:       @ %bb.0: @ %entry
+; CHECK-NEXT:  .LBB1_1: @ %loop
+; CHECK-NEXT:    @ =>This Inner Loop Header: Depth=1
+; CHECK-NEXT:    ldr r2, [r0]
+; CHECK-NEXT:    add r0, r0, #4
+; CHECK-NEXT:    cmp r0, r1
+; CHECK-NEXT:    bne .LBB1_1
+; CHECK-NEXT:  @ %bb.2: @ %bb2
+; CHECK-NEXT:    mov pc, lr
+entry:
+  br label %loop
+
+loop:
+  %iv = phi ptr [ %iv.next, %loop ], [ %start, %entry ]
+  %iv.next = getelementptr inbounds nuw i8, ptr %iv, i64 4
+  load volatile i32, ptr %iv, align 4
+  %cmp = icmp eq ptr %iv.next, %end
+  br i1 %cmp, label %bb2, label %loop
+
+bb2:
+  ret void
+}

``````````

</details>


https://github.com/llvm/llvm-project/pull/196305


More information about the llvm-commits mailing list