summaryrefslogtreecommitdiffstats
path: root/llvm/lib
diff options
context:
space:
mode:
authorChris Lattner <sabre@nondot.org>2004-07-15 01:16:59 +0000
committerChris Lattner <sabre@nondot.org>2004-07-15 01:16:59 +0000
commit4bbd40974912f1619c850ea3aa3a706f11764e8a (patch)
treef59ad42d5abc87e0df91eebfd0e1c6b5075cb1f2 /llvm/lib
parent030f84f2158eee832758ad8fe2c094282feb37aa (diff)
downloadbcm5719-llvm-4bbd40974912f1619c850ea3aa3a706f11764e8a.tar.gz
bcm5719-llvm-4bbd40974912f1619c850ea3aa3a706f11764e8a.zip
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
Diffstat (limited to 'llvm/lib')
-rw-r--r--llvm/lib/VMCore/ConstantFolding.cpp12
1 files changed, 12 insertions, 0 deletions
diff --git a/llvm/lib/VMCore/ConstantFolding.cpp b/llvm/lib/VMCore/ConstantFolding.cpp
index 5a8e41c74ea..e42de2a7e39 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<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))) {
OpenPOWER on IntegriCloud