Skip to content

Commit 03e2bf9

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 87afc4a commit 03e2bf9

File tree

2 files changed

+28
-9
lines changed

2 files changed

+28
-9
lines changed

llvm/lib/Analysis/ValueTracking.cpp

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1543,6 +1543,29 @@ 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+
1556+
case Instruction::URem: {
1557+
// For URem, the result can never exceed the start value.
1558+
SimplifyQuery RecQ = Q.getWithoutCondContext();
1559+
1560+
unsigned OpNum = P->getOperand(0) == R ? 0 : 1;
1561+
Instruction *RInst = P->getIncomingBlock(OpNum)->getTerminator();
1562+
1563+
RecQ.CxtI = RInst;
1564+
computeKnownBits(R, DemandedElts, Known2, Depth + 1, RecQ);
1565+
Known.Zero.setHighBits(Known2.countMinLeadingZeros());
1566+
break;
1567+
}
1568+
15461569
default:
15471570
break;
15481571
}
@@ -8997,12 +9020,14 @@ bool llvm::matchSimpleRecurrence(const PHINode *P, BinaryOperator *&BO,
89979020
switch (Opcode) {
89989021
default:
89999022
continue;
9000-
// TODO: Expand list -- xor, div, gep, uaddo, etc..
9023+
// TODO: Expand list -- xor, gep, uadd.sat etc.
90019024
case Instruction::LShr:
90029025
case Instruction::AShr:
90039026
case Instruction::Shl:
90049027
case Instruction::Add:
90059028
case Instruction::Sub:
9029+
case Instruction::UDiv:
9030+
case Instruction::URem:
90069031
case Instruction::And:
90079032
case Instruction::Or:
90089033
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)