[llvm] 255a507 - [NFC][InstructionCost] Use InstructionCost in lib/Transforms/IPO/IROutliner.cpp

David Sherwood via llvm-commits llvm-commits at lists.llvm.org
Wed Jan 20 01:13:44 PST 2021


Author: David Sherwood
Date: 2021-01-20T08:33:59Z
New Revision: 255a507716bca63a375f3b8a379ccbbc58cb40da

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

LOG: [NFC][InstructionCost] Use InstructionCost in lib/Transforms/IPO/IROutliner.cpp

In places where we call a TTI.getXXCost() function I have changed
the code to use InstructionCost instead of unsigned. This is in
preparation for later on when we will change the TTI interfaces
to return InstructionCost.

See this patch for the introduction of the type: https://reviews.llvm.org/D91174
See this thread for context: http://lists.llvm.org/pipermail/llvm-dev/2020-November/146408.html

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

Added: 
    

Modified: 
    llvm/include/llvm/Transforms/IPO/IROutliner.h
    llvm/lib/Transforms/IPO/IROutliner.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/Transforms/IPO/IROutliner.h b/llvm/include/llvm/Transforms/IPO/IROutliner.h
index 0346803e9ad7..eefcbe5235c1 100644
--- a/llvm/include/llvm/Transforms/IPO/IROutliner.h
+++ b/llvm/include/llvm/Transforms/IPO/IROutliner.h
@@ -44,6 +44,7 @@
 #include "llvm/Analysis/IRSimilarityIdentifier.h"
 #include "llvm/IR/PassManager.h"
 #include "llvm/IR/ValueMap.h"
+#include "llvm/Support/InstructionCost.h"
 #include "llvm/Transforms/Utils/CodeExtractor.h"
 #include <set>
 
@@ -150,7 +151,7 @@ struct OutlinableRegion {
   ///
   /// \param [in] TTI - The TargetTransformInfo for the parent function.
   /// \returns the code size of the region
-  unsigned getBenefit(TargetTransformInfo &TTI);
+  InstructionCost getBenefit(TargetTransformInfo &TTI);
 };
 
 /// This class is a pass that identifies similarity in a Module, extracts
@@ -214,14 +215,14 @@ class IROutliner {
   /// \param [in] CurrentGroup - The collection of OutlinableRegions to be
   /// analyzed.
   /// \returns the number of outlined instructions across all regions.
-  unsigned findBenefitFromAllRegions(OutlinableGroup &CurrentGroup);
+  InstructionCost findBenefitFromAllRegions(OutlinableGroup &CurrentGroup);
 
   /// Find the number of instructions that will be added by reloading arguments.
   ///
   /// \param [in] CurrentGroup - The collection of OutlinableRegions to be
   /// analyzed.
   /// \returns the number of added reload instructions across all regions.
-  unsigned findCostOutputReloads(OutlinableGroup &CurrentGroup);
+  InstructionCost findCostOutputReloads(OutlinableGroup &CurrentGroup);
 
   /// Find the cost and the benefit of \p CurrentGroup and save it back to
   /// \p CurrentGroup.

diff  --git a/llvm/lib/Transforms/IPO/IROutliner.cpp b/llvm/lib/Transforms/IPO/IROutliner.cpp
index 909e26b9a6e1..4b6a4f3d8fc4 100644
--- a/llvm/lib/Transforms/IPO/IROutliner.cpp
+++ b/llvm/lib/Transforms/IPO/IROutliner.cpp
@@ -86,10 +86,10 @@ struct OutlinableGroup {
 
   /// The number of instructions that will be outlined by extracting \ref
   /// Regions.
-  unsigned Benefit = 0;
+  InstructionCost Benefit = 0;
   /// The number of added instructions needed for the outlining of the \ref
   /// Regions.
-  unsigned Cost = 0;
+  InstructionCost Cost = 0;
 
   /// The argument that needs to be marked with the swifterr attribute.  If not
   /// needed, there is no value.
@@ -243,8 +243,8 @@ constantMatches(Value *V, unsigned GVN,
   return false;
 }
 
-unsigned OutlinableRegion::getBenefit(TargetTransformInfo &TTI) {
-  InstructionCost Benefit(0);
+InstructionCost OutlinableRegion::getBenefit(TargetTransformInfo &TTI) {
+  InstructionCost Benefit = 0;
 
   // Estimate the benefit of outlining a specific sections of the program.  We
   // delegate mostly this task to the TargetTransformInfo so that if the target
@@ -274,7 +274,7 @@ unsigned OutlinableRegion::getBenefit(TargetTransformInfo &TTI) {
     }
   }
 
-  return *Benefit.getValue();
+  return Benefit;
 }
 
 /// Find whether \p Region matches the global value numbering to Constant
@@ -1287,8 +1287,9 @@ void IROutliner::pruneIncompatibleRegions(
   }
 }
 
-unsigned IROutliner::findBenefitFromAllRegions(OutlinableGroup &CurrentGroup) {
-  unsigned RegionBenefit = 0;
+InstructionCost
+IROutliner::findBenefitFromAllRegions(OutlinableGroup &CurrentGroup) {
+  InstructionCost RegionBenefit = 0;
   for (OutlinableRegion *Region : CurrentGroup.Regions) {
     TargetTransformInfo &TTI = getTTI(*Region->StartBB->getParent());
     // We add the number of instructions in the region to the benefit as an
@@ -1301,8 +1302,9 @@ unsigned IROutliner::findBenefitFromAllRegions(OutlinableGroup &CurrentGroup) {
   return RegionBenefit;
 }
 
-unsigned IROutliner::findCostOutputReloads(OutlinableGroup &CurrentGroup) {
-  unsigned OverallCost = 0;
+InstructionCost
+IROutliner::findCostOutputReloads(OutlinableGroup &CurrentGroup) {
+  InstructionCost OverallCost = 0;
   for (OutlinableRegion *Region : CurrentGroup.Regions) {
     TargetTransformInfo &TTI = getTTI(*Region->StartBB->getParent());
 
@@ -1311,7 +1313,7 @@ unsigned IROutliner::findCostOutputReloads(OutlinableGroup &CurrentGroup) {
       Optional<Value *> OV = Region->Candidate->fromGVN(OutputGVN);
       assert(OV.hasValue() && "Could not find value for GVN?");
       Value *V = OV.getValue();
-      unsigned LoadCost =
+      InstructionCost LoadCost =
           TTI.getMemoryOpCost(Instruction::Load, V->getType(), Align(1), 0,
                               TargetTransformInfo::TCK_CodeSize);
 
@@ -1333,10 +1335,10 @@ unsigned IROutliner::findCostOutputReloads(OutlinableGroup &CurrentGroup) {
 /// \param [in] TTI - The TargetTransformInfo used to collect information for
 /// new instruction costs.
 /// \returns the additional cost to handle the outputs.
-static unsigned findCostForOutputBlocks(Module &M,
-                                        OutlinableGroup &CurrentGroup,
-                                        TargetTransformInfo &TTI) {
-  unsigned OutputCost = 0;
+static InstructionCost findCostForOutputBlocks(Module &M,
+                                               OutlinableGroup &CurrentGroup,
+                                               TargetTransformInfo &TTI) {
+  InstructionCost OutputCost = 0;
 
   for (const ArrayRef<unsigned> &OutputUse :
        CurrentGroup.OutputGVNCombinations) {
@@ -1345,7 +1347,7 @@ static unsigned findCostForOutputBlocks(Module &M,
       Optional<Value *> OV = Candidate.fromGVN(GVN);
       assert(OV.hasValue() && "Could not find value for GVN?");
       Value *V = OV.getValue();
-      unsigned StoreCost =
+      InstructionCost StoreCost =
           TTI.getMemoryOpCost(Instruction::Load, V->getType(), Align(1), 0,
                               TargetTransformInfo::TCK_CodeSize);
 
@@ -1358,7 +1360,7 @@ static unsigned findCostForOutputBlocks(Module &M,
       OutputCost += StoreCost;
     }
 
-    unsigned BranchCost =
+    InstructionCost BranchCost =
         TTI.getCFInstrCost(Instruction::Br, TargetTransformInfo::TCK_CodeSize);
     LLVM_DEBUG(dbgs() << "Adding " << BranchCost << " to the current cost for"
                       << " a branch instruction\n");
@@ -1368,15 +1370,15 @@ static unsigned findCostForOutputBlocks(Module &M,
   // If there is more than one output scheme, we must have a comparison and
   // branch for each 
diff erent item in the switch statement.
   if (CurrentGroup.OutputGVNCombinations.size() > 1) {
-    unsigned ComparisonCost = TTI.getCmpSelInstrCost(
+    InstructionCost ComparisonCost = TTI.getCmpSelInstrCost(
         Instruction::ICmp, Type::getInt32Ty(M.getContext()),
         Type::getInt32Ty(M.getContext()), CmpInst::BAD_ICMP_PREDICATE,
         TargetTransformInfo::TCK_CodeSize);
-    unsigned BranchCost =
+    InstructionCost BranchCost =
         TTI.getCFInstrCost(Instruction::Br, TargetTransformInfo::TCK_CodeSize);
 
     unsigned DifferentBlocks = CurrentGroup.OutputGVNCombinations.size();
-    unsigned TotalCost = ComparisonCost * BranchCost * DifferentBlocks;
+    InstructionCost TotalCost = ComparisonCost * BranchCost * DifferentBlocks;
 
     LLVM_DEBUG(dbgs() << "Adding: " << TotalCost
                       << " instructions for each switch case for each 
diff erent"
@@ -1388,15 +1390,16 @@ static unsigned findCostForOutputBlocks(Module &M,
 }
 
 void IROutliner::findCostBenefit(Module &M, OutlinableGroup &CurrentGroup) {
-  unsigned RegionBenefit = findBenefitFromAllRegions(CurrentGroup);
+  InstructionCost RegionBenefit = findBenefitFromAllRegions(CurrentGroup);
   CurrentGroup.Benefit += RegionBenefit;
   LLVM_DEBUG(dbgs() << "Current Benefit: " << CurrentGroup.Benefit << "\n");
 
-  unsigned OutputReloadCost = findCostOutputReloads(CurrentGroup);
+  InstructionCost OutputReloadCost = findCostOutputReloads(CurrentGroup);
   CurrentGroup.Cost += OutputReloadCost;
   LLVM_DEBUG(dbgs() << "Current Cost: " << CurrentGroup.Cost << "\n");
 
-  unsigned AverageRegionBenefit = RegionBenefit / CurrentGroup.Regions.size();
+  InstructionCost AverageRegionBenefit =
+      RegionBenefit / CurrentGroup.Regions.size();
   unsigned OverallArgumentNum = CurrentGroup.ArgumentTypes.size();
   unsigned NumRegions = CurrentGroup.Regions.size();
   TargetTransformInfo &TTI =
@@ -1609,8 +1612,7 @@ unsigned IROutliner::doOutline(Module &M) {
           << ore::NV(std::to_string(CurrentGroup.Regions.size()))
           << " regions due to estimated increase of "
           << ore::NV("InstructionIncrease",
-                     std::to_string(static_cast<int>(CurrentGroup.Cost -
-                                                     CurrentGroup.Benefit)))
+                     CurrentGroup.Cost - CurrentGroup.Benefit)
           << " instructions at locations ";
         interleave(
             CurrentGroup.Regions.begin(), CurrentGroup.Regions.end(),
@@ -1658,8 +1660,7 @@ unsigned IROutliner::doOutline(Module &M) {
       OptimizationRemark R(DEBUG_TYPE, "Outlined", C->front()->Inst);
       R << "outlined " << ore::NV(std::to_string(CurrentGroup.Regions.size()))
         << " regions with decrease of "
-        << ore::NV("Benefit", std::to_string(static_cast<int>(
-                                  CurrentGroup.Benefit - CurrentGroup.Cost)))
+        << ore::NV("Benefit", CurrentGroup.Benefit - CurrentGroup.Cost)
         << " instructions at locations ";
       interleave(
           CurrentGroup.Regions.begin(), CurrentGroup.Regions.end(),


        


More information about the llvm-commits mailing list