Skip to content

[ValueTracking] Use select condition to help infer bits of arms #84699

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 37 additions & 4 deletions llvm/lib/Analysis/ValueTracking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1023,11 +1023,44 @@ static void computeKnownBitsFromOperator(const Operator *I,
break;
}
case Instruction::Select: {
computeKnownBits(I->getOperand(2), Known, Depth + 1, Q);
computeKnownBits(I->getOperand(1), Known2, Depth + 1, Q);

auto ComputeForArm = [&](Value *Arm, bool Invert) {
KnownBits Res(Known.getBitWidth());
computeKnownBits(Arm, Res, Depth + 1, Q);
// If we have a constant arm, we are done.
if (Res.isConstant())
return Res;

// See what condition implies about the bits of the two select arms.
KnownBits CondRes(Res.getBitWidth());
computeKnownBitsFromCond(Arm, I->getOperand(0), CondRes, Depth + 1, Q,
Invert);
// If we don't get any information from the condition, no reason to
// proceed.
if (CondRes.isUnknown())
return Res;

// We can have conflict if the condition is dead. I.e if we have
// (x | 64) < 32 ? (x | 64) : y
// we will have conflict at bit 6 from the condition/the `or`.
// In that case just return. Its not particularly important
// what we do, as this select is going to be simplified soon.
CondRes = CondRes.unionWith(Res);
if (CondRes.hasConflict())
return Res;

// Finally make sure the information we found is valid. This is relatively
// expensive so its left for the very end.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// expensive so its left for the very end.
// expensive so it's left for the very end.

if (!isGuaranteedNotToBeUndef(Arm, Q.AC, Q.CxtI, Q.DT, Depth + 1))
return Res;

// Finally, we know we get information from the condition and its valid,
// so return it.
return CondRes;
};
// Only known if known in both the LHS and RHS.
Known = Known.intersectWith(Known2);
Known =
ComputeForArm(I->getOperand(1), /*Invert=*/false)
.intersectWith(ComputeForArm(I->getOperand(2), /*Invert=*/true));
break;
}
case Instruction::FPTrunc:
Expand Down
81 changes: 81 additions & 0 deletions llvm/test/Analysis/ValueTracking/knownbits-select-from-cond.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
; RUN: opt -passes=instcombine -S < %s | FileCheck %s

define i8 @select_condition_implies_highbits_op1(i8 %xx, i8 noundef %y) {
; CHECK-LABEL: @select_condition_implies_highbits_op1(
; CHECK-NEXT: [[X:%.*]] = and i8 [[XX:%.*]], 15
; CHECK-NEXT: [[COND:%.*]] = icmp ult i8 [[Y:%.*]], 3
; CHECK-NEXT: [[SEL:%.*]] = select i1 [[COND]], i8 [[Y]], i8 [[X]]
; CHECK-NEXT: [[R:%.*]] = or disjoint i8 [[SEL]], 32
; CHECK-NEXT: ret i8 [[R]]
;
%x = and i8 %xx, 15
%cond = icmp ult i8 %y, 3
%sel = select i1 %cond, i8 %y, i8 %x
%r = add i8 %sel, 32
ret i8 %r
}

define i8 @select_condition_implies_highbits_op1_maybe_undef_fail(i8 %xx, i8 %y) {
; CHECK-LABEL: @select_condition_implies_highbits_op1_maybe_undef_fail(
; CHECK-NEXT: [[X:%.*]] = and i8 [[XX:%.*]], 15
; CHECK-NEXT: [[COND:%.*]] = icmp ult i8 [[Y:%.*]], 3
; CHECK-NEXT: [[SEL:%.*]] = select i1 [[COND]], i8 [[Y]], i8 [[X]]
; CHECK-NEXT: [[R:%.*]] = add i8 [[SEL]], 32
; CHECK-NEXT: ret i8 [[R]]
;
%x = and i8 %xx, 15
%cond = icmp ult i8 %y, 3
%sel = select i1 %cond, i8 %y, i8 %x
%r = add i8 %sel, 32
ret i8 %r
}

define i8 @select_condition_implies_highbits_op2(i8 %xx, i8 noundef %y) {
; CHECK-LABEL: @select_condition_implies_highbits_op2(
; CHECK-NEXT: [[X:%.*]] = and i8 [[XX:%.*]], 15
; CHECK-NEXT: [[COND:%.*]] = icmp ugt i8 [[Y:%.*]], 3
; CHECK-NEXT: [[SEL:%.*]] = select i1 [[COND]], i8 [[X]], i8 [[Y]]
; CHECK-NEXT: [[R:%.*]] = or disjoint i8 [[SEL]], 32
; CHECK-NEXT: ret i8 [[R]]
;
%x = and i8 %xx, 15
%cond = icmp ugt i8 %y, 3
%sel = select i1 %cond, i8 %x, i8 %y
%r = add i8 %sel, 32
ret i8 %r
}

define i8 @select_condition_implies_highbits_op1_and(i8 %xx, i8 noundef %y, i1 %other_cond) {
; CHECK-LABEL: @select_condition_implies_highbits_op1_and(
; CHECK-NEXT: [[X:%.*]] = and i8 [[XX:%.*]], 15
; CHECK-NEXT: [[COND0:%.*]] = icmp ult i8 [[Y:%.*]], 3
; CHECK-NEXT: [[COND:%.*]] = and i1 [[COND0]], [[OTHER_COND:%.*]]
; CHECK-NEXT: [[SEL:%.*]] = select i1 [[COND]], i8 [[Y]], i8 [[X]]
; CHECK-NEXT: [[R:%.*]] = or disjoint i8 [[SEL]], 32
; CHECK-NEXT: ret i8 [[R]]
;
%x = and i8 %xx, 15
%cond0 = icmp ult i8 %y, 3
%cond = and i1 %cond0, %other_cond
%sel = select i1 %cond, i8 %y, i8 %x
%r = add i8 %sel, 32
ret i8 %r
}

define i8 @select_condition_implies_highbits_op2_or(i8 %xx, i8 noundef %y, i1 %other_cond) {
; CHECK-LABEL: @select_condition_implies_highbits_op2_or(
; CHECK-NEXT: [[X:%.*]] = and i8 [[XX:%.*]], 15
; CHECK-NEXT: [[COND0:%.*]] = icmp ugt i8 [[Y:%.*]], 3
; CHECK-NEXT: [[COND:%.*]] = or i1 [[COND0]], [[OTHER_COND:%.*]]
; CHECK-NEXT: [[SEL:%.*]] = select i1 [[COND]], i8 [[X]], i8 [[Y]]
; CHECK-NEXT: [[R:%.*]] = or disjoint i8 [[SEL]], 32
; CHECK-NEXT: ret i8 [[R]]
;
%x = and i8 %xx, 15
%cond0 = icmp ugt i8 %y, 3
%cond = or i1 %cond0, %other_cond
%sel = select i1 %cond, i8 %x, i8 %y
%r = add i8 %sel, 32
ret i8 %r
}