From 04491bd8f324bce9d2adbc9cc6e45f0d1659ac41 Mon Sep 17 00:00:00 2001 From: Gor Nishanov Date: Sat, 11 Nov 2017 17:00:43 +0000 Subject: [coroutines] Promote cleanup.dest.slot allocas to registers to avoid storing it in the coroutine frame Summary: We don't want to store cleanup dest slot saved into the coroutine frame (as some of the cleanup code may access them after coroutine frame destroyed). This is an alternative to https://reviews.llvm.org/D37093 It is possible to do this for all functions, but, cursory check showed that in -O0, we get slightly longer function (by 1-3 instructions), thus, we are only limiting cleanup.dest.slot elimination to coroutines. Reviewers: rjmccall, hfinkel, eric_niebler Reviewed By: eric_niebler Subscribers: EricWF, cfe-commits Differential Revision: https://reviews.llvm.org/D39768 llvm-svn: 317981 --- clang/lib/CodeGen/CodeGenFunction.cpp | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'clang/lib/CodeGen/CodeGenFunction.cpp') diff --git a/clang/lib/CodeGen/CodeGenFunction.cpp b/clang/lib/CodeGen/CodeGenFunction.cpp index 3b60b996ca19..37d174dd6635 100644 --- a/clang/lib/CodeGen/CodeGenFunction.cpp +++ b/clang/lib/CodeGen/CodeGenFunction.cpp @@ -33,9 +33,11 @@ #include "clang/Frontend/CodeGenOptions.h" #include "clang/Sema/SemaDiagnostic.h" #include "llvm/IR/DataLayout.h" +#include "llvm/IR/Dominators.h" #include "llvm/IR/Intrinsics.h" #include "llvm/IR/MDBuilder.h" #include "llvm/IR/Operator.h" +#include "llvm/Transforms/Utils/PromoteMemToReg.h" using namespace clang; using namespace CodeGen; @@ -419,6 +421,18 @@ void CodeGenFunction::FinishFunction(SourceLocation EndLoc) { I->first->replaceAllUsesWith(I->second); I->first->eraseFromParent(); } + + // Eliminate CleanupDestSlot alloca by replacing it with SSA values and + // PHIs if the current function is a coroutine. We don't do it for all + // functions as it may result in slight increase in numbers of instructions + // if compiled with no optimizations. We do it for coroutine as the lifetime + // of CleanupDestSlot alloca make correct coroutine frame building very + // difficult. + if (NormalCleanupDest && isCoroutine()) { + llvm::DominatorTree DT(*CurFn); + llvm::PromoteMemToReg(NormalCleanupDest, DT); + NormalCleanupDest = nullptr; + } } /// ShouldInstrumentFunction - Return true if the current function should be -- cgit v1.2.3