From dd68469382cafff2051289707ea2d3e0d26919b1 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. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@14837 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/VMCore/ConstantFold.cpp | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/lib/VMCore/ConstantFold.cpp b/lib/VMCore/ConstantFold.cpp index 5a8e41c74ea..e42de2a7e39 100644 --- a/lib/VMCore/ConstantFold.cpp +++ b/lib/VMCore/ConstantFold.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))) { -- 2.34.1