Skip to content

Commit 744a23f

Browse files
committed
[ValueTracking] Use select condition to help infer bits of arms
If we have something like `(select (icmp ult x, 8), x, y)`, we can use the `(icmp ult x, 8)` to help compute the knownbits of `x`. Closes #84699
1 parent 882992a commit 744a23f

File tree

2 files changed

+41
-8
lines changed

2 files changed

+41
-8
lines changed

llvm/lib/Analysis/ValueTracking.cpp

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1023,11 +1023,44 @@ static void computeKnownBitsFromOperator(const Operator *I,
10231023
break;
10241024
}
10251025
case Instruction::Select: {
1026-
computeKnownBits(I->getOperand(2), Known, Depth + 1, Q);
1027-
computeKnownBits(I->getOperand(1), Known2, Depth + 1, Q);
1028-
1026+
auto ComputeForArm = [&](Value *Arm, bool Invert) {
1027+
KnownBits Res(Known.getBitWidth());
1028+
computeKnownBits(Arm, Res, Depth + 1, Q);
1029+
// If we have a constant arm, we are done.
1030+
if (Res.isConstant())
1031+
return Res;
1032+
1033+
// See what condition implies about the bits of the two select arms.
1034+
KnownBits CondRes(Res.getBitWidth());
1035+
computeKnownBitsFromCond(Arm, I->getOperand(0), CondRes, Depth + 1, Q,
1036+
Invert);
1037+
// If we don't get any information from the condition, no reason to
1038+
// proceed.
1039+
if (CondRes.isUnknown())
1040+
return Res;
1041+
1042+
// We can have conflict if the condition is dead. I.e if we have
1043+
// (x | 64) < 32 ? (x | 64) : y
1044+
// we will have conflict at bit 6 from the condition/the `or`.
1045+
// In that case just return. Its not particularly important
1046+
// what we do, as this select is going to be simplified soon.
1047+
CondRes = CondRes.unionWith(Res);
1048+
if (CondRes.hasConflict())
1049+
return Res;
1050+
1051+
// Finally make sure the information we found is valid. This is relatively
1052+
// expensive so it's left for the very end.
1053+
if (!isGuaranteedNotToBeUndef(Arm, Q.AC, Q.CxtI, Q.DT, Depth + 1))
1054+
return Res;
1055+
1056+
// Finally, we know we get information from the condition and its valid,
1057+
// so return it.
1058+
return CondRes;
1059+
};
10291060
// Only known if known in both the LHS and RHS.
1030-
Known = Known.intersectWith(Known2);
1061+
Known =
1062+
ComputeForArm(I->getOperand(1), /*Invert=*/false)
1063+
.intersectWith(ComputeForArm(I->getOperand(2), /*Invert=*/true));
10311064
break;
10321065
}
10331066
case Instruction::FPTrunc:

llvm/test/Analysis/ValueTracking/knownbits-select-from-cond.ll

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ define i8 @select_condition_implies_highbits_op1(i8 %xx, i8 noundef %y) {
66
; CHECK-NEXT: [[X:%.*]] = and i8 [[XX:%.*]], 15
77
; CHECK-NEXT: [[COND:%.*]] = icmp ult i8 [[Y:%.*]], 3
88
; CHECK-NEXT: [[SEL:%.*]] = select i1 [[COND]], i8 [[Y]], i8 [[X]]
9-
; CHECK-NEXT: [[R:%.*]] = add i8 [[SEL]], 32
9+
; CHECK-NEXT: [[R:%.*]] = or disjoint i8 [[SEL]], 32
1010
; CHECK-NEXT: ret i8 [[R]]
1111
;
1212
%x = and i8 %xx, 15
@@ -36,7 +36,7 @@ define i8 @select_condition_implies_highbits_op2(i8 %xx, i8 noundef %y) {
3636
; CHECK-NEXT: [[X:%.*]] = and i8 [[XX:%.*]], 15
3737
; CHECK-NEXT: [[COND:%.*]] = icmp ugt i8 [[Y:%.*]], 3
3838
; CHECK-NEXT: [[SEL:%.*]] = select i1 [[COND]], i8 [[X]], i8 [[Y]]
39-
; CHECK-NEXT: [[R:%.*]] = add i8 [[SEL]], 32
39+
; CHECK-NEXT: [[R:%.*]] = or disjoint i8 [[SEL]], 32
4040
; CHECK-NEXT: ret i8 [[R]]
4141
;
4242
%x = and i8 %xx, 15
@@ -52,7 +52,7 @@ define i8 @select_condition_implies_highbits_op1_and(i8 %xx, i8 noundef %y, i1 %
5252
; CHECK-NEXT: [[COND0:%.*]] = icmp ult i8 [[Y:%.*]], 3
5353
; CHECK-NEXT: [[COND:%.*]] = and i1 [[COND0]], [[OTHER_COND:%.*]]
5454
; CHECK-NEXT: [[SEL:%.*]] = select i1 [[COND]], i8 [[Y]], i8 [[X]]
55-
; CHECK-NEXT: [[R:%.*]] = add i8 [[SEL]], 32
55+
; CHECK-NEXT: [[R:%.*]] = or disjoint i8 [[SEL]], 32
5656
; CHECK-NEXT: ret i8 [[R]]
5757
;
5858
%x = and i8 %xx, 15
@@ -69,7 +69,7 @@ define i8 @select_condition_implies_highbits_op2_or(i8 %xx, i8 noundef %y, i1 %o
6969
; CHECK-NEXT: [[COND0:%.*]] = icmp ugt i8 [[Y:%.*]], 3
7070
; CHECK-NEXT: [[COND:%.*]] = or i1 [[COND0]], [[OTHER_COND:%.*]]
7171
; CHECK-NEXT: [[SEL:%.*]] = select i1 [[COND]], i8 [[X]], i8 [[Y]]
72-
; CHECK-NEXT: [[R:%.*]] = add i8 [[SEL]], 32
72+
; CHECK-NEXT: [[R:%.*]] = or disjoint i8 [[SEL]], 32
7373
; CHECK-NEXT: ret i8 [[R]]
7474
;
7575
%x = and i8 %xx, 15

0 commit comments

Comments
 (0)