Index: llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp =================================================================== --- llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp +++ 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 *TCmp = dyn_cast(Sel->getTrueValue()); + auto *FCmp = dyn_cast(Sel->getFalseValue()); + if (TCmp && FCmp && TCmp->hasOneUse() && FCmp->hasOneUse()) { + TCmp->setPredicate(TCmp->getInversePredicate()); + FCmp->setPredicate(FCmp->getInversePredicate()); + return replaceInstUsesWith(I, Sel); + } + } } if (Instruction *NewXor = sinkNotIntoXor(I, Builder)) Index: llvm/test/Transforms/InstCombine/not.ll =================================================================== --- llvm/test/Transforms/InstCombine/not.ll +++ 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:%.*]]