[llvm] [FuncSpec] Improve handling of Comparison Instructions (PR #114073)

Hari Limaye via llvm-commits llvm-commits at lists.llvm.org
Wed Oct 30 16:28:02 PDT 2024


================
@@ -468,16 +468,26 @@ Constant *InstCostVisitor::visitCastInst(CastInst &I) {
 Constant *InstCostVisitor::visitCmpInst(CmpInst &I) {
   assert(LastVisited != KnownConstants.end() && "Invalid iterator!");
 
-  bool Swap = I.getOperand(1) == LastVisited->first;
-  Value *V = Swap ? I.getOperand(0) : I.getOperand(1);
+  Constant *Const = LastVisited->second;
+  bool ConstOnRHS = I.getOperand(1) == LastVisited->first;
+  Value *V = ConstOnRHS ? I.getOperand(0) : I.getOperand(1);
   Constant *Other = findConstantFor(V, KnownConstants);
-  if (!Other)
-    return nullptr;
 
-  Constant *Const = LastVisited->second;
-  return Swap ?
-        ConstantFoldCompareInstOperands(I.getPredicate(), Other, Const, DL)
-      : ConstantFoldCompareInstOperands(I.getPredicate(), Const, Other, DL);
+  if (Other) {
+    if (ConstOnRHS) {
+      std::swap(Const, Other);
+    }
+    return ConstantFoldCompareInstOperands(I.getPredicate(), Const, Other, DL);
+  }
+
+  // If we haven't found Other to be a specific constant value, we may still be
+  // able constant fold the comparison using information from the lattice value.
+  ValueLatticeElement ConstLV = ValueLatticeElement();
+  ConstLV.markConstant(Const);
+  const ValueLatticeElement &OtherLV = Solver.getLatticeValueFor(V);
----------------
hazzlim wrote:

Good point - I've added a commit to mark the `Solver` as `const` in `InstCostVisitor` to document & enforce that we should never alter the state of the `Solver` when evaluating a candidate specialization's bonus.


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


More information about the llvm-commits mailing list