From 952454dbffed89dacc85b9d6253bc8203a415f14 Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Sun, 11 Jan 2004 23:56:33 +0000 Subject: [PATCH] Implement: Assembler/2004-01-11-getelementptrfolding.llx git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@10759 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/VMCore/ConstantFold.cpp | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/lib/VMCore/ConstantFold.cpp b/lib/VMCore/ConstantFold.cpp index 04ec28bfb83..c21529a010e 100644 --- a/lib/VMCore/ConstantFold.cpp +++ b/lib/VMCore/ConstantFold.cpp @@ -171,7 +171,7 @@ Constant *llvm::ConstantFoldGetElementPtr(const Constant *C, I != E; ++I) LastTy = *I; - if (LastTy && isa(LastTy)) { + if ((LastTy && isa(LastTy)) || IdxList[0]->isNullValue()) { std::vector NewIndices; NewIndices.reserve(IdxList.size() + CE->getNumOperands()); for (unsigned i = 1, e = CE->getNumOperands()-1; i != e; ++i) @@ -179,11 +179,13 @@ Constant *llvm::ConstantFoldGetElementPtr(const Constant *C, // Add the last index of the source with the first index of the new GEP. // Make sure to handle the case when they are actually different types. - Constant *Combined = - ConstantExpr::get(Instruction::Add, - ConstantExpr::getCast(IdxList[0], Type::LongTy), - ConstantExpr::getCast(CE->getOperand(CE->getNumOperands()-1), Type::LongTy)); - + Constant *Combined = CE->getOperand(CE->getNumOperands()-1); + if (!IdxList[0]->isNullValue()) // Otherwise it must be an array + Combined = + ConstantExpr::get(Instruction::Add, + ConstantExpr::getCast(IdxList[0], Type::LongTy), + ConstantExpr::getCast(Combined, Type::LongTy)); + NewIndices.push_back(Combined); NewIndices.insert(NewIndices.end(), IdxList.begin()+1, IdxList.end()); return ConstantExpr::getGetElementPtr(CE->getOperand(0), NewIndices);