[PATCH] D47965: [SCEV] Canonicalize "A /u C1 /u C2" to "A /u (C1*C2)".
Tim Shen via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Fri Jun 8 16:36:51 PDT 2018
timshen created this revision.
timshen added reviewers: sunfish, sanjoy.
Herald added subscribers: bixia, hiraditya.
FWIW InstCombine already folds this. Also avoid the case where C1*C2 overflows.
https://reviews.llvm.org/D47965
Files:
llvm/lib/Analysis/ScalarEvolution.cpp
llvm/test/Analysis/ScalarEvolution/fold.ll
Index: llvm/test/Analysis/ScalarEvolution/fold.ll
===================================================================
--- llvm/test/Analysis/ScalarEvolution/fold.ll
+++ llvm/test/Analysis/ScalarEvolution/fold.ll
@@ -98,3 +98,19 @@
; CHECK-NEXT: --> 0
ret void
}
+
+define i64 @test8(i64 %a) {
+; CHECK-LABEL: @test8
+ %t0 = udiv i64 %a, 56
+ %t1 = udiv i64 %t0, 56
+; CHECK: --> (%a /u 3136)
+ ret i64 %t1
+}
+
+define i64 @test9(i64 %a) {
+; CHECK-LABEL: @test9
+ %t0 = udiv i64 %a, 100000000000000
+ %t1 = udiv i64 %t0, 100000000000000
+; CHECK: ((%a /u 100000000000000) /u 100000000000000)
+ ret i64 %t1
+}
Index: llvm/lib/Analysis/ScalarEvolution.cpp
===================================================================
--- llvm/lib/Analysis/ScalarEvolution.cpp
+++ llvm/lib/Analysis/ScalarEvolution.cpp
@@ -3066,6 +3066,19 @@
}
}
}
+
+ // (A/B)/C --> A/(B*C) if safe and B*C can be folded.
+ if (const SCEVUDivExpr *OtherDiv = dyn_cast<SCEVUDivExpr>(LHS)) {
+ if (auto DivisorConstant = dyn_cast<SCEVConstant>(OtherDiv->getRHS())) {
+ bool Overflow = false;
+ DivisorConstant->getAPInt().umul_ov(RHSC->getAPInt(), Overflow);
+ if (!Overflow) {
+ return getUDivExpr(OtherDiv->getLHS(),
+ getMulExpr(DivisorConstant, RHSC));
+ }
+ }
+ }
+
// (A+B)/C --> (A/C + B/C) if safe and A/C and B/C can be folded.
if (const SCEVAddExpr *A = dyn_cast<SCEVAddExpr>(LHS)) {
SmallVector<const SCEV *, 4> Operands;
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D47965.150585.patch
Type: text/x-patch
Size: 1576 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180608/a852e8cd/attachment.bin>
More information about the llvm-commits
mailing list