llvm.org GIT mirror llvm / f7087ea
Reinstate the optimization from r151449 with a fix to not turn 'gep %x' into 'gep null' when the icmp predicate is unsigned (or is signed without inbounds). git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@151467 91177308-0d34-0410-b5e6-96231b3b80d8 Nick Lewycky 7 years ago
3 changed file(s) with 116 addition(s) and 15 deletion(s). Raw diff Collapse all Expand all
2020 #include "llvm/Operator.h"
2121 #include "llvm/ADT/Statistic.h"
2222 #include "llvm/Analysis/InstructionSimplify.h"
23 #include "llvm/Analysis/AliasAnalysis.h"
2324 #include "llvm/Analysis/ConstantFolding.h"
2425 #include "llvm/Analysis/Dominators.h"
2526 #include "llvm/Analysis/ValueTracking.h"
16081609 }
16091610 }
16101611
1611 // icmp , - Different stack variables have
1612 // different addresses, and what's more the address of a stack variable is
1613 // never null or equal to the address of a global. Note that generalizing
1614 // to the case where LHS is a global variable address or null is pointless,
1615 // since if both LHS and RHS are constants then we already constant folded
1616 // the compare, and if only one of them is then we moved it to RHS already.
1612 // icmp , - Different identified objects have
1613 // different addresses (unless null), and what's more the address of an
1614 // identified local is never equal to another argument (again, barring null).
1615 // Note that generalizing to the case where LHS is a global variable address
1616 // or null is pointless, since if both LHS and RHS are constants then we
1617 // already constant folded the compare, and if only one of them is then we
1618 // moved it to RHS already.
16171619 Value *LHSPtr = LHS->stripPointerCasts();
16181620 Value *RHSPtr = RHS->stripPointerCasts();
16191621 if (LHSPtr == RHSPtr)
16201622 return ConstantInt::get(ITy, CmpInst::isTrueWhenEqual(Pred));
1621
1623
16221624 // Be more aggressive about stripping pointer adjustments when checking a
16231625 // comparison of an alloca address to another object. We can rip off all
16241626 // inbounds GEP operations, even if they are variable.
16251627 LHSPtr = stripPointerAdjustments(LHSPtr);
1626 if (isa(LHSPtr)) {
1628 if (llvm::isIdentifiedObject(LHSPtr)) {
16271629 RHSPtr = stripPointerAdjustments(RHSPtr);
1628 if (LHSPtr != RHSPtr &&
1629 (isa(RHSPtr) || isa(RHSPtr) ||
1630 isa(RHSPtr)))
1630 if (llvm::isKnownNonNull(LHSPtr) || llvm::isKnownNonNull(RHSPtr)) {
1631 // If both sides are different identified objects, they aren't equal
1632 // unless they're null.
1633 if (LHSPtr != RHSPtr && llvm::isIdentifiedObject(RHSPtr))
1634 return ConstantInt::get(ITy, CmpInst::isFalseWhenEqual(Pred));
1635
1636 // A local identified object (alloca or noalias call) can't equal any
1637 // incoming argument, unless they're both null.
1638 if (isa(LHSPtr) && isa(RHSPtr))
1639 return ConstantInt::get(ITy, CmpInst::isFalseWhenEqual(Pred));
1640 }
1641
1642 // Assume that the constant null is on the right.
1643 if (llvm::isKnownNonNull(LHSPtr) && isa(RHSPtr))
1644 return ConstantInt::get(ITy, CmpInst::isFalseWhenEqual(Pred));
1645 } else if (isa(LHSPtr)) {
1646 RHSPtr = stripPointerAdjustments(RHSPtr);
1647 // An alloca can't be equal to an argument.
1648 if (isa(RHSPtr))
16311649 return ConstantInt::get(ITy, CmpInst::isFalseWhenEqual(Pred));
16321650 }
16331651
22372255 if (Pred == CmpInst::ICMP_UGT)
22382256 // Always false.
22392257 return getFalse(ITy);
2258 }
2259
2260 // Simplify comparisons of GEPs.
2261 if (GetElementPtrInst *GLHS = dyn_cast(LHS)) {
2262 if (GEPOperator *GRHS = dyn_cast(RHS)) {
2263 if (GLHS->getPointerOperand() == GRHS->getPointerOperand() &&
2264 GLHS->hasAllConstantIndices() && GRHS->hasAllConstantIndices() &&
2265 (ICmpInst::isEquality(Pred) ||
2266 (GLHS->isInBounds() && GRHS->isInBounds() &&
2267 Pred == ICmpInst::getSignedPredicate(Pred)))) {
2268 // The bases are equal and the indices are constant. Build a constant
2269 // expression GEP with the same indices and a null base pointer to see
2270 // what constant folding can make out of it.
2271 Constant *Null = Constant::getNullValue(GLHS->getPointerOperandType());
2272 SmallVector IndicesLHS(GLHS->idx_begin(), GLHS->idx_end());
2273 Constant *NewLHS = ConstantExpr::getGetElementPtr(Null, IndicesLHS);
2274
2275 SmallVector IndicesRHS(GRHS->idx_begin(), GRHS->idx_end());
2276 Constant *NewRHS = ConstantExpr::getGetElementPtr(Null, IndicesRHS);
2277 return ConstantExpr::getICmp(Pred, NewLHS, NewRHS);
2278 }
2279 }
22402280 }
22412281
22422282 // If the comparison is with the result of a select instruction, check whether
633633 %arrayidx2 = getelementptr inbounds i8* %a, i64 10
634634 %cmp = icmp slt i8* %arrayidx1, %arrayidx2
635635 ret i1 %cmp
636 ; Don't turn a signed cmp of GEPs into an index compare.
637636 ; CHECK: @test62
638 ; CHECK: %cmp = icmp slt i8* %arrayidx1, %arrayidx2
639 ; CHECK-NEXT: ret i1 %cmp
640 }
637 ; CHECK-NEXT: ret i1 true
638 }
3939 ; CHECK-NEXT: ret i1 true
4040 }
4141
42 ; PR11238
43 %gept = type { i32, i32 }
44 @gepy = global %gept zeroinitializer, align 8
45 @gepz = extern_weak global %gept
46
47 define i1 @gep3() {
48 ; CHECK: @gep3
49 %x = alloca %gept, align 8
50 %a = getelementptr %gept* %x, i64 0, i32 0
51 %b = getelementptr %gept* %x, i64 0, i32 1
52 %equal = icmp eq i32* %a, %b
53 ret i1 %equal
54 ; CHECK-NEXT: ret i1 false
55 }
56
57 define i1 @gep4() {
58 ; CHECK: @gep4
59 %x = alloca %gept, align 8
60 %a = getelementptr %gept* @gepy, i64 0, i32 0
61 %b = getelementptr %gept* @gepy, i64 0, i32 1
62 %equal = icmp eq i32* %a, %b
63 ret i1 %equal
64 ; CHECK-NEXT: ret i1 false
65 }
66
67 define i1 @gep5() {
68 ; CHECK: @gep5
69 %x = alloca %gept, align 8
70 %a = getelementptr inbounds %gept* %x, i64 0, i32 1
71 %b = getelementptr %gept* @gepy, i64 0, i32 0
72 %equal = icmp eq i32* %a, %b
73 ret i1 %equal
74 ; CHECK-NEXT: ret i1 false
75 }
76
77 define i1 @gep6(%gept* %x) {
78 ; Same as @gep3 but potentially null.
79 ; CHECK: @gep6
80 %a = getelementptr %gept* %x, i64 0, i32 0
81 %b = getelementptr %gept* %x, i64 0, i32 1
82 %equal = icmp eq i32* %a, %b
83 ret i1 %equal
84 ; CHECK-NEXT: ret i1 false
85 }
86
87 define i1 @gep7(%gept* %x) {
88 ; CHECK: @gep7
89 %a = getelementptr %gept* %x, i64 0, i32 0
90 %b = getelementptr %gept* @gepz, i64 0, i32 0
91 %equal = icmp eq i32* %a, %b
92 ret i1 %equal
93 ; CHECK: ret i1 %equal
94 }
95
96 define i1 @gep8(%gept* %x) {
97 ; CHECK: @gep8
98 %a = getelementptr %gept* %x, i32 1
99 %b = getelementptr %gept* %x, i32 -1
100 %equal = icmp ugt %gept* %a, %b
101 ret i1 %equal
102 ; CHECK: ret i1 %equal
103 }
104
42105 define i1 @zext(i32 %x) {
43106 ; CHECK: @zext
44107 %e1 = zext i32 %x to i64