[llvm-commits] [llvm] r48086 - in /llvm/trunk: lib/Transforms/Utils/InlineFunction.cpp lib/Transforms/Utils/SimplifyCFG.cpp test/Transforms/Inline/unwindto.ll test/Transforms/SimplifyCFG/unwindto.ll

Nick Lewycky nicholas at mxc.ca
Sat Mar 8 21:10:13 PST 2008


Author: nicholas
Date: Sat Mar  8 23:10:13 2008
New Revision: 48086

URL: http://llvm.org/viewvc/llvm-project?rev=48086&view=rev
Log:
Update the inliner and simplifycfg to handle unwind_to.

Added:
    llvm/trunk/test/Transforms/Inline/unwindto.ll
    llvm/trunk/test/Transforms/SimplifyCFG/unwindto.ll
Modified:
    llvm/trunk/lib/Transforms/Utils/InlineFunction.cpp
    llvm/trunk/lib/Transforms/Utils/SimplifyCFG.cpp

Modified: llvm/trunk/lib/Transforms/Utils/InlineFunction.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Utils/InlineFunction.cpp?rev=48086&r1=48085&r2=48086&view=diff

==============================================================================
--- llvm/trunk/lib/Transforms/Utils/InlineFunction.cpp (original)
+++ llvm/trunk/lib/Transforms/Utils/InlineFunction.cpp Sat Mar  8 23:10:13 2008
@@ -202,6 +202,7 @@
 
   BasicBlock *OrigBB = TheCall->getParent();
   Function *Caller = OrigBB->getParent();
+  BasicBlock *UnwindBB = OrigBB->getUnwindDest();
 
   // GC poses two hazards to inlining, which only occur when the callee has GC:
   //  1. If the caller has no GC, then the callee's GC must be propagated to the
@@ -419,6 +420,18 @@
       }
     }
 
+  // If we are inlining a function that unwinds into a BB with an unwind dest,
+  // turn the inlined unwinds into branches to the unwind dest.
+  if (InlinedFunctionInfo.ContainsUnwinds && UnwindBB && isa<CallInst>(TheCall))
+    for (Function::iterator BB = FirstNewBlock, E = Caller->end();
+         BB != E; ++BB) {
+      TerminatorInst *Term = BB->getTerminator();
+      if (isa<UnwindInst>(Term)) {
+        new BranchInst(UnwindBB, Term);
+        BB->getInstList().erase(Term);
+      }
+    }
+
   // If we are inlining for an invoke instruction, we must make sure to rewrite
   // any inlined 'unwind' instructions into branches to the invoke exception
   // destination, and call instructions into invoke instructions.

Modified: llvm/trunk/lib/Transforms/Utils/SimplifyCFG.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Utils/SimplifyCFG.cpp?rev=48086&r1=48085&r2=48086&view=diff

==============================================================================
--- llvm/trunk/lib/Transforms/Utils/SimplifyCFG.cpp (original)
+++ llvm/trunk/lib/Transforms/Utils/SimplifyCFG.cpp Sat Mar  8 23:10:13 2008
@@ -1237,6 +1237,8 @@
       SmallVector<BasicBlock*, 8> UncondBranchPreds;
       SmallVector<BranchInst*, 8> CondBranchPreds;
       for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) {
+        if ((*PI)->getUnwindDest() == BB) continue;
+
         TerminatorInst *PTI = (*PI)->getTerminator();
         if (BranchInst *BI = dyn_cast<BranchInst>(PTI)) {
           if (BI->isUnconditional())
@@ -1788,6 +1790,7 @@
 
   BasicBlock *OnlySucc = 0;
   if (OnlyPred && OnlyPred != BB &&    // Don't break self loops
+      OnlyPred->getUnwindDest() != BB &&
       OnlyPred->getTerminator()->getOpcode() != Instruction::Invoke) {
     // Check to see if there is only one distinct successor...
     succ_iterator SI(succ_begin(OnlyPred)), SE(succ_end(OnlyPred));
@@ -1799,7 +1802,8 @@
       }
   }
 
-  if (OnlySucc) {
+  if (OnlySucc && (BB->getUnwindDest() == OnlyPred->getUnwindDest() ||
+                   !BB->getUnwindDest() || !OnlyPred->getUnwindDest())) {
     DOUT << "Merging: " << *BB << "into: " << *OnlyPred;
 
     // Resolve any PHI nodes at the start of the block.  They are all
@@ -1819,6 +1823,10 @@
     // Move all definitions in the successor to the predecessor.
     OnlyPred->getInstList().splice(OnlyPred->end(), BB->getInstList());
 
+    // Move the unwind_to block
+    if (!OnlyPred->getUnwindDest() && BB->getUnwindDest())
+      OnlyPred->setUnwindDest(BB->getUnwindDest());
+
     // Make all PHI nodes that referred to BB now refer to Pred as their
     // source.
     BB->replaceAllUsesWith(OnlyPred);

Added: llvm/trunk/test/Transforms/Inline/unwindto.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/Inline/unwindto.ll?rev=48086&view=auto

==============================================================================
--- llvm/trunk/test/Transforms/Inline/unwindto.ll (added)
+++ llvm/trunk/test/Transforms/Inline/unwindto.ll Sat Mar  8 23:10:13 2008
@@ -0,0 +1,13 @@
+; RUN: llvm-as < %s | opt -inline | llvm-dis | grep "br label %cleanup"
+
+define void @g() {
+  unwind
+}
+
+define i32 @f1() {
+entry: unwind_to %cleanup
+  call void @g()
+  ret i32 0
+cleanup:
+  ret i32 1
+}

Added: llvm/trunk/test/Transforms/SimplifyCFG/unwindto.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/SimplifyCFG/unwindto.ll?rev=48086&view=auto

==============================================================================
--- llvm/trunk/test/Transforms/SimplifyCFG/unwindto.ll (added)
+++ llvm/trunk/test/Transforms/SimplifyCFG/unwindto.ll Sat Mar  8 23:10:13 2008
@@ -0,0 +1,35 @@
+; RUN: llvm-as < %s | opt -simplifycfg | llvm-dis | grep unwind_to | count 3
+
+declare void @g(i32)
+
+define i32 @f1() {
+entry:
+  br label %bb1
+bb1: unwind_to %cleanup1
+  call void @g(i32 0)
+  br label %bb2
+bb2: unwind_to %cleanup2
+  call void @g(i32 1)
+  br label %exit
+exit:
+  ret i32 0
+cleanup1:
+  ret i32 1
+cleanup2:
+  ret i32 2
+}
+
+define i32 @f2() {
+entry: unwind_to %cleanup
+  br label %bb1
+bb1: unwind_to %cleanup
+  br label %bb2
+bb2: unwind_to %cleanup
+  br label %bb3
+bb3:
+  br label %bb4
+bb4: unwind_to %cleanup
+  ret i32 0
+cleanup:
+  ret i32 1
+}





More information about the llvm-commits mailing list