From 2c4161fc57482afeb26ae0300fb86e8e1044ffcf Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Sat, 27 Nov 2004 17:55:46 +0000 Subject: [PATCH] Implement Regression/Transforms/InstCombine/getelementptr_cast.ll, which occurs many times in crafty llvm-svn: 18273 --- lib/Transforms/Scalar/InstructionCombining.cpp | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/lib/Transforms/Scalar/InstructionCombining.cpp b/lib/Transforms/Scalar/InstructionCombining.cpp index b279d9201ae..4acbc140900 100644 --- a/lib/Transforms/Scalar/InstructionCombining.cpp +++ b/lib/Transforms/Scalar/InstructionCombining.cpp @@ -3783,6 +3783,21 @@ Instruction *InstCombiner::visitGetElementPtrInst(GetElementPtrInst &GEP) { GEP.setOperand(0, X); return &GEP; } + } else if (GEP.getNumOperands() == 2) { + // Transform things like: + // %t = getelementptr ubyte* cast ([2 x sbyte]* %str to ubyte*), uint %V + // into: %t1 = getelementptr [2 x sbyte*]* %str, int 0, uint %V; cast + Constant *X = CE->getOperand(0); + const Type *SrcElTy = cast(X->getType())->getElementType(); + const Type *ResElTy =cast(CE->getType())->getElementType(); + if (isa(SrcElTy) && + TD->getTypeSize(cast(SrcElTy)->getElementType()) == + TD->getTypeSize(ResElTy)) { + Value *V = InsertNewInstBefore( + new GetElementPtrInst(X, Constant::getNullValue(Type::IntTy), + GEP.getOperand(1), GEP.getName()), GEP); + return new CastInst(V, GEP.getType()); + } } } }