Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions llvm/lib/Analysis/ScalarEvolution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5090,6 +5090,33 @@ ScalarEvolution::proveNoWrapViaConstantRanges(const SCEVAddRecExpr *AR) {
return Result;
}

/// Return true if \p AR is known to not wrap via the loop's backedge-taken
/// count.
static SCEV::NoWrapFlags proveNoWrapViaBTC(const SCEVAddRecExpr *AR,
ScalarEvolution &SE) {
SCEV::NoWrapFlags Result = SCEV::FlagAnyWrap;
if (AR->hasNoUnsignedWrap() && AR->hasNoSignedWrap())
return Result;

const Loop *L = AR->getLoop();
const SCEV *BTC = SE.getBackedgeTakenCount(L);
if (isa<SCEVCouldNotCompute>(BTC) || !AR->getStepRecurrence(SE)->isOne())
return Result;

Type *WTy = SE.getWiderType(AR->getType(), BTC->getType());
// If AR's type is wider than BTC, we can zero extend BTC, otherwise bail out.
if (WTy != AR->getType() || !WTy->isIntegerTy())
return Result;

const SCEV *ExtBTC = SE.getNoopOrZeroExtend(BTC, WTy);
// AR has a step of 1, it is NUW/NSW if Start + BTC >= Start.
if (!AR->hasNoUnsignedWrap() &&
SE.willNotOverflow(Instruction::Add, false, AR->getStart(), ExtBTC))
Result = ScalarEvolution::setFlags(Result, SCEV::FlagNUW);

return Result;
}

SCEV::NoWrapFlags
ScalarEvolution::proveNoSignedWrapViaInduction(const SCEVAddRecExpr *AR) {
SCEV::NoWrapFlags Result = AR->getNoWrapFlags();
Expand Down Expand Up @@ -5750,6 +5777,9 @@ const SCEV *ScalarEvolution::createSimpleAffineAddRec(PHINode *PN,
setNoWrapFlags(const_cast<SCEVAddRecExpr *>(AR),
(SCEV::NoWrapFlags)(AR->getNoWrapFlags() |
proveNoWrapViaConstantRanges(AR)));
setNoWrapFlags(const_cast<SCEVAddRecExpr *>(AR),
(SCEV::NoWrapFlags)(AR->getNoWrapFlags() |
proveNoWrapViaBTC(AR, *this)));
}

// We can add Flags to the post-inc expression only if we
Expand Down Expand Up @@ -5881,6 +5911,9 @@ const SCEV *ScalarEvolution::createAddRecFromPHI(PHINode *PN) {
setNoWrapFlags(const_cast<SCEVAddRecExpr *>(AR),
(SCEV::NoWrapFlags)(AR->getNoWrapFlags() |
proveNoWrapViaConstantRanges(AR)));
setNoWrapFlags(const_cast<SCEVAddRecExpr *>(AR),
(SCEV::NoWrapFlags)(AR->getNoWrapFlags() |
proveNoWrapViaBTC(AR, *this)));
}

// We can add Flags to the post-inc expression only if we
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ define void @test(ptr %p) {
; CHECK-NEXT: %iv2.ext = sext i32 %iv2 to i64
; CHECK-NEXT: --> (sext i32 {%iv,+,1}<%loop2> to i64) U: [-2147483648,2147483648) S: [-2147483648,2147483648) Exits: <<Unknown>> LoopDispositions: { %loop.header: Variant, %loop2: Computable, %loop3: Invariant }
; CHECK-NEXT: %iv3 = phi i64 [ %iv2.ext, %loop2.end ], [ %iv3.next, %loop3 ]
; CHECK-NEXT: --> {(sext i32 {%iv,+,1}<%loop2> to i64),+,1}<nsw><%loop3> U: [-2147483648,2147483648) S: [-2147483648,2147483648) Exits: (sext i32 {%iv,+,1}<%loop2> to i64) LoopDispositions: { %loop3: Computable, %loop.header: Variant }
; CHECK-NEXT: --> {(sext i32 {%iv,+,1}<%loop2> to i64),+,1}<nuw><nsw><%loop3> U: [-2147483648,2147483648) S: [-2147483648,2147483648) Exits: (sext i32 {%iv,+,1}<%loop2> to i64) LoopDispositions: { %loop3: Computable, %loop.header: Variant }
; CHECK-NEXT: %iv3.next = add nsw i64 %iv3, 1
; CHECK-NEXT: --> {(1 + (sext i32 {%iv,+,1}<%loop2> to i64))<nsw>,+,1}<nsw><%loop3> U: [-2147483647,2147483649) S: [-2147483647,2147483649) Exits: (1 + (sext i32 {%iv,+,1}<%loop2> to i64))<nsw> LoopDispositions: { %loop3: Computable, %loop.header: Variant }
; CHECK-NEXT: %iv.next = trunc i64 %iv3 to i32
Expand Down
Loading