Skip to content

Commit a49f538

Browse files
committed
[InstCombine] generalize fold for mask-with-signbit-splat, part 2
This removes an over-specified fold. The more general transform was added with: 727e642 There's a difference on an existing test that shows a potentially unnecessary use limit on an icmp fold. That fold is in InstCombinerImpl::foldICmpSubConstant(), and IIRC there was some back-and-forth on it and similar folds because they could cause analysis/passes (SCEV, LSR?) to miss optimizations. Differential Revision: https://reviews.llvm.org/D111410
1 parent cd538a6 commit a49f538

File tree

2 files changed

+3
-13
lines changed

2 files changed

+3
-13
lines changed

llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2061,18 +2061,8 @@ Instruction *InstCombinerImpl::visitAnd(BinaryOperator &I) {
20612061
A->getType()->isIntOrIntVectorTy(1))
20622062
return SelectInst::Create(A, Op0, Constant::getNullValue(Ty));
20632063

2064-
// and(ashr(subNSW(Y, X), ScalarSizeInBits(Y)-1), X) --> X s> Y ? X : 0.
2065-
// TODO: This is a specific case of the more general pattern below, so it
2066-
// should be removed.
2064+
// (iN X s>> (N-1)) & Y --> (X s< 0) ? Y : 0
20672065
unsigned FullShift = Ty->getScalarSizeInBits() - 1;
2068-
if (match(&I, m_c_And(m_OneUse(m_AShr(m_NSWSub(m_Value(Y), m_Value(X)),
2069-
m_SpecificInt(FullShift))),
2070-
m_Deferred(X)))) {
2071-
Value *NewICmpInst = Builder.CreateICmpSGT(X, Y);
2072-
return SelectInst::Create(NewICmpInst, X, ConstantInt::getNullValue(Ty));
2073-
}
2074-
2075-
// (iN X s>> (N-1)) & Y --> (X < 0) ? Y : 0
20762066
if (match(&I, m_c_And(m_OneUse(m_AShr(m_Value(X), m_SpecificInt(FullShift))),
20772067
m_Value(Y)))) {
20782068
Constant *Zero = ConstantInt::getNullValue(Ty);

llvm/test/Transforms/InstCombine/sub-ashr-and-to-icmp-select.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,8 @@ define i32 @sub_ashr_and_i32_extra_use_sub(i32 %x, i32 %y, i32* %p) {
131131
; CHECK-LABEL: @sub_ashr_and_i32_extra_use_sub(
132132
; CHECK-NEXT: [[SUB:%.*]] = sub nsw i32 [[Y:%.*]], [[X:%.*]]
133133
; CHECK-NEXT: store i32 [[SUB]], i32* [[P:%.*]], align 4
134-
; CHECK-NEXT: [[TMP1:%.*]] = icmp slt i32 [[Y]], [[X]]
135-
; CHECK-NEXT: [[AND:%.*]] = select i1 [[TMP1]], i32 [[X]], i32 0
134+
; CHECK-NEXT: [[ISNEG:%.*]] = icmp slt i32 [[SUB]], 0
135+
; CHECK-NEXT: [[AND:%.*]] = select i1 [[ISNEG]], i32 [[X]], i32 0
136136
; CHECK-NEXT: ret i32 [[AND]]
137137
;
138138
%sub = sub nsw i32 %y, %x

0 commit comments

Comments
 (0)