[llvm] b12d54d - [CHERI] Refine LoopIdiomRecognize handling of stable representation non-integral pointers. (#189402)

via llvm-commits llvm-commits at lists.llvm.org
Tue Apr 7 01:50:31 PDT 2026


Author: Owen Anderson
Date: 2026-04-07T10:50:26+02:00
New Revision: b12d54d680f5af54e01c3489aa97ab2b0ce1ddc3

URL: https://github.com/llvm/llvm-project/commit/b12d54d680f5af54e01c3489aa97ab2b0ce1ddc3
DIFF: https://github.com/llvm/llvm-project/commit/b12d54d680f5af54e01c3489aa97ab2b0ce1ddc3.diff

LOG: [CHERI] Refine LoopIdiomRecognize handling of stable representation non-integral pointers. (#189402)

This makes two changes:
- memset can be formed from stores of null pointers, even if they are
non-integral. Null pointers are already assumed to be equivalent to the
all-zeros bit pattern, and it's not the goal of non-integral pointer
support to change that assumption.
- memcpy can be formed from copies of non-integral pointers. We assume
if the environment provides a memcpy implementation for a non-integral
pointer address space, then that memcpy is non-integral pointer aware.

These changes are not applied to pointers with unstable representation.

Added: 
    

Modified: 
    llvm/lib/Transforms/Scalar/LoopIdiomRecognize.cpp
    llvm/test/Transforms/LoopIdiom/non-integral-pointers.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/Scalar/LoopIdiomRecognize.cpp b/llvm/lib/Transforms/Scalar/LoopIdiomRecognize.cpp
index 105757140c758..7a08d99a9e505 100644
--- a/llvm/lib/Transforms/Scalar/LoopIdiomRecognize.cpp
+++ b/llvm/lib/Transforms/Scalar/LoopIdiomRecognize.cpp
@@ -471,11 +471,19 @@ LoopIdiomRecognize::isLegalStore(StoreInst *SI) {
   Value *StoredVal = SI->getValueOperand();
   Value *StorePtr = SI->getPointerOperand();
 
-  // Don't convert stores of non-integral pointer types to memsets (which stores
-  // integers).
-  if (DL->isNonIntegralPointerType(StoredVal->getType()->getScalarType()))
+  if (DL->hasUnstableRepresentation(StoredVal->getType()))
     return LegalStoreKind::None;
 
+  // Transformations could invalidate the external-state pointers
+  //   memcpy - LangRef specifies that a valid memcpy must preserve external
+  //            state, so no transformations are blocked by it.
+  //   memset - We assume that a memset of 0 has an equivalent external state
+  //            effect as a null pointer store. This is currently not explicitly
+  //            specified, but is true of the one exemplar we have (CHERI
+  //            capabilities). All other memset formations are not safe.
+  bool MustPreserveExternalState = DL->hasExternalState(StoredVal->getType()) &&
+                                   !isa<ConstantPointerNull>(StoredVal);
+
   // Reject stores that are so large that they overflow an unsigned.
   // When storing out scalable vectors we bail out for now, since the code
   // below currently only works for constant strides.
@@ -506,14 +514,16 @@ LoopIdiomRecognize::isLegalStore(StoreInst *SI) {
 
   // If we're allowed to form a memset, and the stored value would be
   // acceptable for memset, use it.
-  if (!UnorderedAtomic && HasMemset && SplatValue && !DisableLIRP::Memset &&
+  if (!MustPreserveExternalState && !UnorderedAtomic && HasMemset &&
+      SplatValue && !DisableLIRP::Memset &&
       // Verify that the stored value is loop invariant.  If not, we can't
       // promote the memset.
       CurLoop->isLoopInvariant(SplatValue)) {
     // It looks like we can use SplatValue.
     return LegalStoreKind::Memset;
   }
-  if (!UnorderedAtomic && (HasMemsetPattern || ForceMemsetPatternIntrinsic) &&
+  if (!MustPreserveExternalState && !UnorderedAtomic &&
+      (HasMemsetPattern || ForceMemsetPatternIntrinsic) &&
       !DisableLIRP::Memset &&
       // Don't create memset_pattern16s with address spaces.
       StorePtr->getType()->getPointerAddressSpace() == 0 &&

diff  --git a/llvm/test/Transforms/LoopIdiom/non-integral-pointers.ll b/llvm/test/Transforms/LoopIdiom/non-integral-pointers.ll
index 13ec5150f0559..2414d803de5d0 100644
--- a/llvm/test/Transforms/LoopIdiom/non-integral-pointers.ll
+++ b/llvm/test/Transforms/LoopIdiom/non-integral-pointers.ll
@@ -1,7 +1,7 @@
 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
 ; RUN: opt -S -passes=loop-idiom < %s | FileCheck %s
 
-target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128-ni:4"
+target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128-pe4:64:64-pu5:64:64"
 target triple = "x86_64-unknown-linux-gnu"
 
 ; LIR'ing stores of pointers with address space 3 is fine, since
@@ -9,7 +9,7 @@ target triple = "x86_64-unknown-linux-gnu"
 define void @f_0(ptr %ptr) {
 ; CHECK-LABEL: @f_0(
 ; CHECK-NEXT:  entry:
-; CHECK-NEXT:    call void @llvm.memset.p0.i64(ptr align 4 [[PTR:%.*]], i8 0, i64 80000, i1 false)
+; CHECK-NEXT:    call void @llvm.memset.p0.i64(ptr align 4 [[PTR:%.*]], i8 -1, i64 80000, i1 false)
 ; CHECK-NEXT:    br label [[FOR_BODY:%.*]]
 ; CHECK:       for.body:
 ; CHECK-NEXT:    [[INDVAR:%.*]] = phi i64 [ 0, [[ENTRY:%.*]] ], [ [[INDVAR_NEXT:%.*]], [[FOR_BODY]] ]
@@ -27,7 +27,7 @@ entry:
 for.body:
   %indvar = phi i64 [ 0, %entry ], [ %indvar.next, %for.body ]
   %arrayidx = getelementptr ptr addrspace(3), ptr %ptr, i64 %indvar
-  store ptr addrspace(3) null, ptr %arrayidx, align 4
+  store ptr addrspace(3) inttoptr (i64 -1 to ptr addrspace(3)), ptr %arrayidx, align 4
   %indvar.next = add i64 %indvar, 1
   %exitcond = icmp eq i64 %indvar.next, 10000
   br i1 %exitcond, label %for.end, label %for.body
@@ -37,9 +37,7 @@ for.end:
 }
 
 ; LIR'ing stores of pointers with address space 4 is not ok, since
-; they're non-integral pointers. NOTE: Zero is special value which
-; can be converted, if we add said handling here, convert this test
-; to use any non-null pointer.
+; they're non-integral pointers.
 define void @f_1(ptr %ptr) {
 ; CHECK-LABEL: @f_1(
 ; CHECK-NEXT:  entry:
@@ -47,7 +45,7 @@ define void @f_1(ptr %ptr) {
 ; CHECK:       for.body:
 ; CHECK-NEXT:    [[INDVAR:%.*]] = phi i64 [ 0, [[ENTRY:%.*]] ], [ [[INDVAR_NEXT:%.*]], [[FOR_BODY]] ]
 ; CHECK-NEXT:    [[ARRAYIDX:%.*]] = getelementptr ptr addrspace(4), ptr [[PTR:%.*]], i64 [[INDVAR]]
-; CHECK-NEXT:    store ptr addrspace(4) null, ptr [[ARRAYIDX]], align 4
+; CHECK-NEXT:    store ptr addrspace(4) inttoptr (i64 -1 to ptr addrspace(4)), ptr [[ARRAYIDX]], align 4
 ; CHECK-NEXT:    [[INDVAR_NEXT]] = add i64 [[INDVAR]], 1
 ; CHECK-NEXT:    [[EXITCOND:%.*]] = icmp eq i64 [[INDVAR_NEXT]], 10000
 ; CHECK-NEXT:    br i1 [[EXITCOND]], label [[FOR_END:%.*]], label [[FOR_BODY]]
@@ -61,7 +59,7 @@ entry:
 for.body:
   %indvar = phi i64 [ 0, %entry ], [ %indvar.next, %for.body ]
   %arrayidx = getelementptr ptr addrspace(4), ptr %ptr, i64 %indvar
-  store ptr addrspace(4) null, ptr %arrayidx, align 4
+  store ptr addrspace(4) inttoptr (i64 -1 to ptr addrspace(4)), ptr %arrayidx, align 4
   %indvar.next = add i64 %indvar, 1
   %exitcond = icmp eq i64 %indvar.next, 10000
   br i1 %exitcond, label %for.end, label %for.body
@@ -99,3 +97,101 @@ for.body:
 for.end:
   ret void
 }
+
+; Same as @f_2, but null is a special value that can be converted even for non-integral pointers.
+define void @f_3(ptr %ptr) {
+; CHECK-LABEL: @f_3(
+; CHECK-NEXT:  entry:
+; CHECK-NEXT:    call void @llvm.memset.p0.i64(ptr align 4 [[PTR:%.*]], i8 0, i64 80000, i1 false)
+; CHECK-NEXT:    br label [[FOR_BODY:%.*]]
+; CHECK:       for.body:
+; CHECK-NEXT:    [[INDVAR:%.*]] = phi i64 [ 0, [[ENTRY:%.*]] ], [ [[INDVAR_NEXT:%.*]], [[FOR_BODY]] ]
+; CHECK-NEXT:    [[ARRAYIDX:%.*]] = getelementptr ptr addrspace(4), ptr [[PTR]], i64 [[INDVAR]]
+; CHECK-NEXT:    [[INDVAR_NEXT]] = add i64 [[INDVAR]], 1
+; CHECK-NEXT:    [[EXITCOND:%.*]] = icmp eq i64 [[INDVAR_NEXT]], 10000
+; CHECK-NEXT:    br i1 [[EXITCOND]], label [[FOR_END:%.*]], label [[FOR_BODY]]
+; CHECK:       for.end:
+; CHECK-NEXT:    ret void
+;
+
+entry:
+  br label %for.body
+
+for.body:
+  %indvar = phi i64 [ 0, %entry ], [ %indvar.next, %for.body ]
+  %arrayidx = getelementptr ptr addrspace(4), ptr %ptr, i64 %indvar
+  store ptr addrspace(4) null, ptr %arrayidx, align 4
+  %indvar.next = add i64 %indvar, 1
+  %exitcond = icmp eq i64 %indvar.next, 10000
+  br i1 %exitcond, label %for.end, label %for.body
+
+for.end:
+  ret void
+}
+
+; Same as @f_3, but null is NOT a special value unstable representation pointers.
+define void @f_4(ptr %ptr) {
+; CHECK-LABEL: @f_4(
+; CHECK-NEXT:  entry:
+; CHECK-NEXT:    br label [[FOR_BODY:%.*]]
+; CHECK:       for.body:
+; CHECK-NEXT:    [[INDVAR:%.*]] = phi i64 [ 0, [[ENTRY:%.*]] ], [ [[INDVAR_NEXT:%.*]], [[FOR_BODY]] ]
+; CHECK-NEXT:    [[ARRAYIDX:%.*]] = getelementptr ptr addrspace(5), ptr [[PTR:%.*]], i64 [[INDVAR]]
+; CHECK-NEXT:    store ptr addrspace(5) null, ptr [[ARRAYIDX]], align 4
+; CHECK-NEXT:    [[INDVAR_NEXT]] = add i64 [[INDVAR]], 1
+; CHECK-NEXT:    [[EXITCOND:%.*]] = icmp eq i64 [[INDVAR_NEXT]], 10000
+; CHECK-NEXT:    br i1 [[EXITCOND]], label [[FOR_END:%.*]], label [[FOR_BODY]]
+; CHECK:       for.end:
+; CHECK-NEXT:    ret void
+;
+
+entry:
+  br label %for.body
+
+for.body:
+  %indvar = phi i64 [ 0, %entry ], [ %indvar.next, %for.body ]
+  %arrayidx = getelementptr ptr addrspace(5), ptr %ptr, i64 %indvar
+  store ptr addrspace(5) null, ptr %arrayidx, align 4
+  %indvar.next = add i64 %indvar, 1
+  %exitcond = icmp eq i64 %indvar.next, 10000
+  br i1 %exitcond, label %for.end, label %for.body
+
+for.end:
+  ret void
+}
+
+
+; LIR may create a memcpy, as we assume the system memcpy is non-integral pointer aware.
+define void @f_5(ptr noalias %dst, ptr noalias %src) {
+; CHECK-LABEL: @f_5(
+; CHECK-NEXT:  entry:
+; CHECK-NEXT:    call void @llvm.memcpy.p0.p0.i64(ptr align 8 [[DST:%.*]], ptr align 8 [[SRC:%.*]], i64 8192, i1 false)
+; CHECK-NEXT:    br label [[FOR_BODY:%.*]]
+; CHECK:       for.cond.cleanup:
+; CHECK-NEXT:    ret void
+; CHECK:       for.body:
+; CHECK-NEXT:    [[I_05:%.*]] = phi i64 [ 0, [[ENTRY:%.*]] ], [ [[INC:%.*]], [[FOR_BODY]] ]
+; CHECK-NEXT:    [[ARRAYIDX:%.*]] = getelementptr inbounds nuw [8 x i8], ptr [[SRC]], i64 [[I_05]]
+; CHECK-NEXT:    [[TMP0:%.*]] = load ptr addrspace(4), ptr [[ARRAYIDX]], align 8
+; CHECK-NEXT:    [[ARRAYIDX1:%.*]] = getelementptr inbounds nuw [8 x i8], ptr [[DST]], i64 [[I_05]]
+; CHECK-NEXT:    [[INC]] = add nuw nsw i64 [[I_05]], 1
+; CHECK-NEXT:    [[CMP:%.*]] = icmp samesign ult i64 [[I_05]], 1023
+; CHECK-NEXT:    br i1 [[CMP]], label [[FOR_BODY]], label [[FOR_COND_CLEANUP:%.*]]
+;
+
+entry:
+  br label %for.body
+
+for.cond.cleanup:                                 ; preds = %for.body
+  ret void
+
+for.body:                                         ; preds = %entry, %for.body
+  %i.05 = phi i64 [ 0, %entry ], [ %inc, %for.body ]
+  %arrayidx = getelementptr inbounds nuw [8 x i8], ptr %src, i64 %i.05
+  %0 = load ptr addrspace(4), ptr %arrayidx, align 8
+  %arrayidx1 = getelementptr inbounds nuw [8 x i8], ptr %dst, i64 %i.05
+  store ptr addrspace(4) %0, ptr %arrayidx1, align 8
+  %inc = add nuw nsw i64 %i.05, 1
+  %cmp = icmp samesign ult i64 %i.05, 1023
+  br i1 %cmp, label %for.body, label %for.cond.cleanup
+}


        


More information about the llvm-commits mailing list