Skip to content

Commit f11c7ce

Browse files
committed
Use demanded bits simplification
1 parent 67d247a commit f11c7ce

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
@@ -4213,22 +4213,36 @@ Instruction *InstCombinerImpl::visitSelectInst(SelectInst &SI) {
42134213
});
42144214
SimplifyQuery Q = SQ.getWithInstruction(&SI).getWithCondContext(CC);
42154215
if (!CC.AffectedValues.empty()) {
4216-
if (!isa<Constant>(TrueVal) &&
4217-
hasAffectedValue(TrueVal, CC.AffectedValues, /*Depth=*/0)) {
4218-
KnownBits Known = llvm::computeKnownBits(TrueVal, /*Depth=*/0, Q);
4219-
if (Known.isConstant())
4220-
return replaceOperand(SI, 1,
4221-
ConstantInt::get(SelType, Known.getConstant()));
4222-
}
4216+
std::optional<bool> NotUndef;
4217+
auto SimplifyOp = [&](unsigned OpNum) -> Instruction * {
4218+
Value *V = SI.getOperand(OpNum);
4219+
if (isa<Constant>(V) ||
4220+
!hasAffectedValue(V, CC.AffectedValues, /*Depth=*/0))
4221+
return nullptr;
4222+
4223+
if (!NotUndef)
4224+
NotUndef = isGuaranteedNotToBeUndef(CondVal);
42234225

4226+
if (*NotUndef) {
4227+
unsigned BitWidth = SelType->getScalarSizeInBits();
4228+
KnownBits Known(BitWidth);
4229+
if (SimplifyDemandedBits(&SI, OpNum, APInt::getAllOnes(BitWidth),
4230+
Known, /*Depth=*/0, Q))
4231+
return &SI;
4232+
} else {
4233+
KnownBits Known = llvm::computeKnownBits(V, /*Depth=*/0, Q);
4234+
if (Known.isConstant())
4235+
return replaceOperand(
4236+
SI, OpNum, ConstantInt::get(SelType, Known.getConstant()));
4237+
}
4238+
return nullptr;
4239+
};
4240+
4241+
if (Instruction *Res = SimplifyOp(1))
4242+
return Res;
42244243
CC.Invert = true;
4225-
if (!isa<Constant>(FalseVal) &&
4226-
hasAffectedValue(FalseVal, CC.AffectedValues, /*Depth=*/0)) {
4227-
KnownBits Known = llvm::computeKnownBits(FalseVal, /*Depth=*/0, Q);
4228-
if (Known.isConstant())
4229-
return replaceOperand(SI, 2,
4230-
ConstantInt::get(SelType, Known.getConstant()));
4231-
}
4244+
if (Instruction *Res = SimplifyOp(2))
4245+
return Res;
42324246
}
42334247
}
42344248

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)