From 04b60fe5ad29fa2f3e1daed41cc9a5bc1eebc81c Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Mon, 16 Feb 2004 20:46:13 +0000 Subject: [PATCH] Move the folding of gep null, 0, 0, 0 to a place where it can be shared and enjoyed by all, fixing a fixme. Add an assert llvm-svn: 11505 --- llvm/lib/VMCore/ConstantFolding.cpp | 17 +++++++++++++++-- llvm/lib/VMCore/Constants.cpp | 17 +++++------------ 2 files changed, 20 insertions(+), 14 deletions(-) diff --git a/llvm/lib/VMCore/ConstantFolding.cpp b/llvm/lib/VMCore/ConstantFolding.cpp index 28d671b898c9..39fac8d66a4a 100644 --- a/llvm/lib/VMCore/ConstantFolding.cpp +++ b/llvm/lib/VMCore/ConstantFolding.cpp @@ -927,8 +927,21 @@ Constant *llvm::ConstantFoldGetElementPtr(const Constant *C, (IdxList.size() == 1 && IdxList[0]->isNullValue())) return const_cast(C); - // TODO If C is null and all idx's are null, return null of the right type. - + if (C->isNullValue()) { + bool isNull = true; + for (unsigned i = 0, e = IdxList.size(); i != e; ++i) + if (!IdxList[i]->isNullValue()) { + isNull = false; + break; + } + if (isNull) { + std::vector VIdxList(IdxList.begin(), IdxList.end()); + const Type *Ty = GetElementPtrInst::getIndexedType(C->getType(), VIdxList, + true); + assert(Ty != 0 && "Invalid indices for GEP!"); + return ConstantPointerNull::get(PointerType::get(Ty)); + } + } if (ConstantExpr *CE = dyn_cast(const_cast(C))) { // Combine Indices - If the source pointer to this getelementptr instruction diff --git a/llvm/lib/VMCore/Constants.cpp b/llvm/lib/VMCore/Constants.cpp index dac23aea55c0..5a617fad9ae5 100644 --- a/llvm/lib/VMCore/Constants.cpp +++ b/llvm/lib/VMCore/Constants.cpp @@ -1081,11 +1081,15 @@ Constant *ConstantExpr::getShiftTy(const Type *ReqTy, unsigned Opcode, Constant *ConstantExpr::getGetElementPtrTy(const Type *ReqTy, Constant *C, const std::vector &IdxList) { + assert(GetElementPtrInst::getIndexedType(C->getType(), + std::vector(IdxList.begin(), IdxList.end()), true) && + "GEP indices invalid!"); + if (Constant *FC = ConstantFoldGetElementPtr(C, IdxList)) return FC; // Fold a few common cases... + assert(isa(C->getType()) && "Non-pointer type for constant GetElementPtr expression"); - // Look up the constant in the table first to ensure uniqueness std::vector argVec(1, C); argVec.insert(argVec.end(), IdxList.begin(), IdxList.end()); @@ -1101,17 +1105,6 @@ Constant *ConstantExpr::getGetElementPtr(Constant *C, const Type *Ty = GetElementPtrInst::getIndexedType(C->getType(), VIdxList, true); assert(Ty && "GEP indices invalid!"); - - if (C->isNullValue()) { - bool isNull = true; - for (unsigned i = 0, e = IdxList.size(); i != e; ++i) - if (!IdxList[i]->isNullValue()) { - isNull = false; - break; - } - if (isNull) return ConstantPointerNull::get(PointerType::get(Ty)); - } - return getGetElementPtrTy(PointerType::get(Ty), C, IdxList); }