Skip to content

Commit fa12df5

Browse files
nikictstellar
authored andcommitted
[SCEV] Check correct value for UB (#124302)
This is a followup to #117152. That patch introduced a check for UB/poison on BEValue. However, the SCEV we're actually going to use is Shifted. In some cases, it's possible for Shifted to contain UB, while BEValue doesn't. In the test case the values are: BEValue: (-1 * (zext i8 (-83 + ((-83 /u {1,+,1}<%loop>) * {-1,+,-1}<%loop>)) to i32))<nuw><nsw> Shifted: (-173 + (-1 * (zext i8 ((-83 /u {0,+,1}<%loop>) * {0,+,-1}<%loop>) to i32))<nuw><nsw>)<nuw><nsw> Fixes #123550. (cherry picked from commit 07efe2c)
1 parent a97eed9 commit fa12df5

File tree

2 files changed

+16
-18
lines changed

2 files changed

+16
-18
lines changed

llvm/lib/Analysis/ScalarEvolution.cpp

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5917,20 +5917,18 @@ const SCEV *ScalarEvolution::createAddRecFromPHI(PHINode *PN) {
59175917
// PHI(f(0), f({1,+,1})) --> f({0,+,1})
59185918

59195919
// Do not allow refinement in rewriting of BEValue.
5920-
if (isGuaranteedNotToCauseUB(BEValue)) {
5921-
const SCEV *Shifted = SCEVShiftRewriter::rewrite(BEValue, L, *this);
5922-
const SCEV *Start = SCEVInitRewriter::rewrite(Shifted, L, *this, false);
5923-
if (Shifted != getCouldNotCompute() && Start != getCouldNotCompute() &&
5924-
::impliesPoison(BEValue, Start)) {
5925-
const SCEV *StartVal = getSCEV(StartValueV);
5926-
if (Start == StartVal) {
5927-
// Okay, for the entire analysis of this edge we assumed the PHI
5928-
// to be symbolic. We now need to go back and purge all of the
5929-
// entries for the scalars that use the symbolic expression.
5930-
forgetMemoizedResults(SymbolicName);
5931-
insertValueToMap(PN, Shifted);
5932-
return Shifted;
5933-
}
5920+
const SCEV *Shifted = SCEVShiftRewriter::rewrite(BEValue, L, *this);
5921+
const SCEV *Start = SCEVInitRewriter::rewrite(Shifted, L, *this, false);
5922+
if (Shifted != getCouldNotCompute() && Start != getCouldNotCompute() &&
5923+
isGuaranteedNotToCauseUB(Shifted) && ::impliesPoison(Shifted, Start)) {
5924+
const SCEV *StartVal = getSCEV(StartValueV);
5925+
if (Start == StartVal) {
5926+
// Okay, for the entire analysis of this edge we assumed the PHI
5927+
// to be symbolic. We now need to go back and purge all of the
5928+
// entries for the scalars that use the symbolic expression.
5929+
forgetMemoizedResults(SymbolicName);
5930+
insertValueToMap(PN, Shifted);
5931+
return Shifted;
59345932
}
59355933
}
59365934
}

llvm/test/Analysis/ScalarEvolution/pr123550.ll

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
; NOTE: Assertions have been autogenerated by utils/update_analyze_test_checks.py UTC_ARGS: --version 5
22
; RUN: opt -disable-output -passes='print<scalar-evolution>' < %s 2>&1 | FileCheck %s
33

4-
; FIXME: This is a miscompile.
4+
; %srem should have exit value 130.
55
define i32 @test() {
66
; CHECK-LABEL: 'test'
77
; CHECK-NEXT: Classifying expressions for: @test
88
; CHECK-NEXT: %phi = phi i32 [ -173, %bb ], [ %sub, %loop ]
9-
; CHECK-NEXT: --> (-173 + (-1 * (zext i8 ((-83 /u {0,+,1}<%loop>) * {0,+,-1}<%loop>) to i32))<nuw><nsw>)<nuw><nsw> U: empty-set S: empty-set Exits: -173 LoopDispositions: { %loop: Computable }
9+
; CHECK-NEXT: --> %phi U: [-173,1) S: [-173,1) Exits: -173 LoopDispositions: { %loop: Variant }
1010
; CHECK-NEXT: %iv2 = phi i32 [ 1, %bb ], [ %iv2.inc, %loop ]
1111
; CHECK-NEXT: --> {1,+,1}<nuw><nsw><%loop> U: [1,2) S: [1,2) Exits: 1 LoopDispositions: { %loop: Computable }
1212
; CHECK-NEXT: %srem = srem i32 729259140, %phi
13-
; CHECK-NEXT: --> (729259140 + (-1 * (-173 + (-1 * (zext i8 ((-83 /u {0,+,1}<%loop>) * {0,+,-1}<%loop>) to i32))<nuw><nsw>)<nuw><nsw> * (729259140 /u (-173 + (-1 * (zext i8 ((-83 /u {0,+,1}<%loop>) * {0,+,-1}<%loop>) to i32))<nuw><nsw>)<nuw><nsw>)))<nuw><nsw> U: empty-set S: empty-set Exits: 729259140 LoopDispositions: { %loop: Computable }
13+
; CHECK-NEXT: --> %srem U: [0,1073741824) S: [0,1073741824) Exits: 130 LoopDispositions: { %loop: Variant }
1414
; CHECK-NEXT: %trunc = trunc i32 %iv2 to i8
1515
; CHECK-NEXT: --> {1,+,1}<%loop> U: [1,2) S: [1,2) Exits: 1 LoopDispositions: { %loop: Computable }
1616
; CHECK-NEXT: %urem = urem i8 -83, %trunc
@@ -22,7 +22,7 @@ define i32 @test() {
2222
; CHECK-NEXT: %iv2.inc = add i32 %iv2, 1
2323
; CHECK-NEXT: --> {2,+,1}<nuw><nsw><%loop> U: [2,3) S: [2,3) Exits: 2 LoopDispositions: { %loop: Computable }
2424
; CHECK-NEXT: %srem.lcssa = phi i32 [ %srem, %loop ]
25-
; CHECK-NEXT: --> (729259140 + (-1 * (-173 + (-1 * (zext i8 ((-83 /u {0,+,1}<%loop>) * {0,+,-1}<%loop>) to i32))<nuw><nsw>)<nuw><nsw> * (729259140 /u (-173 + (-1 * (zext i8 ((-83 /u {0,+,1}<%loop>) * {0,+,-1}<%loop>) to i32))<nuw><nsw>)<nuw><nsw>)))<nuw><nsw> U: empty-set S: empty-set --> 729259140 U: [729259140,729259141) S: [729259140,729259141)
25+
; CHECK-NEXT: --> %srem U: [0,1073741824) S: [0,1073741824) --> 130 U: [130,131) S: [130,131)
2626
; CHECK-NEXT: Determining loop execution counts for: @test
2727
; CHECK-NEXT: Loop %loop: backedge-taken count is i32 0
2828
; CHECK-NEXT: Loop %loop: constant max backedge-taken count is i32 0

0 commit comments

Comments
 (0)