Index: lib/Analysis/ScalarEvolution.cpp =================================================================== --- lib/Analysis/ScalarEvolution.cpp +++ lib/Analysis/ScalarEvolution.cpp @@ -8617,8 +8617,14 @@ // going to compare the operands to RHS, we should be certain that we don't // need any size extensions for this. So let's decline all cases when the // sizes of types of LHS and RHS do not match. - // TODO: Maybe try to get RHS from sext to catch more cases? - if (getTypeSizeInBits(LHS->getType()) != getTypeSizeInBits(RHS->getType())) + auto *LTy = LHS->getType(); + // If RHS has a wider type, there is a chance that RHS can be stripped from + // extension. + if (getTypeSizeInBits(LTy) < getTypeSizeInBits(RHS->getType())) + RHS = GetOpFromSExt(RHS); + + // If the sizes still don't match, there is nothing we can do. + if (getTypeSizeInBits(LTy) != getTypeSizeInBits(RHS->getType())) return false; // Should not overflow. @@ -8627,7 +8633,7 @@ auto *LL = LHSAddExpr->getOperand(0); auto *LR = LHSAddExpr->getOperand(1); - auto *MinusOne = getNegativeSCEV(getOne(RHS->getType())); + auto *MinusOne = getNegativeSCEV(getOne(LTy)); // Checks that S1 >= 0 && S2 > RHS, trivially or using the found context. auto IsSumGreaterThanRHS = [&](const SCEV *S1, const SCEV *S2) {