From b86d48c717ec2250527d1e472dfd92d42af1f822 Mon Sep 17 00:00:00 2001 From: Matthijs Kooijman Date: Mon, 13 Oct 2008 15:17:01 +0000 Subject: [PATCH] Make InstructionCombining::getBitCastOperand() recognize GEP instructions and constant expression with all zero indices as being the same as a bitcast. llvm-svn: 57442 --- .../Scalar/InstructionCombining.cpp | 26 ++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/lib/Transforms/Scalar/InstructionCombining.cpp b/lib/Transforms/Scalar/InstructionCombining.cpp index 162e1bf3a93..b85f77dab15 100644 --- a/lib/Transforms/Scalar/InstructionCombining.cpp +++ b/lib/Transforms/Scalar/InstructionCombining.cpp @@ -436,14 +436,34 @@ static const Type *getPromotedType(const Type *Ty) { return Ty; } -/// getBitCastOperand - If the specified operand is a CastInst or a constant -/// expression bitcast, return the operand value, otherwise return null. +/// getBitCastOperand - If the specified operand is a CastInst, a constant +/// expression bitcast, or a GetElementPtrInst with all zero indices, return the +/// operand value, otherwise return null. static Value *getBitCastOperand(Value *V) { if (BitCastInst *I = dyn_cast(V)) + // BitCastInst? return I->getOperand(0); - else if (ConstantExpr *CE = dyn_cast(V)) + else if (GetElementPtrInst *GEP = dyn_cast(V)) { + // GetElementPtrInst? + if (GEP->hasAllZeroIndices()) + return GEP->getOperand(0); + } else if (ConstantExpr *CE = dyn_cast(V)) { if (CE->getOpcode() == Instruction::BitCast) + // BitCast ConstantExp? return CE->getOperand(0); + else if (CE->getOpcode() == Instruction::GetElementPtr) { + // GetElementPtr ConstantExp? + for (User::op_iterator I = CE->op_begin() + 1, E = CE->op_end(); + I != E; ++I) { + ConstantInt *CI = dyn_cast(I); + if (!CI || !CI->isZero()) + // Any non-zero indices? Not cast-like. + return 0; + } + // All-zero indices? This is just like casting. + return CE->getOperand(0); + } + } return 0; }