[llvm] [FuncSpec] Improve estimation of select instruction. (PR #111176)

via llvm-commits llvm-commits at lists.llvm.org
Fri Oct 4 08:42:12 PDT 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-function-specialization

Author: Alexandros Lamprineas (labrinea)

<details>
<summary>Changes</summary>

When propagating a constant to a select instruction we only consider the condition operand as the use. I am extending the logic to consider the true and false values too, in case the condition had been found to be constant in a previous propagation but halted.

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


1 Files Affected:

- (modified) llvm/lib/Transforms/IPO/FunctionSpecialization.cpp (+10-7) 


``````````diff
diff --git a/llvm/lib/Transforms/IPO/FunctionSpecialization.cpp b/llvm/lib/Transforms/IPO/FunctionSpecialization.cpp
index 548335d750e33d..7d109af9091479 100644
--- a/llvm/lib/Transforms/IPO/FunctionSpecialization.cpp
+++ b/llvm/lib/Transforms/IPO/FunctionSpecialization.cpp
@@ -423,13 +423,16 @@ Constant *InstCostVisitor::visitGetElementPtrInst(GetElementPtrInst &I) {
 Constant *InstCostVisitor::visitSelectInst(SelectInst &I) {
   assert(LastVisited != KnownConstants.end() && "Invalid iterator!");
 
-  if (I.getCondition() != LastVisited->first)
-    return nullptr;
-
-  Value *V = LastVisited->second->isZeroValue() ? I.getFalseValue()
-                                                : I.getTrueValue();
-  Constant *C = findConstantFor(V, KnownConstants);
-  return C;
+  if (I.getCondition() == LastVisited->first) {
+    Value *V = LastVisited->second->isZeroValue() ? I.getFalseValue()
+                                                  : I.getTrueValue();
+    return findConstantFor(V, KnownConstants);
+  }
+  if (Constant *Condition = findConstantFor(I.getCondition(), KnownConstants))
+    if (I.getTrueValue() == LastVisited->first && Condition->isOneValue() ||
+        I.getFalseValue() == LastVisited->first && Condition->isZeroValue())
+      return LastVisited->second;
+  return nullptr;
 }
 
 Constant *InstCostVisitor::visitCastInst(CastInst &I) {

``````````

</details>


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


More information about the llvm-commits mailing list