[PATCH] D64387: CodeGen: Do not memset if following stores will overwrite entire variable

Vitaly Buka via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Jul 8 22:57:08 PDT 2019


vitalybuka created this revision.
vitalybuka added reviewers: eugenis, pcc, jfb.
Herald added subscribers: cfe-commits, dexonsmith.
Herald added a project: clang.

In some cases llvm::getMostFrequentByte can't analyze a
constant so we write zeros and then override the rest with stores.
These zeros are not needed and we can keep only stores.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D64387

Files:
  clang/lib/CodeGen/CGDecl.cpp
  clang/test/CodeGen/init-memset.c


Index: clang/test/CodeGen/init-memset.c
===================================================================
--- clang/test/CodeGen/init-memset.c
+++ clang/test/CodeGen/init-memset.c
@@ -49,7 +49,7 @@
 void test_pointers() {
   // CHECK-LABEL: define void @test_pointers()
   void *a[] = {&use, &use, &use, &use, &use, &use};
-  // CHECK: call void @llvm.memset.{{.*}}
+  // CHECK-NOT: @llvm.memset.
   // CHECK: store i8*
   // CHECK: store i8*
   // CHECK: store i8*
Index: clang/lib/CodeGen/CGDecl.cpp
===================================================================
--- clang/lib/CodeGen/CGDecl.cpp
+++ clang/lib/CodeGen/CGDecl.cpp
@@ -973,15 +973,10 @@
   if (GlobalSize <= SizeLimit)
     return nullptr;
 
-  if (!MFB.Value) {
-    // We don't know most frequent byte, we may still completely initialize it
-    // with small number of stores.
-    // TODO: Use llvm::UndefValue::get. If we get here and later we decide
-    // that number of stores is small enough to use memset+store then store are
-    // going to replace entire bzeroed range. With UndefValue we can skip that
-    // bogus memset. I will do that as a separate patch.
-    MFB.Value = llvm::ConstantInt::get(CGM.Int8Ty, 0);
-  }
+  // We don't know most frequent byte, we may still completely initialize it
+  // with small number of stores.
+  if (!MFB.Value)
+    MFB.Value = llvm::UndefValue::get(CGM.Int8Ty);
 
   unsigned StoreBudget = 6;
   return canEmitStoresForInitAfterMemset(CGM, Init, MFB.Value, StoreBudget)


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D64387.208600.patch
Type: text/x-patch
Size: 1501 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20190709/7e66b7e9/attachment.bin>


More information about the cfe-commits mailing list