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

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

llvm/lib/CodeGen/TargetLoweringBase.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1633,7 +1633,6 @@ bool TargetLoweringBase::isSuitableForJumpTable(const SwitchInst *SI,
16331633
// performed in findJumpTable() in SelectionDAGBuiler and
16341634
// getEstimatedNumberOfCaseClusters() in BasicTTIImpl.
16351635
const bool OptForSize =
1636-
SI->getParent()->getParent()->hasOptSize() ||
16371636
llvm::shouldOptimizeForSize(SI->getParent(), PSI, BFI);
16381637
const unsigned MinDensity = getMinimumJumpTableDensity(OptForSize);
16391638
const unsigned MaxJumpTableSize = getMaximumJumpTableSize();

llvm/lib/Target/X86/X86FixupBWInsts.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -443,8 +443,7 @@ void FixupBWInstPass::processBasicBlock(MachineFunction &MF,
443443
// We run after PEI, so we need to AddPristinesAndCSRs.
444444
LiveUnits.addLiveOuts(MBB);
445445

446-
OptForSize = MF.getFunction().hasOptSize() ||
447-
llvm::shouldOptimizeForSize(&MBB, PSI, MBFI);
446+
OptForSize = llvm::shouldOptimizeForSize(&MBB, PSI, MBFI);
448447

449448
for (MachineInstr &MI : llvm::reverse(MBB)) {
450449
if (MachineInstr *NewMI = tryReplaceInstr(&MI, MBB))

llvm/lib/Target/X86/X86OptimizeLEAs.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -741,9 +741,7 @@ bool X86OptimizeLEAPass::runOnMachineFunction(MachineFunction &MF) {
741741

742742
// Remove redundant address calculations. Do it only for -Os/-Oz since only
743743
// a code size gain is expected from this part of the pass.
744-
bool OptForSize = MF.getFunction().hasOptSize() ||
745-
llvm::shouldOptimizeForSize(&MBB, PSI, MBFI);
746-
if (OptForSize)
744+
if (llvm::shouldOptimizeForSize(&MBB, PSI, MBFI))
747745
Changed |= removeRedundantAddrCalc(LEAs);
748746
}
749747

llvm/lib/Target/X86/X86PadShortFunction.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,9 +132,7 @@ bool PadShortFunc::runOnMachineFunction(MachineFunction &MF) {
132132
MachineBasicBlock *MBB = ReturnBB.first;
133133
unsigned Cycles = ReturnBB.second;
134134

135-
// Function::hasOptSize is already checked above.
136-
bool OptForSize = llvm::shouldOptimizeForSize(MBB, PSI, MBFI);
137-
if (OptForSize)
135+
if (llvm::shouldOptimizeForSize(MBB, PSI, MBFI))
138136
continue;
139137

140138
if (Cycles < Threshold) {

llvm/lib/Transforms/IPO/FunctionSpecialization.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1001,8 +1001,7 @@ bool FunctionSpecializer::isCandidateFunction(Function *F) {
10011001
return false;
10021002

10031003
// If we're optimizing the function for size, we shouldn't specialize it.
1004-
if (F->hasOptSize() ||
1005-
shouldOptimizeForSize(F, nullptr, nullptr, PGSOQueryType::IRPass))
1004+
if (shouldOptimizeForSize(F, nullptr, nullptr, PGSOQueryType::IRPass))
10061005
return false;
10071006

10081007
// Exit if the function is not executable. There's no point in specializing

llvm/lib/Transforms/Scalar/ConstantHoisting.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -953,8 +953,7 @@ bool ConstantHoistingPass::runImpl(Function &Fn, TargetTransformInfo &TTI,
953953
this->Ctx = &Fn.getContext();
954954
this->Entry = &Entry;
955955
this->PSI = PSI;
956-
this->OptForSize = Entry.getParent()->hasOptSize() ||
957-
llvm::shouldOptimizeForSize(Entry.getParent(), PSI, BFI,
956+
this->OptForSize = llvm::shouldOptimizeForSize(Entry.getParent(), PSI, BFI,
958957
PGSOQueryType::IRPass);
959958

960959
// Collect all constant candidates.

llvm/lib/Transforms/Scalar/LoopLoadElimination.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -586,11 +586,8 @@ class LoadEliminationForLoop {
586586
}
587587

588588
auto *HeaderBB = L->getHeader();
589-
auto *F = HeaderBB->getParent();
590-
bool OptForSize = F->hasOptSize() ||
591-
llvm::shouldOptimizeForSize(HeaderBB, PSI, BFI,
592-
PGSOQueryType::IRPass);
593-
if (OptForSize) {
589+
if (llvm::shouldOptimizeForSize(HeaderBB, PSI, BFI,
590+
PGSOQueryType::IRPass)) {
594591
LLVM_DEBUG(
595592
dbgs() << "Versioning is needed but not allowed when optimizing "
596593
"for size.\n");

llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1413,8 +1413,7 @@ Value *LibCallSimplifier::optimizeMemChr(CallInst *CI, IRBuilderBase &B) {
14131413
return nullptr;
14141414
}
14151415

1416-
bool OptForSize = CI->getFunction()->hasOptSize() ||
1417-
llvm::shouldOptimizeForSize(CI->getParent(), PSI, BFI,
1416+
bool OptForSize = llvm::shouldOptimizeForSize(CI->getParent(), PSI, BFI,
14181417
PGSOQueryType::IRPass);
14191418

14201419
// If the char is variable but the input str and length are not we can turn
@@ -3482,10 +3481,8 @@ Value *LibCallSimplifier::optimizeSPrintFString(CallInst *CI,
34823481
return B.CreateIntCast(PtrDiff, CI->getType(), false);
34833482
}
34843483

3485-
bool OptForSize = CI->getFunction()->hasOptSize() ||
3486-
llvm::shouldOptimizeForSize(CI->getParent(), PSI, BFI,
3487-
PGSOQueryType::IRPass);
3488-
if (OptForSize)
3484+
if (llvm::shouldOptimizeForSize(CI->getParent(), PSI, BFI,
3485+
PGSOQueryType::IRPass))
34893486
return nullptr;
34903487

34913488
Value *Len = emitStrLen(CI->getArgOperand(2), B, DL, TLI);
@@ -3795,10 +3792,8 @@ Value *LibCallSimplifier::optimizeFPuts(CallInst *CI, IRBuilderBase &B) {
37953792

37963793
// Don't rewrite fputs to fwrite when optimising for size because fwrite
37973794
// requires more arguments and thus extra MOVs are required.
3798-
bool OptForSize = CI->getFunction()->hasOptSize() ||
3799-
llvm::shouldOptimizeForSize(CI->getParent(), PSI, BFI,
3800-
PGSOQueryType::IRPass);
3801-
if (OptForSize)
3795+
if (llvm::shouldOptimizeForSize(CI->getParent(), PSI, BFI,
3796+
PGSOQueryType::IRPass))
38023797
return nullptr;
38033798

38043799
// We can't optimize if return value is used.

llvm/lib/Transforms/Utils/SizeOpts.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,12 +99,16 @@ struct BasicBlockBFIAdapter {
9999
bool llvm::shouldOptimizeForSize(const Function *F, ProfileSummaryInfo *PSI,
100100
BlockFrequencyInfo *BFI,
101101
PGSOQueryType QueryType) {
102+
if (F->hasOptSize())
103+
return true;
102104
return shouldFuncOptimizeForSizeImpl(F, PSI, BFI, QueryType);
103105
}
104106

105107
bool llvm::shouldOptimizeForSize(const BasicBlock *BB, ProfileSummaryInfo *PSI,
106108
BlockFrequencyInfo *BFI,
107109
PGSOQueryType QueryType) {
108110
assert(BB);
111+
if (BB->getParent()->hasOptSize())
112+
return true;
109113
return shouldOptimizeForSizeImpl(BB, PSI, BFI, QueryType);
110114
}

llvm/lib/Transforms/Vectorize/LoopVectorizationLegality.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -460,11 +460,8 @@ int LoopVectorizationLegality::isConsecutivePtr(Type *AccessTy,
460460
const auto &Strides =
461461
LAI ? LAI->getSymbolicStrides() : DenseMap<Value *, const SCEV *>();
462462

463-
Function *F = TheLoop->getHeader()->getParent();
464-
bool OptForSize = F->hasOptSize() ||
465-
llvm::shouldOptimizeForSize(TheLoop->getHeader(), PSI, BFI,
466-
PGSOQueryType::IRPass);
467-
bool CanAddPredicate = !OptForSize;
463+
bool CanAddPredicate = !llvm::shouldOptimizeForSize(
464+
TheLoop->getHeader(), PSI, BFI, PGSOQueryType::IRPass);
468465
int Stride = getPtrStride(PSE, AccessTy, Ptr, TheLoop, Strides,
469466
CanAddPredicate, false).value_or(0);
470467
if (Stride == 1 || Stride == -1)

0 commit comments

Comments
 (0)