Skip to content

Check hasOptSize() in shouldOptimizeForSize() #112626

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions llvm/lib/CodeGen/BranchFolding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -645,11 +645,8 @@ ProfitableToMerge(MachineBasicBlock *MBB1, MachineBasicBlock *MBB2,
// we don't have to split a block. At worst we will be introducing 1 new
// branch instruction, which is likely to be smaller than the 2
// instructions that would be deleted in the merge.
MachineFunction *MF = MBB1->getParent();
bool OptForSize =
MF->getFunction().hasOptSize() ||
(llvm::shouldOptimizeForSize(MBB1, PSI, &MBBFreqInfo) &&
llvm::shouldOptimizeForSize(MBB2, PSI, &MBBFreqInfo));
bool OptForSize = llvm::shouldOptimizeForSize(MBB1, PSI, &MBBFreqInfo) &&
llvm::shouldOptimizeForSize(MBB2, PSI, &MBBFreqInfo);
return EffectiveTailLen >= 2 && OptForSize &&
(FullBlockTail1 || FullBlockTail2);
}
Expand Down
9 changes: 3 additions & 6 deletions llvm/lib/CodeGen/CodeGenPrepare.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -612,7 +612,6 @@ bool CodeGenPrepare::_run(Function &F) {
// bypassSlowDivision may create new BBs, but we don't want to reapply the
// optimization to those blocks.
BasicBlock *Next = BB->getNextNode();
// F.hasOptSize is already checked in the outer if statement.
if (!llvm::shouldOptimizeForSize(BB, PSI, BFI.get()))
EverMadeChange |= bypassSlowDivision(BB, BypassWidths);
BB = Next;
Expand Down Expand Up @@ -2608,7 +2607,7 @@ bool CodeGenPrepare::optimizeCallInst(CallInst *CI, ModifyDT &ModifiedDT) {
// cold block. This interacts with our handling for loads and stores to
// ensure that we can fold all uses of a potential addressing computation
// into their uses. TODO: generalize this to work over profiling data
if (CI->hasFnAttr(Attribute::Cold) && !OptSize &&
if (CI->hasFnAttr(Attribute::Cold) &&
!llvm::shouldOptimizeForSize(BB, PSI, BFI.get()))
for (auto &Arg : CI->args()) {
if (!Arg->getType()->isPointerTy())
Expand Down Expand Up @@ -5505,9 +5504,7 @@ static bool FindAllMemoryUses(
if (CI->hasFnAttr(Attribute::Cold)) {
// If this is a cold call, we can sink the addressing calculation into
// the cold path. See optimizeCallInst
bool OptForSize =
OptSize || llvm::shouldOptimizeForSize(CI->getParent(), PSI, BFI);
if (!OptForSize)
if (!llvm::shouldOptimizeForSize(CI->getParent(), PSI, BFI))
continue;
}

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

if (TLI->isSelectSupported(SelectKind) &&
(!isFormingBranchFromSelectProfitable(TTI, TLI, SI) || OptSize ||
(!isFormingBranchFromSelectProfitable(TTI, TLI, SI) ||
llvm::shouldOptimizeForSize(SI->getParent(), PSI, BFI.get())))
return false;

Expand Down
3 changes: 1 addition & 2 deletions llvm/lib/CodeGen/ExpandMemCmp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -852,8 +852,7 @@ static bool expandMemCmp(CallInst *CI, const TargetTransformInfo *TTI,
// available load sizes.
const bool IsUsedForZeroCmp =
IsBCmp || isOnlyUsedInZeroEqualityComparison(CI);
bool OptForSize = CI->getFunction()->hasOptSize() ||
llvm::shouldOptimizeForSize(CI->getParent(), PSI, BFI);
bool OptForSize = llvm::shouldOptimizeForSize(CI->getParent(), PSI, BFI);
auto Options = TTI->enableMemCmpExpansion(OptForSize,
IsUsedForZeroCmp);
if (!Options) return false;
Expand Down
4 changes: 1 addition & 3 deletions llvm/lib/CodeGen/GlobalISel/Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1620,9 +1620,7 @@ int64_t llvm::getICmpTrueVal(const TargetLowering &TLI, bool IsVector,

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

void llvm::saveUsesAndErase(MachineInstr &MI, MachineRegisterInfo &MRI,
Expand Down
3 changes: 1 addition & 2 deletions llvm/lib/CodeGen/LiveIntervals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -890,8 +890,7 @@ float LiveIntervals::getSpillWeight(bool isDef, bool isUse,
const auto *MF = MBB->getParent();
// When optimizing for size we only consider the codesize impact of spilling
// the register, not the runtime impact.
if (PSI && (MF->getFunction().hasOptSize() ||
llvm::shouldOptimizeForSize(MF, PSI, MBFI)))
if (PSI && llvm::shouldOptimizeForSize(MF, PSI, MBFI))
return Weight;
return Weight * MBFI->getBlockFreqRelativeToEntryBlock(MBB);
}
Expand Down
5 changes: 1 addition & 4 deletions llvm/lib/CodeGen/MachineBlockPlacement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2189,9 +2189,7 @@ MachineBlockPlacement::findBestLoopTop(const MachineLoop &L,
// i.e. when the layout predecessor does not fallthrough to the loop header.
// In practice this never happens though: there always seems to be a preheader
// that can fallthrough and that is also placed before the header.
bool OptForSize = F->getFunction().hasOptSize() ||
llvm::shouldOptimizeForSize(L.getHeader(), PSI, MBFI.get());
if (OptForSize)
if (llvm::shouldOptimizeForSize(L.getHeader(), PSI, MBFI.get()))
return L.getHeader();

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

const bool OptForSize =
MF.getFunction().hasOptSize() ||
llvm::shouldOptimizeForSize(&MF, PSI, &MBFI->getMBFI());
// Determine whether to use ext-tsp for perf/size optimization. The method
// is beneficial only for instances with at least 3 basic blocks and it can be
Expand Down
6 changes: 1 addition & 5 deletions llvm/lib/CodeGen/MachineCombiner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,6 @@ class MachineCombiner : public MachineFunctionPass {

TargetSchedModel TSchedModel;

/// True if optimizing for code size.
bool OptSize = false;

public:
static char ID;
MachineCombiner() : MachineFunctionPass(ID) {
Expand Down Expand Up @@ -571,7 +568,7 @@ bool MachineCombiner::combineInstructions(MachineBasicBlock *MBB) {
SparseSet<LiveRegUnit> RegUnits;
RegUnits.setUniverse(TRI->getNumRegUnits());

bool OptForSize = OptSize || llvm::shouldOptimizeForSize(MBB, PSI, MBFI);
bool OptForSize = llvm::shouldOptimizeForSize(MBB, PSI, MBFI);

bool DoRegPressureReduce =
TII->shouldReduceRegisterPressure(MBB, &RegClassInfo);
Expand Down Expand Up @@ -733,7 +730,6 @@ bool MachineCombiner::runOnMachineFunction(MachineFunction &MF) {
&getAnalysis<LazyMachineBlockFrequencyInfoPass>().getBFI() :
nullptr;
TraceEnsemble = nullptr;
OptSize = MF.getFunction().hasOptSize();
RegClassInfo.runOnMachineFunction(MF);

LLVM_DEBUG(dbgs() << getPassName() << ": " << MF.getName() << '\n');
Expand Down
8 changes: 7 additions & 1 deletion llvm/lib/CodeGen/MachineSizeOpts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ bool llvm::shouldOptimizeForSize(const MachineFunction *MF,
ProfileSummaryInfo *PSI,
const MachineBlockFrequencyInfo *MBFI,
PGSOQueryType QueryType) {
if (MF->getFunction().hasOptSize())
return true;
return shouldFuncOptimizeForSizeImpl(MF, PSI, MBFI, QueryType);
}

Expand All @@ -36,6 +38,8 @@ bool llvm::shouldOptimizeForSize(const MachineBasicBlock *MBB,
const MachineBlockFrequencyInfo *MBFI,
PGSOQueryType QueryType) {
assert(MBB);
if (MBB->getParent()->getFunction().hasOptSize())
return true;
return shouldOptimizeForSizeImpl(MBB, PSI, MBFI, QueryType);
}

Expand All @@ -44,7 +48,9 @@ bool llvm::shouldOptimizeForSize(const MachineBasicBlock *MBB,
MBFIWrapper *MBFIW,
PGSOQueryType QueryType) {
assert(MBB);
if (!PSI || !MBFIW)
if (MBB->getParent()->getFunction().hasOptSize())
return true;
if (!MBFIW)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was dropping !PSI here intentional?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes because PSI is checked in shouldOptimizeForSizeImpl() anyway.

bool shouldOptimizeForSizeImpl(BlockTOrBlockFreq BBOrBlockFreq,
ProfileSummaryInfo *PSI, BFIT *BFI,
PGSOQueryType QueryType) {
if (!PSI || !BFI || !PSI->hasProfileSummary())
return false;

return false;
BlockFrequency BlockFreq = MBFIW->getBlockFreq(MBB);
return shouldOptimizeForSizeImpl(BlockFreq, PSI, &MBFIW->getMBFI(),
Expand Down
4 changes: 2 additions & 2 deletions llvm/lib/CodeGen/SelectOptimize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ PreservedAnalyses SelectOptimizeImpl::run(Function &F,
BFI = &FAM.getResult<BlockFrequencyAnalysis>(F);

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

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

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

return optimizeSelects(F);
Expand Down
3 changes: 1 addition & 2 deletions llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1370,8 +1370,7 @@ SelectionDAG::~SelectionDAG() {
}

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

void SelectionDAG::allnodes_clear() {
Expand Down
4 changes: 1 addition & 3 deletions llvm/lib/CodeGen/TailDuplicator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -586,13 +586,11 @@ bool TailDuplicator::shouldTailDuplicate(bool IsSimple,
// duplicate only one, because one branch instruction can be eliminated to
// compensate for the duplication.
unsigned MaxDuplicateCount;
bool OptForSize = MF->getFunction().hasOptSize() ||
llvm::shouldOptimizeForSize(&TailBB, PSI, MBFI);
if (TailDupSize == 0)
MaxDuplicateCount = TailDuplicateSize;
else
MaxDuplicateCount = TailDupSize;
if (OptForSize)
if (llvm::shouldOptimizeForSize(&TailBB, PSI, MBFI))
MaxDuplicateCount = 1;

// If the block to be duplicated ends in an unanalyzable fallthrough, don't
Expand Down
1 change: 0 additions & 1 deletion llvm/lib/CodeGen/TargetLoweringBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1633,7 +1633,6 @@ bool TargetLoweringBase::isSuitableForJumpTable(const SwitchInst *SI,
// performed in findJumpTable() in SelectionDAGBuiler and
// getEstimatedNumberOfCaseClusters() in BasicTTIImpl.
const bool OptForSize =
SI->getParent()->getParent()->hasOptSize() ||
llvm::shouldOptimizeForSize(SI->getParent(), PSI, BFI);
const unsigned MinDensity = getMinimumJumpTableDensity(OptForSize);
const unsigned MaxJumpTableSize = getMaximumJumpTableSize();
Expand Down
3 changes: 1 addition & 2 deletions llvm/lib/Target/X86/X86FixupBWInsts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -443,8 +443,7 @@ void FixupBWInstPass::processBasicBlock(MachineFunction &MF,
// We run after PEI, so we need to AddPristinesAndCSRs.
LiveUnits.addLiveOuts(MBB);

OptForSize = MF.getFunction().hasOptSize() ||
llvm::shouldOptimizeForSize(&MBB, PSI, MBFI);
OptForSize = llvm::shouldOptimizeForSize(&MBB, PSI, MBFI);

for (MachineInstr &MI : llvm::reverse(MBB)) {
if (MachineInstr *NewMI = tryReplaceInstr(&MI, MBB))
Expand Down
4 changes: 1 addition & 3 deletions llvm/lib/Target/X86/X86OptimizeLEAs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -741,9 +741,7 @@ bool X86OptimizeLEAPass::runOnMachineFunction(MachineFunction &MF) {

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

Expand Down
4 changes: 1 addition & 3 deletions llvm/lib/Target/X86/X86PadShortFunction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,7 @@ bool PadShortFunc::runOnMachineFunction(MachineFunction &MF) {
MachineBasicBlock *MBB = ReturnBB.first;
unsigned Cycles = ReturnBB.second;

// Function::hasOptSize is already checked above.
bool OptForSize = llvm::shouldOptimizeForSize(MBB, PSI, MBFI);
if (OptForSize)
if (llvm::shouldOptimizeForSize(MBB, PSI, MBFI))
continue;

if (Cycles < Threshold) {
Expand Down
3 changes: 1 addition & 2 deletions llvm/lib/Transforms/IPO/FunctionSpecialization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -942,8 +942,7 @@ bool FunctionSpecializer::isCandidateFunction(Function *F) {
return false;

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

// Exit if the function is not executable. There's no point in specializing
Expand Down
3 changes: 1 addition & 2 deletions llvm/lib/Transforms/Scalar/ConstantHoisting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -953,8 +953,7 @@ bool ConstantHoistingPass::runImpl(Function &Fn, TargetTransformInfo &TTI,
this->Ctx = &Fn.getContext();
this->Entry = &Entry;
this->PSI = PSI;
this->OptForSize = Entry.getParent()->hasOptSize() ||
llvm::shouldOptimizeForSize(Entry.getParent(), PSI, BFI,
this->OptForSize = llvm::shouldOptimizeForSize(Entry.getParent(), PSI, BFI,
PGSOQueryType::IRPass);

// Collect all constant candidates.
Expand Down
7 changes: 2 additions & 5 deletions llvm/lib/Transforms/Scalar/LoopLoadElimination.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -586,11 +586,8 @@ class LoadEliminationForLoop {
}

auto *HeaderBB = L->getHeader();
auto *F = HeaderBB->getParent();
bool OptForSize = F->hasOptSize() ||
llvm::shouldOptimizeForSize(HeaderBB, PSI, BFI,
PGSOQueryType::IRPass);
if (OptForSize) {
if (llvm::shouldOptimizeForSize(HeaderBB, PSI, BFI,
PGSOQueryType::IRPass)) {
LLVM_DEBUG(
dbgs() << "Versioning is needed but not allowed when optimizing "
"for size.\n");
Expand Down
15 changes: 5 additions & 10 deletions llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1413,8 +1413,7 @@ Value *LibCallSimplifier::optimizeMemChr(CallInst *CI, IRBuilderBase &B) {
return nullptr;
}

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

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

bool OptForSize = CI->getFunction()->hasOptSize() ||
llvm::shouldOptimizeForSize(CI->getParent(), PSI, BFI,
PGSOQueryType::IRPass);
if (OptForSize)
if (llvm::shouldOptimizeForSize(CI->getParent(), PSI, BFI,
PGSOQueryType::IRPass))
return nullptr;

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

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

// We can't optimize if return value is used.
Expand Down
4 changes: 4 additions & 0 deletions llvm/lib/Transforms/Utils/SizeOpts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,16 @@ struct BasicBlockBFIAdapter {
bool llvm::shouldOptimizeForSize(const Function *F, ProfileSummaryInfo *PSI,
BlockFrequencyInfo *BFI,
PGSOQueryType QueryType) {
if (F->hasOptSize())
return true;
return shouldFuncOptimizeForSizeImpl(F, PSI, BFI, QueryType);
}

bool llvm::shouldOptimizeForSize(const BasicBlock *BB, ProfileSummaryInfo *PSI,
BlockFrequencyInfo *BFI,
PGSOQueryType QueryType) {
assert(BB);
if (BB->getParent()->hasOptSize())
return true;
return shouldOptimizeForSizeImpl(BB, PSI, BFI, QueryType);
}
7 changes: 2 additions & 5 deletions llvm/lib/Transforms/Vectorize/LoopVectorizationLegality.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -460,11 +460,8 @@ int LoopVectorizationLegality::isConsecutivePtr(Type *AccessTy,
const auto &Strides =
LAI ? LAI->getSymbolicStrides() : DenseMap<Value *, const SCEV *>();

Function *F = TheLoop->getHeader()->getParent();
bool OptForSize = F->hasOptSize() ||
llvm::shouldOptimizeForSize(TheLoop->getHeader(), PSI, BFI,
PGSOQueryType::IRPass);
bool CanAddPredicate = !OptForSize;
bool CanAddPredicate = !llvm::shouldOptimizeForSize(
TheLoop->getHeader(), PSI, BFI, PGSOQueryType::IRPass);
int Stride = getPtrStride(PSE, AccessTy, Ptr, TheLoop, Strides,
CanAddPredicate, false).value_or(0);
if (Stride == 1 || Stride == -1)
Expand Down
Loading