From 4bbd40974912f1619c850ea3aa3a706f11764e8a Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Thu, 15 Jul 2004 01:16:59 +0000 Subject: [PATCH] Implement folding of expressions like 'uint cast (int* getelementptr (int* null, uint 1) to uint)' to a constant integer. We can only do this with primitive LLVM types, because other types have target-specific sizes. llvm-svn: 14837 --- llvm/lib/VMCore/ConstantFolding.cpp | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/llvm/lib/VMCore/ConstantFolding.cpp b/llvm/lib/VMCore/ConstantFolding.cpp index 5a8e41c74ea3..e42de2a7e395 100644 --- a/llvm/lib/VMCore/ConstantFolding.cpp +++ b/llvm/lib/VMCore/ConstantFolding.cpp @@ -971,6 +971,18 @@ Constant *llvm::ConstantFoldGetElementPtr(const Constant *C, assert(Ty != 0 && "Invalid indices for GEP!"); return ConstantPointerNull::get(PointerType::get(Ty)); } + + if (IdxList.size() == 1) { + const Type *ElTy = cast(C->getType())->getElementType(); + if (unsigned ElSize = ElTy->getPrimitiveSize()) { + // gep null, C is equal to C*sizeof(nullty). If nullty is a known llvm + // type, we can statically fold this. + Constant *R = ConstantUInt::get(Type::UIntTy, ElSize); + R = ConstantExpr::getCast(R, IdxList[0]->getType()); + R = ConstantExpr::getMul(R, IdxList[0]); + return ConstantExpr::getCast(R, C->getType()); + } + } } if (ConstantExpr *CE = dyn_cast(const_cast(C))) {