Skip to content

Commit 8fc0aca

Browse files
authored
[SCEV] Support larger than 64-bit types in ashr(add(shl(x, n), c), m) (#71600)
In commit 5a9a02f scalar evolution got support for computing SCEV:s for (ashr(add(shl(x, n), c), m)) constructs. The code however used APInt::getZExtValue without first checking that the APInt would fit inside an uint64_t. When for example using 128-bit types we ended up in assertion failures (or maybe miscompiles in non-assert builds). This patch simply avoid converting from APInt to uint64_t when creating the truncated constant. We can just truncate the APInt instead.
1 parent 3716b5b commit 8fc0aca

File tree

2 files changed

+22
-6
lines changed

2 files changed

+22
-6
lines changed

llvm/lib/Analysis/ScalarEvolution.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7888,8 +7888,7 @@ const SCEV *ScalarEvolution::createSCEV(Value *V) {
78887888
// same type, so create a new Constant with type same as TruncTy.
78897889
// Also, the Add constant should be shifted right by AShr amount.
78907890
APInt AddOperand = AddOperandCI->getValue().ashr(AShrAmt);
7891-
AddConstant = getConstant(TruncTy, AddOperand.getZExtValue(),
7892-
AddOperand.isSignBitSet());
7891+
AddConstant = getConstant(AddOperand.trunc(BitWidth - AShrAmt));
78937892
// we model the expression as sext(add(trunc(A), c << n)), since the
78947893
// sext(trunc) part is already handled below, we create a
78957894
// AddExpr(TruncExp) which will be used later.
Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,36 @@
11
; NOTE: Assertions have been autogenerated by utils/update_analyze_test_checks.py UTC_ARGS: --version 2
22
; RUN: opt < %s -disable-output "-passes=print<scalar-evolution>" 2>&1 | FileCheck %s
33

4-
define i64 @test(i64 %a) {
5-
; CHECK-LABEL: 'test'
6-
; CHECK-NEXT: Classifying expressions for: @test
4+
define i64 @test64(i64 %a) {
5+
; CHECK-LABEL: 'test64'
6+
; CHECK-NEXT: Classifying expressions for: @test64
77
; CHECK-NEXT: %add = shl i64 %a, 8
88
; CHECK-NEXT: --> (256 * %a) U: [0,-255) S: [-9223372036854775808,9223372036854775553)
99
; CHECK-NEXT: %shl = add i64 %add, 256
1010
; CHECK-NEXT: --> (256 + (256 * %a)) U: [0,-255) S: [-9223372036854775808,9223372036854775553)
1111
; CHECK-NEXT: %ashr = ashr exact i64 %shl, 8
1212
; CHECK-NEXT: --> (sext i56 (1 + (trunc i64 %a to i56)) to i64) U: [-36028797018963968,36028797018963968) S: [-36028797018963968,36028797018963968)
13-
; CHECK-NEXT: Determining loop execution counts for: @test
13+
; CHECK-NEXT: Determining loop execution counts for: @test64
1414
;
1515
%add = shl i64 %a, 8
1616
%shl = add i64 %add, 256
1717
%ashr = ashr exact i64 %shl, 8
1818
ret i64 %ashr
1919
}
20+
21+
define i128 @test128(i128 %a) {
22+
; CHECK-LABEL: 'test128'
23+
; CHECK-NEXT: Classifying expressions for: @test128
24+
; CHECK-NEXT: %shl = shl i128 %a, 4
25+
; CHECK-NEXT: --> (16 * %a) U: [0,-15) S: [-170141183460469231731687303715884105728,170141183460469231731687303715884105713)
26+
; CHECK-NEXT: %add = add i128 %shl, -55
27+
; CHECK-NEXT: --> (-55 + (16 * %a)) U: [-55,-70) S: [170141183460469231731687303715884105673,170141183460469231731687303715884105658)
28+
; CHECK-NEXT: %ashr = ashr i128 %add, 4
29+
; CHECK-NEXT: --> (sext i124 (-4 + (trunc i128 %a to i124)) to i128) U: [-10633823966279326983230456482242756608,10633823966279326983230456482242756608) S: [-10633823966279326983230456482242756608,10633823966279326983230456482242756608)
30+
; CHECK-NEXT: Determining loop execution counts for: @test128
31+
;
32+
%shl = shl i128 %a, 4
33+
%add = add i128 %shl, -55
34+
%ashr = ashr i128 %add, 4
35+
ret i128 %ashr
36+
}

0 commit comments

Comments
 (0)