diff options
author | Chris Lattner <sabre@nondot.org> | 2004-07-15 01:16:59 +0000 |
---|---|---|
committer | Chris Lattner <sabre@nondot.org> | 2004-07-15 01:16:59 +0000 |
commit | dd68469382cafff2051289707ea2d3e0d26919b1 (patch) | |
tree | 9dcfaf2a3e53f4ce9724105c50970c5371dabc2d /lib | |
parent | 654b7105272964b53c9adacf568f2124da93eb9f (diff) | |
download | external_llvm-dd68469382cafff2051289707ea2d3e0d26919b1.zip external_llvm-dd68469382cafff2051289707ea2d3e0d26919b1.tar.gz external_llvm-dd68469382cafff2051289707ea2d3e0d26919b1.tar.bz2 |
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
Diffstat (limited to 'lib')
-rw-r--r-- | lib/VMCore/ConstantFold.cpp | 12 |
1 files changed, 12 insertions, 0 deletions
diff --git a/lib/VMCore/ConstantFold.cpp b/lib/VMCore/ConstantFold.cpp index 5a8e41c..e42de2a 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<PointerType>(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<ConstantExpr>(const_cast<Constant*>(C))) { |