Index: lib/Transforms/InstCombine/InstCombineSelect.cpp =================================================================== --- lib/Transforms/InstCombine/InstCombineSelect.cpp +++ lib/Transforms/InstCombine/InstCombineSelect.cpp @@ -54,6 +54,27 @@ return Builder.CreateSelect(Builder.CreateICmp(Pred, A, B), A, B); } +/// Fold +/// %A = icmp eq i8 %x, 0 +/// %B = xor i8 %x, %z +/// %C = select i1 %A, i8 %B, i8 %y +/// To +/// %C = select i1 %A, i8 %z, i8 %y +static Value *foldSelectInstWithBinaryOp(Value *Cond, Value *TrueVal, + Value *FalseVal, + InstCombiner::BuilderTy &Builder) { + Value *X, *Z; + CmpInst::Predicate Pred; + if (!match(Cond, m_c_ICmp(Pred, m_Value(X), m_Zero())) || + Pred != ICmpInst::ICMP_EQ) + return nullptr; + if (!match(TrueVal, m_c_Xor(m_Specific(X), m_Value(Z)))) { + return nullptr; + } + + return Builder.CreateSelect(Cond, Z, FalseVal); +} + /// This folds: /// select (icmp eq (and X, C1)), TC, FC /// iff C1 is a power 2 and the difference between TC and FC is a power-of-2. @@ -1711,6 +1732,10 @@ if (Instruction *Result = foldSelectInstWithICmp(SI, ICI)) return Result; + if (Value *V = + foldSelectInstWithBinaryOp(CondVal, TrueVal, FalseVal, Builder)) + return replaceInstUsesWith(SI, V); + if (Instruction *Add = foldAddSubSelect(SI, Builder)) return Add; @@ -1962,4 +1987,4 @@ return Select; return nullptr; -} +} \ No newline at end of file Index: test/Transforms/InstCombine/select-xor-icmp.ll =================================================================== --- test/Transforms/InstCombine/select-xor-icmp.ll +++ test/Transforms/InstCombine/select-xor-icmp.ll @@ -0,0 +1,92 @@ +; NOTE: Assertions have been autogenerated by utils/update_test_checks.py +; RUN: opt < %s -instcombine -S | FileCheck %s + +target triple = "x86_64-unknown-linux-gnu" + +define i32 @select_xor_icmp(i32 %x, i32 %y, i32 %z) { +; CHECK-LABEL: @select_xor_icmp( +; CHECK-NEXT: [[A:%.*]] = icmp eq i32 [[X:%.*]], 0 +; CHECK-NEXT: [[TMP1:%.*]] = select i1 [[A]], i32 [[Z:%.*]], i32 [[Y:%.*]] +; CHECK-NEXT: ret i32 [[TMP1]] +; + %A = icmp eq i32 %x, 0 + %B = xor i32 %x, %z + %C = select i1 %A, i32 %B, i32 %y + ret i32 %C +} + +define <2 x i8> @select_xor_icmp_vec(<2 x i8> %x, <2 x i8> %y, <2 x i8> %z) { +; CHECK-LABEL: @select_xor_icmp_vec( +; CHECK-NEXT: [[A:%.*]] = icmp eq <2 x i8> [[X:%.*]], zeroinitializer +; CHECK-NEXT: [[TMP1:%.*]] = select <2 x i1> [[A]], <2 x i8> [[Z:%.*]], <2 x i8> [[Y:%.*]] +; CHECK-NEXT: ret <2 x i8> [[TMP1]] +; + %A = icmp eq <2 x i8> %x, + %B = xor <2 x i8> %x, %z + %C = select <2 x i1> %A, <2 x i8> %B, <2 x i8> %y + ret <2 x i8> %C +} + +define i32 @select_or_icmp_inv(i32 %x, i32 %y, i32 %z) { +; CHECK-LABEL: @select_or_icmp_inv( +; CHECK-NEXT: [[A:%.*]] = icmp eq i32 [[X:%.*]], 0 +; CHECK-NEXT: [[TMP1:%.*]] = select i1 [[A]], i32 [[Z:%.*]], i32 [[Y:%.*]] +; CHECK-NEXT: ret i32 [[TMP1]] +; + %A = icmp eq i32 0, %x + %B = xor i32 %x, %z + %C = select i1 %A, i32 %B, i32 %y + ret i32 %C +} + +define i32 @select_xor_inv_icmp(i32 %x, i32 %y, i32 %z) { +; CHECK-LABEL: @select_xor_inv_icmp( +; CHECK-NEXT: [[A:%.*]] = icmp eq i32 [[X:%.*]], 0 +; CHECK-NEXT: [[TMP1:%.*]] = select i1 [[A]], i32 [[Z:%.*]], i32 [[Y:%.*]] +; CHECK-NEXT: ret i32 [[TMP1]] +; + %A = icmp eq i32 %x, 0 + %B = xor i32 %z, %x + %C = select i1 %A, i32 %B, i32 %y + ret i32 %C +} + +;Negative tests +define i32 @select_xor_icmp_bad_1(i32 %x, i32 %y, i32 %z) { +; CHECK-LABEL: @select_xor_icmp_bad_1( +; CHECK-NEXT: [[A:%.*]] = icmp eq i32 [[X:%.*]], 0 +; CHECK-NEXT: [[B:%.*]] = xor i32 [[X]], [[Z:%.*]] +; CHECK-NEXT: [[C:%.*]] = select i1 [[A]], i32 [[Y:%.*]], i32 [[B]] +; CHECK-NEXT: ret i32 [[C]] +; + %A = icmp ne i32 %x, 0 + %B = xor i32 %x, %z + %C = select i1 %A, i32 %B, i32 %y + ret i32 %C +} + +define i32 @select_xor_icmp_bad_2(i32 %x, i32 %y, i32 %z) { +; CHECK-LABEL: @select_xor_icmp_bad_2( +; CHECK-NEXT: [[A:%.*]] = icmp eq i32 [[X:%.*]], 0 +; CHECK-NEXT: [[B:%.*]] = or i32 [[X]], [[Z:%.*]] +; CHECK-NEXT: [[C:%.*]] = select i1 [[A]], i32 [[B]], i32 [[Y:%.*]] +; CHECK-NEXT: ret i32 [[C]] +; + %A = icmp eq i32 %x, 0 + %B = or i32 %x, %z + %C = select i1 %A, i32 %B, i32 %y + ret i32 %C +} + +define i32 @select_xor_icmp_bad_3(i32 %x, i32 %y, i32 %z, i32 %k) { +; CHECK-LABEL: @select_xor_icmp_bad_3( +; CHECK-NEXT: [[A:%.*]] = icmp eq i32 [[X:%.*]], [[K:%.*]] +; CHECK-NEXT: [[B:%.*]] = xor i32 [[X]], [[Z:%.*]] +; CHECK-NEXT: [[C:%.*]] = select i1 [[A]], i32 [[B]], i32 [[Y:%.*]] +; CHECK-NEXT: ret i32 [[C]] +; + %A = icmp eq i32 %x, %k + %B = xor i32 %x, %z + %C = select i1 %A, i32 %B, i32 %y + ret i32 %C +}