Skip to content

Commit 047c0e9

Browse files
committed
[SCEV] Fix BinomialCoefficient Iteration to fit in W bits
BinomialCoefficient computes the value of W-bit IV at iteration It of a loop. When W is 1, we can call multiplicative inverse on 0 which triggers an assert since 1b76120. Since the arithmetic is supposed to wrap if It or K does not fit in W bits, do the truncation into W bits after we do the shift. Fixes #87798
1 parent 629bff8 commit 047c0e9

File tree

2 files changed

+2
-4
lines changed

2 files changed

+2
-4
lines changed

llvm/lib/Analysis/ScalarEvolution.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -928,10 +928,9 @@ static const SCEV *BinomialCoefficient(const SCEV *It, unsigned K,
928928
APInt OddFactorial(W, 1);
929929
unsigned T = 1;
930930
for (unsigned i = 3; i <= K; ++i) {
931-
APInt Mult(W, i);
932-
unsigned TwoFactors = Mult.countr_zero();
931+
unsigned TwoFactors = countr_zero(i);
933932
T += TwoFactors;
934-
Mult.lshrInPlace(TwoFactors);
933+
APInt Mult(W, i >> TwoFactors);
935934
OddFactorial *= Mult;
936935
}
937936

llvm/test/Transforms/IndVarSimplify/pr87798.ll

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
; RUN: opt -S -passes='indvars' -verify-scev < %s | FileCheck %s
22

33
; REQUIRES: asserts
4-
; XFAIL: *
54

65
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128-ni:1-p2:32:8:8:32-ni:2"
76
target triple = "x86_64-unknown-linux-gnu"

0 commit comments

Comments
 (0)