Skip to content

Commit dfda689

Browse files
committed
Check hasOptSize() in shouldOptimizeForSize()
1 parent dca43a1 commit dfda689

20 files changed

+38
-62
lines changed

llvm/lib/CodeGen/BranchFolding.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -645,11 +645,8 @@ ProfitableToMerge(MachineBasicBlock *MBB1, MachineBasicBlock *MBB2,
645645
// we don't have to split a block. At worst we will be introducing 1 new
646646
// branch instruction, which is likely to be smaller than the 2
647647
// instructions that would be deleted in the merge.
648-
MachineFunction *MF = MBB1->getParent();
649-
bool OptForSize =
650-
MF->getFunction().hasOptSize() ||
651-
(llvm::shouldOptimizeForSize(MBB1, PSI, &MBBFreqInfo) &&
652-
llvm::shouldOptimizeForSize(MBB2, PSI, &MBBFreqInfo));
648+
bool OptForSize = llvm::shouldOptimizeForSize(MBB1, PSI, &MBBFreqInfo) &&
649+
llvm::shouldOptimizeForSize(MBB2, PSI, &MBBFreqInfo);
653650
return EffectiveTailLen >= 2 && OptForSize &&
654651
(FullBlockTail1 || FullBlockTail2);
655652
}

llvm/lib/CodeGen/CodeGenPrepare.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -612,7 +612,6 @@ bool CodeGenPrepare::_run(Function &F) {
612612
// bypassSlowDivision may create new BBs, but we don't want to reapply the
613613
// optimization to those blocks.
614614
BasicBlock *Next = BB->getNextNode();
615-
// F.hasOptSize is already checked in the outer if statement.
616615
if (!llvm::shouldOptimizeForSize(BB, PSI, BFI.get()))
617616
EverMadeChange |= bypassSlowDivision(BB, BypassWidths);
618617
BB = Next;
@@ -2608,7 +2607,7 @@ bool CodeGenPrepare::optimizeCallInst(CallInst *CI, ModifyDT &ModifiedDT) {
26082607
// cold block. This interacts with our handling for loads and stores to
26092608
// ensure that we can fold all uses of a potential addressing computation
26102609
// into their uses. TODO: generalize this to work over profiling data
2611-
if (CI->hasFnAttr(Attribute::Cold) && !OptSize &&
2610+
if (CI->hasFnAttr(Attribute::Cold) &&
26122611
!llvm::shouldOptimizeForSize(BB, PSI, BFI.get()))
26132612
for (auto &Arg : CI->args()) {
26142613
if (!Arg->getType()->isPointerTy())
@@ -5505,9 +5504,7 @@ static bool FindAllMemoryUses(
55055504
if (CI->hasFnAttr(Attribute::Cold)) {
55065505
// If this is a cold call, we can sink the addressing calculation into
55075506
// the cold path. See optimizeCallInst
5508-
bool OptForSize =
5509-
OptSize || llvm::shouldOptimizeForSize(CI->getParent(), PSI, BFI);
5510-
if (!OptForSize)
5507+
if (!llvm::shouldOptimizeForSize(CI->getParent(), PSI, BFI))
55115508
continue;
55125509
}
55135510

@@ -7402,7 +7399,7 @@ bool CodeGenPrepare::optimizeSelectInst(SelectInst *SI) {
74027399
SelectKind = TargetLowering::ScalarValSelect;
74037400

74047401
if (TLI->isSelectSupported(SelectKind) &&
7405-
(!isFormingBranchFromSelectProfitable(TTI, TLI, SI) || OptSize ||
7402+
(!isFormingBranchFromSelectProfitable(TTI, TLI, SI) ||
74067403
llvm::shouldOptimizeForSize(SI->getParent(), PSI, BFI.get())))
74077404
return false;
74087405

llvm/lib/CodeGen/ExpandMemCmp.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -852,8 +852,7 @@ static bool expandMemCmp(CallInst *CI, const TargetTransformInfo *TTI,
852852
// available load sizes.
853853
const bool IsUsedForZeroCmp =
854854
IsBCmp || isOnlyUsedInZeroEqualityComparison(CI);
855-
bool OptForSize = CI->getFunction()->hasOptSize() ||
856-
llvm::shouldOptimizeForSize(CI->getParent(), PSI, BFI);
855+
bool OptForSize = llvm::shouldOptimizeForSize(CI->getParent(), PSI, BFI);
857856
auto Options = TTI->enableMemCmpExpansion(OptForSize,
858857
IsUsedForZeroCmp);
859858
if (!Options) return false;

llvm/lib/CodeGen/GlobalISel/Utils.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1620,9 +1620,7 @@ int64_t llvm::getICmpTrueVal(const TargetLowering &TLI, bool IsVector,
16201620

16211621
bool llvm::shouldOptForSize(const MachineBasicBlock &MBB,
16221622
ProfileSummaryInfo *PSI, BlockFrequencyInfo *BFI) {
1623-
const auto &F = MBB.getParent()->getFunction();
1624-
return F.hasOptSize() || F.hasMinSize() ||
1625-
llvm::shouldOptimizeForSize(MBB.getBasicBlock(), PSI, BFI);
1623+
return llvm::shouldOptimizeForSize(MBB.getBasicBlock(), PSI, BFI);
16261624
}
16271625

16281626
void llvm::saveUsesAndErase(MachineInstr &MI, MachineRegisterInfo &MRI,

llvm/lib/CodeGen/MachineBlockPlacement.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2189,9 +2189,7 @@ MachineBlockPlacement::findBestLoopTop(const MachineLoop &L,
21892189
// i.e. when the layout predecessor does not fallthrough to the loop header.
21902190
// In practice this never happens though: there always seems to be a preheader
21912191
// that can fallthrough and that is also placed before the header.
2192-
bool OptForSize = F->getFunction().hasOptSize() ||
2193-
llvm::shouldOptimizeForSize(L.getHeader(), PSI, MBFI.get());
2194-
if (OptForSize)
2192+
if (llvm::shouldOptimizeForSize(L.getHeader(), PSI, MBFI.get()))
21952193
return L.getHeader();
21962194

21972195
MachineBasicBlock *OldTop = nullptr;
@@ -3511,7 +3509,6 @@ bool MachineBlockPlacement::runOnMachineFunction(MachineFunction &MF) {
35113509
initTailDupThreshold();
35123510

35133511
const bool OptForSize =
3514-
MF.getFunction().hasOptSize() ||
35153512
llvm::shouldOptimizeForSize(&MF, PSI, &MBFI->getMBFI());
35163513
// Determine whether to use ext-tsp for perf/size optimization. The method
35173514
// is beneficial only for instances with at least 3 basic blocks and it can be

llvm/lib/CodeGen/MachineCombiner.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -571,7 +571,7 @@ bool MachineCombiner::combineInstructions(MachineBasicBlock *MBB) {
571571
SparseSet<LiveRegUnit> RegUnits;
572572
RegUnits.setUniverse(TRI->getNumRegUnits());
573573

574-
bool OptForSize = OptSize || llvm::shouldOptimizeForSize(MBB, PSI, MBFI);
574+
bool OptForSize = llvm::shouldOptimizeForSize(MBB, PSI, MBFI);
575575

576576
bool DoRegPressureReduce =
577577
TII->shouldReduceRegisterPressure(MBB, &RegClassInfo);

llvm/lib/CodeGen/MachineSizeOpts.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ bool llvm::shouldOptimizeForSize(const MachineFunction *MF,
2828
ProfileSummaryInfo *PSI,
2929
const MachineBlockFrequencyInfo *MBFI,
3030
PGSOQueryType QueryType) {
31+
if (MF->getFunction().hasOptSize())
32+
return true;
3133
return shouldFuncOptimizeForSizeImpl(MF, PSI, MBFI, QueryType);
3234
}
3335

@@ -36,6 +38,8 @@ bool llvm::shouldOptimizeForSize(const MachineBasicBlock *MBB,
3638
const MachineBlockFrequencyInfo *MBFI,
3739
PGSOQueryType QueryType) {
3840
assert(MBB);
41+
if (MBB->getParent()->getFunction().hasOptSize())
42+
return true;
3943
return shouldOptimizeForSizeImpl(MBB, PSI, MBFI, QueryType);
4044
}
4145

@@ -44,7 +48,9 @@ bool llvm::shouldOptimizeForSize(const MachineBasicBlock *MBB,
4448
MBFIWrapper *MBFIW,
4549
PGSOQueryType QueryType) {
4650
assert(MBB);
47-
if (!PSI || !MBFIW)
51+
if (MBB->getParent()->getFunction().hasOptSize())
52+
return true;
53+
if (!MBFIW)
4854
return false;
4955
BlockFrequency BlockFreq = MBFIW->getBlockFreq(MBB);
5056
return shouldOptimizeForSizeImpl(BlockFreq, PSI, &MBFIW->getMBFI(),

llvm/lib/CodeGen/SelectOptimize.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,7 @@ PreservedAnalyses SelectOptimizeImpl::run(Function &F,
431431
BFI = &FAM.getResult<BlockFrequencyAnalysis>(F);
432432

433433
// When optimizing for size, selects are preferable over branches.
434-
if (F.hasOptSize() || llvm::shouldOptimizeForSize(&F, PSI, BFI))
434+
if (llvm::shouldOptimizeForSize(&F, PSI, BFI))
435435
return PreservedAnalyses::all();
436436

437437
LI = &FAM.getResult<LoopAnalysis>(F);
@@ -467,7 +467,7 @@ bool SelectOptimizeImpl::runOnFunction(Function &F, Pass &P) {
467467
TSchedModel.init(TSI);
468468

469469
// When optimizing for size, selects are preferable over branches.
470-
if (F.hasOptSize() || llvm::shouldOptimizeForSize(&F, PSI, BFI))
470+
if (llvm::shouldOptimizeForSize(&F, PSI, BFI))
471471
return false;
472472

473473
return optimizeSelects(F);

llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1370,8 +1370,7 @@ SelectionDAG::~SelectionDAG() {
13701370
}
13711371

13721372
bool SelectionDAG::shouldOptForSize() const {
1373-
return MF->getFunction().hasOptSize() ||
1374-
llvm::shouldOptimizeForSize(FLI->MBB->getBasicBlock(), PSI, BFI);
1373+
return llvm::shouldOptimizeForSize(FLI->MBB->getBasicBlock(), PSI, BFI);
13751374
}
13761375

13771376
void SelectionDAG::allnodes_clear() {

llvm/lib/CodeGen/TailDuplicator.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -586,13 +586,11 @@ bool TailDuplicator::shouldTailDuplicate(bool IsSimple,
586586
// duplicate only one, because one branch instruction can be eliminated to
587587
// compensate for the duplication.
588588
unsigned MaxDuplicateCount;
589-
bool OptForSize = MF->getFunction().hasOptSize() ||
590-
llvm::shouldOptimizeForSize(&TailBB, PSI, MBFI);
591589
if (TailDupSize == 0)
592590
MaxDuplicateCount = TailDuplicateSize;
593591
else
594592
MaxDuplicateCount = TailDupSize;
595-
if (OptForSize)
593+
if (llvm::shouldOptimizeForSize(&TailBB, PSI, MBFI))
596594
MaxDuplicateCount = 1;
597595

598596
// If the block to be duplicated ends in an unanalyzable fallthrough, don't

0 commit comments

Comments
 (0)