Skip to content

Commit 7aaf72d

Browse files
committed
Use demanded bits simplification
1 parent 154c8a0 commit 7aaf72d

File tree

2 files changed

+32
-21
lines changed

2 files changed

+32
-21
lines changed

llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4052,22 +4052,36 @@ Instruction *InstCombinerImpl::visitSelectInst(SelectInst &SI) {
40524052
});
40534053
SimplifyQuery Q = SQ.getWithInstruction(&SI).getWithCondContext(CC);
40544054
if (!CC.AffectedValues.empty()) {
4055-
if (!isa<Constant>(TrueVal) &&
4056-
hasAffectedValue(TrueVal, CC.AffectedValues, /*Depth=*/0)) {
4057-
KnownBits Known = llvm::computeKnownBits(TrueVal, /*Depth=*/0, Q);
4058-
if (Known.isConstant())
4059-
return replaceOperand(SI, 1,
4060-
ConstantInt::get(SelType, Known.getConstant()));
4061-
}
4055+
std::optional<bool> NotUndef;
4056+
auto SimplifyOp = [&](unsigned OpNum) -> Instruction * {
4057+
Value *V = SI.getOperand(OpNum);
4058+
if (isa<Constant>(V) ||
4059+
!hasAffectedValue(V, CC.AffectedValues, /*Depth=*/0))
4060+
return nullptr;
4061+
4062+
if (!NotUndef)
4063+
NotUndef = isGuaranteedNotToBeUndef(CondVal);
40624064

4065+
if (*NotUndef) {
4066+
unsigned BitWidth = SelType->getScalarSizeInBits();
4067+
KnownBits Known(BitWidth);
4068+
if (SimplifyDemandedBits(&SI, OpNum, APInt::getAllOnes(BitWidth),
4069+
Known, /*Depth=*/0, Q))
4070+
return &SI;
4071+
} else {
4072+
KnownBits Known = llvm::computeKnownBits(V, /*Depth=*/0, Q);
4073+
if (Known.isConstant())
4074+
return replaceOperand(
4075+
SI, OpNum, ConstantInt::get(SelType, Known.getConstant()));
4076+
}
4077+
return nullptr;
4078+
};
4079+
4080+
if (Instruction *Res = SimplifyOp(1))
4081+
return Res;
40634082
CC.Invert = true;
4064-
if (!isa<Constant>(FalseVal) &&
4065-
hasAffectedValue(FalseVal, CC.AffectedValues, /*Depth=*/0)) {
4066-
KnownBits Known = llvm::computeKnownBits(FalseVal, /*Depth=*/0, Q);
4067-
if (Known.isConstant())
4068-
return replaceOperand(SI, 2,
4069-
ConstantInt::get(SelType, Known.getConstant()));
4070-
}
4083+
if (Instruction *Res = SimplifyOp(2))
4084+
return Res;
40714085
}
40724086
}
40734087

llvm/test/Transforms/InstCombine/select.ll

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2989,9 +2989,8 @@ define i8 @select_replacement_loop3(i32 noundef %x) {
29892989

29902990
define i16 @select_replacement_loop4(i16 noundef %p_12) {
29912991
; CHECK-LABEL: @select_replacement_loop4(
2992-
; CHECK-NEXT: [[AND1:%.*]] = and i16 [[P_12:%.*]], 1
2993-
; CHECK-NEXT: [[CMP21:%.*]] = icmp ult i16 [[P_12]], 2
2994-
; CHECK-NEXT: [[AND3:%.*]] = select i1 [[CMP21]], i16 [[AND1]], i16 0
2992+
; CHECK-NEXT: [[CMP1:%.*]] = icmp ult i16 [[P_12:%.*]], 2
2993+
; CHECK-NEXT: [[AND3:%.*]] = select i1 [[CMP1]], i16 [[P_12]], i16 0
29952994
; CHECK-NEXT: ret i16 [[AND3]]
29962995
;
29972996
%cmp1 = icmp ult i16 %p_12, 2
@@ -4671,8 +4670,7 @@ define i8 @select_knownbits_simplify(i8 noundef %x) {
46714670
; CHECK-LABEL: @select_knownbits_simplify(
46724671
; CHECK-NEXT: [[X_LO:%.*]] = and i8 [[X:%.*]], 1
46734672
; CHECK-NEXT: [[CMP:%.*]] = icmp eq i8 [[X_LO]], 0
4674-
; CHECK-NEXT: [[AND:%.*]] = and i8 [[X]], -2
4675-
; CHECK-NEXT: [[RES:%.*]] = select i1 [[CMP]], i8 [[AND]], i8 0
4673+
; CHECK-NEXT: [[RES:%.*]] = select i1 [[CMP]], i8 [[X]], i8 0
46764674
; CHECK-NEXT: ret i8 [[RES]]
46774675
;
46784676
%x.lo = and i8 %x, 1
@@ -4686,8 +4684,7 @@ define i8 @select_knownbits_simplify_nested(i8 noundef %x) {
46864684
; CHECK-LABEL: @select_knownbits_simplify_nested(
46874685
; CHECK-NEXT: [[X_LO:%.*]] = and i8 [[X:%.*]], 1
46884686
; CHECK-NEXT: [[CMP:%.*]] = icmp eq i8 [[X_LO]], 0
4689-
; CHECK-NEXT: [[AND:%.*]] = and i8 [[X]], -2
4690-
; CHECK-NEXT: [[MUL:%.*]] = mul i8 [[AND]], [[AND]]
4687+
; CHECK-NEXT: [[MUL:%.*]] = mul i8 [[X]], [[X]]
46914688
; CHECK-NEXT: [[RES:%.*]] = select i1 [[CMP]], i8 [[MUL]], i8 0
46924689
; CHECK-NEXT: ret i8 [[RES]]
46934690
;

0 commit comments

Comments
 (0)