From ec68f552f2a5c1df10813a80929995185eb98243 Mon Sep 17 00:00:00 2001 From: Chandler Carruth Date: Tue, 21 Aug 2012 08:39:44 +0000 Subject: Port the global copy optimization from the SROA pass to InstCombine. This optimization is really just replacing allocas wholesale with globals, there is no scalarization. The underlying motivation for this patch is to simplify the SROA pass and focus it on splitting and promoting allocas. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@162271 91177308-0d34-0410-b5e6-96231b3b80d8 --- test/Transforms/InstCombine/memcpy-from-global.ll | 136 ++++++++++++++++++++ test/Transforms/ScalarRepl/memcpy-from-global.ll | 146 ---------------------- 2 files changed, 136 insertions(+), 146 deletions(-) create mode 100644 test/Transforms/InstCombine/memcpy-from-global.ll delete mode 100644 test/Transforms/ScalarRepl/memcpy-from-global.ll (limited to 'test') diff --git a/test/Transforms/InstCombine/memcpy-from-global.ll b/test/Transforms/InstCombine/memcpy-from-global.ll new file mode 100644 index 0000000..83c893e --- /dev/null +++ b/test/Transforms/InstCombine/memcpy-from-global.ll @@ -0,0 +1,136 @@ +; RUN: opt < %s -instcombine -S | FileCheck %s +target datalayout = "E-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64" +@C.0.1248 = internal constant [128 x float] [ float -1.000000e+00, float -1.000000e+00, float -1.000000e+00, float 0.000000e+00, float -1.000000e+00, float -1.000000e+00, float 0.000000e+00, float -1.000000e+00, float -1.000000e+00, float -1.000000e+00, float 0.000000e+00, float 1.000000e+00, float -1.000000e+00, float -1.000000e+00, float 1.000000e+00, float 0.000000e+00, float -1.000000e+00, float 0.000000e+00, float -1.000000e+00, float -1.000000e+00, float -1.000000e+00, float 0.000000e+00, float -1.000000e+00, float 1.000000e+00, float -1.000000e+00, float 0.000000e+00, float 1.000000e+00, float -1.000000e+00, float -1.000000e+00, float 0.000000e+00, float 1.000000e+00, float 1.000000e+00, float -1.000000e+00, float 1.000000e+00, float -1.000000e+00, float 0.000000e+00, float -1.000000e+00, float 1.000000e+00, float 0.000000e+00, float -1.000000e+00, float -1.000000e+00, float 1.000000e+00, float 0.000000e+00, float 1.000000e+00, float -1.000000e+00, float 1.000000e+00, float 1.000000e+00, float 0.000000e+00, float 0.000000e+00, float -1.000000e+00, float -1.000000e+00, float -1.000000e+00, float 0.000000e+00, float -1.000000e+00, float -1.000000e+00, float 1.000000e+00, float 0.000000e+00, float -1.000000e+00, float 1.000000e+00, float -1.000000e+00, float 0.000000e+00, float -1.000000e+00, float 1.000000e+00, float 1.000000e+00, float 1.000000e+00, float -1.000000e+00, float -1.000000e+00, float 0.000000e+00, float 1.000000e+00, float -1.000000e+00, float 0.000000e+00, float -1.000000e+00, float 1.000000e+00, float -1.000000e+00, float 0.000000e+00, float 1.000000e+00, float 1.000000e+00, float -1.000000e+00, float 1.000000e+00, float 0.000000e+00, float 1.000000e+00, float 0.000000e+00, float -1.000000e+00, float -1.000000e+00, float 1.000000e+00, float 0.000000e+00, float -1.000000e+00, float 1.000000e+00, float 1.000000e+00, float 0.000000e+00, float 1.000000e+00, float -1.000000e+00, float 1.000000e+00, float 0.000000e+00, float 1.000000e+00, float 1.000000e+00, float 1.000000e+00, float 1.000000e+00, float -1.000000e+00, float 0.000000e+00, float 1.000000e+00, float 1.000000e+00, float 0.000000e+00, float -1.000000e+00, float 1.000000e+00, float 1.000000e+00, float 0.000000e+00, float 1.000000e+00, float 1.000000e+00, float 1.000000e+00, float 1.000000e+00, float 0.000000e+00, float 0.000000e+00, float 1.000000e+00, float -1.000000e+00, float -1.000000e+00, float 0.000000e+00, float 1.000000e+00, float -1.000000e+00, float 1.000000e+00, float 0.000000e+00, float 1.000000e+00, float 1.000000e+00, float -1.000000e+00, float 0.000000e+00, float 1.000000e+00, float 1.000000e+00, float 1.000000e+00 ], align 32 ; <[128 x float]*> [#uses=1] + +define float @test1(i32 %hash, float %x, float %y, float %z, float %w) { +entry: + %lookupTable = alloca [128 x float], align 16 ; <[128 x float]*> [#uses=5] + %lookupTable1 = bitcast [128 x float]* %lookupTable to i8* ; [#uses=1] + call void @llvm.memcpy.p0i8.p0i8.i64(i8* %lookupTable1, i8* bitcast ([128 x float]* @C.0.1248 to i8*), i64 512, i32 16, i1 false) + +; CHECK: @test1 +; CHECK-NOT: alloca +; CHECK-NOT: call{{.*}}@llvm.memcpy + + %tmp3 = shl i32 %hash, 2 ; [#uses=1] + %tmp5 = and i32 %tmp3, 124 ; [#uses=4] + %tmp753 = getelementptr [128 x float]* %lookupTable, i32 0, i32 %tmp5 ; [#uses=1] + %tmp9 = load float* %tmp753 ; [#uses=1] + %tmp11 = fmul float %tmp9, %x ; [#uses=1] + %tmp13 = fadd float %tmp11, 0.000000e+00 ; [#uses=1] + %tmp17.sum52 = or i32 %tmp5, 1 ; [#uses=1] + %tmp1851 = getelementptr [128 x float]* %lookupTable, i32 0, i32 %tmp17.sum52 ; [#uses=1] + %tmp19 = load float* %tmp1851 ; [#uses=1] + %tmp21 = fmul float %tmp19, %y ; [#uses=1] + %tmp23 = fadd float %tmp21, %tmp13 ; [#uses=1] + %tmp27.sum50 = or i32 %tmp5, 2 ; [#uses=1] + %tmp2849 = getelementptr [128 x float]* %lookupTable, i32 0, i32 %tmp27.sum50 ; [#uses=1] + %tmp29 = load float* %tmp2849 ; [#uses=1] + %tmp31 = fmul float %tmp29, %z ; [#uses=1] + %tmp33 = fadd float %tmp31, %tmp23 ; [#uses=1] + %tmp37.sum48 = or i32 %tmp5, 3 ; [#uses=1] + %tmp3847 = getelementptr [128 x float]* %lookupTable, i32 0, i32 %tmp37.sum48 ; [#uses=1] + %tmp39 = load float* %tmp3847 ; [#uses=1] + %tmp41 = fmul float %tmp39, %w ; [#uses=1] + %tmp43 = fadd float %tmp41, %tmp33 ; [#uses=1] + ret float %tmp43 +} + +declare void @llvm.memcpy.p0i8.p0i8.i64(i8* nocapture, i8* nocapture, i64, i32, i1) nounwind + +%T = type { i8, [123 x i8] } +%U = type { i32, i32, i32, i32, i32 } + +@G = constant %T {i8 1, [123 x i8] zeroinitializer } +@H = constant [2 x %U] zeroinitializer, align 16 + +define void @test2() { + %A = alloca %T + %B = alloca %T + %a = bitcast %T* %A to i8* + %b = bitcast %T* %B to i8* + +; CHECK: @test2 + +; %A alloca is deleted +; CHECK-NEXT: alloca [124 x i8] +; CHECK-NEXT: getelementptr inbounds [124 x i8]* + +; use @G instead of %A +; CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i64(i8* %{{.*}}, i8* getelementptr inbounds (%T* @G, i64 0, i32 0) + call void @llvm.memcpy.p0i8.p0i8.i64(i8* %a, i8* bitcast (%T* @G to i8*), i64 124, i32 4, i1 false) + call void @llvm.memcpy.p0i8.p0i8.i64(i8* %b, i8* %a, i64 124, i32 4, i1 false) + call void @bar(i8* %b) + ret void +} + +declare void @bar(i8*) + + +;; Should be able to eliminate the alloca. +define void @test3() { + %A = alloca %T + %a = bitcast %T* %A to i8* + call void @llvm.memcpy.p0i8.p0i8.i64(i8* %a, i8* bitcast (%T* @G to i8*), i64 124, i32 4, i1 false) + call void @bar(i8* %a) readonly +; CHECK: @test3 +; CHECK-NEXT: call void @bar(i8* getelementptr inbounds (%T* @G, i64 0, i32 0)) + ret void +} + +define void @test4() { + %A = alloca %T + %a = bitcast %T* %A to i8* + call void @llvm.memcpy.p0i8.p0i8.i64(i8* %a, i8* bitcast (%T* @G to i8*), i64 124, i32 4, i1 false) + call void @baz(i8* byval %a) +; CHECK: @test4 +; CHECK-NEXT: call void @baz(i8* byval getelementptr inbounds (%T* @G, i64 0, i32 0)) + ret void +} + +declare void @llvm.lifetime.start(i64, i8*) +define void @test5() { + %A = alloca %T + %a = bitcast %T* %A to i8* + call void @llvm.lifetime.start(i64 -1, i8* %a) + call void @llvm.memcpy.p0i8.p0i8.i64(i8* %a, i8* bitcast (%T* @G to i8*), i64 124, i32 4, i1 false) + call void @baz(i8* byval %a) +; CHECK: @test5 +; CHECK-NEXT: call void @baz(i8* byval getelementptr inbounds (%T* @G, i64 0, i32 0)) + ret void +} + + +declare void @baz(i8* byval) + + +define void @test6() { + %A = alloca %U, align 16 + %a = bitcast %U* %A to i8* + call void @llvm.memcpy.p0i8.p0i8.i64(i8* %a, i8* bitcast ([2 x %U]* @H to i8*), i64 20, i32 16, i1 false) + call void @bar(i8* %a) readonly +; CHECK: @test6 +; CHECK-NEXT: call void @bar(i8* bitcast ([2 x %U]* @H to i8*)) + ret void +} + +define void @test7() { + %A = alloca %U, align 16 + %a = bitcast %U* %A to i8* + call void @llvm.memcpy.p0i8.p0i8.i64(i8* %a, i8* bitcast (%U* getelementptr ([2 x %U]* @H, i64 0, i32 0) to i8*), i64 20, i32 4, i1 false) + call void @bar(i8* %a) readonly +; CHECK: @test7 +; CHECK-NEXT: call void @bar(i8* bitcast ([2 x %U]* @H to i8*)) + ret void +} + +define void @test8() { + %A = alloca %U, align 16 + %a = bitcast %U* %A to i8* + call void @llvm.memcpy.p0i8.p0i8.i64(i8* %a, i8* bitcast (%U* getelementptr ([2 x %U]* @H, i64 0, i32 1) to i8*), i64 20, i32 4, i1 false) + call void @bar(i8* %a) readonly +; CHECK: @test8 +; CHECK: llvm.memcpy +; CHECK: bar + ret void +} diff --git a/test/Transforms/ScalarRepl/memcpy-from-global.ll b/test/Transforms/ScalarRepl/memcpy-from-global.ll deleted file mode 100644 index 5557a8f..0000000 --- a/test/Transforms/ScalarRepl/memcpy-from-global.ll +++ /dev/null @@ -1,146 +0,0 @@ -; RUN: opt < %s -scalarrepl -S | FileCheck %s -target datalayout = "E-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64" -@C.0.1248 = internal constant [128 x float] [ float -1.000000e+00, float -1.000000e+00, float -1.000000e+00, float 0.000000e+00, float -1.000000e+00, float -1.000000e+00, float 0.000000e+00, float -1.000000e+00, float -1.000000e+00, float -1.000000e+00, float 0.000000e+00, float 1.000000e+00, float -1.000000e+00, float -1.000000e+00, float 1.000000e+00, float 0.000000e+00, float -1.000000e+00, float 0.000000e+00, float -1.000000e+00, float -1.000000e+00, float -1.000000e+00, float 0.000000e+00, float -1.000000e+00, float 1.000000e+00, float -1.000000e+00, float 0.000000e+00, float 1.000000e+00, float -1.000000e+00, float -1.000000e+00, float 0.000000e+00, float 1.000000e+00, float 1.000000e+00, float -1.000000e+00, float 1.000000e+00, float -1.000000e+00, float 0.000000e+00, float -1.000000e+00, float 1.000000e+00, float 0.000000e+00, float -1.000000e+00, float -1.000000e+00, float 1.000000e+00, float 0.000000e+00, float 1.000000e+00, float -1.000000e+00, float 1.000000e+00, float 1.000000e+00, float 0.000000e+00, float 0.000000e+00, float -1.000000e+00, float -1.000000e+00, float -1.000000e+00, float 0.000000e+00, float -1.000000e+00, float -1.000000e+00, float 1.000000e+00, float 0.000000e+00, float -1.000000e+00, float 1.000000e+00, float -1.000000e+00, float 0.000000e+00, float -1.000000e+00, float 1.000000e+00, float 1.000000e+00, float 1.000000e+00, float -1.000000e+00, float -1.000000e+00, float 0.000000e+00, float 1.000000e+00, float -1.000000e+00, float 0.000000e+00, float -1.000000e+00, float 1.000000e+00, float -1.000000e+00, float 0.000000e+00, float 1.000000e+00, float 1.000000e+00, float -1.000000e+00, float 1.000000e+00, float 0.000000e+00, float 1.000000e+00, float 0.000000e+00, float -1.000000e+00, float -1.000000e+00, float 1.000000e+00, float 0.000000e+00, float -1.000000e+00, float 1.000000e+00, float 1.000000e+00, float 0.000000e+00, float 1.000000e+00, float -1.000000e+00, float 1.000000e+00, float 0.000000e+00, float 1.000000e+00, float 1.000000e+00, float 1.000000e+00, float 1.000000e+00, float -1.000000e+00, float 0.000000e+00, float 1.000000e+00, float 1.000000e+00, float 0.000000e+00, float -1.000000e+00, float 1.000000e+00, float 1.000000e+00, float 0.000000e+00, float 1.000000e+00, float 1.000000e+00, float 1.000000e+00, float 1.000000e+00, float 0.000000e+00, float 0.000000e+00, float 1.000000e+00, float -1.000000e+00, float -1.000000e+00, float 0.000000e+00, float 1.000000e+00, float -1.000000e+00, float 1.000000e+00, float 0.000000e+00, float 1.000000e+00, float 1.000000e+00, float -1.000000e+00, float 0.000000e+00, float 1.000000e+00, float 1.000000e+00, float 1.000000e+00 ], align 32 ; <[128 x float]*> [#uses=1] - -define float @test1(i32 %hash, float %x, float %y, float %z, float %w) { -entry: - %lookupTable = alloca [128 x float], align 16 ; <[128 x float]*> [#uses=5] - %lookupTable1 = bitcast [128 x float]* %lookupTable to i8* ; [#uses=1] - call void @llvm.memcpy.i32( i8* %lookupTable1, i8* bitcast ([128 x float]* @C.0.1248 to i8*), i32 512, i32 16 ) - -; CHECK: @test1 -; CHECK-NOT: alloca -; CHECK-NOT: call{{.*}}@llvm.memcpy -; CHECK: %lookupTable1 = bitcast [128 x float]* @C.0.1248 to i8* -; CHECK-NOT: call{{.*}}@llvm.memcpy - - %tmp3 = shl i32 %hash, 2 ; [#uses=1] - %tmp5 = and i32 %tmp3, 124 ; [#uses=4] - %tmp753 = getelementptr [128 x float]* %lookupTable, i32 0, i32 %tmp5 ; [#uses=1] - %tmp9 = load float* %tmp753 ; [#uses=1] - %tmp11 = fmul float %tmp9, %x ; [#uses=1] - %tmp13 = fadd float %tmp11, 0.000000e+00 ; [#uses=1] - %tmp17.sum52 = or i32 %tmp5, 1 ; [#uses=1] - %tmp1851 = getelementptr [128 x float]* %lookupTable, i32 0, i32 %tmp17.sum52 ; [#uses=1] - %tmp19 = load float* %tmp1851 ; [#uses=1] - %tmp21 = fmul float %tmp19, %y ; [#uses=1] - %tmp23 = fadd float %tmp21, %tmp13 ; [#uses=1] - %tmp27.sum50 = or i32 %tmp5, 2 ; [#uses=1] - %tmp2849 = getelementptr [128 x float]* %lookupTable, i32 0, i32 %tmp27.sum50 ; [#uses=1] - %tmp29 = load float* %tmp2849 ; [#uses=1] - %tmp31 = fmul float %tmp29, %z ; [#uses=1] - %tmp33 = fadd float %tmp31, %tmp23 ; [#uses=1] - %tmp37.sum48 = or i32 %tmp5, 3 ; [#uses=1] - %tmp3847 = getelementptr [128 x float]* %lookupTable, i32 0, i32 %tmp37.sum48 ; [#uses=1] - %tmp39 = load float* %tmp3847 ; [#uses=1] - %tmp41 = fmul float %tmp39, %w ; [#uses=1] - %tmp43 = fadd float %tmp41, %tmp33 ; [#uses=1] - ret float %tmp43 -} - -declare void @llvm.memcpy.i32(i8*, i8*, i32, i32) - - - -declare void @llvm.memcpy.p0i8.p0i8.i64(i8* nocapture, i8* nocapture, i64, i32, i1) nounwind - -%T = type { i8, [123 x i8] } -%U = type { i32, i32, i32, i32, i32 } - -@G = constant %T {i8 1, [123 x i8] zeroinitializer } -@H = constant [2 x %U] zeroinitializer, align 16 - -define void @test2() { - %A = alloca %T - %B = alloca %T - %a = bitcast %T* %A to i8* - %b = bitcast %T* %B to i8* - -; CHECK: @test2 - -; %A alloca is deleted -; CHECK-NEXT: %B = alloca %T - -; use @G instead of %A -; CHECK-NEXT: %a = bitcast %T* @G to i8* - call void @llvm.memcpy.p0i8.p0i8.i64(i8* %a, i8* bitcast (%T* @G to i8*), i64 124, i32 4, i1 false) - call void @llvm.memcpy.p0i8.p0i8.i64(i8* %b, i8* %a, i64 124, i32 4, i1 false) - call void @bar(i8* %b) - ret void -} - -declare void @bar(i8*) - - -;; Should be able to eliminate the alloca. -define void @test3() { - %A = alloca %T - %a = bitcast %T* %A to i8* - call void @llvm.memcpy.p0i8.p0i8.i64(i8* %a, i8* bitcast (%T* @G to i8*), i64 124, i32 4, i1 false) - call void @bar(i8* %a) readonly -; CHECK: @test3 -; CHECK-NEXT: %a = bitcast %T* @G to i8* -; CHECK-NEXT: call void @bar(i8* %a) - ret void -} - -define void @test4() { - %A = alloca %T - %a = bitcast %T* %A to i8* - call void @llvm.memcpy.p0i8.p0i8.i64(i8* %a, i8* bitcast (%T* @G to i8*), i64 124, i32 4, i1 false) - call void @baz(i8* byval %a) -; CHECK: @test4 -; CHECK-NEXT: %a = bitcast %T* @G to i8* -; CHECK-NEXT: call void @baz(i8* byval %a) - ret void -} - -declare void @llvm.lifetime.start(i64, i8*) -define void @test5() { - %A = alloca %T - %a = bitcast %T* %A to i8* - call void @llvm.lifetime.start(i64 -1, i8* %a) - call void @llvm.memcpy.p0i8.p0i8.i64(i8* %a, i8* bitcast (%T* @G to i8*), i64 124, i32 4, i1 false) - call void @baz(i8* byval %a) -; CHECK: @test5 -; CHECK-NEXT: %a = bitcast %T* @G to i8* -; CHECK-NEXT: call void @baz(i8* byval %a) - ret void -} - - -declare void @baz(i8* byval) - - -define void @test6() { - %A = alloca %U, align 16 - %a = bitcast %U* %A to i8* - call void @llvm.memcpy.p0i8.p0i8.i64(i8* %a, i8* bitcast ([2 x %U]* @H to i8*), i64 20, i32 16, i1 false) - call void @bar(i8* %a) readonly -; CHECK: @test6 -; CHECK-NEXT: %a = bitcast -; CHECK-NEXT: call void @bar(i8* %a) - ret void -} - -define void @test7() { - %A = alloca %U, align 16 - %a = bitcast %U* %A to i8* - call void @llvm.memcpy.p0i8.p0i8.i64(i8* %a, i8* bitcast (%U* getelementptr ([2 x %U]* @H, i64 0, i32 0) to i8*), i64 20, i32 4, i1 false) - call void @bar(i8* %a) readonly -; CHECK: @test7 -; CHECK-NEXT: %a = bitcast -; CHECK-NEXT: call void @bar(i8* %a) - ret void -} - -define void @test8() { - %A = alloca %U, align 16 - %a = bitcast %U* %A to i8* - call void @llvm.memcpy.p0i8.p0i8.i64(i8* %a, i8* bitcast (%U* getelementptr ([2 x %U]* @H, i64 0, i32 1) to i8*), i64 20, i32 4, i1 false) - call void @bar(i8* %a) readonly -; CHECK: @test8 -; CHECK: llvm.memcpy -; CHECK: bar - ret void -} -- cgit v1.1