[llvm] r334425 - [SCEV] Canonicalize "A /u C1 /u C2" to "A /u (C1*C2)".
Tim Shen via llvm-commits
llvm-commits at lists.llvm.org
Mon Jun 11 11:44:59 PDT 2018
Author: timshen
Date: Mon Jun 11 11:44:58 2018
New Revision: 334425
URL: http://llvm.org/viewvc/llvm-project?rev=334425&view=rev
Log:
[SCEV] Canonicalize "A /u C1 /u C2" to "A /u (C1*C2)".
Summary: FWIW InstCombine already folds this. Also avoid the case where C1*C2 overflows.
Reviewers: sunfish, sanjoy
Subscribers: hiraditya, bixia, llvm-commits
Differential Revision: https://reviews.llvm.org/D47965
Modified:
llvm/trunk/lib/Analysis/ScalarEvolution.cpp
llvm/trunk/test/Analysis/ScalarEvolution/fold.ll
Modified: llvm/trunk/lib/Analysis/ScalarEvolution.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Analysis/ScalarEvolution.cpp?rev=334425&r1=334424&r2=334425&view=diff
==============================================================================
--- llvm/trunk/lib/Analysis/ScalarEvolution.cpp (original)
+++ llvm/trunk/lib/Analysis/ScalarEvolution.cpp Mon Jun 11 11:44:58 2018
@@ -3066,6 +3066,21 @@ const SCEV *ScalarEvolution::getUDivExpr
}
}
}
+
+ // (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;
+ APInt NewRHS =
+ DivisorConstant->getAPInt().umul_ov(RHSC->getAPInt(), Overflow);
+ if (Overflow) {
+ return getConstant(RHSC->getType(), 0, false);
+ }
+ return getUDivExpr(OtherDiv->getLHS(), getConstant(NewRHS));
+ }
+ }
+
// (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;
Modified: llvm/trunk/test/Analysis/ScalarEvolution/fold.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Analysis/ScalarEvolution/fold.ll?rev=334425&r1=334424&r2=334425&view=diff
==============================================================================
--- llvm/trunk/test/Analysis/ScalarEvolution/fold.ll (original)
+++ llvm/trunk/test/Analysis/ScalarEvolution/fold.ll Mon Jun 11 11:44:58 2018
@@ -98,3 +98,31 @@ define void @test7(i32 %A) {
; CHECK-NEXT: --> 0
ret void
}
+
+define i64 @test8(i64 %a) {
+; CHECK-LABEL: @test8
+ %t0 = udiv i64 %a, 56
+ %t1 = udiv i64 %t0, 56
+; CHECK: %t1
+; CHECK-NEXT: --> (%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: %t1
+; CHECK-NEXT: --> 0
+ ret i64 %t1
+}
+
+define i64 @test10(i64 %a, i64 %b) {
+; CHECK-LABEL: @test9
+ %t0 = udiv i64 %a, 100000000000000
+ %t1 = udiv i64 %t0, 100000000000000
+ %t2 = mul i64 %b, %t1
+; CHECK: %t2
+; CHECK-NEXT: --> 0
+ ret i64 %t2
+}
More information about the llvm-commits
mailing list