Author: wolfgangp Date: Mon Jul 11 17:22:23 2016 New Revision: 275115 URL: http://llvm.org/viewvc/llvm-project?rev=275115&view=rev Log: Prevent the creation of empty (forwarding) blocks resulting from nested ifs.
Summary: Nested if statements can generate empty BBs whose terminator branches unconditionally to its successor. These branches are not eliminated to help generate better line number information in some cases, but there is no reason to keep the empty blocks that result from nested ifs. Reviewers: mehdi_amini, dblaikie, echristo Subscribers: mehdi_amini, cfe-commits Differential review: http://reviews.llvm.org/D11360 Added: cfe/trunk/test/CodeGen/forwarding-blocks-if.c Modified: cfe/trunk/lib/CodeGen/CGStmt.cpp Modified: cfe/trunk/lib/CodeGen/CGStmt.cpp URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGStmt.cpp?rev=275115&r1=275114&r2=275115&view=diff ============================================================================== --- cfe/trunk/lib/CodeGen/CGStmt.cpp (original) +++ cfe/trunk/lib/CodeGen/CGStmt.cpp Mon Jul 11 17:22:23 2016 @@ -613,7 +613,14 @@ void CodeGenFunction::EmitIfStmt(const I 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()) { @@ -629,7 +636,12 @@ void CodeGenFunction::EmitIfStmt(const I { // 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); } } Added: cfe/trunk/test/CodeGen/forwarding-blocks-if.c URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGen/forwarding-blocks-if.c?rev=275115&view=auto ============================================================================== --- cfe/trunk/test/CodeGen/forwarding-blocks-if.c (added) +++ cfe/trunk/test/CodeGen/forwarding-blocks-if.c Mon Jul 11 17:22:23 2016 @@ -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 _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits