[cfe-commits] r68186 - in /cfe/trunk: lib/CodeGen/CGStmt.cpp lib/CodeGen/CodeGenFunction.h test/CodeGen/rdr-6732143-dangling-block-reference.m

Daniel Dunbar daniel at zuster.org
Tue Mar 31 21:37:48 PDT 2009


Author: ddunbar
Date: Tue Mar 31 23:37:47 2009
New Revision: 68186

URL: http://llvm.org/viewvc/llvm-project?rev=68186&view=rev
Log:
Fix a subtle bug where the cleanup scope entries had a dangling block reference
 - <rdar://problem/6732143> Crash when generating @synchronize for
   zero-cost exception

 - Thanks to Anders for helping track down the problem.

Added:
    cfe/trunk/test/CodeGen/rdr-6732143-dangling-block-reference.m
Modified:
    cfe/trunk/lib/CodeGen/CGStmt.cpp
    cfe/trunk/lib/CodeGen/CodeGenFunction.h

Modified: cfe/trunk/lib/CodeGen/CGStmt.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGStmt.cpp?rev=68186&r1=68185&r2=68186&view=diff

==============================================================================
--- cfe/trunk/lib/CodeGen/CGStmt.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGStmt.cpp Tue Mar 31 23:37:47 2009
@@ -175,6 +175,24 @@
   return RV;
 }
 
+void CodeGenFunction::SimplifyForwardingBlocks(llvm::BasicBlock *BB) {
+  llvm::BranchInst *BI = dyn_cast<llvm::BranchInst>(BB->getTerminator());
+  
+  // If there is a cleanup stack, then we it isn't worth trying to
+  // simplify this block (we would need to remove it from the scope map
+  // and cleanup entry).
+  if (!CleanupEntries.empty())
+    return;
+
+  // Can only simplify direct branches.
+  if (!BI || !BI->isUnconditional())
+    return;
+
+  BB->replaceAllUsesWith(BI->getSuccessor(0));
+  BI->eraseFromParent();
+  BB->eraseFromParent();
+}
+
 void CodeGenFunction::EmitBlock(llvm::BasicBlock *BB, bool IsFinished) {
   // Fall out of the current block (if necessary).
   EmitBranch(BB);
@@ -339,13 +357,10 @@
   // Emit the exit block.
   EmitBlock(ExitBlock, true);
 
-  // If LoopHeader is a simple forwarding block then eliminate it.
-  if (!EmitBoolCondBranch 
-      && &LoopHeader->front() == LoopHeader->getTerminator()) {
-    LoopHeader->replaceAllUsesWith(LoopBody);
-    LoopHeader->getTerminator()->eraseFromParent();
-    LoopHeader->eraseFromParent();
-  }
+  // The LoopHeader typically is just a branch if we skipped emitting
+  // a branch, try to erase it.
+  if (!EmitBoolCondBranch)
+    SimplifyForwardingBlocks(LoopHeader);
 }
 
 void CodeGenFunction::EmitDoStmt(const DoStmt &S) {
@@ -387,14 +402,12 @@
     Builder.CreateCondBr(BoolCondVal, LoopBody, AfterDo);
   
   // Emit the exit block.
-  EmitBlock(AfterDo, true);
+  EmitBlock(AfterDo);
 
-  // If DoCond is a simple forwarding block then eliminate it.
-  if (!EmitBoolCondBranch && &DoCond->front() == DoCond->getTerminator()) {
-    DoCond->replaceAllUsesWith(AfterDo);
-    DoCond->getTerminator()->eraseFromParent();
-    DoCond->eraseFromParent();
-  }
+  // The DoCond block typically is just a branch if we skipped
+  // emitting a branch, try to erase it.
+  if (!EmitBoolCondBranch)
+    SimplifyForwardingBlocks(DoCond);
 }
 
 void CodeGenFunction::EmitForStmt(const ForStmt &S) {

Modified: cfe/trunk/lib/CodeGen/CodeGenFunction.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CodeGenFunction.h?rev=68186&r1=68185&r2=68186&view=diff

==============================================================================
--- cfe/trunk/lib/CodeGen/CodeGenFunction.h (original)
+++ cfe/trunk/lib/CodeGen/CodeGenFunction.h Tue Mar 31 23:37:47 2009
@@ -339,6 +339,11 @@
   /// label maps to.
   llvm::BasicBlock *getBasicBlockForLabel(const LabelStmt *S);
 
+  /// SimplifyForwardingBlocks - If the given basic block is only a
+  /// branch to another basic block, simplify it. This assumes that no
+  /// other code could potentially reference the basic block.
+  void SimplifyForwardingBlocks(llvm::BasicBlock *BB);
+
   /// EmitBlock - Emit the given block \arg BB and set it as the insert point,
   /// adding a fall-through branch from the current insert block if
   /// necessary. It is legal to call this function even if there is no current

Added: cfe/trunk/test/CodeGen/rdr-6732143-dangling-block-reference.m
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGen/rdr-6732143-dangling-block-reference.m?rev=68186&view=auto

==============================================================================
--- cfe/trunk/test/CodeGen/rdr-6732143-dangling-block-reference.m (added)
+++ cfe/trunk/test/CodeGen/rdr-6732143-dangling-block-reference.m Tue Mar 31 23:37:47 2009
@@ -0,0 +1,10 @@
+// RUN: clang-cc -triple x86_64-apple-darwin9 -emit-llvm %s -o -
+
+void f0(id x) {
+  @synchronized (x) {      
+    do { ; } while(0);
+    @try {
+    } @finally {
+    }
+  }
+}





More information about the cfe-commits mailing list