From c5792aa90fa45a1842f190c146f19e2c71ea6fbd Mon Sep 17 00:00:00 2001 From: Akira Hatanaka Date: Wed, 27 Feb 2019 18:17:16 +0000 Subject: Avoid needlessly copying a block to the heap when a block literal initializes a local auto variable or is assigned to a local auto variable that is declared in the scope that introduced the block literal. rdar://problem/13289333 https://reviews.llvm.org/D58514 llvm-svn: 355012 --- clang/lib/CodeGen/CGObjC.cpp | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) (limited to 'clang/lib/CodeGen/CGObjC.cpp') diff --git a/clang/lib/CodeGen/CGObjC.cpp b/clang/lib/CodeGen/CGObjC.cpp index fba1a531a2a..7063517efc3 100644 --- a/clang/lib/CodeGen/CGObjC.cpp +++ b/clang/lib/CodeGen/CGObjC.cpp @@ -2871,6 +2871,7 @@ public: Result visit(const Expr *e); Result visitCastExpr(const CastExpr *e); Result visitPseudoObjectExpr(const PseudoObjectExpr *e); + Result visitBlockExpr(const BlockExpr *e); Result visitBinaryOperator(const BinaryOperator *e); Result visitBinAssign(const BinaryOperator *e); Result visitBinAssignUnsafeUnretained(const BinaryOperator *e); @@ -2946,6 +2947,12 @@ ARCExprEmitter::visitPseudoObjectExpr(const PseudoObjectExpr *E) { return result; } +template +Result ARCExprEmitter::visitBlockExpr(const BlockExpr *e) { + // The default implementation just forwards the expression to visitExpr. + return asImpl().visitExpr(e); +} + template Result ARCExprEmitter::visitCastExpr(const CastExpr *e) { switch (e->getCastKind()) { @@ -3089,7 +3096,8 @@ Result ARCExprEmitter::visit(const Expr *e) { // Look through pseudo-object expressions. } else if (const PseudoObjectExpr *pseudo = dyn_cast(e)) { return asImpl().visitPseudoObjectExpr(pseudo); - } + } else if (auto *be = dyn_cast(e)) + return asImpl().visitBlockExpr(be); return asImpl().visitExpr(e); } @@ -3124,6 +3132,15 @@ struct ARCRetainExprEmitter : return TryEmitResult(result, true); } + TryEmitResult visitBlockExpr(const BlockExpr *e) { + TryEmitResult result = visitExpr(e); + // Avoid the block-retain if this is a block literal that doesn't need to be + // copied to the heap. + if (e->getBlockDecl()->canAvoidCopyToHeap()) + result.setInt(true); + return result; + } + /// Block extends are net +0. Naively, we could just recurse on /// the subexpression, but actually we need to ensure that the /// value is copied as a block, so there's a little filter here. -- cgit v1.2.3