mirror of
https://github.com/RPCSX/llvm.git
synced 2024-12-01 07:30:31 +00:00
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 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@193514 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
parent
1fe9069d53
commit
19ea37059a
@ -3088,15 +3088,20 @@ const SCEV *ScalarEvolution::createNodeForPHI(PHINode *PN) {
|
||||
Flags = setFlags(Flags, SCEV::FlagNUW);
|
||||
if (OBO->hasNoSignedWrap())
|
||||
Flags = setFlags(Flags, SCEV::FlagNSW);
|
||||
} else if (const GEPOperator *GEP =
|
||||
dyn_cast<GEPOperator>(BEValueV)) {
|
||||
} else if (GEPOperator *GEP = dyn_cast<GEPOperator>(BEValueV)) {
|
||||
// If the increment is an inbounds GEP, then we know the address
|
||||
// space cannot be wrapped around. We cannot make any guarantee
|
||||
// about signed or unsigned overflow because pointers are
|
||||
// unsigned but we may have a negative index from the base
|
||||
// pointer.
|
||||
if (GEP->isInBounds())
|
||||
// pointer. We can guarantee that no unsigned wrap occurs if the
|
||||
// indices form a positive value.
|
||||
if (GEP->isInBounds()) {
|
||||
Flags = setFlags(Flags, SCEV::FlagNW);
|
||||
|
||||
const SCEV *Ptr = getSCEV(GEP->getPointerOperand());
|
||||
if (isKnownPositive(getMinusSCEV(getSCEV(GEP), Ptr)))
|
||||
Flags = setFlags(Flags, SCEV::FlagNUW);
|
||||
}
|
||||
}
|
||||
|
||||
const SCEV *StartVal = getSCEV(StartValueV);
|
||||
|
@ -62,11 +62,11 @@ for.body.lr.ph.i.i: ; preds = %entry
|
||||
for.body.i.i: ; preds = %for.body.i.i, %for.body.lr.ph.i.i
|
||||
%__first.addr.02.i.i = phi i32* [ %begin, %for.body.lr.ph.i.i ], [ %ptrincdec.i.i, %for.body.i.i ]
|
||||
; CHECK: %__first.addr.02.i.i
|
||||
; CHECK-NEXT: --> {%begin,+,4}<nw><%for.body.i.i>
|
||||
; CHECK-NEXT: --> {%begin,+,4}<nuw><%for.body.i.i>
|
||||
store i32 0, i32* %__first.addr.02.i.i, align 4
|
||||
%ptrincdec.i.i = getelementptr inbounds i32* %__first.addr.02.i.i, i64 1
|
||||
; CHECK: %ptrincdec.i.i
|
||||
; CHECK-NEXT: --> {(4 + %begin),+,4}<nw><%for.body.i.i>
|
||||
; CHECK-NEXT: --> {(4 + %begin),+,4}<nuw><%for.body.i.i>
|
||||
%cmp.i.i = icmp eq i32* %ptrincdec.i.i, %end
|
||||
br i1 %cmp.i.i, label %for.cond.for.end_crit_edge.i.i, label %for.body.i.i
|
||||
|
||||
@ -122,3 +122,39 @@ exit:
|
||||
%result = phi i32 [ %a, %entry ], [ %tmp2, %greater ]
|
||||
ret i32 %result
|
||||
}
|
||||
|
||||
; TODO: This could fold down to '1'
|
||||
; CHECK-LABEL: PR12375
|
||||
; CHECK: --> {(4 + %arg),+,4}<nuw><%bb1> Exits: (4 + (4 * ((-1 + (-1 * %arg) + ((4 + %arg) umax (8 + %arg)<nsw>)) /u 4)) + %arg)
|
||||
define i32 @PR12375(i32* readnone %arg) {
|
||||
bb:
|
||||
%tmp = getelementptr inbounds i32* %arg, i64 2
|
||||
br label %bb1
|
||||
|
||||
bb1: ; preds = %bb1, %bb
|
||||
%tmp2 = phi i32* [ %arg, %bb ], [ %tmp5, %bb1 ]
|
||||
%tmp3 = phi i32 [ 0, %bb ], [ %tmp4, %bb1 ]
|
||||
%tmp4 = add nsw i32 %tmp3, 1
|
||||
%tmp5 = getelementptr inbounds i32* %tmp2, i64 1
|
||||
%tmp6 = icmp ult i32* %tmp5, %tmp
|
||||
br i1 %tmp6, label %bb1, label %bb7
|
||||
|
||||
bb7: ; preds = %bb1
|
||||
ret i32 %tmp4
|
||||
}
|
||||
|
||||
; CHECK-LABEL: PR12376
|
||||
; CHECK: --> {(4 + %arg),+,4}<nuw><%bb2> Exits: (4 + (4 * ((3 + (-1 * %arg) + (%arg umax %arg1)) /u 4)) + %arg)
|
||||
define void @PR12376(i32* nocapture %arg, i32* nocapture %arg1) {
|
||||
bb:
|
||||
br label %bb2
|
||||
|
||||
bb2: ; preds = %bb2, %bb
|
||||
%tmp = phi i32* [ %arg, %bb ], [ %tmp4, %bb2 ]
|
||||
%tmp3 = icmp ult i32* %tmp, %arg1
|
||||
%tmp4 = getelementptr inbounds i32* %tmp, i64 1
|
||||
br i1 %tmp3, label %bb2, label %bb5
|
||||
|
||||
bb5: ; preds = %bb2
|
||||
ret void
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user