From 82e1af20cb6393345bf53259465d63743d9b60db Mon Sep 17 00:00:00 2001 From: Douglas Gregor Date: Fri, 1 Jul 2011 21:08:19 +0000 Subject: [PATCH] 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 . llvm-svn: 134260 --- clang/lib/CodeGen/CGDecl.cpp | 19 ++++++++++++++++++- clang/test/CodeGenCXX/constructor-init.cpp | 16 ++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/clang/lib/CodeGen/CGDecl.cpp b/clang/lib/CodeGen/CGDecl.cpp index d508ff7390bb..95294bf8e583 100644 --- a/clang/lib/CodeGen/CGDecl.cpp +++ b/clang/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(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/clang/test/CodeGenCXX/constructor-init.cpp b/clang/test/CodeGenCXX/constructor-init.cpp index 47e3b7b0bb90..f439083c0778 100644 --- a/clang/test/CodeGenCXX/constructor-init.cpp +++ b/clang/test/CodeGenCXX/constructor-init.cpp @@ -133,3 +133,19 @@ template X::X(const X &other) : start(0), end(0) { } X get_X(X 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 + } +}