1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt < %s -memcpyopt -S -verify-memoryssa | FileCheck %s
4 ; Test whether memcpy-memcpy dependence is optimized across
5 ; basic blocks (conditional branches and invokes).
6 ; TODO: This is not supported yet.
8 %struct.s = type { i32, i32 }
10 @s_foo = private unnamed_addr constant %struct.s { i32 1, i32 2 }, align 4
11 @s_baz = private unnamed_addr constant %struct.s { i32 1, i32 2 }, align 4
12 @i = external constant i8*
15 declare void @llvm.memcpy.p0i8.p0i8.i64(i8* nocapture writeonly, i8* nocapture readonly, i64, i1)
16 declare void @__cxa_throw(i8*, i8*, i8*)
17 declare i32 @__gxx_personality_v0(...)
18 declare i8* @__cxa_begin_catch(i8*)
20 ; A simple partial redundancy. Test that the second memcpy is optimized
21 ; to copy directly from the original source rather than from the temporary.
23 define void @wobble(i8* noalias %dst, i8* %src, i1 %some_condition) {
24 ; CHECK-LABEL: @wobble(
26 ; CHECK-NEXT: [[TEMP:%.*]] = alloca i8, i32 64, align 1
27 ; CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 8 [[TEMP]], i8* nonnull align 8 [[SRC:%.*]], i64 64, i1 false)
28 ; CHECK-NEXT: br i1 [[SOME_CONDITION:%.*]], label [[MORE:%.*]], label [[OUT:%.*]]
30 ; CHECK-NEXT: call void @qux()
31 ; CHECK-NEXT: unreachable
33 ; CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 8 [[DST:%.*]], i8* align 8 [[SRC]], i64 64, i1 false)
34 ; CHECK-NEXT: ret void
37 %temp = alloca i8, i32 64
38 call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 8 %temp, i8* nonnull align 8%src, i64 64, i1 false)
39 br i1 %some_condition, label %more, label %out
46 call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 8 %dst, i8* align 8 %temp, i64 64, i1 false)
50 ; A CFG triangle with a partial redundancy targeting an alloca. Test that the
51 ; memcpy inside the triangle is optimized to copy directly from the original
52 ; source rather than from the temporary.
54 define i32 @foo(i1 %t3) {
57 ; CHECK-NEXT: [[S:%.*]] = alloca [[STRUCT_S:%.*]], align 4
58 ; CHECK-NEXT: [[T:%.*]] = alloca [[STRUCT_S]], align 4
59 ; CHECK-NEXT: [[S1:%.*]] = bitcast %struct.s* [[S]] to i8*
60 ; CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 4 [[S1]], i8* align 4 bitcast (%struct.s* @s_foo to i8*), i64 8, i1 false)
61 ; CHECK-NEXT: br i1 [[T3:%.*]], label [[BB4:%.*]], label [[BB7:%.*]]
63 ; CHECK-NEXT: [[T5:%.*]] = bitcast %struct.s* [[T]] to i8*
64 ; CHECK-NEXT: [[S6:%.*]] = bitcast %struct.s* [[S]] to i8*
65 ; CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 4 [[T5]], i8* align 4 bitcast (%struct.s* @s_foo to i8*), i64 8, i1 false)
66 ; CHECK-NEXT: br label [[BB7]]
68 ; CHECK-NEXT: [[T8:%.*]] = getelementptr [[STRUCT_S]], %struct.s* [[T]], i32 0, i32 0
69 ; CHECK-NEXT: [[T9:%.*]] = load i32, i32* [[T8]], align 4
70 ; CHECK-NEXT: [[T10:%.*]] = getelementptr [[STRUCT_S]], %struct.s* [[T]], i32 0, i32 1
71 ; CHECK-NEXT: [[T11:%.*]] = load i32, i32* [[T10]], align 4
72 ; CHECK-NEXT: [[T12:%.*]] = add i32 [[T9]], [[T11]]
73 ; CHECK-NEXT: ret i32 [[T12]]
76 %s = alloca %struct.s, align 4
77 %t = alloca %struct.s, align 4
78 %s1 = bitcast %struct.s* %s to i8*
79 call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 4 %s1, i8* align 4 bitcast (%struct.s* @s_foo to i8*), i64 8, i1 false)
80 br i1 %t3, label %bb4, label %bb7
83 %t5 = bitcast %struct.s* %t to i8*
84 %s6 = bitcast %struct.s* %s to i8*
85 call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 4 %t5, i8* align 4 %s6, i64 8, i1 false)
88 bb7: ; preds = %bb4, %bb
89 %t8 = getelementptr %struct.s, %struct.s* %t, i32 0, i32 0
90 %t9 = load i32, i32* %t8, align 4
91 %t10 = getelementptr %struct.s, %struct.s* %t, i32 0, i32 1
92 %t11 = load i32, i32* %t10, align 4
93 %t12 = add i32 %t9, %t11
97 ; A CFG diamond with an invoke on one side, and a partially redundant memcpy
98 ; into an alloca on the other. Test that the memcpy inside the diamond is
99 ; optimized to copy ; directly from the original source rather than from the
100 ; temporary. This more complex test represents a relatively common usage
103 define i32 @baz(i1 %t5) personality i8* bitcast (i32 (...)* @__gxx_personality_v0 to i8*) {
106 ; CHECK-NEXT: [[S:%.*]] = alloca [[STRUCT_S:%.*]], align 4
107 ; CHECK-NEXT: [[T:%.*]] = alloca [[STRUCT_S]], align 4
108 ; CHECK-NEXT: [[S3:%.*]] = bitcast %struct.s* [[S]] to i8*
109 ; CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 4 [[S3]], i8* align 4 bitcast (%struct.s* @s_baz to i8*), i64 8, i1 false)
110 ; CHECK-NEXT: br i1 [[T5:%.*]], label [[BB6:%.*]], label [[BB22:%.*]]
112 ; CHECK-NEXT: invoke void @__cxa_throw(i8* null, i8* bitcast (i8** @i to i8*), i8* null)
113 ; CHECK-NEXT: to label [[BB25:%.*]] unwind label [[BB9:%.*]]
115 ; CHECK-NEXT: [[T10:%.*]] = landingpad { i8*, i32 }
116 ; CHECK-NEXT: catch i8* null
117 ; CHECK-NEXT: br label [[BB13:%.*]]
119 ; CHECK-NEXT: [[T15:%.*]] = call i8* @__cxa_begin_catch(i8* null)
120 ; CHECK-NEXT: br label [[BB23:%.*]]
122 ; CHECK-NEXT: [[T23:%.*]] = bitcast %struct.s* [[T]] to i8*
123 ; CHECK-NEXT: [[S24:%.*]] = bitcast %struct.s* [[S]] to i8*
124 ; CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 4 [[T23]], i8* align 4 bitcast (%struct.s* @s_baz to i8*), i64 8, i1 false)
125 ; CHECK-NEXT: br label [[BB23]]
127 ; CHECK-NEXT: [[T17:%.*]] = getelementptr inbounds [[STRUCT_S]], %struct.s* [[T]], i32 0, i32 0
128 ; CHECK-NEXT: [[T18:%.*]] = load i32, i32* [[T17]], align 4
129 ; CHECK-NEXT: [[T19:%.*]] = getelementptr inbounds [[STRUCT_S]], %struct.s* [[T]], i32 0, i32 1
130 ; CHECK-NEXT: [[T20:%.*]] = load i32, i32* [[T19]], align 4
131 ; CHECK-NEXT: [[T21:%.*]] = add nsw i32 [[T18]], [[T20]]
132 ; CHECK-NEXT: ret i32 [[T21]]
134 ; CHECK-NEXT: unreachable
137 %s = alloca %struct.s, align 4
138 %t = alloca %struct.s, align 4
139 %s3 = bitcast %struct.s* %s to i8*
140 call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 4 %s3, i8* align 4 bitcast (%struct.s* @s_baz to i8*), i64 8, i1 false)
141 br i1 %t5, label %bb6, label %bb22
144 invoke void @__cxa_throw(i8* null, i8* bitcast (i8** @i to i8*), i8* null)
145 to label %bb25 unwind label %bb9
148 %t10 = landingpad { i8*, i32 }
153 %t15 = call i8* @__cxa_begin_catch(i8* null)
157 %t23 = bitcast %struct.s* %t to i8*
158 %s24 = bitcast %struct.s* %s to i8*
159 call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 4 %t23, i8* align 4 %s24, i64 8, i1 false)
162 bb23: ; preds = %bb22, %bb13
163 %t17 = getelementptr inbounds %struct.s, %struct.s* %t, i32 0, i32 0
164 %t18 = load i32, i32* %t17, align 4
165 %t19 = getelementptr inbounds %struct.s, %struct.s* %t, i32 0, i32 1
166 %t20 = load i32, i32* %t19, align 4
167 %t21 = add nsw i32 %t18, %t20