Skip to content

Commit 6094f30

Browse files
committed
SCEV: Make the final add of an inbounds GEP nuw if we know that the index is positive.
We can't do this for the general case as saying a GEP with a negative index doesn't have unsigned wrap isn't valid for negative indices. %gep = getelementptr inbounds i32* %p, i64 -1 But an inbounds GEP cannot run past the end of address space. So we check for the very common case of a positive index and make GEPs derived from that NUW. Together with Andy's recent non-unit stride work this lets us analyze loops like void foo3(int *a, int *b) { for (; a < b; a++) {} } PR12375, PR12376. Differential Revision: http://llvm-reviews.chandlerc.com/D2033 llvm-svn: 193514
1 parent a67c9c3 commit 6094f30

File tree

2 files changed

+47
-6
lines changed

2 files changed

+47
-6
lines changed

llvm/lib/Analysis/ScalarEvolution.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3088,15 +3088,20 @@ const SCEV *ScalarEvolution::createNodeForPHI(PHINode *PN) {
30883088
Flags = setFlags(Flags, SCEV::FlagNUW);
30893089
if (OBO->hasNoSignedWrap())
30903090
Flags = setFlags(Flags, SCEV::FlagNSW);
3091-
} else if (const GEPOperator *GEP =
3092-
dyn_cast<GEPOperator>(BEValueV)) {
3091+
} else if (GEPOperator *GEP = dyn_cast<GEPOperator>(BEValueV)) {
30933092
// If the increment is an inbounds GEP, then we know the address
30943093
// space cannot be wrapped around. We cannot make any guarantee
30953094
// about signed or unsigned overflow because pointers are
30963095
// unsigned but we may have a negative index from the base
3097-
// pointer.
3098-
if (GEP->isInBounds())
3096+
// pointer. We can guarantee that no unsigned wrap occurs if the
3097+
// indices form a positive value.
3098+
if (GEP->isInBounds()) {
30993099
Flags = setFlags(Flags, SCEV::FlagNW);
3100+
3101+
const SCEV *Ptr = getSCEV(GEP->getPointerOperand());
3102+
if (isKnownPositive(getMinusSCEV(getSCEV(GEP), Ptr)))
3103+
Flags = setFlags(Flags, SCEV::FlagNUW);
3104+
}
31003105
}
31013106

31023107
const SCEV *StartVal = getSCEV(StartValueV);

llvm/test/Analysis/ScalarEvolution/nsw.ll

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,11 @@ for.body.lr.ph.i.i: ; preds = %entry
6262
for.body.i.i: ; preds = %for.body.i.i, %for.body.lr.ph.i.i
6363
%__first.addr.02.i.i = phi i32* [ %begin, %for.body.lr.ph.i.i ], [ %ptrincdec.i.i, %for.body.i.i ]
6464
; CHECK: %__first.addr.02.i.i
65-
; CHECK-NEXT: --> {%begin,+,4}<nw><%for.body.i.i>
65+
; CHECK-NEXT: --> {%begin,+,4}<nuw><%for.body.i.i>
6666
store i32 0, i32* %__first.addr.02.i.i, align 4
6767
%ptrincdec.i.i = getelementptr inbounds i32* %__first.addr.02.i.i, i64 1
6868
; CHECK: %ptrincdec.i.i
69-
; CHECK-NEXT: --> {(4 + %begin),+,4}<nw><%for.body.i.i>
69+
; CHECK-NEXT: --> {(4 + %begin),+,4}<nuw><%for.body.i.i>
7070
%cmp.i.i = icmp eq i32* %ptrincdec.i.i, %end
7171
br i1 %cmp.i.i, label %for.cond.for.end_crit_edge.i.i, label %for.body.i.i
7272

@@ -122,3 +122,39 @@ exit:
122122
%result = phi i32 [ %a, %entry ], [ %tmp2, %greater ]
123123
ret i32 %result
124124
}
125+
126+
; TODO: This could fold down to '1'
127+
; CHECK-LABEL: PR12375
128+
; CHECK: --> {(4 + %arg),+,4}<nuw><%bb1> Exits: (4 + (4 * ((-1 + (-1 * %arg) + ((4 + %arg) umax (8 + %arg)<nsw>)) /u 4)) + %arg)
129+
define i32 @PR12375(i32* readnone %arg) {
130+
bb:
131+
%tmp = getelementptr inbounds i32* %arg, i64 2
132+
br label %bb1
133+
134+
bb1: ; preds = %bb1, %bb
135+
%tmp2 = phi i32* [ %arg, %bb ], [ %tmp5, %bb1 ]
136+
%tmp3 = phi i32 [ 0, %bb ], [ %tmp4, %bb1 ]
137+
%tmp4 = add nsw i32 %tmp3, 1
138+
%tmp5 = getelementptr inbounds i32* %tmp2, i64 1
139+
%tmp6 = icmp ult i32* %tmp5, %tmp
140+
br i1 %tmp6, label %bb1, label %bb7
141+
142+
bb7: ; preds = %bb1
143+
ret i32 %tmp4
144+
}
145+
146+
; CHECK-LABEL: PR12376
147+
; CHECK: --> {(4 + %arg),+,4}<nuw><%bb2> Exits: (4 + (4 * ((3 + (-1 * %arg) + (%arg umax %arg1)) /u 4)) + %arg)
148+
define void @PR12376(i32* nocapture %arg, i32* nocapture %arg1) {
149+
bb:
150+
br label %bb2
151+
152+
bb2: ; preds = %bb2, %bb
153+
%tmp = phi i32* [ %arg, %bb ], [ %tmp4, %bb2 ]
154+
%tmp3 = icmp ult i32* %tmp, %arg1
155+
%tmp4 = getelementptr inbounds i32* %tmp, i64 1
156+
br i1 %tmp3, label %bb2, label %bb5
157+
158+
bb5: ; preds = %bb2
159+
ret void
160+
}

0 commit comments

Comments
 (0)