Index: lib/Transforms/InstCombine/InstCombineCompares.cpp =================================================================== --- lib/Transforms/InstCombine/InstCombineCompares.cpp +++ lib/Transforms/InstCombine/InstCombineCompares.cpp @@ -3271,6 +3271,39 @@ // bits, if it is a sign bit comparison, it only demands the sign bit. bool UnusedBit; isSignBit = isSignBitCheck(I.getPredicate(), CI, UnusedBit); + + // Canonicalize icmp instructions based on dominating conditions. + BasicBlock *Parent = I.getParent(); + BasicBlock *Dom = Parent->getSinglePredecessor(); + auto *BI = + Dom ? dyn_cast_or_null(Dom->getTerminator()) : nullptr; + ICmpInst::Predicate Pred; + BasicBlock *TrueBB, *FalseBB; + ConstantInt *CI2; + if (BI && match(BI, m_Br(m_ICmp(Pred, m_Specific(Op0), m_ConstantInt(CI2)), + TrueBB, FalseBB))) { + ConstantRange LHSRange = + (Parent == TrueBB) + ? ConstantRange::makeAllowedICmpRegion(Pred, CI2->getValue()) + : ConstantRange::makeAllowedICmpRegion(Pred, CI2->getValue()) + .inverse(); + ConstantRange RHSRange = ConstantRange::makeAllowedICmpRegion( + I.getPredicate(), CI->getValue()); + ConstantRange Intersection = LHSRange.intersectWith(RHSRange); + if (Intersection.isEmptySet()) + return replaceInstUsesWith(I, Builder->getFalse()); + if (!isSignBit && !I.isEquality()) { + if (Intersection.isSingleElement()) { + const APInt *API = Intersection.getSingleElement(); + return new ICmpInst(ICmpInst::ICMP_EQ, Op0, Builder->getInt(*API)); + } + ConstantRange Difference = LHSRange.difference(RHSRange); + if (Difference.isSingleElement()) { + const APInt *APD = Difference.getSingleElement(); + return new ICmpInst(ICmpInst::ICMP_NE, Op0, Builder->getInt(*APD)); + } + } + } } // See if we can fold the comparison based on range information we can get Index: test/Transforms/InstCombine/icmp.ll =================================================================== --- test/Transforms/InstCombine/icmp.ll +++ test/Transforms/InstCombine/icmp.ll @@ -1979,3 +1979,76 @@ ret i1 %cmp } +; CHECK-LABEL: @idom_sign_bit_check_edge_dominates +define void @idom_sign_bit_check_edge_dominates(i64 %a) { +entry: + %cmp = icmp slt i64 %a, 0 + br i1 %cmp, label %land.lhs.true, label %lor.rhs + +land.lhs.true: ; preds = %entry + br label %lor.end + +; CHECK-LABEL: lor.rhs: +; CHECK-NOT: icmp sgt i64 %a, 0 +; CHECK: icmp eq i64 %a, 0 +lor.rhs: ; preds = %entry + %cmp2 = icmp sgt i64 %a, 0 + br i1 %cmp2, label %land.rhs, label %lor.end + +land.rhs: ; preds = %lor.rhs + br label %lor.end + +lor.end: ; preds = %land.rhs, %lor.rhs, %land.lhs.true + ret void +} + +; CHECK-LABEL: @idom_sign_bit_check_edge_not_dominates +define void @idom_sign_bit_check_edge_not_dominates(i64 %a) { +entry: + %cmp = icmp slt i64 %a, 0 + br i1 %cmp, label %land.lhs.true, label %lor.rhs + +land.lhs.true: ; preds = %entry + br i1 undef, label %lor.end, label %lor.rhs + +; CHECK-LABEL: lor.rhs: +; CHECK: icmp sgt i64 %a, 0 +; CHECK-NOT: icmp eq i64 %a, 0 +lor.rhs: ; preds = %land.lhs.true, %entry + %cmp2 = icmp sgt i64 %a, 0 + br i1 %cmp2, label %land.rhs, label %lor.end + +land.rhs: ; preds = %lor.rhs + br label %lor.end + +lor.end: ; preds = %land.rhs, %lor.rhs, %land.lhs.true + ret void +} + +; CHECK-LABEL: @idom_sign_bit_check_edge_dominates_select +define void @idom_sign_bit_check_edge_dominates_select(i64 %a, i64 %b) { +entry: + %cmp = icmp slt i64 %a, 0 + br i1 %cmp, label %land.lhs.true, label %lor.rhs + +land.lhs.true: ; preds = %entry + br label %lor.end + +; CHECK-LABEL: lor.rhs: +; CHECK-NOT: [[B:%.*]] = icmp sgt i64 %a, 0 +; CHECK: [[C:%.*]] = icmp eq i64 %a, %b +; CHECK-NOT: [[D:%.*]] = select i1 [[B]], i64 %a, i64 0 +; CHECK-NOT: icmp ne i64 [[D]], %b +; CHECK-NEXT: br i1 [[C]], label %lor.end, label %land.rhs +lor.rhs: ; preds = %entry + %cmp2 = icmp sgt i64 %a, 0 + %select = select i1 %cmp2, i64 %a, i64 0 + %cmp3 = icmp ne i64 %select, %b + br i1 %cmp3, label %land.rhs, label %lor.end + +land.rhs: ; preds = %lor.rhs + br label %lor.end + +lor.end: ; preds = %land.rhs, %lor.rhs, %land.lhs.true + ret void +}