for (User::op_iterator i = GEP->op_begin() + 1, e = GEP->op_end(); i != e;
++i, ++GTI) {
Value *Op = *i;
- uint64_t Size = TD.getABITypeSize(GTI.getIndexedType()) & PtrSizeMask;
+ uint64_t Size = TD.getTypePaddedSize(GTI.getIndexedType()) & PtrSizeMask;
if (ConstantInt *OpC = dyn_cast<ConstantInt>(Op)) {
if (OpC->isZero()) continue;
if (const StructType *STy = dyn_cast<StructType>(*GTI)) {
Offset += TD.getStructLayout(STy)->getElementOffset(CI->getZExtValue());
} else {
- uint64_t Size = TD.getABITypeSize(GTI.getIndexedType());
+ uint64_t Size = TD.getTypePaddedSize(GTI.getIndexedType());
Offset += Size*CI->getSExtValue();
}
} else {
Value *VariableIdx = GEP->getOperand(i);
// Determine the scale factor of the variable element. For example, this is
// 4 if the variable index is into an array of i32.
- uint64_t VariableScale = TD.getABITypeSize(GTI.getIndexedType());
+ uint64_t VariableScale = TD.getTypePaddedSize(GTI.getIndexedType());
// Verify that there are no other variable indices. If so, emit the hard way.
for (++i, ++GTI; i != e; ++i, ++GTI) {
if (const StructType *STy = dyn_cast<StructType>(*GTI)) {
Offset += TD.getStructLayout(STy)->getElementOffset(CI->getZExtValue());
} else {
- uint64_t Size = TD.getABITypeSize(GTI.getIndexedType());
+ uint64_t Size = TD.getTypePaddedSize(GTI.getIndexedType());
Offset += Size*CI->getSExtValue();
}
}
const APInt &RHSV = RHS->getValue();
switch (LHSI->getOpcode()) {
+ case Instruction::Trunc:
+ if (ICI.isEquality() && LHSI->hasOneUse()) {
+ // Simplify icmp eq (trunc x to i8), 42 -> icmp eq x, 42|highbits if all
+ // of the high bits truncated out of x are known.
+ unsigned DstBits = LHSI->getType()->getPrimitiveSizeInBits(),
+ SrcBits = LHSI->getOperand(0)->getType()->getPrimitiveSizeInBits();
+ APInt Mask(APInt::getHighBitsSet(SrcBits, SrcBits-DstBits));
+ APInt KnownZero(SrcBits, 0), KnownOne(SrcBits, 0);
+ ComputeMaskedBits(LHSI->getOperand(0), Mask, KnownZero, KnownOne);
+
+ // If all the high bits are known, we can do this xform.
+ if ((KnownZero|KnownOne).countLeadingOnes() >= SrcBits-DstBits) {
+ // Pull in the high bits from known-ones set.
+ APInt NewRHS(RHS->getValue());
+ NewRHS.zext(SrcBits);
+ NewRHS |= KnownOne;
+ return new ICmpInst(ICI.getPredicate(), LHSI->getOperand(0),
+ ConstantInt::get(NewRHS));
+ }
+ }
+ break;
+
case Instruction::Xor: // (icmp pred (xor X, XorCST), CI)
if (ConstantInt *XorCST = dyn_cast<ConstantInt>(LHSI->getOperand(1))) {
// If this is a comparison that tests the signbit (X < 0) or (x > -1),
return &ICI;
}
}
- } else { // Not a ICMP_EQ/ICMP_NE
- // If the LHS is a cast from an integral value of the same size,
- // then since we know the RHS is a constant, try to simlify.
- if (CastInst *Cast = dyn_cast<CastInst>(LHSI)) {
- Value *CastOp = Cast->getOperand(0);
- const Type *SrcTy = CastOp->getType();
- uint32_t SrcTySize = SrcTy->getPrimitiveSizeInBits();
- if (SrcTy->isInteger() &&
- SrcTySize == Cast->getType()->getPrimitiveSizeInBits()) {
- // If this is an unsigned comparison, try to make the comparison use
- // smaller constant values.
- if (ICI.getPredicate() == ICmpInst::ICMP_ULT && RHSV.isSignBit()) {
- // X u< 128 => X s> -1
- return new ICmpInst(ICmpInst::ICMP_SGT, CastOp,
- ConstantInt::get(APInt::getAllOnesValue(SrcTySize)));
- } else if (ICI.getPredicate() == ICmpInst::ICMP_UGT &&
- RHSV == APInt::getSignedMaxValue(SrcTySize)) {
- // X u> 127 => X s< 0
- return new ICmpInst(ICmpInst::ICMP_SLT, CastOp,
- Constant::getNullValue(SrcTy));
- }
- }
- }
}
return 0;
}
// same, we open the door to infinite loops of various kinds.
if (!AI.hasOneUse() && CastElTyAlign == AllocElTyAlign) return 0;
- uint64_t AllocElTySize = TD->getABITypeSize(AllocElTy);
- uint64_t CastElTySize = TD->getABITypeSize(CastElTy);
+ uint64_t AllocElTySize = TD->getTypePaddedSize(AllocElTy);
+ uint64_t CastElTySize = TD->getTypePaddedSize(CastElTy);
if (CastElTySize == 0 || AllocElTySize == 0) return 0;
// See if we can satisfy the modulus by pulling a scale out of the array
// is something like [0 x {int, int}]
const Type *IntPtrTy = TD->getIntPtrType();
int64_t FirstIdx = 0;
- if (int64_t TySize = TD->getABITypeSize(Ty)) {
+ if (int64_t TySize = TD->getTypePaddedSize(Ty)) {
FirstIdx = Offset/TySize;
- Offset %= TySize;
+ Offset -= FirstIdx*TySize;
- // Handle silly modulus not returning values values [0..TySize).
+ // Handle hosts where % returns negative instead of values [0..TySize).
if (Offset < 0) {
--FirstIdx;
Offset += TySize;
// Index into the types. If we fail, set OrigBase to null.
while (Offset) {
+ // Indexing into tail padding between struct/array elements.
+ if (uint64_t(Offset*8) >= TD->getTypeSizeInBits(Ty))
+ return false;
+
if (const StructType *STy = dyn_cast<StructType>(Ty)) {
const StructLayout *SL = TD->getStructLayout(STy);
- if (Offset >= (int64_t)SL->getSizeInBytes()) {
- // We can't index into this, bail out.
- return false;
- }
+ assert(Offset < (int64_t)SL->getSizeInBytes() &&
+ "Offset must stay within the indexed type");
+
unsigned Elt = SL->getElementContainingOffset(Offset);
NewIndices.push_back(ConstantInt::get(Type::Int32Ty, Elt));
Offset -= SL->getElementOffset(Elt);
Ty = STy->getElementType(Elt);
- } else if (isa<ArrayType>(Ty) || isa<VectorType>(Ty)) {
- const SequentialType *STy = cast<SequentialType>(Ty);
- if (uint64_t EltSize = TD->getABITypeSize(STy->getElementType())) {
- NewIndices.push_back(ConstantInt::get(IntPtrTy,Offset/EltSize));
- Offset %= EltSize;
- } else {
- NewIndices.push_back(ConstantInt::get(IntPtrTy, 0));
- }
- Ty = STy->getElementType();
+ } else if (const ArrayType *AT = dyn_cast<ArrayType>(Ty)) {
+ uint64_t EltSize = TD->getTypePaddedSize(AT->getElementType());
+ assert(EltSize && "Cannot index into a zero-sized array");
+ NewIndices.push_back(ConstantInt::get(IntPtrTy,Offset/EltSize));
+ Offset %= EltSize;
+ Ty = AT->getElementType();
} else {
- // Otherwise, we can't index into this, bail out.
+ // Otherwise, we can't index into the middle of this atomic type, bail.
return false;
}
}
// is a single-index GEP.
if (X->getType() == CI.getType()) {
// Get the size of the pointee type.
- uint64_t Size = TD->getABITypeSize(DestPointee);
+ uint64_t Size = TD->getTypePaddedSize(DestPointee);
// Convert the constant to intptr type.
APInt Offset = Cst->getValue();
// "inttoptr+GEP" instead of "add+intptr".
// Get the size of the pointee type.
- uint64_t Size = TD->getABITypeSize(DestPointee);
+ uint64_t Size = TD->getTypePaddedSize(DestPointee);
// Convert the constant to intptr type.
APInt Offset = Cst->getValue();
const Type* DstTy = cast<PointerType>(CI->getType())->getElementType();
if (!SrcTy->isSized() || !DstTy->isSized())
return false;
- if (TD->getABITypeSize(SrcTy) != TD->getABITypeSize(DstTy))
+ if (TD->getTypePaddedSize(SrcTy) != TD->getTypePaddedSize(DstTy))
return false;
return true;
}
const Type *SrcElTy = cast<PointerType>(X->getType())->getElementType();
const Type *ResElTy=cast<PointerType>(PtrOp->getType())->getElementType();
if (isa<ArrayType>(SrcElTy) &&
- TD->getABITypeSize(cast<ArrayType>(SrcElTy)->getElementType()) ==
- TD->getABITypeSize(ResElTy)) {
+ TD->getTypePaddedSize(cast<ArrayType>(SrcElTy)->getElementType()) ==
+ TD->getTypePaddedSize(ResElTy)) {
Value *Idx[2];
Idx[0] = Constant::getNullValue(Type::Int32Ty);
Idx[1] = GEP.getOperand(1);
if (isa<ArrayType>(SrcElTy) && ResElTy == Type::Int8Ty) {
uint64_t ArrayEltSize =
- TD->getABITypeSize(cast<ArrayType>(SrcElTy)->getElementType());
+ TD->getTypePaddedSize(cast<ArrayType>(SrcElTy)->getElementType());
// Check to see if "tmp" is a scale by a multiple of ArrayEltSize. We
// allow either a mul, shift, or constant here.
// Note that we only do this for alloca's, because malloc should allocate and
// return a unique pointer, even for a zero byte allocation.
if (isa<AllocaInst>(AI) && AI.getAllocatedType()->isSized() &&
- TD->getABITypeSize(AI.getAllocatedType()) == 0)
+ TD->getTypePaddedSize(AI.getAllocatedType()) == 0)
return ReplaceInstUsesWith(AI, Constant::getNullValue(AI.getType()));
return 0;