[llvm] 150595a - LoopIdiom: avoid patterned memset if constant is not relocatable.

Tim Northover via llvm-commits llvm-commits at lists.llvm.org
Thu Jan 12 10:53:12 PST 2023


Author: Tim Northover
Date: 2023-01-12T18:53:07Z
New Revision: 150595ab4be34e392e345d347dcb767df04a5383

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

LOG: LoopIdiom: avoid patterned memset if constant is not relocatable.

The pattern we're using for the memset_pattern* call gets put into a static
global variable initialized, which means it has to be representable with
relocations on the target. Most `ConstantExpr` instances do not satisfy this
constraint, so avoid all of them for now.

Added: 
    

Modified: 
    llvm/lib/Transforms/Scalar/LoopIdiomRecognize.cpp
    llvm/test/Transforms/LoopIdiom/basic.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/Scalar/LoopIdiomRecognize.cpp b/llvm/lib/Transforms/Scalar/LoopIdiomRecognize.cpp
index e2493c5aa83d6..035cbdf595a8c 100644
--- a/llvm/lib/Transforms/Scalar/LoopIdiomRecognize.cpp
+++ b/llvm/lib/Transforms/Scalar/LoopIdiomRecognize.cpp
@@ -441,7 +441,7 @@ static Constant *getMemSetPatternValue(Value *V, const DataLayout *DL) {
   // array.  We could theoretically do a store to an alloca or something, but
   // that doesn't seem worthwhile.
   Constant *C = dyn_cast<Constant>(V);
-  if (!C)
+  if (!C || isa<ConstantExpr>(C))
     return nullptr;
 
   // Only handle simple values that are a power of two bytes in size.

diff  --git a/llvm/test/Transforms/LoopIdiom/basic.ll b/llvm/test/Transforms/LoopIdiom/basic.ll
index d114abcffb5f7..43c4f62e50152 100644
--- a/llvm/test/Transforms/LoopIdiom/basic.ll
+++ b/llvm/test/Transforms/LoopIdiom/basic.ll
@@ -1583,4 +1583,36 @@ for.end:                                          ; preds = %for.body, %entry
   ret void
 }
 
+%class.CMSPULog = type { %struct._opaque_pthread_mutex_t, ptr, i32, i32, i32, i8, i8, i8, [512 x i32] }
+%struct._opaque_pthread_mutex_t = type { i64, [56 x i8] }
+
+define noalias ptr @_ZN8CMSPULog9beginImplEja(ptr nocapture writeonly %0) local_unnamed_addr #0 {
+; CHECK-LABEL: @_ZN8CMSPULog9beginImplEja(
+; CHECK-NEXT:    br label [[TMP2:%.*]]
+; CHECK:       2:
+; CHECK-NEXT:    [[TMP3:%.*]] = phi i32 [ 0, [[TMP1:%.*]] ], [ [[TMP4:%.*]], [[TMP2]] ]
+; CHECK-NEXT:    [[TMP4]] = add nuw nsw i32 [[TMP3]], 1
+; CHECK-NEXT:    [[TMP5:%.*]] = zext i32 [[TMP3]] to i64
+; CHECK-NEXT:    [[TMP6:%.*]] = getelementptr [[CLASS_CMSPULOG:%.*]], ptr [[TMP0:%.*]], i64 0, i32 8, i64 [[TMP5]]
+; CHECK-NEXT:    store i32 trunc (i64 and (i64 ptrtoint (ptr @G to i64), i64 16777215) to i32), ptr [[TMP6]], align 4
+; CHECK-NEXT:    [[TMP7:%.*]] = icmp ult i32 [[TMP3]], 511
+; CHECK-NEXT:    br i1 [[TMP7]], label [[TMP2]], label [[TMP8:%.*]]
+; CHECK:       8:
+; CHECK-NEXT:    ret ptr null
+;
+  br label %2
+
+2:                                                ; preds = %1, %2
+  %3 = phi i32 [ 0, %1 ], [ %4, %2 ]
+  %4 = add nuw nsw i32 %3, 1
+  %5 = zext i32 %3 to i64
+  %6 = getelementptr %class.CMSPULog, ptr %0, i64 0, i32 8, i64 %5
+  store i32 trunc (i64 and (i64 ptrtoint (ptr @G to i64), i64 16777215) to i32), ptr %6, align 4
+  %7 = icmp ult i32 %3, 511
+  br i1 %7, label %2, label %8
+
+8:                                                ; preds = %2
+  ret ptr null
+}
+
 ; Validate that "memset_pattern" has the proper attributes.


        


More information about the llvm-commits mailing list