From 17a83cf4b6dbba6ab6e96608ecf77c8c87019f3f Mon Sep 17 00:00:00 2001 From: "Arnaud A. de Grandmaison" Date: Mon, 21 Jul 2014 18:54:21 +0000 Subject: Emit lifetime.start / lifetime.end markers for unnamed temporary objects. This will give more information to the optimizers so that they can reuse stack slots. llvm-svn: 213576 --- clang/lib/CodeGen/CGExpr.cpp | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) (limited to 'clang/lib/CodeGen/CGExpr.cpp') diff --git a/clang/lib/CodeGen/CGExpr.cpp b/clang/lib/CodeGen/CGExpr.cpp index 512b323ba10..2c9c2f130e8 100644 --- a/clang/lib/CodeGen/CGExpr.cpp +++ b/clang/lib/CodeGen/CGExpr.cpp @@ -353,6 +353,17 @@ LValue CodeGenFunction::EmitMaterializeTemporaryExpr( // Create and initialize the reference temporary. llvm::Value *Object = createReferenceTemporary(*this, M, E); + + uint64_t size = + CGM.getDataLayout().getTypeStoreSize(ConvertTypeForMem(E->getType())); + llvm::Value *sizeV = nullptr; + llvm::AllocaInst *Alloca = dyn_cast(Object); + bool useLifetimeMarkers = Alloca && shouldUseLifetimeMarkers(size); + if (useLifetimeMarkers) { + sizeV = llvm::ConstantInt::get(Int64Ty, size); + EmitLifetimeStart(sizeV, Object); + } + if (auto *Var = dyn_cast(Object)) { // If the temporary is a global and has a constant initializer, we may // have already initialized it. @@ -363,6 +374,20 @@ LValue CodeGenFunction::EmitMaterializeTemporaryExpr( } else { EmitAnyExprToMem(E, Object, Qualifiers(), /*IsInit*/true); } + + if (useLifetimeMarkers) + switch (M->getStorageDuration()) { + case SD_FullExpression: + EHStack.pushCleanup(NormalAndEHCleanup, Object, sizeV); + break; + case SD_Automatic: + pushCleanupAfterFullExpr(NormalAndEHCleanup, Object, + sizeV); + break; + default: + llvm_unreachable("unexpected storage duration for Lifetime markers"); + } + pushTemporaryCleanup(*this, M, E, Object); // Perform derived-to-base casts and/or field accesses, to get from the -- cgit v1.2.3