llvm-c166/llvm/lib/CodeGen
Peter Rong e89363f9ff
[CodeGen] Cache shouldOptimizeForSize() when computing spill weights (#214905)
`VirtRegAuxInfo::weightCalcHelper()` calls
LiveIntervals::getSpillWeight() once per use/def operand of every
virtual register, and `getSpillWeight()` evaluates `(PSI &&
llvm::shouldOptimizeForSize(MF, PSI, MBFI))` repeatedly.
`shouldOptimizeForSize` is expensive: `O(#operands * #blocks)`.

We had a function with 20k blocks, and this inefficient repeated query
caused severe delay, the function took 30+ minutes to build.

Since the result is the same for every query in this MF, and
`VirtRegAuxInfo` already holds `MF`, `PSI` and `MBFI`, all fixed for the
lifetime of the object, so the value is provably invariant across every
`getSpillWeight()` call it makes. We should cache it.
2026-08-26 14:36:13 -07:00
..
AsmPrinter [DebugInfo] Add DW_TAG_property support to LLVM DebugInfo (#215776) 2026-08-26 18:43:17 +00:00
GlobalISel [GlobalISel] Preserve destination type kind when lowering G_EXTRACT (#218345) 2026-08-26 22:07:22 +05:30
LiveDebugValues
MIRParser [MIR] Quote basic block names that are not plain identifiers (#214054) 2026-08-12 13:19:56 -04:00
SelectionDAG [SelectionDAG] Legalize non-divisible scalable vector widening (#218777) 2026-08-26 14:37:41 +00:00
AggressiveAntiDepBreaker.cpp
AggressiveAntiDepBreaker.h
AllocationOrder.cpp
AllocationOrder.h
Analysis.cpp
AssignmentTrackingAnalysis.cpp
AtomicExpandPass.cpp Analysis: Move LibcallLoweringInfo from CodeGen to Analysis (#210322) 2026-08-19 22:23:06 +00:00
BasicBlockMatchingAndInference.cpp
BasicBlockPathCloning.cpp
BasicBlockSections.cpp
BasicBlockSectionsProfileReader.cpp
BasicTargetTransformInfo.cpp
BranchFolding.cpp
BranchFolding.h
BranchRelaxation.cpp
BreakFalseDeps.cpp [BreakFalseDeps] Use MCRegister. NFC (#216921) 2026-08-18 16:10:39 +00:00
CalcSpillWeights.cpp [CodeGen] Cache shouldOptimizeForSize() when computing spill weights (#214905) 2026-08-26 14:36:13 -07:00
CallingConvLower.cpp
CFGuardLongjmp.cpp
CFIFixup.cpp
CFIInstrInserter.cpp [NewPM] Port CFIInstrInserter to the new pass manager (#217494) 2026-08-20 15:39:22 -07:00
CMakeLists.txt [MLGO] Model selection for models lowered through EmitC (#212650) 2026-08-18 12:34:21 -07:00
CodeGen.cpp [NewPM] Port EHContGuardTargets to the new pass manager (#217843) 2026-08-22 17:51:32 -07:00
CodeGenCommonISel.cpp [SDAG][NVPTX] Cache control metadata support and lowering (#204067) 2026-08-06 15:55:49 -07:00
CodeGenPrepare.cpp [TLI] Return LibFunc from TargetLibraryInfo::getLibFunc (#217561) 2026-08-20 22:28:37 +08:00
CodeGenTargetMachineImpl.cpp CodeGen: Remove TargetOptions::FloatABIType (#215796) 2026-08-18 17:49:30 +02:00
CommandFlags.cpp CodeGen: Remove TargetOptions::FloatABIType (#215796) 2026-08-18 17:49:30 +02:00
ComplexDeinterleavingPass.cpp [ComplexDeinterleaving] Add missing negated fcmla matches (#217901) 2026-08-21 20:07:16 +02:00
CriticalAntiDepBreaker.cpp
CriticalAntiDepBreaker.h
DeadMachineInstructionElim.cpp
DetectDeadLanes.cpp
DFAPacketizer.cpp
DroppedVariableStatsMIR.cpp
DwarfEHPrepare.cpp Analysis: Move LibcallLoweringInfo from CodeGen to Analysis (#210322) 2026-08-19 22:23:06 +00:00
EarlyIfConversion.cpp [EarlyIfConversion] Extend data dependent analysis across multiple blocks (#180623) 2026-08-23 13:51:15 +03:00
EdgeBundles.cpp
EHContGuardTargets.cpp [NewPM] Port EHContGuardTargets to the new pass manager (#217843) 2026-08-22 17:51:32 -07:00
ExecutionDomainFix.cpp
ExpandIRInsts.cpp Analysis: Move LibcallLoweringInfo from CodeGen to Analysis (#210322) 2026-08-19 22:23:06 +00:00
ExpandPostRAPseudos.cpp
ExpandReductions.cpp [LLVM] Add llvm.vector.reduce.{min, max}imumnum (#218295) 2026-08-26 13:04:16 +02:00
ExpandVectorPredication.cpp [IR] Remove trivial VP intrinsics. NFC (#217863) 2026-08-22 01:10:43 +08:00
FaultMaps.cpp
FEntryInserter.cpp
FinalizeISel.cpp
FixupStatepointCallerSaved.cpp
FuncletLayout.cpp [NewPM] Port FuncletLayout to the new pass manager (#217490) 2026-08-20 00:20:35 +00:00
GCEmptyBasicBlocks.cpp
GCMetadata.cpp
GCMetadataPrinter.cpp
GCRootLowering.cpp
GlobalMerge.cpp
GlobalMergeFunctions.cpp
HardwareLoops.cpp
IfConversion.cpp
ImplicitNullChecks.cpp [NewPM] Port ImplicitNullChecks to the new pass manager (#216965) 2026-08-18 18:40:39 -07:00
IndirectBrExpandPass.cpp
InitUndef.cpp
InlineAsmPrepare.cpp
InlineSpiller.cpp
InsertCodePrefetch.cpp
InterferenceCache.cpp [SlotIndexes] Use analysis block numbers (NFC) (#215171) 2026-08-14 10:50:38 -07:00
InterferenceCache.h
InterleavedAccessPass.cpp
InterleavedLoadCombinePass.cpp
IntrinsicLowering.cpp
JMCInstrumenter.cpp
KCFI.cpp
LatencyPriorityQueue.cpp
LazyMachineBlockFrequencyInfo.cpp
LexicalScopes.cpp
LibcallLoweringInfo.cpp Analysis: Move LibcallLoweringInfo from CodeGen to Analysis (#210322) 2026-08-19 22:23:06 +00:00
LiveDebugVariables.cpp
LiveInterval.cpp
LiveIntervalCalc.cpp
LiveIntervals.cpp [CodeGen] Cache shouldOptimizeForSize() when computing spill weights (#214905) 2026-08-26 14:36:13 -07:00
LiveIntervalUnion.cpp
LivePhysRegs.cpp
LiveRangeCalc.cpp [SlotIndexes] Use analysis block numbers (NFC) (#215171) 2026-08-14 10:50:38 -07:00
LiveRangeEdit.cpp
LiveRangeShrink.cpp
LiveRangeUtils.h
LiveRegMatrix.cpp [3/3][RegAlloc][LiveRegMatrix] Added validity check between VRM and LRM. (#197778) 2026-08-25 15:07:36 -07:00
LiveRegUnits.cpp [LiveRegUnits] Avoid accidentally O(n^2) loop in addCalleeSavedRegs (NFC) (#214740) 2026-08-08 00:02:54 +01:00
LiveStacks.cpp
LiveVariables.cpp [LiveVariables] Use MCRegister. NFC (#216928) 2026-08-18 17:06:51 +00:00
LocalStackSlotAllocation.cpp
LoopTraversal.cpp
LowerEmuTLS.cpp
LowLevelTypeUtils.cpp
MachineBasicBlock.cpp [CodeGen] Fix stale LiveIntervals regmask tables after MachineBasicBl… (#209610) 2026-08-17 13:58:21 -05:00
MachineBlockFrequencyInfo.cpp
MachineBlockHashInfo.cpp
MachineBlockPlacement.cpp
MachineBranchProbabilityInfo.cpp
MachineCFGPrinter.cpp
MachineCheckDebugify.cpp
MachineCombiner.cpp [NewPM] Port MachineCombiner to the new pass manager (#217279) 2026-08-19 13:05:26 -07:00
MachineConvergenceVerifier.cpp
MachineCopyPropagation.cpp
MachineCSE.cpp
MachineCycleAnalysis.cpp CodeGen: Add getDefBlock helper (#216182) 2026-08-14 09:10:14 +02:00
MachineDebugify.cpp
MachineDominanceFrontier.cpp
MachineDominators.cpp
MachineDomTreeUpdater.cpp
MachineFrameInfo.cpp
MachineFunction.cpp [SDAG][NVPTX] Cache control metadata support and lowering (#204067) 2026-08-06 15:55:49 -07:00
MachineFunctionAnalysis.cpp
MachineFunctionPass.cpp
MachineFunctionPrinterPass.cpp
MachineFunctionSplitter.cpp
MachineIDFSSAUpdater.cpp
MachineInstr.cpp
MachineInstrBundle.cpp
MachineLateInstrsCleanup.cpp
MachineLICM.cpp CodeGen: Pass the exception model to the EH register hooks (#216099) 2026-08-13 16:58:33 +00:00
MachineLoopInfo.cpp [NFC] [llvm] mark MachineLoop get{Bottom,Top}Block const (#216860) 2026-08-18 13:47:44 -07:00
MachineLoopUtils.cpp
MachineModuleInfo.cpp TargetMachine: Verify option/module-flag consistency once per module (#217597) 2026-08-20 17:04:40 +02:00
MachineModuleInfoImpls.cpp
MachineModuleSlotTracker.cpp
MachineOperand.cpp [SDAG][NVPTX] Cache control metadata support and lowering (#204067) 2026-08-06 15:55:49 -07:00
MachineOptimizationRemarkEmitter.cpp
MachineOutliner.cpp
MachinePassManager.cpp
MachinePipeliner.cpp [MachinePipeliner] Let targets enable the reg-pressure detector (#212538) 2026-08-25 14:42:00 -05:00
MachinePostDominators.cpp
MachineRegionInfo.cpp
MachineRegisterInfo.cpp
MachineScheduler.cpp [CodeGen] Overload adjustLaneLiveness to cleanly split use cases (NFC) (#215594) 2026-08-20 15:52:12 +02:00
MachineSink.cpp CodeGen: Fix machine sink critical edge crash on an undef register (#216870) 2026-08-18 08:11:04 +02:00
MachineSizeOpts.cpp
MachineSSAContext.cpp CodeGen: Add getDefBlock helper (#216182) 2026-08-14 09:10:14 +02:00
MachineSSAUpdater.cpp
MachineStableHash.cpp
MachineStripDebug.cpp
MachineTraceMetrics.cpp
MachineUniformityAnalysis.cpp
MachineVerifier.cpp [LLVM] Add llvm.vector.reduce.{min, max}imumnum (#218295) 2026-08-26 13:04:16 +02:00
MacroFusion.cpp
MBFIWrapper.cpp
MIR2Vec.cpp
MIRCanonicalizerPass.cpp
MIRFSDiscriminator.cpp
MIRNamerPass.cpp
MIRPrinter.cpp
MIRPrintingPass.cpp
MIRSampleProfile.cpp
MIRVRegNamerUtils.cpp
MIRVRegNamerUtils.h
MIRYamlMapping.cpp
MLRegAllocEvictAdvisor.cpp [MLGO] Model selection for models lowered through EmitC (#212650) 2026-08-18 12:34:21 -07:00
MLRegAllocEvictAdvisor.h
MLRegAllocPriorityAdvisor.cpp
ModuloSchedule.cpp ModuloSchedule: Fix using getVRegDef/getUniqueVRegDef on physregs (#216795) 2026-08-24 08:30:19 +02:00
MultiHazardRecognizer.cpp
NonRelocatableStringpool.cpp
OptimizePHIs.cpp
PatchableFunction.cpp
PeepholeOptimizer.cpp [AMDGPU] PeepholeOptimizer - continue search next new source if a new source found is a subreg (#212563) 2026-08-25 10:18:01 -07:00
PHIElimination.cpp
PHIEliminationUtils.cpp
PHIEliminationUtils.h
PostRAHazardRecognizer.cpp
PostRASchedulerList.cpp
PreISelIntrinsicLowering.cpp Analysis: Move LibcallLoweringInfo from CodeGen to Analysis (#210322) 2026-08-19 22:23:06 +00:00
ProcessImplicitDefs.cpp
PrologEpilogInserter.cpp
PseudoProbeInserter.cpp
PseudoSourceValue.cpp
RDFGraph.cpp CodeGen: Pass the exception model to the EH register hooks (#216099) 2026-08-13 16:58:33 +00:00
RDFLiveness.cpp
RDFRegisters.cpp
ReachingDefAnalysis.cpp
README.txt
RegAllocBase.cpp
RegAllocBase.h
RegAllocBasic.cpp
RegAllocBasic.h
RegAllocEvictionAdvisor.cpp
RegAllocFast.cpp [RegAllocFast] Use MCRegister instead of MCPhysReg. NFC (#216926) 2026-08-18 16:07:42 +00:00
RegAllocGreedy.cpp [RegAlloc] Add a hook for default target specific CSR cost scale (#214857) 2026-08-25 12:19:56 -07:00
RegAllocGreedy.h
RegAllocPBQP.cpp
RegAllocPriorityAdvisor.cpp
RegAllocScore.cpp
RegAllocScore.h
RegisterBank.cpp
RegisterBankInfo.cpp
RegisterClassInfo.cpp [CodeGen] Avoid register pressure limit underflow (#216372) 2026-08-19 09:13:43 -05:00
RegisterCoalescer.cpp
RegisterCoalescer.h
RegisterPressure.cpp [CodeGen] Overload adjustLaneLiveness to cleanly split use cases (NFC) (#215594) 2026-08-20 15:52:12 +02:00
RegisterScavenging.cpp
RegisterUsageInfo.cpp
RegUsageInfoCollector.cpp
RegUsageInfoPropagate.cpp
Rematerializer.cpp [CodeGen] Delete empty sub-ranges after refinement of rematerializations (#215632) 2026-08-11 21:48:03 +02:00
RemoveLoadsIntoFakeUses.cpp
RemoveRedundantDebugValues.cpp
RenameIndependentSubregs.cpp
ReplaceWithVeclib.cpp
ResetMachineFunctionPass.cpp
SafeStack.cpp Analysis: Move LibcallLoweringInfo from CodeGen to Analysis (#210322) 2026-08-19 22:23:06 +00:00
SafeStackLayout.cpp
SafeStackLayout.h
SanitizerBinaryMetadata.cpp
ScheduleDAG.cpp
ScheduleDAGInstrs.cpp
ScheduleDAGPrinter.cpp
ScoreboardHazardRecognizer.cpp
SelectOptimize.cpp
ShadowStackGCLowering.cpp
ShrinkWrap.cpp
SjLjEHPrepare.cpp
SlotIndexes.cpp [SlotIndexes] Use analysis block numbers (NFC) (#215171) 2026-08-14 10:50:38 -07:00
SpillPlacement.cpp
SplitKit.cpp [SlotIndexes] Use analysis block numbers (NFC) (#215171) 2026-08-14 10:50:38 -07:00
SplitKit.h
StackColoring.cpp
StackFrameLayoutAnalysisPass.cpp
StackMapLivenessAnalysis.cpp
StackMaps.cpp
StackProtector.cpp Analysis: Move LibcallLoweringInfo from CodeGen to Analysis (#210322) 2026-08-19 22:23:06 +00:00
StackSlotColoring.cpp
StaticDataAnnotator.cpp
StaticDataSplitter.cpp
SwiftErrorValueTracking.cpp
SwitchLoweringUtils.cpp
TailDuplication.cpp
TailDuplicator.cpp
TargetFrameLoweringImpl.cpp
TargetInstrInfo.cpp
TargetLoweringBase.cpp [LLVM] Add llvm.vector.reduce.{min, max}imumnum (#218295) 2026-08-26 13:04:16 +02:00
TargetLoweringObjectFileImpl.cpp [MC][x86-64] Decide if each individual mergeable constants is small or large (#209881) 2026-08-11 15:37:33 -07:00
TargetOptionsImpl.cpp
TargetPassConfig.cpp [GISel] Drop isGISelCSEEnabled hook 2026-08-18 06:25:01 -07:00
TargetRegisterInfo.cpp PowerPC: Fix FMA reassociation crash on an undef multiply operand (#216602) 2026-08-17 04:15:51 +02:00
TargetSchedule.cpp
TargetSubtargetInfo.cpp
TwoAddressInstructionPass.cpp
TypePromotion.cpp [TypePromotion][RISCV][ARM] Ignore the condition operand of selects. (#215685) 2026-08-11 19:33:27 -07:00
UnreachableBlockElim.cpp
ValueTypes.cpp
VirtRegMap.cpp
VLIWMachineScheduler.cpp
WasmEHPrepare.cpp
WindowScheduler.cpp
WindowsSecureHotPatching.cpp
WinEHPrepare.cpp
XRayInstrumentation.cpp

//===---------------------------------------------------------------------===//

Common register allocation / spilling problem:

        mul lr, r4, lr
        str lr, [sp, #+52]
        ldr lr, [r1, #+32]
        sxth r3, r3
        ldr r4, [sp, #+52]
        mla r4, r3, lr, r4

can be:

        mul lr, r4, lr
        mov r4, lr
        str lr, [sp, #+52]
        ldr lr, [r1, #+32]
        sxth r3, r3
        mla r4, r3, lr, r4

and then "merge" mul and mov:

        mul r4, r4, lr
        str r4, [sp, #+52]
        ldr lr, [r1, #+32]
        sxth r3, r3
        mla r4, r3, lr, r4

It also increase the likelihood the store may become dead.

//===---------------------------------------------------------------------===//

bb27 ...
        ...
        %reg1037 = ADDri %reg1039, 1
        %reg1038 = ADDrs %reg1032, %reg1039, %noreg, 10
    Successors according to CFG: 0x8b03bf0 (#5)

bb76 (0x8b03bf0, LLVM BB @0x8b032d0, ID#5):
    Predecessors according to CFG: 0x8b0c5f0 (#3) 0x8b0a7c0 (#4)
        %reg1039 = PHI %reg1070, mbb<bb76.outer,0x8b0c5f0>, %reg1037, mbb<bb27,0x8b0a7c0>

Note ADDri is not a two-address instruction. However, its result %reg1037 is an
operand of the PHI node in bb76 and its operand %reg1039 is the result of the
PHI node. We should treat it as a two-address code and make sure the ADDri is
scheduled after any node that reads %reg1039.

//===---------------------------------------------------------------------===//

Use local info (i.e. register scavenger) to assign it a free register to allow
reuse:
        ldr r3, [sp, #+4]
        add r3, r3, #3
        ldr r2, [sp, #+8]
        add r2, r2, #2
        ldr r1, [sp, #+4]  <==
        add r1, r1, #1
        ldr r0, [sp, #+4]
        add r0, r0, #2

//===---------------------------------------------------------------------===//

LLVM aggressively lift CSE out of loop. Sometimes this can be negative side-
effects:

R1 = X + 4
R2 = X + 7
R3 = X + 15

loop:
load [i + R1]
...
load [i + R2]
...
load [i + R3]

Suppose there is high register pressure, R1, R2, R3, can be spilled. We need
to implement proper re-materialization to handle this:

R1 = X + 4
R2 = X + 7
R3 = X + 15

loop:
R1 = X + 4  @ re-materialized
load [i + R1]
...
R2 = X + 7 @ re-materialized
load [i + R2]
...
R3 = X + 15 @ re-materialized
load [i + R3]

Furthermore, with re-association, we can enable sharing:

R1 = X + 4
R2 = X + 7
R3 = X + 15

loop:
T = i + X
load [T + 4]
...
load [T + 7]
...
load [T + 15]
//===---------------------------------------------------------------------===//

It's not always a good idea to choose rematerialization over spilling. If all
the load / store instructions would be folded then spilling is cheaper because
it won't require new live intervals / registers. See 2003-05-31-LongShifts for
an example.

//===---------------------------------------------------------------------===//

With a copying garbage collector, derived pointers must not be retained across
collector safe points; the collector could move the objects and invalidate the
derived pointer. This is bad enough in the first place, but safe points can
crop up unpredictably. Consider:

        %array = load { i32, [0 x %obj] }** %array_addr
        %nth_el = getelementptr { i32, [0 x %obj] }* %array, i32 0, i32 %n
        %old = load %obj** %nth_el
        %z = div i64 %x, %y
        store %obj* %new, %obj** %nth_el

If the i64 division is lowered to a libcall, then a safe point will (must)
appear for the call site. If a collection occurs, %array and %nth_el no longer
point into the correct object.

The fix for this is to copy address calculations so that dependent pointers
are never live across safe point boundaries. But the loads cannot be copied
like this if there was an intervening store, so may be hard to get right.

Only a concurrent mutator can trigger a collection at the libcall safe point.
So single-threaded programs do not have this requirement, even with a copying
collector. Still, LLVM optimizations would probably undo a front-end's careful
work.

//===---------------------------------------------------------------------===//

The ocaml frametable structure supports liveness information. It would be good
to support it.

//===---------------------------------------------------------------------===//

The FIXME in ComputeCommonTailLength in BranchFolding.cpp needs to be
revisited. The check is there to work around a misuse of directives in inline
assembly.

//===---------------------------------------------------------------------===//

It would be good to detect collector/target compatibility instead of silently
doing the wrong thing.

//===---------------------------------------------------------------------===//

It would be really nice to be able to write patterns in .td files for copies,
which would eliminate a bunch of explicit predicates on them (e.g. no side
effects).  Once this is in place, it would be even better to have tblgen
synthesize the various copy insertion/inspection methods in TargetInstrInfo.

//===---------------------------------------------------------------------===//

Stack coloring improvements:

1. Do proper LiveStacks analysis on all stack objects including those which are
   not spill slots.
2. Reorder objects to fill in gaps between objects.
   e.g. 4, 1, <gap>, 4, 1, 1, 1, <gap>, 4 => 4, 1, 1, 1, 1, 4, 4

//===---------------------------------------------------------------------===//

The scheduler should be able to sort nearby instructions by their address. For
example, in an expanded memset sequence it's not uncommon to see code like this:

  movl $0, 4(%rdi)
  movl $0, 8(%rdi)
  movl $0, 12(%rdi)
  movl $0, 0(%rdi)

Each of the stores is independent, and the scheduler is currently making an
arbitrary decision about the order.

//===---------------------------------------------------------------------===//

Another opportunitiy in this code is that the $0 could be moved to a register:

  movl $0, 4(%rdi)
  movl $0, 8(%rdi)
  movl $0, 12(%rdi)
  movl $0, 0(%rdi)

This would save substantial code size, especially for longer sequences like
this. It would be easy to have a rule telling isel to avoid matching MOV32mi
if the immediate has more than some fixed number of uses. It's more involved
to teach the register allocator how to do late folding to recover from
excessive register pressure.