[llvm] [TTI][TLI]Add isGlobalAddressOffsetFoldable (PR #104549)

Mingming Liu via llvm-commits llvm-commits at lists.llvm.org
Fri Aug 16 14:41:18 PDT 2024


https://github.com/minglotus-6 updated https://github.com/llvm/llvm-project/pull/104549

>From 6b25313da858c6dc3981fa06f3ec6cd3f6e2181d Mon Sep 17 00:00:00 2001
From: mingmingl <mingmingl at google.com>
Date: Thu, 15 Aug 2024 23:33:56 -0700
Subject: [PATCH 1/2] [TTI][TLI]Add isGlobalAddressOffsetFoldable

---
 .../llvm/Analysis/TargetTransformInfo.h        |  6 ++++++
 .../llvm/Analysis/TargetTransformInfoImpl.h    |  2 ++
 llvm/include/llvm/CodeGen/BasicTTIImpl.h       |  4 ++++
 llvm/include/llvm/CodeGen/TargetLowering.h     |  2 ++
 llvm/lib/Analysis/TargetTransformInfo.cpp      |  4 ++++
 .../CodeGen/SelectionDAG/TargetLowering.cpp    | 18 ++----------------
 llvm/lib/CodeGen/TargetLoweringBase.cpp        | 17 +++++++++++++++++
 7 files changed, 37 insertions(+), 16 deletions(-)

diff --git a/llvm/include/llvm/Analysis/TargetTransformInfo.h b/llvm/include/llvm/Analysis/TargetTransformInfo.h
index 38e8b9da213974..474ea495035392 100644
--- a/llvm/include/llvm/Analysis/TargetTransformInfo.h
+++ b/llvm/include/llvm/Analysis/TargetTransformInfo.h
@@ -665,6 +665,8 @@ class TargetTransformInfo {
   void getPeelingPreferences(Loop *L, ScalarEvolution &SE,
                              PeelingPreferences &PP) const;
 
+  bool isGlobalAddressOffsetFoldable(GlobalValue *GV) const;
+
   /// Targets can implement their own combinations for target-specific
   /// intrinsics. This function will be called from the InstCombine pass every
   /// time a target-specific intrinsic is encountered.
@@ -1880,6 +1882,7 @@ class TargetTransformInfo::Concept {
       APInt &UndefElts, APInt &UndefElts2, APInt &UndefElts3,
       std::function<void(Instruction *, unsigned, APInt, APInt &)>
           SimplifyAndSetOp) = 0;
+  virtual bool isGlobalAddressOffsetFoldable(GlobalValue *GV) = 0;
   virtual bool isLegalAddImmediate(int64_t Imm) = 0;
   virtual bool isLegalAddScalableImmediate(int64_t Imm) = 0;
   virtual bool isLegalICmpImmediate(int64_t Imm) = 0;
@@ -2348,6 +2351,9 @@ class TargetTransformInfo::Model final : public TargetTransformInfo::Concept {
         IC, II, DemandedElts, UndefElts, UndefElts2, UndefElts3,
         SimplifyAndSetOp);
   }
+  bool isGlobalAddressOffsetFoldable(GlobalValue *GV) override {
+    return Impl.isGlobalAddressOffsetFoldable(GV);
+  }
   bool isLegalAddImmediate(int64_t Imm) override {
     return Impl.isLegalAddImmediate(Imm);
   }
diff --git a/llvm/include/llvm/Analysis/TargetTransformInfoImpl.h b/llvm/include/llvm/Analysis/TargetTransformInfoImpl.h
index d208a710bb27fd..4cf5ec627026f8 100644
--- a/llvm/include/llvm/Analysis/TargetTransformInfoImpl.h
+++ b/llvm/include/llvm/Analysis/TargetTransformInfoImpl.h
@@ -220,6 +220,8 @@ class TargetTransformInfoImplBase {
   void getPeelingPreferences(Loop *, ScalarEvolution &,
                              TTI::PeelingPreferences &) const {}
 
+  bool isGlobalAddressOffsetFoldable(GlobalValue *GV) const { return false; }
+
   bool isLegalAddImmediate(int64_t Imm) const { return false; }
 
   bool isLegalAddScalableImmediate(int64_t Imm) const { return false; }
diff --git a/llvm/include/llvm/CodeGen/BasicTTIImpl.h b/llvm/include/llvm/CodeGen/BasicTTIImpl.h
index 890c2b8ca36e11..f38ba4ff177c14 100644
--- a/llvm/include/llvm/CodeGen/BasicTTIImpl.h
+++ b/llvm/include/llvm/CodeGen/BasicTTIImpl.h
@@ -350,6 +350,10 @@ class BasicTTIImplBase : public TargetTransformInfoImplCRTPBase<T> {
     return getTLI()->isLegalAddressingMode(DL, AM, Ty, AddrSpace, I);
   }
 
+  bool isGlobalAddressOffsetFoldable(const GlobalValue *GV) {
+    return getTLI()->isGlobalAddressOffsetFoldable(GV);
+  }
+
   int64_t getPreferredLargeGEPBaseOffset(int64_t MinOffset, int64_t MaxOffset) {
     return getTLI()->getPreferredLargeGEPBaseOffset(MinOffset, MaxOffset);
   }
diff --git a/llvm/include/llvm/CodeGen/TargetLowering.h b/llvm/include/llvm/CodeGen/TargetLowering.h
index 9ccdbab008aec8..8a9bd756921a10 100644
--- a/llvm/include/llvm/CodeGen/TargetLowering.h
+++ b/llvm/include/llvm/CodeGen/TargetLowering.h
@@ -1543,6 +1543,8 @@ class TargetLoweringBase {
        getIndexedStoreAction(IdxMode, VT.getSimpleVT()) == Custom);
   }
 
+  virtual bool isGlobalAddressOffsetFoldable(const GlobalValue *GV) const;
+
   /// Return how the indexed load should be treated: either it is legal, needs
   /// to be promoted to a larger size, needs to be expanded to some other code
   /// sequence, or the target has a custom expander for it.
diff --git a/llvm/lib/Analysis/TargetTransformInfo.cpp b/llvm/lib/Analysis/TargetTransformInfo.cpp
index dcde78925bfa98..92af1f64cae32d 100644
--- a/llvm/lib/Analysis/TargetTransformInfo.cpp
+++ b/llvm/lib/Analysis/TargetTransformInfo.cpp
@@ -396,6 +396,10 @@ void TargetTransformInfo::getPeelingPreferences(Loop *L, ScalarEvolution &SE,
   return TTIImpl->getPeelingPreferences(L, SE, PP);
 }
 
+bool TargetTransformInfo::isGlobalAddressOffsetFoldable(GlobalValue *GV) const {
+  return TTIImpl->isGlobalAddressOffsetFoldable(GV);
+}
+
 bool TargetTransformInfo::isLegalAddImmediate(int64_t Imm) const {
   return TTIImpl->isLegalAddImmediate(Imm);
 }
diff --git a/llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp b/llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp
index bef70dcb71f567..d514a6e6cbc3fd 100644
--- a/llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp
@@ -485,22 +485,8 @@ SDValue TargetLowering::expandIndirectJTBranch(const SDLoc &dl, SDValue Value,
   return DAG.getNode(ISD::BRIND, dl, MVT::Other, Chain, Addr);
 }
 
-bool
-TargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
-  const TargetMachine &TM = getTargetMachine();
-  const GlobalValue *GV = GA->getGlobal();
-
-  // If the address is not even local to this DSO we will have to load it from
-  // a got and then add the offset.
-  if (!TM.shouldAssumeDSOLocal(GV))
-    return false;
-
-  // If the code is position independent we will have to add a base register.
-  if (isPositionIndependent())
-    return false;
-
-  // Otherwise we can do it.
-  return true;
+bool TargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
+  return isGlobalAddressOffsetFoldable(GA->getGlobal());
 }
 
 //===----------------------------------------------------------------------===//
diff --git a/llvm/lib/CodeGen/TargetLoweringBase.cpp b/llvm/lib/CodeGen/TargetLoweringBase.cpp
index 6ca9955993d242..9309324b7b288c 100644
--- a/llvm/lib/CodeGen/TargetLoweringBase.cpp
+++ b/llvm/lib/CodeGen/TargetLoweringBase.cpp
@@ -2187,6 +2187,23 @@ int TargetLoweringBase::getSqrtRefinementSteps(EVT VT,
   return getOpRefinementSteps(true, VT, getRecipEstimateForFunc(MF));
 }
 
+bool TargetLoweringBase::isGlobalAddressOffsetFoldable(
+    const GlobalValue *GV) const {
+  const TargetMachine &TM = getTargetMachine();
+
+  // If the address is not even local to this DSO we will have to load it from
+  // a got and then add the offset.
+  if (!TM.shouldAssumeDSOLocal(GV))
+    return false;
+
+  // If the code is position independent we will have to add a base register.
+  if (TM.isPositionIndependent())
+    return false;
+
+  // Otherwise we can do it.
+  return true;
+}
+
 int TargetLoweringBase::getDivRefinementSteps(EVT VT,
                                               MachineFunction &MF) const {
   return getOpRefinementSteps(false, VT, getRecipEstimateForFunc(MF));

>From 8ebf3c22601605486171ee8afd57b13e6ec221c0 Mon Sep 17 00:00:00 2001
From: mingmingl <mingmingl at google.com>
Date: Fri, 16 Aug 2024 14:40:48 -0700
Subject: [PATCH 2/2] Move isGlobalAddressOffsetFoldable from
 TargetLoweringBase to TargetLowering

---
 llvm/include/llvm/CodeGen/TargetLowering.h    |  6 +++-
 .../CodeGen/SelectionDAG/TargetLowering.cpp   | 32 ++++++++++++++++++-
 llvm/lib/CodeGen/TargetLoweringBase.cpp       | 17 ----------
 3 files changed, 36 insertions(+), 19 deletions(-)

diff --git a/llvm/include/llvm/CodeGen/TargetLowering.h b/llvm/include/llvm/CodeGen/TargetLowering.h
index 8a9bd756921a10..410e68d04d9bd7 100644
--- a/llvm/include/llvm/CodeGen/TargetLowering.h
+++ b/llvm/include/llvm/CodeGen/TargetLowering.h
@@ -1543,7 +1543,9 @@ class TargetLoweringBase {
        getIndexedStoreAction(IdxMode, VT.getSimpleVT()) == Custom);
   }
 
-  virtual bool isGlobalAddressOffsetFoldable(const GlobalValue *GV) const;
+  virtual bool isGlobalAddressOffsetFoldable(const GlobalValue *GV) const {
+    return false;
+  }
 
   /// Return how the indexed load should be treated: either it is legal, needs
   /// to be promoted to a larger size, needs to be expanded to some other code
@@ -3862,6 +3864,8 @@ class TargetLowering : public TargetLoweringBase {
   getPICJumpTableRelocBaseExpr(const MachineFunction *MF,
                                unsigned JTI, MCContext &Ctx) const;
 
+  bool isGlobalAddressOffsetFoldable(const GlobalValue *GV) const override;
+
   /// Return true if folding a constant offset with the given GlobalAddress is
   /// legal.  It is frequently not legal in PIC relocation models.
   virtual bool isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const;
diff --git a/llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp b/llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp
index d514a6e6cbc3fd..adb2d5355f59c2 100644
--- a/llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp
@@ -485,8 +485,38 @@ SDValue TargetLowering::expandIndirectJTBranch(const SDLoc &dl, SDValue Value,
   return DAG.getNode(ISD::BRIND, dl, MVT::Other, Chain, Addr);
 }
 
+bool TargetLowering::isGlobalAddressOffsetFoldable(
+    const GlobalValue *GV) const {
+  const TargetMachine &TM = getTargetMachine();
+
+  // If the address is not even local to this DSO we will have to load it from
+  // a got and then add the offset.
+  if (!TM.shouldAssumeDSOLocal(GV))
+    return false;
+
+  // If the code is position independent we will have to add a base register.
+  if (TM.isPositionIndependent())
+    return false;
+
+  // Otherwise we can do it.
+  return true;
+}
+
 bool TargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
-  return isGlobalAddressOffsetFoldable(GA->getGlobal());
+  const TargetMachine &TM = getTargetMachine();
+  const GlobalValue *GV = GA->getGlobal();
+
+  // If the address is not even local to this DSO we will have to load it from
+  // a got and then add the offset.
+  if (!TM.shouldAssumeDSOLocal(GV))
+    return false;
+
+  // If the code is position independent we will have to add a base register.
+  if (isPositionIndependent())
+    return false;
+
+  // Otherwise we can do it.
+  return true;
 }
 
 //===----------------------------------------------------------------------===//
diff --git a/llvm/lib/CodeGen/TargetLoweringBase.cpp b/llvm/lib/CodeGen/TargetLoweringBase.cpp
index 9309324b7b288c..6ca9955993d242 100644
--- a/llvm/lib/CodeGen/TargetLoweringBase.cpp
+++ b/llvm/lib/CodeGen/TargetLoweringBase.cpp
@@ -2187,23 +2187,6 @@ int TargetLoweringBase::getSqrtRefinementSteps(EVT VT,
   return getOpRefinementSteps(true, VT, getRecipEstimateForFunc(MF));
 }
 
-bool TargetLoweringBase::isGlobalAddressOffsetFoldable(
-    const GlobalValue *GV) const {
-  const TargetMachine &TM = getTargetMachine();
-
-  // If the address is not even local to this DSO we will have to load it from
-  // a got and then add the offset.
-  if (!TM.shouldAssumeDSOLocal(GV))
-    return false;
-
-  // If the code is position independent we will have to add a base register.
-  if (TM.isPositionIndependent())
-    return false;
-
-  // Otherwise we can do it.
-  return true;
-}
-
 int TargetLoweringBase::getDivRefinementSteps(EVT VT,
                                               MachineFunction &MF) const {
   return getOpRefinementSteps(false, VT, getRecipEstimateForFunc(MF));



More information about the llvm-commits mailing list