From b2b7f90cafe73f29e933fc21d7fe96919d6084e7 Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Mon, 11 Oct 2004 03:57:30 +0000 Subject: [PATCH] If we are trying to create a ConstantExpr cast that is really a GEP to the first element of an array, return a GEP instead of a cast. This allows us to transparently fold this: int* getelementptr (int* cast ([100 x int]* %Gbody to int*), int 40) into this: int* getelementptr ([100 x int]* %Gbody, int 0, int 40) llvm-svn: 16911 --- llvm/lib/VMCore/ConstantFolding.cpp | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/llvm/lib/VMCore/ConstantFolding.cpp b/llvm/lib/VMCore/ConstantFolding.cpp index 2a96291fe49d..703a324126a9 100644 --- a/llvm/lib/VMCore/ConstantFolding.cpp +++ b/llvm/lib/VMCore/ConstantFolding.cpp @@ -562,6 +562,17 @@ Constant *llvm::ConstantFoldCastInstruction(const Constant *V, return ConstantExpr::getCast(CE->getOperand(0), DestTy); } + // Check to see if we are casting an array of X to a pointer to X. If so, use + // a GEP to get to the first element of the array instead of a cast! + if (const PointerType *PTy = dyn_cast(V->getType())) + if (const ArrayType *ATy = dyn_cast(PTy->getElementType())) + if (const PointerType *DPTy = dyn_cast(DestTy)) + if (DPTy->getElementType() == ATy->getElementType()) { + std::vector IdxList(2,Constant::getNullValue(Type::IntTy)); + return ConstantExpr::getGetElementPtr(const_cast(V), + IdxList); + } + ConstRules &Rules = ConstRules::get(V, V); switch (DestTy->getTypeID()) {