llvm.org GIT mirror llvm / 9d9e29b
Switch the pointer-difference simplification logic to only work with inbounds GEPs. This isn't really necessary for simplifying pointer differences, but I'm planning to re-use the same code to simplify pointer comparisons where it is necessary. Since real code almost exclusively uses inbounds GEPs, it doesn't seem worth it to support the extra complexity of turning it on and off. If anyone would like that back, feel free to shout. Note that instcombine will still catch any of these patterns. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@153418 91177308-0d34-0410-b5e6-96231b3b80d8 Chandler Carruth 7 years ago
2 changed file(s) with 26 addition(s) and 11 deletion(s). Raw diff Collapse all Expand all
709709 Visited.insert(V);
710710 do {
711711 if (GEPOperator *GEP = dyn_cast(V)) {
712 if (!accumulateGEPOffset(TD, GEP, Offset))
712 if (!GEP->isInBounds() || !accumulateGEPOffset(TD, GEP, Offset))
713713 break;
714714 V = GEP->getPointerOperand();
715715 } else if (Operator::getOpcode(V) == Instruction::BitCast) {
55 ; CHECK: @ptrdiff1
66 ; CHECK-NEXT: ret i64 42
77
8 %first = getelementptr i8* %ptr, i32 0
9 %last = getelementptr i8* %ptr, i32 42
8 %first = getelementptr inbounds i8* %ptr, i32 0
9 %last = getelementptr inbounds i8* %ptr, i32 42
1010 %first.int = ptrtoint i8* %first to i64
1111 %last.int = ptrtoint i8* %last to i64
1212 %diff = sub i64 %last.int, %first.int
1717 ; CHECK: @ptrdiff2
1818 ; CHECK-NEXT: ret i64 42
1919
20 %first1 = getelementptr i8* %ptr, i32 0
21 %first2 = getelementptr i8* %first1, i32 1
22 %first3 = getelementptr i8* %first2, i32 2
23 %first4 = getelementptr i8* %first3, i32 4
24 %last1 = getelementptr i8* %first2, i32 48
25 %last2 = getelementptr i8* %last1, i32 8
26 %last3 = getelementptr i8* %last2, i32 -4
27 %last4 = getelementptr i8* %last3, i32 -4
20 %first1 = getelementptr inbounds i8* %ptr, i32 0
21 %first2 = getelementptr inbounds i8* %first1, i32 1
22 %first3 = getelementptr inbounds i8* %first2, i32 2
23 %first4 = getelementptr inbounds i8* %first3, i32 4
24 %last1 = getelementptr inbounds i8* %first2, i32 48
25 %last2 = getelementptr inbounds i8* %last1, i32 8
26 %last3 = getelementptr inbounds i8* %last2, i32 -4
27 %last4 = getelementptr inbounds i8* %last3, i32 -4
2828 %first.int = ptrtoint i8* %first4 to i64
2929 %last.int = ptrtoint i8* %last4 to i64
3030 %diff = sub i64 %last.int, %first.int
3131 ret i64 %diff
3232 }
33
34 define i64 @ptrdiff3(i8* %ptr) {
35 ; Don't bother with non-inbounds GEPs.
36 ; CHECK: @ptrdiff3
37 ; CHECK: getelementptr
38 ; CHECK: sub
39 ; CHECK: ret
40
41 %first = getelementptr i8* %ptr, i32 0
42 %last = getelementptr i8* %ptr, i32 42
43 %first.int = ptrtoint i8* %first to i64
44 %last.int = ptrtoint i8* %last to i64
45 %diff = sub i64 %last.int, %first.int
46 ret i64 %diff
47 }