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