Index: lib/Transforms/InstCombine/InstCombineSelect.cpp =================================================================== --- lib/Transforms/InstCombine/InstCombineSelect.cpp +++ lib/Transforms/InstCombine/InstCombineSelect.cpp @@ -155,7 +155,7 @@ // Fold this by inserting a select from the input values. Value *NewSI = Builder->CreateSelect(SI.getCondition(), TI->getOperand(0), - FI->getOperand(0), SI.getName()+".v"); + FI->getOperand(0), SI.getName()+".v", &SI); return CastInst::Create(Instruction::CastOps(TI->getOpcode()), NewSI, TI->getType()); } @@ -200,7 +200,7 @@ // If we reach here, they do have operations in common. Value *NewSI = Builder->CreateSelect(SI.getCondition(), OtherOpT, - OtherOpF, SI.getName()+".v"); + OtherOpF, SI.getName()+".v", &SI); if (BinaryOperator *BO = dyn_cast(TI)) { if (MatchIsOpZero) Index: test/Transforms/InstCombine/select_meta.ll =================================================================== --- test/Transforms/InstCombine/select_meta.ll +++ test/Transforms/InstCombine/select_meta.ll @@ -0,0 +1,78 @@ +; RUN: opt < %s -instcombine -S | FileCheck %s + +define i32 @foo(i32) local_unnamed_addr #0 { + %2 = icmp sgt i32 %0, 2 + %3 = add nsw i32 %0, 20 + %4 = add nsw i32 %0, -20 + select i1 %2, i32 %3, i32 %4, !prof !1 + ret i32 %5 +; CHECK-LABEL: @foo +; CHECK: select {{.*}}, !prof ![[MD1:[0-9]+]] +} + + +; SMAX(SMAX(x, y), x) -> SMAX(x, y) +define i32 @test30(i32 %x, i32 %y) { + %cmp = icmp sgt i32 %x, %y + %cond = select i1 %cmp, i32 %x, i32 %y, !prof !1 + %cmp5 = icmp sgt i32 %cond, %x + %retval = select i1 %cmp5, i32 %cond, i32 %x, !prof !2 + ret i32 %retval +; CHECK-LABEL: @test30 +; CHECK: select {{.*}}, !prof ![[MD1]] +} + +define i32 @test70(i32 %x) { +entry: + %cmp = icmp slt i32 %x, 75 + %cond = select i1 %cmp, i32 75, i32 %x, !prof !1 + %cmp3 = icmp slt i32 %cond, 36 + %retval = select i1 %cmp3, i32 36, i32 %cond, !prof !2 + ret i32 %retval +; CHECK-LABEL: @test70 +; CHECK: select {{.*}}, !prof ![[MD1]] +} + + +; SMIN(SMIN(X, 92), 11) -> SMIN(X, 11) +define i32 @test72(i32 %x) { + %cmp = icmp sgt i32 %x, 92 + %cond = select i1 %cmp, i32 92, i32 %x, !prof !1 + %cmp3 = icmp sgt i32 %cond, 11 + %retval = select i1 %cmp3, i32 11, i32 %cond, !prof !2 + ret i32 %retval +; CHECK-LABEL: @test72 +; CHECK: select {{.*}}, !prof ![[MD2:[0-9]+]] +} + + +; SMAX(SMAX(X, 36), 75) -> SMAX(X, 75) +define i32 @test74(i32 %x) { + %cmp = icmp slt i32 %x, 36 + %cond = select i1 %cmp, i32 36, i32 %x, !prof !1 + %cmp3 = icmp slt i32 %cond, 75 + %retval = select i1 %cmp3, i32 75, i32 %cond, !prof !2 + ret i32 %retval +; CHECK-LABEL: @test74 +; CHECK: select {{.*}}, !prof ![[MD2]] +} + +define void @min_max_bitcast(<4 x float> %a, <4 x float> %b, <4 x i32>* %ptr1, <4 x i32>* %ptr2) { + %cmp = fcmp olt <4 x float> %a, %b + %bc1 = bitcast <4 x float> %a to <4 x i32> + %bc2 = bitcast <4 x float> %b to <4 x i32> + %sel1 = select <4 x i1> %cmp, <4 x i32> %bc1, <4 x i32> %bc2, !prof !1 + %sel2 = select <4 x i1> %cmp, <4 x i32> %bc2, <4 x i32> %bc1, !prof !1 + store <4 x i32> %sel1, <4 x i32>* %ptr1 + store <4 x i32> %sel2, <4 x i32>* %ptr2 + ret void +; CHECK-LABEL: @min_max_bitcast +; CHECK: select {{.*}}, !prof ![[MD1]] +} + +!1 = !{!"branch_weights", i32 2, i32 10} +!2 = !{!"branch_weights", i32 3, i32 10} + +; CHECK: ![[MD1]] = !{!"branch_weights", i32 2, i32 10} +; CHECK: ![[MD2]] = !{!"branch_weights", i32 3, i32 10} +