1; NOTE: Assertions have been autogenerated by utils/update_test_checks.py 2; RUN: opt -S -memcpyopt < %s -enable-memcpyopt-memoryssa=0 | FileCheck %s 3; RUN: opt -S -memcpyopt < %s -enable-memcpyopt-memoryssa=1 -verify-memoryssa | FileCheck %s 4 5; PR40118: BasicAA didn't realize that stackrestore ends the lifetime of 6; unescaped dynamic allocas, such as those that might come from inalloca. 7 8source_filename = "t.cpp" 9target datalayout = "e-m:x-p:32:32-i64:64-f80:32-n8:16:32-a:0:32-S32" 10target triple = "i686-unknown-windows-msvc19.14.26433" 11 12@str = internal constant [9 x i8] c"abcdxxxxx" 13 14 15; Test that we can propagate memcpy through an unescaped dynamic alloca across 16; a call to @external. 17 18define i32 @test_norestore(i32 %n) { 19; NO_MSSA-LABEL: @test_norestore( 20; NO_MSSA-NEXT: [[TMPMEM:%.*]] = alloca [10 x i8], align 4 21; NO_MSSA-NEXT: [[TMP:%.*]] = getelementptr inbounds [10 x i8], [10 x i8]* [[TMPMEM]], i32 0, i32 0 22; NO_MSSA-NEXT: [[P:%.*]] = alloca i8, i32 [[N:%.*]], align 4 23; NO_MSSA-NEXT: call void @llvm.memcpy.p0i8.p0i8.i32(i8* [[P]], i8* align 1 getelementptr inbounds ([9 x i8], [9 x i8]* @str, i32 0, i32 0), i32 9, i1 false) 24; NO_MSSA-NEXT: [[P10:%.*]] = getelementptr inbounds i8, i8* [[P]], i32 9 25; NO_MSSA-NEXT: store i8 0, i8* [[P10]], align 1 26; NO_MSSA-NEXT: call void @llvm.memcpy.p0i8.p0i8.i32(i8* [[TMP]], i8* [[P]], i32 10, i1 false) 27; NO_MSSA-NEXT: call void @external() 28; NO_MSSA-NEXT: [[HEAP:%.*]] = call i8* @malloc(i32 9) 29; NO_MSSA-NEXT: call void @llvm.memcpy.p0i8.p0i8.i32(i8* [[HEAP]], i8* [[P]], i32 9, i1 false) 30; NO_MSSA-NEXT: call void @useit(i8* [[HEAP]]) 31; NO_MSSA-NEXT: ret i32 0 32; 33; MSSA-LABEL: @test_norestore( 34; MSSA-NEXT: [[TMPMEM:%.*]] = alloca [10 x i8], align 4 35; MSSA-NEXT: [[TMP:%.*]] = getelementptr inbounds [10 x i8], [10 x i8]* [[TMPMEM]], i32 0, i32 0 36; MSSA-NEXT: [[P:%.*]] = alloca i8, i32 [[N:%.*]], align 4 37; MSSA-NEXT: call void @llvm.memcpy.p0i8.p0i8.i32(i8* [[P]], i8* align 1 getelementptr inbounds ([9 x i8], [9 x i8]* @str, i32 0, i32 0), i32 9, i1 false) 38; MSSA-NEXT: [[P10:%.*]] = getelementptr inbounds i8, i8* [[P]], i32 9 39; MSSA-NEXT: store i8 0, i8* [[P10]], align 1 40; MSSA-NEXT: call void @llvm.memcpy.p0i8.p0i8.i32(i8* [[TMP]], i8* [[P]], i32 10, i1 false) 41; MSSA-NEXT: call void @external() 42; MSSA-NEXT: [[HEAP:%.*]] = call i8* @malloc(i32 9) 43; MSSA-NEXT: call void @llvm.memmove.p0i8.p0i8.i32(i8* [[HEAP]], i8* align 1 getelementptr inbounds ([9 x i8], [9 x i8]* @str, i32 0, i32 0), i32 9, i1 false) 44; MSSA-NEXT: call void @useit(i8* [[HEAP]]) 45; MSSA-NEXT: ret i32 0 46; 47 %tmpmem = alloca [10 x i8], align 4 48 %tmp = getelementptr inbounds [10 x i8], [10 x i8]* %tmpmem, i32 0, i32 0 49 50 ; Make a dynamic alloca, initialize it. 51 %p = alloca i8, i32 %n, align 4 52 call void @llvm.memcpy.p0i8.p0i8.i32(i8* %p, i8* align 1 getelementptr inbounds ([9 x i8], [9 x i8]* @str, i32 0, i32 0), i32 9, i1 false) 53 54 ; This extra byte exists to prevent memcpyopt from propagating @str. 55 %p10 = getelementptr inbounds i8, i8* %p, i32 9 56 store i8 0, i8* %p10 57 58 call void @llvm.memcpy.p0i8.p0i8.i32(i8* %tmp, i8* %p, i32 10, i1 false) 59 call void @external() 60 %heap = call i8* @malloc(i32 9) 61 call void @llvm.memcpy.p0i8.p0i8.i32(i8* %heap, i8* %tmp, i32 9, i1 false) 62 call void @useit(i8* %heap) 63 ret i32 0 64} 65 66; Do not propagate memcpy from %p across the stackrestore. 67 68define i32 @test_stackrestore() { 69; CHECK-LABEL: @test_stackrestore( 70; CHECK-NEXT: [[TMPMEM:%.*]] = alloca [10 x i8], align 4 71; CHECK-NEXT: [[TMP:%.*]] = getelementptr inbounds [10 x i8], [10 x i8]* [[TMPMEM]], i32 0, i32 0 72; CHECK-NEXT: [[INALLOCA_SAVE:%.*]] = tail call i8* @llvm.stacksave() 73; CHECK-NEXT: [[ARGMEM:%.*]] = alloca inalloca [10 x i8], align 4 74; CHECK-NEXT: [[P:%.*]] = getelementptr inbounds [10 x i8], [10 x i8]* [[ARGMEM]], i32 0, i32 0 75; CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i32(i8* [[P]], i8* align 1 getelementptr inbounds ([9 x i8], [9 x i8]* @str, i32 0, i32 0), i32 9, i1 false) 76; CHECK-NEXT: [[P10:%.*]] = getelementptr inbounds [10 x i8], [10 x i8]* [[ARGMEM]], i32 0, i32 9 77; CHECK-NEXT: store i8 0, i8* [[P10]], align 1 78; CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i32(i8* [[TMP]], i8* [[P]], i32 10, i1 false) 79; CHECK-NEXT: call void @llvm.stackrestore(i8* [[INALLOCA_SAVE]]) 80; CHECK-NEXT: [[HEAP:%.*]] = call i8* @malloc(i32 9) 81; CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i32(i8* [[HEAP]], i8* [[TMP]], i32 9, i1 false) 82; CHECK-NEXT: call void @useit(i8* [[HEAP]]) 83; CHECK-NEXT: ret i32 0 84; 85 %tmpmem = alloca [10 x i8], align 4 86 %tmp = getelementptr inbounds [10 x i8], [10 x i8]* %tmpmem, i32 0, i32 0 87 %inalloca.save = tail call i8* @llvm.stacksave() 88 %argmem = alloca inalloca [10 x i8], align 4 89 %p = getelementptr inbounds [10 x i8], [10 x i8]* %argmem, i32 0, i32 0 90 call void @llvm.memcpy.p0i8.p0i8.i32(i8* %p, i8* align 1 getelementptr inbounds ([9 x i8], [9 x i8]* @str, i32 0, i32 0), i32 9, i1 false) 91 92 ; This extra byte exists to prevent memcpyopt from propagating @str. 93 %p10 = getelementptr inbounds [10 x i8], [10 x i8]* %argmem, i32 0, i32 9 94 store i8 0, i8* %p10 95 96 call void @llvm.memcpy.p0i8.p0i8.i32(i8* %tmp, i8* %p, i32 10, i1 false) 97 call void @llvm.stackrestore(i8* %inalloca.save) 98 %heap = call i8* @malloc(i32 9) 99 call void @llvm.memcpy.p0i8.p0i8.i32(i8* %heap, i8* %tmp, i32 9, i1 false) 100 call void @useit(i8* %heap) 101 ret i32 0 102} 103 104declare void @llvm.memcpy.p0i8.p0i8.i32(i8* nocapture writeonly, i8* nocapture readonly, i32, i1) 105declare i8* @llvm.stacksave() 106declare void @llvm.stackrestore(i8*) 107declare i8* @malloc(i32) 108declare void @useit(i8*) 109declare void @external() 110