Skip to content

Commit 9498f46

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 abe0cd4 commit 9498f46

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
@@ -1542,6 +1542,28 @@ static void computeKnownBitsFromOperator(const Operator *I,
15421542
}
15431543
break;
15441544
}
1545+
1546+
case Instruction::UDiv:
1547+
// For UDiv, the result can never exceed either the numerator, or the
1548+
// start value, whichever is greater. The case where the PHI is not
1549+
// the numerator of the UDiv is already handled by other code.
1550+
if (BO->getOperand(0) != P)
1551+
break;
1552+
[[fallthrough]];
1553+
1554+
case Instruction::URem: {
1555+
// For URem, the result can never exceed the start value.
1556+
SimplifyQuery RecQ = Q.getWithoutCondContext();
1557+
1558+
unsigned OpNum = P->getOperand(0) == R ? 0 : 1;
1559+
Instruction *RInst = P->getIncomingBlock(OpNum)->getTerminator();
1560+
1561+
RecQ.CxtI = RInst;
1562+
computeKnownBits(R, DemandedElts, Known2, Depth + 1, RecQ);
1563+
Known.Zero.setHighBits(Known2.countMinLeadingZeros());
1564+
break;
1565+
}
1566+
15451567
default:
15461568
break;
15471569
}
@@ -9039,12 +9061,14 @@ bool llvm::matchSimpleRecurrence(const PHINode *P, BinaryOperator *&BO,
90399061
switch (Opcode) {
90409062
default:
90419063
continue;
9042-
// TODO: Expand list -- xor, div, gep, uaddo, etc..
9064+
// TODO: Expand list -- xor, gep, uadd.sat etc.
90439065
case Instruction::LShr:
90449066
case Instruction::AShr:
90459067
case Instruction::Shl:
90469068
case Instruction::Add:
90479069
case Instruction::Sub:
9070+
case Instruction::UDiv:
9071+
case Instruction::URem:
90489072
case Instruction::And:
90499073
case Instruction::Or:
90509074
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)