Skip to content

Commit 255a507

Browse files
committed
[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
1 parent 3c69ff4 commit 255a507

File tree

2 files changed

+31
-29
lines changed

2 files changed

+31
-29
lines changed

llvm/include/llvm/Transforms/IPO/IROutliner.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
#include "llvm/Analysis/IRSimilarityIdentifier.h"
4545
#include "llvm/IR/PassManager.h"
4646
#include "llvm/IR/ValueMap.h"
47+
#include "llvm/Support/InstructionCost.h"
4748
#include "llvm/Transforms/Utils/CodeExtractor.h"
4849
#include <set>
4950

@@ -150,7 +151,7 @@ struct OutlinableRegion {
150151
///
151152
/// \param [in] TTI - The TargetTransformInfo for the parent function.
152153
/// \returns the code size of the region
153-
unsigned getBenefit(TargetTransformInfo &TTI);
154+
InstructionCost getBenefit(TargetTransformInfo &TTI);
154155
};
155156

156157
/// This class is a pass that identifies similarity in a Module, extracts
@@ -214,14 +215,14 @@ class IROutliner {
214215
/// \param [in] CurrentGroup - The collection of OutlinableRegions to be
215216
/// analyzed.
216217
/// \returns the number of outlined instructions across all regions.
217-
unsigned findBenefitFromAllRegions(OutlinableGroup &CurrentGroup);
218+
InstructionCost findBenefitFromAllRegions(OutlinableGroup &CurrentGroup);
218219

219220
/// Find the number of instructions that will be added by reloading arguments.
220221
///
221222
/// \param [in] CurrentGroup - The collection of OutlinableRegions to be
222223
/// analyzed.
223224
/// \returns the number of added reload instructions across all regions.
224-
unsigned findCostOutputReloads(OutlinableGroup &CurrentGroup);
225+
InstructionCost findCostOutputReloads(OutlinableGroup &CurrentGroup);
225226

226227
/// Find the cost and the benefit of \p CurrentGroup and save it back to
227228
/// \p CurrentGroup.

llvm/lib/Transforms/IPO/IROutliner.cpp

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,10 @@ struct OutlinableGroup {
8686

8787
/// The number of instructions that will be outlined by extracting \ref
8888
/// Regions.
89-
unsigned Benefit = 0;
89+
InstructionCost Benefit = 0;
9090
/// The number of added instructions needed for the outlining of the \ref
9191
/// Regions.
92-
unsigned Cost = 0;
92+
InstructionCost Cost = 0;
9393

9494
/// The argument that needs to be marked with the swifterr attribute. If not
9595
/// needed, there is no value.
@@ -243,8 +243,8 @@ constantMatches(Value *V, unsigned GVN,
243243
return false;
244244
}
245245

246-
unsigned OutlinableRegion::getBenefit(TargetTransformInfo &TTI) {
247-
InstructionCost Benefit(0);
246+
InstructionCost OutlinableRegion::getBenefit(TargetTransformInfo &TTI) {
247+
InstructionCost Benefit = 0;
248248

249249
// Estimate the benefit of outlining a specific sections of the program. We
250250
// delegate mostly this task to the TargetTransformInfo so that if the target
@@ -274,7 +274,7 @@ unsigned OutlinableRegion::getBenefit(TargetTransformInfo &TTI) {
274274
}
275275
}
276276

277-
return *Benefit.getValue();
277+
return Benefit;
278278
}
279279

280280
/// Find whether \p Region matches the global value numbering to Constant
@@ -1287,8 +1287,9 @@ void IROutliner::pruneIncompatibleRegions(
12871287
}
12881288
}
12891289

1290-
unsigned IROutliner::findBenefitFromAllRegions(OutlinableGroup &CurrentGroup) {
1291-
unsigned RegionBenefit = 0;
1290+
InstructionCost
1291+
IROutliner::findBenefitFromAllRegions(OutlinableGroup &CurrentGroup) {
1292+
InstructionCost RegionBenefit = 0;
12921293
for (OutlinableRegion *Region : CurrentGroup.Regions) {
12931294
TargetTransformInfo &TTI = getTTI(*Region->StartBB->getParent());
12941295
// We add the number of instructions in the region to the benefit as an
@@ -1301,8 +1302,9 @@ unsigned IROutliner::findBenefitFromAllRegions(OutlinableGroup &CurrentGroup) {
13011302
return RegionBenefit;
13021303
}
13031304

1304-
unsigned IROutliner::findCostOutputReloads(OutlinableGroup &CurrentGroup) {
1305-
unsigned OverallCost = 0;
1305+
InstructionCost
1306+
IROutliner::findCostOutputReloads(OutlinableGroup &CurrentGroup) {
1307+
InstructionCost OverallCost = 0;
13061308
for (OutlinableRegion *Region : CurrentGroup.Regions) {
13071309
TargetTransformInfo &TTI = getTTI(*Region->StartBB->getParent());
13081310

@@ -1311,7 +1313,7 @@ unsigned IROutliner::findCostOutputReloads(OutlinableGroup &CurrentGroup) {
13111313
Optional<Value *> OV = Region->Candidate->fromGVN(OutputGVN);
13121314
assert(OV.hasValue() && "Could not find value for GVN?");
13131315
Value *V = OV.getValue();
1314-
unsigned LoadCost =
1316+
InstructionCost LoadCost =
13151317
TTI.getMemoryOpCost(Instruction::Load, V->getType(), Align(1), 0,
13161318
TargetTransformInfo::TCK_CodeSize);
13171319

@@ -1333,10 +1335,10 @@ unsigned IROutliner::findCostOutputReloads(OutlinableGroup &CurrentGroup) {
13331335
/// \param [in] TTI - The TargetTransformInfo used to collect information for
13341336
/// new instruction costs.
13351337
/// \returns the additional cost to handle the outputs.
1336-
static unsigned findCostForOutputBlocks(Module &M,
1337-
OutlinableGroup &CurrentGroup,
1338-
TargetTransformInfo &TTI) {
1339-
unsigned OutputCost = 0;
1338+
static InstructionCost findCostForOutputBlocks(Module &M,
1339+
OutlinableGroup &CurrentGroup,
1340+
TargetTransformInfo &TTI) {
1341+
InstructionCost OutputCost = 0;
13401342

13411343
for (const ArrayRef<unsigned> &OutputUse :
13421344
CurrentGroup.OutputGVNCombinations) {
@@ -1345,7 +1347,7 @@ static unsigned findCostForOutputBlocks(Module &M,
13451347
Optional<Value *> OV = Candidate.fromGVN(GVN);
13461348
assert(OV.hasValue() && "Could not find value for GVN?");
13471349
Value *V = OV.getValue();
1348-
unsigned StoreCost =
1350+
InstructionCost StoreCost =
13491351
TTI.getMemoryOpCost(Instruction::Load, V->getType(), Align(1), 0,
13501352
TargetTransformInfo::TCK_CodeSize);
13511353

@@ -1358,7 +1360,7 @@ static unsigned findCostForOutputBlocks(Module &M,
13581360
OutputCost += StoreCost;
13591361
}
13601362

1361-
unsigned BranchCost =
1363+
InstructionCost BranchCost =
13621364
TTI.getCFInstrCost(Instruction::Br, TargetTransformInfo::TCK_CodeSize);
13631365
LLVM_DEBUG(dbgs() << "Adding " << BranchCost << " to the current cost for"
13641366
<< " a branch instruction\n");
@@ -1368,15 +1370,15 @@ static unsigned findCostForOutputBlocks(Module &M,
13681370
// If there is more than one output scheme, we must have a comparison and
13691371
// branch for each different item in the switch statement.
13701372
if (CurrentGroup.OutputGVNCombinations.size() > 1) {
1371-
unsigned ComparisonCost = TTI.getCmpSelInstrCost(
1373+
InstructionCost ComparisonCost = TTI.getCmpSelInstrCost(
13721374
Instruction::ICmp, Type::getInt32Ty(M.getContext()),
13731375
Type::getInt32Ty(M.getContext()), CmpInst::BAD_ICMP_PREDICATE,
13741376
TargetTransformInfo::TCK_CodeSize);
1375-
unsigned BranchCost =
1377+
InstructionCost BranchCost =
13761378
TTI.getCFInstrCost(Instruction::Br, TargetTransformInfo::TCK_CodeSize);
13771379

13781380
unsigned DifferentBlocks = CurrentGroup.OutputGVNCombinations.size();
1379-
unsigned TotalCost = ComparisonCost * BranchCost * DifferentBlocks;
1381+
InstructionCost TotalCost = ComparisonCost * BranchCost * DifferentBlocks;
13801382

13811383
LLVM_DEBUG(dbgs() << "Adding: " << TotalCost
13821384
<< " instructions for each switch case for each different"
@@ -1388,15 +1390,16 @@ static unsigned findCostForOutputBlocks(Module &M,
13881390
}
13891391

13901392
void IROutliner::findCostBenefit(Module &M, OutlinableGroup &CurrentGroup) {
1391-
unsigned RegionBenefit = findBenefitFromAllRegions(CurrentGroup);
1393+
InstructionCost RegionBenefit = findBenefitFromAllRegions(CurrentGroup);
13921394
CurrentGroup.Benefit += RegionBenefit;
13931395
LLVM_DEBUG(dbgs() << "Current Benefit: " << CurrentGroup.Benefit << "\n");
13941396

1395-
unsigned OutputReloadCost = findCostOutputReloads(CurrentGroup);
1397+
InstructionCost OutputReloadCost = findCostOutputReloads(CurrentGroup);
13961398
CurrentGroup.Cost += OutputReloadCost;
13971399
LLVM_DEBUG(dbgs() << "Current Cost: " << CurrentGroup.Cost << "\n");
13981400

1399-
unsigned AverageRegionBenefit = RegionBenefit / CurrentGroup.Regions.size();
1401+
InstructionCost AverageRegionBenefit =
1402+
RegionBenefit / CurrentGroup.Regions.size();
14001403
unsigned OverallArgumentNum = CurrentGroup.ArgumentTypes.size();
14011404
unsigned NumRegions = CurrentGroup.Regions.size();
14021405
TargetTransformInfo &TTI =
@@ -1609,8 +1612,7 @@ unsigned IROutliner::doOutline(Module &M) {
16091612
<< ore::NV(std::to_string(CurrentGroup.Regions.size()))
16101613
<< " regions due to estimated increase of "
16111614
<< ore::NV("InstructionIncrease",
1612-
std::to_string(static_cast<int>(CurrentGroup.Cost -
1613-
CurrentGroup.Benefit)))
1615+
CurrentGroup.Cost - CurrentGroup.Benefit)
16141616
<< " instructions at locations ";
16151617
interleave(
16161618
CurrentGroup.Regions.begin(), CurrentGroup.Regions.end(),
@@ -1658,8 +1660,7 @@ unsigned IROutliner::doOutline(Module &M) {
16581660
OptimizationRemark R(DEBUG_TYPE, "Outlined", C->front()->Inst);
16591661
R << "outlined " << ore::NV(std::to_string(CurrentGroup.Regions.size()))
16601662
<< " regions with decrease of "
1661-
<< ore::NV("Benefit", std::to_string(static_cast<int>(
1662-
CurrentGroup.Benefit - CurrentGroup.Cost)))
1663+
<< ore::NV("Benefit", CurrentGroup.Benefit - CurrentGroup.Cost)
16631664
<< " instructions at locations ";
16641665
interleave(
16651666
CurrentGroup.Regions.begin(), CurrentGroup.Regions.end(),

0 commit comments

Comments
 (0)