Index: lib/Transforms/Utils/SimplifyCFG.cpp =================================================================== --- lib/Transforms/Utils/SimplifyCFG.cpp +++ lib/Transforms/Utils/SimplifyCFG.cpp @@ -4610,13 +4610,23 @@ IRBuilder<> Builder(T); if (BranchInst *BI = dyn_cast(T)) { BB->removePredecessor(PHI->getIncomingBlock(i)); - // Turn uncoditional branches into unreachables and remove the dead + // Turn unconditional branches into unreachables and remove the dead // destination from conditional branches. if (BI->isUnconditional()) Builder.CreateUnreachable(); - else - Builder.CreateBr(BI->getSuccessor(0) == BB ? BI->getSuccessor(1) : - BI->getSuccessor(0)); + else { + bool PickFirst = BI->getSuccessor(1) == BB; + Builder.CreateBr(PickFirst + ? BI->getSuccessor(0) + : BI->getSuccessor(1)); + if (auto Cond = dyn_cast(BI->getCondition())) { + // Inline the result of the conditional check downstream as a + // constant, and then get rid of it if possible. + Value *Const = ConstantInt::get(Cond->getType(), PickFirst); + Cond->replaceAllUsesWith(Const); + RecursivelyDeleteTriviallyDeadInstructions(Cond); + } + } BI->eraseFromParent(); return true; } Index: test/Transforms/SimplifyCFG/load-means-not-null.ll =================================================================== --- /dev/null +++ test/Transforms/SimplifyCFG/load-means-not-null.ll @@ -0,0 +1,23 @@ +; RUN: opt < %s -simplifycfg -S | FileCheck %s + +declare void @g(i8) + +; CHECK-LABEL: @test_postdominate +; CHECK-NEXT: a: +; CHECK-NEXT: call void @g(i8 0) +; CHECK: ret i1 true +define i1 @test_postdominate(i8* %mem) { + %test = icmp eq i8* %mem, null + br i1 %test, label %a, label %b +a: + call void @g(i8 0) + br label %c +b: + br label %c +c: + %join = phi i8* [ %mem, %a ], [ null, %b ] + %gep = getelementptr i8, i8* %join, i64 4 + %res = load i8, i8* %gep + call void @g(i8 %res) + ret i1 %test +} Index: test/Transforms/SimplifyCFG/phi-undef-loadstore.ll =================================================================== --- test/Transforms/SimplifyCFG/phi-undef-loadstore.ll +++ test/Transforms/SimplifyCFG/phi-undef-loadstore.ll @@ -25,10 +25,9 @@ ret i32 %tmp9 ; CHECK-LABEL: @test1( -; CHECK: if.else: -; CHECK: br label %if.end7 - -; CHECK: phi i32* [ %a, %if.then ], [ %c, %if.else ] +; CHECK: tail call void @bar() +; CHECK: %c.a = select i1 %tobool, i32* %c, i32* %a +; CHECK: %tmp9 = load i32, i32* %c.a } define i32 @test2(i32* %a, i32 %b, i32* %c, i32 %d) nounwind { @@ -53,9 +52,8 @@ %tmp9 = load i32, i32* %x.0 ret i32 %tmp9 ; CHECK-LABEL: @test2( -; CHECK: if.else: -; CHECK: unreachable - +; CHECK: tail call void @bar() +; CHECK: %tmp9 = load i32, i32* %a ; CHECK-NOT: phi }