summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJF Bastien <jfbastien@apple.com>2019-04-30 22:56:53 +0000
committerJF Bastien <jfbastien@apple.com>2019-04-30 22:56:53 +0000
commit11659cf432decd2e9de8e2a88faf4bff614ca95d (patch)
tree2e6c909c246f7be8efe4cefd0f927b1699564c07
parent99dc6c40f040f036121b39cf02431c3166779ad7 (diff)
Variable auto-init: don't initialize aggregate padding of all aggregates
Summary: C guarantees that brace-init with fewer initializers than members in the aggregate will initialize the rest of the aggregate as-if it were static initialization. In turn static initialization guarantees that padding is initialized to zero bits. Quoth the Standard: C17 6.7.9 Initialization ❡21 If there are fewer initializers in a brace-enclosed list than there are elements or members of an aggregate, or fewer characters in a string literal used to initialize an array of known size than there are elements in the array, the remainder of the aggregate shall be initialized implicitly the same as objects that have static storage duration. C17 6.7.9 Initialization ❡10 If an object that has automatic storage duration is not initialized explicitly, its value is indeterminate. If an object that has static or thread storage duration is not initialized explicitly, then: * if it has pointer type, it is initialized to a null pointer; * if it has arithmetic type, it is initialized to (positive or unsigned) zero; * if it is an aggregate, every member is initialized (recursively) according to these rules, and any padding is initialized to zero bits; * if it is a union, the first named member is initialized (recursively) according to these rules, and any padding is initialized to zero bits; <rdar://problem/50188861> Reviewers: glider, pcc, kcc, rjmccall, erik.pilkington Subscribers: jkorous, dexonsmith, cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D61280 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@359628 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r--lib/CodeGen/CGDecl.cpp13
-rw-r--r--test/CodeGen/padding-init.c51
2 files changed, 61 insertions, 3 deletions
diff --git a/lib/CodeGen/CGDecl.cpp b/lib/CodeGen/CGDecl.cpp
index a5c02246d0..61f9de9a29 100644
--- a/lib/CodeGen/CGDecl.cpp
+++ b/lib/CodeGen/CGDecl.cpp
@@ -1787,13 +1787,20 @@ void CodeGenFunction::EmitAutoVarInit(const AutoVarEmission &emission) {
D.isUsableInConstantExpressions(getContext())) {
assert(!capturedByInit && "constant init contains a capturing block?");
constant = ConstantEmitter(*this).tryEmitAbstractForInitializer(D);
- if (constant && trivialAutoVarInit !=
- LangOptions::TrivialAutoVarInitKind::Uninitialized) {
+ if (constant && !constant->isZeroValue() &&
+ (trivialAutoVarInit !=
+ LangOptions::TrivialAutoVarInitKind::Uninitialized)) {
IsPattern isPattern =
(trivialAutoVarInit == LangOptions::TrivialAutoVarInitKind::Pattern)
? IsPattern::Yes
: IsPattern::No;
- constant = constWithPadding(CGM, isPattern,
+ // C guarantees that brace-init with fewer initializers than members in
+ // the aggregate will initialize the rest of the aggregate as-if it were
+ // static initialization. In turn static initialization guarantees that
+ // padding is initialized to zero bits. We could instead pattern-init if D
+ // has any ImplicitValueInitExpr, but that seems to be unintuitive
+ // behavior.
+ constant = constWithPadding(CGM, IsPattern::No,
replaceUndef(CGM, isPattern, constant));
}
}
diff --git a/test/CodeGen/padding-init.c b/test/CodeGen/padding-init.c
new file mode 100644
index 0000000000..0ff11efd64
--- /dev/null
+++ b/test/CodeGen/padding-init.c
@@ -0,0 +1,51 @@
+// RUN: %clang_cc1 -triple x86_64-unknown-unknown -ftrivial-auto-var-init=pattern %s -emit-llvm -o - | FileCheck %s
+// RUN: %clang_cc1 -triple x86_64-unknown-unknown -ftrivial-auto-var-init=zero %s -emit-llvm -o - | FileCheck %s
+
+// C guarantees that brace-init with fewer initializers than members in the
+// aggregate will initialize the rest of the aggregate as-if it were static
+// initialization. In turn static initialization guarantees that padding is
+// initialized to zero bits.
+
+// CHECK: @__const.partial_init.s = private unnamed_addr constant { i8, [7 x i8], i64 } { i8 42, [7 x i8] zeroinitializer, i64 0 }, align 8
+
+// Technically, we could initialize this padding to non-zero because all of the
+// struct's members have initializers.
+
+// CHECK: @__const.init_all.s = private unnamed_addr constant { i8, [7 x i8], i64 } { i8 42, [7 x i8] zeroinitializer, i64 -2401053089374216531 }, align 8
+
+struct S {
+ char c;
+ long long l;
+};
+
+void use(struct S*);
+
+// CHECK-LABEL: @empty_braces(
+// CHECK: %s = alloca
+// CHECK-NEXT: %[[B:[0-9+]]] = bitcast %struct.S* %s to i8*
+// CHECK-NEXT: call void @llvm.memset{{.*}}(i8* align 8 %[[B]], i8 0,
+// CHECK-NEXT: call void @use(%struct.S* %s)
+void empty_braces() {
+ struct S s = {};
+ return use(&s);
+}
+
+// CHECK-LABEL: @partial_init(
+// CHECK: %s = alloca
+// CHECK-NEXT: %[[B:[0-9+]]] = bitcast %struct.S* %s to i8*
+// CHECK-NEXT: call void @llvm.memcpy{{.*}}(i8* align 8 %[[B]], {{.*}}@__const.partial_init.s
+// CHECK-NEXT: call void @use(%struct.S* %s)
+void partial_init() {
+ struct S s = { .c = 42 };
+ return use(&s);
+}
+
+// CHECK-LABEL: @init_all(
+// CHECK: %s = alloca
+// CHECK-NEXT: %[[B:[0-9+]]] = bitcast %struct.S* %s to i8*
+// CHECK-NEXT: call void @llvm.memcpy{{.*}}(i8* align 8 %[[B]], {{.*}}@__const.init_all.s
+// CHECK-NEXT: call void @use(%struct.S* %s)
+void init_all() {
+ struct S s = { .c = 42, .l = 0xdeadbeefc0fedead };
+ return use(&s);
+}