diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp --- a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp @@ -6577,6 +6577,39 @@ if (Instruction *NI = foldSelectICmp(I.getSwappedPredicate(), SI, Op0, I)) return NI; + // In case of a comparison with two select instructions having the same + // condition, check whether one of the resulting branches can be simplified. + // If so, just compare the other branch and select the appropriate result. + // For example: + // %tmp1 = select i1 %cmp, i32 %y, i32 %x + // %tmp2 = select i1 %cmp, i32 %z, i32 %x + // %cmp2 = icmp slt i32 %tmp2, %tmp1 + // The icmp will result false for the false value of selects and the result + // will depend upon the comparison of true values of selects if %cmp is + // true. Thus, transform this into: + // %cmp = icmp slt i32 %y, %z + // %sel = select i1 %cond, i1 %cmp, i1 false + // This handles similar cases to transform. + { + Value *Cond1, *Cond2, *A, *B, *C, *D; + if (match(Op0, m_Select(m_Value(Cond1), m_Value(A), m_Value(B))) && + match(Op1, m_Select(m_Value(Cond2), m_Value(C), m_Value(D))) && + Cond1 == Cond2) { + // Check whether comparison of TrueValues can be simplified + if (Value *Res = simplifyICmpInst(Pred, A, C, SQ)) { + Builder.SetInsertPoint(&I); + Value *NewICMP = Builder.CreateICmp(Pred, B, D); + return SelectInst::Create(Cond1, Res, NewICMP); + } + // Check whether comparison of FalseValues can be simplified + if (Value *Res = simplifyICmpInst(Pred, B, D, SQ)) { + Builder.SetInsertPoint(&I); + Value *NewICMP = Builder.CreateICmp(Pred, A, C); + return SelectInst::Create(Cond1, NewICMP, Res); + } + } + } + // Try to optimize equality comparisons against alloca-based pointers. if (Op0->getType()->isPointerTy() && I.isEquality()) { assert(Op1->getType()->isPointerTy() && "Comparing pointer with non-pointer?"); diff --git a/llvm/test/Transforms/InstCombine/icmp-with-selects.ll b/llvm/test/Transforms/InstCombine/icmp-with-selects.ll new file mode 100644 --- /dev/null +++ b/llvm/test/Transforms/InstCombine/icmp-with-selects.ll @@ -0,0 +1,77 @@ +; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 2 +; RUN: opt < %s -passes=instcombine -S | FileCheck %s + +define i1 @_Z3fooib(i32 noundef %param, i1 noundef zeroext %cond) { +; CHECK-LABEL: define i1 @_Z3fooib +; CHECK-SAME: (i32 noundef [[PARAM:%.*]], i1 noundef zeroext [[COND:%.*]]) { +; CHECK-NEXT: entry: +; CHECK-NEXT: ret i1 false +; +entry: + %cond1 = select i1 %cond, i32 1, i32 %param + %cond6 = select i1 %cond, i32 9, i32 %param + %cmp = icmp slt i32 %cond6, %cond1 + ret i1 %cmp +} + +define i1 @_Z4foo1ib(i32 noundef %param, i1 noundef zeroext %cond) { +; CHECK-LABEL: define i1 @_Z4foo1ib +; CHECK-SAME: (i32 noundef [[PARAM:%.*]], i1 noundef zeroext [[COND:%.*]]) { +; CHECK-NEXT: entry: +; CHECK-NEXT: [[NOT_COND:%.*]] = xor i1 [[COND]], true +; CHECK-NEXT: ret i1 [[NOT_COND]] +; +entry: + %cond1 = select i1 %cond, i32 1, i32 %param + %cond6 = select i1 %cond, i32 9, i32 %param + %cmp = icmp eq i32 %cond6, %cond1 + ret i1 %cmp +} + +define i1 @_Z4foo2iiib(i32 noundef %val1, i32 noundef %val2, i32 noundef %param, i1 noundef zeroext %cond) { +; CHECK-LABEL: define i1 @_Z4foo2iiib +; CHECK-SAME: (i32 noundef [[VAL1:%.*]], i32 noundef [[VAL2:%.*]], i32 noundef [[PARAM:%.*]], i1 noundef zeroext [[COND:%.*]]) { +; CHECK-NEXT: entry: +; CHECK-NEXT: [[TMP0:%.*]] = icmp slt i32 [[VAL2]], [[VAL1]] +; CHECK-NEXT: [[CMP:%.*]] = and i1 [[TMP0]], [[COND]] +; CHECK-NEXT: ret i1 [[CMP]] +; +entry: + %cond1 = select i1 %cond, i32 %val1, i32 %param + %cond6 = select i1 %cond, i32 %val2, i32 %param + %cmp = icmp slt i32 %cond6, %cond1 + ret i1 %cmp +} + +define i1 @_Z4foo3iiib(i32 noundef %val1, i32 noundef %val2, i32 noundef %param, i1 noundef zeroext %cond) { +; CHECK-LABEL: define i1 @_Z4foo3iiib +; CHECK-SAME: (i32 noundef [[VAL1:%.*]], i32 noundef [[VAL2:%.*]], i32 noundef [[PARAM:%.*]], i1 noundef zeroext [[COND:%.*]]) { +; CHECK-NEXT: entry: +; CHECK-NEXT: [[TMP0:%.*]] = icmp sgt i32 [[VAL2]], [[VAL1]] +; CHECK-NEXT: [[NOT_COND:%.*]] = xor i1 [[COND]], true +; CHECK-NEXT: [[CMP:%.*]] = and i1 [[TMP0]], [[NOT_COND]] +; CHECK-NEXT: ret i1 [[CMP]] +; +entry: + %cond1 = select i1 %cond, i32 %param, i32 %val1 + %cond6 = select i1 %cond, i32 %param, i32 %val2 + %cmp = icmp sgt i32 %cond6, %cond1 + ret i1 %cmp +} + +define i1 @_Z4foo4iiib(i32 noundef %val1, i32 noundef %val2, i32 noundef %param, i1 noundef zeroext %cond) { +; CHECK-LABEL: define i1 @_Z4foo4iiib +; CHECK-SAME: (i32 noundef [[VAL1:%.*]], i32 noundef [[VAL2:%.*]], i32 noundef [[PARAM:%.*]], i1 noundef zeroext [[COND:%.*]]) { +; CHECK-NEXT: entry: +; CHECK-NEXT: [[TMP0:%.*]] = icmp eq i32 [[VAL2]], [[VAL1]] +; CHECK-NEXT: [[NOT_COND:%.*]] = xor i1 [[COND]], true +; CHECK-NEXT: [[CMP:%.*]] = or i1 [[TMP0]], [[NOT_COND]] +; CHECK-NEXT: ret i1 [[CMP]] +; +entry: + %cond1 = select i1 %cond, i32 %val1, i32 %param + %cond6 = select i1 %cond, i32 %val2, i32 %param + %cmp = icmp eq i32 %cond6, %cond1 + ret i1 %cmp +} +