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,17 +4693,17 @@ 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.
} else if (HasZeroPointerIndex) {
// transform: GEP (cast [10 x ubyte]* X to [0 x ubyte]*), long 0, ... // transform: GEP (cast [10 x ubyte]* X to [0 x ubyte]*), long 0, ...
// into : GEP [10 x ubyte]* X, 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>(PtrOp->getType());
const PointerType *CPTy = cast<PointerType>(CE->getType()); const PointerType *XTy = cast<PointerType>(X->getType());
if (const PointerType *XTy = dyn_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 =
@ -4705,14 +4716,12 @@ Instruction *InstCombiner::visitGetElementPtrInst(GetElementPtrInst &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
Constant *X = CE->getOperand(0);
const Type *SrcElTy = cast<PointerType>(X->getType())->getElementType(); const Type *SrcElTy = cast<PointerType>(X->getType())->getElementType();
const Type *ResElTy =cast<PointerType>(CE->getType())->getElementType(); const Type *ResElTy=cast<PointerType>(PtrOp->getType())->getElementType();
if (isa<ArrayType>(SrcElTy) && if (isa<ArrayType>(SrcElTy) &&
TD->getTypeSize(cast<ArrayType>(SrcElTy)->getElementType()) == TD->getTypeSize(cast<ArrayType>(SrcElTy)->getElementType()) ==
TD->getTypeSize(ResElTy)) { TD->getTypeSize(ResElTy)) {
@ -4723,7 +4732,6 @@ Instruction *InstCombiner::visitGetElementPtrInst(GetElementPtrInst &GEP) {
} }
} }
} }
}
return 0; return 0;
} }