1*9880d681SAndroid Build Coastguard Worker; RUN: opt < %s -basicaa -memcpyopt -dse -S | FileCheck %s 2*9880d681SAndroid Build Coastguard Worker 3*9880d681SAndroid Build Coastguard Workertarget datalayout = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-f32:32:32-f64:32:64-v64:64:64-v128:128:128-a0:0:64-f80:128:128" 4*9880d681SAndroid Build Coastguard Workertarget triple = "i686-apple-darwin9" 5*9880d681SAndroid Build Coastguard Worker 6*9880d681SAndroid Build Coastguard Worker%0 = type { x86_fp80, x86_fp80 } 7*9880d681SAndroid Build Coastguard Worker%1 = type { i32, i32 } 8*9880d681SAndroid Build Coastguard Worker 9*9880d681SAndroid Build Coastguard Workerdefine void @test1(%0* sret %agg.result, x86_fp80 %z.0, x86_fp80 %z.1) nounwind { 10*9880d681SAndroid Build Coastguard Workerentry: 11*9880d681SAndroid Build Coastguard Worker %tmp2 = alloca %0 12*9880d681SAndroid Build Coastguard Worker %memtmp = alloca %0, align 16 13*9880d681SAndroid Build Coastguard Worker %tmp5 = fsub x86_fp80 0xK80000000000000000000, %z.1 14*9880d681SAndroid Build Coastguard Worker call void @ccoshl(%0* sret %memtmp, x86_fp80 %tmp5, x86_fp80 %z.0) nounwind 15*9880d681SAndroid Build Coastguard Worker %tmp219 = bitcast %0* %tmp2 to i8* 16*9880d681SAndroid Build Coastguard Worker %memtmp20 = bitcast %0* %memtmp to i8* 17*9880d681SAndroid Build Coastguard Worker call void @llvm.memcpy.p0i8.p0i8.i32(i8* %tmp219, i8* %memtmp20, i32 32, i32 16, i1 false) 18*9880d681SAndroid Build Coastguard Worker %agg.result21 = bitcast %0* %agg.result to i8* 19*9880d681SAndroid Build Coastguard Worker call void @llvm.memcpy.p0i8.p0i8.i32(i8* %agg.result21, i8* %tmp219, i32 32, i32 16, i1 false) 20*9880d681SAndroid Build Coastguard Worker ret void 21*9880d681SAndroid Build Coastguard Worker 22*9880d681SAndroid Build Coastguard Worker; Check that one of the memcpy's are removed. 23*9880d681SAndroid Build Coastguard Worker;; FIXME: PR 8643 We should be able to eliminate the last memcpy here. 24*9880d681SAndroid Build Coastguard Worker 25*9880d681SAndroid Build Coastguard Worker; CHECK-LABEL: @test1( 26*9880d681SAndroid Build Coastguard Worker; CHECK: call void @ccoshl 27*9880d681SAndroid Build Coastguard Worker; CHECK: call void @llvm.memcpy 28*9880d681SAndroid Build Coastguard Worker; CHECK-NOT: llvm.memcpy 29*9880d681SAndroid Build Coastguard Worker; CHECK: ret void 30*9880d681SAndroid Build Coastguard Worker} 31*9880d681SAndroid Build Coastguard Worker 32*9880d681SAndroid Build Coastguard Workerdeclare void @ccoshl(%0* nocapture sret, x86_fp80, x86_fp80) nounwind 33*9880d681SAndroid Build Coastguard Worker 34*9880d681SAndroid Build Coastguard Worker 35*9880d681SAndroid Build Coastguard Worker; The intermediate alloca and one of the memcpy's should be eliminated, the 36*9880d681SAndroid Build Coastguard Worker; other should be related with a memmove. 37*9880d681SAndroid Build Coastguard Workerdefine void @test2(i8* %P, i8* %Q) nounwind { 38*9880d681SAndroid Build Coastguard Worker %memtmp = alloca %0, align 16 39*9880d681SAndroid Build Coastguard Worker %R = bitcast %0* %memtmp to i8* 40*9880d681SAndroid Build Coastguard Worker call void @llvm.memcpy.p0i8.p0i8.i32(i8* %R, i8* %P, i32 32, i32 16, i1 false) 41*9880d681SAndroid Build Coastguard Worker call void @llvm.memcpy.p0i8.p0i8.i32(i8* %Q, i8* %R, i32 32, i32 16, i1 false) 42*9880d681SAndroid Build Coastguard Worker ret void 43*9880d681SAndroid Build Coastguard Worker 44*9880d681SAndroid Build Coastguard Worker; CHECK-LABEL: @test2( 45*9880d681SAndroid Build Coastguard Worker; CHECK-NEXT: call void @llvm.memmove{{.*}}(i8* %Q, i8* %P 46*9880d681SAndroid Build Coastguard Worker; CHECK-NEXT: ret void 47*9880d681SAndroid Build Coastguard Worker} 48*9880d681SAndroid Build Coastguard Worker 49*9880d681SAndroid Build Coastguard Worker 50*9880d681SAndroid Build Coastguard Worker 51*9880d681SAndroid Build Coastguard Worker 52*9880d681SAndroid Build Coastguard Worker@x = external global %0 53*9880d681SAndroid Build Coastguard Worker 54*9880d681SAndroid Build Coastguard Workerdefine void @test3(%0* noalias sret %agg.result) nounwind { 55*9880d681SAndroid Build Coastguard Worker %x.0 = alloca %0 56*9880d681SAndroid Build Coastguard Worker %x.01 = bitcast %0* %x.0 to i8* 57*9880d681SAndroid Build Coastguard Worker call void @llvm.memcpy.p0i8.p0i8.i32(i8* %x.01, i8* bitcast (%0* @x to i8*), i32 32, i32 16, i1 false) 58*9880d681SAndroid Build Coastguard Worker %agg.result2 = bitcast %0* %agg.result to i8* 59*9880d681SAndroid Build Coastguard Worker call void @llvm.memcpy.p0i8.p0i8.i32(i8* %agg.result2, i8* %x.01, i32 32, i32 16, i1 false) 60*9880d681SAndroid Build Coastguard Worker ret void 61*9880d681SAndroid Build Coastguard Worker; CHECK-LABEL: @test3( 62*9880d681SAndroid Build Coastguard Worker; CHECK-NEXT: %agg.result1 = bitcast 63*9880d681SAndroid Build Coastguard Worker; CHECK-NEXT: call void @llvm.memcpy 64*9880d681SAndroid Build Coastguard Worker; CHECK-NEXT: ret void 65*9880d681SAndroid Build Coastguard Worker} 66*9880d681SAndroid Build Coastguard Worker 67*9880d681SAndroid Build Coastguard Worker 68*9880d681SAndroid Build Coastguard Worker; PR8644 69*9880d681SAndroid Build Coastguard Workerdefine void @test4(i8 *%P) { 70*9880d681SAndroid Build Coastguard Worker %A = alloca %1 71*9880d681SAndroid Build Coastguard Worker %a = bitcast %1* %A to i8* 72*9880d681SAndroid Build Coastguard Worker call void @llvm.memcpy.p0i8.p0i8.i64(i8* %a, i8* %P, i64 8, i32 4, i1 false) 73*9880d681SAndroid Build Coastguard Worker call void @test4a(i8* align 1 byval %a) 74*9880d681SAndroid Build Coastguard Worker ret void 75*9880d681SAndroid Build Coastguard Worker; CHECK-LABEL: @test4( 76*9880d681SAndroid Build Coastguard Worker; CHECK-NEXT: call void @test4a( 77*9880d681SAndroid Build Coastguard Worker} 78*9880d681SAndroid Build Coastguard Worker 79*9880d681SAndroid Build Coastguard Workerdeclare void @test4a(i8* align 1 byval) 80*9880d681SAndroid Build Coastguard Workerdeclare void @llvm.memcpy.p0i8.p0i8.i64(i8* nocapture, i8* nocapture, i64, i32, i1) nounwind 81*9880d681SAndroid Build Coastguard Workerdeclare void @llvm.memcpy.p1i8.p1i8.i64(i8 addrspace(1)* nocapture, i8 addrspace(1)* nocapture, i64, i32, i1) nounwind 82*9880d681SAndroid Build Coastguard Worker 83*9880d681SAndroid Build Coastguard Worker%struct.S = type { i128, [4 x i8]} 84*9880d681SAndroid Build Coastguard Worker 85*9880d681SAndroid Build Coastguard Worker@sS = external global %struct.S, align 16 86*9880d681SAndroid Build Coastguard Worker 87*9880d681SAndroid Build Coastguard Workerdeclare void @test5a(%struct.S* align 16 byval) nounwind ssp 88*9880d681SAndroid Build Coastguard Worker 89*9880d681SAndroid Build Coastguard Worker 90*9880d681SAndroid Build Coastguard Worker; rdar://8713376 - This memcpy can't be eliminated. 91*9880d681SAndroid Build Coastguard Workerdefine i32 @test5(i32 %x) nounwind ssp { 92*9880d681SAndroid Build Coastguard Workerentry: 93*9880d681SAndroid Build Coastguard Worker %y = alloca %struct.S, align 16 94*9880d681SAndroid Build Coastguard Worker %tmp = bitcast %struct.S* %y to i8* 95*9880d681SAndroid Build Coastguard Worker call void @llvm.memcpy.p0i8.p0i8.i64(i8* %tmp, i8* bitcast (%struct.S* @sS to i8*), i64 32, i32 16, i1 false) 96*9880d681SAndroid Build Coastguard Worker %a = getelementptr %struct.S, %struct.S* %y, i64 0, i32 1, i64 0 97*9880d681SAndroid Build Coastguard Worker store i8 4, i8* %a 98*9880d681SAndroid Build Coastguard Worker call void @test5a(%struct.S* align 16 byval %y) 99*9880d681SAndroid Build Coastguard Worker ret i32 0 100*9880d681SAndroid Build Coastguard Worker ; CHECK-LABEL: @test5( 101*9880d681SAndroid Build Coastguard Worker ; CHECK: store i8 4 102*9880d681SAndroid Build Coastguard Worker ; CHECK: call void @test5a(%struct.S* byval align 16 %y) 103*9880d681SAndroid Build Coastguard Worker} 104*9880d681SAndroid Build Coastguard Worker 105*9880d681SAndroid Build Coastguard Worker;; Noop memcpy should be zapped. 106*9880d681SAndroid Build Coastguard Workerdefine void @test6(i8 *%P) { 107*9880d681SAndroid Build Coastguard Worker call void @llvm.memcpy.p0i8.p0i8.i64(i8* %P, i8* %P, i64 8, i32 4, i1 false) 108*9880d681SAndroid Build Coastguard Worker ret void 109*9880d681SAndroid Build Coastguard Worker; CHECK-LABEL: @test6( 110*9880d681SAndroid Build Coastguard Worker; CHECK-NEXT: ret void 111*9880d681SAndroid Build Coastguard Worker} 112*9880d681SAndroid Build Coastguard Worker 113*9880d681SAndroid Build Coastguard Worker 114*9880d681SAndroid Build Coastguard Worker; PR9794 - Should forward memcpy into byval argument even though the memcpy 115*9880d681SAndroid Build Coastguard Worker; isn't itself 8 byte aligned. 116*9880d681SAndroid Build Coastguard Worker%struct.p = type { i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32 } 117*9880d681SAndroid Build Coastguard Worker 118*9880d681SAndroid Build Coastguard Workerdefine i32 @test7(%struct.p* nocapture align 8 byval %q) nounwind ssp { 119*9880d681SAndroid Build Coastguard Workerentry: 120*9880d681SAndroid Build Coastguard Worker %agg.tmp = alloca %struct.p, align 4 121*9880d681SAndroid Build Coastguard Worker %tmp = bitcast %struct.p* %agg.tmp to i8* 122*9880d681SAndroid Build Coastguard Worker %tmp1 = bitcast %struct.p* %q to i8* 123*9880d681SAndroid Build Coastguard Worker call void @llvm.memcpy.p0i8.p0i8.i64(i8* %tmp, i8* %tmp1, i64 48, i32 4, i1 false) 124*9880d681SAndroid Build Coastguard Worker %call = call i32 @g(%struct.p* align 8 byval %agg.tmp) nounwind 125*9880d681SAndroid Build Coastguard Worker ret i32 %call 126*9880d681SAndroid Build Coastguard Worker; CHECK-LABEL: @test7( 127*9880d681SAndroid Build Coastguard Worker; CHECK: call i32 @g(%struct.p* byval align 8 %q) [[NUW:#[0-9]+]] 128*9880d681SAndroid Build Coastguard Worker} 129*9880d681SAndroid Build Coastguard Worker 130*9880d681SAndroid Build Coastguard Workerdeclare i32 @g(%struct.p* align 8 byval) 131*9880d681SAndroid Build Coastguard Worker 132*9880d681SAndroid Build Coastguard Workerdeclare void @llvm.memcpy.p0i8.p0i8.i32(i8* nocapture, i8* nocapture, i32, i32, i1) nounwind 133*9880d681SAndroid Build Coastguard Worker 134*9880d681SAndroid Build Coastguard Worker; PR11142 - When looking for a memcpy-memcpy dependency, don't get stuck on 135*9880d681SAndroid Build Coastguard Worker; instructions between the memcpy's that only affect the destination pointer. 136*9880d681SAndroid Build Coastguard Worker@test8.str = internal constant [7 x i8] c"ABCDEF\00" 137*9880d681SAndroid Build Coastguard Worker 138*9880d681SAndroid Build Coastguard Workerdefine void @test8() { 139*9880d681SAndroid Build Coastguard Worker; CHECK: test8 140*9880d681SAndroid Build Coastguard Worker; CHECK-NOT: memcpy 141*9880d681SAndroid Build Coastguard Worker %A = tail call i8* @malloc(i32 10) 142*9880d681SAndroid Build Coastguard Worker %B = getelementptr inbounds i8, i8* %A, i64 2 143*9880d681SAndroid Build Coastguard Worker tail call void @llvm.memcpy.p0i8.p0i8.i32(i8* %B, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @test8.str, i64 0, i64 0), i32 7, i32 1, i1 false) 144*9880d681SAndroid Build Coastguard Worker %C = tail call i8* @malloc(i32 10) 145*9880d681SAndroid Build Coastguard Worker %D = getelementptr inbounds i8, i8* %C, i64 2 146*9880d681SAndroid Build Coastguard Worker tail call void @llvm.memcpy.p0i8.p0i8.i32(i8* %D, i8* %B, i32 7, i32 1, i1 false) 147*9880d681SAndroid Build Coastguard Worker ret void 148*9880d681SAndroid Build Coastguard Worker; CHECK: ret void 149*9880d681SAndroid Build Coastguard Worker} 150*9880d681SAndroid Build Coastguard Worker 151*9880d681SAndroid Build Coastguard Workerdeclare noalias i8* @malloc(i32) 152*9880d681SAndroid Build Coastguard Worker 153*9880d681SAndroid Build Coastguard Worker; rdar://11341081 154*9880d681SAndroid Build Coastguard Worker%struct.big = type { [50 x i32] } 155*9880d681SAndroid Build Coastguard Worker 156*9880d681SAndroid Build Coastguard Workerdefine void @test9_addrspacecast() nounwind ssp uwtable { 157*9880d681SAndroid Build Coastguard Workerentry: 158*9880d681SAndroid Build Coastguard Worker; CHECK-LABEL: @test9_addrspacecast( 159*9880d681SAndroid Build Coastguard Worker; CHECK: f1 160*9880d681SAndroid Build Coastguard Worker; CHECK-NOT: memcpy 161*9880d681SAndroid Build Coastguard Worker; CHECK: f2 162*9880d681SAndroid Build Coastguard Worker %b = alloca %struct.big, align 4 163*9880d681SAndroid Build Coastguard Worker %tmp = alloca %struct.big, align 4 164*9880d681SAndroid Build Coastguard Worker call void @f1(%struct.big* sret %tmp) 165*9880d681SAndroid Build Coastguard Worker %0 = addrspacecast %struct.big* %b to i8 addrspace(1)* 166*9880d681SAndroid Build Coastguard Worker %1 = addrspacecast %struct.big* %tmp to i8 addrspace(1)* 167*9880d681SAndroid Build Coastguard Worker call void @llvm.memcpy.p1i8.p1i8.i64(i8 addrspace(1)* %0, i8 addrspace(1)* %1, i64 200, i32 4, i1 false) 168*9880d681SAndroid Build Coastguard Worker call void @f2(%struct.big* %b) 169*9880d681SAndroid Build Coastguard Worker ret void 170*9880d681SAndroid Build Coastguard Worker} 171*9880d681SAndroid Build Coastguard Worker 172*9880d681SAndroid Build Coastguard Workerdefine void @test9() nounwind ssp uwtable { 173*9880d681SAndroid Build Coastguard Workerentry: 174*9880d681SAndroid Build Coastguard Worker; CHECK: test9 175*9880d681SAndroid Build Coastguard Worker; CHECK: f1 176*9880d681SAndroid Build Coastguard Worker; CHECK-NOT: memcpy 177*9880d681SAndroid Build Coastguard Worker; CHECK: f2 178*9880d681SAndroid Build Coastguard Worker %b = alloca %struct.big, align 4 179*9880d681SAndroid Build Coastguard Worker %tmp = alloca %struct.big, align 4 180*9880d681SAndroid Build Coastguard Worker call void @f1(%struct.big* sret %tmp) 181*9880d681SAndroid Build Coastguard Worker %0 = bitcast %struct.big* %b to i8* 182*9880d681SAndroid Build Coastguard Worker %1 = bitcast %struct.big* %tmp to i8* 183*9880d681SAndroid Build Coastguard Worker call void @llvm.memcpy.p0i8.p0i8.i64(i8* %0, i8* %1, i64 200, i32 4, i1 false) 184*9880d681SAndroid Build Coastguard Worker call void @f2(%struct.big* %b) 185*9880d681SAndroid Build Coastguard Worker ret void 186*9880d681SAndroid Build Coastguard Worker} 187*9880d681SAndroid Build Coastguard Worker 188*9880d681SAndroid Build Coastguard Worker; rdar://14073661. 189*9880d681SAndroid Build Coastguard Worker; Test10 triggered assertion when the compiler try to get the size of the 190*9880d681SAndroid Build Coastguard Worker; opaque type of *x, where the x is the formal argument with attribute 'sret'. 191*9880d681SAndroid Build Coastguard Worker 192*9880d681SAndroid Build Coastguard Worker%opaque = type opaque 193*9880d681SAndroid Build Coastguard Workerdeclare void @foo(i32* noalias nocapture) 194*9880d681SAndroid Build Coastguard Worker 195*9880d681SAndroid Build Coastguard Workerdefine void @test10(%opaque* noalias nocapture sret %x, i32 %y) { 196*9880d681SAndroid Build Coastguard Worker %a = alloca i32, align 4 197*9880d681SAndroid Build Coastguard Worker store i32 %y, i32* %a 198*9880d681SAndroid Build Coastguard Worker call void @foo(i32* noalias nocapture %a) 199*9880d681SAndroid Build Coastguard Worker %c = load i32, i32* %a 200*9880d681SAndroid Build Coastguard Worker %d = bitcast %opaque* %x to i32* 201*9880d681SAndroid Build Coastguard Worker store i32 %c, i32* %d 202*9880d681SAndroid Build Coastguard Worker ret void 203*9880d681SAndroid Build Coastguard Worker} 204*9880d681SAndroid Build Coastguard Worker 205*9880d681SAndroid Build Coastguard Workerdeclare void @f1(%struct.big* nocapture sret) 206*9880d681SAndroid Build Coastguard Workerdeclare void @f2(%struct.big*) 207*9880d681SAndroid Build Coastguard Worker 208*9880d681SAndroid Build Coastguard Worker; CHECK: attributes [[NUW]] = { nounwind } 209*9880d681SAndroid Build Coastguard Worker; CHECK: attributes #1 = { argmemonly nounwind } 210*9880d681SAndroid Build Coastguard Worker; CHECK: attributes #2 = { nounwind ssp } 211*9880d681SAndroid Build Coastguard Worker; CHECK: attributes #3 = { nounwind ssp uwtable } 212