[llvm] PPC: Avoid constructing TargetTransformInfo in isHardwareLoopProfitable (PR #209972)

via llvm-commits llvm-commits at lists.llvm.org
Wed Jul 15 22:58:55 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-backend-powerpc

Author: Matt Arsenault (arsenm)

<details>
<summary>Changes</summary>

PPCTTIImpl::isHardwareLoopProfitable constructed a throwaway
TargetTransformInfo only to call CodeMetrics::analyzeBasicBlock. TTI
may contain pipeline configuration state, and should only be queried
from the PassManager. It was also particularly ugly to construct one here
given that we're inside a TTIImpl.

Most of what analyzeBasicBlock computes was not used here. Directly
sum the instruction code size costs, which was the only used component
of the analysis.

Co-authored-by: Claude (Claude Opus 4.8) <noreply@<!-- -->anthropic.com>

---
Full diff: https://github.com/llvm/llvm-project/pull/209972.diff


1 Files Affected:

- (modified) llvm/lib/Target/PowerPC/PPCTargetTransformInfo.cpp (+10-8) 


``````````diff
diff --git a/llvm/lib/Target/PowerPC/PPCTargetTransformInfo.cpp b/llvm/lib/Target/PowerPC/PPCTargetTransformInfo.cpp
index a64471b627d7d..b3a54fd4c1ef4 100644
--- a/llvm/lib/Target/PowerPC/PPCTargetTransformInfo.cpp
+++ b/llvm/lib/Target/PowerPC/PPCTargetTransformInfo.cpp
@@ -352,20 +352,22 @@ bool PPCTTIImpl::isHardwareLoopProfitable(Loop *L, ScalarEvolution &SE,
   TargetSchedModel SchedModel;
   SchedModel.init(ST);
 
-  // FIXME: Sure there is no other way to get TTI? This should be cheap though.
-  TargetTransformInfo TTI =
-      TM.getTargetTransformInfo(*L->getHeader()->getParent());
-
   // Do not convert small short loops to CTR loop.
   unsigned ConstTripCount = SE.getSmallConstantTripCount(L);
   if (ConstTripCount && ConstTripCount < SmallCTRLoopThreshold) {
     SmallPtrSet<const Value *, 32> EphValues;
     CodeMetrics::collectEphemeralValues(L, &AC, EphValues);
-    CodeMetrics Metrics;
-    for (BasicBlock *BB : L->blocks())
-      Metrics.analyzeBasicBlock(BB, TTI, EphValues);
+    InstructionCost NumInsts;
+    for (BasicBlock *BB : L->blocks()) {
+      for (Instruction &I : *BB) {
+        if (EphValues.count(&I))
+          continue;
+        SmallVector<const Value *, 4> Operands(I.operand_values());
+        NumInsts += getInstructionCost(&I, Operands, TTI::TCK_CodeSize);
+      }
+    }
     // 6 is an approximate latency for the mtctr instruction.
-    if (Metrics.NumInsts <= (6 * SchedModel.getIssueWidth()))
+    if (NumInsts <= (6 * SchedModel.getIssueWidth()))
       return false;
   }
 

``````````

</details>


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


More information about the llvm-commits mailing list