[llvm-commits] CVS: llvm/lib/VMCore/ConstantFolding.cpp Constants.cpp
Chris Lattner
lattner at cs.uiuc.edu
Mon Feb 16 14:47:02 PST 2004
Changes in directory llvm/lib/VMCore:
ConstantFolding.cpp updated: 1.51 -> 1.52
Constants.cpp updated: 1.79 -> 1.80
---
Log message:
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
---
Diffs of the changes: (+20 -14)
Index: llvm/lib/VMCore/ConstantFolding.cpp
diff -u llvm/lib/VMCore/ConstantFolding.cpp:1.51 llvm/lib/VMCore/ConstantFolding.cpp:1.52
--- llvm/lib/VMCore/ConstantFolding.cpp:1.51 Sat Jan 31 19:23:19 2004
+++ llvm/lib/VMCore/ConstantFolding.cpp Mon Feb 16 14:46:13 2004
@@ -927,8 +927,21 @@
(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
Index: llvm/lib/VMCore/Constants.cpp
diff -u llvm/lib/VMCore/Constants.cpp:1.79 llvm/lib/VMCore/Constants.cpp:1.80
--- llvm/lib/VMCore/Constants.cpp:1.79 Sat Feb 14 23:53:04 2004
+++ llvm/lib/VMCore/Constants.cpp Mon Feb 16 14:46:13 2004
@@ -1081,11 +1081,15 @@
Constant *ConstantExpr::getGetElementPtrTy(const Type *ReqTy, Constant *C,
const std::vector<Constant*> &IdxList) {
+ assert(GetElementPtrInst::getIndexedType(C->getType(),
+ std::vector<Value*>(IdxList.begin(), IdxList.end()), true) &&
+ "GEP indices invalid!");
+
if (Constant *FC = ConstantFoldGetElementPtr(C, IdxList))
return FC; // Fold a few common cases...
+
assert(isa<PointerType>(C->getType()) &&
"Non-pointer type for constant GetElementPtr expression");
-
// Look up the constant in the table first to ensure uniqueness
std::vector<Constant*> argVec(1, C);
argVec.insert(argVec.end(), IdxList.begin(), IdxList.end());
@@ -1101,17 +1105,6 @@
const Type *Ty = GetElementPtrInst::getIndexedType(C->getType(), VIdxList,
true);
assert(Ty && "GEP indices invalid!");
-
- 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) return ConstantPointerNull::get(PointerType::get(Ty));
- }
-
return getGetElementPtrTy(PointerType::get(Ty), C, IdxList);
}
More information about the llvm-commits
mailing list