[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:47:23 PDT 2018


timshen updated this revision to Diff 150587.
timshen added a comment.

Don't generate the multiply, as we get the result for free anyway (we need to check overflow by using umul_ov).


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;
+          APInt NewRHS =
+              DivisorConstant->getAPInt().umul_ov(RHSC->getAPInt(), Overflow);
+          if (!Overflow) {
+            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;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D47965.150587.patch
Type: text/x-patch
Size: 1560 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180608/102008d3/attachment.bin>


More information about the llvm-commits mailing list