[PATCH] D34598: ScalarEvolution: Add URem support

Alexandre Isoard via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sat Jun 24 11:52:35 PDT 2017


alexandre.isoard created this revision.
Herald added a subscriber: mzolotukhin.

Add URem support to SCEV.

In LLVM IR the following code:

  %r = urem <ty> %t, %b


is equivalent to:

  %q = udiv <ty> %t, %b
  %s = mul <ty> nuw %q, %b
  %r = sub <ty> nuw %t, %q ; (t / b) * b + (t % b) = t


As UDiv, Mul and Sub are already supported by SCEV, URem can be implemented with minimal effort this way.

Note: While SRem and SDiv are also related this way, I don't think SCEV provides SDiv yet.


Repository:
  rL LLVM

https://reviews.llvm.org/D34598

Files:
  lib/Analysis/ScalarEvolution.cpp
  test/Analysis/ScalarEvolution/urem-0.ll


Index: test/Analysis/ScalarEvolution/urem-0.ll
===================================================================
--- /dev/null
+++ test/Analysis/ScalarEvolution/urem-0.ll
@@ -0,0 +1,8 @@
+; RUN: opt < %s -scalar-evolution -analyze | FileCheck %s
+
+; CHECK: -->  ((-27 * (%a /u 27)) + %a)
+
+define i8 @foo(i8 %a) {
+        %t0 = urem i8 %a, 27
+        ret i8 %t0
+}
Index: lib/Analysis/ScalarEvolution.cpp
===================================================================
--- lib/Analysis/ScalarEvolution.cpp
+++ lib/Analysis/ScalarEvolution.cpp
@@ -4095,6 +4095,7 @@
   case Instruction::Sub:
   case Instruction::Mul:
   case Instruction::UDiv:
+  case Instruction::URem:
   case Instruction::And:
   case Instruction::Or:
   case Instruction::AShr:
@@ -5417,6 +5418,13 @@
     }
     case Instruction::UDiv:
       return getUDivExpr(getSCEV(BO->LHS), getSCEV(BO->RHS));
+    case Instruction::URem: {
+      const SCEV *LSCEV = getSCEV(BO->LHS);
+      const SCEV *RSCEV = getSCEV(BO->RHS);
+      const SCEV *UDiv = getUDivExpr(LSCEV, RSCEV);
+      const SCEV *Mult = getMulExpr(UDiv, RSCEV, SCEV::FlagNUW);
+      return getMinusSCEV(LSCEV, Mult, SCEV::FlagNUW);
+    }
     case Instruction::Sub: {
       SCEV::NoWrapFlags Flags = SCEV::FlagAnyWrap;
       if (BO->Op)


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D34598.103848.patch
Type: text/x-patch
Size: 1285 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170624/ffb8ff96/attachment.bin>


More information about the llvm-commits mailing list