[llvm] r285529 - [SCEV] In CompareValueComplexity, order global values by their name

Sanjoy Das via llvm-commits llvm-commits at lists.llvm.org
Sun Oct 30 16:52:56 PDT 2016


Author: sanjoy
Date: Sun Oct 30 18:52:56 2016
New Revision: 285529

URL: http://llvm.org/viewvc/llvm-project?rev=285529&view=rev
Log:
[SCEV] In CompareValueComplexity, order global values by their name

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

Modified: llvm/trunk/lib/Analysis/ScalarEvolution.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Analysis/ScalarEvolution.cpp?rev=285529&r1=285528&r2=285529&view=diff
==============================================================================
--- llvm/trunk/lib/Analysis/ScalarEvolution.cpp (original)
+++ llvm/trunk/lib/Analysis/ScalarEvolution.cpp Sun Oct 30 18:52:56 2016
@@ -477,6 +477,21 @@ static int CompareValueComplexity(const
     return (int)LArgNo - (int)RArgNo;
   }
 
+  if (const auto *LGV = dyn_cast<GlobalValue>(LV)) {
+    const auto *RGV = cast<GlobalValue>(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<Instruction>(LV)) {

Modified: llvm/trunk/unittests/Analysis/ScalarEvolutionTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/Analysis/ScalarEvolutionTest.cpp?rev=285529&r1=285528&r2=285529&view=diff
==============================================================================
--- llvm/trunk/unittests/Analysis/ScalarEvolutionTest.cpp (original)
+++ llvm/trunk/unittests/Analysis/ScalarEvolutionTest.cpp Sun Oct 30 18:52:56 2016
@@ -346,6 +346,9 @@ TEST_F(ScalarEvolutionsTest, Commutative
   std::unique_ptr<Module> 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, Commutative
       "  %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, Commutative
     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




More information about the llvm-commits mailing list