1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-10-19 11:02:59 +02:00

[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
This commit is contained in:
David Sherwood 2021-01-11 16:56:10 +00:00
parent cbb19821c4
commit 0c549cead8
2 changed files with 31 additions and 29 deletions

View File

@ -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 @@ private:
/// \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.

View File

@ -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 different 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 different"
@ -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(),