Skip to content

Commit

Permalink
[LSR] Return nullptr from getExpr if the result isn't invertible.
Browse files Browse the repository at this point in the history
getExpr is missing a check to make sure the result is invertible.
This can lead to incorrect results, so return nullptr in those cases
like in other places in IVUsers.

Fixes #62660.

Reviewed By: qcolombet

Differential Revision: https://reviews.llvm.org/D153202
  • Loading branch information
fhahn committed Jun 22, 2023
1 parent 1d56510 commit 3a57152
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 11 deletions.
3 changes: 2 additions & 1 deletion llvm/include/llvm/Analysis/IVUsers.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,8 @@ class IVUsers {
/// value of the OperandValToReplace of the given IVStrideUse.
const SCEV *getReplacementExpr(const IVStrideUse &IU) const;

/// getExpr - Return the expression for the use.
/// getExpr - Return the expression for the use. Returns nullptr if the result
/// is not invertible.
const SCEV *getExpr(const IVStrideUse &IU) const;

const SCEV *getStride(const IVStrideUse &IU, const Loop *L) const;
Expand Down
11 changes: 9 additions & 2 deletions llvm/lib/Analysis/IVUsers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -334,8 +334,15 @@ const SCEV *IVUsers::getReplacementExpr(const IVStrideUse &IU) const {

/// getExpr - Return the expression for the use.
const SCEV *IVUsers::getExpr(const IVStrideUse &IU) const {
return normalizeForPostIncUse(getReplacementExpr(IU), IU.getPostIncLoops(),
*SE);
const SCEV *Replacement = getReplacementExpr(IU);
const SCEV *Normalized =
normalizeForPostIncUse(Replacement, IU.getPostIncLoops(), *SE);
const SCEV *Denormalized =
denormalizeForPostIncUse(Normalized, IU.getPostIncLoops(), *SE);
// If the normalized expression isn't invertible.
if (Denormalized != Replacement)
return nullptr;
return Normalized;
}

static const SCEVAddRecExpr *findAddRecForLoop(const SCEV *S, const Loop *L) {
Expand Down
4 changes: 4 additions & 0 deletions llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2707,6 +2707,8 @@ void LSRInstance::CollectInterestingTypesAndFactors() {
SmallVector<const SCEV *, 4> Worklist;
for (const IVStrideUse &U : IU) {
const SCEV *Expr = IU.getExpr(U);
if (!Expr)
continue;

// Collect interesting types.
Types.insert(SE.getEffectiveSCEVType(Expr->getType()));
Expand Down Expand Up @@ -3325,6 +3327,8 @@ void LSRInstance::CollectFixupsAndInitialFormulae() {
}

const SCEV *S = IU.getExpr(U);
if (!S)
continue;
PostIncLoopSet TmpPostIncLoops = U.getPostIncLoops();

// Equality (== and !=) ICmps are special. We can rewrite (i == N) as
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 2
; RUN: opt -loop-reduce -verify-scev -S %s | FileCheck %s

; XFAIL: *
; REQUIRES: asserts

target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-apple-macos"

Expand All @@ -12,11 +9,16 @@ define i64 @test_pr62660() {
; CHECK-NEXT: entry:
; CHECK-NEXT: br label [[LOOP:%.*]]
; CHECK: loop:
; CHECK-NEXT: [[LSR_IV1:%.*]] = phi i32 [ [[LSR_IV_NEXT2:%.*]], [[LOOP]] ], [ 65533, [[ENTRY:%.*]] ]
; CHECK-NEXT: [[LSR_IV:%.*]] = phi i64 [ [[LSR_IV_NEXT:%.*]], [[LOOP]] ], [ -1, [[ENTRY]] ]
; CHECK-NEXT: [[LSR_IV:%.*]] = phi i64 [ [[LSR_IV_NEXT:%.*]], [[LOOP]] ], [ -1, [[ENTRY:%.*]] ]
; CHECK-NEXT: [[IV:%.*]] = phi i32 [ 0, [[ENTRY]] ], [ [[IV_NEXT:%.*]], [[LOOP]] ]
; CHECK-NEXT: [[TMP0:%.*]] = add i64 [[LSR_IV]], 1
; CHECK-NEXT: [[TMP:%.*]] = trunc i64 [[TMP0]] to i32
; CHECK-NEXT: [[CONV1:%.*]] = and i32 [[TMP]], 65535
; CHECK-NEXT: [[ADD:%.*]] = add nsw i32 [[IV]], -1
; CHECK-NEXT: [[SUB:%.*]] = add i32 [[ADD]], [[CONV1]]
; CHECK-NEXT: [[IV_NEXT]] = add nuw nsw i32 [[IV]], 1
; CHECK-NEXT: [[LSR_IV_NEXT]] = add nsw i64 [[LSR_IV]], 1
; CHECK-NEXT: [[LSR_IV_NEXT2]] = add nuw nsw i32 [[LSR_IV1]], 2
; CHECK-NEXT: [[CMP:%.*]] = icmp sgt i32 [[LSR_IV_NEXT2]], 8
; CHECK-NEXT: [[CMP:%.*]] = icmp sgt i32 [[SUB]], 8
; CHECK-NEXT: br i1 [[CMP]], label [[LOOP]], label [[EXIT:%.*]]
; CHECK: exit:
; CHECK-NEXT: ret i64 [[LSR_IV_NEXT]]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,15 @@ define void @foo() {
; CHECK: bb4:
; CHECK-NEXT: [[LSR_IV1:%.*]] = phi i16 [ [[LSR_IV_NEXT2:%.*]], [[BB13:%.*]] ], [ 6, [[BB:%.*]] ]
; CHECK-NEXT: [[LSR_IV:%.*]] = phi i64 [ [[LSR_IV_NEXT]], [[BB13]] ], [ 8589934593, [[BB]] ]
; CHECK-NEXT: [[T5:%.*]] = phi i64 [ 2, [[BB]] ], [ [[T14:%.*]], [[BB13]] ]
; CHECK-NEXT: [[T6:%.*]] = add i64 [[T5]], 4
; CHECK-NEXT: [[T7:%.*]] = trunc i64 [[T6]] to i16
; CHECK-NEXT: [[T8:%.*]] = urem i16 [[T7]], 3
; CHECK-NEXT: [[T9:%.*]] = mul i16 [[T8]], 2
; CHECK-NEXT: [[LSR_IV_NEXT]] = add nuw nsw i64 [[LSR_IV]], 25769803776
; CHECK-NEXT: [[LSR_IV_NEXT2]] = add nuw nsw i16 [[LSR_IV1]], 6
; CHECK-NEXT: [[T10:%.*]] = icmp eq i16 1, 0
; CHECK-NEXT: [[T14]] = add nuw nsw i64 [[T5]], 6
; CHECK-NEXT: [[T10:%.*]] = icmp eq i16 [[T9]], 1
; CHECK-NEXT: br i1 [[T10]], label [[BB11:%.*]], label [[BB13]]
; CHECK: bb11:
; CHECK-NEXT: [[T12:%.*]] = udiv i16 1, [[LSR_IV1]]
Expand Down

0 comments on commit 3a57152

Please sign in to comment.