From 299e67291c49cf28cf42e77b2ecdce1485a60ceb Mon Sep 17 00:00:00 2001 From: Sanjoy Das Date: Sun, 30 Oct 2016 23:52:56 +0000 Subject: [PATCH] [SCEV] In CompareValueComplexity, order global values by their name llvm-svn: 285529 --- llvm/lib/Analysis/ScalarEvolution.cpp | 15 +++++++++++ .../Analysis/ScalarEvolutionTest.cpp | 27 ++++++++++++++++++- 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/llvm/lib/Analysis/ScalarEvolution.cpp b/llvm/lib/Analysis/ScalarEvolution.cpp index fd8cec548262..aad57dc8f700 100644 --- a/llvm/lib/Analysis/ScalarEvolution.cpp +++ b/llvm/lib/Analysis/ScalarEvolution.cpp @@ -477,6 +477,21 @@ static int CompareValueComplexity(const LoopInfo *const LI, Value *LV, return (int)LArgNo - (int)RArgNo; } + if (const auto *LGV = dyn_cast(LV)) { + const auto *RGV = cast(RV); + + const auto IsGVNameSemantic = [&](const GlobalValue *GV) { + auto LT = GV->getLinkage(); + return !(GlobalValue::isPrivateLinkage(LT) || + GlobalValue::isInternalLinkage(LT)); + }; + + // Use the names to distinguish the two values, but only if the + // names are semantically important. + if (IsGVNameSemantic(LGV) && IsGVNameSemantic(RGV)) + return LGV->getName().compare(RGV->getName()); + } + // For instructions, compare their loop depth, and their operand count. This // is pretty loose. if (const auto *LInst = dyn_cast(LV)) { diff --git a/llvm/unittests/Analysis/ScalarEvolutionTest.cpp b/llvm/unittests/Analysis/ScalarEvolutionTest.cpp index 46f8794a946a..84524664ac88 100644 --- a/llvm/unittests/Analysis/ScalarEvolutionTest.cpp +++ b/llvm/unittests/Analysis/ScalarEvolutionTest.cpp @@ -346,6 +346,9 @@ TEST_F(ScalarEvolutionsTest, CommutativeExprOperandOrder) { std::unique_ptr M = parseAssemblyString( "target datalayout = \"e-m:e-p:32:32-f64:32:64-f80:32-n8:16:32-S128\" " " " + "@var_0 = external global i32, align 4" + "@var_1 = external global i32, align 4" + " " "define void @f_1(i8* nocapture %arr, i32 %n, i32* %A, i32* %B) " " local_unnamed_addr { " "entry: " @@ -381,7 +384,15 @@ TEST_F(ScalarEvolutionsTest, CommutativeExprOperandOrder) { " %y = load i32, i32* %Y " " %z = load i32, i32* %Z " " ret void " - "} ", + "} " + " " + " " + "define void @f_3() { " + " %x = load i32, i32* @var_0" + " %y = load i32, i32* @var_1" + " ret void" + "} " + , Err, C); assert(M && "Could not parse module?"); @@ -439,6 +450,20 @@ TEST_F(ScalarEvolutionsTest, CommutativeExprOperandOrder) { EXPECT_EQ(Mul3, Mul4); EXPECT_EQ(Mul4, Mul5); } + + { + auto *F = M->getFunction("f_3"); + ASSERT_NE(F, nullptr); + + ScalarEvolution SE = buildSE(*F); + auto *LoadArg0 = SE.getSCEV(getInstructionByName(*F, "x")); + auto *LoadArg1 = SE.getSCEV(getInstructionByName(*F, "y")); + + auto *MulA = SE.getMulExpr(LoadArg0, LoadArg1); + auto *MulB = SE.getMulExpr(LoadArg1, LoadArg0); + + EXPECT_EQ(MulA, MulB) << "MulA = " << *MulA << ", MulB = " << *MulB; + } } } // end anonymous namespace