Skip to content

Commit 91ca7b2

Browse files
[clang][CodeGen] Allow memcpy replace with trivial auto var init
Fixes: #84178.
1 parent 96813de commit 91ca7b2

File tree

7 files changed

+43
-57
lines changed

7 files changed

+43
-57
lines changed

clang/lib/CodeGen/CGDecl.cpp

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1241,27 +1241,38 @@ static void emitStoresForConstant(CodeGenModule &CGM, const VarDecl &D,
12411241
return;
12421242
}
12431243

1244-
// If the initializer is small, use a handful of stores.
1244+
// If the initializer is small or trivialAutoVarInit is set, use a handful of
1245+
// stores.
1246+
bool IsTrivialAutoVarInitPattern =
1247+
CGM.getContext().getLangOpts().getTrivialAutoVarInit() ==
1248+
LangOptions::TrivialAutoVarInitKind::Pattern;
12451249
if (shouldSplitConstantStore(CGM, ConstantSize)) {
12461250
if (auto *STy = dyn_cast<llvm::StructType>(Ty)) {
1247-
const llvm::StructLayout *Layout =
1248-
CGM.getDataLayout().getStructLayout(STy);
1249-
for (unsigned i = 0; i != constant->getNumOperands(); i++) {
1250-
CharUnits CurOff = CharUnits::fromQuantity(Layout->getElementOffset(i));
1251-
Address EltPtr = Builder.CreateConstInBoundsByteGEP(
1252-
Loc.withElementType(CGM.Int8Ty), CurOff);
1253-
emitStoresForConstant(CGM, D, EltPtr, isVolatile, Builder,
1254-
constant->getAggregateElement(i), IsAutoInit);
1251+
if (STy == Loc.getElementType() ||
1252+
(STy != Loc.getElementType() && IsTrivialAutoVarInitPattern)) {
1253+
const llvm::StructLayout *Layout =
1254+
CGM.getDataLayout().getStructLayout(STy);
1255+
for (unsigned i = 0; i != constant->getNumOperands(); i++) {
1256+
CharUnits CurOff =
1257+
CharUnits::fromQuantity(Layout->getElementOffset(i));
1258+
Address EltPtr = Builder.CreateConstInBoundsByteGEP(
1259+
Loc.withElementType(CGM.Int8Ty), CurOff);
1260+
emitStoresForConstant(CGM, D, EltPtr, isVolatile, Builder,
1261+
constant->getAggregateElement(i), IsAutoInit);
1262+
}
1263+
return;
12551264
}
1256-
return;
12571265
} else if (auto *ATy = dyn_cast<llvm::ArrayType>(Ty)) {
1258-
for (unsigned i = 0; i != ATy->getNumElements(); i++) {
1259-
Address EltPtr = Builder.CreateConstGEP(
1260-
Loc.withElementType(ATy->getElementType()), i);
1261-
emitStoresForConstant(CGM, D, EltPtr, isVolatile, Builder,
1262-
constant->getAggregateElement(i), IsAutoInit);
1266+
if (ATy == Loc.getElementType() ||
1267+
(ATy != Loc.getElementType() && IsTrivialAutoVarInitPattern)) {
1268+
for (unsigned i = 0; i != ATy->getNumElements(); i++) {
1269+
Address EltPtr = Builder.CreateConstGEP(
1270+
Loc.withElementType(ATy->getElementType()), i);
1271+
emitStoresForConstant(CGM, D, EltPtr, isVolatile, Builder,
1272+
constant->getAggregateElement(i), IsAutoInit);
1273+
}
1274+
return;
12631275
}
1264-
return;
12651276
}
12661277
}
12671278

clang/test/CodeGen/aapcs-align.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,8 @@ void g6() {
134134
f6m(1, 2, 3, 4, 5, s);
135135
}
136136
// CHECK: define{{.*}} void @g6
137-
// CHECK: call void @f6(i32 noundef 1, [4 x i32] [i32 6, i32 7, i32 0, i32 0])
138-
// CHECK: call void @f6m(i32 noundef 1, i32 noundef 2, i32 noundef 3, i32 noundef 4, i32 noundef 5, [4 x i32] [i32 6, i32 7, i32 0, i32 0])
137+
// CHECK: call void @f6(i32 noundef 1, [4 x i32] [i32 6, i32 7, i32 0, i32 undef])
138+
// CHECK: call void @f6m(i32 noundef 1, i32 noundef 2, i32 noundef 3, i32 noundef 4, i32 noundef 5, [4 x i32] [i32 6, i32 7, i32 0, i32 undef])
139139
// CHECK: declare void @f6(i32 noundef, [4 x i32])
140140
// CHECK: declare void @f6m(i32 noundef, i32 noundef, i32 noundef, i32 noundef, i32 noundef, [4 x i32])
141141
}

clang/test/CodeGen/aapcs64-align.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ void g4() {
7575
f4m(1, 2, 3, 4, 5, s);
7676
}
7777
// CHECK: define{{.*}} void @g4()
78-
// CHECK: call void @f4(i32 noundef 1, [2 x i64] %{{.*}})
79-
// CHECK: void @f4m(i32 noundef 1, i32 noundef 2, i32 noundef 3, i32 noundef 4, i32 noundef 5, [2 x i64] %{{.*}})
78+
// CHECK: call void @f4(i32 noundef 1, [2 x i64] [i64 30064771078, i64 0])
79+
// CHECK: void @f4m(i32 noundef 1, i32 noundef 2, i32 noundef 3, i32 noundef 4, i32 noundef 5, [2 x i64] [i64 30064771078, i64 0])
8080
// CHECK: declare void @f4(i32 noundef, [2 x i64])
8181
// CHECK: declare void @f4m(i32 noundef, i32 noundef, i32 noundef, i32 noundef, i32 noundef, [2 x i64])
8282

@@ -95,8 +95,8 @@ void f5m(int, int, int, int, int, P16);
9595
f5m(1, 2, 3, 4, 5, s);
9696
}
9797
// CHECK: define{{.*}} void @g5()
98-
// CHECK: call void @f5(i32 noundef 1, [2 x i64] %{{.*}})
99-
// CHECK: void @f5m(i32 noundef 1, i32 noundef 2, i32 noundef 3, i32 noundef 4, i32 noundef 5, [2 x i64] %{{.*}})
98+
// CHECK: call void @f5(i32 noundef 1, [2 x i64] [i64 30064771078, i64 0])
99+
// CHECK: void @f5m(i32 noundef 1, i32 noundef 2, i32 noundef 3, i32 noundef 4, i32 noundef 5, [2 x i64] [i64 30064771078, i64 0])
100100
// CHECK: declare void @f5(i32 noundef, [2 x i64])
101101
// CHECK: declare void @f5m(i32 noundef, i32 noundef, i32 noundef, i32 noundef, i32 noundef, [2 x i64])
102102

clang/test/CodeGen/attr-counted-by.c

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1314,17 +1314,10 @@ int test14(int idx) {
13141314
// NO-SANITIZE-WITH-ATTR-LABEL: define dso_local i32 @test15(
13151315
// NO-SANITIZE-WITH-ATTR-SAME: i32 noundef [[IDX:%.*]]) local_unnamed_addr #[[ATTR4]] {
13161316
// NO-SANITIZE-WITH-ATTR-NEXT: entry:
1317-
// NO-SANITIZE-WITH-ATTR-NEXT: [[FOO:%.*]] = alloca [[STRUCT_ANON_8:%.*]], align 4
1318-
// NO-SANITIZE-WITH-ATTR-NEXT: call void @llvm.lifetime.start.p0(i64 8, ptr nonnull [[FOO]]) #[[ATTR12]]
1319-
// NO-SANITIZE-WITH-ATTR-NEXT: store i32 1, ptr [[FOO]], align 4
1320-
// NO-SANITIZE-WITH-ATTR-NEXT: [[TMP0:%.*]] = getelementptr inbounds i8, ptr [[FOO]], i64 4
1321-
// NO-SANITIZE-WITH-ATTR-NEXT: store i32 2, ptr [[TMP0]], align 4
1322-
// NO-SANITIZE-WITH-ATTR-NEXT: [[BLAH:%.*]] = getelementptr inbounds i8, ptr [[FOO]], i64 8
13231317
// NO-SANITIZE-WITH-ATTR-NEXT: [[IDXPROM:%.*]] = sext i32 [[IDX]] to i64
1324-
// NO-SANITIZE-WITH-ATTR-NEXT: [[ARRAYIDX:%.*]] = getelementptr inbounds [0 x i32], ptr [[BLAH]], i64 0, i64 [[IDXPROM]]
1325-
// NO-SANITIZE-WITH-ATTR-NEXT: [[TMP1:%.*]] = load i32, ptr [[ARRAYIDX]], align 4, !tbaa [[TBAA2]]
1326-
// NO-SANITIZE-WITH-ATTR-NEXT: call void @llvm.lifetime.end.p0(i64 8, ptr nonnull [[FOO]]) #[[ATTR12]]
1327-
// NO-SANITIZE-WITH-ATTR-NEXT: ret i32 [[TMP1]]
1318+
// NO-SANITIZE-WITH-ATTR-NEXT: [[ARRAYIDX:%.*]] = getelementptr inbounds [0 x i32], ptr getelementptr inbounds ([[STRUCT_ANON_8:%.*]], ptr @__const.test15.foo, i64 1, i32 0), i64 0, i64 [[IDXPROM]]
1319+
// NO-SANITIZE-WITH-ATTR-NEXT: [[TMP0:%.*]] = load i32, ptr [[ARRAYIDX]], align 4, !tbaa [[TBAA2]]
1320+
// NO-SANITIZE-WITH-ATTR-NEXT: ret i32 [[TMP0]]
13281321
//
13291322
// SANITIZE-WITHOUT-ATTR-LABEL: define dso_local i32 @test15(
13301323
// SANITIZE-WITHOUT-ATTR-SAME: i32 noundef [[IDX:%.*]]) local_unnamed_addr #[[ATTR0]] {
@@ -1342,17 +1335,10 @@ int test14(int idx) {
13421335
// NO-SANITIZE-WITHOUT-ATTR-LABEL: define dso_local i32 @test15(
13431336
// NO-SANITIZE-WITHOUT-ATTR-SAME: i32 noundef [[IDX:%.*]]) local_unnamed_addr #[[ATTR1]] {
13441337
// NO-SANITIZE-WITHOUT-ATTR-NEXT: entry:
1345-
// NO-SANITIZE-WITHOUT-ATTR-NEXT: [[FOO:%.*]] = alloca [[STRUCT_ANON_8:%.*]], align 4
1346-
// NO-SANITIZE-WITHOUT-ATTR-NEXT: call void @llvm.lifetime.start.p0(i64 8, ptr nonnull [[FOO]]) #[[ATTR9]]
1347-
// NO-SANITIZE-WITHOUT-ATTR-NEXT: store i32 1, ptr [[FOO]], align 4
1348-
// NO-SANITIZE-WITHOUT-ATTR-NEXT: [[TMP0:%.*]] = getelementptr inbounds i8, ptr [[FOO]], i64 4
1349-
// NO-SANITIZE-WITHOUT-ATTR-NEXT: store i32 2, ptr [[TMP0]], align 4
1350-
// NO-SANITIZE-WITHOUT-ATTR-NEXT: [[BLAH:%.*]] = getelementptr inbounds i8, ptr [[FOO]], i64 8
13511338
// NO-SANITIZE-WITHOUT-ATTR-NEXT: [[IDXPROM:%.*]] = sext i32 [[IDX]] to i64
1352-
// NO-SANITIZE-WITHOUT-ATTR-NEXT: [[ARRAYIDX:%.*]] = getelementptr inbounds [0 x i32], ptr [[BLAH]], i64 0, i64 [[IDXPROM]]
1353-
// NO-SANITIZE-WITHOUT-ATTR-NEXT: [[TMP1:%.*]] = load i32, ptr [[ARRAYIDX]], align 4, !tbaa [[TBAA2]]
1354-
// NO-SANITIZE-WITHOUT-ATTR-NEXT: call void @llvm.lifetime.end.p0(i64 8, ptr nonnull [[FOO]]) #[[ATTR9]]
1355-
// NO-SANITIZE-WITHOUT-ATTR-NEXT: ret i32 [[TMP1]]
1339+
// NO-SANITIZE-WITHOUT-ATTR-NEXT: [[ARRAYIDX:%.*]] = getelementptr inbounds [0 x i32], ptr getelementptr inbounds ([[STRUCT_ANON_8:%.*]], ptr @__const.test15.foo, i64 1, i32 0), i64 0, i64 [[IDXPROM]]
1340+
// NO-SANITIZE-WITHOUT-ATTR-NEXT: [[TMP0:%.*]] = load i32, ptr [[ARRAYIDX]], align 4, !tbaa [[TBAA2]]
1341+
// NO-SANITIZE-WITHOUT-ATTR-NEXT: ret i32 [[TMP0]]
13561342
//
13571343
int test15(int idx) {
13581344
struct {

clang/test/CodeGenCXX/auto-var-init.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1504,9 +1504,7 @@ TEST_CUSTOM(unmatchedreverse, unmatchedreverse, { .c = 42 });
15041504
// PATTERN-O1-NEXT: store i8 -86, ptr %[[I]], align {{.*}}
15051505
// PATTERN-O1-NEXT: %[[I:[^ ]*]] = getelementptr inbounds i8, ptr %custom, i64 3
15061506
// PATTERN-O1-NEXT: store i8 -86, ptr %[[I]], align {{.*}}
1507-
// ZERO-O1: store i8 42, ptr {{.*}}, align 4
1508-
// ZERO-O1-NEXT: %[[I:[^ ]*]] = getelementptr inbounds i8, ptr %custom, i64 1
1509-
// ZERO-O1-NEXT: call void @llvm.memset.{{.*}}({{.*}}, i8 0, i64 3, {{.*}})
1507+
// ZERO-O1: store i32 42, ptr {{.*}}, align 4
15101508

15111509
TEST_UNINIT(unmatchedfp, unmatchedfp);
15121510
// CHECK-LABEL: @test_unmatchedfp_uninit()
@@ -1531,7 +1529,7 @@ TEST_CUSTOM(unmatchedfp, unmatchedfp, { .d = 3.1415926535897932384626433 });
15311529
// CHECK-O0: call void @llvm.memcpy
15321530
// CHECK-NOT: !annotation
15331531
// CHECK-O0: call void @{{.*}}used{{.*}}%custom)
1534-
// CHECK-O1: store double 0x400921FB54442D18, ptr %custom, align 8
1532+
// CHECK-O1: store i64 4614256656552045848, ptr %custom, align 8
15351533
// CHECK-NOT: !annotation
15361534

15371535
TEST_UNINIT(emptyenum, emptyenum);

clang/test/CodeGenOpenCL/amdgpu-printf.cl

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,7 @@ __kernel void test_printf_int(int i) {
3030
// CHECK-NEXT: [[S:%.*]] = alloca [4 x i8], align 1, addrspace(5)
3131
// CHECK-NEXT: store i32 [[I:%.*]], ptr addrspace(5) [[I_ADDR]], align 4, !tbaa [[TBAA8]]
3232
// CHECK-NEXT: call void @llvm.lifetime.start.p5(i64 4, ptr addrspace(5) [[S]]) #[[ATTR5:[0-9]+]]
33-
// CHECK-NEXT: [[LOC0:%.*]] = getelementptr i8, ptr addrspace(5) [[S]], i64 0
34-
// CHECK-NEXT: store i8 102, ptr addrspace(5) [[LOC0]], align 1
35-
// CHECK-NEXT: [[LOC1:%.*]] = getelementptr i8, ptr addrspace(5) [[S]], i64 1
36-
// CHECK-NEXT: store i8 111, ptr addrspace(5) [[LOC1]], align 1
37-
// CHECK-NEXT: [[LOC2:%.*]] = getelementptr i8, ptr addrspace(5) [[S]], i64 2
38-
// CHECK-NEXT: store i8 111, ptr addrspace(5) [[LOC2]], align 1
39-
// CHECK-NEXT: [[LOC3:%.*]] = getelementptr i8, ptr addrspace(5) [[S]], i64 3
40-
// CHECK-NEXT: store i8 0, ptr addrspace(5) [[LOC3]], align 1
33+
// CHECK-NEXT: call void @llvm.memcpy.p5.p4.i64(ptr addrspace(5) align 1 [[S]], ptr addrspace(4) align 1 @__const.test_printf_str_int.s, i64 4, i1 false)
4134
// CHECK-NEXT: [[ARRAYDECAY:%.*]] = getelementptr inbounds [4 x i8], ptr addrspace(5) [[S]], i64 0, i64 0
4235
// CHECK-NEXT: [[TMP2:%.*]] = load i32, ptr addrspace(5) [[I_ADDR]], align 4, !tbaa [[TBAA8]]
4336
// CHECK-NEXT: [[CALL:%.*]] = call i32 (ptr addrspace(4), ...) @printf(ptr addrspace(4) noundef @.str.2, ptr addrspace(5) noundef [[ARRAYDECAY]], i32 noundef [[TMP2]]) #[[ATTR4]]

clang/test/OpenMP/bug54082.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,7 @@ void foo() {
6969
// CHECK-NEXT: [[X_TRAITS:%.*]] = alloca [1 x %struct.omp_alloctrait_t], align 16
7070
// CHECK-NEXT: [[X_ALLOC:%.*]] = alloca i64, align 8
7171
// CHECK-NEXT: call void @llvm.lifetime.start.p0(i64 16, ptr nonnull [[X_TRAITS]]) #[[ATTR5:[0-9]+]]
72-
// CHECK-NEXT: store i32 2, ptr [[X_TRAITS]], align 16
73-
// CHECK-NEXT: [[LOC0:%.*]] = getelementptr inbounds i8, ptr [[X_TRAITS]], i64 8
74-
// CHECK-NEXT: store i64 64, ptr [[LOC0]], align 8
72+
// CHECK-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 16 dereferenceable(16) [[X_TRAITS]], ptr noundef nonnull align 16 dereferenceable(16) @__const.foo.x_traits, i64 16, i1 false)
7573
// CHECK-NEXT: call void @llvm.lifetime.start.p0(i64 8, ptr nonnull [[X_ALLOC]]) #[[ATTR5]]
7674
// CHECK-NEXT: [[CALL:%.*]] = call i64 @omp_init_allocator(i64 noundef 0, i32 noundef 1, ptr noundef nonnull [[X_TRAITS]]) #[[ATTR5]]
7775
// CHECK-NEXT: store i64 [[CALL]], ptr [[X_ALLOC]], align 8, !tbaa [[TBAA3:![0-9]+]]

0 commit comments

Comments
 (0)