Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
31 changes: 29 additions & 2 deletions llvm/lib/Analysis/ScalarEvolution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2951,25 +2951,52 @@ const SCEV *ScalarEvolution::getAddExpr(SmallVectorImpl<const SCEV *> &Ops,
if (AddRecLoop == cast<SCEVAddRecExpr>(Ops[OtherIdx])->getLoop()) {
// Other + {A,+,B}<L> + {C,+,D}<L> --> Other + {A+C,+,B+D}<L>
SmallVector<const SCEV *, 4> AddRecOps(AddRec->operands());

// Track flags: start with the flags from the first AddRec.
bool AllHaveNSW = AddRec->hasNoSignedWrap();
bool AllHaveNUW = AddRec->hasNoUnsignedWrap();

for (; OtherIdx != Ops.size() && isa<SCEVAddRecExpr>(Ops[OtherIdx]);
++OtherIdx) {
const auto *OtherAddRec = cast<SCEVAddRecExpr>(Ops[OtherIdx]);
if (OtherAddRec->getLoop() == AddRecLoop) {
// Update flags based on this AddRec
if (!OtherAddRec->hasNoSignedWrap())
AllHaveNSW = false;
if (!OtherAddRec->hasNoUnsignedWrap())
AllHaveNUW = false;
for (unsigned i = 0, e = OtherAddRec->getNumOperands();
i != e; ++i) {
if (i >= AddRecOps.size()) {
append_range(AddRecOps, OtherAddRec->operands().drop_front(i));
break;
}
// Preserve no-wrap flags when combining AddRec operands.
SCEV::NoWrapFlags CombineFlags = SCEV::FlagAnyWrap;
if (auto *AR1 = dyn_cast<SCEVAddRecExpr>(AddRecOps[i]))
if (auto *AR2 =
dyn_cast<SCEVAddRecExpr>(OtherAddRec->getOperand(i))) {
if (AR1->hasNoSignedWrap() && AR2->hasNoSignedWrap())
CombineFlags = setFlags(CombineFlags, SCEV::FlagNSW);
if (AR1->hasNoUnsignedWrap() && AR2->hasNoUnsignedWrap())
CombineFlags = setFlags(CombineFlags, SCEV::FlagNUW);
}
SmallVector<const SCEV *, 2> TwoOps = {
AddRecOps[i], OtherAddRec->getOperand(i)};
AddRecOps[i] = getAddExpr(TwoOps, SCEV::FlagAnyWrap, Depth + 1);
AddRecOps[i] = getAddExpr(TwoOps, CombineFlags, Depth + 1);
}
Ops.erase(Ops.begin() + OtherIdx); --OtherIdx;
}
}
// Step size has changed, so we cannot guarantee no self-wraparound.
Ops[Idx] = getAddRecExpr(AddRecOps, AddRecLoop, SCEV::FlagAnyWrap);
// However, preserve NSW/NUW flags if all combined AddRecs had them.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I understand the logic here. Adding together two nsw addrecs doesn't necessarily result in an nsw addrec.

SCEV::NoWrapFlags FinalFlags = SCEV::FlagAnyWrap;
if (AllHaveNSW)
FinalFlags = setFlags(FinalFlags, SCEV::FlagNSW);
if (AllHaveNUW)
FinalFlags = setFlags(FinalFlags, SCEV::FlagNUW);

Ops[Idx] = getAddRecExpr(AddRecOps, AddRecLoop, FinalFlags);
return getAddExpr(Ops, SCEV::FlagAnyWrap, Depth + 1);
}
}
Expand Down
4 changes: 2 additions & 2 deletions llvm/test/Analysis/Delinearization/fixed_size_array.ll
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ define void @a_i_j2k_i(ptr %a) {
; CHECK-LABEL: 'a_i_j2k_i'
; CHECK-NEXT: Inst: store i32 1, ptr %idx, align 4
; CHECK-NEXT: In Loop with Header: for.k
; CHECK-NEXT: AccessFunction: {{\{\{\{}}0,+,1028}<%for.i.header>,+,256}<nw><%for.j.header>,+,128}<nw><%for.k>
; CHECK-NEXT: AccessFunction: {{\{\{\{}}0,+,1028}<nuw><nsw><%for.i.header>,+,256}<nw><%for.j.header>,+,128}<nw><%for.k>
; CHECK-NEXT: failed to delinearize
;
entry:
Expand Down Expand Up @@ -391,7 +391,7 @@ define void @a_i_i_jk(ptr %a) {
; CHECK-LABEL: 'a_i_i_jk'
; CHECK-NEXT: Inst: store i32 1, ptr %idx, align 4
; CHECK-NEXT: In Loop with Header: for.k
; CHECK-NEXT: AccessFunction: {{\{\{\{}}0,+,1152}<%for.i.header>,+,4}<nw><%for.j.header>,+,4}<nw><%for.k>
; CHECK-NEXT: AccessFunction: {{\{\{\{}}0,+,1152}<nuw><nsw><%for.i.header>,+,4}<nw><%for.j.header>,+,4}<nw><%for.k>
; CHECK-NEXT: Base offset: %a
; CHECK-NEXT: ArrayDecl[UnknownSize][288] with elements of 4 bytes.
; CHECK-NEXT: ArrayRef[{0,+,1}<nuw><nsw><%for.i.header>][{{\{\{}}0,+,1}<nuw><nsw><%for.j.header>,+,1}<nuw><nsw><%for.k>]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
; NOTE: Assertions have been autogenerated by utils/update_analyze_test_checks.py UTC_ARGS: --version 5
; RUN: opt < %s -disable-output "-passes=print<da>" -aa-pipeline=basic-aa 2>&1 | FileCheck %s

; Test that SCEV NSW flag preservation enables dependence analysis to work
; correctly. Previously, SCEV would lose NSW flags when combining AddRec
; expressions from GEP operations, causing dependence analysis to incorrectly
; classify expressions as "wrapping" and fail analysis.

define void @test_da_with_scev_flags(ptr %A) {
; This test verifies that dependence analysis now correctly identifies
; self-dependences when SCEV preserves NSW flags from GEP index computations.
; CHECK-LABEL: 'test_da_with_scev_flags'
; CHECK-NEXT: Src: %val = load i32, ptr %gep, align 4 --> Dst: %val = load i32, ptr %gep, align 4
; CHECK-NEXT: da analyze - none!
; CHECK-NEXT: Src: %val = load i32, ptr %gep, align 4 --> Dst: store i32 %val, ptr %gep, align 4
; CHECK-NEXT: da analyze - consistent anti [0|<]!
; CHECK-NEXT: Src: store i32 %val, ptr %gep, align 4 --> Dst: store i32 %val, ptr %gep, align 4
; CHECK-NEXT: da analyze - none!
;

entry:
br label %loop

loop:
%i = phi i64 [ 0, %entry ], [ %i.next, %loop ]

; Create NSW-flagged index computation
%mul = mul nsw i64 %i, 3
%sub = add nsw i64 %mul, -6

; GEP that should result in SCEV: {(-2424 + %A),+,1212}<nw>
; The <nw> flag should prevent false "wrapping" detection in DA
%gep = getelementptr inbounds [100 x i32], ptr %A, i64 %sub, i64 %sub

; Self-dependence: should be detected as "none" (no dependence)
%val = load i32, ptr %gep
store i32 %val, ptr %gep

%i.next = add nsw i64 %i, 1
%cond = icmp ult i64 %i.next, 50
br i1 %cond, label %loop, label %exit

exit:
ret void
}
Loading
Loading