Index: lib/Transforms/InstCombine/InstCombineCalls.cpp =================================================================== --- lib/Transforms/InstCombine/InstCombineCalls.cpp +++ lib/Transforms/InstCombine/InstCombineCalls.cpp @@ -120,6 +120,15 @@ return MI; } + // If we have a store to a location which is known constant, we can conclude + // that the store must be storing the constant value (else the memory + // wouldn't be constant), and this must be a noop. + if (AA->pointsToConstantMemory(MI->getDest())) { + // Set the size of the copy to 0, it will be deleted on the next iteration. + MI->setLength(Constant::getNullValue(MI->getLength()->getType())); + return MI; + } + // If MemCpyInst length is 1/2/4/8 bytes then replace memcpy with // load/store. ConstantInt *MemOpLength = dyn_cast(MI->getLength()); @@ -218,6 +227,15 @@ return MI; } + // If we have a store to a location which is known constant, we can conclude + // that the store must be storing the constant value (else the memory + // wouldn't be constant), and this must be a noop. + if (AA->pointsToConstantMemory(MI->getDest())) { + // Set the size of the copy to 0, it will be deleted on the next iteration. + MI->setLength(Constant::getNullValue(MI->getLength()->getType())); + return MI; + } + // Extract the length and alignment and fill if they are constant. ConstantInt *LenC = dyn_cast(MI->getLength()); ConstantInt *FillC = dyn_cast(MI->getValue()); Index: lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp =================================================================== --- lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp +++ lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp @@ -1438,6 +1438,12 @@ } } + // If we have a store to a location which is known constant, we can conclude + // that the store must be storing the constant value (else the memory + // wouldn't be constant), and this must be a noop. + if (AA->pointsToConstantMemory(Ptr)) + return eraseInstFromFunction(SI); + // Do really simple DSE, to catch cases where there are several consecutive // stores to the same location, separated by a few arithmetic operations. This // situation often occurs with bitfield accesses. Index: test/Transforms/InstCombine/gep-custom-dl.ll =================================================================== --- test/Transforms/InstCombine/gep-custom-dl.ll +++ test/Transforms/InstCombine/gep-custom-dl.ll @@ -8,8 +8,7 @@ %struct.C = type { [7 x i8] } -@Global = constant [10 x i8] c"helloworld" - +@Global = external global [10 x i8] ; Test that two array indexing geps fold define i32* @test1(i32* %I) { Index: test/Transforms/InstCombine/getelementptr.ll =================================================================== --- test/Transforms/InstCombine/getelementptr.ll +++ test/Transforms/InstCombine/getelementptr.ll @@ -9,8 +9,8 @@ %struct.C = type { [7 x i8] } -@Global = constant [10 x i8] c"helloworld" -@Global_as1 = addrspace(1) constant [10 x i8] c"helloworld" +@Global = external global [10 x i8] +@Global_as1 = external addrspace(1) global [10 x i8] ; Test noop elimination define i32* @test1(i32* %I) { Index: test/Transforms/InstCombine/memcpy.ll =================================================================== --- test/Transforms/InstCombine/memcpy.ll +++ test/Transforms/InstCombine/memcpy.ll @@ -35,3 +35,9 @@ ret void } +@UnknownConstant = external constant i8 + +define void @memmove_to_constant(i8* %src) { + call void @llvm.memcpy.p0i8.p0i8.i32(i8* @UnknownConstant, i8* %src, i32 1, i1 false) + ret void +} Index: test/Transforms/InstCombine/memmove.ll =================================================================== --- test/Transforms/InstCombine/memmove.ll +++ test/Transforms/InstCombine/memmove.ll @@ -1,3 +1,4 @@ +; NOTE: Assertions have been autogenerated by utils/update_test_checks.py ; This test makes sure that memmove instructions are properly eliminated. ; ; RUN: opt < %s -instcombine -S | FileCheck %s @@ -8,30 +9,33 @@ @hello_u = constant [8 x i8] c"hello_u\00" ; <[8 x i8]*> [#uses=1] define void @test1(i8* %A, i8* %B, i32 %N) { - ;; CHECK-LABEL: test1 - ;; CHECK-NEXT: ret void +; CHECK-LABEL: @test1( +; CHECK-NEXT: ret void +; call void @llvm.memmove.p0i8.p0i8.i32(i8* %A, i8* %B, i32 0, i1 false) ret void } define void @test2(i8* %A, i32 %N) { ;; dest can't alias source since we can't write to source! - ;; CHECK-LABEL: test2 - ;; CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i32(i8* align 1 %A, i8* align 16 getelementptr inbounds ([33 x i8], [33 x i8]* @S, i{{32|64}} 0, i{{32|64}} 0), i32 %N, i1 false) - ;; CHECK-NEXT: ret void +; CHECK-LABEL: @test2( +; CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i32(i8* align 1 [[A:%.*]], i8* align 16 getelementptr inbounds ([33 x i8], [33 x i8]* @S, i64 0, i64 0), i32 [[N:%.*]], i1 false) +; CHECK-NEXT: ret void +; call void @llvm.memmove.p0i8.p0i8.i32(i8* %A, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @S, i32 0, i32 0), i32 %N, i1 false) ret void } define i32 @test3([1024 x i8]* %target) { ; arg: [1024 x i8]*> [#uses=1] - ;; CHECK-LABEL: test3 - ;; CHECK-NEXT: [[P1:%[^\s]+]] = bitcast [1024 x i8]* %target to i16* - ;; CHECK-NEXT: store i16 104, i16* [[P1]], align 2 - ;; CHECK-NEXT: [[P2:%[^\s]+]] = bitcast [1024 x i8]* %target to i32* - ;; CHECK-NEXT: store i32 7103848, i32* [[P2]], align 4 - ;; CHECK-NEXT: [[P3:%[^\s]+]] = bitcast [1024 x i8]* %target to i64* - ;; CHECK-NEXT: store i64 33037504440198504, i64* [[P3]], align 8 - ;; CHECK-NEXT: ret i32 0 +; CHECK-LABEL: @test3( +; CHECK-NEXT: [[TMP1:%.*]] = bitcast [1024 x i8]* [[TARGET:%.*]] to i16* +; CHECK-NEXT: store i16 104, i16* [[TMP1]], align 2 +; CHECK-NEXT: [[TMP2:%.*]] = bitcast [1024 x i8]* [[TARGET]] to i32* +; CHECK-NEXT: store i32 7103848, i32* [[TMP2]], align 4 +; CHECK-NEXT: [[TMP3:%.*]] = bitcast [1024 x i8]* [[TARGET]] to i64* +; CHECK-NEXT: store i64 33037504440198504, i64* [[TMP3]], align 8 +; CHECK-NEXT: ret i32 0 +; %h_p = getelementptr [2 x i8], [2 x i8]* @h, i32 0, i32 0 ; [#uses=1] %hel_p = getelementptr [4 x i8], [4 x i8]* @hel, i32 0, i32 0 ; [#uses=1] %hello_u_p = getelementptr [8 x i8], [8 x i8]* @hello_u, i32 0, i32 0 ; [#uses=1] @@ -44,10 +48,22 @@ ; PR2370 define void @test4(i8* %a) { - ;; CHECK-LABEL: test4 - ;; CHECK-NEXT: ret void +; CHECK-LABEL: @test4( +; CHECK-NEXT: ret void +; tail call void @llvm.memmove.p0i8.p0i8.i32(i8* %a, i8* %a, i32 100, i1 false) ret void } +@UnknownConstant = external constant i8 + +define void @memmove_to_constant(i8* %src) { +; CHECK-LABEL: @memmove_to_constant( +; CHECK-NEXT: ret void +; + call void @llvm.memmove.p0i8.p0i8.i32(i8* @UnknownConstant, i8* %src, i32 1, i1 false) + ret void +} + + declare void @llvm.memmove.p0i8.p0i8.i32(i8* nocapture, i8* nocapture readonly, i32, i1) argmemonly nounwind Index: test/Transforms/InstCombine/memset.ll =================================================================== --- test/Transforms/InstCombine/memset.ll +++ test/Transforms/InstCombine/memset.ll @@ -1,16 +1,18 @@ +; NOTE: Assertions have been autogenerated by utils/update_test_checks.py ; RUN: opt < %s -instcombine -S | FileCheck %s define i32 @test([1024 x i8]* %target) { - ;; CHECK-LABEL: test - ;; CHECK-NEXT: [[P1:%[^\s]+]] = getelementptr inbounds [1024 x i8], [1024 x i8]* %target, i64 0, i64 0 - ;; CHECK-NEXT: store i8 1, i8* [[P1]], align 1 - ;; CHECK-NEXT: [[P2:%[^\s]+]] = bitcast [1024 x i8]* %target to i16* - ;; CHECK-NEXT: store i16 257, i16* [[P2]], align 2 - ;; CHECK-NEXT: [[P3:%[^\s]+]] = bitcast [1024 x i8]* %target to i32* - ;; CHECK-NEXT: store i32 16843009, i32* [[P3]], align 4 - ;; CHECK-NEXT: [[P4:%[^\s]+]] = bitcast [1024 x i8]* %target to i64* - ;; CHECK-NEXT: store i64 72340172838076673, i64* [[P4]], align 8 - ;; CHECK-NEXT: ret i32 0 +; CHECK-LABEL: @test( +; CHECK-NEXT: [[TMP1:%.*]] = getelementptr inbounds [1024 x i8], [1024 x i8]* [[TARGET:%.*]], i64 0, i64 0 +; CHECK-NEXT: store i8 1, i8* [[TMP1]], align 1 +; CHECK-NEXT: [[TMP2:%.*]] = bitcast [1024 x i8]* [[TARGET]] to i16* +; CHECK-NEXT: store i16 257, i16* [[TMP2]], align 2 +; CHECK-NEXT: [[TMP3:%.*]] = bitcast [1024 x i8]* [[TARGET]] to i32* +; CHECK-NEXT: store i32 16843009, i32* [[TMP3]], align 4 +; CHECK-NEXT: [[TMP4:%.*]] = bitcast [1024 x i8]* [[TARGET]] to i64* +; CHECK-NEXT: store i64 72340172838076673, i64* [[TMP4]], align 8 +; CHECK-NEXT: ret i32 0 +; %target_p = getelementptr [1024 x i8], [1024 x i8]* %target, i32 0, i32 0 call void @llvm.memset.p0i8.i32(i8* %target_p, i8 1, i32 0, i1 false) call void @llvm.memset.p0i8.i32(i8* %target_p, i8 1, i32 1, i1 false) @@ -20,4 +22,15 @@ ret i32 0 } +@Unknown = external constant i32 + +define void @memset_to_constant() { +; CHECK-LABEL: @memset_to_constant( +; CHECK-NEXT: ret void +; + %p = bitcast i32* @Unknown to i8* + call void @llvm.memset.p0i8.i32(i8* %p, i8 0, i32 5, i1 false) + ret void +} + declare void @llvm.memset.p0i8.i32(i8* nocapture writeonly, i8, i32, i1) argmemonly nounwind Index: test/Transforms/InstCombine/store.ll =================================================================== --- test/Transforms/InstCombine/store.ll +++ test/Transforms/InstCombine/store.ll @@ -291,6 +291,16 @@ ret void } +@Unknown = external constant i32 + +define void @store_to_constant() { +; CHECK-LABEL: @store_to_constant( +; CHECK-NEXT: ret void +; + store i32 0, i32* @Unknown + ret void +} + !0 = !{!4, !4, i64 0} !1 = !{!"omnipotent char", !2} !2 = !{!"Simple C/C++ TBAA"}