From: John McCall Date: Tue, 3 Aug 2010 22:46:07 +0000 (+0000) Subject: Do a very simple pass over every function we emit to infer whether we can X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=39dad53772c42eb36ebec1c81c56ba99d038fb94;p=clang Do a very simple pass over every function we emit to infer whether we can mark it nounwind based on whether it contains any non-nounwind calls. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@110163 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/CodeGen/CodeGenFunction.cpp b/lib/CodeGen/CodeGenFunction.cpp index 396ab4e6d2..35664af219 100644 --- a/lib/CodeGen/CodeGenFunction.cpp +++ b/lib/CodeGen/CodeGenFunction.cpp @@ -311,6 +311,19 @@ void CodeGenFunction::EmitFunctionBody(FunctionArgList &Args) { EmitStmt(FD->getBody()); } +/// Tries to mark the given function nounwind based on the +/// non-existence of any throwing calls within it. We believe this is +/// lightweight enough to do at -O0. +static void TryMarkNoThrow(llvm::Function *F) { + for (llvm::Function::iterator FI = F->begin(), FE = F->end(); FI != FE; ++FI) + for (llvm::BasicBlock::iterator + BI = FI->begin(), BE = FI->end(); BI != BE; ++BI) + if (llvm::CallInst *Call = dyn_cast(&*BI)) + if (!Call->doesNotThrow()) + return; + F->setDoesNotThrow(true); +} + void CodeGenFunction::GenerateCode(GlobalDecl GD, llvm::Function *Fn) { const FunctionDecl *FD = cast(GD.getDecl()); @@ -369,6 +382,11 @@ void CodeGenFunction::GenerateCode(GlobalDecl GD, llvm::Function *Fn) { // Emit the standard function epilogue. FinishFunction(BodyRange.getEnd()); + + // If we haven't marked the function nothrow through other means, do + // a quick pass now to see if we can. + if (!CurFn->doesNotThrow()) + TryMarkNoThrow(CurFn); } /// ContainsLabel - Return true if the statement contains a label in it. If diff --git a/test/CodeGen/unwind-attr.c b/test/CodeGen/unwind-attr.c index ee3199d274..7b34ae207e 100644 --- a/test/CodeGen/unwind-attr.c +++ b/test/CodeGen/unwind-attr.c @@ -1,6 +1,16 @@ -// RUN: %clang_cc1 -fexceptions -emit-llvm -o - %s | grep "@foo()" | not grep nounwind -// RUN: %clang_cc1 -emit-llvm -o - %s | grep "@foo()" | grep nounwind +// RUN: %clang_cc1 -fexceptions -emit-llvm -o - %s | FileCheck %s +// RUN: %clang_cc1 -emit-llvm -o - %s | FileCheck -check-prefix NOEXC %s -int foo(void) { - return 0; +int opaque(); + +// CHECK: define [[INT:i.*]] @test0() { +// CHECK-NOEXC: define [[INT:i.*]] @test0() nounwind { +int test0(void) { + return opaque(); +} + +// : locally infer nounwind at -O0 +// CHECK: define [[INT:i.*]] @test1() nounwind { +// CHECK-NOEXC: define [[INT:i.*]] @test1() nounwind { +int test1(void) { } diff --git a/test/CodeGenCXX/global-init.cpp b/test/CodeGenCXX/global-init.cpp index 07db598900..210f2a6969 100644 --- a/test/CodeGenCXX/global-init.cpp +++ b/test/CodeGenCXX/global-init.cpp @@ -36,7 +36,7 @@ namespace test1 { const int y = x - 1; // This gets deferred. const int z = ~y; // This also gets deferred, but gets "undeferred" before y. int test() { return z; } -// CHECK: define i32 @_ZN5test14testEv() { +// CHECK: define i32 @_ZN5test14testEv() // All of these initializers end up delayed, so we check them later. }