isa<ConstantPointerRef>(V1) || isa<ConstantPointerRef>(V2))
return EmptyR;
- switch (V1->getType()->getPrimitiveID()) {
+ switch (V1->getType()->getTypeID()) {
default: assert(0 && "Unknown value type for constant folding!");
case Type::BoolTyID: return BoolR;
case Type::PointerTyID: return NullPointerR;
ConstRules &Rules = ConstRules::get(V, V);
- switch (DestTy->getPrimitiveID()) {
+ switch (DestTy->getTypeID()) {
case Type::BoolTyID: return Rules.castToBool(V);
case Type::UByteTyID: return Rules.castToUByte(V);
case Type::SByteTyID: return Rules.castToSByte(V);
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))) {
// Add the last index of the source with the first index of the new GEP.
// Make sure to handle the case when they are actually different types.
Constant *Combined = CE->getOperand(CE->getNumOperands()-1);
- if (!IdxList[0]->isNullValue()) // Otherwise it must be an array
+ if (!IdxList[0]->isNullValue()) { // Otherwise it must be an array
+ const Type *IdxTy = Combined->getType();
+ if (IdxTy != IdxList[0]->getType()) IdxTy = Type::LongTy;
Combined =
ConstantExpr::get(Instruction::Add,
- ConstantExpr::getCast(IdxList[0], Type::LongTy),
- ConstantExpr::getCast(Combined, Type::LongTy));
+ ConstantExpr::getCast(IdxList[0], IdxTy),
+ ConstantExpr::getCast(Combined, IdxTy));
+ }
NewIndices.push_back(Combined);
NewIndices.insert(NewIndices.end(), IdxList.begin()+1, IdxList.end());