[llvm-commits] CVS: llvm/lib/VMCore/ConstantFolding.cpp
Chris Lattner
lattner at cs.uiuc.edu
Wed Jul 14 18:17:09 PDT 2004
Changes in directory llvm/lib/VMCore:
ConstantFolding.cpp updated: 1.60 -> 1.61
---
Log message:
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.
---
Diffs of the changes: (+12 -0)
Index: llvm/lib/VMCore/ConstantFolding.cpp
diff -u llvm/lib/VMCore/ConstantFolding.cpp:1.60 llvm/lib/VMCore/ConstantFolding.cpp:1.61
--- llvm/lib/VMCore/ConstantFolding.cpp:1.60 Tue Jul 6 23:45:13 2004
+++ llvm/lib/VMCore/ConstantFolding.cpp Wed Jul 14 20:16:59 2004
@@ -971,6 +971,18 @@
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))) {
More information about the llvm-commits
mailing list