- // 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<PointerType>(GEPI->getOperand(0)->getType());
- const ArrayType *AT = cast<ArrayType>(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<ConstantSInt>(GEPI->getOperand(2))->getValue() >= NumElements ||
- cast<ConstantSInt>(GEPI->getOperand(2))->getValue() < 0)
- return false;
- }
+ GetElementPtrInst *GEPI = cast<GetElementPtrInst>(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 <ptr>, 0, <cst>
+ if (I == E ||
+ I.getOperand() != Constant::getNullValue(I.getOperand()->getType()))
+ return false;
+
+ ++I;
+ if (I != E || !isa<ConstantInt>(I.getOperand()))
+ return false;