1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt < %s -passes=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 ptr
15 declare void @llvm.memcpy.p0.p0.i64(ptr nocapture writeonly, ptr nocapture readonly, i64, i1)
16 declare void @__cxa_throw(ptr, ptr, ptr)
17 declare i32 @__gxx_personality_v0(...)
18 declare ptr @__cxa_begin_catch(ptr)
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(ptr noalias %dst, ptr %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.p0.p0.i64(ptr align 8 [[TEMP]], ptr 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.p0.p0.i64(ptr align 8 [[DST:%.*]], ptr align 8 [[SRC]], i64 64, i1 false)
34 ; CHECK-NEXT: ret void
37 %temp = alloca i8, i32 64
38 call void @llvm.memcpy.p0.p0.i64(ptr align 8 %temp, ptr nonnull align 8%src, i64 64, i1 false)
39 br i1 %some_condition, label %more, label %out
46 call void @llvm.memcpy.p0.p0.i64(ptr align 8 %dst, ptr 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: call void @llvm.memcpy.p0.p0.i64(ptr align 4 [[S]], ptr align 4 @s_foo, i64 8, i1 false)
60 ; CHECK-NEXT: br i1 [[T3:%.*]], label [[BB4:%.*]], label [[BB7:%.*]]
62 ; CHECK-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr align 4 [[T]], ptr align 4 @s_foo, i64 8, i1 false)
63 ; CHECK-NEXT: br label [[BB7]]
65 ; CHECK-NEXT: [[T9:%.*]] = load i32, ptr [[T]], align 4
66 ; CHECK-NEXT: [[T10:%.*]] = getelementptr [[STRUCT_S]], ptr [[T]], i32 0, i32 1
67 ; CHECK-NEXT: [[T11:%.*]] = load i32, ptr [[T10]], align 4
68 ; CHECK-NEXT: [[T12:%.*]] = add i32 [[T9]], [[T11]]
69 ; CHECK-NEXT: ret i32 [[T12]]
72 %s = alloca %struct.s, align 4
73 %t = alloca %struct.s, align 4
74 call void @llvm.memcpy.p0.p0.i64(ptr align 4 %s, ptr align 4 @s_foo, i64 8, i1 false)
75 br i1 %t3, label %bb4, label %bb7
78 call void @llvm.memcpy.p0.p0.i64(ptr align 4 %t, ptr align 4 %s, i64 8, i1 false)
81 bb7: ; preds = %bb4, %bb
82 %t9 = load i32, ptr %t, align 4
83 %t10 = getelementptr %struct.s, ptr %t, i32 0, i32 1
84 %t11 = load i32, ptr %t10, align 4
85 %t12 = add i32 %t9, %t11
89 ; A CFG diamond with an invoke on one side, and a partially redundant memcpy
90 ; into an alloca on the other. Test that the memcpy inside the diamond is
91 ; optimized to copy ; directly from the original source rather than from the
92 ; temporary. This more complex test represents a relatively common usage
95 define i32 @baz(i1 %t5) personality ptr @__gxx_personality_v0 {
98 ; CHECK-NEXT: [[S:%.*]] = alloca [[STRUCT_S:%.*]], align 4
99 ; CHECK-NEXT: [[T:%.*]] = alloca [[STRUCT_S]], align 4
100 ; CHECK-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr align 4 [[S]], ptr align 4 @s_baz, i64 8, i1 false)
101 ; CHECK-NEXT: br i1 [[T5:%.*]], label [[BB6:%.*]], label [[BB22:%.*]]
103 ; CHECK-NEXT: invoke void @__cxa_throw(ptr null, ptr @i, ptr null)
104 ; CHECK-NEXT: to label [[BB25:%.*]] unwind label [[BB9:%.*]]
106 ; CHECK-NEXT: [[T10:%.*]] = landingpad { ptr, i32 }
107 ; CHECK-NEXT: catch ptr null
108 ; CHECK-NEXT: br label [[BB13:%.*]]
110 ; CHECK-NEXT: [[T15:%.*]] = call ptr @__cxa_begin_catch(ptr null)
111 ; CHECK-NEXT: br label [[BB23:%.*]]
113 ; CHECK-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr align 4 [[T]], ptr align 4 @s_baz, i64 8, i1 false)
114 ; CHECK-NEXT: br label [[BB23]]
116 ; CHECK-NEXT: [[T18:%.*]] = load i32, ptr [[T]], align 4
117 ; CHECK-NEXT: [[T19:%.*]] = getelementptr inbounds [[STRUCT_S]], ptr [[T]], i32 0, i32 1
118 ; CHECK-NEXT: [[T20:%.*]] = load i32, ptr [[T19]], align 4
119 ; CHECK-NEXT: [[T21:%.*]] = add nsw i32 [[T18]], [[T20]]
120 ; CHECK-NEXT: ret i32 [[T21]]
122 ; CHECK-NEXT: unreachable
125 %s = alloca %struct.s, align 4
126 %t = alloca %struct.s, align 4
127 call void @llvm.memcpy.p0.p0.i64(ptr align 4 %s, ptr align 4 @s_baz, i64 8, i1 false)
128 br i1 %t5, label %bb6, label %bb22
131 invoke void @__cxa_throw(ptr null, ptr @i, ptr null)
132 to label %bb25 unwind label %bb9
135 %t10 = landingpad { ptr, i32 }
140 %t15 = call ptr @__cxa_begin_catch(ptr null)
144 call void @llvm.memcpy.p0.p0.i64(ptr align 4 %t, ptr align 4 %s, i64 8, i1 false)
147 bb23: ; preds = %bb22, %bb13
148 %t18 = load i32, ptr %t, align 4
149 %t19 = getelementptr inbounds %struct.s, ptr %t, i32 0, i32 1
150 %t20 = load i32, ptr %t19, align 4
151 %t21 = add nsw i32 %t18, %t20
158 define void @memphi_with_unrelated_clobber(i1 %cond, ptr %arg, ptr noalias %a, ptr noalias %b, ptr noalias %c) {
159 ; CHECK-LABEL: @memphi_with_unrelated_clobber(
161 ; CHECK-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr [[A:%.*]], ptr [[B:%.*]], i64 16, i1 false)
162 ; CHECK-NEXT: br i1 [[COND:%.*]], label [[THEN:%.*]], label [[EXIT:%.*]]
164 ; CHECK-NEXT: store i64 0, ptr [[ARG:%.*]], align 4
165 ; CHECK-NEXT: br label [[EXIT]]
167 ; CHECK-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr [[C:%.*]], ptr [[B]], i64 16, i1 false)
168 ; CHECK-NEXT: ret void
171 call void @llvm.memcpy.p0.p0.i64(ptr %a, ptr %b, i64 16, i1 false)
172 br i1 %cond, label %then, label %exit
175 store i64 0, ptr %arg
179 call void @llvm.memcpy.p0.p0.i64(ptr %c, ptr %a, i64 16, i1 false)