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
This commit is contained in:
Chris Lattner 2004-07-15 01:16:59 +00:00
parent 030f84f215
commit 4bbd409749
1 changed files with 12 additions and 0 deletions

View File

@ -971,6 +971,18 @@ Constant *llvm::ConstantFoldGetElementPtr(const Constant *C,
assert(Ty != 0 && "Invalid indices for GEP!"); assert(Ty != 0 && "Invalid indices for GEP!");
return ConstantPointerNull::get(PointerType::get(Ty)); 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))) { if (ConstantExpr *CE = dyn_cast<ConstantExpr>(const_cast<Constant*>(C))) {