From 8b36ac818cd58751e7327b093eda746ea4355ca5 Mon Sep 17 00:00:00 2001 From: John McCall Date: Wed, 10 Apr 2019 17:03:09 +0000 Subject: Don't emit an unreachable return block. Patch by Brad Moody. llvm-svn: 358104 --- clang/lib/CodeGen/CodeGenFunction.cpp | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'clang/lib/CodeGen/CodeGenFunction.cpp') diff --git a/clang/lib/CodeGen/CodeGenFunction.cpp b/clang/lib/CodeGen/CodeGenFunction.cpp index 5b2d043a0a4..7a8d79ba3b5 100644 --- a/clang/lib/CodeGen/CodeGenFunction.cpp +++ b/clang/lib/CodeGen/CodeGenFunction.cpp @@ -255,6 +255,7 @@ llvm::DebugLoc CodeGenFunction::EmitReturnBlock() { if (CurBB->empty() || ReturnBlock.getBlock()->use_empty()) { ReturnBlock.getBlock()->replaceAllUsesWith(CurBB); delete ReturnBlock.getBlock(); + ReturnBlock = JumpDest(); } else EmitBlock(ReturnBlock.getBlock()); return llvm::DebugLoc(); @@ -274,6 +275,7 @@ llvm::DebugLoc CodeGenFunction::EmitReturnBlock() { Builder.SetInsertPoint(BI->getParent()); BI->eraseFromParent(); delete ReturnBlock.getBlock(); + ReturnBlock = JumpDest(); return Loc; } } @@ -448,6 +450,19 @@ void CodeGenFunction::FinishFunction(SourceLocation EndLoc) { // 5. Width of vector aguments and return types for functions called by this // function. CurFn->addFnAttr("min-legal-vector-width", llvm::utostr(LargestVectorWidth)); + + // If we generated an unreachable return block, delete it now. + if (ReturnBlock.isValid() && ReturnBlock.getBlock()->use_empty()) { + Builder.ClearInsertionPoint(); + ReturnBlock.getBlock()->eraseFromParent(); + } + if (ReturnValue.isValid()) { + auto *RetAlloca = dyn_cast(ReturnValue.getPointer()); + if (RetAlloca && RetAlloca->use_empty()) { + RetAlloca->eraseFromParent(); + ReturnValue = Address::invalid(); + } + } } /// ShouldInstrumentFunction - Return true if the current function should be -- cgit v1.2.3