Add a helper function, allowing us to simplify some code a bit, changing

indentation, no functionality change

llvm-svn: 23325
This commit is contained in:
Chris Lattner 2005-09-13 00:40:14 +00:00
parent 219175c84d
commit 567b81f0d2
1 changed files with 47 additions and 39 deletions

View File

@ -261,6 +261,17 @@ static const Type *getPromotedType(const Type *Ty) {
} }
} }
/// isCast - If the specified operand is a CastInst or a constant expr cast,
/// return the operand value, otherwise return null.
static Value *isCast(Value *V) {
if (CastInst *I = dyn_cast<CastInst>(V))
return I->getOperand(0);
else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
if (CE->getOpcode() == Instruction::Cast)
return CE->getOperand(0);
return 0;
}
// SimplifyCommutative - This performs a few simplifications for commutative // SimplifyCommutative - This performs a few simplifications for commutative
// operators: // operators:
// //
@ -4682,45 +4693,42 @@ Instruction *InstCombiner::visitGetElementPtrInst(GetElementPtrInst &GEP) {
// Replace all uses of the GEP with the new constexpr... // Replace all uses of the GEP with the new constexpr...
return ReplaceInstUsesWith(GEP, CE); return ReplaceInstUsesWith(GEP, CE);
} }
} else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(PtrOp)) { } else if (Value *X = isCast(PtrOp)) { // Is the operand a cast?
if (CE->getOpcode() == Instruction::Cast) { if (!isa<PointerType>(X->getType())) {
if (HasZeroPointerIndex) { // Not interesting. Source pointer must be a cast from pointer.
// transform: GEP (cast [10 x ubyte]* X to [0 x ubyte]*), long 0, ... } else if (HasZeroPointerIndex) {
// into : GEP [10 x ubyte]* X, long 0, ... // transform: GEP (cast [10 x ubyte]* X to [0 x ubyte]*), long 0, ...
// // into : GEP [10 x ubyte]* X, long 0, ...
// This occurs when the program declares an array extern like "int X[];" //
// // This occurs when the program declares an array extern like "int X[];"
Constant *X = CE->getOperand(0); //
const PointerType *CPTy = cast<PointerType>(CE->getType()); const PointerType *CPTy = cast<PointerType>(PtrOp->getType());
if (const PointerType *XTy = dyn_cast<PointerType>(X->getType())) const PointerType *XTy = cast<PointerType>(X->getType());
if (const ArrayType *XATy = if (const ArrayType *XATy =
dyn_cast<ArrayType>(XTy->getElementType())) dyn_cast<ArrayType>(XTy->getElementType()))
if (const ArrayType *CATy = if (const ArrayType *CATy =
dyn_cast<ArrayType>(CPTy->getElementType())) dyn_cast<ArrayType>(CPTy->getElementType()))
if (CATy->getElementType() == XATy->getElementType()) { if (CATy->getElementType() == XATy->getElementType()) {
// At this point, we know that the cast source type is a pointer // At this point, we know that the cast source type is a pointer
// to an array of the same type as the destination pointer // to an array of the same type as the destination pointer
// array. Because the array type is never stepped over (there // array. Because the array type is never stepped over (there
// is a leading zero) we can fold the cast into this GEP. // is a leading zero) we can fold the cast into this GEP.
GEP.setOperand(0, X); GEP.setOperand(0, X);
return &GEP; return &GEP;
} }
} else if (GEP.getNumOperands() == 2 && } else if (GEP.getNumOperands() == 2) {
isa<PointerType>(CE->getOperand(0)->getType())) { // Transform things like:
// Transform things like: // %t = getelementptr ubyte* cast ([2 x sbyte]* %str to ubyte*), uint %V
// %t = getelementptr ubyte* cast ([2 x sbyte]* %str to ubyte*), uint %V // into: %t1 = getelementptr [2 x sbyte*]* %str, int 0, uint %V; cast
// into: %t1 = getelementptr [2 x sbyte*]* %str, int 0, uint %V; cast const Type *SrcElTy = cast<PointerType>(X->getType())->getElementType();
Constant *X = CE->getOperand(0); const Type *ResElTy=cast<PointerType>(PtrOp->getType())->getElementType();
const Type *SrcElTy = cast<PointerType>(X->getType())->getElementType(); if (isa<ArrayType>(SrcElTy) &&
const Type *ResElTy =cast<PointerType>(CE->getType())->getElementType(); TD->getTypeSize(cast<ArrayType>(SrcElTy)->getElementType()) ==
if (isa<ArrayType>(SrcElTy) && TD->getTypeSize(ResElTy)) {
TD->getTypeSize(cast<ArrayType>(SrcElTy)->getElementType()) == Value *V = InsertNewInstBefore(
TD->getTypeSize(ResElTy)) { new GetElementPtrInst(X, Constant::getNullValue(Type::IntTy),
Value *V = InsertNewInstBefore( GEP.getOperand(1), GEP.getName()), GEP);
new GetElementPtrInst(X, Constant::getNullValue(Type::IntTy), return new CastInst(V, GEP.getType());
GEP.getOperand(1), GEP.getName()), GEP);
return new CastInst(V, GEP.getType());
}
} }
} }
} }