[PATCH] D11360: Proposed patch to prevent the creation of empty (forwarding) blocks resulting from nested ifs.

Wolfgang Pieb via cfe-commits cfe-commits at lists.llvm.org
Wed Jul 6 11:09:57 PDT 2016


wolfgangp updated this revision to Diff 62916.
wolfgangp added a comment.

Addressed review comments: documented changes and clang-formatted the test case.


http://reviews.llvm.org/D11360

Files:
  lib/CodeGen/CGStmt.cpp
  test/CodeGen/forwarding-blocks-if.c

Index: test/CodeGen/forwarding-blocks-if.c
===================================================================
--- test/CodeGen/forwarding-blocks-if.c
+++ test/CodeGen/forwarding-blocks-if.c
@@ -0,0 +1,36 @@
+// RUN: %clang_cc1 %s -emit-llvm -o - | FileCheck %s
+// Check that no empty blocks are generated for nested ifs.
+
+extern void func();
+
+int f0(int val) {
+  if (val == 0) {
+    func();
+  } else if (val == 1) {
+    func();
+  }
+  return 0;
+}
+
+// CHECK-LABEL: define i32 @f0
+// CHECK: call void {{.*}} @func
+// CHECK: call void {{.*}} @func
+// CHECK: br label %[[RETBLOCK1:[^ ]*]]
+// CHECK: [[RETBLOCK1]]:
+// CHECK-NOT: br label
+// CHECK: ret i32
+
+int f1(int val, int g) {
+  if (val == 0)
+    if (g == 1) {
+      func();
+    }
+  return 0;
+}
+
+// CHECK-LABEL: define i32 @f1
+// CHECK: call void {{.*}} @func
+// CHECK: br label %[[RETBLOCK2:[^ ]*]]
+// CHECK: [[RETBLOCK2]]:
+// CHECK-NOT: br label
+// CHECK: ret i32
Index: lib/CodeGen/CGStmt.cpp
===================================================================
--- lib/CodeGen/CGStmt.cpp
+++ lib/CodeGen/CGStmt.cpp
@@ -610,7 +610,14 @@
     RunCleanupsScope ThenScope(*this);
     EmitStmt(S.getThen());
   }
-  EmitBranch(ContBlock);
+  {
+    auto CurBlock = Builder.GetInsertBlock();
+    EmitBranch(ContBlock);
+    // Eliminate any empty blocks that may have been created by nested
+    // control flow statements in the 'then' clause.
+    if (CurBlock)
+      SimplifyForwardingBlocks(CurBlock); 
+  }
 
   // Emit the 'else' code if present.
   if (const Stmt *Else = S.getElse()) {
@@ -626,7 +633,12 @@
     {
       // There is no need to emit line number for an unconditional branch.
       auto NL = ApplyDebugLocation::CreateEmpty(*this);
+      auto CurBlock = Builder.GetInsertBlock();
       EmitBranch(ContBlock);
+      // Eliminate any empty blocks that may have been created by nested
+      // control flow statements emitted in the 'else' clause.
+      if (CurBlock)
+        SimplifyForwardingBlocks(CurBlock); 
     }
   }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D11360.62916.patch
Type: text/x-patch
Size: 2038 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20160706/214b8795/attachment.bin>


More information about the cfe-commits mailing list