From b2e86a3cab877dd3136faa091779745079c162cd Mon Sep 17 00:00:00 2001 From: Sanjoy Das Date: Thu, 10 Nov 2016 07:56:12 +0000 Subject: [PATCH] [SCEVExpander] Hoist unsigned divisons when safe That is, when the divisor is a constant non-zero. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@286438 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Analysis/ScalarEvolutionExpander.cpp | 4 +++- unittests/Analysis/ScalarEvolutionTest.cpp | 13 +++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/lib/Analysis/ScalarEvolutionExpander.cpp b/lib/Analysis/ScalarEvolutionExpander.cpp index d94782ab899..216a95870a8 100644 --- a/lib/Analysis/ScalarEvolutionExpander.cpp +++ b/lib/Analysis/ScalarEvolutionExpander.cpp @@ -198,7 +198,9 @@ Value *SCEVExpander::InsertBinop(Instruction::BinaryOps Opcode, DebugLoc Loc = Builder.GetInsertPoint()->getDebugLoc(); SCEVInsertPointGuard Guard(Builder, this); - if (Opcode != Instruction::UDiv) { + auto *RHSConst = dyn_cast(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. diff --git a/unittests/Analysis/ScalarEvolutionTest.cpp b/unittests/Analysis/ScalarEvolutionTest.cpp index f9289250419..bc5b12ae892 100644 --- a/unittests/Analysis/ScalarEvolutionTest.cpp +++ b/unittests/Analysis/ScalarEvolutionTest.cpp @@ -538,6 +538,19 @@ TEST_F(ScalarEvolutionsTest, BadHoistingSCEVExpander_PR30942) { 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(SafeDivExpansion); + ASSERT_NE(SafeDivExpansionInst, nullptr); + EXPECT_EQ("loop.ph", SafeDivExpansionInst->getParent()->getName()); + } }); } -- 2.40.0