llvm.org GIT mirror llvm / 6fd3428
Roll these back to r151448 until I figure out how they're breaking MultiSource/Applications/lua. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@151463 91177308-0d34-0410-b5e6-96231b3b80d8 Nick Lewycky 7 years ago
3 changed file(s) with 14 addition(s) and 98 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"
2423 #include "llvm/Analysis/ConstantFolding.h"
2524 #include "llvm/Analysis/Dominators.h"
2625 #include "llvm/Analysis/ValueTracking.h"
16081607 }
16091608 }
16101609
1611 // icmp <object*>, - Different identified objects have
1610 // icmp <alloca*>, - Different stack variables have
16121611 // different addresses, and what's more the address of a stack variable is
1613 // never equal to another argument. Note that generalizing to the case where
1614 // LHS is a global variable address or null is pointless, since if both LHS
1615 // and RHS are constants then we already constant folded the compare, and if
1616 // only one of them is then we moved it to RHS already.
1612 // never null or equal to the address of a global. Note that generalizing
1613 // to the case where LHS is a global variable address or null is pointless,
1614 // since if both LHS and RHS are constants then we already constant folded
1615 // the compare, and if only one of them is then we moved it to RHS already.
16171616 Value *LHSPtr = LHS->stripPointerCasts();
16181617 Value *RHSPtr = RHS->stripPointerCasts();
16191618 if (LHSPtr == RHSPtr)
16201619 return ConstantInt::get(ITy, CmpInst::isTrueWhenEqual(Pred));
1621
1620
16221621 // Be more aggressive about stripping pointer adjustments when checking a
16231622 // comparison of an alloca address to another object. We can rip off all
16241623 // inbounds GEP operations, even if they are variable.
16251624 LHSPtr = stripPointerAdjustments(LHSPtr);
1626 if (llvm::isIdentifiedObject(LHSPtr)) {
1625 if (isa(LHSPtr)) {
16271626 RHSPtr = stripPointerAdjustments(RHSPtr);
1628 if (llvm::isKnownNonNull(LHSPtr) || llvm::isKnownNonNull(RHSPtr)) {
1629 // If both sides are different identified objects, they aren't equal
1630 // unless they're null.
1631 if (LHSPtr != RHSPtr && llvm::isIdentifiedObject(RHSPtr))
1632 return ConstantInt::get(ITy, CmpInst::isFalseWhenEqual(Pred));
1633
1634 // A local identified object (alloca or noalias call) can't equal any
1635 // incoming argument, unless they're both null.
1636 if ((isa(LHSPtr) && isa(RHSPtr)) ||
1637 (isa(RHSPtr) && isa(LHSPtr)))
1638 return ConstantInt::get(ITy, CmpInst::isFalseWhenEqual(Pred));
1639 }
1640
1641 // Assume that the constant null is on the right.
1642 if (llvm::isKnownNonNull(LHSPtr) && isa(RHSPtr))
1627 if (LHSPtr != RHSPtr &&
1628 (isa(RHSPtr) || isa(RHSPtr) ||
1629 isa(RHSPtr)))
16431630 return ConstantInt::get(ITy, CmpInst::isFalseWhenEqual(Pred));
16441631 }
16451632
22492236 if (Pred == CmpInst::ICMP_UGT)
22502237 // Always false.
22512238 return getFalse(ITy);
2252 }
2253
2254 // Simplify comparisons of GEPs.
2255 if (GetElementPtrInst *GLHS = dyn_cast(LHS)) {
2256 if (GEPOperator *GRHS = dyn_cast(RHS)) {
2257 if (GLHS->getPointerOperand() == GRHS->getPointerOperand() &&
2258 GLHS->hasAllConstantIndices() && GRHS->hasAllConstantIndices()) {
2259 // The bases are equal and the indices are constant. Build a constant
2260 // expression GEP with the same indices and a null base pointer to see
2261 // what constant folding can make out of it.
2262 Constant *Null = Constant::getNullValue(GLHS->getPointerOperandType());
2263 SmallVector IndicesLHS(GLHS->idx_begin(), GLHS->idx_end());
2264 Constant *NewLHS = ConstantExpr::getGetElementPtr(Null, IndicesLHS);
2265
2266 SmallVector IndicesRHS(GRHS->idx_begin(), GRHS->idx_end());
2267 Constant *NewRHS = ConstantExpr::getGetElementPtr(Null, IndicesRHS);
2268 return ConstantExpr::getICmp(Pred, NewLHS, NewRHS);
2269 }
2270 }
22712239 }
22722240
22732241 // 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.
636637 ; CHECK: @test62
637 ; CHECK-NEXT: ret i1 true
638 }
638 ; CHECK: %cmp = icmp slt i8* %arrayidx1, %arrayidx2
639 ; CHECK-NEXT: ret i1 %cmp
640 }
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
9642 define i1 @zext(i32 %x) {
9743 ; CHECK: @zext
9844 %e1 = zext i32 %x to i64