diff --git a/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp b/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp --- a/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp @@ -3279,6 +3279,23 @@ NotLHS, NotRHS); } } + + // Pull 'not' into operands of select if both operands are one-use compares. + // Inverting the predicates eliminates the 'not' operation. + // Example: + // not (select ?, (cmp TPred, ?, ?), (cmp FPred, ?, ?) --> + // select ?, (cmp InvTPred, ?, ?), (cmp InvFPred, ?, ?) + // TODO: Canonicalize by hoisting 'not' into an arm of the select if only + // 1 select operand is a cmp? + if (auto *Sel = dyn_cast(Op0)) { + auto *CmpT = dyn_cast(Sel->getTrueValue()); + auto *CmpF = dyn_cast(Sel->getFalseValue()); + if (CmpT && CmpF && CmpT->hasOneUse() && CmpF->hasOneUse()) { + CmpT->setPredicate(CmpT->getInversePredicate()); + CmpF->setPredicate(CmpF->getInversePredicate()); + return replaceInstUsesWith(I, Sel); + } + } } if (Instruction *NewXor = sinkNotIntoXor(I, Builder)) diff --git a/llvm/test/Transforms/InstCombine/not.ll b/llvm/test/Transforms/InstCombine/not.ll --- a/llvm/test/Transforms/InstCombine/not.ll +++ b/llvm/test/Transforms/InstCombine/not.ll @@ -253,11 +253,10 @@ define i1 @not_select_cmp_cmp(i32 %x, i32 %y, float %z, float %w, i1 %cond) { ; CHECK-LABEL: @not_select_cmp_cmp( -; CHECK-NEXT: [[CMPT:%.*]] = icmp sle i32 [[X:%.*]], [[Y:%.*]] -; CHECK-NEXT: [[CMPF:%.*]] = fcmp ugt float [[Z:%.*]], [[W:%.*]] +; CHECK-NEXT: [[CMPT:%.*]] = icmp sgt i32 [[X:%.*]], [[Y:%.*]] +; CHECK-NEXT: [[CMPF:%.*]] = fcmp ole float [[Z:%.*]], [[W:%.*]] ; CHECK-NEXT: [[SEL:%.*]] = select i1 [[COND:%.*]], i1 [[CMPT]], i1 [[CMPF]] -; CHECK-NEXT: [[NOT:%.*]] = xor i1 [[SEL]], true -; CHECK-NEXT: ret i1 [[NOT]] +; CHECK-NEXT: ret i1 [[SEL]] ; %cmpt = icmp sle i32 %x, %y %cmpf = fcmp ugt float %z, %w @@ -268,6 +267,8 @@ declare void @use1(i1) +; TODO: Missed canonicalization - hoist 'not'? + define i1 @not_select_cmp_cmp_extra_use1(i32 %x, i32 %y, float %z, float %w, i1 %cond) { ; CHECK-LABEL: @not_select_cmp_cmp_extra_use1( ; CHECK-NEXT: [[CMPT:%.*]] = icmp sle i32 [[X:%.*]], [[Y:%.*]] @@ -285,6 +286,8 @@ ret i1 %not } +; TODO: Missed canonicalization - hoist 'not'? + define i1 @not_select_cmp_cmp_extra_use2(i32 %x, i32 %y, float %z, float %w, i1 %cond) { ; CHECK-LABEL: @not_select_cmp_cmp_extra_use2( ; CHECK-NEXT: [[CMPT:%.*]] = icmp sle i32 [[X:%.*]], [[Y:%.*]] @@ -302,6 +305,8 @@ ret i1 %not } +; Negative test - extra uses would require more instructions. + define i1 @not_select_cmp_cmp_extra_use3(i32 %x, i32 %y, float %z, float %w, i1 %cond) { ; CHECK-LABEL: @not_select_cmp_cmp_extra_use3( ; CHECK-NEXT: [[CMPT:%.*]] = icmp sle i32 [[X:%.*]], [[Y:%.*]] @@ -321,6 +326,8 @@ ret i1 %not } +; Negative test - extra uses would require more instructions. + define i1 @not_select_cmp_cmp_extra_use4(i32 %x, i32 %y, float %z, float %w, i1 %cond) { ; CHECK-LABEL: @not_select_cmp_cmp_extra_use4( ; CHECK-NEXT: [[CMPT:%.*]] = icmp sle i32 [[X:%.*]], [[Y:%.*]] @@ -338,6 +345,8 @@ ret i1 %not } +; TODO: Missed canonicalization - hoist 'not'? + define i1 @not_select_cmpt(double %x, double %y, i1 %z, i1 %cond) { ; CHECK-LABEL: @not_select_cmpt( ; CHECK-NEXT: [[CMPT:%.*]] = fcmp oeq double [[X:%.*]], [[Y:%.*]] @@ -351,6 +360,8 @@ ret i1 %not } +; TODO: Missed canonicalization - hoist 'not'? + define i1 @not_select_cmpf(i1 %x, i32 %z, i32 %w, i1 %cond) { ; CHECK-LABEL: @not_select_cmpf( ; CHECK-NEXT: [[CMPF:%.*]] = icmp ugt i32 [[Z:%.*]], [[W:%.*]]