Skip to content

Commit fcdfdbf

Browse files
[clang] Avoid memcopy for small arrays with padding under -ftrivial-auto-var-init
1 parent d5934a4 commit fcdfdbf

File tree

2 files changed

+13
-15
lines changed

2 files changed

+13
-15
lines changed

clang/lib/CodeGen/CGDecl.cpp

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1257,17 +1257,15 @@ static void emitStoresForConstant(CodeGenModule &CGM, const VarDecl &D,
12571257
}
12581258
return;
12591259
} else if (auto *ATy = dyn_cast<llvm::ArrayType>(Ty)) {
1260-
// FIXME: handle the case when ATy != Loc.getElementType().
1261-
if (ATy == Loc.getElementType()) {
1262-
for (unsigned i = 0; i != ATy->getNumElements(); i++) {
1263-
Address EltPtr = Builder.CreateConstArrayGEP(Loc, i);
1264-
emitStoresForConstant(
1265-
CGM, D, EltPtr, isVolatile, Builder,
1266-
cast<llvm::Constant>(Builder.CreateExtractValue(constant, i)),
1267-
IsAutoInit);
1268-
}
1269-
return;
1260+
for (unsigned i = 0; i != ATy->getNumElements(); i++) {
1261+
Address EltPtr = Builder.CreateConstGEP(
1262+
Loc.withElementType(ATy->getElementType()), i);
1263+
emitStoresForConstant(
1264+
CGM, D, EltPtr, isVolatile, Builder,
1265+
cast<llvm::Constant>(Builder.CreateExtractValue(constant, i)),
1266+
IsAutoInit);
12701267
}
1268+
return;
12711269
}
12721270
}
12731271

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -134,12 +134,8 @@ struct arraytail { int i; int arr[]; };
134134
// PATTERN-O1-NOT: @__const.test_bool4_custom.custom
135135
// ZERO-O1-NOT: @__const.test_bool4_custom.custom
136136

137-
// PATTERN: @__const.test_intptr4_custom.custom = private unnamed_addr constant [4 x ptr] [ptr inttoptr ([[IPTRT]] 572662306 to ptr), ptr inttoptr ([[IPTRT]] 572662306 to ptr), ptr inttoptr ([[IPTRT]] 572662306 to ptr), ptr inttoptr ([[IPTRT]] 572662306 to ptr)], align
138-
// ZERO: @__const.test_intptr4_custom.custom = private unnamed_addr constant [4 x ptr] [ptr inttoptr (i64 572662306 to ptr), ptr inttoptr (i64 572662306 to ptr), ptr inttoptr (i64 572662306 to ptr), ptr inttoptr (i64 572662306 to ptr)], align 16
139137
// PATTERN-O0: @__const.test_tailpad4_uninit.uninit = private unnamed_addr constant [4 x { i16, i8, [1 x i8] }] [{ i16, i8, [1 x i8] } { i16 [[I16]], i8 [[I8]], [1 x i8] c"\[[IC]]" }, { i16, i8, [1 x i8] } { i16 [[I16]], i8 [[I8]], [1 x i8] c"\[[IC]]" }, { i16, i8, [1 x i8] } { i16 [[I16]], i8 [[I8]], [1 x i8] c"\[[IC]]" }, { i16, i8, [1 x i8] } { i16 [[I16]], i8 [[I8]], [1 x i8] c"\[[IC]]" }], align
140138
// PATTERN-O1-NOT: @__const.test_tailpad4_uninit.uninit
141-
// PATTERN: @__const.test_tailpad4_custom.custom = private unnamed_addr constant [4 x { i16, i8, [1 x i8] }] [{ i16, i8, [1 x i8] } { i16 257, i8 1, [1 x i8] zeroinitializer }, { i16, i8, [1 x i8] } { i16 257, i8 1, [1 x i8] zeroinitializer }, { i16, i8, [1 x i8] } { i16 257, i8 1, [1 x i8] zeroinitializer }, { i16, i8, [1 x i8] } { i16 257, i8 1, [1 x i8] zeroinitializer }], align
142-
// ZERO: @__const.test_tailpad4_custom.custom = private unnamed_addr constant [4 x { i16, i8, [1 x i8] }] [{ i16, i8, [1 x i8] } { i16 257, i8 1, [1 x i8] zeroinitializer }, { i16, i8, [1 x i8] } { i16 257, i8 1, [1 x i8] zeroinitializer }, { i16, i8, [1 x i8] } { i16 257, i8 1, [1 x i8] zeroinitializer }, { i16, i8, [1 x i8] } { i16 257, i8 1, [1 x i8] zeroinitializer }], align 16
143139
struct tailpad { short s; char c; };
144140
// PATTERN-O0: @__const.test_atomicnotlockfree_uninit.uninit = private unnamed_addr constant %struct.notlockfree { [4 x i64] {{\[}}i64 [[I64]], i64 [[I64]], i64 [[I64]], i64 [[I64]]] }, align
145141
// PATTERN-O1-NOT: @__const.test_atomicnotlockfree_uninit.uninit
@@ -1503,7 +1499,11 @@ TEST_CUSTOM(unmatchedreverse, unmatchedreverse, { .c = 42 });
15031499
// CHECK-O0: call void @{{.*}}used{{.*}}%custom)
15041500
// PATTERN-O1: store i8 42, ptr {{.*}}, align 4
15051501
// PATTERN-O1-NEXT: %[[I:[^ ]*]] = getelementptr inbounds i8, ptr %custom, i64 1
1506-
// PATTERN-O1-NEXT: call void @llvm.memset.{{.*}}({{.*}}, i8 -86, i64 3, {{.*}})
1502+
// PATTERN-O1-NEXT: store i8 -86, ptr %[[I]], align {{.*}}
1503+
// PATTERN-O1-NEXT: %[[I:[^ ]*]] = getelementptr inbounds i8, ptr %custom, i64 2
1504+
// PATTERN-O1-NEXT: store i8 -86, ptr %[[I]], align {{.*}}
1505+
// PATTERN-O1-NEXT: %[[I:[^ ]*]] = getelementptr inbounds i8, ptr %custom, i64 3
1506+
// PATTERN-O1-NEXT: store i8 -86, ptr %[[I]], align {{.*}}
15071507
// ZERO-O1: store i8 42, ptr {{.*}}, align 4
15081508
// ZERO-O1-NEXT: %[[I:[^ ]*]] = getelementptr inbounds i8, ptr %custom, i64 1
15091509
// ZERO-O1-NEXT: call void @llvm.memset.{{.*}}({{.*}}, i8 0, i64 3, {{.*}})

0 commit comments

Comments
 (0)