[llvm] [GlobalISel] Take the result size into account when const folding icmp (PR #134365)

Matt Arsenault via llvm-commits llvm-commits at lists.llvm.org
Sun Apr 13 03:45:36 PDT 2025


================
@@ -1027,39 +1027,45 @@ llvm::ConstantFoldCountZeros(Register Src, const MachineRegisterInfo &MRI,
 
 std::optional<SmallVector<APInt>>
 llvm::ConstantFoldICmp(unsigned Pred, const Register Op1, const Register Op2,
+                       unsigned DstScalarSizeInBits, unsigned ExtOp,
                        const MachineRegisterInfo &MRI) {
-  LLT Ty = MRI.getType(Op1);
-  if (Ty != MRI.getType(Op2))
-    return std::nullopt;
+  assert(ExtOp == TargetOpcode::G_SEXT || ExtOp == TargetOpcode::G_ZEXT ||
+         ExtOp == TargetOpcode::G_ANYEXT);
+
+  const LLT Ty = MRI.getType(Op1);
 
-  auto TryFoldScalar = [&MRI, Pred](Register LHS,
-                                    Register RHS) -> std::optional<APInt> {
+  auto TryFoldScalar = [&](Register LHS, Register RHS) -> std::optional<APInt> {
     auto LHSCst = getIConstantVRegVal(LHS, MRI);
     auto RHSCst = getIConstantVRegVal(RHS, MRI);
     if (!LHSCst || !RHSCst)
       return std::nullopt;
 
+    const APInt FalseCst = APInt::getZero(DstScalarSizeInBits);
+    const APInt TrueCst = (ExtOp == TargetOpcode::G_SEXT)
+                              ? APInt::getAllOnes(DstScalarSizeInBits)
+                              : APInt::getOneBitSet(DstScalarSizeInBits, 0);
----------------
arsenm wrote:

Can you make this a helper function, and use it in the implementation below? Should avoid eagerly constructing these two APInts 

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


More information about the llvm-commits mailing list