@@ -1027,39 +1027,45 @@ llvm::ConstantFoldCountZeros(Register Src, const MachineRegisterInfo &MRI,
10271027
10281028std::optional<SmallVector<APInt>>
10291029llvm::ConstantFoldICmp (unsigned Pred, const Register Op1, const Register Op2,
1030+ unsigned DstSizeInBits, unsigned ExtOp,
10301031 const MachineRegisterInfo &MRI) {
1032+ assert (ExtOp == TargetOpcode::G_SEXT || ExtOp == TargetOpcode::G_ZEXT ||
1033+ ExtOp == TargetOpcode::G_ANYEXT);
1034+
10311035 LLT Ty = MRI.getType (Op1);
10321036 if (Ty != MRI.getType (Op2))
10331037 return std::nullopt ;
10341038
1035- auto TryFoldScalar = [&MRI, Pred](Register LHS,
1036- Register RHS) -> std::optional<APInt> {
1039+ const int64_t Sign = ExtOp == TargetOpcode::G_SEXT ? -1 : 1 ;
1040+
1041+ auto TryFoldScalar = [&MRI, Pred, DstSizeInBits, Sign](
1042+ Register LHS, Register RHS) -> std::optional<APInt> {
10371043 auto LHSCst = getIConstantVRegVal (LHS, MRI);
10381044 auto RHSCst = getIConstantVRegVal (RHS, MRI);
10391045 if (!LHSCst || !RHSCst)
10401046 return std::nullopt ;
10411047
10421048 switch (Pred) {
10431049 case CmpInst::Predicate::ICMP_EQ:
1044- return APInt (/* numBits= */ 1 , LHSCst->eq (*RHSCst));
1050+ return APInt (DstSizeInBits, Sign * LHSCst->eq (*RHSCst), true );
10451051 case CmpInst::Predicate::ICMP_NE:
1046- return APInt (/* numBits= */ 1 , LHSCst->ne (*RHSCst));
1052+ return APInt (DstSizeInBits, Sign * LHSCst->ne (*RHSCst), true );
10471053 case CmpInst::Predicate::ICMP_UGT:
1048- return APInt (/* numBits= */ 1 , LHSCst->ugt (*RHSCst));
1054+ return APInt (DstSizeInBits, Sign * LHSCst->ugt (*RHSCst), true );
10491055 case CmpInst::Predicate::ICMP_UGE:
1050- return APInt (/* numBits= */ 1 , LHSCst->uge (*RHSCst));
1056+ return APInt (DstSizeInBits, Sign * LHSCst->uge (*RHSCst), true );
10511057 case CmpInst::Predicate::ICMP_ULT:
1052- return APInt (/* numBits= */ 1 , LHSCst->ult (*RHSCst));
1058+ return APInt (DstSizeInBits, Sign * LHSCst->ult (*RHSCst), true );
10531059 case CmpInst::Predicate::ICMP_ULE:
1054- return APInt (/* numBits= */ 1 , LHSCst->ule (*RHSCst));
1060+ return APInt (DstSizeInBits, Sign * LHSCst->ule (*RHSCst), true );
10551061 case CmpInst::Predicate::ICMP_SGT:
1056- return APInt (/* numBits= */ 1 , LHSCst->sgt (*RHSCst));
1062+ return APInt (DstSizeInBits, Sign * LHSCst->sgt (*RHSCst), true );
10571063 case CmpInst::Predicate::ICMP_SGE:
1058- return APInt (/* numBits= */ 1 , LHSCst->sge (*RHSCst));
1064+ return APInt (DstSizeInBits, Sign * LHSCst->sge (*RHSCst), true );
10591065 case CmpInst::Predicate::ICMP_SLT:
1060- return APInt (/* numBits= */ 1 , LHSCst->slt (*RHSCst));
1066+ return APInt (DstSizeInBits, Sign * LHSCst->slt (*RHSCst), true );
10611067 case CmpInst::Predicate::ICMP_SLE:
1062- return APInt (/* numBits= */ 1 , LHSCst->sle (*RHSCst));
1068+ return APInt (DstSizeInBits, Sign * LHSCst->sle (*RHSCst), true );
10631069 default :
10641070 return std::nullopt ;
10651071 }
0 commit comments