diff options
author | Dan Gohman <gohman@apple.com> | 2009-09-10 23:37:55 +0000 |
---|---|---|
committer | Dan Gohman <gohman@apple.com> | 2009-09-10 23:37:55 +0000 |
commit | 7190d480758fd7fe2f9d87bee25b97468fcbf39f (patch) | |
tree | dbad9ff5ee1c824d547c5909303d3221c5cbedd6 /llvm/lib | |
parent | 8ef65fbd49bdcd41583424ef40b4ddbd04d7908a (diff) | |
download | bcm5719-llvm-7190d480758fd7fe2f9d87bee25b97468fcbf39f.tar.gz bcm5719-llvm-7190d480758fd7fe2f9d87bee25b97468fcbf39f.zip |
Factor out the code for checking that all indices in a getelementptr are
within the notional bounds of the static type of the getelementptr (which
is not the same as "inbounds") from GlobalOpt into a utility routine,
and use it in ConstantFold.cpp to check whether there are any mis-behaved
indices.
llvm-svn: 81478
Diffstat (limited to 'llvm/lib')
-rw-r--r-- | llvm/lib/Transforms/IPO/GlobalOpt.cpp | 19 | ||||
-rw-r--r-- | llvm/lib/VMCore/ConstantFold.cpp | 8 | ||||
-rw-r--r-- | llvm/lib/VMCore/Constants.cpp | 26 |
3 files changed, 38 insertions, 15 deletions
diff --git a/llvm/lib/Transforms/IPO/GlobalOpt.cpp b/llvm/lib/Transforms/IPO/GlobalOpt.cpp index b995a3d2864..63bc03d7872 100644 --- a/llvm/lib/Transforms/IPO/GlobalOpt.cpp +++ b/llvm/lib/Transforms/IPO/GlobalOpt.cpp @@ -2045,25 +2045,14 @@ static bool isSimpleEnoughPointerToCommit(Constant *C, LLVMContext &Context) { if (!GV->hasDefinitiveInitializer()) return false; - gep_type_iterator GEPI = gep_type_begin(CE), E = gep_type_end(CE); - User::op_iterator OI = next(CE->op_begin()); - // The first index must be zero. - ConstantInt *CI = dyn_cast<ConstantInt>(*OI); + ConstantInt *CI = dyn_cast<ConstantInt>(*next(CE->op_begin())); if (!CI || !CI->isZero()) return false; - ++GEPI; - ++OI; // The remaining indices must be compile-time known integers within the - // bounds of the corresponding static array types. - for (; GEPI != E; ++GEPI, ++OI) { - CI = dyn_cast<ConstantInt>(*OI); - if (!CI) return false; - if (const ArrayType *ATy = dyn_cast<ArrayType>(*GEPI)) - if (CI->getValue().getActiveBits() > 64 || - CI->getZExtValue() >= ATy->getNumElements()) - return false; - } + // notional bounds of the corresponding static array types. + if (!CE->isGEPWithNoNotionalOverIndexing()) + return false; return ConstantFoldLoadThroughGEPConstantExpr(GV->getInitializer(), CE, Context); diff --git a/llvm/lib/VMCore/ConstantFold.cpp b/llvm/lib/VMCore/ConstantFold.cpp index 701a195f7fd..da6c8d4f4bc 100644 --- a/llvm/lib/VMCore/ConstantFold.cpp +++ b/llvm/lib/VMCore/ConstantFold.cpp @@ -1329,6 +1329,14 @@ static ICmpInst::Predicate evaluateICmpRelation(LLVMContext &Context, // ordering of the resultant pointers. unsigned i = 1; + // The logic below assumes that the result of the comparison + // can be determined by finding the first index that differs. + // This doesn't work if there is over-indexing in any + // subsequent indices, so check for that case first. + if (!CE1->isGEPWithNoNotionalOverIndexing() || + !CE2->isGEPWithNoNotionalOverIndexing()) + return ICmpInst::BAD_ICMP_PREDICATE; // Might be equal. + // Compare all of the operands the GEP's have in common. gep_type_iterator GTI = gep_type_begin(CE1); for (;i != CE1->getNumOperands() && i != CE2->getNumOperands(); diff --git a/llvm/lib/VMCore/Constants.cpp b/llvm/lib/VMCore/Constants.cpp index a5b4f289688..54445cd7826 100644 --- a/llvm/lib/VMCore/Constants.cpp +++ b/llvm/lib/VMCore/Constants.cpp @@ -28,6 +28,7 @@ #include "llvm/Support/ManagedStatic.h" #include "llvm/Support/MathExtras.h" #include "llvm/Support/raw_ostream.h" +#include "llvm/Support/GetElementPtrTypeIterator.h" #include "llvm/System/Mutex.h" #include "llvm/System/RWMutex.h" #include "llvm/System/Threading.h" @@ -652,6 +653,31 @@ bool ConstantExpr::isCompare() const { return getOpcode() == Instruction::ICmp || getOpcode() == Instruction::FCmp; } +bool ConstantExpr::isGEPWithNoNotionalOverIndexing() const { + if (getOpcode() != Instruction::GetElementPtr) return false; + + gep_type_iterator GEPI = gep_type_begin(this), E = gep_type_end(this); + User::const_op_iterator OI = next(this->op_begin()); + + // Skip the first index, as it has no static limit. + ++GEPI; + ++OI; + + // The remaining indices must be compile-time known integers within the + // bounds of the corresponding notional static array types. + for (; GEPI != E; ++GEPI, ++OI) { + ConstantInt *CI = dyn_cast<ConstantInt>(*OI); + if (!CI) return false; + if (const ArrayType *ATy = dyn_cast<ArrayType>(*GEPI)) + if (CI->getValue().getActiveBits() > 64 || + CI->getZExtValue() >= ATy->getNumElements()) + return false; + } + + // All the indices checked out. + return true; +} + bool ConstantExpr::hasIndices() const { return getOpcode() == Instruction::ExtractValue || getOpcode() == Instruction::InsertValue; |