Skip to content

Commit 6ab26ea

Browse files
authored
Check hasOptSize() in shouldOptimizeForSize() (#112626)
1 parent 92412c1 commit 6ab26ea

21 files changed

+39
-68
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
@@ -1621,9 +1621,7 @@ int64_t llvm::getICmpTrueVal(const TargetLowering &TLI, bool IsVector,
16211621

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

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

llvm/lib/CodeGen/LiveIntervals.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -890,8 +890,7 @@ float LiveIntervals::getSpillWeight(bool isDef, bool isUse,
890890
const auto *MF = MBB->getParent();
891891
// When optimizing for size we only consider the codesize impact of spilling
892892
// the register, not the runtime impact.
893-
if (PSI && (MF->getFunction().hasOptSize() ||
894-
llvm::shouldOptimizeForSize(MF, PSI, MBFI)))
893+
if (PSI && llvm::shouldOptimizeForSize(MF, PSI, MBFI))
895894
return Weight;
896895
return Weight * MBFI->getBlockFreqRelativeToEntryBlock(MBB);
897896
}

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 & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,6 @@ class MachineCombiner : public MachineFunctionPass {
7777

7878
TargetSchedModel TSchedModel;
7979

80-
/// True if optimizing for code size.
81-
bool OptSize = false;
82-
8380
public:
8481
static char ID;
8582
MachineCombiner() : MachineFunctionPass(ID) {
@@ -571,7 +568,7 @@ bool MachineCombiner::combineInstructions(MachineBasicBlock *MBB) {
571568
SparseSet<LiveRegUnit> RegUnits;
572569
RegUnits.setUniverse(TRI->getNumRegUnits());
573570

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

576573
bool DoRegPressureReduce =
577574
TII->shouldReduceRegisterPressure(MBB, &RegClassInfo);
@@ -733,7 +730,6 @@ bool MachineCombiner::runOnMachineFunction(MachineFunction &MF) {
733730
&getAnalysis<LazyMachineBlockFrequencyInfoPass>().getBFI() :
734731
nullptr;
735732
TraceEnsemble = nullptr;
736-
OptSize = MF.getFunction().hasOptSize();
737733
RegClassInfo.runOnMachineFunction(MF);
738734

739735
LLVM_DEBUG(dbgs() << getPassName() << ": " << MF.getName() << '\n');

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() {

0 commit comments

Comments
 (0)