[llvm] [MachineLICM] Rematerialize instructions that may be hoisted before LICM (PR #158479)

via llvm-commits llvm-commits at lists.llvm.org
Tue Sep 23 08:22:36 PDT 2025


================
@@ -1744,13 +1759,97 @@ bool MachineLICMImpl::isTgtHotterThanSrc(MachineBasicBlock *SrcBlock,
   return Ratio > BlockFrequencyRatioThreshold;
 }
 
+/// Rematerialize instructions into cycles before Machine LICM,
+/// since LICM in the middle-end hoisted every instructions without considering
+/// register pressure.
+bool MachineLICMImpl::rematerializeIntoCycle(MachineCycle *Cycle,
+                                             MachineInstr &I) {
+  LLVM_DEBUG(dbgs() << "Rematerialization: Finding sink block for: " << I);
+  MachineBasicBlock *Preheader = Cycle->getCyclePreheader();
+  assert(Preheader && "Cycle sink needs a preheader block");
+  MachineBasicBlock *SinkBlock = nullptr;
+  const MachineOperand &MO = I.getOperand(0);
----------------
sharkautarch wrote:

Because this is only checking the first def of MachineInstr `I`, and, unlike in `aggressivelySinkIntoCycle()`, instruction candidates (for `rematerializeIntoCycle()`) with more than one non-dead defs aren't rejected, there's probably a correctness issue here.

You should change this to either account for all non-dead defs of `I`, or simply do an early-return for insns that have more than one non-dead defs. For the latter approach, you can simply copy my code here: https://github.com/llvm/llvm-project/compare/main...sharkautarch:llvm-project:MIRAggrSink_ignoreDeadDefs

https://github.com/llvm/llvm-project/pull/158479


More information about the llvm-commits mailing list