OSDN Git Service

BlockExtractor: Don’t delete functions directly
authorVolkan Keles <vkeles@apple.com>
Mon, 12 Mar 2018 22:28:18 +0000 (22:28 +0000)
committerVolkan Keles <vkeles@apple.com>
Mon, 12 Mar 2018 22:28:18 +0000 (22:28 +0000)
Blocks may have function calls, so don’t erase functions
directly to avoid erasing a function that has a user.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@327340 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Transforms/IPO/BlockExtractor.cpp
test/Transforms/BlockExtractor/extract-blocks.ll
test/tools/llvm-extract/extract-block.ll

index 125c7b7..9d30efd 100644 (file)
@@ -161,8 +161,9 @@ bool BlockExtractor::runOnModule(Module &M) {
   // Erase the functions.
   if (EraseFunctions || BlockExtractorEraseFuncs) {
     for (Function *F : Functions) {
-      DEBUG(dbgs() << "BlockExtractor: Deleting " << F->getName() << "\n");
-      F->eraseFromParent();
+      DEBUG(dbgs() << "BlockExtractor: Trying to delete " << F->getName()
+                   << "\n");
+      F->deleteBody();
     }
     // Set linkage as ExternalLinkage to avoid erasing unreachable functions.
     for (Function &F : M)
index daf9a09..47e5fc3 100644 (file)
@@ -6,7 +6,7 @@
 ; CHECK-NO-ERASE: @foo(
 ; CHECK-NO-ERASE: @foo_bb9(
 ; CHECK-NO-ERASE: @foo_bb20(
-; CHECK-ERASE-NOT: @foo(
+; CHECK-ERASE: declare i32 @foo(
 ; CHECK-ERASE: @foo_bb9(
 ; CHECK-ERASE: @foo_bb20(
 define i32 @foo(i32 %arg, i32 %arg1) {
index 761ed32..c812a56 100644 (file)
@@ -1,6 +1,19 @@
 ; RUN: llvm-extract -S -bb foo:bb4 %s | FileCheck %s
 
+; CHECK: declare void @bar()
+define void @bar() {
+bb:
+  ret void
+}
+
+; CHECK-NOT: @unused()
+define void @unused() {
+bb:
+  ret void
+}
+
 ; CHECK: @foo_bb4
+; CHECK: call void @bar()
 ; CHECK: %tmp5
 define i32 @foo(i32 %arg) {
 bb:
@@ -12,6 +25,7 @@ bb:
   br i1 %tmp3, label %bb4, label %bb7
 
 bb4:                                              ; preds = %bb
+  call void @bar()
   %tmp5 = load i32, i32* %tmp1, align 4
   %tmp6 = add nsw i32 %tmp5, 1
   store i32 %tmp6, i32* %tmp1, align 4