[PATCH] D155960: [NaryReassociate] Use new access type aware getGEPCost

Luke Lau via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Jul 21 08:00:54 PDT 2023


luke created this revision.
luke added reviewers: nikic, ebrevnov, krzysz00.
Herald added subscribers: asb, pmatos, StephenFan, hiraditya.
Herald added a project: All.
luke requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

This was originally split out from D149889 <https://reviews.llvm.org/D149889>: Now that getGEPCost can more
accurately determine when an GEP will be folded, this patch updates
NaryReassociate to take advantage of it.
Unfortunately I wasn't able to create a test case for this where the GEP was
both foldable and reassociatable. Actually I couldn't find any instances of
this branch being taken since most targets can't fold more than a reg+reg add.
But posting this patch anyway for completeness sake.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D155960

Files:
  llvm/include/llvm/Analysis/TargetTransformInfo.h
  llvm/lib/Analysis/TargetTransformInfo.cpp
  llvm/lib/Transforms/Scalar/NaryReassociate.cpp


Index: llvm/lib/Transforms/Scalar/NaryReassociate.cpp
===================================================================
--- llvm/lib/Transforms/Scalar/NaryReassociate.cpp
+++ llvm/lib/Transforms/Scalar/NaryReassociate.cpp
@@ -325,16 +325,9 @@
   return nullptr;
 }
 
-static bool isGEPFoldable(GetElementPtrInst *GEP,
-                          const TargetTransformInfo *TTI) {
-  SmallVector<const Value *, 4> Indices(GEP->indices());
-  return TTI->getGEPCost(GEP->getSourceElementType(), GEP->getPointerOperand(),
-                         Indices) == TargetTransformInfo::TCC_Free;
-}
-
 Instruction *NaryReassociatePass::tryReassociateGEP(GetElementPtrInst *GEP) {
   // Not worth reassociating GEP if it is foldable.
-  if (isGEPFoldable(GEP, TTI))
+  if (TTI->getGEPCost(GEP) == TTI::TCC_Free)
     return nullptr;
 
   gep_type_iterator GTI = gep_type_begin(*GEP);
Index: llvm/lib/Analysis/TargetTransformInfo.cpp
===================================================================
--- llvm/lib/Analysis/TargetTransformInfo.cpp
+++ llvm/lib/Analysis/TargetTransformInfo.cpp
@@ -232,6 +232,20 @@
   return TTIImpl->getGEPCost(PointeeType, Ptr, Operands, AccessType, CostKind);
 }
 
+InstructionCost
+TargetTransformInfo::getGEPCost(const GetElementPtrInst *GEP,
+                                TTI::TargetCostKind CostKind) const {
+  Type *AccessType = nullptr;
+  if (!GEP->user_empty()) {
+    // Only take into account the first user as a rough approximation to avoid
+    // O(N) complexity.
+    AccessType = GEP->user_back()->getAccessType();
+  }
+  SmallVector<const Value *> Ops(GEP->indices());
+  return getGEPCost(GEP->getSourceElementType(), GEP->getPointerOperand(), Ops,
+                    AccessType, CostKind);
+}
+
 InstructionCost TargetTransformInfo::getPointersChainCost(
     ArrayRef<const Value *> Ptrs, const Value *Base,
     const TTI::PointersChainInfo &Info, Type *AccessTy,
Index: llvm/include/llvm/Analysis/TargetTransformInfo.h
===================================================================
--- llvm/include/llvm/Analysis/TargetTransformInfo.h
+++ llvm/include/llvm/Analysis/TargetTransformInfo.h
@@ -23,7 +23,7 @@
 
 #include "llvm/ADT/SmallBitVector.h"
 #include "llvm/IR/FMF.h"
-#include "llvm/IR/InstrTypes.h"
+#include "llvm/IR/Instructions.h"
 #include "llvm/IR/PassManager.h"
 #include "llvm/Pass.h"
 #include "llvm/Support/AtomicOrdering.h"
@@ -299,6 +299,12 @@
              ArrayRef<const Value *> Operands, Type *AccessType = nullptr,
              TargetCostKind CostKind = TCK_SizeAndLatency) const;
 
+  // Helper function for using getGEPCost on an already materialized GEP
+  // instruction. Uses the first user as an approximation for AccessType.
+  InstructionCost
+  getGEPCost(const GetElementPtrInst *GEP,
+             TargetCostKind CostKind = TCK_SizeAndLatency) const;
+
   /// Describe known properties for a set of pointers.
   struct PointersChainInfo {
     /// All the GEPs in a set have same base address.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D155960.542919.patch
Type: text/x-patch
Size: 2988 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230721/2d14f086/attachment.bin>


More information about the llvm-commits mailing list