MemCpyOpt: When merging memsets also merge the trivial case of two memsets with the...
authorBenjamin Kramer <benny.kra@googlemail.com>
Mon, 10 Mar 2014 21:05:13 +0000 (21:05 +0000)
committerBenjamin Kramer <benny.kra@googlemail.com>
Mon, 10 Mar 2014 21:05:13 +0000 (21:05 +0000)
The testcase is from PR19092, but I think the bug described there is actually a clang issue.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@203489 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Transforms/Scalar/MemCpyOptimizer.cpp
test/Transforms/MemCpyOpt/form-memset.ll

index 348e12f35b8a8faf062c70540934dfaff37ef665..143ba38ec98f521311b2b9c52c734a15ed116699 100644 (file)
@@ -75,6 +75,13 @@ static bool IsPointerOffset(Value *Ptr1, Value *Ptr2, int64_t &Offset,
                             const DataLayout &TD) {
   Ptr1 = Ptr1->stripPointerCasts();
   Ptr2 = Ptr2->stripPointerCasts();
+
+  // Handle the trivial case first.
+  if (Ptr1 == Ptr2) {
+    Offset = 0;
+    return true;
+  }
+
   GEPOperator *GEP1 = dyn_cast<GEPOperator>(Ptr1);
   GEPOperator *GEP2 = dyn_cast<GEPOperator>(Ptr2);
 
index 7c7b4fc0880955e58cdc13c57c5884a7479a5279..d980b7fdbd69bcc072d15236af6377a89340635e 100644 (file)
@@ -272,3 +272,15 @@ define void @test9() nounwind {
 ; CHECK-LABEL: @test9(
 ; CHECK: call void @llvm.memset.p0i8.i64(i8* bitcast ([16 x i64]* @test9buf to i8*), i8 -1, i64 16, i32 16, i1 false)
 }
+
+; PR19092
+define void @test10(i8* nocapture %P) nounwind {
+  tail call void @llvm.memset.p0i8.i64(i8* %P, i8 0, i64 42, i32 1, i1 false)
+  tail call void @llvm.memset.p0i8.i64(i8* %P, i8 0, i64 23, i32 1, i1 false)
+  ret void
+; CHECK-LABEL: @test10(
+; CHECK-NOT: memset
+; CHECK: call void @llvm.memset.p0i8.i64(i8* %P, i8 0, i64 42, i32 1, i1 false)
+; CHECK-NOT: memset
+; CHECK: ret void
+}