Index: lib/Transforms/Utils/SimplifyCFG.cpp =================================================================== --- lib/Transforms/Utils/SimplifyCFG.cpp +++ lib/Transforms/Utils/SimplifyCFG.cpp @@ -1449,6 +1449,10 @@ // have no uses. if (!isa(I) && !I->hasOneUse()) return false; + // An intrinsic should not be moved even if one of its operand can be + // replaced by a variable. + if (isa(I)) + return false; } const Instruction *I0 = Insts.front(); Index: test/Transforms/SimplifyCFG/sink-common-code.ll =================================================================== --- test/Transforms/SimplifyCFG/sink-common-code.ll +++ test/Transforms/SimplifyCFG/sink-common-code.ll @@ -310,6 +310,32 @@ ; CHECK: call i32 @llvm.ctlz ; CHECK: call i32 @llvm.cttz +; We can't common an intrinsic even with non-constant operands! +define i32 @test_intrinsic(i1 zeroext %flag, i32 %w, i32 %x, i32 %y) { +entry: + br i1 %flag, label %if.then, label %if.else + +if.then: + %dummy = add i32 %w, 5 + %sv1 = call i32 @llvm.ctpop.i32(i32 %y) + br label %if.end + +if.else: + %dummy1 = add i32 %w, 6 + %sv2 = call i32 @llvm.ctpop.i32(i32 %x) + br label %if.end + +if.end: + %p = phi i32 [ %sv1, %if.then ], [ %sv2, %if.else ] + ret i32 1 +} + +declare i32 @llvm.ctpop.i32(i32 %x) + +; CHECK-LABEL: test_intrinsic +; CHECK: call i32 @llvm.ctpop +; CHECK: call i32 @llvm.ctpop + ; The TBAA metadata should be properly combined. define i32 @test13(i1 zeroext %flag, i32 %x, i32* %y) { entry: