[llvm] [GISel] Add G_FDIV to CSE (PR #123624)

via llvm-commits llvm-commits at lists.llvm.org
Mon Jan 20 08:21:46 PST 2025


https://github.com/lialan updated https://github.com/llvm/llvm-project/pull/123624

>From ec075b92842003a351c6012b35858a7ed697783d Mon Sep 17 00:00:00 2001
From: Alan Li <me at alanli.org>
Date: Mon, 20 Jan 2025 22:05:08 +0800
Subject: [PATCH 1/2] First commit

---
 llvm/lib/CodeGen/GlobalISel/CSEInfo.cpp       |  1 +
 llvm/unittests/CodeGen/GlobalISel/CSETest.cpp | 32 +++++++++++++++++++
 2 files changed, 33 insertions(+)

diff --git a/llvm/lib/CodeGen/GlobalISel/CSEInfo.cpp b/llvm/lib/CodeGen/GlobalISel/CSEInfo.cpp
index 0ac4a8a0aa910b..4e225cccdec65f 100644
--- a/llvm/lib/CodeGen/GlobalISel/CSEInfo.cpp
+++ b/llvm/lib/CodeGen/GlobalISel/CSEInfo.cpp
@@ -65,6 +65,7 @@ bool CSEConfigFull::shouldCSEOpc(unsigned Opc) {
   case TargetOpcode::G_BUILD_VECTOR:
   case TargetOpcode::G_BUILD_VECTOR_TRUNC:
   case TargetOpcode::G_SEXT_INREG:
+  case TargetOpcode::G_FDIV:
     return true;
   }
   return false;
diff --git a/llvm/unittests/CodeGen/GlobalISel/CSETest.cpp b/llvm/unittests/CodeGen/GlobalISel/CSETest.cpp
index 822707a1f4ed32..4d3ff1ffcc1583 100644
--- a/llvm/unittests/CodeGen/GlobalISel/CSETest.cpp
+++ b/llvm/unittests/CodeGen/GlobalISel/CSETest.cpp
@@ -9,10 +9,36 @@
 #include "GISelMITest.h"
 #include "llvm/CodeGen/GlobalISel/CSEInfo.h"
 #include "llvm/CodeGen/GlobalISel/CSEMIRBuilder.h"
+#include "llvm/CodeGenTypes/LowLevelType.h"
 #include "gtest/gtest.h"
 
 namespace {
 
+TEST_F(AArch64GISelMITest, TestFPCSE) {
+  setUp();
+  if (!TM)
+    GTEST_SKIP();
+  LLT s16{LLT::scalar(16)};
+  LLT s32{LLT::scalar(32)};
+
+  GISelCSEInfo CSEInfo;
+  CSEInfo.setCSEConfig(std::make_unique<CSEConfigFull>());
+  CSEInfo.analyze(*MF);
+  B.setCSEInfo(&CSEInfo);
+  CSEMIRBuilder CSEB(B.getState());
+
+  CSEB.setInsertPt(B.getMBB(), B.getInsertPt());
+
+  // Build some fp constants.
+  auto MIBFP0 = CSEB.buildFConstant(s32, 1.0);
+  auto MIBFP0_1 = CSEB.buildFConstant(s32, 1.0);
+  EXPECT_TRUE(&*MIBFP0 == &*MIBFP0_1);
+  CSEInfo.print();
+
+  // Build some
+
+}
+
 TEST_F(AArch64GISelMITest, TestCSE) {
   setUp();
   if (!TM)
@@ -75,6 +101,11 @@ TEST_F(AArch64GISelMITest, TestCSE) {
   auto MIBUnmerge2 = CSEB.buildUnmerge({s32, s32}, Copies[0]);
   EXPECT_TRUE(&*MIBUnmerge == &*MIBUnmerge2);
 
+  // Check G_FDIV
+  auto MIBFDiv = CSEB.buildFDiv(s32, Copies[0], Copies[1]);
+  auto MIBFDiv2 = CSEB.buildFDiv(s32, Copies[0], Copies[1]);
+  EXPECT_TRUE(&*MIBFDiv == &*MIBFDiv2);
+
   // Check G_BUILD_VECTOR
   Register Reg1 = MRI->createGenericVirtualRegister(s32);
   Register Reg2 = MRI->createGenericVirtualRegister(s32);
@@ -100,6 +131,7 @@ TEST_F(AArch64GISelMITest, TestCSE) {
   auto Undef1 = CSEB.buildUndef(s32);
   EXPECT_EQ(&*Undef0, &*Undef1);
 
+
   // If the observer is installed to the MF, CSE can also
   // track new instructions built without the CSEBuilder and
   // the newly built instructions are available for CSEing next

>From 36db9290adf5f49603b2ffe8e2631bb4659496fa Mon Sep 17 00:00:00 2001
From: Alan Li <me at alanli.org>
Date: Tue, 21 Jan 2025 00:19:42 +0800
Subject: [PATCH 2/2] remote useless code

---
 llvm/unittests/CodeGen/GlobalISel/CSETest.cpp | 25 -------------------
 1 file changed, 25 deletions(-)

diff --git a/llvm/unittests/CodeGen/GlobalISel/CSETest.cpp b/llvm/unittests/CodeGen/GlobalISel/CSETest.cpp
index 4d3ff1ffcc1583..4e2b5a1eb37136 100644
--- a/llvm/unittests/CodeGen/GlobalISel/CSETest.cpp
+++ b/llvm/unittests/CodeGen/GlobalISel/CSETest.cpp
@@ -14,31 +14,6 @@
 
 namespace {
 
-TEST_F(AArch64GISelMITest, TestFPCSE) {
-  setUp();
-  if (!TM)
-    GTEST_SKIP();
-  LLT s16{LLT::scalar(16)};
-  LLT s32{LLT::scalar(32)};
-
-  GISelCSEInfo CSEInfo;
-  CSEInfo.setCSEConfig(std::make_unique<CSEConfigFull>());
-  CSEInfo.analyze(*MF);
-  B.setCSEInfo(&CSEInfo);
-  CSEMIRBuilder CSEB(B.getState());
-
-  CSEB.setInsertPt(B.getMBB(), B.getInsertPt());
-
-  // Build some fp constants.
-  auto MIBFP0 = CSEB.buildFConstant(s32, 1.0);
-  auto MIBFP0_1 = CSEB.buildFConstant(s32, 1.0);
-  EXPECT_TRUE(&*MIBFP0 == &*MIBFP0_1);
-  CSEInfo.print();
-
-  // Build some
-
-}
-
 TEST_F(AArch64GISelMITest, TestCSE) {
   setUp();
   if (!TM)



More information about the llvm-commits mailing list