Skip to content

Commit cb9f1aa

Browse files
committed
[ValueTracking] Implied conditions for lshr
`V1 >> V2 u<= V1` for any V1, V2 This works for lshr and any div's that are changed to lshr's This fixes issues in clang and rustc: llvm#62441 rust-lang/rust#110971 Reviewed By: goldstein.w.n Differential Revision: https://reviews.llvm.org/D151541
1 parent f23b4fa commit cb9f1aa

File tree

2 files changed

+7
-12
lines changed

2 files changed

+7
-12
lines changed

llvm/lib/Analysis/ValueTracking.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -7913,6 +7913,10 @@ static bool isTruePredicate(CmpInst::Predicate Pred, const Value *LHS,
79137913
if (match(RHS, m_NUWAdd(m_Specific(LHS), m_APInt(C))))
79147914
return true;
79157915

7916+
// RHS >> V u<= RHS for any V
7917+
if (match(LHS, m_LShr(m_Specific(RHS), m_Value())))
7918+
return true;
7919+
79167920
// Match A to (X +_{nuw} CA) and B to (X +_{nuw} CB)
79177921
auto MatchNUWAddsToSameValue = [&](const Value *A, const Value *B,
79187922
const Value *&X,

llvm/test/Transforms/InstSimplify/implies.ll

+3-12
Original file line numberDiff line numberDiff line change
@@ -449,9 +449,8 @@ define i1 @test_shift(i64 %x, i64 %y, i64 %shift) {
449449
; CHECK-LABEL: @test_shift(
450450
; CHECK-NEXT: [[LSHR:%.*]] = lshr i64 [[X:%.*]], [[SHIFT:%.*]]
451451
; CHECK-NEXT: [[ICMP1:%.*]] = icmp ugt i64 [[LSHR]], [[Y:%.*]]
452-
; CHECK-NEXT: [[ICMP2:%.*]] = icmp ugt i64 [[X]], [[Y]]
453452
; CHECK-NEXT: call void @llvm.assume(i1 [[ICMP1]])
454-
; CHECK-NEXT: ret i1 [[ICMP2]]
453+
; CHECK-NEXT: ret i1 true
455454
;
456455
%lshr = lshr i64 %x, %shift
457456
%icmp1 = icmp ugt i64 %lshr, %y
@@ -479,11 +478,7 @@ define i1 @assume_x_ugt_y_plus_1(i64 %x, i64 %y) {
479478
; i <u L ==> i >> C <u L
480479
define i1 @lshr_constant(i32 %length.i, i32 %i) {
481480
; CHECK-LABEL: @lshr_constant(
482-
; CHECK-NEXT: [[SHL:%.*]] = lshr i32 [[I:%.*]], 1
483-
; CHECK-NEXT: [[VAR29:%.*]] = icmp ult i32 [[I]], [[LENGTH_I:%.*]]
484-
; CHECK-NEXT: [[VAR30:%.*]] = icmp ult i32 [[SHL]], [[LENGTH_I]]
485-
; CHECK-NEXT: [[RES:%.*]] = icmp ule i1 [[VAR29]], [[VAR30]]
486-
; CHECK-NEXT: ret i1 [[RES]]
481+
; CHECK-NEXT: ret i1 true
487482
;
488483
%shl = lshr i32 %i, 1
489484
%var29 = icmp ult i32 %i, %length.i
@@ -495,11 +490,7 @@ define i1 @lshr_constant(i32 %length.i, i32 %i) {
495490
; i <u L ==> i >> V <u L
496491
define i1 @lshr_value(i32 %length.i, i32 %i, i32 %v) {
497492
; CHECK-LABEL: @lshr_value(
498-
; CHECK-NEXT: [[SHL:%.*]] = lshr i32 [[I:%.*]], [[V:%.*]]
499-
; CHECK-NEXT: [[VAR29:%.*]] = icmp ult i32 [[I]], [[LENGTH_I:%.*]]
500-
; CHECK-NEXT: [[VAR30:%.*]] = icmp ult i32 [[SHL]], [[LENGTH_I]]
501-
; CHECK-NEXT: [[RES:%.*]] = icmp ule i1 [[VAR29]], [[VAR30]]
502-
; CHECK-NEXT: ret i1 [[RES]]
493+
; CHECK-NEXT: ret i1 true
503494
;
504495
%shl = lshr i32 %i, %v
505496
%var29 = icmp ult i32 %i, %length.i

0 commit comments

Comments
 (0)