aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDouglas Gregor <dgregor@apple.com>2011-07-01 21:08:19 +0000
committerDouglas Gregor <dgregor@apple.com>2011-07-01 21:08:19 +0000
commitbcc3e660a1fdc19722157ef3e2f133418856ca3d (patch)
tree06c41ab25f97aa8ce03de107727768041fc7f6c4
parent31fd2b7881efc6b7b1e466823c10c64ba5ddffe3 (diff)
Don't zero-initialize default-initialized local variables that have
trivial default constructors. This generated-code regression was caused by r131796, which had simplified the handling of default initialization in Sema. Fixes <rdar://problem/9694300>. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@134260 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r--lib/CodeGen/CGDecl.cpp19
-rw-r--r--test/CodeGenCXX/constructor-init.cpp16
2 files changed, 34 insertions, 1 deletions
diff --git a/lib/CodeGen/CGDecl.cpp b/lib/CodeGen/CGDecl.cpp
index d508ff7390..95294bf8e5 100644
--- a/lib/CodeGen/CGDecl.cpp
+++ b/lib/CodeGen/CGDecl.cpp
@@ -873,6 +873,21 @@ static bool isCapturedBy(const VarDecl &var, const Expr *e) {
return false;
}
+/// \brief Determine whether the given initializer is trivial in the sense
+/// that it requires no code to be generated.
+static bool isTrivialInitializer(const Expr *Init) {
+ if (!Init)
+ return true;
+
+ if (const CXXConstructExpr *Construct = dyn_cast<CXXConstructExpr>(Init))
+ if (CXXConstructorDecl *Constructor = Construct->getConstructor())
+ if (Constructor->isTrivial() &&
+ Constructor->isDefaultConstructor() &&
+ !Construct->requiresZeroInitialization())
+ return true;
+
+ return false;
+}
void CodeGenFunction::EmitAutoVarInit(const AutoVarEmission &emission) {
assert(emission.Variable && "emission was not valid!");
@@ -896,7 +911,9 @@ void CodeGenFunction::EmitAutoVarInit(const AutoVarEmission &emission) {
if (emission.IsByRef)
emitByrefStructureInit(emission);
- if (!Init) return;
+ if (isTrivialInitializer(Init))
+ return;
+
CharUnits alignment = emission.Alignment;
diff --git a/test/CodeGenCXX/constructor-init.cpp b/test/CodeGenCXX/constructor-init.cpp
index 47e3b7b0bb..f439083c07 100644
--- a/test/CodeGenCXX/constructor-init.cpp
+++ b/test/CodeGenCXX/constructor-init.cpp
@@ -133,3 +133,19 @@ template<typename T>
X<T>::X(const X &other) : start(0), end(0) { }
X<int> get_X(X<int> x) { return x; }
+
+namespace rdar9694300 {
+ struct X {
+ int x;
+ };
+
+ // CHECK: define void @_ZN11rdar96943001fEv
+ void f() {
+ // CHECK: alloca
+ X x;
+ // CHECK-NEXT: [[I:%.*]] = alloca i32
+ // CHECK-NEXT: store i32 17, i32* [[I]]
+ int i = 17;
+ // CHECK-NEXT: ret void
+ }
+}