aboutsummaryrefslogtreecommitdiff
path: root/lib/VMCore/ConstantFold.cpp
diff options
context:
space:
mode:
authorChris Lattner <sabre@nondot.org>2004-02-16 20:46:13 +0000
committerChris Lattner <sabre@nondot.org>2004-02-16 20:46:13 +0000
commit2e9bb1a88e806644e96b2a73bf8c2eb540d68ede (patch)
treeadf937b68b3d5d45e16b696b976cad5d5e86d5ed /lib/VMCore/ConstantFold.cpp
parent2ca265299cb74c7dbc8b507910e7d2033fd44f87 (diff)
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 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@11505 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/VMCore/ConstantFold.cpp')
-rw-r--r--lib/VMCore/ConstantFold.cpp17
1 files changed, 15 insertions, 2 deletions
diff --git a/lib/VMCore/ConstantFold.cpp b/lib/VMCore/ConstantFold.cpp
index 28d671b898..39fac8d66a 100644
--- a/lib/VMCore/ConstantFold.cpp
+++ b/lib/VMCore/ConstantFold.cpp
@@ -927,8 +927,21 @@ Constant *llvm::ConstantFoldGetElementPtr(const Constant *C,
(IdxList.size() == 1 && IdxList[0]->isNullValue()))
return const_cast<Constant*>(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<Value*> 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<ConstantExpr>(const_cast<Constant*>(C))) {
// Combine Indices - If the source pointer to this getelementptr instruction