Index: lib/Transforms/Scalar/NewGVN.cpp =================================================================== --- lib/Transforms/Scalar/NewGVN.cpp +++ lib/Transforms/Scalar/NewGVN.cpp @@ -1151,7 +1151,9 @@ return SimplifiedE; } else if (isa(I)) { if (isa(E->getOperand(0)) || - E->getOperand(1) == E->getOperand(2)) { + E->getOperand(1) == E->getOperand(2) || + isa(E->getOperand(1)) || + isa(E->getOperand(2))) { assert(E->getOperand(1)->getType() == I->getOperand(1)->getType() && E->getOperand(2)->getType() == I->getOperand(2)->getType()); Value *V = SimplifySelectInst(E->getOperand(0), E->getOperand(1), Index: test/Transforms/NewGVN/pr33165.ll =================================================================== --- /dev/null +++ test/Transforms/NewGVN/pr33165.ll @@ -0,0 +1,24 @@ +; NOTE: Assertions have been autogenerated by utils/update_test_checks.py +; RUN: opt -newgvn -S < %s | FileCheck %s + +define i2 @t1(i2, i1) { +; CHECK-LABEL: @t1( +; CHECK-NEXT: [[A:%.*]] = xor i2 [[TMP0:%.*]], -1 +; CHECK-NEXT: ret i2 [[A]] +; + %a = xor i2 %0, -1 + %b = select i1 %1, i2 %a, i2 undef + %c = and i2 %a, %b + ret i2 %c +} + +define i2 @t2(i2, i1) { +; CHECK-LABEL: @t2( +; CHECK-NEXT: [[A:%.*]] = xor i2 [[TMP0:%.*]], -1 +; CHECK-NEXT: ret i2 [[A]] +; + %a = xor i2 %0, -1 + %b = select i1 %1, i2 undef, i2 %a + %c = and i2 %a, %b + ret i2 %c +}