Skip to content

Commit 5d702a1

Browse files
committed
ValueTracking: simplify udiv/urem recurrences
udiv and urem recurrences have the property that the result can never exceed the start value. Implement a simplification based on this property.
1 parent 4b29d1e commit 5d702a1

File tree

2 files changed

+27
-9
lines changed

2 files changed

+27
-9
lines changed

llvm/lib/Analysis/ValueTracking.cpp

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1543,6 +1543,28 @@ static void computeKnownBitsFromOperator(const Operator *I,
15431543
}
15441544
break;
15451545
}
1546+
1547+
case Instruction::UDiv:
1548+
// For UDiv, the result can never exceed either the numerator, or the
1549+
// start value, whichever is greater. The case where the PHI is not
1550+
// the numerator of the UDiv is already handled by other code.
1551+
if (BO->getOperand(0) != P)
1552+
break;
1553+
[[fallthrough]];
1554+
1555+
case Instruction::URem: {
1556+
// For URem, the result can never exceed the start value.
1557+
SimplifyQuery RecQ = Q.getWithoutCondContext();
1558+
1559+
unsigned OpNum = P->getOperand(0) == R ? 0 : 1;
1560+
Instruction *RInst = P->getIncomingBlock(OpNum)->getTerminator();
1561+
1562+
RecQ.CxtI = RInst;
1563+
computeKnownBits(R, DemandedElts, Known2, Depth + 1, RecQ);
1564+
Known.Zero.setHighBits(Known2.countMinLeadingZeros());
1565+
break;
1566+
}
1567+
15461568
default:
15471569
break;
15481570
}
@@ -9042,12 +9064,14 @@ bool llvm::matchSimpleRecurrence(const PHINode *P, BinaryOperator *&BO,
90429064
switch (Opcode) {
90439065
default:
90449066
continue;
9045-
// TODO: Expand list -- xor, div, gep, uaddo, etc..
9067+
// TODO: Expand list -- xor, gep, uadd.sat etc.
90469068
case Instruction::LShr:
90479069
case Instruction::AShr:
90489070
case Instruction::Shl:
90499071
case Instruction::Add:
90509072
case Instruction::Sub:
9073+
case Instruction::UDiv:
9074+
case Instruction::URem:
90519075
case Instruction::And:
90529076
case Instruction::Or:
90539077
case Instruction::Mul:

llvm/test/Analysis/ValueTracking/recurrence-knownbits.ll

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,9 @@ define i64 @test_udiv(i1 %c) {
8686
; CHECK-NEXT: entry:
8787
; CHECK-NEXT: br label [[LOOP:%.*]]
8888
; CHECK: loop:
89-
; CHECK-NEXT: [[IV:%.*]] = phi i64 [ 9, [[ENTRY:%.*]] ], [ [[IV_NEXT:%.*]], [[LOOP]] ]
90-
; CHECK-NEXT: [[IV_NEXT]] = udiv i64 [[IV]], 3
9189
; CHECK-NEXT: br i1 [[C:%.*]], label [[EXIT:%.*]], label [[LOOP]]
9290
; CHECK: exit:
93-
; CHECK-NEXT: [[RES:%.*]] = and i64 [[IV]], 16
94-
; CHECK-NEXT: ret i64 [[RES]]
91+
; CHECK-NEXT: ret i64 0
9592
;
9693
entry:
9794
br label %loop
@@ -132,12 +129,9 @@ define i64 @test_urem(i1 %c) {
132129
; CHECK-NEXT: entry:
133130
; CHECK-NEXT: br label [[LOOP:%.*]]
134131
; CHECK: loop:
135-
; CHECK-NEXT: [[IV:%.*]] = phi i64 [ 3, [[ENTRY:%.*]] ], [ [[IV_NEXT:%.*]], [[LOOP]] ]
136-
; CHECK-NEXT: [[IV_NEXT]] = urem i64 9, [[IV]]
137132
; CHECK-NEXT: br i1 [[C:%.*]], label [[EXIT:%.*]], label [[LOOP]]
138133
; CHECK: exit:
139-
; CHECK-NEXT: [[RES:%.*]] = and i64 [[IV]], 4
140-
; CHECK-NEXT: ret i64 [[RES]]
134+
; CHECK-NEXT: ret i64 0
141135
;
142136
entry:
143137
br label %loop

0 commit comments

Comments
 (0)