From: Chris Lattner Date: Tue, 25 Nov 2003 21:09:18 +0000 (+0000) Subject: Do not use index type to determine what it is indexing into! X-Git-Url: http://plrg.eecs.uci.edu/git/?a=commitdiff_plain;h=be883a23ed64b83235d509ad0befc1d6aa6b0cd8;p=oota-llvm.git Do not use index type to determine what it is indexing into! git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@10226 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Transforms/Scalar/ScalarReplAggregates.cpp b/lib/Transforms/Scalar/ScalarReplAggregates.cpp index d29119061dc..e374ccc7a2a 100644 --- a/lib/Transforms/Scalar/ScalarReplAggregates.cpp +++ b/lib/Transforms/Scalar/ScalarReplAggregates.cpp @@ -26,6 +26,7 @@ #include "llvm/Pass.h" #include "llvm/iMemory.h" #include "llvm/Analysis/Dominators.h" +#include "llvm/Support/GetElementPtrTypeIterator.h" #include "llvm/Target/TargetData.h" #include "llvm/Transforms/Utils/PromoteMemToReg.h" #include "Support/Debug.h" @@ -222,35 +223,34 @@ bool SROA::performScalarRepl(Function &F) { /// aggregate allocation. /// bool SROA::isSafeUseOfAllocation(Instruction *User) { - if (GetElementPtrInst *GEPI = dyn_cast(User)) { - // The GEP is safe to transform if it is of the form GEP , 0, - if (GEPI->getNumOperands() <= 2 || - GEPI->getOperand(1) != Constant::getNullValue(Type::LongTy) || - !isa(GEPI->getOperand(2)) || - isa(GEPI->getOperand(2))) - return false; + if (!isa(User)) return false; - // If this is a use of an array allocation, do a bit more checking for - // sanity. - if (GEPI->getOperand(2)->getType() == Type::LongTy) { - const PointerType *PTy =cast(GEPI->getOperand(0)->getType()); - const ArrayType *AT = cast(PTy->getElementType()); - int64_t NumElements = AT->getNumElements(); - - // Check to make sure that index falls within the array. If not, - // something funny is going on, so we won't do the optimization. - // - if (cast(GEPI->getOperand(2))->getValue() >= NumElements || - cast(GEPI->getOperand(2))->getValue() < 0) - return false; - } + GetElementPtrInst *GEPI = cast(User); + gep_type_iterator I = gep_type_begin(GEPI), E = gep_type_end(GEPI); + + // The GEP is safe to transform if it is of the form GEP , 0, + if (I == E || + I.getOperand() != Constant::getNullValue(I.getOperand()->getType())) + return false; + + ++I; + if (I != E || !isa(I.getOperand())) + return false; - // If there are any non-simple uses of this getelementptr, make sure to - // reject them. - if (isSafeElementUse(GEPI)) - return true; + // If this is a use of an array allocation, do a bit more checking for sanity. + if (const ArrayType *AT = dyn_cast(*I)) { + uint64_t NumElements = AT->getNumElements(); + + // Check to make sure that index falls within the array. If not, + // something funny is going on, so we won't do the optimization. + // + if (cast(GEPI->getOperand(2))->getRawValue() >= NumElements) + return false; } - return false; + + // If there are any non-simple uses of this getelementptr, make sure to reject + // them. + return isSafeElementUse(GEPI); } /// isSafeElementUse - Check to see if this use is an allowed use for a