llvm.org GIT mirror llvm / 8eca988
[MemCpyOpt] Do move the memset, but look at its dest's dependencies. In effect a partial revert of r237858, which was a dumb shortcut. Looking at the dependencies of the destination should be the proper fix: if the new memset would depend on anything other than itself, the transformation isn't correct. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@237874 91177308-0d34-0410-b5e6-96231b3b80d8 Ahmed Bougacha 4 years ago
2 changed file(s) with 29 addition(s) and 2 deletion(s). Raw diff Collapse all Expand all
858858 if (MemSet->getDest() != MemCpy->getDest())
859859 return false;
860860
861 // Check that there are no other dependencies on the memset destination.
862 MemDepResult DstDepInfo =
863 MD->getPointerDependencyFrom(AliasAnalysis::getLocationForDest(MemSet),
864 false, MemCpy, MemCpy->getParent());
865 if (DstDepInfo.getInst() != MemSet)
866 return false;
867
861868 // Use the same i8* dest as the memcpy, killing the memset dest if different.
862869 Value *Dest = MemCpy->getRawDest();
863870 Value *DestSize = MemSet->getLength();
873880 if (ConstantInt *SrcSizeC = dyn_cast(SrcSize))
874881 Align = MinAlign(SrcSizeC->getZExtValue(), DestAlign);
875882
876 IRBuilder<> Builder(MemSet);
883 IRBuilder<> Builder(MemCpy);
877884
878885 // If the sizes have different types, zext the smaller one.
879886 if (DestSize->getType() != SrcSize->getType()) {
None ; RUN: opt -memcpyopt -S %s | FileCheck %s
0 ; RUN: opt -basicaa -memcpyopt -S %s | FileCheck %s
11
22 target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128"
33
139139 ret i8 %r
140140 }
141141
142 %struct = type { [8 x i8], [8 x i8] }
143
144 ; CHECK-LABEL: define void @test_intermediate_write
145 ; CHECK-NEXT: %a = alloca %struct
146 ; CHECK-NEXT: %a0 = getelementptr %struct, %struct* %a, i32 0, i32 0, i32 0
147 ; CHECK-NEXT: %a1 = getelementptr %struct, %struct* %a, i32 0, i32 1, i32 0
148 ; CHECK-NEXT: call void @llvm.memset.p0i8.i64(i8* %a0, i8 0, i64 16, i32 1, i1 false)
149 ; CHECK-NEXT: store i8 1, i8* %a1
150 ; CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i64(i8* %a0, i8* %b, i64 8, i32 1, i1 false)
151 ; CHECK-NEXT: ret void
152 define void @test_intermediate_write(i8* %b) #0 {
153 %a = alloca %struct
154 %a0 = getelementptr %struct, %struct* %a, i32 0, i32 0, i32 0
155 %a1 = getelementptr %struct, %struct* %a, i32 0, i32 1, i32 0
156 call void @llvm.memset.p0i8.i64(i8* %a0, i8 0, i64 16, i32 1, i1 false)
157 store i8 1, i8* %a1
158 call void @llvm.memcpy.p0i8.p0i8.i64(i8* %a0, i8* %b, i64 8, i32 1, i1 false)
159 ret void
160 }
161
142162 declare void @llvm.memset.p0i8.i64(i8* nocapture, i8, i64, i32, i1)
143163 declare void @llvm.memcpy.p0i8.p0i8.i64(i8* nocapture, i8* nocapture readonly, i64, i32, i1)
144164 declare void @llvm.memset.p0i8.i32(i8* nocapture, i8, i32, i32, i1)