Skip to content

Commit a4b5288

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`.
1 parent 1bee754 commit a4b5288

File tree

2 files changed

+24
-8
lines changed

2 files changed

+24
-8
lines changed

llvm/lib/Analysis/ValueTracking.cpp

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1023,11 +1023,27 @@ 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 (isGuaranteedNotToBeUndef(Arm, Q.AC, Q.CxtI, Q.DT, Depth + 1)) {
1030+
// See what condition implies about the bits of the two select arms.
1031+
computeKnownBitsFromCond(Arm, I->getOperand(0), Res, Depth + 1, Q,
1032+
Invert);
1033+
// We can have conflict if the condition is dead. I.e if we have
1034+
// (x | 64) < 32 ? (x | 64) : y
1035+
// we will have conflict at bit 6 from the condition/the `or`.
1036+
// In that case, we just reset. Its not particularly important
1037+
// what we do, as this select is going to be simplified soon.
1038+
if (Res.hasConflict())
1039+
Res.resetAll();
1040+
}
1041+
return Res;
1042+
};
10291043
// Only known if known in both the LHS and RHS.
1030-
Known = Known.intersectWith(Known2);
1044+
Known =
1045+
ComputeForArm(I->getOperand(1), /*Invert=*/false)
1046+
.intersectWith(ComputeForArm(I->getOperand(2), /*Invert=*/true));
10311047
break;
10321048
}
10331049
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)