[llvm] r286438 - [SCEVExpander] Hoist unsigned divisons when safe

Sanjoy Das via llvm-commits llvm-commits at lists.llvm.org
Wed Nov 9 23:56:12 PST 2016


Author: sanjoy
Date: Thu Nov 10 01:56:12 2016
New Revision: 286438

URL: http://llvm.org/viewvc/llvm-project?rev=286438&view=rev
Log:
[SCEVExpander] Hoist unsigned divisons when safe

That is, when the divisor is a constant non-zero.

Modified:
    llvm/trunk/lib/Analysis/ScalarEvolutionExpander.cpp
    llvm/trunk/unittests/Analysis/ScalarEvolutionTest.cpp

Modified: llvm/trunk/lib/Analysis/ScalarEvolutionExpander.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Analysis/ScalarEvolutionExpander.cpp?rev=286438&r1=286437&r2=286438&view=diff
==============================================================================
--- llvm/trunk/lib/Analysis/ScalarEvolutionExpander.cpp (original)
+++ llvm/trunk/lib/Analysis/ScalarEvolutionExpander.cpp Thu Nov 10 01:56:12 2016
@@ -198,7 +198,9 @@ Value *SCEVExpander::InsertBinop(Instruc
   DebugLoc Loc = Builder.GetInsertPoint()->getDebugLoc();
   SCEVInsertPointGuard Guard(Builder, this);
 
-  if (Opcode != Instruction::UDiv) {
+  auto *RHSConst = dyn_cast<ConstantInt>(RHS);
+
+  if (Opcode != Instruction::UDiv || (RHSConst && !RHSConst->isZero())) {
     // FIXME: There is alredy similar logic in expandCodeFor, we should see if
     // this is actually needed here.
 

Modified: llvm/trunk/unittests/Analysis/ScalarEvolutionTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/Analysis/ScalarEvolutionTest.cpp?rev=286438&r1=286437&r2=286438&view=diff
==============================================================================
--- llvm/trunk/unittests/Analysis/ScalarEvolutionTest.cpp (original)
+++ llvm/trunk/unittests/Analysis/ScalarEvolutionTest.cpp Thu Nov 10 01:56:12 2016
@@ -538,6 +538,19 @@ TEST_F(ScalarEvolutionsTest, BadHoisting
       ASSERT_NE(DivFromScratchExpansionInst, nullptr);
       EXPECT_EQ(DivInst->getParent(), DivFromScratchExpansionInst->getParent());
     }
+
+    {
+      auto *ArgY = getArgByName(F, "y");
+      auto *SafeDivSCEV =
+          SE.getUDivExpr(SE.getSCEV(ArgY), SE.getConstant(APInt(32, 19)));
+
+      auto *SafeDivExpansion =
+          Expander.expandCodeFor(SafeDivSCEV, SafeDivSCEV->getType(),
+                                 DivInst->getParent()->getTerminator());
+      auto *SafeDivExpansionInst = dyn_cast<Instruction>(SafeDivExpansion);
+      ASSERT_NE(SafeDivExpansionInst, nullptr);
+      EXPECT_EQ("loop.ph", SafeDivExpansionInst->getParent()->getName());
+    }
   });
 }
 




More information about the llvm-commits mailing list