Skip to content

Commit 88f40f2

Browse files
Copilotjakobbotsch
andauthored
Reject minimum-value steps before induction-variable scaling
Co-authored-by: jakobbotsch <[email protected]>
1 parent 43776e6 commit 88f40f2

1 file changed

Lines changed: 10 additions & 12 deletions

File tree

‎src/coreclr/jit/inductionvariableopts.cpp‎

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2056,12 +2056,6 @@ static T Gcd(T a, T b)
20562056
{
20572057
while (a != 0)
20582058
{
2059-
if (a == -1)
2060-
{
2061-
// Avoid signed remainder overflow for MinValue % -1.
2062-
return -1;
2063-
}
2064-
20652059
T newA = b % a;
20662060
T newB = a;
20672061
a = newA;
@@ -2159,6 +2153,12 @@ ScevAddRec* StrengthReductionContext::ComputeRephrasableIVByScaling(ScevAddRec*
21592153
return nullptr;
21602154
}
21612155

2156+
// Avoid the edge case of computing MinValue / -1.
2157+
if (((T)iv1Step == std::numeric_limits<T>::min()) || ((T)iv2Step == std::numeric_limits<T>::min()))
2158+
{
2159+
return nullptr;
2160+
}
2161+
21622162
T gcd = Gcd((T)iv1Step, (T)iv2Step);
21632163

21642164
if ((!allowRephrasingByScalingIV1 && (gcd != (T)iv1Step)) || (!allowRephrasingByScalingIV2 && (gcd != (T)iv2Step)))
@@ -2261,12 +2261,10 @@ GenTree* StrengthReductionContext::RephraseIV(ScevAddRec* iv, ScevAddRec* source
22612261

22622262
assert(iv->Type == sourceIV->Type);
22632263

2264-
// Use unsigned negation for a source step of -1 so that MinValue wraps instead of overflowing.
22652264
if (iv->Type == TYP_INT)
22662265
{
2267-
assert(((int32_t)sourceIVStep == -1) || ((int32_t)ivStep % (int32_t)sourceIVStep == 0));
2268-
int32_t scale =
2269-
((int32_t)sourceIVStep == -1) ? (int32_t)(0u - (uint32_t)ivStep) : (int32_t)ivStep / (int32_t)sourceIVStep;
2266+
assert((int32_t)ivStep % (int32_t)sourceIVStep == 0);
2267+
int32_t scale = (int32_t)ivStep / (int32_t)sourceIVStep;
22702268
if (isPow2(scale))
22712269
{
22722270
return m_compiler->gtNewOperNode(GT_LSH, TYP_INT, sourceTree,
@@ -2280,8 +2278,8 @@ GenTree* StrengthReductionContext::RephraseIV(ScevAddRec* iv, ScevAddRec* source
22802278

22812279
if (iv->Type == TYP_LONG)
22822280
{
2283-
assert((sourceIVStep == -1) || (ivStep % sourceIVStep == 0));
2284-
int64_t scale = (sourceIVStep == -1) ? (int64_t)(0ull - (uint64_t)ivStep) : ivStep / sourceIVStep;
2281+
assert(ivStep % sourceIVStep == 0);
2282+
int64_t scale = ivStep / sourceIVStep;
22852283
if (isPow2(scale))
22862284
{
22872285
return m_compiler->gtNewOperNode(GT_LSH, TYP_LONG, sourceTree,

0 commit comments

Comments
 (0)