Index: lib/CodeGen/CGStmt.cpp =================================================================== --- lib/CodeGen/CGStmt.cpp +++ lib/CodeGen/CGStmt.cpp @@ -566,7 +566,12 @@ RunCleanupsScope ThenScope(*this); EmitStmt(S.getThen()); } - EmitBranch(ContBlock); + { + auto CurBlock = Builder.GetInsertBlock(); + EmitBranch(ContBlock); + if (CurBlock) + SimplifyForwardingBlocks(CurBlock); + } // Emit the 'else' code if present. if (const Stmt *Else = S.getElse()) { @@ -582,7 +587,10 @@ { // There is no need to emit line number for an unconditional branch. auto NL = ApplyDebugLocation::CreateEmpty(*this); + auto CurBlock = Builder.GetInsertBlock(); EmitBranch(ContBlock); + if (CurBlock) + SimplifyForwardingBlocks(CurBlock); } } Index: test/CodeGen/forwarding-blocks-if.c =================================================================== --- test/CodeGen/forwarding-blocks-if.c +++ test/CodeGen/forwarding-blocks-if.c @@ -0,0 +1,38 @@ +// 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: if.end{{.*:}} +// 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: if.end{{.*:}} +// CHECK-NOT: br label +// CHECK: ret i32