diff --git a/lib/Transforms/Scalar/InstructionCombining.cpp b/lib/Transforms/Scalar/InstructionCombining.cpp index 67c40fe21c9..3b181c85d26 100644 --- a/lib/Transforms/Scalar/InstructionCombining.cpp +++ b/lib/Transforms/Scalar/InstructionCombining.cpp @@ -5043,8 +5043,12 @@ Instruction *InstCombiner::visitFCmpInst(FCmpInst &I) { if (Instruction *LHSI = dyn_cast(Op0)) switch (LHSI->getOpcode()) { case Instruction::PHI: - if (Instruction *NV = FoldOpIntoPhi(I)) - return NV; + // Only fold fcmp into the PHI if the phi and fcmp are in the same + // block. If in the same block, we're encouraging jump threading. If + // not, we are just pessimizing the code by making an i1 phi. + if (LHSI->getParent() == I.getParent()) + if (Instruction *NV = FoldOpIntoPhi(I)) + return NV; break; case Instruction::SIToFP: case Instruction::UIToFP: @@ -5348,8 +5352,12 @@ Instruction *InstCombiner::visitICmpInst(ICmpInst &I) { break; case Instruction::PHI: - if (Instruction *NV = FoldOpIntoPhi(I)) - return NV; + // Only fold icmp into the PHI if the phi and fcmp are in the same + // block. If in the same block, we're encouraging jump threading. If + // not, we are just pessimizing the code by making an i1 phi. + if (LHSI->getParent() == I.getParent()) + if (Instruction *NV = FoldOpIntoPhi(I)) + return NV; break; case Instruction::Select: { // If either operand of the select is a constant, we can fold the diff --git a/test/Transforms/InstCombine/2008-06-08-ICmpPHI.ll b/test/Transforms/InstCombine/2008-06-08-ICmpPHI.ll new file mode 100644 index 00000000000..555f81087b7 --- /dev/null +++ b/test/Transforms/InstCombine/2008-06-08-ICmpPHI.ll @@ -0,0 +1,49 @@ +; RUN: llvm-as < %s | opt -instcombine | llvm-dis | grep {phi i32} | count 2 +target datalayout = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-f32:32:32-f64:32:64-v64:64:64-v128:128:128-a0:0:64-f80:128:128" +target triple = "i386-apple-darwin8" + +define void @test() nounwind { +entry: + br label %bb + +bb: ; preds = %bb16, %entry + %i.0 = phi i32 [ 0, %entry ], [ %indvar.next, %somebb ] ; [#uses=1] + %x.0 = phi i32 [ 37, %entry ], [ %tmp17, %somebb ] ; [#uses=1] + %tmp = tail call i32 (...)* @bork( ) nounwind ; [#uses=0] + %tmp1 = tail call i32 (...)* @bork( ) nounwind ; [#uses=0] + %tmp2 = tail call i32 (...)* @bork( ) nounwind ; [#uses=1] + %tmp3 = icmp eq i32 %tmp2, 0 ; [#uses=1] + br i1 %tmp3, label %bb7, label %bb5 + +bb5: ; preds = %bb + %tmp6 = tail call i32 (...)* @bork( ) nounwind ; [#uses=0] + br label %bb7 + +bb7: ; preds = %bb5, %bb + %tmp8 = tail call i32 (...)* @bork( ) nounwind ; [#uses=0] + %tmp9 = tail call i32 (...)* @bork( ) nounwind ; [#uses=0] + %tmp11 = icmp eq i32 %x.0, 37 ; [#uses=1] + br i1 %tmp11, label %bb14, label %bb16 + +bb14: ; preds = %bb7 + %tmp15 = tail call i32 (...)* @bar( ) nounwind ; [#uses=0] + br label %bb16 + +bb16: ; preds = %bb14, %bb7 + %tmp17 = tail call i32 (...)* @zap( ) nounwind ; [#uses=1] + %indvar.next = add i32 %i.0, 1 ; [#uses=2] + %exitcond = icmp eq i32 %indvar.next, 42 ; [#uses=1] + br i1 %exitcond, label %return, label %somebb + +somebb: + br label %bb + +return: ; preds = %bb16 + ret void +} + +declare i32 @bork(...) + +declare i32 @bar(...) + +declare i32 @zap(...)