diff options
author | Nick Lewycky <nicholas@mxc.ca> | 2012-02-25 19:07:42 +0000 |
---|---|---|
committer | Nick Lewycky <nicholas@mxc.ca> | 2012-02-25 19:07:42 +0000 |
commit | 1e4e1c768bcbc548345348c062f402967c78c48a (patch) | |
tree | bf604e6501bfd695f1f5ad71608b1a7c8d86d3f4 | |
parent | 55f4ab84e567ac101d70f052771d1bc67a7560e3 (diff) | |
download | llvm-1e4e1c768bcbc548345348c062f402967c78c48a.tar.gz llvm-1e4e1c768bcbc548345348c062f402967c78c48a.tar.bz2 llvm-1e4e1c768bcbc548345348c062f402967c78c48a.tar.xz |
Teach instsimplify to be more aggressive when analyzing comparisons of pointers
by using llvm::isIdentifiedObject. Also teach it to handle GEPs that have
the same base pointer and constant operands. Fixes PR11238!
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@151449 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r-- | lib/Analysis/InstructionSimplify.cpp | 51 | ||||
-rw-r--r-- | test/Transforms/InstCombine/icmp.ll | 4 | ||||
-rw-r--r-- | test/Transforms/InstSimplify/compare.ll | 44 |
3 files changed, 86 insertions, 13 deletions
diff --git a/lib/Analysis/InstructionSimplify.cpp b/lib/Analysis/InstructionSimplify.cpp index 37253f72cf..a6a080e381 100644 --- a/lib/Analysis/InstructionSimplify.cpp +++ b/lib/Analysis/InstructionSimplify.cpp @@ -21,6 +21,7 @@ #include "llvm/Operator.h" #include "llvm/ADT/Statistic.h" #include "llvm/Analysis/InstructionSimplify.h" +#include "llvm/Analysis/AliasAnalysis.h" #include "llvm/Analysis/ConstantFolding.h" #include "llvm/Analysis/Dominators.h" #include "llvm/Analysis/ValueTracking.h" @@ -1608,26 +1609,37 @@ static Value *SimplifyICmpInst(unsigned Predicate, Value *LHS, Value *RHS, } } - // icmp <alloca*>, <global/alloca*/null> - Different stack variables have + // icmp <object*>, <object*/null> - Different identified objects have // different addresses, and what's more the address of a stack variable is - // never null or equal to the address of a global. Note that generalizing - // to the case where LHS is a global variable address or null is pointless, - // since if both LHS and RHS are constants then we already constant folded - // the compare, and if only one of them is then we moved it to RHS already. + // never equal to another argument. Note that generalizing to the case where + // LHS is a global variable address or null is pointless, since if both LHS + // and RHS are constants then we already constant folded the compare, and if + // only one of them is then we moved it to RHS already. Value *LHSPtr = LHS->stripPointerCasts(); Value *RHSPtr = RHS->stripPointerCasts(); if (LHSPtr == RHSPtr) return ConstantInt::get(ITy, CmpInst::isTrueWhenEqual(Pred)); - + // Be more aggressive about stripping pointer adjustments when checking a // comparison of an alloca address to another object. We can rip off all // inbounds GEP operations, even if they are variable. LHSPtr = stripPointerAdjustments(LHSPtr); - if (isa<AllocaInst>(LHSPtr)) { + if (llvm::isIdentifiedObject(LHSPtr)) { RHSPtr = stripPointerAdjustments(RHSPtr); - if (LHSPtr != RHSPtr && - (isa<GlobalValue>(RHSPtr) || isa<AllocaInst>(RHSPtr) || - isa<ConstantPointerNull>(RHSPtr))) + // If both sides are different identified objects, they aren't equal unless + // they're null. + if (LHSPtr != RHSPtr && llvm::isIdentifiedObject(RHSPtr) && + (llvm::isKnownNonNull(LHSPtr) || llvm::isKnownNonNull(RHSPtr))) + return ConstantInt::get(ITy, CmpInst::isFalseWhenEqual(Pred)); + + // Assume that the null is on the right. + if (llvm::isKnownNonNull(LHSPtr) && isa<ConstantPointerNull>(RHSPtr)) + return ConstantInt::get(ITy, CmpInst::isFalseWhenEqual(Pred)); + + // A local instruction (alloca or noalias call) can't alias any incoming + // argument. + if ((isa<Instruction>(LHSPtr) && isa<Argument>(RHSPtr)) || + (isa<Instruction>(RHSPtr) && isa<Argument>(LHSPtr))) return ConstantInt::get(ITy, CmpInst::isFalseWhenEqual(Pred)); } @@ -2239,6 +2251,25 @@ static Value *SimplifyICmpInst(unsigned Predicate, Value *LHS, Value *RHS, return getFalse(ITy); } + // Simplify comparisons of GEPs. + if (GetElementPtrInst *GLHS = dyn_cast<GetElementPtrInst>(LHS)) { + if (GEPOperator *GRHS = dyn_cast<GEPOperator>(RHS)) { + if (GLHS->getPointerOperand() == GRHS->getPointerOperand() && + GLHS->hasAllConstantIndices() && GRHS->hasAllConstantIndices()) { + // The bases are equal and the indices are constant. Build a constant + // expression GEP with the same indices and a null base pointer to see + // what constant folding can make out of it. + Constant *Null = Constant::getNullValue(GLHS->getPointerOperandType()); + SmallVector<Value *, 4> IndicesLHS(GLHS->idx_begin(), GLHS->idx_end()); + Constant *NewLHS = ConstantExpr::getGetElementPtr(Null, IndicesLHS); + + SmallVector<Value *, 4> IndicesRHS(GRHS->idx_begin(), GRHS->idx_end()); + Constant *NewRHS = ConstantExpr::getGetElementPtr(Null, IndicesRHS); + return ConstantExpr::getICmp(Pred, NewLHS, NewRHS); + } + } + } + // If the comparison is with the result of a select instruction, check whether // comparing with either branch of the select always yields the same value. if (isa<SelectInst>(LHS) || isa<SelectInst>(RHS)) diff --git a/test/Transforms/InstCombine/icmp.ll b/test/Transforms/InstCombine/icmp.ll index dabb0f3adf..a9ae221d8f 100644 --- a/test/Transforms/InstCombine/icmp.ll +++ b/test/Transforms/InstCombine/icmp.ll @@ -634,8 +634,6 @@ define i1 @test62(i8* %a) { %arrayidx2 = getelementptr inbounds i8* %a, i64 10 %cmp = icmp slt i8* %arrayidx1, %arrayidx2 ret i1 %cmp -; Don't turn a signed cmp of GEPs into an index compare. ; CHECK: @test62 -; CHECK: %cmp = icmp slt i8* %arrayidx1, %arrayidx2 -; CHECK-NEXT: ret i1 %cmp +; CHECK-NEXT: ret i1 true } diff --git a/test/Transforms/InstSimplify/compare.ll b/test/Transforms/InstSimplify/compare.ll index 6ee6dfbdbc..85aea19e69 100644 --- a/test/Transforms/InstSimplify/compare.ll +++ b/test/Transforms/InstSimplify/compare.ll @@ -40,6 +40,50 @@ define i1 @gep2() { ; CHECK-NEXT: ret i1 true } +; PR11238 +%t = type { i32, i32 } +@y = global %t zeroinitializer, align 8 + +define i1 @gep3() { +; CHECK: @gep3 + %x = alloca %t, align 8 + %a = getelementptr %t* %x, i64 0, i32 0 + %b = getelementptr %t* %x, i64 0, i32 1 + %equal = icmp eq i32* %a, %b + ret i1 %equal +; CHECK-NEXT: ret i1 false +} + +define i1 @gep4() { +; CHECK: @gep4 + %x = alloca %t, align 8 + %a = getelementptr %t* @y, i64 0, i32 0 + %b = getelementptr %t* @y, i64 0, i32 1 + %equal = icmp eq i32* %a, %b + ret i1 %equal +; CHECK-NEXT: ret i1 false +} + +define i1 @gep5() { +; CHECK: @gep5 + %x = alloca %t, align 8 + %a = getelementptr inbounds %t* %x, i64 0, i32 1 + %b = getelementptr %t* @y, i64 0, i32 0 + %equal = icmp eq i32* %a, %b + ret i1 %equal +; CHECK-NEXT: ret i1 false +} + +define i1 @gep6(%t* %x) { +; Same as @gep3 but potentially null. +; CHECK: @gep6 + %a = getelementptr %t* %x, i64 0, i32 0 + %b = getelementptr %t* %x, i64 0, i32 1 + %equal = icmp eq i32* %a, %b + ret i1 %equal +; CHECK-NEXT: ret i1 false +} + define i1 @zext(i32 %x) { ; CHECK: @zext %e1 = zext i32 %x to i64 |