From 80fe8a6c2142df4718e2482a0562ee6142de1685 Mon Sep 17 00:00:00 2001 From: Richard Smith Date: Tue, 3 Jun 2014 08:26:00 +0000 Subject: Implement DR990 and DR1070. Aggregate initialization initializes uninitialized elements from {}, rather than value-initializing them. This permits calling an initializer-list constructor or constructing a std::initializer_list object. (It would also permit initializing a const reference or rvalue reference if that weren't explicitly prohibited by other rules.) git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@210091 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/CodeGen/CGExprCXX.cpp | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) (limited to 'lib/CodeGen/CGExprCXX.cpp') diff --git a/lib/CodeGen/CGExprCXX.cpp b/lib/CodeGen/CGExprCXX.cpp index c6995d8a1b..762d8e2959 100644 --- a/lib/CodeGen/CGExprCXX.cpp +++ b/lib/CodeGen/CGExprCXX.cpp @@ -858,9 +858,21 @@ CodeGenFunction::EmitNewArrayInitializer(const CXXNewExpr *E, return true; }; + // If all elements have already been initialized, skip any further + // initialization. + llvm::ConstantInt *ConstNum = dyn_cast(NumElements); + if (ConstNum && ConstNum->getZExtValue() <= InitListElements) { + // If there was a Cleanup, deactivate it. + if (CleanupDominator) + DeactivateCleanupBlock(Cleanup, CleanupDominator); + return; + } + + assert(Init && "have trailing elements to initialize but no initializer"); + // If this is a constructor call, try to optimize it out, and failing that // emit a single loop to initialize all remaining elements. - if (const CXXConstructExpr *CCE = dyn_cast_or_null(Init)){ + if (const CXXConstructExpr *CCE = dyn_cast(Init)) { CXXConstructorDecl *Ctor = CCE->getConstructor(); if (Ctor->isTrivial()) { // If new expression did not specify value-initialization, then there @@ -891,7 +903,7 @@ CodeGenFunction::EmitNewArrayInitializer(const CXXNewExpr *E, // If this is value-initialization, we can usually use memset. ImplicitValueInitExpr IVIE(ElementType); - if (Init && isa(Init)) { + if (isa(Init)) { if (TryMemsetInitialization()) return; @@ -906,15 +918,10 @@ CodeGenFunction::EmitNewArrayInitializer(const CXXNewExpr *E, assert(getContext().hasSameUnqualifiedType(ElementType, Init->getType()) && "got wrong type of element to initialize"); - llvm::ConstantInt *ConstNum = dyn_cast(NumElements); - - // If all elements have already been initialized, skip the whole loop. - if (ConstNum && ConstNum->getZExtValue() <= InitListElements) { - // If there was a Cleanup, deactivate it. - if (CleanupDominator) - DeactivateCleanupBlock(Cleanup, CleanupDominator); - return; - } + // If we have an empty initializer list, we can usually use memset. + if (auto *ILE = dyn_cast(Init)) + if (ILE->getNumInits() == 0 && TryMemsetInitialization()) + return; // Create the loop blocks. llvm::BasicBlock *EntryBB = Builder.GetInsertBlock(); -- cgit v1.2.3