Skip to content

[ValueTracking] Try to infer range of select from true and false values. #68256

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

Merged
merged 3 commits into from
Oct 5, 2023
Merged
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
11 changes: 8 additions & 3 deletions llvm/lib/Analysis/ValueTracking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8841,9 +8841,14 @@ ConstantRange llvm::computeConstantRange(const Value *V, bool ForSigned,
CR = ConstantRange::getNonEmpty(Lower, Upper);
} else if (auto *II = dyn_cast<IntrinsicInst>(V))
CR = getRangeForIntrinsic(*II);
else if (auto *SI = dyn_cast<SelectInst>(V))
CR = getRangeForSelectPattern(*SI, IIQ);
else if (isa<FPToUIInst>(V) || isa<FPToSIInst>(V)) {
else if (auto *SI = dyn_cast<SelectInst>(V)) {
ConstantRange CRTrue = computeConstantRange(
SI->getTrueValue(), ForSigned, UseInstrInfo, AC, CtxI, DT, Depth + 1);
ConstantRange CRFalse = computeConstantRange(
SI->getFalseValue(), ForSigned, UseInstrInfo, AC, CtxI, DT, Depth + 1);
CR = CRTrue.unionWith(CRFalse);
Copy link
Contributor

Choose a reason for hiding this comment

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

code wise looks fine.
Maybe add a todo for (or just implement) trying to use select condition. I.e we have something like:
select (icmp ule x, 10), x, 10
we can bound to [0, 10].
Something like:

const APInt * C;
if(match(SelectCond, m_ICmp(Pred, m_Specific(SelectTrue), m_APInt(C))) {
    CRTrue = CRTrue.intersectWith(ConstantRange::makeExactICmpRegion(Pred), *C);
}
if(match(SelectCond, m_ICmp(Pred, m_Specific(SelectFalse), m_APInt(C))) {
   CRFalse = CRFalse.intersectWith(ConstantRange::makeExactICmpRegion(getInverse(Pred), *C));
}

You could get a bit more sophisticated and do non-constants of just m_APInt(C) and build a CR for the other operand.

Copy link
Contributor Author

@mgudim mgudim Oct 4, 2023

Choose a reason for hiding this comment

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

@goldsteinn I think setLimitsForSelectPattern which is called below does what you describe.

Copy link
Contributor

Choose a reason for hiding this comment

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

not quite, but the logic i'm proposing looks like it belongs better there. I'll make a patch for that.

Copy link
Contributor

Choose a reason for hiding this comment

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

Still think the logic belongs here actually, but feel free to commit w.o adding, I'll likely add a follow up shortly after.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@goldsteinn Maybe I didn't understand what you mean. But I tried the following test without my patch:

define i1 @foo(i32 %x)  {
entry:
  %cmp0_ = icmp ule i32 %x, 10
  %select_ = select i1 %cmp0_, i32 %x, i32 10
  %cmp1_ = icmp ule i32 %select_, 10
  ret i1 %cmp1_
}

This simplifies to:

define i1 @foo(i32 %x) {
entry:
  ret i1 true
}

I put a print statement right after a call to setLimitsForSelectPattern(*SI, Lower, Upper, IIQ), the return range is [0, 11).
Can you please demonstrate what's missing with another test case?

Copy link
Contributor

Choose a reason for hiding this comment

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

@mgudim I believe the case @goldsteinn has in mind is where it's not a min/max pattern. So e.g. if you do icmp ule i32 %x, 20 then you know the result is <= 20, but it's not a min/max pattern.

CR = CR.intersectWith(getRangeForSelectPattern(*SI, IIQ));
} else if (isa<FPToUIInst>(V) || isa<FPToSIInst>(V)) {
APInt Lower = APInt(BitWidth, 0);
APInt Upper = APInt(BitWidth, 0);
// TODO: Return ConstantRange.
Expand Down
14 changes: 13 additions & 1 deletion llvm/test/Analysis/BasicAA/range.ll
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

%struct.S = type { i32, [2 x i32], i32 }
%struct.S2 = type { i32, [4 x i32], [4 x i32] }
@G = global [10 x i32] zeroinitializer, align 4

; CHECK: Function: t1
; CHECK: NoAlias: i32* %gep1, i32* %gep2
Expand Down Expand Up @@ -258,8 +259,19 @@ join:
ret void
}

declare void @llvm.assume(i1)

; CHECK-LABEL: Function: select_in_gep
; CHECK: NoAlias: i32* %arrayidx, i32* getelementptr inbounds ([10 x i32], ptr @G, i64 0, i64 3)
define i32 @select_in_gep(i1 %c) {
entry:
%select_ = select i1 %c, i64 2, i64 1
%arrayidx = getelementptr inbounds [10 x i32], ptr @G, i64 0, i64 %select_
store i32 42, ptr %arrayidx, align 4
%load_ = load i32, ptr getelementptr inbounds ([10 x i32], ptr @G, i64 0, i64 3), align 4
ret i32 %load_
}

declare void @llvm.assume(i1)

!0 = !{ i32 0, i32 2 }
!1 = !{ i32 0, i32 1 }
Expand Down
4 changes: 2 additions & 2 deletions llvm/test/Transforms/InstCombine/binop-select.ll
Original file line number Diff line number Diff line change
Expand Up @@ -324,12 +324,12 @@ define i32 @sub_sel_op1_use(i1 %b) {
; CHECK-LABEL: @sub_sel_op1_use(
; CHECK-NEXT: [[S:%.*]] = select i1 [[B:%.*]], i32 42, i32 41
; CHECK-NEXT: call void @use(i32 [[S]])
; CHECK-NEXT: [[R:%.*]] = sub nsw i32 42, [[S]]
; CHECK-NEXT: [[R:%.*]] = sub nuw nsw i32 42, [[S]]
; CHECK-NEXT: ret i32 [[R]]
;
%s = select i1 %b, i32 42, i32 41
call void @use(i32 %s)
%r = sub nsw i32 42, %s
%r = sub nuw nsw i32 42, %s
ret i32 %r
}

Expand Down