[llvm] [SLSR] Adding a cost model considering register pressure (PR #213808)

Igor Wodiany via llvm-commits llvm-commits at lists.llvm.org
Thu Aug 13 03:13:30 PDT 2026


================
@@ -1362,11 +1376,124 @@ bool StraightLineStrengthReduceLegacyPass::runOnFunction(Function &F) {
   return StraightLineStrengthReduce(DL, DT, SE, TTI).runOnFunction(F);
 }
 
+// Go through all operands of instruction, and check if any operand is used in
+// another block different from the instruction's block and the other use is
+// not rewritable. return true if such operand is found, otherwise return false.
+bool StraightLineStrengthReduce::
+    hasOperandsUsedInNonRewritableUsersInAnotherBlock(
+        llvm::Instruction *Inst) const {
+  llvm::BasicBlock *InstBB = Inst->getParent();
+
+  for (Value *OpVal : Inst->operand_values()) {
+    auto *OpInst = dyn_cast<Instruction>(OpVal);
+    if (!OpInst)
+      continue;
+
+    for (const User *U : OpInst->users())
+      if (auto *UI = dyn_cast<Instruction>(U)) {
+        if (UI->isDebugOrPseudoInst())
+          continue;
+        if (UI->getParent() != InstBB && !hasRewritableCandidates(UI)) {
+          LLVM_DEBUG(dbgs()
+                     << "Inst's operand is used in another block "
+                     << "("
+                     << (InstBB->hasName() ? InstBB->getName() : "unnamed")
+                     << " -> "
+                     << (UI->getParent() && UI->getParent()->hasName()
+                             ? UI->getParent()->getName()
+                             : "unnamed")
+                     << ") " << *UI << "\n");
+          return true;
+        }
+      }
+  }
+  return false;
+}
+
+bool StraightLineStrengthReduce::hasRewritableCandidates(
+    const Instruction *Inst) const {
+  if (!RewriteCandidates.count(Inst))
+    return false;
+
+  for (const Candidate *C : RewriteCandidates.at(Inst))
+    if (C->Basis)
+      return true;
+
+  return false;
+}
+
+// Assign a monotonically increasing index to each (non-debug/pseudo)
+// instruction in BB so in-block distances can be queried in O(1) once built.
+static DenseMap<const Instruction *, int>
----------------
IgWod wrote:

Should the function take a map as an argument to potentially avoid a copy on return? Not sure what's a LLVM guidance on that.

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


More information about the llvm-commits mailing list