From 3db143ea8c4717cfbf54f257ba24629502b59279 Mon Sep 17 00:00:00 2001 From: Nick Lewycky Date: Sun, 26 Feb 2012 02:09:49 +0000 Subject: 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). llvm-svn: 151467 --- llvm/lib/Analysis/InstructionSimplify.cpp | 62 +++++++++++++++++++++++++------ 1 file changed, 51 insertions(+), 11 deletions(-) (limited to 'llvm/lib/Analysis/InstructionSimplify.cpp') diff --git a/llvm/lib/Analysis/InstructionSimplify.cpp b/llvm/lib/Analysis/InstructionSimplify.cpp index b518d0ca71c..370ab962888 100644 --- a/llvm/lib/Analysis/InstructionSimplify.cpp +++ b/llvm/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" @@ -1609,26 +1610,43 @@ static Value *SimplifyICmpInst(unsigned Predicate, Value *LHS, Value *RHS, } } - // icmp , - Different stack variables 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. + // icmp , - Different identified objects have + // different addresses (unless null), and what's more the address of an + // identified local is never equal to another argument (again, barring null). + // 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(LHSPtr)) { + if (llvm::isIdentifiedObject(LHSPtr)) { + RHSPtr = stripPointerAdjustments(RHSPtr); + if (llvm::isKnownNonNull(LHSPtr) || llvm::isKnownNonNull(RHSPtr)) { + // If both sides are different identified objects, they aren't equal + // unless they're null. + if (LHSPtr != RHSPtr && llvm::isIdentifiedObject(RHSPtr)) + return ConstantInt::get(ITy, CmpInst::isFalseWhenEqual(Pred)); + + // A local identified object (alloca or noalias call) can't equal any + // incoming argument, unless they're both null. + if (isa(LHSPtr) && isa(RHSPtr)) + return ConstantInt::get(ITy, CmpInst::isFalseWhenEqual(Pred)); + } + + // Assume that the constant null is on the right. + if (llvm::isKnownNonNull(LHSPtr) && isa(RHSPtr)) + return ConstantInt::get(ITy, CmpInst::isFalseWhenEqual(Pred)); + } else if (isa(LHSPtr)) { RHSPtr = stripPointerAdjustments(RHSPtr); - if (LHSPtr != RHSPtr && - (isa(RHSPtr) || isa(RHSPtr) || - isa(RHSPtr))) + // An alloca can't be equal to an argument. + if (isa(RHSPtr)) return ConstantInt::get(ITy, CmpInst::isFalseWhenEqual(Pred)); } @@ -2240,6 +2258,28 @@ static Value *SimplifyICmpInst(unsigned Predicate, Value *LHS, Value *RHS, return getFalse(ITy); } + // Simplify comparisons of GEPs. + if (GetElementPtrInst *GLHS = dyn_cast(LHS)) { + if (GEPOperator *GRHS = dyn_cast(RHS)) { + if (GLHS->getPointerOperand() == GRHS->getPointerOperand() && + GLHS->hasAllConstantIndices() && GRHS->hasAllConstantIndices() && + (ICmpInst::isEquality(Pred) || + (GLHS->isInBounds() && GRHS->isInBounds() && + Pred == ICmpInst::getSignedPredicate(Pred)))) { + // 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 IndicesLHS(GLHS->idx_begin(), GLHS->idx_end()); + Constant *NewLHS = ConstantExpr::getGetElementPtr(Null, IndicesLHS); + + SmallVector 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(LHS) || isa(RHS)) -- cgit v1.2.3