[llvm] dda3b70 - [ConstantHoisting] stop rematerializing InsertionPt

Nick Desaulniers via llvm-commits llvm-commits at lists.llvm.org
Mon Jul 17 15:01:35 PDT 2023


Author: Nick Desaulniers
Date: 2023-07-17T15:01:29-07:00
New Revision: dda3b70602bcf8e0490fbd38ce8a0a45527a110d

URL: https://github.com/llvm/llvm-project/commit/dda3b70602bcf8e0490fbd38ce8a0a45527a110d
DIFF: https://github.com/llvm/llvm-project/commit/dda3b70602bcf8e0490fbd38ce8a0a45527a110d.diff

LOG: [ConstantHoisting] stop rematerializing InsertionPt

Reading this code, I noticed that we call findMatInsertPt a lot, for the
same inputs. Calculate it once and save the result.

Reviewed By: MaskRay

Differential Revision: https://reviews.llvm.org/D155237

Added: 
    

Modified: 
    llvm/include/llvm/Transforms/Scalar/ConstantHoisting.h
    llvm/lib/Transforms/Scalar/ConstantHoisting.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/Transforms/Scalar/ConstantHoisting.h b/llvm/include/llvm/Transforms/Scalar/ConstantHoisting.h
index 5f2514a5ba6607..fa13ed73d506a4 100644
--- a/llvm/include/llvm/Transforms/Scalar/ConstantHoisting.h
+++ b/llvm/include/llvm/Transforms/Scalar/ConstantHoisting.h
@@ -36,6 +36,7 @@
 #ifndef LLVM_TRANSFORMS_SCALAR_CONSTANTHOISTING_H
 #define LLVM_TRANSFORMS_SCALAR_CONSTANTHOISTING_H
 
+#include "llvm/ADT/ArrayRef.h"
 #include "llvm/ADT/DenseMap.h"
 #include "llvm/ADT/MapVector.h"
 #include "llvm/ADT/PointerUnion.h"
@@ -168,9 +169,13 @@ class ConstantHoistingPass : public PassInfoMixin<ConstantHoistingPass> {
   /// Keep track of cast instructions we already cloned.
   MapVector<Instruction *, Instruction *> ClonedCastMap;
 
+  void collectMatInsertPts(
+      const consthoist::RebasedConstantListType &RebasedConstants,
+      SmallVectorImpl<Instruction *> &MatInsertPts) const;
   Instruction *findMatInsertPt(Instruction *Inst, unsigned Idx = ~0U) const;
   SetVector<Instruction *>
-  findConstantInsertionPoint(const consthoist::ConstantInfo &ConstInfo) const;
+  findConstantInsertionPoint(const consthoist::ConstantInfo &ConstInfo,
+                             const ArrayRef<Instruction *> MatInsertPts) const;
   void collectConstantCandidates(ConstCandMapType &ConstCandMap,
                                  Instruction *Inst, unsigned Idx,
                                  ConstantInt *ConstInt);
@@ -197,9 +202,11 @@ class ConstantHoistingPass : public PassInfoMixin<ConstantHoistingPass> {
   struct UserAdjustment {
     Constant *Offset;
     Type *Ty;
+    Instruction *MatInsertPt;
     const consthoist::ConstantUser User;
-    UserAdjustment(Constant *O, Type *T, consthoist::ConstantUser U)
-        : Offset(O), Ty(T), User(U) {}
+    UserAdjustment(Constant *O, Type *T, Instruction *I,
+                   consthoist::ConstantUser U)
+        : Offset(O), Ty(T), MatInsertPt(I), User(U) {}
   };
   void emitBaseConstants(Instruction *Base, UserAdjustment *Adj);
   // If BaseGV is nullptr, emit Constant Integer base; otherwise emit

diff  --git a/llvm/lib/Transforms/Scalar/ConstantHoisting.cpp b/llvm/lib/Transforms/Scalar/ConstantHoisting.cpp
index c96802b5c1675b..611e64bd097680 100644
--- a/llvm/lib/Transforms/Scalar/ConstantHoisting.cpp
+++ b/llvm/lib/Transforms/Scalar/ConstantHoisting.cpp
@@ -160,6 +160,14 @@ bool ConstantHoistingLegacyPass::runOnFunction(Function &Fn) {
   return MadeChange;
 }
 
+void ConstantHoistingPass::collectMatInsertPts(
+    const RebasedConstantListType &RebasedConstants,
+    SmallVectorImpl<Instruction *> &MatInsertPts) const {
+  for (const RebasedConstantInfo &RCI : RebasedConstants)
+    for (const ConstantUser &U : RCI.Uses)
+      MatInsertPts.emplace_back(findMatInsertPt(U.Inst, U.OpndIdx));
+}
+
 /// Find the constant materialization insertion point.
 Instruction *ConstantHoistingPass::findMatInsertPt(Instruction *Inst,
                                                    unsigned Idx) const {
@@ -307,14 +315,15 @@ static void findBestInsertionSet(DominatorTree &DT, BlockFrequencyInfo &BFI,
 
 /// Find an insertion point that dominates all uses.
 SetVector<Instruction *> ConstantHoistingPass::findConstantInsertionPoint(
-    const ConstantInfo &ConstInfo) const {
+    const ConstantInfo &ConstInfo,
+    const ArrayRef<Instruction *> MatInsertPts) const {
   assert(!ConstInfo.RebasedConstants.empty() && "Invalid constant info entry.");
   // Collect all basic blocks.
   SetVector<BasicBlock *> BBs;
   SetVector<Instruction *> InsertPts;
-  for (auto const &RCI : ConstInfo.RebasedConstants)
-    for (auto const &U : RCI.Uses)
-      BBs.insert(findMatInsertPt(U.Inst, U.OpndIdx)->getParent());
+
+  for (Instruction *MatInsertPt : MatInsertPts)
+    BBs.insert(MatInsertPt->getParent());
 
   if (BBs.count(Entry)) {
     InsertPts.insert(&Entry->front());
@@ -750,20 +759,18 @@ void ConstantHoistingPass::emitBaseConstants(Instruction *Base,
     Adj->Offset = ConstantInt::get(Type::getInt32Ty(*Ctx), 0);
 
   if (Adj->Offset) {
-    Instruction *InsertionPt =
-        findMatInsertPt(Adj->User.Inst, Adj->User.OpndIdx);
     if (Adj->Ty) {
       // Constant being rebased is a ConstantExpr.
       PointerType *Int8PtrTy = Type::getInt8PtrTy(
           *Ctx, cast<PointerType>(Adj->Ty)->getAddressSpace());
-      Base = new BitCastInst(Base, Int8PtrTy, "base_bitcast", InsertionPt);
+      Base = new BitCastInst(Base, Int8PtrTy, "base_bitcast", Adj->MatInsertPt);
       Mat = GetElementPtrInst::Create(Type::getInt8Ty(*Ctx), Base, Adj->Offset,
-                                      "mat_gep", InsertionPt);
-      Mat = new BitCastInst(Mat, Adj->Ty, "mat_bitcast", InsertionPt);
+                                      "mat_gep", Adj->MatInsertPt);
+      Mat = new BitCastInst(Mat, Adj->Ty, "mat_bitcast", Adj->MatInsertPt);
     } else
       // Constant being rebased is a ConstantInt.
       Mat = BinaryOperator::Create(Instruction::Add, Base, Adj->Offset,
-                                   "const_mat", InsertionPt);
+                                   "const_mat", Adj->MatInsertPt);
 
     LLVM_DEBUG(dbgs() << "Materialize constant (" << *Base->getOperand(0)
                       << " + " << *Adj->Offset << ") in BB "
@@ -814,8 +821,7 @@ void ConstantHoistingPass::emitBaseConstants(Instruction *Base,
 
     // Aside from constant GEPs, only constant cast expressions are collected.
     assert(ConstExpr->isCast() && "ConstExpr should be a cast");
-    Instruction *ConstExprInst = ConstExpr->getAsInstruction(
-        findMatInsertPt(Adj->User.Inst, Adj->User.OpndIdx));
+    Instruction *ConstExprInst = ConstExpr->getAsInstruction(Adj->MatInsertPt);
     ConstExprInst->setOperand(0, Mat);
 
     // Use the same debug location as the instruction we are about to update.
@@ -840,8 +846,11 @@ bool ConstantHoistingPass::emitBaseConstants(GlobalVariable *BaseGV) {
   bool MadeChange = false;
   SmallVectorImpl<consthoist::ConstantInfo> &ConstInfoVec =
       BaseGV ? ConstGEPInfoMap[BaseGV] : ConstIntInfoVec;
-  for (auto const &ConstInfo : ConstInfoVec) {
-    SetVector<Instruction *> IPSet = findConstantInsertionPoint(ConstInfo);
+  for (const consthoist::ConstantInfo &ConstInfo : ConstInfoVec) {
+    SmallVector<Instruction *, 4> MatInsertPts;
+    collectMatInsertPts(ConstInfo.RebasedConstants, MatInsertPts);
+    SetVector<Instruction *> IPSet =
+        findConstantInsertionPoint(ConstInfo, MatInsertPts);
     // We can have an empty set if the function contains unreachable blocks.
     if (IPSet.empty())
       continue;
@@ -853,16 +862,17 @@ bool ConstantHoistingPass::emitBaseConstants(GlobalVariable *BaseGV) {
       // First, collect constants depending on this IP of the base.
       UsesNum = 0;
       SmallVector<UserAdjustment, 4> ToBeRebased;
+      unsigned MatCtr = 0;
       for (auto const &RCI : ConstInfo.RebasedConstants) {
         UsesNum += RCI.Uses.size();
         for (auto const &U : RCI.Uses) {
-          BasicBlock *OrigMatInsertBB =
-              findMatInsertPt(U.Inst, U.OpndIdx)->getParent();
+          Instruction *MatInsertPt = MatInsertPts[MatCtr++];
+          BasicBlock *OrigMatInsertBB = MatInsertPt->getParent();
           // If Base constant is to be inserted in multiple places,
           // generate rebase for U using the Base dominating U.
           if (IPSet.size() == 1 ||
               DT->dominates(IP->getParent(), OrigMatInsertBB))
-            ToBeRebased.emplace_back(RCI.Offset, RCI.Ty, U);
+            ToBeRebased.emplace_back(RCI.Offset, RCI.Ty, MatInsertPt, U);
         }
       }
 


        


More information about the llvm-commits mailing list