[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
Mon Apr 7 04:32:34 PDT 2025


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

Should defer this until we need the constant 

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


More information about the llvm-commits mailing list