Skip to content

Commit

Permalink
[LVI] Don't compute range on not guaranteed not to be undef condition…
Browse files Browse the repository at this point in the history
… in SelectInst

Fixes:#62901

Reviewed By: nikic

Differential Revision: https://reviews.llvm.org/D151295
  • Loading branch information
luxufan committed May 24, 2023
1 parent 849d01b commit 82082b7
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 15 deletions.
12 changes: 8 additions & 4 deletions llvm/lib/Analysis/LazyValueInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -876,10 +876,14 @@ LazyValueInfoImpl::solveBlockValueSelect(SelectInst *SI, BasicBlock *BB) {
// condition itself? This shows up with idioms like e.g. select(a > 5, a, 5).
// TODO: We could potentially refine an overdefined true value above.
Value *Cond = SI->getCondition();
TrueVal = intersect(TrueVal,
getValueFromCondition(SI->getTrueValue(), Cond, true));
FalseVal = intersect(FalseVal,
getValueFromCondition(SI->getFalseValue(), Cond, false));
// If the value is undef, a different value may be chosen in
// the select condition.
if (isGuaranteedNotToBeUndefOrPoison(Cond, AC)) {
TrueVal = intersect(TrueVal,
getValueFromCondition(SI->getTrueValue(), Cond, true));
FalseVal = intersect(
FalseVal, getValueFromCondition(SI->getFalseValue(), Cond, false));
}

ValueLatticeElement Result = TrueVal;
Result.mergeIn(FalseVal);
Expand Down
18 changes: 9 additions & 9 deletions llvm/test/Transforms/CorrelatedValuePropagation/basic.ll
Original file line number Diff line number Diff line change
Expand Up @@ -735,7 +735,7 @@ define i1 @umin_rhs_overdefined_lhs_range(i32 %a, i32 %b) {
ret i1 %cmp2
}

define i1 @clamp_low1(i32 %a) {
define i1 @clamp_low1(i32 noundef %a) {
; CHECK-LABEL: @clamp_low1(
; CHECK-NEXT: entry:
; CHECK-NEXT: [[CMP:%.*]] = icmp sge i32 [[A:%.*]], 5
Expand All @@ -762,7 +762,7 @@ out:
ret i1 false
}

define i1 @clamp_low2(i32 %a) {
define i1 @clamp_low2(i32 noundef %a) {
; CHECK-LABEL: @clamp_low2(
; CHECK-NEXT: entry:
; CHECK-NEXT: [[CMP:%.*]] = icmp sge i32 [[A:%.*]], 5
Expand All @@ -789,7 +789,7 @@ out:
ret i1 false
}

define i1 @clamp_low3(i32 %a) {
define i1 @clamp_low3(i32 noundef %a) {
; CHECK-LABEL: @clamp_low3(
; CHECK-NEXT: entry:
; CHECK-NEXT: [[CMP:%.*]] = icmp sge i32 [[A:%.*]], 5
Expand All @@ -816,7 +816,7 @@ out:
ret i1 false
}

define i1 @clamp_low4(i32 %a) {
define i1 @clamp_low4(i32 noundef %a) {
; CHECK-LABEL: @clamp_low4(
; CHECK-NEXT: entry:
; CHECK-NEXT: [[CMP:%.*]] = icmp sge i32 [[A:%.*]], 5
Expand All @@ -843,7 +843,7 @@ out:
ret i1 false
}

define i1 @clamp_high1(i32 %a) {
define i1 @clamp_high1(i32 noundef %a) {
; CHECK-LABEL: @clamp_high1(
; CHECK-NEXT: entry:
; CHECK-NEXT: [[CMP:%.*]] = icmp sle i32 [[A:%.*]], 5
Expand All @@ -870,7 +870,7 @@ out:
ret i1 false
}

define i1 @clamp_high2(i32 %a) {
define i1 @clamp_high2(i32 noundef %a) {
; CHECK-LABEL: @clamp_high2(
; CHECK-NEXT: entry:
; CHECK-NEXT: [[CMP:%.*]] = icmp sle i32 [[A:%.*]], 5
Expand All @@ -897,7 +897,7 @@ out:
ret i1 false
}

define i1 @clamp_high3(i32 %a) {
define i1 @clamp_high3(i32 noundef %a) {
; CHECK-LABEL: @clamp_high3(
; CHECK-NEXT: entry:
; CHECK-NEXT: [[CMP:%.*]] = icmp sle i32 [[A:%.*]], 5
Expand All @@ -924,7 +924,7 @@ out:
ret i1 false
}

define i1 @clamp_high4(i32 %a) {
define i1 @clamp_high4(i32 noundef %a) {
; CHECK-LABEL: @clamp_high4(
; CHECK-NEXT: entry:
; CHECK-NEXT: [[CMP:%.*]] = icmp sle i32 [[A:%.*]], 5
Expand Down Expand Up @@ -952,7 +952,7 @@ out:
}

; Just showing arbitrary constants work, not really a clamp
define i1 @not_clamp_high(i32 %a) {
define i1 @not_clamp_high(i32 noundef %a) {
; CHECK-LABEL: @not_clamp_high(
; CHECK-NEXT: entry:
; CHECK-NEXT: [[CMP:%.*]] = icmp sle i32 [[A:%.*]], 5
Expand Down
19 changes: 17 additions & 2 deletions llvm/test/Transforms/CorrelatedValuePropagation/select.ll
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ define i1 @test5(ptr %p, i1 %unknown) {
; CHECK: exit:
; CHECK-NEXT: ret i1 true
;
%pval = load i32, i32* %p
%pval = load i32, i32* %p, !noundef !0
%cmp1 = icmp slt i32 %pval, 255
br i1 %cmp1, label %next, label %exit

Expand All @@ -248,7 +248,7 @@ define i1 @test6(ptr %p, i1 %unknown) {
; CHECK: exit:
; CHECK-NEXT: ret i1 true
;
%pval = load i32, i32* %p
%pval = load i32, i32* %p, !noundef !0
%cmp1 = icmp ult i32 %pval, 255
br i1 %cmp1, label %next, label %exit

Expand All @@ -261,3 +261,18 @@ next:
exit:
ret i1 true
}

define i64 @select_cond_may_undef(i32 %a) {
; CHECK-LABEL: @select_cond_may_undef(
; CHECK-NEXT: [[IS_A_NONNEGATIVE:%.*]] = icmp sgt i32 [[A:%.*]], 1
; CHECK-NEXT: [[NARROW:%.*]] = select i1 [[IS_A_NONNEGATIVE]], i32 [[A]], i32 0
; CHECK-NEXT: [[MAX:%.*]] = sext i32 [[NARROW]] to i64
; CHECK-NEXT: ret i64 [[MAX]]
;
%is_a_nonnegative = icmp sgt i32 %a, 1
%narrow = select i1 %is_a_nonnegative, i32 %a, i32 0
%max = sext i32 %narrow to i64
ret i64 %max
}

!0 = !{}

0 comments on commit 82082b7

Please sign in to comment.